01de4f5489c578ebfcdd9caf50ca95920f8b1163
[lttng-tools.git] / src / common / compat / compat-poll.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2019 Yannick Lamarre <ylamarre@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include <assert.h>
11 #include <stdlib.h>
12 #include <sys/resource.h>
13 #include <sys/time.h>
14 #include <stdbool.h>
15
16 #include <common/defaults.h>
17 #include <common/error.h>
18 #include <common/macros.h>
19 #include <common/utils.h>
20
21 #include "poll.h"
22
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 */
30 static unsigned int poll_max_size;
31
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 */
37 static 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
44 /* Refuse to resize the array more than the max size. */
45 if (new_size > poll_max_size) {
46 goto error;
47 }
48
49 ptr = realloc(array->events, new_size * sizeof(*ptr));
50 if (ptr == NULL) {
51 PERROR("realloc epoll add");
52 goto error;
53 }
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 }
59 array->events = ptr;
60 array->alloc_size = new_size;
61
62 return 0;
63
64 error:
65 return -1;
66 }
67
68 /*
69 * Update events with the current events object.
70 */
71 static 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;
82 if (current->alloc_size != wait->alloc_size) {
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
91 /* Update is done. */
92 events->need_update = 0;
93
94 return 0;
95
96 error:
97 return -1;
98 }
99
100 /*
101 * Create pollfd data structure.
102 */
103 LTTNG_HIDDEN
104 int compat_poll_create(struct lttng_poll_event *events, int size)
105 {
106 struct compat_poll_event_array *current, *wait;
107
108 if (events == NULL || size <= 0) {
109 ERR("Wrong arguments for poll create");
110 goto error;
111 }
112
113 if (!poll_max_size) {
114 if (lttng_poll_set_max_size()) {
115 goto error;
116 }
117 }
118
119 /* Don't bust the limit here */
120 if (size > poll_max_size) {
121 size = poll_max_size;
122 }
123
124 /* Reset everything before begining the allocation. */
125 memset(events, 0, sizeof(struct lttng_poll_event));
126
127 current = &events->current;
128 wait = &events->wait;
129
130 /* This *must* be freed by using lttng_poll_free() */
131 wait->events = zmalloc(size * sizeof(struct pollfd));
132 if (wait->events == NULL) {
133 PERROR("zmalloc struct pollfd");
134 goto error;
135 }
136
137 wait->alloc_size = wait->init_size = size;
138
139 current->events = zmalloc(size * sizeof(struct pollfd));
140 if (current->events == NULL) {
141 PERROR("zmalloc struct current pollfd");
142 goto error;
143 }
144
145 current->alloc_size = current->init_size = size;
146
147 return 0;
148
149 error:
150 return -1;
151 }
152
153 /*
154 * Add fd to pollfd data structure with requested events.
155 */
156 LTTNG_HIDDEN
157 int compat_poll_add(struct lttng_poll_event *events, int fd,
158 uint32_t req_events)
159 {
160 int new_size, ret, i;
161 struct compat_poll_event_array *current;
162
163 if (events == NULL || events->current.events == NULL || fd < 0) {
164 ERR("Bad compat poll add arguments");
165 goto error;
166 }
167
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++) {
172 if (current->events[i].fd == fd) {
173 errno = EEXIST;
174 goto error;
175 }
176 }
177
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) {
181 ret = resize_poll_event(current, new_size);
182 if (ret < 0) {
183 goto error;
184 }
185 }
186
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);
193
194 return 0;
195
196 error:
197 return -1;
198 }
199
200 /*
201 * Modify an fd's events..
202 */
203 LTTNG_HIDDEN
204 int compat_poll_mod(struct lttng_poll_event *events, int fd,
205 uint32_t req_events)
206 {
207 int i;
208 struct compat_poll_event_array *current;
209
210 if (events == NULL || events->current.nb_fd == 0 ||
211 events->current.events == NULL || fd < 0) {
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) {
220 current->events[i].events = req_events;
221 events->need_update = 1;
222 break;
223 }
224 }
225
226 /*
227 * The epoll flavor doesn't flag modifying a non-included FD as an
228 * error.
229 */
230
231 return 0;
232
233 error:
234 return -1;
235 }
236
237 /*
238 * Remove a fd from the pollfd structure.
239 */
240 LTTNG_HIDDEN
241 int compat_poll_del(struct lttng_poll_event *events, int fd)
242 {
243 int i, count = 0, ret;
244 uint32_t new_size;
245 struct compat_poll_event_array *current;
246
247 if (events == NULL || events->current.nb_fd == 0 ||
248 events->current.events == NULL || fd < 0) {
249 goto error;
250 }
251
252 /* Ease our life a bit. */
253 current = &events->current;
254
255 for (i = 0; i < current->nb_fd; i++) {
256 /* Don't put back the fd we want to delete */
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;
260 count++;
261 }
262 }
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
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);
275 if (new_size != current->alloc_size && new_size >= current->init_size
276 && current->nb_fd != 0) {
277 ret = resize_poll_event(current, new_size);
278 if (ret < 0) {
279 goto error;
280 }
281 }
282
283 events->need_update = 1;
284
285 end:
286 return 0;
287
288 error:
289 return -1;
290 }
291
292 /*
293 * Wait on poll() with timeout. Blocking call.
294 */
295 LTTNG_HIDDEN
296 int compat_poll_wait(struct lttng_poll_event *events, int timeout,
297 bool interruptible)
298 {
299 int ret, active_fd_count;
300 size_t pos = 0, consecutive_entries = 0, non_idle_pos;
301
302 if (events == NULL || events->current.events == NULL) {
303 ERR("poll wait arguments error");
304 goto error;
305 }
306
307 if (events->current.nb_fd == 0) {
308 /* Return an invalid error to be consistent with epoll. */
309 errno = EINVAL;
310 events->wait.nb_fd = 0;
311 goto error;
312 }
313
314 if (events->need_update) {
315 ret = update_current_events(events);
316 if (ret < 0) {
317 errno = ENOMEM;
318 goto error;
319 }
320 }
321
322 do {
323 ret = poll(events->wait.events, events->wait.nb_fd, timeout);
324 } while (!interruptible && ret == -1 && errno == EINTR);
325 if (ret < 0) {
326 if (errno != EINTR) {
327 PERROR("poll wait");
328 }
329 goto error;
330 }
331
332 active_fd_count = ret;
333
334 /*
335 * Move all active pollfd structs to the beginning of the
336 * array to emulate compat-epoll behaviour.
337 */
338 if (active_fd_count == events->wait.nb_fd) {
339 goto end;
340 }
341
342 while (consecutive_entries != active_fd_count) {
343 struct pollfd *current = &events->wait.events[pos];
344 struct pollfd idle_entry;
345
346 if (current->revents != 0) {
347 consecutive_entries++;
348 pos++;
349 continue;
350 }
351
352 non_idle_pos = pos;
353
354 /* Look for next non-idle entry. */
355 while (events->wait.events[++non_idle_pos].revents == 0);
356
357 /* Swap idle and non-idle entries. */
358 idle_entry = *current;
359 *current = events->wait.events[non_idle_pos];
360 events->wait.events[non_idle_pos] = idle_entry;
361
362 consecutive_entries++;
363 pos++;
364 }
365 end:
366 return ret;
367
368 error:
369 return -1;
370 }
371
372 /*
373 * Setup poll set maximum size.
374 */
375 LTTNG_HIDDEN
376 int compat_poll_set_max_size(void)
377 {
378 int ret, retval = 0;
379 struct rlimit lim;
380
381 ret = getrlimit(RLIMIT_NOFILE, &lim);
382 if (ret < 0) {
383 PERROR("getrlimit poll RLIMIT_NOFILE");
384 retval = -1;
385 goto end;
386 }
387
388 poll_max_size = lim.rlim_cur;
389 end:
390 if (poll_max_size == 0) {
391 poll_max_size = DEFAULT_POLL_SIZE;
392 }
393 DBG("poll set max size set to %u", poll_max_size);
394 return retval;
395 }
This page took 0.037564 seconds and 4 git commands to generate.