1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
* Copyright (C) 2008-2016 Andrea Di Pasquale <spikey.it@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* $ArpON: sig.c,v 3.0-ng 01/29/2016 03:06:38 spikey Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <pthread.h>
#include "config.h"
#include "env.h"
#include "exit.h"
#include "msg.h"
#include "sig.h"
/*
* Integer signal to string.
*/
#define SIG_ITOA(x) x == SIGINT ? "interrupt signal" : \
x == SIGTERM ? "termination signal" : \
x == SIGHUP ? "hangup signal" : \
x == SIGSEGV ? "segmentation fault signal" : \
x == SIGBUS ? "bus error signal" : "unknown signal"
/*
* Function prototypes not exported.
*/
static void sig_init(void);
static void sig_destroy(void);
static void sig_setblockmask(void);
static void sig_loop(char **argv, char **envp);
static void sig_handleterm(int sig);
static void sig_handlehup(char **argv, char **envp);
static void sig_handlebug(int sig);
/*
* Initialize the signal mask.
*/
static sigset_t mask = {{0}};
/*
* Initialize and handle the signal.
*/
void
sig_handle(char **argv, char **envp)
{
MSG_DEBUG("Start signal handle");
/* Initialize the signal mask. */
sig_init();
/* Set and block the signal mask. */
sig_setblockmask();
/* Handle the signal masked. */
sig_loop(argv, envp);
}
/*
* Initialize the signal mask.
*/
static void
sig_init(void)
{
/* Initialize the signal mask. */
if (sigemptyset(&mask) < 0) {
MSG_ERROR("%s", strerror(errno));
exit(EXIT_FAILURE);
}
MSG_DEBUG("Initialize signal mask successful");
/* Push sig_destroy() to be called on exit_cleanup(). */
exit_push(sig_destroy, "sig_destroy");
}
/*
* Destroy and unblock the signal mask.
*/
static void
sig_destroy(void)
{
do {
/* Unblock the signal mask. */
if (pthread_sigmask(SIG_UNBLOCK, &mask, NULL) != 0)
break;
/* Destroy the signal mask. */
if (sigemptyset(&mask) < 0)
break;
MSG_DEBUG("Unblock signal mask successful");
return;
} while (0);
MSG_ERROR("%s", strerror(errno));
exit(EXIT_FAILURE);
}
/*
* Set and block the value of the signal mask.
*/
static void
sig_setblockmask(void)
{
do {
/* Add the interrupt signal in the signal mask. */
if (sigaddset(&mask, SIGINT) < 0)
break;
MSG_DEBUG("Add %s to signal mask..", SIG_ITOA(SIGINT));
/* Add the termination signal in the signal mask. */
if (sigaddset(&mask, SIGTERM) < 0)
break;
MSG_DEBUG("Add %s to signal mask..", SIG_ITOA(SIGTERM));
/* Add the hangup signal in the signal mask. */
if (sigaddset(&mask, SIGHUP) < 0)
break;
MSG_DEBUG("Add %s to signal mask..", SIG_ITOA(SIGHUP));
/* Add the segmentation fault signal in the signal mask. */
if (sigaddset(&mask, SIGSEGV) < 0)
break;
MSG_DEBUG("Add %s to signal mask..", SIG_ITOA(SIGSEGV));
/* Add the bus error signal in the signal mask. */
if (sigaddset(&mask, SIGBUS) < 0)
break;
MSG_DEBUG("Add %s to signal mask...", SIG_ITOA(SIGBUS));
/* Block the signal mask. */
if (pthread_sigmask(SIG_BLOCK, &mask, NULL) != 0)
break;
MSG_DEBUG("Block signal mask successful");
return;
} while (0);
MSG_ERROR("%s", strerror(errno));
exit(EXIT_FAILURE);
}
/*
* Handle the signal masked.
*/
static void
sig_loop(char **argv, char **envp)
{
/* Loop until a signal is caught. */
while (1) {
int sig;
/* Suspend the execution of the main thread until a signal is caught. */
if (sigwait(&mask, &sig) != 0) {
MSG_ERROR("%s", strerror(errno));
exit(EXIT_FAILURE);
}
/* Handle the signal caught of the main thread. */
switch(sig) {
case SIGINT:
/* Remove ^C symbol from the output terminal. */
printf("\r");
/* FALLTHRU */
case SIGTERM:
MSG_DEBUG("Caught %s (%d)..", SIG_ITOA(sig), sig);
/* Handle the interrupt or termination signal. */
sig_handleterm(sig);
/* Never reaches here. */
break;
case SIGHUP:
MSG_DEBUG("Caught %s (%d)..", SIG_ITOA(sig), sig);
/* Handle the hangup signal. */
sig_handlehup(argv, envp);
/* Never reaches here. */
break;
case SIGSEGV:
case SIGBUS:
MSG_DEBUG("Caught %s (%d)..", SIG_ITOA(sig), sig);
/* Handle the segmentation fault or bus error signal. */
sig_handlebug(sig);
/* Never reaches here. */
break;
default:
MSG_DEBUG("Caught no valid %s (%d)..", SIG_ITOA(sig), sig);
/* Ignore the no valid signal. */
break;
}
}
}
/*
* Handle the interrupt or termination signal.
*/
static void
sig_handleterm(int sig)
{
MSG_DEBUG("Handle the %s..", SIG_ITOA(sig));
/* Print the correct termination name. */
switch (sig) {
case SIGINT:
MSG_INFO("Interrupt requested, quitting now.");
break;
/* SIGTERM. */
default:
MSG_INFO("Termination requested, quitting now.");
break;
}
/* Cleanup and exit. */
exit_cleanup(true);
}
/*
* Handle the hangup signal.
*/
static void
sig_handlehup(char **argv, char **envp)
{
const char *path = NULL;
MSG_DEBUG("Handle the %s..", SIG_ITOA(SIGHUP));
MSG_INFO("Hangup requested, rebooting now...");
/* Get the environment binary path file. */
path = env_getpath(*(argv + 0));
MSG_DEBUG("Re-exec the current process from %s binary path file..", path);
/* Cleanup without exit. */
exit_cleanup(false);
/* Re-exec the current process. */
if (execve(path, argv, envp) < 0) {
MSG_ERROR("%s", strerror(errno));
exit(EXIT_FAILURE);
}
}
/*
* Handle the segmentation fault or bus error signal.
*/
static void
sig_handlebug(int sig)
{
MSG_DEBUG("Handle the %s..", SIG_ITOA(sig));
/* Print the correct bug name. */
switch (sig) {
case SIGSEGV:
MSG_BUG("Ops... SEGMENTATION FAULT!");
break;
/* SIGBUS. */
default:
MSG_BUG("Ops... BUS ERROR!");
break;
}
MSG_BUG("Please open with a browser web the documentation: %s", DOC_FILE);
MSG_BUG("Follow the steps explained in the \"Runtime bugs\" section.");
MSG_BUG("Thank you so much and have a nice day!");
/* Exit immediately. */
exit(EXIT_FAILURE);
}
/*
* EOF
*
* vim:ts=4:expandtab
*/
|