diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml index eff75d033a..291178679d 100644 --- a/man/systemd.exec.xml +++ b/man/systemd.exec.xml @@ -89,8 +89,12 @@ Takes an absolute directory path. Sets the working - directory for executed - processes. + directory for executed processes. If + not set defaults to the root directory + when systemd is running as a system + instance and the respective user's + home directory if run as + user. diff --git a/src/core/mount.c b/src/core/mount.c index 15d5f21530..a88b255875 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -631,6 +631,10 @@ static int mount_load(Unit *u) { r = mount_add_extras(m); if (r < 0) return r; + + r = unit_patch_working_directory(UNIT(m), &m->exec_context); + if (r < 0) + return r; } return mount_verify(m); diff --git a/src/core/service.c b/src/core/service.c index 0a6658809e..7a7e25ffcd 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -1282,6 +1282,10 @@ static int service_load(Unit *u) { if (UNIT(s)->default_dependencies) if ((r = service_add_default_dependencies(s)) < 0) return r; + + r = unit_patch_working_directory(UNIT(s), &s->exec_context); + if (r < 0) + return r; } return service_verify(s); diff --git a/src/core/socket.c b/src/core/socket.c index 3613e8420e..8153a8e762 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -372,6 +372,10 @@ static int socket_load(Unit *u) { if (UNIT(s)->default_dependencies) if ((r = socket_add_default_dependencies(s)) < 0) return r; + + r = unit_patch_working_directory(UNIT(s), &s->exec_context); + if (r < 0) + return r; } return socket_verify(s); diff --git a/src/core/swap.c b/src/core/swap.c index aeb8bdab36..ed61ba3c81 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -294,6 +294,10 @@ static int swap_load(Unit *u) { if (UNIT(s)->default_dependencies) if ((r = swap_add_default_dependencies(s)) < 0) return r; + + r = unit_patch_working_directory(UNIT(s), &s->exec_context); + if (r < 0) + return r; } return swap_verify(s); diff --git a/src/core/unit.c b/src/core/unit.c index 673af13c3e..7994f7038e 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -2817,6 +2817,19 @@ int unit_add_mount_links(Unit *u) { return 0; } +int unit_patch_working_directory(Unit *u, ExecContext *c) { + assert(u); + assert(c); + + if (u->manager->running_as != MANAGER_USER) + return 0; + + if (c->working_directory) + return 0; + + return get_home_dir(&c->working_directory); +} + static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = { [UNIT_ACTIVE] = "active", [UNIT_RELOADING] = "reloading", diff --git a/src/core/unit.h b/src/core/unit.h index 45849a4b12..2483e4ea9d 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -537,6 +537,8 @@ void unit_ref_unset(UnitRef *ref); int unit_add_one_mount_link(Unit *u, Mount *m); int unit_add_mount_links(Unit *u); +int unit_patch_working_directory(Unit *u, ExecContext *c); + const char *unit_active_state_to_string(UnitActiveState i); UnitActiveState unit_active_state_from_string(const char *s);