Bug Summary

File:build-scan/../src/libsystemd/sd-bus/sd-bus.c
Warning:line 3804, column 45
Dereference of null pointer

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name sd-bus.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -fdenormal-fp-math=preserve-sign,preserve-sign -ffp-contract=fast -fno-rounding-math -ffast-math -ffinite-math-only -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/12.0.0 -include config.h -I src/libsystemd/libsystemd_static.a.p -I src/libsystemd -I ../src/libsystemd -I src/basic -I ../src/basic -I src/shared -I ../src/shared -I src/systemd -I ../src/systemd -I src/journal -I ../src/journal -I src/journal-remote -I ../src/journal-remote -I src/nspawn -I ../src/nspawn -I src/resolve -I ../src/resolve -I src/timesync -I ../src/timesync -I ../src/time-wait-sync -I src/login -I ../src/login -I src/udev -I ../src/udev -I src/libudev -I ../src/libudev -I src/core -I ../src/core -I ../src/libsystemd/sd-bus -I ../src/libsystemd/sd-device -I ../src/libsystemd/sd-hwdb -I ../src/libsystemd/sd-id128 -I ../src/libsystemd/sd-netlink -I ../src/libsystemd/sd-network -I src/libsystemd-network -I ../src/libsystemd-network -I . -I .. -D _FILE_OFFSET_BITS=64 -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/12.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wwrite-strings -Wno-unused-parameter -Wno-missing-field-initializers -Wno-unused-result -Wno-format-signedness -Wno-error=nonnull -std=gnu99 -fconst-strings -fdebug-compilation-dir /home/mrc0mmand/repos/@redhat-plumbers/systemd-rhel8/build-scan -ferror-limit 19 -fvisibility default -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-07-16-221226-1465241-1 -x c ../src/libsystemd/sd-bus/sd-bus.c
1/* SPDX-License-Identifier: LGPL-2.1+ */
2/***
3***/
4
5#include <endian.h>
6#include <netdb.h>
7#include <poll.h>
8#include <pthread.h>
9#include <signal.h>
10#include <stdlib.h>
11#include <sys/mman.h>
12#include <sys/wait.h>
13#include <unistd.h>
14
15#include "sd-bus.h"
16
17#include "alloc-util.h"
18#include "bus-container.h"
19#include "bus-control.h"
20#include "bus-internal.h"
21#include "bus-kernel.h"
22#include "bus-label.h"
23#include "bus-message.h"
24#include "bus-objects.h"
25#include "bus-protocol.h"
26#include "bus-slot.h"
27#include "bus-socket.h"
28#include "bus-track.h"
29#include "bus-type.h"
30#include "bus-util.h"
31#include "cgroup-util.h"
32#include "def.h"
33#include "fd-util.h"
34#include "hexdecoct.h"
35#include "hostname-util.h"
36#include "macro.h"
37#include "missing.h"
38#include "parse-util.h"
39#include "process-util.h"
40#include "string-util.h"
41#include "strv.h"
42#include "util.h"
43
44#define log_debug_bus_message(m)do { sd_bus_message *_mm = (m); ({ int _level = (((7))), _e =
((0)), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm
(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((
_realm) << 10 | (_level)), _e, "../src/libsystemd/sd-bus/sd-bus.c"
, 44, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); }); } while (0)
\
45 do { \
46 sd_bus_message *_mm = (m); \
47 log_debug("Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " signature=%s error-name=%s error-message=%s", \({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
48 bus_message_type_to_string(_mm->header->type), \({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
49 strna(sd_bus_message_get_sender(_mm)), \({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
50 strna(sd_bus_message_get_destination(_mm)), \({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
51 strna(sd_bus_message_get_path(_mm)), \({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
52 strna(sd_bus_message_get_interface(_mm)), \({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
53 strna(sd_bus_message_get_member(_mm)), \({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
54 BUS_MESSAGE_COOKIE(_mm), \({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
55 _mm->reply_cookie, \({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
56 strna(_mm->root_container.signature), \({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
57 strna(_mm->error.name), \({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
58 strna(_mm->error.message))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 58, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); })
; \
59 } while (false0)
60
61static int bus_poll(sd_bus *bus, bool_Bool need_more, uint64_t timeout_usec);
62static void bus_detach_io_events(sd_bus *b);
63static void bus_detach_inotify_event(sd_bus *b);
64
65static thread_local__thread sd_bus *default_system_bus = NULL((void*)0);
66static thread_local__thread sd_bus *default_user_bus = NULL((void*)0);
67static thread_local__thread sd_bus *default_starter_bus = NULL((void*)0);
68
69static sd_bus **bus_choose_default(int (**bus_open)(sd_bus **)) {
70 const char *e;
71
72 /* Let's try our best to reuse another cached connection. If
73 * the starter bus type is set, connect via our normal
74 * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
75 * we can share the connection with the user/system default
76 * bus. */
77
78 e = secure_getenv("DBUS_STARTER_BUS_TYPE");
79 if (e) {
80 if (streq(e, "system")(strcmp((e),("system")) == 0)) {
81 if (bus_open)
82 *bus_open = sd_bus_open_system;
83 return &default_system_bus;
84 } else if (STR_IN_SET(e, "user", "session")(!!strv_find((((char**) ((const char*[]) { "user", "session",
((void*)0) }))), (e)))
) {
85 if (bus_open)
86 *bus_open = sd_bus_open_user;
87 return &default_user_bus;
88 }
89 }
90
91 /* No type is specified, so we have not other option than to
92 * use the starter address if it is set. */
93 e = secure_getenv("DBUS_STARTER_ADDRESS");
94 if (e) {
95 if (bus_open)
96 *bus_open = sd_bus_open;
97 return &default_starter_bus;
98 }
99
100 /* Finally, if nothing is set use the cached connection for
101 * the right scope */
102
103 if (cg_pid_get_owner_uid(0, NULL((void*)0)) >= 0) {
104 if (bus_open)
105 *bus_open = sd_bus_open_user;
106 return &default_user_bus;
107 } else {
108 if (bus_open)
109 *bus_open = sd_bus_open_system;
110 return &default_system_bus;
111 }
112}
113
114sd_bus *bus_resolve(sd_bus *bus) {
115 switch ((uintptr_t) bus) {
116 case (uintptr_t) SD_BUS_DEFAULT((sd_bus *) 1):
117 return *(bus_choose_default(NULL((void*)0)));
118 case (uintptr_t) SD_BUS_DEFAULT_USER((sd_bus *) 2):
119 return default_user_bus;
120 case (uintptr_t) SD_BUS_DEFAULT_SYSTEM((sd_bus *) 3):
121 return default_system_bus;
122 default:
123 return bus;
124 }
125}
126
127void bus_close_io_fds(sd_bus *b) {
128 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 128, __PRETTY_FUNCTION__); } while (0)
;
129
130 bus_detach_io_events(b);
131
132 if (b->input_fd != b->output_fd)
133 safe_close(b->output_fd);
134 b->output_fd = b->input_fd = safe_close(b->input_fd);
135}
136
137void bus_close_inotify_fd(sd_bus *b) {
138 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 138, __PRETTY_FUNCTION__); } while (0)
;
139
140 bus_detach_inotify_event(b);
141
142 b->inotify_fd = safe_close(b->inotify_fd);
143 b->inotify_watches = mfree(b->inotify_watches);
144 b->n_inotify_watches = 0;
145}
146
147static void bus_reset_queues(sd_bus *b) {
148 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 148, __PRETTY_FUNCTION__); } while (0)
;
149
150 while (b->rqueue_size > 0)
151 bus_message_unref_queued(b->rqueue[--b->rqueue_size], b);
152
153 b->rqueue = mfree(b->rqueue);
154 b->rqueue_allocated = 0;
155
156 while (b->wqueue_size > 0)
157 bus_message_unref_queued(b->wqueue[--b->wqueue_size], b);
158
159 b->wqueue = mfree(b->wqueue);
160 b->wqueue_allocated = 0;
161}
162
163static sd_bus* bus_free(sd_bus *b) {
164 sd_bus_slot *s;
165
166 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 166, __PRETTY_FUNCTION__); } while (0)
;
167 assert(!b->track_queue)do { if ((__builtin_expect(!!(!(!b->track_queue)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("!b->track_queue"), "../src/libsystemd/sd-bus/sd-bus.c"
, 167, __PRETTY_FUNCTION__); } while (0)
;
168 assert(!b->tracks)do { if ((__builtin_expect(!!(!(!b->tracks)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("!b->tracks"), "../src/libsystemd/sd-bus/sd-bus.c"
, 168, __PRETTY_FUNCTION__); } while (0)
;
169
170 b->state = BUS_CLOSED;
171
172 sd_bus_detach_event(b);
173
174 while ((s = b->slots)) {
175 /* At this point only floating slots can still be
176 * around, because the non-floating ones keep a
177 * reference to the bus, and we thus couldn't be
178 * destructing right now... We forcibly disconnect the
179 * slots here, so that they still can be referenced by
180 * apps, but are dead. */
181
182 assert(s->floating)do { if ((__builtin_expect(!!(!(s->floating)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("s->floating"), "../src/libsystemd/sd-bus/sd-bus.c"
, 182, __PRETTY_FUNCTION__); } while (0)
;
183 bus_slot_disconnect(s);
184 sd_bus_slot_unref(s);
185 }
186
187 if (b->default_bus_ptr)
188 *b->default_bus_ptr = NULL((void*)0);
189
190 bus_close_io_fds(b);
191 bus_close_inotify_fd(b);
192
193 free(b->label);
194 free(b->groups);
195 free(b->rbuffer);
196 free(b->unique_name);
197 free(b->auth_buffer);
198 free(b->address);
199 free(b->machine);
200 free(b->cgroup_root);
201 free(b->description);
202 free(b->patch_sender);
203
204 free(b->exec_path);
205 strv_free(b->exec_argv);
206
207 close_many(b->fds, b->n_fds);
208 free(b->fds);
209
210 bus_reset_queues(b);
211
212 ordered_hashmap_free_free(b->reply_callbacks);
213 prioq_free(b->reply_callbacks_prioq);
214
215 assert(b->match_callbacks.type == BUS_MATCH_ROOT)do { if ((__builtin_expect(!!(!(b->match_callbacks.type ==
BUS_MATCH_ROOT)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("b->match_callbacks.type == BUS_MATCH_ROOT"), "../src/libsystemd/sd-bus/sd-bus.c"
, 215, __PRETTY_FUNCTION__); } while (0)
;
216 bus_match_free(&b->match_callbacks);
217
218 hashmap_free_free(b->vtable_methods);
219 hashmap_free_free(b->vtable_properties);
220
221 assert(hashmap_isempty(b->nodes))do { if ((__builtin_expect(!!(!(hashmap_isempty(b->nodes))
),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("hashmap_isempty(b->nodes)"
), "../src/libsystemd/sd-bus/sd-bus.c", 221, __PRETTY_FUNCTION__
); } while (0)
;
222 hashmap_free(b->nodes);
223
224 bus_flush_memfd(b);
225
226 assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0)do { if ((__builtin_expect(!!(!(pthread_mutex_destroy(&b->
memfd_cache_mutex) == 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("pthread_mutex_destroy(&b->memfd_cache_mutex) == 0"
), "../src/libsystemd/sd-bus/sd-bus.c", 226, __PRETTY_FUNCTION__
); } while (0)
;
227
228 return mfree(b);
229}
230
231DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, bus_free)static inline void bus_freep(sd_bus* *p) { if (*p) bus_free(*
p); }
;
232
233_public___attribute__ ((visibility("default"))) int sd_bus_new(sd_bus **ret) {
234 _cleanup_free___attribute__((cleanup(freep))) sd_bus *b = NULL((void*)0);
235
236 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/sd-bus.c"
, 236, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
237
238 b = new0(sd_bus, 1)((sd_bus*) calloc((1), sizeof(sd_bus)));
239 if (!b)
240 return -ENOMEM12;
241
242 b->n_ref = REFCNT_INIT((RefCount) { ._value = 1 });
243 b->input_fd = b->output_fd = -1;
244 b->inotify_fd = -1;
245 b->message_version = 1;
246 b->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
247 b->accept_fd = true1;
248 b->original_pid = getpid_cached();
249 b->n_groups = (size_t) -1;
250
251 /* We guarantee that wqueue always has space for at least one entry */
252 if (!GREEDY_REALLOC(b->wqueue, b->wqueue_allocated, 1)greedy_realloc((void**) &(b->wqueue), &(b->wqueue_allocated
), (1), sizeof((b->wqueue)[0]))
)
253 return -ENOMEM12;
254
255 assert_se(pthread_mutex_init(&b->memfd_cache_mutex, NULL) == 0)do { if ((__builtin_expect(!!(!(pthread_mutex_init(&b->
memfd_cache_mutex, ((void*)0)) == 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("pthread_mutex_init(&b->memfd_cache_mutex, NULL) == 0"
), "../src/libsystemd/sd-bus/sd-bus.c", 255, __PRETTY_FUNCTION__
); } while (0)
;
256
257 *ret = TAKE_PTR(b)({ typeof(b) _ptr_ = (b); (b) = ((void*)0); _ptr_; });
258 return 0;
259}
260
261_public___attribute__ ((visibility("default"))) int sd_bus_set_address(sd_bus *bus, const char *address) {
262 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 262, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
263 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 263, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
264 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 264, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
265 assert_return(address, -EINVAL)do { if (!(((__builtin_expect(!!(address),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("address"), "../src/libsystemd/sd-bus/sd-bus.c"
, 265, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
266 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 266, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
267
268 return free_and_strdup(&bus->address, address);
269}
270
271_public___attribute__ ((visibility("default"))) int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
272 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 272, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
273 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 273, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
274 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 274, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
275 assert_return(input_fd >= 0, -EBADF)do { if (!(((__builtin_expect(!!(input_fd >= 0),1))) ? (1)
: (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("input_fd >= 0"
), "../src/libsystemd/sd-bus/sd-bus.c", 275, __PRETTY_FUNCTION__
), 0))) return (-9); } while (0)
;
276 assert_return(output_fd >= 0, -EBADF)do { if (!(((__builtin_expect(!!(output_fd >= 0),1))) ? (1
) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("output_fd >= 0"
), "../src/libsystemd/sd-bus/sd-bus.c", 276, __PRETTY_FUNCTION__
), 0))) return (-9); } while (0)
;
277 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 277, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
278
279 bus->input_fd = input_fd;
280 bus->output_fd = output_fd;
281 return 0;
282}
283
284_public___attribute__ ((visibility("default"))) int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
285 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **a = NULL((void*)0);
286 int r;
287
288 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 288, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
289 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 289, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
290 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 290, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
291 assert_return(path, -EINVAL)do { if (!(((__builtin_expect(!!(path),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("path"), "../src/libsystemd/sd-bus/sd-bus.c"
, 291, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
292 assert_return(!strv_isempty(argv), -EINVAL)do { if (!(((__builtin_expect(!!(!strv_isempty(argv)),1))) ? (
1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!strv_isempty(argv)"
), "../src/libsystemd/sd-bus/sd-bus.c", 292, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
293 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 293, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
294
295 a = strv_copy(argv);
296 if (!a)
297 return -ENOMEM12;
298
299 r = free_and_strdup(&bus->exec_path, path);
300 if (r < 0)
301 return r;
302
303 return strv_free_and_replace(bus->exec_argv, a)({ strv_free(bus->exec_argv); (bus->exec_argv) = (a); (
a) = ((void*)0); 0; })
;
304}
305
306_public___attribute__ ((visibility("default"))) int sd_bus_set_bus_client(sd_bus *bus, int b) {
307 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 307, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
308 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 308, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
309 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 309, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
310 assert_return(!bus->patch_sender, -EPERM)do { if (!(((__builtin_expect(!!(!bus->patch_sender),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus->patch_sender"
), "../src/libsystemd/sd-bus/sd-bus.c", 310, __PRETTY_FUNCTION__
), 0))) return (-1); } while (0)
;
311 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 311, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
312
313 bus->bus_client = !!b;
314 return 0;
315}
316
317_public___attribute__ ((visibility("default"))) int sd_bus_set_monitor(sd_bus *bus, int b) {
318 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 318, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
319 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 319, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
320 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 320, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
321 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 321, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
322
323 bus->is_monitor = !!b;
324 return 0;
325}
326
327_public___attribute__ ((visibility("default"))) int sd_bus_negotiate_fds(sd_bus *bus, int b) {
328 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 328, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
329 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 329, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
330 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 330, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
331 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 331, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
332
333 bus->accept_fd = !!b;
334 return 0;
335}
336
337_public___attribute__ ((visibility("default"))) int sd_bus_negotiate_timestamp(sd_bus *bus, int b) {
338 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 338, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
339 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 339, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
340 assert_return(!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED), -EPERM)do { if (!(((__builtin_expect(!!(!({ _Bool _found = 0; static
__attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){BUS_CLOSING, BUS_CLOSED})/sizeof(int)]; switch
(bus->state) { case BUS_CLOSING: case BUS_CLOSED: _found =
1; break; default: break; } _found; })),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED)"
), "../src/libsystemd/sd-bus/sd-bus.c", 340, __PRETTY_FUNCTION__
), 0))) return (-1); } while (0)
;
341 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 341, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
342
343 /* This is not actually supported by any of our transports these days, but we do honour it for synthetic
344 * replies, and maybe one day classic D-Bus learns this too */
345 bus->attach_timestamp = !!b;
346
347 return 0;
348}
349
350_public___attribute__ ((visibility("default"))) int sd_bus_negotiate_creds(sd_bus *bus, int b, uint64_t mask) {
351 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 351, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
352 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 352, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
353 assert_return(mask <= _SD_BUS_CREDS_ALL, -EINVAL)do { if (!(((__builtin_expect(!!(mask <= _SD_BUS_CREDS_ALL
),1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("mask <= _SD_BUS_CREDS_ALL"), "../src/libsystemd/sd-bus/sd-bus.c"
, 353, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
354 assert_return(!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED), -EPERM)do { if (!(((__builtin_expect(!!(!({ _Bool _found = 0; static
__attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){BUS_CLOSING, BUS_CLOSED})/sizeof(int)]; switch
(bus->state) { case BUS_CLOSING: case BUS_CLOSED: _found =
1; break; default: break; } _found; })),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED)"
), "../src/libsystemd/sd-bus/sd-bus.c", 354, __PRETTY_FUNCTION__
), 0))) return (-1); } while (0)
;
355 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 355, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
356
357 SET_FLAG(bus->creds_mask, mask, b)(bus->creds_mask) = (b) ? ((bus->creds_mask) | (mask)) :
((bus->creds_mask) & ~(mask))
;
358
359 /* The well knowns we need unconditionally, so that matches can work */
360 bus->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
361
362 return 0;
363}
364
365_public___attribute__ ((visibility("default"))) int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
366 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 366, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
367 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 367, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
368 assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL)do { if (!(((__builtin_expect(!!(b || sd_id128_equal(server_id
, ((const sd_id128_t) { .qwords = { 0, 0 }}))),1))) ? (1) : (
log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("b || sd_id128_equal(server_id, SD_ID128_NULL)"
), "../src/libsystemd/sd-bus/sd-bus.c", 368, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
369 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 369, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
370 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 370, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
371
372 bus->is_server = !!b;
373 bus->server_id = server_id;
374 return 0;
375}
376
377_public___attribute__ ((visibility("default"))) int sd_bus_set_anonymous(sd_bus *bus, int b) {
378 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 378, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
379 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 379, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
380 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 380, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
381 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 381, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
382
383 bus->anonymous_auth = !!b;
384 return 0;
385}
386
387_public___attribute__ ((visibility("default"))) int sd_bus_set_trusted(sd_bus *bus, int b) {
388 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 388, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
389 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 389, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
390 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 390, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
391 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 391, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
392
393 bus->trusted = !!b;
394 return 0;
395}
396
397_public___attribute__ ((visibility("default"))) int sd_bus_set_description(sd_bus *bus, const char *description) {
398 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 398, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
399 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 399, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
400 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 400, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
401 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 401, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
402
403 return free_and_strdup(&bus->description, description);
404}
405
406_public___attribute__ ((visibility("default"))) int sd_bus_set_allow_interactive_authorization(sd_bus *bus, int b) {
407 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 407, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
408 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 408, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
409 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 409, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
410
411 bus->allow_interactive_authorization = !!b;
412 return 0;
413}
414
415_public___attribute__ ((visibility("default"))) int sd_bus_get_allow_interactive_authorization(sd_bus *bus) {
416 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 416, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
417 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 417, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
418 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 418, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
419
420 return bus->allow_interactive_authorization;
421}
422
423_public___attribute__ ((visibility("default"))) int sd_bus_set_watch_bind(sd_bus *bus, int b) {
424 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 424, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
425 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 425, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
426 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 426, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
427 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 427, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
428
429 bus->watch_bind = !!b;
430 return 0;
431}
432
433_public___attribute__ ((visibility("default"))) int sd_bus_get_watch_bind(sd_bus *bus) {
434 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 434, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
435 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 435, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
436 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 436, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
437
438 return bus->watch_bind;
439}
440
441_public___attribute__ ((visibility("default"))) int sd_bus_set_connected_signal(sd_bus *bus, int b) {
442 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 442, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
443 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 443, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
444 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 444, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
445 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 445, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
446
447 bus->connected_signal = !!b;
448 return 0;
449}
450
451_public___attribute__ ((visibility("default"))) int sd_bus_get_connected_signal(sd_bus *bus) {
452 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 452, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
453 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 453, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
454 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 454, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
455
456 return bus->connected_signal;
457}
458
459static int synthesize_connected_signal(sd_bus *bus) {
460 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
461 int r;
462
463 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 463, __PRETTY_FUNCTION__); } while (0)
;
464
465 /* If enabled, synthesizes a local "Connected" signal mirroring the local "Disconnected" signal. This is called
466 * whenever we fully established a connection, i.e. after the authorization phase, and after receiving the
467 * Hello() reply. Or in other words, whenver we enter BUS_RUNNING state.
468 *
469 * This is useful so that clients can start doing stuff whenver the connection is fully established in a way
470 * that works independently from whether we connected to a full bus or just a direct connection. */
471
472 if (!bus->connected_signal)
473 return 0;
474
475 r = sd_bus_message_new_signal(
476 bus,
477 &m,
478 "/org/freedesktop/DBus/Local",
479 "org.freedesktop.DBus.Local",
480 "Connected");
481 if (r < 0)
482 return r;
483
484 bus_message_set_sender_local(bus, m);
485
486 r = bus_seal_synthetic_message(bus, m);
487 if (r < 0)
488 return r;
489
490 r = bus_rqueue_make_room(bus);
491 if (r < 0)
492 return r;
493
494 /* Insert at the very front */
495 memmove(bus->rqueue + 1, bus->rqueue, sizeof(sd_bus_message*) * bus->rqueue_size);
496 bus->rqueue[0] = bus_message_ref_queued(m, bus);
497 bus->rqueue_size++;
498
499 return 0;
500}
501
502void bus_set_state(sd_bus *bus, enum bus_state state) {
503
504 static const char * const table[_BUS_STATE_MAX] = {
505 [BUS_UNSET] = "UNSET",
506 [BUS_WATCH_BIND] = "WATCH_BIND",
507 [BUS_OPENING] = "OPENING",
508 [BUS_AUTHENTICATING] = "AUTHENTICATING",
509 [BUS_HELLO] = "HELLO",
510 [BUS_RUNNING] = "RUNNING",
511 [BUS_CLOSING] = "CLOSING",
512 [BUS_CLOSED] = "CLOSED",
513 };
514
515 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 515, __PRETTY_FUNCTION__); } while (0)
;
516 assert(state < _BUS_STATE_MAX)do { if ((__builtin_expect(!!(!(state < _BUS_STATE_MAX)),0
))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("state < _BUS_STATE_MAX"
), "../src/libsystemd/sd-bus/sd-bus.c", 516, __PRETTY_FUNCTION__
); } while (0)
;
517
518 if (state == bus->state)
519 return;
520
521 log_debug("Bus %s: changing state %s → %s", strna(bus->description), table[bus->state], table[state])({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 521, __func__, "Bus %s: changing state %s → %s"
, strna(bus->description), table[bus->state], table[state
]) : -abs(_e); })
;
522 bus->state = state;
523}
524
525static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
526 const char *s;
527 sd_bus *bus;
528 int r;
529
530 assert(reply)do { if ((__builtin_expect(!!(!(reply)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("reply"), "../src/libsystemd/sd-bus/sd-bus.c"
, 530, __PRETTY_FUNCTION__); } while (0)
;
531 bus = reply->bus;
532 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 532, __PRETTY_FUNCTION__); } while (0)
;
533 assert(IN_SET(bus->state, BUS_HELLO, BUS_CLOSING))do { if ((__builtin_expect(!!(!(({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){BUS_HELLO, BUS_CLOSING})/sizeof(int)]; switch
(bus->state) { case BUS_HELLO: case BUS_CLOSING: _found = 1
; break; default: break; } _found; }))),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("IN_SET(bus->state, BUS_HELLO, BUS_CLOSING)"
), "../src/libsystemd/sd-bus/sd-bus.c", 533, __PRETTY_FUNCTION__
); } while (0)
;
534
535 r = sd_bus_message_get_errno(reply);
536 if (r > 0)
537 return -r;
538
539 r = sd_bus_message_read(reply, "s", &s);
540 if (r < 0)
541 return r;
542
543 if (!service_name_is_valid(s) || s[0] != ':')
544 return -EBADMSG74;
545
546 r = free_and_strdup(&bus->unique_name, s);
547 if (r < 0)
548 return r;
549
550 if (bus->state == BUS_HELLO) {
551 bus_set_state(bus, BUS_RUNNING);
552
553 r = synthesize_connected_signal(bus);
554 if (r < 0)
555 return r;
556 }
557
558 return 1;
559}
560
561static int bus_send_hello(sd_bus *bus) {
562 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
563 int r;
564
565 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 565, __PRETTY_FUNCTION__); } while (0)
;
566
567 if (!bus->bus_client)
568 return 0;
569
570 r = sd_bus_message_new_method_call(
571 bus,
572 &m,
573 "org.freedesktop.DBus",
574 "/org/freedesktop/DBus",
575 "org.freedesktop.DBus",
576 "Hello");
577 if (r < 0)
578 return r;
579
580 return sd_bus_call_async(bus, NULL((void*)0), m, hello_callback, NULL((void*)0), 0);
581}
582
583int bus_start_running(sd_bus *bus) {
584 struct reply_callback *c;
585 Iterator i;
586 usec_t n;
587 int r;
588
589 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 589, __PRETTY_FUNCTION__); } while (0)
;
590 assert(bus->state < BUS_HELLO)do { if ((__builtin_expect(!!(!(bus->state < BUS_HELLO)
),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("bus->state < BUS_HELLO"
), "../src/libsystemd/sd-bus/sd-bus.c", 590, __PRETTY_FUNCTION__
); } while (0)
;
591
592 /* We start all method call timeouts when we enter BUS_HELLO or BUS_RUNNING mode. At this point let's convert
593 * all relative to absolute timestamps. Note that we do not reshuffle the reply callback priority queue since
594 * adding a fixed value to all entries should not alter the internal order. */
595
596 n = now(CLOCK_MONOTONIC1);
597 ORDERED_HASHMAP_FOREACH(c, bus->reply_callbacks, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); ordered_hashmap_iterate((bus->reply_callbacks
), &(i), (void**)&(c), ((void*)0)); )
{
598 if (c->timeout_usec == 0)
599 continue;
600
601 c->timeout_usec = usec_add(n, c->timeout_usec);
602 }
603
604 if (bus->bus_client) {
605 bus_set_state(bus, BUS_HELLO);
606 return 1;
607 }
608
609 bus_set_state(bus, BUS_RUNNING);
610
611 r = synthesize_connected_signal(bus);
612 if (r < 0)
613 return r;
614
615 return 1;
616}
617
618static int parse_address_key(const char **p, const char *key, char **value) {
619 size_t l, n = 0, allocated = 0;
620 _cleanup_free___attribute__((cleanup(freep))) char *r = NULL((void*)0);
621 const char *a;
622
623 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 623, __PRETTY_FUNCTION__); } while (0)
;
624 assert(*p)do { if ((__builtin_expect(!!(!(*p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("*p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 624, __PRETTY_FUNCTION__); } while (0)
;
625 assert(value)do { if ((__builtin_expect(!!(!(value)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("value"), "../src/libsystemd/sd-bus/sd-bus.c"
, 625, __PRETTY_FUNCTION__); } while (0)
;
626
627 if (key) {
628 l = strlen(key);
629 if (strncmp(*p, key, l) != 0)
630 return 0;
631
632 if ((*p)[l] != '=')
633 return 0;
634
635 if (*value)
636 return -EINVAL22;
637
638 a = *p + l + 1;
639 } else
640 a = *p;
641
642 while (!IN_SET(*a, ';', ',', 0)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){';', ',', 0})/sizeof(int)]; switch(*a) {
case ';': case ',': case 0: _found = 1; break; default: break
; } _found; })
) {
643 char c;
644
645 if (*a == '%') {
646 int x, y;
647
648 x = unhexchar(a[1]);
649 if (x < 0)
650 return x;
651
652 y = unhexchar(a[2]);
653 if (y < 0)
654 return y;
655
656 c = (char) ((x << 4) | y);
657 a += 3;
658 } else {
659 c = *a;
660 a++;
661 }
662
663 if (!GREEDY_REALLOC(r, allocated, n + 2)greedy_realloc((void**) &(r), &(allocated), (n + 2), sizeof
((r)[0]))
)
664 return -ENOMEM12;
665
666 r[n++] = c;
667 }
668
669 if (!r) {
670 r = strdup("");
671 if (!r)
672 return -ENOMEM12;
673 } else
674 r[n] = 0;
675
676 if (*a == ',')
677 a++;
678
679 *p = a;
680
681 free_and_replace(*value, r)({ free(*value); (*value) = (r); (r) = ((void*)0); 0; });
682
683 return 1;
684}
685
686static void skip_address_key(const char **p) {
687 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 687, __PRETTY_FUNCTION__); } while (0)
;
688 assert(*p)do { if ((__builtin_expect(!!(!(*p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("*p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 688, __PRETTY_FUNCTION__); } while (0)
;
689
690 *p += strcspn(*p, ",");
691
692 if (**p == ',')
693 (*p)++;
694}
695
696static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
697 _cleanup_free___attribute__((cleanup(freep))) char *path = NULL((void*)0), *abstract = NULL((void*)0);
698 size_t l;
699 int r;
700
701 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 701, __PRETTY_FUNCTION__); } while (0)
;
702 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 702, __PRETTY_FUNCTION__); } while (0)
;
703 assert(*p)do { if ((__builtin_expect(!!(!(*p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("*p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 703, __PRETTY_FUNCTION__); } while (0)
;
704 assert(guid)do { if ((__builtin_expect(!!(!(guid)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("guid"), "../src/libsystemd/sd-bus/sd-bus.c"
, 704, __PRETTY_FUNCTION__); } while (0)
;
705
706 while (!IN_SET(**p, 0, ';')({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){0, ';'})/sizeof(int)]; switch(**p) { case
0: case ';': _found = 1; break; default: break; } _found; })
) {
707 r = parse_address_key(p, "guid", guid);
708 if (r < 0)
709 return r;
710 else if (r > 0)
711 continue;
712
713 r = parse_address_key(p, "path", &path);
714 if (r < 0)
715 return r;
716 else if (r > 0)
717 continue;
718
719 r = parse_address_key(p, "abstract", &abstract);
720 if (r < 0)
721 return r;
722 else if (r > 0)
723 continue;
724
725 skip_address_key(p);
726 }
727
728 if (!path && !abstract)
729 return -EINVAL22;
730
731 if (path && abstract)
732 return -EINVAL22;
733
734 if (path) {
735 l = strlen(path);
736 if (l > sizeof(b->sockaddr.un.sun_path))
737 return -E2BIG7;
738
739 b->sockaddr.un.sun_family = AF_UNIX1;
740 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
741 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path)__builtin_offsetof(struct sockaddr_un, sun_path) + l;
742 } else if (abstract) {
743 l = strlen(abstract);
744 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
745 return -E2BIG7;
746
747 b->sockaddr.un.sun_family = AF_UNIX1;
748 b->sockaddr.un.sun_path[0] = 0;
749 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
750 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path)__builtin_offsetof(struct sockaddr_un, sun_path) + 1 + l;
751 }
752
753 b->is_local = true1;
754
755 return 0;
756}
757
758static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
759 _cleanup_free___attribute__((cleanup(freep))) char *host = NULL((void*)0), *port = NULL((void*)0), *family = NULL((void*)0);
760 int r;
761 struct addrinfo *result, hints = {
762 .ai_socktype = SOCK_STREAMSOCK_STREAM,
763 .ai_flags = AI_ADDRCONFIG0x0020,
764 };
765
766 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 766, __PRETTY_FUNCTION__); } while (0)
;
767 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 767, __PRETTY_FUNCTION__); } while (0)
;
768 assert(*p)do { if ((__builtin_expect(!!(!(*p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("*p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 768, __PRETTY_FUNCTION__); } while (0)
;
769 assert(guid)do { if ((__builtin_expect(!!(!(guid)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("guid"), "../src/libsystemd/sd-bus/sd-bus.c"
, 769, __PRETTY_FUNCTION__); } while (0)
;
770
771 while (!IN_SET(**p, 0, ';')({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){0, ';'})/sizeof(int)]; switch(**p) { case
0: case ';': _found = 1; break; default: break; } _found; })
) {
772 r = parse_address_key(p, "guid", guid);
773 if (r < 0)
774 return r;
775 else if (r > 0)
776 continue;
777
778 r = parse_address_key(p, "host", &host);
779 if (r < 0)
780 return r;
781 else if (r > 0)
782 continue;
783
784 r = parse_address_key(p, "port", &port);
785 if (r < 0)
786 return r;
787 else if (r > 0)
788 continue;
789
790 r = parse_address_key(p, "family", &family);
791 if (r < 0)
792 return r;
793 else if (r > 0)
794 continue;
795
796 skip_address_key(p);
797 }
798
799 if (!host || !port)
800 return -EINVAL22;
801
802 if (family) {
803 if (streq(family, "ipv4")(strcmp((family),("ipv4")) == 0))
804 hints.ai_family = AF_INET2;
805 else if (streq(family, "ipv6")(strcmp((family),("ipv6")) == 0))
806 hints.ai_family = AF_INET610;
807 else
808 return -EINVAL22;
809 }
810
811 r = getaddrinfo(host, port, &hints, &result);
812 if (r == EAI_SYSTEM-11)
813 return -errno(*__errno_location ());
814 else if (r != 0)
815 return -EADDRNOTAVAIL99;
816
817 memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
818 b->sockaddr_size = result->ai_addrlen;
819
820 freeaddrinfo(result);
821
822 b->is_local = false0;
823
824 return 0;
825}
826
827static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
828 char *path = NULL((void*)0);
829 unsigned n_argv = 0, j;
830 char **argv = NULL((void*)0);
831 size_t allocated = 0;
832 int r;
833
834 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 834, __PRETTY_FUNCTION__); } while (0)
;
835 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 835, __PRETTY_FUNCTION__); } while (0)
;
836 assert(*p)do { if ((__builtin_expect(!!(!(*p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("*p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 836, __PRETTY_FUNCTION__); } while (0)
;
837 assert(guid)do { if ((__builtin_expect(!!(!(guid)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("guid"), "../src/libsystemd/sd-bus/sd-bus.c"
, 837, __PRETTY_FUNCTION__); } while (0)
;
838
839 while (!IN_SET(**p, 0, ';')({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){0, ';'})/sizeof(int)]; switch(**p) { case
0: case ';': _found = 1; break; default: break; } _found; })
) {
840 r = parse_address_key(p, "guid", guid);
841 if (r < 0)
842 goto fail;
843 else if (r > 0)
844 continue;
845
846 r = parse_address_key(p, "path", &path);
847 if (r < 0)
848 goto fail;
849 else if (r > 0)
850 continue;
851
852 if (startswith(*p, "argv")) {
853 unsigned ul;
854
855 errno(*__errno_location ()) = 0;
856 ul = strtoul(*p + 4, (char**) p, 10);
857 if (errno(*__errno_location ()) > 0 || **p != '=' || ul > 256) {
858 r = -EINVAL22;
859 goto fail;
860 }
861
862 (*p)++;
863
864 if (ul >= n_argv) {
865 if (!GREEDY_REALLOC0(argv, allocated, ul + 2)greedy_realloc0((void**) &(argv), &(allocated), (ul +
2), sizeof((argv)[0]))
) {
866 r = -ENOMEM12;
867 goto fail;
868 }
869
870 n_argv = ul + 1;
871 }
872
873 r = parse_address_key(p, NULL((void*)0), argv + ul);
874 if (r < 0)
875 goto fail;
876
877 continue;
878 }
879
880 skip_address_key(p);
881 }
882
883 if (!path) {
884 r = -EINVAL22;
885 goto fail;
886 }
887
888 /* Make sure there are no holes in the array, with the
889 * exception of argv[0] */
890 for (j = 1; j < n_argv; j++)
891 if (!argv[j]) {
892 r = -EINVAL22;
893 goto fail;
894 }
895
896 if (argv && argv[0] == NULL((void*)0)) {
897 argv[0] = strdup(path);
898 if (!argv[0]) {
899 r = -ENOMEM12;
900 goto fail;
901 }
902 }
903
904 b->exec_path = path;
905 b->exec_argv = argv;
906
907 b->is_local = false0;
908
909 return 0;
910
911fail:
912 for (j = 0; j < n_argv; j++)
913 free(argv[j]);
914
915 free(argv);
916 free(path);
917 return r;
918}
919
920static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) {
921 _cleanup_free___attribute__((cleanup(freep))) char *machine = NULL((void*)0), *pid = NULL((void*)0);
922 int r;
923
924 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 924, __PRETTY_FUNCTION__); } while (0)
;
925 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 925, __PRETTY_FUNCTION__); } while (0)
;
926 assert(*p)do { if ((__builtin_expect(!!(!(*p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("*p"), "../src/libsystemd/sd-bus/sd-bus.c"
, 926, __PRETTY_FUNCTION__); } while (0)
;
927 assert(guid)do { if ((__builtin_expect(!!(!(guid)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("guid"), "../src/libsystemd/sd-bus/sd-bus.c"
, 927, __PRETTY_FUNCTION__); } while (0)
;
928
929 while (!IN_SET(**p, 0, ';')({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){0, ';'})/sizeof(int)]; switch(**p) { case
0: case ';': _found = 1; break; default: break; } _found; })
) {
930 r = parse_address_key(p, "guid", guid);
931 if (r < 0)
932 return r;
933 else if (r > 0)
934 continue;
935
936 r = parse_address_key(p, "machine", &machine);
937 if (r < 0)
938 return r;
939 else if (r > 0)
940 continue;
941
942 r = parse_address_key(p, "pid", &pid);
943 if (r < 0)
944 return r;
945 else if (r > 0)
946 continue;
947
948 skip_address_key(p);
949 }
950
951 if (!machine == !pid)
952 return -EINVAL22;
953
954 if (machine) {
955 if (!streq(machine, ".host")(strcmp((machine),(".host")) == 0) && !machine_name_is_valid(machine)hostname_is_valid(machine, 0))
956 return -EINVAL22;
957
958 free_and_replace(b->machine, machine)({ free(b->machine); (b->machine) = (machine); (machine
) = ((void*)0); 0; })
;
959 } else {
960 b->machine = mfree(b->machine);
961 }
962
963 if (pid) {
964 r = parse_pid(pid, &b->nspid);
965 if (r < 0)
966 return r;
967 } else
968 b->nspid = 0;
969
970 b->sockaddr.un.sun_family = AF_UNIX1;
971 /* Note that we use the old /var/run prefix here, to increase compatibility with really old containers */
972 strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
973 b->sockaddr_size = SOCKADDR_UN_LEN(b->sockaddr.un)({ const struct sockaddr_un *_sa = &(b->sockaddr.un); do
{ if ((__builtin_expect(!!(!(_sa->sun_family == 1)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("_sa->sun_family == AF_UNIX"), "../src/libsystemd/sd-bus/sd-bus.c"
, 973, __PRETTY_FUNCTION__); } while (0); __builtin_offsetof(
struct sockaddr_un, sun_path) + (_sa->sun_path[0] == 0 ? 1
+ strnlen(_sa->sun_path+1, sizeof(_sa->sun_path)-1) : strnlen
(_sa->sun_path, sizeof(_sa->sun_path))); })
;
974 b->is_local = false0;
975
976 return 0;
977}
978
979static void bus_reset_parsed_address(sd_bus *b) {
980 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 980, __PRETTY_FUNCTION__); } while (0)
;
981
982 zero(b->sockaddr)(({ size_t _l_ = (sizeof(b->sockaddr)); void *_x_ = (&
(b->sockaddr)); _l_ == 0 ? _x_ : memset(_x_, 0, _l_); }))
;
983 b->sockaddr_size = 0;
984 b->exec_argv = strv_free(b->exec_argv);
985 b->exec_path = mfree(b->exec_path);
986 b->server_id = SD_ID128_NULL((const sd_id128_t) { .qwords = { 0, 0 }});
987 b->machine = mfree(b->machine);
988 b->nspid = 0;
989}
990
991static int bus_parse_next_address(sd_bus *b) {
992 _cleanup_free___attribute__((cleanup(freep))) char *guid = NULL((void*)0);
993 const char *a;
994 int r;
995
996 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 996, __PRETTY_FUNCTION__); } while (0)
;
997
998 if (!b->address)
999 return 0;
1000 if (b->address[b->address_index] == 0)
1001 return 0;
1002
1003 bus_reset_parsed_address(b);
1004
1005 a = b->address + b->address_index;
1006
1007 while (*a != 0) {
1008
1009 if (*a == ';') {
1010 a++;
1011 continue;
1012 }
1013
1014 if (startswith(a, "unix:")) {
1015 a += 5;
1016
1017 r = parse_unix_address(b, &a, &guid);
1018 if (r < 0)
1019 return r;
1020 break;
1021
1022 } else if (startswith(a, "tcp:")) {
1023
1024 a += 4;
1025 r = parse_tcp_address(b, &a, &guid);
1026 if (r < 0)
1027 return r;
1028
1029 break;
1030
1031 } else if (startswith(a, "unixexec:")) {
1032
1033 a += 9;
1034 r = parse_exec_address(b, &a, &guid);
1035 if (r < 0)
1036 return r;
1037
1038 break;
1039
1040 } else if (startswith(a, "x-machine-unix:")) {
1041
1042 a += 15;
1043 r = parse_container_unix_address(b, &a, &guid);
1044 if (r < 0)
1045 return r;
1046
1047 break;
1048 }
1049
1050 a = strchr(a, ';');
1051 if (!a)
1052 return 0;
1053 }
1054
1055 if (guid) {
1056 r = sd_id128_from_string(guid, &b->server_id);
1057 if (r < 0)
1058 return r;
1059 }
1060
1061 b->address_index = a - b->address;
1062 return 1;
1063}
1064
1065static void bus_kill_exec(sd_bus *bus) {
1066 if (pid_is_valid(bus->busexec_pid) > 0) {
1067 sigterm_wait(bus->busexec_pid);
1068 bus->busexec_pid = 0;
1069 }
1070}
1071
1072static int bus_start_address(sd_bus *b) {
1073 int r;
1074
1075 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1075, __PRETTY_FUNCTION__); } while (0)
;
1076
1077 for (;;) {
1078 bus_close_io_fds(b);
1079 bus_close_inotify_fd(b);
1080
1081 bus_kill_exec(b);
1082
1083 /* If you provide multiple different bus-addresses, we
1084 * try all of them in order and use the first one that
1085 * succeeds. */
1086
1087 if (b->exec_path)
1088 r = bus_socket_exec(b);
1089 else if ((b->nspid > 0 || b->machine) && b->sockaddr.sa.sa_family != AF_UNSPEC0)
1090 r = bus_container_connect_socket(b);
1091 else if (b->sockaddr.sa.sa_family != AF_UNSPEC0)
1092 r = bus_socket_connect(b);
1093 else
1094 goto next;
1095
1096 if (r >= 0) {
1097 int q;
1098
1099 q = bus_attach_io_events(b);
1100 if (q < 0)
1101 return q;
1102
1103 q = bus_attach_inotify_event(b);
1104 if (q < 0)
1105 return q;
1106
1107 return r;
1108 }
1109
1110 b->last_connect_error = -r;
1111
1112 next:
1113 r = bus_parse_next_address(b);
1114 if (r < 0)
1115 return r;
1116 if (r == 0)
1117 return b->last_connect_error > 0 ? -b->last_connect_error : -ECONNREFUSED111;
1118 }
1119}
1120
1121int bus_next_address(sd_bus *b) {
1122 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1122, __PRETTY_FUNCTION__); } while (0)
;
1123
1124 bus_reset_parsed_address(b);
1125 return bus_start_address(b);
1126}
1127
1128static int bus_start_fd(sd_bus *b) {
1129 struct stat st;
1130 int r;
1131
1132 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1132, __PRETTY_FUNCTION__); } while (0)
;
1133 assert(b->input_fd >= 0)do { if ((__builtin_expect(!!(!(b->input_fd >= 0)),0)))
log_assert_failed_realm(LOG_REALM_SYSTEMD, ("b->input_fd >= 0"
), "../src/libsystemd/sd-bus/sd-bus.c", 1133, __PRETTY_FUNCTION__
); } while (0)
;
1134 assert(b->output_fd >= 0)do { if ((__builtin_expect(!!(!(b->output_fd >= 0)),0))
) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("b->output_fd >= 0"
), "../src/libsystemd/sd-bus/sd-bus.c", 1134, __PRETTY_FUNCTION__
); } while (0)
;
1135
1136 r = fd_nonblock(b->input_fd, true1);
1137 if (r < 0)
1138 return r;
1139
1140 r = fd_cloexec(b->input_fd, true1);
1141 if (r < 0)
1142 return r;
1143
1144 if (b->input_fd != b->output_fd) {
1145 r = fd_nonblock(b->output_fd, true1);
1146 if (r < 0)
1147 return r;
1148
1149 r = fd_cloexec(b->output_fd, true1);
1150 if (r < 0)
1151 return r;
1152 }
1153
1154 if (fstat(b->input_fd, &st) < 0)
1155 return -errno(*__errno_location ());
1156
1157 return bus_socket_take_fd(b);
1158}
1159
1160_public___attribute__ ((visibility("default"))) int sd_bus_start(sd_bus *bus) {
1161 int r;
1162
1163 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1163, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1164 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1164, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
1165 assert_return(bus->state == BUS_UNSET, -EPERM)do { if (!(((__builtin_expect(!!(bus->state == BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state == BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1165, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
1166 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 1166, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
1167
1168 bus_set_state(bus, BUS_OPENING);
1169
1170 if (bus->is_server && bus->bus_client)
1171 return -EINVAL22;
1172
1173 if (bus->input_fd >= 0)
1174 r = bus_start_fd(bus);
1175 else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC0 || bus->exec_path || bus->machine)
1176 r = bus_start_address(bus);
1177 else
1178 return -EINVAL22;
1179
1180 if (r < 0) {
1181 sd_bus_close(bus);
1182 return r;
1183 }
1184
1185 return bus_send_hello(bus);
1186}
1187
1188_public___attribute__ ((visibility("default"))) int sd_bus_open_with_description(sd_bus **ret, const char *description) {
1189 const char *e;
1190 _cleanup_(bus_freep)__attribute__((cleanup(bus_freep))) sd_bus *b = NULL((void*)0);
1191 int r;
1192
1193 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1193, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1194
1195 /* Let's connect to the starter bus if it is set, and
1196 * otherwise to the bus that is appropropriate for the scope
1197 * we are running in */
1198
1199 e = secure_getenv("DBUS_STARTER_BUS_TYPE");
1200 if (e) {
1201 if (streq(e, "system")(strcmp((e),("system")) == 0))
1202 return sd_bus_open_system_with_description(ret, description);
1203 else if (STR_IN_SET(e, "session", "user")(!!strv_find((((char**) ((const char*[]) { "session", "user",
((void*)0) }))), (e)))
)
1204 return sd_bus_open_user_with_description(ret, description);
1205 }
1206
1207 e = secure_getenv("DBUS_STARTER_ADDRESS");
1208 if (!e) {
1209 if (cg_pid_get_owner_uid(0, NULL((void*)0)) >= 0)
1210 return sd_bus_open_user_with_description(ret, description);
1211 else
1212 return sd_bus_open_system_with_description(ret, description);
1213 }
1214
1215 r = sd_bus_new(&b);
1216 if (r < 0)
1217 return r;
1218
1219 r = sd_bus_set_address(b, e);
1220 if (r < 0)
1221 return r;
1222
1223 b->bus_client = true1;
1224
1225 /* We don't know whether the bus is trusted or not, so better
1226 * be safe, and authenticate everything */
1227 b->trusted = false0;
1228 b->is_local = false0;
1229 b->creds_mask |= SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_EFFECTIVE_CAPS;
1230
1231 r = sd_bus_start(b);
1232 if (r < 0)
1233 return r;
1234
1235 *ret = TAKE_PTR(b)({ typeof(b) _ptr_ = (b); (b) = ((void*)0); _ptr_; });
1236 return 0;
1237}
1238
1239_public___attribute__ ((visibility("default"))) int sd_bus_open(sd_bus **ret) {
1240 return sd_bus_open_with_description(ret, NULL((void*)0));
1241}
1242
1243int bus_set_address_system(sd_bus *b) {
1244 const char *e;
1245 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1245, __PRETTY_FUNCTION__); } while (0)
;
1246
1247 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1248 if (e)
1249 return sd_bus_set_address(b, e);
1250
1251 return sd_bus_set_address(b, DEFAULT_SYSTEM_BUS_ADDRESS"unix:path=/run/dbus/system_bus_socket");
1252}
1253
1254_public___attribute__ ((visibility("default"))) int sd_bus_open_system_with_description(sd_bus **ret, const char *description) {
1255 _cleanup_(bus_freep)__attribute__((cleanup(bus_freep))) sd_bus *b = NULL((void*)0);
1256 int r;
1257
1258 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1258, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1259
1260 r = sd_bus_new(&b);
1261 if (r < 0)
1262 return r;
1263
1264 if (description) {
1265 r = sd_bus_set_description(b, description);
1266 if (r < 0)
1267 return r;
1268 }
1269
1270 r = bus_set_address_system(b);
1271 if (r < 0)
1272 return r;
1273
1274 b->bus_client = true1;
1275 b->is_system = true1;
1276
1277 /* Let's do per-method access control on the system bus. We
1278 * need the caller's UID and capability set for that. */
1279 b->trusted = false0;
1280 b->creds_mask |= SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_EFFECTIVE_CAPS;
1281 b->is_local = true1;
1282
1283 r = sd_bus_start(b);
1284 if (r < 0)
1285 return r;
1286
1287 *ret = TAKE_PTR(b)({ typeof(b) _ptr_ = (b); (b) = ((void*)0); _ptr_; });
1288 return 0;
1289}
1290
1291_public___attribute__ ((visibility("default"))) int sd_bus_open_system(sd_bus **ret) {
1292 return sd_bus_open_system_with_description(ret, NULL((void*)0));
1293}
1294
1295int bus_set_address_user(sd_bus *b) {
1296 const char *e;
1297 _cleanup_free___attribute__((cleanup(freep))) char *ee = NULL((void*)0), *s = NULL((void*)0);
1298
1299 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1299, __PRETTY_FUNCTION__); } while (0)
;
1300
1301 e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1302 if (e)
1303 return sd_bus_set_address(b, e);
1304
1305 e = secure_getenv("XDG_RUNTIME_DIR");
1306 if (!e)
1307 return -ENOENT2;
1308
1309 ee = bus_address_escape(e);
1310 if (!ee)
1311 return -ENOMEM12;
1312
1313 if (asprintf(&s, DEFAULT_USER_BUS_ADDRESS_FMT"unix:path=%s/bus", ee) < 0)
1314 return -ENOMEM12;
1315
1316 b->address = TAKE_PTR(s)({ typeof(s) _ptr_ = (s); (s) = ((void*)0); _ptr_; });
1317
1318 return 0;
1319}
1320
1321_public___attribute__ ((visibility("default"))) int sd_bus_open_user_with_description(sd_bus **ret, const char *description) {
1322 _cleanup_(bus_freep)__attribute__((cleanup(bus_freep))) sd_bus *b = NULL((void*)0);
1323 int r;
1324
1325 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1325, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1326
1327 r = sd_bus_new(&b);
1328 if (r < 0)
1329 return r;
1330
1331 if (description) {
1332 r = sd_bus_set_description(b, description);
1333 if (r < 0)
1334 return r;
1335 }
1336
1337 r = bus_set_address_user(b);
1338 if (r < 0)
1339 return r;
1340
1341 b->bus_client = true1;
1342 b->is_user = true1;
1343
1344 /* We don't do any per-method access control on the user bus. */
1345 b->trusted = true1;
1346 b->is_local = true1;
1347
1348 r = sd_bus_start(b);
1349 if (r < 0)
1350 return r;
1351
1352 *ret = TAKE_PTR(b)({ typeof(b) _ptr_ = (b); (b) = ((void*)0); _ptr_; });
1353 return 0;
1354}
1355
1356_public___attribute__ ((visibility("default"))) int sd_bus_open_user(sd_bus **ret) {
1357 return sd_bus_open_user_with_description(ret, NULL((void*)0));
1358}
1359
1360int bus_set_address_system_remote(sd_bus *b, const char *host) {
1361 _cleanup_free___attribute__((cleanup(freep))) char *e = NULL((void*)0);
1362 char *m = NULL((void*)0), *c = NULL((void*)0), *a;
1363
1364 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1364, __PRETTY_FUNCTION__); } while (0)
;
1365 assert(host)do { if ((__builtin_expect(!!(!(host)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("host"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1365, __PRETTY_FUNCTION__); } while (0)
;
1366
1367 /* Let's see if we shall enter some container */
1368 m = strchr(host, ':');
1369 if (m) {
1370 m++;
1371
1372 /* Let's make sure this is not a port of some kind,
1373 * and is a valid machine name. */
1374 if (!in_charset(m, DIGITS"0123456789") && machine_name_is_valid(m)hostname_is_valid(m, 0)) {
1375 char *t;
1376
1377 /* Cut out the host part */
1378 t = strndupa(host, m - host - 1)(__extension__ ({ const char *__old = (host); size_t __len = strnlen
(__old, (m - host - 1)); char *__new = (char *) __builtin_alloca
(__len + 1); __new[__len] = '\0'; (char *) memcpy (__new, __old
, __len); }))
;
1379 e = bus_address_escape(t);
1380 if (!e)
1381 return -ENOMEM12;
1382
1383 c = strjoina(",argv5=--machine=", m)({ const char *_appendees_[] = { ",argv5=--machine=", m }; char
*_d_, *_p_; size_t _len_ = 0; size_t _i_; for (_i_ = 0; _i_ <
__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p
(typeof(_appendees_), typeof(&*(_appendees_))), sizeof(_appendees_
)/sizeof((_appendees_)[0]), ((void)0))) && _appendees_
[_i_]; _i_++) _len_ += strlen(_appendees_[_i_]); _p_ = _d_ = __builtin_alloca
(_len_ + 1); for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr
( !__builtin_types_compatible_p(typeof(_appendees_), typeof(&
*(_appendees_))), sizeof(_appendees_)/sizeof((_appendees_)[0]
), ((void)0))) && _appendees_[_i_]; _i_++) _p_ = stpcpy
(_p_, _appendees_[_i_]); *_p_ = 0; _d_; })
;
1384 }
1385 }
1386
1387 if (!e) {
1388 e = bus_address_escape(host);
1389 if (!e)
1390 return -ENOMEM12;
1391 }
1392
1393 a = strjoin("unixexec:path=ssh,argv1=-xT,argv2=--,argv3=", e, ",argv4=systemd-stdio-bridge", c)strjoin_real(("unixexec:path=ssh,argv1=-xT,argv2=--,argv3="),
e, ",argv4=systemd-stdio-bridge", c, ((void*)0))
;
1394 if (!a)
1395 return -ENOMEM12;
1396
1397 return free_and_replace(b->address, a)({ free(b->address); (b->address) = (a); (a) = ((void*)
0); 0; })
;
1398}
1399
1400_public___attribute__ ((visibility("default"))) int sd_bus_open_system_remote(sd_bus **ret, const char *host) {
1401 _cleanup_(bus_freep)__attribute__((cleanup(bus_freep))) sd_bus *b = NULL((void*)0);
1402 int r;
1403
1404 assert_return(host, -EINVAL)do { if (!(((__builtin_expect(!!(host),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("host"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1404, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1405 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1405, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1406
1407 r = sd_bus_new(&b);
1408 if (r < 0)
1409 return r;
1410
1411 r = bus_set_address_system_remote(b, host);
1412 if (r < 0)
1413 return r;
1414
1415 b->bus_client = true1;
1416 b->trusted = false0;
1417 b->is_system = true1;
1418 b->is_local = false0;
1419
1420 r = sd_bus_start(b);
1421 if (r < 0)
1422 return r;
1423
1424 *ret = TAKE_PTR(b)({ typeof(b) _ptr_ = (b); (b) = ((void*)0); _ptr_; });
1425 return 0;
1426}
1427
1428int bus_set_address_system_machine(sd_bus *b, const char *machine) {
1429 _cleanup_free___attribute__((cleanup(freep))) char *e = NULL((void*)0);
1430 char *a;
1431
1432 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1432, __PRETTY_FUNCTION__); } while (0)
;
1433 assert(machine)do { if ((__builtin_expect(!!(!(machine)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("machine"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1433, __PRETTY_FUNCTION__); } while (0)
;
1434
1435 e = bus_address_escape(machine);
1436 if (!e)
1437 return -ENOMEM12;
1438
1439 a = strjoin("x-machine-unix:machine=", e)strjoin_real(("x-machine-unix:machine="), e, ((void*)0));
1440 if (!a)
1441 return -ENOMEM12;
1442
1443 return free_and_replace(b->address, a)({ free(b->address); (b->address) = (a); (a) = ((void*)
0); 0; })
;
1444}
1445
1446_public___attribute__ ((visibility("default"))) int sd_bus_open_system_machine(sd_bus **ret, const char *machine) {
1447 _cleanup_(bus_freep)__attribute__((cleanup(bus_freep))) sd_bus *b = NULL((void*)0);
1448 int r;
1449
1450 assert_return(machine, -EINVAL)do { if (!(((__builtin_expect(!!(machine),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("machine"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1450, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1451 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1451, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1452 assert_return(streq(machine, ".host") || machine_name_is_valid(machine), -EINVAL)do { if (!(((__builtin_expect(!!((strcmp((machine),(".host"))
== 0) || hostname_is_valid(machine, 0)),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("streq(machine, \".host\") || machine_name_is_valid(machine)"
), "../src/libsystemd/sd-bus/sd-bus.c", 1452, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
1453
1454 r = sd_bus_new(&b);
1455 if (r < 0)
1456 return r;
1457
1458 r = bus_set_address_system_machine(b, machine);
1459 if (r < 0)
1460 return r;
1461
1462 b->bus_client = true1;
1463 b->trusted = false0;
1464 b->is_system = true1;
1465 b->is_local = false0;
1466
1467 r = sd_bus_start(b);
1468 if (r < 0)
1469 return r;
1470
1471 *ret = TAKE_PTR(b)({ typeof(b) _ptr_ = (b); (b) = ((void*)0); _ptr_; });
1472 return 0;
1473}
1474
1475_public___attribute__ ((visibility("default"))) void sd_bus_close(sd_bus *bus) {
1476 if (!bus)
1477 return;
1478 if (bus->state == BUS_CLOSED)
1479 return;
1480 if (bus_pid_changed(bus))
1481 return;
1482
1483 /* Don't leave ssh hanging around */
1484 bus_kill_exec(bus);
1485
1486 bus_set_state(bus, BUS_CLOSED);
1487
1488 sd_bus_detach_event(bus);
1489
1490 /* Drop all queued messages so that they drop references to
1491 * the bus object and the bus may be freed */
1492 bus_reset_queues(bus);
1493
1494 bus_close_io_fds(bus);
1495 bus_close_inotify_fd(bus);
1496}
1497
1498_public___attribute__ ((visibility("default"))) sd_bus* sd_bus_flush_close_unref(sd_bus *bus) {
1499 if (!bus)
1500 return NULL((void*)0);
1501
1502 /* Have to do this before flush() to prevent hang */
1503 bus_kill_exec(bus);
1504
1505 sd_bus_flush(bus);
1506 sd_bus_close(bus);
1507
1508 return sd_bus_unref(bus);
1509}
1510
1511void bus_enter_closing(sd_bus *bus) {
1512 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1512, __PRETTY_FUNCTION__); } while (0)
;
1513
1514 if (!IN_SET(bus->state, BUS_WATCH_BIND, BUS_OPENING, BUS_AUTHENTICATING, BUS_HELLO, BUS_RUNNING)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){BUS_WATCH_BIND, BUS_OPENING, BUS_AUTHENTICATING
, BUS_HELLO, BUS_RUNNING})/sizeof(int)]; switch(bus->state
) { case BUS_WATCH_BIND: case BUS_OPENING: case BUS_AUTHENTICATING
: case BUS_HELLO: case BUS_RUNNING: _found = 1; break; default
: break; } _found; })
)
1515 return;
1516
1517 bus_set_state(bus, BUS_CLOSING);
1518}
1519
1520_public___attribute__ ((visibility("default"))) sd_bus *sd_bus_ref(sd_bus *bus) {
1521 if (!bus)
1522 return NULL((void*)0);
1523
1524 assert_se(REFCNT_INC(bus->n_ref) >= 2)do { if ((__builtin_expect(!!(!((__sync_add_and_fetch(&(bus
->n_ref)._value, 1)) >= 2)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("REFCNT_INC(bus->n_ref) >= 2"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1524, __PRETTY_FUNCTION__); } while (0)
;
1525
1526 return bus;
1527}
1528
1529_public___attribute__ ((visibility("default"))) sd_bus *sd_bus_unref(sd_bus *bus) {
1530 unsigned i;
1531
1532 if (!bus)
1533 return NULL((void*)0);
1534
1535 i = REFCNT_DEC(bus->n_ref)(__sync_sub_and_fetch(&(bus->n_ref)._value, 1));
1536 if (i > 0)
1537 return NULL((void*)0);
1538
1539 return bus_free(bus);
1540}
1541
1542_public___attribute__ ((visibility("default"))) int sd_bus_is_open(sd_bus *bus) {
1543 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1543, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1544 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1544, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
1545 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 1545, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
1546
1547 return BUS_IS_OPEN(bus->state);
1548}
1549
1550_public___attribute__ ((visibility("default"))) int sd_bus_is_ready(sd_bus *bus) {
1551 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1551, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1552 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1552, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
1553 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 1553, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
1554
1555 return bus->state == BUS_RUNNING;
1556}
1557
1558_public___attribute__ ((visibility("default"))) int sd_bus_can_send(sd_bus *bus, char type) {
1559 int r;
1560
1561 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1561, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1562 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1562, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
1563 assert_return(bus->state != BUS_UNSET, -ENOTCONN)do { if (!(((__builtin_expect(!!(bus->state != BUS_UNSET),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->state != BUS_UNSET"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1563, __PRETTY_FUNCTION__), 0))) return (-107); } while (0)
;
1564 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 1564, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
1565
1566 if (bus->is_monitor)
1567 return 0;
1568
1569 if (type == SD_BUS_TYPE_UNIX_FD) {
1570 if (!bus->accept_fd)
1571 return 0;
1572
1573 r = bus_ensure_running(bus);
1574 if (r < 0)
1575 return r;
1576
1577 return bus->can_fds;
1578 }
1579
1580 return bus_type_is_valid(type);
1581}
1582
1583_public___attribute__ ((visibility("default"))) int sd_bus_get_bus_id(sd_bus *bus, sd_id128_t *id) {
1584 int r;
1585
1586 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1586, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1587 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1587, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
1588 assert_return(id, -EINVAL)do { if (!(((__builtin_expect(!!(id),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("id"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1588, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1589 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 1589, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
1590
1591 r = bus_ensure_running(bus);
1592 if (r < 0)
1593 return r;
1594
1595 *id = bus->server_id;
1596 return 0;
1597}
1598
1599#define COOKIE_CYCLED(1U << 31) (UINT32_C(1)1U << 31)
1600
1601static uint64_t cookie_inc(uint64_t cookie) {
1602
1603 /* Stay within the 32bit range, since classic D-Bus can't deal with more */
1604 if (cookie >= UINT32_MAX(4294967295U))
1605 return COOKIE_CYCLED(1U << 31); /* Don't go back to zero, but use the highest bit for checking
1606 * whether we are looping. */
1607
1608 return cookie + 1;
1609}
1610
1611static int next_cookie(sd_bus *b) {
1612 uint64_t new_cookie;
1613
1614 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1614, __PRETTY_FUNCTION__); } while (0)
;
1615
1616 new_cookie = cookie_inc(b->cookie);
1617
1618 /* Small optimization: don't bother with checking for cookie reuse until we overran cookiespace at
1619 * least once, but then do it thorougly. */
1620 if (FLAGS_SET(new_cookie, COOKIE_CYCLED)(((new_cookie) & ((1U << 31))) == ((1U << 31)
))
) {
1621 uint32_t i;
1622
1623 /* Check if the cookie is currently in use. If so, pick the next one */
1624 for (i = 0; i < COOKIE_CYCLED(1U << 31); i++) {
1625 if (!ordered_hashmap_contains(b->reply_callbacks, &new_cookie))
1626 goto good;
1627
1628 new_cookie = cookie_inc(new_cookie);
1629 }
1630
1631 /* Can't fulfill request */
1632 return -EBUSY16;
1633 }
1634
1635good:
1636 b->cookie = new_cookie;
1637 return 0;
1638}
1639
1640static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
1641 int r;
1642
1643 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1643, __PRETTY_FUNCTION__); } while (0)
;
1644 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1644, __PRETTY_FUNCTION__); } while (0)
;
1645
1646 if (m->sealed) {
1647 /* If we copy the same message to multiple
1648 * destinations, avoid using the same cookie
1649 * numbers. */
1650 b->cookie = MAX(b->cookie, BUS_MESSAGE_COOKIE(m))__extension__ ({ const typeof((b->cookie)) __unique_prefix_A40
= ((b->cookie)); const typeof((BUS_MESSAGE_COOKIE(m))) __unique_prefix_B41
= ((BUS_MESSAGE_COOKIE(m))); __unique_prefix_A40 > __unique_prefix_B41
? __unique_prefix_A40 : __unique_prefix_B41; })
;
1651 return 0;
1652 }
1653
1654 if (timeout == 0)
1655 timeout = BUS_DEFAULT_TIMEOUT((usec_t) (25 * ((usec_t) 1000000ULL)));
1656
1657 if (!m->sender && b->patch_sender) {
1658 r = sd_bus_message_set_sender(m, b->patch_sender);
1659 if (r < 0)
1660 return r;
1661 }
1662
1663 r = next_cookie(b);
1664 if (r < 0)
1665 return r;
1666
1667 return sd_bus_message_seal(m, b->cookie, timeout);
1668}
1669
1670static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) {
1671 bool_Bool remarshal = false0;
1672
1673 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1673, __PRETTY_FUNCTION__); } while (0)
;
1674
1675 /* wrong packet version */
1676 if (b->message_version != 0 && b->message_version != (*m)->header->version)
1677 remarshal = true1;
1678
1679 /* wrong packet endianness */
1680 if (b->message_endian != 0 && b->message_endian != (*m)->header->endian)
1681 remarshal = true1;
1682
1683 return remarshal ? bus_message_remarshal(b, m) : 0;
1684}
1685
1686int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1687 assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1687, __PRETTY_FUNCTION__); } while (0)
;
1688 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1688, __PRETTY_FUNCTION__); } while (0)
;
1689
1690 /* Fake some timestamps, if they were requested, and not
1691 * already initialized */
1692 if (b->attach_timestamp) {
1693 if (m->realtime <= 0)
1694 m->realtime = now(CLOCK_REALTIME0);
1695
1696 if (m->monotonic <= 0)
1697 m->monotonic = now(CLOCK_MONOTONIC1);
1698 }
1699
1700 /* The bus specification says the serial number cannot be 0,
1701 * hence let's fill something in for synthetic messages. Since
1702 * synthetic messages might have a fake sender and we don't
1703 * want to interfere with the real sender's serial numbers we
1704 * pick a fixed, artificial one. We use (uint32_t) -1 rather
1705 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1706 * even though kdbus can do 64bit. */
1707 return sd_bus_message_seal(m, 0xFFFFFFFFULL, 0);
1708}
1709
1710static int bus_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
1711 int r;
1712
1713 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1713, __PRETTY_FUNCTION__); } while (0)
;
1714 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1714, __PRETTY_FUNCTION__); } while (0)
;
1715
1716 r = bus_socket_write_message(bus, m, idx);
1717 if (r <= 0)
1718 return r;
1719
1720 if (*idx >= BUS_MESSAGE_SIZE(m))
1721 log_debug("Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " signature=%s error-name=%s error-message=%s",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
1722 bus_message_type_to_string(m->header->type),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
1723 strna(sd_bus_message_get_sender(m)),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
1724 strna(sd_bus_message_get_destination(m)),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
1725 strna(sd_bus_message_get_path(m)),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
1726 strna(sd_bus_message_get_interface(m)),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
1727 strna(sd_bus_message_get_member(m)),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
1728 BUS_MESSAGE_COOKIE(m),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
1729 m->reply_cookie,({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
1730 strna(m->root_container.signature),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
1731 strna(m->error.name),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
1732 strna(m->error.message))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 1732, __func__, "Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender
(m)), strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m)), BUS_MESSAGE_COOKIE(m), m->reply_cookie, strna(m->
root_container.signature), strna(m->error.name), strna(m->
error.message)) : -abs(_e); })
;
1733
1734 return r;
1735}
1736
1737static int dispatch_wqueue(sd_bus *bus) {
1738 int r, ret = 0;
1739
1740 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1740, __PRETTY_FUNCTION__); } while (0)
;
1741 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO))do { if ((__builtin_expect(!!(!(({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){BUS_RUNNING, BUS_HELLO})/sizeof(int)]; switch
(bus->state) { case BUS_RUNNING: case BUS_HELLO: _found = 1
; break; default: break; } _found; }))),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)"
), "../src/libsystemd/sd-bus/sd-bus.c", 1741, __PRETTY_FUNCTION__
); } while (0)
;
1742
1743 while (bus->wqueue_size > 0) {
1744
1745 r = bus_write_message(bus, bus->wqueue[0], &bus->windex);
1746 if (r < 0)
1747 return r;
1748 else if (r == 0)
1749 /* Didn't do anything this time */
1750 return ret;
1751 else if (bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1752 /* Fully written. Let's drop the entry from
1753 * the queue.
1754 *
1755 * This isn't particularly optimized, but
1756 * well, this is supposed to be our worst-case
1757 * buffer only, and the socket buffer is
1758 * supposed to be our primary buffer, and if
1759 * it got full, then all bets are off
1760 * anyway. */
1761
1762 bus->wqueue_size--;
1763 bus_message_unref_queued(bus->wqueue[0], bus);
1764 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1765 bus->windex = 0;
1766
1767 ret = 1;
1768 }
1769 }
1770
1771 return ret;
1772}
1773
1774static int bus_read_message(sd_bus *bus, bool_Bool hint_priority, int64_t priority) {
1775 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1775, __PRETTY_FUNCTION__); } while (0)
;
1776
1777 return bus_socket_read_message(bus);
1778}
1779
1780int bus_rqueue_make_room(sd_bus *bus) {
1781 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1781, __PRETTY_FUNCTION__); } while (0)
;
1782
1783 if (bus->rqueue_size >= BUS_RQUEUE_MAX(384*1024))
1784 return -ENOBUFS105;
1785
1786 if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1)greedy_realloc((void**) &(bus->rqueue), &(bus->
rqueue_allocated), (bus->rqueue_size + 1), sizeof((bus->
rqueue)[0]))
)
1787 return -ENOMEM12;
1788
1789 return 0;
1790}
1791
1792static void rqueue_drop_one(sd_bus *bus, size_t i) {
1793 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1793, __PRETTY_FUNCTION__); } while (0)
;
1794 assert(i < bus->rqueue_size)do { if ((__builtin_expect(!!(!(i < bus->rqueue_size)),
0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("i < bus->rqueue_size"
), "../src/libsystemd/sd-bus/sd-bus.c", 1794, __PRETTY_FUNCTION__
); } while (0)
;
1795
1796 bus_message_unref_queued(bus->rqueue[i], bus);
1797 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1798 bus->rqueue_size--;
1799}
1800
1801static int dispatch_rqueue(sd_bus *bus, bool_Bool hint_priority, int64_t priority, sd_bus_message **m) {
1802 int r, ret = 0;
1803
1804 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1804, __PRETTY_FUNCTION__); } while (0)
;
1805 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1805, __PRETTY_FUNCTION__); } while (0)
;
1806 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO))do { if ((__builtin_expect(!!(!(({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){BUS_RUNNING, BUS_HELLO})/sizeof(int)]; switch
(bus->state) { case BUS_RUNNING: case BUS_HELLO: _found = 1
; break; default: break; } _found; }))),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)"
), "../src/libsystemd/sd-bus/sd-bus.c", 1806, __PRETTY_FUNCTION__
); } while (0)
;
1807
1808 /* Note that the priority logic is only available on kdbus,
1809 * where the rqueue is unused. We check the rqueue here
1810 * anyway, because it's simple... */
1811
1812 for (;;) {
1813 if (bus->rqueue_size > 0) {
1814 /* Dispatch a queued message */
1815 *m = sd_bus_message_ref(bus->rqueue[0]);
1816 rqueue_drop_one(bus, 0);
1817 return 1;
1818 }
1819
1820 /* Try to read a new message */
1821 r = bus_read_message(bus, hint_priority, priority);
1822 if (r < 0)
1823 return r;
1824 if (r == 0) {
1825 *m = NULL((void*)0);
1826 return ret;
1827 }
1828
1829 ret = 1;
1830 }
1831}
1832
1833_public___attribute__ ((visibility("default"))) int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie) {
1834 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = sd_bus_message_ref(_m);
1835 int r;
1836
1837 assert_return(m, -EINVAL)do { if (!(((__builtin_expect(!!(m),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1837, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1838
1839 if (!bus)
1840 bus = m->bus;
1841
1842 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 1842, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
1843
1844 if (!BUS_IS_OPEN(bus->state))
1845 return -ENOTCONN107;
1846
1847 if (m->n_fds > 0) {
1848 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1849 if (r < 0)
1850 return r;
1851 if (r == 0)
1852 return -EOPNOTSUPP95;
1853 }
1854
1855 /* If the cookie number isn't kept, then we know that no reply
1856 * is expected */
1857 if (!cookie && !m->sealed)
1858 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
1859
1860 r = bus_seal_message(bus, m, 0);
1861 if (r < 0)
1862 return r;
1863
1864 /* Remarshall if we have to. This will possibly unref the
1865 * message and place a replacement in m */
1866 r = bus_remarshal_message(bus, &m);
1867 if (r < 0)
1868 return r;
1869
1870 /* If this is a reply and no reply was requested, then let's
1871 * suppress this, if we can */
1872 if (m->dont_send)
1873 goto finish;
1874
1875 if (IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){BUS_RUNNING, BUS_HELLO})/sizeof(int)]; switch
(bus->state) { case BUS_RUNNING: case BUS_HELLO: _found = 1
; break; default: break; } _found; })
&& bus->wqueue_size <= 0) {
1876 size_t idx = 0;
1877
1878 r = bus_write_message(bus, m, &idx);
1879 if (r < 0) {
1880 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-107, -104, -32, -108})/sizeof(int)]; switch
(r) { case -107: case -104: case -32: case -108: _found = 1; break
; default: break; } _found; })
) {
1881 bus_enter_closing(bus);
1882 return -ECONNRESET104;
1883 }
1884
1885 return r;
1886 }
1887
1888 if (idx < BUS_MESSAGE_SIZE(m)) {
1889 /* Wasn't fully written. So let's remember how
1890 * much was written. Note that the first entry
1891 * of the wqueue array is always allocated so
1892 * that we always can remember how much was
1893 * written. */
1894 bus->wqueue[0] = bus_message_ref_queued(m, bus);
1895 bus->wqueue_size = 1;
1896 bus->windex = idx;
1897 }
1898
1899 } else {
1900 /* Just append it to the queue. */
1901
1902 if (bus->wqueue_size >= BUS_WQUEUE_MAX(384*1024))
1903 return -ENOBUFS105;
1904
1905 if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1)greedy_realloc((void**) &(bus->wqueue), &(bus->
wqueue_allocated), (bus->wqueue_size + 1), sizeof((bus->
wqueue)[0]))
)
1906 return -ENOMEM12;
1907
1908 bus->wqueue[bus->wqueue_size++] = bus_message_ref_queued(m, bus);
1909 }
1910
1911finish:
1912 if (cookie)
1913 *cookie = BUS_MESSAGE_COOKIE(m);
1914
1915 return 1;
1916}
1917
1918_public___attribute__ ((visibility("default"))) int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) {
1919 int r;
1920
1921 assert_return(m, -EINVAL)do { if (!(((__builtin_expect(!!(m),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1921, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1922
1923 if (!bus)
1924 bus = m->bus;
1925
1926 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 1926, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
1927
1928 if (!BUS_IS_OPEN(bus->state))
1929 return -ENOTCONN107;
1930
1931 if (!streq_ptr(m->destination, destination)) {
1932
1933 if (!destination)
1934 return -EEXIST17;
1935
1936 r = sd_bus_message_set_destination(m, destination);
1937 if (r < 0)
1938 return r;
1939 }
1940
1941 return sd_bus_send(bus, m, cookie);
1942}
1943
1944static usec_t calc_elapse(sd_bus *bus, uint64_t usec) {
1945 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1945, __PRETTY_FUNCTION__); } while (0)
;
1946
1947 if (usec == (uint64_t) -1)
1948 return 0;
1949
1950 /* We start all timeouts the instant we enter BUS_HELLO/BUS_RUNNING state, so that the don't run in parallel
1951 * with any connection setup states. Hence, if a method callback is started earlier than that we just store the
1952 * relative timestamp, and afterwards the absolute one. */
1953
1954 if (IN_SET(bus->state, BUS_WATCH_BIND, BUS_OPENING, BUS_AUTHENTICATING)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){BUS_WATCH_BIND, BUS_OPENING, BUS_AUTHENTICATING
})/sizeof(int)]; switch(bus->state) { case BUS_WATCH_BIND:
case BUS_OPENING: case BUS_AUTHENTICATING: _found = 1; break
; default: break; } _found; })
)
1955 return usec;
1956 else
1957 return now(CLOCK_MONOTONIC1) + usec;
1958}
1959
1960static int timeout_compare(const void *a, const void *b) {
1961 const struct reply_callback *x = a, *y = b;
1962
1963 if (x->timeout_usec != 0 && y->timeout_usec == 0)
1964 return -1;
1965
1966 if (x->timeout_usec == 0 && y->timeout_usec != 0)
1967 return 1;
1968
1969 if (x->timeout_usec < y->timeout_usec)
1970 return -1;
1971
1972 if (x->timeout_usec > y->timeout_usec)
1973 return 1;
1974
1975 return 0;
1976}
1977
1978_public___attribute__ ((visibility("default"))) int sd_bus_call_async(
1979 sd_bus *bus,
1980 sd_bus_slot **slot,
1981 sd_bus_message *_m,
1982 sd_bus_message_handler_t callback,
1983 void *userdata,
1984 uint64_t usec) {
1985
1986 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = sd_bus_message_ref(_m);
1987 _cleanup_(sd_bus_slot_unrefp)__attribute__((cleanup(sd_bus_slot_unrefp))) sd_bus_slot *s = NULL((void*)0);
1988 int r;
1989
1990 assert_return(m, -EINVAL)do { if (!(((__builtin_expect(!!(m),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1990, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1991 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL)do { if (!(((__builtin_expect(!!(m->header->type == SD_BUS_MESSAGE_METHOD_CALL
),1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("m->header->type == SD_BUS_MESSAGE_METHOD_CALL"), "../src/libsystemd/sd-bus/sd-bus.c"
, 1991, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1992 assert_return(!m->sealed || (!!callback == !(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)), -EINVAL)do { if (!(((__builtin_expect(!!(!m->sealed || (!!callback
== !(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED
))),1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("!m->sealed || (!!callback == !(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED))"
), "../src/libsystemd/sd-bus/sd-bus.c", 1992, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
1993
1994 if (!bus)
1995 bus = m->bus;
1996
1997 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 1997, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
1998
1999 if (!BUS_IS_OPEN(bus->state))
2000 return -ENOTCONN107;
2001
2002 /* If no callback is specified and there's no interest in a slot, then there's no reason to ask for a reply */
2003 if (!callback && !slot && !m->sealed)
2004 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
2005
2006 r = ordered_hashmap_ensure_allocated(&bus->reply_callbacks, &uint64_hash_ops)internal_ordered_hashmap_ensure_allocated(&bus->reply_callbacks
, &uint64_hash_ops )
;
2007 if (r < 0)
2008 return r;
2009
2010 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
2011 if (r < 0)
2012 return r;
2013
2014 r = bus_seal_message(bus, m, usec);
2015 if (r < 0)
2016 return r;
2017
2018 r = bus_remarshal_message(bus, &m);
2019 if (r < 0)
2020 return r;
2021
2022 if (slot || callback) {
2023 s = bus_slot_allocate(bus, !slot, BUS_REPLY_CALLBACK, sizeof(struct reply_callback), userdata);
2024 if (!s)
2025 return -ENOMEM12;
2026
2027 s->reply_callback.callback = callback;
2028
2029 s->reply_callback.cookie = BUS_MESSAGE_COOKIE(m);
2030 r = ordered_hashmap_put(bus->reply_callbacks, &s->reply_callback.cookie, &s->reply_callback);
2031 if (r < 0) {
2032 s->reply_callback.cookie = 0;
2033 return r;
2034 }
2035
2036 s->reply_callback.timeout_usec = calc_elapse(bus, m->timeout);
2037 if (s->reply_callback.timeout_usec != 0) {
2038 r = prioq_put(bus->reply_callbacks_prioq, &s->reply_callback, &s->reply_callback.prioq_idx);
2039 if (r < 0) {
2040 s->reply_callback.timeout_usec = 0;
2041 return r;
2042 }
2043 }
2044 }
2045
2046 r = sd_bus_send(bus, m, s ? &s->reply_callback.cookie : NULL((void*)0));
2047 if (r < 0)
2048 return r;
2049
2050 if (slot)
2051 *slot = s;
2052 s = NULL((void*)0);
2053
2054 return r;
2055}
2056
2057int bus_ensure_running(sd_bus *bus) {
2058 int r;
2059
2060 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2060, __PRETTY_FUNCTION__); } while (0)
;
2061
2062 if (bus->state == BUS_RUNNING)
2063 return 1;
2064
2065 for (;;) {
2066 if (IN_SET(bus->state, BUS_UNSET, BUS_CLOSED, BUS_CLOSING)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){BUS_UNSET, BUS_CLOSED, BUS_CLOSING})/sizeof
(int)]; switch(bus->state) { case BUS_UNSET: case BUS_CLOSED
: case BUS_CLOSING: _found = 1; break; default: break; } _found
; })
)
2067 return -ENOTCONN107;
2068
2069 r = sd_bus_process(bus, NULL((void*)0));
2070 if (r < 0)
2071 return r;
2072 if (bus->state == BUS_RUNNING)
2073 return 1;
2074 if (r > 0)
2075 continue;
2076
2077 r = sd_bus_wait(bus, (uint64_t) -1);
2078 if (r < 0)
2079 return r;
2080 }
2081}
2082
2083_public___attribute__ ((visibility("default"))) int sd_bus_call(
2084 sd_bus *bus,
2085 sd_bus_message *_m,
2086 uint64_t usec,
2087 sd_bus_error *error,
2088 sd_bus_message **reply) {
2089
2090 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = sd_bus_message_ref(_m);
2091 usec_t timeout;
2092 uint64_t cookie;
2093 size_t i;
2094 int r;
2095
2096 bus_assert_return(m, -EINVAL, error)do { if (!(((__builtin_expect(!!(m),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2096, __PRETTY_FUNCTION__), 0))) return sd_bus_error_set_errno
(error, -22); } while (0)
;
2097 bus_assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL, error)do { if (!(((__builtin_expect(!!(m->header->type == SD_BUS_MESSAGE_METHOD_CALL
),1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("m->header->type == SD_BUS_MESSAGE_METHOD_CALL"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2097, __PRETTY_FUNCTION__), 0))) return sd_bus_error_set_errno
(error, -22); } while (0)
;
2098 bus_assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL, error)do { if (!(((__builtin_expect(!!(!(m->header->flags &
BUS_MESSAGE_NO_REPLY_EXPECTED)),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)"
), "../src/libsystemd/sd-bus/sd-bus.c", 2098, __PRETTY_FUNCTION__
), 0))) return sd_bus_error_set_errno(error, -22); } while (0
)
;
2099 bus_assert_return(!bus_error_is_dirty(error), -EINVAL, error)do { if (!(((__builtin_expect(!!(!bus_error_is_dirty(error)),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("!bus_error_is_dirty(error)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2099, __PRETTY_FUNCTION__), 0))) return sd_bus_error_set_errno
(error, -22); } while (0)
;
2100
2101 if (!bus)
2102 bus = m->bus;
2103
2104 bus_assert_return(!bus_pid_changed(bus), -ECHILD, error)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 2104, __PRETTY_FUNCTION__
), 0))) return sd_bus_error_set_errno(error, -10); } while (0
)
;
2105
2106 if (!BUS_IS_OPEN(bus->state)) {
2107 r = -ENOTCONN107;
2108 goto fail;
2109 }
2110
2111 r = bus_ensure_running(bus);
2112 if (r < 0)
2113 goto fail;
2114
2115 i = bus->rqueue_size;
2116
2117 r = bus_seal_message(bus, m, usec);
2118 if (r < 0)
2119 goto fail;
2120
2121 r = bus_remarshal_message(bus, &m);
2122 if (r < 0)
2123 goto fail;
2124
2125 r = sd_bus_send(bus, m, &cookie);
2126 if (r < 0)
2127 goto fail;
2128
2129 timeout = calc_elapse(bus, m->timeout);
2130
2131 for (;;) {
2132 usec_t left;
2133
2134 while (i < bus->rqueue_size) {
2135 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *incoming = NULL((void*)0);
2136
2137 incoming = sd_bus_message_ref(bus->rqueue[i]);
2138
2139 if (incoming->reply_cookie == cookie) {
2140 /* Found a match! */
2141
2142 rqueue_drop_one(bus, i);
2143 log_debug_bus_message(incoming)do { sd_bus_message *_mm = (incoming); ({ int _level = (((7))
), _e = ((0)), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm
(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((
_realm) << 10 | (_level)), _e, "../src/libsystemd/sd-bus/sd-bus.c"
, 2143, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); }); } while (0)
;
2144
2145 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
2146
2147 if (incoming->n_fds <= 0 || bus->accept_fd) {
2148 if (reply)
2149 *reply = TAKE_PTR(incoming)({ typeof(incoming) _ptr_ = (incoming); (incoming) = ((void*)
0); _ptr_; })
;
2150
2151 return 1;
2152 }
2153
2154 return sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE"org.freedesktop.DBus.Error.InconsistentMessage", "Reply message contained file descriptors which I couldn't accept. Sorry.");
2155
2156 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
2157 return sd_bus_error_copy(error, &incoming->error);
2158 else {
2159 r = -EIO5;
2160 goto fail;
2161 }
2162
2163 } else if (BUS_MESSAGE_COOKIE(incoming) == cookie &&
2164 bus->unique_name &&
2165 incoming->sender &&
2166 streq(bus->unique_name, incoming->sender)(strcmp((bus->unique_name),(incoming->sender)) == 0)) {
2167
2168 rqueue_drop_one(bus, i);
2169
2170 /* Our own message? Somebody is trying to send its own client a message,
2171 * let's not dead-lock, let's fail immediately. */
2172
2173 r = -ELOOP40;
2174 goto fail;
2175 }
2176
2177 /* Try to read more, right-away */
2178 i++;
2179 }
2180
2181 r = bus_read_message(bus, false0, 0);
2182 if (r < 0) {
2183 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-107, -104, -32, -108})/sizeof(int)]; switch
(r) { case -107: case -104: case -32: case -108: _found = 1; break
; default: break; } _found; })
) {
2184 bus_enter_closing(bus);
2185 r = -ECONNRESET104;
2186 }
2187
2188 goto fail;
2189 }
2190 if (r > 0)
2191 continue;
2192
2193 if (timeout > 0) {
2194 usec_t n;
2195
2196 n = now(CLOCK_MONOTONIC1);
2197 if (n >= timeout) {
2198 r = -ETIMEDOUT110;
2199 goto fail;
2200 }
2201
2202 left = timeout - n;
2203 } else
2204 left = (uint64_t) -1;
2205
2206 r = bus_poll(bus, true1, left);
2207 if (r < 0)
2208 goto fail;
2209 if (r == 0) {
2210 r = -ETIMEDOUT110;
2211 goto fail;
2212 }
2213
2214 r = dispatch_wqueue(bus);
2215 if (r < 0) {
2216 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-107, -104, -32, -108})/sizeof(int)]; switch
(r) { case -107: case -104: case -32: case -108: _found = 1; break
; default: break; } _found; })
) {
2217 bus_enter_closing(bus);
2218 r = -ECONNRESET104;
2219 }
2220
2221 goto fail;
2222 }
2223 }
2224
2225fail:
2226 return sd_bus_error_set_errno(error, r);
2227}
2228
2229_public___attribute__ ((visibility("default"))) int sd_bus_get_fd(sd_bus *bus) {
2230
2231 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2231, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
2232 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2232, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
2233 assert_return(bus->input_fd == bus->output_fd, -EPERM)do { if (!(((__builtin_expect(!!(bus->input_fd == bus->
output_fd),1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("bus->input_fd == bus->output_fd"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2233, __PRETTY_FUNCTION__), 0))) return (-1); } while (0)
;
2234 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 2234, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
2235
2236 if (bus->state == BUS_CLOSED)
2237 return -ENOTCONN107;
2238
2239 if (bus->inotify_fd >= 0)
2240 return bus->inotify_fd;
2241
2242 if (bus->input_fd >= 0)
2243 return bus->input_fd;
2244
2245 return -ENOTCONN107;
2246}
2247
2248_public___attribute__ ((visibility("default"))) int sd_bus_get_events(sd_bus *bus) {
2249 int flags = 0;
2250
2251 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2251, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
2252 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2252, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
2253 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 2253, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
2254
2255 switch (bus->state) {
2256
2257 case BUS_UNSET:
2258 case BUS_CLOSED:
2259 return -ENOTCONN107;
2260
2261 case BUS_WATCH_BIND:
2262 flags |= POLLIN0x001;
2263 break;
2264
2265 case BUS_OPENING:
2266 flags |= POLLOUT0x004;
2267 break;
2268
2269 case BUS_AUTHENTICATING:
2270 if (bus_socket_auth_needs_write(bus))
2271 flags |= POLLOUT0x004;
2272
2273 flags |= POLLIN0x001;
2274 break;
2275
2276 case BUS_RUNNING:
2277 case BUS_HELLO:
2278 if (bus->rqueue_size <= 0)
2279 flags |= POLLIN0x001;
2280 if (bus->wqueue_size > 0)
2281 flags |= POLLOUT0x004;
2282 break;
2283
2284 case BUS_CLOSING:
2285 break;
2286
2287 default:
2288 assert_not_reached("Unknown state")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unknown state"), "../src/libsystemd/sd-bus/sd-bus.c", 2288, __PRETTY_FUNCTION__
); } while (0)
;
2289 }
2290
2291 return flags;
2292}
2293
2294_public___attribute__ ((visibility("default"))) int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
2295 struct reply_callback *c;
2296
2297 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2297, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
2298 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2298, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
2299 assert_return(timeout_usec, -EINVAL)do { if (!(((__builtin_expect(!!(timeout_usec),1))) ? (1) : (
log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("timeout_usec"
), "../src/libsystemd/sd-bus/sd-bus.c", 2299, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
2300 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 2300, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
2301
2302 if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
2303 return -ENOTCONN107;
2304
2305 if (bus->track_queue) {
2306 *timeout_usec = 0;
2307 return 1;
2308 }
2309
2310 switch (bus->state) {
2311
2312 case BUS_AUTHENTICATING:
2313 *timeout_usec = bus->auth_timeout;
2314 return 1;
2315
2316 case BUS_RUNNING:
2317 case BUS_HELLO:
2318 if (bus->rqueue_size > 0) {
2319 *timeout_usec = 0;
2320 return 1;
2321 }
2322
2323 c = prioq_peek(bus->reply_callbacks_prioq);
2324 if (!c) {
2325 *timeout_usec = (uint64_t) -1;
2326 return 0;
2327 }
2328
2329 if (c->timeout_usec == 0) {
2330 *timeout_usec = (uint64_t) -1;
2331 return 0;
2332 }
2333
2334 *timeout_usec = c->timeout_usec;
2335 return 1;
2336
2337 case BUS_CLOSING:
2338 *timeout_usec = 0;
2339 return 1;
2340
2341 case BUS_WATCH_BIND:
2342 case BUS_OPENING:
2343 *timeout_usec = (uint64_t) -1;
2344 return 0;
2345
2346 default:
2347 assert_not_reached("Unknown or unexpected stat")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unknown or unexpected stat"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2347, __PRETTY_FUNCTION__); } while (0)
;
2348 }
2349}
2350
2351static int process_timeout(sd_bus *bus) {
2352 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error_buffer = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2353 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message* m = NULL((void*)0);
2354 struct reply_callback *c;
2355 sd_bus_slot *slot;
2356 bool_Bool is_hello;
2357 usec_t n;
2358 int r;
2359
2360 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2360, __PRETTY_FUNCTION__); } while (0)
;
2361 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO))do { if ((__builtin_expect(!!(!(({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){BUS_RUNNING, BUS_HELLO})/sizeof(int)]; switch
(bus->state) { case BUS_RUNNING: case BUS_HELLO: _found = 1
; break; default: break; } _found; }))),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)"
), "../src/libsystemd/sd-bus/sd-bus.c", 2361, __PRETTY_FUNCTION__
); } while (0)
;
2362
2363 c = prioq_peek(bus->reply_callbacks_prioq);
2364 if (!c)
2365 return 0;
2366
2367 n = now(CLOCK_MONOTONIC1);
2368 if (c->timeout_usec > n)
2369 return 0;
2370
2371 r = bus_message_new_synthetic_error(
2372 bus,
2373 c->cookie,
2374 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out")((const sd_bus_error) {("org.freedesktop.DBus.Error.NoReply")
, ("Method call timed out"), 0})
,
2375 &m);
2376 if (r < 0)
2377 return r;
2378
2379 r = bus_seal_synthetic_message(bus, m);
2380 if (r < 0)
2381 return r;
2382
2383 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c)do { if ((__builtin_expect(!!(!(prioq_pop(bus->reply_callbacks_prioq
) == c)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("prioq_pop(bus->reply_callbacks_prioq) == c"
), "../src/libsystemd/sd-bus/sd-bus.c", 2383, __PRETTY_FUNCTION__
); } while (0)
;
2384 c->timeout_usec = 0;
2385
2386 ordered_hashmap_remove(bus->reply_callbacks, &c->cookie);
2387 c->cookie = 0;
2388
2389 slot = container_of(c, sd_bus_slot, reply_callback)__extension__ ({ const typeof( ((sd_bus_slot*)0)->reply_callback
) *__unique_prefix_A42 = ((c)); (sd_bus_slot*)( (char *)__unique_prefix_A42
- __builtin_offsetof(sd_bus_slot, reply_callback) ); })
;
2390
2391 bus->iteration_counter++;
2392
2393 is_hello = bus->state == BUS_HELLO && c->callback == hello_callback;
2394
2395 bus->current_message = m;
2396 bus->current_slot = sd_bus_slot_ref(slot);
2397 bus->current_handler = c->callback;
2398 bus->current_userdata = slot->userdata;
2399 r = c->callback(m, slot->userdata, &error_buffer);
2400 bus->current_userdata = NULL((void*)0);
2401 bus->current_handler = NULL((void*)0);
2402 bus->current_slot = NULL((void*)0);
2403 bus->current_message = NULL((void*)0);
2404
2405 if (slot->floating) {
2406 bus_slot_disconnect(slot);
2407 sd_bus_slot_unref(slot);
2408 }
2409
2410 sd_bus_slot_unref(slot);
2411
2412 /* When this is the hello message and it timed out, then make sure to propagate the error up, don't just log
2413 * and ignore the callback handler's return value. */
2414 if (is_hello)
2415 return r;
2416
2417 return bus_maybe_reply_error(m, r, &error_buffer);
2418}
2419
2420static int process_hello(sd_bus *bus, sd_bus_message *m) {
2421 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2421, __PRETTY_FUNCTION__); } while (0)
;
2422 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2422, __PRETTY_FUNCTION__); } while (0)
;
2423
2424 if (bus->state != BUS_HELLO)
2425 return 0;
2426
2427 /* Let's make sure the first message on the bus is the HELLO
2428 * reply. But note that we don't actually parse the message
2429 * here (we leave that to the usual handling), we just verify
2430 * we don't let any earlier msg through. */
2431
2432 if (!IN_SET(m->header->type, SD_BUS_MESSAGE_METHOD_RETURN, SD_BUS_MESSAGE_METHOD_ERROR)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){SD_BUS_MESSAGE_METHOD_RETURN, SD_BUS_MESSAGE_METHOD_ERROR
})/sizeof(int)]; switch(m->header->type) { case SD_BUS_MESSAGE_METHOD_RETURN
: case SD_BUS_MESSAGE_METHOD_ERROR: _found = 1; break; default
: break; } _found; })
)
2433 return -EIO5;
2434
2435 if (m->reply_cookie != 1)
2436 return -EIO5;
2437
2438 return 0;
2439}
2440
2441static int process_reply(sd_bus *bus, sd_bus_message *m) {
2442 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *synthetic_reply = NULL((void*)0);
2443 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error_buffer = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2444 struct reply_callback *c;
2445 sd_bus_slot *slot;
2446 bool_Bool is_hello;
2447 int r;
2448
2449 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2449, __PRETTY_FUNCTION__); } while (0)
;
2450 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2450, __PRETTY_FUNCTION__); } while (0)
;
2451
2452 if (!IN_SET(m->header->type, SD_BUS_MESSAGE_METHOD_RETURN, SD_BUS_MESSAGE_METHOD_ERROR)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){SD_BUS_MESSAGE_METHOD_RETURN, SD_BUS_MESSAGE_METHOD_ERROR
})/sizeof(int)]; switch(m->header->type) { case SD_BUS_MESSAGE_METHOD_RETURN
: case SD_BUS_MESSAGE_METHOD_ERROR: _found = 1; break; default
: break; } _found; })
)
2453 return 0;
2454
2455 if (m->destination && bus->unique_name && !streq_ptr(m->destination, bus->unique_name))
2456 return 0;
2457
2458 c = ordered_hashmap_remove(bus->reply_callbacks, &m->reply_cookie);
2459 if (!c)
2460 return 0;
2461
2462 c->cookie = 0;
2463
2464 slot = container_of(c, sd_bus_slot, reply_callback)__extension__ ({ const typeof( ((sd_bus_slot*)0)->reply_callback
) *__unique_prefix_A43 = ((c)); (sd_bus_slot*)( (char *)__unique_prefix_A43
- __builtin_offsetof(sd_bus_slot, reply_callback) ); })
;
2465
2466 if (m->n_fds > 0 && !bus->accept_fd) {
2467
2468 /* If the reply contained a file descriptor which we
2469 * didn't want we pass an error instead. */
2470
2471 r = bus_message_new_synthetic_error(
2472 bus,
2473 m->reply_cookie,
2474 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptor")((const sd_bus_error) {("org.freedesktop.DBus.Error.InconsistentMessage"
), ("Reply message contained file descriptor"), 0})
,
2475 &synthetic_reply);
2476 if (r < 0)
2477 return r;
2478
2479 /* Copy over original timestamp */
2480 synthetic_reply->realtime = m->realtime;
2481 synthetic_reply->monotonic = m->monotonic;
2482 synthetic_reply->seqnum = m->seqnum;
2483
2484 r = bus_seal_synthetic_message(bus, synthetic_reply);
2485 if (r < 0)
2486 return r;
2487
2488 m = synthetic_reply;
2489 } else {
2490 r = sd_bus_message_rewind(m, true1);
2491 if (r < 0)
2492 return r;
2493 }
2494
2495 if (c->timeout_usec != 0) {
2496 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2497 c->timeout_usec = 0;
2498 }
2499
2500 is_hello = bus->state == BUS_HELLO && c->callback == hello_callback;
2501
2502 bus->current_slot = sd_bus_slot_ref(slot);
2503 bus->current_handler = c->callback;
2504 bus->current_userdata = slot->userdata;
2505 r = c->callback(m, slot->userdata, &error_buffer);
2506 bus->current_userdata = NULL((void*)0);
2507 bus->current_handler = NULL((void*)0);
2508 bus->current_slot = NULL((void*)0);
2509
2510 if (slot->floating) {
2511 bus_slot_disconnect(slot);
2512 sd_bus_slot_unref(slot);
2513 }
2514
2515 sd_bus_slot_unref(slot);
2516
2517 /* When this is the hello message and it failed, then make sure to propagate the error up, don't just log and
2518 * ignore the callback handler's return value. */
2519 if (is_hello)
2520 return r;
2521
2522 return bus_maybe_reply_error(m, r, &error_buffer);
2523}
2524
2525static int process_filter(sd_bus *bus, sd_bus_message *m) {
2526 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error_buffer = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2527 struct filter_callback *l;
2528 int r;
2529
2530 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2530, __PRETTY_FUNCTION__); } while (0)
;
2531 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2531, __PRETTY_FUNCTION__); } while (0)
;
2532
2533 do {
2534 bus->filter_callbacks_modified = false0;
2535
2536 LIST_FOREACH(callbacks, l, bus->filter_callbacks)for ((l) = (bus->filter_callbacks); (l); (l) = (l)->callbacks_next
)
{
2537 sd_bus_slot *slot;
2538
2539 if (bus->filter_callbacks_modified)
2540 break;
2541
2542 /* Don't run this more than once per iteration */
2543 if (l->last_iteration == bus->iteration_counter)
2544 continue;
2545
2546 l->last_iteration = bus->iteration_counter;
2547
2548 r = sd_bus_message_rewind(m, true1);
2549 if (r < 0)
2550 return r;
2551
2552 slot = container_of(l, sd_bus_slot, filter_callback)__extension__ ({ const typeof( ((sd_bus_slot*)0)->filter_callback
) *__unique_prefix_A44 = ((l)); (sd_bus_slot*)( (char *)__unique_prefix_A44
- __builtin_offsetof(sd_bus_slot, filter_callback) ); })
;
2553
2554 bus->current_slot = sd_bus_slot_ref(slot);
2555 bus->current_handler = l->callback;
2556 bus->current_userdata = slot->userdata;
2557 r = l->callback(m, slot->userdata, &error_buffer);
2558 bus->current_userdata = NULL((void*)0);
2559 bus->current_handler = NULL((void*)0);
2560 bus->current_slot = sd_bus_slot_unref(slot);
2561
2562 r = bus_maybe_reply_error(m, r, &error_buffer);
2563 if (r != 0)
2564 return r;
2565
2566 }
2567
2568 } while (bus->filter_callbacks_modified);
2569
2570 return 0;
2571}
2572
2573static int process_match(sd_bus *bus, sd_bus_message *m) {
2574 int r;
2575
2576 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2576, __PRETTY_FUNCTION__); } while (0)
;
2577 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2577, __PRETTY_FUNCTION__); } while (0)
;
2578
2579 do {
2580 bus->match_callbacks_modified = false0;
2581
2582 r = bus_match_run(bus, &bus->match_callbacks, m);
2583 if (r != 0)
2584 return r;
2585
2586 } while (bus->match_callbacks_modified);
2587
2588 return 0;
2589}
2590
2591static int process_builtin(sd_bus *bus, sd_bus_message *m) {
2592 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0);
2593 int r;
2594
2595 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2595, __PRETTY_FUNCTION__); } while (0)
;
2596 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2596, __PRETTY_FUNCTION__); } while (0)
;
2597
2598 if (bus->is_monitor)
2599 return 0;
2600
2601 if (bus->manual_peer_interface)
2602 return 0;
2603
2604 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2605 return 0;
2606
2607 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
2608 return 0;
2609
2610 if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
2611 return 1;
2612
2613 if (streq_ptr(m->member, "Ping"))
2614 r = sd_bus_message_new_method_return(m, &reply);
2615 else if (streq_ptr(m->member, "GetMachineId")) {
2616 sd_id128_t id;
2617 char sid[33];
2618
2619 r = sd_id128_get_machine(&id);
2620 if (r < 0)
2621 return r;
2622
2623 r = sd_bus_message_new_method_return(m, &reply);
2624 if (r < 0)
2625 return r;
2626
2627 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
2628 } else {
2629 r = sd_bus_message_new_method_errorf(
2630 m, &reply,
2631 SD_BUS_ERROR_UNKNOWN_METHOD"org.freedesktop.DBus.Error.UnknownMethod",
2632 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
2633 }
2634 if (r < 0)
2635 return r;
2636
2637 r = sd_bus_send(bus, reply, NULL((void*)0));
2638 if (r < 0)
2639 return r;
2640
2641 return 1;
2642}
2643
2644static int process_fd_check(sd_bus *bus, sd_bus_message *m) {
2645 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2645, __PRETTY_FUNCTION__); } while (0)
;
2646 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2646, __PRETTY_FUNCTION__); } while (0)
;
2647
2648 /* If we got a message with a file descriptor which we didn't
2649 * want to accept, then let's drop it. How can this even
2650 * happen? For example, when the kernel queues a message into
2651 * an activatable names's queue which allows fds, and then is
2652 * delivered to us later even though we ourselves did not
2653 * negotiate it. */
2654
2655 if (bus->is_monitor)
2656 return 0;
2657
2658 if (m->n_fds <= 0)
2659 return 0;
2660
2661 if (bus->accept_fd)
2662 return 0;
2663
2664 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2665 return 1; /* just eat it up */
2666
2667 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INCONSISTENT_MESSAGE"org.freedesktop.DBus.Error.InconsistentMessage", "Message contains file descriptors, which I cannot accept. Sorry.");
2668}
2669
2670static int process_message(sd_bus *bus, sd_bus_message *m) {
2671 int r;
2672
2673 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2673, __PRETTY_FUNCTION__); } while (0)
;
2674 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2674, __PRETTY_FUNCTION__); } while (0)
;
2675
2676 bus->current_message = m;
2677 bus->iteration_counter++;
2678
2679 log_debug_bus_message(m)do { sd_bus_message *_mm = (m); ({ int _level = (((7))), _e =
((0)), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm
(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((
_realm) << 10 | (_level)), _e, "../src/libsystemd/sd-bus/sd-bus.c"
, 2679, __func__, "Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%"
"l" "u" " reply_cookie=%" "l" "u" " signature=%s error-name=%s error-message=%s"
, bus_message_type_to_string(_mm->header->type), strna(
sd_bus_message_get_sender(_mm)), strna(sd_bus_message_get_destination
(_mm)), strna(sd_bus_message_get_path(_mm)), strna(sd_bus_message_get_interface
(_mm)), strna(sd_bus_message_get_member(_mm)), BUS_MESSAGE_COOKIE
(_mm), _mm->reply_cookie, strna(_mm->root_container.signature
), strna(_mm->error.name), strna(_mm->error.message)) :
-abs(_e); }); } while (0)
;
2680
2681 r = process_hello(bus, m);
2682 if (r != 0)
2683 goto finish;
2684
2685 r = process_reply(bus, m);
2686 if (r != 0)
2687 goto finish;
2688
2689 r = process_fd_check(bus, m);
2690 if (r != 0)
2691 goto finish;
2692
2693 r = process_filter(bus, m);
2694 if (r != 0)
2695 goto finish;
2696
2697 r = process_match(bus, m);
2698 if (r != 0)
2699 goto finish;
2700
2701 r = process_builtin(bus, m);
2702 if (r != 0)
2703 goto finish;
2704
2705 r = bus_process_object(bus, m);
2706
2707finish:
2708 bus->current_message = NULL((void*)0);
2709 return r;
2710}
2711
2712static int dispatch_track(sd_bus *bus) {
2713 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2713, __PRETTY_FUNCTION__); } while (0)
;
2714
2715 if (!bus->track_queue)
2716 return 0;
2717
2718 bus_track_dispatch(bus->track_queue);
2719 return 1;
2720}
2721
2722static int process_running(sd_bus *bus, bool_Bool hint_priority, int64_t priority, sd_bus_message **ret) {
2723 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
2724 int r;
2725
2726 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2726, __PRETTY_FUNCTION__); } while (0)
;
2727 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO))do { if ((__builtin_expect(!!(!(({ _Bool _found = 0; static __attribute__
((unused)) char _static_assert__macros_need_to_be_extended[20
- sizeof((int[]){BUS_RUNNING, BUS_HELLO})/sizeof(int)]; switch
(bus->state) { case BUS_RUNNING: case BUS_HELLO: _found = 1
; break; default: break; } _found; }))),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)"
), "../src/libsystemd/sd-bus/sd-bus.c", 2727, __PRETTY_FUNCTION__
); } while (0)
;
2728
2729 r = process_timeout(bus);
2730 if (r != 0)
2731 goto null_message;
2732
2733 r = dispatch_wqueue(bus);
2734 if (r != 0)
2735 goto null_message;
2736
2737 r = dispatch_track(bus);
2738 if (r != 0)
2739 goto null_message;
2740
2741 r = dispatch_rqueue(bus, hint_priority, priority, &m);
2742 if (r < 0)
2743 return r;
2744 if (!m)
2745 goto null_message;
2746
2747 r = process_message(bus, m);
2748 if (r != 0)
2749 goto null_message;
2750
2751 if (ret) {
2752 r = sd_bus_message_rewind(m, true1);
2753 if (r < 0)
2754 return r;
2755
2756 *ret = TAKE_PTR(m)({ typeof(m) _ptr_ = (m); (m) = ((void*)0); _ptr_; });
2757 return 1;
2758 }
2759
2760 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2761
2762 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 2766, __func__, "Unprocessed message call sender=%s object=%s interface=%s member=%s"
, strna(sd_bus_message_get_sender(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m))) : -abs(_e); })
2763 strna(sd_bus_message_get_sender(m)),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 2766, __func__, "Unprocessed message call sender=%s object=%s interface=%s member=%s"
, strna(sd_bus_message_get_sender(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m))) : -abs(_e); })
2764 strna(sd_bus_message_get_path(m)),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 2766, __func__, "Unprocessed message call sender=%s object=%s interface=%s member=%s"
, strna(sd_bus_message_get_sender(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m))) : -abs(_e); })
2765 strna(sd_bus_message_get_interface(m)),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 2766, __func__, "Unprocessed message call sender=%s object=%s interface=%s member=%s"
, strna(sd_bus_message_get_sender(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m))) : -abs(_e); })
2766 strna(sd_bus_message_get_member(m)))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 2766, __func__, "Unprocessed message call sender=%s object=%s interface=%s member=%s"
, strna(sd_bus_message_get_sender(m)), strna(sd_bus_message_get_path
(m)), strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member
(m))) : -abs(_e); })
;
2767
2768 r = sd_bus_reply_method_errorf(
2769 m,
2770 SD_BUS_ERROR_UNKNOWN_OBJECT"org.freedesktop.DBus.Error.UnknownObject",
2771 "Unknown object '%s'.", m->path);
2772 if (r < 0)
2773 return r;
2774 }
2775
2776 return 1;
2777
2778null_message:
2779 if (r >= 0 && ret)
2780 *ret = NULL((void*)0);
2781
2782 return r;
2783}
2784
2785static int bus_exit_now(sd_bus *bus) {
2786 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2786, __PRETTY_FUNCTION__); } while (0)
;
2787
2788 /* Exit due to close, if this is requested. If this is bus object is attached to an event source, invokes
2789 * sd_event_exit(), otherwise invokes libc exit(). */
2790
2791 if (bus->exited) /* did we already exit? */
2792 return 0;
2793 if (!bus->exit_triggered) /* was the exit condition triggered? */
2794 return 0;
2795 if (!bus->exit_on_disconnect) /* Shall we actually exit on disconnection? */
2796 return 0;
2797
2798 bus->exited = true1; /* never exit more than once */
2799
2800 log_debug("Bus connection disconnected, exiting.")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 2800, __func__, "Bus connection disconnected, exiting."
) : -abs(_e); })
;
2801
2802 if (bus->event)
2803 return sd_event_exit(bus->event, EXIT_FAILURE1);
2804 else
2805 exit(EXIT_FAILURE1);
2806
2807 assert_not_reached("exit() didn't exit?")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"exit() didn't exit?"), "../src/libsystemd/sd-bus/sd-bus.c", 2807
, __PRETTY_FUNCTION__); } while (0)
;
2808}
2809
2810static int process_closing_reply_callback(sd_bus *bus, struct reply_callback *c) {
2811 _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error_buffer = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0});
2812 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
2813 sd_bus_slot *slot;
2814 int r;
2815
2816 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2816, __PRETTY_FUNCTION__); } while (0)
;
2817 assert(c)do { if ((__builtin_expect(!!(!(c)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2817, __PRETTY_FUNCTION__); } while (0)
;
2818
2819 r = bus_message_new_synthetic_error(
2820 bus,
2821 c->cookie,
2822 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated")((const sd_bus_error) {("org.freedesktop.DBus.Error.NoReply")
, ("Connection terminated"), 0})
,
2823 &m);
2824 if (r < 0)
2825 return r;
2826
2827 r = bus_seal_synthetic_message(bus, m);
2828 if (r < 0)
2829 return r;
2830
2831 if (c->timeout_usec != 0) {
2832 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2833 c->timeout_usec = 0;
2834 }
2835
2836 ordered_hashmap_remove(bus->reply_callbacks, &c->cookie);
2837 c->cookie = 0;
2838
2839 slot = container_of(c, sd_bus_slot, reply_callback)__extension__ ({ const typeof( ((sd_bus_slot*)0)->reply_callback
) *__unique_prefix_A45 = ((c)); (sd_bus_slot*)( (char *)__unique_prefix_A45
- __builtin_offsetof(sd_bus_slot, reply_callback) ); })
;
2840
2841 bus->iteration_counter++;
2842
2843 bus->current_message = m;
2844 bus->current_slot = sd_bus_slot_ref(slot);
2845 bus->current_handler = c->callback;
2846 bus->current_userdata = slot->userdata;
2847 r = c->callback(m, slot->userdata, &error_buffer);
2848 bus->current_userdata = NULL((void*)0);
2849 bus->current_handler = NULL((void*)0);
2850 bus->current_slot = NULL((void*)0);
2851 bus->current_message = NULL((void*)0);
2852
2853 if (slot->floating) {
2854 bus_slot_disconnect(slot);
2855 sd_bus_slot_unref(slot);
2856 }
2857
2858 sd_bus_slot_unref(slot);
2859
2860 return bus_maybe_reply_error(m, r, &error_buffer);
2861}
2862
2863static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2864 _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0);
2865 struct reply_callback *c;
2866 int r;
2867
2868 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2868, __PRETTY_FUNCTION__); } while (0)
;
2869 assert(bus->state == BUS_CLOSING)do { if ((__builtin_expect(!!(!(bus->state == BUS_CLOSING)
),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("bus->state == BUS_CLOSING"
), "../src/libsystemd/sd-bus/sd-bus.c", 2869, __PRETTY_FUNCTION__
); } while (0)
;
2870
2871 /* First, fail all outstanding method calls */
2872 c = ordered_hashmap_first(bus->reply_callbacks);
2873 if (c)
2874 return process_closing_reply_callback(bus, c);
2875
2876 /* Then, fake-drop all remaining bus tracking references */
2877 if (bus->tracks) {
2878 bus_track_close(bus->tracks);
2879 return 1;
2880 }
2881
2882 /* Then, synthesize a Disconnected message */
2883 r = sd_bus_message_new_signal(
2884 bus,
2885 &m,
2886 "/org/freedesktop/DBus/Local",
2887 "org.freedesktop.DBus.Local",
2888 "Disconnected");
2889 if (r < 0)
2890 return r;
2891
2892 bus_message_set_sender_local(bus, m);
2893
2894 r = bus_seal_synthetic_message(bus, m);
2895 if (r < 0)
2896 return r;
2897
2898 sd_bus_close(bus);
2899
2900 bus->current_message = m;
2901 bus->iteration_counter++;
2902
2903 r = process_filter(bus, m);
2904 if (r != 0)
2905 goto finish;
2906
2907 r = process_match(bus, m);
2908 if (r != 0)
2909 goto finish;
2910
2911 /* Nothing else to do, exit now, if the condition holds */
2912 bus->exit_triggered = true1;
2913 (void) bus_exit_now(bus);
2914
2915 if (ret)
2916 *ret = TAKE_PTR(m)({ typeof(m) _ptr_ = (m); (m) = ((void*)0); _ptr_; });
2917
2918 r = 1;
2919
2920finish:
2921 bus->current_message = NULL((void*)0);
2922
2923 return r;
2924}
2925
2926static int bus_process_internal(sd_bus *bus, bool_Bool hint_priority, int64_t priority, sd_bus_message **ret) {
2927 int r;
2928
2929 /* Returns 0 when we didn't do anything. This should cause the
2930 * caller to invoke sd_bus_wait() before returning the next
2931 * time. Returns > 0 when we did something, which possibly
2932 * means *ret is filled in with an unprocessed message. */
2933
2934 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2934, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
2935 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2935, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
2936 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 2936, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
2937
2938 /* We don't allow recursively invoking sd_bus_process(). */
2939 assert_return(!bus->current_message, -EBUSY)do { if (!(((__builtin_expect(!!(!bus->current_message),1)
)) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD,
("!bus->current_message"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2939, __PRETTY_FUNCTION__), 0))) return (-16); } while (0)
;
2940 assert(!bus->current_slot)do { if ((__builtin_expect(!!(!(!bus->current_slot)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("!bus->current_slot"), "../src/libsystemd/sd-bus/sd-bus.c"
, 2940, __PRETTY_FUNCTION__); } while (0)
;
2941
2942 BUS_DONT_DESTROY(bus)__attribute__((cleanup(sd_bus_unrefp))) __attribute__ ((unused
)) sd_bus *_dont_destroy_bus = sd_bus_ref(bus)
;
2943
2944 switch (bus->state) {
2945
2946 case BUS_UNSET:
2947 return -ENOTCONN107;
2948
2949 case BUS_CLOSED:
2950 return -ECONNRESET104;
2951
2952 case BUS_WATCH_BIND:
2953 r = bus_socket_process_watch_bind(bus);
2954 break;
2955
2956 case BUS_OPENING:
2957 r = bus_socket_process_opening(bus);
2958 break;
2959
2960 case BUS_AUTHENTICATING:
2961 r = bus_socket_process_authenticating(bus);
2962 break;
2963
2964 case BUS_RUNNING:
2965 case BUS_HELLO:
2966 r = process_running(bus, hint_priority, priority, ret);
2967 if (r >= 0)
2968 return r;
2969
2970 /* This branch initializes *ret, hence we don't use the generic error checking below */
2971 break;
2972
2973 case BUS_CLOSING:
2974 return process_closing(bus, ret);
2975
2976 default:
2977 assert_not_reached("Unknown state")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unknown state"), "../src/libsystemd/sd-bus/sd-bus.c", 2977, __PRETTY_FUNCTION__
); } while (0)
;
2978 }
2979
2980 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-107, -104, -32, -108})/sizeof(int)]; switch
(r) { case -107: case -104: case -32: case -108: _found = 1; break
; default: break; } _found; })
) {
2981 bus_enter_closing(bus);
2982 r = 1;
2983 } else if (r < 0)
2984 return r;
2985
2986 if (ret)
2987 *ret = NULL((void*)0);
2988
2989 return r;
2990}
2991
2992_public___attribute__ ((visibility("default"))) int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2993 return bus_process_internal(bus, false0, 0, ret);
2994}
2995
2996_public___attribute__ ((visibility("default"))) int sd_bus_process_priority(sd_bus *bus, int64_t priority, sd_bus_message **ret) {
2997 return bus_process_internal(bus, true1, priority, ret);
2998}
2999
3000static int bus_poll(sd_bus *bus, bool_Bool need_more, uint64_t timeout_usec) {
3001 struct pollfd p[2] = {};
3002 int r, n;
3003 struct timespec ts;
3004 usec_t m = USEC_INFINITY((usec_t) -1);
3005
3006 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3006, __PRETTY_FUNCTION__); } while (0)
;
3007
3008 if (bus->state == BUS_CLOSING)
3009 return 1;
3010
3011 if (!BUS_IS_OPEN(bus->state))
3012 return -ENOTCONN107;
3013
3014 if (bus->state == BUS_WATCH_BIND) {
3015 assert(bus->inotify_fd >= 0)do { if ((__builtin_expect(!!(!(bus->inotify_fd >= 0)),
0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("bus->inotify_fd >= 0"
), "../src/libsystemd/sd-bus/sd-bus.c", 3015, __PRETTY_FUNCTION__
); } while (0)
;
3016
3017 p[0].events = POLLIN0x001;
3018 p[0].fd = bus->inotify_fd;
3019 n = 1;
3020 } else {
3021 int e;
3022
3023 e = sd_bus_get_events(bus);
3024 if (e < 0)
3025 return e;
3026
3027 if (need_more)
3028 /* The caller really needs some more data, he doesn't
3029 * care about what's already read, or any timeouts
3030 * except its own. */
3031 e |= POLLIN0x001;
3032 else {
3033 usec_t until;
3034 /* The caller wants to process if there's something to
3035 * process, but doesn't care otherwise */
3036
3037 r = sd_bus_get_timeout(bus, &until);
3038 if (r < 0)
3039 return r;
3040 if (r > 0)
3041 m = usec_sub_unsigned(until, now(CLOCK_MONOTONIC1));
3042 }
3043
3044 p[0].fd = bus->input_fd;
3045 if (bus->output_fd == bus->input_fd) {
3046 p[0].events = e;
3047 n = 1;
3048 } else {
3049 p[0].events = e & POLLIN0x001;
3050 p[1].fd = bus->output_fd;
3051 p[1].events = e & POLLOUT0x004;
3052 n = 2;
3053 }
3054 }
3055
3056 if (timeout_usec != (uint64_t) -1 && (m == USEC_INFINITY((usec_t) -1) || timeout_usec < m))
3057 m = timeout_usec;
3058
3059 r = ppoll(p, n, m == USEC_INFINITY((usec_t) -1) ? NULL((void*)0) : timespec_store(&ts, m), NULL((void*)0));
3060 if (r < 0)
3061 return -errno(*__errno_location ());
3062
3063 return r > 0 ? 1 : 0;
3064}
3065
3066_public___attribute__ ((visibility("default"))) int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
3067
3068 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3068, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3069 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3069, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
3070 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 3070, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
3071
3072 if (bus->state == BUS_CLOSING)
3073 return 0;
3074
3075 if (!BUS_IS_OPEN(bus->state))
3076 return -ENOTCONN107;
3077
3078 if (bus->rqueue_size > 0)
3079 return 0;
3080
3081 return bus_poll(bus, false0, timeout_usec);
3082}
3083
3084_public___attribute__ ((visibility("default"))) int sd_bus_flush(sd_bus *bus) {
3085 int r;
3086
3087 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3087, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3088 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3088, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
3089 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 3089, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
3090
3091 if (bus->state == BUS_CLOSING)
3092 return 0;
3093
3094 if (!BUS_IS_OPEN(bus->state))
3095 return -ENOTCONN107;
3096
3097 /* We never were connected? Don't hang in inotify for good, as there's no timeout set for it */
3098 if (bus->state == BUS_WATCH_BIND)
3099 return -EUNATCH49;
3100
3101 r = bus_ensure_running(bus);
3102 if (r < 0)
3103 return r;
3104
3105 if (bus->wqueue_size <= 0)
3106 return 0;
3107
3108 for (;;) {
3109 r = dispatch_wqueue(bus);
3110 if (r < 0) {
3111 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-107, -104, -32, -108})/sizeof(int)]; switch
(r) { case -107: case -104: case -32: case -108: _found = 1; break
; default: break; } _found; })
) {
3112 bus_enter_closing(bus);
3113 return -ECONNRESET104;
3114 }
3115
3116 return r;
3117 }
3118
3119 if (bus->wqueue_size <= 0)
3120 return 0;
3121
3122 r = bus_poll(bus, false0, (uint64_t) -1);
3123 if (r < 0)
3124 return r;
3125 }
3126}
3127
3128_public___attribute__ ((visibility("default"))) int sd_bus_add_filter(
3129 sd_bus *bus,
3130 sd_bus_slot **slot,
3131 sd_bus_message_handler_t callback,
3132 void *userdata) {
3133
3134 sd_bus_slot *s;
3135
3136 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3136, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3137 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3137, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
3138 assert_return(callback, -EINVAL)do { if (!(((__builtin_expect(!!(callback),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("callback"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3138, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3139 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 3139, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
3140
3141 s = bus_slot_allocate(bus, !slot, BUS_FILTER_CALLBACK, sizeof(struct filter_callback), userdata);
3142 if (!s)
3143 return -ENOMEM12;
3144
3145 s->filter_callback.callback = callback;
3146
3147 bus->filter_callbacks_modified = true1;
3148 LIST_PREPEND(callbacks, bus->filter_callbacks, &s->filter_callback)do { typeof(*(bus->filter_callbacks)) **_head = &(bus->
filter_callbacks), *_item = (&s->filter_callback); do {
if ((__builtin_expect(!!(!(_item)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("_item"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3148, __PRETTY_FUNCTION__); } while (0); if ((_item->callbacks_next
= *_head)) _item->callbacks_next->callbacks_prev = _item
; _item->callbacks_prev = ((void*)0); *_head = _item; } while
(0)
;
3149
3150 if (slot)
3151 *slot = s;
3152
3153 return 0;
3154}
3155
3156static int add_match_callback(
3157 sd_bus_message *m,
3158 void *userdata,
3159 sd_bus_error *ret_error) {
3160
3161 sd_bus_slot *match_slot = userdata;
3162 bool_Bool failed = false0;
3163 int r;
3164
3165 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3165, __PRETTY_FUNCTION__); } while (0)
;
3166 assert(match_slot)do { if ((__builtin_expect(!!(!(match_slot)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("match_slot"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3166, __PRETTY_FUNCTION__); } while (0)
;
3167
3168 sd_bus_slot_ref(match_slot);
3169
3170 if (sd_bus_message_is_method_error(m, NULL((void*)0))) {
3171 log_debug_errno(sd_bus_message_get_errno(m),({ int _level = ((7)), _e = ((sd_bus_message_get_errno(m))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/libsystemd/sd-bus/sd-bus.c", 3174
, __func__, "Unable to add match %s, failing connection: %s",
match_slot->match_callback.match_string, sd_bus_message_get_error
(m)->message) : -abs(_e); })
3172 "Unable to add match %s, failing connection: %s",({ int _level = ((7)), _e = ((sd_bus_message_get_errno(m))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/libsystemd/sd-bus/sd-bus.c", 3174
, __func__, "Unable to add match %s, failing connection: %s",
match_slot->match_callback.match_string, sd_bus_message_get_error
(m)->message) : -abs(_e); })
3173 match_slot->match_callback.match_string,({ int _level = ((7)), _e = ((sd_bus_message_get_errno(m))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/libsystemd/sd-bus/sd-bus.c", 3174
, __func__, "Unable to add match %s, failing connection: %s",
match_slot->match_callback.match_string, sd_bus_message_get_error
(m)->message) : -abs(_e); })
3174 sd_bus_message_get_error(m)->message)({ int _level = ((7)), _e = ((sd_bus_message_get_errno(m))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/libsystemd/sd-bus/sd-bus.c", 3174
, __func__, "Unable to add match %s, failing connection: %s",
match_slot->match_callback.match_string, sd_bus_message_get_error
(m)->message) : -abs(_e); })
;
3175
3176 failed = true1;
3177 } else
3178 log_debug("Match %s successfully installed.", match_slot->match_callback.match_string)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 3178, __func__, "Match %s successfully installed."
, match_slot->match_callback.match_string) : -abs(_e); })
;
3179
3180 if (match_slot->match_callback.install_callback) {
3181 sd_bus *bus;
3182
3183 bus = sd_bus_message_get_bus(m);
3184
3185 /* This function has been called as slot handler, and we want to call another slot handler. Let's
3186 * update the slot callback metadata temporarily with our own data, and then revert back to the old
3187 * values. */
3188
3189 assert(bus->current_slot == match_slot->match_callback.install_slot)do { if ((__builtin_expect(!!(!(bus->current_slot == match_slot
->match_callback.install_slot)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus->current_slot == match_slot->match_callback.install_slot"
), "../src/libsystemd/sd-bus/sd-bus.c", 3189, __PRETTY_FUNCTION__
); } while (0)
;
3190 assert(bus->current_handler == add_match_callback)do { if ((__builtin_expect(!!(!(bus->current_handler == add_match_callback
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("bus->current_handler == add_match_callback"
), "../src/libsystemd/sd-bus/sd-bus.c", 3190, __PRETTY_FUNCTION__
); } while (0)
;
3191 assert(bus->current_userdata == userdata)do { if ((__builtin_expect(!!(!(bus->current_userdata == userdata
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("bus->current_userdata == userdata"
), "../src/libsystemd/sd-bus/sd-bus.c", 3191, __PRETTY_FUNCTION__
); } while (0)
;
3192
3193 bus->current_slot = match_slot;
3194 bus->current_handler = match_slot->match_callback.install_callback;
3195 bus->current_userdata = match_slot->userdata;
3196
3197 r = match_slot->match_callback.install_callback(m, match_slot->userdata, ret_error);
3198
3199 bus->current_slot = match_slot->match_callback.install_slot;
3200 bus->current_handler = add_match_callback;
3201 bus->current_userdata = userdata;
3202
3203 match_slot->match_callback.install_slot = sd_bus_slot_unref(match_slot->match_callback.install_slot);
3204 } else {
3205 if (failed) /* Generic failure handling: destroy the connection */
3206 bus_enter_closing(sd_bus_message_get_bus(m));
3207
3208 r = 1;
3209 }
3210
3211 if (failed && match_slot->floating) {
3212 bus_slot_disconnect(match_slot);
3213 sd_bus_slot_unref(match_slot);
3214 }
3215
3216 sd_bus_slot_unref(match_slot);
3217
3218 return r;
3219}
3220
3221static int bus_add_match_full(
3222 sd_bus *bus,
3223 sd_bus_slot **slot,
3224 bool_Bool asynchronous,
3225 const char *match,
3226 sd_bus_message_handler_t callback,
3227 sd_bus_message_handler_t install_callback,
3228 void *userdata) {
3229
3230 struct bus_match_component *components = NULL((void*)0);
3231 unsigned n_components = 0;
3232 sd_bus_slot *s = NULL((void*)0);
3233 int r = 0;
3234
3235 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3235, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3236 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3236, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
3237 assert_return(match, -EINVAL)do { if (!(((__builtin_expect(!!(match),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("match"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3237, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3238 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 3238, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
3239
3240 r = bus_match_parse(match, &components, &n_components);
3241 if (r < 0)
3242 goto finish;
3243
3244 s = bus_slot_allocate(bus, !slot, BUS_MATCH_CALLBACK, sizeof(struct match_callback), userdata);
3245 if (!s) {
3246 r = -ENOMEM12;
3247 goto finish;
3248 }
3249
3250 s->match_callback.callback = callback;
3251 s->match_callback.install_callback = install_callback;
3252
3253 if (bus->bus_client) {
3254 enum bus_match_scope scope;
3255
3256 scope = bus_match_get_scope(components, n_components);
3257
3258 /* Do not install server-side matches for matches against the local service, interface or bus path. */
3259 if (scope != BUS_MATCH_LOCAL) {
3260
3261 /* We store the original match string, so that we can use it to remove the match again. */
3262
3263 s->match_callback.match_string = strdup(match);
3264 if (!s->match_callback.match_string) {
3265 r = -ENOMEM12;
3266 goto finish;
3267 }
3268
3269 if (asynchronous) {
3270 r = bus_add_match_internal_async(bus,
3271 &s->match_callback.install_slot,
3272 s->match_callback.match_string,
3273 add_match_callback,
3274 s);
3275
3276 if (r < 0)
3277 return r;
3278
3279 /* Make the slot of the match call floating now. We need the reference, but we don't
3280 * want that this match pins the bus object, hence we first create it non-floating, but
3281 * then make it floating. */
3282 r = sd_bus_slot_set_floating(s->match_callback.install_slot, true1);
3283 } else
3284 r = bus_add_match_internal(bus, s->match_callback.match_string);
3285 if (r < 0)
3286 goto finish;
3287
3288 s->match_added = true1;
3289 }
3290 }
3291
3292 bus->match_callbacks_modified = true1;
3293 r = bus_match_add(&bus->match_callbacks, components, n_components, &s->match_callback);
3294 if (r < 0)
3295 goto finish;
3296
3297 if (slot)
3298 *slot = s;
3299 s = NULL((void*)0);
3300
3301finish:
3302 bus_match_parse_free(components, n_components);
3303 sd_bus_slot_unref(s);
3304
3305 return r;
3306}
3307
3308_public___attribute__ ((visibility("default"))) int sd_bus_add_match(
3309 sd_bus *bus,
3310 sd_bus_slot **slot,
3311 const char *match,
3312 sd_bus_message_handler_t callback,
3313 void *userdata) {
3314
3315 return bus_add_match_full(bus, slot, false0, match, callback, NULL((void*)0), userdata);
3316}
3317
3318_public___attribute__ ((visibility("default"))) int sd_bus_add_match_async(
3319 sd_bus *bus,
3320 sd_bus_slot **slot,
3321 const char *match,
3322 sd_bus_message_handler_t callback,
3323 sd_bus_message_handler_t install_callback,
3324 void *userdata) {
3325
3326 return bus_add_match_full(bus, slot, true1, match, callback, install_callback, userdata);
3327}
3328
3329bool_Bool bus_pid_changed(sd_bus *bus) {
3330 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3330, __PRETTY_FUNCTION__); } while (0)
;
3331
3332 /* We don't support people creating a bus connection and
3333 * keeping it around over a fork(). Let's complain. */
3334
3335 return bus->original_pid != getpid_cached();
3336}
3337
3338static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
3339 sd_bus *bus = userdata;
3340 int r;
3341
3342 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3342, __PRETTY_FUNCTION__); } while (0)
;
3343
3344 /* Note that this is called both on input_fd, output_fd as well as inotify_fd events */
3345
3346 r = sd_bus_process(bus, NULL((void*)0));
3347 if (r < 0) {
3348 log_debug_errno(r, "Processing of bus failed, closing down: %m")({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 3348, __func__, "Processing of bus failed, closing down: %m"
) : -abs(_e); })
;
3349 bus_enter_closing(bus);
3350 }
3351
3352 return 1;
3353}
3354
3355static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
3356 sd_bus *bus = userdata;
3357 int r;
3358
3359 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3359, __PRETTY_FUNCTION__); } while (0)
;
3360
3361 r = sd_bus_process(bus, NULL((void*)0));
3362 if (r < 0) {
3363 log_debug_errno(r, "Processing of bus failed, closing down: %m")({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 3363, __func__, "Processing of bus failed, closing down: %m"
) : -abs(_e); })
;
3364 bus_enter_closing(bus);
3365 }
3366
3367 return 1;
3368}
3369
3370static int prepare_callback(sd_event_source *s, void *userdata) {
3371 sd_bus *bus = userdata;
3372 int r, e;
3373 usec_t until;
3374
3375 assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("s"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3375, __PRETTY_FUNCTION__); } while (0)
;
3376 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3376, __PRETTY_FUNCTION__); } while (0)
;
3377
3378 e = sd_bus_get_events(bus);
3379 if (e < 0) {
3380 r = e;
3381 goto fail;
3382 }
3383
3384 if (bus->output_fd != bus->input_fd) {
3385
3386 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN0x001);
3387 if (r < 0)
3388 goto fail;
3389
3390 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT0x004);
3391 } else
3392 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
3393 if (r < 0)
3394 goto fail;
3395
3396 r = sd_bus_get_timeout(bus, &until);
3397 if (r < 0)
3398 goto fail;
3399 if (r > 0) {
3400 int j;
3401
3402 j = sd_event_source_set_time(bus->time_event_source, until);
3403 if (j < 0) {
3404 r = j;
3405 goto fail;
3406 }
3407 }
3408
3409 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
3410 if (r < 0)
3411 goto fail;
3412
3413 return 1;
3414
3415fail:
3416 log_debug_errno(r, "Preparing of bus events failed, closing down: %m")({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/sd-bus.c", 3416, __func__, "Preparing of bus events failed, closing down: %m"
) : -abs(_e); })
;
3417 bus_enter_closing(bus);
3418
3419 return 1;
3420}
3421
3422static int quit_callback(sd_event_source *event, void *userdata) {
3423 sd_bus *bus = userdata;
3424
3425 assert(event)do { if ((__builtin_expect(!!(!(event)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("event"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3425, __PRETTY_FUNCTION__); } while (0)
;
3426
3427 sd_bus_flush(bus);
3428 sd_bus_close(bus);
3429
3430 return 1;
3431}
3432
3433int bus_attach_io_events(sd_bus *bus) {
3434 int r;
3435
3436 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3436, __PRETTY_FUNCTION__); } while (0)
;
3437
3438 if (bus->input_fd < 0)
3439 return 0;
3440
3441 if (!bus->event)
3442 return 0;
3443
3444 if (!bus->input_io_event_source) {
3445 r = sd_event_add_io(bus->event, &bus->input_io_event_source, bus->input_fd, 0, io_callback, bus);
3446 if (r < 0)
3447 return r;
3448
3449 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
3450 if (r < 0)
3451 return r;
3452
3453 r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority);
3454 if (r < 0)
3455 return r;
3456
3457 r = sd_event_source_set_description(bus->input_io_event_source, "bus-input");
3458 } else
3459 r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd);
3460
3461 if (r < 0)
3462 return r;
3463
3464 if (bus->output_fd != bus->input_fd) {
3465 assert(bus->output_fd >= 0)do { if ((__builtin_expect(!!(!(bus->output_fd >= 0)),0
))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("bus->output_fd >= 0"
), "../src/libsystemd/sd-bus/sd-bus.c", 3465, __PRETTY_FUNCTION__
); } while (0)
;
3466
3467 if (!bus->output_io_event_source) {
3468 r = sd_event_add_io(bus->event, &bus->output_io_event_source, bus->output_fd, 0, io_callback, bus);
3469 if (r < 0)
3470 return r;
3471
3472 r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority);
3473 if (r < 0)
3474 return r;
3475
3476 r = sd_event_source_set_description(bus->input_io_event_source, "bus-output");
3477 } else
3478 r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd);
3479
3480 if (r < 0)
3481 return r;
3482 }
3483
3484 return 0;
3485}
3486
3487static void bus_detach_io_events(sd_bus *bus) {
3488 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3488, __PRETTY_FUNCTION__); } while (0)
;
3489
3490 if (bus->input_io_event_source) {
3491 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
3492 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
3493 }
3494
3495 if (bus->output_io_event_source) {
3496 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
3497 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
3498 }
3499}
3500
3501int bus_attach_inotify_event(sd_bus *bus) {
3502 int r;
3503
3504 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3504, __PRETTY_FUNCTION__); } while (0)
;
3505
3506 if (bus->inotify_fd < 0)
3507 return 0;
3508
3509 if (!bus->event)
3510 return 0;
3511
3512 if (!bus->inotify_event_source) {
3513 r = sd_event_add_io(bus->event, &bus->inotify_event_source, bus->inotify_fd, EPOLLINEPOLLIN, io_callback, bus);
3514 if (r < 0)
3515 return r;
3516
3517 r = sd_event_source_set_priority(bus->inotify_event_source, bus->event_priority);
3518 if (r < 0)
3519 return r;
3520
3521 r = sd_event_source_set_description(bus->inotify_event_source, "bus-inotify");
3522 } else
3523 r = sd_event_source_set_io_fd(bus->inotify_event_source, bus->inotify_fd);
3524 if (r < 0)
3525 return r;
3526
3527 return 0;
3528}
3529
3530static void bus_detach_inotify_event(sd_bus *bus) {
3531 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3531, __PRETTY_FUNCTION__); } while (0)
;
3532
3533 if (bus->inotify_event_source) {
3534 sd_event_source_set_enabled(bus->inotify_event_source, SD_EVENT_OFF);
3535 bus->inotify_event_source = sd_event_source_unref(bus->inotify_event_source);
3536 }
3537}
3538
3539_public___attribute__ ((visibility("default"))) int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
3540 int r;
3541
3542 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3542, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3543 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3543, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
3544 assert_return(!bus->event, -EBUSY)do { if (!(((__builtin_expect(!!(!bus->event),1))) ? (1) :
(log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus->event"
), "../src/libsystemd/sd-bus/sd-bus.c", 3544, __PRETTY_FUNCTION__
), 0))) return (-16); } while (0)
;
3545
3546 assert(!bus->input_io_event_source)do { if ((__builtin_expect(!!(!(!bus->input_io_event_source
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!bus->input_io_event_source"
), "../src/libsystemd/sd-bus/sd-bus.c", 3546, __PRETTY_FUNCTION__
); } while (0)
;
3547 assert(!bus->output_io_event_source)do { if ((__builtin_expect(!!(!(!bus->output_io_event_source
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!bus->output_io_event_source"
), "../src/libsystemd/sd-bus/sd-bus.c", 3547, __PRETTY_FUNCTION__
); } while (0)
;
3548 assert(!bus->time_event_source)do { if ((__builtin_expect(!!(!(!bus->time_event_source)),
0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!bus->time_event_source"
), "../src/libsystemd/sd-bus/sd-bus.c", 3548, __PRETTY_FUNCTION__
); } while (0)
;
3549
3550 if (event)
3551 bus->event = sd_event_ref(event);
3552 else {
3553 r = sd_event_default(&bus->event);
3554 if (r < 0)
3555 return r;
3556 }
3557
3558 bus->event_priority = priority;
3559
3560 r = sd_event_add_time(bus->event, &bus->time_event_source, CLOCK_MONOTONIC1, 0, 0, time_callback, bus);
3561 if (r < 0)
3562 goto fail;
3563
3564 r = sd_event_source_set_priority(bus->time_event_source, priority);
3565 if (r < 0)
3566 goto fail;
3567
3568 r = sd_event_source_set_description(bus->time_event_source, "bus-time");
3569 if (r < 0)
3570 goto fail;
3571
3572 r = sd_event_add_exit(bus->event, &bus->quit_event_source, quit_callback, bus);
3573 if (r < 0)
3574 goto fail;
3575
3576 r = sd_event_source_set_description(bus->quit_event_source, "bus-exit");
3577 if (r < 0)
3578 goto fail;
3579
3580 r = bus_attach_io_events(bus);
3581 if (r < 0)
3582 goto fail;
3583
3584 r = bus_attach_inotify_event(bus);
3585 if (r < 0)
3586 goto fail;
3587
3588 return 0;
3589
3590fail:
3591 sd_bus_detach_event(bus);
3592 return r;
3593}
3594
3595_public___attribute__ ((visibility("default"))) int sd_bus_detach_event(sd_bus *bus) {
3596 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3596, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3597 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3597, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
3598
3599 if (!bus->event)
3600 return 0;
3601
3602 bus_detach_io_events(bus);
3603 bus_detach_inotify_event(bus);
3604
3605 if (bus->time_event_source) {
3606 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
3607 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
3608 }
3609
3610 if (bus->quit_event_source) {
3611 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
3612 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
3613 }
3614
3615 bus->event = sd_event_unref(bus->event);
3616 return 1;
3617}
3618
3619_public___attribute__ ((visibility("default"))) sd_event* sd_bus_get_event(sd_bus *bus) {
3620 assert_return(bus, NULL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3620, __PRETTY_FUNCTION__), 0))) return (((void*)0)); } while
(0)
;
3621
3622 return bus->event;
3623}
3624
3625_public___attribute__ ((visibility("default"))) sd_bus_message* sd_bus_get_current_message(sd_bus *bus) {
3626 assert_return(bus, NULL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3626, __PRETTY_FUNCTION__), 0))) return (((void*)0)); } while
(0)
;
3627
3628 return bus->current_message;
3629}
3630
3631_public___attribute__ ((visibility("default"))) sd_bus_slot* sd_bus_get_current_slot(sd_bus *bus) {
3632 assert_return(bus, NULL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3632, __PRETTY_FUNCTION__), 0))) return (((void*)0)); } while
(0)
;
3633
3634 return bus->current_slot;
3635}
3636
3637_public___attribute__ ((visibility("default"))) sd_bus_message_handler_t sd_bus_get_current_handler(sd_bus *bus) {
3638 assert_return(bus, NULL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3638, __PRETTY_FUNCTION__), 0))) return (((void*)0)); } while
(0)
;
3639
3640 return bus->current_handler;
3641}
3642
3643_public___attribute__ ((visibility("default"))) void* sd_bus_get_current_userdata(sd_bus *bus) {
3644 assert_return(bus, NULL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3644, __PRETTY_FUNCTION__), 0))) return (((void*)0)); } while
(0)
;
3645
3646 return bus->current_userdata;
3647}
3648
3649static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
3650 sd_bus *b = NULL((void*)0);
3651 int r;
3652
3653 assert(bus_open)do { if ((__builtin_expect(!!(!(bus_open)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus_open"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3653, __PRETTY_FUNCTION__); } while (0)
;
3654 assert(default_bus)do { if ((__builtin_expect(!!(!(default_bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("default_bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3654, __PRETTY_FUNCTION__); } while (0)
;
3655
3656 if (!ret)
3657 return !!*default_bus;
3658
3659 if (*default_bus) {
3660 *ret = sd_bus_ref(*default_bus);
3661 return 0;
3662 }
3663
3664 r = bus_open(&b);
3665 if (r < 0)
3666 return r;
3667
3668 b->default_bus_ptr = default_bus;
3669 b->tid = gettid();
3670 *default_bus = b;
3671
3672 *ret = b;
3673 return 1;
3674}
3675
3676_public___attribute__ ((visibility("default"))) int sd_bus_default_system(sd_bus **ret) {
3677 return bus_default(sd_bus_open_system, &default_system_bus, ret);
3678}
3679
3680_public___attribute__ ((visibility("default"))) int sd_bus_default_user(sd_bus **ret) {
3681 return bus_default(sd_bus_open_user, &default_user_bus, ret);
3682}
3683
3684_public___attribute__ ((visibility("default"))) int sd_bus_default(sd_bus **ret) {
3685 int (*bus_open)(sd_bus **) = NULL((void*)0);
3686 sd_bus **busp;
3687
3688 busp = bus_choose_default(&bus_open);
3689 return bus_default(bus_open, busp, ret);
3690}
3691
3692_public___attribute__ ((visibility("default"))) int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
3693 assert_return(b, -EINVAL)do { if (!(((__builtin_expect(!!(b),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("b"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3693, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3694 assert_return(tid, -EINVAL)do { if (!(((__builtin_expect(!!(tid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("tid"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3694, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3695 assert_return(!bus_pid_changed(b), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(b)),1))) ? (
1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(b)"
), "../src/libsystemd/sd-bus/sd-bus.c", 3695, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
3696
3697 if (b->tid != 0) {
3698 *tid = b->tid;
3699 return 0;
3700 }
3701
3702 if (b->event)
3703 return sd_event_get_tid(b->event, tid);
3704
3705 return -ENXIO6;
3706}
3707
3708_public___attribute__ ((visibility("default"))) int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path) {
3709 _cleanup_free___attribute__((cleanup(freep))) char *e = NULL((void*)0);
3710 char *ret;
3711
3712 assert_return(object_path_is_valid(prefix), -EINVAL)do { if (!(((__builtin_expect(!!(object_path_is_valid(prefix)
),1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("object_path_is_valid(prefix)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3712, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3713 assert_return(external_id, -EINVAL)do { if (!(((__builtin_expect(!!(external_id),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("external_id"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3713, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3714 assert_return(ret_path, -EINVAL)do { if (!(((__builtin_expect(!!(ret_path),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret_path"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3714, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3715
3716 e = bus_label_escape(external_id);
3717 if (!e)
3718 return -ENOMEM12;
3719
3720 ret = strjoin(prefix, "/", e)strjoin_real((prefix), "/", e, ((void*)0));
3721 if (!ret)
3722 return -ENOMEM12;
3723
3724 *ret_path = ret;
3725 return 0;
3726}
3727
3728_public___attribute__ ((visibility("default"))) int sd_bus_path_decode(const char *path, const char *prefix, char **external_id) {
3729 const char *e;
3730 char *ret;
3731
3732 assert_return(object_path_is_valid(path), -EINVAL)do { if (!(((__builtin_expect(!!(object_path_is_valid(path)),
1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("object_path_is_valid(path)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3732, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3733 assert_return(object_path_is_valid(prefix), -EINVAL)do { if (!(((__builtin_expect(!!(object_path_is_valid(prefix)
),1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("object_path_is_valid(prefix)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3733, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3734 assert_return(external_id, -EINVAL)do { if (!(((__builtin_expect(!!(external_id),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("external_id"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3734, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3735
3736 e = object_path_startswith(path, prefix);
3737 if (!e) {
3738 *external_id = NULL((void*)0);
3739 return 0;
3740 }
3741
3742 ret = bus_label_unescape(e);
3743 if (!ret)
3744 return -ENOMEM12;
3745
3746 *external_id = ret;
3747 return 1;
3748}
3749
3750_public___attribute__ ((visibility("default"))) int sd_bus_path_encode_many(char **out, const char *path_template, ...) {
3751 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **labels = NULL((void*)0);
3752 char *path, *path_pos, **label_pos;
3753 const char *sep, *template_pos;
3754 size_t path_length;
3755 va_list list;
3756 int r;
3757
3758 assert_return(out, -EINVAL)do { if (!(((__builtin_expect(!!(out),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("out"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3758, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
1
Assuming 'out' is non-null
2
'?' condition is true
3
Taking false branch
4
Loop condition is false. Exiting loop
3759 assert_return(path_template, -EINVAL)do { if (!(((__builtin_expect(!!(path_template),1))) ? (1) : (
log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("path_template"
), "../src/libsystemd/sd-bus/sd-bus.c", 3759, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
5
Assuming 'path_template' is non-null
6
'?' condition is true
7
Taking false branch
8
Loop condition is false. Exiting loop
3760
3761 path_length = strlen(path_template);
3762
3763 va_start(list, path_template)__builtin_va_start(list, path_template);
3764 for (sep = strchr(path_template, '%'); sep; sep = strchr(sep + 1, '%')) {
9
Loop condition is false. Execution continues on line 3789
3765 const char *arg;
3766 char *label;
3767
3768 arg = va_arg(list, const char *)__builtin_va_arg(list, const char *);
3769 if (!arg) {
3770 va_end(list)__builtin_va_end(list);
3771 return -EINVAL22;
3772 }
3773
3774 label = bus_label_escape(arg);
3775 if (!label) {
3776 va_end(list)__builtin_va_end(list);
3777 return -ENOMEM12;
3778 }
3779
3780 r = strv_consume(&labels, label);
3781 if (r < 0) {
3782 va_end(list)__builtin_va_end(list);
3783 return r;
3784 }
3785
3786 /* add label length, but account for the format character */
3787 path_length += strlen(label) - 1;
3788 }
3789 va_end(list)__builtin_va_end(list);
3790
3791 path = malloc(path_length + 1);
3792 if (!path)
10
Assuming 'path' is non-null
11
Taking false branch
3793 return -ENOMEM12;
3794
3795 path_pos = path;
3796 label_pos = labels;
3797
3798 for (template_pos = path_template; *template_pos; ) {
12
Loop condition is true. Entering loop body
3799 sep = strchrnul(template_pos, '%');
3800 path_pos = mempcpy(path_pos, template_pos, sep - template_pos);
3801 if (!*sep)
13
Assuming the condition is false
14
Taking false branch
3802 break;
3803
3804 path_pos = stpcpy(path_pos, *label_pos++);
15
Null pointer value stored to 'label_pos'
16
Dereference of null pointer
3805 template_pos = sep + 1;
3806 }
3807
3808 *path_pos = 0;
3809 *out = path;
3810 return 0;
3811}
3812
3813_public___attribute__ ((visibility("default"))) int sd_bus_path_decode_many(const char *path, const char *path_template, ...) {
3814 _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **labels = NULL((void*)0);
3815 const char *template_pos, *path_pos;
3816 char **label_pos;
3817 va_list list;
3818 int r;
3819
3820 /*
3821 * This decodes an object-path based on a template argument. The
3822 * template consists of a verbatim path, optionally including special
3823 * directives:
3824 *
3825 * - Each occurrence of '%' in the template matches an arbitrary
3826 * substring of a label in the given path. At most one such
3827 * directive is allowed per label. For each such directive, the
3828 * caller must provide an output parameter (char **) via va_arg. If
3829 * NULL is passed, the given label is verified, but not returned.
3830 * For each matched label, the *decoded* label is stored in the
3831 * passed output argument, and the caller is responsible to free
3832 * it. Note that the output arguments are only modified if the
3833 * actualy path matched the template. Otherwise, they're left
3834 * untouched.
3835 *
3836 * This function returns <0 on error, 0 if the path does not match the
3837 * template, 1 if it matched.
3838 */
3839
3840 assert_return(path, -EINVAL)do { if (!(((__builtin_expect(!!(path),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("path"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3840, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3841 assert_return(path_template, -EINVAL)do { if (!(((__builtin_expect(!!(path_template),1))) ? (1) : (
log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("path_template"
), "../src/libsystemd/sd-bus/sd-bus.c", 3841, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
3842
3843 path_pos = path;
3844
3845 for (template_pos = path_template; *template_pos; ) {
3846 const char *sep;
3847 size_t length;
3848 char *label;
3849
3850 /* verify everything until the next '%' matches verbatim */
3851 sep = strchrnul(template_pos, '%');
3852 length = sep - template_pos;
3853 if (strncmp(path_pos, template_pos, length))
3854 return 0;
3855
3856 path_pos += length;
3857 template_pos += length;
3858
3859 if (!*template_pos)
3860 break;
3861
3862 /* We found the next '%' character. Everything up until here
3863 * matched. We now skip ahead to the end of this label and make
3864 * sure it matches the tail of the label in the path. Then we
3865 * decode the string in-between and save it for later use. */
3866
3867 ++template_pos; /* skip over '%' */
3868
3869 sep = strchrnul(template_pos, '/');
3870 length = sep - template_pos; /* length of suffix to match verbatim */
3871
3872 /* verify the suffixes match */
3873 sep = strchrnul(path_pos, '/');
3874 if (sep - path_pos < (ssize_t)length ||
3875 strncmp(sep - length, template_pos, length))
3876 return 0;
3877
3878 template_pos += length; /* skip over matched label */
3879 length = sep - path_pos - length; /* length of sub-label to decode */
3880
3881 /* store unescaped label for later use */
3882 label = bus_label_unescape_n(path_pos, length);
3883 if (!label)
3884 return -ENOMEM12;
3885
3886 r = strv_consume(&labels, label);
3887 if (r < 0)
3888 return r;
3889
3890 path_pos = sep; /* skip decoded label and suffix */
3891 }
3892
3893 /* end of template must match end of path */
3894 if (*path_pos)
3895 return 0;
3896
3897 /* copy the labels over to the caller */
3898 va_start(list, path_template)__builtin_va_start(list, path_template);
3899 for (label_pos = labels; label_pos && *label_pos; ++label_pos) {
3900 char **arg;
3901
3902 arg = va_arg(list, char **)__builtin_va_arg(list, char **);
3903 if (arg)
3904 *arg = *label_pos;
3905 else
3906 free(*label_pos);
3907 }
3908 va_end(list)__builtin_va_end(list);
3909
3910 labels = mfree(labels);
3911 return 1;
3912}
3913
3914_public___attribute__ ((visibility("default"))) int sd_bus_try_close(sd_bus *bus) {
3915 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3915, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3916 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3916, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
3917 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 3917, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
3918
3919 return -EOPNOTSUPP95;
3920}
3921
3922_public___attribute__ ((visibility("default"))) int sd_bus_get_description(sd_bus *bus, const char **description) {
3923 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3923, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3924 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3924, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
3925 assert_return(description, -EINVAL)do { if (!(((__builtin_expect(!!(description),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("description"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3925, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3926 assert_return(bus->description, -ENXIO)do { if (!(((__builtin_expect(!!(bus->description),1))) ? (
1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("bus->description"
), "../src/libsystemd/sd-bus/sd-bus.c", 3926, __PRETTY_FUNCTION__
), 0))) return (-6); } while (0)
;
3927 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 3927, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
3928
3929 if (bus->description)
3930 *description = bus->description;
3931 else if (bus->is_system)
3932 *description = "system";
3933 else if (bus->is_user)
3934 *description = "user";
3935 else
3936 *description = NULL((void*)0);
3937
3938 return 0;
3939}
3940
3941int bus_get_root_path(sd_bus *bus) {
3942 int r;
3943
3944 if (bus->cgroup_root)
3945 return 0;
3946
3947 r = cg_get_root_path(&bus->cgroup_root);
3948 if (r == -ENOENT2) {
3949 bus->cgroup_root = strdup("/");
3950 if (!bus->cgroup_root)
3951 return -ENOMEM12;
3952
3953 r = 0;
3954 }
3955
3956 return r;
3957}
3958
3959_public___attribute__ ((visibility("default"))) int sd_bus_get_scope(sd_bus *bus, const char **scope) {
3960 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3960, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3961 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3961, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
3962 assert_return(scope, -EINVAL)do { if (!(((__builtin_expect(!!(scope),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("scope"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3962, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3963 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 3963, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
3964
3965 if (bus->is_user) {
3966 *scope = "user";
3967 return 0;
3968 }
3969
3970 if (bus->is_system) {
3971 *scope = "system";
3972 return 0;
3973 }
3974
3975 return -ENODATA61;
3976}
3977
3978_public___attribute__ ((visibility("default"))) int sd_bus_get_address(sd_bus *bus, const char **address) {
3979
3980 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3980, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3981 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3981, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
3982 assert_return(address, -EINVAL)do { if (!(((__builtin_expect(!!(address),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("address"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3982, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3983 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 3983, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
3984
3985 if (bus->address) {
3986 *address = bus->address;
3987 return 0;
3988 }
3989
3990 return -ENODATA61;
3991}
3992
3993_public___attribute__ ((visibility("default"))) int sd_bus_get_creds_mask(sd_bus *bus, uint64_t *mask) {
3994 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3994, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3995 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3995, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
3996 assert_return(mask, -EINVAL)do { if (!(((__builtin_expect(!!(mask),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("mask"), "../src/libsystemd/sd-bus/sd-bus.c"
, 3996, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
3997 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 3997, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
3998
3999 *mask = bus->creds_mask;
4000 return 0;
4001}
4002
4003_public___attribute__ ((visibility("default"))) int sd_bus_is_bus_client(sd_bus *bus) {
4004 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4004, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4005 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4005, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4006 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 4006, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
4007
4008 return bus->bus_client;
4009}
4010
4011_public___attribute__ ((visibility("default"))) int sd_bus_is_server(sd_bus *bus) {
4012 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4012, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4013 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4013, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4014 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 4014, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
4015
4016 return bus->is_server;
4017}
4018
4019_public___attribute__ ((visibility("default"))) int sd_bus_is_anonymous(sd_bus *bus) {
4020 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4020, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4021 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4021, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4022 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 4022, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
4023
4024 return bus->anonymous_auth;
4025}
4026
4027_public___attribute__ ((visibility("default"))) int sd_bus_is_trusted(sd_bus *bus) {
4028 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4028, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4029 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4029, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4030 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 4030, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
4031
4032 return bus->trusted;
4033}
4034
4035_public___attribute__ ((visibility("default"))) int sd_bus_is_monitor(sd_bus *bus) {
4036 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4036, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4037 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4037, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4038 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 4038, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
4039
4040 return bus->is_monitor;
4041}
4042
4043static void flush_close(sd_bus *bus) {
4044 if (!bus)
4045 return;
4046
4047 /* Flushes and closes the specified bus. We take a ref before,
4048 * to ensure the flushing does not cause the bus to be
4049 * unreferenced. */
4050
4051 sd_bus_flush_close_unref(sd_bus_ref(bus));
4052}
4053
4054_public___attribute__ ((visibility("default"))) void sd_bus_default_flush_close(void) {
4055 flush_close(default_starter_bus);
4056 flush_close(default_user_bus);
4057 flush_close(default_system_bus);
4058}
4059
4060_public___attribute__ ((visibility("default"))) int sd_bus_set_exit_on_disconnect(sd_bus *bus, int b) {
4061 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4061, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4062 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4062, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4063
4064 /* Turns on exit-on-disconnect, and triggers it immediately if the bus connection was already
4065 * disconnected. Note that this is triggered exclusively on disconnections triggered by the server side, never
4066 * from the client side. */
4067 bus->exit_on_disconnect = b;
4068
4069 /* If the exit condition was triggered already, exit immediately. */
4070 return bus_exit_now(bus);
4071}
4072
4073_public___attribute__ ((visibility("default"))) int sd_bus_get_exit_on_disconnect(sd_bus *bus) {
4074 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4074, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4075 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4075, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4076
4077 return bus->exit_on_disconnect;
4078}
4079
4080_public___attribute__ ((visibility("default"))) int sd_bus_set_sender(sd_bus *bus, const char *sender) {
4081 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4081, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4082 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4082, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4083 assert_return(!bus->bus_client, -EPERM)do { if (!(((__builtin_expect(!!(!bus->bus_client),1))) ? (
1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus->bus_client"
), "../src/libsystemd/sd-bus/sd-bus.c", 4083, __PRETTY_FUNCTION__
), 0))) return (-1); } while (0)
;
4084 assert_return(!sender || service_name_is_valid(sender), -EINVAL)do { if (!(((__builtin_expect(!!(!sender || service_name_is_valid
(sender)),1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("!sender || service_name_is_valid(sender)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4084, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4085
4086 return free_and_strdup(&bus->patch_sender, sender);
4087}
4088
4089_public___attribute__ ((visibility("default"))) int sd_bus_get_sender(sd_bus *bus, const char **ret) {
4090 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4090, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4091 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4091, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4092 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4092, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4093
4094 if (!bus->patch_sender)
4095 return -ENODATA61;
4096
4097 *ret = bus->patch_sender;
4098 return 0;
4099}
4100
4101_public___attribute__ ((visibility("default"))) int sd_bus_get_n_queued_read(sd_bus *bus, uint64_t *ret) {
4102 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4102, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4103 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4103, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4104 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 4104, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
4105 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4105, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4106
4107 *ret = bus->rqueue_size;
4108 return 0;
4109}
4110
4111_public___attribute__ ((visibility("default"))) int sd_bus_get_n_queued_write(sd_bus *bus, uint64_t *ret) {
4112 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4112, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4113 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4113, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4114 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 4114, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
4115 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4115, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4116
4117 *ret = bus->wqueue_size;
4118 return 0;
4119}
4120
4121_public___attribute__ ((visibility("default"))) int sd_bus_enqueue_for_read(sd_bus *bus, sd_bus_message *m) {
4122 int r;
4123
4124 assert_return(bus, -EINVAL)do { if (!(((__builtin_expect(!!(bus),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4124, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4125 assert_return(bus = bus_resolve(bus), -ENOPKG)do { if (!(((__builtin_expect(!!(bus = bus_resolve(bus)),1)))
? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, (
"bus = bus_resolve(bus)"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4125, __PRETTY_FUNCTION__), 0))) return (-65); } while (0)
;
4126 assert_return(m, -EINVAL)do { if (!(((__builtin_expect(!!(m),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/sd-bus.c"
, 4126, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
4127 assert_return(m->sealed, -EINVAL)do { if (!(((__builtin_expect(!!(m->sealed),1))) ? (1) : (
log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("m->sealed"
), "../src/libsystemd/sd-bus/sd-bus.c", 4127, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
4128 assert_return(!bus_pid_changed(bus), -ECHILD)do { if (!(((__builtin_expect(!!(!bus_pid_changed(bus)),1))) ?
(1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("!bus_pid_changed(bus)"
), "../src/libsystemd/sd-bus/sd-bus.c", 4128, __PRETTY_FUNCTION__
), 0))) return (-10); } while (0)
;
4129
4130 if (!BUS_IS_OPEN(bus->state))
4131 return -ENOTCONN107;
4132
4133 /* Re-enqeue a message for reading. This is primarily useful for PolicyKit-style authentication,
4134 * where we want accept a message, then determine we need to interactively authenticate the user, and
4135 * when we have that process the message again. */
4136
4137 r = bus_rqueue_make_room(bus);
4138 if (r < 0)
4139 return r;
4140
4141 bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(m, bus);
4142 return 0;
4143}