Merge branch 'master' into compat-freebsd
[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 it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
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 with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, 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 free(events);
46 }
47
48 /*
49 * epoll(7) implementation.
50 */
51 #ifdef HAVE_EPOLL
52 #include <sys/epoll.h>
53
54 /* See man epoll(7) for this define path */
55 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
56
57 enum {
58 /* Polling variables compatibility for epoll */
59 LPOLLIN = EPOLLIN,
60 LPOLLPRI = EPOLLPRI,
61 LPOLLOUT = EPOLLOUT,
62 LPOLLRDNORM = EPOLLRDNORM,
63 LPOLLRDBAND = EPOLLRDBAND,
64 LPOLLWRNORM = EPOLLWRNORM,
65 LPOLLWRBAND = EPOLLWRBAND,
66 LPOLLMSG = EPOLLMSG,
67 LPOLLERR = EPOLLERR,
68 LPOLLHUP = EPOLLHUP,
69 LPOLLNVAL = EPOLLHUP,
70 LPOLLRDHUP = EPOLLRDHUP,
71 /* Close on exec feature of epoll */
72 LTTNG_CLOEXEC = EPOLL_CLOEXEC,
73 };
74
75 struct compat_epoll_event {
76 int epfd;
77 uint32_t nb_fd; /* Current number of fd in events */
78 uint32_t events_size; /* Size of events array */
79 struct epoll_event *events;
80 };
81 #define lttng_poll_event compat_epoll_event
82
83 /*
84 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
85 * being the index of the events array.
86 */
87 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
88 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
89 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
90 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
91
92 /*
93 * Create the epoll set. No memory allocation is done here.
94 */
95 extern int compat_epoll_create(struct lttng_poll_event *events,
96 int size, int flags);
97 #define lttng_poll_create(events, size, flags) \
98 compat_epoll_create(events, size, flags)
99
100 /*
101 * Wait on epoll set with the number of fd registered to the lttng_poll_event
102 * data structure (events).
103 */
104 extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout);
105 #define lttng_poll_wait(events, timeout) \
106 compat_epoll_wait(events, timeout)
107
108 /*
109 * Add a fd to the epoll set and resize the epoll_event structure if needed.
110 */
111 extern int compat_epoll_add(struct lttng_poll_event *events,
112 int fd, uint32_t req_events);
113 #define lttng_poll_add(events, fd, req_events) \
114 compat_epoll_add(events, fd, req_events)
115
116 /*
117 * Remove a fd from the epoll set.
118 */
119 extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
120 #define lttng_poll_del(events, fd) \
121 compat_epoll_del(events, fd)
122
123 /*
124 * Set up the poll set limits variable poll_max_size
125 */
126 extern void compat_epoll_set_max_size(void);
127 #define lttng_poll_set_max_size() \
128 compat_epoll_set_max_size()
129
130 /*
131 * This function memset with zero the structure since it can be reused at each
132 * round of a main loop. Being in a loop and using a non static number of fds,
133 * this function must be called to insure coherent events with associted fds.
134 */
135 static inline void lttng_poll_reset(struct lttng_poll_event *events)
136 {
137 if (events && events->events) {
138 memset(events->events, 0,
139 events->nb_fd * sizeof(struct epoll_event));
140 }
141 }
142
143 /*
144 * Clean the events structure of a lttng_poll_event. It's the caller
145 * responsability to free the lttng_poll_event memory.
146 */
147 static inline void lttng_poll_clean(struct lttng_poll_event *events)
148 {
149 if (events) {
150 close(events->epfd);
151 __lttng_poll_free((void *) events->events);
152 }
153 }
154
155 #else /* HAVE_EPOLL */
156 /*
157 * Fallback on poll(2) API
158 */
159
160 /* Needed for some poll event values */
161 #ifndef __USE_XOPEN
162 #define __USE_XOPEN
163 #endif
164
165 /* Needed for some poll event values */
166 #ifndef __USE_GNU
167 #define __USE_GNU
168 #endif
169
170 #include <poll.h>
171 #include <stdint.h>
172
173 enum {
174 /* Polling variables compatibility for poll */
175 LPOLLIN = POLLIN,
176 LPOLLPRI = POLLPRI,
177 LPOLLOUT = POLLOUT,
178 LPOLLRDNORM = POLLRDNORM,
179 LPOLLRDBAND = POLLRDBAND,
180 LPOLLWRNORM = POLLWRNORM,
181 LPOLLWRBAND = POLLWRBAND,
182 #if __linux__
183 LPOLLMSG = POLLMSG,
184 LPOLLRDHUP = POLLRDHUP,
185 #elif defined(__FreeBSD__)
186 LPOLLMSG = 0,
187 LPOLLRDHUP = 0,
188 #else
189 #error "Please add support for your OS."
190 #endif /* __linux__ */
191 LPOLLERR = POLLERR,
192 LPOLLHUP = POLLHUP | POLLNVAL,
193 /* Close on exec feature does not exist for poll(2) */
194 LTTNG_CLOEXEC = 0xdead,
195 };
196
197 struct compat_poll_event {
198 uint32_t nb_fd; /* Current number of fd in events */
199 uint32_t events_size; /* Size of events array */
200 struct pollfd *events;
201 };
202 #define lttng_poll_event compat_poll_event
203
204 /*
205 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
206 * being the index of the events array.
207 */
208 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].fd
209 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].revents
210 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
211 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
212
213 /*
214 * Create a pollfd structure of size 'size'.
215 */
216 extern int compat_poll_create(struct lttng_poll_event *events, int size);
217 #define lttng_poll_create(events, size, flags) \
218 compat_poll_create(events, size)
219
220 /*
221 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
222 * structure.
223 */
224 extern int compat_poll_wait(struct lttng_poll_event *events, int timeout);
225 #define lttng_poll_wait(events, timeout) \
226 compat_poll_wait(events, timeout)
227
228 /*
229 * Add the fd to the pollfd structure. Resize if needed.
230 */
231 extern int compat_poll_add(struct lttng_poll_event *events,
232 int fd, uint32_t req_events);
233 #define lttng_poll_add(events, fd, req_events) \
234 compat_poll_add(events, fd, req_events)
235
236 /*
237 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
238 * pollfd, data is copied from the old pollfd to the new and, finally, the old
239 * one is freed().
240 */
241 extern int compat_poll_del(struct lttng_poll_event *events, int fd);
242 #define lttng_poll_del(events, fd) \
243 compat_poll_del(events, fd)
244
245 /*
246 * Set up the poll set limits variable poll_max_size
247 */
248 extern void compat_poll_set_max_size(void);
249 #define lttng_poll_set_max_size() \
250 compat_poll_set_max_size()
251
252 /*
253 * No need to reset a pollfd structure for poll(2)
254 */
255 static inline void lttng_poll_reset(struct lttng_poll_event *events)
256 {}
257
258 /*
259 * Clean the events structure of a lttng_poll_event. It's the caller
260 * responsability to free the lttng_poll_event memory.
261 */
262 static inline void lttng_poll_clean(struct lttng_poll_event *events)
263 {
264 if (events) {
265 __lttng_poll_free((void *) events->events);
266 }
267 }
268
269 #endif /* HAVE_EPOLL */
270
271 #endif /* _LTT_POLL_H */
This page took 0.03385 seconds and 4 git commands to generate.