mirror of
https://github.com/clearlinux/hyperstart.git
synced 2026-08-18 20:05:48 +00:00
send kill signal if term is ignored by container process
Fix the bug that stoppod is blocked. Signed-off-by: Gao feng <feng@hyper.sh>
This commit is contained in:
+8
-1
@@ -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;
|
||||
}
|
||||
|
||||
+87
-1
@@ -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;
|
||||
|
||||
+1
-1
@@ -406,7 +406,7 @@ void hyper_shutdown(struct hyper_pod *pod)
|
||||
{
|
||||
hyper_send_finish(pod);
|
||||
|
||||
hyper_kill_all();
|
||||
//hyper_kill_all();
|
||||
|
||||
hyper_unmount_all();
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user