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

2
.gitignore vendored
View file

@ -39,3 +39,5 @@ fuzz
fuzz_hand_tester fuzz_hand_tester
*CORPUS *CORPUS
crash-* crash-*
test.sh
std.supp

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/04 19:50:42 by kcolin #+# #+# */ /* Created: 2025/04/04 19:50:42 by kcolin #+# #+# */
/* Updated: 2025/04/30 14:06:24 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:26:49 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,14 +21,12 @@
t_subprocess group_cmd_execute(t_group_cmd *cmd, t_minishell *app) t_subprocess group_cmd_execute(t_group_cmd *cmd, t_minishell *app)
{ {
int pid; int pid;
t_std_fds fds;
pid = fork(); pid = fork();
if (pid == 0) if (pid == 0)
{ {
handle_redirections(cmd->redirects, app, &fds); handle_redirections(cmd->redirects, app);
cmd_execute(cmd->cmd, app); cmd_execute(cmd->cmd, app);
restore_std_fds(&fds);
return (SUBPROCESS); return (SUBPROCESS);
} }
if (pid < 0) if (pid < 0)

View file

@ -5,12 +5,13 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 16:37:21 by kcolin #+# #+# */ /* Created: 2025/05/06 14:48:26 by kcolin #+# #+# */
/* Updated: 2025/04/29 15:54:21 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:49:50 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "builtins.h" #include "builtins.h"
#include "handle_redirections.h"
#include "libft.h" #include "libft.h"
#include "simple_cmd_execute.h" #include "simple_cmd_execute.h"
#include "std_fds.h" #include "std_fds.h"
@ -40,8 +41,9 @@ t_builtin_type get_builtin(t_simple_cmd *cmd)
} }
t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app, t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds, t_builtin_type type) t_builtin_type type)
{ {
t_std_fds fds;
t_subprocess retvalue; t_subprocess retvalue;
static t_subprocess (*builtins[])(t_simple_cmd *, t_minishell *) = { static t_subprocess (*builtins[])(t_simple_cmd *, t_minishell *) = {
[BUILTIN_INVALID] = builtin_invalid, [BUILTIN_INVALID] = builtin_invalid,
@ -54,8 +56,13 @@ t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app,
[BUILTIN_UNSET] = builtin_unset, [BUILTIN_UNSET] = builtin_unset,
}; };
std_fds_save(&fds);
if (handle_redirections(cmd->redirections, app) == NULL)
{
restore_std_fds(&fds);
return (PARENTPROCESS);
}
retvalue = builtins[type](cmd, app); retvalue = builtins[type](cmd, app);
if (type != BUILTIN_INVALID) restore_std_fds(&fds);
restore_std_fds(fds);
return (retvalue); return (retvalue);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 14:16:13 by kcolin #+# #+# */ /* Created: 2025/03/31 14:16:13 by kcolin #+# #+# */
/* Updated: 2025/04/29 15:46:28 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:49:59 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,6 +26,6 @@ t_subprocess builtin_unset(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type get_builtin(t_simple_cmd *cmd); t_builtin_type get_builtin(t_simple_cmd *cmd);
t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app, t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds, t_builtin_type type); t_builtin_type type);
#endif // BUILTINS_H #endif // BUILTINS_H

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:14:40 by kcolin #+# #+# */ /* Created: 2025/04/17 11:14:40 by kcolin #+# #+# */
/* Updated: 2025/04/29 16:55:24 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:23:45 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -41,15 +41,12 @@ static t_redirect *do_redirection(t_redirect *redirection)
return (redirection); return (redirection);
} }
t_minishell *handle_redirections(t_redirect *redirections, t_minishell *app, t_minishell *handle_redirections(t_redirect *redirections, t_minishell *app)
t_std_fds *fds)
{ {
std_fds_save(fds);
while (redirections != NULL) while (redirections != NULL)
{ {
if (do_redirection(redirections) == NULL) if (do_redirection(redirections) == NULL)
{ {
restore_std_fds(fds);
app->last_return_value = 1; app->last_return_value = 1;
return (NULL); return (NULL);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:14:29 by kcolin #+# #+# */ /* Created: 2025/04/17 11:14:29 by kcolin #+# #+# */
/* Updated: 2025/04/28 12:45:17 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:23:49 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,6 @@
# include "../../minishell.h" # include "../../minishell.h"
t_minishell *handle_redirections(t_redirect *redirections, t_minishell *handle_redirections(t_redirect *redirections,
t_minishell *app, t_std_fds *fds); t_minishell *app);
#endif // HANDLE_REDIRECTIONS_H #endif // HANDLE_REDIRECTIONS_H

View file

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

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/02 18:19:23 by kcolin #+# #+# */ /* Created: 2025/05/06 14:50:26 by kcolin #+# #+# */
/* Updated: 2025/04/29 15:14:32 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:50:26 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -72,9 +72,8 @@ static int ft_execve(char *exe, char **argv, char **envp)
} }
t_subprocess execute_subprocess(char *exe, t_simple_cmd *cmd, t_subprocess execute_subprocess(char *exe, t_simple_cmd *cmd,
t_minishell *app, t_std_fds *fds) t_minishell *app)
{ {
std_fds_close(fds);
app->last_return_value = ft_execve(exe, argv_from_wordlist(cmd->words), app->last_return_value = ft_execve(exe, argv_from_wordlist(cmd->words),
envp_from_env(app->env)); envp_from_env(app->env));
return (SUBPROCESS); return (SUBPROCESS);

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/02 18:17:48 by kcolin #+# #+# */ /* Created: 2025/04/02 18:17:48 by kcolin #+# #+# */
/* Updated: 2025/04/29 15:14:09 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:50:33 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,6 @@
# include "../../minishell.h" # include "../../minishell.h"
t_subprocess execute_subprocess(char *exe, t_simple_cmd *cmd, t_subprocess execute_subprocess(char *exe, t_simple_cmd *cmd,
t_minishell *app, t_std_fds *fds); t_minishell *app);
#endif // SUBPROCESS_H #endif // SUBPROCESS_H