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