xref: /netbsd/lib/libc/sys/ntp_adjtime.c (revision bf9ec67e)
1 /*	$NetBSD: ntp_adjtime.c,v 1.3 2001/12/09 16:11:45 manu 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 "namespace.h"
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <paths.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/time.h>
41 #include <sys/timex.h>
42 #include <sys/ioctl.h>
43 #include <sys/syscall.h>
44 #include <sys/systm.h>
45 
46 #include <sys/clockctl.h>
47 
48 #include <fcntl.h>
49 #include <paths.h>
50 #include <string.h>
51 #include <unistd.h>
52 
53 #ifdef __weak_alias
54 __weak_alias(ntp_adjtime,_ntp_adjtime)
55 #endif
56 
57 extern int __clockctl_fd;
58 
59 int
60 ntp_adjtime(tp)
61 	struct timex *tp;
62 {
63 	struct clockctl_ntp_adjtime_args args;
64 	int error;
65 	quad_t q;
66 	int rv;
67 
68 	/*
69 	 * if __clockctl_fd == -1, then this is not our first time,
70 	 * and we know root is the calling user. We use the system call
71 	 */
72 	if (__clockctl_fd == -1) {
73 try_syscall:
74 		q = __syscall((quad_t)SYS_ntp_adjtime, tp);
75 		if (/* LINTED constant */ sizeof (quad_t) == sizeof (register_t)
76 		    || /* LINTED constant */ BYTE_ORDER == LITTLE_ENDIAN)
77 			rv = (int)q;
78 		else
79 			rv = (int)((u_quad_t)q >> 32);
80 
81 		/*
82 		 * If credentials changed from root to an unprivilegied
83 		 * user, and we already had __clockctl_fd = -1, then we
84 		 * tried the system call as a non root user, it failed
85 		 * with EPERM, and we will try clockctl.
86 		 */
87 		if (rv != -1 || errno != EPERM)
88 			return rv;
89 		__clockctl_fd = -2;
90 	}
91 
92 	/*
93 	 * If __clockctl_fd = -2 then this is our first time here,
94 	 * or credentials have changed (the calling process dropped root
95 	 * root privilege). Check if root is the calling user. If it is,
96 	 * we try the system call, if it is not, we try clockctl.
97 	 */
98 	if (__clockctl_fd == -2) {
99 		/*
100 		 * Root always uses the syscall
101 		 */
102 		if (geteuid() == 0) {
103 			__clockctl_fd = -1;
104 			goto try_syscall;
105 		}
106 
107 		/*
108 		 * If this fails, it means that we are not root
109 		 * and we cannot open clockctl. This is a failure.
110 		 */
111 		__clockctl_fd = open(_PATH_CLOCKCTL, O_WRONLY, 0);
112 		if (__clockctl_fd == -1)
113 			return -1;
114 	}
115 
116 	/*
117 	 * If __clockctl_fd >=0, clockctl has already been open
118 	 * and used, so we carry on using it.
119 	 */
120 	SCARG(&args.uas, tp) = tp;
121 	error = ioctl(__clockctl_fd, CLOCKCTL_NTP_ADJTIME, &args);
122 
123 	/*
124 	 * There is no way to get retval set through ioctl(), hence we
125 	 * have to handle it here, using args.retval
126 	 */
127 	if (error == 0) {
128 		rv = (int)args.retval;
129 		return rv;
130 	}
131 	return error;
132 
133 }
134