Merge branch 'v3.10/topic/cpufreq' of git://git.linaro.org/kernel/linux-linaro-stable...
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / Monitor.cpp
1 /**
2  * Copyright (C) ARM Limited 2013-2014. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include "Monitor.h"
10
11 #include <errno.h>
12 #include <string.h>
13 #include <unistd.h>
14
15 #include "Logging.h"
16
17 Monitor::Monitor() : mFd(-1) {
18 }
19
20 Monitor::~Monitor() {
21         if (mFd >= -1) {
22                 close(mFd);
23         }
24 }
25
26 bool Monitor::init() {
27         mFd = epoll_create(16);
28         if (mFd < 0) {
29                 logg->logMessage("%s(%s:%i): epoll_create1 failed", __FUNCTION__, __FILE__, __LINE__);
30                 return false;
31         }
32
33         return true;
34 }
35
36 bool Monitor::add(const int fd) {
37         struct epoll_event event;
38         memset(&event, 0, sizeof(event));
39         event.data.fd = fd;
40         event.events = EPOLLIN;
41         if (epoll_ctl(mFd, EPOLL_CTL_ADD, fd, &event) != 0) {
42                 logg->logMessage("%s(%s:%i): epoll_ctl failed", __FUNCTION__, __FILE__, __LINE__);
43                 return false;
44         }
45
46         return true;
47 }
48
49 int Monitor::wait(struct epoll_event *const events, int maxevents, int timeout) {
50         int result = epoll_wait(mFd, events, maxevents, timeout);
51         if (result < 0) {
52                 // Ignore if the call was interrupted as this will happen when SIGINT is received
53                 if (errno == EINTR) {
54                         result = 0;
55                 } else {
56                         logg->logMessage("%s(%s:%i): epoll_wait failed", __FUNCTION__, __FILE__, __LINE__);
57                 }
58         }
59
60         return result;
61 }