diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-09-13 11:34:26 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-09-13 14:51:10 +0200 |
| commit | e63d7878ad5d5ad7ceec11e1e8ac79aced93fbc0 (patch) | |
| tree | 1cf021174499677c415eeda12bb9210758a661cf /src | |
| parent | 7e7219da26b9d256e451f8dddb66641d11f75434 (diff) | |
| download | minishell-e63d7878ad5d5ad7ceec11e1e8ac79aced93fbc0.tar.gz minishell-e63d7878ad5d5ad7ceec11e1e8ac79aced93fbc0.tar.bz2 minishell-e63d7878ad5d5ad7ceec11e1e8ac79aced93fbc0.zip | |
Fixing pipe zombie children
Diffstat (limited to 'src')
| -rw-r--r-- | src/builtin/export.c | 26 | ||||
| -rw-r--r-- | src/eval/cmd.c | 14 | ||||
| -rw-r--r-- | src/eval/op.c | 22 | ||||
| -rw-r--r-- | src/main.c | 31 | ||||
| -rw-r--r-- | src/parser/parser.c | 4 |
5 files changed, 57 insertions, 40 deletions
diff --git a/src/builtin/export.c b/src/builtin/export.c index d394af6..f19842e 100644 --- a/src/builtin/export.c +++ b/src/builtin/export.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:11:34 by charles #+# #+# */ -/* Updated: 2020/09/12 11:06:47 by charles ### ########.fr */ +/* Updated: 2020/09/13 14:16:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,20 +20,32 @@ static void st_put_declare_x(char *s) { char *equal_ptr; + int shlvl; if (s == NULL) return ; - ft_putstr("export "); if ((equal_ptr = ft_strchr(s, '=')) == NULL) equal_ptr = ft_strchr(s, '\0'); - write(STDOUT_FILENO, s, equal_ptr - s); + *equal_ptr = '\0'; + if (ft_strcmp(s, "_") == 0) + return ; + ft_putstr("declare -x "); + ft_putstr(s); ft_putchar('='); ft_putchar('"'); - while (*++equal_ptr != '\0') + if (ft_strcmp(s, "SHLVL") == 0) + { + shlvl = ft_atoi(equal_ptr + 1); + ft_putnbr(shlvl + 1); + } + else { - if (*equal_ptr == '"') - ft_putchar('\\'); - ft_putchar(*equal_ptr); + while (*++equal_ptr != '\0') + { + if (*equal_ptr == '"') + ft_putchar('\\'); + ft_putchar(*equal_ptr); + } } ft_putchar('"'); ft_putchar('\n'); diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 9745632..82e30c9 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/12 17:04:23 by charles ### ########.fr */ +/* Updated: 2020/09/13 14:19:37 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,12 +68,6 @@ int forked_cmd(void *void_param) struct stat statbuf; param = void_param; - /* ft_vecpop(param->env_local, NULL); */ - /* if (ft_vecswallow_at(param->env, param->env->size - 1, param->env_local) == NULL) */ - /* { */ - /* ft_vecdestroy(param->env_local, free); */ - /* return (EVAL_FATAL); */ - /* } */ if (param->builtin != NULL) return (param->builtin->func(param->argv, param->env)); else @@ -96,7 +90,7 @@ int forked_cmd(void *void_param) } } -int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) +int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) { t_fork_param_cmd param; char **argv; @@ -124,12 +118,12 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) return (127); } } - else if (!param.builtin->forked) + else if (!param.builtin->forked && child_pid == NULL) return (param.builtin->func(argv, env)); param.argv = argv; param.env = env; - status = fork_wrap(fds, ¶m, &forked_cmd, NULL); + status = fork_wrap(fds, ¶m, &forked_cmd, child_pid); ft_split_destroy(argv); g_last_status = status; return (status); diff --git a/src/eval/op.c b/src/eval/op.c index a39e380..7181e83 100644 --- a/src/eval/op.c +++ b/src/eval/op.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 15:27:22 by charles #+# #+# */ -/* Updated: 2020/09/12 17:00:11 by charles ### ########.fr */ +/* Updated: 2020/09/13 14:22:45 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,16 +31,16 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast) pid_t left_pid; pid_t right_pid; - eval_forked(left_fds, env, path, ast->op.left, &left_pid); - eval_forked(right_fds, env, path, ast->op.right, &right_pid); + eval(left_fds, env, path, ast->op.left, &left_pid); + close(p[FD_WRITE]); + eval(right_fds, env, path, ast->op.right, &right_pid); + close(p[FD_READ]); pid_t finished; finished = wait(NULL); - close(p[FD_READ]); - close(p[FD_WRITE]); if (finished == left_pid) { - /* waitpid(right_pid, &right_pid, 0); */ + waitpid(right_pid, &right_pid, 0); } else if (finished == right_pid) { @@ -49,14 +49,14 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast) } return (0); } - if ((status = eval(left_fds, env, path, ast->op.left)) == EVAL_FATAL) + if ((status = eval(left_fds, env, path, ast->op.left, NULL)) == EVAL_FATAL) return (EVAL_FATAL); g_last_status = status; if ((ast->op.sep == TAG_AND && status != 0) || (ast->op.sep == TAG_PIPE && status != 0) || (ast->op.sep == TAG_OR && status == 0)) return (status); - return (eval(right_fds, env, path, ast->op.right)); + return (eval(right_fds, env, path, ast->op.right, NULL)); } int wrapped_eval(void *void_param) @@ -64,7 +64,7 @@ int wrapped_eval(void *void_param) t_fork_param_args *param; param = void_param; - return (eval(param->fds, param->env, param->path, param->ast)); + return (eval(param->fds, param->env, param->path, param->ast, NULL)); } int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast) @@ -89,13 +89,13 @@ int eval_forked(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_p return (fork_wrap(fds, ¶m, wrapped_eval, child_pid)); } -int eval(int fds[2], t_env env, t_path path, t_ast *ast) +int eval(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) { if (ast->tag == AST_PARENT) return (eval_parent(fds, env, path, ast)); if (ast->tag == AST_OP) return (eval_op(fds, env, path, ast)); if (ast->tag == AST_CMD) - return (eval_cmd(fds, env, path, ast)); + return (eval_cmd(fds, env, path, ast, child_pid)); return (EVAL_FATAL); } @@ -6,7 +6,7 @@ /* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/09/13 10:13:44 by nahaddac ### ########.fr */ +/* Updated: 2020/09/13 14:23:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,19 +54,28 @@ int main(int argc, char **argv, char **envp) if (!(getcwd(buf, PATH_MAX))) return(1); if (!env_set_default(env, "PWD", buf) || - !env_set_default(env, "SHLVL", "1") || // TODO increment if set + !env_set_default(env, "SHLVL", "0") || // TODO increment if set !env_set_default(env, "PATH", "/sbin:")) // FIXME need to prefix if /sbin not in return (1); + /* char *path_str = env_search(env, "PATH"); */ + /* if (ft_strstr(path_str, "/sbin") == NULL) */ + /* { */ + /* char *value = ft_strjoin("/sbin:", path_str); */ + /* env_export(env, "PATH", value); */ + /* free(value); */ + /* } */ + path = path_update(NULL, env_search(env, "PATH")); - /* char *env_exec_path; */ - // if ((env_exec_path = ft_htget(path, "env")) == NULL) - // { - // errorf("env: command not found\n"); - // return (127); - // } - // env_export(env, "_", env_exec_path); + char *env_exec_path; + if ((env_exec_path = ft_htget(path, "env")) == NULL) + { + env_exec_path = "/sbin/env"; + /* errorf("env: command not found\n"); */ + /* return (127); */ + } + env_export(env, "_", env_exec_path); g_last_status = 0; signal(SIGINT, signal_sigint); @@ -104,7 +113,7 @@ int main(int argc, char **argv, char **envp) /* printf("===redirs===\n"); */ /* ft_lstiter(parser_out->ast->redirs, token_debug); */ int fds[2] = {FD_NONE, FD_NONE}; - int status = eval(fds, env, path, parser_out->ast); + int status = eval(fds, env, path, parser_out->ast, NULL); if (status == EVAL_FATAL) exit(1); g_last_status = status; @@ -138,7 +147,7 @@ int main(int argc, char **argv, char **envp) } int fds[2] = {FD_NONE, FD_NONE}; - int status = eval(fds, env, path, parser_out->ast); + int status = eval(fds, env, path, parser_out->ast, NULL); if (status == EVAL_FATAL) exit(1); g_last_status = status; diff --git a/src/parser/parser.c b/src/parser/parser.c index c486c1e..1a3de7f 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/09/09 13:40:49 by charles ### ########.fr */ +/* Updated: 2020/09/13 11:50:11 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,6 +88,8 @@ t_parsed *parse_op(t_tok_lst *input) if (!(sep_tag & TAG_IS_SEP)) return (parsed_error("syntax error near unexpected token `%s'\n", input->content)); ft_lstpop_front((t_ftlst**)&input, free); + if (input == NULL && sep_tag == TAG_END) + return (left); if (input == NULL) return (parsed_error("syntax error expected token\n")); right = parse_op(input); |
