Fix: hang in thread_rotation when using compat-poll
[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{
296 int ret;
297
d21b0d71 298 if (events == NULL || events->current.events == NULL) {
5eb91c98
DG
299 ERR("poll wait arguments error");
300 goto error;
301 }
dbe23f45 302 assert(events->current.nb_fd >= 0);
5eb91c98 303
d21b0d71
DG
304 if (events->current.nb_fd == 0) {
305 /* Return an invalid error to be consistent with epoll. */
306 errno = EINVAL;
dbe23f45 307 events->wait.nb_fd = 0;
d21b0d71
DG
308 goto error;
309 }
310
311 if (events->need_update) {
312 ret = update_current_events(events);
313 if (ret < 0) {
314 errno = ENOMEM;
315 goto error;
316 }
317 }
318
a9b0dbc2
JG
319 do {
320 ret = poll(events->wait.events, events->wait.nb_fd, timeout);
321 } while (ret == -1 && errno == EINTR);
5eb91c98
DG
322 if (ret < 0) {
323 /* At this point, every error is fatal */
6f04ed72 324 PERROR("poll wait");
5eb91c98
DG
325 goto error;
326 }
327
9ddba525
DG
328 /*
329 * poll() should always iterate on all FDs since we handle the pollset in
330 * user space and after poll returns, we have to try every fd for a match.
331 */
d21b0d71 332 return events->wait.nb_fd;
5eb91c98
DG
333
334error:
335 return -1;
336}
337
338/*
339 * Setup poll set maximum size.
340 */
dbe23f45 341int compat_poll_set_max_size(void)
5eb91c98 342{
dbe23f45 343 int ret, retval = 0;
5eb91c98
DG
344 struct rlimit lim;
345
5eb91c98
DG
346 ret = getrlimit(RLIMIT_NOFILE, &lim);
347 if (ret < 0) {
6f04ed72 348 PERROR("getrlimit poll RLIMIT_NOFILE");
dbe23f45
MD
349 retval = -1;
350 goto end;
5eb91c98
DG
351 }
352
353 poll_max_size = lim.rlim_cur;
dbe23f45 354end:
d21b0d71 355 if (poll_max_size == 0) {
990570ed 356 poll_max_size = DEFAULT_POLL_SIZE;
5eb91c98 357 }
5eb91c98 358 DBG("poll set max size set to %u", poll_max_size);
dbe23f45 359 return retval;
5eb91c98 360}
This page took 0.105345 seconds and 4 git commands to generate.