HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-bus/sd-bus.c
Git blob: 1a6eed2baad85e39cff35c47f1771ee148f511c6
Lines 1001–2000 of 4,512
assert(*p);
assert(guid);
while (!IN_SET(**p, 0, ';')) {
r = parse_address_key(p, "guid", guid);
if (r < 0)
return r;
if (r > 0)
continue;
r = parse_address_key(p, "machine", &machine);
if (r < 0)
return r;
if (r > 0)
continue;
r = parse_address_key(p, "pid", &pid);
if (r < 0)
return r;
if (r > 0)
continue;
skip_address_key(p);
}
if (!machine == !pid)
return -EINVAL;
if (machine) {
if (!hostname_is_valid(machine, VALID_HOSTNAME_DOT_HOST))
return -EINVAL;
free_and_replace(b->machine, machine);
} else
b->machine = mfree(b->machine);
if (pid) {
r = parse_pid(pid, &b->nspid);
if (r < 0)
return r;
} else
b->nspid = 0;
b->sockaddr.un = (struct sockaddr_un) {
.sun_family = AF_UNIX,
/* Note that we use the old /var/run prefix here, to increase compatibility with really old containers */
.sun_path = "/var/run/dbus/system_bus_socket",
};
b->sockaddr_size = sockaddr_un_len(&b->sockaddr.un);
b->is_local = false;
return 0;
}
static void bus_reset_parsed_address(sd_bus *b) {
assert(b);
zero(b->sockaddr);
b->sockaddr_size = 0;
b->exec_argv = strv_free(b->exec_argv);
b->exec_path = mfree(b->exec_path);
b->server_id = SD_ID128_NULL;
b->machine = mfree(b->machine);
b->nspid = 0;
}
static int bus_parse_next_address(sd_bus *b) {
_cleanup_free_ char *guid = NULL;
const char *a;
int r;
assert(b);
if (!b->address)
return 0;
if (b->address[b->address_index] == 0)
return 0;
bus_reset_parsed_address(b);
a = b->address + b->address_index;
while (*a != 0) {
if (*a == ';') {
a++;
continue;
}
if (startswith(a, "unix:")) {
a += 5;
r = parse_unix_address(b, &a, &guid);
if (r < 0)
return r;
break;
} else if (startswith(a, "tcp:")) {
a += 4;
r = parse_tcp_address(b, &a, &guid);
if (r < 0)
return r;
break;
} else if (startswith(a, "unixexec:")) {
a += 9;
r = parse_exec_address(b, &a, &guid);
if (r < 0)
return r;
break;
} else if (startswith(a, "x-machine-unix:")) {
a += 15;
r = parse_container_unix_address(b, &a, &guid);
if (r < 0)
return r;
break;
}
a = strchr(a, ';');
if (!a)
return 0;
}
if (guid) {
r = sd_id128_from_string(guid, &b->server_id);
if (r < 0)
return r;
}
b->address_index = a - b->address;
return 1;
}
static int bus_start_address(sd_bus *b) {
int r;
assert(b);
for (;;) {
bus_close_fds(b);
pidref_done_sigterm_wait(&b->busexec_pidref);
/* If you provide multiple different bus-addresses, we
* try all of them in order and use the first one that
* succeeds. */
if (b->exec_path)
r = bus_socket_exec(b);
else if ((b->nspid > 0 || b->machine) && b->sockaddr.sa.sa_family != AF_UNSPEC)
r = bus_container_connect_socket(b);
else if (b->sockaddr.sa.sa_family != AF_UNSPEC)
r = bus_socket_connect(b);
else
goto next;
if (r >= 0) {
int q;
q = bus_attach_io_events(b);
if (q < 0)
return q;
q = bus_attach_inotify_event(b);
if (q < 0)
return q;
return r;
}
b->last_connect_error = -r;
next:
r = bus_parse_next_address(b);
if (r < 0)
return r;
if (r == 0)
return b->last_connect_error > 0 ? -b->last_connect_error : -ECONNREFUSED;
}
}
int bus_next_address(sd_bus *b) {
assert(b);
bus_reset_parsed_address(b);
return bus_start_address(b);
}
static int bus_start_fd(sd_bus *b) {
struct stat st;
int r;
assert(b);
assert(b->input_fd >= 0);
assert(b->output_fd >= 0);
if (DEBUG_LOGGING) {
_cleanup_free_ char *pi = NULL, *po = NULL;
(void) fd_get_path(b->input_fd, &pi);
(void) fd_get_path(b->output_fd, &po);
log_debug("sd-bus: starting bus%s%s on fds %d/%d (%s, %s)...",
b->description ? " " : "", strempty(b->description),
b->input_fd, b->output_fd,
pi ?: "???", po ?: "???");
}
r = fd_nonblock(b->input_fd, true);
if (r < 0)
return r;
r = fd_cloexec(b->input_fd, true);
if (r < 0)
return r;
if (b->input_fd != b->output_fd) {
r = fd_nonblock(b->output_fd, true);
if (r < 0)
return r;
r = fd_cloexec(b->output_fd, true);
if (r < 0)
return r;
}
if (fstat(b->input_fd, &st) < 0)
return -errno;
return bus_socket_take_fd(b);
}
_public_ int sd_bus_start(sd_bus *bus) {
int r;
assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_origin_changed(bus), -ECHILD);
bus_set_state(bus, BUS_OPENING);
if (bus->is_server && bus->bus_client)
return -EINVAL;
if (bus->input_fd >= 0)
r = bus_start_fd(bus);
else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->machine)
r = bus_start_address(bus);
else
return -EINVAL;
if (r < 0) {
sd_bus_close(bus);
return r;
}
return bus_send_hello(bus);
}
_public_ int sd_bus_open_with_description(sd_bus **ret, const char *description) {
const char *e;
_cleanup_(bus_freep) sd_bus *b = NULL;
int r;
assert_return(ret, -EINVAL);
/* Let's connect to the starter bus if it is set, and
* otherwise to the bus that is appropriate for the scope
* we are running in */
e = secure_getenv("DBUS_STARTER_BUS_TYPE");
if (e) {
if (streq(e, "system"))
return sd_bus_open_system_with_description(ret, description);
if (STR_IN_SET(e, "session", "user"))
return sd_bus_open_user_with_description(ret, description);
}
e = secure_getenv("DBUS_STARTER_ADDRESS");
if (!e) {
if (cg_pid_get_owner_uid(0, NULL) >= 0)
return sd_bus_open_user_with_description(ret, description);
else
return sd_bus_open_system_with_description(ret, description);
}
r = sd_bus_new(&b);
if (r < 0)
return r;
r = sd_bus_set_address(b, e);
if (r < 0)
return r;
b->bus_client = true;
/* We don't know whether the bus is trusted or not, so better
* be safe, and authenticate everything */
b->trusted = false;
b->is_local = false;
b->creds_mask |= SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_EFFECTIVE_CAPS;
r = sd_bus_start(b);
if (r < 0)
return r;
*ret = TAKE_PTR(b);
return 0;
}
_public_ int sd_bus_open(sd_bus **ret) {
return sd_bus_open_with_description(ret, NULL);
}
int bus_set_address_system(sd_bus *b) {
const char *e;
int r;
assert(b);
e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
r = sd_bus_set_address(b, e ?: DEFAULT_SYSTEM_BUS_ADDRESS);
if (r < 0)
return r;
b->runtime_scope = RUNTIME_SCOPE_SYSTEM;
return r;
}
_public_ int sd_bus_open_system_with_description(sd_bus **ret, const char *description) {
_cleanup_(bus_freep) sd_bus *b = NULL;
int r;
assert_return(ret, -EINVAL);
r = sd_bus_new(&b);
if (r < 0)
return r;
if (description) {
r = sd_bus_set_description(b, description);
if (r < 0)
return r;
}
r = bus_set_address_system(b);
if (r < 0)
return r;
b->bus_client = true;
/* Let's do per-method access control on the system bus. We
* need the caller's UID and capability set for that. */
b->trusted = false;
b->creds_mask |= SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_EFFECTIVE_CAPS;
b->is_local = true;
r = sd_bus_start(b);
if (r < 0)
return r;
*ret = TAKE_PTR(b);
return 0;
}
_public_ int sd_bus_open_system(sd_bus **ret) {
return sd_bus_open_system_with_description(ret, NULL);
}
int bus_set_address_user(sd_bus *b) {
const char *a;
_cleanup_free_ char *_a = NULL;
int r;
assert(b);
a = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
if (!a) {
const char *e;
_cleanup_free_ char *ee = NULL;
e = secure_getenv("XDG_RUNTIME_DIR");
if (!e)
return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM),
"sd-bus: $XDG_RUNTIME_DIR not set, cannot connect to user bus.");
ee = bus_address_escape(e);
if (!ee)
return -ENOMEM;
if (asprintf(&_a, DEFAULT_USER_BUS_ADDRESS_FMT, ee) < 0)
return -ENOMEM;
a = _a;
}
r = sd_bus_set_address(b, a);
if (r < 0)
return r;
b->runtime_scope = RUNTIME_SCOPE_USER;
return r;
}
_public_ int sd_bus_open_user_with_description(sd_bus **ret, const char *description) {
_cleanup_(bus_freep) sd_bus *b = NULL;
int r;
assert_return(ret, -EINVAL);
r = sd_bus_new(&b);
if (r < 0)
return r;
if (description) {
r = sd_bus_set_description(b, description);
if (r < 0)
return r;
}
r = bus_set_address_user(b);
if (r < 0)
return r;
b->bus_client = true;
/* We don't do any per-method access control on the user bus. */
b->trusted = true;
b->is_local = true;
r = sd_bus_start(b);
if (r < 0)
return r;
*ret = TAKE_PTR(b);
return 0;
}
_public_ int sd_bus_open_user(sd_bus **ret) {
return sd_bus_open_user_with_description(ret, NULL);
}
int bus_set_address_system_remote(sd_bus *b, const char *host) {
_cleanup_free_ char *e = NULL;
const char *m = NULL, *c = NULL, *a, *rbracket = NULL, *p = NULL;
assert(b);
assert(host);
/* Skip ":"s in ipv6 addresses */
if (*host == '[') {
char *t;
rbracket = strchr(host, ']');
if (!rbracket)
return -EINVAL;
t = strndupa_safe(host + 1, rbracket - host - 1);
e = bus_address_escape(t);
if (!e)
return -ENOMEM;
} else if ((a = strchr(host, '@'))) {
if (*(a + 1) == '[') {
_cleanup_free_ char *t = NULL;
rbracket = strchr(a + 1, ']');
if (!rbracket)
return -EINVAL;
t = new0(char, strlen(host));
if (!t)
return -ENOMEM;
strncat(t, host, a - host + 1);
strncat(t, a + 2, rbracket - a - 2);
e = bus_address_escape(t);
if (!e)
return -ENOMEM;
} else if (*(a + 1) == '\0' || strchr(a + 1, '@'))
return -EINVAL;
}
/* Let's see if a port was given */
m = strchr(rbracket ? rbracket + 1 : host, ':');
if (m) {
const char *t;
bool got_forward_slash = false;
p = m + 1;
t = strchr(p, '/');
if (t) {
p = strndupa_safe(p, t - p);
got_forward_slash = true;
}
if (!in_charset(p, DIGITS) || *p == '\0') {
if (!hostname_is_valid(p, 0) || got_forward_slash)
return -EINVAL;
m = TAKE_PTR(p);
goto interpret_port_as_machine_old_syntax;
}
}
/* Let's see if a machine was given */
m = strchr(rbracket ? rbracket + 1 : host, '/');
if (m) {
m++;
interpret_port_as_machine_old_syntax:
/* Let's make sure this is not a port of some kind,
* and is a valid machine name. */
if (!in_charset(m, DIGITS) && hostname_is_valid(m, 0))
c = strjoina(",argv", p ? "7" : "5", "=--machine=", m);
}
if (!e) {
char *t;
t = strndupa_safe(host, strcspn(host, ":/"));
e = bus_address_escape(t);
if (!e)
return -ENOMEM;
}
const char *ssh = secure_getenv("SYSTEMD_SSH") ?: "ssh";
_cleanup_free_ char *ssh_escaped = bus_address_escape(ssh);
if (!ssh_escaped)
return -ENOMEM;
char *address = strjoin(
"unixexec:path=", ssh_escaped, ",argv1=-xT",
p ? ",argv2=-p,argv3=" : "", strempty(p),
",argv", p ? "4" : "2", "=--,argv", p ? "5" : "3", "=", e,
",argv", p ? "6" : "4", "=systemd-stdio-bridge", c);
if (!address)
return -ENOMEM;
return free_and_replace(b->address, address);
}
_public_ int sd_bus_open_system_remote(sd_bus **ret, const char *host) {
_cleanup_(bus_freep) sd_bus *b = NULL;
int r;
assert_return(host, -EINVAL);
assert_return(ret, -EINVAL);
r = sd_bus_new(&b);
if (r < 0)
return r;
r = bus_set_address_system_remote(b, host);
if (r < 0)
return r;
b->bus_client = true;
b->trusted = false;
b->runtime_scope = RUNTIME_SCOPE_SYSTEM;
b->is_local = false;
r = sd_bus_start(b);
if (r < 0)
return r;
*ret = TAKE_PTR(b);
return 0;
}
int bus_set_address_machine(sd_bus *b, RuntimeScope runtime_scope, const char *machine) {
_cleanup_free_ char *a = NULL;
assert(b);
assert(IN_SET(runtime_scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER));
assert(machine);
_cleanup_free_ char *u = NULL, *h = NULL;
int with_at;
with_at = split_user_at_host(machine, &u, &h);
if (with_at < 0)
return with_at;
if (with_at || runtime_scope == RUNTIME_SCOPE_USER) {
_cleanup_free_ char *eu = NULL, *eh = NULL;
/* If there's an "@" in the container specification, we'll connect as a user specified at its
* left hand side, which is useful in combination with user=true. This isn't as trivial as it
* might sound: it's not sufficient to enter the container and connect to some socket there,
* since the --user socket path depends on $XDG_RUNTIME_DIR which is set via PAM. Thus, to be
* able to connect, we need to have a PAM session. Our way out? We use systemd-run to get
* into the container and acquire a PAM session there, and then invoke systemd-stdio-bridge
* in it, which propagates the bus transport to us. */
if (with_at) {
if (!u) {
u = getusername_malloc(); /* Empty user name, let's use the local one */
if (!u)
return -ENOMEM;
}
eu = bus_address_escape(u);
if (!eu)
return -ENOMEM;
}
/* No "@" specified but we shall connect to the user instance? Then assume root (and
* not a user named identically to the calling one). This means:
*
* --machine=foobar --user → connect to user bus of root user in container "foobar"
* --machine=@foobar --user → connect to user bus of user named like the calling user in container "foobar"
*
* Why? so that behaviour for "--machine=foobar --system" is roughly similar to
* "--machine=foobar --user": both times we unconditionally connect as root user
* regardless what the calling user is. */
if (h) {
eh = bus_address_escape(h);
if (!eh)
return -ENOMEM;
}
/* systemd-run -M… -PGq --wait -pUser=… -pPAMName=login systemd-stdio-bridge */
a = strjoin("unixexec:path=systemd-run,"
"argv1=-M", eh ?: ".host", ","
"argv2=-PGq,"
"argv3=--wait,"
"argv4=-pUser%3d", eu ?: "root", ",",
"argv5=-pPAMName%3dlogin,"
"argv6=systemd-stdio-bridge");
if (!a)
return -ENOMEM;
bool local = !eh || streq(eh, ".host");
/* Ideally we'd always use the "--user" and "--quiet" switches to systemd-stdio-bridge here,
* but they're only available in recent systemd versions, meaning we can only use them if
* we're not connecting to a container. Using the "-p" switch with an explicit path is a
* working alternative for "--user", and is compatible with older versions, hence that's what
* we use when connecting to a container. */
if (runtime_scope == RUNTIME_SCOPE_USER) {
if (!strextend(&a, local ? ",argv7=--user,argv8=--quiet" : ",argv7=-punix:path%3d%24%7bXDG_RUNTIME_DIR%7d/bus"))
return -ENOMEM;
} else if (local)
if (!strextend(&a, ",argv7=--quiet"))
return -ENOMEM;
} else {
_cleanup_free_ char *e = NULL;
/* Just a container name, we can go the simple way, and just join the container, and connect
* to the well-known path of the system bus there. */
e = bus_address_escape(h ?: ".host");
if (!e)
return -ENOMEM;
a = strjoin("x-machine-unix:machine=", e);
if (!a)
return -ENOMEM;
}
return free_and_replace(b->address, a);
}
static int machine_spec_is_current_identity(const char *user_and_machine) {
/* Returns true if the specified user+machine name are actually equivalent to our own identity and
* our own host. If so we can shortcut things. Why bother? Because that way we don't have to fork
* off short-lived worker processes that are then unavailable for authentication and logging in the
* peer. Moreover joining a namespace requires privileges. If we are in the right namespace anyway,
* we can avoid permission problems thus. */
assert(user_and_machine);
/* Omitting the user name means that we shall use the same user name as we run as locally, which
* means we'll end up on the same host, let's shortcut */
if (STR_IN_SET(user_and_machine, "@.host", "@"))
return true;
/* Otherwise, if we are root, then we can also allow the ".host" syntax, as that's the user this
* would connect to. */
uid_t uid = geteuid();
if (uid == 0 && STR_IN_SET(user_and_machine, ".host", "root@.host", "0@.host"))
return true;
/* Otherwise, we have to figure out our user id and name, and compare things with that. */
_cleanup_free_ char *un = NULL;
const char *f;
f = startswith(user_and_machine, FORMAT_UID(uid));
if (!f) {
un = getusername_malloc();
if (!un)
return -ENOMEM;
f = startswith(user_and_machine, un);
if (!f)
return false;
}
return STR_IN_SET(f, "@", "@.host");
}
_public_ int sd_bus_open_system_machine(sd_bus **ret, const char *user_and_machine) {
_cleanup_(bus_freep) sd_bus *b = NULL;
int r;
assert_return(user_and_machine, -EINVAL);
assert_return(ret, -EINVAL);
if (machine_spec_is_current_identity(user_and_machine))
return sd_bus_open_system(ret);
r = machine_spec_valid(user_and_machine);
if (r < 0)
return r;
if (r == 0)
return -EINVAL;
r = sd_bus_new(&b);
if (r < 0)
return r;
r = bus_set_address_machine(b, RUNTIME_SCOPE_SYSTEM, user_and_machine);
if (r < 0)
return r;
b->bus_client = true;
b->runtime_scope = RUNTIME_SCOPE_SYSTEM;
r = sd_bus_start(b);
if (r < 0)
return r;
*ret = TAKE_PTR(b);
return 0;
}
_public_ int sd_bus_open_user_machine(sd_bus **ret, const char *user_and_machine) {
_cleanup_(bus_freep) sd_bus *b = NULL;
int r;
assert_return(user_and_machine, -EINVAL);
assert_return(ret, -EINVAL);
/* Shortcut things if we'd end up on this host and as the same user and have one of the necessary
* environment variables set already. */
if (machine_spec_is_current_identity(user_and_machine) &&
(secure_getenv("DBUS_SESSION_BUS_ADDRESS") || secure_getenv("XDG_RUNTIME_DIR")))
return sd_bus_open_user(ret);
r = machine_spec_valid(user_and_machine);
if (r < 0)
return r;
if (r == 0)
return -EINVAL;
r = sd_bus_new(&b);
if (r < 0)
return r;
r = bus_set_address_machine(b, RUNTIME_SCOPE_USER, user_and_machine);
if (r < 0)
return r;
b->bus_client = true;
b->trusted = true;
r = sd_bus_start(b);
if (r < 0)
return r;
*ret = TAKE_PTR(b);
return 0;
}
_public_ void sd_bus_close(sd_bus *bus) {
if (!bus)
return;
if (bus->state == BUS_CLOSED)
return;
if (bus_origin_changed(bus))
return;
/* Don't leave ssh hanging around */
pidref_done_sigterm_wait(&bus->busexec_pidref);
bus_set_state(bus, BUS_CLOSED);
sd_bus_detach_event(bus);
/* Drop all queued messages so that they drop references to
* the bus object and the bus may be freed */
bus_reset_queues(bus);
bus_close_fds(bus);
}
_public_ sd_bus* sd_bus_close_unref(sd_bus *bus) {
if (!bus)
return NULL;
if (bus_origin_changed(bus))
return NULL;
sd_bus_close(bus);
return sd_bus_unref(bus);
}
_public_ sd_bus* sd_bus_flush_close_unref(sd_bus *bus) {
if (!bus)
return NULL;
if (bus_origin_changed(bus))
return NULL;
/* Have to do this before flush() to prevent hang */
pidref_done_sigterm_wait(&bus->busexec_pidref);
sd_bus_flush(bus);
return sd_bus_close_unref(bus);
}
void bus_enter_closing(sd_bus *bus, int exit_code) {
sd_future *f;
int r;
assert(bus);
if (!IN_SET(bus->state, BUS_WATCH_BIND, BUS_OPENING, BUS_AUTHENTICATING, BUS_HELLO, BUS_RUNNING))
return;
bus_set_state(bus, BUS_CLOSING);
bus->exit_code = exit_code;
/* Cancel all outstanding fiber-dispatched method handlers. Most cancellations are scheduled
* asynchronously (fibers resolve with -ECANCELED the next time they run), but a fiber still
* in FIBER_STATE_INITIAL resolves synchronously, which fires bus_fiber_resolved() and
* removes f from this set mid-iteration. That's safe because SET_FOREACH permits removal of
* exactly the current entry — see the assertion in hashmap_iterate_entry(). Either way this
* doesn't block here: process_closing() waits for the fiber_futures set to drain before it
* continues tearing down the rest of the bus. */
SET_FOREACH(f, bus->fiber_futures) {
r = sd_future_cancel(f);
if (r < 0)
log_debug_errno(r, "Failed to cancel outstanding fiber method handler, ignoring: %m");
}
}
/* Define manually so we can add the PID check */
_public_ sd_bus* sd_bus_ref(sd_bus *bus) {
if (!bus)
return NULL;
if (bus_origin_changed(bus))
return NULL;
bus->n_ref++;
return bus;
}
_public_ sd_bus* sd_bus_unref(sd_bus *bus) {
if (!bus)
return NULL;
if (bus_origin_changed(bus))
return NULL;
assert(bus->n_ref > 0);
if (--bus->n_ref > 0)
return NULL;
return bus_free(bus);
}
_public_ int sd_bus_is_open(sd_bus *bus) {
if (!bus)
return 0;
assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_origin_changed(bus), -ECHILD);
return BUS_IS_OPEN(bus->state);
}
_public_ int sd_bus_is_ready(sd_bus *bus) {
if (!bus)
return 0;
assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(!bus_origin_changed(bus), -ECHILD);
return bus->state == BUS_RUNNING;
}
_public_ int sd_bus_can_send(sd_bus *bus, char type) {
int r;
assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(bus->state != BUS_UNSET, -ENOTCONN);
assert_return(!bus_origin_changed(bus), -ECHILD);
if (bus->is_monitor)
return 0;
if (type == SD_BUS_TYPE_UNIX_FD) {
if (!bus->accept_fd)
return 0;
r = bus_ensure_running(bus);
if (r < 0)
return r;
return bus->can_fds;
}
return bus_type_is_valid(type);
}
_public_ int sd_bus_get_bus_id(sd_bus *bus, sd_id128_t *ret) {
int r;
assert_return(bus, -EINVAL);
assert_return(bus = bus_resolve(bus), -ENOPKG);
assert_return(ret, -EINVAL);
assert_return(!bus_origin_changed(bus), -ECHILD);
r = bus_ensure_running(bus);
if (r < 0)
return r;
*ret = bus->server_id;
return 0;
}
#define COOKIE_CYCLED (UINT32_C(1) << 31)
static uint64_t cookie_inc(uint64_t cookie) {
/* Stay within the 32-bit range, since classic D-Bus can't deal with more */
if (cookie >= UINT32_MAX)
return COOKIE_CYCLED; /* Don't go back to zero, but use the highest bit for checking
* whether we are looping. */
return cookie + 1;
}
static int next_cookie(sd_bus *b) {
uint64_t new_cookie;
assert(b);
new_cookie = cookie_inc(b->cookie);
/* Small optimization: don't bother with checking for cookie reuse until we overran cookiespace at
* least once, but then do it thorougly. */
if (FLAGS_SET(new_cookie, COOKIE_CYCLED)) {
uint32_t i;
/* Check if the cookie is currently in use. If so, pick the next one */
for (i = 0; i < COOKIE_CYCLED; i++) {
if (!ordered_hashmap_contains(b->reply_callbacks, &new_cookie))
goto good;
new_cookie = cookie_inc(new_cookie);
}
/* Can't fulfill request */
return -EBUSY;
}
good:
b->cookie = new_cookie;
return 0;
}
static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
int r;
assert(b);
assert(m);
if (m->sealed) {
/* If we copy the same message to multiple
* destinations, avoid using the same cookie
* numbers. */
b->cookie = MAX(b->cookie, BUS_MESSAGE_COOKIE(m));
return 0;
}
if (timeout == 0) {
r = sd_bus_get_method_call_timeout(b, &timeout);
if (r < 0)
return r;
}