xref: /minix/external/bsd/dhcpcd/dist/eloop.h (revision bb9622b5)
1 /* $NetBSD: eloop.h,v 1.9 2015/05/16 23:31:32 roy Exp $ */
2 
3 /*
4  * dhcpcd - DHCP client daemon
5  * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
6  * All rights reserved
7 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef ELOOP_H
31 #define ELOOP_H
32 
33 #include <time.h>
34 
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #else
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>
42 #endif
43 #if defined(BSD)
44 /* Assume BSD has a working sys/queue.h and kqueue(2) interface */
45 #define HAVE_SYS_QUEUE_H
46 #define HAVE_KQUEUE
47 #elif defined(__linux__)
48 /* Assume Linux has a working epoll(3) interface */
49 #define HAVE_EPOLL
50 #endif
51 #endif
52 
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. */
60 #ifndef TAILQ_FOREACH
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)
67 #else
68 #include "queue.h"
69 #endif
70 #endif
71 
72 /* Some systems don't define timespec macros */
73 #ifndef timespecclear
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)                                      \
81         do {                                                            \
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) {                    \
85                         (vsp)->tv_sec++;                                \
86                         (vsp)->tv_nsec -= 1000000000L;                  \
87                 }                                                       \
88         } while (/* CONSTCOND */ 0)
89 #define timespecsub(tsp, usp, vsp)                                      \
90         do {                                                            \
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) {                               \
94                         (vsp)->tv_sec--;                                \
95                         (vsp)->tv_nsec += 1000000000L;                  \
96                 }                                                       \
97         } while (/* CONSTCOND */ 0)
98 #endif
99 
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. */
104 #ifndef ELOOP_QUEUE
105   #define ELOOP_QUEUE 1
106 #endif
107 
108 struct eloop_event {
109 	TAILQ_ENTRY(eloop_event) next;
110 	int fd;
111 	void (*read_cb)(void *);
112 	void *read_cb_arg;
113 	void (*write_cb)(void *);
114 	void *write_cb_arg;
115 #if !defined(HAVE_KQUEUE) && !defined(HAVE_EPOLL)
116 	struct pollfd *pollfd;
117 #endif
118 };
119 
120 struct eloop_timeout {
121 	TAILQ_ENTRY(eloop_timeout) next;
122 	struct timespec when;
123 	void (*callback)(void *);
124 	void *arg;
125 	int queue;
126 };
127 
128 struct eloop {
129 	size_t events_len;
130 	TAILQ_HEAD (event_head, eloop_event) events;
131 	struct event_head free_events;
132 
133 	TAILQ_HEAD (timeout_head, eloop_timeout) timeouts;
134 	struct timeout_head free_timeouts;
135 
136 	void (*timeout0)(void *);
137 	void *timeout0_arg;
138 	const int *signals;
139 	size_t signals_len;
140 	void (*signal_cb)(int, void *);
141 	void *signal_cb_ctx;
142 
143 #if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
144 	int poll_fd;
145 #else
146 	struct pollfd *fds;
147 	size_t fds_len;
148 #endif
149 
150 	int exitnow;
151 	int exitcode;
152 };
153 
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);
162 
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 *);
178 
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);
182 
183 struct eloop * eloop_new(void);
184 #if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
185 int eloop_requeue(struct eloop *);
186 #else
187 #define eloop_requeue(eloop) (0)
188 #endif
189 void eloop_free(struct eloop *);
190 void eloop_exit(struct eloop *, int);
191 int eloop_start(struct eloop *, sigset_t *);
192 
193 #endif
194