a52f674
From ead2d686d9c9145fef59ce737e91515d9b42f845 Mon Sep 17 00:00:00 2001
794d16c
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
794d16c
Date: Wed, 2 Oct 2013 13:23:10 +0200
794d16c
Subject: [PATCH] execute.c: always set $SHELL
794d16c
794d16c
In e6dca81 $SHELL was added to user@.service. Let's
794d16c
instead provide it to all units which have a user.
794d16c
---
794d16c
 TODO                   |  2 --
794d16c
 man/systemd.exec.xml   | 21 +++++++++++++++++--
794d16c
 src/core/execute.c     | 56 +++++++++++++++++---------------------------------
794d16c
 units/user@.service.in |  1 -
794d16c
 4 files changed, 38 insertions(+), 42 deletions(-)
794d16c
794d16c
diff --git a/TODO b/TODO
794d16c
index 07269f4..425f673 100644
794d16c
--- a/TODO
794d16c
+++ b/TODO
794d16c
@@ -54,8 +54,6 @@ CGroup Rework Completion:
794d16c
 
794d16c
 Features:
794d16c
 
794d16c
-* set $SHELL where we set $HOME and $USER when User= is set of a service, drop its manual setting from user@.service
794d16c
-
794d16c
 * we probably should replace the left-over uses of strv_append() and replace them by strv_push() or strv_extend()
794d16c
 
794d16c
 * move config_parse_path_strv() out of conf-parser.c
794d16c
diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml
794d16c
index f50161f..e213ec4 100644
794d16c
--- a/man/systemd.exec.xml
794d16c
+++ b/man/systemd.exec.xml
794d16c
@@ -1021,10 +1021,13 @@
794d16c
 
794d16c
                         <varlistentry>
794d16c
                                 <term><varname>$USER</varname></term>
794d16c
+                                <term><varname>$LOGNAME</varname></term>
794d16c
                                 <term><varname>$HOME</varname></term>
794d16c
+                                <term><varname>$SHELL</varname></term>
794d16c
 
794d16c
-                                <listitem><para>User name and home
794d16c
-                                directory.  Set for the units which
794d16c
+                                <listitem><para>User name (twice), home
794d16c
+                                directory, and the login shell.
794d16c
+                                Set for the units which
794d16c
                                 have <varname>User=</varname> set,
794d16c
                                 which includes user
794d16c
                                 <command>systemd</command> instances.
794d16c
@@ -1080,6 +1083,20 @@
794d16c
                                 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
794d16c
                                 </para></listitem>
794d16c
                         </varlistentry>
794d16c
+
794d16c
+                        <varlistentry>
794d16c
+                                <term><varname>$TERM</varname></term>
794d16c
+
794d16c
+                                <listitem><para>Terminal type, set
794d16c
+                                only for units connected to a terminal
794d16c
+                                (<varname>StandardInput=tty</varname>,
794d16c
+                                <varname>StandardOutput=tty</varname>,
794d16c
+                                or
794d16c
+                                <varname>StandardError=tty</varname>).
794d16c
+                                See
794d16c
+                                <citerefentry><refentrytitle>termcap</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
794d16c
+                                </para></listitem>
794d16c
+                        </varlistentry>
794d16c
                 </variablelist>
794d16c
 
794d16c
                 <para>Additional variables may be configured by the
794d16c
diff --git a/src/core/execute.c b/src/core/execute.c
794d16c
index a53ef48..3979f35 100644
794d16c
--- a/src/core/execute.c
794d16c
+++ b/src/core/execute.c
794d16c
@@ -1094,7 +1094,7 @@ int exec_spawn(ExecCommand *command,
794d16c
         if (pid == 0) {
794d16c
                 int i, err;
794d16c
                 sigset_t ss;
794d16c
-                const char *username = NULL, *home = NULL;
794d16c
+                const char *username = NULL, *home = NULL, *shell = NULL;
794d16c
                 uid_t uid = (uid_t) -1;
794d16c
                 gid_t gid = (gid_t) -1;
794d16c
                 _cleanup_strv_free_ char **our_env = NULL, **pam_env = NULL,
794d16c
@@ -1277,7 +1277,7 @@ int exec_spawn(ExecCommand *command,
794d16c
 
794d16c
                 if (context->user) {
794d16c
                         username = context->user;
794d16c
-                        err = get_user_creds(&username, &uid, &gid, &home, NULL);
794d16c
+                        err = get_user_creds(&username, &uid, &gid, &home, &shell);
794d16c
                         if (err < 0) {
794d16c
                                 r = EXIT_USER;
794d16c
                                 goto fail_child;
794d16c
@@ -1462,46 +1462,28 @@ int exec_spawn(ExecCommand *command,
794d16c
                         }
794d16c
                 }
794d16c
 
794d16c
-                our_env = new0(char*, 7);
794d16c
-                if (!our_env) {
794d16c
+                our_env = new(char*, 8);
794d16c
+                if (!our_env ||
794d16c
+                    (n_fds > 0 && (
794d16c
+                            asprintf(our_env + n_env++, "LISTEN_PID=%lu", (unsigned long) getpid()) < 0 ||
794d16c
+                            asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0)) ||
794d16c
+                    (home && asprintf(our_env + n_env++, "HOME=%s", home) < 0) ||
794d16c
+                    (username && (
794d16c
+                            asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 ||
794d16c
+                            asprintf(our_env + n_env++, "USER=%s", username) < 0)) ||
794d16c
+                    (shell && asprintf(our_env + n_env++, "SHELL=%s", shell) < 0) ||
794d16c
+                    ((is_terminal_input(context->std_input) ||
794d16c
+                      context->std_output == EXEC_OUTPUT_TTY ||
794d16c
+                      context->std_error == EXEC_OUTPUT_TTY) && (
794d16c
+                              !(our_env[n_env++] = strdup(default_term_for_tty(tty_path(context))))))) {
794d16c
+
794d16c
                         err = -ENOMEM;
794d16c
                         r = EXIT_MEMORY;
794d16c
                         goto fail_child;
794d16c
                 }
794d16c
 
794d16c
-                if (n_fds > 0)
794d16c
-                        if (asprintf(our_env + n_env++, "LISTEN_PID=%lu", (unsigned long) getpid()) < 0 ||
794d16c
-                            asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) {
794d16c
-                                err = -ENOMEM;
794d16c
-                                r = EXIT_MEMORY;
794d16c
-                                goto fail_child;
794d16c
-                        }
794d16c
-
794d16c
-                if (home)
794d16c
-                        if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) {
794d16c
-                                err = -ENOMEM;
794d16c
-                                r = EXIT_MEMORY;
794d16c
-                                goto fail_child;
794d16c
-                        }
794d16c
-
794d16c
-                if (username)
794d16c
-                        if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 ||
794d16c
-                            asprintf(our_env + n_env++, "USER=%s", username) < 0) {
794d16c
-                                err = -ENOMEM;
794d16c
-                                r = EXIT_MEMORY;
794d16c
-                                goto fail_child;
794d16c
-                        }
794d16c
-
794d16c
-                if (is_terminal_input(context->std_input) ||
794d16c
-                    context->std_output == EXEC_OUTPUT_TTY ||
794d16c
-                    context->std_error == EXEC_OUTPUT_TTY)
794d16c
-                        if (!(our_env[n_env++] = strdup(default_term_for_tty(tty_path(context))))) {
794d16c
-                                err = -ENOMEM;
794d16c
-                                r = EXIT_MEMORY;
794d16c
-                                goto fail_child;
794d16c
-                        }
794d16c
-
794d16c
-                assert(n_env <= 7);
794d16c
+                our_env[n_env++] = NULL;
794d16c
+                assert(n_env <= 8);
794d16c
 
794d16c
                 final_env = strv_env_merge(5,
794d16c
                                            environment,
794d16c
diff --git a/units/user@.service.in b/units/user@.service.in
794d16c
index 3718a57..3f8b59d 100644
794d16c
--- a/units/user@.service.in
794d16c
+++ b/units/user@.service.in
794d16c
@@ -13,7 +13,6 @@ After=systemd-user-sessions.service
794d16c
 User=%I
794d16c
 PAMName=systemd-user
794d16c
 Type=notify
794d16c
-Environment=SHELL=%s
794d16c
 ExecStart=-@rootlibexecdir@/systemd --user
794d16c
 Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%I/dbus/user_bus_socket
794d16c
 Slice=user-%i.slice