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. */
46 static struct signal signals[N_SIGNALS];
48 static void signal_handler(int signr);
50 /* Sets up a handler for 'signr' and returns a structure that represents it.
52 * Only one handler for a given signal may be registered. */
54 signal_register(int signr)
59 ovs_assert(signr >= 1 && signr < N_SIGNALS);
63 ovs_assert(!s->fds[0] && !s->fds[1]);
64 xpipe_nonblocking(s->fds);
66 /* Install signal handler. */
67 memset(&sa, 0, sizeof sa);
68 sa.sa_handler = signal_handler;
69 sigemptyset(&sa.sa_mask);
70 sa.sa_flags = SA_RESTART;
71 xsigaction(signr, &sa, NULL);
76 /* Returns true if signal 's' has been received since the last call to this
77 * function with argument 's'. */
79 signal_poll(struct signal *s)
81 char buf[_POSIX_PIPE_BUF];
83 return read(s->fds[0], buf, sizeof buf) > 0;
86 /* Causes the next call to poll_block() to wake up when signal_poll(s) would
89 signal_wait(struct signal *s)
91 poll_fd_wait(s->fds[0], POLLIN);
95 signal_handler(int signr)
97 if (signr >= 1 && signr < N_SIGNALS) {
98 ignore(write(signals[signr].fds[1], "", 1));
102 /* Returns the name of signal 'signum' as a string. The return value is either
103 * a statically allocated constant string or the 'bufsize'-byte buffer
104 * 'namebuf'. 'bufsize' should be at least SIGNAL_NAME_BUFSIZE.
106 * The string is probably a (possibly multi-word) description of the signal
107 * (e.g. "Hangup") instead of just the stringified version of the macro
108 * (e.g. "SIGHUP"). */
110 signal_name(int signum, char *namebuf, size_t bufsize)
112 #if HAVE_DECL_SYS_SIGLIST
113 if (signum >= 0 && signum < N_SIGNALS) {
114 const char *name = sys_siglist[signum];
121 snprintf(namebuf, bufsize, "signal %d", signum);
126 xsigaction(int signum, const struct sigaction *new, struct sigaction *old)
128 if (sigaction(signum, new, old)) {
129 char namebuf[SIGNAL_NAME_BUFSIZE];
131 VLOG_FATAL("sigaction(%s) failed (%s)",
132 signal_name(signum, namebuf, sizeof namebuf),
133 ovs_strerror(errno));
138 xpthread_sigmask(int how, const sigset_t *new, sigset_t *old)
140 int error = pthread_sigmask(how, new, old);
142 VLOG_FATAL("pthread_sigmask failed (%s)", ovs_strerror(error));