xref: /freebsd/sys/kern/kern_time.c (revision 7edfb592df21762fd4fd75974f9715b78381ccaf)
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
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38e96c1fdcSPeter Wemm #include <sys/systm.h>
39fb919e4dSMark Murray #include <sys/lock.h>
40fb919e4dSMark Murray #include <sys/mutex.h>
41d2d3e875SBruce Evans #include <sys/sysproto.h>
42df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
43797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
44df8bae1dSRodney W. Grimes #include <sys/kernel.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
4694c8fcd8SPeter Wemm #include <sys/sysent.h>
47df8bae1dSRodney W. Grimes #include <sys/proc.h>
48708e7684SPeter Wemm #include <sys/time.h>
4991266b96SPoul-Henning Kamp #include <sys/timetc.h>
50df8bae1dSRodney W. Grimes #include <sys/vnode.h>
51fb919e4dSMark Murray 
525b870b7bSPeter Wemm #include <vm/vm.h>
535b870b7bSPeter Wemm #include <vm/vm_extern.h>
54df8bae1dSRodney W. Grimes 
55ac7e6123SDavid Greenman struct timezone tz;
56ac7e6123SDavid Greenman 
57df8bae1dSRodney W. Grimes /*
58df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
59df8bae1dSRodney W. Grimes  *
60df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
61df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
62df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
63df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
64df8bae1dSRodney W. Grimes  * timers when they expire.
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
674d77a549SAlfred Perlstein static int	nanosleep1(struct thread *td, struct timespec *rqt,
684d77a549SAlfred Perlstein 		    struct timespec *rmt);
697edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
704d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
714d77a549SAlfred Perlstein static void	no_lease_updatetime(int);
7294c8fcd8SPeter Wemm 
731b09ae77SPoul-Henning Kamp static void
741b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
751b09ae77SPoul-Henning Kamp 	int deltat;
761b09ae77SPoul-Henning Kamp {
771b09ae77SPoul-Henning Kamp }
781b09ae77SPoul-Henning Kamp 
794d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
801b09ae77SPoul-Henning Kamp 
8194c8fcd8SPeter Wemm static int
827edfb592SJohn Baldwin settime(td, tv)
837edfb592SJohn Baldwin 	struct thread *td;
8494c8fcd8SPeter Wemm 	struct timeval *tv;
8594c8fcd8SPeter Wemm {
86fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
87c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
887ec73f64SPoul-Henning Kamp 	struct timespec ts;
8994c8fcd8SPeter Wemm 	int s;
9094c8fcd8SPeter Wemm 
91708e7684SPeter Wemm 	s = splclock();
929c8fff87SBruce Evans 	microtime(&tv1);
9300af9731SPoul-Henning Kamp 	delta = *tv;
9400af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
9594c8fcd8SPeter Wemm 
9694c8fcd8SPeter Wemm 	/*
979c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
98fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
99fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
100fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
101fcae3aa6SNick Sayer 	 * back to the past.
102c0bd94a7SNick Sayer 	 *
103c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
104c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
105c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
10694c8fcd8SPeter Wemm 	 */
1077edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
108fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1093f92429aSMatt Jacob 			/*
110c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1113f92429aSMatt Jacob 			 */
112fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
113fcae3aa6SNick Sayer 				maxtime = tv1;
114fcae3aa6SNick Sayer 			tv2 = *tv;
115fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
116fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1173f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
118fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
119fcae3aa6SNick Sayer 			}
1203f92429aSMatt Jacob 		} else {
121c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
122c0bd94a7SNick Sayer 				splx(s);
123c0bd94a7SNick Sayer 				return (EPERM);
124c0bd94a7SNick Sayer 			}
125c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
126c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
127c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
128c0bd94a7SNick Sayer 			}
129c0bd94a7SNick Sayer 			laststep = *tv;
130fcae3aa6SNick Sayer 		}
1319c8fff87SBruce Evans 	}
1329c8fff87SBruce Evans 
1337ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1347ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1357edfb592SJohn Baldwin 	mtx_lock(&Giant);
13691266b96SPoul-Henning Kamp 	tc_setclock(&ts);
13794c8fcd8SPeter Wemm 	(void) splsoftclock();
13894c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
13994c8fcd8SPeter Wemm 	splx(s);
14094c8fcd8SPeter Wemm 	resettodr();
1417edfb592SJohn Baldwin 	mtx_unlock(&Giant);
14294c8fcd8SPeter Wemm 	return (0);
14394c8fcd8SPeter Wemm }
14494c8fcd8SPeter Wemm 
14594c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
14694c8fcd8SPeter Wemm struct clock_gettime_args {
14794c8fcd8SPeter Wemm 	clockid_t clock_id;
14894c8fcd8SPeter Wemm 	struct	timespec *tp;
14994c8fcd8SPeter Wemm };
15094c8fcd8SPeter Wemm #endif
151708e7684SPeter Wemm 
152fb99ab88SMatthew Dillon /*
153fb99ab88SMatthew Dillon  * MPSAFE
154fb99ab88SMatthew Dillon  */
15594c8fcd8SPeter Wemm /* ARGSUSED */
15694c8fcd8SPeter Wemm int
157b40ce416SJulian Elischer clock_gettime(td, uap)
158b40ce416SJulian Elischer 	struct thread *td;
15994c8fcd8SPeter Wemm 	struct clock_gettime_args *uap;
16094c8fcd8SPeter Wemm {
16194c8fcd8SPeter Wemm 	struct timespec ats;
16294c8fcd8SPeter Wemm 
163708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
16494c8fcd8SPeter Wemm 		return (EINVAL);
165fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1667ec73f64SPoul-Henning Kamp 	nanotime(&ats);
167fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
168708e7684SPeter Wemm 	return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
16994c8fcd8SPeter Wemm }
17094c8fcd8SPeter Wemm 
17194c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
17294c8fcd8SPeter Wemm struct clock_settime_args {
17394c8fcd8SPeter Wemm 	clockid_t clock_id;
17494c8fcd8SPeter Wemm 	const struct	timespec *tp;
17594c8fcd8SPeter Wemm };
17694c8fcd8SPeter Wemm #endif
177708e7684SPeter Wemm 
178fb99ab88SMatthew Dillon /*
179fb99ab88SMatthew Dillon  * MPSAFE
180fb99ab88SMatthew Dillon  */
18194c8fcd8SPeter Wemm /* ARGSUSED */
18294c8fcd8SPeter Wemm int
183b40ce416SJulian Elischer clock_settime(td, uap)
184b40ce416SJulian Elischer 	struct thread *td;
18594c8fcd8SPeter Wemm 	struct clock_settime_args *uap;
18694c8fcd8SPeter Wemm {
18794c8fcd8SPeter Wemm 	struct timeval atv;
18894c8fcd8SPeter Wemm 	struct timespec ats;
18994c8fcd8SPeter Wemm 	int error;
19094c8fcd8SPeter Wemm 
19144731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
1927edfb592SJohn Baldwin 		return (error);
1937edfb592SJohn Baldwin 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
1947edfb592SJohn Baldwin 		return (EINVAL);
19594c8fcd8SPeter Wemm 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
1967edfb592SJohn Baldwin 		return (error);
1977edfb592SJohn Baldwin 	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
1987edfb592SJohn Baldwin 		return (EINVAL);
199a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
20094c8fcd8SPeter Wemm 	TIMESPEC_TO_TIMEVAL(&atv, &ats);
2017edfb592SJohn Baldwin 	error = settime(td, &atv);
20294c8fcd8SPeter Wemm 	return (error);
20394c8fcd8SPeter Wemm }
20494c8fcd8SPeter Wemm 
20594c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
20694c8fcd8SPeter Wemm struct clock_getres_args {
20794c8fcd8SPeter Wemm 	clockid_t clock_id;
20894c8fcd8SPeter Wemm 	struct	timespec *tp;
20994c8fcd8SPeter Wemm };
21094c8fcd8SPeter Wemm #endif
211708e7684SPeter Wemm 
21294c8fcd8SPeter Wemm int
213b40ce416SJulian Elischer clock_getres(td, uap)
214b40ce416SJulian Elischer 	struct thread *td;
21594c8fcd8SPeter Wemm 	struct clock_getres_args *uap;
21694c8fcd8SPeter Wemm {
21794c8fcd8SPeter Wemm 	struct timespec ts;
218708e7684SPeter Wemm 	int error;
21994c8fcd8SPeter Wemm 
220708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
22194c8fcd8SPeter Wemm 		return (EINVAL);
222708e7684SPeter Wemm 	error = 0;
22394c8fcd8SPeter Wemm 	if (SCARG(uap, tp)) {
22494c8fcd8SPeter Wemm 		ts.tv_sec = 0;
225a58f0f8eSPoul-Henning Kamp 		ts.tv_nsec = 1000000000 / timecounter->tc_frequency;
22694c8fcd8SPeter Wemm 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
22794c8fcd8SPeter Wemm 	}
228708e7684SPeter Wemm 	return (error);
22994c8fcd8SPeter Wemm }
23094c8fcd8SPeter Wemm 
23194c8fcd8SPeter Wemm static int nanowait;
23294c8fcd8SPeter Wemm 
2335b870b7bSPeter Wemm static int
234b40ce416SJulian Elischer nanosleep1(td, rqt, rmt)
235b40ce416SJulian Elischer 	struct thread *td;
2365b870b7bSPeter Wemm 	struct timespec *rqt, *rmt;
2375b870b7bSPeter Wemm {
2385704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
23933841826SPoul-Henning Kamp 	struct timeval tv;
24033841826SPoul-Henning Kamp 	int error;
2415b870b7bSPeter Wemm 
2427d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
243708e7684SPeter Wemm 		return (EINVAL);
244d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
2457d7fb492SBruce Evans 		return (0);
246c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
24700af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
24833841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
24933841826SPoul-Henning Kamp 	for (;;) {
25033841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
25133841826SPoul-Henning Kamp 		    tvtohz(&tv));
252c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
25333841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
25433841826SPoul-Henning Kamp 			if (error == ERESTART)
25594c8fcd8SPeter Wemm 				error = EINTR;
25633841826SPoul-Henning Kamp 			if (rmt != NULL) {
25733841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
25833841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
25933841826SPoul-Henning Kamp 					timespecclear(&ts);
26000af9731SPoul-Henning Kamp 				*rmt = ts;
26100af9731SPoul-Henning Kamp 			}
26233841826SPoul-Henning Kamp 			return (error);
26333841826SPoul-Henning Kamp 		}
26433841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
26533841826SPoul-Henning Kamp 			return (0);
2665704ba6aSPoul-Henning Kamp 		ts3 = ts;
2675704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
2685704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
26933841826SPoul-Henning Kamp 	}
2705b870b7bSPeter Wemm }
27194c8fcd8SPeter Wemm 
2725b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
2735b870b7bSPeter Wemm struct nanosleep_args {
2745b870b7bSPeter Wemm 	struct	timespec *rqtp;
2755b870b7bSPeter Wemm 	struct	timespec *rmtp;
2765b870b7bSPeter Wemm };
2775b870b7bSPeter Wemm #endif
2785b870b7bSPeter Wemm 
279fb99ab88SMatthew Dillon /*
280fb99ab88SMatthew Dillon  * MPSAFE
281fb99ab88SMatthew Dillon  */
2825b870b7bSPeter Wemm /* ARGSUSED */
2835b870b7bSPeter Wemm int
284b40ce416SJulian Elischer nanosleep(td, uap)
285b40ce416SJulian Elischer 	struct thread *td;
2865b870b7bSPeter Wemm 	struct nanosleep_args *uap;
2875b870b7bSPeter Wemm {
2885b870b7bSPeter Wemm 	struct timespec rmt, rqt;
289fb99ab88SMatthew Dillon 	int error;
2905b870b7bSPeter Wemm 
2915b870b7bSPeter Wemm 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
2925b870b7bSPeter Wemm 	if (error)
2935b870b7bSPeter Wemm 		return (error);
294fb99ab88SMatthew Dillon 
295fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
296fb99ab88SMatthew Dillon 	if (SCARG(uap, rmtp)) {
29702c58685SPoul-Henning Kamp 		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
298fb99ab88SMatthew Dillon 		    VM_PROT_WRITE)) {
299fb99ab88SMatthew Dillon 			error = EFAULT;
300fb99ab88SMatthew Dillon 			goto done2;
301fb99ab88SMatthew Dillon 		}
302fb99ab88SMatthew Dillon 	}
303b40ce416SJulian Elischer 	error = nanosleep1(td, &rqt, &rmt);
304d59fbbf6SPeter Wemm 	if (error && SCARG(uap, rmtp)) {
305fb99ab88SMatthew Dillon 		int error2;
306fb99ab88SMatthew Dillon 
307708e7684SPeter Wemm 		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
3085b870b7bSPeter Wemm 		if (error2)	/* XXX shouldn't happen, did useracc() above */
309fb99ab88SMatthew Dillon 			error = error2;
310708e7684SPeter Wemm 	}
311fb99ab88SMatthew Dillon done2:
312fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
313708e7684SPeter Wemm 	return (error);
31494c8fcd8SPeter Wemm }
31594c8fcd8SPeter Wemm 
3165b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
317df8bae1dSRodney W. Grimes struct gettimeofday_args {
318df8bae1dSRodney W. Grimes 	struct	timeval *tp;
319df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
320df8bae1dSRodney W. Grimes };
321d2d3e875SBruce Evans #endif
322fb99ab88SMatthew Dillon /*
323fb99ab88SMatthew Dillon  * MPSAFE
324fb99ab88SMatthew Dillon  */
325df8bae1dSRodney W. Grimes /* ARGSUSED */
32626f9a767SRodney W. Grimes int
327b40ce416SJulian Elischer gettimeofday(td, uap)
328b40ce416SJulian Elischer 	struct thread *td;
329df8bae1dSRodney W. Grimes 	register struct gettimeofday_args *uap;
330df8bae1dSRodney W. Grimes {
331df8bae1dSRodney W. Grimes 	struct timeval atv;
332df8bae1dSRodney W. Grimes 	int error = 0;
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes 	if (uap->tp) {
335df8bae1dSRodney W. Grimes 		microtime(&atv);
33621dcdb38SPoul-Henning Kamp 		error = copyout((caddr_t)&atv, (caddr_t)uap->tp, sizeof (atv));
337df8bae1dSRodney W. Grimes 	}
33821dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
33921dcdb38SPoul-Henning Kamp 		mtx_lock(&Giant);
340df8bae1dSRodney W. Grimes 		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
341df8bae1dSRodney W. Grimes 		    sizeof (tz));
342fb99ab88SMatthew Dillon 		mtx_unlock(&Giant);
34321dcdb38SPoul-Henning Kamp 	}
344df8bae1dSRodney W. Grimes 	return (error);
345df8bae1dSRodney W. Grimes }
346df8bae1dSRodney W. Grimes 
347d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
348df8bae1dSRodney W. Grimes struct settimeofday_args {
349df8bae1dSRodney W. Grimes 	struct	timeval *tv;
350df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
351df8bae1dSRodney W. Grimes };
352d2d3e875SBruce Evans #endif
353fb99ab88SMatthew Dillon /*
354fb99ab88SMatthew Dillon  * MPSAFE
355fb99ab88SMatthew Dillon  */
356df8bae1dSRodney W. Grimes /* ARGSUSED */
35726f9a767SRodney W. Grimes int
358b40ce416SJulian Elischer settimeofday(td, uap)
359b40ce416SJulian Elischer 	struct thread *td;
360df8bae1dSRodney W. Grimes 	struct settimeofday_args *uap;
361df8bae1dSRodney W. Grimes {
362708e7684SPeter Wemm 	struct timeval atv;
363df8bae1dSRodney W. Grimes 	struct timezone atz;
364fb99ab88SMatthew Dillon 	int error = 0;
365fb99ab88SMatthew Dillon 
36644731cabSJohn Baldwin 	if ((error = suser(td)))
3677edfb592SJohn Baldwin 		return (error);
368df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
369708e7684SPeter Wemm 	if (uap->tv) {
370708e7684SPeter Wemm 		if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
3717edfb592SJohn Baldwin 		    sizeof(atv))))
3727edfb592SJohn Baldwin 			return (error);
3737edfb592SJohn Baldwin 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
3747edfb592SJohn Baldwin 			return (EINVAL);
375708e7684SPeter Wemm 	}
376df8bae1dSRodney W. Grimes 	if (uap->tzp &&
3777edfb592SJohn Baldwin 	    (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
3787edfb592SJohn Baldwin 		return (error);
3797edfb592SJohn Baldwin 
3807edfb592SJohn Baldwin 	if (uap->tv && (error = settime(td, &atv)))
3817edfb592SJohn Baldwin 		return (error);
3827edfb592SJohn Baldwin 	if (uap->tzp) {
3837edfb592SJohn Baldwin 		mtx_lock(&Giant);
384df8bae1dSRodney W. Grimes 		tz = atz;
385fb99ab88SMatthew Dillon 		mtx_unlock(&Giant);
3867edfb592SJohn Baldwin 	}
387fb99ab88SMatthew Dillon 	return (error);
388df8bae1dSRodney W. Grimes }
389df8bae1dSRodney W. Grimes 
390df8bae1dSRodney W. Grimes int	tickdelta;			/* current clock skew, us. per tick */
391df8bae1dSRodney W. Grimes long	timedelta;			/* unapplied time correction, us. */
39287b6de2bSPoul-Henning Kamp static long	bigadj = 1000000;	/* use 10x skew above bigadj us. */
393df8bae1dSRodney W. Grimes 
394d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
395df8bae1dSRodney W. Grimes struct adjtime_args {
396df8bae1dSRodney W. Grimes 	struct timeval *delta;
397df8bae1dSRodney W. Grimes 	struct timeval *olddelta;
398df8bae1dSRodney W. Grimes };
399d2d3e875SBruce Evans #endif
400fb99ab88SMatthew Dillon /*
401fb99ab88SMatthew Dillon  * MPSAFE
402fb99ab88SMatthew Dillon  */
403df8bae1dSRodney W. Grimes /* ARGSUSED */
40426f9a767SRodney W. Grimes int
405b40ce416SJulian Elischer adjtime(td, uap)
406b40ce416SJulian Elischer 	struct thread *td;
407df8bae1dSRodney W. Grimes 	register struct adjtime_args *uap;
408df8bae1dSRodney W. Grimes {
409df8bae1dSRodney W. Grimes 	struct timeval atv;
410df8bae1dSRodney W. Grimes 	register long ndelta, ntickdelta, odelta;
411df8bae1dSRodney W. Grimes 	int s, error;
412df8bae1dSRodney W. Grimes 
413fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
414fb99ab88SMatthew Dillon 
41544731cabSJohn Baldwin 	if ((error = suser(td)))
416fb99ab88SMatthew Dillon 		goto done2;
417fb99ab88SMatthew Dillon 	error = copyin((caddr_t)uap->delta, (caddr_t)&atv,
418fb99ab88SMatthew Dillon 		    sizeof(struct timeval));
419fb99ab88SMatthew Dillon 	if (error)
420fb99ab88SMatthew Dillon 		goto done2;
421df8bae1dSRodney W. Grimes 
422df8bae1dSRodney W. Grimes 	/*
423df8bae1dSRodney W. Grimes 	 * Compute the total correction and the rate at which to apply it.
424df8bae1dSRodney W. Grimes 	 * Round the adjustment down to a whole multiple of the per-tick
425df8bae1dSRodney W. Grimes 	 * delta, so that after some number of incremental changes in
426df8bae1dSRodney W. Grimes 	 * hardclock(), tickdelta will become zero, lest the correction
427df8bae1dSRodney W. Grimes 	 * overshoot and start taking us away from the desired final time.
428df8bae1dSRodney W. Grimes 	 */
429df8bae1dSRodney W. Grimes 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
4300cd97b20SBruce Evans 	if (ndelta > bigadj || ndelta < -bigadj)
431df8bae1dSRodney W. Grimes 		ntickdelta = 10 * tickadj;
432df8bae1dSRodney W. Grimes 	else
433df8bae1dSRodney W. Grimes 		ntickdelta = tickadj;
434df8bae1dSRodney W. Grimes 	if (ndelta % ntickdelta)
435df8bae1dSRodney W. Grimes 		ndelta = ndelta / ntickdelta * ntickdelta;
436df8bae1dSRodney W. Grimes 
437df8bae1dSRodney W. Grimes 	/*
438df8bae1dSRodney W. Grimes 	 * To make hardclock()'s job easier, make the per-tick delta negative
439df8bae1dSRodney W. Grimes 	 * if we want time to run slower; then hardclock can simply compute
440df8bae1dSRodney W. Grimes 	 * tick + tickdelta, and subtract tickdelta from timedelta.
441df8bae1dSRodney W. Grimes 	 */
442df8bae1dSRodney W. Grimes 	if (ndelta < 0)
443df8bae1dSRodney W. Grimes 		ntickdelta = -ntickdelta;
444df8bae1dSRodney W. Grimes 	s = splclock();
445df8bae1dSRodney W. Grimes 	odelta = timedelta;
446df8bae1dSRodney W. Grimes 	timedelta = ndelta;
447df8bae1dSRodney W. Grimes 	tickdelta = ntickdelta;
448df8bae1dSRodney W. Grimes 	splx(s);
449df8bae1dSRodney W. Grimes 
450df8bae1dSRodney W. Grimes 	if (uap->olddelta) {
451df8bae1dSRodney W. Grimes 		atv.tv_sec = odelta / 1000000;
452df8bae1dSRodney W. Grimes 		atv.tv_usec = odelta % 1000000;
453df8bae1dSRodney W. Grimes 		(void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
454df8bae1dSRodney W. Grimes 		    sizeof(struct timeval));
455df8bae1dSRodney W. Grimes 	}
456fb99ab88SMatthew Dillon done2:
457fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
458fb99ab88SMatthew Dillon 	return (error);
459df8bae1dSRodney W. Grimes }
460df8bae1dSRodney W. Grimes 
461df8bae1dSRodney W. Grimes /*
462df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
463df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
464df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
465df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
466df8bae1dSRodney W. Grimes  *
467df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
468df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
469df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
470df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
471df8bae1dSRodney W. Grimes  *
472df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
473df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
474df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
475df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
476df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
477df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
478df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
479df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
480df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
481df8bae1dSRodney W. Grimes  */
482d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
483df8bae1dSRodney W. Grimes struct getitimer_args {
484df8bae1dSRodney W. Grimes 	u_int	which;
485df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
486df8bae1dSRodney W. Grimes };
487d2d3e875SBruce Evans #endif
488fb99ab88SMatthew Dillon /*
489fb99ab88SMatthew Dillon  * MPSAFE
490fb99ab88SMatthew Dillon  */
491df8bae1dSRodney W. Grimes /* ARGSUSED */
49226f9a767SRodney W. Grimes int
493b40ce416SJulian Elischer getitimer(td, uap)
494b40ce416SJulian Elischer 	struct thread *td;
495df8bae1dSRodney W. Grimes 	register struct getitimer_args *uap;
496df8bae1dSRodney W. Grimes {
497b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
498227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
499df8bae1dSRodney W. Grimes 	struct itimerval aitv;
500df8bae1dSRodney W. Grimes 	int s;
501fb99ab88SMatthew Dillon 	int error;
502df8bae1dSRodney W. Grimes 
503df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
504df8bae1dSRodney W. Grimes 		return (EINVAL);
505fb99ab88SMatthew Dillon 
506fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
507fb99ab88SMatthew Dillon 
508227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX still needed ? */
509df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
510df8bae1dSRodney W. Grimes 		/*
511ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
512df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
513df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
514df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
515df8bae1dSRodney W. Grimes 		 */
516df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
5174cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value)) {
518c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
5194cf41af3SPoul-Henning Kamp 			if (timevalcmp(&aitv.it_value, &ctv, <))
5204cf41af3SPoul-Henning Kamp 				timevalclear(&aitv.it_value);
521df8bae1dSRodney W. Grimes 			else
522227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
523227ee8a1SPoul-Henning Kamp 		}
524fb99ab88SMatthew Dillon 	} else {
525df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
526fb99ab88SMatthew Dillon 	}
527df8bae1dSRodney W. Grimes 	splx(s);
528fb99ab88SMatthew Dillon 	error = copyout((caddr_t)&aitv, (caddr_t)uap->itv,
529fb99ab88SMatthew Dillon 	    sizeof (struct itimerval));
530fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
531fb99ab88SMatthew Dillon 	return(error);
532df8bae1dSRodney W. Grimes }
533df8bae1dSRodney W. Grimes 
534d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
535df8bae1dSRodney W. Grimes struct setitimer_args {
536df8bae1dSRodney W. Grimes 	u_int	which;
537df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
538df8bae1dSRodney W. Grimes };
539d2d3e875SBruce Evans #endif
540fb99ab88SMatthew Dillon /*
541fb99ab88SMatthew Dillon  * MPSAFE
542fb99ab88SMatthew Dillon  */
543df8bae1dSRodney W. Grimes /* ARGSUSED */
54426f9a767SRodney W. Grimes int
545b40ce416SJulian Elischer setitimer(td, uap)
546b40ce416SJulian Elischer 	struct thread *td;
547df8bae1dSRodney W. Grimes 	register struct setitimer_args *uap;
548df8bae1dSRodney W. Grimes {
549b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
550df8bae1dSRodney W. Grimes 	struct itimerval aitv;
551227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
552df8bae1dSRodney W. Grimes 	register struct itimerval *itvp;
553fb99ab88SMatthew Dillon 	int s, error = 0;
554df8bae1dSRodney W. Grimes 
555df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
556df8bae1dSRodney W. Grimes 		return (EINVAL);
557df8bae1dSRodney W. Grimes 	itvp = uap->itv;
558df8bae1dSRodney W. Grimes 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
559df8bae1dSRodney W. Grimes 	    sizeof(struct itimerval))))
560df8bae1dSRodney W. Grimes 		return (error);
561fb99ab88SMatthew Dillon 
562fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
563fb99ab88SMatthew Dillon 
5640808c591SBruce Evans 	if ((uap->itv = uap->oitv) &&
565b40ce416SJulian Elischer 	    (error = getitimer(td, (struct getitimer_args *)uap))) {
566fb99ab88SMatthew Dillon 		goto done2;
567fb99ab88SMatthew Dillon 	}
568fb99ab88SMatthew Dillon 	if (itvp == 0) {
569fb99ab88SMatthew Dillon 		error = 0;
570fb99ab88SMatthew Dillon 		goto done2;
571fb99ab88SMatthew Dillon 	}
572fb99ab88SMatthew Dillon 	if (itimerfix(&aitv.it_value)) {
573fb99ab88SMatthew Dillon 		error = EINVAL;
574fb99ab88SMatthew Dillon 		goto done2;
575fb99ab88SMatthew Dillon 	}
576fb99ab88SMatthew Dillon 	if (!timevalisset(&aitv.it_value)) {
5774cf41af3SPoul-Henning Kamp 		timevalclear(&aitv.it_interval);
578fb99ab88SMatthew Dillon 	} else if (itimerfix(&aitv.it_interval)) {
579fb99ab88SMatthew Dillon 		error = EINVAL;
580fb99ab88SMatthew Dillon 		goto done2;
581fb99ab88SMatthew Dillon 	}
582227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX: still needed ? */
583df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
5844cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
5854f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
5864cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value))
5874f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
5884f559836SJake Burkholder 			    realitexpire, p);
589c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
590bfe6c9faSPoul-Henning Kamp 		timevaladd(&aitv.it_value, &ctv);
591df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
592fb99ab88SMatthew Dillon 	} else {
593df8bae1dSRodney W. Grimes 		p->p_stats->p_timer[uap->which] = aitv;
594fb99ab88SMatthew Dillon 	}
595df8bae1dSRodney W. Grimes 	splx(s);
596fb99ab88SMatthew Dillon done2:
597fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
598fb99ab88SMatthew Dillon 	return (error);
599df8bae1dSRodney W. Grimes }
600df8bae1dSRodney W. Grimes 
601df8bae1dSRodney W. Grimes /*
602df8bae1dSRodney W. Grimes  * Real interval timer expired:
603df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
604df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
605df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
606df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
607df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
608c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
6099207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
6109207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
6119207f00aSBruce Evans  * interrupt even when we're delayed.
612df8bae1dSRodney W. Grimes  */
613df8bae1dSRodney W. Grimes void
614df8bae1dSRodney W. Grimes realitexpire(arg)
615df8bae1dSRodney W. Grimes 	void *arg;
616df8bae1dSRodney W. Grimes {
617df8bae1dSRodney W. Grimes 	register struct proc *p;
618bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
619df8bae1dSRodney W. Grimes 	int s;
620df8bae1dSRodney W. Grimes 
621df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
62237824023SJohn Baldwin 	PROC_LOCK(p);
623df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
6244cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
6254cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
62637824023SJohn Baldwin 		PROC_UNLOCK(p);
627df8bae1dSRodney W. Grimes 		return;
628df8bae1dSRodney W. Grimes 	}
629df8bae1dSRodney W. Grimes 	for (;;) {
630227ee8a1SPoul-Henning Kamp 		s = splclock(); /* XXX: still neeeded ? */
631df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
632df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
633c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
6344cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
635bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
636bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
6374f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
6384f559836SJake Burkholder 			    realitexpire, p);
639df8bae1dSRodney W. Grimes 			splx(s);
64037824023SJohn Baldwin 			PROC_UNLOCK(p);
641df8bae1dSRodney W. Grimes 			return;
642df8bae1dSRodney W. Grimes 		}
643df8bae1dSRodney W. Grimes 		splx(s);
644df8bae1dSRodney W. Grimes 	}
64537824023SJohn Baldwin 	/*NOTREACHED*/
646df8bae1dSRodney W. Grimes }
647df8bae1dSRodney W. Grimes 
648df8bae1dSRodney W. Grimes /*
649df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
650df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
651df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
652df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
653df8bae1dSRodney W. Grimes  */
65426f9a767SRodney W. Grimes int
655df8bae1dSRodney W. Grimes itimerfix(tv)
656df8bae1dSRodney W. Grimes 	struct timeval *tv;
657df8bae1dSRodney W. Grimes {
658df8bae1dSRodney W. Grimes 
659df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
660df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
661df8bae1dSRodney W. Grimes 		return (EINVAL);
662df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
663df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
664df8bae1dSRodney W. Grimes 	return (0);
665df8bae1dSRodney W. Grimes }
666df8bae1dSRodney W. Grimes 
667df8bae1dSRodney W. Grimes /*
668df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
669df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
670df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
671df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
672df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
673df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
674df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
675df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
676df8bae1dSRodney W. Grimes  */
67726f9a767SRodney W. Grimes int
678df8bae1dSRodney W. Grimes itimerdecr(itp, usec)
679df8bae1dSRodney W. Grimes 	register struct itimerval *itp;
680df8bae1dSRodney W. Grimes 	int usec;
681df8bae1dSRodney W. Grimes {
682df8bae1dSRodney W. Grimes 
683df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
684df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
685df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
686df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
687df8bae1dSRodney W. Grimes 			goto expire;
688df8bae1dSRodney W. Grimes 		}
689df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
690df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
691df8bae1dSRodney W. Grimes 	}
692df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
693df8bae1dSRodney W. Grimes 	usec = 0;
6944cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
695df8bae1dSRodney W. Grimes 		return (1);
696df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
697df8bae1dSRodney W. Grimes expire:
6984cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
699df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
700df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
701df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
702df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
703df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
704df8bae1dSRodney W. Grimes 		}
705df8bae1dSRodney W. Grimes 	} else
706df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
707df8bae1dSRodney W. Grimes 	return (0);
708df8bae1dSRodney W. Grimes }
709df8bae1dSRodney W. Grimes 
710df8bae1dSRodney W. Grimes /*
711df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
712df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
713df8bae1dSRodney W. Grimes  * results which are before the beginning,
714df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
715df8bae1dSRodney W. Grimes  * Caveat emptor.
716df8bae1dSRodney W. Grimes  */
71726f9a767SRodney W. Grimes void
718df8bae1dSRodney W. Grimes timevaladd(t1, t2)
719df8bae1dSRodney W. Grimes 	struct timeval *t1, *t2;
720df8bae1dSRodney W. Grimes {
721df8bae1dSRodney W. Grimes 
722df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
723df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
724df8bae1dSRodney W. Grimes 	timevalfix(t1);
725df8bae1dSRodney W. Grimes }
726df8bae1dSRodney W. Grimes 
72726f9a767SRodney W. Grimes void
728df8bae1dSRodney W. Grimes timevalsub(t1, t2)
729df8bae1dSRodney W. Grimes 	struct timeval *t1, *t2;
730df8bae1dSRodney W. Grimes {
731df8bae1dSRodney W. Grimes 
732df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
733df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
734df8bae1dSRodney W. Grimes 	timevalfix(t1);
735df8bae1dSRodney W. Grimes }
736df8bae1dSRodney W. Grimes 
73787b6de2bSPoul-Henning Kamp static void
738df8bae1dSRodney W. Grimes timevalfix(t1)
739df8bae1dSRodney W. Grimes 	struct timeval *t1;
740df8bae1dSRodney W. Grimes {
741df8bae1dSRodney W. Grimes 
742df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
743df8bae1dSRodney W. Grimes 		t1->tv_sec--;
744df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
745df8bae1dSRodney W. Grimes 	}
746df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
747df8bae1dSRodney W. Grimes 		t1->tv_sec++;
748df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
749df8bae1dSRodney W. Grimes 	}
750df8bae1dSRodney W. Grimes }
751