From 6680827fafa0e80714ea01ab9e9450ce9f4f288e Mon Sep 17 00:00:00 2001 From: Gao feng Date: Thu, 13 Aug 2015 16:52:36 +0800 Subject: [PATCH] send kill signal if term is ignored by container process Fix the bug that stoppod is blocked. Signed-off-by: Gao feng --- src/container.c | 9 ++++- src/init.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++- src/util.c | 2 +- src/util.h | 1 - 4 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/container.c b/src/container.c index bc9cb77..d46c422 100644 --- a/src/container.c +++ b/src/container.c @@ -370,12 +370,12 @@ fail: int hyper_start_container(struct hyper_container *container) { int stacksize = getpagesize() * 4; - void *stack = malloc(stacksize); struct hyper_container_arg arg = { .c = container, }; int flags = CLONE_NEWNS | SIGCHLD; uint32_t type; + void *stack; int pid; if (container->image == NULL || container->exec.argv == NULL) { @@ -389,6 +389,12 @@ int hyper_start_container(struct hyper_container *container) goto fail; } + stack = malloc(stacksize); + if (stack == NULL) { + perror("fail to allocate stack for container init"); + return -1; + } + pid = clone(hyper_container_init, stack + stacksize, flags, &arg); free(stack); if (pid < 0) { @@ -412,6 +418,7 @@ int hyper_start_container(struct hyper_container *container) fail: fprintf(stdout, "container %s init exit code %d\n", container->id, -1); + container->exec.code = -1; return -1; } diff --git a/src/init.c b/src/init.c index aca618c..d1f5175 100644 --- a/src/init.c +++ b/src/init.c @@ -631,12 +631,98 @@ static void hyper_print_uptime(void) close(fd); } +static void hyper_kill_process(int pid) +{ + char path[64]; + char *line = NULL, *ignore = "SigIgn:"; + size_t len = 0; + ssize_t read; + FILE *file; + char *sub; + + sprintf(path, "/proc/%u/status", pid); + + fprintf(stdout, "fopen %s\n", path); + file = fopen(path, "r"); + if (file == NULL) { + perror("can not open process proc status file"); + return; + } + + while ((read = getline(&line, &len, file)) != -1) { + long mask; + + if (strstr(line, ignore) == NULL) + continue; + + sub = line + strlen(ignore); + fprintf(stdout, "find sigign %s", sub); + + mask = atol(sub); + fprintf(stdout, "mask is %ld\n", mask); + + if ((mask >> (SIGTERM - 1)) & 0x1) { + fprintf(stdout, "signal term is ignored, kill it\n"); + kill(pid, SIGKILL); + } + + break; + } + + fclose(file); + free(line); +} + +static void hyper_term_all(struct hyper_pod *pod) +{ + int npids = 0; + int index = 0; + int pid; + DIR *dp; + struct dirent *de; + pid_t *pids = NULL; + + dp = opendir("/proc"); + if (dp == NULL) + return; + + while ((de = readdir(dp)) && de != NULL) { + if (!isdigit(de->d_name[0])) + continue; + pid = atoi(de->d_name); + if (pid == 1) + continue; + if (index <= npids) { + pids = realloc(pids, npids + 16384); + if (pids == NULL) + return; + npids += 16384; + } + + pids[index++] = pid; + } + + fprintf(stdout, "Sending SIGTERM\n"); + + for (--index; index >= 0; --index) { + fprintf(stdout, "kill process %d\n", pids[index]); + kill(pids[index], SIGTERM); + } + + free(pids); + closedir(dp); + + for (index = 0; index < pod->c_num; index++) { + hyper_kill_process(pod->c[index].exec.pid); + } +} + static void hyper_cleanup_pod(struct hyper_pod *pod) { close(pod->sig.fd); hyper_reset_event(&pod->sig); - hyper_kill_all(); + hyper_term_all(pod); hyper_handle_exit(pod, pod->ctl.fd, 1, 0); pod->init_pid = 0; diff --git a/src/util.c b/src/util.c index 99eb7bf..d1212b6 100644 --- a/src/util.c +++ b/src/util.c @@ -406,7 +406,7 @@ void hyper_shutdown(struct hyper_pod *pod) { hyper_send_finish(pod); - hyper_kill_all(); + //hyper_kill_all(); hyper_unmount_all(); diff --git a/src/util.h b/src/util.h index 5376b4c..807a6eb 100644 --- a/src/util.h +++ b/src/util.h @@ -23,7 +23,6 @@ int hyper_setfd_block(int fd); int hyper_setfd_nonblock(int fd); void hyper_shutdown(struct hyper_pod *pod); int hyper_send_finish(struct hyper_pod *pod); -void hyper_kill_all(void); void hyper_unmount_all(void); int hyper_insmod(char *module); #endif