summaryrefslogtreecommitdiff
path: root/src/libsystemd/sd-bus/bus-internal.h
Commit message (Collapse)AuthorAgeFilesLines
* sd-bus: use the new information in the client's sockaddr in the creds structureLennart Poettering2023-05-161-0/+2
| | | | | | | Now that clients might convey comm/description strings via the sockaddr, let's actually use them on the other side, read the data via getpeername() parse it, and include it in the "owner" creds (which is how we call the peer's creds).
* sd-bus: bus_message_type_from_string is not pureXi Ruoyao2023-05-101-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | GCC document [1] says: The pure attribute prohibits a function from modifying the state of the program that is observable by means other than inspecting the function’s return value. And there is an example: `int hash (char *) __attribute__ ((pure));` ... Even though hash takes a non-const pointer argument it must not modify the array it points to, ... But we are modifying the object pointed to by the pointer u, which is clearly a violation of the semantic of pure. With -ftrivial-auto-var-init (enabled by -Dmode=release), on some targets (GCC 12.2 on AArch64 and GCC 13.1 on x86_64) performs an optimization: as the variable "u" in bus_match_parse has been zero-initialized (by the -ftrivial-auto-var-init option) and never modified (because a "pure" bus_message_type_from_string is not allowed to modify it), "u" will be always 0. Then 0 is used to initialize .value_u8 field of struct bus_match_component. This then causes a infinite event loop, so "systemctl restart" never stops, and pam_systemd timeouts communicating with logind, etc. So we should remove the "pure" attribute here. Fixes #26395. [1]:https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute
* sd-bus: store and compare per-module static origin idLuca Boccassi2023-04-251-2/+2
| | | | | | | | | sd-bus objects use hashmaps, which use module-global state, so it is not safe to pass a sd-bus object created by a module instance to another module instance (e.g.: when two libraries static linking sd-bus are pulled in a single process). Initialize a random per-module origin id and store it in the object, and compare it when entering a public API, and error out if they don't match, together with the PID.
* basic: add RuntimeScope enumLennart Poettering2023-03-101-3/+4
| | | | | | | | | | | | In various tools and services we have a per-system and per-user concept. So far we sometimes used a boolean indicating whether we are in system mode, or a reversed boolean indicating whether we are in user mode, or the LookupScope enum used by the lookup path logic. Let's address that, in introduce a common enum for this, we can use all across the board. This is mostly just search/replace, no actual code changes.
* Rename def.h to constants.hZbigniew Jędrzejewski-Szmek2022-11-081-1/+1
| | | | | | The name "def.h" originates from before the rule of "no needless abbreviations" was established. Let's rename the file to clarify that it contains a collection of various semi-related constants.
* alloc-util: simplify GREEDY_REALLOC() logic by relying on malloc_usable_size()Lennart Poettering2021-05-191-2/+0
| | | | | | | | | | | | | | | | | | | | | | | | We recently started making more use of malloc_usable_size() and rely on it (see the string_erase() story). Given that we don't really support sytems where malloc_usable_size() cannot be trusted beyond statistics anyway, let's go fully in and rework GREEDY_REALLOC() on top of it: instead of passing around and maintaining the currenly allocated size everywhere, let's just derive it automatically from malloc_usable_size(). I am mostly after this for the simplicity this brings. It also brings minor efficiency improvements I guess, but things become so much nicer to look at if we can avoid these allocation size variables everywhere. Note that the malloc_usable_size() man page says relying on it wasn't "good programming practice", but I think it does this for reasons that don't apply here: the greedy realloc logic specifically doesn't rely on the returned extra size, beyond the fact that it is equal or larger than what was requested. (This commit was supposed to be a quick patch btw, but apparently we use the greedy realloc stuff quite a bit across the codebase, so this ends up touching *a*lot* of code.)
* bus/bus-match: inline iterator variable declarationsZbigniew Jędrzejewski-Szmek2021-03-061-2/+2
|
* tree-wide: use -EINVAL for enum invalid valuesZbigniew Jędrzejewski-Szmek2021-02-101-1/+1
| | | | | | | | | As suggested in https://github.com/systemd/systemd/pull/11484#issuecomment-775288617. This does not touch anything exposed in src/systemd. Changing the defines there would be a compatibility break. Note that tests are broken after this commit. They will be fixed in the next one.
* sd-bus: drop some bitfields in sd_bus_slotZbigniew Jędrzejewski-Szmek2021-02-101-11/+11
| | | | | | | | | | | A hole was/is present after the booleans, so changing them to be one byte each doesn't change the structure size (122 bits on amd64). If we add more stuff later, it might make sense to turn some of those into bitfields again. For now, let's take the easy route. EINVAL fits into type now. Code size it minimally reduced: -rwxrwxr-x 1 zbyszek zbyszek 4109792 Feb 10 14:00 build/libsystemd.so.0.30.0 -rwxrwxr-x 1 zbyszek zbyszek 4109712 Feb 10 14:01 build/libsystemd.so.0.30.0
* sd-bus: add API for connecting to a specific user's user bus of a specific ↵Lennart Poettering2020-12-151-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | container This is unfortunately harder to implement than it sounds. The user's bus is bound a to the user's lifecycle after all (i.e. only exists as long as the user has at least one PAM session), and the path dynamically (at least theoretically, in practice it's going to be the same always) generated via $XDG_RUNTIME_DIR in /run/. To fix this properly, we'll thus go through PAM before connecting to a user bus. Which is hard since we cannot just link against libpam in the container, since the container might have been compiled entirely differently. So our way out is to use systemd-run from outside, which invokes a transient unit that does PAM from outside, doing so via D-Bus. Inside the transient unit we then invoke systemd-stdio-bridge which forwards D-Bus from the user bus to us. The systemd-stdio-bridge makes up the PAM session and thus we can sure tht the bus exists at least as long as the bus connection is kept. Or so say this differently: if you use "systemctl -M lennart@foobar" now, the bus connection works like this: 1. sd-bus on the host forks off: systemd-run -M foobar -PGq --wait -pUser=lennart -pPAMName=login systemd-stdio-bridge 2. systemd-run gets a connection to the "foobar" container's system bus, and invokes the "systemd-stdio-bridge" binary as transient service inside a PAM session for the user "lennart" 3. The systemd-stdio-bridge then proxies our D-Bus traffic to the user bus. sd-bus (on host) → systemd-run (on host) → systemd-stdio-bridge (in container) Complicated? Well, to some point yes, but otoh it's actually nice in various other ways, primarily as it makes the -H and -M codepaths more alike. In the -H case (i.e. connect to remote host via SSH) a very similar three steps are used. The only difference is that instead of "systemd-run" the "ssh" binary is used to invoke the stdio bridge in a PAM session of some other system. Thus we get similar implementation and isolation for similar operations. Fixes: #14580
* license: LGPL-2.1+ -> LGPL-2.1-or-laterYu Watanabe2020-11-091-1/+1
|
* tree-wide: fixes for assorted grammar and spelling issuesZbigniew Jędrzejewski-Szmek2020-07-061-1/+1
| | | | Fixes #16363. Also includes some changes where I generalized the pattern.
* sd-bus: make name validation functions publicZbigniew Jędrzejewski-Szmek2020-05-251-0/+1
| | | | | | | | | | Those are fairly trivial to reimplement, but any non-trivial user of sd-bus is likely to need them. So let's expose them to save everyone the trouble. I'm keeping the internal functions and making the public ones thin wrappers, because for the internal uses we don't need the additional asserts, and also we can't expose _pure_ annotation easily, and dropping it would likely make the compiled code a bit less efficient.
* tree-wide: drop socket.h when socket-util.h is includedYu Watanabe2019-11-041-1/+0
|
* sd-bus: when installing a match make sure not to apply it to already queued ↵Lennart Poettering2019-07-111-0/+5
| | | | | | | | | | | | | 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.
* sd-bus: maintain a counter for incoming msgsLennart Poettering2019-07-111-0/+1
| | | | | | 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.
* sd-bus: bump message queue size againFranck Bui2019-04-261-2/+2
| | | | | Simliarly to issue #4068, the current limit turns out to be too small for a big storage setup that uses many small disks. Let's bump it further.
* sd-bus: change "int" → "signed int" on bitfieldLennart Poettering2019-03-281-1/+1
| | | | | | | | | Apparently by the C standard "int" bitfields can have any signedness (unlike non-bitfield declarations which are "signed" if the signedness is not specified). Let's fix the LGTM warning about this hence and be explicit that we mean "signed" here.
* tree-wide: reorder various structures to make them smaller and use fewer ↵Lennart Poettering2019-03-271-16/+19
| | | | | | cache lines Some "pahole" spelunking.
* headers: remove unneeded includes from util.hZbigniew Jędrzejewski-Szmek2019-03-271-1/+1
| | | | | This means we need to include many more headers in various files that simply included util.h before, but it seems cleaner to do it this way.
* util: move dbus specific definition into bus-internal.hLennart Poettering2019-03-141-0/+5
| | | | | It shouldn't be defined globally, since it's pretty much an implementation detail of DBus.
* bus: make reference counting non-atomicZbigniew Jędrzejewski-Szmek2019-03-041-10/+1
| | | | | | | | 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.
* sd-bus: make rqueue/wqueue sizes of type size_tLennart Poettering2019-03-011-2/+2
| | | | | | | | 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.
* Refuse dbus message paths longer than BUS_PATH_SIZE_MAX limit.Riccardo Schirone2019-02-181-0/+4
| | | | | | | | | Even though the dbus specification does not enforce any length limit on the path of a dbus message, having to analyze too long strings in PID1 may be time-consuming and it may have security impacts. In any case, the limit is set so high that real-life applications should not have a problem with it.
* tree-wide: s/time-out/timeout/gZbigniew Jędrzejewski-Szmek2018-12-141-1/+1
| | | | | | | | | | | | | | | | | From WordNet (r) 3.0 (2006) [wn]: time-out n 1: a brief suspension of play; "each team has two time-outs left" From The Free On-line Dictionary of Computing (18 March 2015) [foldoc]: timeout A period of time after which an error condition is raised if some event has not occured. A common example is sending a message. If the receiver does not acknowledge the message within some preset timeout period, a transmission error is assumed to have occured.
* tree-wide: remove various unused functionsLennart Poettering2018-12-021-5/+0
| | | | All found with "cppcheck --enable=unusedFunction".
* sd-bus: make "close+flush-on-exit" optional when using sd-event with sd-busLennart Poettering2018-11-091-0/+1
| | | | | | | | | | | 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.
* sd-bus: make BUS_DEFAULT_TIMEOUT configurableYu Watanabe2018-07-161-4/+5
| | | | | | | 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.
* tree-wide: drop double newlineYu Watanabe2018-06-291-1/+0
|
* tree-wide: drop empty commentsYu Watanabe2018-06-291-2/+0
|
* sd-bus: stop using the result of an assignment as an operand of &&Evegeny Vereshchagin2018-06-281-1/+1
| | | | | | This makes OBJECT_PATH_FOREACH_PREFIX consistent with PATH_FOREACH_PREFIX and also fixes 7 alerts reported by LGTM at https://lgtm.com/projects/g/systemd/systemd/snapshot/ac0a08700344a5690803df8dd80e8bb5013184a5/files/src/libsystemd/sd-bus/bus-objects.c?sort=name&dir=ASC&mode=heatmap&showExcluded=true#V1383
* tree-wide: remove Lennart's copyright linesLennart Poettering2018-06-141-1/+0
| | | | | | | | | | | 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.
* tree-wide: drop 'This file is part of systemd' blurbLennart Poettering2018-06-141-2/+0
| | | | | | | | | | | | | | | | 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.
* bus: optionally call a callbacks for cleanupZbigniew Jędrzejewski-Szmek2018-06-061-0/+1
| | | | | | | | | | | | | | | This adds a function sd_bus_slot_set_destroy_callback() to set a function which can free userdata or perform other cleanups. sd_bus_slot_get_destory_callback() queries the callback, and is included for completeness. Without something like this, for floating asynchronous callbacks, which might be called or not, depending on the sequence of events, it's hard to perform resource cleanup. The alternative would be to always perform the cleanup from the caller too, but that requires more coordination and keeping of some shared state. It's nicer to keep the cleanup contained between the callback and the function that requests the callback.
* sd-bus: add new sd_bus_slot_set_floating() callLennart Poettering2018-05-301-0/+8
| | | | | | | | | | This new call allows explicit control of the "floating" state of a bus slot object. This is useful for creating a bus slot object first, retaining a reference to it, using it for making changes to the slot object (for example, set a description) and then handing it over to sd-bus for lifecycle management. It's also useful to fix #8551.
* use max. message size allowed by DBus spec (#8936)David Tardon2018-05-091-1/+1
| | | C.f. https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages.
* tree-wide: be more careful with the type of array sizesLennart Poettering2018-04-271-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously we were a bit sloppy with the index and size types of arrays, we'd regularly use unsigned. While I don't think this ever resulted in real issues I think we should be more careful there and follow a stricter regime: unless there's a strong reason not to use size_t for array sizes and indexes, size_t it should be. Any allocations we do ultimately will use size_t anyway, and converting forth and back between unsigned and size_t will always be a source of problems. Note that on 32bit machines "unsigned" and "size_t" are equivalent, and on 64bit machines our arrays shouldn't grow that large anyway, and if they do we have a problem, however that kind of overly large allocation we have protections for usually, but for overflows we do not have that so much, hence let's add it. So yeah, it's a story of the current code being already "good enough", but I think some extra type hygiene is better. This patch tries to be comprehensive, but it probably isn't and I missed a few cases. But I guess we can cover that later as we notice it. Among smaller fixes, this changes: 1. strv_length()' return type becomes size_t 2. the unit file changes array size becomes size_t 3. DNS answer and query array sizes become size_t Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
* tree-wide: drop license boilerplateZbigniew Jędrzejewski-Szmek2018-04-061-13/+0
| | | | | | | | | | Files which are installed as-is (any .service and other unit files, .conf files, .policy files, etc), are left as is. My assumption is that SPDX identifiers are not yet that well known, so it's better to retain the extended header to avoid any doubt. I also kept any copyright lines. We can probably remove them, but it'd nice to obtain explicit acks from all involved authors before doing that.
* sd-bus: cleanup ssh sessions (Closes: #8076)Shawn Landden2018-02-081-0/+1
| | | | | | | | | | | | | | | | | | | 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()
* Add support for SD_BUS_DEFAULT*Nathaniel McCallum2018-01-231-0/+2
| | | | | | | | | | | | | | | | | | | | | | 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.
* sd-bus: add API to optionally set a sender field on all outgoing messagesLennart Poettering2018-01-051-0/+1
| | | | | | | | | | 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.
* sd-bus: log about bus state changesLennart Poettering2018-01-051-1/+4
| | | | | Let's unify all state changes in a new helper function, from which we can then debug log all state changes
* sd-bus: add new sd_bus_set_connected_signal() APILennart Poettering2018-01-051-0/+1
| | | | | | | | | | | | | | | | | | 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.
* sd-bus: add asynchronous version of sd_bus_match()Lennart Poettering2018-01-051-0/+3
| | | | | | | 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.
* sd-bus: remove bus_remove_match_by_string() helper which is unusedLennart Poettering2018-01-051-2/+0
|
* sd-bus: add APIs to request/release names asynchronouslyLennart Poettering2018-01-051-0/+2
| | | | | | | | | | | 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.
* sd-bus: get rid of kdbus flags cruftLennart Poettering2018-01-051-65/+3
| | | | | We only need three bits from the old kdbus flags cruft, hence let's make them proper booleans.
* sd-bus: start reply callback timeouts only when the connection is establishedLennart Poettering2018-01-051-5/+5
| | | | | | | | | | | | | | | | | | | | | | | | 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...
* sd-bus: optionally, use inotify to wait for bus sockets to appearLennart Poettering2018-01-051-0/+13
| | | | | | | | | | | 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.
* sd-bus: use SO_PEERGROUPS when available to identify groups of peerLennart Poettering2018-01-041-0/+2
|