From 95be4e209ccc9d0fb0be96dfea792717d4d2ad71 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Wed, 8 Mar 2017 14:52:44 +0100 Subject: [PATCH] stuff --- 42sh/Makefile | 2 +- 42sh/includes/exec.h | 6 ++-- 42sh/src/exec/exec_leaf.c | 2 +- 42sh/src/exec/exec_reset.c | 31 ++++++++++++++++++++ 42sh/src/exec/launch_builtin.c | 2 +- 42sh/src/exec/launch_file.c | 5 +++- 42sh/src/exec/launch_process.c | 24 +-------------- 42sh/src/exec/launch_subshell.c | 2 +- 42sh/src/exec/process_reset.c | 8 ++--- 42sh/src/exec/process_setexec.c | 52 --------------------------------- 42sh/src/exec/set_process.c | 8 ++--- 42sh/src/exec/set_process_cmd.c | 38 ++++++++++++++++++++++-- 42sh/src/exec/set_process_map.c | 6 ++-- 42sh/src/main/data_init.c | 14 ++------- 42sh/src/main/main.c | 2 +- 15 files changed, 92 insertions(+), 110 deletions(-) create mode 100644 42sh/src/exec/exec_reset.c delete mode 100644 42sh/src/exec/process_setexec.c diff --git a/42sh/Makefile b/42sh/Makefile index 7e90da5e..0cac1aa5 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -71,6 +71,7 @@ exec/exec_leaf.c\ exec/exec_math.c\ exec/exec_or_if.c\ exec/exec_pipe.c\ +exec/exec_reset.c\ exec/exec_semi.c\ exec/exec_var.c\ exec/fd_is_valid.c\ @@ -91,7 +92,6 @@ exec/mark_process_status.c\ exec/node_copy.c\ exec/process_redirect.c\ exec/process_reset.c\ -exec/process_setexec.c\ exec/process_setgroup.c\ exec/process_setsig.c\ exec/redir_copy.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index e8c72bc6..59fd4d22 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/08 12:26:19 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 14:51:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -131,7 +131,8 @@ extern t_itof g_execmap[]; extern t_itof g_redirmap[]; extern t_itof g_launchmap[]; -int process_setexec(t_process *p); + +int exec_reset(void); int process_setgroup(t_process *p, pid_t pid); void process_setsig(void); void process_reset(t_process *p); @@ -190,6 +191,7 @@ int launch_function(t_process *p); /* ** Mapping pour set les process */ + int set_process(t_process *p, t_btree *ast); int set_process_map(t_process *p, t_btree *ast, t_cmd *cmd); int set_process_cmd(t_process *p, t_btree *ast, t_cmd *cmd); diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c index e8ef5244..95f1d38b 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/08 11:50:20 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 14:47:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c new file mode 100644 index 00000000..2b925196 --- /dev/null +++ b/42sh/src/exec/exec_reset.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_reset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ +/* Updated: 2017/03/08 14:45:10 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int exec_reset(void) +{ + t_exec *exec; + + exec = &data_singleton()->exec; + exec->fd_save[0] = fcntl(0, F_DUPFD_CLOEXEC); + exec->fd_save[1] = fcntl(1, F_DUPFD_CLOEXEC); + exec->fd_save[2] = fcntl(2, F_DUPFD_CLOEXEC); + exec->op_stack = NULL; + exec->fdin = STDIN; + exec->attrs = 0; + exec->job.id = 0; + exec->job.pgid = 0; + exec->job.attrs = 0; + exec->job.first_process = NULL; + return (0); +} diff --git a/42sh/src/exec/launch_builtin.c b/42sh/src/exec/launch_builtin.c index 47d090cc..71497c10 100644 --- a/42sh/src/exec/launch_builtin.c +++ b/42sh/src/exec/launch_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:48:24 by jhalford #+# #+# */ -/* Updated: 2017/03/08 13:04:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 14:43:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_file.c b/42sh/src/exec/launch_file.c index 56b06a04..180d0f58 100644 --- a/42sh/src/exec/launch_file.c +++ b/42sh/src/exec/launch_file.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ -/* Updated: 2017/03/08 12:42:22 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 14:46:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,10 +40,13 @@ int launch_file(t_process *p) ft_dprintf(2, "{red}%s: permission denied: %s{eoc}\n", SHELL_NAME, p->data.cmd.av[0]); exit(126); } + // for all leaves process_setgroup(p, 0); process_setsig(); if (process_redirect(p)) exit (1); + exec_reset(); + // (*p->data.cmd.execf)(p->data.cmd.path, p->data.cmd.av, data_singleton()->env); ft_dprintf(2, "{red}%s: internal execve error on %s{eoc}\n", SHELL_NAME, p->data.cmd.av[0]); exit(42); diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index e812c0b3..dbe58ef0 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ -/* Updated: 2017/03/08 14:26:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 14:45:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,6 @@ t_itof g_launchmap[] = int launch_process(t_process *p) { -// int i; int pid; if (p->type >= PROCESS_MAX) @@ -42,25 +41,4 @@ int launch_process(t_process *p) p->pid = pid; process_setgroup(p, pid); return (0); - /* - i = 0; - while (g_launchmap[i].id) - { - if (p->type == g_launchmap[i].id) - { - if (!g_launchmap[i].f) - return (-1); - p->attrs &= ~PROCESS_STATE_MASK; - p->attrs |= PROCESS_RUNNING; - if ((pid = (*g_launchmap[i].f)(p))) - { - p->pid = pid; - process_setgroup(p, pid); - return (0); - } - } - i++; - } - return (-1); - */ } diff --git a/42sh/src/exec/launch_subshell.c b/42sh/src/exec/launch_subshell.c index aa2f481d..98fd0bcb 100644 --- a/42sh/src/exec/launch_subshell.c +++ b/42sh/src/exec/launch_subshell.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 00:11:44 by wescande #+# #+# */ -/* Updated: 2017/03/08 00:32:13 by wescande ### ########.fr */ +/* Updated: 2017/03/08 14:44:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_reset.c b/42sh/src/exec/process_reset.c index 3b5c065e..7e12b8b3 100644 --- a/42sh/src/exec/process_reset.c +++ b/42sh/src/exec/process_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/07 17:44:22 by jhalford #+# #+# */ -/* Updated: 2017/03/07 14:36:21 by wescande ### ########.fr */ +/* Updated: 2017/03/08 14:51:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,10 @@ void process_reset(t_process *p) // p->path = NULL; // p->execf = NULL; p->pid = 0; - /* p->fdin = STDIN; */ - /* p->fdout = STDOUT; */ + p->fdin = STDIN; + p->fdout = STDOUT; p->to_close = 0; p->redirs = NULL; p->status = -1; p->attrs = 0; -// p->condition = NULL; -// p->content = NULL; } diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c deleted file mode 100644 index 9ad7aedf..00000000 --- a/42sh/src/exec/process_setexec.c +++ /dev/null @@ -1,52 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* process_setexec.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */ -/* Updated: 2017/03/08 12:54:54 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -int process_setexec(t_process *p) -{ - t_btree *func; - p->data.cmd.path = NULL; - p->data.cmd.execf = NULL; - p->data.cmd.stat = ft_memalloc(sizeof(struct stat)); - DG("gonna setexec av[0]=[%s]", p->data.cmd.av[0]); - if ((func = is_function(p))) - { - p->data.subshell.content = func; - p->type = PROCESS_FUNCTION; - } - else if ((p->data.cmd.execf = is_builtin(p))) - p->type = PROCESS_BUILTIN; - else if (ft_strchr(p->data.cmd.av[0], '/')) - { - p->type = PROCESS_FILE; - p->data.cmd.execf = &execve; - p->data.cmd.path = ft_strdup(p->data.cmd.av[0]); - if (stat(p->data.cmd.path, p->data.cmd.stat) == -1) - ft_memdel((void**)&p->data.cmd.stat); - } - else - { - p->type = PROCESS_FILE; - if (ft_hash(p)) - { - p->data.cmd.execf = &execve; - DG("found hash at [%s]", p->data.cmd.path); - if (stat(p->data.cmd.path, p->data.cmd.stat) == -1) - { - ft_memdel((void**)&p->data.cmd.stat); - ft_dprintf(2, "{red}%s: %s: unexpected stat (2) failure\n", SHELL_NAME, p->data.cmd.path); - } - } - } - return (0); -} diff --git a/42sh/src/exec/set_process.c b/42sh/src/exec/set_process.c index b3912717..a19e1e1a 100644 --- a/42sh/src/exec/set_process.c +++ b/42sh/src/exec/set_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 14:54:45 by jhalford #+# #+# */ -/* Updated: 2017/03/08 11:53:31 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 14:51:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ int set_process(t_process *p, t_btree *ast) int fds[2]; cmd = &((t_astnode *)ast->item)->data.cmd; - process_reset(p); + /* process_reset(p); */ exec = &data_singleton()->exec; op = pop(&exec->op_stack); if ((EXEC_IS_AND_IF(exec->attrs) @@ -39,7 +39,5 @@ int set_process(t_process *p, t_btree *ast) p->fdout = fds[PIPE_WRITE]; exec->fdin = fds[PIPE_READ]; p->redirs = ft_lstmap(cmd->redir, &redir_copy); - if (set_process_map(p, ast, cmd)) - return (1); - return (0); + return (set_process_map(p, ast, cmd)); } diff --git a/42sh/src/exec/set_process_cmd.c b/42sh/src/exec/set_process_cmd.c index e2bdfd50..3d4c3c72 100644 --- a/42sh/src/exec/set_process_cmd.c +++ b/42sh/src/exec/set_process_cmd.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:06:05 by wescande #+# #+# */ -/* Updated: 2017/03/08 11:49:32 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 14:46:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,43 @@ int set_process_cmd(t_process *p, t_btree *ast, t_cmd *cmd) { + t_btree *func; + (void)ast; if (!(p->data.cmd.av = token_to_argv(cmd->token, 1))) return (1); - process_setexec(p); + p->data.cmd.path = NULL; + p->data.cmd.execf = NULL; + p->data.cmd.stat = ft_memalloc(sizeof(struct stat)); + DG("gonna setexec av[0]=[%s]", p->data.cmd.av[0]); + if ((func = is_function(p))) + { + p->data.subshell.content = func; + p->type = PROCESS_FUNCTION; + } + else if ((p->data.cmd.execf = is_builtin(p))) + p->type = PROCESS_BUILTIN; + else if (ft_strchr(p->data.cmd.av[0], '/')) + { + p->type = PROCESS_FILE; + p->data.cmd.execf = &execve; + p->data.cmd.path = ft_strdup(p->data.cmd.av[0]); + if (stat(p->data.cmd.path, p->data.cmd.stat) == -1) + ft_memdel((void**)&p->data.cmd.stat); + } + else + { + p->type = PROCESS_FILE; + if (ft_hash(p)) + { + p->data.cmd.execf = &execve; + DG("found hash at [%s]", p->data.cmd.path); + if (stat(p->data.cmd.path, p->data.cmd.stat) == -1) + { + ft_memdel((void**)&p->data.cmd.stat); + ft_dprintf(2, "{red}%s: %s: unexpected stat (2) failure\n", SHELL_NAME, p->data.cmd.path); + } + } + } return (0); } diff --git a/42sh/src/exec/set_process_map.c b/42sh/src/exec/set_process_map.c index bdf91a50..3e32cc57 100644 --- a/42sh/src/exec/set_process_map.c +++ b/42sh/src/exec/set_process_map.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:08:12 by wescande #+# #+# */ -/* Updated: 2017/03/08 11:50:08 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 14:48:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,8 +48,8 @@ int set_process_map(t_process *p, t_btree *ast, t_cmd *cmd) if (item->type == g_setprocessmap[i].id) { if (!g_setprocessmap[i].f) - return (0); + return (1); return ((*g_setprocessmap[i].f)(p, ast, cmd)); } - return (0); + return (1); } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 7e1bc423..843047d7 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/08 14:26:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 14:39:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,17 +24,7 @@ int data_init(void) set_exitstatus(0, 1); data->comp = NULL; data->opts = 0; - data->exec.fd_save[0] = fcntl(0, F_DUPFD_CLOEXEC); - data->exec.fd_save[1] = fcntl(1, F_DUPFD_CLOEXEC); - data->exec.fd_save[2] = fcntl(2, F_DUPFD_CLOEXEC); - data->exec.op_stack = NULL; - data->exec.fdin = STDIN; - data->exec.attrs = 0; - - data->exec.job.id = 0; - data->exec.job.pgid = 0; - data->exec.job.attrs = 0; - data->exec.job.first_process = NULL; + exec_reset(); data->jobc.first_job = NULL; data->jobc.current_id = 1; diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 1f7a6f58..7cf15069 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/08 13:27:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 14:39:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */