2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / clock_settime.c
blob217ae3f29b96af474491f28a454f4135354bb540
1 /* Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <sysdep.h>
22 #include "kernel-posix-cpu-timers.h"
23 #include <kernel-features.h>
25 #ifndef HAVE_CLOCK_GETRES_VSYSCALL
26 # undef INTERNAL_VSYSCALL
27 # define INTERNAL_VSYSCALL INTERNAL_SYSCALL
28 # undef INLINE_VSYSCALL
29 # define INLINE_VSYSCALL INLINE_SYSCALL
30 #else
31 # include <bits/libc-vdso.h>
32 #endif
34 #if __ASSUME_POSIX_CPU_TIMERS <= 0 && defined __NR_clock_settime
35 extern int __libc_missing_posix_timers attribute_hidden;
36 extern int __libc_missing_posix_cpu_timers attribute_hidden;
38 static int
39 maybe_syscall_settime_cpu (clockid_t clock_id, const struct timespec *tp)
41 int e = EINVAL;
43 if (!__libc_missing_posix_cpu_timers)
45 INTERNAL_SYSCALL_DECL (err);
46 int r = INTERNAL_SYSCALL (clock_settime, err, 2, clock_id, tp);
47 if (!INTERNAL_SYSCALL_ERROR_P (r, err))
48 return 0;
50 e = INTERNAL_SYSCALL_ERRNO (r, err);
51 # ifndef __ASSUME_POSIX_TIMERS
52 if (e == ENOSYS)
54 __libc_missing_posix_timers = 1;
55 __libc_missing_posix_cpu_timers = 1;
56 e = EINVAL;
58 else
59 # endif
61 if (e == EINVAL)
63 /* Check whether the kernel supports CPU clocks at all.
64 If not, record it for the future. */
65 r = INTERNAL_VSYSCALL (clock_getres, err, 2,
66 MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED),
67 NULL);
68 if (INTERNAL_SYSCALL_ERROR_P (r, err))
69 __libc_missing_posix_cpu_timers = 1;
74 return e;
76 #endif
79 #ifdef __ASSUME_POSIX_TIMERS
80 /* This means the REALTIME clock is definitely supported in the
81 kernel. */
82 # define SYSDEP_SETTIME \
83 case CLOCK_REALTIME: \
84 retval = INLINE_SYSCALL (clock_settime, 2, clock_id, tp); \
85 break
86 #elif defined __NR_clock_settime
87 /* Is the syscall known to exist? */
88 extern int __libc_missing_posix_timers attribute_hidden;
90 /* The REALTIME clock might be available. Try the syscall first. */
91 # define SYSDEP_SETTIME \
92 case CLOCK_REALTIME: \
93 { \
94 int e = EINVAL; \
96 if (!__libc_missing_posix_timers) \
97 { \
98 INTERNAL_SYSCALL_DECL (err); \
99 int r = INTERNAL_SYSCALL (clock_settime, err, 2, clock_id, tp); \
100 if (!INTERNAL_SYSCALL_ERROR_P (r, err)) \
102 retval = 0; \
103 break; \
106 e = INTERNAL_SYSCALL_ERRNO (r, err); \
107 if (e == ENOSYS) \
109 __libc_missing_posix_timers = 1; \
110 e = EINVAL; \
114 /* Fallback code. */ \
115 if (e == EINVAL && clock_id == CLOCK_REALTIME) \
116 HANDLE_REALTIME; \
117 else \
119 __set_errno (e); \
120 retval = -1; \
123 break
124 #endif
126 #ifdef __NR_clock_settime
127 /* We handled the REALTIME clock here. */
128 # define HANDLED_REALTIME 1
129 #endif
131 #if __ASSUME_POSIX_CPU_TIMERS > 0
132 # define HANDLED_CPUTIME 1
133 # define SYSDEP_SETTIME_CPU \
134 retval = INLINE_SYSCALL (clock_settime, 2, clock_id, tp)
135 #elif defined __NR_clock_settime
136 # define SYSDEP_SETTIME_CPU \
137 retval = maybe_syscall_settime_cpu (clock_id, tp); \
138 if (retval == 0) \
139 break; \
140 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
142 __set_errno (retval); \
143 retval = -1; \
144 break; \
146 do { } while (0)
147 #endif
149 #include <sysdeps/unix/clock_settime.c>