Fix: poll compat layer reallocation new size
[lttng-tools.git] / src / common / compat / compat-poll.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
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 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <sys/resource.h>
22 #include <sys/time.h>
23
24 #include <common/defaults.h>
25 #include <common/error.h>
26 #include <common/macros.h>
27 #include <common/utils.h>
28
29 #include "poll.h"
30
31 unsigned int poll_max_size;
32
33 /*
34 * Resize the epoll events structure of the new size.
35 *
36 * Return 0 on success or else -1 with the current events pointer untouched.
37 */
38 static int resize_poll_event(struct compat_poll_event_array *array,
39 uint32_t new_size)
40 {
41 struct pollfd *ptr;
42
43 assert(array);
44
45 /* Refuse to resize the array more than the max size. */
46 if (new_size > poll_max_size) {
47 goto error;
48 }
49
50 ptr = realloc(array->events, new_size * sizeof(*ptr));
51 if (ptr == NULL) {
52 PERROR("realloc epoll add");
53 goto error;
54 }
55 array->events = ptr;
56 array->alloc_size = new_size;
57
58 return 0;
59
60 error:
61 return -1;
62 }
63
64 /*
65 * Update events with the current events object.
66 */
67 static int update_current_events(struct lttng_poll_event *events)
68 {
69 int ret;
70 struct compat_poll_event_array *current, *wait;
71
72 assert(events);
73
74 current = &events->current;
75 wait = &events->wait;
76
77 wait->nb_fd = current->nb_fd;
78 if (events->need_realloc) {
79 ret = resize_poll_event(wait, current->alloc_size);
80 if (ret < 0) {
81 goto error;
82 }
83 }
84 memcpy(wait->events, current->events,
85 current->nb_fd * sizeof(*current->events));
86
87 /* Update is done and realloc as well. */
88 events->need_update = 0;
89 events->need_realloc = 0;
90
91 return 0;
92
93 error:
94 return -1;
95 }
96
97 /*
98 * Create pollfd data structure.
99 */
100 int compat_poll_create(struct lttng_poll_event *events, int size)
101 {
102 struct compat_poll_event_array *current, *wait;
103
104 if (events == NULL || size <= 0) {
105 ERR("Wrong arguments for poll create");
106 goto error;
107 }
108
109 /* Don't bust the limit here */
110 if (size > poll_max_size) {
111 size = poll_max_size;
112 }
113
114 /* Reset everything before begining the allocation. */
115 memset(events, 0, sizeof(struct lttng_poll_event));
116
117 /* Ease our life a bit. */
118 current = &events->current;
119 wait = &events->wait;
120
121 /* This *must* be freed by using lttng_poll_free() */
122 wait->events = zmalloc(size * sizeof(struct pollfd));
123 if (wait->events == NULL) {
124 perror("zmalloc struct pollfd");
125 goto error;
126 }
127
128 wait->alloc_size = wait->init_size = size;
129
130 current->events = zmalloc(size * sizeof(struct pollfd));
131 if (current->events == NULL) {
132 perror("zmalloc struct current pollfd");
133 goto error;
134 }
135
136 current->alloc_size = current->init_size = size;
137
138 return 0;
139
140 error:
141 return -1;
142 }
143
144 /*
145 * Add fd to pollfd data structure with requested events.
146 */
147 int compat_poll_add(struct lttng_poll_event *events, int fd,
148 uint32_t req_events)
149 {
150 int new_size, ret, i;
151 struct compat_poll_event_array *current;
152
153 if (events == NULL || events->current.events == NULL || fd < 0) {
154 ERR("Bad compat poll add arguments");
155 goto error;
156 }
157
158 /* Ease our life a bit. */
159 current = &events->current;
160
161 /* Check if fd we are trying to add is already there. */
162 for (i = 0; i < current->nb_fd; i++) {
163 /* Don't put back the fd we want to delete */
164 if (current->events[i].fd == fd) {
165 errno = EEXIST;
166 goto error;
167 }
168 }
169
170 /* Check for a needed resize of the array. */
171 if (current->nb_fd > current->alloc_size) {
172 /* Expand it by a power of two of the current size. */
173 new_size = max_t(int,
174 1U << utils_get_count_order_u32(current->nb_fd),
175 current->alloc_size << 1UL);
176 ret = resize_poll_event(current, new_size);
177 if (ret < 0) {
178 goto error;
179 }
180 events->need_realloc = 1;
181 }
182
183 current->events[current->nb_fd].fd = fd;
184 current->events[current->nb_fd].events = req_events;
185 current->nb_fd++;
186 events->need_update = 1;
187
188 DBG("fd %d of %d added to pollfd", fd, current->nb_fd);
189
190 return 0;
191
192 error:
193 return -1;
194 }
195
196 /*
197 * Remove a fd from the pollfd structure.
198 */
199 int compat_poll_del(struct lttng_poll_event *events, int fd)
200 {
201 int new_size, i, count = 0, ret;
202 struct compat_poll_event_array *current;
203
204 if (events == NULL || events->current.events == NULL || fd < 0) {
205 ERR("Wrong arguments for poll del");
206 goto error;
207 }
208
209 /* Ease our life a bit. */
210 current = &events->current;
211
212 /* Check if we need to shrink it down. */
213 if ((current->nb_fd << 1UL) <= current->alloc_size &&
214 current->nb_fd >= current->init_size) {
215 /*
216 * Shrink if nb_fd multiplied by two is <= than the actual size and we
217 * are above the initial size.
218 */
219 new_size = max_t(int,
220 utils_get_count_order_u32(current->nb_fd) >> 1U,
221 current->alloc_size >> 1U);
222 ret = resize_poll_event(current, new_size);
223 if (ret < 0) {
224 goto error;
225 }
226 events->need_realloc = 1;
227 }
228
229 for (i = 0; i < current->nb_fd; i++) {
230 /* Don't put back the fd we want to delete */
231 if (current->events[i].fd != fd) {
232 current->events[count].fd = current->events[i].fd;
233 current->events[count].events = current->events[i].events;
234 count++;
235 }
236 }
237
238 current->nb_fd--;
239 events->need_update = 1;
240
241 return 0;
242
243 error:
244 return -1;
245 }
246
247 /*
248 * Wait on poll() with timeout. Blocking call.
249 */
250 int compat_poll_wait(struct lttng_poll_event *events, int timeout)
251 {
252 int ret;
253
254 if (events == NULL || events->current.events == NULL) {
255 ERR("poll wait arguments error");
256 goto error;
257 }
258
259 if (events->current.nb_fd == 0) {
260 /* Return an invalid error to be consistent with epoll. */
261 errno = EINVAL;
262 goto error;
263 }
264
265 if (events->need_update) {
266 ret = update_current_events(events);
267 if (ret < 0) {
268 errno = ENOMEM;
269 goto error;
270 }
271 }
272
273 ret = poll(events->wait.events, events->wait.nb_fd, timeout);
274 if (ret < 0) {
275 /* At this point, every error is fatal */
276 perror("poll wait");
277 goto error;
278 }
279
280 /*
281 * poll() should always iterate on all FDs since we handle the pollset in
282 * user space and after poll returns, we have to try every fd for a match.
283 */
284 return events->wait.nb_fd;
285
286 error:
287 return -1;
288 }
289
290 /*
291 * Setup poll set maximum size.
292 */
293 void compat_poll_set_max_size(void)
294 {
295 int ret;
296 struct rlimit lim;
297
298 /* Default value */
299 poll_max_size = DEFAULT_POLL_SIZE;
300
301 ret = getrlimit(RLIMIT_NOFILE, &lim);
302 if (ret < 0) {
303 perror("getrlimit poll RLIMIT_NOFILE");
304 return;
305 }
306
307 poll_max_size = lim.rlim_cur;
308 if (poll_max_size == 0) {
309 /* Extra precaution */
310 poll_max_size = DEFAULT_POLL_SIZE;
311 }
312
313 DBG("poll set max size set to %u", poll_max_size);
314 }
This page took 0.039822 seconds and 5 git commands to generate.