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