aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/unix/sysv/linux/select.c
blob: ba17746ee9851f1979143f8d40c4f2f5cb8ce0c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* Linux select implementation.
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library.  If not, see
   <http://www.gnu.org/licenses/>.  */

#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <errno.h>
#include <sysdep-cancel.h>
#include <y2038-support.h>

/* Check the first NFDS descriptors each in READFDS (if not NULL) for read
   readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
   (if not NULL) for exceptional conditions.  If TIMEOUT is not NULL, time out
   after waiting the interval specified therein.  Returns the number of ready
   descriptors, or -1 for errors.  */

#ifdef __NR__newselect
# undef __NR_select
# define __NR_select __NR__newselect
#endif

int
__select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
	  struct timeval *timeout)
{
#ifdef __NR_select
  return SYSCALL_CANCEL (select, nfds, readfds, writefds, exceptfds,
			 timeout);
#else
  int result;
  struct timespec ts, *tsp = NULL;

  if (timeout)
    {
      TIMEVAL_TO_TIMESPEC (timeout, &ts);
      tsp = &ts;
    }

  result = SYSCALL_CANCEL (pselect6, nfds, readfds, writefds, exceptfds, tsp,
			   NULL);

  if (timeout)
    {
      /* Linux by default will update the timeout after a pselect6 syscall
         (though the pselect() glibc call suppresses this behavior).
         Since select() on Linux has the same behavior as the pselect6
         syscall, we update the timeout here.  */
      TIMESPEC_TO_TIMEVAL (timeout, &ts);
    }

  return result;
#endif
}
libc_hidden_def (__select)

weak_alias (__select, select)
weak_alias (__select, __libc_select)

/* 64-bit time version */

int
__select64 (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
	      struct __timeval64 *timeout)
{
#ifdef __NR_select
  struct timeval tval32, *timeout32 = NULL;
#else
  int result;
  struct timespec ts32, *tsp32 = NULL;
#endif

#ifdef __NR_pselect6_time64
  int res;
  if (__y2038_linux_support > 0)
  {
    res = SYSCALL_CANCEL (pselect6_time64, nfds, readfds, writefds,
			  exceptfds, timeout, NULL);
    if (res == 0 || errno != ENOSYS)
      return res;
    __y2038_linux_support = -1;
  }
#endif

#ifdef __NR_select
  if (timeout != NULL)
    {
      if (timeout->tv_sec > INT_MAX)
      {
        errno = EOVERFLOW;
        return -1;
      }
      tval32.tv_sec = timeout->tv_sec;
      tval32.tv_usec = timeout->tv_usec;
      timeout32 = &tval32;
    }

  return SYSCALL_CANCEL (select, nfds, readfds, writefds, exceptfds,
			 timeout32);
#else
  if (timeout)
    {
      if (timeout->tv_sec > INT_MAX)
      {
        errno = EOVERFLOW;
        return -1;
      }
      ts32.tv_sec = timeout->tv_sec;
      ts32.tv_nsec = timeout->tv_usec * 1000;
      tsp32 = &ts32;
    }

  result = SYSCALL_CANCEL (pselect6, nfds, readfds, writefds, exceptfds, tsp32,
			   NULL);

  if (timeout)
    {
      /* Linux by default will update the timeout after a pselect6 syscall
         (though the pselect() glibc call suppresses this behavior).
         Since select() on Linux has the same behavior as the pselect6
         syscall, we update the timeout here.  */
      timeout->tv_sec = ts32.tv_sec;
      timeout->tv_usec = ts32.tv_nsec / 1000;
    }

  return result;
#endif
}