Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / linux / clock_settime.c
blob5656db90f3f18e1fdb52f46a248b85b57680d94f
1 /* Copyright (C) 2003, 2004, 2006, 2010 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <sysdep.h>
21 #include "kernel-posix-cpu-timers.h"
22 #include <kernel-features.h>
24 #ifndef HAVE_CLOCK_GETRES_VSYSCALL
25 # undef INTERNAL_VSYSCALL
26 # define INTERNAL_VSYSCALL INTERNAL_SYSCALL
27 # undef INLINE_VSYSCALL
28 # define INLINE_VSYSCALL INLINE_SYSCALL
29 #else
30 # include <bits/libc-vdso.h>
31 #endif
33 #if __ASSUME_POSIX_CPU_TIMERS <= 0 && defined __NR_clock_settime
34 extern int __libc_missing_posix_timers attribute_hidden;
35 extern int __libc_missing_posix_cpu_timers attribute_hidden;
37 static int
38 maybe_syscall_settime_cpu (clockid_t clock_id, const struct timespec *tp)
40 int e = EINVAL;
42 if (!__libc_missing_posix_cpu_timers)
44 INTERNAL_SYSCALL_DECL (err);
45 int r = INTERNAL_SYSCALL (clock_settime, err, 2, clock_id, tp);
46 if (!INTERNAL_SYSCALL_ERROR_P (r, err))
47 return 0;
49 e = INTERNAL_SYSCALL_ERRNO (r, err);
50 # ifndef __ASSUME_POSIX_TIMERS
51 if (e == ENOSYS)
53 __libc_missing_posix_timers = 1;
54 __libc_missing_posix_cpu_timers = 1;
55 e = EINVAL;
57 else
58 # endif
60 if (e == EINVAL)
62 /* Check whether the kernel supports CPU clocks at all.
63 If not, record it for the future. */
64 r = INTERNAL_VSYSCALL (clock_getres, err, 2,
65 MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED),
66 NULL);
67 if (INTERNAL_SYSCALL_ERROR_P (r, err))
68 __libc_missing_posix_cpu_timers = 1;
73 return e;
75 #endif
78 #ifdef __ASSUME_POSIX_TIMERS
79 /* This means the REALTIME clock is definitely supported in the
80 kernel. */
81 # define SYSDEP_SETTIME \
82 case CLOCK_REALTIME: \
83 retval = INLINE_SYSCALL (clock_settime, 2, clock_id, tp); \
84 break
85 #elif defined __NR_clock_settime
86 /* Is the syscall known to exist? */
87 extern int __libc_missing_posix_timers attribute_hidden;
89 /* The REALTIME clock might be available. Try the syscall first. */
90 # define SYSDEP_SETTIME \
91 case CLOCK_REALTIME: \
92 case CLOCK_REALTIME_COARSE: \
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>