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

Ran Benita ran234 at gmail.com
Fri Dec 16 02:34:00 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 %h, 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           |   24 ++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml
index 897f99f..2843007 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>%h</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 03c90f5..d45e16f 100644
--- a/src/unit.c
+++ b/src/unit.c
@@ -2145,6 +2145,28 @@ 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;
+
+        username = getlogname_malloc();
+        if (!username)
+                return NULL;
+
+        if (specifier == 'u')
+                return username;
+
+        _username = username;
+        r = get_user_creds(&_username, NULL, NULL, &home);
+        free(username);
+        if (r)
+                return NULL;
+
+        /* specifier == 'h' */
+        return strdup(home);
+}
+
 char *unit_name_printf(Unit *u, const char* format) {
 
         /*
@@ -2194,6 +2216,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 },
+                { 'h', specifier_user_details,        NULL },
                 { 0, NULL, NULL }
         };
 
-- 
1.7.8



More information about the systemd-devel mailing list