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