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