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; | |
b85e60c1 | 83 | if (current->alloc_size != wait->alloc_size) { |
d21b0d71 DG |
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 | ||
b85e60c1 | 92 | /* Update is done. */ |
d21b0d71 | 93 | events->need_update = 0; |
d21b0d71 DG |
94 | |
95 | return 0; | |
96 | ||
97 | error: | |
98 | return -1; | |
99 | } | |
100 | ||
5eb91c98 DG |
101 | /* |
102 | * Create pollfd data structure. | |
103 | */ | |
104 | int compat_poll_create(struct lttng_poll_event *events, int size) | |
105 | { | |
d21b0d71 DG |
106 | struct compat_poll_event_array *current, *wait; |
107 | ||
5eb91c98 DG |
108 | if (events == NULL || size <= 0) { |
109 | ERR("Wrong arguments for poll create"); | |
110 | goto error; | |
111 | } | |
112 | ||
b85e60c1 MD |
113 | if (!poll_max_size) { |
114 | ERR("poll_max_size not initialized yet"); | |
115 | goto error; | |
116 | } | |
117 | ||
5eb91c98 DG |
118 | /* Don't bust the limit here */ |
119 | if (size > poll_max_size) { | |
120 | size = poll_max_size; | |
121 | } | |
122 | ||
d21b0d71 DG |
123 | /* Reset everything before begining the allocation. */ |
124 | memset(events, 0, sizeof(struct lttng_poll_event)); | |
125 | ||
d21b0d71 DG |
126 | current = &events->current; |
127 | wait = &events->wait; | |
128 | ||
5eb91c98 | 129 | /* This *must* be freed by using lttng_poll_free() */ |
d21b0d71 DG |
130 | wait->events = zmalloc(size * sizeof(struct pollfd)); |
131 | if (wait->events == NULL) { | |
ba7f0ae5 | 132 | perror("zmalloc struct pollfd"); |
5eb91c98 DG |
133 | goto error; |
134 | } | |
135 | ||
d21b0d71 DG |
136 | wait->alloc_size = wait->init_size = size; |
137 | ||
138 | current->events = zmalloc(size * sizeof(struct pollfd)); | |
139 | if (current->events == NULL) { | |
140 | perror("zmalloc struct current pollfd"); | |
141 | goto error; | |
142 | } | |
143 | ||
144 | current->alloc_size = current->init_size = size; | |
5eb91c98 DG |
145 | |
146 | return 0; | |
147 | ||
148 | error: | |
149 | return -1; | |
150 | } | |
151 | ||
152 | /* | |
153 | * Add fd to pollfd data structure with requested events. | |
154 | */ | |
155 | int compat_poll_add(struct lttng_poll_event *events, int fd, | |
156 | uint32_t req_events) | |
157 | { | |
d21b0d71 DG |
158 | int new_size, ret, i; |
159 | struct compat_poll_event_array *current; | |
5eb91c98 | 160 | |
d21b0d71 | 161 | if (events == NULL || events->current.events == NULL || fd < 0) { |
5eb91c98 DG |
162 | ERR("Bad compat poll add arguments"); |
163 | goto error; | |
164 | } | |
165 | ||
d21b0d71 DG |
166 | current = &events->current; |
167 | ||
168 | /* Check if fd we are trying to add is already there. */ | |
169 | for (i = 0; i < current->nb_fd; i++) { | |
d21b0d71 DG |
170 | if (current->events[i].fd == fd) { |
171 | errno = EEXIST; | |
5eb91c98 DG |
172 | goto error; |
173 | } | |
5eb91c98 DG |
174 | } |
175 | ||
b85e60c1 MD |
176 | /* Resize array if needed. */ |
177 | new_size = 1U << utils_get_count_order_u32(current->nb_fd + 1); | |
178 | if (new_size != current->alloc_size && new_size >= current->init_size) { | |
d21b0d71 DG |
179 | ret = resize_poll_event(current, new_size); |
180 | if (ret < 0) { | |
181 | goto error; | |
182 | } | |
d21b0d71 | 183 | } |
5eb91c98 | 184 | |
d21b0d71 DG |
185 | current->events[current->nb_fd].fd = fd; |
186 | current->events[current->nb_fd].events = req_events; | |
187 | current->nb_fd++; | |
188 | events->need_update = 1; | |
189 | ||
190 | DBG("fd %d of %d added to pollfd", fd, current->nb_fd); | |
5eb91c98 DG |
191 | |
192 | return 0; | |
193 | ||
194 | error: | |
195 | return -1; | |
196 | } | |
197 | ||
198 | /* | |
199 | * Remove a fd from the pollfd structure. | |
200 | */ | |
201 | int compat_poll_del(struct lttng_poll_event *events, int fd) | |
202 | { | |
d21b0d71 DG |
203 | int new_size, i, count = 0, ret; |
204 | struct compat_poll_event_array *current; | |
5eb91c98 | 205 | |
d21b0d71 | 206 | if (events == NULL || events->current.events == NULL || fd < 0) { |
5eb91c98 DG |
207 | ERR("Wrong arguments for poll del"); |
208 | goto error; | |
209 | } | |
210 | ||
d21b0d71 DG |
211 | /* Ease our life a bit. */ |
212 | current = &events->current; | |
5eb91c98 | 213 | |
d21b0d71 | 214 | for (i = 0; i < current->nb_fd; i++) { |
5eb91c98 | 215 | /* Don't put back the fd we want to delete */ |
d21b0d71 DG |
216 | if (current->events[i].fd != fd) { |
217 | current->events[count].fd = current->events[i].fd; | |
218 | current->events[count].events = current->events[i].events; | |
5eb91c98 DG |
219 | count++; |
220 | } | |
221 | } | |
b85e60c1 MD |
222 | /* No fd duplicate should be ever added into array. */ |
223 | assert(current->nb_fd - 1 == count); | |
224 | current->nb_fd = count; | |
225 | ||
226 | /* Resize array if needed. */ | |
227 | new_size = 1U << utils_get_count_order_u32(current->nb_fd); | |
228 | if (new_size != current->alloc_size && new_size >= current->init_size) { | |
229 | ret = resize_poll_event(current, new_size); | |
230 | if (ret < 0) { | |
231 | goto error; | |
232 | } | |
233 | } | |
5eb91c98 | 234 | |
d21b0d71 | 235 | events->need_update = 1; |
5eb91c98 DG |
236 | |
237 | return 0; | |
238 | ||
239 | error: | |
240 | return -1; | |
241 | } | |
242 | ||
243 | /* | |
244 | * Wait on poll() with timeout. Blocking call. | |
245 | */ | |
246 | int compat_poll_wait(struct lttng_poll_event *events, int timeout) | |
247 | { | |
248 | int ret; | |
249 | ||
d21b0d71 | 250 | if (events == NULL || events->current.events == NULL) { |
5eb91c98 DG |
251 | ERR("poll wait arguments error"); |
252 | goto error; | |
253 | } | |
b85e60c1 | 254 | assert(events->current.nb_fd >= 0); |
5eb91c98 | 255 | |
d21b0d71 DG |
256 | if (events->current.nb_fd == 0) { |
257 | /* Return an invalid error to be consistent with epoll. */ | |
258 | errno = EINVAL; | |
b85e60c1 | 259 | events->wait.nb_fd = 0; |
d21b0d71 DG |
260 | goto error; |
261 | } | |
262 | ||
263 | if (events->need_update) { | |
264 | ret = update_current_events(events); | |
265 | if (ret < 0) { | |
266 | errno = ENOMEM; | |
267 | goto error; | |
268 | } | |
269 | } | |
270 | ||
271 | ret = poll(events->wait.events, events->wait.nb_fd, timeout); | |
5eb91c98 DG |
272 | if (ret < 0) { |
273 | /* At this point, every error is fatal */ | |
274 | perror("poll wait"); | |
275 | goto error; | |
276 | } | |
277 | ||
9ddba525 DG |
278 | /* |
279 | * poll() should always iterate on all FDs since we handle the pollset in | |
280 | * user space and after poll returns, we have to try every fd for a match. | |
281 | */ | |
d21b0d71 | 282 | return events->wait.nb_fd; |
5eb91c98 DG |
283 | |
284 | error: | |
285 | return -1; | |
286 | } | |
287 | ||
288 | /* | |
289 | * Setup poll set maximum size. | |
290 | */ | |
b85e60c1 | 291 | int compat_poll_set_max_size(void) |
5eb91c98 | 292 | { |
b85e60c1 | 293 | int ret, retval = 0; |
5eb91c98 DG |
294 | struct rlimit lim; |
295 | ||
5eb91c98 DG |
296 | ret = getrlimit(RLIMIT_NOFILE, &lim); |
297 | if (ret < 0) { | |
298 | perror("getrlimit poll RLIMIT_NOFILE"); | |
b85e60c1 MD |
299 | retval = -1; |
300 | goto end; | |
5eb91c98 DG |
301 | } |
302 | ||
303 | poll_max_size = lim.rlim_cur; | |
b85e60c1 | 304 | end: |
d21b0d71 | 305 | if (poll_max_size == 0) { |
990570ed | 306 | poll_max_size = DEFAULT_POLL_SIZE; |
5eb91c98 | 307 | } |
5eb91c98 | 308 | DBG("poll set max size set to %u", poll_max_size); |
b85e60c1 | 309 | return retval; |
5eb91c98 | 310 | } |