From 338913ade0711515f7339436055ffabf4caa090c Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 20 Mar 2017 20:53:23 +0100 Subject: [PATCH] ctrlD ok --- 42sh/Makefile | 4 ++-- 42sh/src/builtin/builtin_env.c | 4 ++-- 42sh/src/exec/exec_leaf.c | 3 ++- 42sh/src/exec/exec_reset.c | 34 +++++++++++++++++---------- 42sh/src/exec/process_launch.c | 5 ++-- 42sh/src/exec/process_resetfds.c | 11 +++++---- 42sh/src/exec/process_set.c | 3 ++- 42sh/src/job_control/job_addprocess.c | 4 ++-- 42sh/src/job_control/job_hup_all.c | 5 ++-- 42sh/src/job_control/job_remove.c | 4 +--- 10 files changed, 46 insertions(+), 31 deletions(-) diff --git a/42sh/Makefile b/42sh/Makefile index 67a83bca..a3bd3bf5 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,14 +6,14 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/20 12:04:31 by wescande ### ########.fr # +# Updated: 2017/03/20 19:08:06 by jhalford ### ########.fr # # # # **************************************************************************** # NAME = 42sh CC = gcc -FLAGS = -Wall -Wextra -Werror -fvisibility=hidden +FLAGS = -Wall -Wextra -Werror -fvisibility=hidden -fsanitize=address D_FLAGS = -g DELTA = $$(echo "$$(tput cols)-47"|bc) diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 400e0022..a30da58a 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2017/03/20 15:02:31 by wescande ### ########.fr */ +/* Updated: 2017/03/20 20:51:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,7 +87,7 @@ static int env_treat_flag(char ***custom_env, char *const *arg[]) return (0); } -int builtin_env(const char *path, +int builtin_env(const char *path, char *const argv[], char *const envp[]) { char **env; diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c index 4e71d269..4468ef57 100644 --- a/42sh/src/exec/exec_leaf.c +++ b/42sh/src/exec/exec_leaf.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:47:30 by wescande #+# #+# */ -/* Updated: 2017/03/20 12:47:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 20:41:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ int exec_leaf(t_btree **ast) p.map = g_process_map[p.type]; if (!(process_launch(&p))) { + DG("check"); job_addprocess(&p); if (IS_PIPEEND(p)) { diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index 161bf6a0..ed1892a3 100644 --- a/42sh/src/exec/exec_reset.c +++ b/42sh/src/exec/exec_reset.c @@ -6,16 +6,26 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/20 16:09:02 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 20:40:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static void print_error(char *std) +static int print_error(char *std) { ft_dprintf(2, "{red}%s: internal fcntl %s error errno=%i{eoc}\n", SHELL_NAME, std, errno); + return (errno); +} + +int exec_reset_job(t_job *job) +{ + job->id = 0; + job->pgid = 0; + job->attrs = JOB_NOTIFIED; + job->first_process = NULL; + return (0); } int exec_reset(void) @@ -27,20 +37,20 @@ int exec_reset(void) jobc = &data_singleton()->jobc; if (errno != EBADF) { - if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1) - print_error("STDIN"); - if ((exec->fd_save[1] = fcntl(STDOUT, F_DUPFD_CLOEXEC, 10)) == -1) - print_error("STDOUT"); - if ((exec->fd_save[2] = fcntl(STDERR, F_DUPFD_CLOEXEC, 10)) == -1) - print_error("STDERR"); + if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1 + && errno != EBADF) + return (print_error("STDIN")); + if ((exec->fd_save[1] = fcntl(STDOUT, F_DUPFD_CLOEXEC, 10)) == -1 + && errno != EBADF) + return (print_error("STDOUT")); + if ((exec->fd_save[2] = fcntl(STDERR, F_DUPFD_CLOEXEC, 10)) == -1 + && errno != EBADF) + return (print_error("STDERR")); } exec->op_stack = NULL; exec->fdin = STDIN; exec->attrs = 0; - exec->job.id = 0; - exec->job.pgid = 0; - exec->job.attrs = JOB_NOTIFIED; - exec->job.first_process = NULL; + exec_reset_job(&exec->job); tcgetattr(STDIN, &exec->job.tmodes); jobc->first_job = NULL; jobc->current_id = 1; diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index 4566bd1b..5ee8f200 100644 --- a/42sh/src/exec/process_launch.c +++ b/42sh/src/exec/process_launch.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ -/* Updated: 2017/03/20 18:41:25 by wescande ### ########.fr */ +/* Updated: 2017/03/20 20:52:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,8 @@ static int do_the_fork_if_i_have_to(t_process *p) set_exitstatus(1, 1); return (0); } - return (p->map.launch(p)); + set_exitstatus(p->map.launch(p), 1); + return (0); } return (do_the_muther_forker(p)); } diff --git a/42sh/src/exec/process_resetfds.c b/42sh/src/exec/process_resetfds.c index 4045c943..aae6ee4c 100644 --- a/42sh/src/exec/process_resetfds.c +++ b/42sh/src/exec/process_resetfds.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/09 14:51:23 by jhalford #+# #+# */ -/* Updated: 2017/03/19 19:36:11 by wescande ### ########.fr */ +/* Updated: 2017/03/20 20:25:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,10 @@ void process_resetfds(t_process *p) /* else */ /* i++; */ } - dup2(exec->fd_save[0], STDIN); - dup2(exec->fd_save[1], STDOUT); - dup2(exec->fd_save[2], STDERR); + if (exec->fd_save[0] != -1) + dup2(exec->fd_save[0], STDIN); + if (exec->fd_save[1] != -1) + dup2(exec->fd_save[1], STDOUT); + if (exec->fd_save[2] != -1) + dup2(exec->fd_save[2], STDERR); } diff --git a/42sh/src/exec/process_set.c b/42sh/src/exec/process_set.c index 2c12279c..29c64f87 100644 --- a/42sh/src/exec/process_set.c +++ b/42sh/src/exec/process_set.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 14:54:45 by jhalford #+# #+# */ -/* Updated: 2017/03/20 18:50:44 by wescande ### ########.fr */ +/* Updated: 2017/03/20 20:41:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,6 +65,7 @@ int process_set(t_process *p, t_btree *ast) p->fdin = exec->fdin; p->to_close = fds[PIPE_READ]; p->fdout = fds[PIPE_WRITE]; + p->pid = 0; exec->fdin = fds[PIPE_READ]; if (!ast) return (0); diff --git a/42sh/src/job_control/job_addprocess.c b/42sh/src/job_control/job_addprocess.c index 4a4356d4..7a860da3 100644 --- a/42sh/src/job_control/job_addprocess.c +++ b/42sh/src/job_control/job_addprocess.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:54:51 by jhalford #+# #+# */ -/* Updated: 2017/03/20 14:16:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 20:51:59 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ int job_addprocess(t_process *p) job_update_id(); job->id = jobc->current_id; job->pgid = SH_IS_INTERACTIVE(data_singleton()->opts) ? - p->pid : getpgid(0); + p->pid : data_singleton()->jobc.shell_pgid; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); } job = jobc->first_job->content; diff --git a/42sh/src/job_control/job_hup_all.c b/42sh/src/job_control/job_hup_all.c index 0a4e238c..37944153 100644 --- a/42sh/src/job_control/job_hup_all.c +++ b/42sh/src/job_control/job_hup_all.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 11:37:40 by jhalford #+# #+# */ -/* Updated: 2017/03/20 11:37:53 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 20:51:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,8 @@ void job_hup_all(void) while (jlist) { job = jlist->content; - kill(-job->pgid, SIGHUP); + if (job->pgid != 1) + kill(-job->pgid, SIGHUP); jlist = jlist->next; } } diff --git a/42sh/src/job_control/job_remove.c b/42sh/src/job_control/job_remove.c index 00cbb7dc..b8ffc169 100644 --- a/42sh/src/job_control/job_remove.c +++ b/42sh/src/job_control/job_remove.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:51:08 by jhalford #+# #+# */ -/* Updated: 2017/03/20 14:31:22 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 20:19:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,12 +25,10 @@ void job_remove(int id) j = jlist->content; if (job_is_completed(j)) { - DG(); p = ft_lstlast(j->first_process)->content; set_exitstatus(p->status, 0); if (id < data_singleton()->jobc.current_id) data_singleton()->jobc.current_id = id; ft_lst_delif(&jobc->first_job, &id, job_cmp_id, job_free); - DG(); } }