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 (current
->alloc_size
!= wait
->alloc_size
) {
84 ret
= resize_poll_event(wait
, current
->alloc_size
);
89 memcpy(wait
->events
, current
->events
,
90 current
->nb_fd
* sizeof(*current
->events
));
93 events
->need_update
= 0;
102 * Create pollfd data structure.
104 int compat_poll_create(struct lttng_poll_event
*events
, int size
)
106 struct compat_poll_event_array
*current
, *wait
;
108 if (events
== NULL
|| size
<= 0) {
109 ERR("Wrong arguments for poll create");
113 if (!poll_max_size
) {
114 ERR("poll_max_size not initialized yet");
118 /* Don't bust the limit here */
119 if (size
> poll_max_size
) {
120 size
= poll_max_size
;
123 /* Reset everything before begining the allocation. */
124 memset(events
, 0, sizeof(struct lttng_poll_event
));
126 current
= &events
->current
;
127 wait
= &events
->wait
;
129 /* This *must* be freed by using lttng_poll_free() */
130 wait
->events
= zmalloc(size
* sizeof(struct pollfd
));
131 if (wait
->events
== NULL
) {
132 PERROR("zmalloc struct pollfd");
136 wait
->alloc_size
= wait
->init_size
= size
;
138 current
->events
= zmalloc(size
* sizeof(struct pollfd
));
139 if (current
->events
== NULL
) {
140 PERROR("zmalloc struct current pollfd");
144 current
->alloc_size
= current
->init_size
= size
;
153 * Add fd to pollfd data structure with requested events.
155 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
158 int new_size
, ret
, i
;
159 struct compat_poll_event_array
*current
;
161 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
162 ERR("Bad compat poll add arguments");
166 current
= &events
->current
;
168 /* Check if fd we are trying to add is already there. */
169 for (i
= 0; i
< current
->nb_fd
; i
++) {
170 if (current
->events
[i
].fd
== fd
) {
176 /* Resize array if needed. */
177 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
+ 1);
178 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
) {
179 ret
= resize_poll_event(current
, new_size
);
185 current
->events
[current
->nb_fd
].fd
= fd
;
186 current
->events
[current
->nb_fd
].events
= req_events
;
188 events
->need_update
= 1;
190 DBG("fd %d of %d added to pollfd", fd
, current
->nb_fd
);
199 * Remove a fd from the pollfd structure.
201 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
203 int new_size
, i
, count
= 0, ret
;
204 struct compat_poll_event_array
*current
;
206 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
207 ERR("Wrong arguments for poll del");
211 /* Ease our life a bit. */
212 current
= &events
->current
;
214 for (i
= 0; i
< current
->nb_fd
; i
++) {
215 /* Don't put back the fd we want to delete */
216 if (current
->events
[i
].fd
!= fd
) {
217 current
->events
[count
].fd
= current
->events
[i
].fd
;
218 current
->events
[count
].events
= current
->events
[i
].events
;
222 /* No fd duplicate should be ever added into array. */
223 assert(current
->nb_fd
- 1 == count
);
224 current
->nb_fd
= count
;
226 /* Resize array if needed. */
227 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
);
228 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
) {
229 ret
= resize_poll_event(current
, new_size
);
235 events
->need_update
= 1;
244 * Wait on poll() with timeout. Blocking call.
246 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
)
250 if (events
== NULL
|| events
->current
.events
== NULL
) {
251 ERR("poll wait arguments error");
254 assert(events
->current
.nb_fd
>= 0);
256 if (events
->current
.nb_fd
== 0) {
257 /* Return an invalid error to be consistent with epoll. */
259 events
->wait
.nb_fd
= 0;
263 if (events
->need_update
) {
264 ret
= update_current_events(events
);
272 ret
= poll(events
->wait
.events
, events
->wait
.nb_fd
, timeout
);
273 } while (ret
== -1 && errno
== EINTR
);
275 /* At this point, every error is fatal */
281 * poll() should always iterate on all FDs since we handle the pollset in
282 * user space and after poll returns, we have to try every fd for a match.
284 return events
->wait
.nb_fd
;
291 * Setup poll set maximum size.
293 int compat_poll_set_max_size(void)
298 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
300 PERROR("getrlimit poll RLIMIT_NOFILE");
305 poll_max_size
= lim
.rlim_cur
;
307 if (poll_max_size
== 0) {
308 poll_max_size
= DEFAULT_POLL_SIZE
;
310 DBG("poll set max size set to %u", poll_max_size
);
This page took 0.273658 seconds and 4 git commands to generate.