summaryrefslogtreecommitdiffstats
path: root/dhcpcd/eloop.c
blob: 0a7f69a63e3e9e92ff71fb25920d7f8a6ac4b2ba (plain) (blame)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * dhcpcd - DHCP client daemon
 * Copyright (c) 2006-2013 Roy Marples <roy@marples.name>
 * All rights reserved

 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* Needed for ppoll(2) */
#define _GNU_SOURCE

#include <sys/queue.h>
#include <sys/time.h>

#include <errno.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <syslog.h>

#include "common.h"
#include "dhcpcd.h"
#include "eloop.h"

static struct timeval now;

struct event {
	TAILQ_ENTRY(event) next;
	int fd;
	void (*callback)(void *);
	void *arg;
	struct pollfd *pollfd;
};
static size_t events_len;
static TAILQ_HEAD (event_head, event) events = TAILQ_HEAD_INITIALIZER(events);
static struct event_head free_events = TAILQ_HEAD_INITIALIZER(free_events);

struct timeout {
	TAILQ_ENTRY(timeout) next;
	struct timeval when;
	void (*callback)(void *);
	void *arg;
	int queue;
};
static TAILQ_HEAD (timeout_head, timeout) timeouts
    = TAILQ_HEAD_INITIALIZER(timeouts);
static struct timeout_head free_timeouts
    = TAILQ_HEAD_INITIALIZER(free_timeouts);

static void (*volatile timeout0)(void *);
static void *volatile timeout0_arg;

static struct pollfd *fds;
static size_t fds_len;

static void
eloop_event_setup_fds(void)
{
	struct event *e;
	size_t i;

	i = 0;
	TAILQ_FOREACH(e, &events, next) {
		fds[i].fd = e->fd;
		fds[i].events = POLLIN;
		fds[i].revents = 0;
		e->pollfd = &fds[i];
		i++;
	}
}

int
eloop_event_add(int fd, void (*callback)(void *), void *arg)
{
	struct event *e;

	/* We should only have one callback monitoring the fd */
	TAILQ_FOREACH(e, &events, next) {
		if (e->fd == fd) {
			e->callback = callback;
			e->arg = arg;
			return 0;
		}
	}

	/* Allocate a new event if no free ones already allocated */
	if ((e = TAILQ_FIRST(&free_events))) {
		TAILQ_REMOVE(&free_events, e, next);
	} else {
		e = malloc(sizeof(*e));
		if (e == NULL) {
			syslog(LOG_ERR, "%s: %m", __func__);
			return -1;
		}
	}

	/* Ensure we can actually listen to it */
	events_len++;
	if (events_len > fds_len) {
		fds_len += 5;
		free(fds);
		fds = malloc(sizeof(*fds) * fds_len);
		if (fds == NULL) {
			syslog(LOG_ERR, "%s: %m", __func__);
			free(e);
			return -1;
		}
	}

	/* Now populate the structure and add it to the list */
	e->fd = fd;
	e->callback = callback;
	e->arg = arg;
	/* The order of events should not matter.
	 * However, some PPP servers love to close the link right after
	 * sending their final message. So to ensure dhcpcd processes this
	 * message (which is likely to be that the DHCP addresses are wrong)
	 * we insert new events at the queue head as the link fd will be
	 * the first event added. */
	TAILQ_INSERT_HEAD(&events, e, next);
	eloop_event_setup_fds();
	return 0;
}

void
eloop_event_delete(int fd)
{
	struct event *e;

	TAILQ_FOREACH(e, &events, next) {
		if (e->fd == fd) {
			TAILQ_REMOVE(&events, e, next);
			TAILQ_INSERT_TAIL(&free_events, e, next);
			events_len--;
			eloop_event_setup_fds();
			break;
		}
	}
}

int
eloop_q_timeout_add_tv(int queue,
    const struct timeval *when, void (*callback)(void *), void *arg)
{
	struct timeval w;
	struct timeout *t, *tt = NULL;

	get_monotonic(&now);
	timeradd(&now, when, &w);
	/* Check for time_t overflow. */
	if (timercmp(&w, &now, <)) {
		errno = ERANGE;
		return -1;
	}

	/* Remove existing timeout if present */
	TAILQ_FOREACH(t, &timeouts, next) {
		if (t->callback == callback && t->arg == arg) {
			TAILQ_REMOVE(&timeouts, t, next);
			break;
		}
	}

	if (t == NULL) {
		/* No existing, so allocate or grab one from the free pool */
		if ((t = TAILQ_FIRST(&free_timeouts))) {
			TAILQ_REMOVE(&free_timeouts, t, next);
		} else {
			t = malloc(sizeof(*t));
			if (t == NULL) {
				syslog(LOG_ERR, "%s: %m", __func__);
				return -1;
			}
		}
	}

	t->when.tv_sec = w.tv_sec;
	t->when.tv_usec = w.tv_usec;
	t->callback = callback;
	t->arg = arg;
	t->queue = queue;

	/* The timeout list should be in chronological order,
	 * soonest first. */
	TAILQ_FOREACH(tt, &timeouts, next) {
		if (timercmp(&t->when, &tt->when, <)) {
			TAILQ_INSERT_BEFORE(tt, t, next);
			return 0;
		}
	}
	TAILQ_INSERT_TAIL(&timeouts, t, next);
	return 0;
}

int
eloop_q_timeout_add_sec(int queue, time_t when,
    void (*callback)(void *), void *arg)
{
	struct timeval tv;

	tv.tv_sec = when;
	tv.tv_usec = 0;
	return eloop_q_timeout_add_tv(queue, &tv, callback, arg);
}

int
eloop_timeout_add_now(void (*callback)(void *), void *arg)
{

	if (timeout0 != NULL) {
		syslog(LOG_WARNING, "%s: timeout0 already set", __func__);
		return eloop_q_timeout_add_sec(0, 0, callback, arg);
	}

	timeout0 = callback;
	timeout0_arg = arg;
	return 0;
}

/* This deletes all timeouts for the interface EXCEPT for ones with the
 * callbacks given. Handy for deleting everything apart from the expire
 * timeout. */
static void
eloop_q_timeouts_delete_v(int queue, void *arg,
    void (*callback)(void *), va_list v)
{
	struct timeout *t, *tt;
	va_list va;
	void (*f)(void *);

	TAILQ_FOREACH_SAFE(t, &timeouts, next, tt) {
		if ((queue == 0 || t->queue == queue) && t->arg == arg &&
		    t->callback != callback)
		{
			va_copy(va, v);
			while ((f = va_arg(va, void (*)(void *)))) {
				if (f == t->callback)
					break;
			}
			va_end(va);
			if (f == NULL) {
				TAILQ_REMOVE(&timeouts, t, next);
				TAILQ_INSERT_TAIL(&free_timeouts, t, next);
			}
		}
	}
}

void
eloop_q_timeouts_delete(int queue, void *arg, void (*callback)(void *), ...)
{
	va_list va;

	va_start(va, callback);
	eloop_q_timeouts_delete_v(queue, arg, callback, va);
	va_end(va);
}

void
eloop_q_timeout_delete(int queue, void (*callback)(void *), void *arg)
{
	struct timeout *t, *tt;

	TAILQ_FOREACH_SAFE(t, &timeouts, next, tt) {
		if (t->queue == queue && t->arg == arg &&
		    (!callback || t->callback == callback))
		{
			TAILQ_REMOVE(&timeouts, t, next);
			TAILQ_INSERT_TAIL(&free_timeouts, t, next);
		}
	}
}

#ifdef DEBUG_MEMORY
/* Define this to free all malloced memory.
 * Normally we don't do this as the OS will do it for us at exit,
 * but it's handy for debugging other leaks in valgrind. */
static void
eloop_cleanup(void)
{
	struct event *e;
	struct timeout *t;

	while ((e = TAILQ_FIRST(&events))) {
		TAILQ_REMOVE(&events, e, next);
		free(e);
	}
	while ((e = TAILQ_FIRST(&free_events))) {
		TAILQ_REMOVE(&free_events, e, next);
		free(e);
	}
	while ((t = TAILQ_FIRST(&timeouts))) {
		TAILQ_REMOVE(&timeouts, t, next);
		free(t);
	}
	while ((t = TAILQ_FIRST(&free_timeouts))) {
		TAILQ_REMOVE(&free_timeouts, t, next);
		free(t);
	}
	free(fds);
}

void
eloop_init(void)
{

	atexit(eloop_cleanup);
}
#endif

__dead void
eloop_start(const sigset_t *sigmask)
{
	int n;
	struct event *e;
	struct timeout *t;
	struct timeval tv;
	struct timespec ts, *tsp;
	void (*t0)(void *);

	for (;;) {
		/* Run all timeouts first */
		if (timeout0) {
			t0 = timeout0;
			timeout0 = NULL;
			t0(timeout0_arg);
			continue;
		}
		if ((t = TAILQ_FIRST(&timeouts))) {
			get_monotonic(&now);
			if (timercmp(&now, &t->when, >)) {
				TAILQ_REMOVE(&timeouts, t, next);
				t->callback(t->arg);
				TAILQ_INSERT_TAIL(&free_timeouts, t, next);
				continue;
			}
			timersub(&t->when, &now, &tv);
			TIMEVAL_TO_TIMESPEC(&tv, &ts);
			tsp = &ts;
		} else
			/* No timeouts, so wait forever */
			tsp = NULL;

		if (tsp == NULL && events_len == 0) {
			syslog(LOG_ERR, "nothing to do");
			exit(EXIT_FAILURE);
		}

		n = pollts(fds, events_len, tsp, sigmask);
		if (n == -1) {
			if (errno == EAGAIN || errno == EINTR)
				continue;
			syslog(LOG_ERR, "poll: %m");
			exit(EXIT_FAILURE);
		}

		/* Process any triggered events. */
		if (n > 0) {
			TAILQ_FOREACH(e, &events, next) {
				if (e->pollfd->revents & (POLLIN | POLLHUP)) {
					e->callback(e->arg);
					/* We need to break here as the
					 * callback could destroy the next
					 * fd to process. */
					break;
				}
			}
		}
	}
}