/src/openvswitch/lib/signals.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2008, 2009, 2011, 2012, 2013 Nicira, Inc. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at: |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include <config.h> |
18 | | #include "signals.h" |
19 | | #include <errno.h> |
20 | | #include <limits.h> |
21 | | #include <signal.h> |
22 | | #include <stdlib.h> |
23 | | #include <unistd.h> |
24 | | #include "openvswitch/poll-loop.h" |
25 | | #include "socket-util.h" |
26 | | #include "openvswitch/type-props.h" |
27 | | #include "util.h" |
28 | | #include "openvswitch/vlog.h" |
29 | | |
30 | | VLOG_DEFINE_THIS_MODULE(signals); |
31 | | |
32 | | #if defined(_NSIG) |
33 | 0 | #define N_SIGNALS _NSIG |
34 | | #elif defined(NSIG) |
35 | | #define N_SIGNALS NSIG |
36 | | #else |
37 | | /* We could try harder to get the maximum signal number, but in practice we |
38 | | * only care about SIGHUP, which is normally signal 1 anyway. */ |
39 | | #define N_SIGNALS 32 |
40 | | #endif |
41 | | |
42 | | /* Returns the name of signal 'signum' as a string. The return value is either |
43 | | * a statically allocated constant string or the 'bufsize'-byte buffer |
44 | | * 'namebuf'. 'bufsize' should be at least SIGNAL_NAME_BUFSIZE. |
45 | | * |
46 | | * The string is probably a (possibly multi-word) description of the signal |
47 | | * (e.g. "Hangup") instead of just the stringified version of the macro |
48 | | * (e.g. "SIGHUP"). */ |
49 | | const char * |
50 | | signal_name(int signum, char *namebuf, size_t bufsize) |
51 | 0 | { |
52 | 0 | #if HAVE_DECL_SYS_SIGLIST |
53 | 0 | if (signum >= 0 && signum < N_SIGNALS) { |
54 | 0 | const char *name = sys_siglist[signum]; |
55 | 0 | if (name) { |
56 | 0 | return name; |
57 | 0 | } |
58 | 0 | } |
59 | | #elif HAVE_SIGDESCR_NP |
60 | | const char *name = sigdescr_np(signum); |
61 | | if (name) { |
62 | | return name; |
63 | | } |
64 | | #endif |
65 | | |
66 | 0 | snprintf(namebuf, bufsize, "signal %d", signum); |
67 | 0 | return namebuf; |
68 | 0 | } |
69 | | |
70 | | void |
71 | | xsigaction(int signum, const struct sigaction *new, struct sigaction *old) |
72 | 0 | { |
73 | 0 | if (sigaction(signum, new, old)) { |
74 | 0 | char namebuf[SIGNAL_NAME_BUFSIZE]; |
75 | |
|
76 | 0 | VLOG_FATAL("sigaction(%s) failed (%s)", |
77 | 0 | signal_name(signum, namebuf, sizeof namebuf), |
78 | 0 | ovs_strerror(errno)); |
79 | 0 | } |
80 | 0 | } |