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.
22 #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 (events
->need_realloc
) {
85 ret
= resize_poll_event(wait
, current
->alloc_size
);
90 memcpy(wait
->events
, current
->events
,
91 current
->nb_fd
* sizeof(*current
->events
));
93 /* Update is done and realloc as well. */
94 events
->need_update
= 0;
95 events
->need_realloc
= 0;
104 * Create pollfd data structure.
106 int compat_poll_create(struct lttng_poll_event
*events
, int size
)
108 struct compat_poll_event_array
*current
, *wait
;
110 if (events
== NULL
|| size
<= 0) {
111 ERR("Wrong arguments for poll create");
115 /* Don't bust the limit here */
116 if (size
> poll_max_size
) {
117 size
= poll_max_size
;
120 /* Reset everything before begining the allocation. */
121 memset(events
, 0, sizeof(struct lttng_poll_event
));
123 /* Ease our life a bit. */
124 current
= &events
->current
;
125 wait
= &events
->wait
;
127 /* This *must* be freed by using lttng_poll_free() */
128 wait
->events
= zmalloc(size
* sizeof(struct pollfd
));
129 if (wait
->events
== NULL
) {
130 PERROR("zmalloc struct pollfd");
134 wait
->alloc_size
= wait
->init_size
= size
;
136 current
->events
= zmalloc(size
* sizeof(struct pollfd
));
137 if (current
->events
== NULL
) {
138 PERROR("zmalloc struct current pollfd");
142 current
->alloc_size
= current
->init_size
= size
;
151 * Add fd to pollfd data structure with requested events.
153 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
156 int new_size
, ret
, i
;
157 struct compat_poll_event_array
*current
;
159 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
160 ERR("Bad compat poll add arguments");
164 /* Ease our life a bit. */
165 current
= &events
->current
;
167 /* Check if fd we are trying to add is already there. */
168 for (i
= 0; i
< current
->nb_fd
; i
++) {
169 /* Don't put back the fd we want to delete */
170 if (current
->events
[i
].fd
== fd
) {
176 /* Check for a needed resize of the array. */
177 if (current
->nb_fd
> current
->alloc_size
) {
178 /* Expand it by a power of two of the current size. */
179 new_size
= max_t(int,
180 1U << utils_get_count_order_u32(current
->nb_fd
),
181 current
->alloc_size
<< 1UL);
182 ret
= resize_poll_event(current
, new_size
);
186 events
->need_realloc
= 1;
189 current
->events
[current
->nb_fd
].fd
= fd
;
190 current
->events
[current
->nb_fd
].events
= req_events
;
192 events
->need_update
= 1;
194 DBG("fd %d of %d added to pollfd", fd
, current
->nb_fd
);
203 * Remove a fd from the pollfd structure.
205 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
207 int new_size
, i
, count
= 0, ret
;
208 struct compat_poll_event_array
*current
;
210 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
211 ERR("Wrong arguments for poll del");
215 /* Ease our life a bit. */
216 current
= &events
->current
;
218 /* Check if we need to shrink it down. */
219 if ((current
->nb_fd
<< 1UL) <= current
->alloc_size
&&
220 current
->nb_fd
>= current
->init_size
) {
222 * Shrink if nb_fd multiplied by two is <= than the actual size and we
223 * are above the initial size.
225 new_size
= max_t(int,
226 utils_get_count_order_u32(current
->nb_fd
) >> 1U,
227 current
->alloc_size
>> 1U);
228 ret
= resize_poll_event(current
, new_size
);
232 events
->need_realloc
= 1;
235 for (i
= 0; i
< current
->nb_fd
; i
++) {
236 /* Don't put back the fd we want to delete */
237 if (current
->events
[i
].fd
!= fd
) {
238 current
->events
[count
].fd
= current
->events
[i
].fd
;
239 current
->events
[count
].events
= current
->events
[i
].events
;
245 events
->need_update
= 1;
254 * Wait on poll() with timeout. Blocking call.
256 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
)
260 if (events
== NULL
|| events
->current
.events
== NULL
) {
261 ERR("poll wait arguments error");
265 if (events
->current
.nb_fd
== 0) {
266 /* Return an invalid error to be consistent with epoll. */
271 if (events
->need_update
) {
272 ret
= update_current_events(events
);
279 ret
= poll(events
->wait
.events
, events
->wait
.nb_fd
, timeout
);
281 /* At this point, every error is fatal */
287 * poll() should always iterate on all FDs since we handle the pollset in
288 * user space and after poll returns, we have to try every fd for a match.
290 return events
->wait
.nb_fd
;
297 * Setup poll set maximum size.
299 void compat_poll_set_max_size(void)
305 poll_max_size
= DEFAULT_POLL_SIZE
;
307 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
309 PERROR("getrlimit poll RLIMIT_NOFILE");
313 poll_max_size
= lim
.rlim_cur
;
314 if (poll_max_size
== 0) {
315 /* Extra precaution */
316 poll_max_size
= DEFAULT_POLL_SIZE
;
319 DBG("poll set max size set to %u", poll_max_size
);
This page took 0.070107 seconds and 4 git commands to generate.