GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libevent/select.c Lines: 114 122 93.4 %
Date: 2017-11-13 Branches: 50 66 75.8 %

Line Branch Exec Source
1
/*	$OpenBSD: select.c,v 1.25 2016/09/03 11:31:17 nayden Exp $	*/
2
3
/*
4
 * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 * 3. The name of the author may not be used to endorse or promote products
16
 *    derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
30
#include <sys/types.h>
31
#include <sys/time.h>
32
#include <sys/select.h>
33
#include <sys/queue.h>
34
35
#include <signal.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <unistd.h>
40
#include <errno.h>
41
#ifdef CHECK_INVARIANTS
42
#include <assert.h>
43
#endif
44
45
#include "event.h"
46
#include "event-internal.h"
47
#include "evsignal.h"
48
#include "log.h"
49
50
struct selectop {
51
	int event_fds;		/* Highest fd in fd set */
52
	size_t event_fdsz;
53
	fd_set *event_readset_in;
54
	fd_set *event_writeset_in;
55
	fd_set *event_readset_out;
56
	fd_set *event_writeset_out;
57
	struct event **event_r_by_fd;
58
	struct event **event_w_by_fd;
59
};
60
61
static void *select_init	(struct event_base *);
62
static int select_add		(void *, struct event *);
63
static int select_del		(void *, struct event *);
64
static int select_dispatch	(struct event_base *, void *, struct timeval *);
65
static void select_dealloc     (struct event_base *, void *);
66
67
const struct eventop selectops = {
68
	"select",
69
	select_init,
70
	select_add,
71
	select_del,
72
	select_dispatch,
73
	select_dealloc,
74
	0
75
};
76
77
static int select_resize(struct selectop *sop, size_t fdsz);
78
79
static void *
80
select_init(struct event_base *base)
81
{
82
	struct selectop *sop;
83
84
	/* Disable select when this environment variable is set */
85

216
	if (!issetugid() && getenv("EVENT_NOSELECT"))
86
		return (NULL);
87
88
72
	if (!(sop = calloc(1, sizeof(struct selectop))))
89
		return (NULL);
90
91
72
	select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask));
92
93
72
	evsignal_init(base);
94
95
72
	return (sop);
96
72
}
97
98
#ifdef CHECK_INVARIANTS
99
static void
100
check_selectop(struct selectop *sop)
101
{
102
	int i;
103
	for (i = 0; i <= sop->event_fds; ++i) {
104
		if (FD_ISSET(i, sop->event_readset_in)) {
105
			assert(sop->event_r_by_fd[i]);
106
			assert(sop->event_r_by_fd[i]->ev_events & EV_READ);
107
			assert(sop->event_r_by_fd[i]->ev_fd == i);
108
		} else {
109
			assert(! sop->event_r_by_fd[i]);
110
		}
111
		if (FD_ISSET(i, sop->event_writeset_in)) {
112
			assert(sop->event_w_by_fd[i]);
113
			assert(sop->event_w_by_fd[i]->ev_events & EV_WRITE);
114
			assert(sop->event_w_by_fd[i]->ev_fd == i);
115
		} else {
116
			assert(! sop->event_w_by_fd[i]);
117
		}
118
	}
119
120
}
121
#else
122
#define check_selectop(sop) do { (void) sop; } while (0)
123
#endif
124
125
static int
126
select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
127
{
128
	int res, i, j;
129
42324
	struct selectop *sop = arg;
130
131
	check_selectop(sop);
132
133
42324
	memcpy(sop->event_readset_out, sop->event_readset_in,
134
21162
	       sop->event_fdsz);
135
42324
	memcpy(sop->event_writeset_out, sop->event_writeset_in,
136
21162
	       sop->event_fdsz);
137
138
42324
	res = select(sop->event_fds + 1, sop->event_readset_out,
139
21162
	    sop->event_writeset_out, NULL, tv);
140
141
	check_selectop(sop);
142
143
21162
	if (res == -1) {
144
6
		if (errno != EINTR) {
145
			event_warn("select");
146
			return (-1);
147
		}
148
149
6
		evsignal_process(base);
150
6
		return (0);
151
21156
	} else if (base->sig.evsignal_caught) {
152
27
		evsignal_process(base);
153
27
	}
154
155
	event_debug(("%s: select reports %d", __func__, res));
156
157
	check_selectop(sop);
158
21156
	i = arc4random_uniform(sop->event_fds + 1);
159
464620
	for (j = 0; j <= sop->event_fds; ++j) {
160
		struct event *r_ev = NULL, *w_ev = NULL;
161
211154
		if (++i >= sop->event_fds+1)
162
			i = 0;
163
164
		res = 0;
165
211154
		if (FD_ISSET(i, sop->event_readset_out)) {
166
19995
			r_ev = sop->event_r_by_fd[i];
167
			res |= EV_READ;
168
19995
		}
169
211154
		if (FD_ISSET(i, sop->event_writeset_out)) {
170
22302
			w_ev = sop->event_w_by_fd[i];
171
22302
			res |= EV_WRITE;
172
22302
		}
173

231149
		if (r_ev && (res & r_ev->ev_events)) {
174
19995
			event_active(r_ev, res & r_ev->ev_events, 1);
175
19995
		}
176

255758
		if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) {
177
22302
			event_active(w_ev, res & w_ev->ev_events, 1);
178
22302
		}
179
	}
180
	check_selectop(sop);
181
182
21156
	return (0);
183
21162
}
184
185
186
static int
187
select_resize(struct selectop *sop, size_t fdsz)
188
{
189
	size_t n_events, n_events_old;
190
191
	fd_set *readset_in = NULL;
192
	fd_set *writeset_in = NULL;
193
	fd_set *readset_out = NULL;
194
	fd_set *writeset_out = NULL;
195
	struct event **r_by_fd = NULL;
196
	struct event **w_by_fd = NULL;
197
198
156
	n_events = (fdsz/sizeof(fd_mask)) * NFDBITS;
199
78
	n_events_old = (sop->event_fdsz/sizeof(fd_mask)) * NFDBITS;
200
201
78
	if (sop->event_readset_in)
202
6
		check_selectop(sop);
203
204
78
	if ((readset_in = realloc(sop->event_readset_in, fdsz)) == NULL)
205
		goto error;
206
78
	sop->event_readset_in = readset_in;
207
78
	if ((readset_out = realloc(sop->event_readset_out, fdsz)) == NULL)
208
		goto error;
209
78
	sop->event_readset_out = readset_out;
210
78
	if ((writeset_in = realloc(sop->event_writeset_in, fdsz)) == NULL)
211
		goto error;
212
78
	sop->event_writeset_in = writeset_in;
213
78
	if ((writeset_out = realloc(sop->event_writeset_out, fdsz)) == NULL)
214
		goto error;
215
78
	sop->event_writeset_out = writeset_out;
216
156
	if ((r_by_fd = reallocarray(sop->event_r_by_fd, n_events,
217
78
	    sizeof(struct event *))) == NULL)
218
		goto error;
219
78
	sop->event_r_by_fd = r_by_fd;
220
156
	if ((w_by_fd = reallocarray(sop->event_w_by_fd, n_events,
221
78
	    sizeof(struct event *))) == NULL)
222
		goto error;
223
78
	sop->event_w_by_fd = w_by_fd;
224
225
156
	memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
226
78
	    fdsz - sop->event_fdsz);
227
156
	memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
228
78
	    fdsz - sop->event_fdsz);
229
156
	memset(sop->event_r_by_fd + n_events_old, 0,
230
78
	    (n_events-n_events_old) * sizeof(struct event*));
231
78
	memset(sop->event_w_by_fd + n_events_old, 0,
232
	    (n_events-n_events_old) * sizeof(struct event*));
233
234
78
	sop->event_fdsz = fdsz;
235
	check_selectop(sop);
236
237
78
	return (0);
238
239
 error:
240
	event_warn("malloc");
241
	return (-1);
242
78
}
243
244
245
static int
246
select_add(void *arg, struct event *ev)
247
{
248
81660
	struct selectop *sop = arg;
249
250
40830
	if (ev->ev_events & EV_SIGNAL)
251
42
		return (evsignal_add(ev));
252
253
	check_selectop(sop);
254
	/*
255
	 * Keep track of the highest fd, so that we can calculate the size
256
	 * of the fd_sets for select(2)
257
	 */
258
40788
	if (sop->event_fds < ev->ev_fd) {
259
771
		size_t fdsz = sop->event_fdsz;
260
261
771
		if (fdsz < sizeof(fd_mask))
262
			fdsz = sizeof(fd_mask);
263
264
1554
		while (fdsz <
265
777
		    (howmany(ev->ev_fd + 1, NFDBITS) * sizeof(fd_mask)))
266
6
			fdsz *= 2;
267
268
771
		if (fdsz != sop->event_fdsz) {
269
6
			if (select_resize(sop, fdsz)) {
270
				check_selectop(sop);
271
				return (-1);
272
			}
273
		}
274
275
771
		sop->event_fds = ev->ev_fd;
276
771
	}
277
278
40788
	if (ev->ev_events & EV_READ) {
279
39819
		FD_SET(ev->ev_fd, sop->event_readset_in);
280
39819
		sop->event_r_by_fd[ev->ev_fd] = ev;
281
39819
	}
282
40788
	if (ev->ev_events & EV_WRITE) {
283
969
		FD_SET(ev->ev_fd, sop->event_writeset_in);
284
969
		sop->event_w_by_fd[ev->ev_fd] = ev;
285
969
	}
286
	check_selectop(sop);
287
288
40788
	return (0);
289
40830
}
290
291
/*
292
 * Nothing to be done here.
293
 */
294
295
static int
296
select_del(void *arg, struct event *ev)
297
{
298
81624
	struct selectop *sop = arg;
299
300
	check_selectop(sop);
301
40812
	if (ev->ev_events & EV_SIGNAL)
302
39
		return (evsignal_del(ev));
303
304
40773
	if (sop->event_fds < ev->ev_fd) {
305
		check_selectop(sop);
306
		return (0);
307
	}
308
309
40773
	if (ev->ev_events & EV_READ) {
310
39804
		FD_CLR(ev->ev_fd, sop->event_readset_in);
311
39804
		sop->event_r_by_fd[ev->ev_fd] = NULL;
312
39804
	}
313
314
40773
	if (ev->ev_events & EV_WRITE) {
315
969
		FD_CLR(ev->ev_fd, sop->event_writeset_in);
316
969
		sop->event_w_by_fd[ev->ev_fd] = NULL;
317
969
	}
318
319
	check_selectop(sop);
320
40773
	return (0);
321
40812
}
322
323
static void
324
select_dealloc(struct event_base *base, void *arg)
325
{
326
96
	struct selectop *sop = arg;
327
328
48
	evsignal_dealloc(base);
329
48
	free(sop->event_readset_in);
330
48
	free(sop->event_writeset_in);
331
48
	free(sop->event_readset_out);
332
48
	free(sop->event_writeset_out);
333
48
	free(sop->event_r_by_fd);
334
48
	free(sop->event_w_by_fd);
335
336
48
	memset(sop, 0, sizeof(struct selectop));
337
48
	free(sop);
338
48
}