xref: /freebsd/sys/kern/kern_time.c (revision 00af9731c9222d8b48655e01fadef7161f8b64dc)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
3400af9731SPoul-Henning Kamp  * $Id: kern_time.c,v 1.44 1998/03/30 09:50:23 phk Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38d2d3e875SBruce Evans #include <sys/sysproto.h>
39df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
40797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
41df8bae1dSRodney W. Grimes #include <sys/kernel.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
4394c8fcd8SPeter Wemm #include <sys/sysent.h>
44df8bae1dSRodney W. Grimes #include <sys/proc.h>
45708e7684SPeter Wemm #include <sys/time.h>
46df8bae1dSRodney W. Grimes #include <sys/vnode.h>
475b870b7bSPeter Wemm #include <vm/vm.h>
485b870b7bSPeter Wemm #include <vm/vm_extern.h>
49df8bae1dSRodney W. Grimes 
50ac7e6123SDavid Greenman struct timezone tz;
51ac7e6123SDavid Greenman 
52df8bae1dSRodney W. Grimes /*
53df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
54df8bae1dSRodney W. Grimes  *
55df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
56df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
57df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
58df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
59df8bae1dSRodney W. Grimes  * timers when they expire.
60df8bae1dSRodney W. Grimes  */
61df8bae1dSRodney W. Grimes 
625b870b7bSPeter Wemm static int	nanosleep1 __P((struct proc *p, struct timespec *rqt,
635b870b7bSPeter Wemm 		    struct timespec *rmt));
647d7fb492SBruce Evans static int	settime __P((struct timeval *));
657d7fb492SBruce Evans static void	timevalfix __P((struct timeval *));
66cb226aaaSPoul-Henning Kamp static void	no_lease_updatetime __P((int));
6794c8fcd8SPeter Wemm 
681b09ae77SPoul-Henning Kamp static void
691b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
701b09ae77SPoul-Henning Kamp 	int deltat;
711b09ae77SPoul-Henning Kamp {
721b09ae77SPoul-Henning Kamp }
731b09ae77SPoul-Henning Kamp 
741b09ae77SPoul-Henning Kamp void (*lease_updatetime) __P((int))  = no_lease_updatetime;
751b09ae77SPoul-Henning Kamp 
7694c8fcd8SPeter Wemm static int
7794c8fcd8SPeter Wemm settime(tv)
7894c8fcd8SPeter Wemm 	struct timeval *tv;
7994c8fcd8SPeter Wemm {
809c8fff87SBruce Evans 	struct timeval delta, tv1;
817ec73f64SPoul-Henning Kamp 	struct timespec ts;
8294c8fcd8SPeter Wemm 	struct proc *p;
8394c8fcd8SPeter Wemm 	int s;
8494c8fcd8SPeter Wemm 
85708e7684SPeter Wemm 	s = splclock();
869c8fff87SBruce Evans 	microtime(&tv1);
8700af9731SPoul-Henning Kamp 	delta = *tv;
8800af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
8994c8fcd8SPeter Wemm 
9094c8fcd8SPeter Wemm 	/*
919c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
929c8fff87SBruce Evans 	 * set to an earlier value (it may be slowed using adjtime,
939c8fff87SBruce Evans 	 * but not set back). This feature prevent interlopers from
949c8fff87SBruce Evans 	 * setting arbitrary time stamps on files.
9594c8fcd8SPeter Wemm 	 */
969c8fff87SBruce Evans 	if (delta.tv_sec < 0 && securelevel > 1) {
979c8fff87SBruce Evans 		splx(s);
989c8fff87SBruce Evans 		return (EPERM);
999c8fff87SBruce Evans 	}
1009c8fff87SBruce Evans 
1017ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1027ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1037ec73f64SPoul-Henning Kamp 	set_timecounter(&ts);
10494c8fcd8SPeter Wemm 	(void) splsoftclock();
1055b870b7bSPeter Wemm 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
10694c8fcd8SPeter Wemm 		if (timerisset(&p->p_realtimer.it_value))
10794c8fcd8SPeter Wemm 			timevaladd(&p->p_realtimer.it_value, &delta);
1085b870b7bSPeter Wemm 	}
10994c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
11094c8fcd8SPeter Wemm 	splx(s);
11194c8fcd8SPeter Wemm 	resettodr();
11294c8fcd8SPeter Wemm 	return (0);
11394c8fcd8SPeter Wemm }
11494c8fcd8SPeter Wemm 
11594c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
11694c8fcd8SPeter Wemm struct clock_gettime_args {
11794c8fcd8SPeter Wemm 	clockid_t clock_id;
11894c8fcd8SPeter Wemm 	struct	timespec *tp;
11994c8fcd8SPeter Wemm };
12094c8fcd8SPeter Wemm #endif
121708e7684SPeter Wemm 
12294c8fcd8SPeter Wemm /* ARGSUSED */
12394c8fcd8SPeter Wemm int
124cb226aaaSPoul-Henning Kamp clock_gettime(p, uap)
12594c8fcd8SPeter Wemm 	struct proc *p;
12694c8fcd8SPeter Wemm 	struct clock_gettime_args *uap;
12794c8fcd8SPeter Wemm {
12894c8fcd8SPeter Wemm 	struct timespec ats;
12994c8fcd8SPeter Wemm 
130708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
13194c8fcd8SPeter Wemm 		return (EINVAL);
1327ec73f64SPoul-Henning Kamp 	nanotime(&ats);
133708e7684SPeter Wemm 	return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
13494c8fcd8SPeter Wemm }
13594c8fcd8SPeter Wemm 
13694c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
13794c8fcd8SPeter Wemm struct clock_settime_args {
13894c8fcd8SPeter Wemm 	clockid_t clock_id;
13994c8fcd8SPeter Wemm 	const struct	timespec *tp;
14094c8fcd8SPeter Wemm };
14194c8fcd8SPeter Wemm #endif
142708e7684SPeter Wemm 
14394c8fcd8SPeter Wemm /* ARGSUSED */
14494c8fcd8SPeter Wemm int
145cb226aaaSPoul-Henning Kamp clock_settime(p, uap)
14694c8fcd8SPeter Wemm 	struct proc *p;
14794c8fcd8SPeter Wemm 	struct clock_settime_args *uap;
14894c8fcd8SPeter Wemm {
14994c8fcd8SPeter Wemm 	struct timeval atv;
15094c8fcd8SPeter Wemm 	struct timespec ats;
15194c8fcd8SPeter Wemm 	int error;
15294c8fcd8SPeter Wemm 
15394c8fcd8SPeter Wemm 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
15494c8fcd8SPeter Wemm 		return (error);
155708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
15694c8fcd8SPeter Wemm 		return (EINVAL);
15794c8fcd8SPeter Wemm 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
15894c8fcd8SPeter Wemm 		return (error);
1593fc9295dSBruce Evans 	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
160708e7684SPeter Wemm 		return (EINVAL);
161a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
16294c8fcd8SPeter Wemm 	TIMESPEC_TO_TIMEVAL(&atv, &ats);
16394c8fcd8SPeter Wemm 	if ((error = settime(&atv)))
16494c8fcd8SPeter Wemm 		return (error);
165708e7684SPeter Wemm 	return (0);
16694c8fcd8SPeter Wemm }
16794c8fcd8SPeter Wemm 
16894c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
16994c8fcd8SPeter Wemm struct clock_getres_args {
17094c8fcd8SPeter Wemm 	clockid_t clock_id;
17194c8fcd8SPeter Wemm 	struct	timespec *tp;
17294c8fcd8SPeter Wemm };
17394c8fcd8SPeter Wemm #endif
174708e7684SPeter Wemm 
17594c8fcd8SPeter Wemm int
176cb226aaaSPoul-Henning Kamp clock_getres(p, uap)
17794c8fcd8SPeter Wemm 	struct proc *p;
17894c8fcd8SPeter Wemm 	struct clock_getres_args *uap;
17994c8fcd8SPeter Wemm {
18094c8fcd8SPeter Wemm 	struct timespec ts;
181708e7684SPeter Wemm 	int error;
18294c8fcd8SPeter Wemm 
183708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
18494c8fcd8SPeter Wemm 		return (EINVAL);
185708e7684SPeter Wemm 	error = 0;
18694c8fcd8SPeter Wemm 	if (SCARG(uap, tp)) {
18794c8fcd8SPeter Wemm 		ts.tv_sec = 0;
1887ec73f64SPoul-Henning Kamp 		ts.tv_nsec = 1000000000 / timecounter->frequency;
18994c8fcd8SPeter Wemm 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
19094c8fcd8SPeter Wemm 	}
191708e7684SPeter Wemm 	return (error);
19294c8fcd8SPeter Wemm }
19394c8fcd8SPeter Wemm 
19494c8fcd8SPeter Wemm static int nanowait;
19594c8fcd8SPeter Wemm 
1965b870b7bSPeter Wemm static int
1975b870b7bSPeter Wemm nanosleep1(p, rqt, rmt)
1985b870b7bSPeter Wemm 	struct proc *p;
1995b870b7bSPeter Wemm 	struct timespec *rqt, *rmt;
2005b870b7bSPeter Wemm {
20100af9731SPoul-Henning Kamp 	struct timespec ts, ts2;
20200af9731SPoul-Henning Kamp 	int error, timo;
2035b870b7bSPeter Wemm 
2047d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
205708e7684SPeter Wemm 		return (EINVAL);
2067d7fb492SBruce Evans 	if (rqt->tv_sec < 0 || rqt->tv_sec == 0 && rqt->tv_nsec == 0)
2077d7fb492SBruce Evans 		return (0);
20894c8fcd8SPeter Wemm 
20900af9731SPoul-Henning Kamp 	getnanoruntime(&ts);
21000af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
21100af9731SPoul-Henning Kamp 	error = 0;
21200af9731SPoul-Henning Kamp 	while (1) {
21300af9731SPoul-Henning Kamp 		getnanoruntime(&ts2);
21400af9731SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
21500af9731SPoul-Henning Kamp 			break;
21600af9731SPoul-Henning Kamp 		else if (ts2.tv_sec + 60 * 60 * 24 * hz < ts.tv_sec)
21700af9731SPoul-Henning Kamp 			timo = 60 * 60 * 24 * hz;
21800af9731SPoul-Henning Kamp 		else if (ts2.tv_sec + 2 < ts.tv_sec) {
21900af9731SPoul-Henning Kamp 			/* Leave one second for the difference in tv_nsec */
22000af9731SPoul-Henning Kamp 			timo = ts.tv_sec - ts2.tv_sec - 1;
22100af9731SPoul-Henning Kamp 			timo *= hz;
22200af9731SPoul-Henning Kamp 		} else {
22300af9731SPoul-Henning Kamp 			timo = (ts.tv_sec - ts2.tv_sec) * 1000000000;
22400af9731SPoul-Henning Kamp 			timo += ts.tv_nsec - ts2.tv_nsec;
22500af9731SPoul-Henning Kamp 			timo /= (1000000000 / hz);
22600af9731SPoul-Henning Kamp 			timo ++;
22776aab1daSAndrey A. Chernov 		}
2285b870b7bSPeter Wemm 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", timo);
22900af9731SPoul-Henning Kamp 		if (error == ERESTART) {
23094c8fcd8SPeter Wemm 			error = EINTR;
23100af9731SPoul-Henning Kamp 			break;
2325b870b7bSPeter Wemm 		}
23376aab1daSAndrey A. Chernov 	}
23400af9731SPoul-Henning Kamp 	if (rmt) {
23500af9731SPoul-Henning Kamp 		*rmt = ts;
23600af9731SPoul-Henning Kamp 		timespecsub(rmt, &ts2);
23700af9731SPoul-Henning Kamp 	}
23800af9731SPoul-Henning Kamp 	return(error);
2395b870b7bSPeter Wemm }
24094c8fcd8SPeter Wemm 
2415b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
2425b870b7bSPeter Wemm struct nanosleep_args {
2435b870b7bSPeter Wemm 	struct	timespec *rqtp;
2445b870b7bSPeter Wemm 	struct	timespec *rmtp;
2455b870b7bSPeter Wemm };
2465b870b7bSPeter Wemm #endif
2475b870b7bSPeter Wemm 
2485b870b7bSPeter Wemm /* ARGSUSED */
2495b870b7bSPeter Wemm int
250cb226aaaSPoul-Henning Kamp nanosleep(p, uap)
2515b870b7bSPeter Wemm 	struct proc *p;
2525b870b7bSPeter Wemm 	struct nanosleep_args *uap;
2535b870b7bSPeter Wemm {
2545b870b7bSPeter Wemm 	struct timespec rmt, rqt;
2555b870b7bSPeter Wemm 	int error, error2;
2565b870b7bSPeter Wemm 
2575b870b7bSPeter Wemm 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
2585b870b7bSPeter Wemm 	if (error)
2595b870b7bSPeter Wemm 		return (error);
260bf5acbf5SPeter Wemm 	if (SCARG(uap, rmtp))
2615b870b7bSPeter Wemm 		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), B_WRITE))
2625b870b7bSPeter Wemm 			return (EFAULT);
2635b870b7bSPeter Wemm 	error = nanosleep1(p, &rqt, &rmt);
2645b870b7bSPeter Wemm 	if (SCARG(uap, rmtp)) {
265708e7684SPeter Wemm 		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
2665b870b7bSPeter Wemm 		if (error2)	/* XXX shouldn't happen, did useracc() above */
267708e7684SPeter Wemm 			return (error2);
268708e7684SPeter Wemm 	}
269708e7684SPeter Wemm 	return (error);
27094c8fcd8SPeter Wemm }
27194c8fcd8SPeter Wemm 
2725b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
2735b870b7bSPeter Wemm struct signanosleep_args {
2745b870b7bSPeter Wemm 	struct	timespec *rqtp;
2755b870b7bSPeter Wemm 	struct	timespec *rmtp;
2765b870b7bSPeter Wemm 	sigset_t *mask;
2775b870b7bSPeter Wemm };
2785b870b7bSPeter Wemm #endif
2795b870b7bSPeter Wemm 
2805b870b7bSPeter Wemm /* ARGSUSED */
2815b870b7bSPeter Wemm int
282cb226aaaSPoul-Henning Kamp signanosleep(p, uap)
2835b870b7bSPeter Wemm 	struct proc *p;
2845b870b7bSPeter Wemm 	struct signanosleep_args *uap;
2855b870b7bSPeter Wemm {
2865b870b7bSPeter Wemm 	struct timespec rmt, rqt;
2875b870b7bSPeter Wemm 	int error, error2;
2885b870b7bSPeter Wemm 	sigset_t mask;
2895b870b7bSPeter Wemm 
2905b870b7bSPeter Wemm 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
2915b870b7bSPeter Wemm 	if (error)
2925b870b7bSPeter Wemm 		return (error);
293bf5acbf5SPeter Wemm 	if (SCARG(uap, rmtp))
2945b870b7bSPeter Wemm 		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), B_WRITE))
2955b870b7bSPeter Wemm 			return (EFAULT);
2965b870b7bSPeter Wemm 	error = copyin(SCARG(uap, mask), &mask, sizeof(mask));
2975b870b7bSPeter Wemm 	if (error)
2985b870b7bSPeter Wemm 		return (error);
2995b870b7bSPeter Wemm 
30098716364SPeter Wemm 	/* change mask for sleep */
3015b870b7bSPeter Wemm 	p->p_sigmask = mask &~ sigcantmask;
3025b870b7bSPeter Wemm 
3035b870b7bSPeter Wemm 	error = nanosleep1(p, &rqt, &rmt);
3045b870b7bSPeter Wemm 
3055b870b7bSPeter Wemm 	if (SCARG(uap, rmtp)) {
3065b870b7bSPeter Wemm 		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
3075b870b7bSPeter Wemm 		if (error2)	/* XXX shouldn't happen, did useracc() above */
3085b870b7bSPeter Wemm 			return (error2);
3095b870b7bSPeter Wemm 	}
3105b870b7bSPeter Wemm 	return (error);
3115b870b7bSPeter Wemm }
31280c07b66SBruce Evans 
313d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
314df8bae1dSRodney W. Grimes struct gettimeofday_args {
315df8bae1dSRodney W. Grimes 	struct	timeval *tp;
316df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
317df8bae1dSRodney W. Grimes };
318d2d3e875SBruce Evans #endif
319df8bae1dSRodney W. Grimes /* ARGSUSED */
32026f9a767SRodney W. Grimes int
321cb226aaaSPoul-Henning Kamp gettimeofday(p, uap)
322df8bae1dSRodney W. Grimes 	struct proc *p;
323df8bae1dSRodney W. Grimes 	register struct gettimeofday_args *uap;
324df8bae1dSRodney W. Grimes {
325df8bae1dSRodney W. Grimes 	struct timeval atv;
326df8bae1dSRodney W. Grimes 	int error = 0;
327df8bae1dSRodney W. Grimes 
328df8bae1dSRodney W. Grimes 	if (uap->tp) {
329df8bae1dSRodney W. Grimes 		microtime(&atv);
330bb56ec4aSPoul-Henning Kamp 		if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
331bb56ec4aSPoul-Henning Kamp 		    sizeof (atv))))
332df8bae1dSRodney W. Grimes 			return (error);
333df8bae1dSRodney W. Grimes 	}
334df8bae1dSRodney W. Grimes 	if (uap->tzp)
335df8bae1dSRodney W. Grimes 		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
336df8bae1dSRodney W. Grimes 		    sizeof (tz));
337df8bae1dSRodney W. Grimes 	return (error);
338df8bae1dSRodney W. Grimes }
339df8bae1dSRodney W. Grimes 
340d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
341df8bae1dSRodney W. Grimes struct settimeofday_args {
342df8bae1dSRodney W. Grimes 	struct	timeval *tv;
343df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
344df8bae1dSRodney W. Grimes };
345d2d3e875SBruce Evans #endif
346df8bae1dSRodney W. Grimes /* ARGSUSED */
34726f9a767SRodney W. Grimes int
348cb226aaaSPoul-Henning Kamp settimeofday(p, uap)
349df8bae1dSRodney W. Grimes 	struct proc *p;
350df8bae1dSRodney W. Grimes 	struct settimeofday_args *uap;
351df8bae1dSRodney W. Grimes {
352708e7684SPeter Wemm 	struct timeval atv;
353df8bae1dSRodney W. Grimes 	struct timezone atz;
354708e7684SPeter Wemm 	int error;
355df8bae1dSRodney W. Grimes 
356bb56ec4aSPoul-Henning Kamp 	if ((error = suser(p->p_ucred, &p->p_acflag)))
357df8bae1dSRodney W. Grimes 		return (error);
358df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
359708e7684SPeter Wemm 	if (uap->tv) {
360708e7684SPeter Wemm 		if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
361708e7684SPeter Wemm 		    sizeof(atv))))
362df8bae1dSRodney W. Grimes 			return (error);
3630808c591SBruce Evans 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
3640808c591SBruce Evans 			return (EINVAL);
365708e7684SPeter Wemm 	}
366df8bae1dSRodney W. Grimes 	if (uap->tzp &&
367df8bae1dSRodney W. Grimes 	    (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
368df8bae1dSRodney W. Grimes 		return (error);
36994c8fcd8SPeter Wemm 	if (uap->tv && (error = settime(&atv)))
37094c8fcd8SPeter Wemm 		return (error);
371df8bae1dSRodney W. Grimes 	if (uap->tzp)
372df8bae1dSRodney W. Grimes 		tz = atz;
373df8bae1dSRodney W. Grimes 	return (0);
374df8bae1dSRodney W. Grimes }
375df8bae1dSRodney W. Grimes 
376df8bae1dSRodney W. Grimes int	tickdelta;			/* current clock skew, us. per tick */
377df8bae1dSRodney W. Grimes long	timedelta;			/* unapplied time correction, us. */
37887b6de2bSPoul-Henning Kamp static long	bigadj = 1000000;	/* use 10x skew above bigadj us. */
379df8bae1dSRodney W. Grimes 
380d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
381df8bae1dSRodney W. Grimes struct adjtime_args {
382df8bae1dSRodney W. Grimes 	struct timeval *delta;
383df8bae1dSRodney W. Grimes 	struct timeval *olddelta;
384df8bae1dSRodney W. Grimes };
385d2d3e875SBruce Evans #endif
386df8bae1dSRodney W. Grimes /* ARGSUSED */
38726f9a767SRodney W. Grimes int
388cb226aaaSPoul-Henning Kamp adjtime(p, uap)
389df8bae1dSRodney W. Grimes 	struct proc *p;
390df8bae1dSRodney W. Grimes 	register struct adjtime_args *uap;
391df8bae1dSRodney W. Grimes {
392df8bae1dSRodney W. Grimes 	struct timeval atv;
393df8bae1dSRodney W. Grimes 	register long ndelta, ntickdelta, odelta;
394df8bae1dSRodney W. Grimes 	int s, error;
395df8bae1dSRodney W. Grimes 
396bb56ec4aSPoul-Henning Kamp 	if ((error = suser(p->p_ucred, &p->p_acflag)))
397df8bae1dSRodney W. Grimes 		return (error);
398bb56ec4aSPoul-Henning Kamp 	if ((error =
399bb56ec4aSPoul-Henning Kamp 	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
400df8bae1dSRodney W. Grimes 		return (error);
401df8bae1dSRodney W. Grimes 
402df8bae1dSRodney W. Grimes 	/*
403df8bae1dSRodney W. Grimes 	 * Compute the total correction and the rate at which to apply it.
404df8bae1dSRodney W. Grimes 	 * Round the adjustment down to a whole multiple of the per-tick
405df8bae1dSRodney W. Grimes 	 * delta, so that after some number of incremental changes in
406df8bae1dSRodney W. Grimes 	 * hardclock(), tickdelta will become zero, lest the correction
407df8bae1dSRodney W. Grimes 	 * overshoot and start taking us away from the desired final time.
408df8bae1dSRodney W. Grimes 	 */
409df8bae1dSRodney W. Grimes 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
4100cd97b20SBruce Evans 	if (ndelta > bigadj || ndelta < -bigadj)
411df8bae1dSRodney W. Grimes 		ntickdelta = 10 * tickadj;
412df8bae1dSRodney W. Grimes 	else
413df8bae1dSRodney W. Grimes 		ntickdelta = tickadj;
414df8bae1dSRodney W. Grimes 	if (ndelta % ntickdelta)
415df8bae1dSRodney W. Grimes 		ndelta = ndelta / ntickdelta * ntickdelta;
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes 	/*
418df8bae1dSRodney W. Grimes 	 * To make hardclock()'s job easier, make the per-tick delta negative
419df8bae1dSRodney W. Grimes 	 * if we want time to run slower; then hardclock can simply compute
420df8bae1dSRodney W. Grimes 	 * tick + tickdelta, and subtract tickdelta from timedelta.
421df8bae1dSRodney W. Grimes 	 */
422df8bae1dSRodney W. Grimes 	if (ndelta < 0)
423df8bae1dSRodney W. Grimes 		ntickdelta = -ntickdelta;
424df8bae1dSRodney W. Grimes 	s = splclock();
425df8bae1dSRodney W. Grimes 	odelta = timedelta;
426df8bae1dSRodney W. Grimes 	timedelta = ndelta;
427df8bae1dSRodney W. Grimes 	tickdelta = ntickdelta;
428df8bae1dSRodney W. Grimes 	splx(s);
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes 	if (uap->olddelta) {
431df8bae1dSRodney W. Grimes 		atv.tv_sec = odelta / 1000000;
432df8bae1dSRodney W. Grimes 		atv.tv_usec = odelta % 1000000;
433df8bae1dSRodney W. Grimes 		(void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
434df8bae1dSRodney W. Grimes 		    sizeof(struct timeval));
435df8bae1dSRodney W. Grimes 	}
436df8bae1dSRodney W. Grimes 	return (0);
437df8bae1dSRodney W. Grimes }
438df8bae1dSRodney W. Grimes 
439df8bae1dSRodney W. Grimes /*
440df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
441df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
442df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
443df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
444df8bae1dSRodney W. Grimes  *
445df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
446df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
447df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
448df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
449df8bae1dSRodney W. Grimes  *
450df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
451df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
452df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
453df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
454df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
455df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
456df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
457df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
458df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
459df8bae1dSRodney W. Grimes  */
460d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
461df8bae1dSRodney W. Grimes struct getitimer_args {
462df8bae1dSRodney W. Grimes 	u_int	which;
463df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
464df8bae1dSRodney W. Grimes };
465d2d3e875SBruce Evans #endif
466df8bae1dSRodney W. Grimes /* ARGSUSED */
46726f9a767SRodney W. Grimes int
468cb226aaaSPoul-Henning Kamp getitimer(p, uap)
469df8bae1dSRodney W. Grimes 	struct proc *p;
470df8bae1dSRodney W. Grimes 	register struct getitimer_args *uap;
471df8bae1dSRodney W. Grimes {
472227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
473df8bae1dSRodney W. Grimes 	struct itimerval aitv;
474df8bae1dSRodney W. Grimes 	int s;
475df8bae1dSRodney W. Grimes 
476df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
477df8bae1dSRodney W. Grimes 		return (EINVAL);
478227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX still needed ? */
479df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
480df8bae1dSRodney W. Grimes 		/*
481df8bae1dSRodney W. Grimes 		 * Convert from absoulte to relative time in .it_value
482df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
483df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
484df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
485df8bae1dSRodney W. Grimes 		 */
486df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
487227ee8a1SPoul-Henning Kamp 		if (timerisset(&aitv.it_value)) {
488227ee8a1SPoul-Henning Kamp 			getmicrotime(&ctv);
489227ee8a1SPoul-Henning Kamp 			if (timercmp(&aitv.it_value, &ctv, <))
490df8bae1dSRodney W. Grimes 				timerclear(&aitv.it_value);
491df8bae1dSRodney W. Grimes 			else
492227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
493227ee8a1SPoul-Henning Kamp 		}
494df8bae1dSRodney W. Grimes 	} else
495df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
496df8bae1dSRodney W. Grimes 	splx(s);
497df8bae1dSRodney W. Grimes 	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
498df8bae1dSRodney W. Grimes 	    sizeof (struct itimerval)));
499df8bae1dSRodney W. Grimes }
500df8bae1dSRodney W. Grimes 
501d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
502df8bae1dSRodney W. Grimes struct setitimer_args {
503df8bae1dSRodney W. Grimes 	u_int	which;
504df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
505df8bae1dSRodney W. Grimes };
506d2d3e875SBruce Evans #endif
507df8bae1dSRodney W. Grimes /* ARGSUSED */
50826f9a767SRodney W. Grimes int
509cb226aaaSPoul-Henning Kamp setitimer(p, uap)
510df8bae1dSRodney W. Grimes 	struct proc *p;
511df8bae1dSRodney W. Grimes 	register struct setitimer_args *uap;
512df8bae1dSRodney W. Grimes {
513df8bae1dSRodney W. Grimes 	struct itimerval aitv;
514227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
515df8bae1dSRodney W. Grimes 	register struct itimerval *itvp;
516df8bae1dSRodney W. Grimes 	int s, error;
517df8bae1dSRodney W. Grimes 
518df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
519df8bae1dSRodney W. Grimes 		return (EINVAL);
520df8bae1dSRodney W. Grimes 	itvp = uap->itv;
521df8bae1dSRodney W. Grimes 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
522df8bae1dSRodney W. Grimes 	    sizeof(struct itimerval))))
523df8bae1dSRodney W. Grimes 		return (error);
5240808c591SBruce Evans 	if ((uap->itv = uap->oitv) &&
525cb226aaaSPoul-Henning Kamp 	    (error = getitimer(p, (struct getitimer_args *)uap)))
526df8bae1dSRodney W. Grimes 		return (error);
527df8bae1dSRodney W. Grimes 	if (itvp == 0)
528df8bae1dSRodney W. Grimes 		return (0);
529cd9f713dSAndrey A. Chernov 	if (itimerfix(&aitv.it_value))
530cd9f713dSAndrey A. Chernov 		return (EINVAL);
531cd9f713dSAndrey A. Chernov 	if (!timerisset(&aitv.it_value))
532cd9f713dSAndrey A. Chernov 		timerclear(&aitv.it_interval);
533cd9f713dSAndrey A. Chernov 	else if (itimerfix(&aitv.it_interval))
534df8bae1dSRodney W. Grimes 		return (EINVAL);
535227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX: still needed ? */
536df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
537ab36c067SJustin T. Gibbs 		if (timerisset(&p->p_realtimer.it_value))
538ab36c067SJustin T. Gibbs 			untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
539df8bae1dSRodney W. Grimes 		if (timerisset(&aitv.it_value)) {
540227ee8a1SPoul-Henning Kamp 			getmicrotime(&ctv);
541227ee8a1SPoul-Henning Kamp 			timevaladd(&aitv.it_value, &ctv);
542ab36c067SJustin T. Gibbs 			p->p_ithandle = timeout(realitexpire, (caddr_t)p,
543ab36c067SJustin T. Gibbs 						hzto(&aitv.it_value));
544df8bae1dSRodney W. Grimes 		}
545df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
546df8bae1dSRodney W. Grimes 	} else
547df8bae1dSRodney W. Grimes 		p->p_stats->p_timer[uap->which] = aitv;
548df8bae1dSRodney W. Grimes 	splx(s);
549df8bae1dSRodney W. Grimes 	return (0);
550df8bae1dSRodney W. Grimes }
551df8bae1dSRodney W. Grimes 
552df8bae1dSRodney W. Grimes /*
553df8bae1dSRodney W. Grimes  * Real interval timer expired:
554df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
555df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
556df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
557df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
558df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
5599207f00aSBruce Evans  * hzto() always adds 1 to allow for the time until the next clock
5609207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
5619207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
5629207f00aSBruce Evans  * interrupt even when we're delayed.
563df8bae1dSRodney W. Grimes  */
564df8bae1dSRodney W. Grimes void
565df8bae1dSRodney W. Grimes realitexpire(arg)
566df8bae1dSRodney W. Grimes 	void *arg;
567df8bae1dSRodney W. Grimes {
568df8bae1dSRodney W. Grimes 	register struct proc *p;
569227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
570df8bae1dSRodney W. Grimes 	int s;
571df8bae1dSRodney W. Grimes 
572df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
573df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
574df8bae1dSRodney W. Grimes 	if (!timerisset(&p->p_realtimer.it_interval)) {
575df8bae1dSRodney W. Grimes 		timerclear(&p->p_realtimer.it_value);
576df8bae1dSRodney W. Grimes 		return;
577df8bae1dSRodney W. Grimes 	}
578df8bae1dSRodney W. Grimes 	for (;;) {
579227ee8a1SPoul-Henning Kamp 		s = splclock(); /* XXX: still neeeded ? */
580df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
581df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
582227ee8a1SPoul-Henning Kamp 		getmicrotime(&ctv);
583227ee8a1SPoul-Henning Kamp 		if (timercmp(&p->p_realtimer.it_value, &ctv, >)) {
584ab36c067SJustin T. Gibbs 			p->p_ithandle =
585df8bae1dSRodney W. Grimes 			    timeout(realitexpire, (caddr_t)p,
5869207f00aSBruce Evans 				    hzto(&p->p_realtimer.it_value) - 1);
587df8bae1dSRodney W. Grimes 			splx(s);
588df8bae1dSRodney W. Grimes 			return;
589df8bae1dSRodney W. Grimes 		}
590df8bae1dSRodney W. Grimes 		splx(s);
591df8bae1dSRodney W. Grimes 	}
592df8bae1dSRodney W. Grimes }
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes /*
595df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
596df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
597df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
598df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
599df8bae1dSRodney W. Grimes  */
60026f9a767SRodney W. Grimes int
601df8bae1dSRodney W. Grimes itimerfix(tv)
602df8bae1dSRodney W. Grimes 	struct timeval *tv;
603df8bae1dSRodney W. Grimes {
604df8bae1dSRodney W. Grimes 
605df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
606df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
607df8bae1dSRodney W. Grimes 		return (EINVAL);
608df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
609df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
610df8bae1dSRodney W. Grimes 	return (0);
611df8bae1dSRodney W. Grimes }
612df8bae1dSRodney W. Grimes 
613df8bae1dSRodney W. Grimes /*
614df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
615df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
616df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
617df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
618df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
619df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
620df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
621df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
622df8bae1dSRodney W. Grimes  */
62326f9a767SRodney W. Grimes int
624df8bae1dSRodney W. Grimes itimerdecr(itp, usec)
625df8bae1dSRodney W. Grimes 	register struct itimerval *itp;
626df8bae1dSRodney W. Grimes 	int usec;
627df8bae1dSRodney W. Grimes {
628df8bae1dSRodney W. Grimes 
629df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
630df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
631df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
632df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
633df8bae1dSRodney W. Grimes 			goto expire;
634df8bae1dSRodney W. Grimes 		}
635df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
636df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
637df8bae1dSRodney W. Grimes 	}
638df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
639df8bae1dSRodney W. Grimes 	usec = 0;
640df8bae1dSRodney W. Grimes 	if (timerisset(&itp->it_value))
641df8bae1dSRodney W. Grimes 		return (1);
642df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
643df8bae1dSRodney W. Grimes expire:
644df8bae1dSRodney W. Grimes 	if (timerisset(&itp->it_interval)) {
645df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
646df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
647df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
648df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
649df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
650df8bae1dSRodney W. Grimes 		}
651df8bae1dSRodney W. Grimes 	} else
652df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
653df8bae1dSRodney W. Grimes 	return (0);
654df8bae1dSRodney W. Grimes }
655df8bae1dSRodney W. Grimes 
656df8bae1dSRodney W. Grimes /*
657df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
658df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
659df8bae1dSRodney W. Grimes  * results which are before the beginning,
660df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
661df8bae1dSRodney W. Grimes  * Caveat emptor.
662df8bae1dSRodney W. Grimes  */
66326f9a767SRodney W. Grimes void
664df8bae1dSRodney W. Grimes timevaladd(t1, t2)
665df8bae1dSRodney W. Grimes 	struct timeval *t1, *t2;
666df8bae1dSRodney W. Grimes {
667df8bae1dSRodney W. Grimes 
668df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
669df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
670df8bae1dSRodney W. Grimes 	timevalfix(t1);
671df8bae1dSRodney W. Grimes }
672df8bae1dSRodney W. Grimes 
67326f9a767SRodney W. Grimes void
674df8bae1dSRodney W. Grimes timevalsub(t1, t2)
675df8bae1dSRodney W. Grimes 	struct timeval *t1, *t2;
676df8bae1dSRodney W. Grimes {
677df8bae1dSRodney W. Grimes 
678df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
679df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
680df8bae1dSRodney W. Grimes 	timevalfix(t1);
681df8bae1dSRodney W. Grimes }
682df8bae1dSRodney W. Grimes 
68387b6de2bSPoul-Henning Kamp static void
684df8bae1dSRodney W. Grimes timevalfix(t1)
685df8bae1dSRodney W. Grimes 	struct timeval *t1;
686df8bae1dSRodney W. Grimes {
687df8bae1dSRodney W. Grimes 
688df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
689df8bae1dSRodney W. Grimes 		t1->tv_sec--;
690df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
691df8bae1dSRodney W. Grimes 	}
692df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
693df8bae1dSRodney W. Grimes 		t1->tv_sec++;
694df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
695df8bae1dSRodney W. Grimes 	}
696df8bae1dSRodney W. Grimes }
697