2 * Copyright (c) 2008, 2009, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "poll-loop.h"
25 #include "socket-util.h"
26 #include "type-props.h"
30 VLOG_DEFINE_THIS_MODULE(signals);
33 #define N_SIGNALS _NSIG
35 #define N_SIGNALS NSIG
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. */
43 struct sigaction saved_sa;
47 static volatile sig_atomic_t signaled[N_SIGNALS];
51 static void signal_handler(int signr);
53 /* Initializes the signals subsystem (if it is not already initialized). Calls
54 * exit() if initialization fails.
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. */
65 xpipe_nonblocking(fds);
69 /* Sets up a handler for 'signr' and returns a structure that represents it.
71 * Only one handler for a given signal may be registered at a time. */
73 signal_register(int signr)
80 s = xmalloc(sizeof *s);
83 /* Set up signal handler. */
84 ovs_assert(signr >= 1 && signr < N_SIGNALS);
85 memset(&sa, 0, sizeof sa);
86 sa.sa_handler = signal_handler;
87 sigemptyset(&sa.sa_mask);
88 sa.sa_flags = SA_RESTART;
89 xsigaction(signr, &sa, &s->saved_sa);
94 /* Unregisters the handler for 's', restores the signal handler that was in
95 * effect before signal_register() was called, and frees 's'. */
97 signal_unregister(struct signal *s)
100 xsigaction(s->signr, &s->saved_sa, NULL);
105 /* Returns true if signal 's' has been received since the last call to this
106 * function with argument 's'. */
108 signal_poll(struct signal *s)
110 char buf[_POSIX_PIPE_BUF];
111 ignore(read(fds[0], buf, sizeof buf));
112 if (signaled[s->signr]) {
113 signaled[s->signr] = 0;
119 /* Causes the next call to poll_block() to wake up when signal_poll(s) would
122 signal_wait(struct signal *s)
124 if (signaled[s->signr]) {
125 poll_immediate_wake();
127 poll_fd_wait(fds[0], POLLIN);
132 signal_handler(int signr)
134 if (signr >= 1 && signr < N_SIGNALS) {
135 ignore(write(fds[1], "", 1));
136 signaled[signr] = true;
140 /* Returns the name of signal 'signum' as a string. The return value is either
141 * a statically allocated constant string or the 'bufsize'-byte buffer
142 * 'namebuf'. 'bufsize' should be at least SIGNAL_NAME_BUFSIZE.
144 * The string is probably a (possibly multi-word) description of the signal
145 * (e.g. "Hangup") instead of just the stringified version of the macro
146 * (e.g. "SIGHUP"). */
148 signal_name(int signum, char *namebuf, size_t bufsize)
150 #if HAVE_DECL_SYS_SIGLIST
151 if (signum >= 0 && signum < ARRAY_SIZE(sys_siglist)) {
152 const char *name = sys_siglist[signum];
159 snprintf(namebuf, bufsize, "signal %d", signum);
164 xsigaction(int signum, const struct sigaction *new, struct sigaction *old)
166 if (sigaction(signum, new, old)) {
167 char namebuf[SIGNAL_NAME_BUFSIZE];
169 VLOG_FATAL("sigaction(%s) failed (%s)",
170 signal_name(signum, namebuf, sizeof namebuf),
176 xpthread_sigmask(int how, const sigset_t *new, sigset_t *old)
178 int error = pthread_sigmask(how, new, old);
180 VLOG_FATAL("pthread_sigmask failed (%s)", strerror(error));