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