chiark / gitweb /
install: allow %u an and %U specifiers in WantedBy/RequiredBy/Alias
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 29 Jan 2013 03:34:38 +0000 (22:34 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 30 Jan 2013 00:01:41 +0000 (19:01 -0500)
src/shared/install-printf.c
src/shared/install.c
src/shared/install.h

index 85aebc4..10b0cf3 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "specifier.h"
 #include "unit-name.h"
+#include "util.h"
 #include "install-printf.h"
 
 static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
@@ -56,6 +57,42 @@ static char *specifier_instance(char specifier, void *data, void *userdata) {
                 return strdup("");
 }
 
+static char *specifier_user_name(char specifier, void *data, void *userdata) {
+        InstallInfo *i = userdata;
+        const char *username;
+        char _cleanup_free_ *tmp = NULL;
+        char *printed = NULL;
+
+        assert(i);
+
+        if (i->user)
+                username = i->user;
+        else
+                /* get USER env from env or our own uid */
+                username = tmp = getusername_malloc();
+
+        switch (specifier) {
+        case 'u':
+                printed = strdup(username);
+                break;
+        case 'U': {
+                /* fish username from passwd */
+                uid_t uid;
+                int r;
+
+                r = get_user_creds(&username, &uid, NULL, NULL, NULL);
+                if (r < 0)
+                        return NULL;
+
+                if (asprintf(&printed, "%d", uid) < 0)
+                        return NULL;
+                break;
+        }}
+
+        return printed;
+}
+
+
 char *install_full_printf(InstallInfo *i, const char *format) {
 
         /* This is similar to unit_full_printf() but does not support
@@ -79,8 +116,8 @@ char *install_full_printf(InstallInfo *i, const char *format) {
                 { 'p', specifier_prefix,              NULL },
                 { 'i', specifier_instance,            NULL },
 
-//                { 'U', specifier_user_name,           NULL },
-//                { 'u', specifier_user_name,           NULL },
+                { 'U', specifier_user_name,           NULL },
+                { 'u', specifier_user_name,           NULL },
 
                 { 'm', specifier_machine_id,          NULL },
                 { 'H', specifier_host_name,           NULL },
index 7fb352c..b82d189 100644 (file)
@@ -956,6 +956,33 @@ static int config_parse_also(
         return 0;
 }
 
+static int config_parse_user(
+                const char *filename,
+                unsigned line,
+                const char *section,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
+
+        InstallInfo *i = data;
+        char* printed;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+
+        printed = install_full_printf(i, rvalue);
+        if (!printed)
+                return -ENOMEM;
+
+        free(i->user);
+        i->user = printed;
+
+        return 0;
+}
+
 static int unit_file_load(
                 InstallContext *c,
                 InstallInfo *info,
@@ -967,6 +994,7 @@ static int unit_file_load(
                 { "Install", "WantedBy",   config_parse_strv, 0, &info->wanted_by   },
                 { "Install", "RequiredBy", config_parse_strv, 0, &info->required_by },
                 { "Install", "Also",       config_parse_also, 0, c                  },
+                { "Exec",    "User",       config_parse_user, 0, info               },
                 { NULL, NULL, NULL, 0, NULL }
         };
 
index 5a35125..755ddde 100644 (file)
@@ -66,6 +66,7 @@ typedef struct UnitFileList {
 typedef struct {
         char *name;
         char *path;
+        char *user;
 
         char **aliases;
         char **wanted_by;