From df1f449778995031fb9aa7a5e5ad60358e02f7a6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sat, 7 Jan 2017 22:27:13 +0100 Subject: [PATCH] pipelines fixed --- 42sh/includes/job_control.h | 2 ++ 42sh/src/exec/exec_command.c | 5 +++-- 42sh/src/exec/process_redirect.c | 2 ++ 42sh/src/exec/process_reset.c | 6 ------ 42sh/src/exec/process_setgroup.c | 3 ++- 42sh/src/job-control/job_addprocess.c | 4 +--- 42sh/src/job-control/sigttin_handler.c | 19 +++++++++++++++++++ 42sh/src/job-control/sigttou_handler.c | 19 +++++++++++++++++++ 42sh/src/line-editing/sigint_handler.c | 18 ++++++++---------- 42sh/src/main/data_init.c | 9 ++++++++- 42sh/src/main/shell_init.c | 4 ++-- 11 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 42sh/src/job-control/sigttin_handler.c create mode 100644 42sh/src/job-control/sigttou_handler.c diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 08e0952b..98b0c21c 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -73,6 +73,8 @@ int check_chlds(void); void sigchld_handler(int signo); void sigint_handler(int signo); void sigtstp_handler(int signo); +void sigttin_handler(int signo); +void sigttou_handler(int signo); int process_cmp_pid(t_process *p, pid_t *pid); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index fe010c2f..73248fcf 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -21,7 +21,6 @@ int exec_command(t_btree **ast) node = (*ast)->item; p = &data_singleton()->exec.process; job = &data_singleton()->exec.job; - /* job = data_singleton()->jobc.first_job->content; */ p->av = ft_sstrdup(node->data.sstr); process_setexec(p); if (!(launch_process(p))) @@ -32,7 +31,9 @@ int exec_command(t_btree **ast) put_job_in_foreground(job, 0): put_job_in_background(job, 0); } - process_reset(); + p->av = NULL; + p->pid = 0; + p->attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index 51e1ed1c..04c70cfc 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -16,11 +16,13 @@ int process_redirect(t_process *p) { if (p->fdin != STDIN) { + DG("dup2 %i->%i", p->fdin, STDIN); dup2(p->fdin, STDIN); close(p->fdin); } if (p->fdout != STDOUT) { + DG("dup2 %i->%i", p->fdout, STDOUT); dup2(p->fdout, STDOUT); close(p->fdout); } diff --git a/42sh/src/exec/process_reset.c b/42sh/src/exec/process_reset.c index 719862f1..4b3c02a9 100644 --- a/42sh/src/exec/process_reset.c +++ b/42sh/src/exec/process_reset.c @@ -5,10 +5,4 @@ void process_reset(void) t_data *data; data = data_singleton(); - data->exec.process.path = NULL; - data->exec.process.av = NULL; - /* data->exec.process.fdin = STDIN; */ - /* data->exec.process.fdout = STDOUT; */ - data->exec.process.pid = 0; - data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; } diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index acd46689..88be05e4 100644 --- a/42sh/src/exec/process_setgroup.c +++ b/42sh/src/exec/process_setgroup.c @@ -20,9 +20,10 @@ int process_setgroup(t_process *p) (void)p; job = &data_singleton()->exec.job; - pid = p->pid; + pid = getpid(); if (job->pgid == 0) job->pgid = pid; + DG("job->pgid=%i", job->pgid); setpgid(pid, job->pgid); if (JOB_IS_FG(job->attributes)) tcsetpgrp(STDIN_FILENO, job->pgid); diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index 37061a13..b3d24661 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -19,10 +19,8 @@ int job_addprocess(t_process *p) jobc = &data_singleton()->jobc; job = &data_singleton()->exec.job; - DG("check; attr=%b", p->attributes); if (IS_PIPESTART(p->attributes)) { - DG("check"); job_update_id(); job->id = jobc->current_id; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); @@ -32,7 +30,7 @@ int job_addprocess(t_process *p) if (p->pid > 0) { ft_lsteadd(&job->first_process, ft_lstnew(p, sizeof(*p))); - DG("added process to first_job : %i", p->pid); + DG("added pid=%i to [%i]", p->pid, job->id); } if (JOB_IS_BG(job->attributes) && IS_PIPEEND(p->attributes)) job_notify_new(job); diff --git a/42sh/src/job-control/sigttin_handler.c b/42sh/src/job-control/sigttin_handler.c new file mode 100644 index 00000000..4edd2696 --- /dev/null +++ b/42sh/src/job-control/sigttin_handler.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sigttin_handler.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ +/* Updated: 2016/12/10 18:20:57 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void sigttin_handler(int signo) +{ + (void)signo; + DG("got SIGTTIN"); +} diff --git a/42sh/src/job-control/sigttou_handler.c b/42sh/src/job-control/sigttou_handler.c new file mode 100644 index 00000000..a0d318d6 --- /dev/null +++ b/42sh/src/job-control/sigttou_handler.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sigttou_handler.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */ +/* Updated: 2016/12/10 18:20:57 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void sigttou_handler(int signo) +{ + (void)signo; + DG("got SIGTTOU"); +} diff --git a/42sh/src/line-editing/sigint_handler.c b/42sh/src/line-editing/sigint_handler.c index d2929ece..123c4d41 100644 --- a/42sh/src/line-editing/sigint_handler.c +++ b/42sh/src/line-editing/sigint_handler.c @@ -12,17 +12,15 @@ #include "minishell.h" -pid_t g_pid; - void sigint_handler(int signo) { + t_job *job; + (void)signo; - if (signo == SIGINT) - { - DG("got SIGINT"); - if (g_pid) - kill(g_pid, SIGINT); - if (kill(g_pid, 0) == 0) - ft_putendl(""); - } + job = &data_singleton()->exec.job; + DG("got SIGINT; job->pgid=%i", job->pgid); + if (job->pgid) + kill(job->pgid, SIGINT); + if (kill(job->pgid, 0) == 0) + ft_putchar('\n'); } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 4953aa39..9443e93a 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -23,7 +23,14 @@ int data_init(void) data->line.input = NULL; data->env = ft_sstrdup(environ); data->line.history = NULL; - process_reset(); + + data->exec.process.path = NULL; + data->exec.process.av = NULL; + data->exec.process.fdin = STDIN; + data->exec.process.fdout = STDOUT; + data->exec.process.pid = 0; + data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; + data->exec.aol_status = NULL; data->exec.aol_search = 0; data->exec.job.id = 0; diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 6419fa2c..10d4e773 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -26,8 +26,8 @@ void shell_init(void) signal(SIGINT, sigint_handler); signal(SIGQUIT, SIG_IGN); signal(SIGTSTP, sigtstp_handler); - signal(SIGTTIN, SIG_IGN); - signal(SIGTTOU, SIG_IGN); + signal(SIGTTIN, sigttin_handler); + signal(SIGTTOU, sigttou_handler); signal(SIGCHLD, sigchld_handler); *shell_pgid = getpid(); if (setpgid(*shell_pgid, *shell_pgid))