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