| 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 | 
|  | 33 | * @brief Wait for events on a set of file descriptors (but use <poll.h> instead). | 
|  | 34 | */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 |  | 
| Yabin Cui | db49903 | 2014-12-09 20:15:48 -0800 | [diff] [blame] | 36 | #include <sys/cdefs.h> | 
|  | 37 | #include <sys/types.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 38 |  | 
| Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 39 | #include <linux/time.h> | 
|  | 40 | #include <signal.h> | 
|  | 41 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 | __BEGIN_DECLS | 
|  | 43 |  | 
| Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 44 | typedef unsigned long fd_mask; | 
|  | 45 |  | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 46 | /** | 
|  | 47 | * The limit on the largest fd that can be used with this API. | 
|  | 48 | * Use <poll.h> instead. | 
|  | 49 | */ | 
| Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 50 | #define FD_SETSIZE 1024 | 
| Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 51 | #define NFDBITS (8 * sizeof(fd_mask)) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 52 |  | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 53 | /** | 
|  | 54 | * The type of a file descriptor set. Limited to 1024 fds. | 
|  | 55 | * Use <poll.h> instead. | 
|  | 56 | */ | 
| Elliott Hughes | d3e64a3 | 2013-09-30 17:41:08 -0700 | [diff] [blame] | 57 | typedef struct { | 
| Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 58 | fd_mask fds_bits[FD_SETSIZE/NFDBITS]; | 
| Elliott Hughes | d3e64a3 | 2013-09-30 17:41:08 -0700 | [diff] [blame] | 59 | } fd_set; | 
|  | 60 |  | 
| Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 61 | #define __FDELT(fd) ((fd) / NFDBITS) | 
|  | 62 | #define __FDMASK(fd) (1UL << ((fd) % NFDBITS)) | 
| Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 63 | #define __FDS_BITS(type,set) (__BIONIC_CAST(static_cast, type, set)->fds_bits) | 
| Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 64 |  | 
| Elliott Hughes | 5038b19 | 2015-01-28 21:02:34 -0800 | [diff] [blame] | 65 | /* Inline loop so we don't have to declare memset. */ | 
|  | 66 | #define FD_ZERO(set) \ | 
|  | 67 | do { \ | 
|  | 68 | size_t __i; \ | 
| Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 69 | for (__i = 0; __i < sizeof(fd_set)/sizeof(fd_mask); ++__i) { \ | 
| Elliott Hughes | 5038b19 | 2015-01-28 21:02:34 -0800 | [diff] [blame] | 70 | (set)->fds_bits[__i] = 0; \ | 
|  | 71 | } \ | 
|  | 72 | } while (0) | 
| Nick Kralevich | 7943df6 | 2013-10-03 14:08:39 -0700 | [diff] [blame] | 73 |  | 
| Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 74 | void __FD_CLR_chk(int, fd_set*, size_t) __INTRODUCED_IN(21); | 
|  | 75 | void __FD_SET_chk(int, fd_set*, size_t) __INTRODUCED_IN(21); | 
| Elliott Hughes | a6714d1 | 2017-12-04 14:16:38 -0800 | [diff] [blame] | 76 | int __FD_ISSET_chk(int, const fd_set*, size_t) __INTRODUCED_IN(21); | 
| Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 77 |  | 
| Andreas Gampe | 00a6d5f | 2018-04-13 13:52:10 -0700 | [diff] [blame] | 78 | #define __FD_CLR(fd, set) (__FDS_BITS(fd_set*,set)[__FDELT(fd)] &= ~__FDMASK(fd)) | 
|  | 79 | #define __FD_SET(fd, set) (__FDS_BITS(fd_set*,set)[__FDELT(fd)] |= __FDMASK(fd)) | 
|  | 80 | #define __FD_ISSET(fd, set) ((__FDS_BITS(const fd_set*,set)[__FDELT(fd)] & __FDMASK(fd)) != 0) | 
|  | 81 |  | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 82 | /** Removes `fd` from the given set. Use <poll.h> instead. */ | 
| Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 83 | #define FD_CLR(fd, set) __FD_CLR_chk(fd, set, __bos(set)) | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 84 | /** Adds `fd` to the given set. Use <poll.h> instead. */ | 
| Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 85 | #define FD_SET(fd, set) __FD_SET_chk(fd, set, __bos(set)) | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 86 | /** Tests whether `fd` is in the given set. Use <poll.h> instead. */ | 
| Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 87 | #define FD_ISSET(fd, set) __FD_ISSET_chk(fd, set, __bos(set)) | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 88 |  | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 89 | /** | 
|  | 90 | * [select(2)](http://man7.org/linux/man-pages/man2/select.2.html) waits on a | 
| Elliott Hughes | c849ef1 | 2021-06-17 14:41:29 -0700 | [diff] [blame] | 91 | * set of file descriptors. | 
|  | 92 | * | 
|  | 93 | * Use poll() instead. | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 94 | * | 
|  | 95 | * Returns the number of ready file descriptors on success, 0 for timeout, | 
|  | 96 | * and returns -1 and sets `errno` on failure. | 
|  | 97 | */ | 
| Elliott Hughes | c849ef1 | 2021-06-17 14:41:29 -0700 | [diff] [blame] | 98 | int select(int __max_fd_plus_one, fd_set* __read_fds, fd_set* __write_fds, fd_set* __exception_fds, struct timeval* __timeout); | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 99 |  | 
|  | 100 | /** | 
|  | 101 | * [pselect(2)](http://man7.org/linux/man-pages/man2/select.2.html) waits on a | 
| Elliott Hughes | c849ef1 | 2021-06-17 14:41:29 -0700 | [diff] [blame] | 102 | * set of file descriptors. | 
|  | 103 | * | 
|  | 104 | * Use ppoll() instead. | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 105 | * | 
|  | 106 | * Returns the number of ready file descriptors on success, 0 for timeout, | 
|  | 107 | * and returns -1 and sets `errno` on failure. | 
|  | 108 | */ | 
| Elliott Hughes | c849ef1 | 2021-06-17 14:41:29 -0700 | [diff] [blame] | 109 | int pselect(int __max_fd_plus_one, fd_set* __read_fds, fd_set* __write_fds, fd_set* __exception_fds, const struct timespec* __timeout, const sigset_t* __mask); | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 110 |  | 
|  | 111 | /** | 
|  | 112 | * [pselect64(2)](http://man7.org/linux/man-pages/man2/select.2.html) waits on a | 
| Elliott Hughes | c849ef1 | 2021-06-17 14:41:29 -0700 | [diff] [blame] | 113 | * set of file descriptors. | 
|  | 114 | * | 
|  | 115 | * Use ppoll64() instead. | 
| Elliott Hughes | cfac8d0 | 2019-10-02 14:12:31 -0700 | [diff] [blame] | 116 | * | 
|  | 117 | * Returns the number of ready file descriptors on success, 0 for timeout, | 
|  | 118 | * and returns -1 and sets `errno` on failure. | 
|  | 119 | * | 
|  | 120 | * Available since API level 28. | 
|  | 121 | */ | 
| Elliott Hughes | c849ef1 | 2021-06-17 14:41:29 -0700 | [diff] [blame] | 122 | int pselect64(int __max_fd_plus_one, fd_set* __read_fds, fd_set* __write_fds, fd_set* __exception_fds, const struct timespec* __timeout, const sigset64_t* __mask) __INTRODUCED_IN(28); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 123 |  | 
|  | 124 | __END_DECLS |