CO-CODE Hugo-Luke

+ signals handling adjusted
+ wait_subshell() with last_exit_status
+ miscs
This commit is contained in:
LuckyLaszlo
2021-11-17 01:35:06 +01:00
parent 965cf99ca5
commit 26993144cc
8 changed files with 185 additions and 128 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/16 08:42:01 by lperrey ### ########.fr */
/* Updated: 2021/11/17 01:08:38 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,9 +18,10 @@
int pipeline(t_all *c);
int open_pipes(t_cmd *pipeline[]);
int pipeline_access_cmd(t_cmd *pipeline[], char *path[]);
void pipeline_exec(t_cmd *pipeline[], t_all *c);
pid_t pipeline_exec(t_cmd *pipeline[], t_all *c);
int cmd_exec_in_subshell(t_cmd *cmd, t_all *c);
int simple_cmd_builtin(t_cmd *cmd, t_all *c);
void wait_subshell(pid_t last_cmd_pid, int *last_exit_status);
int exec_cmd_line(t_all *c)
{
@@ -45,9 +46,8 @@ int pipeline(t_all *c)
}
else
{
pipeline_exec(c->cmd_arr, c);
wait_subshell(pipeline_exec(c->cmd_arr, c), &c->last_exit_status);
free_pipeline(&c->cmd_arr);
// TODO wait process here
}
return (1);
}
@@ -95,7 +95,7 @@ int simple_cmd_builtin(t_cmd *cmd, t_all *c)
// TODO : Change exit status as in documentation :
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02
void pipeline_exec(t_cmd *pipeline[], t_all *c)
pid_t pipeline_exec(t_cmd *pipeline[], t_all *c)
{
int i;
int ret;
@@ -103,7 +103,7 @@ void pipeline_exec(t_cmd *pipeline[], t_all *c)
i = 0;
while (pipeline[i])
{
if (pipeline[i]->pid == 0) // Pid var as error mark, WIP
if (!pipeline[i]->error)
{
ret = cmd_exec_in_subshell(pipeline[i], c);
if (ret != EXIT_SUCCESS)
@@ -111,6 +111,49 @@ void pipeline_exec(t_cmd *pipeline[], t_all *c)
}
i++;
}
close_pipeline_fd(c->cmd_arr);
i -= 1;
if (pipeline[i]->error)
c->last_exit_status = pipeline[i]->error;
return (pipeline[i]->pid);
}
int handle_wait_error(void)
{
if (errno == ECHILD)
return (-1);
else if (errno == EINTR)
return (0);
else
perror("wait()");
return (-1);
}
void wait_subshell(pid_t last_cmd_pid, int *last_exit_status)
{
int wstatus;
int ret;
//wstatus = 0;
if (last_cmd_pid > 0)
{
if (waitpid(last_cmd_pid, &wstatus, 0) == -1)
perror("waitpid()");
if (WIFEXITED(wstatus))
*last_exit_status = WEXITSTATUS(wstatus);
}
ret = 0;
while (ret != -1)
{
ret = wait(&wstatus);
if (ret == -1)
ret = handle_wait_error();
}
if (WIFSIGNALED(wstatus))
{
write(STDIN_FILENO, "\n", 1);
*last_exit_status = 128 + WTERMSIG(wstatus);
}
}
int cmd_exec_in_subshell(t_cmd *cmd, t_all *c)
@@ -120,6 +163,8 @@ int cmd_exec_in_subshell(t_cmd *cmd, t_all *c)
perror("fork()");
if (cmd->pid == 0)
{
c->signal_behaviour.sa_handler = SIG_DFL;
sigaction(SIGINT, &c->signal_behaviour, NULL);
if (cmd->fd_in != STDIN_FILENO)
if (dup2(cmd->fd_in, STDIN_FILENO) == -1)
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));