Fix consumer to handle compat layer
[lttng-tools.git] / src / common / compat / poll.h
CommitLineData
5eb91c98
DG
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
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{
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 */
990570ed 55#define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
5eb91c98
DG
56
57enum {
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
75struct 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 */
95extern int compat_epoll_create(struct lttng_poll_event *events,
96 int size, int flags);
97#define lttng_poll_create(events, size, flags) \
65b1b198 98 compat_epoll_create(events, size, flags)
5eb91c98
DG
99
100/*
101 * Wait on epoll set with the number of fd registered to the lttng_poll_event
102 * data structure (events).
103 */
104extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout);
105#define lttng_poll_wait(events, timeout) \
65b1b198 106 compat_epoll_wait(events, timeout)
5eb91c98
DG
107
108/*
109 * Add a fd to the epoll set and resize the epoll_event structure if needed.
110 */
111extern 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) \
65b1b198 114 compat_epoll_add(events, fd, req_events)
5eb91c98
DG
115
116/*
117 * Remove a fd from the epoll set.
118 */
119extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
120#define lttng_poll_del(events, fd) \
65b1b198 121 compat_epoll_del(events, fd)
5eb91c98
DG
122
123/*
124 * Set up the poll set limits variable poll_max_size
125 */
126extern void compat_epoll_set_max_size(void);
65b1b198
MD
127#define lttng_poll_set_max_size() \
128 compat_epoll_set_max_size()
5eb91c98
DG
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 */
135static 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 */
147static 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
173enum {
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 LPOLLMSG = POLLMSG,
183 LPOLLERR = POLLERR,
184 LPOLLHUP = POLLHUP | POLLNVAL,
ca4537d3 185 LPOLLRDHUP = POLLRDHUP,
5eb91c98
DG
186 /* Close on exec feature does not exist for poll(2) */
187 LTTNG_CLOEXEC = 0xdead,
188};
189
190struct compat_poll_event {
191 uint32_t nb_fd; /* Current number of fd in events */
192 uint32_t events_size; /* Size of events array */
193 struct pollfd *events;
194};
195#define lttng_poll_event compat_poll_event
196
197/*
198 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
199 * being the index of the events array.
200 */
201#define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].fd
202#define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].revents
203#define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
204#define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
205
206/*
207 * Create a pollfd structure of size 'size'.
208 */
209extern int compat_poll_create(struct lttng_poll_event *events, int size);
210#define lttng_poll_create(events, size, flags) \
65b1b198 211 compat_poll_create(events, size)
5eb91c98
DG
212
213/*
214 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
215 * structure.
216 */
217extern int compat_poll_wait(struct lttng_poll_event *events, int timeout);
218#define lttng_poll_wait(events, timeout) \
65b1b198 219 compat_poll_wait(events, timeout)
5eb91c98
DG
220
221/*
222 * Add the fd to the pollfd structure. Resize if needed.
223 */
224extern int compat_poll_add(struct lttng_poll_event *events,
225 int fd, uint32_t req_events);
226#define lttng_poll_add(events, fd, req_events) \
65b1b198 227 compat_poll_add(events, fd, req_events)
5eb91c98
DG
228
229/*
230 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
231 * pollfd, data is copied from the old pollfd to the new and, finally, the old
232 * one is freed().
233 */
234extern int compat_poll_del(struct lttng_poll_event *events, int fd);
235#define lttng_poll_del(events, fd) \
65b1b198 236 compat_poll_del(events, fd)
5eb91c98
DG
237
238/*
239 * Set up the poll set limits variable poll_max_size
240 */
241extern void compat_poll_set_max_size(void);
65b1b198
MD
242#define lttng_poll_set_max_size() \
243 compat_poll_set_max_size()
5eb91c98
DG
244
245/*
246 * No need to reset a pollfd structure for poll(2)
247 */
248static inline void lttng_poll_reset(struct lttng_poll_event *events)
249{}
250
251/*
252 * Clean the events structure of a lttng_poll_event. It's the caller
253 * responsability to free the lttng_poll_event memory.
254 */
255static inline void lttng_poll_clean(struct lttng_poll_event *events)
256{
257 if (events) {
258 __lttng_poll_free((void *) events->events);
259 }
260}
261
262#endif /* HAVE_EPOLL */
263
264#endif /* _LTT_POLL_H */
This page took 0.058163 seconds and 4 git commands to generate.