summaryrefslogtreecommitdiff
path: root/src/core/execute.c
diff options
context:
space:
mode:
authorDjalal Harouni <tixxdz@opendz.org>2016-10-27 09:21:44 +0200
committerDjalal Harouni <tixxdz@opendz.org>2016-10-27 09:40:21 +0200
commite7f1e7c6e2f91f3cad5eadfcc6ab9673caedb838 (patch)
treecbda5c5798de094dc0147adab47b4c3cd5d3e710 /src/core/execute.c
parent93c6bb51b6a3c80577fd823d0108c1dfcf01a0ce (diff)
core: move apply working directory code into its own apply_working_directory()
Diffstat (limited to 'src/core/execute.c')
-rw-r--r--src/core/execute.c52
1 files changed, 31 insertions, 21 deletions
diff --git a/src/core/execute.c b/src/core/execute.c
index 2908ba8fa3..642add0360 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -2056,6 +2056,33 @@ static int apply_mount_namespace(Unit *u, const ExecContext *context,
return r;
}
+static int apply_working_directory(const ExecContext *context,
+ const ExecParameters *params,
+ const char *working_directory,
+ const bool needs_mount_ns) {
+
+ if (params->flags & EXEC_APPLY_CHROOT) {
+ if (!needs_mount_ns && context->root_directory)
+ if (chroot(context->root_directory) < 0)
+ return -errno;
+
+ if (chdir(working_directory) < 0 &&
+ !context->working_directory_missing_ok)
+ return -errno;
+
+ } else {
+ const char *d;
+
+ d = strjoina(strempty(context->root_directory), "/",
+ strempty(working_directory));
+ if (chdir(d) < 0 &&
+ !context->working_directory_missing_ok)
+ return -errno;
+ }
+
+ return 0;
+}
+
static void append_socket_pair(int *array, unsigned *n, int pair[2]) {
assert(array);
assert(n);
@@ -2542,27 +2569,10 @@ static int exec_child(
}
}
- if (params->flags & EXEC_APPLY_CHROOT) {
- if (!needs_mount_namespace && context->root_directory)
- if (chroot(context->root_directory) < 0) {
- *exit_status = EXIT_CHROOT;
- return -errno;
- }
-
- if (chdir(wd) < 0 &&
- !context->working_directory_missing_ok) {
- *exit_status = EXIT_CHDIR;
- return -errno;
- }
- } else {
- const char *d;
-
- d = strjoina(strempty(context->root_directory), "/", strempty(wd));
- if (chdir(d) < 0 &&
- !context->working_directory_missing_ok) {
- *exit_status = EXIT_CHDIR;
- return -errno;
- }
+ r = apply_working_directory(context, params, wd, needs_mount_namespace);
+ if (r < 0) {
+ *exit_status = EXIT_CHROOT;
+ return r;
}
#ifdef HAVE_SELINUX