fix(group_cmd): prevent fd leak

This commit is contained in:
Jérôme Guélen 2025-05-06 13:36:55 +02:00 committed by Khaïs COLIN
parent dd94aba23b
commit 4e9f631918
9 changed files with 46 additions and 48 deletions

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* simple_cmd_execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by kcolin #+# #+# */
/* Updated: 2025/05/02 12:49:22 by jguelen ### ########.fr */
/* Created: 2025/05/06 14:25:45 by kcolin #+# #+# */
/* Updated: 2025/05/06 14:59:02 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -76,34 +76,31 @@ static t_subprocess fork_fail(t_minishell *app)
return (PARENTPROCESS);
}
static t_subprocess exec_external_cmd(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds)
static t_subprocess exec_external_cmd(t_simple_cmd *cmd, t_minishell *app)
{
char *exe;
int pid;
if (cmd->words == NULL)
return (restore_std_fds(fds), PARENTPROCESS);
exe = get_cmdpath(cmd->words->word->word, app);
if (exe != NULL)
pid = fork();
if (pid == 0)
{
pid = fork();
if (pid == 0)
return (execute_subprocess(exe, cmd, app, fds));
free(exe);
if (pid < 0)
return (fork_fail(app));
do_waitpid(app, pid);
if (handle_redirections(cmd->redirections, app) == NULL)
return (SUBPROCESS);
if (cmd->words == NULL)
return (SUBPROCESS);
exe = get_cmdpath(cmd->words->word->word, app);
if (exe == NULL)
return (command_not_found(cmd, app), SUBPROCESS);
return (execute_subprocess(exe, cmd, app));
}
restore_std_fds(fds);
if (exe == NULL)
command_not_found(cmd, app);
if (pid < 0)
return (fork_fail(app));
do_waitpid(app, pid);
return (PARENTPROCESS);
}
t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
{
t_std_fds fds;
t_builtin_type type;
if (cmd == NULL)
@ -116,10 +113,8 @@ t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
return (PARENTPROCESS);
}
simple_cmd_execute_debug(cmd, app);
if (handle_redirections(cmd->redirections, app, &fds) == NULL)
return (PARENTPROCESS);
type = get_builtin(cmd);
if (type != BUILTIN_INVALID)
return (execute_builtin(cmd, app, &fds, type));
return (exec_external_cmd(cmd, app, &fds));
return (execute_builtin(cmd, app, type));
return (exec_external_cmd(cmd, app));
}