2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include "poll-loop.h"
25 #include "socket-util.h"
29 #define N_SIGNALS _NSIG
31 #define N_SIGNALS NSIG
33 /* We could try harder to get the maximum signal number, but in practice we
34 * only care about SIGHUP, which is normally signal 1 anyway. */
42 static volatile sig_atomic_t signaled[N_SIGNALS];
46 static void signal_handler(int signr);
48 /* Initializes the signals subsystem (if it is not already initialized). Calls
49 * exit() if initialization fails.
51 * Calling this function is optional; it will be called automatically by
52 * signal_start() if necessary. Calling it explicitly allows the client to
53 * prevent the process from exiting at an unexpected time. */
61 ovs_fatal(errno, "could not create pipe");
63 set_nonblocking(fds[0]);
64 set_nonblocking(fds[1]);
68 /* Sets up a handler for 'signr' and returns a structure that represents it.
70 * Only one handler for a given signal may be registered at a time. */
72 signal_register(int signr)
79 /* Set up signal handler. */
80 assert(signr >= 1 && signr < N_SIGNALS);
81 memset(&sa, 0, sizeof sa);
82 sa.sa_handler = signal_handler;
83 sigemptyset(&sa.sa_mask);
84 sa.sa_flags = SA_RESTART;
85 if (sigaction(signr, &sa, NULL)) {
86 ovs_fatal(errno, "sigaction(%d) failed", signr);
89 /* Return structure. */
90 s = xmalloc(sizeof *s);
95 /* Returns true if signal 's' has been received since the last call to this
96 * function with argument 's'. */
98 signal_poll(struct signal *s)
100 char buf[_POSIX_PIPE_BUF];
101 read(fds[0], buf, sizeof buf);
102 if (signaled[s->signr]) {
103 signaled[s->signr] = 0;
109 /* Causes the next call to poll_block() to wake up when signal_poll(s) would
112 signal_wait(struct signal *s)
114 if (signaled[s->signr]) {
115 poll_immediate_wake();
117 poll_fd_wait(fds[0], POLLIN);
122 signal_handler(int signr)
124 if (signr >= 1 && signr < N_SIGNALS) {
125 write(fds[1], "", 1);
126 signaled[signr] = true;