49673cd5af643e40a10aa9173d1ff4cf489d0802
[lttng-tools.git] / src / common / compat / poll.h
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
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 *
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.
16 */
17
18 #ifndef _LTT_POLL_H
19 #define _LTT_POLL_H
20
21 #include <assert.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include <common/common.h>
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 */
39 extern unsigned int poll_max_size;
40
41 /*
42 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
43 */
44 static inline void __lttng_poll_free(void *events)
45 {
46 if (events) {
47 free(events);
48 }
49 }
50
51 /*
52 * epoll(7) implementation.
53 */
54 #ifdef HAVE_EPOLL
55 #include <sys/epoll.h>
56 #include <stdio.h>
57
58 /* See man epoll(7) for this define path */
59 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
60
61 enum {
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
79 struct compat_epoll_event {
80 int epfd;
81 uint32_t nb_fd; /* Current number of fd in events */
82 uint32_t alloc_size; /* Size of events array */
83 uint32_t init_size; /* Initial size of events array */
84 struct epoll_event *events;
85 };
86 #define lttng_poll_event compat_epoll_event
87
88 static 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
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
109 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
110 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
111
112 /*
113 * Create the epoll set. No memory allocation is done here.
114 */
115 extern int compat_epoll_create(struct lttng_poll_event *events,
116 int size, int flags);
117 #define lttng_poll_create(events, size, flags) \
118 compat_epoll_create(events, size, flags)
119
120 /*
121 * Wait on epoll set with the number of fd registered to the lttng_poll_event
122 * data structure (events).
123 */
124 extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout);
125 #define lttng_poll_wait(events, timeout) \
126 compat_epoll_wait(events, timeout)
127
128 /*
129 * Add a fd to the epoll set and resize the epoll_event structure if needed.
130 */
131 extern 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) \
134 compat_epoll_add(events, fd, req_events)
135
136 /*
137 * Remove a fd from the epoll set.
138 */
139 extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
140 #define lttng_poll_del(events, fd) \
141 compat_epoll_del(events, fd)
142
143 /*
144 * Set up the poll set limits variable poll_max_size
145 */
146 extern void compat_epoll_set_max_size(void);
147 #define lttng_poll_set_max_size() \
148 compat_epoll_set_max_size()
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 */
155 static 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 */
167 static inline void lttng_poll_clean(struct lttng_poll_event *events)
168 {
169 int ret;
170
171 if (events) {
172 ret = close(events->epfd);
173 if (ret) {
174 perror("close");
175 }
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
198 enum {
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,
207 #if __linux__
208 LPOLLMSG = POLLMSG,
209 LPOLLRDHUP = POLLRDHUP,
210 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
211 LPOLLMSG = 0,
212 LPOLLRDHUP = 0,
213 #else
214 #error "Please add support for your OS."
215 #endif /* __linux__ */
216 LPOLLERR = POLLERR,
217 LPOLLHUP = POLLHUP | POLLNVAL,
218 /* Close on exec feature does not exist for poll(2) */
219 LTTNG_CLOEXEC = 0xdead,
220 };
221
222 struct compat_poll_event_array {
223 uint32_t nb_fd; /* Current number of fd in events */
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;
227 struct pollfd *events;
228 };
229
230 struct 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 };
246 #define lttng_poll_event compat_poll_event
247
248 static 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
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 */
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
269 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
270 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
271
272 /*
273 * Create a pollfd structure of size 'size'.
274 */
275 extern int compat_poll_create(struct lttng_poll_event *events, int size);
276 #define lttng_poll_create(events, size, flags) \
277 compat_poll_create(events, size)
278
279 /*
280 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
281 * structure.
282 */
283 extern int compat_poll_wait(struct lttng_poll_event *events, int timeout);
284 #define lttng_poll_wait(events, timeout) \
285 compat_poll_wait(events, timeout)
286
287 /*
288 * Add the fd to the pollfd structure. Resize if needed.
289 */
290 extern 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) \
293 compat_poll_add(events, fd, req_events)
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 */
300 extern int compat_poll_del(struct lttng_poll_event *events, int fd);
301 #define lttng_poll_del(events, fd) \
302 compat_poll_del(events, fd)
303
304 /*
305 * Set up the poll set limits variable poll_max_size
306 */
307 extern void compat_poll_set_max_size(void);
308 #define lttng_poll_set_max_size() \
309 compat_poll_set_max_size()
310
311 /*
312 * No need to reset a pollfd structure for poll(2)
313 */
314 static 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 */
321 static inline void lttng_poll_clean(struct lttng_poll_event *events)
322 {
323 if (events) {
324 __lttng_poll_free((void *) events->wait.events);
325 __lttng_poll_free((void *) events->current.events);
326 }
327 }
328
329 #endif /* HAVE_EPOLL */
330
331 #endif /* _LTT_POLL_H */
This page took 0.034377 seconds and 3 git commands to generate.