Fix: namespace our gettid wrapper
[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 * 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.
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 *
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.
17 */
18
19 #define _LGPL_SOURCE
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <sys/resource.h>
23 #include <sys/time.h>
24 #include <stdbool.h>
25
26 #include <common/defaults.h>
27 #include <common/error.h>
28 #include <common/macros.h>
29 #include <common/utils.h>
30
31 #include "poll.h"
32
33 unsigned int poll_max_size;
34
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 */
40 static 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
47 /* Refuse to resize the array more than the max size. */
48 if (new_size > poll_max_size) {
49 goto error;
50 }
51
52 ptr = realloc(array->events, new_size * sizeof(*ptr));
53 if (ptr == NULL) {
54 PERROR("realloc epoll add");
55 goto error;
56 }
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 }
62 array->events = ptr;
63 array->alloc_size = new_size;
64
65 return 0;
66
67 error:
68 return -1;
69 }
70
71 /*
72 * Update events with the current events object.
73 */
74 static 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;
85 if (current->alloc_size != wait->alloc_size) {
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
94 /* Update is done. */
95 events->need_update = 0;
96
97 return 0;
98
99 error:
100 return -1;
101 }
102
103 /*
104 * Create pollfd data structure.
105 */
106 int compat_poll_create(struct lttng_poll_event *events, int size)
107 {
108 struct compat_poll_event_array *current, *wait;
109
110 if (events == NULL || size <= 0) {
111 ERR("Wrong arguments for poll create");
112 goto error;
113 }
114
115 if (!poll_max_size) {
116 if (lttng_poll_set_max_size()) {
117 goto error;
118 }
119 }
120
121 /* Don't bust the limit here */
122 if (size > poll_max_size) {
123 size = poll_max_size;
124 }
125
126 /* Reset everything before begining the allocation. */
127 memset(events, 0, sizeof(struct lttng_poll_event));
128
129 current = &events->current;
130 wait = &events->wait;
131
132 /* This *must* be freed by using lttng_poll_free() */
133 wait->events = zmalloc(size * sizeof(struct pollfd));
134 if (wait->events == NULL) {
135 PERROR("zmalloc struct pollfd");
136 goto error;
137 }
138
139 wait->alloc_size = wait->init_size = size;
140
141 current->events = zmalloc(size * sizeof(struct pollfd));
142 if (current->events == NULL) {
143 PERROR("zmalloc struct current pollfd");
144 goto error;
145 }
146
147 current->alloc_size = current->init_size = size;
148
149 return 0;
150
151 error:
152 return -1;
153 }
154
155 /*
156 * Add fd to pollfd data structure with requested events.
157 */
158 int compat_poll_add(struct lttng_poll_event *events, int fd,
159 uint32_t req_events)
160 {
161 int new_size, ret, i;
162 struct compat_poll_event_array *current;
163
164 if (events == NULL || events->current.events == NULL || fd < 0) {
165 ERR("Bad compat poll add arguments");
166 goto error;
167 }
168
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++) {
173 if (current->events[i].fd == fd) {
174 errno = EEXIST;
175 goto error;
176 }
177 }
178
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) {
182 ret = resize_poll_event(current, new_size);
183 if (ret < 0) {
184 goto error;
185 }
186 }
187
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);
194
195 return 0;
196
197 error:
198 return -1;
199 }
200
201 /*
202 * Modify an fd's events..
203 */
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 int compat_poll_del(struct lttng_poll_event *events, int fd)
241 {
242 int i, count = 0, ret;
243 uint32_t new_size;
244 struct compat_poll_event_array *current;
245
246 if (events == NULL || events->current.nb_fd == 0 ||
247 events->current.events == NULL || fd < 0) {
248 goto error;
249 }
250
251 /* Ease our life a bit. */
252 current = &events->current;
253
254 for (i = 0; i < current->nb_fd; i++) {
255 /* Don't put back the fd we want to delete */
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;
259 count++;
260 }
261 }
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
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);
274 if (new_size != current->alloc_size && new_size >= current->init_size
275 && current->nb_fd != 0) {
276 ret = resize_poll_event(current, new_size);
277 if (ret < 0) {
278 goto error;
279 }
280 }
281
282 events->need_update = 1;
283
284 end:
285 return 0;
286
287 error:
288 return -1;
289 }
290
291 /*
292 * Wait on poll() with timeout. Blocking call.
293 */
294 int compat_poll_wait(struct lttng_poll_event *events, int timeout,
295 bool interruptible)
296 {
297 int ret, active_fd_count;
298 int idle_pfd_index = 0;
299 size_t i;
300
301 if (events == NULL || events->current.events == NULL) {
302 ERR("poll wait arguments error");
303 goto error;
304 }
305 assert(events->current.nb_fd >= 0);
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 * Swap all active pollfd structs to the beginning of the
336 * array to emulate compat-epoll behaviour. This algorithm takes
337 * advantage of poll's returned value and the burst nature of active
338 * events on the file descriptors. The while loop guarantees that
339 * idle_pfd will always point to an idle fd.
340 */
341 if (active_fd_count == events->wait.nb_fd) {
342 goto end;
343 }
344 while (idle_pfd_index < active_fd_count &&
345 events->wait.events[idle_pfd_index].revents != 0) {
346 idle_pfd_index++;
347 }
348
349 for (i = idle_pfd_index + 1; idle_pfd_index < active_fd_count;
350 i++) {
351 struct pollfd swap_pfd;
352 struct pollfd *idle_pfd = &events->wait.events[idle_pfd_index];
353 struct pollfd *current_pfd = &events->wait.events[i];
354
355 if (idle_pfd->revents != 0) {
356 swap_pfd = *current_pfd;
357 *current_pfd = *idle_pfd;
358 *idle_pfd = swap_pfd;
359 idle_pfd_index++;
360 }
361 }
362
363 end:
364 return ret;
365
366 error:
367 return -1;
368 }
369
370 /*
371 * Setup poll set maximum size.
372 */
373 int compat_poll_set_max_size(void)
374 {
375 int ret, retval = 0;
376 struct rlimit lim;
377
378 ret = getrlimit(RLIMIT_NOFILE, &lim);
379 if (ret < 0) {
380 PERROR("getrlimit poll RLIMIT_NOFILE");
381 retval = -1;
382 goto end;
383 }
384
385 poll_max_size = lim.rlim_cur;
386 end:
387 if (poll_max_size == 0) {
388 poll_max_size = DEFAULT_POLL_SIZE;
389 }
390 DBG("poll set max size set to %u", poll_max_size);
391 return retval;
392 }
This page took 0.038355 seconds and 4 git commands to generate.