Fix: add missing errno.h in pthread compat
[lttng-tools.git] / src / common / compat / compat-poll.c
CommitLineData
5eb91c98 1/*
ab5be9fa
MJ
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2019 Yannick Lamarre <ylamarre@efficios.com>
5eb91c98 4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
5eb91c98 6 *
5eb91c98
DG
7 */
8
6c1c0768 9#define _LGPL_SOURCE
d21b0d71 10#include <assert.h>
5eb91c98
DG
11#include <stdlib.h>
12#include <sys/resource.h>
13#include <sys/time.h>
f057dfc3 14#include <stdbool.h>
5eb91c98 15
990570ed 16#include <common/defaults.h>
db758600 17#include <common/error.h>
cfa9a5a2
DG
18#include <common/macros.h>
19#include <common/utils.h>
5eb91c98
DG
20
21#include "poll.h"
22
cc0acbd1
JG
23
24/*
25 * Maximum number of fd we can monitor.
26 *
27 * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by
28 * getrlimit(2).
29 */
30static unsigned int poll_max_size;
5eb91c98 31
d21b0d71
DG
32/*
33 * Resize the epoll events structure of the new size.
34 *
35 * Return 0 on success or else -1 with the current events pointer untouched.
36 */
37static int resize_poll_event(struct compat_poll_event_array *array,
38 uint32_t new_size)
39{
40 struct pollfd *ptr;
41
42 assert(array);
43
ac018a8b
DG
44 /* Refuse to resize the array more than the max size. */
45 if (new_size > poll_max_size) {
46 goto error;
47 }
48
d21b0d71
DG
49 ptr = realloc(array->events, new_size * sizeof(*ptr));
50 if (ptr == NULL) {
51 PERROR("realloc epoll add");
52 goto error;
53 }
53efb85a
MD
54 if (new_size > array->alloc_size) {
55 /* Zero newly allocated memory */
56 memset(ptr + array->alloc_size, 0,
57 (new_size - array->alloc_size) * sizeof(*ptr));
58 }
d21b0d71
DG
59 array->events = ptr;
60 array->alloc_size = new_size;
61
62 return 0;
63
64error:
65 return -1;
66}
67
68/*
69 * Update events with the current events object.
70 */
71static int update_current_events(struct lttng_poll_event *events)
72{
73 int ret;
74 struct compat_poll_event_array *current, *wait;
75
76 assert(events);
77
78 current = &events->current;
79 wait = &events->wait;
80
81 wait->nb_fd = current->nb_fd;
dbe23f45 82 if (current->alloc_size != wait->alloc_size) {
d21b0d71
DG
83 ret = resize_poll_event(wait, current->alloc_size);
84 if (ret < 0) {
85 goto error;
86 }
87 }
88 memcpy(wait->events, current->events,
89 current->nb_fd * sizeof(*current->events));
90
dbe23f45 91 /* Update is done. */
d21b0d71 92 events->need_update = 0;
d21b0d71
DG
93
94 return 0;
95
96error:
97 return -1;
98}
99
5eb91c98
DG
100/*
101 * Create pollfd data structure.
102 */
cc0acbd1 103LTTNG_HIDDEN
5eb91c98
DG
104int compat_poll_create(struct lttng_poll_event *events, int size)
105{
d21b0d71
DG
106 struct compat_poll_event_array *current, *wait;
107
5eb91c98
DG
108 if (events == NULL || size <= 0) {
109 ERR("Wrong arguments for poll create");
110 goto error;
111 }
112
dbe23f45 113 if (!poll_max_size) {
c607fe03
MJ
114 if (lttng_poll_set_max_size()) {
115 goto error;
116 }
dbe23f45
MD
117 }
118
5eb91c98
DG
119 /* Don't bust the limit here */
120 if (size > poll_max_size) {
121 size = poll_max_size;
122 }
123
d21b0d71
DG
124 /* Reset everything before begining the allocation. */
125 memset(events, 0, sizeof(struct lttng_poll_event));
126
d21b0d71
DG
127 current = &events->current;
128 wait = &events->wait;
129
5eb91c98 130 /* This *must* be freed by using lttng_poll_free() */
d21b0d71
DG
131 wait->events = zmalloc(size * sizeof(struct pollfd));
132 if (wait->events == NULL) {
6f04ed72 133 PERROR("zmalloc struct pollfd");
5eb91c98
DG
134 goto error;
135 }
136
d21b0d71
DG
137 wait->alloc_size = wait->init_size = size;
138
139 current->events = zmalloc(size * sizeof(struct pollfd));
140 if (current->events == NULL) {
6f04ed72 141 PERROR("zmalloc struct current pollfd");
d21b0d71
DG
142 goto error;
143 }
144
145 current->alloc_size = current->init_size = size;
5eb91c98
DG
146
147 return 0;
148
149error:
150 return -1;
151}
152
153/*
154 * Add fd to pollfd data structure with requested events.
155 */
cc0acbd1 156LTTNG_HIDDEN
5eb91c98
DG
157int compat_poll_add(struct lttng_poll_event *events, int fd,
158 uint32_t req_events)
159{
d21b0d71
DG
160 int new_size, ret, i;
161 struct compat_poll_event_array *current;
5eb91c98 162
d21b0d71 163 if (events == NULL || events->current.events == NULL || fd < 0) {
5eb91c98
DG
164 ERR("Bad compat poll add arguments");
165 goto error;
166 }
167
d21b0d71
DG
168 current = &events->current;
169
170 /* Check if fd we are trying to add is already there. */
171 for (i = 0; i < current->nb_fd; i++) {
d21b0d71
DG
172 if (current->events[i].fd == fd) {
173 errno = EEXIST;
5eb91c98
DG
174 goto error;
175 }
5eb91c98
DG
176 }
177
dbe23f45
MD
178 /* Resize array if needed. */
179 new_size = 1U << utils_get_count_order_u32(current->nb_fd + 1);
180 if (new_size != current->alloc_size && new_size >= current->init_size) {
d21b0d71
DG
181 ret = resize_poll_event(current, new_size);
182 if (ret < 0) {
183 goto error;
184 }
d21b0d71 185 }
5eb91c98 186
d21b0d71
DG
187 current->events[current->nb_fd].fd = fd;
188 current->events[current->nb_fd].events = req_events;
189 current->nb_fd++;
190 events->need_update = 1;
191
192 DBG("fd %d of %d added to pollfd", fd, current->nb_fd);
5eb91c98
DG
193
194 return 0;
195
196error:
197 return -1;
198}
199
f057dfc3
JG
200/*
201 * Modify an fd's events..
202 */
cc0acbd1 203LTTNG_HIDDEN
f057dfc3
JG
204int compat_poll_mod(struct lttng_poll_event *events, int fd,
205 uint32_t req_events)
206{
8a282751 207 int i;
f057dfc3
JG
208 struct compat_poll_event_array *current;
209
a1de8fcc
YL
210 if (events == NULL || events->current.nb_fd == 0 ||
211 events->current.events == NULL || fd < 0) {
f057dfc3
JG
212 ERR("Bad compat poll mod arguments");
213 goto error;
214 }
215
216 current = &events->current;
217
218 for (i = 0; i < current->nb_fd; i++) {
219 if (current->events[i].fd == fd) {
f057dfc3
JG
220 current->events[i].events = req_events;
221 events->need_update = 1;
222 break;
223 }
224 }
225
a1de8fcc
YL
226 /*
227 * The epoll flavor doesn't flag modifying a non-included FD as an
228 * error.
229 */
f057dfc3
JG
230
231 return 0;
232
233error:
234 return -1;
235}
236
5eb91c98
DG
237/*
238 * Remove a fd from the pollfd structure.
239 */
cc0acbd1 240LTTNG_HIDDEN
5eb91c98
DG
241int compat_poll_del(struct lttng_poll_event *events, int fd)
242{
a1de8fcc
YL
243 int i, count = 0, ret;
244 uint32_t new_size;
d21b0d71 245 struct compat_poll_event_array *current;
5eb91c98 246
a1de8fcc
YL
247 if (events == NULL || events->current.nb_fd == 0 ||
248 events->current.events == NULL || fd < 0) {
5eb91c98
DG
249 goto error;
250 }
251
d21b0d71
DG
252 /* Ease our life a bit. */
253 current = &events->current;
5eb91c98 254
d21b0d71 255 for (i = 0; i < current->nb_fd; i++) {
5eb91c98 256 /* Don't put back the fd we want to delete */
d21b0d71
DG
257 if (current->events[i].fd != fd) {
258 current->events[count].fd = current->events[i].fd;
259 current->events[count].events = current->events[i].events;
5eb91c98
DG
260 count++;
261 }
262 }
a1de8fcc
YL
263
264 /* The fd was not in our set, return no error as with epoll. */
265 if (current->nb_fd == count) {
266 goto end;
267 }
268
dbe23f45
MD
269 /* No fd duplicate should be ever added into array. */
270 assert(current->nb_fd - 1 == count);
271 current->nb_fd = count;
272
273 /* Resize array if needed. */
274 new_size = 1U << utils_get_count_order_u32(current->nb_fd);
a1de8fcc
YL
275 if (new_size != current->alloc_size && new_size >= current->init_size
276 && current->nb_fd != 0) {
dbe23f45
MD
277 ret = resize_poll_event(current, new_size);
278 if (ret < 0) {
279 goto error;
280 }
281 }
5eb91c98 282
d21b0d71 283 events->need_update = 1;
5eb91c98 284
a1de8fcc 285end:
5eb91c98
DG
286 return 0;
287
288error:
289 return -1;
290}
291
292/*
293 * Wait on poll() with timeout. Blocking call.
294 */
cc0acbd1 295LTTNG_HIDDEN
9f32e9bf
MD
296int compat_poll_wait(struct lttng_poll_event *events, int timeout,
297 bool interruptible)
5eb91c98 298{
22a73671
YL
299 int ret, active_fd_count;
300 int idle_pfd_index = 0;
301 size_t i;
5eb91c98 302
d21b0d71 303 if (events == NULL || events->current.events == NULL) {
5eb91c98
DG
304 ERR("poll wait arguments error");
305 goto error;
306 }
307
d21b0d71
DG
308 if (events->current.nb_fd == 0) {
309 /* Return an invalid error to be consistent with epoll. */
310 errno = EINVAL;
dbe23f45 311 events->wait.nb_fd = 0;
d21b0d71
DG
312 goto error;
313 }
314
315 if (events->need_update) {
316 ret = update_current_events(events);
317 if (ret < 0) {
318 errno = ENOMEM;
319 goto error;
320 }
321 }
322
a9b0dbc2
JG
323 do {
324 ret = poll(events->wait.events, events->wait.nb_fd, timeout);
9f32e9bf 325 } while (!interruptible && ret == -1 && errno == EINTR);
5eb91c98 326 if (ret < 0) {
9f32e9bf
MD
327 if (errno != EINTR) {
328 PERROR("poll wait");
329 }
5eb91c98
DG
330 goto error;
331 }
332
22a73671
YL
333 active_fd_count = ret;
334
9ddba525 335 /*
22a73671
YL
336 * Swap all active pollfd structs to the beginning of the
337 * array to emulate compat-epoll behaviour. This algorithm takes
338 * advantage of poll's returned value and the burst nature of active
339 * events on the file descriptors. The while loop guarantees that
340 * idle_pfd will always point to an idle fd.
9ddba525 341 */
22a73671
YL
342 if (active_fd_count == events->wait.nb_fd) {
343 goto end;
344 }
345 while (idle_pfd_index < active_fd_count &&
346 events->wait.events[idle_pfd_index].revents != 0) {
347 idle_pfd_index++;
348 }
349
350 for (i = idle_pfd_index + 1; idle_pfd_index < active_fd_count;
351 i++) {
352 struct pollfd swap_pfd;
353 struct pollfd *idle_pfd = &events->wait.events[idle_pfd_index];
354 struct pollfd *current_pfd = &events->wait.events[i];
355
fbccfe84 356 if (idle_pfd->revents != 0) {
22a73671
YL
357 swap_pfd = *current_pfd;
358 *current_pfd = *idle_pfd;
359 *idle_pfd = swap_pfd;
360 idle_pfd_index++;
361 }
362 }
363
364end:
365 return ret;
5eb91c98
DG
366
367error:
368 return -1;
369}
370
371/*
372 * Setup poll set maximum size.
373 */
cc0acbd1 374LTTNG_HIDDEN
dbe23f45 375int compat_poll_set_max_size(void)
5eb91c98 376{
dbe23f45 377 int ret, retval = 0;
5eb91c98
DG
378 struct rlimit lim;
379
5eb91c98
DG
380 ret = getrlimit(RLIMIT_NOFILE, &lim);
381 if (ret < 0) {
6f04ed72 382 PERROR("getrlimit poll RLIMIT_NOFILE");
dbe23f45
MD
383 retval = -1;
384 goto end;
5eb91c98
DG
385 }
386
387 poll_max_size = lim.rlim_cur;
dbe23f45 388end:
d21b0d71 389 if (poll_max_size == 0) {
990570ed 390 poll_max_size = DEFAULT_POLL_SIZE;
5eb91c98 391 }
5eb91c98 392 DBG("poll set max size set to %u", poll_max_size);
dbe23f45 393 return retval;
5eb91c98 394}
This page took 0.075169 seconds and 4 git commands to generate.