The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 29 | #pragma once |
| 30 | |
| 31 | /** |
| 32 | * @file sys/select.h |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 33 | * @brief Wait for events on a set of file descriptors. |
| 34 | * New code should prefer the different interface specified in <poll.h> instead, |
| 35 | * because it scales better and easily avoids the limits on `fd_set` size. |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 36 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 37 | |
Yabin Cui | db49903 | 2014-12-09 20:15:48 -0800 | [diff] [blame] | 38 | #include <sys/cdefs.h> |
| 39 | #include <sys/types.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 41 | #include <linux/time.h> |
| 42 | #include <signal.h> |
| 43 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 44 | __BEGIN_DECLS |
| 45 | |
Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 46 | typedef unsigned long fd_mask; |
| 47 | |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 48 | /** |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 49 | * The limit on the largest fd that can be used with type `fd_set`. |
| 50 | * You can allocate your own memory, |
| 51 | * but new code should prefer the different interface specified in <poll.h> instead, |
| 52 | * because it scales better and easily avoids the limits on `fd_set` size. |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 53 | */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 54 | #define FD_SETSIZE 1024 |
Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 55 | #define NFDBITS (8 * sizeof(fd_mask)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 56 | |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 57 | /** |
| 58 | * The type of a file descriptor set. Limited to 1024 fds. |
Elliott Hughes | 61f6dbf | 2024-07-30 18:49:33 +0000 | [diff] [blame] | 59 | * The underlying system calls do not have this limit, |
| 60 | * and callers can allocate their own sets with calloc(). |
| 61 | * |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 62 | * New code should prefer the different interface specified in <poll.h> instead, |
| 63 | * because it scales better and easily avoids the limits on `fd_set` size. |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 64 | */ |
Elliott Hughes | d3e64a3 | 2013-09-30 17:41:08 -0700 | [diff] [blame] | 65 | typedef struct { |
Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 66 | fd_mask fds_bits[FD_SETSIZE/NFDBITS]; |
Elliott Hughes | d3e64a3 | 2013-09-30 17:41:08 -0700 | [diff] [blame] | 67 | } fd_set; |
| 68 | |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 69 | #define __FDELT(fd) ((fd) / NFDBITS) |
| 70 | #define __FDMASK(fd) (1UL << ((fd) % NFDBITS)) |
Elliott Hughes | 61f6dbf | 2024-07-30 18:49:33 +0000 | [diff] [blame] | 71 | #define __FDS_BITS(type, set) (__BIONIC_CAST(static_cast, type, set)->fds_bits) |
Nick Kralevich | 7943df6 | 2013-10-03 14:08:39 -0700 | [diff] [blame] | 72 | |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 73 | void __FD_CLR_chk(int, fd_set* _Nonnull , size_t); |
| 74 | void __FD_SET_chk(int, fd_set* _Nonnull, size_t); |
| 75 | int __FD_ISSET_chk(int, const fd_set* _Nonnull, size_t); |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 77 | /** |
| 78 | * FD_CLR() with no bounds checking for users that allocated their own set. |
| 79 | * New code should prefer <poll.h> instead. |
| 80 | */ |
Elliott Hughes | 61f6dbf | 2024-07-30 18:49:33 +0000 | [diff] [blame] | 81 | #define __FD_CLR(fd, set) (__FDS_BITS(fd_set*, set)[__FDELT(fd)] &= ~__FDMASK(fd)) |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * FD_SET() with no bounds checking for users that allocated their own set. |
| 85 | * New code should prefer <poll.h> instead. |
| 86 | */ |
Elliott Hughes | 61f6dbf | 2024-07-30 18:49:33 +0000 | [diff] [blame] | 87 | #define __FD_SET(fd, set) (__FDS_BITS(fd_set*, set)[__FDELT(fd)] |= __FDMASK(fd)) |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * FD_ISSET() with no bounds checking for users that allocated their own set. |
| 91 | * New code should prefer <poll.h> instead. |
| 92 | */ |
Elliott Hughes | 61f6dbf | 2024-07-30 18:49:33 +0000 | [diff] [blame] | 93 | #define __FD_ISSET(fd, set) ((__FDS_BITS(const fd_set*, set)[__FDELT(fd)] & __FDMASK(fd)) != 0) |
Andreas Gampe | 00a6d5f | 2018-04-13 13:52:10 -0700 | [diff] [blame] | 94 | |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Removes all 1024 fds from the given set. |
| 97 | * Limited to fds under 1024. |
| 98 | * New code should prefer <poll.h> instead for this reason, |
| 99 | * rather than using memset() directly. |
| 100 | */ |
Elliott Hughes | 61f6dbf | 2024-07-30 18:49:33 +0000 | [diff] [blame] | 101 | #define FD_ZERO(set) __builtin_memset(set, 0, sizeof(*__BIONIC_CAST(static_cast, const fd_set*, set))) |
| 102 | |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 103 | /** |
| 104 | * Removes `fd` from the given set. |
| 105 | * Limited to fds under 1024. |
| 106 | * New code should prefer <poll.h> instead for this reason, |
| 107 | * rather than using __FD_CLR(). |
| 108 | */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 109 | #define FD_CLR(fd, set) __FD_CLR_chk(fd, set, __bos(set)) |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Adds `fd` to the given set. |
| 113 | * Limited to fds under 1024. |
| 114 | * New code should prefer <poll.h> instead for this reason, |
| 115 | * rather than using __FD_SET(). |
| 116 | */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 117 | #define FD_SET(fd, set) __FD_SET_chk(fd, set, __bos(set)) |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Tests whether `fd` is in the given set. |
| 121 | * Limited to fds under 1024. |
| 122 | * New code should prefer <poll.h> instead for this reason, |
| 123 | * rather than using __FD_ISSET(). |
| 124 | */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 125 | #define FD_ISSET(fd, set) __FD_ISSET_chk(fd, set, __bos(set)) |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 126 | |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 127 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 128 | * [select(2)](https://man7.org/linux/man-pages/man2/select.2.html) waits on a |
Elliott Hughes | c849ef1 | 2021-06-17 14:41:29 -0700 | [diff] [blame] | 129 | * set of file descriptors. |
| 130 | * |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 131 | * New code should prefer poll() from <poll.h> instead, |
| 132 | * because it scales better and easily avoids the limits on `fd_set` size. |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 133 | * |
| 134 | * Returns the number of ready file descriptors on success, 0 for timeout, |
| 135 | * and returns -1 and sets `errno` on failure. |
| 136 | */ |
zijunzhao | 271abeb | 2023-04-20 01:19:03 +0000 | [diff] [blame] | 137 | int select(int __max_fd_plus_one, fd_set* _Nullable __read_fds, fd_set* _Nullable __write_fds, fd_set* _Nullable __exception_fds, struct timeval* _Nullable __timeout); |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 138 | |
| 139 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 140 | * [pselect(2)](https://man7.org/linux/man-pages/man2/select.2.html) waits on a |
Elliott Hughes | c849ef1 | 2021-06-17 14:41:29 -0700 | [diff] [blame] | 141 | * set of file descriptors. |
| 142 | * |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 143 | * New code should prefer ppoll() from <poll.h> instead, |
| 144 | * because it scales better and easily avoids the limits on `fd_set` size. |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 145 | * |
| 146 | * Returns the number of ready file descriptors on success, 0 for timeout, |
| 147 | * and returns -1 and sets `errno` on failure. |
| 148 | */ |
zijunzhao | 271abeb | 2023-04-20 01:19:03 +0000 | [diff] [blame] | 149 | int pselect(int __max_fd_plus_one, fd_set* _Nullable __read_fds, fd_set* _Nullable __write_fds, fd_set* _Nullable __exception_fds, const struct timespec* _Nullable __timeout, const sigset_t* _Nullable __mask); |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 150 | |
| 151 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 152 | * [pselect64(2)](https://man7.org/linux/man-pages/man2/select.2.html) waits on a |
Elliott Hughes | c849ef1 | 2021-06-17 14:41:29 -0700 | [diff] [blame] | 153 | * set of file descriptors. |
| 154 | * |
Elliott Hughes | b6b3cce | 2024-12-12 14:31:58 -0800 | [diff] [blame] | 155 | * New code should prefer ppoll64() from <poll.h> instead, |
| 156 | * because it scales better and easily avoids the limits on `fd_set` size. |
Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 157 | * |
| 158 | * Returns the number of ready file descriptors on success, 0 for timeout, |
| 159 | * and returns -1 and sets `errno` on failure. |
| 160 | * |
| 161 | * Available since API level 28. |
| 162 | */ |
Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame] | 163 | #if __BIONIC_AVAILABILITY_GUARD(28) |
zijunzhao | 271abeb | 2023-04-20 01:19:03 +0000 | [diff] [blame] | 164 | int pselect64(int __max_fd_plus_one, fd_set* _Nullable __read_fds, fd_set* _Nullable __write_fds, fd_set* _Nullable __exception_fds, const struct timespec* _Nullable __timeout, const sigset64_t* _Nullable __mask) __INTRODUCED_IN(28); |
Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame] | 165 | #endif /* __BIONIC_AVAILABILITY_GUARD(28) */ |
| 166 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 167 | __END_DECLS |