sighandler.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /*! \file
3  * \brief Signal handlers
4  *
5  * Copyright (c) 2012-2024 by the developers. See the LICENSE file for details.
6  *
7  * If nothing else is specified, function return zero to indicate success
8  * and a negative value to indicate an error.
9  */
10 
11 
12 /* ========================================================================== */
13 /* Include headers */
14 
15 #include "posix.h" /* Include this first beause of feature test macro */
16 
17 #include "config.h"
18 #include "main.h"
19 #include "sighandler.h"
20 #include "ui.h"
21 
22 
23 /* ========================================================================== */
24 /*! \addtogroup MAIN */
25 /*! @{ */
26 
27 
28 /* ========================================================================== */
29 /* Constants */
30 
31 /*! \brief Message prefix for MAIN module */
32 #define MAIN_ERR_PREFIX "MAIN: "
33 
34 
35 /* ========================================================================== */
36 /* Variables */
37 
38 /*! \brief Program abort request flag */
39 static volatile api_posix_sig_atomic_t sighandler_abort = 0;
40 
41 
42 /* ========================================================================== */
43 /* Signal handler */
44 
45 static void sighandler_handler(int signum)
46 {
47  (void) signum;
48 
49  /* Set global flag to schedule save config & exit */
50  sighandler_abort = 1;
51 
52  /* Note: The UI wakes up automatically due to the signal */
53 }
54 
55 
56 /* ========================================================================== */
57 /*! \brief Install signal handlers
58  *
59  * Ignored signals: SIGHUP, SIGPIPE
60  *
61  * Signals that trigger save exit: SIGTERM, SIGQUIT, SIGINT
62  *
63  * \return
64  * - 0 on success
65  * - Negative value on error
66  */
67 
69 {
70  int res = 0;
71  int rv;
72  api_posix_struct_sigaction sa;
73 
74  api_posix_sigemptyset(&sa.sa_mask);
75  sa.sa_flags = 0;
76 
77  /* Signals mapped to 'sighandler_handler()' */
78  sa.sa_handler = sighandler_handler;
79  rv = api_posix_sigaction(API_POSIX_SIGINT, &sa,
80  (api_posix_struct_sigaction*) NULL);
81  if(!rv)
82  {
83  rv = api_posix_sigaction(API_POSIX_SIGQUIT, &sa,
84  (api_posix_struct_sigaction*) NULL);
85  }
86  if(!rv)
87  {
88  rv = api_posix_sigaction(API_POSIX_SIGTERM, &sa,
89  (api_posix_struct_sigaction*) NULL);
90  }
91 
92  /* Signals that should be ignored */
93  sa.sa_handler = API_POSIX_SIG_IGN;
94  if(!rv)
95  {
96  rv = api_posix_sigaction(API_POSIX_SIGHUP, &sa,
97  (api_posix_struct_sigaction*) NULL);
98  }
99  if(!rv)
100  {
101  rv = api_posix_sigaction(API_POSIX_SIGPIPE, &sa,
102  (api_posix_struct_sigaction*) NULL);
103  }
104 
105  /* Check for error */
106  if(rv)
107  {
108  PRINT_ERROR("Installation of signal handlers failed");
109  res = -1;
110  }
111 
112  return(res);
113 }
114 
115 
116 /* ========================================================================== */
117 /*! \brief Prepare for \c exec()
118  *
119  * Restore default state for ignored signals.
120  *
121  * \return
122  * - 0 on success
123  * - Negative value on error
124  */
125 
127 {
128  int res = 0;
129  int rv;
130  api_posix_struct_sigaction sa;
131 
132  api_posix_sigemptyset(&sa.sa_mask);
133  sa.sa_flags = 0;
134 
135  /* Signals that should be ignored */
136  sa.sa_handler = API_POSIX_SIG_DFL;
137  rv = api_posix_sigaction(API_POSIX_SIGHUP, &sa,
138  (api_posix_struct_sigaction*) NULL);
139  if(!rv)
140  {
141  rv = api_posix_sigaction(API_POSIX_SIGPIPE, &sa,
142  (api_posix_struct_sigaction*) NULL);
143  }
144 
145  /* Check for error */
146  if(rv)
147  {
148  PRINT_ERROR("Restore of default signal handlers failed");
149  res = -1;
150  }
151 
152  return(res);
153 }
154 
155 
156 /* ========================================================================== */
157 /*! \brief Check abort flag
158  *
159  * \return
160  * - False if abort flag is not set
161  * - True if abort flag is set
162  */
163 
165 {
166  return((int) sighandler_abort);
167 }
168 
169 
170 /*! @} */
171 
172 /* EOF */
sighandler_install
int sighandler_install(void)
Install signal handlers.
Definition: sighandler.c:68
sighandler_check_abort
int sighandler_check_abort(void)
Check abort flag.
Definition: sighandler.c:164
PRINT_ERROR
#define PRINT_ERROR(s)
Prepend module prefix and print error message.
Definition: main.h:19
sighandler_exec_prepare
int sighandler_exec_prepare(void)
Prepare for exec()
Definition: sighandler.c:126

Generated at 2026-01-27 using  doxygen