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