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