2012-07-18 16:01:30 +02:00
|
|
|
/*
|
|
|
|
* \brief poll() implementation
|
|
|
|
* \author Josef Soentgen
|
2019-10-02 16:25:23 +02:00
|
|
|
* \author Christian Helmuth
|
2019-04-26 12:47:33 +02:00
|
|
|
* \author Emery Hemingway
|
2012-07-18 16:01:30 +02:00
|
|
|
* \date 2012-07-12
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2019-04-26 12:47:33 +02:00
|
|
|
* Copyright (C) 2010-2019 Genode Labs GmbH
|
2012-07-18 16:01:30 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2012-07-18 16:01:30 +02:00
|
|
|
*/
|
|
|
|
|
2017-02-10 22:38:37 +01:00
|
|
|
/* Libc includes */
|
2012-07-18 16:01:30 +02:00
|
|
|
#include <libc-plugin/plugin_registry.h>
|
|
|
|
#include <libc-plugin/plugin.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
/* internal includes */
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
#include <internal/errno.h>
|
|
|
|
#include <internal/file.h>
|
|
|
|
#include <internal/init.h>
|
|
|
|
#include <internal/suspend.h>
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
using namespace Libc;
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
|
2019-09-20 17:09:38 +02:00
|
|
|
/**
|
|
|
|
* The poll function was taken from OpenSSH portable (bsd-poll.c) and adepted
|
|
|
|
* to better fit within Genode's libc.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, 2005, 2007 Darren Tucker (dtucker at zip com au).
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
extern "C" int
|
|
|
|
__attribute__((weak))
|
|
|
|
poll(struct pollfd fds[], nfds_t nfds, int timeout)
|
2012-07-18 16:01:30 +02:00
|
|
|
{
|
2019-09-20 17:09:38 +02:00
|
|
|
nfds_t i;
|
|
|
|
int ret, fd, maxfd = 0;
|
|
|
|
fd_set readfds, writefds, exceptfds;
|
|
|
|
struct timeval tv, *tvp = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
fd = fds[i].fd;
|
|
|
|
if (fd >= (int)FD_SETSIZE) {
|
2019-10-02 16:25:23 +02:00
|
|
|
return Libc::Errno(EINVAL);
|
2012-07-18 16:01:30 +02:00
|
|
|
}
|
2019-09-20 17:09:38 +02:00
|
|
|
maxfd = MAX(maxfd, fd);
|
|
|
|
}
|
2012-07-18 16:01:30 +02:00
|
|
|
|
2019-09-20 17:09:38 +02:00
|
|
|
/* populate event bit vectors for the events we're interested in */
|
2019-04-26 12:47:33 +02:00
|
|
|
|
2019-09-20 17:09:38 +02:00
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_ZERO(&writefds);
|
|
|
|
FD_ZERO(&exceptfds);
|
2019-04-26 12:47:33 +02:00
|
|
|
|
2019-09-20 17:09:38 +02:00
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
fd = fds[i].fd;
|
|
|
|
if (fd == -1)
|
|
|
|
continue;
|
2019-10-02 16:25:23 +02:00
|
|
|
if (fds[i].events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
|
2019-09-20 17:09:38 +02:00
|
|
|
FD_SET(fd, &readfds);
|
|
|
|
FD_SET(fd, &exceptfds);
|
|
|
|
}
|
2019-10-02 16:25:23 +02:00
|
|
|
if (fds[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
|
2019-09-20 17:09:38 +02:00
|
|
|
FD_SET(fd, &writefds);
|
|
|
|
FD_SET(fd, &exceptfds);
|
|
|
|
}
|
2012-07-18 16:01:30 +02:00
|
|
|
}
|
2019-04-26 12:47:33 +02:00
|
|
|
|
2019-09-20 17:09:38 +02:00
|
|
|
/* poll timeout is msec, select is timeval (sec + usec) */
|
|
|
|
if (timeout >= 0) {
|
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
tvp = &tv;
|
|
|
|
}
|
|
|
|
ret = select(maxfd + 1, &readfds, &writefds, &exceptfds, tvp);
|
2019-10-02 16:25:23 +02:00
|
|
|
|
2019-09-20 17:09:38 +02:00
|
|
|
/* scan through select results and set poll() flags */
|
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
fd = fds[i].fd;
|
|
|
|
fds[i].revents = 0;
|
|
|
|
if (fd == -1)
|
|
|
|
continue;
|
|
|
|
if ((fds[i].events & POLLIN) && FD_ISSET(fd, &readfds)) {
|
|
|
|
fds[i].revents |= POLLIN;
|
|
|
|
}
|
|
|
|
if ((fds[i].events & POLLOUT) && FD_ISSET(fd, &writefds)) {
|
|
|
|
fds[i].revents |= POLLOUT;
|
2012-07-18 16:01:30 +02:00
|
|
|
}
|
2019-09-20 17:09:38 +02:00
|
|
|
if (FD_ISSET(fd, &exceptfds)) {
|
|
|
|
fds[i].revents |= POLLERR;
|
2012-07-18 16:01:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 17:09:38 +02:00
|
|
|
return ret;
|
2019-04-26 12:47:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak, alias("poll")))
|
|
|
|
int __sys_poll(struct pollfd fds[], nfds_t nfds, int timeout_ms);
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak, alias("poll")))
|
|
|
|
int _poll(struct pollfd fds[], nfds_t nfds, int timeout_ms);
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak))
|
|
|
|
int ppoll(struct pollfd fds[], nfds_t nfds,
|
|
|
|
const struct timespec *timeout,
|
|
|
|
const sigset_t*)
|
|
|
|
{
|
|
|
|
int timeout_ms = timeout->tv_sec * 1000 + timeout->tv_nsec / 1000;
|
|
|
|
return poll(fds, nfds, timeout_ms);
|
2012-07-18 16:01:30 +02:00
|
|
|
}
|
2019-04-26 12:47:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak, alias("ppoll")))
|
|
|
|
int __sys_ppoll(struct pollfd fds[], nfds_t nfds,
|
|
|
|
const struct timespec *timeout,
|
|
|
|
const sigset_t*);
|