Commit Graph

171 Commits

Author SHA1 Message Date
Lennart Poettering ae2a15bc14 macro: introduce TAKE_PTR() macro
This macro will read a pointer of any type, return it, and set the
pointer to NULL. This is useful as an explicit concept of passing
ownership of a memory area between pointers.

This takes inspiration from Rust:

https://doc.rust-lang.org/std/option/enum.Option.html#method.take

and was suggested by Alan Jenkins (@sourcejedi).

It drops ~160 lines of code from our codebase, which makes me like it.
Also, I think it clarifies passing of ownership, and thus helps
readability a bit (at least for the initiated who know the new macro)
2018-03-22 20:21:42 +01:00
Lennart Poettering 2770da027a sd-bus: add APIs to query the current read and write queue size 2018-02-27 19:54:29 +01:00
Lennart Poettering 201e419aea sd-bus: synthesize a description for user/system bus if otherwise unset
Let's make debugging easier, by synthesizing a name when we have some
indication what kind of bus this is.
2018-02-12 11:34:00 +01:00
Lennart Poettering af77d6f8da
Merge pull request #8083 from shawnl/close-ssh
sd-bus: cleanup ssh sessions (Closes: #8076)
2018-02-09 12:17:26 +01:00
Yu Watanabe e5c8029e13 sd-bus: explicitly convert int to bool 2018-02-09 16:22:00 +09:00
Yu Watanabe cad4fb19c8 sd-bus: use free_and_replace() 2018-02-09 16:21:51 +09:00
Yu Watanabe b4ca3f45dc sd-bus: avoid potential memory leaks 2018-02-09 16:21:29 +09:00
Shawn Landden 392cf1d05d sd-bus: cleanup ssh sessions (Closes: #8076)
we still invoke ssh unnecessarily when there in incompatible or erreneous input
The fallow-up to finish that would make the code a bit more verbose,
as it would require repeating this bit:
```
        r = bus_connect_transport(arg_transport, arg_host, false, &bus);
        if (r < 0) {
                log_error_errno(r, "Failed to create bus connection: %m");
                goto finish;
        }

        sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
```
in every verb, after parsing.

v2: add waitpid() to avoid a zombie process, switch to SIGTERM from SIGKILL
v3: refactor, wait in bus_start_address()
2018-02-08 10:14:48 -08:00
Nathaniel McCallum 45b1f410ba Add support for SD_BUS_DEFAULT*
Currently, sd-bus supports the ability to have thread-local default busses.
However, this is less useful than it can be since all functions which
require an sd_bus* as input require the caller to pass it. This patch adds
a new macro which allows the developer to pass a constant SD_BUS_DEFAULT,
SD_BUS_DEFAULT_USER or SD_BUS_DEFAULT_SYSTEM instead. This reduces work for
the caller.

For example:

    r = sd_bus_default(&bus);
    r = sd_bus_call_method(bus, ...);
    sd_bus_unref(bus);

Becomes:

    r = sd_bus_call_method(SD_BUS_DEFAULT, ...);

If the specified thread-local default bus does not exist, the function
calls will return -ENOPKG. No bus will ever be implicitly created.
2018-01-23 09:40:25 -05:00
Lennart Poettering dccca82b1a log: minimize includes in log.h
log.h really should only include the bare minimum of other headers, as
it is really pulled into pretty much everything else and already in
itself one of the most basic pieces of code we have.

Let's hence drop inclusion of:

1. sd-id128.h because it's entirely unneeded in current log.h
2. errno.h, dito.
3. sys/signalfd.h which we can replace by a simple struct forward
   declaration
4. process-util.h which was needed for getpid_cached() which we now hide
   in a funciton log_emergency_level() instead, which nicely abstracts
   the details away.
5. sys/socket.h which was needed for struct iovec, but a simple struct
   forward declaration suffices for that too.

Ultimately this actually makes our source tree larger (since users of
the functionality above must now include it themselves, log.h won't do
that for them), but I think it helps to untangle our web of includes a
tiny bit.

(Background: I'd like to isolate the generic bits of src/basic/ enough
so that we can do a git submodule import into casync for it)
2018-01-11 14:44:31 +01:00
Lennart Poettering 66baf8c644 sd-bus: remove 'hint_sync_call' parameter from various function calls
This is unused since kdbus is gone, hence remove this too. This permits
us to get rid of sd_bus_send_internal() and just implement sd_bus_send()
directly.
2018-01-05 13:58:33 +01:00
Lennart Poettering 48ef41a335 sd-bus: add API to optionally set a sender field on all outgoing messages
This is useful on direct connections to generate messages with valid
sender fields.

This is particularly useful for services that are accessible both
through direct connections and the broker, as it allows clients to
install matches on the sender service name, and they work the same in
both cases.
2018-01-05 13:58:33 +01:00
Lennart Poettering dbc526f0bc sd-bus: accept NULL callbacks in sd_bus_call_async()
This way sd_bus_call_method_async() (which is just a wrapper around
sd_bus_call_async()) can be used to put method calls together that
expect no reply.
2018-01-05 13:58:32 +01:00
Lennart Poettering 3e0e196efd sd-bus: log about bus state changes
Let's unify all state changes in a new helper function, from which we
can then debug log all state changes
2018-01-05 13:58:32 +01:00
Lennart Poettering b38cc8d563 sd-bus: add new sd_bus_set_connected_signal() API
With this new API sd-bus can synthesize a local "Connected" signal when
the connection is fully established. It mirrors the local "Disconnected"
signal that is already generated when the connection is terminated. This
is useful to be notified when connection setup is done, in order to
start method calls then, in particular when using "slow" connection
methods (for example slow TCP, or most importantly the "watch_bind"
inotify logic).

Note that one could also use hook into the initial NameAcquired signal
received from the bus broker, but that scheme works only if we actually
connect to a bus. The benefit of "Connected" OTOH is that it works with
any kind of connection.

Ideally, we'd just generate this message unconditionally, but in order
not to break clients that do not expect this message it is opt-in.
2018-01-05 13:58:32 +01:00
Lennart Poettering bdbc866914 sd-bus: add new sd_bus_is_ready() API
This new call is much light sd_bus_is_open(), but returns true only if
the connection is fully set up, i.e. after we finished with the
authentication and Hello() phase. This API is useful for clients in
particular when using the "watch_bind" feature, as that way it can be
determined in advance whether it makes sense to sync on some operation.
2018-01-05 13:58:32 +01:00
Lennart Poettering 15ca0a42c7 sd-bus: drop references to legacy /var/run D-Bus socket
Let's directly reference /run instead, so that we can work without /var
being around, or with /var/run being incorrectly set up.

Note that we keep the old socket path in place when referencing the
system bus of containers, as they might be foreign operating systems,
that still don't have adopted /run, and where it makes sense to use the
standardized name instead. On local systems, we insist on /run being set
up properly however, hence this limitation does not apply.

Also, get rid of the UNIX_SYSTEM_BUS_ADDRESS and
UNIX_USER_BUS_ADDRESS_FMT defines. They had a purpose when we still did
kdbus, as we then had to support two different backends. But since
that's gone, we don't need this indirection anymore, hence settle on a
one define only.
2018-01-05 13:58:32 +01:00
Lennart Poettering 7593c7a495 sd-bus: add asynchronous version of sd_bus_match()
We usually enqueue a number of these calls on each service
initialization. Let's do this asynchronously, and thus remove
synchronization points. This improves both performance behaviour and
reduces the chances to deadlock.
2018-01-05 13:58:32 +01:00
Lennart Poettering aec7b7bae7 sd-bus: remove bus_remove_match_by_string() helper which is unused 2018-01-05 13:58:32 +01:00
Lennart Poettering 0259b87f5e sd-bus: drop unused parameters from bus_add_match_internal()
We don't need the match components anymore, since kdbus is gone, hence
drop it.
2018-01-05 13:58:32 +01:00
Lennart Poettering 98c5bbc85d sd-bus: add APIs to request/release names asynchronously
They do the same thing as their synchronous counterparts, but only
enqueue the operation, thus removing synchronization points during
service initialization.

If the callback function is passed as NULL we'll fallback to generic
implementations of the reply handlers, that terminate the connection if
the requested name cannot be acquired, under the assumption that not
being able to acquire the name is a technical problem.
2018-01-05 13:58:32 +01:00
Lennart Poettering c7db1984d0 sd-bus: get rid of kdbus flags cruft
We only need three bits from the old kdbus flags cruft, hence let's make
them proper booleans.
2018-01-05 13:55:08 +01:00
Lennart Poettering ac8029fc25 sd-bus: start reply callback timeouts only when the connection is established
Currently, reply callback timeouts are started the instant the method
calls are enqueued, which can be very early on. For example, the Hello()
method call is enqueued right when sd_bus_start() is called, i.e. before
the socket connection and everything is established.

With this change we instead start the method timeout the moment we
actually leave the authentication phase of the connection. This way, the
timeout the kernel applies on socket connecting, and we apply on the
authentication phase no longer runs in parallel to the Hello() method
call, but all three run serially one after the other, which is
definitely a cleaner approach.

Moreover, this makes the "watch bind" feature a lot more useful, as it
allows enqueuing method calls while we are still waiting for inotify
events, without them timeouting until the connection is actually
established, i.e. when the method call actually has a chance of being
actually run.

This is a change of behaviour of course, but I think the new behaviour
is much better than the old one, since we don't race timeouts against
each other anymore...
2018-01-05 13:55:08 +01:00
Lennart Poettering 8a5cd31e5f sd-bus: optionally, use inotify to wait for bus sockets to appear
This adds a "watch-bind" feature to sd-bus connections. If set and the
AF_UNIX socket we are connecting to doesn't exist yet, we'll establish
an inotify watch instead, and wait for the socket to appear. In other
words, a missing AF_UNIX just makes connecting slower.

This is useful for daemons such as networkd or resolved that shall be
able to run during early-boot, before dbus-daemon is up, and want to
connect to dbus-daemon as soon as it becomes ready.
2018-01-05 13:55:08 +01:00
Lennart Poettering 5ae37ad833 sd-bus: when attached to an sd-event loop, disconnect on processing errors
If we can't process the bus for some reason we shouldn't just disable
the event source, but log something and give up on the connection. Hence
do that, and disconnect.
2018-01-05 13:55:08 +01:00
Lennart Poettering b057498a52 sd-bus: propagate handling errors for Hello method reply directly
Currently, when sd-bus is used to issue a method call, and we get a
reply and the specified reply handler fails, we log this locally at
debug priority and proceed. The idea is that a bad server-side reply
should not be fatal for the program, except when the developer
explicitly terminates the event loop.

The reply to the initial Hello() method call we issue when joining a bus
should not be handled like that however. Instead, propagate the error
immediately, as anything that is wrong with the Hello() reply should be
considered a fatal connection problem.
2018-01-05 13:55:08 +01:00
Lennart Poettering b33652fe91 sd-bus: minor coding style fix 2018-01-05 13:55:08 +01:00
Lennart Poettering e32fd6b47c sd-bus: when debug logging about messages, show the same bits of it everywhere
Also, include the message signature everywhere.
2018-01-05 13:55:08 +01:00
Lennart Poettering 86ed6d1b1f sd-bus: let's use mfree() where we can 2018-01-04 13:28:24 +01:00
Lennart Poettering 18ac4643cb sd-bus: use SO_PEERGROUPS when available to identify groups of peer 2018-01-04 13:28:24 +01:00
Daniel Lockyer f9ecfd3bbe Replace free and reassignment with free_and_replace 2017-11-24 10:33:41 +00:00
Zbigniew Jędrzejewski-Szmek 53e1b68390 Add SPDX license identifiers to source files under the LGPL
This follows what the kernel is doing, c.f.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
2017-11-19 19:08:15 +01:00
aeywalee 75bcbcf2e7 Add sd_bus_message_new and sd_bus_message_seal as public functions (#6609) 2017-10-30 16:08:46 +01:00
Lennart Poettering 532f808fd1 sd-bus: drop match cookie concept
THe match cookie was used by kdbus to identify matches we install
uniquely. But given that kdbus is gone, the cookie serves no process
anymore, let's kill it.
2017-09-29 17:57:34 +02:00
Lennart Poettering e28d086553 sd-bus: when showing brief message info show error name in debug out put too
When debug logging is enabled we show brief information about every bus
message we send or receieve. Pretty much all information is shown,
except for the error name if a message is an error (interestingly we do
print the error text however). Fix that, and add the error name as well.
2017-09-29 17:48:29 +02:00
Yu Watanabe 945c2931bb libsystemd: use IN_SET macro 2017-09-28 17:37:59 +09:00
Evgeny Vereshchagin 58c6e4a2c0 sd-bus: use -- when passing arguments to ssh (#6706)
This prevents `systemctl` from runnning /bin/touch when the following
command is used:
```
systemctl -H '-oProxyCommand=/bin/touch i-shouldnt-be-here' show-environment
```
2017-08-31 10:38:30 +02:00
Evgeny Vereshchagin 4f6631c8fc sd-bus: free everything when bus_set_address_user fails (#6552)
Fixes:
```
$ env -i valgrind --leak-check=full ./build/test-bus-chat
...
==7763== 1,888 (1,824 direct, 64 indirect) bytes in 1 blocks are
definitely lost in loss record 2 of 2
==7763==    at 0x4C2FA50: calloc (vg_replace_malloc.c:711)
==7763==    by 0x4F8FF9A: sd_bus_new (sd-bus.c:175)
==7763==    by 0x4F938BF: sd_bus_open_user (sd-bus.c:1138)
==7763==    by 0x109ACD: server_init (test-bus-chat.c:70)
==7763==    by 0x10BCF8: main (test-bus-chat.c:526)
==7763==
```

Closes #6481
2017-08-08 08:42:51 +02:00
Zbigniew Jędrzejewski-Szmek a132bef023 Drop kdbus bits
Some kdbus_flag and memfd related parts are left behind, because they
are entangled with the "legacy" dbus support.

test-bus-benchmark is switched to "manual". It was already broken before
(in the non-kdbus mode) but apparently nobody noticed. Hopefully it can
be fixed later.
2017-07-23 12:01:54 -04:00
Lennart Poettering df0ff12775 tree-wide: make use of getpid_cached() wherever we can
This moves pretty much all uses of getpid() over to getpid_raw(). I
didn't specifically check whether the optimization is worth it for each
replacement, but in order to keep things simple and systematic I
switched over everything at once.
2017-07-20 20:27:24 +02:00
Lennart Poettering 694859b5e7 sd-bus: never augment creds when we are operating on remote connections (#6217)
It's not always clear when something is a remote connection, hence only
flag the obvious cases as local.

Fixes: #6207
2017-06-28 13:20:16 -04:00
Zbigniew Jędrzejewski-Szmek 605405c6cc tree-wide: drop NULL sentinel from strjoin
This makes strjoin and strjoina more similar and avoids the useless final
argument.

spatch -I . -I ./src -I ./src/basic -I ./src/basic -I ./src/shared -I ./src/shared -I ./src/network -I ./src/locale -I ./src/login -I ./src/journal -I ./src/journal -I ./src/timedate -I ./src/timesync -I ./src/nspawn -I ./src/resolve -I ./src/resolve -I ./src/systemd -I ./src/core -I ./src/core -I ./src/libudev -I ./src/udev -I ./src/udev/net -I ./src/udev -I ./src/libsystemd/sd-bus -I ./src/libsystemd/sd-event -I ./src/libsystemd/sd-login -I ./src/libsystemd/sd-netlink -I ./src/libsystemd/sd-network -I ./src/libsystemd/sd-hwdb -I ./src/libsystemd/sd-device -I ./src/libsystemd/sd-id128 -I ./src/libsystemd-network --sp-file coccinelle/strjoin.cocci --in-place $(git ls-files src/*.c)

git grep -e '\bstrjoin\b.*NULL' -l|xargs sed -i -r 's/strjoin\((.*), NULL\)/strjoin(\1)/'

This might have missed a few cases (spatch has a really hard time dealing
with _cleanup_ macros), but that's no big issue, they can always be fixed
later.
2016-10-23 11:43:27 -04:00
Lennart Poettering fbb4603d48 sd-bus: optionally, exit process or event loop on disconnect
Old libdbus has a feature that the process is terminated whenever the the bus
connection receives a disconnect. This is pretty useful on desktop apps (where
a disconnect indicates session termination), as well as on command line apps
(where we really shouldn't stay hanging in most cases if dbus daemon goes
down).

Add a similar feature to sd-bus, but make it opt-in rather than opt-out, like
it is on libdbus. Also, if the bus is attached to an event loop just exit the
event loop rather than the the whole process.
2016-08-22 17:31:41 +02:00
Lennart Poettering 232f367766 sd-bus: when the server-side disconnects, make sure to dispatch all tracking objects immediately
If the server side kicks us from the bus, from our view no names are on the bus
anymore, hence let's make sure to dispatch all tracking objects immediately.
2016-08-22 17:31:36 +02:00
Lennart Poettering 217fcc7eb3 sd-bus: split out handling of reply callbacks on close into its own function
When a bus connection is closed we dispatch all reply callbacks. Do so in a new
function if its own.

No behaviour changes.
2016-08-22 16:14:21 +02:00
Lennart Poettering fc2fffe770 tree-wide: introduce new SOCKADDR_UN_LEN() macro, and use it everywhere
The macro determines the right length of a AF_UNIX "struct sockaddr_un" to pass to
connect() or bind(). It automatically figures out if the socket refers to an
abstract namespace socket, or a socket in the file system, and properly handles
the full length of the path field.

This macro is not only safer, but also simpler to use, than the usual
offsetof() + strlen() logic.
2016-05-05 22:24:36 +02:00
Zbigniew Jędrzejewski-Szmek 103a5027f6 sd-bus: use IN_SET 2016-04-16 18:21:58 -04:00
Alexander Kuleshov 5883ff6017 tree-wide: use SET_FLAG() macro to make code more clear 2016-03-05 18:26:01 +06:00
Vito Caputo 313cefa1d9 tree-wide: make ++/-- usage consistent WRT spacing
Throughout the tree there's spurious use of spaces separating ++ and --
operators from their respective operands.  Make ++ and -- operator
consistent with the majority of existing uses; discard the spaces.
2016-02-22 20:32:04 -08:00
Daniel Mack b26fa1a2fb tree-wide: remove Emacs lines from all files
This should be handled fine now by .dir-locals.el, so need to carry that
stuff in every file.
2016-02-10 13:41:57 +01:00