gestion des signaux avec execve ok dans test
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
- [get root access 1](https://vitux.com/how-to-become-root-user-in-ubuntu-command-line-using-su-and-sudo/)
|
||||
- [get root access using sudo su](https://superuser.com/questions/408990/how-do-i-log-out-of-sudo-su)
|
||||
- [what is a tty](https://www.howtogeek.com/428174/what-is-a-tty-on-linux-and-how-to-use-the-tty-command/)
|
||||
- [really handling process](https://stackoverflow.com/questions/47489128/handling-signals-sigstp-sigcont-sigint-with-child-process)
|
||||
- [signal in a child process](https://stackoverflow.com/questions/55190460/using-signals-in-a-child-process)
|
||||
|
||||
```text
|
||||
| BASH | MINISHELL |
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/12 10:45:33 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/11/15 19:23:23 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -57,4 +57,8 @@ void *ft_resize_2d_arr(void *ptr, size_t add_nbr);
|
||||
void print_matrix(char **matrix, char *sep);
|
||||
t_list *ft_lstbeforelast(t_list *lst);
|
||||
|
||||
// signals.c
|
||||
void sigint_handler_interactiv(int signum);
|
||||
void sigint_handler_execution(int signum);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/11 09:54:40 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/11/15 20:21:59 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -57,12 +57,20 @@ void execute_cmd(char **envp, t_cmd **cmd_arr, t_all *c)
|
||||
int i;
|
||||
int argc;
|
||||
|
||||
// put signal handling for SIGINT to ignore so parent process will not activate signal_handling_executiv while childs are in process
|
||||
// c->signal_behaviour.sa_handler = SIG_IGN;
|
||||
// sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
signal(SIGINT, SIG_IGN);
|
||||
i = 0;
|
||||
while(cmd_arr[i])
|
||||
{
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
// activate singal handling for execution mode
|
||||
// c->signal_behaviour.sa_handler = sigint_handler_execution;
|
||||
// sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
signal(SIGINT, sigint_handler_execution);
|
||||
if (cmd_arr[i]->fd_in != 0)
|
||||
dup2(cmd_arr[i]->fd_in, STDIN_FILENO);
|
||||
if (cmd_arr[i]->fd_out != 1)
|
||||
@@ -74,9 +82,13 @@ void execute_cmd(char **envp, t_cmd **cmd_arr, t_all *c)
|
||||
while (cmd_arr[i]->argv[argc])
|
||||
argc++;
|
||||
cmd_arr[i]->builtin_command(argc, cmd_arr[i]->argv, c);
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
write(1, "1", 1);
|
||||
execve(cmd_arr[i]->argv[0], cmd_arr[i]->argv, envp);
|
||||
}
|
||||
}
|
||||
else
|
||||
close_fd(cmd_arr[i]);
|
||||
@@ -84,4 +96,7 @@ void execute_cmd(char **envp, t_cmd **cmd_arr, t_all *c)
|
||||
}
|
||||
// waitpid pour la derniere commande (pour '$?')
|
||||
while ((wpid = wait(&status)) > 0);
|
||||
// put signal handling for sigint back to the signal handler for interactiv mode
|
||||
c->signal_behaviour.sa_handler = sigint_handler_interactiv;
|
||||
sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
}
|
||||
|
||||
@@ -6,25 +6,31 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/23 18:56:53 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/10 13:44:09 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/11/15 20:08:26 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void sigint_handler(int signum)
|
||||
void sigint_handler_interactiv(int signum)
|
||||
{
|
||||
(void)signum;
|
||||
write(1, "\n", 1);
|
||||
rl_replace_line("", 1);
|
||||
rl_on_new_line();
|
||||
rl_redisplay();
|
||||
return ;
|
||||
}
|
||||
|
||||
void sigint_handler_execution(int signum)
|
||||
{
|
||||
(void)signum;
|
||||
write(1, "\n", 1);
|
||||
// exit(0);
|
||||
}
|
||||
|
||||
int set_signals_handling(struct sigaction *signal_behaviour)
|
||||
{
|
||||
signal_behaviour->sa_handler = sigint_handler;
|
||||
signal_behaviour->sa_handler = sigint_handler_interactiv;
|
||||
sigaction(SIGINT, signal_behaviour, NULL);
|
||||
signal_behaviour->sa_handler = SIG_IGN;
|
||||
sigaction(SIGQUIT, signal_behaviour, NULL);
|
||||
|
||||
@@ -27,6 +27,8 @@ int main(int argc, char *argv[], char *envp[])
|
||||
char *line_input;
|
||||
char *prompt;
|
||||
pid_t pid;
|
||||
char **argvtmp;
|
||||
int wstatus;
|
||||
|
||||
signal(SIGINT, handler_sigint); // signal handling for SINGINT while interactive mode
|
||||
line_input = NULL;
|
||||
@@ -43,7 +45,15 @@ int main(int argc, char *argv[], char *envp[])
|
||||
if (pid == 0)
|
||||
{
|
||||
signal(SIGINT, handler_sigint_execution); // start signal handling for SIGINT in child process
|
||||
if (!strncmp("echo", line_input, 4))
|
||||
if (!strncmp("sleep", line_input, 5))
|
||||
{
|
||||
argvtmp = malloc(sizeof(char*) * 3);
|
||||
argvtmp[0] = strdup("sleep");
|
||||
argvtmp[1] = strdup("2");
|
||||
argvtmp[2] = NULL;
|
||||
execve("/bin/sleep", argvtmp, envp);
|
||||
}
|
||||
else if (!strncmp("echo", line_input, 4))
|
||||
{
|
||||
write(1, "3", 2);
|
||||
sleep(1);
|
||||
@@ -68,7 +78,13 @@ int main(int argc, char *argv[], char *envp[])
|
||||
}
|
||||
}
|
||||
else
|
||||
wait(0);
|
||||
{
|
||||
wait(&wstatus);
|
||||
if (WIFEXITED(wstatus))
|
||||
write(1, "the child terminated normally\n", 30);
|
||||
if (WIFSIGNALED(wstatus))
|
||||
write(1, "the child was terminated by a signal\n", 37);
|
||||
}
|
||||
signal(SIGINT, handler_sigint); // restart signal handling for SIGINT because re-entring in interactive mode
|
||||
}
|
||||
else if (!line_input)
|
||||
|
||||
Reference in New Issue
Block a user