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>
24 #include <common/defaults.h>
25 #include <common/error.h>
26 #include <common/macros.h>
27 #include <common/utils.h>
31 unsigned int poll_max_size
;
34 * Resize the epoll events structure of the new size.
36 * Return 0 on success or else -1 with the current events pointer untouched.
38 static int resize_poll_event(struct compat_poll_event_array
*array
,
45 /* Refuse to resize the array more than the max size. */
46 if (new_size
> poll_max_size
) {
50 ptr
= realloc(array
->events
, new_size
* sizeof(*ptr
));
52 PERROR("realloc epoll add");
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
));
61 array
->alloc_size
= new_size
;
70 * Update events with the current events object.
72 static int update_current_events(struct lttng_poll_event
*events
)
75 struct compat_poll_event_array
*current
, *wait
;
79 current
= &events
->current
;
82 wait
->nb_fd
= current
->nb_fd
;
83 if (events
->need_realloc
) {
84 ret
= resize_poll_event(wait
, current
->alloc_size
);
89 memcpy(wait
->events
, current
->events
,
90 current
->nb_fd
* sizeof(*current
->events
));
92 /* Update is done and realloc as well. */
93 events
->need_update
= 0;
94 events
->need_realloc
= 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 /* Don't bust the limit here */
115 if (size
> poll_max_size
) {
116 size
= poll_max_size
;
119 /* Reset everything before begining the allocation. */
120 memset(events
, 0, sizeof(struct lttng_poll_event
));
122 /* Ease our life a bit. */
123 current
= &events
->current
;
124 wait
= &events
->wait
;
126 /* This *must* be freed by using lttng_poll_free() */
127 wait
->events
= zmalloc(size
* sizeof(struct pollfd
));
128 if (wait
->events
== NULL
) {
129 perror("zmalloc struct pollfd");
133 wait
->alloc_size
= wait
->init_size
= size
;
135 current
->events
= zmalloc(size
* sizeof(struct pollfd
));
136 if (current
->events
== NULL
) {
137 perror("zmalloc struct current pollfd");
141 current
->alloc_size
= current
->init_size
= size
;
150 * Add fd to pollfd data structure with requested events.
152 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
155 int new_size
, ret
, i
;
156 struct compat_poll_event_array
*current
;
158 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
159 ERR("Bad compat poll add arguments");
163 /* Ease our life a bit. */
164 current
= &events
->current
;
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
) {
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. */
178 new_size
= max_t(int,
179 1U << utils_get_count_order_u32(current
->nb_fd
),
180 current
->alloc_size
<< 1UL);
181 ret
= resize_poll_event(current
, new_size
);
185 events
->need_realloc
= 1;
188 current
->events
[current
->nb_fd
].fd
= fd
;
189 current
->events
[current
->nb_fd
].events
= req_events
;
191 events
->need_update
= 1;
193 DBG("fd %d of %d added to pollfd", fd
, current
->nb_fd
);
202 * Remove a fd from the pollfd structure.
204 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
206 int new_size
, i
, count
= 0, ret
;
207 struct compat_poll_event_array
*current
;
209 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
210 ERR("Wrong arguments for poll del");
214 /* Ease our life a bit. */
215 current
= &events
->current
;
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
) {
221 * Shrink if nb_fd multiplied by two is <= than the actual size and we
222 * are above the initial size.
224 new_size
= max_t(int,
225 utils_get_count_order_u32(current
->nb_fd
) >> 1U,
226 current
->alloc_size
>> 1U);
227 ret
= resize_poll_event(current
, new_size
);
231 events
->need_realloc
= 1;
234 for (i
= 0; i
< current
->nb_fd
; i
++) {
235 /* Don't put back the fd we want to delete */
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
;
244 events
->need_update
= 1;
253 * Wait on poll() with timeout. Blocking call.
255 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
)
259 if (events
== NULL
|| events
->current
.events
== NULL
) {
260 ERR("poll wait arguments error");
264 if (events
->current
.nb_fd
== 0) {
265 /* Return an invalid error to be consistent with epoll. */
270 if (events
->need_update
) {
271 ret
= update_current_events(events
);
278 ret
= poll(events
->wait
.events
, events
->wait
.nb_fd
, timeout
);
280 /* At this point, every error is fatal */
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.
289 return events
->wait
.nb_fd
;
296 * Setup poll set maximum size.
298 void compat_poll_set_max_size(void)
304 poll_max_size
= DEFAULT_POLL_SIZE
;
306 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
308 perror("getrlimit poll RLIMIT_NOFILE");
312 poll_max_size
= lim
.rlim_cur
;
313 if (poll_max_size
== 0) {
314 /* Extra precaution */
315 poll_max_size
= DEFAULT_POLL_SIZE
;
318 DBG("poll set max size set to %u", poll_max_size
);
This page took 0.049711 seconds and 4 git commands to generate.