2 0a53bd62 2023-08-16 op * This is free and unencumbered software released into the public domain.
4 0a53bd62 2023-08-16 op * Anyone is free to copy, modify, publish, use, compile, sell, or
5 0a53bd62 2023-08-16 op * distribute this software, either in source code form or as a compiled
6 0a53bd62 2023-08-16 op * binary, for any purpose, commercial or non-commercial, and by any
9 0a53bd62 2023-08-16 op * In jurisdictions that recognize copyright laws, the author or authors
10 0a53bd62 2023-08-16 op * of this software dedicate any and all copyright interest in the
11 0a53bd62 2023-08-16 op * software to the public domain. We make this dedication for the benefit
12 0a53bd62 2023-08-16 op * of the public at large and to the detriment of our heirs and
13 0a53bd62 2023-08-16 op * successors. We intend this dedication to be an overt act of
14 0a53bd62 2023-08-16 op * relinquishment in perpetuity of all present and future rights to this
15 0a53bd62 2023-08-16 op * software under copyright law.
17 0a53bd62 2023-08-16 op * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 0a53bd62 2023-08-16 op * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 0a53bd62 2023-08-16 op * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 0a53bd62 2023-08-16 op * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 0a53bd62 2023-08-16 op * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 0a53bd62 2023-08-16 op * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 0a53bd62 2023-08-16 op * OTHER DEALINGS IN THE SOFTWARE.
26 0a53bd62 2023-08-16 op #include "config.h"
28 0a53bd62 2023-08-16 op #include <errno.h>
29 0a53bd62 2023-08-16 op #include <fcntl.h>
30 0a53bd62 2023-08-16 op #include <limits.h>
31 0a53bd62 2023-08-16 op #include <poll.h>
32 0a53bd62 2023-08-16 op #include <signal.h>
33 0a53bd62 2023-08-16 op #include <stdlib.h>
34 27bf07a4 2023-08-16 op #include <time.h>
35 0a53bd62 2023-08-16 op #include <unistd.h>
37 0a53bd62 2023-08-16 op #include "ev.h"
40 0a53bd62 2023-08-16 op void (*cb)(int, int, void *);
44 0a53bd62 2023-08-16 op struct evbase {
47 0a53bd62 2023-08-16 op struct pollfd *pfds;
48 0a53bd62 2023-08-16 op size_t pfdlen;
50 0a53bd62 2023-08-16 op struct evcb *cbs;
53 0a53bd62 2023-08-16 op int sigpipe[2];
54 0a53bd62 2023-08-16 op struct evcb sigcb;
57 0a53bd62 2023-08-16 op struct evcb toutcb;
60 0a53bd62 2023-08-16 op static struct evbase *base;
61 0a53bd62 2023-08-16 op static int ev_stop;
64 0a53bd62 2023-08-16 op ev_resize(size_t len)
69 0a53bd62 2023-08-16 op t = recallocarray(base->pfds, base->pfdlen, len, sizeof(*base->pfds));
70 0a53bd62 2023-08-16 op if (t == NULL)
72 0a53bd62 2023-08-16 op base->pfds = t;
73 0a53bd62 2023-08-16 op base->pfdlen = len;
75 0a53bd62 2023-08-16 op for (i = base->len; i < len; ++i)
76 0a53bd62 2023-08-16 op base->pfds[i].fd = -1;
78 0a53bd62 2023-08-16 op t = recallocarray(base->cbs, base->cblen, len, sizeof(*base->cbs));
79 0a53bd62 2023-08-16 op if (t == NULL)
81 0a53bd62 2023-08-16 op base->cbs = t;
82 0a53bd62 2023-08-16 op base->cblen = len;
84 0a53bd62 2023-08-16 op base->len = len;
91 0a53bd62 2023-08-16 op if (base != NULL) {
92 0a53bd62 2023-08-16 op errno = EINVAL;
96 0a53bd62 2023-08-16 op if ((base = calloc(1, sizeof(*base))) == NULL)
99 0a53bd62 2023-08-16 op base->sigpipe[0] = -1;
100 0a53bd62 2023-08-16 op base->sigpipe[1] = -1;
101 0a53bd62 2023-08-16 op base->timeout = INFTIM;
103 0a53bd62 2023-08-16 op if (ev_resize(16) == -1) {
104 0a53bd62 2023-08-16 op free(base->pfds);
105 0a53bd62 2023-08-16 op free(base->cbs);
115 0a53bd62 2023-08-16 op ev_add(int fd, int ev, void (*cb)(int, int, void *), void *udata)
117 0a53bd62 2023-08-16 op if (fd >= base->len) {
118 0a53bd62 2023-08-16 op if (ev_resize(fd + 1) == -1)
122 0a53bd62 2023-08-16 op base->pfds[fd].fd = fd;
123 0a53bd62 2023-08-16 op base->pfds[fd].events = ev;
125 0a53bd62 2023-08-16 op base->cbs[fd].cb = cb;
126 0a53bd62 2023-08-16 op base->cbs[fd].udata = udata;
132 0a53bd62 2023-08-16 op ev_sigcatch(int signo)
134 0a53bd62 2023-08-16 op unsigned char s;
140 0a53bd62 2023-08-16 op * We should be able to write up to PIPE_BUF bytes without
144 0a53bd62 2023-08-16 op (void) write(base->sigpipe[1], &s, sizeof(s));
150 0a53bd62 2023-08-16 op ev_sigdispatch(int fd, int ev, void *data)
152 0a53bd62 2023-08-16 op unsigned char signo;
154 0a53bd62 2023-08-16 op if (read(fd, &signo, sizeof(signo)) != sizeof(signo))
157 0a53bd62 2023-08-16 op base->sigcb.cb(signo, 0, base->sigcb.udata);
161 0a53bd62 2023-08-16 op ev_signal(int sig, void (*cb)(int, int, void *), void *udata)
165 0a53bd62 2023-08-16 op if (base->sigpipe[0] == -1) {
166 dc3e25b1 2023-10-07 op /* pipe2(2) is not available everywhere... sigh */
167 dc3e25b1 2023-10-07 op if (pipe(base->sigpipe) == -1)
170 dc3e25b1 2023-10-07 op if ((flags = fcntl(base->sigpipe[1], F_GETFL)) == -1 ||
171 dc3e25b1 2023-10-07 op fcntl(base->sigpipe[1], F_SETFL, flags | O_NONBLOCK) == -1)
174 0a53bd62 2023-08-16 op if (ev_add(base->sigpipe[0], POLLIN, ev_sigdispatch, NULL)
179 0a53bd62 2023-08-16 op base->sigcb.cb = cb;
180 0a53bd62 2023-08-16 op base->sigcb.udata = udata;
182 0a53bd62 2023-08-16 op signal(sig, ev_sigcatch);
187 0a53bd62 2023-08-16 op ev_timer(const struct timeval *tv, void (*cb)(int, int, void*), void *udata)
189 0a53bd62 2023-08-16 op base->timeout = INFTIM;
191 0a53bd62 2023-08-16 op base->timeout = tv->tv_sec * 1000;
192 0a53bd62 2023-08-16 op base->timeout += tv->tv_usec / 1000;
195 0a53bd62 2023-08-16 op base->toutcb.cb = cb;
196 0a53bd62 2023-08-16 op base->toutcb.udata = udata;
202 0a53bd62 2023-08-16 op ev_timer_pending(void)
204 0a53bd62 2023-08-16 op return base->timeout != INFTIM;
208 0a53bd62 2023-08-16 op ev_del(int fd)
210 0a53bd62 2023-08-16 op if (fd >= base->len) {
211 0a53bd62 2023-08-16 op errno = ERANGE;
215 0a53bd62 2023-08-16 op base->pfds[fd].fd = -1;
216 0a53bd62 2023-08-16 op base->pfds[fd].events = 0;
218 0a53bd62 2023-08-16 op base->cbs[fd].cb = NULL;
219 0a53bd62 2023-08-16 op base->cbs[fd].udata = NULL;
225 0a53bd62 2023-08-16 op ev_loop(void)
227 0a53bd62 2023-08-16 op struct timespec elapsed, beg, end;
231 0a53bd62 2023-08-16 op while (!ev_stop) {
232 0a53bd62 2023-08-16 op clock_gettime(CLOCK_MONOTONIC, &beg);
233 0a53bd62 2023-08-16 op if ((n = poll(base->pfds, base->len, base->timeout)) == -1) {
234 0a53bd62 2023-08-16 op if (errno != EINTR)
237 0a53bd62 2023-08-16 op clock_gettime(CLOCK_MONOTONIC, &end);
239 0a53bd62 2023-08-16 op timespecsub(&end, &beg, &elapsed);
240 0a53bd62 2023-08-16 op em = elapsed.tv_sec * 1000 + elapsed.tv_nsec / 1000000;
241 0a53bd62 2023-08-16 op if (base->timeout != INFTIM) {
242 0a53bd62 2023-08-16 op if (base->timeout - em < 0 || n == 0) {
243 0a53bd62 2023-08-16 op base->timeout = INFTIM;
244 0a53bd62 2023-08-16 op base->toutcb.cb(-1, 0, base->toutcb.udata);
246 0a53bd62 2023-08-16 op base->timeout -= em;
249 0a53bd62 2023-08-16 op for (i = 0; i < base->len && n > 0 && !ev_stop; ++i) {
250 0a53bd62 2023-08-16 op if (base->pfds[i].fd == -1)
252 0a53bd62 2023-08-16 op if (base->pfds[i].revents & (POLLIN|POLLOUT|POLLHUP)) {
254 0a53bd62 2023-08-16 op base->cbs[i].cb(base->pfds[i].fd,
255 0a53bd62 2023-08-16 op base->pfds[i].revents,
256 0a53bd62 2023-08-16 op base->cbs[i].udata);
265 0a53bd62 2023-08-16 op ev_break(void)