sig: Starting to deal with signals

This commit is contained in:
Jérôme Guélen 2025-02-20 18:03:38 +01:00 committed by Khaïs COLIN
parent a50b2f6d74
commit 837abc8361
2 changed files with 105 additions and 0 deletions

81
src/sig/sig.c Normal file
View file

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sig.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

24
src/sig/sig.h Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sig.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#ifndef SIG_H
# define SIG_H
# include <signal.h>
# include <stddef.h>
# include <asm-generic/signal-defs.h>
int set_interactive_mode_sig_handling(void);
int set_exec_mode_sig_handling(void);
int set_default_sig_handling(void);
#endif