Commit Graph

230 Commits

Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek d0e5db44d9 sd-bus: allow localhost addresses
By settings AI_ADDRCONFIG in hints we cannot for example resolve "localhost"
when the local machine only has a loopback interface. This seems like an
unnecessary restriction, drop it.

Inspired by https://bugzilla.redhat.com/show_bug.cgi?id=1839007.
2020-09-15 16:45:03 +02:00
Zbigniew Jędrzejewski-Szmek 90e74a66e6 tree-wide: define iterator inside of the macro 2020-09-08 12:14:05 +02:00
Lennart Poettering dad28bffd6 tree-wide: check POLLNVAL everywhere
poll() sets POLLNVAL inside of the poll structures if an invalid fd is
passed. So far we generally didn't check for that, thus not taking
notice of the error. Given that this specific kind of error is generally
indication of a programming error, and given that our code is embedded
into our projects via NSS or because people link against our library,
let's explicitly check for this and convert it to EBADF.

(I ran into a busy loop because of this missing check when some of my
test code accidentally closed an fd it shouldn't close, so this is a
real thing)
2020-06-10 08:57:31 +02:00
Zbigniew Jędrzejewski-Szmek 062ac2ea85 sd-bus: internalize setting of bus is_system/is_user
Each of bus_set_address_{user,system} had two users, and each of the two users
would set the internal flag manually. We should do that internally in the
functions instead.

While at it, only set the flag when setting the address is actually successful.
This doesn't change anything for current users, but it seems more correct.
2020-05-25 11:09:21 +02:00
Daan De Meyer 59a77060e0 sd-bus: Use pointer syntax for sd_bus_set_exec argv parameter 2020-04-11 21:57:28 +02:00
Daan De Meyer 1e9a7c44a9 sd-bus: remove unused priority logic 2020-04-02 21:42:32 +02:00
Zbigniew Jędrzejewski-Szmek f41df6954c sd-bus: whitespace adjustments 2020-03-19 21:38:46 +01:00
Zbigniew Jędrzejewski-Szmek 70bc558cc1 sd-bus: support SD_BUS_DEFAULT* and don't crash in functions where bus is optional
In those functions where bus defaults to the m->bus, we should also
resolve the magic parameters. And if neither called with bus=NULL
and an unattached message, return properly instead of crashing in assert
later.
2020-03-19 21:38:46 +01:00
Zbigniew Jędrzejewski-Szmek 501ecd670c sd-bus: make sure SD_BUS_DEFAULT* works everywhere
I'm not sure why those functions were not touched in
45b1f410ba. Anyway, it seems easier
to just support the magic parameters everywhere.
2020-03-19 21:38:46 +01:00
Zbigniew Jędrzejewski-Szmek d3d5ff4bc2 sd-bus: make bus_set_address_user always go through sd_bus_set_address
This way all the checks are done, and we don't leak a pointer if
bus_set_address_user() is called twice.
2020-03-18 19:57:43 +01:00
Zbigniew Jędrzejewski-Szmek bc130b6858 Fix typo in function name 2020-02-04 18:54:12 +01:00
Lennart Poettering 1068447e69 sd-bus: introduce API for re-enqueuing incoming messages
When authorizing via PolicyKit we want to process incoming method calls
twice: once to process and figure out that we need PK authentication,
and a second time after we aquired PK authentication to actually execute
the operation. With this new call sd_bus_enqueue_for_read() we have a
way to put an incoming message back into the read queue for this
purpose.

This might have other uses too, for example debugging.
2020-02-04 18:47:31 +01:00
Lennart Poettering 5905d7cf5b tree-wide: use SD_ID128_STRING_MAX where appropriate 2019-12-10 11:56:18 +01:00
Lennart Poettering 19fa17c7c4 sd-bus: invalidate connection when Hello() fails
Fixes: #13969
2019-11-16 13:47:32 +01:00
Yu Watanabe f5947a5e92 tree-wide: drop missing.h 2019-10-31 17:57:03 +09:00
Zbigniew Jędrzejewski-Szmek 61252bae91 sd-bus: adjust indentation of comments 2019-08-27 19:49:09 +02:00
Lennart Poettering 6b39223cd3 sd-bus: when installing a match make sure not to apply it to already queued messages
This tweaks match installation a bit: the match callbacks are now only
called for messages read after the AddMatch() reply was received and
never anything already read before. Thus, installing a match gives you a
time guarantee: only messages received after it will be matched.

This is useful when listening to PropertiesChanged signals as an example
to ensure that only changes after the point the match was installed are
honoured, nothing before.
2019-07-11 12:18:51 +02:00
Lennart Poettering 3cf8dd5359 sd-bus: destroy AddMatch() reply slot in failure case too
If AddMatch() doesn't work, let's destroy the slot for it too as soon as
we received the failure for it.

This way the mere existance of the slot tells us whether the AddMatch()
method call is still pending or is complete.
2019-07-11 12:18:51 +02:00
Lennart Poettering f1617a3b61 sd-bus: maintain a counter for incoming msgs
Let's count incoming messages and attach the current counter when we
first read them to the message objects. This allows us to nicely order
messages later on.
2019-07-11 12:18:51 +02:00
Yu Watanabe 657ee2d82b tree-wide: replace strjoin() with path_join() 2019-06-21 03:26:16 +09:00
Ben Boeckel 5238e95759 codespell: fix spelling errors 2019-04-29 16:47:18 +02:00
Lennart Poettering f60a028a4e tree-wide: use ERRNO_IS_DISCONNECT() at more places 2019-03-19 15:41:30 +01:00
Lennart Poettering 0a9707187b util: split out memcmp()/memset() related calls into memory-util.[ch]
Just some source rearranging.
2019-03-13 12:16:43 +01:00
Zbigniew Jędrzejewski-Szmek 42541a71a2 bus: make reference counting non-atomic
We had atomic counters, but all other operations were non-serialized. This
means that concurrent access to the bus object was only safe if _all_ threads
were doing read-only access. Even sending of messages from threads would not be
possible, because after sending of the message we usually want to remove it
from the send queue in the bus object, which would race. Let's just kill this.
2019-03-04 14:16:24 +01:00
Lennart Poettering c1757a70ea sd-bus: use "queue" message references for managing r/w message queues in connection objects
Let's make use of the new concept the previous commit added.

See: #4846
2019-03-01 15:37:39 +01:00
Lennart Poettering 39feb2ce41 sd-bus: drop two inappropriate empty lines 2019-03-01 15:19:45 +01:00
Lennart Poettering c0bc4ec5cc sd-bus: make sure dispatch_rqueue() initializes return parameter on all types of success
Let's make sure our own code follows coding style and initializes all
return values on all types of success (and leaves it uninitialized in
all types of failure).
2019-03-01 15:19:45 +01:00
Lennart Poettering 143d4e045a sd-bus: make rqueue/wqueue sizes of type size_t
Let's do this like we usually do and size arrays with size_t.

We already do this for the "allocated" counter correctly, and externally
we expose the queue sizes as uint64_t anyway, hence there's really no
point in usigned "unsigned" internally.
2019-03-01 15:19:45 +01:00
Lennart Poettering 2fe9a10d76 sd-bus: initialize mutex after we allocated the wqueue
That way the mutex doesn't have to be destroyed when we exit early due
to OOM.
2019-03-01 15:19:45 +01:00
Lennart Poettering 1f82f5bb42 sd-bus: deal with cookie overruns
Apparently this happens IRL. Let's carefully deal with issues like this:
when we overrun, let's not go back to zero but instead leave the highest
cookie bit set. We use that as indication that we are in "overrun
territory", and then are particularly careful with checking cookies,
i.e. that they haven't been used for still outstanding replies yet. This
should retain the quick cookie generation behaviour we used to have, but
permits dealing with overruns.

Replaces: #11804
Fixes: #11809
2019-02-28 13:44:05 +01:00
Lennart Poettering bd62b74486 sd-bus: add sd_bus_close_unref() helper
It's similar to sd_bus_flush_close_unref() but doesn't do the flushing.
This is useful since this will still discnnect the connection properly
but not synchronously wait for the peer to take our messages.

Primary usecase is within _cleanup_() expressions where synchronously
waiting on the peer is not OK.
2019-01-17 16:12:38 +01:00
Lennart Poettering 5dd9527883 tree-wide: remove various unused functions
All found with "cppcheck --enable=unusedFunction".
2018-12-02 13:35:34 +09:00
Lennart Poettering c4e48030cf sd-bus: make "close+flush-on-exit" optional when using sd-event with sd-bus
This adds a new pair of API calls sd_bus_set_close_on_exit() and
sd_bus_get_close_on_exit(). They control whether an sd_bus object
attached to a an sd-event loop shall automatically be flushed/closed
when the event loop goes down. Usually that's a good thing, except for
very few cases where the bus connection is longer living than the event
loop it is attached on. Specifically, this is the case for nspawn, where
we run the event loop only while the container is up, but afterwards
still want to be able to use the bus connection.
2018-11-09 17:08:59 +01:00
Lennart Poettering 490c5a37cb tree-wide: some automatic coccinelle fixes (#10463)
Nothing fancy, just coccinelle doing its work.
2018-10-20 00:07:46 +09:00
Lennart Poettering 95cb14b068 sd-bus: rework how we initialize struct sockaddr_un
Let's use structured initialization, but more importantly, let's
increase salen by 1, if we reference AF_UNIX sockets in the file system,
so that they also contain the trailing NUL byte. This is what unix(7)
suggests to do, hence follow it.
2018-10-15 19:40:51 +02:00
Lennart Poettering 1d261418e2 sd-bus: make parsing of AF_UNIX socket addresses more strict
Insist on NUL termination, just to be safe rather than sorry. The kernel
doesn't require it, but it's really annoying if people rely on this,
hence refuse this early.
2018-10-15 19:40:51 +02:00
Lennart Poettering 44ed5214ad tree-wide: use structured initialization for sockaddr_un 2018-10-15 19:35:00 +02:00
Yu Watanabe 9c57a73b13 tree-wide: use CMP() macros where applicable 2018-10-09 14:45:55 +02:00
Yu Watanabe 1c71f7f329 libsystemd: use DEFINE_ATOMIC_REF_UNREF_FUNC or frineds where applicable 2018-08-28 05:09:40 +09:00
Shawn Landden 6a5558491b sd-bus: fix test cases on host handling 2018-08-06 14:30:53 -07:00
Shawn Landden b85b4a70d7 sd-bus rework host handling
--machine has been missing for a while in systemd-stdio-bridge
this syntax can be switched to be more standard.

v2: Support the old syntax too.

timedatectl -H server1.myhostingcompany.com:5555/container1

Closes: #8071
2018-08-06 14:30:53 -07:00
Lennart Poettering 1e5057b904 sd-bus: allow connecting to the pseudo-container ".host"
machined exposes the pseudo-container ".host" as a reference to the host
system, and this means "machinectl login .host" and "machinectl shell
.host" get your a login/shell on the host. systemd-run currently doesn't
allow that. Let's fix that, and make sd-bus understand ".host" as an
alias for connecting to the host system.
2018-07-25 22:48:11 +02:00
Yu Watanabe 2e7e8e34aa sd-bus: make bus_slot_disconnect() also unref the slot object
This makes bus_slot_disconnect() unref the slot object from bus when
`unref == true` and it is floating, as the function removes the
reference from the relevant bus object.

This reverts 20d4ee2cbc, as it
introduces #9604.

Fixes #9604.
2018-07-18 20:54:19 +02:00
Zbigniew Jędrzejewski-Szmek 6d77a30c09 bus: add comment to explain assert 2018-07-18 16:36:37 +02:00
Zbigniew Jędrzejewski-Szmek 7ae8edcd03 bus: move BUS_DONT_DESTROY calls after asserts
It's not useful to bump the reference count before checking if the object is
NULL. Thanks to d40f5cc498 we can do this ;).

Related to https://bugzilla.redhat.com/show_bug.cgi?id=1576084,
https://bugzilla.redhat.com/show_bug.cgi?id=1575340,
https://bugzilla.redhat.com/show_bug.cgi?id=1575350. I'm not sure why those two
people hit this code path, while most people don't. At least we won't abort.
2018-07-18 12:16:33 +02:00
Yu Watanabe 385b2eb262 sd-bus: make BUS_DEFAULT_TIMEOUT configurable
This adds sd_bus_{get,set}_method_call_timeout().
If the timeout is not set or set to 0, then the timeout value is
parsed from $SYSTEMD_BUS_TIMEOUT= environment variable. If the
environment variable is not set, then built-in timeout is used.
2018-07-16 15:11:50 +02:00
Yu Watanabe 8f5dd8c094 tree-wide: drop empty comments 2018-06-29 11:00:30 +09:00
Lennart Poettering 0c69794138 tree-wide: remove Lennart's copyright lines
These lines are generally out-of-date, incomplete and unnecessary. With
SPDX and git repository much more accurate and fine grained information
about licensing and authorship is available, hence let's drop the
per-file copyright notice. Of course, removing copyright lines of others
is problematic, hence this commit only removes my own lines and leaves
all others untouched. It might be nicer if sooner or later those could
go away too, making git the only and accurate source of authorship
information.
2018-06-14 10:20:20 +02:00
Lennart Poettering 818bf54632 tree-wide: drop 'This file is part of systemd' blurb
This part of the copyright blurb stems from the GPL use recommendations:

https://www.gnu.org/licenses/gpl-howto.en.html

The concept appears to originate in times where version control was per
file, instead of per tree, and was a way to glue the files together.
Ultimately, we nowadays don't live in that world anymore, and this
information is entirely useless anyway, as people are very welcome to
copy these files into any projects they like, and they shouldn't have to
change bits that are part of our copyright header for that.

hence, let's just get rid of this old cruft, and shorten our codebase a
bit.
2018-06-14 10:20:20 +02:00
Lennart Poettering fc2d4c89b8 sd-bus: make add match method callback slot "floating"
When we allocate an asynchronous match object we will allocate an
asynchronous bus call object to install the match server side.
Previously the call slot would be created as regular slot, i.e.
non-floating which meant installing the match even if it was itself
floating would result in a non-floating slot to be created internally,
which ultimately would mean the sd_bus object would be referenced by it,
and thus never be freed.

Let's fix that by making the match method callback floating in any case
as we have no interest in leaving the bus allocated beyond the match
slot.

Fixes: #8551
2018-05-30 17:34:34 +02:00