From 30b3bf772ba9fb258ef5a4c7d125eca0584ef4ff Mon Sep 17 00:00:00 2001 From: Gao feng Date: Mon, 16 May 2016 23:41:06 +0800 Subject: [PATCH] kill processes in container if container init exited consist with docker behavior. Signed-off-by: Gao feng --- src/exec.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/exec.c b/src/exec.c index 3820a7f..743f9fa 100644 --- a/src/exec.c +++ b/src/exec.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -726,6 +727,58 @@ struct hyper_exec *hyper_find_exec_by_seq(struct hyper_pod *pod, uint64_t seq) return NULL; } +static int hyper_kill_container_processes(struct hyper_container *c) { + struct stat st; + int pid, loop = 1; + DIR *dp; + struct dirent *de; + + if (fstat(c->ns, &st) < 0) { + perror("fail to stat mnt ns"); + return -1; + } + + fprintf(stdout, "container init process %d\n", c->exec.pid); + while (loop) { + loop = 0; + + dp = opendir("/proc"); + if (dp == NULL) { + perror("open /proc failed"); + return -1; + } + + while ((de = readdir(dp)) && de != NULL) { + char mntns[512]; + struct stat st1; + + if (!isdigit(de->d_name[0])) + continue; + pid = atoi(de->d_name); + if (pid == 1 || pid == c->exec.pid) + continue; + + sprintf(mntns, "/proc/%d/ns/mnt", pid); + + if (stat(mntns, &st1) < 0) { + fprintf(stdout, "fail to stat mnt ns of process %d: %s\n", + pid, strerror(errno)); + continue; + } + + if (st.st_ino != st1.st_ino) + continue; + + fprintf(stdout, "kill process of container %d\n", pid); + kill(pid, SIGKILL); + loop = 1; + } + + closedir(dp); + } + return 0; +} + int hyper_handle_exec_exit(struct hyper_pod *pod, int pid, uint8_t code) { struct hyper_exec *exec; @@ -754,6 +807,9 @@ int hyper_handle_exec_exit(struct hyper_pod *pod, int pid, uint8_t code) hyper_release_exec(exec, pod); + if (exec->init) + hyper_kill_container_processes(container_of(exec, struct hyper_container, exec)); + return 0; }