]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
92829687 | 2 | * Copyright (c) 2008, 2009, 2011, 2012, 2013 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "signals.h" | |
064af421 BP |
19 | #include <errno.h> |
20 | #include <limits.h> | |
21 | #include <signal.h> | |
5a08f9b0 | 22 | #include <stdlib.h> |
064af421 BP |
23 | #include <unistd.h> |
24 | #include "poll-loop.h" | |
25 | #include "socket-util.h" | |
b725cf02 | 26 | #include "type-props.h" |
064af421 | 27 | #include "util.h" |
279c9e03 BP |
28 | #include "vlog.h" |
29 | ||
30 | VLOG_DEFINE_THIS_MODULE(signals); | |
064af421 BP |
31 | |
32 | #if defined(_NSIG) | |
33 | #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 | struct signal { | |
5a08f9b0 | 43 | struct sigaction saved_sa; |
064af421 BP |
44 | int signr; |
45 | }; | |
46 | ||
47 | static volatile sig_atomic_t signaled[N_SIGNALS]; | |
48 | ||
49 | static int fds[2]; | |
50 | ||
51 | static void signal_handler(int signr); | |
52 | ||
53 | /* Initializes the signals subsystem (if it is not already initialized). Calls | |
54 | * exit() if initialization fails. | |
55 | * | |
56 | * Calling this function is optional; it will be called automatically by | |
57 | * signal_start() if necessary. Calling it explicitly allows the client to | |
58 | * prevent the process from exiting at an unexpected time. */ | |
59 | void | |
60 | signal_init(void) | |
61 | { | |
62 | static bool inited; | |
63 | if (!inited) { | |
64 | inited = true; | |
c0d95206 | 65 | xpipe_nonblocking(fds); |
064af421 BP |
66 | } |
67 | } | |
68 | ||
69 | /* Sets up a handler for 'signr' and returns a structure that represents it. | |
70 | * | |
71 | * Only one handler for a given signal may be registered at a time. */ | |
72 | struct signal * | |
73 | signal_register(int signr) | |
74 | { | |
75 | struct sigaction sa; | |
76 | struct signal *s; | |
77 | ||
78 | signal_init(); | |
79 | ||
5a08f9b0 BP |
80 | s = xmalloc(sizeof *s); |
81 | s->signr = signr; | |
82 | ||
064af421 | 83 | /* Set up signal handler. */ |
cb22974d | 84 | ovs_assert(signr >= 1 && signr < N_SIGNALS); |
064af421 BP |
85 | memset(&sa, 0, sizeof sa); |
86 | sa.sa_handler = signal_handler; | |
87 | sigemptyset(&sa.sa_mask); | |
88 | sa.sa_flags = SA_RESTART; | |
5a08f9b0 | 89 | xsigaction(signr, &sa, &s->saved_sa); |
064af421 | 90 | |
064af421 BP |
91 | return s; |
92 | } | |
93 | ||
5a08f9b0 BP |
94 | /* Unregisters the handler for 's', restores the signal handler that was in |
95 | * effect before signal_register() was called, and frees 's'. */ | |
96 | void | |
97 | signal_unregister(struct signal *s) | |
98 | { | |
99 | if (s) { | |
100 | xsigaction(s->signr, &s->saved_sa, NULL); | |
101 | free(s); | |
102 | } | |
103 | } | |
104 | ||
064af421 BP |
105 | /* Returns true if signal 's' has been received since the last call to this |
106 | * function with argument 's'. */ | |
107 | bool | |
108 | signal_poll(struct signal *s) | |
109 | { | |
110 | char buf[_POSIX_PIPE_BUF]; | |
18b9283b | 111 | ignore(read(fds[0], buf, sizeof buf)); |
064af421 BP |
112 | if (signaled[s->signr]) { |
113 | signaled[s->signr] = 0; | |
114 | return true; | |
115 | } | |
116 | return false; | |
117 | } | |
118 | ||
119 | /* Causes the next call to poll_block() to wake up when signal_poll(s) would | |
120 | * return true. */ | |
121 | void | |
122 | signal_wait(struct signal *s) | |
123 | { | |
124 | if (signaled[s->signr]) { | |
125 | poll_immediate_wake(); | |
126 | } else { | |
127 | poll_fd_wait(fds[0], POLLIN); | |
128 | } | |
129 | } | |
130 | \f | |
131 | static void | |
132 | signal_handler(int signr) | |
133 | { | |
134 | if (signr >= 1 && signr < N_SIGNALS) { | |
18b9283b | 135 | ignore(write(fds[1], "", 1)); |
064af421 BP |
136 | signaled[signr] = true; |
137 | } | |
138 | } | |
b725cf02 BP |
139 | |
140 | /* Returns the name of signal 'signum' as a string. The string may be in a | |
141 | * static buffer that is reused from one call to the next. | |
142 | * | |
143 | * The string is probably a (possibly multi-word) description of the signal | |
144 | * (e.g. "Hangup") instead of just the stringified version of the macro | |
145 | * (e.g. "SIGHUP"). */ | |
146 | const char * | |
147 | signal_name(int signum) | |
148 | { | |
149 | const char *name = NULL; | |
22c4e104 BP |
150 | |
151 | #if HAVE_DECL_SYS_SIGLIST | |
152 | if (signum >= 0 && signum < ARRAY_SIZE(sys_siglist)) { | |
153 | name = sys_siglist[signum]; | |
154 | } | |
b725cf02 | 155 | #endif |
22c4e104 | 156 | |
b725cf02 BP |
157 | if (!name) { |
158 | static char buffer[7 + INT_STRLEN(int) + 1]; | |
159 | sprintf(buffer, "signal %d", signum); | |
160 | name = buffer; | |
161 | } | |
162 | return name; | |
163 | } | |
279c9e03 BP |
164 | |
165 | void | |
166 | xsigaction(int signum, const struct sigaction *new, struct sigaction *old) | |
167 | { | |
168 | if (sigaction(signum, new, old)) { | |
169 | VLOG_FATAL("sigaction(%s) failed (%s)", | |
170 | signal_name(signum), strerror(errno)); | |
171 | } | |
172 | } | |
173 | ||
174 | void | |
92829687 | 175 | xpthread_sigmask(int how, const sigset_t *new, sigset_t *old) |
279c9e03 | 176 | { |
92829687 BP |
177 | int error = pthread_sigmask(how, new, old); |
178 | if (error) { | |
179 | VLOG_FATAL("pthread_sigmask failed (%s)", strerror(error)); | |
279c9e03 BP |
180 | } |
181 | } |