Clarify limitations of the --syscall flag with enable-event command
[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>
5eb91c98
DG
55
56/* See man epoll(7) for this define path */
990570ed 57#define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
5eb91c98
DG
58
59enum {
60 /* Polling variables compatibility for epoll */
61 LPOLLIN = EPOLLIN,
62 LPOLLPRI = EPOLLPRI,
63 LPOLLOUT = EPOLLOUT,
64 LPOLLRDNORM = EPOLLRDNORM,
65 LPOLLRDBAND = EPOLLRDBAND,
66 LPOLLWRNORM = EPOLLWRNORM,
67 LPOLLWRBAND = EPOLLWRBAND,
68 LPOLLMSG = EPOLLMSG,
69 LPOLLERR = EPOLLERR,
70 LPOLLHUP = EPOLLHUP,
71 LPOLLNVAL = EPOLLHUP,
72 LPOLLRDHUP = EPOLLRDHUP,
73 /* Close on exec feature of epoll */
74 LTTNG_CLOEXEC = EPOLL_CLOEXEC,
75};
76
77struct compat_epoll_event {
78 int epfd;
79 uint32_t nb_fd; /* Current number of fd in events */
d21b0d71
DG
80 uint32_t alloc_size; /* Size of events array */
81 uint32_t init_size; /* Initial size of events array */
5eb91c98
DG
82 struct epoll_event *events;
83};
84#define lttng_poll_event compat_epoll_event
85
beaad64c
DG
86static inline int __lttng_epoll_get_prev_fd(struct lttng_poll_event *events,
87 int index, uint32_t nb_fd)
88{
89 assert(events);
90 assert(index != nb_fd);
91
92 if (index == 0 || nb_fd == 0) {
93 return -1;
94 } else {
95 return events->events[index - 1].data.fd;
96 }
97}
98
5eb91c98
DG
99/*
100 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
101 * being the index of the events array.
102 */
103#define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
104#define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
105#define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
106#define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
beaad64c
DG
107#define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
108 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
5eb91c98
DG
109
110/*
111 * Create the epoll set. No memory allocation is done here.
112 */
113extern int compat_epoll_create(struct lttng_poll_event *events,
114 int size, int flags);
115#define lttng_poll_create(events, size, flags) \
65b1b198 116 compat_epoll_create(events, size, flags)
5eb91c98
DG
117
118/*
119 * Wait on epoll set with the number of fd registered to the lttng_poll_event
120 * data structure (events).
121 */
122extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout);
123#define lttng_poll_wait(events, timeout) \
65b1b198 124 compat_epoll_wait(events, timeout)
5eb91c98
DG
125
126/*
127 * Add a fd to the epoll set and resize the epoll_event structure if needed.
128 */
129extern int compat_epoll_add(struct lttng_poll_event *events,
130 int fd, uint32_t req_events);
131#define lttng_poll_add(events, fd, req_events) \
65b1b198 132 compat_epoll_add(events, fd, req_events)
5eb91c98
DG
133
134/*
135 * Remove a fd from the epoll set.
136 */
137extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
138#define lttng_poll_del(events, fd) \
65b1b198 139 compat_epoll_del(events, fd)
5eb91c98
DG
140
141/*
142 * Set up the poll set limits variable poll_max_size
143 */
144extern void compat_epoll_set_max_size(void);
65b1b198
MD
145#define lttng_poll_set_max_size() \
146 compat_epoll_set_max_size()
5eb91c98
DG
147
148/*
149 * This function memset with zero the structure since it can be reused at each
150 * round of a main loop. Being in a loop and using a non static number of fds,
151 * this function must be called to insure coherent events with associted fds.
152 */
153static inline void lttng_poll_reset(struct lttng_poll_event *events)
154{
155 if (events && events->events) {
156 memset(events->events, 0,
157 events->nb_fd * sizeof(struct epoll_event));
158 }
159}
160
6d737ce4
DG
161/*
162 * Initialize an already allocated poll event data structure. For epoll(), the
163 * epfd is set to -1 to indicate that it's not usable.
164 */
165static inline void lttng_poll_init(struct lttng_poll_event *events)
166{
c5854b1c 167 memset(events, 0, sizeof(struct lttng_poll_event));
6d737ce4
DG
168 /* Set fd to -1 so if clean before created, we don't close 0. */
169 events->epfd = -1;
170}
171
5eb91c98
DG
172/*
173 * Clean the events structure of a lttng_poll_event. It's the caller
174 * responsability to free the lttng_poll_event memory.
175 */
176static inline void lttng_poll_clean(struct lttng_poll_event *events)
177{
76d7553f
MD
178 int ret;
179
3cc04881
DG
180 if (!events) {
181 return;
182 }
183
184 if (events->epfd >= 0) {
76d7553f
MD
185 ret = close(events->epfd);
186 if (ret) {
187 perror("close");
188 }
5eb91c98 189 }
3cc04881
DG
190
191 __lttng_poll_free((void *) events->events);
5eb91c98
DG
192}
193
194#else /* HAVE_EPOLL */
195/*
196 * Fallback on poll(2) API
197 */
198
199/* Needed for some poll event values */
200#ifndef __USE_XOPEN
201#define __USE_XOPEN
202#endif
203
204/* Needed for some poll event values */
205#ifndef __USE_GNU
206#define __USE_GNU
207#endif
208
209#include <poll.h>
210#include <stdint.h>
211
212enum {
213 /* Polling variables compatibility for poll */
214 LPOLLIN = POLLIN,
215 LPOLLPRI = POLLPRI,
216 LPOLLOUT = POLLOUT,
217 LPOLLRDNORM = POLLRDNORM,
218 LPOLLRDBAND = POLLRDBAND,
219 LPOLLWRNORM = POLLWRNORM,
220 LPOLLWRBAND = POLLWRBAND,
1268b9d6 221#if __linux__
5eb91c98 222 LPOLLMSG = POLLMSG,
1268b9d6 223 LPOLLRDHUP = POLLRDHUP,
3247be18 224#elif (defined(__FreeBSD__) || defined(__CYGWIN__))
1268b9d6
DG
225 LPOLLMSG = 0,
226 LPOLLRDHUP = 0,
b2c3836f
MD
227#else
228#error "Please add support for your OS."
1268b9d6 229#endif /* __linux__ */
5eb91c98
DG
230 LPOLLERR = POLLERR,
231 LPOLLHUP = POLLHUP | POLLNVAL,
5eb91c98
DG
232 /* Close on exec feature does not exist for poll(2) */
233 LTTNG_CLOEXEC = 0xdead,
234};
235
d21b0d71 236struct compat_poll_event_array {
5eb91c98 237 uint32_t nb_fd; /* Current number of fd in events */
d21b0d71
DG
238 uint32_t alloc_size; /* Size of events array */
239 /* Initial size of the pollset. We never shrink below that. */
240 uint32_t init_size;
5eb91c98
DG
241 struct pollfd *events;
242};
d21b0d71
DG
243
244struct compat_poll_event {
245 /*
246 * Modified by the wait action. Updated using current fields if the
247 * need_update flag is set.
248 */
249 struct compat_poll_event_array wait;
250 /*
251 * This is modified by add/del actions being the _current_ flow of
252 * execution before a poll wait is done.
253 */
254 struct compat_poll_event_array current;
255 /* Indicate if wait.events needs to be realloc. */
256 int need_realloc:1;
257 /* Indicate if wait.events need to be updated from current. */
258 int need_update:1;
259};
5eb91c98
DG
260#define lttng_poll_event compat_poll_event
261
beaad64c
DG
262static inline int __lttng_poll_get_prev_fd(struct lttng_poll_event *events,
263 int index, uint32_t nb_fd)
264{
265 assert(events);
266 assert(index != nb_fd);
267
268 if (index == 0 || nb_fd == 0) {
269 return -1;
270 } else {
271 return events->current.events[index - 1].fd;
272 }
273}
274
5eb91c98
DG
275/*
276 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
277 * being the index of the events array.
278 */
d21b0d71
DG
279#define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
280#define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
281#define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->wait.nb_fd
282#define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
beaad64c
DG
283#define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
284 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
5eb91c98
DG
285
286/*
287 * Create a pollfd structure of size 'size'.
288 */
289extern int compat_poll_create(struct lttng_poll_event *events, int size);
290#define lttng_poll_create(events, size, flags) \
65b1b198 291 compat_poll_create(events, size)
5eb91c98
DG
292
293/*
294 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
295 * structure.
296 */
297extern int compat_poll_wait(struct lttng_poll_event *events, int timeout);
298#define lttng_poll_wait(events, timeout) \
65b1b198 299 compat_poll_wait(events, timeout)
5eb91c98
DG
300
301/*
302 * Add the fd to the pollfd structure. Resize if needed.
303 */
304extern int compat_poll_add(struct lttng_poll_event *events,
305 int fd, uint32_t req_events);
306#define lttng_poll_add(events, fd, req_events) \
65b1b198 307 compat_poll_add(events, fd, req_events)
5eb91c98
DG
308
309/*
310 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
311 * pollfd, data is copied from the old pollfd to the new and, finally, the old
312 * one is freed().
313 */
314extern int compat_poll_del(struct lttng_poll_event *events, int fd);
315#define lttng_poll_del(events, fd) \
65b1b198 316 compat_poll_del(events, fd)
5eb91c98
DG
317
318/*
319 * Set up the poll set limits variable poll_max_size
320 */
321extern void compat_poll_set_max_size(void);
65b1b198
MD
322#define lttng_poll_set_max_size() \
323 compat_poll_set_max_size()
5eb91c98
DG
324
325/*
326 * No need to reset a pollfd structure for poll(2)
327 */
328static inline void lttng_poll_reset(struct lttng_poll_event *events)
329{}
330
6d737ce4
DG
331/*
332 * Initialize an already allocated poll event data structure.
333 */
334static inline void lttng_poll_init(struct lttng_poll_event *events)
335{
336 memset(events, 0, sizeof(struct lttng_poll_event));
337}
338
5eb91c98
DG
339/*
340 * Clean the events structure of a lttng_poll_event. It's the caller
341 * responsability to free the lttng_poll_event memory.
342 */
343static inline void lttng_poll_clean(struct lttng_poll_event *events)
344{
345 if (events) {
d21b0d71
DG
346 __lttng_poll_free((void *) events->wait.events);
347 __lttng_poll_free((void *) events->current.events);
5eb91c98
DG
348 }
349}
350
351#endif /* HAVE_EPOLL */
352
353#endif /* _LTT_POLL_H */
This page took 0.078954 seconds and 4 git commands to generate.