summaryrefslogtreecommitdiff
path: root/source4/lib/events/events_signal.c
blob: 85b42ae4abe01d335693bb80b6966c11d0601d8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* 
   Unix SMB/CIFS implementation.

   common events code for signal events

   Copyright (C) Andrew Tridgell	2007
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "system/filesys.h"
#include "system/select.h"
#include "lib/util/dlinklist.h"
#include "lib/events/events.h"
#include "lib/events/events_internal.h"

#define NUM_SIGNALS 64

/*
  the poor design of signals means that this table must be static global
*/
static struct {
	struct signal_event *sig_handlers[NUM_SIGNALS];
	uint32_t signal_count[NUM_SIGNALS];
	uint32_t got_signal;
	int pipe_hack[2];
} sig_state;


/*
  signal handler - redirects to registered signals
*/
static void signal_handler(int signum)
{
	char c = 0;
	sig_state.signal_count[signum]++;
	sig_state.got_signal++;
	/* doesn't matter if this pipe overflows */
	write(sig_state.pipe_hack[1], &c, 1);
}


/*
  destroy a signal event
*/
static int signal_event_destructor(struct signal_event *se)
{
	se->event_ctx->num_signal_handlers--;
	DLIST_REMOVE(sig_state.sig_handlers[se->signum], se);
	if (sig_state.sig_handlers[se->signum] == NULL) {
		signal(se->signum, SIG_DFL);
	}
	return 0;
}

/*
  this is part of the pipe hack needed to avoid the signal race condition
*/
static void signal_pipe_handler(struct event_context *ev, struct fd_event *fde, 
				uint16_t flags, void *private)
{
	char c[16];
	/* its non-blocking, doesn't matter if we read too much */
	read(sig_state.pipe_hack[0], c, sizeof(c));
}

/*
  add a signal event
  return NULL on failure (memory allocation error)
*/
struct signal_event *common_event_add_signal(struct event_context *ev, 
					    TALLOC_CTX *mem_ctx,
					    int signum,
					    event_signal_handler_t handler, 
					    void *private_data) 
{
	struct signal_event *se;

	if (signum >= NUM_SIGNALS) {
		return NULL;
	}

	se = talloc(mem_ctx?mem_ctx:ev, struct signal_event);
	if (se == NULL) return NULL;

	se->event_ctx		= ev;
	se->handler		= handler;
	se->private_data	= private_data;
	se->signum              = signum;

	if (sig_state.sig_handlers[signum] == NULL) {
		signal(signum, signal_handler);
	}

	DLIST_ADD(sig_state.sig_handlers[signum], se);

	talloc_set_destructor(se, signal_event_destructor);

	if (ev->pipe_fde == NULL) {
		if (sig_state.pipe_hack[0] == 0 && 
		    sig_state.pipe_hack[1] == 0) {
			pipe(sig_state.pipe_hack);
			set_blocking(sig_state.pipe_hack[0], False);
			set_blocking(sig_state.pipe_hack[1], False);
		}
		ev->pipe_fde = event_add_fd(ev, ev, sig_state.pipe_hack[0],
					    EVENT_FD_READ, signal_pipe_handler, NULL);
	}
	ev->num_signal_handlers++;

	return se;
}


/*
  check if a signal is pending
  return != 0 if a signal was pending
*/
int common_event_check_signal(struct event_context *ev)
{
	int i;
	if (sig_state.got_signal == 0) {
		return 0;
	}
	
	for (i=0;i<NUM_SIGNALS+1;i++) {
		uint32_t count = sig_state.signal_count[i];
		if (count != 0) {
			struct signal_event *se, *next;
			for (se=sig_state.sig_handlers[i];se;se=next) {
				next = se->next;
				se->handler(ev, se, i, count, se->private_data);
			}
			sig_state.signal_count[i] -= count;
			sig_state.got_signal -= count;
		}
	}

	return 1;
}