From: Lennart Poettering Date: Mon, 16 Jul 2012 10:34:54 +0000 (+0200) Subject: unit: introduce %s specifier for the user shell X-Git-Tag: v187~71 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=d05c5031ad4c528fe6bbfed289519edb9f13180a;hp=b2896c905bef7be7ed9a760d9d61aa6ad0f614a3 unit: introduce %s specifier for the user shell --- diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml index 3236bfa74..4a2e9c3bf 100644 --- a/man/systemd.unit.xml +++ b/man/systemd.unit.xml @@ -268,6 +268,11 @@ User home directory This is the home directory of the configured user of the unit, or (if none is set) the user running the systemd instance. + + %s + User shell + This is the shell of the configured user of the unit, or (if none is set) the user running the systemd instance. + diff --git a/src/core/execute.c b/src/core/execute.c index c0e8f9e01..daba1a384 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -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; diff --git a/src/core/unit.c b/src/core/unit.c index ed519b3bd..673af13c3 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -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 } }; diff --git a/src/login/loginctl.c b/src/login/loginctl.c index a838a5408..7b751e778 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -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; diff --git a/src/login/logind.c b/src/login/logind.c index 1eb745adc..077558314 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -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; diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index ffd8fd4ef..2879db11e 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -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; } diff --git a/src/shared/util.c b/src/shared/util.c index a0755efb1..43ec62eac 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -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; } diff --git a/src/shared/util.h b/src/shared/util.h index d3546ba1c..391590444 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -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); diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 3b52b9889..7a453dcc2 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -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;