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>
35 * Maximum number of fd we can monitor.
37 * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by
40 static unsigned int poll_max_size
;
43 * Resize the epoll events structure of the new size.
45 * Return 0 on success or else -1 with the current events pointer untouched.
47 static int resize_poll_event(struct compat_poll_event_array
*array
,
54 /* Refuse to resize the array more than the max size. */
55 if (new_size
> poll_max_size
) {
59 ptr
= realloc(array
->events
, new_size
* sizeof(*ptr
));
61 PERROR("realloc epoll add");
64 if (new_size
> array
->alloc_size
) {
65 /* Zero newly allocated memory */
66 memset(ptr
+ array
->alloc_size
, 0,
67 (new_size
- array
->alloc_size
) * sizeof(*ptr
));
70 array
->alloc_size
= new_size
;
79 * Update events with the current events object.
81 static int update_current_events(struct lttng_poll_event
*events
)
84 struct compat_poll_event_array
*current
, *wait
;
88 current
= &events
->current
;
91 wait
->nb_fd
= current
->nb_fd
;
92 if (current
->alloc_size
!= wait
->alloc_size
) {
93 ret
= resize_poll_event(wait
, current
->alloc_size
);
98 memcpy(wait
->events
, current
->events
,
99 current
->nb_fd
* sizeof(*current
->events
));
101 /* Update is done. */
102 events
->need_update
= 0;
111 * Create pollfd data structure.
114 int compat_poll_create(struct lttng_poll_event
*events
, int size
)
116 struct compat_poll_event_array
*current
, *wait
;
118 if (events
== NULL
|| size
<= 0) {
119 ERR("Wrong arguments for poll create");
123 if (!poll_max_size
) {
124 if (lttng_poll_set_max_size()) {
129 /* Don't bust the limit here */
130 if (size
> poll_max_size
) {
131 size
= poll_max_size
;
134 /* Reset everything before begining the allocation. */
135 memset(events
, 0, sizeof(struct lttng_poll_event
));
137 current
= &events
->current
;
138 wait
= &events
->wait
;
140 /* This *must* be freed by using lttng_poll_free() */
141 wait
->events
= zmalloc(size
* sizeof(struct pollfd
));
142 if (wait
->events
== NULL
) {
143 PERROR("zmalloc struct pollfd");
147 wait
->alloc_size
= wait
->init_size
= size
;
149 current
->events
= zmalloc(size
* sizeof(struct pollfd
));
150 if (current
->events
== NULL
) {
151 PERROR("zmalloc struct current pollfd");
155 current
->alloc_size
= current
->init_size
= size
;
164 * Add fd to pollfd data structure with requested events.
167 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
170 int new_size
, ret
, i
;
171 struct compat_poll_event_array
*current
;
173 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
174 ERR("Bad compat poll add arguments");
178 current
= &events
->current
;
180 /* Check if fd we are trying to add is already there. */
181 for (i
= 0; i
< current
->nb_fd
; i
++) {
182 if (current
->events
[i
].fd
== fd
) {
188 /* Resize array if needed. */
189 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
+ 1);
190 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
) {
191 ret
= resize_poll_event(current
, new_size
);
197 current
->events
[current
->nb_fd
].fd
= fd
;
198 current
->events
[current
->nb_fd
].events
= req_events
;
200 events
->need_update
= 1;
202 DBG("fd %d of %d added to pollfd", fd
, current
->nb_fd
);
211 * Modify an fd's events..
214 int compat_poll_mod(struct lttng_poll_event
*events
, int fd
,
218 struct compat_poll_event_array
*current
;
220 if (events
== NULL
|| events
->current
.nb_fd
== 0 ||
221 events
->current
.events
== NULL
|| fd
< 0) {
222 ERR("Bad compat poll mod arguments");
226 current
= &events
->current
;
228 for (i
= 0; i
< current
->nb_fd
; i
++) {
229 if (current
->events
[i
].fd
== fd
) {
230 current
->events
[i
].events
= req_events
;
231 events
->need_update
= 1;
237 * The epoll flavor doesn't flag modifying a non-included FD as an
248 * Remove a fd from the pollfd structure.
251 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
253 int i
, count
= 0, ret
;
255 struct compat_poll_event_array
*current
;
257 if (events
== NULL
|| events
->current
.nb_fd
== 0 ||
258 events
->current
.events
== NULL
|| fd
< 0) {
262 /* Ease our life a bit. */
263 current
= &events
->current
;
265 for (i
= 0; i
< current
->nb_fd
; i
++) {
266 /* Don't put back the fd we want to delete */
267 if (current
->events
[i
].fd
!= fd
) {
268 current
->events
[count
].fd
= current
->events
[i
].fd
;
269 current
->events
[count
].events
= current
->events
[i
].events
;
274 /* The fd was not in our set, return no error as with epoll. */
275 if (current
->nb_fd
== count
) {
279 /* No fd duplicate should be ever added into array. */
280 assert(current
->nb_fd
- 1 == count
);
281 current
->nb_fd
= count
;
283 /* Resize array if needed. */
284 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
);
285 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
286 && current
->nb_fd
!= 0) {
287 ret
= resize_poll_event(current
, new_size
);
293 events
->need_update
= 1;
303 * Wait on poll() with timeout. Blocking call.
306 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
,
309 int ret
, active_fd_count
;
310 int idle_pfd_index
= 0;
313 if (events
== NULL
|| events
->current
.events
== NULL
) {
314 ERR("poll wait arguments error");
318 if (events
->current
.nb_fd
== 0) {
319 /* Return an invalid error to be consistent with epoll. */
321 events
->wait
.nb_fd
= 0;
325 if (events
->need_update
) {
326 ret
= update_current_events(events
);
334 ret
= poll(events
->wait
.events
, events
->wait
.nb_fd
, timeout
);
335 } while (!interruptible
&& ret
== -1 && errno
== EINTR
);
337 if (errno
!= EINTR
) {
343 active_fd_count
= ret
;
346 * Swap all active pollfd structs to the beginning of the
347 * array to emulate compat-epoll behaviour. This algorithm takes
348 * advantage of poll's returned value and the burst nature of active
349 * events on the file descriptors. The while loop guarantees that
350 * idle_pfd will always point to an idle fd.
352 if (active_fd_count
== events
->wait
.nb_fd
) {
355 while (idle_pfd_index
< active_fd_count
&&
356 events
->wait
.events
[idle_pfd_index
].revents
!= 0) {
360 for (i
= idle_pfd_index
+ 1; idle_pfd_index
< active_fd_count
;
362 struct pollfd swap_pfd
;
363 struct pollfd
*idle_pfd
= &events
->wait
.events
[idle_pfd_index
];
364 struct pollfd
*current_pfd
= &events
->wait
.events
[i
];
366 if (idle_pfd
->revents
!= 0) {
367 swap_pfd
= *current_pfd
;
368 *current_pfd
= *idle_pfd
;
369 *idle_pfd
= swap_pfd
;
382 * Setup poll set maximum size.
385 int compat_poll_set_max_size(void)
390 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
392 PERROR("getrlimit poll RLIMIT_NOFILE");
397 poll_max_size
= lim
.rlim_cur
;
399 if (poll_max_size
== 0) {
400 poll_max_size
= DEFAULT_POLL_SIZE
;
402 DBG("poll set max size set to %u", poll_max_size
);
This page took 0.046956 seconds and 4 git commands to generate.