2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2019 - Yannick Lamarre <ylamarre@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <sys/resource.h>
26 #include <common/defaults.h>
27 #include <common/error.h>
28 #include <common/macros.h>
29 #include <common/utils.h>
33 unsigned int poll_max_size
;
36 * Resize the epoll events structure of the new size.
38 * Return 0 on success or else -1 with the current events pointer untouched.
40 static int resize_poll_event(struct compat_poll_event_array
*array
,
47 /* Refuse to resize the array more than the max size. */
48 if (new_size
> poll_max_size
) {
52 ptr
= realloc(array
->events
, new_size
* sizeof(*ptr
));
54 PERROR("realloc epoll add");
57 if (new_size
> array
->alloc_size
) {
58 /* Zero newly allocated memory */
59 memset(ptr
+ array
->alloc_size
, 0,
60 (new_size
- array
->alloc_size
) * sizeof(*ptr
));
63 array
->alloc_size
= new_size
;
72 * Update events with the current events object.
74 static int update_current_events(struct lttng_poll_event
*events
)
77 struct compat_poll_event_array
*current
, *wait
;
81 current
= &events
->current
;
84 wait
->nb_fd
= current
->nb_fd
;
85 if (current
->alloc_size
!= wait
->alloc_size
) {
86 ret
= resize_poll_event(wait
, current
->alloc_size
);
91 memcpy(wait
->events
, current
->events
,
92 current
->nb_fd
* sizeof(*current
->events
));
95 events
->need_update
= 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 if (!poll_max_size
) {
116 if (lttng_poll_set_max_size()) {
121 /* Don't bust the limit here */
122 if (size
> poll_max_size
) {
123 size
= poll_max_size
;
126 /* Reset everything before begining the allocation. */
127 memset(events
, 0, sizeof(struct lttng_poll_event
));
129 current
= &events
->current
;
130 wait
= &events
->wait
;
132 /* This *must* be freed by using lttng_poll_free() */
133 wait
->events
= zmalloc(size
* sizeof(struct pollfd
));
134 if (wait
->events
== NULL
) {
135 PERROR("zmalloc struct pollfd");
139 wait
->alloc_size
= wait
->init_size
= size
;
141 current
->events
= zmalloc(size
* sizeof(struct pollfd
));
142 if (current
->events
== NULL
) {
143 PERROR("zmalloc struct current pollfd");
147 current
->alloc_size
= current
->init_size
= size
;
156 * Add fd to pollfd data structure with requested events.
158 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
161 int new_size
, ret
, i
;
162 struct compat_poll_event_array
*current
;
164 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
165 ERR("Bad compat poll add arguments");
169 current
= &events
->current
;
171 /* Check if fd we are trying to add is already there. */
172 for (i
= 0; i
< current
->nb_fd
; i
++) {
173 if (current
->events
[i
].fd
== fd
) {
179 /* Resize array if needed. */
180 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
+ 1);
181 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
) {
182 ret
= resize_poll_event(current
, new_size
);
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 * Modify an fd's events..
204 int compat_poll_mod(struct lttng_poll_event
*events
, int fd
,
208 struct compat_poll_event_array
*current
;
210 if (events
== NULL
|| events
->current
.nb_fd
== 0 ||
211 events
->current
.events
== NULL
|| fd
< 0) {
212 ERR("Bad compat poll mod arguments");
216 current
= &events
->current
;
218 for (i
= 0; i
< current
->nb_fd
; i
++) {
219 if (current
->events
[i
].fd
== fd
) {
220 current
->events
[i
].events
= req_events
;
221 events
->need_update
= 1;
227 * The epoll flavor doesn't flag modifying a non-included FD as an
238 * Remove a fd from the pollfd structure.
240 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
242 int i
, count
= 0, ret
;
244 struct compat_poll_event_array
*current
;
246 if (events
== NULL
|| events
->current
.nb_fd
== 0 ||
247 events
->current
.events
== NULL
|| fd
< 0) {
251 /* Ease our life a bit. */
252 current
= &events
->current
;
254 for (i
= 0; i
< current
->nb_fd
; i
++) {
255 /* Don't put back the fd we want to delete */
256 if (current
->events
[i
].fd
!= fd
) {
257 current
->events
[count
].fd
= current
->events
[i
].fd
;
258 current
->events
[count
].events
= current
->events
[i
].events
;
263 /* The fd was not in our set, return no error as with epoll. */
264 if (current
->nb_fd
== count
) {
268 /* No fd duplicate should be ever added into array. */
269 assert(current
->nb_fd
- 1 == count
);
270 current
->nb_fd
= count
;
272 /* Resize array if needed. */
273 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
);
274 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
275 && current
->nb_fd
!= 0) {
276 ret
= resize_poll_event(current
, new_size
);
282 events
->need_update
= 1;
292 * Wait on poll() with timeout. Blocking call.
294 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
)
296 int ret
, active_fd_count
;
297 int idle_pfd_index
= 0;
300 if (events
== NULL
|| events
->current
.events
== NULL
) {
301 ERR("poll wait arguments error");
304 assert(events
->current
.nb_fd
>= 0);
306 if (events
->current
.nb_fd
== 0) {
307 /* Return an invalid error to be consistent with epoll. */
309 events
->wait
.nb_fd
= 0;
313 if (events
->need_update
) {
314 ret
= update_current_events(events
);
322 ret
= poll(events
->wait
.events
, events
->wait
.nb_fd
, timeout
);
323 } while (ret
== -1 && errno
== EINTR
);
325 /* At this point, every error is fatal */
330 active_fd_count
= ret
;
333 * Swap all active pollfd structs to the beginning of the
334 * array to emulate compat-epoll behaviour. This algorithm takes
335 * advantage of poll's returned value and the burst nature of active
336 * events on the file descriptors. The while loop guarantees that
337 * idle_pfd will always point to an idle fd.
339 if (active_fd_count
== events
->wait
.nb_fd
) {
342 while (idle_pfd_index
< active_fd_count
&&
343 events
->wait
.events
[idle_pfd_index
].revents
!= 0) {
347 for (i
= idle_pfd_index
+ 1; idle_pfd_index
< active_fd_count
;
349 struct pollfd swap_pfd
;
350 struct pollfd
*idle_pfd
= &events
->wait
.events
[idle_pfd_index
];
351 struct pollfd
*current_pfd
= &events
->wait
.events
[i
];
353 if (idle_pfd
->revents
!= 0) {
354 swap_pfd
= *current_pfd
;
355 *current_pfd
= *idle_pfd
;
356 *idle_pfd
= swap_pfd
;
369 * Setup poll set maximum size.
371 int compat_poll_set_max_size(void)
376 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
378 PERROR("getrlimit poll RLIMIT_NOFILE");
383 poll_max_size
= lim
.rlim_cur
;
385 if (poll_max_size
== 0) {
386 poll_max_size
= DEFAULT_POLL_SIZE
;
388 DBG("poll set max size set to %u", poll_max_size
);
This page took 0.037763 seconds and 4 git commands to generate.