esh: fixup pgrp juggling

This commit is contained in:
K. Lange 2018-10-30 08:44:09 +09:00
parent 422a264b49
commit 821eb55496
1 changed files with 26 additions and 16 deletions

View File

@ -784,7 +784,6 @@ int wait_for_child(int pgid, char * name) {
int outpid; int outpid;
int ret_code = 0; int ret_code = 0;
set_pgrp(pgid);
do { do {
outpid = waitpid(waitee, &ret_code, 0); outpid = waitpid(waitee, &ret_code, 0);
if (WIFSTOPPED(ret_code)) { if (WIFSTOPPED(ret_code)) {
@ -1247,8 +1246,9 @@ _nope:
pipe(last_output); pipe(last_output);
child_pid = fork(); child_pid = fork();
if (!child_pid) { if (!child_pid) {
is_subshell = 1;
set_pgid(0); set_pgid(0);
set_pgrp(getpid());
is_subshell = 1;
dup2(last_output[1], STDOUT_FILENO); dup2(last_output[1], STDOUT_FILENO);
close(last_output[0]); close(last_output[0]);
run_cmd(arg_starts[0]); run_cmd(arg_starts[0]);
@ -1302,8 +1302,9 @@ _nope:
} else { } else {
child_pid = fork(); child_pid = fork();
if (!child_pid) { if (!child_pid) {
is_subshell = 1;
set_pgid(0); set_pgid(0);
set_pgrp(getpid());
is_subshell = 1;
if (output_files[cmdi]) { if (output_files[cmdi]) {
int fd = open(output_files[cmdi], file_args[cmdi], 0666); int fd = open(output_files[cmdi], file_args[cmdi], 0666);
if (fd < 0) { if (fd < 0) {
@ -1518,6 +1519,10 @@ int main(int argc, char ** argv) {
shell_interactive = 1; shell_interactive = 1;
signal(SIGTSTP, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
source_eshrc(); source_eshrc();
add_path(); add_path();
sort_commands(); sort_commands();
@ -1670,11 +1675,11 @@ uint32_t shell_cmd_if(int argc, char * argv[]) {
pid_t child_pid = fork(); pid_t child_pid = fork();
if (!child_pid) { if (!child_pid) {
is_subshell = 1;
set_pgid(0); set_pgid(0);
set_pgrp(getpid());
is_subshell = 1;
run_cmd(if_args); run_cmd(if_args);
} }
set_pgrp(child_pid);
int pid, ret_code = 0; int pid, ret_code = 0;
do { do {
@ -1694,11 +1699,11 @@ uint32_t shell_cmd_if(int argc, char * argv[]) {
} else { } else {
child_pid = fork(); child_pid = fork();
if (!child_pid) { if (!child_pid) {
is_subshell = 1;
set_pgid(0); set_pgid(0);
set_pgrp(getpid());
is_subshell = 1;
run_cmd(then_args); run_cmd(then_args);
} }
set_pgrp(child_pid);
do { do {
pid = waitpid(child_pid, &ret_code, 0); pid = waitpid(child_pid, &ret_code, 0);
} while (pid != -1 || (pid == -1 && errno != ECHILD)); } while (pid != -1 || (pid == -1 && errno != ECHILD));
@ -1717,11 +1722,11 @@ uint32_t shell_cmd_if(int argc, char * argv[]) {
} else { } else {
child_pid = fork(); child_pid = fork();
if (!child_pid) { if (!child_pid) {
is_subshell = 1;
set_pgid(0); set_pgid(0);
set_pgrp(getpid());
is_subshell = 1;
run_cmd(else_args); run_cmd(else_args);
} }
set_pgrp(child_pid);
do { do {
pid = waitpid(child_pid, &ret_code, 0); pid = waitpid(child_pid, &ret_code, 0);
} while (pid != -1 || (pid == -1 && errno != ECHILD)); } while (pid != -1 || (pid == -1 && errno != ECHILD));
@ -1756,11 +1761,11 @@ uint32_t shell_cmd_while(int argc, char * argv[]) {
do { do {
pid_t child_pid = fork(); pid_t child_pid = fork();
if (!child_pid) { if (!child_pid) {
is_subshell = 1;
set_pgid(0); set_pgid(0);
set_pgrp(getpid());
is_subshell = 1;
run_cmd(while_args); run_cmd(while_args);
} }
set_pgrp(child_pid);
int pid, ret_code = 0; int pid, ret_code = 0;
do { do {
@ -1771,8 +1776,9 @@ uint32_t shell_cmd_while(int argc, char * argv[]) {
if (WEXITSTATUS(ret_code) == 0) { if (WEXITSTATUS(ret_code) == 0) {
child_pid = fork(); child_pid = fork();
if (!child_pid) { if (!child_pid) {
is_subshell = 1;
set_pgid(0); set_pgid(0);
set_pgrp(getpid());
is_subshell = 1;
run_cmd(do_args); run_cmd(do_args);
} }
do { do {
@ -1798,8 +1804,9 @@ uint32_t shell_cmd_export_cmd(int argc, char * argv[]) {
pipe(pipe_fds); pipe(pipe_fds);
pid_t child_pid = fork(); pid_t child_pid = fork();
if (!child_pid) { if (!child_pid) {
is_subshell = 1;
set_pgid(0); set_pgid(0);
set_pgrp(getpid());
is_subshell = 1;
dup2(pipe_fds[1], STDOUT_FILENO); dup2(pipe_fds[1], STDOUT_FILENO);
close(pipe_fds[0]); close(pipe_fds[0]);
run_cmd(&argv[2]); run_cmd(&argv[2]);
@ -1807,7 +1814,6 @@ uint32_t shell_cmd_export_cmd(int argc, char * argv[]) {
close(pipe_fds[1]); close(pipe_fds[1]);
set_pgrp(child_pid);
char buf[1024]; char buf[1024];
size_t accum = 0; size_t accum = 0;
@ -1822,6 +1828,8 @@ uint32_t shell_cmd_export_cmd(int argc, char * argv[]) {
accum += r; accum += r;
} while (accum < 1023); } while (accum < 1023);
waitpid(child_pid, NULL, 0);
reset_pgrp(); reset_pgrp();
buf[accum] = '\0'; buf[accum] = '\0';
@ -1883,11 +1891,11 @@ uint32_t shell_cmd_not(int argc, char * argv[]) {
int ret_code = 0; int ret_code = 0;
pid_t child_pid = fork(); pid_t child_pid = fork();
if (!child_pid) { if (!child_pid) {
is_subshell = 1;
set_pgid(0); set_pgid(0);
set_pgrp(getpid());
is_subshell = 1;
run_cmd(&argv[1]); run_cmd(&argv[1]);
} }
set_pgrp(child_pid);
do { do {
pid = waitpid(child_pid, &ret_code, 0); pid = waitpid(child_pid, &ret_code, 0);
} while (pid != -1 || (pid == -1 && errno != ECHILD)); } while (pid != -1 || (pid == -1 && errno != ECHILD));
@ -1978,6 +1986,8 @@ uint32_t shell_cmd_fg(int argc, char * argv[]) {
return 1; return 1;
} }
set_pgrp(pid);
if (kill(-pid, SIGCONT) < 0) { if (kill(-pid, SIGCONT) < 0) {
fprintf(stderr, "no current job / bad pid\n"); fprintf(stderr, "no current job / bad pid\n");
hashmap_remove(job_hash, (void*)pid); hashmap_remove(job_hash, (void*)pid);