Debian Codegraph @

libmowgli-2/src/libmowgli/base/mowgli_signal.c

raw symbols SCIP
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * mowgli_signal.c: Safe signal handling.
4 *
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #ifndef _WIN32
25
26 #include "mowgli.h"
27
28 #if defined(__linux__) && defined(__GNUC__) && defined(__STRICT_ANSI__)
29 # error GCC/Linux in -std=c99 mode will not compile mowgli_signal; use -std=gnu99 instead
30 #endif
31
32 static mowgli_signal_handler_t
33 mowgli_signal_install_handler_full(int signum, mowgli_signal_handler_t handler, int *sigtoblock, size_t sigtoblocksize)
34 {
35 struct sigaction action, old_action;
36
37 size_t i;
38
39 action.sa_handler = handler;
40 action.sa_flags = SA_RESTART;
41
42 sigemptyset(&action.sa_mask);
43
44 for (i = 0; i < sigtoblocksize; i++)
45 sigaddset(&action.sa_mask, sigtoblock[i]);
46
47 if (sigaction(signum, &action, &old_action) == -1)
48 {
49 mowgli_log("Failed to install signal handler for signal %d", signum);
50 return NULL;
51 }
52
53 return old_action.sa_handler;
54 }
55
56 /*
57 * A version of signal(2) that works more reliably across different
58 * platforms.
59 *
60 * It restarts interrupted system calls, does not reset the handler,
61 * and blocks the same signal from within the handler.
62 */
63 mowgli_signal_handler_t
64 mowgli_signal_install_handler(int signum, mowgli_signal_handler_t handler)
65 {
66 return mowgli_signal_install_handler_full(signum, handler, NULL, 0);
67 }
68
69 #endif