[systemd-devel] [PATCH] unit: add specifiers for user name and home directory

Ran Benita ran234 at gmail.com
Thu Dec 8 17:00:22 PST 2011


Currently there is no way to refer to the user's name or home directory
from within a unit file. This is mainly a problem for unit files
intended for a --user systemd instance, where you might want to set the
user's home directory in WorkingDirectory, or otherwise refer to the
user's name.

This patch add two specifiers, %u and %~, for the user's name and home
directory. Note that these refer to the user who's running the instance,
and is otherwise unaffected by User=, RootDirectory=, etc.
---
 man/systemd.unit.xml |   10 ++++++++++
 src/unit.c           |   26 ++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml
index 897f99f..59868d5 100644
--- a/man/systemd.unit.xml
+++ b/man/systemd.unit.xml
@@ -258,6 +258,16 @@
                         <entry>Runtime socket dir</entry>
                         <entry>This is either /run (for the system manager) or $XDG_RUNTIME_DIR (for user managers).</entry>
                       </row>
+                      <row>
+                        <entry><literal>%u</literal></entry>
+                        <entry>User name</entry>
+                        <entry>This is the name of the user who is running the systemd instance.</entry>
+                      </row>
+                      <row>
+                        <entry><literal>%~</literal></entry>
+                        <entry>User home directory</entry>
+                        <entry>This is the home directory of the user who is running the systemd instance.</entry>
+                      </row>
                     </tbody>
                   </tgroup>
                 </table>
diff --git a/src/unit.c b/src/unit.c
index 018e986..b1e2bca 100644
--- a/src/unit.c
+++ b/src/unit.c
@@ -2133,6 +2133,30 @@ static char *specifier_runtime(char specifier, void *data, void *userdata) {
         return strdup("/run");
 }
 
+static char *specifier_user_details(char specifier, void *data, void *userdata) {
+        int r;
+        char *username;
+        const char *home, *_username;
+        uid_t uid;
+        gid_t gid;
+
+        username = getlogname_malloc();
+        if (!username)
+                return NULL;
+
+        if (specifier == 'u')
+                return username;
+
+        _username = username;
+        r = get_user_creds(&_username, &uid, &gid, &home);
+        free(username);
+        if (r)
+                return NULL;
+
+        /* specifier == '~' */
+        return strdup(home);
+}
+
 char *unit_name_printf(Unit *u, const char* format) {
 
         /*
@@ -2182,6 +2206,8 @@ 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_details,        NULL },
+                { '~', specifier_user_details,        NULL },
                 { 0, NULL, NULL }
         };
 
-- 
1.7.7.4



More information about the systemd-devel mailing list