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