X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fcore%2Funit-printf.c;h=4e18afab8bb62f989d3acd111657f6641a1bbec7;hp=cbae45d9f75c2f2db756982acac52f7154ef4772;hb=d003f514dab2dbf1a66e11800a50aeaf039d036c;hpb=67445f4e22ad924394acdd4fd49e6f238244a5ca diff --git a/src/core/unit-printf.c b/src/core/unit-printf.c index cbae45d9f..4e18afab8 100644 --- a/src/core/unit-printf.c +++ b/src/core/unit-printf.c @@ -26,222 +26,318 @@ #include "strv.h" #include "unit-name.h" #include "unit-printf.h" +#include "macro.h" +#include "cgroup-util.h" +#include "special.h" -static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) { +static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; + char *n; + assert(u); - return unit_name_to_prefix_and_instance(u->id); + n = unit_name_to_prefix_and_instance(u->id); + if (!n) + return -ENOMEM; + + *ret = n; + return 0; } -static char *specifier_prefix(char specifier, void *data, void *userdata) { +static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; + char *n; + assert(u); - return unit_name_to_prefix(u->id); + n = unit_name_to_prefix(u->id); + if (!n) + return -ENOMEM; + + *ret = n; + return 0; } -static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) { +static int specifier_prefix_unescaped(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; - char *p, *r; + _cleanup_free_ char *p = NULL; + char *n; assert(u); p = unit_name_to_prefix(u->id); if (!p) - return NULL; + return -ENOMEM; - r = unit_name_unescape(p); - free(p); + n = unit_name_unescape(p); + if (!n) + return -ENOMEM; - return r; + *ret = n; + return 0; } -static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) { +static int specifier_instance_unescaped(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; + char *n; + assert(u); - if (u->instance) - return unit_name_unescape(u->instance); + if (!u->instance) + return -ENOTSUP; - return strdup(""); + n = unit_name_unescape(u->instance); + if (!n) + return -ENOMEM; + + *ret = n; + return 0; } -static char *specifier_filename(char specifier, void *data, void *userdata) { +static int specifier_filename(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; + char *n; + assert(u); if (u->instance) - return unit_name_path_unescape(u->instance); - - return unit_name_to_path(u->id); + n = unit_name_path_unescape(u->instance); + else + n = unit_name_to_path(u->id); + if (!n) + return -ENOMEM; + + *ret = n; + return 0; } -static char *specifier_cgroup(char specifier, void *data, void *userdata) { +static int specifier_cgroup(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; + char *n; + assert(u); - return unit_default_cgroup_path(u); + if (u->cgroup_path) + n = strdup(u->cgroup_path); + else + n = unit_default_cgroup_path(u); + if (!n) + return -ENOMEM; + + *ret = n; + return 0; } -static char *specifier_cgroup_root(char specifier, void *data, void *userdata) { +static int specifier_cgroup_root(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; - char *p; + const char *slice; + char *n; + int r; + assert(u); - if (specifier == 'r') - return strdup(u->manager->cgroup_hierarchy); + slice = unit_slice_name(u); + if (specifier == 'R' || !slice) + n = strdup(u->manager->cgroup_root); + else { + _cleanup_free_ char *p = NULL; - if (path_get_parent(u->manager->cgroup_hierarchy, &p) < 0) - return strdup(""); + r = cg_slice_to_path(slice, &p); + if (r < 0) + return r; - if (streq(p, "/")) { - free(p); - return strdup(""); + n = strjoin(u->manager->cgroup_root, "/", p, NULL); + if (!n) + return -ENOMEM; } - return p; + *ret = n; + return 0; } -static char *specifier_runtime(char specifier, void *data, void *userdata) { +static int specifier_runtime(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; - assert(u); + const char *e; + char *n = NULL; - if (u->manager->running_as == SYSTEMD_USER) { - const char *e; + assert(u); + if (u->manager->running_as == SYSTEMD_SYSTEM) + e = "/run"; + else { e = getenv("XDG_RUNTIME_DIR"); - if (e) - return strdup(e); + if (!e) + return -ENOTSUP; } - return strdup("/run"); + n = strdup(e); + if (!n) + return -ENOMEM; + + *ret = n; + return 0; } -static char *specifier_user_name(char specifier, void *data, void *userdata) { +static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) { + char *printed = NULL; Unit *u = userdata; ExecContext *c; int r; - const char *username; assert(u); c = unit_get_exec_context(u); if (!c) - return NULL; + return -ENOTSUP; + + if (u->manager->running_as == SYSTEMD_SYSTEM) { + + /* We cannot use NSS from PID 1, hence try to make the + * best of it in that case, and fail if we can't help + * it */ + + if (!c->user || streq(c->user, "root") || streq(c->user, "0")) + printed = strdup(specifier == 'u' ? "root" : "0"); + else { + if (specifier == 'u') + printed = strdup(c->user); + else { + uid_t uid; + + r = parse_uid(c->user, &uid); + if (r < 0) + return -ENODATA; + + asprintf(&printed, "%lu", (unsigned long) uid); + } + } + + } else { + _cleanup_free_ char *tmp = NULL; + const char *username = NULL; + uid_t uid; + + if (c->user) + username = c->user; + else + /* get USER env from env or our own uid */ + username = tmp = getusername_malloc(); + + /* fish username from passwd */ + r = get_user_creds(&username, &uid, NULL, NULL, NULL); + if (r < 0) + return r; - /* get USER env from our own env if set */ - if (!c->user) - return getusername_malloc(); + if (specifier == 'u') + printed = strdup(username); + else + asprintf(&printed, "%lu", (unsigned long) uid); + } - /* fish username from passwd */ - username = c->user; - r = get_user_creds(&username, NULL, NULL, NULL, NULL); - if (r < 0) - return NULL; + if (!printed) + return -ENOMEM; - return strdup(username); + *ret = printed; + return 0; } -static char *specifier_user_home(char specifier, void *data, void *userdata) { +static int specifier_user_home(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; ExecContext *c; + char *n; int r; - const char *username, *home; assert(u); c = unit_get_exec_context(u); if (!c) - return NULL; + return -ENOTSUP; - /* return HOME if set, otherwise from passwd */ - if (!c->user) { - char *h; + if (u->manager->running_as == SYSTEMD_SYSTEM) { - r = get_home_dir(&h); - if (r < 0) - return NULL; + /* We cannot use NSS from PID 1, hence try to make the + * best of it if we can, but fail if we can't */ + + if (!c->user || streq(c->user, "root") || streq(c->user, "0")) + n = strdup("/root"); + else + return -ENOTSUP; - return h; + } else { + + /* return HOME if set, otherwise from passwd */ + if (!c || !c->user) { + r = get_home_dir(&n); + if (r < 0) + return r; + } else { + const char *username, *home; + + username = c->user; + r = get_user_creds(&username, NULL, NULL, &home, NULL); + if (r < 0) + return r; + + n = strdup(home); + } } - username = c->user; - r = get_user_creds(&username, NULL, NULL, &home, NULL); - if (r < 0) - return NULL; + if (!n) + return -ENOMEM; - return strdup(home); + *ret = n; + return 0; } -static char *specifier_user_shell(char specifier, void *data, void *userdata) { +static int specifier_user_shell(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; ExecContext *c; + char *n; int r; - const char *username, *shell; assert(u); c = unit_get_exec_context(u); if (!c) - return NULL; - - /* return HOME if set, otherwise from passwd */ - if (!c->user) { - char *sh; - - r = get_shell(&sh); - if (r < 0) - return strdup("/bin/sh"); - - return sh; - } - - username = c->user; - r = get_user_creds(&username, NULL, NULL, NULL, &shell); - if (r < 0) - return strdup("/bin/sh"); - - return strdup(shell); -} + return -ENOTSUP; -static char *specifier_machine_id(char specifier, void *data, void *userdata) { - sd_id128_t id; - char *buf; - int r; + if (u->manager->running_as == SYSTEMD_SYSTEM) { - r = sd_id128_get_machine(&id); - if (r < 0) - return NULL; + /* We cannot use NSS from PID 1, hence try to make the + * best of it if we can, but fail if we can't */ - buf = new(char, 33); - if (!buf) - return NULL; + if (!c->user || streq(c->user, "root") || streq(c->user, "0")) + n = strdup("/bin/sh"); + else + return -ENOTSUP; - return sd_id128_to_string(id, buf); -} + } else { -static char *specifier_boot_id(char specifier, void *data, void *userdata) { - sd_id128_t id; - char *buf; - int r; + /* return /bin/sh for root, otherwise the value from passwd */ + if (!c->user) { + r = get_shell(&n); + if (r < 0) + return r; + } else { + const char *username, *shell; - r = sd_id128_get_boot(&id); - if (r < 0) - return NULL; + username = c->user; + r = get_user_creds(&username, NULL, NULL, NULL, &shell); + if (r < 0) + return r; - buf = new(char, 33); - if (!buf) - return NULL; + n = strdup(shell); + } + } - return sd_id128_to_string(id, buf); -} + if (!n) + return -ENOMEM; -static char *specifier_host_name(char specifier, void *data, void *userdata) { - return gethostname_malloc(); + *ret = n; + return 0; } -char *unit_name_printf(Unit *u, const char* format) { +int unit_name_printf(Unit *u, const char* format, char **ret) { /* * This will use the passed string as format string and @@ -263,26 +359,29 @@ char *unit_name_printf(Unit *u, const char* format) { assert(u); assert(format); + assert(ret); - return specifier_printf(format, table, u); + return specifier_printf(format, table, u, ret); } -char *unit_full_printf(Unit *u, const char *format) { +int unit_full_printf(Unit *u, const char *format, char **ret) { /* This is similar to unit_name_printf() but also supports * unescaping. Also, adds a couple of additional codes: * * %f the the instance if set, otherwise the id * %c cgroup path of unit - * %r root cgroup path of this systemd instance (e.g. "/user/lennart/shared/systemd-4711") - * %R parent of root cgroup path (e.g. "/usr/lennart/shared") + * %r where units in this slice are placed in the cgroup tree + * %R the root of this systemd's instance tree * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR) + * %U the UID of the configured user or running user * %u the username of the configured user or running user * %h the homedir of the configured user or running user * %s the shell of the configured user or running user * %m the machine ID of the running system - * %b the boot ID of the running system * %H the host name of the running system + * %b the boot ID of the running system + * %v `uname -r` of the running system */ const Specifier table[] = { @@ -298,6 +397,7 @@ char *unit_full_printf(Unit *u, const char *format) { { 'r', specifier_cgroup_root, NULL }, { 'R', specifier_cgroup_root, NULL }, { 't', specifier_runtime, NULL }, + { 'U', specifier_user_name, NULL }, { 'u', specifier_user_name, NULL }, { 'h', specifier_user_home, NULL }, { 's', specifier_user_shell, NULL }, @@ -305,18 +405,21 @@ char *unit_full_printf(Unit *u, const char *format) { { 'm', specifier_machine_id, NULL }, { 'H', specifier_host_name, NULL }, { 'b', specifier_boot_id, NULL }, - { 0, NULL, NULL } + { 'v', specifier_kernel_release, NULL }, + {} }; assert(u); assert(format); + assert(ret); - return specifier_printf(format, table, u); + return specifier_printf(format, table, u, ret); } -char **unit_full_printf_strv(Unit *u, char **l) { +int unit_full_printf_strv(Unit *u, char **l, char ***ret) { size_t n; char **r, **i, **j; + int q; /* Applies unit_full_printf to every entry in l */ @@ -325,22 +428,22 @@ char **unit_full_printf_strv(Unit *u, char **l) { n = strv_length(l); r = new(char*, n+1); if (!r) - return NULL; + return -ENOMEM; for (i = l, j = r; *i; i++, j++) { - *j = unit_full_printf(u, *i); - if (!*j) + q = unit_full_printf(u, *i, j); + if (q < 0) goto fail; } *j = NULL; - return r; + *ret = r; + return 0; fail: for (j--; j >= r; j--) free(*j); free(r); - - return NULL; + return q; }