2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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
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.
21 #include <sys/resource.h>
25 #include <common/defaults.h>
26 #include <common/error.h>
27 #include <common/macros.h>
28 #include <common/utils.h>
32 unsigned int poll_max_size
;
35 * Resize the epoll events structure of the new size.
37 * Return 0 on success or else -1 with the current events pointer untouched.
39 static int resize_poll_event(struct compat_poll_event_array
*array
,
46 /* Refuse to resize the array more than the max size. */
47 if (new_size
> poll_max_size
) {
51 ptr
= realloc(array
->events
, new_size
* sizeof(*ptr
));
53 PERROR("realloc epoll add");
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
));
62 array
->alloc_size
= new_size
;
71 * Update events with the current events object.
73 static int update_current_events(struct lttng_poll_event
*events
)
76 struct compat_poll_event_array
*current
, *wait
;
80 current
= &events
->current
;
83 wait
->nb_fd
= current
->nb_fd
;
84 if (current
->alloc_size
!= wait
->alloc_size
) {
85 ret
= resize_poll_event(wait
, current
->alloc_size
);
90 memcpy(wait
->events
, current
->events
,
91 current
->nb_fd
* sizeof(*current
->events
));
94 events
->need_update
= 0;
103 * Create pollfd data structure.
105 int compat_poll_create(struct lttng_poll_event
*events
, int size
)
107 struct compat_poll_event_array
*current
, *wait
;
109 if (events
== NULL
|| size
<= 0) {
110 ERR("Wrong arguments for poll create");
114 if (!poll_max_size
) {
115 if (lttng_poll_set_max_size()) {
120 /* Don't bust the limit here */
121 if (size
> poll_max_size
) {
122 size
= poll_max_size
;
125 /* Reset everything before begining the allocation. */
126 memset(events
, 0, sizeof(struct lttng_poll_event
));
128 current
= &events
->current
;
129 wait
= &events
->wait
;
131 /* This *must* be freed by using lttng_poll_free() */
132 wait
->events
= zmalloc(size
* sizeof(struct pollfd
));
133 if (wait
->events
== NULL
) {
134 PERROR("zmalloc struct pollfd");
138 wait
->alloc_size
= wait
->init_size
= size
;
140 current
->events
= zmalloc(size
* sizeof(struct pollfd
));
141 if (current
->events
== NULL
) {
142 PERROR("zmalloc struct current pollfd");
146 current
->alloc_size
= current
->init_size
= size
;
155 * Add fd to pollfd data structure with requested events.
157 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
160 int new_size
, ret
, i
;
161 struct compat_poll_event_array
*current
;
163 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
164 ERR("Bad compat poll add arguments");
168 current
= &events
->current
;
170 /* Check if fd we are trying to add is already there. */
171 for (i
= 0; i
< current
->nb_fd
; i
++) {
172 if (current
->events
[i
].fd
== fd
) {
178 /* Resize array if needed. */
179 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
+ 1);
180 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
) {
181 ret
= resize_poll_event(current
, new_size
);
187 current
->events
[current
->nb_fd
].fd
= fd
;
188 current
->events
[current
->nb_fd
].events
= req_events
;
190 events
->need_update
= 1;
192 DBG("fd %d of %d added to pollfd", fd
, current
->nb_fd
);
201 * Modify an fd's events..
203 int compat_poll_mod(struct lttng_poll_event
*events
, int fd
,
207 bool fd_found
= false;
208 struct compat_poll_event_array
*current
;
210 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
211 ERR("Bad compat poll mod arguments");
215 current
= &events
->current
;
217 for (i
= 0; i
< current
->nb_fd
; i
++) {
218 if (current
->events
[i
].fd
== fd
) {
220 current
->events
[i
].events
= req_events
;
221 events
->need_update
= 1;
237 * Remove a fd from the pollfd structure.
239 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
241 int new_size
, i
, count
= 0, ret
;
242 struct compat_poll_event_array
*current
;
244 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
245 ERR("Wrong arguments for poll del");
249 /* Ease our life a bit. */
250 current
= &events
->current
;
252 for (i
= 0; i
< current
->nb_fd
; i
++) {
253 /* Don't put back the fd we want to delete */
254 if (current
->events
[i
].fd
!= fd
) {
255 current
->events
[count
].fd
= current
->events
[i
].fd
;
256 current
->events
[count
].events
= current
->events
[i
].events
;
260 /* No fd duplicate should be ever added into array. */
261 assert(current
->nb_fd
- 1 == count
);
262 current
->nb_fd
= count
;
264 /* Resize array if needed. */
265 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
);
266 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
) {
267 ret
= resize_poll_event(current
, new_size
);
273 events
->need_update
= 1;
282 * Wait on poll() with timeout. Blocking call.
284 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
)
288 if (events
== NULL
|| events
->current
.events
== NULL
) {
289 ERR("poll wait arguments error");
292 assert(events
->current
.nb_fd
>= 0);
294 if (events
->current
.nb_fd
== 0) {
295 /* Return an invalid error to be consistent with epoll. */
297 events
->wait
.nb_fd
= 0;
301 if (events
->need_update
) {
302 ret
= update_current_events(events
);
310 ret
= poll(events
->wait
.events
, events
->wait
.nb_fd
, timeout
);
311 } while (ret
== -1 && errno
== EINTR
);
313 /* At this point, every error is fatal */
319 * poll() should always iterate on all FDs since we handle the pollset in
320 * user space and after poll returns, we have to try every fd for a match.
322 return events
->wait
.nb_fd
;
329 * Setup poll set maximum size.
331 int compat_poll_set_max_size(void)
336 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
338 PERROR("getrlimit poll RLIMIT_NOFILE");
343 poll_max_size
= lim
.rlim_cur
;
345 if (poll_max_size
== 0) {
346 poll_max_size
= DEFAULT_POLL_SIZE
;
348 DBG("poll set max size set to %u", poll_max_size
);
This page took 0.048599 seconds and 4 git commands to generate.