diff --git a/42sh/Makefile b/42sh/Makefile index 1a249d59..b20dd88f 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -57,6 +57,7 @@ exec/process_redirect.c\ exec/process_reset.c\ exec/process_setexec.c\ exec/process_setgroup.c\ +exec/process_setsig.c\ exec/set_exitstatus.c\ glob/dir_glob.c\ glob/expand_brace.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index ea6efda7..777b4097 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -46,6 +46,7 @@ struct s_process pid_t pid; int fdin; int fdout; + int toclose; int status; t_flag attributes; }; @@ -83,8 +84,9 @@ int exec_command(t_btree **ast); int launch_process(t_process *p); int process_setexec(t_type type, t_process *p); -int process_setgroup(t_process *p); +int process_setgroup(t_process *p, pid_t pid); int process_redirect(t_process *p); +void process_setsig(void); void process_free(void *content, size_t content_size); void process_reset(void); diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 8e777ae1..cd454b9b 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -25,11 +25,13 @@ int exec_pipe(t_btree **ast) DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); p->fdout = fds[PIPE_WRITE]; start = IS_PIPESTART(p->attributes); + p->toclose = fds[PIPE_READ]; p->attributes &= ~PROCESS_PIPEEND; ft_exec(&(*ast)->left); p->attributes &= ~PROCESS_PIPESTART; + p->toclose = STDIN; close(fds[PIPE_WRITE]); p->fdout = STDOUT; p->fdin = fds[PIPE_READ]; diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 116af8ed..970b5d25 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -39,19 +39,17 @@ int launch_process(t_process *p) pid = fork(); if (pid == 0) { - process_setgroup(p); - signal(SIGINT, SIG_DFL); - signal(SIGQUIT, SIG_DFL); - signal(SIGTSTP, SIG_DFL); - signal(SIGTTIN, sigttin_handler); - signal(SIGTTOU, sigttou_handler); - signal(SIGCHLD, SIG_DFL); + process_setgroup(p, 0); + process_setsig(); process_redirect(p); (*p->execf)(p->path, p->av, data_singleton()->env); exit(42); } else if (pid > 0) + { p->pid = pid; + process_setgroup(p, pid); + } else if (pid == -1) perror("fork"); } diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index fed5209d..77830bb0 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -14,15 +14,15 @@ int process_redirect(t_process *p) { + if (p->toclose != STDIN) + close(p->toclose); if (p->fdin != STDIN) { - /* DG("redirect STDIN to %i", p->fdin); */ dup2(p->fdin, STDIN); close(p->fdin); } if (p->fdout != STDOUT) { - /* DG("redirect STDOUT to %i", p->fdout); */ dup2(p->fdout, STDOUT); close(p->fdout); } diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index 4cf1e1c4..afac9487 100644 --- a/42sh/src/exec/process_setgroup.c +++ b/42sh/src/exec/process_setgroup.c @@ -13,22 +13,22 @@ #include "job_control.h" #include "exec.h" -int process_setgroup(t_process *p) +int process_setgroup(t_process *p, pid_t pid) { - t_job *job; - int pid; + t_job *j; (void)p; - job = &data_singleton()->exec.job; - pid = getpid(); - if (job->pgid == 0) - job->pgid = pid; - if (setpgid(pid, job->pgid)) - DG("setpgid(%i, %i) failed", pid, job->pgid); - if (JOB_IS_FG(job->attributes)) + if (!SHELL_HAS_JOBC(data_singleton()->opts)) + return (0); + j = &data_singleton()->exec.job; + if (!j->pgid) + j->pgid = pid ? pid : getpid(); + DG("in pid %i gonna setpgid(%i, %i)", getpid(), pid, j->pgid); + setpgid(pid, j->pgid); + if (pid == 0 && JOB_IS_FG(j->attributes)) { signal(SIGTTOU, SIG_IGN); - tcsetpgrp(STDIN, job->pgid); + tcsetpgrp(STDIN, j->pgid); signal(SIGTTOU, SIG_DFL); } return (0); diff --git a/42sh/src/exec/process_setsig.c b/42sh/src/exec/process_setsig.c new file mode 100644 index 00000000..920424cc --- /dev/null +++ b/42sh/src/exec/process_setsig.c @@ -0,0 +1,11 @@ +#include "exec.h" + +void process_setsig(void) +{ + signal(SIGINT, SIG_DFL); + signal(SIGQUIT, SIG_DFL); + signal(SIGTSTP, SIG_DFL); + signal(SIGTTIN, SIG_DFL); + signal(SIGTTOU, SIG_DFL); + signal(SIGCHLD, SIG_DFL); +} diff --git a/42sh/src/job-control/put_job_in_background.c b/42sh/src/job-control/put_job_in_background.c index 8de13514..40820cc2 100644 --- a/42sh/src/job-control/put_job_in_background.c +++ b/42sh/src/job-control/put_job_in_background.c @@ -12,10 +12,10 @@ #include "job_control.h" -int put_job_in_background(t_job *job, int cont) +int put_job_in_background(t_job *j, int cont) { if (cont) - if (kill(-job->pgid, SIGCONT) < 0) - perror("kill (SIGCONT)"); + if (kill(-j->pgid, SIGCONT) < 0) + DG("kill (SIGCONT) malfunction"); return (0); } diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 1df2c754..65ee9de3 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -12,27 +12,24 @@ #include "job_control.h" -int put_job_in_foreground(t_job *job, int cont) +int put_job_in_foreground(t_job *j, int cont) { t_jobc *jobc; jobc = &data_singleton()->jobc; + tcsetpgrp (STDIN, j->pgid); if (cont) { - signal(SIGTTOU, SIG_IGN); - if (tcsetpgrp(STDIN, job->pgid) == -1) - return (1); - signal(SIGTTOU, sigttou_handler); - tcsetattr(STDIN, TCSANOW, &job->tmodes); - if (kill(-job->pgid, SIGCONT) < 0) - perror("kill (SIGCONT)"); + tcsetattr(STDIN, TCSADRAIN, &j->tmodes); + if (kill(-j->pgid, SIGCONT) < 0) + DG("kill (SIGCONT) malfunction"); } - job_wait(job->id); - job_remove(job->id); - signal(SIGTTOU, SIG_IGN); + job_wait(j->id); + job_remove(j->id); + tcsetpgrp(STDIN, jobc->shell_pgid); - signal(SIGTTOU, sigttou_handler); - tcgetattr(STDIN, &job->tmodes); + + tcgetattr(STDIN, &j->tmodes); tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); return (0); } diff --git a/42sh/src/job-control/sigttin_handler.c b/42sh/src/job-control/sigttin_handler.c index 1b424bf5..95197fce 100644 --- a/42sh/src/job-control/sigttin_handler.c +++ b/42sh/src/job-control/sigttin_handler.c @@ -15,5 +15,5 @@ void sigttin_handler(int signo) { (void)signo; - DG("got SIGTTIN"); + DG("got SIGTTIN, pid=%i, pgid=%i", getpid(), getpgrp()); } diff --git a/42sh/src/job-control/sigttou_handler.c b/42sh/src/job-control/sigttou_handler.c index a999d1e1..e89a5641 100644 --- a/42sh/src/job-control/sigttou_handler.c +++ b/42sh/src/job-control/sigttou_handler.c @@ -15,5 +15,5 @@ void sigttou_handler(int signo) { (void)signo; - DG("got SIGTTOU"); + DG("got SIGTTOU, pid=%i, pgid=%i", getpid(), getpgid(getpid())); } diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index b540d564..d33221da 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -28,9 +28,8 @@ void shell_init(int ac, char **av) signal(SIGINT, sigint_handler); signal(SIGQUIT, SIG_IGN); signal(SIGTSTP, sigtstp_handler); - /* signal(SIGTSTP, SIG_IGN); */ - signal(SIGTTIN, sigttin_handler); - signal(SIGTTOU, sigttou_handler); + signal(SIGTTIN, SIG_IGN); + signal(SIGTTOU, SIG_IGN); signal(SIGCHLD, sigchld_handler); *shell_pgid = getpid(); if (setpgid(*shell_pgid, *shell_pgid))