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