Fix: libcompat is now part of libcommon
[lttng-tools.git] / src / common / compat / compat-poll.c
CommitLineData
5eb91c98
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
5eb91c98
DG
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5eb91c98
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
d21b0d71 19#include <assert.h>
5eb91c98
DG
20#include <stdlib.h>
21#include <sys/resource.h>
22#include <sys/time.h>
f057dfc3 23#include <stdbool.h>
5eb91c98 24
990570ed 25#include <common/defaults.h>
db758600 26#include <common/error.h>
cfa9a5a2
DG
27#include <common/macros.h>
28#include <common/utils.h>
5eb91c98
DG
29
30#include "poll.h"
31
32unsigned int poll_max_size;
33
d21b0d71
DG
34/*
35 * Resize the epoll events structure of the new size.
36 *
37 * Return 0 on success or else -1 with the current events pointer untouched.
38 */
39static int resize_poll_event(struct compat_poll_event_array *array,
40 uint32_t new_size)
41{
42 struct pollfd *ptr;
43
44 assert(array);
45
ac018a8b
DG
46 /* Refuse to resize the array more than the max size. */
47 if (new_size > poll_max_size) {
48 goto error;
49 }
50
d21b0d71
DG
51 ptr = realloc(array->events, new_size * sizeof(*ptr));
52 if (ptr == NULL) {
53 PERROR("realloc epoll add");
54 goto error;
55 }
53efb85a
MD
56 if (new_size > array->alloc_size) {
57 /* Zero newly allocated memory */
58 memset(ptr + array->alloc_size, 0,
59 (new_size - array->alloc_size) * sizeof(*ptr));
60 }
d21b0d71
DG
61 array->events = ptr;
62 array->alloc_size = new_size;
63
64 return 0;
65
66error:
67 return -1;
68}
69
70/*
71 * Update events with the current events object.
72 */
73static int update_current_events(struct lttng_poll_event *events)
74{
75 int ret;
76 struct compat_poll_event_array *current, *wait;
77
78 assert(events);
79
80 current = &events->current;
81 wait = &events->wait;
82
83 wait->nb_fd = current->nb_fd;
dbe23f45 84 if (current->alloc_size != wait->alloc_size) {
d21b0d71
DG
85 ret = resize_poll_event(wait, current->alloc_size);
86 if (ret < 0) {
87 goto error;
88 }
89 }
90 memcpy(wait->events, current->events,
91 current->nb_fd * sizeof(*current->events));
92
dbe23f45 93 /* Update is done. */
d21b0d71 94 events->need_update = 0;
d21b0d71
DG
95
96 return 0;
97
98error:
99 return -1;
100}
101
5eb91c98
DG
102/*
103 * Create pollfd data structure.
104 */
db14a5c9 105LTTNG_HIDDEN
5eb91c98
DG
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 */
db14a5c9 204LTTNG_HIDDEN
f057dfc3
JG
205int compat_poll_mod(struct lttng_poll_event *events, int fd,
206 uint32_t req_events)
207{
8a282751 208 int i;
f057dfc3
JG
209 bool fd_found = false;
210 struct compat_poll_event_array *current;
211
212 if (events == NULL || events->current.events == NULL || fd < 0) {
213 ERR("Bad compat poll mod arguments");
214 goto error;
215 }
216
217 current = &events->current;
218
219 for (i = 0; i < current->nb_fd; i++) {
220 if (current->events[i].fd == fd) {
221 fd_found = true;
222 current->events[i].events = req_events;
223 events->need_update = 1;
224 break;
225 }
226 }
227
228 if (!fd_found) {
229 goto error;
230 }
231
232 return 0;
233
234error:
235 return -1;
236}
237
5eb91c98
DG
238/*
239 * Remove a fd from the pollfd structure.
240 */
db14a5c9 241LTTNG_HIDDEN
5eb91c98
DG
242int compat_poll_del(struct lttng_poll_event *events, int fd)
243{
d21b0d71
DG
244 int new_size, i, count = 0, ret;
245 struct compat_poll_event_array *current;
5eb91c98 246
d21b0d71 247 if (events == NULL || events->current.events == NULL || fd < 0) {
5eb91c98
DG
248 ERR("Wrong arguments for poll del");
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 }
dbe23f45
MD
263 /* No fd duplicate should be ever added into array. */
264 assert(current->nb_fd - 1 == count);
265 current->nb_fd = count;
266
267 /* Resize array if needed. */
268 new_size = 1U << utils_get_count_order_u32(current->nb_fd);
269 if (new_size != current->alloc_size && new_size >= current->init_size) {
270 ret = resize_poll_event(current, new_size);
271 if (ret < 0) {
272 goto error;
273 }
274 }
5eb91c98 275
d21b0d71 276 events->need_update = 1;
5eb91c98
DG
277
278 return 0;
279
280error:
281 return -1;
282}
283
284/*
285 * Wait on poll() with timeout. Blocking call.
286 */
db14a5c9 287LTTNG_HIDDEN
5eb91c98
DG
288int compat_poll_wait(struct lttng_poll_event *events, int timeout)
289{
290 int ret;
291
d21b0d71 292 if (events == NULL || events->current.events == NULL) {
5eb91c98
DG
293 ERR("poll wait arguments error");
294 goto error;
295 }
dbe23f45 296 assert(events->current.nb_fd >= 0);
5eb91c98 297
d21b0d71
DG
298 if (events->current.nb_fd == 0) {
299 /* Return an invalid error to be consistent with epoll. */
300 errno = EINVAL;
dbe23f45 301 events->wait.nb_fd = 0;
d21b0d71
DG
302 goto error;
303 }
304
305 if (events->need_update) {
306 ret = update_current_events(events);
307 if (ret < 0) {
308 errno = ENOMEM;
309 goto error;
310 }
311 }
312
a9b0dbc2
JG
313 do {
314 ret = poll(events->wait.events, events->wait.nb_fd, timeout);
315 } while (ret == -1 && errno == EINTR);
5eb91c98
DG
316 if (ret < 0) {
317 /* At this point, every error is fatal */
6f04ed72 318 PERROR("poll wait");
5eb91c98
DG
319 goto error;
320 }
321
9ddba525
DG
322 /*
323 * poll() should always iterate on all FDs since we handle the pollset in
324 * user space and after poll returns, we have to try every fd for a match.
325 */
d21b0d71 326 return events->wait.nb_fd;
5eb91c98
DG
327
328error:
329 return -1;
330}
331
332/*
333 * Setup poll set maximum size.
334 */
db14a5c9 335LTTNG_HIDDEN
dbe23f45 336int compat_poll_set_max_size(void)
5eb91c98 337{
dbe23f45 338 int ret, retval = 0;
5eb91c98
DG
339 struct rlimit lim;
340
5eb91c98
DG
341 ret = getrlimit(RLIMIT_NOFILE, &lim);
342 if (ret < 0) {
6f04ed72 343 PERROR("getrlimit poll RLIMIT_NOFILE");
dbe23f45
MD
344 retval = -1;
345 goto end;
5eb91c98
DG
346 }
347
348 poll_max_size = lim.rlim_cur;
dbe23f45 349end:
d21b0d71 350 if (poll_max_size == 0) {
990570ed 351 poll_max_size = DEFAULT_POLL_SIZE;
5eb91c98 352 }
5eb91c98 353 DBG("poll set max size set to %u", poll_max_size);
dbe23f45 354 return retval;
5eb91c98 355}
This page took 0.081829 seconds and 5 git commands to generate.