xref: /illumos-gate/usr/src/uts/common/syscall/adjtime.c (revision b93865c3d90e9b0d73e338c9abb3293c35c571a8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved	*/
24 
25 
26 /*
27  * Copyright 1999,2001-2003 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/systm.h>
35 #include <sys/tuneable.h>
36 #include <sys/errno.h>
37 #include <sys/proc.h>
38 #include <sys/time.h>
39 #include <sys/debug.h>
40 #include <sys/model.h>
41 #include <sys/policy.h>
42 
43 int
adjtime(const struct timeval * delta,struct timeval * olddelta)44 adjtime(const struct timeval *delta, struct timeval *olddelta)
45 {
46 	struct timeval atv, oatv;
47 	int64_t	ndelta;
48 	int64_t old_delta;
49 	int s;
50 	model_t datamodel = get_udatamodel();
51 
52 	if (secpolicy_settime(CRED()) != 0)
53 		return (set_errno(EPERM));
54 
55 	if (datamodel == DATAMODEL_NATIVE) {
56 		if (copyin(delta, &atv, sizeof (atv)))
57 			return (set_errno(EFAULT));
58 	} else {
59 		struct timeval32 atv32;
60 
61 		if (copyin(delta, &atv32, sizeof (atv32)))
62 			return (set_errno(EFAULT));
63 		TIMEVAL32_TO_TIMEVAL(&atv, &atv32);
64 	}
65 
66 	if (atv.tv_usec <= -MICROSEC || atv.tv_usec >= MICROSEC)
67 		return (set_errno(EINVAL));
68 
69 	/*
70 	 * The SVID specifies that if delta is 0, then there is
71 	 * no effect upon time correction, just return olddelta.
72 	 */
73 	ndelta = (int64_t)atv.tv_sec * NANOSEC + atv.tv_usec * 1000;
74 	mutex_enter(&tod_lock);
75 	s = hr_clock_lock();
76 	old_delta = timedelta;
77 	if (ndelta)
78 		timedelta = ndelta;
79 	/*
80 	 * Always set tod_needsync on all adjtime() calls, since it implies
81 	 * someone is watching over us and keeping the local clock in sync.
82 	 */
83 	tod_needsync = 1;
84 	hr_clock_unlock(s);
85 	mutex_exit(&tod_lock);
86 
87 	if (olddelta) {
88 		oatv.tv_sec = old_delta / NANOSEC;
89 		oatv.tv_usec = (old_delta % NANOSEC) / 1000;
90 		if (datamodel == DATAMODEL_NATIVE) {
91 			if (copyout(&oatv, olddelta, sizeof (oatv)))
92 				return (set_errno(EFAULT));
93 		} else {
94 			struct timeval32 oatv32;
95 
96 			if (TIMEVAL_OVERFLOW(&oatv))
97 				return (set_errno(EOVERFLOW));
98 
99 			TIMEVAL_TO_TIMEVAL32(&oatv32, &oatv);
100 
101 			if (copyout(&oatv32, olddelta, sizeof (oatv32)))
102 				return (set_errno(EFAULT));
103 		}
104 	}
105 	return (0);
106 }
107