]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/signals.c
2 * Copyright (c) 2008, 2009, 2011, 2012 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.
25 #include "poll-loop.h"
26 #include "socket-util.h"
27 #include "type-props.h"
31 VLOG_DEFINE_THIS_MODULE(signals
);
34 #define N_SIGNALS _NSIG
36 #define N_SIGNALS NSIG
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. */
44 struct sigaction saved_sa
;
48 static volatile sig_atomic_t signaled
[N_SIGNALS
];
52 static void signal_handler(int signr
);
54 /* Initializes the signals subsystem (if it is not already initialized). Calls
55 * exit() if initialization fails.
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. */
66 xpipe_nonblocking(fds
);
70 /* Sets up a handler for 'signr' and returns a structure that represents it.
72 * Only one handler for a given signal may be registered at a time. */
74 signal_register(int signr
)
81 s
= xmalloc(sizeof *s
);
84 /* Set up signal handler. */
85 assert(signr
>= 1 && signr
< N_SIGNALS
);
86 memset(&sa
, 0, sizeof sa
);
87 sa
.sa_handler
= signal_handler
;
88 sigemptyset(&sa
.sa_mask
);
89 sa
.sa_flags
= SA_RESTART
;
90 xsigaction(signr
, &sa
, &s
->saved_sa
);
95 /* Unregisters the handler for 's', restores the signal handler that was in
96 * effect before signal_register() was called, and frees 's'. */
98 signal_unregister(struct signal
*s
)
101 xsigaction(s
->signr
, &s
->saved_sa
, NULL
);
106 /* Returns true if signal 's' has been received since the last call to this
107 * function with argument 's'. */
109 signal_poll(struct signal
*s
)
111 char buf
[_POSIX_PIPE_BUF
];
112 ignore(read(fds
[0], buf
, sizeof buf
));
113 if (signaled
[s
->signr
]) {
114 signaled
[s
->signr
] = 0;
120 /* Causes the next call to poll_block() to wake up when signal_poll(s) would
123 signal_wait(struct signal
*s
)
125 if (signaled
[s
->signr
]) {
126 poll_immediate_wake();
128 poll_fd_wait(fds
[0], POLLIN
);
133 signal_handler(int signr
)
135 if (signr
>= 1 && signr
< N_SIGNALS
) {
136 ignore(write(fds
[1], "", 1));
137 signaled
[signr
] = true;
141 /* Returns the name of signal 'signum' as a string. The string may be in a
142 * static buffer that is reused from one call to the next.
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
)
150 const char *name
= NULL
;
151 #ifdef HAVE_STRSIGNAL
152 name
= strsignal(signum
);
155 static char buffer
[7 + INT_STRLEN(int) + 1];
156 sprintf(buffer
, "signal %d", signum
);
163 xsigaction(int signum
, const struct sigaction
*new, struct sigaction
*old
)
165 if (sigaction(signum
, new, old
)) {
166 VLOG_FATAL("sigaction(%s) failed (%s)",
167 signal_name(signum
), strerror(errno
));
172 xsigprocmask(int how
, const sigset_t
*new, sigset_t
*old
)
174 if (sigprocmask(how
, new, old
)) {
175 VLOG_FATAL("sigprocmask failed (%s)", strerror(errno
));