mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
fix(pipe_cmd): check for fork failures
This commit is contained in:
parent
a8c3473b1a
commit
4b08629bef
1 changed files with 59 additions and 28 deletions
|
|
@ -6,28 +6,17 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/11 12:01:29 by kcolin #+# #+# */
|
/* Created: 2025/04/11 12:01:29 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/04/29 15:11:23 by kcolin ### ########.fr */
|
/* Updated: 2025/04/30 11:49:59 by kcolin ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../ft_errno.h"
|
||||||
#include "connec_pipe_cmd_execute.h"
|
#include "connec_pipe_cmd_execute.h"
|
||||||
#include "../common/do_waitpid.h"
|
#include "../common/do_waitpid.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "../cmd/cmd_execute.h"
|
#include "../cmd/cmd_execute.h"
|
||||||
|
|
||||||
static void pipe_do_close(int pipefd[2])
|
|
||||||
{
|
|
||||||
close(pipefd[0]);
|
|
||||||
close(pipefd[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dup_and_close(int fd, int fd2, int pipefd[2])
|
|
||||||
{
|
|
||||||
dup2(fd, fd2);
|
|
||||||
pipe_do_close(pipefd);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int do_pipe(int pipefd[2], t_minishell *app)
|
static int do_pipe(int pipefd[2], t_minishell *app)
|
||||||
{
|
{
|
||||||
if (pipe(pipefd) < 0)
|
if (pipe(pipefd) < 0)
|
||||||
|
|
@ -39,11 +28,52 @@ static int do_pipe(int pipefd[2], t_minishell *app)
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void close_and_wait(t_minishell *app, int pid1, int pid2, int pipefd[2])
|
static void fork_failure(t_minishell *app, int pipefd[2])
|
||||||
{
|
{
|
||||||
pipe_do_close(pipefd);
|
perror("minishell: fork");
|
||||||
do_waitpid(app, pid1);
|
close(pipefd[0]);
|
||||||
do_waitpid(app, pid2);
|
close(pipefd[1]);
|
||||||
|
app->last_return_value = 255;
|
||||||
|
ft_errno(FT_EERRNO);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** return the pid returned by fork. values greater than 0 indicate parentprocess
|
||||||
|
*/
|
||||||
|
static int exec_pid1(t_minishell *app, int pipefd[2], t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int pid1;
|
||||||
|
|
||||||
|
pid1 = fork();
|
||||||
|
if (pid1 < 0)
|
||||||
|
return (fork_failure(app, pipefd), 1);
|
||||||
|
if (pid1 == 0)
|
||||||
|
{
|
||||||
|
dup2(pipefd[1], STDOUT_FILENO);
|
||||||
|
close(pipefd[0]);
|
||||||
|
close(pipefd[1]);
|
||||||
|
cmd_execute(cmd, app);
|
||||||
|
return (pid1);
|
||||||
|
}
|
||||||
|
return (pid1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int exec_pid2(t_minishell *app, int pipefd[2], t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int pid1;
|
||||||
|
|
||||||
|
pid1 = fork();
|
||||||
|
if (pid1 < 0)
|
||||||
|
return (fork_failure(app, pipefd), 1);
|
||||||
|
if (pid1 == 0)
|
||||||
|
{
|
||||||
|
dup2(pipefd[0], STDIN_FILENO);
|
||||||
|
close(pipefd[0]);
|
||||||
|
close(pipefd[1]);
|
||||||
|
cmd_execute(cmd, app);
|
||||||
|
return (pid1);
|
||||||
|
}
|
||||||
|
return (pid1);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_subprocess connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
|
t_subprocess connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
|
||||||
|
|
@ -54,20 +84,21 @@ t_subprocess connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
|
||||||
|
|
||||||
if (do_pipe(pipefd, app) < 0)
|
if (do_pipe(pipefd, app) < 0)
|
||||||
return (PARENTPROCESS);
|
return (PARENTPROCESS);
|
||||||
pid1 = fork();
|
ft_errno(FT_ESUCCESS);
|
||||||
|
pid1 = exec_pid1(app, pipefd, cmd->first);
|
||||||
|
if (pid1 > 0 && ft_errno_get() != FT_ESUCCESS)
|
||||||
|
return (PARENTPROCESS);
|
||||||
if (pid1 == 0)
|
if (pid1 == 0)
|
||||||
{
|
|
||||||
dup_and_close(pipefd[1], STDOUT_FILENO, pipefd);
|
|
||||||
cmd_execute(cmd->first, app);
|
|
||||||
return (SUBPROCESS);
|
return (SUBPROCESS);
|
||||||
}
|
ft_errno(FT_ESUCCESS);
|
||||||
pid2 = fork();
|
pid2 = exec_pid2(app, pipefd, cmd->second);
|
||||||
|
if (pid2 > 0 && ft_errno_get() != FT_ESUCCESS)
|
||||||
|
return (PARENTPROCESS);
|
||||||
if (pid2 == 0)
|
if (pid2 == 0)
|
||||||
{
|
|
||||||
dup_and_close(pipefd[0], STDIN_FILENO, pipefd);
|
|
||||||
cmd_execute(cmd->second, app);
|
|
||||||
return (SUBPROCESS);
|
return (SUBPROCESS);
|
||||||
}
|
close(pipefd[0]);
|
||||||
close_and_wait(app, pid1, pid2, pipefd);
|
close(pipefd[1]);
|
||||||
|
do_waitpid(app, pid1);
|
||||||
|
do_waitpid(app, pid2);
|
||||||
return (PARENTPROCESS);
|
return (PARENTPROCESS);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue