Fix: Add pointer check when freeing poll events
[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 */
81 uint32_t events_size; /* Size of events array */
82 struct epoll_event *events;
83};
84#define lttng_poll_event compat_epoll_event
85
86/*
87 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
88 * being the index of the events array.
89 */
90#define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
91#define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
92#define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
93#define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
94
95/*
96 * Create the epoll set. No memory allocation is done here.
97 */
98extern int compat_epoll_create(struct lttng_poll_event *events,
99 int size, int flags);
100#define lttng_poll_create(events, size, flags) \
65b1b198 101 compat_epoll_create(events, size, flags)
5eb91c98
DG
102
103/*
104 * Wait on epoll set with the number of fd registered to the lttng_poll_event
105 * data structure (events).
106 */
107extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout);
108#define lttng_poll_wait(events, timeout) \
65b1b198 109 compat_epoll_wait(events, timeout)
5eb91c98
DG
110
111/*
112 * Add a fd to the epoll set and resize the epoll_event structure if needed.
113 */
114extern int compat_epoll_add(struct lttng_poll_event *events,
115 int fd, uint32_t req_events);
116#define lttng_poll_add(events, fd, req_events) \
65b1b198 117 compat_epoll_add(events, fd, req_events)
5eb91c98
DG
118
119/*
120 * Remove a fd from the epoll set.
121 */
122extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
123#define lttng_poll_del(events, fd) \
65b1b198 124 compat_epoll_del(events, fd)
5eb91c98
DG
125
126/*
127 * Set up the poll set limits variable poll_max_size
128 */
129extern void compat_epoll_set_max_size(void);
65b1b198
MD
130#define lttng_poll_set_max_size() \
131 compat_epoll_set_max_size()
5eb91c98
DG
132
133/*
134 * This function memset with zero the structure since it can be reused at each
135 * round of a main loop. Being in a loop and using a non static number of fds,
136 * this function must be called to insure coherent events with associted fds.
137 */
138static inline void lttng_poll_reset(struct lttng_poll_event *events)
139{
140 if (events && events->events) {
141 memset(events->events, 0,
142 events->nb_fd * sizeof(struct epoll_event));
143 }
144}
145
146/*
147 * Clean the events structure of a lttng_poll_event. It's the caller
148 * responsability to free the lttng_poll_event memory.
149 */
150static inline void lttng_poll_clean(struct lttng_poll_event *events)
151{
76d7553f
MD
152 int ret;
153
5eb91c98 154 if (events) {
76d7553f
MD
155 ret = close(events->epfd);
156 if (ret) {
157 perror("close");
158 }
5eb91c98
DG
159 __lttng_poll_free((void *) events->events);
160 }
161}
162
163#else /* HAVE_EPOLL */
164/*
165 * Fallback on poll(2) API
166 */
167
168/* Needed for some poll event values */
169#ifndef __USE_XOPEN
170#define __USE_XOPEN
171#endif
172
173/* Needed for some poll event values */
174#ifndef __USE_GNU
175#define __USE_GNU
176#endif
177
178#include <poll.h>
179#include <stdint.h>
180
181enum {
182 /* Polling variables compatibility for poll */
183 LPOLLIN = POLLIN,
184 LPOLLPRI = POLLPRI,
185 LPOLLOUT = POLLOUT,
186 LPOLLRDNORM = POLLRDNORM,
187 LPOLLRDBAND = POLLRDBAND,
188 LPOLLWRNORM = POLLWRNORM,
189 LPOLLWRBAND = POLLWRBAND,
1268b9d6 190#if __linux__
5eb91c98 191 LPOLLMSG = POLLMSG,
1268b9d6 192 LPOLLRDHUP = POLLRDHUP,
3247be18 193#elif (defined(__FreeBSD__) || defined(__CYGWIN__))
1268b9d6
DG
194 LPOLLMSG = 0,
195 LPOLLRDHUP = 0,
b2c3836f
MD
196#else
197#error "Please add support for your OS."
1268b9d6 198#endif /* __linux__ */
5eb91c98
DG
199 LPOLLERR = POLLERR,
200 LPOLLHUP = POLLHUP | POLLNVAL,
5eb91c98
DG
201 /* Close on exec feature does not exist for poll(2) */
202 LTTNG_CLOEXEC = 0xdead,
203};
204
205struct compat_poll_event {
206 uint32_t nb_fd; /* Current number of fd in events */
207 uint32_t events_size; /* Size of events array */
208 struct pollfd *events;
209};
210#define lttng_poll_event compat_poll_event
211
212/*
213 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
214 * being the index of the events array.
215 */
216#define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].fd
217#define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].revents
218#define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
219#define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
220
221/*
222 * Create a pollfd structure of size 'size'.
223 */
224extern int compat_poll_create(struct lttng_poll_event *events, int size);
225#define lttng_poll_create(events, size, flags) \
65b1b198 226 compat_poll_create(events, size)
5eb91c98
DG
227
228/*
229 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
230 * structure.
231 */
232extern int compat_poll_wait(struct lttng_poll_event *events, int timeout);
233#define lttng_poll_wait(events, timeout) \
65b1b198 234 compat_poll_wait(events, timeout)
5eb91c98
DG
235
236/*
237 * Add the fd to the pollfd structure. Resize if needed.
238 */
239extern int compat_poll_add(struct lttng_poll_event *events,
240 int fd, uint32_t req_events);
241#define lttng_poll_add(events, fd, req_events) \
65b1b198 242 compat_poll_add(events, fd, req_events)
5eb91c98
DG
243
244/*
245 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
246 * pollfd, data is copied from the old pollfd to the new and, finally, the old
247 * one is freed().
248 */
249extern int compat_poll_del(struct lttng_poll_event *events, int fd);
250#define lttng_poll_del(events, fd) \
65b1b198 251 compat_poll_del(events, fd)
5eb91c98
DG
252
253/*
254 * Set up the poll set limits variable poll_max_size
255 */
256extern void compat_poll_set_max_size(void);
65b1b198
MD
257#define lttng_poll_set_max_size() \
258 compat_poll_set_max_size()
5eb91c98
DG
259
260/*
261 * No need to reset a pollfd structure for poll(2)
262 */
263static inline void lttng_poll_reset(struct lttng_poll_event *events)
264{}
265
266/*
267 * Clean the events structure of a lttng_poll_event. It's the caller
268 * responsability to free the lttng_poll_event memory.
269 */
270static inline void lttng_poll_clean(struct lttng_poll_event *events)
271{
272 if (events) {
273 __lttng_poll_free((void *) events->events);
274 }
275}
276
277#endif /* HAVE_EPOLL */
278
279#endif /* _LTT_POLL_H */
This page took 0.057462 seconds and 4 git commands to generate.