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