From f5940ee99e6c92099569a26d13bd5a63e913a052 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Mon, 13 Mar 2023 19:29:20 +0100 Subject: [PATCH] fix: waitpid infinite loop --- execution/execution.c | 27 ++++++++++++++++----------- utils/fd.c | 7 +++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/execution/execution.c b/execution/execution.c index b32171f..cc921ef 100644 --- a/execution/execution.c +++ b/execution/execution.c @@ -11,7 +11,6 @@ /* ************************************************************************** */ #include "execution_private.h" -#include static int ft_own_cmd(t_data *data, t_cmd *cmd) { @@ -42,7 +41,7 @@ static int ft_own_cmd(t_data *data, t_cmd *cmd) return (return_code); } -static bool ft_executor(t_cmd *cmd, char **env) +static bool ft_executor(t_cmd *cmd, char **env, int fd) { if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL) return (0); @@ -51,12 +50,14 @@ static bool ft_executor(t_cmd *cmd, char **env) return (1); if (cmd->pid == 0) { - if (cmd->fd_in[1] != -1) - dup2(cmd->fd_in[1], 0); - if (cmd->fd_out[1] != -1) - dup2(cmd->fd_out[1], 1); + /* if (cmd->fd_in[1] != -1) */ + /* dup2(cmd->fd_in[1], 0); */ + /* if (cmd->fd_out[1] != -1) */ + /* dup2(cmd->fd_out[1], 1); */ dup2(cmd->fd_in[0], 0); dup2(cmd->fd_out[0], 1); + if (fd != -1) + close(fd); ft_closer(cmd->fd_in); ft_closer(cmd->fd_out); execve(cmd->executable, cmd->args, env); @@ -66,7 +67,7 @@ static bool ft_executor(t_cmd *cmd, char **env) return (0); } -static int ft_cmd_executor(t_data *data, t_cmd *cmd) +static int ft_cmd_executor(t_data *data, t_cmd *cmd, int fd) { int exit_code; char **env; @@ -74,6 +75,8 @@ static int ft_cmd_executor(t_data *data, t_cmd *cmd) if (cmd->own_cmd == 1) { exit_code = ft_own_cmd(data, cmd); + ft_closer(cmd->fd_in); + ft_closer(cmd->fd_out); if (exit_code == -1) return (1); } @@ -82,7 +85,9 @@ static int ft_cmd_executor(t_data *data, t_cmd *cmd) env = env_to_strs(data->env); if (env == NULL) return (1); - exit_code = ft_executor(cmd, env); + exit_code = ft_executor(cmd, env, fd); + ft_closer(cmd->fd_in); + ft_closer(cmd->fd_out); ft_freer_tab_ultimate(1, env); if (exit_code == 1) return (1); @@ -102,6 +107,8 @@ int ft_cmds_executor(t_data *data) while (current != NULL) { content = current->content; + fds[0] = -1; + fds[1] = -1; if (current->next != NULL) { if (pipe(fds) == -1) @@ -109,10 +116,8 @@ int ft_cmds_executor(t_data *data) ft_add_fd(content->fd_out, fds[1]); ft_add_fd(((t_cmd *) (current->next->content))->fd_in, fds[0]); } - if (ft_cmd_executor(data, content)) + if (ft_cmd_executor(data, content, fds[0])) return (1); - ft_closer(content->fd_in); - ft_closer(content->fd_out); current = current->next; } return (0); diff --git a/utils/fd.c b/utils/fd.c index 55d23b6..89f857a 100644 --- a/utils/fd.c +++ b/utils/fd.c @@ -1,11 +1,18 @@ #include "./utils.h" +#include void ft_closer(int fds[2]) { if (fds[0] > 2) + { + //dprintf(2, "close(%d)\n", fds[0]); close(fds[0]); + } if (fds[1] > 2) + { + //dprintf(2, "close(%d)\n", fds[1]); close(fds[1]); + } } void ft_add_fd(int fds[2], int fd)