Add debugging output to ht-cleanup thread
[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
5bbf8b1b 18#define _GNU_SOURCE
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>
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;
84 if (events->need_realloc) {
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
93 /* Update is done and realloc as well. */
94 events->need_update = 0;
95 events->need_realloc = 0;
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
115 /* Don't bust the limit here */
116 if (size > poll_max_size) {
117 size = poll_max_size;
118 }
119
d21b0d71
DG
120 /* Reset everything before begining the allocation. */
121 memset(events, 0, sizeof(struct lttng_poll_event));
122
123 /* Ease our life a bit. */
124 current = &events->current;
125 wait = &events->wait;
126
5eb91c98 127 /* This *must* be freed by using lttng_poll_free() */
d21b0d71
DG
128 wait->events = zmalloc(size * sizeof(struct pollfd));
129 if (wait->events == NULL) {
6f04ed72 130 PERROR("zmalloc struct pollfd");
5eb91c98
DG
131 goto error;
132 }
133
d21b0d71
DG
134 wait->alloc_size = wait->init_size = size;
135
136 current->events = zmalloc(size * sizeof(struct pollfd));
137 if (current->events == NULL) {
6f04ed72 138 PERROR("zmalloc struct current pollfd");
d21b0d71
DG
139 goto error;
140 }
141
142 current->alloc_size = current->init_size = size;
5eb91c98
DG
143
144 return 0;
145
146error:
147 return -1;
148}
149
150/*
151 * Add fd to pollfd data structure with requested events.
152 */
153int compat_poll_add(struct lttng_poll_event *events, int fd,
154 uint32_t req_events)
155{
d21b0d71
DG
156 int new_size, ret, i;
157 struct compat_poll_event_array *current;
5eb91c98 158
d21b0d71 159 if (events == NULL || events->current.events == NULL || fd < 0) {
5eb91c98
DG
160 ERR("Bad compat poll add arguments");
161 goto error;
162 }
163
d21b0d71
DG
164 /* Ease our life a bit. */
165 current = &events->current;
166
167 /* Check if fd we are trying to add is already there. */
168 for (i = 0; i < current->nb_fd; i++) {
169 /* Don't put back the fd we want to delete */
170 if (current->events[i].fd == fd) {
171 errno = EEXIST;
5eb91c98
DG
172 goto error;
173 }
5eb91c98
DG
174 }
175
d21b0d71
DG
176 /* Check for a needed resize of the array. */
177 if (current->nb_fd > current->alloc_size) {
178 /* Expand it by a power of two of the current size. */
cfa9a5a2
DG
179 new_size = max_t(int,
180 1U << utils_get_count_order_u32(current->nb_fd),
181 current->alloc_size << 1UL);
d21b0d71
DG
182 ret = resize_poll_event(current, new_size);
183 if (ret < 0) {
184 goto error;
185 }
186 events->need_realloc = 1;
187 }
5eb91c98 188
d21b0d71
DG
189 current->events[current->nb_fd].fd = fd;
190 current->events[current->nb_fd].events = req_events;
191 current->nb_fd++;
192 events->need_update = 1;
193
194 DBG("fd %d of %d added to pollfd", fd, current->nb_fd);
5eb91c98
DG
195
196 return 0;
197
198error:
199 return -1;
200}
201
202/*
203 * Remove a fd from the pollfd structure.
204 */
205int compat_poll_del(struct lttng_poll_event *events, int fd)
206{
d21b0d71
DG
207 int new_size, i, count = 0, ret;
208 struct compat_poll_event_array *current;
5eb91c98 209
d21b0d71 210 if (events == NULL || events->current.events == NULL || fd < 0) {
5eb91c98
DG
211 ERR("Wrong arguments for poll del");
212 goto error;
213 }
214
d21b0d71
DG
215 /* Ease our life a bit. */
216 current = &events->current;
5eb91c98 217
d21b0d71
DG
218 /* Check if we need to shrink it down. */
219 if ((current->nb_fd << 1UL) <= current->alloc_size &&
220 current->nb_fd >= current->init_size) {
221 /*
222 * Shrink if nb_fd multiplied by two is <= than the actual size and we
223 * are above the initial size.
224 */
cfa9a5a2
DG
225 new_size = max_t(int,
226 utils_get_count_order_u32(current->nb_fd) >> 1U,
227 current->alloc_size >> 1U);
d21b0d71
DG
228 ret = resize_poll_event(current, new_size);
229 if (ret < 0) {
230 goto error;
231 }
232 events->need_realloc = 1;
5eb91c98
DG
233 }
234
d21b0d71 235 for (i = 0; i < current->nb_fd; i++) {
5eb91c98 236 /* Don't put back the fd we want to delete */
d21b0d71
DG
237 if (current->events[i].fd != fd) {
238 current->events[count].fd = current->events[i].fd;
239 current->events[count].events = current->events[i].events;
5eb91c98
DG
240 count++;
241 }
242 }
243
d21b0d71
DG
244 current->nb_fd--;
245 events->need_update = 1;
5eb91c98
DG
246
247 return 0;
248
249error:
250 return -1;
251}
252
253/*
254 * Wait on poll() with timeout. Blocking call.
255 */
256int compat_poll_wait(struct lttng_poll_event *events, int timeout)
257{
258 int ret;
259
d21b0d71 260 if (events == NULL || events->current.events == NULL) {
5eb91c98
DG
261 ERR("poll wait arguments error");
262 goto error;
263 }
264
d21b0d71
DG
265 if (events->current.nb_fd == 0) {
266 /* Return an invalid error to be consistent with epoll. */
267 errno = EINVAL;
268 goto error;
269 }
270
271 if (events->need_update) {
272 ret = update_current_events(events);
273 if (ret < 0) {
274 errno = ENOMEM;
275 goto error;
276 }
277 }
278
279 ret = poll(events->wait.events, events->wait.nb_fd, timeout);
5eb91c98
DG
280 if (ret < 0) {
281 /* At this point, every error is fatal */
6f04ed72 282 PERROR("poll wait");
5eb91c98
DG
283 goto error;
284 }
285
9ddba525
DG
286 /*
287 * poll() should always iterate on all FDs since we handle the pollset in
288 * user space and after poll returns, we have to try every fd for a match.
289 */
d21b0d71 290 return events->wait.nb_fd;
5eb91c98
DG
291
292error:
293 return -1;
294}
295
296/*
297 * Setup poll set maximum size.
298 */
299void compat_poll_set_max_size(void)
300{
301 int ret;
302 struct rlimit lim;
303
304 /* Default value */
990570ed 305 poll_max_size = DEFAULT_POLL_SIZE;
5eb91c98
DG
306
307 ret = getrlimit(RLIMIT_NOFILE, &lim);
308 if (ret < 0) {
6f04ed72 309 PERROR("getrlimit poll RLIMIT_NOFILE");
5eb91c98
DG
310 return;
311 }
312
313 poll_max_size = lim.rlim_cur;
d21b0d71 314 if (poll_max_size == 0) {
5eb91c98 315 /* Extra precaution */
990570ed 316 poll_max_size = DEFAULT_POLL_SIZE;
5eb91c98
DG
317 }
318
319 DBG("poll set max size set to %u", poll_max_size);
320}
This page took 0.081673 seconds and 4 git commands to generate.