Fix: libcompat is now part of libcommon
[lttng-tools.git] / src / common / compat / compat-epoll.c
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 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include <common/error.h>
28 #include <common/defaults.h>
29 #include <common/macros.h>
30 #include <common/utils.h>
31
32 #include "poll.h"
33
34 unsigned int poll_max_size;
35
36 /*
37 * Resize the epoll events structure of the new size.
38 *
39 * Return 0 on success or else -1 with the current events pointer untouched.
40 */
41 static int resize_poll_event(struct lttng_poll_event *events,
42 uint32_t new_size)
43 {
44 struct epoll_event *ptr;
45
46 assert(events);
47
48 ptr = realloc(events->events, new_size * sizeof(*ptr));
49 if (ptr == NULL) {
50 PERROR("realloc epoll add");
51 goto error;
52 }
53 if (new_size > events->alloc_size) {
54 /* Zero newly allocated memory */
55 memset(ptr + events->alloc_size, 0,
56 (new_size - events->alloc_size) * sizeof(*ptr));
57 }
58 events->events = ptr;
59 events->alloc_size = new_size;
60
61 return 0;
62
63 error:
64 return -1;
65 }
66
67 /*
68 * Create epoll set and allocate returned events structure.
69 */
70 LTTNG_HIDDEN
71 int compat_epoll_create(struct lttng_poll_event *events, int size, int flags)
72 {
73 int ret;
74
75 if (events == NULL || size <= 0) {
76 goto error;
77 }
78
79 if (!poll_max_size) {
80 if (lttng_poll_set_max_size()) {
81 goto error;
82 }
83 }
84
85 /* Don't bust the limit here */
86 if (size > poll_max_size) {
87 size = poll_max_size;
88 }
89
90 ret = compat_glibc_epoll_create(size, flags);
91 if (ret < 0) {
92 /* At this point, every error is fatal */
93 PERROR("epoll_create1");
94 goto error;
95 }
96
97 events->epfd = ret;
98
99 /* This *must* be freed by using lttng_poll_free() */
100 events->events = zmalloc(size * sizeof(struct epoll_event));
101 if (events->events == NULL) {
102 PERROR("zmalloc epoll set");
103 goto error_close;
104 }
105
106 events->alloc_size = events->init_size = size;
107 events->nb_fd = 0;
108
109 return 0;
110
111 error_close:
112 ret = close(events->epfd);
113 if (ret) {
114 PERROR("close");
115 }
116 error:
117 return -1;
118 }
119
120 /*
121 * Add a fd to the epoll set with requesting events.
122 */
123 LTTNG_HIDDEN
124 int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_events)
125 {
126 int ret;
127 struct epoll_event ev;
128
129 if (events == NULL || events->events == NULL || fd < 0) {
130 ERR("Bad compat epoll add arguments");
131 goto error;
132 }
133
134 /*
135 * Zero struct epoll_event to ensure all representations of its
136 * union are zeroed.
137 */
138 memset(&ev, 0, sizeof(ev));
139 ev.events = req_events;
140 ev.data.fd = fd;
141
142 ret = epoll_ctl(events->epfd, EPOLL_CTL_ADD, fd, &ev);
143 if (ret < 0) {
144 switch (errno) {
145 case EEXIST:
146 /* If exist, it's OK. */
147 goto end;
148 case ENOSPC:
149 case EPERM:
150 /* Print PERROR and goto end not failing. Show must go on. */
151 PERROR("epoll_ctl ADD");
152 goto end;
153 default:
154 PERROR("epoll_ctl ADD fatal");
155 goto error;
156 }
157 }
158
159 events->nb_fd++;
160
161 end:
162 return 0;
163
164 error:
165 return -1;
166 }
167
168 /*
169 * Remove a fd from the epoll set.
170 */
171 LTTNG_HIDDEN
172 int compat_epoll_del(struct lttng_poll_event *events, int fd)
173 {
174 int ret;
175
176 if (events == NULL || fd < 0 || events->nb_fd == 0) {
177 goto error;
178 }
179
180 ret = epoll_ctl(events->epfd, EPOLL_CTL_DEL, fd, NULL);
181 if (ret < 0) {
182 switch (errno) {
183 case ENOENT:
184 case EPERM:
185 /* Print PERROR and goto end not failing. Show must go on. */
186 PERROR("epoll_ctl DEL");
187 goto end;
188 default:
189 PERROR("epoll_ctl DEL fatal");
190 goto error;
191 }
192 }
193
194 events->nb_fd--;
195
196 end:
197 return 0;
198
199 error:
200 return -1;
201 }
202
203 /*
204 * Set an fd's events.
205 */
206 LTTNG_HIDDEN
207 int compat_epoll_mod(struct lttng_poll_event *events, int fd, uint32_t req_events)
208 {
209 int ret;
210 struct epoll_event ev;
211
212 if (events == NULL || fd < 0 || events->nb_fd == 0) {
213 goto error;
214 }
215
216 /*
217 * Zero struct epoll_event to ensure all representations of its
218 * union are zeroed.
219 */
220 memset(&ev, 0, sizeof(ev));
221 ev.events = req_events;
222 ev.data.fd = fd;
223
224 ret = epoll_ctl(events->epfd, EPOLL_CTL_MOD, fd, &ev);
225 if (ret < 0) {
226 switch (errno) {
227 case ENOENT:
228 case EPERM:
229 /* Print PERROR and goto end not failing. Show must go on. */
230 PERROR("epoll_ctl MOD");
231 goto end;
232 default:
233 PERROR("epoll_ctl MOD fatal");
234 goto error;
235 }
236 }
237
238 end:
239 return 0;
240
241 error:
242 return -1;
243 }
244
245 /*
246 * Wait on epoll set. This is a blocking call of timeout value.
247 */
248 LTTNG_HIDDEN
249 int compat_epoll_wait(struct lttng_poll_event *events, int timeout)
250 {
251 int ret;
252 uint32_t new_size;
253
254 if (events == NULL || events->events == NULL) {
255 ERR("Wrong arguments in compat_epoll_wait");
256 goto error;
257 }
258
259 if (events->nb_fd == 0) {
260 errno = EINVAL;
261 return -1;
262 }
263
264 /*
265 * Resize if needed before waiting. We could either expand the array or
266 * shrink it down. It's important to note that after this step, we are
267 * ensured that the events argument of the epoll_wait call will be large
268 * enough to hold every possible returned events.
269 */
270 new_size = 1U << utils_get_count_order_u32(events->nb_fd);
271 if (new_size != events->alloc_size && new_size >= events->init_size) {
272 ret = resize_poll_event(events, new_size);
273 if (ret < 0) {
274 /* ENOMEM problem at this point. */
275 goto error;
276 }
277 }
278
279 do {
280 ret = epoll_wait(events->epfd, events->events, events->nb_fd, timeout);
281 } while (ret == -1 && errno == EINTR);
282 if (ret < 0) {
283 /* At this point, every error is fatal */
284 PERROR("epoll_wait");
285 goto error;
286 }
287
288 /*
289 * Since the returned events are set sequentially in the "events" structure
290 * we only need to return the epoll_wait value and iterate over it.
291 */
292 return ret;
293
294 error:
295 return -1;
296 }
297
298 /*
299 * Setup poll set maximum size.
300 */
301 LTTNG_HIDDEN
302 int compat_epoll_set_max_size(void)
303 {
304 int ret, fd, retval = 0;
305 ssize_t size_ret;
306 char buf[64];
307
308 fd = open(COMPAT_EPOLL_PROC_PATH, O_RDONLY);
309 if (fd < 0) {
310 /*
311 * Failing on opening [1] is not an error per see. [1] was
312 * introduced in Linux 2.6.28 but epoll is available since
313 * 2.5.44. Hence, goto end and set a default value without
314 * setting an error return value.
315 *
316 * [1] /proc/sys/fs/epoll/max_user_watches
317 */
318 retval = 0;
319 goto end;
320 }
321
322 size_ret = lttng_read(fd, buf, sizeof(buf));
323 /*
324 * Allow reading a file smaller than buf, but keep space for
325 * final \0.
326 */
327 if (size_ret < 0 || size_ret >= sizeof(buf)) {
328 PERROR("read set max size");
329 retval = -1;
330 goto end_read;
331 }
332 buf[size_ret] = '\0';
333 poll_max_size = atoi(buf);
334 end_read:
335 ret = close(fd);
336 if (ret) {
337 PERROR("close");
338 }
339 end:
340 if (!poll_max_size) {
341 poll_max_size = DEFAULT_POLL_SIZE;
342 }
343 DBG("epoll set max size is %d", poll_max_size);
344 return retval;
345 }
This page took 0.037899 seconds and 6 git commands to generate.