GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libevent/select.c Lines: 114 122 93.4 %
Date: 2017-11-07 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

384
	if (!issetugid() && getenv("EVENT_NOSELECT"))
86
		return (NULL);
87
88
128
	if (!(sop = calloc(1, sizeof(struct selectop))))
89
		return (NULL);
90
91
128
	select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask));
92
93
128
	evsignal_init(base);
94
95
128
	return (sop);
96
128
}
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
77330
	struct selectop *sop = arg;
130
131
	check_selectop(sop);
132
133
77330
	memcpy(sop->event_readset_out, sop->event_readset_in,
134
38665
	       sop->event_fdsz);
135
77330
	memcpy(sop->event_writeset_out, sop->event_writeset_in,
136
38665
	       sop->event_fdsz);
137
138
77330
	res = select(sop->event_fds + 1, sop->event_readset_out,
139
38665
	    sop->event_writeset_out, NULL, tv);
140
141
	check_selectop(sop);
142
143
38665
	if (res == -1) {
144
10
		if (errno != EINTR) {
145
			event_warn("select");
146
			return (-1);
147
		}
148
149
10
		evsignal_process(base);
150
10
		return (0);
151
38655
	} else if (base->sig.evsignal_caught) {
152
45
		evsignal_process(base);
153
45
	}
154
155
	event_debug(("%s: select reports %d", __func__, res));
156
157
	check_selectop(sop);
158
38655
	i = arc4random_uniform(sop->event_fds + 1);
159
850088
	for (j = 0; j <= sop->event_fds; ++j) {
160
		struct event *r_ev = NULL, *w_ev = NULL;
161
386389
		if (++i >= sop->event_fds+1)
162
			i = 0;
163
164
		res = 0;
165
386389
		if (FD_ISSET(i, sop->event_readset_out)) {
166
36585
			r_ev = sop->event_r_by_fd[i];
167
			res |= EV_READ;
168
36585
		}
169
386389
		if (FD_ISSET(i, sop->event_writeset_out)) {
170
40804
			w_ev = sop->event_w_by_fd[i];
171
40804
			res |= EV_WRITE;
172
40804
		}
173

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

467997
		if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) {
177
40804
			event_active(w_ev, res & w_ev->ev_events, 1);
178
40804
		}
179
	}
180
	check_selectop(sop);
181
182
38655
	return (0);
183
38665
}
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
278
	n_events = (fdsz/sizeof(fd_mask)) * NFDBITS;
199
139
	n_events_old = (sop->event_fdsz/sizeof(fd_mask)) * NFDBITS;
200
201
139
	if (sop->event_readset_in)
202
11
		check_selectop(sop);
203
204
139
	if ((readset_in = realloc(sop->event_readset_in, fdsz)) == NULL)
205
		goto error;
206
139
	sop->event_readset_in = readset_in;
207
139
	if ((readset_out = realloc(sop->event_readset_out, fdsz)) == NULL)
208
		goto error;
209
139
	sop->event_readset_out = readset_out;
210
139
	if ((writeset_in = realloc(sop->event_writeset_in, fdsz)) == NULL)
211
		goto error;
212
139
	sop->event_writeset_in = writeset_in;
213
139
	if ((writeset_out = realloc(sop->event_writeset_out, fdsz)) == NULL)
214
		goto error;
215
139
	sop->event_writeset_out = writeset_out;
216
278
	if ((r_by_fd = reallocarray(sop->event_r_by_fd, n_events,
217
139
	    sizeof(struct event *))) == NULL)
218
		goto error;
219
139
	sop->event_r_by_fd = r_by_fd;
220
278
	if ((w_by_fd = reallocarray(sop->event_w_by_fd, n_events,
221
139
	    sizeof(struct event *))) == NULL)
222
		goto error;
223
139
	sop->event_w_by_fd = w_by_fd;
224
225
278
	memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
226
139
	    fdsz - sop->event_fdsz);
227
278
	memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
228
139
	    fdsz - sop->event_fdsz);
229
278
	memset(sop->event_r_by_fd + n_events_old, 0,
230
139
	    (n_events-n_events_old) * sizeof(struct event*));
231
139
	memset(sop->event_w_by_fd + n_events_old, 0,
232
	    (n_events-n_events_old) * sizeof(struct event*));
233
234
139
	sop->event_fdsz = fdsz;
235
	check_selectop(sop);
236
237
139
	return (0);
238
239
 error:
240
	event_warn("malloc");
241
	return (-1);
242
139
}
243
244
245
static int
246
select_add(void *arg, struct event *ev)
247
{
248
149456
	struct selectop *sop = arg;
249
250
74728
	if (ev->ev_events & EV_SIGNAL)
251
72
		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
74656
	if (sop->event_fds < ev->ev_fd) {
259
1411
		size_t fdsz = sop->event_fdsz;
260
261
1411
		if (fdsz < sizeof(fd_mask))
262
			fdsz = sizeof(fd_mask);
263
264
2844
		while (fdsz <
265
1422
		    (howmany(ev->ev_fd + 1, NFDBITS) * sizeof(fd_mask)))
266
11
			fdsz *= 2;
267
268
1411
		if (fdsz != sop->event_fdsz) {
269
11
			if (select_resize(sop, fdsz)) {
270
				check_selectop(sop);
271
				return (-1);
272
			}
273
		}
274
275
1411
		sop->event_fds = ev->ev_fd;
276
1411
	}
277
278
74656
	if (ev->ev_events & EV_READ) {
279
72947
		FD_SET(ev->ev_fd, sop->event_readset_in);
280
72947
		sop->event_r_by_fd[ev->ev_fd] = ev;
281
72947
	}
282
74656
	if (ev->ev_events & EV_WRITE) {
283
1709
		FD_SET(ev->ev_fd, sop->event_writeset_in);
284
1709
		sop->event_w_by_fd[ev->ev_fd] = ev;
285
1709
	}
286
	check_selectop(sop);
287
288
74656
	return (0);
289
74728
}
290
291
/*
292
 * Nothing to be done here.
293
 */
294
295
static int
296
select_del(void *arg, struct event *ev)
297
{
298
149386
	struct selectop *sop = arg;
299
300
	check_selectop(sop);
301
74693
	if (ev->ev_events & EV_SIGNAL)
302
66
		return (evsignal_del(ev));
303
304
74627
	if (sop->event_fds < ev->ev_fd) {
305
		check_selectop(sop);
306
		return (0);
307
	}
308
309
74627
	if (ev->ev_events & EV_READ) {
310
72918
		FD_CLR(ev->ev_fd, sop->event_readset_in);
311
72918
		sop->event_r_by_fd[ev->ev_fd] = NULL;
312
72918
	}
313
314
74627
	if (ev->ev_events & EV_WRITE) {
315
1709
		FD_CLR(ev->ev_fd, sop->event_writeset_in);
316
1709
		sop->event_w_by_fd[ev->ev_fd] = NULL;
317
1709
	}
318
319
	check_selectop(sop);
320
74627
	return (0);
321
74693
}
322
323
static void
324
select_dealloc(struct event_base *base, void *arg)
325
{
326
168
	struct selectop *sop = arg;
327
328
84
	evsignal_dealloc(base);
329
84
	free(sop->event_readset_in);
330
84
	free(sop->event_writeset_in);
331
84
	free(sop->event_readset_out);
332
84
	free(sop->event_writeset_out);
333
84
	free(sop->event_r_by_fd);
334
84
	free(sop->event_w_by_fd);
335
336
84
	memset(sop, 0, sizeof(struct selectop));
337
84
	free(sop);
338
84
}