test: correctly set the 'ret' variable

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
domain: add kdbus_domain_user_assign_id() to assign IDs to domain users

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
domain: add __kdbus_domain_user_account() to account domain users

Add __kdbus_domain_user_account() to account and link users into a
domain.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
domain: add kdbus_domain_user_account()

Add kdbus_domain_user_account() to account and link users into a
domain.

This function will take the domain lock, and it will be used as a
replacement for kdbus_domain_user_find_or_new().

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
kdbus: improve user quota accounting by using kdbus_domain_user_account()

Currently kdbus_domain_user_find_or_new() is used to find a user
domain or create a new one and link it into the domain.

kdbus_domain_user_find_or_new() may fail due to memory allocation
errors or if the domain was shutdown, but since callers will
receive only a NULL pointer on failure, they assume -ENOMEM and
ignore -ESHUTDOWN. Fix this in kdbus_domain_user_account() by returning
the appropriate error code.

There are also some races with kdbus_domain_user_find_or_new(), if it is
called with the same parameters and if we do not find a previously
linked domain user, then both threads will race to assign a different ID
for the same uid, thus, invalidating the users array. We fix this in the
new kdbus_domain_user_account() and __kdbus_domain_user_account() by
taking the domain lock only one time.

Replace some kdbus_domain_user_find_or_new() calls with
kdbus_domain_user_account(). The last one in bus.c is updated in the
next patch.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
bus: improve user quota accounting and domain locking

Currently kdbus_bus_new() execution path might take the domain lock
three times on success, four times on failure.

kdbus_bus_new():
  => kdbus_domain_user_find_or_new(): takes it 2 times (and it is racy)
  +
  kdbus_bus_new(): take it an extra time to account the user and link
  the bus into the domain bus_list.

And as discussed in the previous patch kdbus_domain_user_find_or_new()
is racy, so convert to __kdbus_domain_user_account()

This allows also to improve the execution path to take the domain lock
only one time in case of success. On failure the domain lock will be
taken two times.

kdbus_bus_new():
  => take domain lock
  => check if domain is still active/connected
     => __kdbus_domain_user_account()
  ...

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
connection: fix user quota accounting corruption

First use kzalloc to allocate the users array, so we do not reference
unintialized values.

And free the old conn->msg_users array not the newly allocated 'users'
one.

Patch tested, and users will hit the KDBUS_CONN_MAX_MSGS_PER_USER limit
and fail with -ENOBUFS

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
domain: remove dead kdbus_domain_user_find_or_new()

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test: do not use __ prefix for ordinary functions
do not use __ prefix for ordinary functions
domain: add BUG_ON()
do not use __ prefix for ordinary functions
kdbus.h: clarify policy holder docs
prevent special-purpose connections from owning names or exchanging messages
use conn->type instead of conn->flags where possible