From 479e1f4aa2b9f1c911a4d0dd18e222d241a978ea Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Wed, 9 Nov 2016 20:35:30 -0800
Subject: [PATCH 42/48] Make root's home directory configurable

OpenEmbedded has a configurable home directory for root. Allow
systemd to be built using its idea of what root's home directory
should be.

Upstream-Status: Denied
Upstream wants to have a unified hierarchy where everyone is
using the same root folder.
https://github.com/systemd/systemd/issues/541

Signed-off-by: Dan McGregor <dan.mcgregor@usask.ca>
Signed-off-by: Khem Raj <raj.khem@gmail.com>

---
 Makefile.am                | 2 ++
 configure.ac               | 7 +++++++
 src/basic/user-util.c      | 4 ++--
 src/nspawn/nspawn.c        | 4 ++--
 units/emergency.service.in | 4 ++--
 units/rescue.service.in    | 4 ++--
 6 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 1bcd932c2..c2b4a99d2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -226,6 +226,7 @@ AM_CPPFLAGS = \
 	-DLIBDIR=\"$(libdir)\" \
 	-DROOTLIBDIR=\"$(rootlibdir)\" \
 	-DROOTLIBEXECDIR=\"$(rootlibexecdir)\" \
+	-DROOTHOMEDIR=\"$(roothomedir)\" \
 	-I $(top_srcdir)/src \
 	-I $(top_builddir)/src/basic \
 	-I $(top_srcdir)/src/basic \
@@ -6356,6 +6357,7 @@ substitutions = \
        '|rootlibdir=$(rootlibdir)|' \
        '|rootlibexecdir=$(rootlibexecdir)|' \
        '|rootbindir=$(rootbindir)|' \
+       '|roothomedir=$(roothomedir)|' \
        '|bindir=$(bindir)|' \
        '|SYSTEMCTL=$(rootbindir)/systemctl|' \
        '|SYSTEMD_NOTIFY=$(rootbindir)/systemd-notify|' \
diff --git a/configure.ac b/configure.ac
index 0354ffe6a..b53ca1f1a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1641,6 +1641,11 @@ AC_ARG_WITH([rootlibdir],
         [with_rootlibdir=${libdir}])
 AX_NORMALIZE_PATH([with_rootlibdir])
 
+AC_ARG_WITH([roothomedir],
+        AS_HELP_STRING([--with-roothomedir=DIR], [Home directory for the root user]),
+        [],
+        [with_roothomedir=/root])
+
 AC_ARG_WITH([pamlibdir],
         AS_HELP_STRING([--with-pamlibdir=DIR], [directory for PAM modules]),
         [],
@@ -1733,6 +1738,7 @@ AC_SUBST([pamconfdir], [$with_pamconfdir])
 AC_SUBST([rpmmacrosdir], [$with_rpmmacrosdir])
 AC_SUBST([rootprefix], [$with_rootprefix])
 AC_SUBST([rootlibdir], [$with_rootlibdir])
+AC_SUBST([roothomedir], [$with_roothomedir])
 
 AC_CONFIG_FILES([
         Makefile
@@ -1829,6 +1835,7 @@ AC_MSG_RESULT([
         includedir:                        ${includedir}
         lib dir:                           ${libdir}
         rootlib dir:                       ${with_rootlibdir}
+        root home dir:                     ${with_roothomedir}
         SysV init scripts:                 ${SYSTEM_SYSVINIT_PATH}
         SysV rc?.d directories:            ${SYSTEM_SYSVRCND_PATH}
         build Python:                      ${PYTHON}
diff --git a/src/basic/user-util.c b/src/basic/user-util.c
index c619dad52..662682adf 100644
--- a/src/basic/user-util.c
+++ b/src/basic/user-util.c
@@ -129,7 +129,7 @@ int get_user_creds(
                         *gid = 0;
 
                 if (home)
-                        *home = "/root";
+                        *home = ROOTHOMEDIR;
 
                 if (shell)
                         *shell = "/bin/sh";
@@ -389,7 +389,7 @@ int get_home_dir(char **_h) {
         /* Hardcode home directory for root to avoid NSS */
         u = getuid();
         if (u == 0) {
-                h = strdup("/root");
+                h = strdup(ROOTHOMEDIR);
                 if (!h)
                         return -ENOMEM;
 
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 8a5fedd4b..7b01ec078 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -2291,7 +2291,7 @@ static int inner_child(
         if (envp[n_env])
                 n_env++;
 
-        if ((asprintf((char**)(envp + n_env++), "HOME=%s", home ? home: "/root") < 0) ||
+        if ((asprintf((char**)(envp + n_env++), "HOME=%s", home ? home: ROOTHOMEDIR) < 0) ||
             (asprintf((char**)(envp + n_env++), "USER=%s", arg_user ? arg_user : "root") < 0) ||
             (asprintf((char**)(envp + n_env++), "LOGNAME=%s", arg_user ? arg_user : "root") < 0))
                 return log_oom();
@@ -2373,7 +2373,7 @@ static int inner_child(
         } else {
                 if (!arg_chdir)
                         /* If we cannot change the directory, we'll end up in /, that is expected. */
-                        (void) chdir(home ?: "/root");
+                        (void) chdir(home ?: ROOTHOMEDIR);
 
                 execle("/bin/bash", "-bash", NULL, env_use);
                 execle("/bin/sh", "-sh", NULL, env_use);
diff --git a/units/emergency.service.in b/units/emergency.service.in
index e9eb238b9..32588e48a 100644
--- a/units/emergency.service.in
+++ b/units/emergency.service.in
@@ -15,8 +15,8 @@ Conflicts=syslog.socket
 Before=shutdown.target
 
 [Service]
-Environment=HOME=/root
-WorkingDirectory=-/root
+Environment=HOME=@roothomedir@
+WorkingDirectory=-@roothomedir@
 ExecStart=-@rootlibexecdir@/systemd-sulogin-shell emergency
 Type=idle
 StandardInput=tty-force
diff --git a/units/rescue.service.in b/units/rescue.service.in
index 4ab66f485..bd9898f2c 100644
--- a/units/rescue.service.in
+++ b/units/rescue.service.in
@@ -14,8 +14,8 @@ After=sysinit.target plymouth-start.service
 Before=shutdown.target
 
 [Service]
-Environment=HOME=/root
-WorkingDirectory=-/root
+Environment=HOME=@roothomedir@
+WorkingDirectory=-@roothomedir@
 ExecStart=-@rootlibexecdir@/systemd-sulogin-shell rescue
 Type=idle
 StandardInput=tty-force
-- 
2.13.2