Fix: liblttng-ctl: poll compatibility symbols inadvertently exported
[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 26
5eb91c98
DG
27/*
28 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
29 */
30static inline void __lttng_poll_free(void *events)
31{
0e428499 32 free(events);
5eb91c98
DG
33}
34
35/*
36 * epoll(7) implementation.
37 */
38#ifdef HAVE_EPOLL
39#include <sys/epoll.h>
76d7553f 40#include <stdio.h>
f263b7fd
JD
41#include <features.h>
42#include <common/compat/fcntl.h>
5eb91c98
DG
43
44/* See man epoll(7) for this define path */
990570ed 45#define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
5eb91c98
DG
46
47enum {
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 */
062fc3d8 62#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
5eb91c98 63 LTTNG_CLOEXEC = EPOLL_CLOEXEC,
f263b7fd
JD
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
5eb91c98
DG
73};
74
75struct compat_epoll_event {
76 int epfd;
77 uint32_t nb_fd; /* Current number of fd in events */
d21b0d71
DG
78 uint32_t alloc_size; /* Size of events array */
79 uint32_t init_size; /* Initial size of events array */
5eb91c98
DG
80 struct epoll_event *events;
81};
82#define lttng_poll_event compat_epoll_event
83
beaad64c
DG
84static 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
5eb91c98
DG
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
beaad64c
DG
105#define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
106 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
5eb91c98 107
5a12931e 108/* Create the epoll set. */
5eb91c98
DG
109extern int compat_epoll_create(struct lttng_poll_event *events,
110 int size, int flags);
111#define lttng_poll_create(events, size, flags) \
65b1b198 112 compat_epoll_create(events, size, flags)
5eb91c98 113
062fc3d8 114#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
f263b7fd
JD
115static inline int compat_glibc_epoll_create(int size __attribute__((unused)),
116 int flags)
117{
118 return epoll_create1(flags);
119}
120#else
121static 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
5eb91c98
DG
135/*
136 * Wait on epoll set with the number of fd registered to the lttng_poll_event
137 * data structure (events).
138 */
9f32e9bf
MD
139extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout,
140 bool interruptible);
5eb91c98 141#define lttng_poll_wait(events, timeout) \
9f32e9bf
MD
142 compat_epoll_wait(events, timeout, false)
143#define lttng_poll_wait_interruptible(events, timeout) \
144 compat_epoll_wait(events, timeout, true)
5eb91c98
DG
145
146/*
147 * Add a fd to the epoll set and resize the epoll_event structure if needed.
148 */
149extern 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) \
65b1b198 152 compat_epoll_add(events, fd, req_events)
5eb91c98
DG
153
154/*
155 * Remove a fd from the epoll set.
156 */
157extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
158#define lttng_poll_del(events, fd) \
65b1b198 159 compat_epoll_del(events, fd)
5eb91c98 160
f057dfc3
JG
161/*
162 * Modify an fd's events in the epoll set.
163 */
164extern 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) \
2a85be8e 167 compat_epoll_mod(events, fd, req_events)
f057dfc3 168
5eb91c98
DG
169/*
170 * Set up the poll set limits variable poll_max_size
171 */
dbe23f45 172extern int compat_epoll_set_max_size(void);
65b1b198
MD
173#define lttng_poll_set_max_size() \
174 compat_epoll_set_max_size()
5eb91c98
DG
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 */
181static 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
6d737ce4
DG
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 */
193static inline void lttng_poll_init(struct lttng_poll_event *events)
194{
c5854b1c 195 memset(events, 0, sizeof(struct lttng_poll_event));
6d737ce4
DG
196 /* Set fd to -1 so if clean before created, we don't close 0. */
197 events->epfd = -1;
198}
199
5eb91c98
DG
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 */
204static inline void lttng_poll_clean(struct lttng_poll_event *events)
205{
76d7553f
MD
206 int ret;
207
3cc04881
DG
208 if (!events) {
209 return;
210 }
211
212 if (events->epfd >= 0) {
76d7553f
MD
213 ret = close(events->epfd);
214 if (ret) {
6f04ed72 215 PERROR("close");
76d7553f 216 }
5eb91c98 217 }
3cc04881
DG
218
219 __lttng_poll_free((void *) events->events);
5eb91c98
DG
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
240enum {
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,
1268b9d6 249#if __linux__
5eb91c98 250 LPOLLMSG = POLLMSG,
1268b9d6 251 LPOLLRDHUP = POLLRDHUP,
a97dd6ce 252#elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
1268b9d6
DG
253 LPOLLMSG = 0,
254 LPOLLRDHUP = 0,
b2c3836f
MD
255#else
256#error "Please add support for your OS."
1268b9d6 257#endif /* __linux__ */
5eb91c98
DG
258 LPOLLERR = POLLERR,
259 LPOLLHUP = POLLHUP | POLLNVAL,
5eb91c98
DG
260 /* Close on exec feature does not exist for poll(2) */
261 LTTNG_CLOEXEC = 0xdead,
262};
263
d21b0d71 264struct compat_poll_event_array {
5eb91c98 265 uint32_t nb_fd; /* Current number of fd in events */
d21b0d71
DG
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;
5eb91c98
DG
269 struct pollfd *events;
270};
d21b0d71
DG
271
272struct 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;
dbe23f45 283
d21b0d71
DG
284 /* Indicate if wait.events need to be updated from current. */
285 int need_update:1;
286};
5eb91c98
DG
287#define lttng_poll_event compat_poll_event
288
beaad64c
DG
289static 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
5eb91c98
DG
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.
7e222fa8
YL
305 * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the
306 * current list for test compatibility purposes.
5eb91c98 307 */
d21b0d71
DG
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
7e222fa8 310#define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->current.nb_fd
d21b0d71 311#define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
beaad64c
DG
312#define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
313 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
5eb91c98
DG
314
315/*
316 * Create a pollfd structure of size 'size'.
317 */
318extern int compat_poll_create(struct lttng_poll_event *events, int size);
319#define lttng_poll_create(events, size, flags) \
65b1b198 320 compat_poll_create(events, size)
5eb91c98
DG
321
322/*
323 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
324 * structure.
325 */
9f32e9bf
MD
326extern int compat_poll_wait(struct lttng_poll_event *events, int timeout,
327 bool interruptible);
5eb91c98 328#define lttng_poll_wait(events, timeout) \
9f32e9bf
MD
329 compat_poll_wait(events, timeout, false)
330#define lttng_poll_wait_interruptible(events, timeout) \
331 compat_poll_wait(events, timeout, true)
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 349
f057dfc3 350/*
b14f53d4 351 * Modify an fd's events in the poll set.
f057dfc3
JG
352 */
353extern 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) \
2a85be8e 356 compat_poll_mod(events, fd, req_events)
f057dfc3 357
5eb91c98
DG
358/*
359 * Set up the poll set limits variable poll_max_size
360 */
dbe23f45 361extern int compat_poll_set_max_size(void);
65b1b198
MD
362#define lttng_poll_set_max_size() \
363 compat_poll_set_max_size()
5eb91c98
DG
364
365/*
366 * No need to reset a pollfd structure for poll(2)
367 */
368static inline void lttng_poll_reset(struct lttng_poll_event *events)
369{}
370
6d737ce4
DG
371/*
372 * Initialize an already allocated poll event data structure.
373 */
374static inline void lttng_poll_init(struct lttng_poll_event *events)
375{
376 memset(events, 0, sizeof(struct lttng_poll_event));
377}
378
5eb91c98
DG
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 */
383static inline void lttng_poll_clean(struct lttng_poll_event *events)
384{
385 if (events) {
d21b0d71
DG
386 __lttng_poll_free((void *) events->wait.events);
387 __lttng_poll_free((void *) events->current.events);
5eb91c98
DG
388 }
389}
390
391#endif /* HAVE_EPOLL */
392
393#endif /* _LTT_POLL_H */
This page took 0.084536 seconds and 5 git commands to generate.