sighandler.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /*! \file
3  * \brief Signal handlers
4  *
5  * Copyright (c) 2012-2020 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 posix_sig_atomic_t sighandler_abort = 0;
40 
41 
42 /* ========================================================================== */
43 /* Signal handler */
44 
45 static void sighandler_handler(int signum)
46 {
47  /* Set global flag to schedule save config & exit */
48  sighandler_abort = 1;
49 
50  /* Note: The UI wakes up automatically due to the signal */
51 }
52 
53 
54 /* ========================================================================== */
55 /*! \brief Install signal handlers
56  *
57  * Ignored signals: SIGHUP, SIGPIPE
58  *
59  * Signals that trigger save exit: SIGTERM, SIGQUIT, SIGINT
60  *
61  * \return
62  * - 0 on success
63  * - Negative value on error
64  */
65 
67 {
68  int res = 0;
69  int rv;
70  struct_posix_sigaction sa;
71 
72  posix_sigemptyset(&sa.sa_mask);
73  sa.sa_flags = 0;
74 
75  /* Signals mapped to 'sighandler_handler()' */
76  sa.sa_handler = sighandler_handler;
77  rv = posix_sigaction(POSIX_SIGINT, &sa, (struct_posix_sigaction*) NULL);
78  if(!rv)
79  {
80  rv = posix_sigaction(POSIX_SIGQUIT, &sa, (struct_posix_sigaction*) NULL);
81  }
82  if(!rv)
83  {
84  rv = posix_sigaction(POSIX_SIGTERM, &sa, (struct_posix_sigaction*) NULL);
85  }
86 
87  /* Signals that should be ignored */
88  sa.sa_handler = POSIX_SIG_IGN;
89  if(!rv)
90  {
91  rv = posix_sigaction(POSIX_SIGHUP, &sa, (struct_posix_sigaction*) NULL);
92  }
93  if(!rv)
94  {
95  rv = posix_sigaction(POSIX_SIGPIPE, &sa, (struct_posix_sigaction*) NULL);
96  }
97 
98  /* Check for error */
99  if(rv)
100  {
101  PRINT_ERROR("Installation of signal handlers failed");
102  res = -1;
103  }
104 
105  return(res);
106 }
107 
108 
109 /* ========================================================================== */
110 /*! \brief Prepare for \c exec()
111  *
112  * Restore default state for ignored signals.
113  *
114  * \return
115  * - 0 on success
116  * - Negative value on error
117  */
118 
120 {
121  int res = 0;
122  int rv;
123  struct_posix_sigaction sa;
124 
125  posix_sigemptyset(&sa.sa_mask);
126  sa.sa_flags = 0;
127 
128  /* Signals that should be ignored */
129  sa.sa_handler = POSIX_SIG_DFL;
130  rv = posix_sigaction(POSIX_SIGHUP, &sa, (struct_posix_sigaction*) NULL);
131  if(!rv)
132  {
133  rv = posix_sigaction(POSIX_SIGPIPE, &sa, (struct_posix_sigaction*) NULL);
134  }
135 
136  /* Check for error */
137  if(rv)
138  {
139  PRINT_ERROR("Restore of default signal handlers failed");
140  res = -1;
141  }
142 
143  return(res);
144 }
145 
146 
147 /* ========================================================================== */
148 /*! \brief Check abort flag
149  *
150  * \return
151  * - False if abort flag is not set
152  * - True if abort flag is set
153  */
154 
156 {
157  return((int) sighandler_abort);
158 }
159 
160 
161 /*! @} */
162 
163 /* EOF */
sighandler_install
int sighandler_install(void)
Install signal handlers.
Definition: sighandler.c:66
sighandler_check_abort
int sighandler_check_abort(void)
Check abort flag.
Definition: sighandler.c:155
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:119

Generated at 2024-04-27 using  doxygen