fix(exec): correctly exit subprocesses, do not keep multiple shells in parallel

This commit is contained in:
Khaïs COLIN 2025-04-16 16:39:47 +02:00
parent 2ea4afda4a
commit 733ae1093a
14 changed files with 81 additions and 46 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 12:01:29 by khais #+# #+# */
/* Updated: 2025/04/11 12:02:26 by khais ### ########.fr */
/* Updated: 2025/04/16 16:56:55 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -46,7 +46,8 @@ static void close_and_wait(t_minishell *app, int pid1, int pid2, int pipefd[2])
do_waitpid(app, pid2);
}
void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app,
bool should_exit)
{
int pid1;
int pid2;
@ -58,15 +59,19 @@ void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
if (pid1 == 0)
{
dup_and_close(pipefd[1], STDOUT_FILENO, pipefd);
cmd_execute(cmd->first, app);
cmd_execute(cmd->first, app, true);
return ;
}
pid2 = fork();
if (pid2 == 0)
{
dup_and_close(pipefd[0], STDIN_FILENO, pipefd);
cmd_execute(cmd->second, app);
cmd_execute(cmd->second, app, true);
}
else
{
close_and_wait(app, pid1, pid2, pipefd);
if (should_exit)
exit(app->last_return_value);
}
}