minishell/src/sig/sig.c

89 lines
2.4 KiB
C
Raw Normal View History

2025-02-20 18:03:38 +01:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sig.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/20 10:26:05 by jguelen #+# #+# */
/* Updated: 2025/02/21 14:48:48 by jguelen ### ########.fr */
2025-02-20 18:03:38 +01:00
/* */
/* ************************************************************************** */
#include "sig.h"
int g_signum = 0;
2025-02-20 18:03:38 +01:00
static void sig_interactive(int signum)
{
g_signum = signum;
}
/*
** Ignores the signal if it was sent by a kill command.
*/
static void ignoreif_sent_by_kill(int signum, siginfo_t *siginfo, void *context)
{
(void *)context;
if (siginfo->si_code == SI_USER)
return ;
g_signum = signum;
}
int set_interactive_mode_sig_handling(void)
2025-02-20 18:03:38 +01:00
{
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);
}
int set_exec_mode_sig_handling(void)
2025-02-20 18:03:38 +01:00
{
struct sigaction sig_act;
ft_bzero(&sig_act);
if (sigemptyset(sig_act.sa_mask) == -1)
return (-1);
sig_act.sa_sigaction = &ignoreif_sent_by_kill;
2025-02-20 18:03:38 +01:00
sig_act.sa_flags |= SA_SIGINFO;
if (sigaction(SIGINT, &sig_act, NULL) == -1)
return (-1);
if (sigaction(SIGQUIT, &sig_act, NULL) == -1)
2025-02-20 18:03:38 +01:00
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)
2025-02-20 18:03:38 +01:00
{
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 < NSIG)
2025-02-20 18:03:38 +01:00
{
if (sigaction(i, &sig, NULL) == -1)
return (-1);
i++;
}
return (0);
}