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