blob: 04d0ac9f5889f818b3713a86869e0772cf0963c9 [file] [log] [blame]
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00001/*
2 * Power debug tool (powerdebug)
Daniel Lezcano8be52602011-06-21 00:57:08 +02003 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00004 * Copyright (C) 2016, Linaro Limited.
Daniel Lezcano8be52602011-06-21 00:57:08 +02005 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
Daniel Lezcano8be52602011-06-21 00:57:08 +020010 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +000011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Daniel Lezcano8be52602011-06-21 00:57:08 +020015 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +000016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 */
Daniel Lezcano8be52602011-06-21 00:57:08 +020021
22#include <stdlib.h>
23#include <errno.h>
24#include <unistd.h>
Daniel Lezcano32e6cb52016-02-19 21:16:43 +000025#include <signal.h>
Daniel Lezcano8be52602011-06-21 00:57:08 +020026#include <sys/epoll.h>
27#include "mainloop.h"
28
29static int epfd = -1;
30static unsigned short nrhandler;
31
32struct mainloop_data {
33 mainloop_callback_t cb;
34 void *data;
35 int fd;
36};
37
38struct mainloop_data **mds;
39
40#define MAX_EVENTS 10
41
Daniel Lezcanodb145802011-06-21 00:57:08 +020042int mainloop(unsigned int timeout)
Daniel Lezcano8be52602011-06-21 00:57:08 +020043{
44 int i, nfds;
45 struct epoll_event events[MAX_EVENTS];
46 struct mainloop_data *md;
47
48 if (epfd < 0)
49 return -1;
50
51 for (;;) {
52
53 nfds = epoll_wait(epfd, events, MAX_EVENTS, timeout);
54 if (nfds < 0) {
55 if (errno == EINTR)
56 continue;
57 return -1;
58 }
59
Daniel Lezcano32e6cb52016-02-19 21:16:43 +000060 /*
61 * A timeout occured. Let's send to ourself a SIGWINCH
62 * so the window get refreshed automatically. No need
63 * to use exported functions and this code stay self
64 * contained.
65 */
66 if (!nfds) {
67 kill(getpid(), SIGWINCH);
68 continue;
69 }
70
Daniel Lezcano8be52602011-06-21 00:57:08 +020071 for (i = 0; i < nfds; i++) {
72 md = events[i].data.ptr;
73
74 if (md->cb(md->fd, md->data) > 0)
75 return 0;
76 }
77
78 }
79}
80
81int mainloop_add(int fd, mainloop_callback_t cb, void *data)
82{
83 struct epoll_event ev = {
84 .events = EPOLLIN,
85 };
86
87 struct mainloop_data *md;
88
89 if (fd >= nrhandler) {
90 mds = realloc(mds, sizeof(*mds) * (fd + 1));
91 if (!mds)
92 return -1;
93 nrhandler = fd + 1;
94 }
95
96 md = malloc(sizeof(*md));
97 if (!md)
98 return -1;
99
100 md->data = data;
101 md->cb = cb;
102 md->fd = fd;
103
104 mds[fd] = md;
105 ev.data.ptr = md;
106
107 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
108 free(md);
109 return -1;
110 }
111
112 return 0;
113}
114
115int mainloop_del(int fd)
116{
117 if (fd >= nrhandler)
118 return -1;
119
120 if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) < 0)
121 return -1;
122
123 free(mds[fd]);
124
125 return 0;
126}
127
128int mainloop_init(void)
129{
130 epfd = epoll_create(2);
131 if (epfd < 0)
132 return -1;
133
134 return 0;
135}
136
137void mainloop_fini(void)
138{
139 close(epfd);
140}