From 83d88681a2df4b2c6984385a102d413f1c5c2f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gu=C3=A9len?= Date: Fri, 21 Feb 2025 15:10:46 +0100 Subject: [PATCH] Signal: A basic version for the capture of signals. --- src/sig/sig.c | 41 ++++++++++++++++++++++++----------------- src/sig/sig.h | 6 +++++- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/sig/sig.c b/src/sig/sig.c index 494aed1..2bd4b11 100644 --- a/src/sig/sig.c +++ b/src/sig/sig.c @@ -6,15 +6,31 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/20 10:26:05 by jguelen #+# #+# */ -/* Updated: 2025/02/20 18:03:06 by jguelen ### ########.fr */ +/* Updated: 2025/02/21 14:48:48 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ #include "sig.h" -// TODO Check the return values and implement handlers +int g_signum = 0; -int set_interactive_mode_sig_handling(void) +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) { struct sigaction sig_act; @@ -30,27 +46,18 @@ int set_interactive_mode_sig_handling(void) 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) +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_sigaction = &ignoreif_sent_by_kill; 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) + if (sigaction(SIGQUIT, &sig_act, NULL) == -1) return (-1); return (0); } @@ -61,7 +68,7 @@ int set_exec_mode_sig_handling(void) ** @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) +int set_default_sig_handling(void) { struct sigaction sig; int i; @@ -71,7 +78,7 @@ int set_default_sig_handling(void) if (sigemptyset(sig.sa_mask) == -1) return (-1); i = 0; - while (i < 128) + while (i < NSIG) { if (sigaction(i, &sig, NULL) == -1) return (-1); diff --git a/src/sig/sig.h b/src/sig/sig.h index 6cfb533..c46fe50 100644 --- a/src/sig/sig.h +++ b/src/sig/sig.h @@ -6,7 +6,7 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/19 18:21:55 by jguelen #+# #+# */ -/* Updated: 2025/02/20 16:37:19 by jguelen ### ########.fr */ +/* Updated: 2025/02/21 13:52:47 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,10 @@ # include # include +# define NSIG 64 + +extern int g_signum; + int set_interactive_mode_sig_handling(void); int set_exec_mode_sig_handling(void); int set_default_sig_handling(void);