Home | History | Annotate | Line # | Download | only in src
      1 /* SPDX-License-Identifier: BSD-2-Clause */
      2 /*
      3  * dhcpcd - DHCP client daemon
      4  * Copyright (c) 2006-2025 Roy Marples <roy (at) 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 <time.h>
     33 
     34 /* Handy macros to create subsecond timeouts */
     35 #define CSEC_PER_SEC  100
     36 #define MSEC_PER_SEC  1000
     37 #define NSEC_PER_CSEC 10000000
     38 #define NSEC_PER_MSEC 1000000
     39 #define NSEC_PER_SEC  1000000000
     40 
     41 /* eloop queues are really only for deleting timeouts registered
     42  * for a function or object.
     43  * The idea being that one interface has different timeouts for
     44  * say DHCP and DHCPv6. */
     45 #ifndef ELOOP_QUEUE
     46 #define ELOOP_QUEUE 1
     47 #endif
     48 
     49 /* Used for deleting a timeout for all queues. */
     50 #define ELOOP_QUEUE_ALL 0
     51 
     52 /* Forward declare eloop - the content should be invisible to the outside */
     53 struct eloop;
     54 
     55 /* eloop fd events */
     56 #define ELE_READ   0x0001
     57 #define ELE_WRITE  0x0002
     58 #define ELE_ERROR  0x0100
     59 #define ELE_HANGUP 0x0200
     60 #define ELE_NVAL   0x0400
     61 
     62 /* What to keep when forking */
     63 #define ELF_KEEP_SIGNALS  0x0001
     64 #define ELF_KEEP_EVENTS	  0x0002
     65 #define ELF_KEEP_TIMEOUTS 0x0004
     66 
     67 #define ELF_KEEP_ALL	  (ELF_KEEP_SIGNALS | ELF_KEEP_EVENTS | ELF_KEEP_TIMEOUTS)
     68 
     69 size_t eloop_event_count(const struct eloop *);
     70 int eloop_event_add(struct eloop *, int, unsigned short,
     71     void (*)(void *, unsigned short), void *);
     72 int eloop_event_delete(struct eloop *, int);
     73 
     74 unsigned long long eloop_timespec_diff(const struct timespec *tsp,
     75     const struct timespec *usp, unsigned int *nsp);
     76 #define eloop_timeout_add_tv(eloop, tv, cb, ctx) \
     77 	eloop_q_timeout_add_tv((eloop), ELOOP_QUEUE, (tv), (cb), (ctx))
     78 #define eloop_timeout_add_sec(eloop, tv, cb, ctx) \
     79 	eloop_q_timeout_add_sec((eloop), ELOOP_QUEUE, (tv), (cb), (ctx))
     80 #define eloop_timeout_add_msec(eloop, ms, cb, ctx) \
     81 	eloop_q_timeout_add_msec((eloop), ELOOP_QUEUE, (ms), (cb), (ctx))
     82 #define eloop_timeout_delete(eloop, cb, ctx) \
     83 	eloop_q_timeout_delete((eloop), ELOOP_QUEUE, (cb), (ctx))
     84 int eloop_q_timeout_add_tv(struct eloop *, int, const struct timespec *,
     85     void (*)(void *), void *);
     86 int eloop_q_timeout_add_sec(struct eloop *, int, unsigned int, void (*)(void *),
     87     void *);
     88 int eloop_q_timeout_add_msec(struct eloop *, int, unsigned long,
     89     void (*)(void *), void *);
     90 int eloop_q_timeout_delete(struct eloop *, int, void (*)(void *), void *);
     91 
     92 int eloop_signal_set_cb(struct eloop *, const int *, size_t,
     93     void (*)(int, void *), void *);
     94 int eloop_signal_mask(struct eloop *);
     95 
     96 struct eloop *eloop_new(void);
     97 void eloop_free(struct eloop *);
     98 void eloop_exit(struct eloop *, int);
     99 int eloop_forked(struct eloop *, unsigned short);
    100 int eloop_waitfd(int);
    101 int eloop_start(struct eloop *);
    102 
    103 #endif
    104