fix: waitpid infinite loop

This commit is contained in:
Camille Chauvet 2023-03-13 19:29:20 +01:00
parent ee3d535393
commit f5940ee99e
2 changed files with 23 additions and 11 deletions

View File

@ -11,7 +11,6 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "execution_private.h" #include "execution_private.h"
#include <unistd.h>
static int ft_own_cmd(t_data *data, t_cmd *cmd) 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); 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) if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL)
return (0); return (0);
@ -51,12 +50,14 @@ static bool ft_executor(t_cmd *cmd, char **env)
return (1); return (1);
if (cmd->pid == 0) if (cmd->pid == 0)
{ {
if (cmd->fd_in[1] != -1) /* if (cmd->fd_in[1] != -1) */
dup2(cmd->fd_in[1], 0); /* dup2(cmd->fd_in[1], 0); */
if (cmd->fd_out[1] != -1) /* if (cmd->fd_out[1] != -1) */
dup2(cmd->fd_out[1], 1); /* dup2(cmd->fd_out[1], 1); */
dup2(cmd->fd_in[0], 0); dup2(cmd->fd_in[0], 0);
dup2(cmd->fd_out[0], 1); dup2(cmd->fd_out[0], 1);
if (fd != -1)
close(fd);
ft_closer(cmd->fd_in); ft_closer(cmd->fd_in);
ft_closer(cmd->fd_out); ft_closer(cmd->fd_out);
execve(cmd->executable, cmd->args, env); execve(cmd->executable, cmd->args, env);
@ -66,7 +67,7 @@ static bool ft_executor(t_cmd *cmd, char **env)
return (0); 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; int exit_code;
char **env; char **env;
@ -74,6 +75,8 @@ static int ft_cmd_executor(t_data *data, t_cmd *cmd)
if (cmd->own_cmd == 1) if (cmd->own_cmd == 1)
{ {
exit_code = ft_own_cmd(data, cmd); exit_code = ft_own_cmd(data, cmd);
ft_closer(cmd->fd_in);
ft_closer(cmd->fd_out);
if (exit_code == -1) if (exit_code == -1)
return (1); return (1);
} }
@ -82,7 +85,9 @@ static int ft_cmd_executor(t_data *data, t_cmd *cmd)
env = env_to_strs(data->env); env = env_to_strs(data->env);
if (env == NULL) if (env == NULL)
return (1); 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); ft_freer_tab_ultimate(1, env);
if (exit_code == 1) if (exit_code == 1)
return (1); return (1);
@ -102,6 +107,8 @@ int ft_cmds_executor(t_data *data)
while (current != NULL) while (current != NULL)
{ {
content = current->content; content = current->content;
fds[0] = -1;
fds[1] = -1;
if (current->next != NULL) if (current->next != NULL)
{ {
if (pipe(fds) == -1) 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(content->fd_out, fds[1]);
ft_add_fd(((t_cmd *) (current->next->content))->fd_in, fds[0]); 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); return (1);
ft_closer(content->fd_in);
ft_closer(content->fd_out);
current = current->next; current = current->next;
} }
return (0); return (0);

View File

@ -1,12 +1,19 @@
#include "./utils.h" #include "./utils.h"
#include <stdio.h>
void ft_closer(int fds[2]) void ft_closer(int fds[2])
{ {
if (fds[0] > 2) if (fds[0] > 2)
{
//dprintf(2, "close(%d)\n", fds[0]);
close(fds[0]); close(fds[0]);
}
if (fds[1] > 2) if (fds[1] > 2)
{
//dprintf(2, "close(%d)\n", fds[1]);
close(fds[1]); close(fds[1]);
} }
}
void ft_add_fd(int fds[2], int fd) void ft_add_fd(int fds[2], int fd)
{ {