blob: a7227b0711a295a0f0acfa64c105080135f21a3f [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 Hughes3503ce22013-11-05 13:28:36 -080028
Elliott Hughescfac8d02019-10-02 14:12:31 -070029#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 Project1dc9e472009-03-03 19:28:35 -080035
Yabin Cuidb499032014-12-09 20:15:48 -080036#include <sys/cdefs.h>
37#include <sys/types.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038
Josh Gao16016df2016-11-07 18:27:16 -080039#include <linux/time.h>
40#include <signal.h>
41
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042__BEGIN_DECLS
43
Elliott Hughesa6714d12017-12-04 14:16:38 -080044typedef unsigned long fd_mask;
45
Elliott Hughescfac8d02019-10-02 14:12:31 -070046/**
Elliott Hughes61f6dbf2024-07-30 18:49:33 +000047 * The limit on the largest fd that can be used with fd_set.
Elliott Hughescfac8d02019-10-02 14:12:31 -070048 * Use <poll.h> instead.
49 */
Elliott Hughes3503ce22013-11-05 13:28:36 -080050#define FD_SETSIZE 1024
Elliott Hughesa6714d12017-12-04 14:16:38 -080051#define NFDBITS (8 * sizeof(fd_mask))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
Elliott Hughescfac8d02019-10-02 14:12:31 -070053/**
54 * The type of a file descriptor set. Limited to 1024 fds.
Elliott Hughes61f6dbf2024-07-30 18:49:33 +000055 * The underlying system calls do not have this limit,
56 * and callers can allocate their own sets with calloc().
57 *
Elliott Hughescfac8d02019-10-02 14:12:31 -070058 * Use <poll.h> instead.
59 */
Elliott Hughesd3e64a32013-09-30 17:41:08 -070060typedef struct {
Elliott Hughesa6714d12017-12-04 14:16:38 -080061 fd_mask fds_bits[FD_SETSIZE/NFDBITS];
Elliott Hughesd3e64a32013-09-30 17:41:08 -070062} fd_set;
63
Elliott Hughes3503ce22013-11-05 13:28:36 -080064#define __FDELT(fd) ((fd) / NFDBITS)
65#define __FDMASK(fd) (1UL << ((fd) % NFDBITS))
Elliott Hughes61f6dbf2024-07-30 18:49:33 +000066#define __FDS_BITS(type, set) (__BIONIC_CAST(static_cast, type, set)->fds_bits)
Nick Kralevich7943df62013-10-03 14:08:39 -070067
Elliott Hughes655e4302023-06-16 12:39:33 -070068void __FD_CLR_chk(int, fd_set* _Nonnull , size_t);
69void __FD_SET_chk(int, fd_set* _Nonnull, size_t);
70int __FD_ISSET_chk(int, const fd_set* _Nonnull, size_t);
Dan Albert658727e2014-10-07 11:10:36 -070071
Elliott Hughes61f6dbf2024-07-30 18:49:33 +000072/** FD_CLR() with no bounds checking for users that allocated their own set. */
73#define __FD_CLR(fd, set) (__FDS_BITS(fd_set*, set)[__FDELT(fd)] &= ~__FDMASK(fd))
74/** FD_SET() with no bounds checking for users that allocated their own set. */
75#define __FD_SET(fd, set) (__FDS_BITS(fd_set*, set)[__FDELT(fd)] |= __FDMASK(fd))
76/** FD_ISSET() with no bounds checking for users that allocated their own set. */
77#define __FD_ISSET(fd, set) ((__FDS_BITS(const fd_set*, set)[__FDELT(fd)] & __FDMASK(fd)) != 0)
Andreas Gampe00a6d5f2018-04-13 13:52:10 -070078
Elliott Hughes61f6dbf2024-07-30 18:49:33 +000079/** Removes all 1024 fds from the given set. Use <poll.h> instead. */
80#define FD_ZERO(set) __builtin_memset(set, 0, sizeof(*__BIONIC_CAST(static_cast, const fd_set*, set)))
81
82/** Removes `fd` from the given set. Limited to fds under 1024. Use <poll.h> instead. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080083#define FD_CLR(fd, set) __FD_CLR_chk(fd, set, __bos(set))
Elliott Hughes61f6dbf2024-07-30 18:49:33 +000084/** Adds `fd` to the given set. Limited to fds under 1024. Use <poll.h> instead. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080085#define FD_SET(fd, set) __FD_SET_chk(fd, set, __bos(set))
Elliott Hughes61f6dbf2024-07-30 18:49:33 +000086/** Tests whether `fd` is in the given set. Limited to fds under 1024. Use <poll.h> instead. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080087#define FD_ISSET(fd, set) __FD_ISSET_chk(fd, set, __bos(set))
Elliott Hughescfac8d02019-10-02 14:12:31 -070088
Elliott Hughescfac8d02019-10-02 14:12:31 -070089/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000090 * [select(2)](https://man7.org/linux/man-pages/man2/select.2.html) waits on a
Elliott Hughesc849ef12021-06-17 14:41:29 -070091 * set of file descriptors.
92 *
93 * Use poll() instead.
Elliott Hughescfac8d02019-10-02 14:12:31 -070094 *
95 * Returns the number of ready file descriptors on success, 0 for timeout,
96 * and returns -1 and sets `errno` on failure.
97 */
zijunzhao271abeb2023-04-20 01:19:03 +000098int 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 Hughescfac8d02019-10-02 14:12:31 -070099
100/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000101 * [pselect(2)](https://man7.org/linux/man-pages/man2/select.2.html) waits on a
Elliott Hughesc849ef12021-06-17 14:41:29 -0700102 * set of file descriptors.
103 *
104 * Use ppoll() instead.
Elliott Hughescfac8d02019-10-02 14:12:31 -0700105 *
106 * Returns the number of ready file descriptors on success, 0 for timeout,
107 * and returns -1 and sets `errno` on failure.
108 */
zijunzhao271abeb2023-04-20 01:19:03 +0000109int 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 Hughescfac8d02019-10-02 14:12:31 -0700110
111/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000112 * [pselect64(2)](https://man7.org/linux/man-pages/man2/select.2.html) waits on a
Elliott Hughesc849ef12021-06-17 14:41:29 -0700113 * set of file descriptors.
114 *
115 * Use ppoll64() instead.
Elliott Hughescfac8d02019-10-02 14:12:31 -0700116 *
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 */
Dan Albert02ce4012024-10-25 19:13:49 +0000122
123#if __BIONIC_AVAILABILITY_GUARD(28)
zijunzhao271abeb2023-04-20 01:19:03 +0000124int 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 Albert02ce4012024-10-25 19:13:49 +0000125#endif /* __BIONIC_AVAILABILITY_GUARD(28) */
126
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127
128__END_DECLS