mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
Signal: A basic version for the capture of signals.
This commit is contained in:
parent
837abc8361
commit
83d88681a2
2 changed files with 29 additions and 18 deletions
|
|
@ -6,15 +6,31 @@
|
|||
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stddef.h>
|
||||
# include <asm-generic/signal-defs.h>
|
||||
|
||||
# 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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue