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>
29 unsigned int poll_max_size
;
32 * Resize the epoll events structure of the new size.
34 * Return 0 on success or else -1 with the current events pointer untouched.
36 static int resize_poll_event(struct compat_poll_event_array
*array
,
43 /* Refuse to resize the array more than the max size. */
44 if (new_size
> poll_max_size
) {
48 ptr
= realloc(array
->events
, new_size
* sizeof(*ptr
));
50 PERROR("realloc epoll add");
54 array
->alloc_size
= new_size
;
63 * Update events with the current events object.
65 static int update_current_events(struct lttng_poll_event
*events
)
68 struct compat_poll_event_array
*current
, *wait
;
72 current
= &events
->current
;
75 wait
->nb_fd
= current
->nb_fd
;
76 if (events
->need_realloc
) {
77 ret
= resize_poll_event(wait
, current
->alloc_size
);
82 memcpy(wait
->events
, current
->events
,
83 current
->nb_fd
* sizeof(*current
->events
));
85 /* Update is done and realloc as well. */
86 events
->need_update
= 0;
87 events
->need_realloc
= 0;
96 * Create pollfd data structure.
98 int compat_poll_create(struct lttng_poll_event
*events
, int size
)
100 struct compat_poll_event_array
*current
, *wait
;
102 if (events
== NULL
|| size
<= 0) {
103 ERR("Wrong arguments for poll create");
107 /* Don't bust the limit here */
108 if (size
> poll_max_size
) {
109 size
= poll_max_size
;
112 /* Reset everything before begining the allocation. */
113 memset(events
, 0, sizeof(struct lttng_poll_event
));
115 /* Ease our life a bit. */
116 current
= &events
->current
;
117 wait
= &events
->wait
;
119 /* This *must* be freed by using lttng_poll_free() */
120 wait
->events
= zmalloc(size
* sizeof(struct pollfd
));
121 if (wait
->events
== NULL
) {
122 perror("zmalloc struct pollfd");
126 wait
->alloc_size
= wait
->init_size
= size
;
128 current
->events
= zmalloc(size
* sizeof(struct pollfd
));
129 if (current
->events
== NULL
) {
130 perror("zmalloc struct current pollfd");
134 current
->alloc_size
= current
->init_size
= size
;
143 * Add fd to pollfd data structure with requested events.
145 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
148 int new_size
, ret
, i
;
149 struct compat_poll_event_array
*current
;
151 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
152 ERR("Bad compat poll add arguments");
156 /* Ease our life a bit. */
157 current
= &events
->current
;
159 /* Check if fd we are trying to add is already there. */
160 for (i
= 0; i
< current
->nb_fd
; i
++) {
161 /* Don't put back the fd we want to delete */
162 if (current
->events
[i
].fd
== fd
) {
168 /* Check for a needed resize of the array. */
169 if (current
->nb_fd
> current
->alloc_size
) {
170 /* Expand it by a power of two of the current size. */
171 new_size
= current
->alloc_size
<< 1UL;
172 ret
= resize_poll_event(current
, new_size
);
176 events
->need_realloc
= 1;
179 current
->events
[current
->nb_fd
].fd
= fd
;
180 current
->events
[current
->nb_fd
].events
= req_events
;
182 events
->need_update
= 1;
184 DBG("fd %d of %d added to pollfd", fd
, current
->nb_fd
);
193 * Remove a fd from the pollfd structure.
195 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
197 int new_size
, i
, count
= 0, ret
;
198 struct compat_poll_event_array
*current
;
200 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
201 ERR("Wrong arguments for poll del");
205 /* Ease our life a bit. */
206 current
= &events
->current
;
208 /* Check if we need to shrink it down. */
209 if ((current
->nb_fd
<< 1UL) <= current
->alloc_size
&&
210 current
->nb_fd
>= current
->init_size
) {
212 * Shrink if nb_fd multiplied by two is <= than the actual size and we
213 * are above the initial size.
215 new_size
= current
->alloc_size
>> 1UL;
216 ret
= resize_poll_event(current
, new_size
);
220 events
->need_realloc
= 1;
223 for (i
= 0; i
< current
->nb_fd
; i
++) {
224 /* Don't put back the fd we want to delete */
225 if (current
->events
[i
].fd
!= fd
) {
226 current
->events
[count
].fd
= current
->events
[i
].fd
;
227 current
->events
[count
].events
= current
->events
[i
].events
;
233 events
->need_update
= 1;
242 * Wait on poll() with timeout. Blocking call.
244 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
)
248 if (events
== NULL
|| events
->current
.events
== NULL
) {
249 ERR("poll wait arguments error");
253 if (events
->current
.nb_fd
== 0) {
254 /* Return an invalid error to be consistent with epoll. */
259 if (events
->need_update
) {
260 ret
= update_current_events(events
);
267 ret
= poll(events
->wait
.events
, events
->wait
.nb_fd
, timeout
);
269 /* At this point, every error is fatal */
275 * poll() should always iterate on all FDs since we handle the pollset in
276 * user space and after poll returns, we have to try every fd for a match.
278 return events
->wait
.nb_fd
;
285 * Setup poll set maximum size.
287 void compat_poll_set_max_size(void)
293 poll_max_size
= DEFAULT_POLL_SIZE
;
295 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
297 perror("getrlimit poll RLIMIT_NOFILE");
301 poll_max_size
= lim
.rlim_cur
;
302 if (poll_max_size
== 0) {
303 /* Extra precaution */
304 poll_max_size
= DEFAULT_POLL_SIZE
;
307 DBG("poll set max size set to %u", poll_max_size
);
This page took 0.03675 seconds and 4 git commands to generate.