[systemd-devel] [PATCH] Set WorkinDirectory to $HOME for user session units.

Auke Kok auke-jan.h.kok at intel.com
Sat Jul 14 00:23:39 PDT 2012


Change the default WorkingDirectory to $HOME in case running as
USER_MANAGER. We assume that HOME is set by default for all
user managers, so looking up pw_dir doesn't make much sense.

Mount, socket and service units are affected, as these all
can execute programs and thus the working directory may
affect how these programs function.
---
 src/core/mount.c   | 2 ++
 src/core/service.c | 2 ++
 src/core/socket.c  | 2 ++
 3 files changed, 6 insertions(+)

diff --git a/src/core/mount.c b/src/core/mount.c
index fab922e..1c1be99 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -67,6 +67,8 @@ static void mount_init(Unit *u) {
         m->directory_mode = 0755;
 
         exec_context_init(&m->exec_context);
+        if (UNIT(m)->manager->running_as == MANAGER_USER)
+                m->exec_context.working_directory = strdup(getenv("HOME"));
 
         if (unit_has_name(u, "-.mount")) {
                 /* Don't allow start/stop for root directory */
diff --git a/src/core/service.c b/src/core/service.c
index e57b0e9..074fe2b 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -153,6 +153,8 @@ static void service_init(Unit *u) {
         for (i = 0; i < RLIMIT_NLIMITS; i++)
                 if (UNIT(s)->manager->rlimit[i])
                         s->exec_context.rlimit[i] = newdup(struct rlimit, UNIT(s)->manager->rlimit[i], 1);
+        if (UNIT(s)->manager->running_as == MANAGER_USER)
+                s->exec_context.working_directory = strdup(getenv("HOME"));
 
         RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
 
diff --git a/src/core/socket.c b/src/core/socket.c
index 3613e84..dfa4058 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -81,6 +81,8 @@ static void socket_init(Unit *u) {
         s->mark = -1;
 
         exec_context_init(&s->exec_context);
+        if (UNIT(s)->manager->running_as == MANAGER_USER)
+                s->exec_context.working_directory = strdup(getenv("HOME"));
         s->exec_context.std_output = u->manager->default_std_output;
         s->exec_context.std_error = u->manager->default_std_error;
 
-- 
1.7.11.2



More information about the systemd-devel mailing list