From f17e232003d9f54edcf73ba3027a7d1558be8b3b Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 27 Mar 2017 16:05:38 +0200 Subject: [PATCH] init/destroy methods for shell_fds and jobc. (#210, #214) --- 42sh/Makefile | 6 +- 42sh/includes/exec.h | 5 +- 42sh/includes/job_control.h | 3 +- 42sh/includes/minishell.h | 13 ++- 42sh/src/builtin/is_builtin.c | 2 +- 42sh/src/exec/exec_destroy.c | 6 +- 42sh/src/exec/exec_init.c | 12 +-- 42sh/src/exec/exec_pushfds.c | 29 ------- 42sh/src/exec/plaunch_builtin.c | 2 +- 42sh/src/exec/plaunch_file.c | 4 +- 42sh/src/exec/process_launch.c | 20 +++-- 42sh/src/job_control/do_job_notification.c | 2 +- 42sh/src/job_control/jobc_destroy.c | 2 +- .../exec_popfds.c => job_control/jobc_init.c} | 22 ++--- 42sh/src/main/data_exit.c | 7 +- 42sh/src/main/data_init.c | 3 +- 42sh/src/main/main.c | 2 +- 42sh/src/main/shell_fds.c | 84 +++++++++++++++++++ 42sh/src/main/shell_init.c | 5 +- 42sh/src/main/{shell_reset.c => shell_sig.c} | 24 +----- 20 files changed, 142 insertions(+), 111 deletions(-) delete mode 100644 42sh/src/exec/exec_pushfds.c rename 42sh/src/{exec/exec_popfds.c => job_control/jobc_init.c} (66%) create mode 100644 42sh/src/main/shell_fds.c rename 42sh/src/main/{shell_reset.c => shell_sig.c} (68%) diff --git a/42sh/Makefile b/42sh/Makefile index 4caa3e26..3b588045 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -78,8 +78,6 @@ exec/exec_init.c\ exec/exec_leaf.c\ exec/exec_or_if.c\ exec/exec_pipe.c\ -exec/exec_popfds.c\ -exec/exec_pushfds.c\ exec/exec_semi.c\ exec/exec_var.c\ exec/fd_is_valid.c\ @@ -197,6 +195,7 @@ job_control/job_update_id.c\ job_control/job_update_status.c\ job_control/job_wait.c\ job_control/jobc_destroy.c\ +job_control/jobc_init.c\ job_control/mark_job_as_running.c\ job_control/pprint_brace.c\ job_control/pprint_case.c\ @@ -286,8 +285,9 @@ main/data_init.c\ main/data_singleton.c\ main/ft_putast.c\ main/main.c\ +main/shell_fds.c\ main/shell_init.c\ -main/shell_reset.c\ +main/shell_sig.c\ parser/add_bang.c\ parser/add_case.c\ parser/add_cmd.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index c2445149..ce602f64 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/03/26 21:39:33 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 15:46:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -131,7 +131,6 @@ struct s_job struct s_exec { t_job job; - t_list *fd_save[10]; t_flag attrs; int fdin; t_list *op_stack; @@ -141,8 +140,6 @@ struct s_exec int exec_init(t_exec *exec); int exec_destroy(t_exec *exec); -int exec_pushfds(); -int exec_popfds(); int process_fork(t_process *p); int process_setgroup(t_process *p, pid_t pid); void process_setsig(void); diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 6d9a8b19..26262861 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */ -/* Updated: 2017/03/27 03:11:08 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 16:01:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,7 @@ struct s_jobc struct termios shell_tmodes; }; +int jobc_init(t_jobc *jobc); int jobc_destroy(t_jobc *jobc); t_list *job_getprocess(pid_t pid); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 2488f600..0b755490 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/03/25 15:44:08 by ariard ### ########.fr */ +/* Updated: 2017/03/27 15:55:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,7 @@ struct s_data char **argv; t_line line; t_lexer lexer; + t_list *fd_save[10]; t_parser parser; t_comp *comp; t_exec exec; @@ -64,8 +65,14 @@ int data_init(int ac, char **av, char **env); void data_exit(void); int get_c_arg(char *opt_arg, t_data *data); -void shell_resetfds(void); -void shell_resetsig(void); +void shell_sig_reset(void); + +void shell_fds_init(void); +void shell_fds_destroy(void); +int shell_fds_pop(void); +int shell_fds_push(void); +void shell_fds_reset(void); +void shell_fds_destroy(void); char *ft_putast(void *node); void ft_putast2(void *node); diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 0798eb5a..65a87bcc 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ -/* Updated: 2017/03/25 16:36:55 by ariard ### ########.fr */ +/* Updated: 2017/03/27 16:03:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_destroy.c b/42sh/src/exec/exec_destroy.c index f1d7c764..c01bae48 100644 --- a/42sh/src/exec/exec_destroy.c +++ b/42sh/src/exec/exec_destroy.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/27 03:09:58 by jhalford #+# #+# */ -/* Updated: 2017/03/27 03:10:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 15:56:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,9 @@ int exec_destroy(t_exec *exec) { if (!exec) - return (0); + return (1); ft_lstdel(&exec->op_stack, ft_lst_cfree); + exec->attrs = 0; + exec->fdin = STDIN; return (0); } diff --git a/42sh/src/exec/exec_init.c b/42sh/src/exec/exec_init.c index 4bcae2d2..d8b840b8 100644 --- a/42sh/src/exec/exec_init.c +++ b/42sh/src/exec/exec_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/26 21:28:09 by jhalford #+# #+# */ -/* Updated: 2017/03/26 22:13:04 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 15:57:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,19 +24,9 @@ int job_init(t_job *job) int exec_init(t_exec *exec) { - t_jobc *jobc; - int i; - - jobc = &data_singleton()->jobc; job_init(&exec->job); - i = -1; - while (++i < 10) - exec->fd_save[i] = NULL; - exec_pushfds(); exec->attrs = 0; exec->fdin = STDIN; exec->op_stack = NULL; - jobc->first_job = NULL; - jobc->current_id = 1; return (0); } diff --git a/42sh/src/exec/exec_pushfds.c b/42sh/src/exec/exec_pushfds.c deleted file mode 100644 index 31c16cde..00000000 --- a/42sh/src/exec/exec_pushfds.c +++ /dev/null @@ -1,29 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* exec_pushfds.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/26 21:31:46 by jhalford #+# #+# */ -/* Updated: 2017/03/27 00:58:25 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -int exec_pushfds(void) -{ - int i; - int fd; - t_exec *exec; - - exec = &data_singleton()->exec; - i = -1; - while (++i < 10) - { - fd = fcntl(i, F_DUPFD_CLOEXEC, 10); - push(&(exec->fd_save[i]), fd); - } - return (0); -} diff --git a/42sh/src/exec/plaunch_builtin.c b/42sh/src/exec/plaunch_builtin.c index 93960d1a..1efa4ae8 100644 --- a/42sh/src/exec/plaunch_builtin.c +++ b/42sh/src/exec/plaunch_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:48:24 by jhalford #+# #+# */ -/* Updated: 2017/03/24 14:56:11 by wescande ### ########.fr */ +/* Updated: 2017/03/27 16:03:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/plaunch_file.c b/42sh/src/exec/plaunch_file.c index a9e66228..0c66bee9 100644 --- a/42sh/src/exec/plaunch_file.c +++ b/42sh/src/exec/plaunch_file.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ -/* Updated: 2017/03/22 21:32:36 by wescande ### ########.fr */ +/* Updated: 2017/03/27 16:02:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int plaunch_file(t_process *p) error_launch(p->data.cmd.av[0], ": is a directory", 126); else if (access(p->data.cmd.path, X_OK) == -1) error_launch("permission denied: ", p->data.cmd.av[0], 126); - (*p->data.cmd.execf)( + execve( p->data.cmd.path, p->data.cmd.av, data_singleton()->env); diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index bde46f83..ad08a3b9 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/27 01:01:27 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 16:04:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,10 +27,16 @@ int process_fork(t_process *p) process_setgroup(p, 0); process_setsig(); exec_destroy(&data_singleton()->exec); - exec_init(&data_singleton()->exec); + jobc_destroy(&data_singleton()->jobc); + if (p->type == PROCESS_FILE) + shell_fds_destroy(); + else + shell_fds_push(); data_singleton()->opts &= ~SH_INTERACTIVE; data_singleton()->opts &= ~SH_OPTS_JOBC; - exit(p->map.launch(p)); + pid = p->map.launch(p); + shell_fds_destroy(); + exit(pid); } int process_launch(t_process *p) @@ -52,12 +58,12 @@ int process_launch(t_process *p) set_exitstatus(1, 1); else { - exec_pushfds(); + shell_fds_push(); p->map.launch(p); - exec_popfds(); - shell_resetfds(); - shell_resetsig(); + shell_fds_pop(); } + shell_fds_reset(); + shell_sig_reset(); process_free(p, 0); return (0); } diff --git a/42sh/src/job_control/do_job_notification.c b/42sh/src/job_control/do_job_notification.c index b9536e8a..4fa661df 100644 --- a/42sh/src/job_control/do_job_notification.c +++ b/42sh/src/job_control/do_job_notification.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 13:01:19 by jhalford #+# #+# */ -/* Updated: 2017/03/21 14:14:45 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 15:58:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job_control/jobc_destroy.c b/42sh/src/job_control/jobc_destroy.c index 2f86df98..068d1238 100644 --- a/42sh/src/job_control/jobc_destroy.c +++ b/42sh/src/job_control/jobc_destroy.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/27 03:09:41 by jhalford #+# #+# */ -/* Updated: 2017/03/27 03:10:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 15:59:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_popfds.c b/42sh/src/job_control/jobc_init.c similarity index 66% rename from 42sh/src/exec/exec_popfds.c rename to 42sh/src/job_control/jobc_init.c index d4fc942e..fd8fc134 100644 --- a/42sh/src/exec/exec_popfds.c +++ b/42sh/src/job_control/jobc_init.c @@ -1,30 +1,20 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* exec_popfds.c :+: :+: :+: */ +/* jobc_init.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/26 21:31:05 by jhalford #+# #+# */ -/* Updated: 2017/03/27 00:58:34 by jhalford ### ########.fr */ +/* Created: 2017/03/27 15:59:03 by jhalford #+# #+# */ +/* Updated: 2017/03/27 16:01:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int exec_popfds(void) +int jobc_init(t_jobc *jobc) { - int i; - int fd; - t_exec *exec; - - exec = &data_singleton()->exec; - i = -1; - while (++i < 10) - { - fd = pop(&exec->fd_save[i]); - if (fd != -1) - close(fd); - } + jobc->first_job = NULL; + jobc->current_id = 1; return (0); } diff --git a/42sh/src/main/data_exit.c b/42sh/src/main/data_exit.c index 11ca50b9..bea98b57 100644 --- a/42sh/src/main/data_exit.c +++ b/42sh/src/main/data_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */ -/* Updated: 2017/03/27 03:11:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 16:04:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ void data_exit(void) { t_data *data; - int i; data = data_singleton(); /* ft_strdel(&data->line.input); */ @@ -27,9 +26,7 @@ void data_exit(void) parser_destroy(&data->parser); exec_destroy(&data->exec); jobc_destroy(&data->jobc); - i = -1; - while (++i < 10) - ft_lstdel(&data->exec.fd_save[i], ft_lst_cfree); + shell_fds_destroy(); ft_lstdel(&data->lst_func, &tree_func_free); ft_save_termios(-1); ft_free_hash_table(); diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index a554d48f..85205e8c 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/03/26 21:54:19 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 16:00:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,6 +86,7 @@ int data_init(int ac, char **av, char **env) lexer_init(&data->lexer); parser_init(&data->parser); exec_init(&data->exec); + jobc_init(&data->jobc); if ((term_name = ft_getenv(data->env, "TERM")) == NULL) term_name = "dumb"; if (tgetent(NULL, term_name) != 1) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 73156a39..f77f4c60 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */ -/* Updated: 2017/03/26 21:57:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 15:58:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_fds.c b/42sh/src/main/shell_fds.c new file mode 100644 index 00000000..8761e728 --- /dev/null +++ b/42sh/src/main/shell_fds.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* shell_fds.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/27 15:32:13 by jhalford #+# #+# */ +/* Updated: 2017/03/27 15:57:21 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void shell_fds_init(void) +{ + t_data *data; + int i; + + data = data_singleton(); + i = -1; + while (++i < 10) + data->fd_save[i] = NULL; +} + +int shell_fds_push(void) +{ + t_data *data; + int i; + int fd; + + data = data_singleton(); + i = -1; + while (++i < 10) + { + fd = fcntl(i, F_DUPFD_CLOEXEC, 10); + push(&(data->fd_save[i]), fd); + } + return (0); +} + +int shell_fds_pop(void) +{ + t_data *data; + int i; + int fd; + + data = data_singleton(); + i = -1; + while (++i < 10) + { + fd = pop(&data->fd_save[i]); + if (fd != -1) + close(fd); + } + return (0); +} + +void shell_fds_reset(void) +{ + t_data *data; + int i; + int fd; + + data = data_singleton(); + i = -1; + while (++i < 10) + { + fd = top(data->fd_save[i]); + if (fd != -1) + dup2(fd, i); + } +} + +void shell_fds_destroy(void) +{ + t_data *data; + int i; + + data = data_singleton(); + i = -1; + while (++i < 10) + ft_lstdel(&data->fd_save[i], ft_lst_cfree); +} diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index ec68356f..5397a51c 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/03/25 01:41:36 by wescande ### ########.fr */ +/* Updated: 2017/03/27 16:04:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,7 +70,7 @@ static int interactive_settings(void) while (tcgetpgrp(STDIN) != (*shell_pgid = getpgrp())) kill(-*shell_pgid, SIGTTIN); *shell_pgid = getpid(); - shell_resetsig(); + shell_sig_reset(); if (setpgid(*shell_pgid, *shell_pgid)) { SH_ERR("setpgid(): %s", strerror(errno)); @@ -99,5 +99,6 @@ int shell_init(int ac, char **av, char **env) return (-1); if (SH_IS_INTERACTIVE(data->opts) && interactive_settings() < 0) return (-1); + shell_fds_init(); return (0); } diff --git a/42sh/src/main/shell_reset.c b/42sh/src/main/shell_sig.c similarity index 68% rename from 42sh/src/main/shell_reset.c rename to 42sh/src/main/shell_sig.c index dca33bfd..01ced561 100644 --- a/42sh/src/main/shell_reset.c +++ b/42sh/src/main/shell_sig.c @@ -1,18 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* shell_reset.c :+: :+: :+: */ +/* shell_sig_reset.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/22 16:07:14 by jhalford #+# #+# */ -/* Updated: 2017/03/24 20:09:29 by jhalford ### ########.fr */ +/* Created: 2017/03/27 15:35:01 by jhalford #+# #+# */ +/* Updated: 2017/03/27 15:35:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void shell_resetsig(void) +void shell_sig_reset(void) { signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); @@ -21,19 +21,3 @@ void shell_resetsig(void) signal(SIGTTOU, SIG_IGN); signal(SIGCHLD, SIG_DFL); } - -void shell_resetfds(void) -{ - t_exec *exec; - int i; - int fd; - - exec = &data_singleton()->exec; - i = -1; - while (++i < 10) - { - fd = top(exec->fd_save[i]); - if (fd != -1) - dup2(fd, i); - } -}