1 /* $NetBSD: eloop.h,v 1.9 2015/05/16 23:31:32 roy Exp $ */
4 * dhcpcd - DHCP client daemon
5 * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 /* Attempt to autodetect kqueue or epoll.
39 * If we can't, the system has to support pselect, which is a POSIX call. */
40 #if (defined(__unix__) || defined(unix)) && !defined(USG)
41 #include <sys/param.h>
44 /* Assume BSD has a working sys/queue.h and kqueue(2) interface */
45 #define HAVE_SYS_QUEUE_H
47 #elif defined(__linux__)
48 /* Assume Linux has a working epoll(3) interface */
53 /* Our structures require TAILQ macros, which really every libc should
54 * ship as they are useful beyond belief.
55 * Sadly some libc's don't have sys/queue.h and some that do don't have
56 * the TAILQ_FOREACH macro. For those that don't, the application using
57 * this implementation will need to ship a working queue.h somewhere.
58 * If we don't have sys/queue.h found in config.h, then
59 * allow QUEUE_H to override loading queue.h in the current directory. */
61 #ifdef HAVE_SYS_QUEUE_H
62 #include <sys/queue.h>
63 #elif defined(QUEUE_H)
64 #define __QUEUE_HEADER(x) #x
65 #define _QUEUE_HEADER(x) __QUEUE_HEADER(x)
66 #include _QUEUE_HEADER(QUEUE_H)
72 /* Some systems don't define timespec macros */
74 #define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
75 #define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
76 #define timespeccmp(tsp, usp, cmp) \
77 (((tsp)->tv_sec == (usp)->tv_sec) ? \
78 ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
79 ((tsp)->tv_sec cmp (usp)->tv_sec))
80 #define timespecadd(tsp, usp, vsp) \
82 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
83 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
84 if ((vsp)->tv_nsec >= 1000000000L) { \
86 (vsp)->tv_nsec -= 1000000000L; \
88 } while (/* CONSTCOND */ 0)
89 #define timespecsub(tsp, usp, vsp) \
91 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
92 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
93 if ((vsp)->tv_nsec < 0) { \
95 (vsp)->tv_nsec += 1000000000L; \
97 } while (/* CONSTCOND */ 0)
100 /* eloop queues are really only for deleting timeouts registered
101 * for a function or object.
102 * The idea being that one interface as different timeouts for
103 * say DHCP and DHCPv6. */
105 #define ELOOP_QUEUE 1
109 TAILQ_ENTRY(eloop_event
) next
;
111 void (*read_cb
)(void *);
113 void (*write_cb
)(void *);
115 #if !defined(HAVE_KQUEUE) && !defined(HAVE_EPOLL)
116 struct pollfd
*pollfd
;
120 struct eloop_timeout
{
121 TAILQ_ENTRY(eloop_timeout
) next
;
122 struct timespec when
;
123 void (*callback
)(void *);
130 TAILQ_HEAD (event_head
, eloop_event
) events
;
131 struct event_head free_events
;
133 TAILQ_HEAD (timeout_head
, eloop_timeout
) timeouts
;
134 struct timeout_head free_timeouts
;
136 void (*timeout0
)(void *);
140 void (*signal_cb
)(int, void *);
143 #if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
154 int eloop_event_add(struct eloop
*, int,
155 void (*)(void *), void *,
156 void (*)(void *), void *);
157 #define eloop_event_delete(eloop, fd) \
158 eloop_event_delete_write((eloop), (fd), 0)
159 #define eloop_event_remove_writecb(eloop, fd) \
160 eloop_event_delete_write((eloop), (fd), 1)
161 void eloop_event_delete_write(struct eloop
*, int, int);
163 #define eloop_timeout_add_tv(eloop, tv, cb, ctx) \
164 eloop_q_timeout_add_tv((eloop), ELOOP_QUEUE, (tv), (cb), (ctx))
165 #define eloop_timeout_add_sec(eloop, tv, cb, ctx) \
166 eloop_q_timeout_add_sec((eloop), ELOOP_QUEUE, (tv), (cb), (ctx))
167 #define eloop_timeout_add_msec(eloop, ms, cb, ctx) \
168 eloop_q_timeout_add_msec((eloop), ELOOP_QUEUE, (ms), (cb), (ctx))
169 #define eloop_timeout_delete(eloop, cb, ctx) \
170 eloop_q_timeout_delete((eloop), ELOOP_QUEUE, (cb), (ctx))
171 int eloop_q_timeout_add_tv(struct eloop
*, int,
172 const struct timespec
*, void (*)(void *), void *);
173 int eloop_q_timeout_add_sec(struct eloop
*, int,
174 time_t, void (*)(void *), void *);
175 int eloop_q_timeout_add_msec(struct eloop
*, int,
176 long, void (*)(void *), void *);
177 void eloop_q_timeout_delete(struct eloop
*, int, void (*)(void *), void *);
179 int eloop_signal_set_cb(struct eloop
*, const int *, size_t,
180 void (*)(int, void *), void *);
181 int eloop_signal_mask(struct eloop
*, sigset_t
*oldset
);
183 struct eloop
* eloop_new(void);
184 #if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
185 int eloop_requeue(struct eloop
*);
187 #define eloop_requeue(eloop) (0)
189 void eloop_free(struct eloop
*);
190 void eloop_exit(struct eloop
*, int);
191 int eloop_start(struct eloop
*, sigset_t
*);