Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / common / compat / poll.h
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef _LTT_POLL_H
9 #define _LTT_POLL_H
10
11 #include <assert.h>
12 #include <string.h>
13 #include <unistd.h>
14
15 #include <common/common.h>
16
17 /*
18 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
19 */
20 static inline void __lttng_poll_free(void *events)
21 {
22 free(events);
23 }
24
25 /*
26 * epoll(7) implementation.
27 */
28 #ifdef HAVE_EPOLL
29 #include <sys/epoll.h>
30 #include <stdio.h>
31 #include <features.h>
32 #include <common/compat/fcntl.h>
33
34 /* See man epoll(7) for this define path */
35 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
36
37 enum {
38 /* Polling variables compatibility for epoll */
39 LPOLLIN = EPOLLIN,
40 LPOLLPRI = EPOLLPRI,
41 LPOLLOUT = EPOLLOUT,
42 LPOLLRDNORM = EPOLLRDNORM,
43 LPOLLRDBAND = EPOLLRDBAND,
44 LPOLLWRNORM = EPOLLWRNORM,
45 LPOLLWRBAND = EPOLLWRBAND,
46 LPOLLMSG = EPOLLMSG,
47 LPOLLERR = EPOLLERR,
48 LPOLLHUP = EPOLLHUP,
49 LPOLLNVAL = EPOLLHUP,
50 LPOLLRDHUP = EPOLLRDHUP,
51 /* Close on exec feature of epoll */
52 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
53 LTTNG_CLOEXEC = EPOLL_CLOEXEC,
54 #else
55 /*
56 * EPOLL_CLOEXEC was added in glibc 2.8 (usually used in conjunction with
57 * epoll_create1(..)), but since neither EPOLL_CLOEXEC exists nor
58 * epoll_create1(..), we set it to FD_CLOEXEC so that we can pass it
59 * directly to fcntl(..) instead.
60 */
61 LTTNG_CLOEXEC = FD_CLOEXEC,
62 #endif
63 };
64
65 struct compat_epoll_event {
66 int epfd;
67 uint32_t nb_fd; /* Current number of fd in events */
68 uint32_t alloc_size; /* Size of events array */
69 uint32_t init_size; /* Initial size of events array */
70 struct epoll_event *events;
71 };
72 #define lttng_poll_event compat_epoll_event
73
74 static inline int __lttng_epoll_get_prev_fd(struct lttng_poll_event *events,
75 int index, uint32_t nb_fd)
76 {
77 assert(events);
78 assert(index != nb_fd);
79
80 if (index == 0 || nb_fd == 0) {
81 return -1;
82 } else {
83 return events->events[index - 1].data.fd;
84 }
85 }
86
87 /*
88 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
89 * being the index of the events array.
90 */
91 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
92 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
93 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
94 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
95 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
96 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
97
98 /* Create the epoll set. */
99 extern int compat_epoll_create(struct lttng_poll_event *events,
100 int size, int flags);
101 #define lttng_poll_create(events, size, flags) \
102 compat_epoll_create(events, size, flags)
103
104 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
105 static inline int compat_glibc_epoll_create(int size __attribute__((unused)),
106 int flags)
107 {
108 return epoll_create1(flags);
109 }
110 #else
111 static inline int compat_glibc_epoll_create(int size, int flags)
112 {
113 /*
114 * epoll_create1 was added in glibc 2.9, but unfortunatly reverting to
115 * epoll_create(..) also means that we lose the possibility to
116 * directly set the EPOLL_CLOEXEC, so try and do it anyway but through
117 * fcntl(..).
118 */
119 int efd = epoll_create(size);
120 assert(fcntl(efd, F_SETFD, flags) != -1);
121 return efd;
122 }
123 #endif
124
125 /*
126 * Wait on epoll set with the number of fd registered to the lttng_poll_event
127 * data structure (events).
128 */
129 extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout,
130 bool interruptible);
131 #define lttng_poll_wait(events, timeout) \
132 compat_epoll_wait(events, timeout, false)
133 #define lttng_poll_wait_interruptible(events, timeout) \
134 compat_epoll_wait(events, timeout, true)
135
136 /*
137 * Add a fd to the epoll set and resize the epoll_event structure if needed.
138 */
139 extern int compat_epoll_add(struct lttng_poll_event *events,
140 int fd, uint32_t req_events);
141 #define lttng_poll_add(events, fd, req_events) \
142 compat_epoll_add(events, fd, req_events)
143
144 /*
145 * Remove a fd from the epoll set.
146 */
147 extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
148 #define lttng_poll_del(events, fd) \
149 compat_epoll_del(events, fd)
150
151 /*
152 * Modify an fd's events in the epoll set.
153 */
154 extern int compat_epoll_mod(struct lttng_poll_event *events,
155 int fd, uint32_t req_events);
156 #define lttng_poll_mod(events, fd, req_events) \
157 compat_epoll_mod(events, fd, req_events)
158
159 /*
160 * Set up the poll set limits variable poll_max_size
161 */
162 extern int compat_epoll_set_max_size(void);
163 #define lttng_poll_set_max_size() \
164 compat_epoll_set_max_size()
165
166 /*
167 * This function memset with zero the structure since it can be reused at each
168 * round of a main loop. Being in a loop and using a non static number of fds,
169 * this function must be called to insure coherent events with associted fds.
170 */
171 static inline void lttng_poll_reset(struct lttng_poll_event *events)
172 {
173 if (events && events->events) {
174 memset(events->events, 0,
175 events->nb_fd * sizeof(struct epoll_event));
176 }
177 }
178
179 /*
180 * Initialize an already allocated poll event data structure. For epoll(), the
181 * epfd is set to -1 to indicate that it's not usable.
182 */
183 static inline void lttng_poll_init(struct lttng_poll_event *events)
184 {
185 memset(events, 0, sizeof(struct lttng_poll_event));
186 /* Set fd to -1 so if clean before created, we don't close 0. */
187 events->epfd = -1;
188 }
189
190 /*
191 * Clean the events structure of a lttng_poll_event. It's the caller
192 * responsability to free the lttng_poll_event memory.
193 */
194 static inline void lttng_poll_clean(struct lttng_poll_event *events)
195 {
196 int ret;
197
198 if (!events) {
199 return;
200 }
201
202 if (events->epfd >= 0) {
203 ret = close(events->epfd);
204 if (ret) {
205 PERROR("close");
206 }
207 }
208
209 __lttng_poll_free((void *) events->events);
210 }
211
212 #else /* HAVE_EPOLL */
213 /*
214 * Fallback on poll(2) API
215 */
216
217 /* Needed for some poll event values */
218 #ifndef __USE_XOPEN
219 #define __USE_XOPEN
220 #endif
221
222 /* Needed for some poll event values */
223 #ifndef __USE_GNU
224 #define __USE_GNU
225 #endif
226
227 #include <poll.h>
228 #include <stdint.h>
229
230 enum {
231 /* Polling variables compatibility for poll */
232 LPOLLIN = POLLIN,
233 LPOLLPRI = POLLPRI,
234 LPOLLOUT = POLLOUT,
235 LPOLLRDNORM = POLLRDNORM,
236 LPOLLRDBAND = POLLRDBAND,
237 LPOLLWRNORM = POLLWRNORM,
238 LPOLLWRBAND = POLLWRBAND,
239 #if __linux__
240 LPOLLMSG = POLLMSG,
241 LPOLLRDHUP = POLLRDHUP,
242 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
243 LPOLLMSG = 0,
244 LPOLLRDHUP = 0,
245 #else
246 #error "Please add support for your OS."
247 #endif /* __linux__ */
248 LPOLLERR = POLLERR,
249 LPOLLHUP = POLLHUP | POLLNVAL,
250 /* Close on exec feature does not exist for poll(2) */
251 LTTNG_CLOEXEC = 0xdead,
252 };
253
254 struct compat_poll_event_array {
255 uint32_t nb_fd; /* Current number of fd in events */
256 uint32_t alloc_size; /* Size of events array */
257 /* Initial size of the pollset. We never shrink below that. */
258 uint32_t init_size;
259 struct pollfd *events;
260 };
261
262 struct compat_poll_event {
263 /*
264 * Modified by the wait action. Updated using current fields if the
265 * need_update flag is set.
266 */
267 struct compat_poll_event_array wait;
268 /*
269 * This is modified by add/del actions being the _current_ flow of
270 * execution before a poll wait is done.
271 */
272 struct compat_poll_event_array current;
273
274 /* Indicate if wait.events need to be updated from current. */
275 int need_update:1;
276 };
277 #define lttng_poll_event compat_poll_event
278
279 static inline int __lttng_poll_get_prev_fd(struct lttng_poll_event *events,
280 int index, uint32_t nb_fd)
281 {
282 assert(events);
283 assert(index != nb_fd);
284
285 if (index == 0 || nb_fd == 0) {
286 return -1;
287 } else {
288 return events->current.events[index - 1].fd;
289 }
290 }
291
292 /*
293 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
294 * being the index of the events array.
295 * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the
296 * current list for test compatibility purposes.
297 */
298 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
299 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
300 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->current.nb_fd
301 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
302 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
303 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
304
305 /*
306 * Create a pollfd structure of size 'size'.
307 */
308 extern int compat_poll_create(struct lttng_poll_event *events, int size);
309 #define lttng_poll_create(events, size, flags) \
310 compat_poll_create(events, size)
311
312 /*
313 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
314 * structure.
315 */
316 extern int compat_poll_wait(struct lttng_poll_event *events, int timeout,
317 bool interruptible);
318 #define lttng_poll_wait(events, timeout) \
319 compat_poll_wait(events, timeout, false)
320 #define lttng_poll_wait_interruptible(events, timeout) \
321 compat_poll_wait(events, timeout, true)
322
323 /*
324 * Add the fd to the pollfd structure. Resize if needed.
325 */
326 extern int compat_poll_add(struct lttng_poll_event *events,
327 int fd, uint32_t req_events);
328 #define lttng_poll_add(events, fd, req_events) \
329 compat_poll_add(events, fd, req_events)
330
331 /*
332 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
333 * pollfd, data is copied from the old pollfd to the new and, finally, the old
334 * one is freed().
335 */
336 extern int compat_poll_del(struct lttng_poll_event *events, int fd);
337 #define lttng_poll_del(events, fd) \
338 compat_poll_del(events, fd)
339
340 /*
341 * Modify an fd's events in the poll set.
342 */
343 extern int compat_poll_mod(struct lttng_poll_event *events,
344 int fd, uint32_t req_events);
345 #define lttng_poll_mod(events, fd, req_events) \
346 compat_poll_mod(events, fd, req_events)
347
348 /*
349 * Set up the poll set limits variable poll_max_size
350 */
351 extern int compat_poll_set_max_size(void);
352 #define lttng_poll_set_max_size() \
353 compat_poll_set_max_size()
354
355 /*
356 * No need to reset a pollfd structure for poll(2)
357 */
358 static inline void lttng_poll_reset(struct lttng_poll_event *events)
359 {}
360
361 /*
362 * Initialize an already allocated poll event data structure.
363 */
364 static inline void lttng_poll_init(struct lttng_poll_event *events)
365 {
366 memset(events, 0, sizeof(struct lttng_poll_event));
367 }
368
369 /*
370 * Clean the events structure of a lttng_poll_event. It's the caller
371 * responsability to free the lttng_poll_event memory.
372 */
373 static inline void lttng_poll_clean(struct lttng_poll_event *events)
374 {
375 if (events) {
376 __lttng_poll_free((void *) events->wait.events);
377 __lttng_poll_free((void *) events->current.events);
378 }
379 }
380
381 #endif /* HAVE_EPOLL */
382
383 #endif /* _LTT_POLL_H */
This page took 0.036274 seconds and 4 git commands to generate.