blob: 7d80b4c7bb1e6edc3c702ad4bbc2505b628be98a [file] [log] [blame]
Elliott Hughes11952072013-10-24 15:15:14 -07001/*
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 Hughes40360b32014-12-29 13:29:50 -080029#include <errno.h>
Elliott Hughes11952072013-10-24 15:15:14 -070030#include <sys/poll.h>
31#include <sys/select.h>
32
Josh Gao5074e7d2019-12-13 13:55:53 -080033#include <platform/bionic/reserved_signals.h>
34
Elliott Hughes11952072013-10-24 15:15:14 -070035#include "private/bionic_time_conversions.h"
Elliott Hughes5905d6f2018-01-30 15:09:51 -080036#include "private/SigSetConverter.h"
Elliott Hughes11952072013-10-24 15:15:14 -070037
Elliott Hughes5905d6f2018-01-30 15:09:51 -080038extern "C" int __ppoll(pollfd*, unsigned int, timespec*, const sigset64_t*, size_t);
Elliott Hughes11952072013-10-24 15:15:14 -070039extern "C" int __pselect6(int, fd_set*, fd_set*, fd_set*, timespec*, void*);
40
George Burgess IV90242352018-02-06 12:51:31 -080041int poll(pollfd* fds, nfds_t fd_count, int ms) {
Elliott Hughes11952072013-10-24 15:15:14 -070042 timespec ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080043 timespec* ts_ptr = nullptr;
Elliott Hughes11952072013-10-24 15:15:14 -070044 if (ms >= 0) {
45 timespec_from_ms(ts, ms);
46 ts_ptr = &ts;
47 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -080048 return __ppoll(fds, fd_count, ts_ptr, nullptr, 0);
Elliott Hughes11952072013-10-24 15:15:14 -070049}
50
George Burgess IV90242352018-02-06 12:51:31 -080051int ppoll(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset_t* ss) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -080052 // The underlying `__ppoll` system call only takes `sigset64_t`.
Elliott Hughes215baed2023-07-17 17:15:01 -070053 SigSetConverter set{ss};
54 return ppoll64(fds, fd_count, ts, set.ptr);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080055}
56
57int ppoll64(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset64_t* ss) {
58 // The underlying __ppoll system call modifies its `struct timespec` argument.
Elliott Hughes11952072013-10-24 15:15:14 -070059 timespec mutable_ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080060 timespec* mutable_ts_ptr = nullptr;
61 if (ts != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -070062 mutable_ts = *ts;
63 mutable_ts_ptr = &mutable_ts;
64 }
Josh Gao6fcba932018-02-09 13:38:32 -080065
66 sigset64_t mutable_ss;
67 sigset64_t* mutable_ss_ptr = nullptr;
68 if (ss != nullptr) {
Josh Gaobaf20fc2018-10-08 17:28:07 -070069 mutable_ss = filter_reserved_signals(*ss, SIG_SETMASK);
Josh Gao6fcba932018-02-09 13:38:32 -080070 mutable_ss_ptr = &mutable_ss;
71 }
72
73 return __ppoll(fds, fd_count, mutable_ts_ptr, mutable_ss_ptr, sizeof(*mutable_ss_ptr));
Elliott Hughes11952072013-10-24 15:15:14 -070074}
75
76int select(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds, timeval* tv) {
77 timespec ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080078 timespec* ts_ptr = nullptr;
79 if (tv != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -070080 if (!timespec_from_timeval(ts, *tv)) {
81 errno = EINVAL;
82 return -1;
83 }
84 ts_ptr = &ts;
85 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -080086 int result = __pselect6(fd_count, read_fds, write_fds, error_fds, ts_ptr, nullptr);
87 if (tv != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -070088 timeval_from_timespec(*tv, ts);
89 }
90 return result;
91}
92
93int pselect(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds,
94 const timespec* ts, const sigset_t* ss) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -080095 // The underlying `__pselect6` system call only takes `sigset64_t`.
Elliott Hughes215baed2023-07-17 17:15:01 -070096 SigSetConverter set{ss};
97 return pselect64(fd_count, read_fds, write_fds, error_fds, ts, set.ptr);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080098}
99
100int pselect64(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds,
101 const timespec* ts, const sigset64_t* ss) {
102 // The underlying __pselect6 system call modifies its `struct timespec` argument.
Elliott Hughes11952072013-10-24 15:15:14 -0700103 timespec mutable_ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800104 timespec* mutable_ts_ptr = nullptr;
105 if (ts != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -0700106 mutable_ts = *ts;
107 mutable_ts_ptr = &mutable_ts;
108 }
109
Josh Gao6fcba932018-02-09 13:38:32 -0800110 sigset64_t mutable_ss;
111 sigset64_t* mutable_ss_ptr = nullptr;
112 if (ss != nullptr) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700113 mutable_ss = filter_reserved_signals(*ss, SIG_SETMASK);
Josh Gao6fcba932018-02-09 13:38:32 -0800114 mutable_ss_ptr = &mutable_ss;
115 }
116
Elliott Hughes11952072013-10-24 15:15:14 -0700117 // The Linux kernel only handles 6 arguments and this system call really needs 7,
118 // so the last argument is a void* pointing to:
119 struct pselect6_extra_data_t {
120 uintptr_t ss_addr;
121 size_t ss_len;
122 };
123 pselect6_extra_data_t extra_data;
Josh Gao6fcba932018-02-09 13:38:32 -0800124 extra_data.ss_addr = reinterpret_cast<uintptr_t>(mutable_ss_ptr);
125 extra_data.ss_len = sizeof(*mutable_ss_ptr);
Elliott Hughes11952072013-10-24 15:15:14 -0700126
127 return __pselect6(fd_count, read_fds, write_fds, error_fds, mutable_ts_ptr, &extra_data);
128}