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