chiark / gitweb /
unit: introduce %s specifier for the user shell
authorLennart Poettering <lennart@poettering.net>
Mon, 16 Jul 2012 10:34:54 +0000 (12:34 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 16 Jul 2012 10:34:54 +0000 (12:34 +0200)
man/systemd.unit.xml
src/core/execute.c
src/core/unit.c
src/login/loginctl.c
src/login/logind.c
src/nspawn/nspawn.c
src/shared/util.c
src/shared/util.h
src/tmpfiles/tmpfiles.c

index 3236bfa7416f07670294827c0984ba6df5df9f6f..4a2e9c3bf93dc07103eb0e1cb55d0732b4b1284f 100644 (file)
                         <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>
                       </row>
+                      <row>
+                        <entry><literal>%s</literal></entry>
+                        <entry>User shell</entry>
+                        <entry>This is the shell of the configured user of the unit, or (if none is set) the user running the systemd instance.</entry>
+                      </row>
                     </tbody>
                   </tgroup>
                 </table>
index c0e8f9e01302677ea1fd89016ea0885ce530b90d..daba1a384683f766470847bdf7fddebac8f5db76 100644 (file)
@@ -1182,7 +1182,7 @@ int exec_spawn(ExecCommand *command,
 
                 if (context->user) {
                         username = context->user;
-                        err = get_user_creds(&username, &uid, &gid, &home);
+                        err = get_user_creds(&username, &uid, &gid, &home, NULL);
                         if (err < 0) {
                                 r = EXIT_USER;
                                 goto fail_child;
index ed519b3bd561f265ec7fe4f6d1af6034b692380c..673af13c3eb3bf5a5e3bb3764d08908be8a82aac 100644 (file)
@@ -2232,7 +2232,7 @@ static char *specifier_user_name(char specifier, void *data, void *userdata) {
 
         /* fish username from passwd */
         username = s->exec_context.user;
-        r = get_user_creds(&username, NULL, NULL, NULL);
+        r = get_user_creds(&username, NULL, NULL, NULL, NULL);
         if (r < 0)
                 return NULL;
 
@@ -2256,13 +2256,37 @@ static char *specifier_user_home(char specifier, void *data, void *userdata) {
         }
 
         username = s->exec_context.user;
-        r = get_user_creds(&username, NULL, NULL, &home);
+        r = get_user_creds(&username, NULL, NULL, &home, NULL);
         if (r < 0)
                return NULL;
 
         return strdup(home);
 }
 
+static char *specifier_user_shell(char specifier, void *data, void *userdata) {
+        Service *s = userdata;
+        int r;
+        const char *username, *shell;
+
+        /* return HOME if set, otherwise from passwd */
+        if (!s->exec_context.user) {
+                char *sh;
+
+                r = get_shell(&sh);
+                if (r < 0)
+                        return strdup("/bin/sh");
+
+                return sh;
+        }
+
+        username = s->exec_context.user;
+        r = get_user_creds(&username, NULL, NULL, NULL, &shell);
+        if (r < 0)
+                return strdup("/bin/sh");
+
+        return strdup(shell);
+}
+
 char *unit_name_printf(Unit *u, const char* format) {
 
         /*
@@ -2316,6 +2340,7 @@ char *unit_full_printf(Unit *u, const char *format) {
                 { 't', specifier_runtime,             NULL },
                 { 'u', specifier_user_name,           NULL },
                 { 'h', specifier_user_home,           NULL },
+                { 's', specifier_user_shell,          NULL },
                 { 0, NULL, NULL }
         };
 
index a838a54086b7daa52dc23fbb402abdac7d2d73fd..7b751e778fa29d4dc826d28b339de115da4e78b7 100644 (file)
@@ -1089,7 +1089,7 @@ static int show(DBusConnection *bus, char **args, unsigned n) {
                         uid_t uid;
                         uint32_t u;
 
-                        ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL);
+                        ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
                         if (ret < 0) {
                                 log_error("User %s unknown.", args[i]);
                                 goto finish;
@@ -1320,7 +1320,7 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) {
                         goto finish;
                 }
 
-                ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL);
+                ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
                 if (ret < 0) {
                         log_error("Failed to resolve user %s: %s", args[i], strerror(-ret));
                         goto finish;
@@ -1387,7 +1387,7 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) {
                         goto finish;
                 }
 
-                ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL);
+                ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
                 if (ret < 0) {
                         log_error("Failed to look up user %s: %s", args[i], strerror(-ret));
                         goto finish;
@@ -1455,7 +1455,7 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) {
                         goto finish;
                 }
 
-                ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL);
+                ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
                 if (ret < 0) {
                         log_error("Failed to look up user %s: %s", args[i], strerror(-ret));
                         goto finish;
index 1eb745adcecfafb9166ca506bb75da5a26cc6a52..07755831477e6c3a36287f375a067f7d656e288e 100644 (file)
@@ -279,7 +279,7 @@ int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
         assert(m);
         assert(name);
 
-        r = get_user_creds(&name, &uid, &gid, NULL);
+        r = get_user_creds(&name, &uid, &gid, NULL, NULL);
         if (r < 0)
                 return r;
 
index ffd8fd4ef50a74ac22ebb8fb6c590f1abd09ea85..2879db11eb5542447648211757a922ecc73e3b7a 100644 (file)
@@ -1059,7 +1059,7 @@ int main(int argc, char *argv[]) {
 
                 if (arg_user) {
 
-                        if (get_user_creds((const char**)&arg_user, &uid, &gid, &home) < 0) {
+                        if (get_user_creds((const char**)&arg_user, &uid, &gid, &home, NULL) < 0) {
                                 log_error("get_user_creds() failed: %m");
                                 goto child_fail;
                         }
index a0755efb147593a2046e0feaf386986b047b4664..43ec62eac653d67a10190187c82eb0c76a551934 100644 (file)
@@ -4905,7 +4905,12 @@ int socket_from_display(const char *display, char **path) {
         return 0;
 }
 
-int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) {
+int get_user_creds(
+                const char **username,
+                uid_t *uid, gid_t *gid,
+                const char **home,
+                const char **shell) {
+
         struct passwd *p;
         uid_t u;
 
@@ -4926,6 +4931,10 @@ int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **h
 
                 if (home)
                         *home = "/root";
+
+                if (shell)
+                        *shell = "/bin/sh";
+
                 return 0;
         }
 
@@ -4957,6 +4966,9 @@ int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **h
         if (home)
                 *home = p->pw_dir;
 
+        if (shell)
+                *shell = p->pw_shell;
+
         return 0;
 }
 
index d3546ba1cdbaa9e4bef7ef700cc3fe16baecb40e..3915904442e2047ba7993832429c61812213f141 100644 (file)
@@ -441,7 +441,7 @@ int fchmod_umask(int fd, mode_t mode);
 bool display_is_local(const char *display);
 int socket_from_display(const char *display, char **path);
 
-int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home);
+int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
 int get_group_creds(const char **groupname, gid_t *gid);
 
 int in_group(const char *name);
index 3b52b9889cc55f71cb54c5434b8594c4d215f045..7a453dcc2aeddef8bfb9564801f0589c6705dd36 100644 (file)
@@ -1076,7 +1076,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
         if (user && !streq(user, "-")) {
                 const char *u = user;
 
-                r = get_user_creds(&u, &i->uid, NULL, NULL);
+                r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
                 if (r < 0) {
                         log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
                         goto finish;