2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2019 Yannick Lamarre <ylamarre@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
12 #include <sys/resource.h>
16 #include <common/defaults.h>
17 #include <common/error.h>
18 #include <common/macros.h>
19 #include <common/utils.h>
25 * Maximum number of fd we can monitor.
27 * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by
30 static unsigned int poll_max_size
;
33 * Resize the epoll events structure of the new size.
35 * Return 0 on success or else -1 with the current events pointer untouched.
37 static int resize_poll_event(struct compat_poll_event_array
*array
,
44 /* Refuse to resize the array more than the max size. */
45 if (new_size
> poll_max_size
) {
49 ptr
= realloc(array
->events
, new_size
* sizeof(*ptr
));
51 PERROR("realloc epoll add");
54 if (new_size
> array
->alloc_size
) {
55 /* Zero newly allocated memory */
56 memset(ptr
+ array
->alloc_size
, 0,
57 (new_size
- array
->alloc_size
) * sizeof(*ptr
));
60 array
->alloc_size
= new_size
;
69 * Update events with the current events object.
71 static int update_current_events(struct lttng_poll_event
*events
)
74 struct compat_poll_event_array
*current
, *wait
;
78 current
= &events
->current
;
81 wait
->nb_fd
= current
->nb_fd
;
82 if (current
->alloc_size
!= wait
->alloc_size
) {
83 ret
= resize_poll_event(wait
, current
->alloc_size
);
88 memcpy(wait
->events
, current
->events
,
89 current
->nb_fd
* sizeof(*current
->events
));
92 events
->need_update
= 0;
101 * 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 if (lttng_poll_set_max_size()) {
119 /* Don't bust the limit here */
120 if (size
> poll_max_size
) {
121 size
= poll_max_size
;
124 /* Reset everything before begining the allocation. */
125 memset(events
, 0, sizeof(struct lttng_poll_event
));
127 current
= &events
->current
;
128 wait
= &events
->wait
;
130 /* This *must* be freed by using lttng_poll_free() */
131 wait
->events
= zmalloc(size
* sizeof(struct pollfd
));
132 if (wait
->events
== NULL
) {
133 PERROR("zmalloc struct pollfd");
137 wait
->alloc_size
= wait
->init_size
= size
;
139 current
->events
= zmalloc(size
* sizeof(struct pollfd
));
140 if (current
->events
== NULL
) {
141 PERROR("zmalloc struct current pollfd");
145 current
->alloc_size
= current
->init_size
= size
;
154 * Add fd to pollfd data structure with requested events.
157 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
160 int new_size
, ret
, i
;
161 struct compat_poll_event_array
*current
;
163 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
164 ERR("Bad compat poll add arguments");
168 current
= &events
->current
;
170 /* Check if fd we are trying to add is already there. */
171 for (i
= 0; i
< current
->nb_fd
; i
++) {
172 if (current
->events
[i
].fd
== fd
) {
178 /* Resize array if needed. */
179 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
+ 1);
180 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
) {
181 ret
= resize_poll_event(current
, new_size
);
187 current
->events
[current
->nb_fd
].fd
= fd
;
188 current
->events
[current
->nb_fd
].events
= req_events
;
190 events
->need_update
= 1;
192 DBG("fd %d of %d added to pollfd", fd
, current
->nb_fd
);
201 * 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.
241 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
243 int i
, count
= 0, ret
;
245 struct compat_poll_event_array
*current
;
247 if (events
== NULL
|| events
->current
.nb_fd
== 0 ||
248 events
->current
.events
== NULL
|| fd
< 0) {
252 /* Ease our life a bit. */
253 current
= &events
->current
;
255 for (i
= 0; i
< current
->nb_fd
; i
++) {
256 /* Don't put back the fd we want to delete */
257 if (current
->events
[i
].fd
!= fd
) {
258 current
->events
[count
].fd
= current
->events
[i
].fd
;
259 current
->events
[count
].events
= current
->events
[i
].events
;
264 /* The fd was not in our set, return no error as with epoll. */
265 if (current
->nb_fd
== count
) {
269 /* No fd duplicate should be ever added into array. */
270 assert(current
->nb_fd
- 1 == count
);
271 current
->nb_fd
= count
;
273 /* Resize array if needed. */
274 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
);
275 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
276 && current
->nb_fd
!= 0) {
277 ret
= resize_poll_event(current
, new_size
);
283 events
->need_update
= 1;
293 * Wait on poll() with timeout. Blocking call.
296 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
,
299 int ret
, active_fd_count
;
300 int idle_pfd_index
= 0;
303 if (events
== NULL
|| events
->current
.events
== NULL
) {
304 ERR("poll wait arguments error");
308 if (events
->current
.nb_fd
== 0) {
309 /* Return an invalid error to be consistent with epoll. */
311 events
->wait
.nb_fd
= 0;
315 if (events
->need_update
) {
316 ret
= update_current_events(events
);
324 ret
= poll(events
->wait
.events
, events
->wait
.nb_fd
, timeout
);
325 } while (!interruptible
&& ret
== -1 && errno
== EINTR
);
327 if (errno
!= EINTR
) {
333 active_fd_count
= ret
;
336 * Swap all active pollfd structs to the beginning of the
337 * array to emulate compat-epoll behaviour. This algorithm takes
338 * advantage of poll's returned value and the burst nature of active
339 * events on the file descriptors. The while loop guarantees that
340 * idle_pfd will always point to an idle fd.
342 if (active_fd_count
== events
->wait
.nb_fd
) {
345 while (idle_pfd_index
< active_fd_count
&&
346 events
->wait
.events
[idle_pfd_index
].revents
!= 0) {
350 for (i
= idle_pfd_index
+ 1; idle_pfd_index
< active_fd_count
;
352 struct pollfd swap_pfd
;
353 struct pollfd
*idle_pfd
= &events
->wait
.events
[idle_pfd_index
];
354 struct pollfd
*current_pfd
= &events
->wait
.events
[i
];
356 if (idle_pfd
->revents
!= 0) {
357 swap_pfd
= *current_pfd
;
358 *current_pfd
= *idle_pfd
;
359 *idle_pfd
= swap_pfd
;
372 * Setup poll set maximum size.
375 int compat_poll_set_max_size(void)
380 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
382 PERROR("getrlimit poll RLIMIT_NOFILE");
387 poll_max_size
= lim
.rlim_cur
;
389 if (poll_max_size
== 0) {
390 poll_max_size
= DEFAULT_POLL_SIZE
;
392 DBG("poll set max size set to %u", poll_max_size
);
This page took 0.04331 seconds and 5 git commands to generate.