xref: /illumos-gate/usr/src/uts/common/syscall/adjtime.c (revision 89b2a9fbeabf42fa54594df0e5927bcc50a07cc9)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* from SVr4.0 1.78 */
32 
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/sysmacros.h>
36 #include <sys/systm.h>
37 #include <sys/tuneable.h>
38 #include <sys/errno.h>
39 #include <sys/proc.h>
40 #include <sys/time.h>
41 #include <sys/debug.h>
42 #include <sys/model.h>
43 #include <sys/policy.h>
44 
45 int
46 adjtime(struct timeval *delta, struct timeval *olddelta)
47 {
48 	struct timeval atv, oatv;
49 	int64_t	ndelta;
50 	int64_t old_delta;
51 	int s;
52 	model_t datamodel = get_udatamodel();
53 
54 	if (secpolicy_settime(CRED()) != 0)
55 		return (set_errno(EPERM));
56 
57 	if (datamodel == DATAMODEL_NATIVE) {
58 		if (copyin(delta, &atv, sizeof (atv)))
59 			return (set_errno(EFAULT));
60 	} else {
61 		struct timeval32 atv32;
62 
63 		if (copyin(delta, &atv32, sizeof (atv32)))
64 			return (set_errno(EFAULT));
65 		TIMEVAL32_TO_TIMEVAL(&atv, &atv32);
66 	}
67 
68 	if (atv.tv_usec <= -MICROSEC || atv.tv_usec >= MICROSEC)
69 		return (set_errno(EINVAL));
70 
71 	/*
72 	 * The SVID specifies that if delta is 0, then there is
73 	 * no effect upon time correction, just return olddelta.
74 	 */
75 	ndelta = (int64_t)atv.tv_sec * NANOSEC + atv.tv_usec * 1000;
76 	mutex_enter(&tod_lock);
77 	s = hr_clock_lock();
78 	old_delta = timedelta;
79 	if (ndelta)
80 		timedelta = ndelta;
81 	/*
82 	 * Always set tod_needsync on all adjtime() calls, since it implies
83 	 * someone is watching over us and keeping the local clock in sync.
84 	 */
85 	tod_needsync = 1;
86 	hr_clock_unlock(s);
87 	mutex_exit(&tod_lock);
88 
89 	if (olddelta) {
90 		oatv.tv_sec = old_delta / NANOSEC;
91 		oatv.tv_usec = (old_delta % NANOSEC) / 1000;
92 		if (datamodel == DATAMODEL_NATIVE) {
93 			if (copyout(&oatv, olddelta, sizeof (oatv)))
94 				return (set_errno(EFAULT));
95 		} else {
96 			struct timeval32 oatv32;
97 
98 			if (TIMEVAL_OVERFLOW(&oatv))
99 				return (set_errno(EOVERFLOW));
100 
101 			TIMEVAL_TO_TIMEVAL32(&oatv32, &oatv);
102 
103 			if (copyout(&oatv32, olddelta, sizeof (oatv32)))
104 				return (set_errno(EFAULT));
105 		}
106 	}
107 	return (0);
108 }
109