| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2013 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 4 | * | 
|  | 5 | * Redistribution and use in source and binary forms, with or without | 
|  | 6 | * modification, are permitted provided that the following conditions | 
|  | 7 | * are met: | 
|  | 8 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
| Elliott Hughes | 40360b3 | 2014-12-29 13:29:50 -0800 | [diff] [blame] | 29 | #include <errno.h> | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 30 | #include <sys/poll.h> | 
|  | 31 | #include <sys/select.h> | 
|  | 32 |  | 
| Josh Gao | 5074e7d | 2019-12-13 13:55:53 -0800 | [diff] [blame] | 33 | #include <platform/bionic/reserved_signals.h> | 
|  | 34 |  | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 35 | #include "private/bionic_time_conversions.h" | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 36 | #include "private/SigSetConverter.h" | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 37 |  | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 38 | extern "C" int __ppoll(pollfd*, unsigned int, timespec*, const sigset64_t*, size_t); | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 39 | extern "C" int __pselect6(int, fd_set*, fd_set*, fd_set*, timespec*, void*); | 
|  | 40 |  | 
| George Burgess IV | 9024235 | 2018-02-06 12:51:31 -0800 | [diff] [blame] | 41 | int poll(pollfd* fds, nfds_t fd_count, int ms) { | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 42 | timespec ts; | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 43 | timespec* ts_ptr = nullptr; | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 44 | if (ms >= 0) { | 
|  | 45 | timespec_from_ms(ts, ms); | 
|  | 46 | ts_ptr = &ts; | 
|  | 47 | } | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 48 | return __ppoll(fds, fd_count, ts_ptr, nullptr, 0); | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
| Elliott Hughes | e3614f0 | 2024-08-13 13:14:39 +0000 | [diff] [blame] | 51 | // The underlying ppoll(2) system call only takes `sigset64_t`. | 
|  | 52 | #if defined(__LP64__) | 
|  | 53 | // That's fine for LP64 where `sigset_t` and `sigset64_t` are the same. | 
|  | 54 | __strong_alias(ppoll, ppoll64); | 
|  | 55 | #else | 
|  | 56 | // ILP32 needs a shim. | 
| George Burgess IV | 9024235 | 2018-02-06 12:51:31 -0800 | [diff] [blame] | 57 | int ppoll(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset_t* ss) { | 
| Elliott Hughes | 215baed | 2023-07-17 17:15:01 -0700 | [diff] [blame] | 58 | SigSetConverter set{ss}; | 
|  | 59 | return ppoll64(fds, fd_count, ts, set.ptr); | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 60 | } | 
| Elliott Hughes | e3614f0 | 2024-08-13 13:14:39 +0000 | [diff] [blame] | 61 | #endif | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 62 |  | 
|  | 63 | int ppoll64(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset64_t* ss) { | 
|  | 64 | // The underlying __ppoll system call modifies its `struct timespec` argument. | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 65 | timespec mutable_ts; | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 66 | timespec* mutable_ts_ptr = nullptr; | 
|  | 67 | if (ts != nullptr) { | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 68 | mutable_ts = *ts; | 
|  | 69 | mutable_ts_ptr = &mutable_ts; | 
|  | 70 | } | 
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 71 |  | 
|  | 72 | sigset64_t mutable_ss; | 
|  | 73 | sigset64_t* mutable_ss_ptr = nullptr; | 
|  | 74 | if (ss != nullptr) { | 
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 75 | mutable_ss = filter_reserved_signals(*ss, SIG_SETMASK); | 
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 76 | mutable_ss_ptr = &mutable_ss; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | return __ppoll(fds, fd_count, mutable_ts_ptr, mutable_ss_ptr, sizeof(*mutable_ss_ptr)); | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
|  | 82 | int select(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds, timeval* tv) { | 
|  | 83 | timespec ts; | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 84 | timespec* ts_ptr = nullptr; | 
|  | 85 | if (tv != nullptr) { | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 86 | if (!timespec_from_timeval(ts, *tv)) { | 
|  | 87 | errno = EINVAL; | 
|  | 88 | return -1; | 
|  | 89 | } | 
|  | 90 | ts_ptr = &ts; | 
|  | 91 | } | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 92 | int result = __pselect6(fd_count, read_fds, write_fds, error_fds, ts_ptr, nullptr); | 
|  | 93 | if (tv != nullptr) { | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 94 | timeval_from_timespec(*tv, ts); | 
|  | 95 | } | 
|  | 96 | return result; | 
|  | 97 | } | 
|  | 98 |  | 
| Elliott Hughes | e3614f0 | 2024-08-13 13:14:39 +0000 | [diff] [blame] | 99 | // The underlying pselect6(2) system call only takes `sigset64_t`. | 
|  | 100 | #if defined(__LP64__) | 
|  | 101 | // That's fine for LP64 where `sigset_t` and `sigset64_t` are the same. | 
|  | 102 | __strong_alias(pselect, pselect64); | 
|  | 103 | #else | 
|  | 104 | // ILP32 needs a shim. | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 105 | int pselect(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds, | 
|  | 106 | const timespec* ts, const sigset_t* ss) { | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 107 | // The underlying `__pselect6` system call only takes `sigset64_t`. | 
| Elliott Hughes | 215baed | 2023-07-17 17:15:01 -0700 | [diff] [blame] | 108 | SigSetConverter set{ss}; | 
|  | 109 | return pselect64(fd_count, read_fds, write_fds, error_fds, ts, set.ptr); | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 110 | } | 
| Elliott Hughes | e3614f0 | 2024-08-13 13:14:39 +0000 | [diff] [blame] | 111 | #endif | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 112 |  | 
|  | 113 | int pselect64(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds, | 
|  | 114 | const timespec* ts, const sigset64_t* ss) { | 
|  | 115 | // The underlying __pselect6 system call modifies its `struct timespec` argument. | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 116 | timespec mutable_ts; | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 117 | timespec* mutable_ts_ptr = nullptr; | 
|  | 118 | if (ts != nullptr) { | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 119 | mutable_ts = *ts; | 
|  | 120 | mutable_ts_ptr = &mutable_ts; | 
|  | 121 | } | 
|  | 122 |  | 
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 123 | sigset64_t mutable_ss; | 
|  | 124 | sigset64_t* mutable_ss_ptr = nullptr; | 
|  | 125 | if (ss != nullptr) { | 
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 126 | mutable_ss = filter_reserved_signals(*ss, SIG_SETMASK); | 
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 127 | mutable_ss_ptr = &mutable_ss; | 
|  | 128 | } | 
|  | 129 |  | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 130 | // The Linux kernel only handles 6 arguments and this system call really needs 7, | 
|  | 131 | // so the last argument is a void* pointing to: | 
|  | 132 | struct pselect6_extra_data_t { | 
|  | 133 | uintptr_t ss_addr; | 
|  | 134 | size_t ss_len; | 
|  | 135 | }; | 
|  | 136 | pselect6_extra_data_t extra_data; | 
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 137 | extra_data.ss_addr = reinterpret_cast<uintptr_t>(mutable_ss_ptr); | 
|  | 138 | extra_data.ss_len = sizeof(*mutable_ss_ptr); | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 139 |  | 
|  | 140 | return __pselect6(fd_count, read_fds, write_fds, error_fds, mutable_ts_ptr, &extra_data); | 
|  | 141 | } |