chiark / gitweb /
core: add new "scope" unit type for making a unit of pre-existing processes
[elogind.git] / src / core / unit-printf.c
index cbae45d9f75c2f2db756982acac52f7154ef4772..caf51259d2d18a72eefabb5e69cf5a5bfd7bfb41 100644 (file)
@@ -26,6 +26,9 @@
 #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) {
         Unit *u = userdata;
@@ -85,22 +88,22 @@ static char *specifier_cgroup(char specifier, void *data, void *userdata) {
 }
 
 static char *specifier_cgroup_root(char specifier, void *data, void *userdata) {
+        _cleanup_free_ char *p = NULL;
         Unit *u = userdata;
-        char *p;
-        assert(u);
+        const char *slice;
+        int r;
 
-        if (specifier == 'r')
-                return strdup(u->manager->cgroup_hierarchy);
+        assert(u);
 
-        if (path_get_parent(u->manager->cgroup_hierarchy, &p) < 0)
-                return strdup("");
+        slice = unit_slice_name(u);
+        if (specifier == 'R' || !slice)
+                return strdup(u->manager->cgroup_root);
 
-        if (streq(p, "/")) {
-                free(p);
-                return strdup("");
-        }
+        r = cg_slice_to_path(slice, &p);
+        if (r < 0)
+                return NULL;
 
-        return p;
+        return strjoin(u->manager->cgroup_root, "/", p, NULL);
 }
 
 static char *specifier_runtime(char specifier, void *data, void *userdata) {
@@ -123,24 +126,36 @@ static char *specifier_user_name(char specifier, void *data, void *userdata) {
         ExecContext *c;
         int r;
         const char *username;
+        _cleanup_free_ char *tmp = NULL;
+        uid_t uid;
+        char *printed = NULL;
 
         assert(u);
 
         c = unit_get_exec_context(u);
-        if (!c)
-                return NULL;
 
-        /* get USER env from our own env if set */
-        if (!c->user)
-                return getusername_malloc();
+        if (c && c->user)
+                username = c->user;
+        else
+                /* get USER env from env or our own uid */
+                username = tmp = getusername_malloc();
 
         /* fish username from passwd */
-        username = c->user;
-        r = get_user_creds(&username, NULL, NULL, NULL, NULL);
+        r = get_user_creds(&username, &uid, NULL, NULL, NULL);
         if (r < 0)
                 return NULL;
 
-        return strdup(username);
+        switch (specifier) {
+                case 'U':
+                        if (asprintf(&printed, "%d", uid) < 0)
+                                return NULL;
+                        break;
+                case 'u':
+                        printed = strdup(username);
+                        break;
+        }
+
+        return printed;
 }
 
 static char *specifier_user_home(char specifier, void *data, void *userdata) {
@@ -152,11 +167,9 @@ static char *specifier_user_home(char specifier, void *data, void *userdata) {
         assert(u);
 
         c = unit_get_exec_context(u);
-        if (!c)
-                return NULL;
 
         /* return HOME if set, otherwise from passwd */
-        if (!c->user) {
+        if (!c || !c->user) {
                 char *h;
 
                 r = get_home_dir(&h);
@@ -179,66 +192,37 @@ static char *specifier_user_shell(char specifier, void *data, void *userdata) {
         ExecContext *c;
         int r;
         const char *username, *shell;
+        char *ret;
 
         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;
-        }
+        if (c && c->user)
+                username = c->user;
+        else
+                username = "root";
 
-        username = c->user;
+        /* return /bin/sh for root, otherwise the value from passwd */
         r = get_user_creds(&username, NULL, NULL, NULL, &shell);
-        if (r < 0)
-                return strdup("/bin/sh");
-
-        return strdup(shell);
-}
-
-static char *specifier_machine_id(char specifier, void *data, void *userdata) {
-        sd_id128_t id;
-        char *buf;
-        int r;
-
-        r = sd_id128_get_machine(&id);
-        if (r < 0)
-                return NULL;
-
-        buf = new(char, 33);
-        if (!buf)
-                return NULL;
-
-        return sd_id128_to_string(id, buf);
-}
-
-static char *specifier_boot_id(char specifier, void *data, void *userdata) {
-        sd_id128_t id;
-        char *buf;
-        int r;
-
-        r = sd_id128_get_boot(&id);
-        if (r < 0)
+        if (r < 0) {
+                log_warning_unit(u->id,
+                                 "Failed to determine shell: %s",
+                                 strerror(-r));
                 return NULL;
+        }
 
-        buf = new(char, 33);
-        if (!buf)
-                return NULL;
+        if (!path_is_absolute(shell)) {
+                log_warning_unit(u->id,
+                                 "Shell %s is not absolute, ignoring.",
+                                 shell);
+        }
 
-        return sd_id128_to_string(id, buf);
-}
+        ret = strdup(shell);
+        if (!ret)
+                log_oom();
 
-static char *specifier_host_name(char specifier, void *data, void *userdata) {
-        return gethostname_malloc();
+        return ret;
 }
 
 char *unit_name_printf(Unit *u, const char* format) {
@@ -274,15 +258,16 @@ char *unit_full_printf(Unit *u, const char *format) {
          *
          * %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 place 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
          */
 
         const Specifier table[] = {
@@ -298,6 +283,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 },
@@ -308,7 +294,6 @@ char *unit_full_printf(Unit *u, const char *format) {
                 { 0, NULL, NULL }
         };
 
-        assert(u);
         assert(format);
 
         return specifier_printf(format, table, u);