2
Unix SMB/CIFS implementation.
4
common events code for signal events
6
Copyright (C) Andrew Tridgell 2007
8
** NOTE! The following LGPL license applies to the tevent
9
** library. This does NOT imply that all of Samba is released
12
This library is free software; you can redistribute it and/or
13
modify it under the terms of the GNU Lesser General Public
14
License as published by the Free Software Foundation; either
15
version 3 of the License, or (at your option) any later version.
17
This library is distributed in the hope that it will be useful,
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
Lesser General Public License for more details.
22
You should have received a copy of the GNU Lesser General Public
23
License along with this library; if not, see <http://www.gnu.org/licenses/>.
27
#include "system/filesys.h"
28
#include "system/wait.h"
30
#include "tevent_internal.h"
31
#include "tevent_util.h"
33
#define NUM_SIGNALS 64
35
/* maximum number of SA_SIGINFO signals to hold in the queue.
36
NB. This *MUST* be a power of 2, in order for the ring buffer
37
wrap to work correctly. Thanks to Petr Vandrovec <petr@vandrovec.name>
40
#define SA_INFO_QUEUE_COUNT 64
47
#define SIG_INCREMENT(s) (s).count++
48
#define SIG_SEEN(s, n) (s).seen += (n)
49
#define SIG_PENDING(s) ((s).seen != (s).count)
51
struct tevent_common_signal_list {
52
struct tevent_common_signal_list *prev, *next;
53
struct tevent_signal *se;
57
the poor design of signals means that this table must be static global
59
static struct sig_state {
60
struct tevent_common_signal_list *sig_handlers[NUM_SIGNALS+1];
61
struct sigaction *oldact[NUM_SIGNALS+1];
62
struct sigcounter signal_count[NUM_SIGNALS+1];
63
struct sigcounter got_signal;
65
/* with SA_SIGINFO we get quite a lot of info per signal */
66
siginfo_t *sig_info[NUM_SIGNALS+1];
67
struct sigcounter sig_blocked[NUM_SIGNALS+1];
72
return number of sigcounter events not processed yet
74
static uint32_t sig_count(struct sigcounter s)
76
return s.count - s.seen;
80
signal handler - redirects to registered signals
82
static void tevent_common_signal_handler(int signum)
86
struct tevent_common_signal_list *sl;
87
struct tevent_context *ev = NULL;
88
int saved_errno = errno;
90
SIG_INCREMENT(sig_state->signal_count[signum]);
91
SIG_INCREMENT(sig_state->got_signal);
93
/* Write to each unique event context. */
94
for (sl = sig_state->sig_handlers[signum]; sl; sl = sl->next) {
95
if (sl->se->event_ctx && sl->se->event_ctx != ev) {
96
ev = sl->se->event_ctx;
97
/* doesn't matter if this pipe overflows */
98
res = write(ev->pipe_fds[1], &c, 1);
107
signal handler with SA_SIGINFO - redirects to registered signals
109
static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
112
uint32_t count = sig_count(sig_state->signal_count[signum]);
113
/* sig_state->signal_count[signum].seen % SA_INFO_QUEUE_COUNT
114
* is the base of the unprocessed signals in the ringbuffer. */
115
uint32_t ofs = (sig_state->signal_count[signum].seen + count) %
117
sig_state->sig_info[signum][ofs] = *info;
119
tevent_common_signal_handler(signum);
121
/* handle SA_SIGINFO */
122
if (count+1 == SA_INFO_QUEUE_COUNT) {
123
/* we've filled the info array - block this signal until
124
these ones are delivered */
127
sigaddset(&set, signum);
128
sigprocmask(SIG_BLOCK, &set, NULL);
129
SIG_INCREMENT(sig_state->sig_blocked[signum]);
134
static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
136
if (sig_state->sig_handlers[sl->se->signum]) {
137
DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
143
destroy a signal event
145
static int tevent_signal_destructor(struct tevent_signal *se)
147
struct tevent_common_signal_list *sl;
148
sl = talloc_get_type(se->additional_data,
149
struct tevent_common_signal_list);
152
DLIST_REMOVE(se->event_ctx->signal_events, se);
157
if (sig_state->sig_handlers[se->signum] == NULL) {
158
/* restore old handler, if any */
159
if (sig_state->oldact[se->signum]) {
160
sigaction(se->signum, sig_state->oldact[se->signum], NULL);
161
sig_state->oldact[se->signum] = NULL;
164
if (se->sa_flags & SA_SIGINFO) {
165
if (sig_state->sig_info[se->signum]) {
166
talloc_free(sig_state->sig_info[se->signum]);
167
sig_state->sig_info[se->signum] = NULL;
177
this is part of the pipe hack needed to avoid the signal race condition
179
static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde,
180
uint16_t flags, void *_private)
184
/* its non-blocking, doesn't matter if we read too much */
185
res = read(fde->fd, c, sizeof(c));
190
return NULL on failure (memory allocation error)
192
struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
196
tevent_signal_handler_t handler,
198
const char *handler_name,
199
const char *location)
201
struct tevent_signal *se;
202
struct tevent_common_signal_list *sl;
203
sigset_t set, oldset;
205
if (signum >= NUM_SIGNALS) {
210
/* the sig_state needs to be on a global context as it can last across
211
multiple event contexts */
212
if (sig_state == NULL) {
213
sig_state = talloc_zero(talloc_autofree_context(), struct sig_state);
214
if (sig_state == NULL) {
219
se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
220
if (se == NULL) return NULL;
224
se->sa_flags = sa_flags;
225
se->handler = handler;
226
se->private_data = private_data;
227
se->handler_name = handler_name;
228
se->location = location;
229
se->additional_data = NULL;
231
sl = talloc(se, struct tevent_common_signal_list);
237
se->additional_data = sl;
239
/* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
240
if (!talloc_reference(se, sig_state)) {
245
/* we need to setup the pipe hack handler if not already
247
if (ev->pipe_fde == NULL) {
248
if (pipe(ev->pipe_fds) == -1) {
252
ev_set_blocking(ev->pipe_fds[0], false);
253
ev_set_blocking(ev->pipe_fds[1], false);
254
ev->pipe_fde = tevent_add_fd(ev, ev, ev->pipe_fds[0],
256
signal_pipe_handler, NULL);
258
close(ev->pipe_fds[0]);
259
close(ev->pipe_fds[1]);
265
/* only install a signal handler if not already installed */
266
if (sig_state->sig_handlers[signum] == NULL) {
267
struct sigaction act;
269
act.sa_handler = tevent_common_signal_handler;
270
act.sa_flags = sa_flags;
272
if (sa_flags & SA_SIGINFO) {
273
act.sa_handler = NULL;
274
act.sa_sigaction = tevent_common_signal_handler_info;
275
if (sig_state->sig_info[signum] == NULL) {
276
sig_state->sig_info[signum] = talloc_zero_array(sig_state, siginfo_t, SA_INFO_QUEUE_COUNT);
277
if (sig_state->sig_info[signum] == NULL) {
284
sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
285
if (sig_state->oldact[signum] == NULL) {
289
if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
295
DLIST_ADD(se->event_ctx->signal_events, se);
297
/* Make sure the signal doesn't come in while we're mangling list. */
299
sigaddset(&set, signum);
300
sigprocmask(SIG_BLOCK, &set, &oldset);
301
DLIST_ADD(sig_state->sig_handlers[signum], sl);
302
sigprocmask(SIG_SETMASK, &oldset, NULL);
304
talloc_set_destructor(se, tevent_signal_destructor);
305
talloc_set_destructor(sl, tevent_common_signal_list_destructor);
312
check if a signal is pending
313
return != 0 if a signal was pending
315
int tevent_common_check_signal(struct tevent_context *ev)
319
if (!sig_state || !SIG_PENDING(sig_state->got_signal)) {
323
for (i=0;i<NUM_SIGNALS+1;i++) {
324
struct tevent_common_signal_list *sl, *next;
325
struct sigcounter counter = sig_state->signal_count[i];
326
uint32_t count = sig_count(counter);
328
/* Ensure we null out any stored siginfo_t entries
329
* after processing for debugging purposes. */
330
bool clear_processed_siginfo = false;
336
for (sl=sig_state->sig_handlers[i];sl;sl=next) {
337
struct tevent_signal *se = sl->se;
340
if (se->sa_flags & SA_SIGINFO) {
343
clear_processed_siginfo = true;
345
for (j=0;j<count;j++) {
346
/* sig_state->signal_count[i].seen
347
* % SA_INFO_QUEUE_COUNT is
348
* the base position of the unprocessed
349
* signals in the ringbuffer. */
350
uint32_t ofs = (counter.seen + j)
351
% SA_INFO_QUEUE_COUNT;
352
se->handler(ev, se, i, 1,
353
(void*)&sig_state->sig_info[i][ofs],
356
if (se->sa_flags & SA_RESETHAND) {
362
se->handler(ev, se, i, count, NULL, se->private_data);
363
if (se->sa_flags & SA_RESETHAND) {
369
if (clear_processed_siginfo) {
371
for (j=0;j<count;j++) {
372
uint32_t ofs = (counter.seen + j)
373
% SA_INFO_QUEUE_COUNT;
374
memset((void*)&sig_state->sig_info[i][ofs],
381
SIG_SEEN(sig_state->signal_count[i], count);
382
SIG_SEEN(sig_state->got_signal, count);
385
if (SIG_PENDING(sig_state->sig_blocked[i])) {
386
/* We'd filled the queue, unblock the
387
signal now the queue is empty again.
388
Note we MUST do this after the
389
SIG_SEEN(sig_state->signal_count[i], count)
390
call to prevent a new signal running
391
out of room in the sig_state->sig_info[i][]
396
SIG_SEEN(sig_state->sig_blocked[i],
397
sig_count(sig_state->sig_blocked[i]));
398
sigprocmask(SIG_UNBLOCK, &set, NULL);
406
void tevent_cleanup_pending_signal_handlers(struct tevent_signal *se)
408
struct tevent_common_signal_list *sl;
409
sl = talloc_get_type(se->additional_data,
410
struct tevent_common_signal_list);
412
tevent_common_signal_list_destructor(sl);
414
if (sig_state->sig_handlers[se->signum] == NULL) {
415
if (sig_state->oldact[se->signum]) {
416
sigaction(se->signum, sig_state->oldact[se->signum], NULL);
417
sig_state->oldact[se->signum] = NULL;