poll.cpp 823 B

1234567891011121314151617181920212223242526272829303132
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <errno.h>
  7. #include <poll.h>
  8. #include <sys/time.h>
  9. #include <syscall.h>
  10. extern "C" {
  11. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html
  12. int poll(pollfd* fds, nfds_t nfds, int timeout_ms)
  13. {
  14. timespec timeout;
  15. timespec* timeout_ts = &timeout;
  16. if (timeout_ms < 0)
  17. timeout_ts = nullptr;
  18. else
  19. timeout = { timeout_ms / 1000, (timeout_ms % 1000) * 1'000'000 };
  20. return ppoll(fds, nfds, timeout_ts, nullptr);
  21. }
  22. int ppoll(pollfd* fds, nfds_t nfds, timespec const* timeout, sigset_t const* sigmask)
  23. {
  24. Syscall::SC_poll_params params { fds, nfds, timeout, sigmask };
  25. int rc = syscall(SC_poll, &params);
  26. __RETURN_WITH_ERRNO(rc, rc, -1);
  27. }
  28. }