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
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/macros.h>
17 #include <common/utils.h>
25 #include <sys/types.h>
30 * Maximum number of fd we can monitor.
32 * For epoll(7), /proc/sys/fs/epoll/max_user_watches (since Linux 2.6.28) will
33 * be used for the maximum size of the poll set. If this interface is not
34 * available, according to the manpage, the max_user_watches value is 1/25 (4%)
35 * of the available low memory divided by the registration cost in bytes which
36 * is 90 bytes on a 32-bit kernel and 160 bytes on a 64-bit kernel.
39 static unsigned int poll_max_size
;
42 * Resize the epoll events structure of the new size.
44 * Return 0 on success or else -1 with the current events pointer untouched.
46 static int resize_poll_event(struct lttng_poll_event
*events
,
49 struct epoll_event
*ptr
;
53 ptr
= realloc(events
->events
, new_size
* sizeof(*ptr
));
55 PERROR("realloc epoll add");
58 if (new_size
> events
->alloc_size
) {
59 /* Zero newly allocated memory */
60 memset(ptr
+ events
->alloc_size
, 0,
61 (new_size
- events
->alloc_size
) * sizeof(*ptr
));
64 events
->alloc_size
= new_size
;
73 * Create epoll set and allocate returned events structure.
76 int compat_epoll_create(struct lttng_poll_event
*events
, int size
, int flags
)
80 if (events
== NULL
|| size
<= 0) {
85 if (lttng_poll_set_max_size()) {
90 /* Don't bust the limit here */
91 if (size
> poll_max_size
) {
95 ret
= compat_glibc_epoll_create(size
, flags
);
97 /* At this point, every error is fatal */
98 PERROR("epoll_create1");
104 /* This *must* be freed by using lttng_poll_free() */
105 events
->events
= zmalloc(size
* sizeof(struct epoll_event
));
106 if (events
->events
== NULL
) {
107 PERROR("zmalloc epoll set");
111 events
->alloc_size
= events
->init_size
= size
;
117 ret
= close(events
->epfd
);
126 * Add a fd to the epoll set with requesting events.
129 int compat_epoll_add(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
132 struct epoll_event ev
;
134 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
135 ERR("Bad compat epoll add arguments");
140 * Zero struct epoll_event to ensure all representations of its
143 memset(&ev
, 0, sizeof(ev
));
144 ev
.events
= req_events
;
147 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_ADD
, fd
, &ev
);
151 /* If exist, it's OK. */
155 /* Print PERROR and goto end not failing. Show must go on. */
156 PERROR("epoll_ctl ADD");
159 PERROR("epoll_ctl ADD fatal");
174 * Remove a fd from the epoll set.
177 int compat_epoll_del(struct lttng_poll_event
*events
, int fd
)
181 if (events
== NULL
|| fd
< 0 || events
->nb_fd
== 0) {
185 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_DEL
, fd
, NULL
);
190 /* Print PERROR and goto end not failing. Show must go on. */
191 PERROR("epoll_ctl DEL");
194 PERROR("epoll_ctl DEL fatal");
209 * Set an fd's events.
212 int compat_epoll_mod(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
215 struct epoll_event ev
;
217 if (events
== NULL
|| fd
< 0 || events
->nb_fd
== 0) {
222 * Zero struct epoll_event to ensure all representations of its
225 memset(&ev
, 0, sizeof(ev
));
226 ev
.events
= req_events
;
229 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_MOD
, fd
, &ev
);
234 /* Print PERROR and goto end not failing. Show must go on. */
235 PERROR("epoll_ctl MOD");
238 PERROR("epoll_ctl MOD fatal");
251 * Wait on epoll set. This is a blocking call of timeout value.
254 int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
,
260 if (events
== NULL
|| events
->events
== NULL
) {
261 ERR("Wrong arguments in compat_epoll_wait");
265 if (events
->nb_fd
== 0) {
271 * Resize if needed before waiting. We could either expand the array or
272 * shrink it down. It's important to note that after this step, we are
273 * ensured that the events argument of the epoll_wait call will be large
274 * enough to hold every possible returned events.
276 new_size
= 1U << utils_get_count_order_u32(events
->nb_fd
);
277 if (new_size
!= events
->alloc_size
&& new_size
>= events
->init_size
) {
278 ret
= resize_poll_event(events
, new_size
);
280 /* ENOMEM problem at this point. */
286 ret
= epoll_wait(events
->epfd
, events
->events
, events
->nb_fd
, timeout
);
287 } while (!interruptible
&& ret
== -1 && errno
== EINTR
);
289 if (errno
!= EINTR
) {
290 PERROR("epoll_wait");
296 * Since the returned events are set sequentially in the "events" structure
297 * we only need to return the epoll_wait value and iterate over it.
306 * Setup poll set maximum size.
309 int compat_epoll_set_max_size(void)
311 int ret
, fd
, retval
= 0;
315 fd
= open(COMPAT_EPOLL_PROC_PATH
, O_RDONLY
);
318 * Failing on opening [1] is not an error per see. [1] was
319 * introduced in Linux 2.6.28 but epoll is available since
320 * 2.5.44. Hence, goto end and set a default value without
321 * setting an error return value.
323 * [1] /proc/sys/fs/epoll/max_user_watches
329 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
331 * Allow reading a file smaller than buf, but keep space for
334 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
335 PERROR("read set max size");
339 buf
[size_ret
] = '\0';
340 poll_max_size
= atoi(buf
);
347 if (!poll_max_size
) {
348 poll_max_size
= DEFAULT_POLL_SIZE
;
350 DBG("epoll set max size is %d", poll_max_size
);
354 #else /* HAVE_EPOLL */
356 #include <sys/resource.h>
357 #include <sys/time.h>
360 * Maximum number of fd we can monitor.
362 * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by
365 static unsigned int poll_max_size
;
368 * Resize the epoll events structure of the new size.
370 * Return 0 on success or else -1 with the current events pointer untouched.
372 static int resize_poll_event(struct compat_poll_event_array
*array
,
379 /* Refuse to resize the array more than the max size. */
380 if (new_size
> poll_max_size
) {
384 ptr
= realloc(array
->events
, new_size
* sizeof(*ptr
));
386 PERROR("realloc epoll add");
389 if (new_size
> array
->alloc_size
) {
390 /* Zero newly allocated memory */
391 memset(ptr
+ array
->alloc_size
, 0,
392 (new_size
- array
->alloc_size
) * sizeof(*ptr
));
395 array
->alloc_size
= new_size
;
404 * Update events with the current events object.
406 static int update_current_events(struct lttng_poll_event
*events
)
409 struct compat_poll_event_array
*current
, *wait
;
413 current
= &events
->current
;
414 wait
= &events
->wait
;
416 wait
->nb_fd
= current
->nb_fd
;
417 if (current
->alloc_size
!= wait
->alloc_size
) {
418 ret
= resize_poll_event(wait
, current
->alloc_size
);
423 memcpy(wait
->events
, current
->events
,
424 current
->nb_fd
* sizeof(*current
->events
));
426 /* Update is done. */
427 events
->need_update
= 0;
436 * Create pollfd data structure.
439 int compat_poll_create(struct lttng_poll_event
*events
, int size
)
441 struct compat_poll_event_array
*current
, *wait
;
443 if (events
== NULL
|| size
<= 0) {
444 ERR("Wrong arguments for poll create");
448 if (!poll_max_size
) {
449 if (lttng_poll_set_max_size()) {
454 /* Don't bust the limit here */
455 if (size
> poll_max_size
) {
456 size
= poll_max_size
;
459 /* Reset everything before begining the allocation. */
460 memset(events
, 0, sizeof(struct lttng_poll_event
));
462 current
= &events
->current
;
463 wait
= &events
->wait
;
465 /* This *must* be freed by using lttng_poll_free() */
466 wait
->events
= zmalloc(size
* sizeof(struct pollfd
));
467 if (wait
->events
== NULL
) {
468 PERROR("zmalloc struct pollfd");
472 wait
->alloc_size
= wait
->init_size
= size
;
474 current
->events
= zmalloc(size
* sizeof(struct pollfd
));
475 if (current
->events
== NULL
) {
476 PERROR("zmalloc struct current pollfd");
480 current
->alloc_size
= current
->init_size
= size
;
489 * Add fd to pollfd data structure with requested events.
492 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
495 int new_size
, ret
, i
;
496 struct compat_poll_event_array
*current
;
498 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
499 ERR("Bad compat poll add arguments");
503 current
= &events
->current
;
505 /* Check if fd we are trying to add is already there. */
506 for (i
= 0; i
< current
->nb_fd
; i
++) {
507 if (current
->events
[i
].fd
== fd
) {
513 /* Resize array if needed. */
514 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
+ 1);
515 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
) {
516 ret
= resize_poll_event(current
, new_size
);
522 current
->events
[current
->nb_fd
].fd
= fd
;
523 current
->events
[current
->nb_fd
].events
= req_events
;
525 events
->need_update
= 1;
527 DBG("fd %d of %d added to pollfd", fd
, current
->nb_fd
);
536 * Modify an fd's events..
539 int compat_poll_mod(struct lttng_poll_event
*events
, int fd
,
543 struct compat_poll_event_array
*current
;
545 if (events
== NULL
|| events
->current
.nb_fd
== 0 ||
546 events
->current
.events
== NULL
|| fd
< 0) {
547 ERR("Bad compat poll mod arguments");
551 current
= &events
->current
;
553 for (i
= 0; i
< current
->nb_fd
; i
++) {
554 if (current
->events
[i
].fd
== fd
) {
555 current
->events
[i
].events
= req_events
;
556 events
->need_update
= 1;
562 * The epoll flavor doesn't flag modifying a non-included FD as an
573 * Remove a fd from the pollfd structure.
576 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
578 int i
, count
= 0, ret
;
580 struct compat_poll_event_array
*current
;
582 if (events
== NULL
|| events
->current
.nb_fd
== 0 ||
583 events
->current
.events
== NULL
|| fd
< 0) {
587 /* Ease our life a bit. */
588 current
= &events
->current
;
590 for (i
= 0; i
< current
->nb_fd
; i
++) {
591 /* Don't put back the fd we want to delete */
592 if (current
->events
[i
].fd
!= fd
) {
593 current
->events
[count
].fd
= current
->events
[i
].fd
;
594 current
->events
[count
].events
= current
->events
[i
].events
;
599 /* The fd was not in our set, return no error as with epoll. */
600 if (current
->nb_fd
== count
) {
604 /* No fd duplicate should be ever added into array. */
605 assert(current
->nb_fd
- 1 == count
);
606 current
->nb_fd
= count
;
608 /* Resize array if needed. */
609 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
);
610 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
611 && current
->nb_fd
!= 0) {
612 ret
= resize_poll_event(current
, new_size
);
618 events
->need_update
= 1;
628 * Wait on poll() with timeout. Blocking call.
631 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
,
634 int ret
, active_fd_count
;
635 size_t pos
= 0, consecutive_entries
= 0, non_idle_pos
;
637 if (events
== NULL
|| events
->current
.events
== NULL
) {
638 ERR("poll wait arguments error");
642 if (events
->current
.nb_fd
== 0) {
643 /* Return an invalid error to be consistent with epoll. */
645 events
->wait
.nb_fd
= 0;
649 if (events
->need_update
) {
650 ret
= update_current_events(events
);
658 ret
= poll(events
->wait
.events
, events
->wait
.nb_fd
, timeout
);
659 } while (!interruptible
&& ret
== -1 && errno
== EINTR
);
661 if (errno
!= EINTR
) {
667 active_fd_count
= ret
;
670 * Move all active pollfd structs to the beginning of the
671 * array to emulate compat-epoll behaviour.
673 if (active_fd_count
== events
->wait
.nb_fd
) {
677 while (consecutive_entries
!= active_fd_count
) {
678 struct pollfd
*current
= &events
->wait
.events
[pos
];
679 struct pollfd idle_entry
;
681 if (current
->revents
!= 0) {
682 consecutive_entries
++;
689 /* Look for next non-idle entry. */
690 while (events
->wait
.events
[++non_idle_pos
].revents
== 0);
692 /* Swap idle and non-idle entries. */
693 idle_entry
= *current
;
694 *current
= events
->wait
.events
[non_idle_pos
];
695 events
->wait
.events
[non_idle_pos
] = idle_entry
;
697 consecutive_entries
++;
708 * Setup poll set maximum size.
711 int compat_poll_set_max_size(void)
716 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
718 PERROR("getrlimit poll RLIMIT_NOFILE");
723 poll_max_size
= lim
.rlim_cur
;
725 if (poll_max_size
== 0) {
726 poll_max_size
= DEFAULT_POLL_SIZE
;
728 DBG("poll set max size set to %u", poll_max_size
);
732 #endif /* !HAVE_EPOLL */
This page took 0.044191 seconds and 4 git commands to generate.