Merge branch 'master' of git://git.lttng.org/lttng-tools
[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 __FreeBSD__
186 LPOLLMSG = 0,
187 LPOLLRDHUP = 0,
188 #endif /* __linux__ */
189 LPOLLERR = POLLERR,
190 LPOLLHUP = POLLHUP | POLLNVAL,
191 /* Close on exec feature does not exist for poll(2) */
192 LTTNG_CLOEXEC = 0xdead,
193 };
194
195 struct compat_poll_event {
196 uint32_t nb_fd; /* Current number of fd in events */
197 uint32_t events_size; /* Size of events array */
198 struct pollfd *events;
199 };
200 #define lttng_poll_event compat_poll_event
201
202 /*
203 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
204 * being the index of the events array.
205 */
206 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].fd
207 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].revents
208 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
209 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
210
211 /*
212 * Create a pollfd structure of size 'size'.
213 */
214 extern int compat_poll_create(struct lttng_poll_event *events, int size);
215 #define lttng_poll_create(events, size, flags) \
216 compat_poll_create(events, size)
217
218 /*
219 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
220 * structure.
221 */
222 extern int compat_poll_wait(struct lttng_poll_event *events, int timeout);
223 #define lttng_poll_wait(events, timeout) \
224 compat_poll_wait(events, timeout)
225
226 /*
227 * Add the fd to the pollfd structure. Resize if needed.
228 */
229 extern int compat_poll_add(struct lttng_poll_event *events,
230 int fd, uint32_t req_events);
231 #define lttng_poll_add(events, fd, req_events) \
232 compat_poll_add(events, fd, req_events)
233
234 /*
235 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
236 * pollfd, data is copied from the old pollfd to the new and, finally, the old
237 * one is freed().
238 */
239 extern int compat_poll_del(struct lttng_poll_event *events, int fd);
240 #define lttng_poll_del(events, fd) \
241 compat_poll_del(events, fd)
242
243 /*
244 * Set up the poll set limits variable poll_max_size
245 */
246 extern void compat_poll_set_max_size(void);
247 #define lttng_poll_set_max_size() \
248 compat_poll_set_max_size()
249
250 /*
251 * No need to reset a pollfd structure for poll(2)
252 */
253 static inline void lttng_poll_reset(struct lttng_poll_event *events)
254 {}
255
256 /*
257 * Clean the events structure of a lttng_poll_event. It's the caller
258 * responsability to free the lttng_poll_event memory.
259 */
260 static inline void lttng_poll_clean(struct lttng_poll_event *events)
261 {
262 if (events) {
263 __lttng_poll_free((void *) events->events);
264 }
265 }
266
267 #endif /* HAVE_EPOLL */
268
269 #endif /* _LTT_POLL_H */
This page took 0.035388 seconds and 5 git commands to generate.