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