fix(fork): handle fork error in all places

This commit is contained in:
Khaïs COLIN 2025-04-30 14:06:38 +02:00
parent 4b08629bef
commit f8f3d8ccca
2 changed files with 18 additions and 2 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/04 19:50:42 by kcolin #+# #+# */
/* Updated: 2025/04/29 15:11:00 by kcolin ### ########.fr */
/* Updated: 2025/04/30 14:06:24 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@
#include "../simple_cmd/handle_redirections.h"
#include "../simple_cmd/std_fds.h"
#include <unistd.h>
#include <stdio.h>
t_subprocess group_cmd_execute(t_group_cmd *cmd, t_minishell *app)
{
@ -30,6 +31,12 @@ t_subprocess group_cmd_execute(t_group_cmd *cmd, t_minishell *app)
restore_std_fds(&fds);
return (SUBPROCESS);
}
if (pid < 0)
{
perror("minishell: fork");
app->last_return_value = 255;
return (PARENTPROCESS);
}
do_waitpid(app, pid);
return (PARENTPROCESS);
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by kcolin #+# #+# */
/* Updated: 2025/04/29 15:47:27 by kcolin ### ########.fr */
/* Updated: 2025/04/30 14:05:26 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -72,6 +72,13 @@ static t_simple_cmd *post_process_command(t_simple_cmd *cmd, t_minishell *app)
return (cmd);
}
static t_subprocess fork_fail(t_minishell *app)
{
perror("minishell: fork");
app->last_return_value = 255;
return (PARENTPROCESS);
}
static t_subprocess exec_external_cmd(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds)
{
@ -87,6 +94,8 @@ static t_subprocess exec_external_cmd(t_simple_cmd *cmd, t_minishell *app,
if (pid == 0)
return (execute_subprocess(exe, cmd, app, fds));
free(exe);
if (pid < 0)
return (fork_fail(app));
do_waitpid(app, pid);
}
restore_std_fds(fds);