blob: 1d72fe5b4a3b6e28297e7d2af7d5a674a1e61b40 [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"
Elliott Hughes5905d6f2018-01-30 15:09:51 -080034#include "private/SigSetConverter.h"
Elliott Hughes11952072013-10-24 15:15:14 -070035
Elliott Hughes5905d6f2018-01-30 15:09:51 -080036extern "C" int __ppoll(pollfd*, unsigned int, timespec*, const sigset64_t*, size_t);
Elliott Hughes11952072013-10-24 15:15:14 -070037extern "C" int __pselect6(int, fd_set*, fd_set*, fd_set*, timespec*, void*);
38
George Burgess IV7cc779f2017-02-09 00:00:31 -080039int poll(pollfd* fds, nfds_t fd_count, int ms) __overloadable {
Elliott Hughes11952072013-10-24 15:15:14 -070040 timespec ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080041 timespec* ts_ptr = nullptr;
Elliott Hughes11952072013-10-24 15:15:14 -070042 if (ms >= 0) {
43 timespec_from_ms(ts, ms);
44 ts_ptr = &ts;
45 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -080046 return __ppoll(fds, fd_count, ts_ptr, nullptr, 0);
Elliott Hughes11952072013-10-24 15:15:14 -070047}
48
George Burgess IV7cc779f2017-02-09 00:00:31 -080049int ppoll(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset_t* ss) __overloadable {
Elliott Hughes5905d6f2018-01-30 15:09:51 -080050 // The underlying `__ppoll` system call only takes `sigset64_t`.
51 SigSetConverter set;
52 sigset64_t* ss_ptr = nullptr;
53 if (ss != nullptr) {
54 set = {};
55 set.sigset = *ss;
56 ss_ptr = &set.sigset64;
57 }
58 return ppoll64(fds, fd_count, ts, ss_ptr);
59}
60
61int ppoll64(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset64_t* ss) {
62 // The underlying __ppoll system call modifies its `struct timespec` argument.
Elliott Hughes11952072013-10-24 15:15:14 -070063 timespec mutable_ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080064 timespec* mutable_ts_ptr = nullptr;
65 if (ts != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -070066 mutable_ts = *ts;
67 mutable_ts_ptr = &mutable_ts;
68 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -080069 return __ppoll(fds, fd_count, mutable_ts_ptr, ss, sizeof(*ss));
Elliott Hughes11952072013-10-24 15:15:14 -070070}
71
72int select(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds, timeval* tv) {
73 timespec ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080074 timespec* ts_ptr = nullptr;
75 if (tv != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -070076 if (!timespec_from_timeval(ts, *tv)) {
77 errno = EINVAL;
78 return -1;
79 }
80 ts_ptr = &ts;
81 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -080082 int result = __pselect6(fd_count, read_fds, write_fds, error_fds, ts_ptr, nullptr);
83 if (tv != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -070084 timeval_from_timespec(*tv, ts);
85 }
86 return result;
87}
88
89int pselect(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds,
90 const timespec* ts, const sigset_t* ss) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -080091 // The underlying `__pselect6` system call only takes `sigset64_t`.
92 SigSetConverter set;
93 sigset64_t* ss_ptr = nullptr;
94 if (ss != nullptr) {
95 set = {};
96 set.sigset = *ss;
97 ss_ptr = &set.sigset64;
98 }
99 return pselect64(fd_count, read_fds, write_fds, error_fds, ts, ss_ptr);
100}
101
102int pselect64(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds,
103 const timespec* ts, const sigset64_t* ss) {
104 // The underlying __pselect6 system call modifies its `struct timespec` argument.
Elliott Hughes11952072013-10-24 15:15:14 -0700105 timespec mutable_ts;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800106 timespec* mutable_ts_ptr = nullptr;
107 if (ts != nullptr) {
Elliott Hughes11952072013-10-24 15:15:14 -0700108 mutable_ts = *ts;
109 mutable_ts_ptr = &mutable_ts;
110 }
111
Elliott Hughes11952072013-10-24 15:15:14 -0700112 // The Linux kernel only handles 6 arguments and this system call really needs 7,
113 // so the last argument is a void* pointing to:
114 struct pselect6_extra_data_t {
115 uintptr_t ss_addr;
116 size_t ss_len;
117 };
118 pselect6_extra_data_t extra_data;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800119 extra_data.ss_addr = reinterpret_cast<uintptr_t>(ss);
120 extra_data.ss_len = sizeof(*ss);
Elliott Hughes11952072013-10-24 15:15:14 -0700121
122 return __pselect6(fd_count, read_fds, write_fds, error_fds, mutable_ts_ptr, &extra_data);
123}