path: root/src/select
diff options
authorRich Felker <>2014-05-29 21:01:32 -0400
committerRich Felker <>2014-05-29 21:01:32 -0400
commitdd5f50da6f6c3df5647e922e47f8568a8896a752 (patch)
treec5ab9a1006d2ab4c449f8fa922ce3237acad30e4 /src/select
parent2e55da911896a91e95b24ab5dc8a9d9b0718f4de (diff)
support linux kernel apis (new archs) with old syscalls removed
such archs are expected to omit definitions of the SYS_* macros for syscalls their kernels lack from arch/$ARCH/bits/syscall.h. the preprocessor is then able to select the an appropriate implementation for affected functions. two basic strategies are used on a case-by-case basis: where the old syscalls correspond to deprecated library-level functions, the deprecated functions have been converted to wrappers for the modern function, and the modern function has fallback code (omitted at the preprocessor level on new archs) to make use of the old syscalls if the new syscall fails with ENOSYS. this also improves functionality on older kernels and eliminates the incentive to program with deprecated library-level functions for the sake of compatibility with older kernels. in other situations where the old syscalls correspond to library-level functions which are not deprecated but merely lack some new features, such as the *at functions, the old syscalls are still used on archs which support them. this may change at some point in the future if or when fallback code is added to the new functions to make them usable (possibly with reduced functionality) on old kernels.
Diffstat (limited to 'src/select')
2 files changed, 26 insertions, 0 deletions
diff --git a/src/select/poll.c b/src/select/poll.c
index f1e73e82..9e0bcbd8 100644
--- a/src/select/poll.c
+++ b/src/select/poll.c
@@ -1,8 +1,16 @@
#include <poll.h>
+#include <time.h>
+#include <signal.h>
#include "syscall.h"
#include "libc.h"
int poll(struct pollfd *fds, nfds_t n, int timeout)
+#ifdef SYS_poll
return syscall_cp(SYS_poll, fds, n, timeout);
+ return syscall_cp(SYS_ppoll, fds, n, timeout>=0 ?
+ &((struct timespec){ .tv_sec = timeout/1000,
+ .tv_nsec = timeout%1000*1000000 }) : 0, 0, _NSIG/8);
diff --git a/src/select/select.c b/src/select/select.c
index f93597b5..7b5f6dcf 100644
--- a/src/select/select.c
+++ b/src/select/select.c
@@ -1,8 +1,26 @@
#include <sys/select.h>
+#include <signal.h>
+#include <stdint.h>
+#include <errno.h>
#include "syscall.h"
#include "libc.h"
int select(int n, fd_set *restrict rfds, fd_set *restrict wfds, fd_set *restrict efds, struct timeval *restrict tv)
+#ifdef SYS_select
return syscall_cp(SYS_select, n, rfds, wfds, efds, tv);
+ syscall_arg_t data[2] = { 0, _NSIG/8 };
+ struct timespec ts;
+ if (tv) {
+ if (tv->tv_sec < 0 || tv->tv_usec < 0)
+ return __syscall_ret(-EINVAL);
+ time_t extra_secs = tv->tv_usec / 1000000;
+ ts.tv_nsec = tv->tv_usec % 1000000 * 1000;
+ const time_t max_time = (1ULL<<8*sizeof(time_t)-1)-1;
+ ts.tv_sec = extra_secs > max_time - tv->tv_sec ?
+ max_time : tv->tv_sec + extra_secs;
+ }
+ return syscall_cp(SYS_pselect6, n, rfds, wfds, efds, tv ? &ts : 0, data);