]> git.proxmox.com Git - mirror_ovs.git/blob - lib/signals.c
Merge branch 'master' into next
[mirror_ovs.git] / lib / signals.c
1 /*
2 * Copyright (c) 2008, 2009 Nicira Networks.
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 <assert.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include "poll-loop.h"
25 #include "socket-util.h"
26 #include "util.h"
27
28 #if defined(_NSIG)
29 #define N_SIGNALS _NSIG
30 #elif defined(NSIG)
31 #define N_SIGNALS NSIG
32 #else
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. */
35 #define N_SIGNALS 32
36 #endif
37
38 struct signal {
39 int signr;
40 };
41
42 static volatile sig_atomic_t signaled[N_SIGNALS];
43
44 static int fds[2];
45
46 static void signal_handler(int signr);
47
48 /* Initializes the signals subsystem (if it is not already initialized). Calls
49 * exit() if initialization fails.
50 *
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. */
54 void
55 signal_init(void)
56 {
57 static bool inited;
58 if (!inited) {
59 inited = true;
60 if (pipe(fds)) {
61 ovs_fatal(errno, "could not create pipe");
62 }
63 set_nonblocking(fds[0]);
64 set_nonblocking(fds[1]);
65 }
66 }
67
68 /* Sets up a handler for 'signr' and returns a structure that represents it.
69 *
70 * Only one handler for a given signal may be registered at a time. */
71 struct signal *
72 signal_register(int signr)
73 {
74 struct sigaction sa;
75 struct signal *s;
76
77 signal_init();
78
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);
87 }
88
89 /* Return structure. */
90 s = xmalloc(sizeof *s);
91 s->signr = signr;
92 return s;
93 }
94
95 /* Returns true if signal 's' has been received since the last call to this
96 * function with argument 's'. */
97 bool
98 signal_poll(struct signal *s)
99 {
100 char buf[_POSIX_PIPE_BUF];
101 ignore(read(fds[0], buf, sizeof buf));
102 if (signaled[s->signr]) {
103 signaled[s->signr] = 0;
104 return true;
105 }
106 return false;
107 }
108
109 /* Causes the next call to poll_block() to wake up when signal_poll(s) would
110 * return true. */
111 void
112 signal_wait(struct signal *s)
113 {
114 if (signaled[s->signr]) {
115 poll_immediate_wake();
116 } else {
117 poll_fd_wait(fds[0], POLLIN);
118 }
119 }
120 \f
121 static void
122 signal_handler(int signr)
123 {
124 if (signr >= 1 && signr < N_SIGNALS) {
125 ignore(write(fds[1], "", 1));
126 signaled[signr] = true;
127 }
128 }