xref: /dragonfly/contrib/dhcpcd/src/eloop.h (revision b8bbc03f)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2019 Roy Marples <roy@marples.name>
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef ELOOP_H
30 #define ELOOP_H
31 
32 #include <sys/signal.h>
33 
34 #include <time.h>
35 
36 /* Some systems don't define timespec macros */
37 #ifndef timespecclear
38 #define timespecclear(tsp)      (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
39 #define timespecisset(tsp)      ((tsp)->tv_sec || (tsp)->tv_nsec)
40 #define timespeccmp(tsp, usp, cmp)                                      \
41         (((tsp)->tv_sec == (usp)->tv_sec) ?                             \
42             ((tsp)->tv_nsec cmp (usp)->tv_nsec) :                       \
43             ((tsp)->tv_sec cmp (usp)->tv_sec))
44 #define timespecadd(tsp, usp, vsp)                                      \
45         do {                                                            \
46                 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;          \
47                 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;       \
48                 if ((vsp)->tv_nsec >= 1000000000L) {                    \
49                         (vsp)->tv_sec++;                                \
50                         (vsp)->tv_nsec -= 1000000000L;                  \
51                 }                                                       \
52         } while (/* CONSTCOND */ 0)
53 #define timespecsub(tsp, usp, vsp)                                      \
54         do {                                                            \
55                 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;          \
56                 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;       \
57                 if ((vsp)->tv_nsec < 0) {                               \
58                         (vsp)->tv_sec--;                                \
59                         (vsp)->tv_nsec += 1000000000L;                  \
60                 }                                                       \
61         } while (/* CONSTCOND */ 0)
62 #endif
63 
64 /* eloop queues are really only for deleting timeouts registered
65  * for a function or object.
66  * The idea being that one interface has different timeouts for
67  * say DHCP and DHCPv6. */
68 #ifndef ELOOP_QUEUE
69   #define ELOOP_QUEUE 1
70 #endif
71 
72 /* Forward declare eloop - the content should be invisible to the outside */
73 struct eloop;
74 
75 int eloop_event_add_rw(struct eloop *, int,
76     void (*)(void *), void *,
77     void (*)(void *), void *);
78 int eloop_event_add(struct eloop *, int,
79     void (*)(void *), void *);
80 int eloop_event_add_w(struct eloop *, int,
81     void (*)(void *), void *);
82 #define eloop_event_delete(eloop, fd) \
83     eloop_event_delete_write((eloop), (fd), 0)
84 #define eloop_event_remove_writecb(eloop, fd) \
85     eloop_event_delete_write((eloop), (fd), 1)
86 int eloop_event_delete_write(struct eloop *, int, int);
87 
88 #define eloop_timeout_add_tv(eloop, tv, cb, ctx) \
89     eloop_q_timeout_add_tv((eloop), ELOOP_QUEUE, (tv), (cb), (ctx))
90 #define eloop_timeout_add_sec(eloop, tv, cb, ctx) \
91     eloop_q_timeout_add_sec((eloop), ELOOP_QUEUE, (tv), (cb), (ctx))
92 #define eloop_timeout_add_msec(eloop, ms, cb, ctx) \
93     eloop_q_timeout_add_msec((eloop), ELOOP_QUEUE, (ms), (cb), (ctx))
94 #define eloop_timeout_delete(eloop, cb, ctx) \
95     eloop_q_timeout_delete((eloop), ELOOP_QUEUE, (cb), (ctx))
96 int eloop_q_timeout_add_tv(struct eloop *, int,
97     const struct timespec *, void (*)(void *), void *);
98 int eloop_q_timeout_add_sec(struct eloop *, int,
99     time_t, void (*)(void *), void *);
100 int eloop_q_timeout_add_msec(struct eloop *, int,
101     long, void (*)(void *), void *);
102 int eloop_q_timeout_delete(struct eloop *, int, void (*)(void *), void *);
103 
104 int eloop_signal_set_cb(struct eloop *, const int *, size_t,
105     void (*)(int, void *), void *);
106 int eloop_signal_mask(struct eloop *, sigset_t *oldset);
107 
108 struct eloop * eloop_new(void);
109 int eloop_requeue(struct eloop *);
110 void eloop_free(struct eloop *);
111 void eloop_exit(struct eloop *, int);
112 int eloop_start(struct eloop *, sigset_t *);
113 
114 #endif
115