mirror of
https://github.com/systemd/systemd.git
synced 2025-01-13 17:18:18 +03:00
sd-bus: add new call sd_bus_get_scope() for querying whether one is connected to a system or a user bus
This commit is contained in:
parent
52cfc0379a
commit
3acc1dafd1
@ -1845,3 +1845,39 @@ int bus_kernel_fix_attach_mask(void) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bus_kernel_get_bus_name(sd_bus *bus, char **name) {
|
||||
struct kdbus_cmd_info cmd = {
|
||||
.size = sizeof(struct kdbus_cmd_info),
|
||||
};
|
||||
struct kdbus_info *info;
|
||||
struct kdbus_item *item;
|
||||
char *n = NULL;
|
||||
int r;
|
||||
|
||||
assert(bus);
|
||||
assert(name);
|
||||
assert(bus->is_kernel);
|
||||
|
||||
r = ioctl(bus->input_fd, KDBUS_CMD_BUS_CREATOR_INFO, &cmd);
|
||||
if (r < 0)
|
||||
return -errno;
|
||||
|
||||
info = (struct kdbus_info*) ((uint8_t*) bus->kdbus_buffer + cmd.offset);
|
||||
|
||||
KDBUS_ITEM_FOREACH(item, info, items)
|
||||
if (item->type == KDBUS_ITEM_MAKE_NAME) {
|
||||
r = free_and_strdup(&n, item->str);
|
||||
break;
|
||||
}
|
||||
|
||||
bus_kernel_cmd_free(bus, cmd.offset);
|
||||
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (!n)
|
||||
return -EIO;
|
||||
|
||||
*name = n;
|
||||
return 0;
|
||||
}
|
||||
|
@ -92,4 +92,6 @@ int bus_kernel_realize_attach_flags(sd_bus *bus);
|
||||
|
||||
int bus_kernel_fix_attach_mask(void);
|
||||
|
||||
int bus_kernel_get_bus_name(sd_bus *bus, char **name);
|
||||
|
||||
int bus_kernel_cmd_free(sd_bus *bus, uint64_t offset);
|
||||
|
@ -1193,11 +1193,18 @@ static int status(sd_bus *bus, char *argv[]) {
|
||||
&creds,
|
||||
pid,
|
||||
_SD_BUS_CREDS_ALL);
|
||||
} else
|
||||
} else {
|
||||
const char *scope;
|
||||
|
||||
r = sd_bus_get_scope(bus, &scope);
|
||||
if (r >= 0)
|
||||
printf("Scope=%s%s%s\n", ansi_highlight(), scope, ansi_highlight_off());
|
||||
|
||||
r = sd_bus_get_owner_creds(
|
||||
bus,
|
||||
(arg_augment_creds ? SD_BUS_CREDS_AUGMENT : 0) | _SD_BUS_CREDS_ALL,
|
||||
&creds);
|
||||
}
|
||||
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to get credentials: %m");
|
||||
@ -2006,10 +2013,13 @@ int main(int argc, char *argv[]) {
|
||||
switch (arg_transport) {
|
||||
|
||||
case BUS_TRANSPORT_LOCAL:
|
||||
if (arg_user)
|
||||
if (arg_user) {
|
||||
bus->is_user = true;
|
||||
r = bus_set_address_user(bus);
|
||||
else
|
||||
} else {
|
||||
bus->is_system = true;
|
||||
r = bus_set_address_system(bus);
|
||||
}
|
||||
break;
|
||||
|
||||
case BUS_TRANSPORT_REMOTE:
|
||||
|
@ -1283,6 +1283,7 @@ _public_ int sd_bus_open_system_remote(sd_bus **ret, const char *host) {
|
||||
|
||||
bus->bus_client = true;
|
||||
bus->trusted = false;
|
||||
bus->is_system = true;
|
||||
|
||||
r = sd_bus_start(bus);
|
||||
if (r < 0)
|
||||
@ -1335,6 +1336,7 @@ _public_ int sd_bus_open_system_container(sd_bus **ret, const char *machine) {
|
||||
|
||||
bus->bus_client = true;
|
||||
bus->trusted = false;
|
||||
bus->is_system = true;
|
||||
|
||||
r = sd_bus_start(bus);
|
||||
if (r < 0)
|
||||
@ -3376,3 +3378,43 @@ int bus_get_root_path(sd_bus *bus) {
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
_public_ int sd_bus_get_scope(sd_bus *bus, const char **scope) {
|
||||
int r;
|
||||
|
||||
assert_return(bus, -EINVAL);
|
||||
assert_return(scope, -EINVAL);
|
||||
assert_return(!bus_pid_changed(bus), -ECHILD);
|
||||
|
||||
if (bus->is_kernel) {
|
||||
_cleanup_free_ char *n = NULL;
|
||||
const char *dash;
|
||||
|
||||
r = bus_kernel_get_bus_name(bus, &n);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (streq(n, "0-system")) {
|
||||
*scope = "system";
|
||||
return 1;
|
||||
}
|
||||
|
||||
dash = strchr(n, '-');
|
||||
if (streq(dash, "-user")) {
|
||||
*scope = "user";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (bus->is_user) {
|
||||
*scope = "user";
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (bus->is_system) {
|
||||
*scope = "system";
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -ENODATA;
|
||||
}
|
||||
|
@ -33,7 +33,7 @@
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
_cleanup_close_ int bus_ref = -1;
|
||||
_cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
|
||||
_cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL, *bname = NULL;
|
||||
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
|
||||
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
const char *ua = NULL, *ub = NULL, *the_string = NULL;
|
||||
@ -102,6 +102,9 @@ int main(int argc, char *argv[]) {
|
||||
assert_se(r >= 0);
|
||||
printf("name of b: %s\n", nn);
|
||||
|
||||
assert_se(bus_kernel_get_bus_name(b, &bname) >= 0);
|
||||
assert_se(endswith(bname, name));
|
||||
|
||||
r = sd_bus_call_method(a, "this.doesnt.exist", "/foo", "meh.mah", "muh", &error, NULL, "s", "yayayay");
|
||||
assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_SERVICE_UNKNOWN));
|
||||
assert_se(r == -EHOSTUNREACH);
|
||||
|
@ -140,6 +140,7 @@ int sd_bus_is_open(sd_bus *bus);
|
||||
int sd_bus_can_send(sd_bus *bus, char type);
|
||||
int sd_bus_get_owner_id(sd_bus *bus, sd_id128_t *id);
|
||||
int sd_bus_get_owner_creds(sd_bus *bus, uint64_t creds_mask, sd_bus_creds **ret);
|
||||
int sd_bus_get_scope(sd_bus *bus, const char **scope);
|
||||
int sd_bus_get_description(sd_bus *bus, const char **description);
|
||||
int sd_bus_get_tid(sd_bus *bus, pid_t *tid);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user