Update copyright dates
[src/app-framework-binder.git] / src / fdev-epoll.c
1 /*
2  * Copyright (C) 2015-2020 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <errno.h>
19 #include <unistd.h>
20 #include <sys/epoll.h>
21
22 #define FDEV_PROVIDER
23 #include "fdev.h"
24 #include "fdev-epoll.h"
25
26 /*
27  * For sake of simplicity there is no struct fdev_epoll.
28  * Instead, the file descriptor of the internal epoll is used
29  * and wrapped in a pseudo pointer to a pseudo struct.
30  */
31 #define epollfd(fdev_epoll)  ((int)(intptr_t)fdev_epoll)
32
33 /*
34  * disable callback for fdev
35  *
36  * refs to fdev must not be counted here
37  */
38 static void disable(void *closure, const struct fdev *fdev)
39 {
40         struct fdev_epoll *fdev_epoll = closure;
41         epoll_ctl(epollfd(fdev_epoll), EPOLL_CTL_DEL, fdev_fd(fdev), 0);
42 }
43
44 /*
45  * enable callback for fdev
46  *
47  * refs to fdev must not be counted here
48  */
49 static void enable_or_update(void *closure, const struct fdev *fdev, int op, int err)
50 {
51         struct fdev_epoll *fdev_epoll = closure;
52         struct epoll_event event;
53         int rc, fd;
54
55         fd = fdev_fd(fdev);
56         event.events = fdev_events(fdev);
57         event.data.ptr = (void*)fdev;
58         rc = epoll_ctl(epollfd(fdev_epoll), op, fd, &event);
59         if (rc < 0 && errno == err)
60                 epoll_ctl(epollfd(fdev_epoll), (EPOLL_CTL_MOD + EPOLL_CTL_ADD) - op, fd, &event);
61 }
62
63 /*
64  * enable callback for fdev
65  *
66  * refs to fdev must not be counted here
67  */
68 static void enable(void *closure, const struct fdev *fdev)
69 {
70         enable_or_update(closure, fdev, EPOLL_CTL_ADD, EEXIST);
71 }
72
73 /*
74  * update callback for fdev
75  *
76  * refs to fdev must not be counted here
77  */
78 static void update(void *closure, const struct fdev *fdev)
79 {
80         enable_or_update(closure, fdev, EPOLL_CTL_MOD, ENOENT);
81 }
82
83 /*
84  * unref is not handled here
85  */
86 static struct fdev_itf itf =
87 {
88         .unref = 0,
89         .disable = disable,
90         .enable = enable,
91         .update = update
92 };
93
94 /*
95  * create an fdev_epoll
96  */
97 struct fdev_epoll *fdev_epoll_create()
98 {
99         int fd = epoll_create1(EPOLL_CLOEXEC);
100         if (!fd) {
101                 fd = dup(fd);
102                 close(0);
103         }
104         return fd < 0 ? 0 : (struct fdev_epoll*)(intptr_t)fd;
105 }
106
107 /*
108  * destroy the fdev_epoll
109  */
110 void fdev_epoll_destroy(struct fdev_epoll *fdev_epoll)
111 {
112         close(epollfd(fdev_epoll));
113 }
114
115 /*
116  * get pollable fd for the fdev_epoll
117  */
118 int fdev_epoll_fd(struct fdev_epoll *fdev_epoll)
119 {
120         return epollfd(fdev_epoll);
121 }
122
123 /*
124  * create an fdev linked to the 'fdev_epoll' for 'fd'
125  */
126 struct fdev *fdev_epoll_add(struct fdev_epoll *fdev_epoll, int fd)
127 {
128         struct fdev *fdev;
129
130         fdev = fdev_create(fd);
131         if (fdev)
132                 fdev_set_itf(fdev, &itf, fdev_epoll);
133         return fdev;
134 }
135
136 /*
137  * get pollable fd for the fdev_epoll
138  */
139 int fdev_epoll_wait_and_dispatch(struct fdev_epoll *fdev_epoll, int timeout_ms)
140 {
141         struct fdev *fdev;
142         struct epoll_event events;
143         int rc;
144
145         rc = epoll_wait(epollfd(fdev_epoll), &events, 1, timeout_ms < 0 ? -1 : timeout_ms);
146         if (rc == 1) {
147                 fdev = events.data.ptr;
148                 fdev_dispatch(fdev, events.events);
149         }
150         return rc;
151 }
152