annotate src/eloop.c @ 5578:57e4bf2cc9e7 draft

eloop: Allow eloop to process all fds returned from poll(2) We do this by ensuring the events list or pollfd struct storage is not modified during the revent processing. An event with a fd of -1 means it's been deleted and one without a pollfd struct reference has been newly added. This also allows us to count down the number of fd's that returned a revent so we can break the loop early if possible. This is a really minor optimisation that at best only applies if more than one revent is returned via poll(2). In the case on dhcpcd on NetBSD with privsep, the number of fd's is really low. And on other platforms or without privsep it's low also (just not as low). It's only when you run dhcpcd per interface that the number of fd's starts to creep upwards as you then need one per address dhcpcd is monitoring (as well as the ARP listener per IPv4 address for non NetBSD). However, I use eloop in other code where this could be a good saving and dhcpcd is where the master version of this lives!
author Roy Marples <roy@marples.name>
date Sun, 24 Jan 2021 22:22:25 +0000
parents 9edfc000a89b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4548
c7df03794de3 Add SPDX identifiers to all dhcpcd source files.
Yegor Yefremov <yegorslists@googlemail.com>
parents: 4333
diff changeset
1 /* SPDX-License-Identifier: BSD-2-Clause */
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2 /*
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3 * eloop - portable event based main loop.
4922
555d7d1a4939 Welcome to 2020!
Roy Marples <roy@marples.name>
parents: 4796
diff changeset
4 * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
5 * All rights reserved.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
6
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
7 * Redistribution and use in source and binary forms, with or without
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
8 * modification, are permitted provided that the following conditions
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
9 * are met:
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
10 * 1. Redistributions of source code must retain the above copyright
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
15 *
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
26 * SUCH DAMAGE.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
27 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
28
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
29 #include <sys/time.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
30
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
31 #include <assert.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
32 #include <errno.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
33 #include <limits.h>
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
34 #include <poll.h>
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
35 #include <stdbool.h>
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
36 #include <signal.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
37 #include <stdint.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
38 #include <stdlib.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
39 #include <string.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
40 #include <unistd.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
41
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
42 /* config.h should define HAVE_PPOLL, etc. */
4036
44392110a71d Default eloop to epoll on Solaris.
Roy Marples <roy@marples.name>
parents: 4028
diff changeset
43 #if defined(HAVE_CONFIG_H) && !defined(NO_CONFIG_H)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
44 #include "config.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
45 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
46
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
47 #if defined(HAVE_PPOLL)
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
48 #elif defined(HAVE_POLLTS)
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
49 #define ppoll pollts
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
50 #elif !defined(HAVE_PSELECT)
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
51 #pragma message("Compiling eloop with pselect(2) support.")
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
52 #define HAVE_PSELECT
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
53 #define ppoll eloop_ppoll
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
54 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
55
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
56 #include "eloop.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
57
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
58 #ifndef UNUSED
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
59 #define UNUSED(a) (void)((a))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
60 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
61 #ifndef __unused
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
62 #ifdef __GNUC__
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
63 #define __unused __attribute__((__unused__))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
64 #else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
65 #define __unused
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
66 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
67 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
68
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
69 #ifdef HAVE_PSELECT
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
70 #include <sys/select.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
71 #endif
4038
43d52698a334 If no eloop mechanism has been selected for us, print a message
Roy Marples <roy@marples.name>
parents: 4036
diff changeset
72
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
73 /* Our structures require TAILQ macros, which really every libc should
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
74 * ship as they are useful beyond belief.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
75 * Sadly some libc's don't have sys/queue.h and some that do don't have
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
76 * the TAILQ_FOREACH macro. For those that don't, the application using
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
77 * this implementation will need to ship a working queue.h somewhere.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
78 * If we don't have sys/queue.h found in config.h, then
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
79 * allow QUEUE_H to override loading queue.h in the current directory. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
80 #ifndef TAILQ_FOREACH
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
81 #ifdef HAVE_SYS_QUEUE_H
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
82 #include <sys/queue.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
83 #elif defined(QUEUE_H)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
84 #define __QUEUE_HEADER(x) #x
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
85 #define _QUEUE_HEADER(x) __QUEUE_HEADER(x)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
86 #include _QUEUE_HEADER(QUEUE_H)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
87 #else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
88 #include "queue.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
89 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
90 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
91
5324
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
92 #ifdef ELOOP_DEBUG
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
93 #include <stdio.h>
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
94 #endif
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
95
5370
ca830b3b9a58 eloop: Add rationale for ELOOP_NSIGNALS
Roy Marples <roy@marples.name>
parents: 5369
diff changeset
96 /*
ca830b3b9a58 eloop: Add rationale for ELOOP_NSIGNALS
Roy Marples <roy@marples.name>
parents: 5369
diff changeset
97 * Allow a backlog of signals.
ca830b3b9a58 eloop: Add rationale for ELOOP_NSIGNALS
Roy Marples <roy@marples.name>
parents: 5369
diff changeset
98 * If you use many eloops in the same process, they should all
ca830b3b9a58 eloop: Add rationale for ELOOP_NSIGNALS
Roy Marples <roy@marples.name>
parents: 5369
diff changeset
99 * use the same signal handler or have the signal handler unset.
ca830b3b9a58 eloop: Add rationale for ELOOP_NSIGNALS
Roy Marples <roy@marples.name>
parents: 5369
diff changeset
100 * Otherwise the signal might not behave as expected.
ca830b3b9a58 eloop: Add rationale for ELOOP_NSIGNALS
Roy Marples <roy@marples.name>
parents: 5369
diff changeset
101 */
ca830b3b9a58 eloop: Add rationale for ELOOP_NSIGNALS
Roy Marples <roy@marples.name>
parents: 5369
diff changeset
102 #define ELOOP_NSIGNALS 5
5366
1d7408a4160b eloop: Try and survive a signal storm
Roy Marples <roy@marples.name>
parents: 5331
diff changeset
103
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
104 /*
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
105 * time_t is a signed integer of an unspecified size.
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
106 * To adjust for time_t wrapping, we need to work the maximum signed
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
107 * value and use that as a maximum.
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
108 */
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
109 #ifndef TIME_MAX
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
110 #define TIME_MAX ((1ULL << (sizeof(time_t) * NBBY - 1)) - 1)
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
111 #endif
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
112 /* The unsigned maximum is then simple - multiply by two and add one. */
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
113 #ifndef UTIME_MAX
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
114 #define UTIME_MAX (TIME_MAX * 2) + 1
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
115 #endif
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
116
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
117 struct eloop_event {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
118 TAILQ_ENTRY(eloop_event) next;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
119 int fd;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
120 void (*read_cb)(void *);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
121 void *read_cb_arg;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
122 void (*write_cb)(void *);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
123 void *write_cb_arg;
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
124 struct pollfd *pollfd;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
125 };
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
126
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
127 struct eloop_timeout {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
128 TAILQ_ENTRY(eloop_timeout) next;
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
129 unsigned int seconds;
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
130 unsigned int nseconds;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
131 void (*callback)(void *);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
132 void *arg;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
133 int queue;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
134 };
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
135
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
136 struct eloop {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
137 TAILQ_HEAD (event_head, eloop_event) events;
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
138 size_t nevents;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
139 struct event_head free_events;
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
140 bool events_need_setup;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
141
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
142 struct timespec now;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
143 TAILQ_HEAD (timeout_head, eloop_timeout) timeouts;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
144 struct timeout_head free_timeouts;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
145
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
146 const int *signals;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
147 size_t signals_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
148 void (*signal_cb)(int, void *);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
149 void *signal_cb_ctx;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
150
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
151 struct pollfd *fds;
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
152 size_t nfds;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
153
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
154 int exitnow;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
155 int exitcode;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
156 };
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
157
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
158 #ifdef HAVE_REALLOCARRAY
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
159 #define eloop_realloca reallocarray
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
160 #else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
161 /* Handy routing to check for potential overflow.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
162 * reallocarray(3) and reallocarr(3) are not portable. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
163 #define SQRT_SIZE_MAX (((size_t)1) << (sizeof(size_t) * CHAR_BIT / 2))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
164 static void *
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
165 eloop_realloca(void *ptr, size_t n, size_t size)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
166 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
167
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
168 if ((n | size) >= SQRT_SIZE_MAX && n > SIZE_MAX / size) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
169 errno = EOVERFLOW;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
170 return NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
171 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
172 return realloc(ptr, n * size);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
173 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
174 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
175
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
176 #ifdef HAVE_PSELECT
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
177 /* Wrapper around pselect, to imitate the ppoll call. */
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
178 static int
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
179 eloop_ppoll(struct pollfd * fds, nfds_t nfds,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
180 const struct timespec *ts, const sigset_t *sigmask)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
181 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
182 fd_set read_fds, write_fds;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
183 nfds_t n;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
184 int maxfd, r;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
185
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
186 FD_ZERO(&read_fds);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
187 FD_ZERO(&write_fds);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
188 maxfd = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
189 for (n = 0; n < nfds; n++) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
190 if (fds[n].events & POLLIN) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
191 FD_SET(fds[n].fd, &read_fds);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
192 if (fds[n].fd > maxfd)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
193 maxfd = fds[n].fd;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
194 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
195 if (fds[n].events & POLLOUT) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
196 FD_SET(fds[n].fd, &write_fds);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
197 if (fds[n].fd > maxfd)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
198 maxfd = fds[n].fd;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
199 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
200 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
201
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
202 r = pselect(maxfd + 1, &read_fds, &write_fds, NULL, ts, sigmask);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
203 if (r > 0) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
204 for (n = 0; n < nfds; n++) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
205 fds[n].revents =
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
206 FD_ISSET(fds[n].fd, &read_fds) ? POLLIN : 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
207 if (FD_ISSET(fds[n].fd, &write_fds))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
208 fds[n].revents |= POLLOUT;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
209 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
210 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
211
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
212 return r;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
213 }
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
214 #endif
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
215
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
216 unsigned long long
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
217 eloop_timespec_diff(const struct timespec *tsp, const struct timespec *usp,
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
218 unsigned int *nsp)
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
219 {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
220 unsigned long long tsecs, usecs, secs;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
221 long nsecs;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
222
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
223 if (tsp->tv_sec < 0) /* time wreapped */
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
224 tsecs = UTIME_MAX - (unsigned long long)(-tsp->tv_sec);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
225 else
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
226 tsecs = (unsigned long long)tsp->tv_sec;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
227 if (usp->tv_sec < 0) /* time wrapped */
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
228 usecs = UTIME_MAX - (unsigned long long)(-usp->tv_sec);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
229 else
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
230 usecs = (unsigned long long)usp->tv_sec;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
231
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
232 if (usecs > tsecs) /* time wrapped */
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
233 secs = (UTIME_MAX - usecs) + tsecs;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
234 else
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
235 secs = tsecs - usecs;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
236
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
237 nsecs = tsp->tv_nsec - usp->tv_nsec;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
238 if (nsecs < 0) {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
239 if (secs == 0)
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
240 nsecs = 0;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
241 else {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
242 secs--;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
243 nsecs += NSEC_PER_SEC;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
244 }
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
245 }
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
246 if (nsp != NULL)
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
247 *nsp = (unsigned int)nsecs;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
248 return secs;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
249 }
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
250
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
251 static void
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
252 eloop_reduce_timers(struct eloop *eloop)
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
253 {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
254 struct timespec now;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
255 unsigned long long secs;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
256 unsigned int nsecs;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
257 struct eloop_timeout *t;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
258
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
259 clock_gettime(CLOCK_MONOTONIC, &now);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
260 secs = eloop_timespec_diff(&now, &eloop->now, &nsecs);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
261
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
262 TAILQ_FOREACH(t, &eloop->timeouts, next) {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
263 if (secs > t->seconds) {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
264 t->seconds = 0;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
265 t->nseconds = 0;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
266 } else {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
267 t->seconds -= (unsigned int)secs;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
268 if (nsecs > t->nseconds) {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
269 if (t->seconds == 0)
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
270 t->nseconds = 0;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
271 else {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
272 t->seconds--;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
273 t->nseconds = NSEC_PER_SEC
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
274 - (nsecs - t->nseconds);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
275 }
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
276 } else
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
277 t->nseconds -= nsecs;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
278 }
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
279 }
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
280
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
281 eloop->now = now;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
282 }
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
283
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
284 static void
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
285 eloop_event_setup_fds(struct eloop *eloop)
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
286 {
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
287 struct eloop_event *e, *ne;
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
288 struct pollfd *pfd;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
289
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
290 pfd = eloop->fds;
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
291 TAILQ_FOREACH_SAFE(e, &eloop->events, next, ne) {
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
292 if (e->fd == -1) {
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
293 TAILQ_REMOVE(&eloop->events, e, next);
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
294 TAILQ_INSERT_TAIL(&eloop->free_events, e, next);
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
295 continue;
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
296 }
5324
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
297 #ifdef ELOOP_DEBUG
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
298 fprintf(stderr, "%s(%d) fd=%d, rcb=%p, wcb=%p\n",
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
299 __func__, getpid(), e->fd, e->read_cb, e->write_cb);
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
300 #endif
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
301 e->pollfd = pfd;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
302 pfd->fd = e->fd;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
303 pfd->events = 0;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
304 if (e->read_cb != NULL)
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
305 pfd->events |= POLLIN;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
306 if (e->write_cb != NULL)
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
307 pfd->events |= POLLOUT;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
308 pfd->revents = 0;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
309 pfd++;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
310 }
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
311 eloop->events_need_setup = false;
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
312 }
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
313
5331
d075e31eb148 privsep: For Linux and Solaris, set RLIMIT_NOFILES to nevents
Roy Marples <roy@marples.name>
parents: 5325
diff changeset
314 size_t
d075e31eb148 privsep: For Linux and Solaris, set RLIMIT_NOFILES to nevents
Roy Marples <roy@marples.name>
parents: 5325
diff changeset
315 eloop_event_count(const struct eloop *eloop)
d075e31eb148 privsep: For Linux and Solaris, set RLIMIT_NOFILES to nevents
Roy Marples <roy@marples.name>
parents: 5325
diff changeset
316 {
d075e31eb148 privsep: For Linux and Solaris, set RLIMIT_NOFILES to nevents
Roy Marples <roy@marples.name>
parents: 5325
diff changeset
317
d075e31eb148 privsep: For Linux and Solaris, set RLIMIT_NOFILES to nevents
Roy Marples <roy@marples.name>
parents: 5325
diff changeset
318 return eloop->nevents;
d075e31eb148 privsep: For Linux and Solaris, set RLIMIT_NOFILES to nevents
Roy Marples <roy@marples.name>
parents: 5325
diff changeset
319 }
d075e31eb148 privsep: For Linux and Solaris, set RLIMIT_NOFILES to nevents
Roy Marples <roy@marples.name>
parents: 5325
diff changeset
320
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
321 int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
322 eloop_event_add_rw(struct eloop *eloop, int fd,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
323 void (*read_cb)(void *), void *read_cb_arg,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
324 void (*write_cb)(void *), void *write_cb_arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
325 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
326 struct eloop_event *e;
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
327 struct pollfd *pfd;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
328
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
329 assert(eloop != NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
330 assert(read_cb != NULL || write_cb != NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
331 if (fd == -1) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
332 errno = EINVAL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
333 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
334 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
335
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
336 TAILQ_FOREACH(e, &eloop->events, next) {
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
337 if (e->fd == fd)
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
338 break;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
339 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
340
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
341 if (e == NULL) {
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
342 if (eloop->nevents + 1 > eloop->nfds) {
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
343 pfd = eloop_realloca(eloop->fds, eloop->nevents + 1,
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
344 sizeof(*pfd));
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
345 if (pfd == NULL)
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
346 return -1;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
347 eloop->fds = pfd;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
348 eloop->nfds++;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
349 }
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
350
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
351 e = TAILQ_FIRST(&eloop->free_events);
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
352 if (e != NULL)
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
353 TAILQ_REMOVE(&eloop->free_events, e, next);
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
354 else {
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
355 e = malloc(sizeof(*e));
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
356 if (e == NULL)
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
357 return -1;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
358 }
5323
742ab5ed32b8 eloop: if we take a free event, add it to the main queue
Roy Marples <roy@marples.name>
parents: 5301
diff changeset
359 TAILQ_INSERT_HEAD(&eloop->events, e, next);
5325
896eb853623a eloop: Fix making the initial event listener
Roy Marples <roy@marples.name>
parents: 5324
diff changeset
360 eloop->nevents++;
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
361 e->fd = fd;
5325
896eb853623a eloop: Fix making the initial event listener
Roy Marples <roy@marples.name>
parents: 5324
diff changeset
362 e->read_cb = read_cb;
896eb853623a eloop: Fix making the initial event listener
Roy Marples <roy@marples.name>
parents: 5324
diff changeset
363 e->read_cb_arg = read_cb_arg;
896eb853623a eloop: Fix making the initial event listener
Roy Marples <roy@marples.name>
parents: 5324
diff changeset
364 e->write_cb = write_cb;
896eb853623a eloop: Fix making the initial event listener
Roy Marples <roy@marples.name>
parents: 5324
diff changeset
365 e->write_cb_arg = write_cb_arg;
896eb853623a eloop: Fix making the initial event listener
Roy Marples <roy@marples.name>
parents: 5324
diff changeset
366 goto setup;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
367 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
368
5324
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
369 if (read_cb) {
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
370 e->read_cb = read_cb;
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
371 e->read_cb_arg = read_cb_arg;
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
372 }
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
373 if (write_cb) {
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
374 e->write_cb = write_cb;
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
375 e->write_cb_arg = write_cb_arg;
e5cbedfa3dd9 eloop: Don't remove existing callbacks when adding events
Roy Marples <roy@marples.name>
parents: 5323
diff changeset
376 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
377
5325
896eb853623a eloop: Fix making the initial event listener
Roy Marples <roy@marples.name>
parents: 5324
diff changeset
378 setup:
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
379 e->pollfd = NULL;
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
380 eloop->events_need_setup = true;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
381 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
382 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
383
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
384 int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
385 eloop_event_add(struct eloop *eloop, int fd,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
386 void (*read_cb)(void *), void *read_cb_arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
387 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
388
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
389 return eloop_event_add_rw(eloop, fd, read_cb, read_cb_arg, NULL, NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
390 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
391
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
392 int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
393 eloop_event_add_w(struct eloop *eloop, int fd,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
394 void (*write_cb)(void *), void *write_cb_arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
395 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
396
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
397 return eloop_event_add_rw(eloop, fd, NULL,NULL, write_cb, write_cb_arg);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
398 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
399
4101
5b2ce8934993 Add a return value to deleteing events and timers so we know
Roy Marples <roy@marples.name>
parents: 4038
diff changeset
400 int
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
401 eloop_event_delete_write(struct eloop *eloop, int fd, int write_only)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
402 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
403 struct eloop_event *e;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
404
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
405 assert(eloop != NULL);
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
406 if (fd == -1) {
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
407 errno = EINVAL;
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
408 return -1;
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
409 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
410
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
411 TAILQ_FOREACH(e, &eloop->events, next) {
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
412 if (e->fd == fd)
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
413 break;
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
414 }
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
415 if (e == NULL) {
4101
5b2ce8934993 Add a return value to deleteing events and timers so we know
Roy Marples <roy@marples.name>
parents: 4038
diff changeset
416 errno = ENOENT;
5b2ce8934993 Add a return value to deleteing events and timers so we know
Roy Marples <roy@marples.name>
parents: 4038
diff changeset
417 return -1;
5b2ce8934993 Add a return value to deleteing events and timers so we know
Roy Marples <roy@marples.name>
parents: 4038
diff changeset
418 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
419
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
420 if (write_only) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
421 if (e->read_cb == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
422 goto remove;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
423 e->write_cb = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
424 e->write_cb_arg = NULL;
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
425 if (e->pollfd != NULL) {
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
426 e->pollfd->events &= ~POLLOUT;
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
427 e->pollfd->revents &= ~POLLOUT;
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
428 }
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
429 return 1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
430 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
431
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
432 remove:
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
433 e->fd = -1;
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
434 eloop->nevents--;
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
435 eloop->events_need_setup = true;
4101
5b2ce8934993 Add a return value to deleteing events and timers so we know
Roy Marples <roy@marples.name>
parents: 4038
diff changeset
436 return 1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
437 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
438
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
439 /*
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
440 * This implementation should cope with UINT_MAX seconds on a system
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
441 * where time_t is INT32_MAX. It should also cope with the monotonic timer
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
442 * wrapping, although this is highly unlikely.
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
443 * unsigned int should match or be greater than any on wire specified timeout.
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
444 */
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
445 static int
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
446 eloop_q_timeout_add(struct eloop *eloop, int queue,
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
447 unsigned int seconds, unsigned int nseconds,
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
448 void (*callback)(void *), void *arg)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
449 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
450 struct eloop_timeout *t, *tt = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
451
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
452 assert(eloop != NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
453 assert(callback != NULL);
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
454 assert(nseconds <= NSEC_PER_SEC);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
455
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
456 /* Remove existing timeout if present. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
457 TAILQ_FOREACH(t, &eloop->timeouts, next) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
458 if (t->callback == callback && t->arg == arg) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
459 TAILQ_REMOVE(&eloop->timeouts, t, next);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
460 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
461 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
462 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
463
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
464 if (t == NULL) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
465 /* No existing, so allocate or grab one from the free pool. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
466 if ((t = TAILQ_FIRST(&eloop->free_timeouts))) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
467 TAILQ_REMOVE(&eloop->free_timeouts, t, next);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
468 } else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
469 if ((t = malloc(sizeof(*t))) == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
470 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
471 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
472 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
473
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
474 eloop_reduce_timers(eloop);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
475
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
476 t->seconds = seconds;
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
477 t->nseconds = nseconds;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
478 t->callback = callback;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
479 t->arg = arg;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
480 t->queue = queue;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
481
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
482 /* The timeout list should be in chronological order,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
483 * soonest first. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
484 TAILQ_FOREACH(tt, &eloop->timeouts, next) {
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
485 if (t->seconds < tt->seconds ||
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
486 (t->seconds == tt->seconds && t->nseconds < tt->nseconds))
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
487 {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
488 TAILQ_INSERT_BEFORE(tt, t, next);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
489 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
490 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
491 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
492 TAILQ_INSERT_TAIL(&eloop->timeouts, t, next);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
493 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
494 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
495
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
496 int
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
497 eloop_q_timeout_add_tv(struct eloop *eloop, int queue,
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
498 const struct timespec *when, void (*callback)(void *), void *arg)
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
499 {
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
500
4973
61580c15b4fb Linux: Silence a warning on 32-bit Linux
Roy Marples <roy@marples.name>
parents: 4941
diff changeset
501 if (when->tv_sec < 0 || (unsigned long)when->tv_sec > UINT_MAX) {
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
502 errno = EINVAL;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
503 return -1;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
504 }
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
505 if (when->tv_nsec < 0 || when->tv_nsec > NSEC_PER_SEC) {
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
506 errno = EINVAL;
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
507 return -1;
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
508 }
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
509
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
510 return eloop_q_timeout_add(eloop, queue,
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
511 (unsigned int)when->tv_sec, (unsigned int)when->tv_sec,
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
512 callback, arg);
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
513 }
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
514
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
515 int
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
516 eloop_q_timeout_add_sec(struct eloop *eloop, int queue, unsigned int seconds,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
517 void (*callback)(void *), void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
518 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
519
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
520 return eloop_q_timeout_add(eloop, queue, seconds, 0, callback, arg);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
521 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
522
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
523 int
4925
74b140568feb DHCP6: Calulate ReTransmission using milliseconds
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
524 eloop_q_timeout_add_msec(struct eloop *eloop, int queue, unsigned long when,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
525 void (*callback)(void *), void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
526 {
4925
74b140568feb DHCP6: Calulate ReTransmission using milliseconds
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
527 unsigned long seconds, nseconds;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
528
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
529 seconds = when / MSEC_PER_SEC;
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
530 if (seconds > UINT_MAX) {
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
531 errno = EINVAL;
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
532 return -1;
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
533 }
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
534
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
535 nseconds = (when % MSEC_PER_SEC) * NSEC_PER_MSEC;
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
536 return eloop_q_timeout_add(eloop, queue,
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
537 (unsigned int)seconds, (unsigned int)nseconds, callback, arg);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
538 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
539
4101
5b2ce8934993 Add a return value to deleteing events and timers so we know
Roy Marples <roy@marples.name>
parents: 4038
diff changeset
540 int
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
541 eloop_q_timeout_delete(struct eloop *eloop, int queue,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
542 void (*callback)(void *), void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
543 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
544 struct eloop_timeout *t, *tt;
4101
5b2ce8934993 Add a return value to deleteing events and timers so we know
Roy Marples <roy@marples.name>
parents: 4038
diff changeset
545 int n;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
546
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
547 assert(eloop != NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
548
4101
5b2ce8934993 Add a return value to deleteing events and timers so we know
Roy Marples <roy@marples.name>
parents: 4038
diff changeset
549 n = 0;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
550 TAILQ_FOREACH_SAFE(t, &eloop->timeouts, next, tt) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
551 if ((queue == 0 || t->queue == queue) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
552 t->arg == arg &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
553 (!callback || t->callback == callback))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
554 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
555 TAILQ_REMOVE(&eloop->timeouts, t, next);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
556 TAILQ_INSERT_TAIL(&eloop->free_timeouts, t, next);
4101
5b2ce8934993 Add a return value to deleteing events and timers so we know
Roy Marples <roy@marples.name>
parents: 4038
diff changeset
557 n++;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
558 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
559 }
4101
5b2ce8934993 Add a return value to deleteing events and timers so we know
Roy Marples <roy@marples.name>
parents: 4038
diff changeset
560 return n;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
561 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
562
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
563 void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
564 eloop_exit(struct eloop *eloop, int code)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
565 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
566
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
567 assert(eloop != NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
568
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
569 eloop->exitcode = code;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
570 eloop->exitnow = 1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
571 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
572
5297
477edd06fea7 privsep: harden process handling
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
573 void
477edd06fea7 privsep: harden process handling
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
574 eloop_enter(struct eloop *eloop)
477edd06fea7 privsep: harden process handling
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
575 {
477edd06fea7 privsep: harden process handling
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
576
477edd06fea7 privsep: harden process handling
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
577 eloop->exitnow = 0;
477edd06fea7 privsep: harden process handling
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
578 }
477edd06fea7 privsep: harden process handling
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
579
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
580 void
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
581 eloop_signal_set_cb(struct eloop *eloop,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
582 const int *signals, size_t signals_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
583 void (*signal_cb)(int, void *), void *signal_cb_ctx)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
584 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
585
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
586 assert(eloop != NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
587
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
588 eloop->signals = signals;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
589 eloop->signals_len = signals_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
590 eloop->signal_cb = signal_cb;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
591 eloop->signal_cb_ctx = signal_cb_ctx;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
592 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
593
5366
1d7408a4160b eloop: Try and survive a signal storm
Roy Marples <roy@marples.name>
parents: 5331
diff changeset
594 static volatile int _eloop_sig[ELOOP_NSIGNALS];
1d7408a4160b eloop: Try and survive a signal storm
Roy Marples <roy@marples.name>
parents: 5331
diff changeset
595 static volatile size_t _eloop_nsig;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
596
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
597 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
598 eloop_signal3(int sig, __unused siginfo_t *siginfo, __unused void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
599 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
600
5366
1d7408a4160b eloop: Try and survive a signal storm
Roy Marples <roy@marples.name>
parents: 5331
diff changeset
601 if (_eloop_nsig == __arraycount(_eloop_sig)) {
5368
cffaa4ac8bbc eloop: Guard diagnostic in prior with ELOOP_DEBUG
Roy Marples <roy@marples.name>
parents: 5366
diff changeset
602 #ifdef ELOOP_DEBUG
cffaa4ac8bbc eloop: Guard diagnostic in prior with ELOOP_DEBUG
Roy Marples <roy@marples.name>
parents: 5366
diff changeset
603 fprintf(stderr, "%s: signal storm, discarding signal %d\n",
5366
1d7408a4160b eloop: Try and survive a signal storm
Roy Marples <roy@marples.name>
parents: 5331
diff changeset
604 __func__, sig);
5368
cffaa4ac8bbc eloop: Guard diagnostic in prior with ELOOP_DEBUG
Roy Marples <roy@marples.name>
parents: 5366
diff changeset
605 #endif
5366
1d7408a4160b eloop: Try and survive a signal storm
Roy Marples <roy@marples.name>
parents: 5331
diff changeset
606 return;
1d7408a4160b eloop: Try and survive a signal storm
Roy Marples <roy@marples.name>
parents: 5331
diff changeset
607 }
1d7408a4160b eloop: Try and survive a signal storm
Roy Marples <roy@marples.name>
parents: 5331
diff changeset
608
1d7408a4160b eloop: Try and survive a signal storm
Roy Marples <roy@marples.name>
parents: 5331
diff changeset
609 _eloop_sig[_eloop_nsig++] = sig;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
610 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
611
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
612 int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
613 eloop_signal_mask(struct eloop *eloop, sigset_t *oldset)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
614 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
615 sigset_t newset;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
616 size_t i;
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
617 struct sigaction sa = {
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
618 .sa_sigaction = eloop_signal3,
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
619 .sa_flags = SA_SIGINFO,
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
620 };
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
621
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
622 assert(eloop != NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
623
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
624 sigemptyset(&newset);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
625 for (i = 0; i < eloop->signals_len; i++)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
626 sigaddset(&newset, eloop->signals[i]);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
627 if (sigprocmask(SIG_SETMASK, &newset, oldset) == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
628 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
629
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
630 sigemptyset(&sa.sa_mask);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
631
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
632 for (i = 0; i < eloop->signals_len; i++) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
633 if (sigaction(eloop->signals[i], &sa, NULL) == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
634 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
635 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
636 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
637 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
638
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
639 struct eloop *
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
640 eloop_new(void)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
641 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
642 struct eloop *eloop;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
643
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
644 eloop = calloc(1, sizeof(*eloop));
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
645 if (eloop == NULL)
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
646 return NULL;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
647
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
648 /* Check we have a working monotonic clock. */
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
649 if (clock_gettime(CLOCK_MONOTONIC, &eloop->now) == -1) {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
650 free(eloop);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
651 return NULL;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
652 }
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
653
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
654 TAILQ_INIT(&eloop->events);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
655 TAILQ_INIT(&eloop->free_events);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
656 TAILQ_INIT(&eloop->timeouts);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
657 TAILQ_INIT(&eloop->free_timeouts);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
658 eloop->exitcode = EXIT_FAILURE;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
659
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
660 return eloop;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
661 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
662
4783
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
663 void
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
664 eloop_clear(struct eloop *eloop)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
665 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
666 struct eloop_event *e;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
667 struct eloop_timeout *t;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
668
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
669 if (eloop == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
670 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
671
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
672 eloop->nevents = 0;
4783
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
673 eloop->signals = NULL;
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
674 eloop->signals_len = 0;
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
675
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
676 while ((e = TAILQ_FIRST(&eloop->events))) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
677 TAILQ_REMOVE(&eloop->events, e, next);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
678 free(e);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
679 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
680 while ((e = TAILQ_FIRST(&eloop->free_events))) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
681 TAILQ_REMOVE(&eloop->free_events, e, next);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
682 free(e);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
683 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
684 while ((t = TAILQ_FIRST(&eloop->timeouts))) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
685 TAILQ_REMOVE(&eloop->timeouts, t, next);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
686 free(t);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
687 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
688 while ((t = TAILQ_FIRST(&eloop->free_timeouts))) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
689 TAILQ_REMOVE(&eloop->free_timeouts, t, next);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
690 free(t);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
691 }
4783
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
692
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
693 free(eloop->fds);
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
694 eloop->fds = NULL;
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
695 eloop->nfds = 0;
4783
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
696 }
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
697
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
698 void
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
699 eloop_free(struct eloop *eloop)
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
700 {
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
701
01894b320306 eloop: Add eloop_clear function
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
702 eloop_clear(eloop);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
703 free(eloop);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
704 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
705
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
706 int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
707 eloop_start(struct eloop *eloop, sigset_t *signals)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
708 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
709 int n;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
710 struct eloop_event *e;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
711 struct eloop_timeout *t;
4931
ebcc4f883a5c eloop: fix some warnings
Roy Marples <roy@marples.name>
parents: 4925
diff changeset
712 struct timespec ts, *tsp;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
713
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
714 assert(eloop != NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
715
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
716 for (;;) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
717 if (eloop->exitnow)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
718 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
719
5425
9edfc000a89b privsep: Only the master process accepts signals
Roy Marples <roy@marples.name>
parents: 5370
diff changeset
720 if (_eloop_nsig != 0) {
5366
1d7408a4160b eloop: Try and survive a signal storm
Roy Marples <roy@marples.name>
parents: 5331
diff changeset
721 n = _eloop_sig[--_eloop_nsig];
5425
9edfc000a89b privsep: Only the master process accepts signals
Roy Marples <roy@marples.name>
parents: 5370
diff changeset
722 if (eloop->signal_cb != NULL)
9edfc000a89b privsep: Only the master process accepts signals
Roy Marples <roy@marples.name>
parents: 5370
diff changeset
723 eloop->signal_cb(n, eloop->signal_cb_ctx);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
724 continue;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
725 }
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
726
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
727 t = TAILQ_FIRST(&eloop->timeouts);
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
728 if (t == NULL && eloop->nevents == 0)
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
729 break;
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
730
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5016
diff changeset
731 if (t != NULL)
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5016
diff changeset
732 eloop_reduce_timers(eloop);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5016
diff changeset
733
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
734 if (t != NULL && t->seconds == 0 && t->nseconds == 0) {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
735 TAILQ_REMOVE(&eloop->timeouts, t, next);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
736 t->callback(t->arg);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
737 TAILQ_INSERT_TAIL(&eloop->free_timeouts, t, next);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
738 continue;
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
739 }
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
740
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
741 if (t != NULL) {
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
742 if (t->seconds > INT_MAX) {
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
743 ts.tv_sec = (time_t)INT_MAX;
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
744 ts.tv_nsec = 0;
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
745 } else {
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
746 ts.tv_sec = (time_t)t->seconds;
5016
4f2b9a29d4af eloop: cast away a compile warning
Roy Marples <roy@marples.name>
parents: 4973
diff changeset
747 ts.tv_nsec = (long)t->nseconds;
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
748 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
749 tsp = &ts;
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
750 } else
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
751 tsp = NULL;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
752
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
753 if (eloop->events_need_setup)
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
754 eloop_event_setup_fds(eloop);
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
755
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
756 n = ppoll(eloop->fds, (nfds_t)eloop->nevents, tsp, signals);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
757 if (n == -1) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
758 if (errno == EINTR)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
759 continue;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
760 return -errno;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
761 }
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
762 if (n == 0)
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
763 continue;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
764
5301
e6f1372f2cf0 eloop: Just use ppoll(2)
Roy Marples <roy@marples.name>
parents: 5297
diff changeset
765 TAILQ_FOREACH(e, &eloop->events, next) {
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
766 /* Skip freshly added events */
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
767 if (e->pollfd == NULL)
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
768 continue;
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
769 if (e->pollfd->revents)
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
770 n--;
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
771 if (e->fd != -1 && e->pollfd->revents & POLLOUT) {
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
772 if (e->write_cb != NULL)
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
773 e->write_cb(e->write_cb_arg);
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
774 }
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
775 if (e->fd != -1 &&
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
776 e->pollfd != NULL && e->pollfd->revents)
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
777 {
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
778 if (e->read_cb != NULL)
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
779 e->read_cb(e->read_cb_arg);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
780 }
5578
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
781 if (n == 0)
57e4bf2cc9e7 eloop: Allow eloop to process all fds returned from poll(2)
Roy Marples <roy@marples.name>
parents: 5425
diff changeset
782 break;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
783 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
784 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
785
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
786 return eloop->exitcode;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
787 }