blob: 41b26571ba38b936ab7814233c3d402b5c38b7b2 [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
33#include "private/bionic_time_conversions.h"
Josh Gao6fcba932018-02-09 13:38:32 -080034#include "private/sigrtmin.h"
Elliott Hughes5905d6f2018-01-30 15:09:51 -080035#include "private/SigSetConverter.h"
Elliott Hughes11952072013-10-24 15:15:14 -070036
Elliott Hughes5905d6f2018-01-30 15:09:51 -080037extern "C" int __ppoll(pollfd*, unsigned int, timespec*, const sigset64_t*, size_t);
Elliott Hughes11952072013-10-24 15:15:14 -070038extern "C" int __pselect6(int, fd_set*, fd_set*, fd_set*, timespec*, void*);
39
George Burgess IV90242352018-02-06 12:51:31 -080040int poll(pollfd* fds, nfds_t fd_count, int ms) {
Elliott Hughes11952072013-10-24 15:15:14 -070041 timespec ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080042 timespec* ts_ptr = nullptr;
Elliott Hughes11952072013-10-24 15:15:14 -070043 if (ms >= 0) {
44 timespec_from_ms(ts, ms);
45 ts_ptr = &ts;
46 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -080047 return __ppoll(fds, fd_count, ts_ptr, nullptr, 0);
Elliott Hughes11952072013-10-24 15:15:14 -070048}
49
George Burgess IV90242352018-02-06 12:51:31 -080050int ppoll(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset_t* ss) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -080051 // The underlying `__ppoll` system call only takes `sigset64_t`.
52 SigSetConverter set;
53 sigset64_t* ss_ptr = nullptr;
54 if (ss != nullptr) {
55 set = {};
56 set.sigset = *ss;
57 ss_ptr = &set.sigset64;
58 }
59 return ppoll64(fds, fd_count, ts, ss_ptr);
60}
61
62int ppoll64(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset64_t* ss) {
63 // The underlying __ppoll system call modifies its `struct timespec` argument.
Elliott Hughes11952072013-10-24 15:15:14 -070064 timespec mutable_ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080065 timespec* mutable_ts_ptr = nullptr;
66 if (ts != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -070067 mutable_ts = *ts;
68 mutable_ts_ptr = &mutable_ts;
69 }
Josh Gao6fcba932018-02-09 13:38:32 -080070
71 sigset64_t mutable_ss;
72 sigset64_t* mutable_ss_ptr = nullptr;
73 if (ss != nullptr) {
Josh Gaobaf20fc2018-10-08 17:28:07 -070074 mutable_ss = filter_reserved_signals(*ss, SIG_SETMASK);
Josh Gao6fcba932018-02-09 13:38:32 -080075 mutable_ss_ptr = &mutable_ss;
76 }
77
78 return __ppoll(fds, fd_count, mutable_ts_ptr, mutable_ss_ptr, sizeof(*mutable_ss_ptr));
Elliott Hughes11952072013-10-24 15:15:14 -070079}
80
81int select(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds, timeval* tv) {
82 timespec ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080083 timespec* ts_ptr = nullptr;
84 if (tv != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -070085 if (!timespec_from_timeval(ts, *tv)) {
86 errno = EINVAL;
87 return -1;
88 }
89 ts_ptr = &ts;
90 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -080091 int result = __pselect6(fd_count, read_fds, write_fds, error_fds, ts_ptr, nullptr);
92 if (tv != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -070093 timeval_from_timespec(*tv, ts);
94 }
95 return result;
96}
97
98int pselect(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds,
99 const timespec* ts, const sigset_t* ss) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800100 // The underlying `__pselect6` system call only takes `sigset64_t`.
101 SigSetConverter set;
102 sigset64_t* ss_ptr = nullptr;
103 if (ss != nullptr) {
104 set = {};
105 set.sigset = *ss;
106 ss_ptr = &set.sigset64;
107 }
108 return pselect64(fd_count, read_fds, write_fds, error_fds, ts, ss_ptr);
109}
110
111int pselect64(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds,
112 const timespec* ts, const sigset64_t* ss) {
113 // The underlying __pselect6 system call modifies its `struct timespec` argument.
Elliott Hughes11952072013-10-24 15:15:14 -0700114 timespec mutable_ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800115 timespec* mutable_ts_ptr = nullptr;
116 if (ts != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -0700117 mutable_ts = *ts;
118 mutable_ts_ptr = &mutable_ts;
119 }
120
Josh Gao6fcba932018-02-09 13:38:32 -0800121 sigset64_t mutable_ss;
122 sigset64_t* mutable_ss_ptr = nullptr;
123 if (ss != nullptr) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700124 mutable_ss = filter_reserved_signals(*ss, SIG_SETMASK);
Josh Gao6fcba932018-02-09 13:38:32 -0800125 mutable_ss_ptr = &mutable_ss;
126 }
127
Elliott Hughes11952072013-10-24 15:15:14 -0700128 // The Linux kernel only handles 6 arguments and this system call really needs 7,
129 // so the last argument is a void* pointing to:
130 struct pselect6_extra_data_t {
131 uintptr_t ss_addr;
132 size_t ss_len;
133 };
134 pselect6_extra_data_t extra_data;
Josh Gao6fcba932018-02-09 13:38:32 -0800135 extra_data.ss_addr = reinterpret_cast<uintptr_t>(mutable_ss_ptr);
136 extra_data.ss_len = sizeof(*mutable_ss_ptr);
Elliott Hughes11952072013-10-24 15:15:14 -0700137
138 return __pselect6(fd_count, read_fds, write_fds, error_fds, mutable_ts_ptr, &extra_data);
139}