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