fix(builtin/exit): prevent leak when calling exit in a subprocess

This commit is contained in:
Khaïs COLIN 2025-04-29 15:50:38 +02:00
parent 93f3ea7c66
commit 5ea55a9f9c
12 changed files with 65 additions and 58 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 16:20:17 by khais #+# #+# */
/* Updated: 2025/04/25 16:07:25 by khais ### ########.fr */
/* Updated: 2025/04/29 15:41:46 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -47,7 +47,7 @@ static void chdir_home(t_minishell *app)
ft_chdir(home, app);
}
t_builtin_type builtin_cd(t_simple_cmd *cmd, t_minishell *app)
t_subprocess builtin_cd(t_simple_cmd *cmd, t_minishell *app)
{
t_worddesc *arg;
@ -55,12 +55,12 @@ t_builtin_type builtin_cd(t_simple_cmd *cmd, t_minishell *app)
{
ft_dprintf(STDERR_FILENO, "minishell: cd: too many arguments\n");
app->last_return_value = 1;
return (BUILTIN_CD);
return (PARENTPROCESS);
}
arg = wordlist_get(cmd->words, 1);
if (arg == NULL)
chdir_home(app);
else
ft_chdir(arg->word, app);
return (BUILTIN_CD);
return (PARENTPROCESS);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 13:59:13 by khais #+# #+# */
/* Updated: 2025/04/14 11:52:37 by khais ### ########.fr */
/* Updated: 2025/04/29 15:43:22 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -43,14 +43,14 @@ static bool should_print_newline(t_wordlist **arg)
return (print_newline);
}
static t_builtin_type write_error(t_minishell *app)
static t_subprocess write_error(t_minishell *app)
{
app->last_return_value = 1;
perror("minishell: echo: write error");
return (BUILTIN_ECHO);
return (PARENTPROCESS);
}
t_builtin_type builtin_echo(t_simple_cmd *cmd, t_minishell *app)
t_subprocess builtin_echo(t_simple_cmd *cmd, t_minishell *app)
{
t_wordlist *arg;
bool print_newline;
@ -74,5 +74,5 @@ t_builtin_type builtin_echo(t_simple_cmd *cmd, t_minishell *app)
return (write_error(app));
}
app->last_return_value = 0;
return (BUILTIN_ECHO);
return (PARENTPROCESS);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/02 19:35:52 by khais #+# #+# */
/* Updated: 2025/04/14 11:58:15 by khais ### ########.fr */
/* Updated: 2025/04/29 15:40:55 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,14 +14,14 @@
#include "libft.h"
#include <stdio.h>
static t_builtin_type write_error(t_minishell *app)
static t_subprocess write_error(t_minishell *app)
{
app->last_return_value = 125;
perror("minishell: env: write error");
return (BUILTIN_ENV);
return (PARENTPROCESS);
}
t_builtin_type builtin_env(t_simple_cmd *cmd, t_minishell *app)
t_subprocess builtin_env(t_simple_cmd *cmd, t_minishell *app)
{
t_env *env;
@ -35,5 +35,5 @@ t_builtin_type builtin_env(t_simple_cmd *cmd, t_minishell *app)
env = env->next;
}
app->last_return_value = 0;
return (BUILTIN_ENV);
return (PARENTPROCESS);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 18:17:56 by khais #+# #+# */
/* Updated: 2025/04/16 16:40:57 by khais ### ########.fr */
/* Updated: 2025/04/29 15:37:56 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -56,7 +56,7 @@ static int numeric_arg_required(char *arg)
return (2);
}
t_builtin_type builtin_exit(t_simple_cmd *cmd, t_minishell *app)
t_subprocess builtin_exit(t_simple_cmd *cmd, t_minishell *app)
{
int status;
bool ok;
@ -71,10 +71,9 @@ t_builtin_type builtin_exit(t_simple_cmd *cmd, t_minishell *app)
{
ft_dprintf(STDERR_FILENO, "minishell: exit: too many arguments\n");
app->last_return_value = 1;
return (BUILTIN_EXIT);
return (PARENTPROCESS);
}
}
simple_cmd_destroy(cmd);
env_destroy(app->env);
exit(status);
app->last_return_value = status;
return (SUBPROCESS);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 14:00:32 by khais #+# #+# */
/* Updated: 2025/04/18 10:09:09 by khais ### ########.fr */
/* Updated: 2025/04/29 15:41:24 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -49,7 +49,7 @@ static void single_export(char *arg, t_minishell *app)
}
}
t_builtin_type builtin_export(t_simple_cmd *cmd, t_minishell *app)
t_subprocess builtin_export(t_simple_cmd *cmd, t_minishell *app)
{
int i;
t_worddesc *arg;
@ -62,5 +62,5 @@ t_builtin_type builtin_export(t_simple_cmd *cmd, t_minishell *app)
single_export(arg->word, app);
arg = wordlist_get(cmd->words, i++);
}
return (BUILTIN_EXPORT);
return (PARENTPROCESS);
}

View file

@ -6,15 +6,15 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 14:17:14 by khais #+# #+# */
/* Updated: 2025/03/31 14:17:42 by khais ### ########.fr */
/* Updated: 2025/04/29 15:41:58 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
t_builtin_type builtin_invalid(t_simple_cmd *cmd, t_minishell *app)
t_subprocess builtin_invalid(t_simple_cmd *cmd, t_minishell *app)
{
(void)cmd;
(void)app;
return (BUILTIN_INVALID);
return (PARENTPROCESS);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 14:21:52 by khais #+# #+# */
/* Updated: 2025/04/14 11:53:17 by khais ### ########.fr */
/* Updated: 2025/04/29 15:42:51 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -38,14 +38,14 @@ static char *get_current_dir(void)
return (path);
}
static t_builtin_type write_error(t_minishell *app)
static t_subprocess write_error(t_minishell *app)
{
app->last_return_value = 1;
perror("minishell: pwd: write error");
return (BUILTIN_PWD);
return (PARENTPROCESS);
}
t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app)
t_subprocess builtin_pwd(t_simple_cmd *cmd, t_minishell *app)
{
char *path;
@ -63,5 +63,5 @@ t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app)
free(path);
app->last_return_value = 0;
}
return (BUILTIN_PWD);
return (PARENTPROCESS);
}

View file

@ -6,14 +6,14 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 14:35:53 by khais #+# #+# */
/* Updated: 2025/04/03 14:39:45 by khais ### ########.fr */
/* Updated: 2025/04/29 15:40:24 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
#include "../../env/env_manip.h"
t_builtin_type builtin_unset(t_simple_cmd *cmd, t_minishell *app)
t_subprocess builtin_unset(t_simple_cmd *cmd, t_minishell *app)
{
t_wordlist *arg;
@ -24,5 +24,5 @@ t_builtin_type builtin_unset(t_simple_cmd *cmd, t_minishell *app)
arg = arg->next;
}
app->last_return_value = 0;
return (BUILTIN_UNSET);
return (PARENTPROCESS);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 16:37:21 by khais #+# #+# */
/* Updated: 2025/04/28 14:40:19 by khais ### ########.fr */
/* Updated: 2025/04/29 15:54:21 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,11 +39,11 @@ t_builtin_type get_builtin(t_simple_cmd *cmd)
return (BUILTIN_INVALID);
}
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds)
t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds, t_builtin_type type)
{
t_builtin_type type;
static t_builtin_type (*builtins[])(t_simple_cmd *, t_minishell *) = {
t_subprocess retvalue;
static t_subprocess (*builtins[])(t_simple_cmd *, t_minishell *) = {
[BUILTIN_INVALID] = builtin_invalid,
[BUILTIN_PWD] = builtin_pwd,
[BUILTIN_CD] = builtin_cd,
@ -54,9 +54,8 @@ t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app,
[BUILTIN_UNSET] = builtin_unset,
};
type = get_builtin(cmd);
builtins[type](cmd, app);
retvalue = builtins[type](cmd, app);
if (type != BUILTIN_INVALID)
restore_std_fds(fds);
return (type);
return (retvalue);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 14:16:13 by khais #+# #+# */
/* Updated: 2025/04/28 12:48:00 by khais ### ########.fr */
/* Updated: 2025/04/29 15:46:28 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,17 +15,17 @@
# include "simple_cmd_execute.h"
t_builtin_type builtin_invalid(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_cd(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_export(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_exit(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_echo(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_env(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_unset(t_simple_cmd *cmd, t_minishell *app);
t_subprocess builtin_invalid(t_simple_cmd *cmd, t_minishell *app);
t_subprocess builtin_pwd(t_simple_cmd *cmd, t_minishell *app);
t_subprocess builtin_cd(t_simple_cmd *cmd, t_minishell *app);
t_subprocess builtin_export(t_simple_cmd *cmd, t_minishell *app);
t_subprocess builtin_exit(t_simple_cmd *cmd, t_minishell *app);
t_subprocess builtin_echo(t_simple_cmd *cmd, t_minishell *app);
t_subprocess builtin_env(t_simple_cmd *cmd, t_minishell *app);
t_subprocess builtin_unset(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type get_builtin(t_simple_cmd *cmd);
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds);
t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds, t_builtin_type type);
#endif // BUILTINS_H

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
/* Updated: 2025/04/29 15:10:35 by khais ### ########.fr */
/* Updated: 2025/04/29 15:47:27 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -98,6 +98,7 @@ static t_subprocess exec_external_cmd(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;
if (cmd == NULL)
return (PARENTPROCESS);
@ -111,7 +112,8 @@ t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
simple_cmd_execute_debug(cmd, app);
if (handle_redirections(cmd->redirections, app, &fds) == NULL)
return (PARENTPROCESS);
if (execute_builtin(cmd, app, &fds) == BUILTIN_INVALID)
type = get_builtin(cmd);
if (type != BUILTIN_INVALID)
return (execute_builtin(cmd, app, &fds, type));
return (exec_external_cmd(cmd, app, &fds));
return (PARENTPROCESS);
}

View file

@ -1595,4 +1595,11 @@ minishell: warning: here-document delimited by end-of-file (wanted `'hola'')
test
EOF
when_run <<EOF "exit in pipeline"
exit 10 | echo hi
EOF
expecting <<EOF
hi
EOF
finalize