pool: handle ENOMEM case properly in kdbus_pool_slice_alloc

If we got larger slice than requested and could not allocate memory for
new slice to perform split operation, then free and busy trees will stay
in inconsistent state, as we already moved found slice between them.

Fix such behavior by doing split-off firstly and moving slice between
trees after. This also increases readability, as previously we checked
two times for the same condition (!n is equal to s->size > slice_size).

Signed-off-by: Sergei Zviagintsev <sergei@s15v.net>
Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
pool: drop redundant assignments

Turn some redundant assignments into WARN_ON().

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
pool: avoid BUG_ON()

If you deref a NULL pointer, you trigger a WARN. It seems highly
excessive to trigger a panic (BUG) if we leak a child slice. So turn the
BUG into a WARN and add one more to be consistent with
kdbus_pool_slice_release().

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
match: fix memory leak in error path

Call kdbus_match_rule_free() in the error path to cleanly free all
associated memory. For this to work, rule->type must be assigned as
soon as possible. Also, make kdbus_match_rule_free() accept NULL
pointers so we don't have to check for that on the call site.

Signed-off-by: Daniel Mack <daniel@zonque.org>
domain, endpoint: don't line-break container_of() users

Just to be consistent.

Signed-off-by: Daniel Mack <daniel@zonque.org>
policy: remove BUG_ON() from kdbus_policy_db_clear()

There's no reason for checking on this explicitly, and the only users
call that function on an embedded struct anyway, which can't be NULL
at the time of calling.

Signed-off-by: Daniel Mack <daniel@zonque.org>
pool, connection, endpoint: handle errors more gracefully

Refrain from excessive BUG_ON() usage, and user WARN_ON() for
unexpected conditions that don't indicate memory corruption.

There are more such locations that need similar tweaking.

Signed-off-by: Daniel Mack <daniel@zonque.org>
bus.c: use WARN_ON() rather than BUG_ON()

Signed-off-by: Daniel Mack <daniel@zonque.org>
names: use WARN_ON() rather than BUG_ON()

Signed-off-by: Daniel Mack <daniel@zonque.org>
test: fix usage print endless loop

do not use the some iterator in nested loops

Signed-off-by: Rui Miguel Silva <rmfrfs@gmail.com>
match: init list rule so we can remove it on error

initialise the rules_entry so we don't oops on error paths when the rule is not
in list yet.

Signed-off-by: Rui Miguel Silva <rmfrfs@gmail.com>
connection: return -ETIMEDOUT immediately if msg->timeout_ns <= now

If the timeout of a message has already exceeded at the time of
sending a message, there's no point in going to sleep. Return
-ETIMEDOUT immediately in such cases.

While at it, move the !reply_wait check to kdbus_conn_wait_reply().

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: kdbus_conn_reply_unref() can take NULL pointers

No need to check the parameter prior to calling that function.

Signed-off-by: Daniel Mack <daniel@zonque.org>
kdbus.h: remove KDBUS_CMD_CANCEL

Kill KDBUS_CMD_CANCEL. The API wasn't thought to the end, as it cannot
be used in a race-free way from a threaded user-space environment.

Instead, we have to implement a signal mask that is installed before
the waitqueue is entered.

Signed-off-by: Daniel Mack <daniel@zonque.org>
test: preparation support for a namespaced test setup

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test: allow our tests to run in a namespaced setup

Tests can take run now in their namespaced setup, if the --mntns switch
is given a new mount namespace is created and kdbusfs is remounted in
that namespace.

Currently all tests succeed, only when we combine the userns switch some
test will fail the ones that call setuid(), we have to make sure that
the uid/gid are mapped in the userns, next patch will fix the ones that
fail.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test-attach: fix attach-flags so we can SKIP it in unprivleged userns

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test-activator: check if all uids/gids are mapped in

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test: tree-wide check if all uids and gids are mapped in current userns

Tests that are running in user namespaces where not all the uids and
gids are mapped will be SKIPPED automatically, this is the case of
unprivileged which turn privileged in its userns.

We skip test since even if we have CAP_SETUID we also need the target
uid to be mapped in that userns. Well, we do not need all the uids but
we are just lazy here...

The ones that do not like CONFIG_USER_NS can also test kdbus as it was
in a container, running under root:
  # ./test/kdbus-test --mntns --pidns

With this change unprivileged running on kernel with CONFIR_USER_NS can run
kdbus tests:
$ ./test/kdbus-test --userns --uidns "0 1000 1" --gidns "0 1000 1" --mntns --pidns

(but some tests are skipped)

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test: when starting tests print the attach flags mask

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>