Make the forks close ALL fds except the needed ones
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 30 Aug 2024 15:36:49 +0000 (17:36 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 30 Aug 2024 15:36:49 +0000 (17:36 +0200)
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
src/env.c
src/execution.c

index 0ebdff970e858fd69514262b4b1eb0ea3f2f4a15..3c04174d7bdca37cf9a107c71c39553dc0ef3822 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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;
 
index 7f6dac81855edd80eca9909ff3b49914f39a4e46..42f7b37f1f55eec80f2270827e91e59c53c777c1 100644 (file)
--- a/src/env.c
+++ b/src/env.c
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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);
index 085a286f7fc18498f0ad5e2032b4f77218bcaf36..1b0c36d7f41ae0e399375c6af98e3df426cdbcc3 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/30 14:48:10 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/30 17:31:53 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -677,6 +677,10 @@ int        dup_pipes(const t_execution_env *env)
                return (1);
        if (dup2(env->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);