xref: /netbsd-src/lib/libc/sys/clock_settime.c (revision 56a34939419542e88b386b2229be7565f4f45461)
1 /*	$NetBSD: clock_settime.c,v 1.10 2007/11/23 12:39:15 uebayasi Exp $ */
2 
3 /*
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Emmanuel Dreyfus.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 __RCSID("$NetBSD: clock_settime.c,v 1.10 2007/11/23 12:39:15 uebayasi Exp $");
37 #endif /* LIBC_SCCS and not lint */
38 
39 #include "namespace.h"
40 
41 #include <sys/types.h>
42 #include <sys/ioctl.h>
43 #include <sys/syscall.h>
44 
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <paths.h>
48 #include <string.h>
49 #include <time.h>
50 #include <unistd.h>
51 
52 #include <sys/clockctl.h>
53 
54 #ifdef __weak_alias
55 __weak_alias(clock_settime,_clock_settime)
56 #endif
57 
58 extern int __clockctl_fd;
59 
60 int __clock_settime(clockid_t, const struct timespec *);
61 
62 int
63 clock_settime(clock_id, tp)
64 	clockid_t clock_id;
65 	const struct timespec *tp;
66 {
67 	struct clockctl_clock_settime args;
68 	int rv;
69 
70 	/*
71 	 * always try the syscall first and attempt to switch to
72 	 * clockctl if that fails.
73 	 */
74 	if (__clockctl_fd == -1) {
75 		rv = __clock_settime(clock_id, tp);
76 
77 		/*
78 		 * return unless we failed with EPERM
79 		 */
80 		if (rv != -1 || errno != EPERM)
81 			return rv;
82 
83 		/*
84 		 * If this fails, it means that we are not root
85 		 * and we cannot open clockctl. This is a failure.
86 		 */
87 		__clockctl_fd = open(_PATH_CLOCKCTL, O_WRONLY, 0);
88 		if (__clockctl_fd == -1) {
89 			/* original error was EPERM - don't leak open errors */
90 			errno = EPERM;
91 			return -1;
92 		}
93 
94 		(void) fcntl(__clockctl_fd, F_SETFD, FD_CLOEXEC);
95 	}
96 
97 	/*
98 	 * If __clockctl_fd >=0, clockctl has already been open
99 	 * and used, so we carry on using it.
100 	 */
101 	args.clock_id = clock_id;
102 	args.tp = tp;
103 	return ioctl(__clockctl_fd, CLOCKCTL_CLOCK_SETTIME, &args);
104 }
105