summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2017-06-14 16:22:06 -0400
committerLuke Shumaker <lukeshu@lukeshu.com>2017-06-16 17:06:57 -0400
commitc4f1e6445be169b5f4d0016de11016bd0c17a39c (patch)
tree3b61d098b8b52da5a7166ec28ad1fd159f07ffbf
parent40d39b0ff8e3e5c9f148bcd820a6a570001a7182 (diff)
nspawn: Track the inner child and outer child PIDs separately
-rw-r--r--src/nspawn/nspawn.c61
1 files changed, 33 insertions, 28 deletions
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index caf8ba3868..2c3fc4659a 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -3602,7 +3602,8 @@ static int run(int master,
FDSet *fds,
char veth_name[IFNAMSIZ], bool *veth_created,
union in_addr_union *exposed,
- pid_t *pid, int *ret) {
+ pid_t *helper_pid, pid_t *main_pid,
+ int *ret) {
static const struct sigaction sa = {
.sa_handler = nop_signal_handler,
@@ -3678,13 +3679,13 @@ static int run(int master,
if (r < 0)
return log_error_errno(errno, "Failed to install SIGCHLD handler: %m");
- *pid = raw_clone(SIGCHLD|CLONE_NEWNS);
- if (*pid < 0)
+ *helper_pid = raw_clone(SIGCHLD|CLONE_NEWNS);
+ if (*helper_pid < 0)
return log_error_errno(errno, "clone() failed%s: %m",
errno == EINVAL ?
", do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in)" : "");
- if (*pid == 0) {
+ if (*helper_pid == 0) {
/* The outer child only has a file system namespace. */
barrier_set_role(&barrier, BARRIER_CHILD);
@@ -3765,15 +3766,16 @@ static int run(int master,
}
/* Wait for the outer child. */
- r = wait_for_terminate_and_warn("namespace helper", *pid, NULL);
+ r = wait_for_terminate_and_warn("namespace helper", *helper_pid, NULL);
+ *helper_pid = 0;
if (r != 0)
return r < 0 ? r : -EIO;
/* And now retrieve the PID of the inner child. */
- l = recv(pid_socket_pair[0], pid, sizeof *pid, 0);
+ l = recv(pid_socket_pair[0], main_pid, sizeof *main_pid, 0);
if (l < 0)
return log_error_errno(errno, "Failed to read inner child PID: %m");
- if (l != sizeof *pid) {
+ if (l != sizeof *main_pid) {
log_error("Short read while reading inner child PID.");
return -EIO;
}
@@ -3793,7 +3795,7 @@ static int run(int master,
return log_error_errno(notify_socket,
"Failed to receive notification socket from the outer child: %m");
- log_debug("Init process invoked as PID "PID_FMT, *pid);
+ log_debug("Init process invoked as PID "PID_FMT, *main_pid);
if (arg_userns_mode != USER_NAMESPACE_NO) {
if (!barrier_place_and_sync(&barrier)) { /* #1 */
@@ -3801,7 +3803,7 @@ static int run(int master,
return -ESRCH;
}
- r = setup_uid_map(*pid);
+ r = setup_uid_map(*main_pid);
if (r < 0)
return r;
@@ -3810,12 +3812,12 @@ static int run(int master,
if (arg_private_network) {
- r = move_network_interfaces(*pid, arg_network_interfaces);
+ r = move_network_interfaces(*main_pid, arg_network_interfaces);
if (r < 0)
return r;
if (arg_network_veth) {
- r = setup_veth(arg_machine, *pid, veth_name,
+ r = setup_veth(arg_machine, *main_pid, veth_name,
arg_network_bridge || arg_network_zone);
if (r < 0)
return r;
@@ -3839,7 +3841,7 @@ static int run(int master,
}
}
- r = setup_veth_extra(arg_machine, *pid, arg_network_veth_extra);
+ r = setup_veth_extra(arg_machine, *main_pid, arg_network_veth_extra);
if (r < 0)
return r;
@@ -3849,11 +3851,11 @@ static int run(int master,
remove them on its own, since they cannot be referenced by anything yet. */
*veth_created = true;
- r = setup_macvlan(arg_machine, *pid, arg_network_macvlan);
+ r = setup_macvlan(arg_machine, *main_pid, arg_network_macvlan);
if (r < 0)
return r;
- r = setup_ipvlan(arg_machine, *pid, arg_network_ipvlan);
+ r = setup_ipvlan(arg_machine, *main_pid, arg_network_ipvlan);
if (r < 0)
return r;
}
@@ -3861,7 +3863,7 @@ static int run(int master,
if (arg_register) {
r = register_machine(
arg_machine,
- *pid,
+ *main_pid,
arg_directory,
arg_uuid,
ifi,
@@ -3875,7 +3877,7 @@ static int run(int master,
return r;
}
- r = cgroup_setup(*pid, outer_cgver, arg_unified_cgroup_hierarchy, arg_uid_shift);
+ r = cgroup_setup(*main_pid, outer_cgver, arg_unified_cgroup_hierarchy, arg_uid_shift);
if (r < 0)
return r;
@@ -3898,7 +3900,7 @@ static int run(int master,
if (r < 0)
return log_error_errno(r, "Failed to get default event source: %m");
- r = setup_sd_notify_parent(event, notify_socket, PID_TO_PTR(*pid));
+ r = setup_sd_notify_parent(event, notify_socket, PID_TO_PTR(*main_pid));
if (r < 0)
return r;
@@ -3914,14 +3916,14 @@ static int run(int master,
sd_notifyf(false,
"STATUS=Container running.\n"
- "X_NSPAWN_LEADER_PID=" PID_FMT, *pid);
+ "X_NSPAWN_LEADER_PID=" PID_FMT, *main_pid);
if (!arg_notify_ready)
sd_notify(false, "READY=1\n");
if (arg_kill_signal > 0) {
/* Try to kill the init system on SIGINT or SIGTERM */
- sd_event_add_signal(event, NULL, SIGINT, on_orderly_shutdown, PID_TO_PTR(*pid));
- sd_event_add_signal(event, NULL, SIGTERM, on_orderly_shutdown, PID_TO_PTR(*pid));
+ sd_event_add_signal(event, NULL, SIGINT, on_orderly_shutdown, PID_TO_PTR(*main_pid));
+ sd_event_add_signal(event, NULL, SIGTERM, on_orderly_shutdown, PID_TO_PTR(*main_pid));
} else {
/* Immediately exit */
sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
@@ -3960,13 +3962,13 @@ static int run(int master,
/* Kill if it is not dead yet anyway */
if (arg_register && !arg_keep_unit)
- terminate_machine(*pid);
+ terminate_machine(*main_pid);
/* Normally redundant, but better safe than sorry */
- kill(*pid, SIGKILL);
+ kill(*main_pid, SIGKILL);
- r = wait_for_container(*pid, &container_status);
- *pid = 0;
+ r = wait_for_container(*main_pid, &container_status);
+ *main_pid = 0;
if (r < 0)
/* We failed to wait for the container, or the container exited abnormally. */
@@ -4012,7 +4014,7 @@ int main(int argc, char *argv[]) {
int r, n_fd_passed, loop_nr = -1, ret = EXIT_SUCCESS;
char veth_name[IFNAMSIZ] = "";
bool secondary = false, remove_subvol = false;
- pid_t pid = 0;
+ pid_t helper_pid = 0, main_pid = 0;
union in_addr_union exposed = {};
_cleanup_release_lock_file_ LockFile tree_global_lock = LOCK_FILE_INIT, tree_local_lock = LOCK_FILE_INIT;
bool interactive, veth_created = false;
@@ -4260,7 +4262,8 @@ int main(int argc, char *argv[]) {
fds,
veth_name, &veth_created,
&exposed,
- &pid, &ret);
+ &helper_pid, &main_pid,
+ &ret);
if (r <= 0)
break;
}
@@ -4270,8 +4273,10 @@ finish:
"STOPPING=1\n"
"STATUS=Terminating...");
- if (pid > 0)
- kill(pid, SIGKILL);
+ if (helper_pid > 0)
+ kill(helper_pid, SIGKILL);
+ if (main_pid > 0)
+ kill(main_pid, SIGKILL);
/* Try to flush whatever is still queued in the pty */
if (master >= 0)