xref: /minix3/lib/libc/sys/ntp_adjtime.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1 /*	$NetBSD: ntp_adjtime.c,v 1.13 2012/03/20 16:26:12 matt 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: ntp_adjtime.c,v 1.13 2012/03/20 16:26:12 matt Exp $");
37 #endif /* LIBC_SCCS and not lint */
38 
39 #include "namespace.h"
40 #include <sys/types.h>
41 #include <sys/time.h>
42 #include <sys/timex.h>
43 #include <sys/ioctl.h>
44 #include <sys/syscall.h>
45 
46 #include <sys/clockctl.h>
47 
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <paths.h>
51 #include <string.h>
52 #include <unistd.h>
53 
54 #ifdef __weak_alias
55 __weak_alias(ntp_adjtime,_ntp_adjtime)
56 #endif
57 
58 #if defined(__minix) && !defined(O_CLOEXEC)
59 #define O_CLOEXEC 0
60 #endif
61 
62 extern int __clockctl_fd;
63 
64 int __ntp_adjtime(struct timex *);
65 
66 int
67 ntp_adjtime(struct timex *tp)
68 {
69 	struct clockctl_ntp_adjtime args;
70 	int error;
71 	int rv;
72 
73 	/*
74 	 * we always attempt to use the syscall unless we had to
75 	 * use the clockctl device before
76 	 *
77 	 * ntp_adjtime() is callable for mortals if tp->modes == 0 !
78 	 */
79 	if (__clockctl_fd == -1) {
80 		rv = __ntp_adjtime(tp);
81 
82 		/*
83 		 * if we fail with EPERM we try the clockctl device
84 		 */
85 		if (rv != -1 || errno != EPERM)
86 			return rv;
87 
88 		/*
89 		 * If this fails, it means that we are not root
90 		 * and we cannot open clockctl. This is a true
91 		 * failure.
92 		 */
93 		__clockctl_fd = open(_PATH_CLOCKCTL, O_WRONLY | O_CLOEXEC, 0);
94 		if (__clockctl_fd == -1) {
95 			/* original error was EPERM - don't leak open errors */
96 			errno = EPERM;
97 			return -1;
98 		}
99 	}
100 
101 	/*
102 	 * If __clockctl_fd >=0, clockctl has already been open
103 	 * and used, so we carry on using it.
104 	 */
105 	args.tp = tp;
106 	error = ioctl(__clockctl_fd, CLOCKCTL_NTP_ADJTIME, &args);
107 
108 	/*
109 	 * There is no way to get retval set through ioctl(), hence we
110 	 * have to handle it here, using args.retval
111 	 */
112 	if (error == 0) {
113 		rv = (int)args.retval;
114 		return rv;
115 	}
116 
117 	return error;
118 }
119