Add the container_of() macro to macro.h
[lttng-tools.git] / src / common / compat / poll.h
CommitLineData
5eb91c98
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
5eb91c98
DG
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 *
d14d33bf
AM
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.
5eb91c98
DG
16 */
17
18#ifndef _LTT_POLL_H
19#define _LTT_POLL_H
20
beaad64c 21#include <assert.h>
5eb91c98
DG
22#include <string.h>
23#include <unistd.h>
24
990570ed 25#include <common/common.h>
5eb91c98
DG
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 */
39extern unsigned int poll_max_size;
40
41/*
42 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
43 */
44static inline void __lttng_poll_free(void *events)
45{
0e428499 46 free(events);
5eb91c98
DG
47}
48
49/*
50 * epoll(7) implementation.
51 */
52#ifdef HAVE_EPOLL
53#include <sys/epoll.h>
76d7553f 54#include <stdio.h>
f263b7fd
JD
55#include <features.h>
56#include <common/compat/fcntl.h>
5eb91c98
DG
57
58/* See man epoll(7) for this define path */
990570ed 59#define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
5eb91c98
DG
60
61enum {
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 */
062fc3d8 76#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
5eb91c98 77 LTTNG_CLOEXEC = EPOLL_CLOEXEC,
f263b7fd
JD
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
5eb91c98
DG
87};
88
89struct compat_epoll_event {
90 int epfd;
91 uint32_t nb_fd; /* Current number of fd in events */
d21b0d71
DG
92 uint32_t alloc_size; /* Size of events array */
93 uint32_t init_size; /* Initial size of events array */
5eb91c98
DG
94 struct epoll_event *events;
95};
96#define lttng_poll_event compat_epoll_event
97
beaad64c
DG
98static 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
5eb91c98
DG
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
beaad64c
DG
119#define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
120 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
5eb91c98
DG
121
122/*
123 * Create the epoll set. No memory allocation is done here.
124 */
125extern int compat_epoll_create(struct lttng_poll_event *events,
126 int size, int flags);
127#define lttng_poll_create(events, size, flags) \
65b1b198 128 compat_epoll_create(events, size, flags)
5eb91c98 129
062fc3d8 130#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
f263b7fd
JD
131static inline int compat_glibc_epoll_create(int size __attribute__((unused)),
132 int flags)
133{
134 return epoll_create1(flags);
135}
136#else
137static inline int compat_glibc_epoll_create(int size, int flags)
138{
139 /*
140 * epoll_create1 was added in glibc 2.9, but unfortunatly reverting to
141 * epoll_create(..) also means that we lose the possibility to
142 * directly set the EPOLL_CLOEXEC, so try and do it anyway but through
143 * fcntl(..).
144 */
145 int efd = epoll_create(size);
146 assert(fcntl(efd, F_SETFD, flags) != -1);
147 return efd;
148}
149#endif
150
5eb91c98
DG
151/*
152 * Wait on epoll set with the number of fd registered to the lttng_poll_event
153 * data structure (events).
154 */
155extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout);
156#define lttng_poll_wait(events, timeout) \
65b1b198 157 compat_epoll_wait(events, timeout)
5eb91c98
DG
158
159/*
160 * Add a fd to the epoll set and resize the epoll_event structure if needed.
161 */
162extern int compat_epoll_add(struct lttng_poll_event *events,
163 int fd, uint32_t req_events);
164#define lttng_poll_add(events, fd, req_events) \
65b1b198 165 compat_epoll_add(events, fd, req_events)
5eb91c98
DG
166
167/*
168 * Remove a fd from the epoll set.
169 */
170extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
171#define lttng_poll_del(events, fd) \
65b1b198 172 compat_epoll_del(events, fd)
5eb91c98
DG
173
174/*
175 * Set up the poll set limits variable poll_max_size
176 */
dbe23f45 177extern int compat_epoll_set_max_size(void);
65b1b198
MD
178#define lttng_poll_set_max_size() \
179 compat_epoll_set_max_size()
5eb91c98
DG
180
181/*
182 * This function memset with zero the structure since it can be reused at each
183 * round of a main loop. Being in a loop and using a non static number of fds,
184 * this function must be called to insure coherent events with associted fds.
185 */
186static inline void lttng_poll_reset(struct lttng_poll_event *events)
187{
188 if (events && events->events) {
189 memset(events->events, 0,
190 events->nb_fd * sizeof(struct epoll_event));
191 }
192}
193
6d737ce4
DG
194/*
195 * Initialize an already allocated poll event data structure. For epoll(), the
196 * epfd is set to -1 to indicate that it's not usable.
197 */
198static inline void lttng_poll_init(struct lttng_poll_event *events)
199{
c5854b1c 200 memset(events, 0, sizeof(struct lttng_poll_event));
6d737ce4
DG
201 /* Set fd to -1 so if clean before created, we don't close 0. */
202 events->epfd = -1;
203}
204
5eb91c98
DG
205/*
206 * Clean the events structure of a lttng_poll_event. It's the caller
207 * responsability to free the lttng_poll_event memory.
208 */
209static inline void lttng_poll_clean(struct lttng_poll_event *events)
210{
76d7553f
MD
211 int ret;
212
3cc04881
DG
213 if (!events) {
214 return;
215 }
216
217 if (events->epfd >= 0) {
76d7553f
MD
218 ret = close(events->epfd);
219 if (ret) {
6f04ed72 220 PERROR("close");
76d7553f 221 }
5eb91c98 222 }
3cc04881
DG
223
224 __lttng_poll_free((void *) events->events);
5eb91c98
DG
225}
226
227#else /* HAVE_EPOLL */
228/*
229 * Fallback on poll(2) API
230 */
231
232/* Needed for some poll event values */
233#ifndef __USE_XOPEN
234#define __USE_XOPEN
235#endif
236
237/* Needed for some poll event values */
238#ifndef __USE_GNU
239#define __USE_GNU
240#endif
241
242#include <poll.h>
243#include <stdint.h>
244
245enum {
246 /* Polling variables compatibility for poll */
247 LPOLLIN = POLLIN,
248 LPOLLPRI = POLLPRI,
249 LPOLLOUT = POLLOUT,
250 LPOLLRDNORM = POLLRDNORM,
251 LPOLLRDBAND = POLLRDBAND,
252 LPOLLWRNORM = POLLWRNORM,
253 LPOLLWRBAND = POLLWRBAND,
1268b9d6 254#if __linux__
5eb91c98 255 LPOLLMSG = POLLMSG,
1268b9d6 256 LPOLLRDHUP = POLLRDHUP,
a97dd6ce 257#elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
1268b9d6
DG
258 LPOLLMSG = 0,
259 LPOLLRDHUP = 0,
b2c3836f
MD
260#else
261#error "Please add support for your OS."
1268b9d6 262#endif /* __linux__ */
5eb91c98
DG
263 LPOLLERR = POLLERR,
264 LPOLLHUP = POLLHUP | POLLNVAL,
5eb91c98
DG
265 /* Close on exec feature does not exist for poll(2) */
266 LTTNG_CLOEXEC = 0xdead,
267};
268
d21b0d71 269struct compat_poll_event_array {
5eb91c98 270 uint32_t nb_fd; /* Current number of fd in events */
d21b0d71
DG
271 uint32_t alloc_size; /* Size of events array */
272 /* Initial size of the pollset. We never shrink below that. */
273 uint32_t init_size;
5eb91c98
DG
274 struct pollfd *events;
275};
d21b0d71
DG
276
277struct compat_poll_event {
278 /*
279 * Modified by the wait action. Updated using current fields if the
280 * need_update flag is set.
281 */
282 struct compat_poll_event_array wait;
283 /*
284 * This is modified by add/del actions being the _current_ flow of
285 * execution before a poll wait is done.
286 */
287 struct compat_poll_event_array current;
dbe23f45 288
d21b0d71
DG
289 /* Indicate if wait.events need to be updated from current. */
290 int need_update:1;
291};
5eb91c98
DG
292#define lttng_poll_event compat_poll_event
293
beaad64c
DG
294static inline int __lttng_poll_get_prev_fd(struct lttng_poll_event *events,
295 int index, uint32_t nb_fd)
296{
297 assert(events);
298 assert(index != nb_fd);
299
300 if (index == 0 || nb_fd == 0) {
301 return -1;
302 } else {
303 return events->current.events[index - 1].fd;
304 }
305}
306
5eb91c98
DG
307/*
308 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
309 * being the index of the events array.
310 */
d21b0d71
DG
311#define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
312#define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
313#define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->wait.nb_fd
314#define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
beaad64c
DG
315#define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
316 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
5eb91c98
DG
317
318/*
319 * Create a pollfd structure of size 'size'.
320 */
321extern int compat_poll_create(struct lttng_poll_event *events, int size);
322#define lttng_poll_create(events, size, flags) \
65b1b198 323 compat_poll_create(events, size)
5eb91c98
DG
324
325/*
326 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
327 * structure.
328 */
329extern int compat_poll_wait(struct lttng_poll_event *events, int timeout);
330#define lttng_poll_wait(events, timeout) \
65b1b198 331 compat_poll_wait(events, timeout)
5eb91c98
DG
332
333/*
334 * Add the fd to the pollfd structure. Resize if needed.
335 */
336extern 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) \
65b1b198 339 compat_poll_add(events, fd, req_events)
5eb91c98
DG
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 */
346extern int compat_poll_del(struct lttng_poll_event *events, int fd);
347#define lttng_poll_del(events, fd) \
65b1b198 348 compat_poll_del(events, fd)
5eb91c98
DG
349
350/*
351 * Set up the poll set limits variable poll_max_size
352 */
dbe23f45 353extern int compat_poll_set_max_size(void);
65b1b198
MD
354#define lttng_poll_set_max_size() \
355 compat_poll_set_max_size()
5eb91c98
DG
356
357/*
358 * No need to reset a pollfd structure for poll(2)
359 */
360static inline void lttng_poll_reset(struct lttng_poll_event *events)
361{}
362
6d737ce4
DG
363/*
364 * Initialize an already allocated poll event data structure.
365 */
366static inline void lttng_poll_init(struct lttng_poll_event *events)
367{
368 memset(events, 0, sizeof(struct lttng_poll_event));
369}
370
5eb91c98
DG
371/*
372 * Clean the events structure of a lttng_poll_event. It's the caller
373 * responsability to free the lttng_poll_event memory.
374 */
375static inline void lttng_poll_clean(struct lttng_poll_event *events)
376{
377 if (events) {
d21b0d71
DG
378 __lttng_poll_free((void *) events->wait.events);
379 __lttng_poll_free((void *) events->current.events);
5eb91c98
DG
380 }
381}
382
383#endif /* HAVE_EPOLL */
384
385#endif /* _LTT_POLL_H */
This page took 0.088977 seconds and 4 git commands to generate.