Fix not closing pipes on a failed command
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 5 Sep 2024 07:27:09 +0000 (09:27 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 5 Sep 2024 07:27:09 +0000 (09:27 +0200)
The symptom of this is no output after execution of command of he form
"nonexistent_cmd | rest of pipeline". The nonexistent command produces
error, which prevents forgetting the pipe fds. This makes it so (at
least) two fds are not forgotten which interferes later on.

inc/execution.h
src/exe_pipeline.c

index d76503a7da614a0332667541287d8945a0ab44c5..2bfb016ded041881d389bb799640b51c1e9796a1 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/02 17:40:19 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/09/01 14:43:27 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/09/05 09:25:52 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -41,7 +41,7 @@ int           ex_fields(char **fields, t_vec *assignments,
                        const t_vec *redirections, t_execution_env *env);
 int            ex_command(t_parse_tree_node *command, t_execution_env *env);
 int            ex_pipeline(t_parse_tree_node *pipeline,
-                       t_execution_env *env, int depth);
+                       t_execution_env *env, size_t depth);
 int            ex_program(t_parse_tree_node *program, t_execution_env *env);
 
 char   *get_word(const t_parse_tree_node *parent);
index bf8e0bb7c0f45f6aa426cabb49f9d936c49f8e7e..dae951dc57412c4c0ed5903fa9f92dc611ef3a16 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/09/01 11:52:56 by lnikolov          #+#    #+#             */
-/*   Updated: 2024/09/01 13:43:51 by lnikolov         ###   ########.fr       */
+/*   Updated: 2024/09/05 09:26:28 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -84,7 +84,7 @@ static void   wait_for_return(t_execution_env *env)
 }
 
 static int     advance_pipeline(t_parse_tree_node *pipeline,
-               t_execution_env *env, int depth)
+               t_execution_env *env, size_t depth)
 {
        int     pipe_fds[2];
        int     res;
@@ -98,11 +98,10 @@ static int  advance_pipeline(t_parse_tree_node *pipeline,
                || ex_pipeline(ft_vec_access(&pipeline->children, 0), env, depth + 1);
        ft_swap_int(&pipe_fds[1], &env->stdout_fd);
        close(pipe_fds[1]);
-       if (res == 0)
-       {
+       if (env->fds_to_close.size == 2 * depth + 2)
                ft_vec_forget(&env->fds_to_close, env->fds_to_close.size - 1);
+       if (env->fds_to_close.size == 2 * depth + 1)
                ft_vec_forget(&env->fds_to_close, env->fds_to_close.size - 1);
-       }
        ft_swap_int(&pipe_fds[0], &env->stdin_fd);
        res = res || ex_command(ft_vec_access(&pipeline->children, 2), env);
        ft_swap_int(&pipe_fds[0], &env->stdin_fd);
@@ -110,7 +109,7 @@ static int  advance_pipeline(t_parse_tree_node *pipeline,
        return (res);
 }
 
-int    ex_pipeline(t_parse_tree_node *pipeline, t_execution_env *env, int depth)
+int    ex_pipeline(t_parse_tree_node *pipeline, t_execution_env *env, size_t depth)
 {
        if (g_last_signal != 0)
                return (0);