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 #undef _FORTIFY_SOURCE
30 #include <errno.h>
31 #include <sys/poll.h>
32 #include <sys/select.h>
33
34 #include "private/bionic_time_conversions.h"
35 #include "private/kernel_sigset_t.h"
36
37 extern "C" int __ppoll(pollfd*, unsigned int, timespec*, const kernel_sigset_t*, size_t);
38 extern "C" int __pselect6(int, fd_set*, fd_set*, fd_set*, timespec*, void*);
39
poll(pollfd * fds,nfds_t fd_count,int ms)40 int poll(pollfd* fds, nfds_t fd_count, int ms) __overloadable {
41 timespec ts;
42 timespec* ts_ptr = NULL;
43 if (ms >= 0) {
44 timespec_from_ms(ts, ms);
45 ts_ptr = &ts;
46 }
47 return __ppoll(fds, fd_count, ts_ptr, NULL, 0);
48 }
49
ppoll(pollfd * fds,nfds_t fd_count,const timespec * ts,const sigset_t * ss)50 int ppoll(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset_t* ss) __overloadable {
51 timespec mutable_ts;
52 timespec* mutable_ts_ptr = NULL;
53 if (ts != NULL) {
54 mutable_ts = *ts;
55 mutable_ts_ptr = &mutable_ts;
56 }
57
58 kernel_sigset_t kernel_ss;
59 kernel_sigset_t* kernel_ss_ptr = NULL;
60 if (ss != NULL) {
61 kernel_ss.set(ss);
62 kernel_ss_ptr = &kernel_ss;
63 }
64
65 return __ppoll(fds, fd_count, mutable_ts_ptr, kernel_ss_ptr, sizeof(kernel_ss));
66 }
67
select(int fd_count,fd_set * read_fds,fd_set * write_fds,fd_set * error_fds,timeval * tv)68 int select(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds, timeval* tv) {
69 timespec ts;
70 timespec* ts_ptr = NULL;
71 if (tv != NULL) {
72 if (!timespec_from_timeval(ts, *tv)) {
73 errno = EINVAL;
74 return -1;
75 }
76 ts_ptr = &ts;
77 }
78 int result = __pselect6(fd_count, read_fds, write_fds, error_fds, ts_ptr, NULL);
79 if (tv != NULL) {
80 timeval_from_timespec(*tv, ts);
81 }
82 return result;
83 }
84
pselect(int fd_count,fd_set * read_fds,fd_set * write_fds,fd_set * error_fds,const timespec * ts,const sigset_t * ss)85 int pselect(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds,
86 const timespec* ts, const sigset_t* ss) {
87 timespec mutable_ts;
88 timespec* mutable_ts_ptr = NULL;
89 if (ts != NULL) {
90 mutable_ts = *ts;
91 mutable_ts_ptr = &mutable_ts;
92 }
93
94 kernel_sigset_t kernel_ss;
95 kernel_sigset_t* kernel_ss_ptr = NULL;
96 if (ss != NULL) {
97 kernel_ss.set(ss);
98 kernel_ss_ptr = &kernel_ss;
99 }
100
101 // The Linux kernel only handles 6 arguments and this system call really needs 7,
102 // so the last argument is a void* pointing to:
103 struct pselect6_extra_data_t {
104 uintptr_t ss_addr;
105 size_t ss_len;
106 };
107 pselect6_extra_data_t extra_data;
108 extra_data.ss_addr = reinterpret_cast<uintptr_t>(kernel_ss_ptr);
109 extra_data.ss_len = sizeof(kernel_ss);
110
111 return __pselect6(fd_count, read_fds, write_fds, error_fds, mutable_ts_ptr, &extra_data);
112 }
113