Add SDT userspace probe location
[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 */
105int compat_poll_create(struct lttng_poll_event *events, int size)
106{
d21b0d71
DG
107 struct compat_poll_event_array *current, *wait;
108
5eb91c98
DG
109 if (events == NULL || size <= 0) {
110 ERR("Wrong arguments for poll create");
111 goto error;
112 }
113
dbe23f45 114 if (!poll_max_size) {
c607fe03
MJ
115 if (lttng_poll_set_max_size()) {
116 goto error;
117 }
dbe23f45
MD
118 }
119
5eb91c98
DG
120 /* Don't bust the limit here */
121 if (size > poll_max_size) {
122 size = poll_max_size;
123 }
124
d21b0d71
DG
125 /* Reset everything before begining the allocation. */
126 memset(events, 0, sizeof(struct lttng_poll_event));
127
d21b0d71
DG
128 current = &events->current;
129 wait = &events->wait;
130
5eb91c98 131 /* This *must* be freed by using lttng_poll_free() */
d21b0d71
DG
132 wait->events = zmalloc(size * sizeof(struct pollfd));
133 if (wait->events == NULL) {
6f04ed72 134 PERROR("zmalloc struct pollfd");
5eb91c98
DG
135 goto error;
136 }
137
d21b0d71
DG
138 wait->alloc_size = wait->init_size = size;
139
140 current->events = zmalloc(size * sizeof(struct pollfd));
141 if (current->events == NULL) {
6f04ed72 142 PERROR("zmalloc struct current pollfd");
d21b0d71
DG
143 goto error;
144 }
145
146 current->alloc_size = current->init_size = size;
5eb91c98
DG
147
148 return 0;
149
150error:
151 return -1;
152}
153
154/*
155 * Add fd to pollfd data structure with requested events.
156 */
157int compat_poll_add(struct lttng_poll_event *events, int fd,
158 uint32_t req_events)
159{
d21b0d71
DG
160 int new_size, ret, i;
161 struct compat_poll_event_array *current;
5eb91c98 162
d21b0d71 163 if (events == NULL || events->current.events == NULL || fd < 0) {
5eb91c98
DG
164 ERR("Bad compat poll add arguments");
165 goto error;
166 }
167
d21b0d71
DG
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++) {
d21b0d71
DG
172 if (current->events[i].fd == fd) {
173 errno = EEXIST;
5eb91c98
DG
174 goto error;
175 }
5eb91c98
DG
176 }
177
dbe23f45
MD
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) {
d21b0d71
DG
181 ret = resize_poll_event(current, new_size);
182 if (ret < 0) {
183 goto error;
184 }
d21b0d71 185 }
5eb91c98 186
d21b0d71
DG
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);
5eb91c98
DG
193
194 return 0;
195
196error:
197 return -1;
198}
199
f057dfc3
JG
200/*
201 * Modify an fd's events..
202 */
203int compat_poll_mod(struct lttng_poll_event *events, int fd,
204 uint32_t req_events)
205{
8a282751 206 int i;
f057dfc3
JG
207 bool fd_found = false;
208 struct compat_poll_event_array *current;
209
210 if (events == NULL || events->current.events == NULL || fd < 0) {
211 ERR("Bad compat poll mod arguments");
212 goto error;
213 }
214
215 current = &events->current;
216
217 for (i = 0; i < current->nb_fd; i++) {
218 if (current->events[i].fd == fd) {
219 fd_found = true;
220 current->events[i].events = req_events;
221 events->need_update = 1;
222 break;
223 }
224 }
225
226 if (!fd_found) {
227 goto error;
228 }
229
230 return 0;
231
232error:
233 return -1;
234}
235
5eb91c98
DG
236/*
237 * Remove a fd from the pollfd structure.
238 */
239int compat_poll_del(struct lttng_poll_event *events, int fd)
240{
d21b0d71
DG
241 int new_size, i, count = 0, ret;
242 struct compat_poll_event_array *current;
5eb91c98 243
d21b0d71 244 if (events == NULL || events->current.events == NULL || fd < 0) {
5eb91c98
DG
245 ERR("Wrong arguments for poll del");
246 goto error;
247 }
248
d21b0d71
DG
249 /* Ease our life a bit. */
250 current = &events->current;
5eb91c98 251
d21b0d71 252 for (i = 0; i < current->nb_fd; i++) {
5eb91c98 253 /* Don't put back the fd we want to delete */
d21b0d71
DG
254 if (current->events[i].fd != fd) {
255 current->events[count].fd = current->events[i].fd;
256 current->events[count].events = current->events[i].events;
5eb91c98
DG
257 count++;
258 }
259 }
dbe23f45
MD
260 /* No fd duplicate should be ever added into array. */
261 assert(current->nb_fd - 1 == count);
262 current->nb_fd = count;
263
264 /* Resize array if needed. */
265 new_size = 1U << utils_get_count_order_u32(current->nb_fd);
266 if (new_size != current->alloc_size && new_size >= current->init_size) {
267 ret = resize_poll_event(current, new_size);
268 if (ret < 0) {
269 goto error;
270 }
271 }
5eb91c98 272
d21b0d71 273 events->need_update = 1;
5eb91c98
DG
274
275 return 0;
276
277error:
278 return -1;
279}
280
281/*
282 * Wait on poll() with timeout. Blocking call.
283 */
284int compat_poll_wait(struct lttng_poll_event *events, int timeout)
285{
286 int ret;
287
d21b0d71 288 if (events == NULL || events->current.events == NULL) {
5eb91c98
DG
289 ERR("poll wait arguments error");
290 goto error;
291 }
dbe23f45 292 assert(events->current.nb_fd >= 0);
5eb91c98 293
d21b0d71
DG
294 if (events->current.nb_fd == 0) {
295 /* Return an invalid error to be consistent with epoll. */
296 errno = EINVAL;
dbe23f45 297 events->wait.nb_fd = 0;
d21b0d71
DG
298 goto error;
299 }
300
301 if (events->need_update) {
302 ret = update_current_events(events);
303 if (ret < 0) {
304 errno = ENOMEM;
305 goto error;
306 }
307 }
308
a9b0dbc2
JG
309 do {
310 ret = poll(events->wait.events, events->wait.nb_fd, timeout);
311 } while (ret == -1 && errno == EINTR);
5eb91c98
DG
312 if (ret < 0) {
313 /* At this point, every error is fatal */
6f04ed72 314 PERROR("poll wait");
5eb91c98
DG
315 goto error;
316 }
317
9ddba525
DG
318 /*
319 * poll() should always iterate on all FDs since we handle the pollset in
320 * user space and after poll returns, we have to try every fd for a match.
321 */
d21b0d71 322 return events->wait.nb_fd;
5eb91c98
DG
323
324error:
325 return -1;
326}
327
328/*
329 * Setup poll set maximum size.
330 */
dbe23f45 331int compat_poll_set_max_size(void)
5eb91c98 332{
dbe23f45 333 int ret, retval = 0;
5eb91c98
DG
334 struct rlimit lim;
335
5eb91c98
DG
336 ret = getrlimit(RLIMIT_NOFILE, &lim);
337 if (ret < 0) {
6f04ed72 338 PERROR("getrlimit poll RLIMIT_NOFILE");
dbe23f45
MD
339 retval = -1;
340 goto end;
5eb91c98
DG
341 }
342
343 poll_max_size = lim.rlim_cur;
dbe23f45 344end:
d21b0d71 345 if (poll_max_size == 0) {
990570ed 346 poll_max_size = DEFAULT_POLL_SIZE;
5eb91c98 347 }
5eb91c98 348 DBG("poll set max size set to %u", poll_max_size);
dbe23f45 349 return retval;
5eb91c98 350}
This page took 0.078527 seconds and 5 git commands to generate.