1 /*
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
29 #include <errno.h>
30 #include <sys/poll.h>
31 #include <sys/select.h>
32
33 #include <platform/bionic/reserved_signals.h>
34
35 #include "private/bionic_time_conversions.h"
36 #include "private/SigSetConverter.h"
37
38 extern "C" int __ppoll(pollfd*, unsigned int, timespec*, const sigset64_t*, size_t);
39 extern "C" int __pselect6(int, fd_set*, fd_set*, fd_set*, timespec*, void*);
40
poll(pollfd * fds,nfds_t fd_count,int ms)41 int poll(pollfd* fds, nfds_t fd_count, int ms) {
42 timespec ts;
43 timespec* ts_ptr = nullptr;
44 if (ms >= 0) {
45 timespec_from_ms(ts, ms);
46 ts_ptr = &ts;
47 }
48 return __ppoll(fds, fd_count, ts_ptr, nullptr, 0);
49 }
50
ppoll(pollfd * fds,nfds_t fd_count,const timespec * ts,const sigset_t * ss)51 int ppoll(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset_t* ss) {
52 // The underlying `__ppoll` system call only takes `sigset64_t`.
53 SigSetConverter set;
54 sigset64_t* ss_ptr = nullptr;
55 if (ss != nullptr) {
56 set = {};
57 set.sigset = *ss;
58 ss_ptr = &set.sigset64;
59 }
60 return ppoll64(fds, fd_count, ts, ss_ptr);
61 }
62
ppoll64(pollfd * fds,nfds_t fd_count,const timespec * ts,const sigset64_t * ss)63 int ppoll64(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset64_t* ss) {
64 // The underlying __ppoll system call modifies its `struct timespec` argument.
65 timespec mutable_ts;
66 timespec* mutable_ts_ptr = nullptr;
67 if (ts != nullptr) {
68 mutable_ts = *ts;
69 mutable_ts_ptr = &mutable_ts;
70 }
71
72 sigset64_t mutable_ss;
73 sigset64_t* mutable_ss_ptr = nullptr;
74 if (ss != nullptr) {
75 mutable_ss = filter_reserved_signals(*ss, SIG_SETMASK);
76 mutable_ss_ptr = &mutable_ss;
77 }
78
79 return __ppoll(fds, fd_count, mutable_ts_ptr, mutable_ss_ptr, sizeof(*mutable_ss_ptr));
80 }
81
select(int fd_count,fd_set * read_fds,fd_set * write_fds,fd_set * error_fds,timeval * tv)82 int select(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds, timeval* tv) {
83 timespec ts;
84 timespec* ts_ptr = nullptr;
85 if (tv != nullptr) {
86 if (!timespec_from_timeval(ts, *tv)) {
87 errno = EINVAL;
88 return -1;
89 }
90 ts_ptr = &ts;
91 }
92 int result = __pselect6(fd_count, read_fds, write_fds, error_fds, ts_ptr, nullptr);
93 if (tv != nullptr) {
94 timeval_from_timespec(*tv, ts);
95 }
96 return result;
97 }
98
pselect(int fd_count,fd_set * read_fds,fd_set * write_fds,fd_set * error_fds,const timespec * ts,const sigset_t * ss)99 int pselect(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds,
100 const timespec* ts, const sigset_t* ss) {
101 // The underlying `__pselect6` system call only takes `sigset64_t`.
102 SigSetConverter set;
103 sigset64_t* ss_ptr = nullptr;
104 if (ss != nullptr) {
105 set = {};
106 set.sigset = *ss;
107 ss_ptr = &set.sigset64;
108 }
109 return pselect64(fd_count, read_fds, write_fds, error_fds, ts, ss_ptr);
110 }
111
pselect64(int fd_count,fd_set * read_fds,fd_set * write_fds,fd_set * error_fds,const timespec * ts,const sigset64_t * ss)112 int pselect64(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds,
113 const timespec* ts, const sigset64_t* ss) {
114 // The underlying __pselect6 system call modifies its `struct timespec` argument.
115 timespec mutable_ts;
116 timespec* mutable_ts_ptr = nullptr;
117 if (ts != nullptr) {
118 mutable_ts = *ts;
119 mutable_ts_ptr = &mutable_ts;
120 }
121
122 sigset64_t mutable_ss;
123 sigset64_t* mutable_ss_ptr = nullptr;
124 if (ss != nullptr) {
125 mutable_ss = filter_reserved_signals(*ss, SIG_SETMASK);
126 mutable_ss_ptr = &mutable_ss;
127 }
128
129 // The Linux kernel only handles 6 arguments and this system call really needs 7,
130 // so the last argument is a void* pointing to:
131 struct pselect6_extra_data_t {
132 uintptr_t ss_addr;
133 size_t ss_len;
134 };
135 pselect6_extra_data_t extra_data;
136 extra_data.ss_addr = reinterpret_cast<uintptr_t>(mutable_ss_ptr);
137 extra_data.ss_len = sizeof(*mutable_ss_ptr);
138
139 return __pselect6(fd_count, read_fds, write_fds, error_fds, mutable_ts_ptr, &extra_data);
140 }
141