cfde4fc8381d36786f23683de1ffa7cc3f9e40f8
[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 <string.h>
22 #include <unistd.h>
23
24 #include <common/common.h>
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 */
38 extern unsigned int poll_max_size;
39
40 /*
41 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
42 */
43 static inline void __lttng_poll_free(void *events)
44 {
45 if (events) {
46 free(events);
47 }
48 }
49
50 /*
51 * epoll(7) implementation.
52 */
53 #ifdef HAVE_EPOLL
54 #include <sys/epoll.h>
55 #include <stdio.h>
56
57 /* See man epoll(7) for this define path */
58 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
59
60 enum {
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
78 struct 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 */
98 extern int compat_epoll_create(struct lttng_poll_event *events,
99 int size, int flags);
100 #define lttng_poll_create(events, size, flags) \
101 compat_epoll_create(events, size, flags)
102
103 /*
104 * Wait on epoll set with the number of fd registered to the lttng_poll_event
105 * data structure (events).
106 */
107 extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout);
108 #define lttng_poll_wait(events, timeout) \
109 compat_epoll_wait(events, timeout)
110
111 /*
112 * Add a fd to the epoll set and resize the epoll_event structure if needed.
113 */
114 extern 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) \
117 compat_epoll_add(events, fd, req_events)
118
119 /*
120 * Remove a fd from the epoll set.
121 */
122 extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
123 #define lttng_poll_del(events, fd) \
124 compat_epoll_del(events, fd)
125
126 /*
127 * Set up the poll set limits variable poll_max_size
128 */
129 extern void compat_epoll_set_max_size(void);
130 #define lttng_poll_set_max_size() \
131 compat_epoll_set_max_size()
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 */
138 static 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 */
150 static inline void lttng_poll_clean(struct lttng_poll_event *events)
151 {
152 int ret;
153
154 if (events) {
155 ret = close(events->epfd);
156 if (ret) {
157 perror("close");
158 }
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
181 enum {
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,
190 #if __linux__
191 LPOLLMSG = POLLMSG,
192 LPOLLRDHUP = POLLRDHUP,
193 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
194 LPOLLMSG = 0,
195 LPOLLRDHUP = 0,
196 #else
197 #error "Please add support for your OS."
198 #endif /* __linux__ */
199 LPOLLERR = POLLERR,
200 LPOLLHUP = POLLHUP | POLLNVAL,
201 /* Close on exec feature does not exist for poll(2) */
202 LTTNG_CLOEXEC = 0xdead,
203 };
204
205 struct 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 */
224 extern int compat_poll_create(struct lttng_poll_event *events, int size);
225 #define lttng_poll_create(events, size, flags) \
226 compat_poll_create(events, size)
227
228 /*
229 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
230 * structure.
231 */
232 extern int compat_poll_wait(struct lttng_poll_event *events, int timeout);
233 #define lttng_poll_wait(events, timeout) \
234 compat_poll_wait(events, timeout)
235
236 /*
237 * Add the fd to the pollfd structure. Resize if needed.
238 */
239 extern 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) \
242 compat_poll_add(events, fd, req_events)
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 */
249 extern int compat_poll_del(struct lttng_poll_event *events, int fd);
250 #define lttng_poll_del(events, fd) \
251 compat_poll_del(events, fd)
252
253 /*
254 * Set up the poll set limits variable poll_max_size
255 */
256 extern void compat_poll_set_max_size(void);
257 #define lttng_poll_set_max_size() \
258 compat_poll_set_max_size()
259
260 /*
261 * No need to reset a pollfd structure for poll(2)
262 */
263 static 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 */
270 static 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.033469 seconds and 3 git commands to generate.