1*3b7c5e8aSDaniel Lezcano // SPDX-License-Identifier: LGPL-2.1+
2*3b7c5e8aSDaniel Lezcano // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <[email protected]>
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 sig_atomic_t exit_mainloop;
13*3b7c5e8aSDaniel Lezcano
14*3b7c5e8aSDaniel Lezcano struct mainloop_data {
15*3b7c5e8aSDaniel Lezcano mainloop_callback_t cb;
16*3b7c5e8aSDaniel Lezcano void *data;
17*3b7c5e8aSDaniel Lezcano int fd;
18*3b7c5e8aSDaniel Lezcano };
19*3b7c5e8aSDaniel Lezcano
20*3b7c5e8aSDaniel Lezcano #define MAX_EVENTS 10
21*3b7c5e8aSDaniel Lezcano
mainloop(unsigned int timeout)22*3b7c5e8aSDaniel Lezcano int mainloop(unsigned int timeout)
23*3b7c5e8aSDaniel Lezcano {
24*3b7c5e8aSDaniel Lezcano int i, nfds;
25*3b7c5e8aSDaniel Lezcano struct epoll_event events[MAX_EVENTS];
26*3b7c5e8aSDaniel Lezcano struct mainloop_data *md;
27*3b7c5e8aSDaniel Lezcano
28*3b7c5e8aSDaniel Lezcano if (epfd < 0)
29*3b7c5e8aSDaniel Lezcano return -1;
30*3b7c5e8aSDaniel Lezcano
31*3b7c5e8aSDaniel Lezcano for (;;) {
32*3b7c5e8aSDaniel Lezcano
33*3b7c5e8aSDaniel Lezcano nfds = epoll_wait(epfd, events, MAX_EVENTS, timeout);
34*3b7c5e8aSDaniel Lezcano
35*3b7c5e8aSDaniel Lezcano if (exit_mainloop || !nfds)
36*3b7c5e8aSDaniel Lezcano return 0;
37*3b7c5e8aSDaniel Lezcano
38*3b7c5e8aSDaniel Lezcano if (nfds < 0) {
39*3b7c5e8aSDaniel Lezcano if (errno == EINTR)
40*3b7c5e8aSDaniel Lezcano continue;
41*3b7c5e8aSDaniel Lezcano return -1;
42*3b7c5e8aSDaniel Lezcano }
43*3b7c5e8aSDaniel Lezcano
44*3b7c5e8aSDaniel Lezcano for (i = 0; i < nfds; i++) {
45*3b7c5e8aSDaniel Lezcano md = events[i].data.ptr;
46*3b7c5e8aSDaniel Lezcano
47*3b7c5e8aSDaniel Lezcano if (md->cb(md->fd, md->data) > 0)
48*3b7c5e8aSDaniel Lezcano return 0;
49*3b7c5e8aSDaniel Lezcano }
50*3b7c5e8aSDaniel Lezcano }
51*3b7c5e8aSDaniel Lezcano }
52*3b7c5e8aSDaniel Lezcano
mainloop_add(int fd,mainloop_callback_t cb,void * data)53*3b7c5e8aSDaniel Lezcano int mainloop_add(int fd, mainloop_callback_t cb, void *data)
54*3b7c5e8aSDaniel Lezcano {
55*3b7c5e8aSDaniel Lezcano struct epoll_event ev = {
56*3b7c5e8aSDaniel Lezcano .events = EPOLLIN,
57*3b7c5e8aSDaniel Lezcano };
58*3b7c5e8aSDaniel Lezcano
59*3b7c5e8aSDaniel Lezcano struct mainloop_data *md;
60*3b7c5e8aSDaniel Lezcano
61*3b7c5e8aSDaniel Lezcano md = malloc(sizeof(*md));
62*3b7c5e8aSDaniel Lezcano if (!md)
63*3b7c5e8aSDaniel Lezcano return -1;
64*3b7c5e8aSDaniel Lezcano
65*3b7c5e8aSDaniel Lezcano md->data = data;
66*3b7c5e8aSDaniel Lezcano md->cb = cb;
67*3b7c5e8aSDaniel Lezcano md->fd = fd;
68*3b7c5e8aSDaniel Lezcano
69*3b7c5e8aSDaniel Lezcano ev.data.ptr = md;
70*3b7c5e8aSDaniel Lezcano
71*3b7c5e8aSDaniel Lezcano if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
72*3b7c5e8aSDaniel Lezcano free(md);
73*3b7c5e8aSDaniel Lezcano return -1;
74*3b7c5e8aSDaniel Lezcano }
75*3b7c5e8aSDaniel Lezcano
76*3b7c5e8aSDaniel Lezcano return 0;
77*3b7c5e8aSDaniel Lezcano }
78*3b7c5e8aSDaniel Lezcano
mainloop_del(int fd)79*3b7c5e8aSDaniel Lezcano int mainloop_del(int fd)
80*3b7c5e8aSDaniel Lezcano {
81*3b7c5e8aSDaniel Lezcano if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) < 0)
82*3b7c5e8aSDaniel Lezcano return -1;
83*3b7c5e8aSDaniel Lezcano
84*3b7c5e8aSDaniel Lezcano return 0;
85*3b7c5e8aSDaniel Lezcano }
86*3b7c5e8aSDaniel Lezcano
mainloop_init(void)87*3b7c5e8aSDaniel Lezcano int mainloop_init(void)
88*3b7c5e8aSDaniel Lezcano {
89*3b7c5e8aSDaniel Lezcano epfd = epoll_create(2);
90*3b7c5e8aSDaniel Lezcano if (epfd < 0)
91*3b7c5e8aSDaniel Lezcano return -1;
92*3b7c5e8aSDaniel Lezcano
93*3b7c5e8aSDaniel Lezcano return 0;
94*3b7c5e8aSDaniel Lezcano }
95*3b7c5e8aSDaniel Lezcano
mainloop_exit(void)96*3b7c5e8aSDaniel Lezcano void mainloop_exit(void)
97*3b7c5e8aSDaniel Lezcano {
98*3b7c5e8aSDaniel Lezcano exit_mainloop = 1;
99*3b7c5e8aSDaniel Lezcano }
100*3b7c5e8aSDaniel Lezcano
mainloop_fini(void)101*3b7c5e8aSDaniel Lezcano void mainloop_fini(void)
102*3b7c5e8aSDaniel Lezcano {
103*3b7c5e8aSDaniel Lezcano close(epfd);
104*3b7c5e8aSDaniel Lezcano }
105