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