Fix: poll and epoll fd set reallocation
[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
21#include <string.h>
22#include <unistd.h>
23
990570ed 24#include <common/common.h>
5eb91c98
DG
25
26/*
27 * Maximum number of fd we can monitor.
28 *
29 * For epoll(7), /proc/sys/fs/epoll/max_user_watches (since Linux 2.6.28) will
30 * be used for the maximum size of the poll set. If this interface is not
31 * available, according to the manpage, the max_user_watches value is 1/25 (4%)
32 * of the available low memory divided by the registration cost in bytes which
33 * is 90 bytes on a 32-bit kernel and 160 bytes on a 64-bit kernel.
34 *
35 * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by
36 * getrlimit(2).
37 */
38extern unsigned int poll_max_size;
39
40/*
41 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
42 */
43static inline void __lttng_poll_free(void *events)
44{
b3066218
DG
45 if (events) {
46 free(events);
47 }
5eb91c98
DG
48}
49
50/*
51 * epoll(7) implementation.
52 */
53#ifdef HAVE_EPOLL
54#include <sys/epoll.h>
76d7553f 55#include <stdio.h>
5eb91c98
DG
56
57/* See man epoll(7) for this define path */
990570ed 58#define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
5eb91c98
DG
59
60enum {
61 /* Polling variables compatibility for epoll */
62 LPOLLIN = EPOLLIN,
63 LPOLLPRI = EPOLLPRI,
64 LPOLLOUT = EPOLLOUT,
65 LPOLLRDNORM = EPOLLRDNORM,
66 LPOLLRDBAND = EPOLLRDBAND,
67 LPOLLWRNORM = EPOLLWRNORM,
68 LPOLLWRBAND = EPOLLWRBAND,
69 LPOLLMSG = EPOLLMSG,
70 LPOLLERR = EPOLLERR,
71 LPOLLHUP = EPOLLHUP,
72 LPOLLNVAL = EPOLLHUP,
73 LPOLLRDHUP = EPOLLRDHUP,
74 /* Close on exec feature of epoll */
75 LTTNG_CLOEXEC = EPOLL_CLOEXEC,
76};
77
78struct compat_epoll_event {
79 int epfd;
80 uint32_t nb_fd; /* Current number of fd in events */
d21b0d71
DG
81 uint32_t alloc_size; /* Size of events array */
82 uint32_t init_size; /* Initial size of events array */
5eb91c98
DG
83 struct epoll_event *events;
84};
85#define lttng_poll_event compat_epoll_event
86
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
95
96/*
97 * Create the epoll set. No memory allocation is done here.
98 */
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
DG
103
104/*
105 * Wait on epoll set with the number of fd registered to the lttng_poll_event
106 * data structure (events).
107 */
108extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout);
109#define lttng_poll_wait(events, timeout) \
65b1b198 110 compat_epoll_wait(events, timeout)
5eb91c98
DG
111
112/*
113 * Add a fd to the epoll set and resize the epoll_event structure if needed.
114 */
115extern int compat_epoll_add(struct lttng_poll_event *events,
116 int fd, uint32_t req_events);
117#define lttng_poll_add(events, fd, req_events) \
65b1b198 118 compat_epoll_add(events, fd, req_events)
5eb91c98
DG
119
120/*
121 * Remove a fd from the epoll set.
122 */
123extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
124#define lttng_poll_del(events, fd) \
65b1b198 125 compat_epoll_del(events, fd)
5eb91c98
DG
126
127/*
128 * Set up the poll set limits variable poll_max_size
129 */
130extern void compat_epoll_set_max_size(void);
65b1b198
MD
131#define lttng_poll_set_max_size() \
132 compat_epoll_set_max_size()
5eb91c98
DG
133
134/*
135 * This function memset with zero the structure since it can be reused at each
136 * round of a main loop. Being in a loop and using a non static number of fds,
137 * this function must be called to insure coherent events with associted fds.
138 */
139static inline void lttng_poll_reset(struct lttng_poll_event *events)
140{
141 if (events && events->events) {
142 memset(events->events, 0,
143 events->nb_fd * sizeof(struct epoll_event));
144 }
145}
146
147/*
148 * Clean the events structure of a lttng_poll_event. It's the caller
149 * responsability to free the lttng_poll_event memory.
150 */
151static inline void lttng_poll_clean(struct lttng_poll_event *events)
152{
76d7553f
MD
153 int ret;
154
5eb91c98 155 if (events) {
76d7553f
MD
156 ret = close(events->epfd);
157 if (ret) {
158 perror("close");
159 }
5eb91c98
DG
160 __lttng_poll_free((void *) events->events);
161 }
162}
163
164#else /* HAVE_EPOLL */
165/*
166 * Fallback on poll(2) API
167 */
168
169/* Needed for some poll event values */
170#ifndef __USE_XOPEN
171#define __USE_XOPEN
172#endif
173
174/* Needed for some poll event values */
175#ifndef __USE_GNU
176#define __USE_GNU
177#endif
178
179#include <poll.h>
180#include <stdint.h>
181
182enum {
183 /* Polling variables compatibility for poll */
184 LPOLLIN = POLLIN,
185 LPOLLPRI = POLLPRI,
186 LPOLLOUT = POLLOUT,
187 LPOLLRDNORM = POLLRDNORM,
188 LPOLLRDBAND = POLLRDBAND,
189 LPOLLWRNORM = POLLWRNORM,
190 LPOLLWRBAND = POLLWRBAND,
1268b9d6 191#if __linux__
5eb91c98 192 LPOLLMSG = POLLMSG,
1268b9d6 193 LPOLLRDHUP = POLLRDHUP,
3247be18 194#elif (defined(__FreeBSD__) || defined(__CYGWIN__))
1268b9d6
DG
195 LPOLLMSG = 0,
196 LPOLLRDHUP = 0,
b2c3836f
MD
197#else
198#error "Please add support for your OS."
1268b9d6 199#endif /* __linux__ */
5eb91c98
DG
200 LPOLLERR = POLLERR,
201 LPOLLHUP = POLLHUP | POLLNVAL,
5eb91c98
DG
202 /* Close on exec feature does not exist for poll(2) */
203 LTTNG_CLOEXEC = 0xdead,
204};
205
d21b0d71 206struct compat_poll_event_array {
5eb91c98 207 uint32_t nb_fd; /* Current number of fd in events */
d21b0d71
DG
208 uint32_t alloc_size; /* Size of events array */
209 /* Initial size of the pollset. We never shrink below that. */
210 uint32_t init_size;
5eb91c98
DG
211 struct pollfd *events;
212};
d21b0d71
DG
213
214struct compat_poll_event {
215 /*
216 * Modified by the wait action. Updated using current fields if the
217 * need_update flag is set.
218 */
219 struct compat_poll_event_array wait;
220 /*
221 * This is modified by add/del actions being the _current_ flow of
222 * execution before a poll wait is done.
223 */
224 struct compat_poll_event_array current;
225 /* Indicate if wait.events needs to be realloc. */
226 int need_realloc:1;
227 /* Indicate if wait.events need to be updated from current. */
228 int need_update:1;
229};
5eb91c98
DG
230#define lttng_poll_event compat_poll_event
231
232/*
233 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
234 * being the index of the events array.
235 */
d21b0d71
DG
236#define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
237#define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
238#define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->wait.nb_fd
239#define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
5eb91c98
DG
240
241/*
242 * Create a pollfd structure of size 'size'.
243 */
244extern int compat_poll_create(struct lttng_poll_event *events, int size);
245#define lttng_poll_create(events, size, flags) \
65b1b198 246 compat_poll_create(events, size)
5eb91c98
DG
247
248/*
249 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
250 * structure.
251 */
252extern int compat_poll_wait(struct lttng_poll_event *events, int timeout);
253#define lttng_poll_wait(events, timeout) \
65b1b198 254 compat_poll_wait(events, timeout)
5eb91c98
DG
255
256/*
257 * Add the fd to the pollfd structure. Resize if needed.
258 */
259extern int compat_poll_add(struct lttng_poll_event *events,
260 int fd, uint32_t req_events);
261#define lttng_poll_add(events, fd, req_events) \
65b1b198 262 compat_poll_add(events, fd, req_events)
5eb91c98
DG
263
264/*
265 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
266 * pollfd, data is copied from the old pollfd to the new and, finally, the old
267 * one is freed().
268 */
269extern int compat_poll_del(struct lttng_poll_event *events, int fd);
270#define lttng_poll_del(events, fd) \
65b1b198 271 compat_poll_del(events, fd)
5eb91c98
DG
272
273/*
274 * Set up the poll set limits variable poll_max_size
275 */
276extern void compat_poll_set_max_size(void);
65b1b198
MD
277#define lttng_poll_set_max_size() \
278 compat_poll_set_max_size()
5eb91c98
DG
279
280/*
281 * No need to reset a pollfd structure for poll(2)
282 */
283static inline void lttng_poll_reset(struct lttng_poll_event *events)
284{}
285
286/*
287 * Clean the events structure of a lttng_poll_event. It's the caller
288 * responsability to free the lttng_poll_event memory.
289 */
290static inline void lttng_poll_clean(struct lttng_poll_event *events)
291{
292 if (events) {
d21b0d71
DG
293 __lttng_poll_free((void *) events->wait.events);
294 __lttng_poll_free((void *) events->current.events);
5eb91c98
DG
295 }
296}
297
298#endif /* HAVE_EPOLL */
299
300#endif /* _LTT_POLL_H */
This page took 0.039048 seconds and 4 git commands to generate.