Fix: libcompat is now part of libcommon
[lttng-tools.git] / src / common / compat / compat-epoll.c
CommitLineData
5eb91c98
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
5eb91c98
DG
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 *
d14d33bf
AM
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.
5eb91c98
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
d21b0d71 19#include <assert.h>
5eb91c98
DG
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
db758600 27#include <common/error.h>
990570ed 28#include <common/defaults.h>
cfa9a5a2
DG
29#include <common/macros.h>
30#include <common/utils.h>
5eb91c98
DG
31
32#include "poll.h"
33
34unsigned int poll_max_size;
35
d21b0d71
DG
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 */
41static 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 }
53efb85a
MD
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 }
d21b0d71
DG
58 events->events = ptr;
59 events->alloc_size = new_size;
60
61 return 0;
62
63error:
64 return -1;
65}
66
5eb91c98
DG
67/*
68 * Create epoll set and allocate returned events structure.
69 */
db14a5c9 70LTTNG_HIDDEN
5eb91c98
DG
71int 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
dbe23f45 79 if (!poll_max_size) {
22dad568
JG
80 if (lttng_poll_set_max_size()) {
81 goto error;
82 }
dbe23f45
MD
83 }
84
5eb91c98 85 /* Don't bust the limit here */
dbe23f45 86 if (size > poll_max_size) {
5eb91c98
DG
87 size = poll_max_size;
88 }
89
f263b7fd 90 ret = compat_glibc_epoll_create(size, flags);
5eb91c98
DG
91 if (ret < 0) {
92 /* At this point, every error is fatal */
4c462e79 93 PERROR("epoll_create1");
5eb91c98
DG
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) {
4c462e79 102 PERROR("zmalloc epoll set");
5eb91c98
DG
103 goto error_close;
104 }
105
d21b0d71 106 events->alloc_size = events->init_size = size;
5eb91c98
DG
107 events->nb_fd = 0;
108
109 return 0;
110
111error_close:
4c462e79
MD
112 ret = close(events->epfd);
113 if (ret) {
114 PERROR("close");
115 }
5eb91c98
DG
116error:
117 return -1;
118}
119
120/*
121 * Add a fd to the epoll set with requesting events.
122 */
db14a5c9 123LTTNG_HIDDEN
5eb91c98
DG
124int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_events)
125{
d21b0d71
DG
126 int ret;
127 struct epoll_event ev;
5eb91c98
DG
128
129 if (events == NULL || events->events == NULL || fd < 0) {
130 ERR("Bad compat epoll add arguments");
131 goto error;
132 }
133
53efb85a
MD
134 /*
135 * Zero struct epoll_event to ensure all representations of its
136 * union are zeroed.
137 */
138 memset(&ev, 0, sizeof(ev));
5eb91c98
DG
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:
b7a6b49f
DG
146 /* If exist, it's OK. */
147 goto end;
5eb91c98
DG
148 case ENOSPC:
149 case EPERM:
4c462e79
MD
150 /* Print PERROR and goto end not failing. Show must go on. */
151 PERROR("epoll_ctl ADD");
5eb91c98
DG
152 goto end;
153 default:
4c462e79 154 PERROR("epoll_ctl ADD fatal");
5eb91c98
DG
155 goto error;
156 }
157 }
158
159 events->nb_fd++;
160
5eb91c98
DG
161end:
162 return 0;
163
164error:
165 return -1;
166}
167
168/*
169 * Remove a fd from the epoll set.
170 */
db14a5c9 171LTTNG_HIDDEN
5eb91c98
DG
172int compat_epoll_del(struct lttng_poll_event *events, int fd)
173{
174 int ret;
175
dbe23f45 176 if (events == NULL || fd < 0 || events->nb_fd == 0) {
5eb91c98
DG
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:
4c462e79
MD
185 /* Print PERROR and goto end not failing. Show must go on. */
186 PERROR("epoll_ctl DEL");
5eb91c98
DG
187 goto end;
188 default:
4c462e79 189 PERROR("epoll_ctl DEL fatal");
5eb91c98
DG
190 goto error;
191 }
5eb91c98
DG
192 }
193
194 events->nb_fd--;
195
196end:
197 return 0;
f057dfc3
JG
198
199error:
200 return -1;
201}
202
203/*
204 * Set an fd's events.
205 */
db14a5c9 206LTTNG_HIDDEN
f057dfc3
JG
207int 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
238end:
239 return 0;
5eb91c98
DG
240
241error:
242 return -1;
243}
244
245/*
246 * Wait on epoll set. This is a blocking call of timeout value.
247 */
db14a5c9 248LTTNG_HIDDEN
5eb91c98
DG
249int compat_epoll_wait(struct lttng_poll_event *events, int timeout)
250{
251 int ret;
d21b0d71 252 uint32_t new_size;
5eb91c98 253
d21b0d71 254 if (events == NULL || events->events == NULL) {
5eb91c98
DG
255 ERR("Wrong arguments in compat_epoll_wait");
256 goto error;
257 }
dbe23f45
MD
258
259 if (events->nb_fd == 0) {
260 errno = EINVAL;
261 return -1;
262 }
5eb91c98 263
d21b0d71
DG
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 */
dbe23f45
MD
270 new_size = 1U << utils_get_count_order_u32(events->nb_fd);
271 if (new_size != events->alloc_size && new_size >= events->init_size) {
d21b0d71
DG
272 ret = resize_poll_event(events, new_size);
273 if (ret < 0) {
274 /* ENOMEM problem at this point. */
275 goto error;
276 }
277 }
278
3ada8405
DG
279 do {
280 ret = epoll_wait(events->epfd, events->events, events->nb_fd, timeout);
281 } while (ret == -1 && errno == EINTR);
5eb91c98
DG
282 if (ret < 0) {
283 /* At this point, every error is fatal */
4c462e79 284 PERROR("epoll_wait");
5eb91c98
DG
285 goto error;
286 }
287
9ddba525
DG
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 */
5eb91c98
DG
292 return ret;
293
294error:
295 return -1;
296}
297
298/*
299 * Setup poll set maximum size.
300 */
db14a5c9 301LTTNG_HIDDEN
dbe23f45 302int compat_epoll_set_max_size(void)
5eb91c98 303{
dbe23f45 304 int ret, fd, retval = 0;
13021756 305 ssize_t size_ret;
5eb91c98
DG
306 char buf[64];
307
990570ed 308 fd = open(COMPAT_EPOLL_PROC_PATH, O_RDONLY);
5eb91c98 309 if (fd < 0) {
d3f531ff
JR
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;
dbe23f45 319 goto end;
5eb91c98
DG
320 }
321
6cd525e8
MD
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)) {
4c462e79 328 PERROR("read set max size");
dbe23f45
MD
329 retval = -1;
330 goto end_read;
5eb91c98 331 }
6cd525e8 332 buf[size_ret] = '\0';
5eb91c98 333 poll_max_size = atoi(buf);
dbe23f45 334end_read:
4c462e79
MD
335 ret = close(fd);
336 if (ret) {
337 PERROR("close");
338 }
dbe23f45
MD
339end:
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;
5eb91c98 345}
This page took 0.078971 seconds and 5 git commands to generate.