lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2006-2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2006. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <limits.h> |
| 21 | #include <signal.h> |
| 22 | #include <stddef.h> /* For NULL. */ |
| 23 | #include <sys/poll.h> |
| 24 | #include <sysdep-cancel.h> |
| 25 | |
| 26 | |
| 27 | int |
| 28 | ppoll (struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, |
| 29 | const sigset_t *sigmask) |
| 30 | { |
| 31 | int tval = -1; |
| 32 | |
| 33 | /* poll uses a simple millisecond value. Convert it. */ |
| 34 | if (timeout != NULL) |
| 35 | { |
| 36 | if (timeout->tv_sec < 0 |
| 37 | || timeout->tv_nsec < 0 || timeout->tv_nsec >= 1000000000) |
| 38 | { |
| 39 | __set_errno (EINVAL); |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | if (timeout->tv_sec > INT_MAX / 1000 |
| 44 | || (timeout->tv_sec == INT_MAX / 1000 |
| 45 | && ((timeout->tv_nsec + 999999) / 1000000 > INT_MAX % 1000))) |
| 46 | /* We cannot represent the timeout in an int value. Wait |
| 47 | forever. */ |
| 48 | tval = -1; |
| 49 | else |
| 50 | tval = (timeout->tv_sec * 1000 |
| 51 | + (timeout->tv_nsec + 999999) / 1000000); |
| 52 | } |
| 53 | |
| 54 | /* The setting and restoring of the signal mask and the select call |
| 55 | should be an atomic operation. This can't be done without kernel |
| 56 | help. */ |
| 57 | sigset_t savemask; |
| 58 | if (sigmask != NULL) |
| 59 | __sigprocmask (SIG_SETMASK, sigmask, &savemask); |
| 60 | |
| 61 | /* Note the ppoll() is a cancellation point. But since we call |
| 62 | poll() which itself is a cancellation point we do not have |
| 63 | to do anything here. */ |
| 64 | int retval = __poll (fds, nfds, tval); |
| 65 | |
| 66 | if (sigmask != NULL) |
| 67 | __sigprocmask (SIG_SETMASK, &savemask, NULL); |
| 68 | |
| 69 | return retval; |
| 70 | } |
| 71 | |
| 72 | #ifndef ppoll |
| 73 | /* __poll handles cancellation. */ |
| 74 | LIBC_CANCEL_HANDLED (); |
| 75 | libc_hidden_def (ppoll); |
| 76 | #endif |