2025-02-20 18:03:38 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* sig.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
2025-05-02 12:21:05 +02:00
|
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
2025-02-20 18:03:38 +01:00
|
|
|
/* +#+#+#+#+#+ +#+ */
|
2025-05-02 12:21:05 +02:00
|
|
|
/* Created: 2025/05/02 12:16:37 by kcolin #+# #+# */
|
|
|
|
|
/* Updated: 2025/05/02 12:18:17 by kcolin ### ########.fr */
|
2025-02-20 18:03:38 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include "sig.h"
|
2025-04-04 14:25:39 +02:00
|
|
|
#include "signal.h"
|
2025-04-17 09:44:04 +02:00
|
|
|
#include "sig_handlers.h"
|
2025-02-20 18:03:38 +01:00
|
|
|
|
2025-02-21 17:03:41 +01:00
|
|
|
/*
|
|
|
|
|
** g_signum exists to store the value of a signal if relevant for later
|
|
|
|
|
** processing.
|
|
|
|
|
** NOTE: g_signum starts at 0 and should be set back to 0 after the information
|
|
|
|
|
** it stores has been properly processed.
|
|
|
|
|
*/
|
2025-02-21 15:10:46 +01:00
|
|
|
int g_signum = 0;
|
2025-02-20 18:03:38 +01:00
|
|
|
|
2025-02-21 17:03:41 +01:00
|
|
|
/*
|
|
|
|
|
** Catches SIGINT and SIGQUIT.
|
|
|
|
|
** Set to ignore SIGQUIT and catch SIGINT to set g_signum for
|
|
|
|
|
** further processing when in interactive mode.
|
|
|
|
|
*/
|
2025-02-21 15:10:46 +01:00
|
|
|
int set_interactive_mode_sig_handling(void)
|
2025-02-20 18:03:38 +01:00
|
|
|
{
|
|
|
|
|
struct sigaction sig_act;
|
|
|
|
|
|
2025-02-21 15:35:33 +01:00
|
|
|
ft_bzero(&sig_act, sizeof(struct sigaction));
|
|
|
|
|
sig_act.sa_handler = &sig_interactive;
|
2025-04-04 14:25:39 +02:00
|
|
|
sig_act.sa_flags |= SA_RESTART;
|
2025-02-21 15:35:33 +01:00
|
|
|
if (sigemptyset(&sig_act.sa_mask) == -1)
|
2025-02-20 18:03:38 +01:00
|
|
|
return (-1);
|
|
|
|
|
if (sigaction(SIGINT, &sig_act, NULL) == -1)
|
|
|
|
|
return (-1);
|
2025-05-02 12:21:05 +02:00
|
|
|
sig_act.sa_handler = SIG_IGN;
|
2025-02-20 18:03:38 +01:00
|
|
|
if (sigaction(SIGQUIT, &sig_act, NULL) == -1)
|
|
|
|
|
return (-1);
|
|
|
|
|
return (0);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 17:03:41 +01:00
|
|
|
/*
|
|
|
|
|
** Set to ignore SIGINT and SIGQUIT signals if they are generated through
|
|
|
|
|
** a call to kill when the program is currently executing commands.
|
|
|
|
|
** Otherwise set g_signum to the number corresponding to the caught signal.
|
|
|
|
|
*/
|
2025-02-21 15:10:46 +01:00
|
|
|
int set_exec_mode_sig_handling(void)
|
2025-02-20 18:03:38 +01:00
|
|
|
{
|
|
|
|
|
struct sigaction sig_act;
|
|
|
|
|
|
2025-02-21 15:35:33 +01:00
|
|
|
ft_bzero(&sig_act, sizeof(struct sigaction));
|
|
|
|
|
if (sigemptyset(&sig_act.sa_mask) == -1)
|
2025-02-20 18:03:38 +01:00
|
|
|
return (-1);
|
2025-04-04 16:55:21 +02:00
|
|
|
sig_act.sa_sigaction = &sig_cmd;
|
2025-02-20 18:03:38 +01:00
|
|
|
sig_act.sa_flags |= SA_SIGINFO;
|
2025-04-04 16:55:21 +02:00
|
|
|
sig_act.sa_flags |= SA_RESTART;
|
2025-02-20 18:03:38 +01:00
|
|
|
if (sigaction(SIGINT, &sig_act, NULL) == -1)
|
|
|
|
|
return (-1);
|
2025-02-21 15:10:46 +01:00
|
|
|
if (sigaction(SIGQUIT, &sig_act, NULL) == -1)
|
2025-02-20 18:03:38 +01:00
|
|
|
return (-1);
|
|
|
|
|
return (0);
|
|
|
|
|
}
|
2025-04-17 09:44:04 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Set to ignore SIGINT and SIGQUIT signals if they are generated through
|
|
|
|
|
** a call to kill when the program is currently executing commands.
|
|
|
|
|
** Otherwise set g_signum to the number corresponding to the caught signal.
|
|
|
|
|
*/
|
|
|
|
|
int set_here_doc_mode_sig_handling(void)
|
|
|
|
|
{
|
|
|
|
|
struct sigaction sig_act;
|
|
|
|
|
|
|
|
|
|
ft_bzero(&sig_act, sizeof(struct sigaction));
|
|
|
|
|
if (sigemptyset(&sig_act.sa_mask) == -1)
|
|
|
|
|
return (-1);
|
|
|
|
|
sig_act.sa_sigaction = &sig_cmd;
|
|
|
|
|
sig_act.sa_flags |= SA_SIGINFO;
|
|
|
|
|
if (sigaction(SIGINT, &sig_act, NULL) == -1)
|
|
|
|
|
return (-1);
|
|
|
|
|
sig_act.sa_handler = SIG_IGN;
|
|
|
|
|
sig_act.sa_flags = 0;
|
|
|
|
|
if (sigaction(SIGQUIT, &sig_act, NULL) == -1)
|
|
|
|
|
return (-1);
|
|
|
|
|
return (0);
|
|
|
|
|
}
|