[systemd-devel] [PATCH] add %U for uids in units

Daniel Wallace danielwallace at gtmanfred.com
Thu Dec 6 21:01:15 PST 2012


---
 man/systemd.unit.xml   |  5 +++++
 src/core/unit-printf.c | 17 +++++++++++++++--
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml
index 35644d3..bf91b4e 100644
--- a/man/systemd.unit.xml
+++ b/man/systemd.unit.xml
@@ -264,6 +264,11 @@
                         <entry>This is the name of the configured user of the unit, or (if none is set) the user running the systemd instance.</entry>
                       </row>
                       <row>
+                        <entry><literal>%U</literal></entry>
+                        <entry>User uid</entry>
+                        <entry>This is the uid of the configured user of the unit, or (if none is set) the user running the systemd instance.</entry>
+                      </row>
+                      <row>
                         <entry><literal>%h</literal></entry>
                         <entry>User home directory</entry>
                         <entry>This is the home directory of the configured user of the unit, or (if none is set) the user running the systemd instance.</entry>
diff --git a/src/core/unit-printf.c b/src/core/unit-printf.c
index a8eb60a..a58c96c 100644
--- a/src/core/unit-printf.c
+++ b/src/core/unit-printf.c
@@ -123,6 +123,8 @@ static char *specifier_user_name(char specifier, void *data, void *userdata) {
         ExecContext *c;
         int r;
         const char *username;
+        uid_t uid;
+        char *printed = NULL;
 
         assert(u);
 
@@ -134,11 +136,21 @@ static char *specifier_user_name(char specifier, void *data, void *userdata) {
 
         /* 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) {
@@ -292,6 +304,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 },
-- 
1.8.0.1



More information about the systemd-devel mailing list