1*3b7c5e8aSDaniel Lezcano // SPDX-License-Identifier: LGPL-2.1+
2*3b7c5e8aSDaniel Lezcano // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
3*3b7c5e8aSDaniel Lezcano #include <stdlib.h>
4*3b7c5e8aSDaniel Lezcano #include <errno.h>
5*3b7c5e8aSDaniel Lezcano #include <unistd.h>
6*3b7c5e8aSDaniel Lezcano #include <signal.h>
7*3b7c5e8aSDaniel Lezcano #include <sys/epoll.h>
8*3b7c5e8aSDaniel Lezcano #include "mainloop.h"
9*3b7c5e8aSDaniel Lezcano #include "log.h"
10*3b7c5e8aSDaniel Lezcano
11*3b7c5e8aSDaniel Lezcano static int epfd = -1;
12*3b7c5e8aSDaniel Lezcano static unsigned short nrhandler;
13*3b7c5e8aSDaniel Lezcano static sig_atomic_t exit_mainloop;
14*3b7c5e8aSDaniel Lezcano
15*3b7c5e8aSDaniel Lezcano struct mainloop_data {
16*3b7c5e8aSDaniel Lezcano mainloop_callback_t cb;
17*3b7c5e8aSDaniel Lezcano void *data;
18*3b7c5e8aSDaniel Lezcano int fd;
19*3b7c5e8aSDaniel Lezcano };
20*3b7c5e8aSDaniel Lezcano
21*3b7c5e8aSDaniel Lezcano static struct mainloop_data **mds;
22*3b7c5e8aSDaniel Lezcano
23*3b7c5e8aSDaniel Lezcano #define MAX_EVENTS 10
24*3b7c5e8aSDaniel Lezcano
mainloop(unsigned int timeout)25*3b7c5e8aSDaniel Lezcano int mainloop(unsigned int timeout)
26*3b7c5e8aSDaniel Lezcano {
27*3b7c5e8aSDaniel Lezcano int i, nfds;
28*3b7c5e8aSDaniel Lezcano struct epoll_event events[MAX_EVENTS];
29*3b7c5e8aSDaniel Lezcano struct mainloop_data *md;
30*3b7c5e8aSDaniel Lezcano
31*3b7c5e8aSDaniel Lezcano if (epfd < 0)
32*3b7c5e8aSDaniel Lezcano return -1;
33*3b7c5e8aSDaniel Lezcano
34*3b7c5e8aSDaniel Lezcano for (;;) {
35*3b7c5e8aSDaniel Lezcano
36*3b7c5e8aSDaniel Lezcano nfds = epoll_wait(epfd, events, MAX_EVENTS, timeout);
37*3b7c5e8aSDaniel Lezcano
38*3b7c5e8aSDaniel Lezcano if (exit_mainloop || !nfds)
39*3b7c5e8aSDaniel Lezcano return 0;
40*3b7c5e8aSDaniel Lezcano
41*3b7c5e8aSDaniel Lezcano if (nfds < 0) {
42*3b7c5e8aSDaniel Lezcano if (errno == EINTR)
43*3b7c5e8aSDaniel Lezcano continue;
44*3b7c5e8aSDaniel Lezcano return -1;
45*3b7c5e8aSDaniel Lezcano }
46*3b7c5e8aSDaniel Lezcano
47*3b7c5e8aSDaniel Lezcano for (i = 0; i < nfds; i++) {
48*3b7c5e8aSDaniel Lezcano md = events[i].data.ptr;
49*3b7c5e8aSDaniel Lezcano
50*3b7c5e8aSDaniel Lezcano if (md->cb(md->fd, md->data) > 0)
51*3b7c5e8aSDaniel Lezcano return 0;
52*3b7c5e8aSDaniel Lezcano }
53*3b7c5e8aSDaniel Lezcano }
54*3b7c5e8aSDaniel Lezcano }
55*3b7c5e8aSDaniel Lezcano
mainloop_add(int fd,mainloop_callback_t cb,void * data)56*3b7c5e8aSDaniel Lezcano int mainloop_add(int fd, mainloop_callback_t cb, void *data)
57*3b7c5e8aSDaniel Lezcano {
58*3b7c5e8aSDaniel Lezcano struct epoll_event ev = {
59*3b7c5e8aSDaniel Lezcano .events = EPOLLIN,
60*3b7c5e8aSDaniel Lezcano };
61*3b7c5e8aSDaniel Lezcano
62*3b7c5e8aSDaniel Lezcano struct mainloop_data *md;
63*3b7c5e8aSDaniel Lezcano
64*3b7c5e8aSDaniel Lezcano if (fd >= nrhandler) {
65*3b7c5e8aSDaniel Lezcano mds = realloc(mds, sizeof(*mds) * (fd + 1));
66*3b7c5e8aSDaniel Lezcano if (!mds)
67*3b7c5e8aSDaniel Lezcano return -1;
68*3b7c5e8aSDaniel Lezcano nrhandler = fd + 1;
69*3b7c5e8aSDaniel Lezcano }
70*3b7c5e8aSDaniel Lezcano
71*3b7c5e8aSDaniel Lezcano md = malloc(sizeof(*md));
72*3b7c5e8aSDaniel Lezcano if (!md)
73*3b7c5e8aSDaniel Lezcano return -1;
74*3b7c5e8aSDaniel Lezcano
75*3b7c5e8aSDaniel Lezcano md->data = data;
76*3b7c5e8aSDaniel Lezcano md->cb = cb;
77*3b7c5e8aSDaniel Lezcano md->fd = fd;
78*3b7c5e8aSDaniel Lezcano
79*3b7c5e8aSDaniel Lezcano mds[fd] = md;
80*3b7c5e8aSDaniel Lezcano ev.data.ptr = md;
81*3b7c5e8aSDaniel Lezcano
82*3b7c5e8aSDaniel Lezcano if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
83*3b7c5e8aSDaniel Lezcano free(md);
84*3b7c5e8aSDaniel Lezcano return -1;
85*3b7c5e8aSDaniel Lezcano }
86*3b7c5e8aSDaniel Lezcano
87*3b7c5e8aSDaniel Lezcano return 0;
88*3b7c5e8aSDaniel Lezcano }
89*3b7c5e8aSDaniel Lezcano
mainloop_del(int fd)90*3b7c5e8aSDaniel Lezcano int mainloop_del(int fd)
91*3b7c5e8aSDaniel Lezcano {
92*3b7c5e8aSDaniel Lezcano if (fd >= nrhandler)
93*3b7c5e8aSDaniel Lezcano return -1;
94*3b7c5e8aSDaniel Lezcano
95*3b7c5e8aSDaniel Lezcano if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) < 0)
96*3b7c5e8aSDaniel Lezcano return -1;
97*3b7c5e8aSDaniel Lezcano
98*3b7c5e8aSDaniel Lezcano free(mds[fd]);
99*3b7c5e8aSDaniel Lezcano
100*3b7c5e8aSDaniel Lezcano return 0;
101*3b7c5e8aSDaniel Lezcano }
102*3b7c5e8aSDaniel Lezcano
mainloop_init(void)103*3b7c5e8aSDaniel Lezcano int mainloop_init(void)
104*3b7c5e8aSDaniel Lezcano {
105*3b7c5e8aSDaniel Lezcano epfd = epoll_create(2);
106*3b7c5e8aSDaniel Lezcano if (epfd < 0)
107*3b7c5e8aSDaniel Lezcano return -1;
108*3b7c5e8aSDaniel Lezcano
109*3b7c5e8aSDaniel Lezcano return 0;
110*3b7c5e8aSDaniel Lezcano }
111*3b7c5e8aSDaniel Lezcano
mainloop_exit(void)112*3b7c5e8aSDaniel Lezcano void mainloop_exit(void)
113*3b7c5e8aSDaniel Lezcano {
114*3b7c5e8aSDaniel Lezcano exit_mainloop = 1;
115*3b7c5e8aSDaniel Lezcano }
116*3b7c5e8aSDaniel Lezcano
mainloop_fini(void)117*3b7c5e8aSDaniel Lezcano void mainloop_fini(void)
118*3b7c5e8aSDaniel Lezcano {
119*3b7c5e8aSDaniel Lezcano close(epfd);
120*3b7c5e8aSDaniel Lezcano }
121