diff --git a/src/sig/sig.c b/src/sig/sig.c new file mode 100644 index 0000000..494aed1 --- /dev/null +++ b/src/sig/sig.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sig.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jguelen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/20 10:26:05 by jguelen #+# #+# */ +/* Updated: 2025/02/20 18:03:06 by jguelen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "sig.h" + +// TODO Check the return values and implement handlers + +int set_interactive_mode_sig_handling(void) +{ + struct sigaction sig_act; + + ft_bzero(&sig_act); + sig_act.sa_handler = &sig_int_new_prompt; + if (sigemptyset(sig_act.sa_mask) == -1) + return (-1); + if (sigaction(SIGINT, &sig_act, NULL) == -1) + return (-1); + sig_act.sa_handler = SIG_IGN; + if (sigaction(SIGQUIT, &sig_act, NULL) == -1) + return (-1); + return (0); +} + +/* +** in case a SIGINT is sent by kill +** deals with a SIGQUIT the same way as above +*/ +int set_exec_mode_sig_handling(void) +{ + struct sigaction sig_act; + struct sigaction sig_hand; + + ft_bzero(&sig_act); + if (sigemptyset(sig_act.sa_mask) == -1) + return (-1); + sig_act.sa_sigaction = &ignoreif_sent_by_kill; //ignore if code == SI_USER + sig_act.sa_flags |= SA_SIGINFO; + if (sigaction(SIGINT, &sig_act, NULL) == -1) + return (-1); + ft_bzero(&sig_hand); + if (sigemptyset(sig_hand.sa_mask) == -1) + return (-1); + sig_hand.sa_handler = SIG_DFL; + if (sigaction(SIGQUIT, &sig_hand, NULL) == -1) + return (-1); + return (0); +} + +/* +** Can be used at the start of a child process. +** Check if necessary since execve is executed. +** @RETURN Return 0 i case of success, -1 in case of error and errno is set +** to indicate the error. +*/ +int set_default_sig_handling(void) +{ + struct sigaction sig; + int i; + + ft_bzero(&sig); + sig.sa_handler = SIG_DFL; + if (sigemptyset(sig.sa_mask) == -1) + return (-1); + i = 0; + while (i < 128) + { + if (sigaction(i, &sig, NULL) == -1) + return (-1); + i++; + } + return (0); +} diff --git a/src/sig/sig.h b/src/sig/sig.h new file mode 100644 index 0000000..6cfb533 --- /dev/null +++ b/src/sig/sig.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sig.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jguelen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/19 18:21:55 by jguelen #+# #+# */ +/* Updated: 2025/02/20 16:37:19 by jguelen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SIG_H +# define SIG_H + +# include +# include +# include + +int set_interactive_mode_sig_handling(void); +int set_exec_mode_sig_handling(void); +int set_default_sig_handling(void); + +#endif