From 4c45343b2c5af1d99b109c9ae07ec8409c4fbf9b Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 30 Aug 2024 17:36:49 +0200 Subject: [PATCH] Make the forks close ALL fds except the needed ones The recorsive (non-forking) ex_pipeline creates pipes that are inherited by forked processes. These processes only need the latest opened pipe so for the healt of other commands the other pipes should be closed. --- inc/minishell_structs.h | 3 ++- src/env.c | 3 ++- src/execution.c | 26 +++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/inc/minishell_structs.h b/inc/minishell_structs.h index 0ebdff9..3c04174 100644 --- a/inc/minishell_structs.h +++ b/inc/minishell_structs.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */ -/* Updated: 2024/08/30 09:16:51 by ljiriste ### ########.fr */ +/* Updated: 2024/08/30 17:30:11 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,7 @@ typedef struct s_execution_env int last_was_builtin; int last_builtin_ret_val; t_vars *vars; + t_vec fds_to_close; t_vec child_pids; } t_execution_env; diff --git a/src/env.c b/src/env.c index 7f6dac8..42f7b37 100644 --- a/src/env.c +++ b/src/env.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/21 21:24:52 by ljiriste #+# #+# */ -/* Updated: 2024/08/28 09:36:37 by ljiriste ### ########.fr */ +/* Updated: 2024/08/30 17:31:12 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ int init_env(t_execution_env *env, char **envp) return (1); res = init_vars(env->vars, envp); res = (ft_vec_init(&env->child_pids, sizeof(pid_t)) != success) || res; + res = (ft_vec_init(&env->fds_to_close, sizeof(int)) != success) || res; if (res) clean_env(env); return (res); diff --git a/src/execution.c b/src/execution.c index 085a286..1b0c36d 100644 --- a/src/execution.c +++ b/src/execution.c @@ -6,7 +6,7 @@ /* By: lnikolov stdout_fd, STDOUT_FILENO) == -1) return (1); + if (env->stdin_fd != STDIN_FILENO) + close(env->stdin_fd); + if (env->stdout_fd != STDOUT_FILENO) + close(env->stdout_fd); return (0); } @@ -830,6 +834,21 @@ int ex_builtin(char **fields, __attribute__((unused)) t_vec *assignments, __attr return (0); } +void close_fds(t_vec *fds_to_close) +{ + int fd; + size_t i; + + i = 0; + while (i < fds_to_close->size) + { + fd = *(int *)ft_vec_access(fds_to_close, i); + close(fd); + ++i; + } + return ; +} + int ex_fields(char **fields, t_vec *assignments, const t_vec *redirections, t_execution_env *env) { pid_t pid; @@ -851,6 +870,7 @@ int ex_fields(char **fields, t_vec *assignments, const t_vec *redirections, t_ex pid = fork(); if (pid == 0) { + close_fds(&env->fds_to_close); dup_pipes(env); dup_redirections(redirections); add_exported(assignments, env); @@ -1033,10 +1053,14 @@ int ex_pipeline(t_parse_tree_node *pipeline, t_execution_env *env, int depth) { if (pipe(pipe_fds)) return (1); + ft_vec_append(&env->fds_to_close, &pipe_fds[0]); + ft_vec_append(&env->fds_to_close, &env->stdout_fd); ft_swap_int(&pipe_fds[1], &env->stdout_fd); ex_pipeline(ft_vec_access(&pipeline->children, 0), env, depth + 1); ft_swap_int(&pipe_fds[1], &env->stdout_fd); close(pipe_fds[1]); + ft_vec_forget(&env->fds_to_close, env->fds_to_close.size - 1); + ft_vec_forget(&env->fds_to_close, env->fds_to_close.size - 1); ft_swap_int(&pipe_fds[0], &env->stdin_fd); ex_command(ft_vec_access(&pipeline->children, 2), env); ft_swap_int(&pipe_fds[0], &env->stdin_fd); -- 2.30.2