Browse Source

fix: subprocess create new session

dev
Dnomd343 2 years ago
parent
commit
70cc89ea77
  1. 11
      src/utils/process.c

11
src/utils/process.c

@ -55,10 +55,17 @@ void process_exec(process *proc) {
log_perror("%s fork error -> ", proc->name); log_perror("%s fork error -> ", proc->name);
server_exit(EXIT_FORK_ERROR); server_exit(EXIT_FORK_ERROR);
} else if (pid == 0) { // child process } else if (pid == 0) { // child process
log_debug("Subprocess fork success -> PID = %d", getpid());
if (chdir(proc->cwd)) { // change working directory if (chdir(proc->cwd)) { // change working directory
log_perror("%s with invalid cwd `%s` -> ", proc->name, proc->cwd); log_perror("%s with invalid cwd `%s` -> ", proc->name, proc->cwd);
exit(EXIT_EXEC_ERROR); exit(EXIT_EXEC_ERROR);
} }
pid_t sid = setsid(); // create new session -> detach current terminal
if (sid == -1) { // session create failed
log_warn("Subprocess failed to create new session");
} else {
log_debug("Subprocess at new session -> SID = %d", sid);
}
prctl(PR_SET_PDEATHSIG, SIGKILL); // child process die with father process prctl(PR_SET_PDEATHSIG, SIGKILL); // child process die with father process
if (execvpe(*(proc->cmd), proc->cmd, proc->env) < 0) { if (execvpe(*(proc->cmd), proc->cmd, proc->env) < 0) {
log_perror("%s exec error -> ", proc->name); log_perror("%s exec error -> ", proc->name);
@ -152,7 +159,7 @@ void get_sub_exit() { // catch child process exit
return; return;
} }
int status; int status;
log_debug("Handle SIGCHLD start"); log_debug("Handle SIGCHLD begin");
for (process **proc = process_list; *proc != NULL; ++proc) { for (process **proc = process_list; *proc != NULL; ++proc) {
if ((*proc)->pid == 0) { if ((*proc)->pid == 0) {
continue; // skip not running process continue; // skip not running process
@ -177,7 +184,7 @@ void get_sub_exit() { // catch child process exit
server_exit(EXIT_WAIT_ERROR); server_exit(EXIT_WAIT_ERROR);
} else if (wait_ret) { // process exit } else if (wait_ret) { // process exit
char *exit_msg = get_exit_msg(status); char *exit_msg = get_exit_msg(status);
log_debug("Sub-process (PID = %d) -> %s", wait_ret, exit_msg); log_debug("Subprocess (PID = %d) -> %s", wait_ret, exit_msg);
free(exit_msg); free(exit_msg);
} }
log_debug("Handle SIGCHLD complete"); log_debug("Handle SIGCHLD complete");

Loading…
Cancel
Save