xref: /freebsd/sys/kern/kern_time.c (revision addea9d4d74e7e8fae161b7375e627b62161ead2)
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 
374b8d5f2dSRobert Watson #include "opt_mac.h"
384b8d5f2dSRobert Watson 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40e96c1fdcSPeter Wemm #include <sys/systm.h>
41fb919e4dSMark Murray #include <sys/lock.h>
42fb919e4dSMark Murray #include <sys/mutex.h>
43d2d3e875SBruce Evans #include <sys/sysproto.h>
44df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
45797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
46df8bae1dSRodney W. Grimes #include <sys/kernel.h>
474b8d5f2dSRobert Watson #include <sys/mac.h>
48df8bae1dSRodney W. Grimes #include <sys/systm.h>
4994c8fcd8SPeter Wemm #include <sys/sysent.h>
50df8bae1dSRodney W. Grimes #include <sys/proc.h>
51708e7684SPeter Wemm #include <sys/time.h>
5291266b96SPoul-Henning Kamp #include <sys/timetc.h>
53df8bae1dSRodney W. Grimes #include <sys/vnode.h>
54fb919e4dSMark Murray 
555b870b7bSPeter Wemm #include <vm/vm.h>
565b870b7bSPeter Wemm #include <vm/vm_extern.h>
57df8bae1dSRodney W. Grimes 
58ac7e6123SDavid Greenman struct timezone tz;
59ac7e6123SDavid Greenman 
60df8bae1dSRodney W. Grimes /*
61df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
62df8bae1dSRodney W. Grimes  *
63df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
64df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
65df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
66df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
67df8bae1dSRodney W. Grimes  * timers when they expire.
68df8bae1dSRodney W. Grimes  */
69df8bae1dSRodney W. Grimes 
704d77a549SAlfred Perlstein static int	nanosleep1(struct thread *td, struct timespec *rqt,
714d77a549SAlfred Perlstein 		    struct timespec *rmt);
727edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
734d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
744d77a549SAlfred Perlstein static void	no_lease_updatetime(int);
7594c8fcd8SPeter Wemm 
761b09ae77SPoul-Henning Kamp static void
771b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
781b09ae77SPoul-Henning Kamp 	int deltat;
791b09ae77SPoul-Henning Kamp {
801b09ae77SPoul-Henning Kamp }
811b09ae77SPoul-Henning Kamp 
824d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
831b09ae77SPoul-Henning Kamp 
8494c8fcd8SPeter Wemm static int
8591afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
8694c8fcd8SPeter Wemm {
87fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
88c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
897ec73f64SPoul-Henning Kamp 	struct timespec ts;
9094c8fcd8SPeter Wemm 	int s;
9194c8fcd8SPeter Wemm 
92708e7684SPeter Wemm 	s = splclock();
939c8fff87SBruce Evans 	microtime(&tv1);
9400af9731SPoul-Henning Kamp 	delta = *tv;
9500af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
9694c8fcd8SPeter Wemm 
9794c8fcd8SPeter Wemm 	/*
989c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
99fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
100fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
101fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
102fcae3aa6SNick Sayer 	 * back to the past.
103c0bd94a7SNick Sayer 	 *
104c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
105c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
106c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
10794c8fcd8SPeter Wemm 	 */
1087edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
109fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1103f92429aSMatt Jacob 			/*
111c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1123f92429aSMatt Jacob 			 */
113fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
114fcae3aa6SNick Sayer 				maxtime = tv1;
115fcae3aa6SNick Sayer 			tv2 = *tv;
116fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
117fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1183f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
119fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
120fcae3aa6SNick Sayer 			}
1213f92429aSMatt Jacob 		} else {
122c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
123c0bd94a7SNick Sayer 				splx(s);
124c0bd94a7SNick Sayer 				return (EPERM);
125c0bd94a7SNick Sayer 			}
126c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
127c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
128c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
129c0bd94a7SNick Sayer 			}
130c0bd94a7SNick Sayer 			laststep = *tv;
131fcae3aa6SNick Sayer 		}
1329c8fff87SBruce Evans 	}
1339c8fff87SBruce Evans 
1347ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1357ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1367edfb592SJohn Baldwin 	mtx_lock(&Giant);
13791266b96SPoul-Henning Kamp 	tc_setclock(&ts);
13894c8fcd8SPeter Wemm 	(void) splsoftclock();
13994c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
14094c8fcd8SPeter Wemm 	splx(s);
14194c8fcd8SPeter Wemm 	resettodr();
1427edfb592SJohn Baldwin 	mtx_unlock(&Giant);
14394c8fcd8SPeter Wemm 	return (0);
14494c8fcd8SPeter Wemm }
14594c8fcd8SPeter Wemm 
14694c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
14794c8fcd8SPeter Wemm struct clock_gettime_args {
14894c8fcd8SPeter Wemm 	clockid_t clock_id;
14994c8fcd8SPeter Wemm 	struct	timespec *tp;
15094c8fcd8SPeter Wemm };
15194c8fcd8SPeter Wemm #endif
152708e7684SPeter Wemm 
153fb99ab88SMatthew Dillon /*
154fb99ab88SMatthew Dillon  * MPSAFE
155fb99ab88SMatthew Dillon  */
15694c8fcd8SPeter Wemm /* ARGSUSED */
15794c8fcd8SPeter Wemm int
15891afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
15994c8fcd8SPeter Wemm {
16094c8fcd8SPeter Wemm 	struct timespec ats;
16194c8fcd8SPeter Wemm 
162d1e405c5SAlfred Perlstein 	if (uap->clock_id != CLOCK_REALTIME)
16394c8fcd8SPeter Wemm 		return (EINVAL);
164fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1657ec73f64SPoul-Henning Kamp 	nanotime(&ats);
166fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
167d1e405c5SAlfred Perlstein 	return (copyout(&ats, uap->tp, sizeof(ats)));
16894c8fcd8SPeter Wemm }
16994c8fcd8SPeter Wemm 
17094c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
17194c8fcd8SPeter Wemm struct clock_settime_args {
17294c8fcd8SPeter Wemm 	clockid_t clock_id;
17394c8fcd8SPeter Wemm 	const struct	timespec *tp;
17494c8fcd8SPeter Wemm };
17594c8fcd8SPeter Wemm #endif
176708e7684SPeter Wemm 
177fb99ab88SMatthew Dillon /*
178fb99ab88SMatthew Dillon  * MPSAFE
179fb99ab88SMatthew Dillon  */
18094c8fcd8SPeter Wemm /* ARGSUSED */
18194c8fcd8SPeter Wemm int
18291afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
18394c8fcd8SPeter Wemm {
18494c8fcd8SPeter Wemm 	struct timeval atv;
18594c8fcd8SPeter Wemm 	struct timespec ats;
18694c8fcd8SPeter Wemm 	int error;
18794c8fcd8SPeter Wemm 
1884b8d5f2dSRobert Watson #ifdef MAC
1894b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
1904b8d5f2dSRobert Watson 	if (error)
1914b8d5f2dSRobert Watson 		return (error);
1924b8d5f2dSRobert Watson #endif
19344731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
1947edfb592SJohn Baldwin 		return (error);
195d1e405c5SAlfred Perlstein 	if (uap->clock_id != CLOCK_REALTIME)
1967edfb592SJohn Baldwin 		return (EINVAL);
197d1e405c5SAlfred Perlstein 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
1987edfb592SJohn Baldwin 		return (error);
1997edfb592SJohn Baldwin 	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
2007edfb592SJohn Baldwin 		return (EINVAL);
201a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
20294c8fcd8SPeter Wemm 	TIMESPEC_TO_TIMEVAL(&atv, &ats);
2037edfb592SJohn Baldwin 	error = settime(td, &atv);
20494c8fcd8SPeter Wemm 	return (error);
20594c8fcd8SPeter Wemm }
20694c8fcd8SPeter Wemm 
20794c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
20894c8fcd8SPeter Wemm struct clock_getres_args {
20994c8fcd8SPeter Wemm 	clockid_t clock_id;
21094c8fcd8SPeter Wemm 	struct	timespec *tp;
21194c8fcd8SPeter Wemm };
21294c8fcd8SPeter Wemm #endif
213708e7684SPeter Wemm 
21494c8fcd8SPeter Wemm int
21591afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
21694c8fcd8SPeter Wemm {
21794c8fcd8SPeter Wemm 	struct timespec ts;
218708e7684SPeter Wemm 	int error;
21994c8fcd8SPeter Wemm 
220d1e405c5SAlfred Perlstein 	if (uap->clock_id != CLOCK_REALTIME)
22194c8fcd8SPeter Wemm 		return (EINVAL);
222708e7684SPeter Wemm 	error = 0;
223d1e405c5SAlfred Perlstein 	if (uap->tp) {
22494c8fcd8SPeter Wemm 		ts.tv_sec = 0;
225ac0653dcSBruce Evans 		/*
226ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
227ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
228ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
229ac0653dcSBruce Evans 		 */
230ac0653dcSBruce Evans 		ts.tv_nsec = 1000000000 / tc_getfrequency() + 1;
231d1e405c5SAlfred Perlstein 		error = copyout(&ts, uap->tp, sizeof(ts));
23294c8fcd8SPeter Wemm 	}
233708e7684SPeter Wemm 	return (error);
23494c8fcd8SPeter Wemm }
23594c8fcd8SPeter Wemm 
23694c8fcd8SPeter Wemm static int nanowait;
23794c8fcd8SPeter Wemm 
2385b870b7bSPeter Wemm static int
23991afe087SPoul-Henning Kamp nanosleep1(struct thread *td, struct timespec *rqt, struct timespec *rmt)
2405b870b7bSPeter Wemm {
2415704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
24233841826SPoul-Henning Kamp 	struct timeval tv;
24333841826SPoul-Henning Kamp 	int error;
2445b870b7bSPeter Wemm 
2457d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
246708e7684SPeter Wemm 		return (EINVAL);
247d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
2487d7fb492SBruce Evans 		return (0);
249c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
25000af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
25133841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
25233841826SPoul-Henning Kamp 	for (;;) {
25333841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
25433841826SPoul-Henning Kamp 		    tvtohz(&tv));
255c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
25633841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
25733841826SPoul-Henning Kamp 			if (error == ERESTART)
25894c8fcd8SPeter Wemm 				error = EINTR;
25933841826SPoul-Henning Kamp 			if (rmt != NULL) {
26033841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
26133841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
26233841826SPoul-Henning Kamp 					timespecclear(&ts);
26300af9731SPoul-Henning Kamp 				*rmt = ts;
26400af9731SPoul-Henning Kamp 			}
26533841826SPoul-Henning Kamp 			return (error);
26633841826SPoul-Henning Kamp 		}
26733841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
26833841826SPoul-Henning Kamp 			return (0);
2695704ba6aSPoul-Henning Kamp 		ts3 = ts;
2705704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
2715704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
27233841826SPoul-Henning Kamp 	}
2735b870b7bSPeter Wemm }
27494c8fcd8SPeter Wemm 
2755b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
2765b870b7bSPeter Wemm struct nanosleep_args {
2775b870b7bSPeter Wemm 	struct	timespec *rqtp;
2785b870b7bSPeter Wemm 	struct	timespec *rmtp;
2795b870b7bSPeter Wemm };
2805b870b7bSPeter Wemm #endif
2815b870b7bSPeter Wemm 
282fb99ab88SMatthew Dillon /*
283fb99ab88SMatthew Dillon  * MPSAFE
284fb99ab88SMatthew Dillon  */
2855b870b7bSPeter Wemm /* ARGSUSED */
2865b870b7bSPeter Wemm int
28791afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
2885b870b7bSPeter Wemm {
2895b870b7bSPeter Wemm 	struct timespec rmt, rqt;
290fb99ab88SMatthew Dillon 	int error;
2915b870b7bSPeter Wemm 
292d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
2935b870b7bSPeter Wemm 	if (error)
2945b870b7bSPeter Wemm 		return (error);
295fb99ab88SMatthew Dillon 
296fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
297d1e405c5SAlfred Perlstein 	if (uap->rmtp) {
298d1e405c5SAlfred Perlstein 		if (!useracc((caddr_t)uap->rmtp, sizeof(rmt),
299fb99ab88SMatthew Dillon 		    VM_PROT_WRITE)) {
300fb99ab88SMatthew Dillon 			error = EFAULT;
301fb99ab88SMatthew Dillon 			goto done2;
302fb99ab88SMatthew Dillon 		}
303fb99ab88SMatthew Dillon 	}
304b40ce416SJulian Elischer 	error = nanosleep1(td, &rqt, &rmt);
305d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
306fb99ab88SMatthew Dillon 		int error2;
307fb99ab88SMatthew Dillon 
308d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
3095b870b7bSPeter Wemm 		if (error2)	/* XXX shouldn't happen, did useracc() above */
310fb99ab88SMatthew Dillon 			error = error2;
311708e7684SPeter Wemm 	}
312fb99ab88SMatthew Dillon done2:
313fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
314708e7684SPeter Wemm 	return (error);
31594c8fcd8SPeter Wemm }
31694c8fcd8SPeter Wemm 
3175b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
318df8bae1dSRodney W. Grimes struct gettimeofday_args {
319df8bae1dSRodney W. Grimes 	struct	timeval *tp;
320df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
321df8bae1dSRodney W. Grimes };
322d2d3e875SBruce Evans #endif
323fb99ab88SMatthew Dillon /*
324fb99ab88SMatthew Dillon  * MPSAFE
325fb99ab88SMatthew Dillon  */
326df8bae1dSRodney W. Grimes /* ARGSUSED */
32726f9a767SRodney W. Grimes int
32891afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
329df8bae1dSRodney W. Grimes {
330df8bae1dSRodney W. Grimes 	struct timeval atv;
331df8bae1dSRodney W. Grimes 	int error = 0;
332df8bae1dSRodney W. Grimes 
333df8bae1dSRodney W. Grimes 	if (uap->tp) {
334df8bae1dSRodney W. Grimes 		microtime(&atv);
33501609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
336df8bae1dSRodney W. Grimes 	}
33721dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
33821dcdb38SPoul-Henning Kamp 		mtx_lock(&Giant);
33901609114SAlfred Perlstein 		error = copyout(&tz, uap->tzp, sizeof (tz));
340fb99ab88SMatthew Dillon 		mtx_unlock(&Giant);
34121dcdb38SPoul-Henning Kamp 	}
342df8bae1dSRodney W. Grimes 	return (error);
343df8bae1dSRodney W. Grimes }
344df8bae1dSRodney W. Grimes 
345d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
346df8bae1dSRodney W. Grimes struct settimeofday_args {
347df8bae1dSRodney W. Grimes 	struct	timeval *tv;
348df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
349df8bae1dSRodney W. Grimes };
350d2d3e875SBruce Evans #endif
351fb99ab88SMatthew Dillon /*
352fb99ab88SMatthew Dillon  * MPSAFE
353fb99ab88SMatthew Dillon  */
354df8bae1dSRodney W. Grimes /* ARGSUSED */
35526f9a767SRodney W. Grimes int
35691afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
357df8bae1dSRodney W. Grimes {
358708e7684SPeter Wemm 	struct timeval atv;
359df8bae1dSRodney W. Grimes 	struct timezone atz;
360fb99ab88SMatthew Dillon 	int error = 0;
361fb99ab88SMatthew Dillon 
3624b8d5f2dSRobert Watson #ifdef MAC
3634b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
3644b8d5f2dSRobert Watson 	if (error)
3654b8d5f2dSRobert Watson 		return (error);
3664b8d5f2dSRobert Watson #endif
36744731cabSJohn Baldwin 	if ((error = suser(td)))
3687edfb592SJohn Baldwin 		return (error);
369df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
370708e7684SPeter Wemm 	if (uap->tv) {
37101609114SAlfred Perlstein 		if ((error = copyin(uap->tv, &atv, 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 &&
37701609114SAlfred Perlstein 	    (error = copyin(uap->tzp, &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  * Get value of an interval timer.  The process virtual and
391df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
392df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
393df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
394df8bae1dSRodney W. Grimes  *
395df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
396df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
397df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
398df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
399df8bae1dSRodney W. Grimes  *
400df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
401df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
402df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
403df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
404df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
405df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
406df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
407df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
408df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
409df8bae1dSRodney W. Grimes  */
410d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
411df8bae1dSRodney W. Grimes struct getitimer_args {
412df8bae1dSRodney W. Grimes 	u_int	which;
413df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
414df8bae1dSRodney W. Grimes };
415d2d3e875SBruce Evans #endif
416fb99ab88SMatthew Dillon /*
417fb99ab88SMatthew Dillon  * MPSAFE
418fb99ab88SMatthew Dillon  */
419df8bae1dSRodney W. Grimes /* ARGSUSED */
42026f9a767SRodney W. Grimes int
42191afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
422df8bae1dSRodney W. Grimes {
423b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
424227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
425df8bae1dSRodney W. Grimes 	struct itimerval aitv;
426df8bae1dSRodney W. Grimes 	int s;
427fb99ab88SMatthew Dillon 	int error;
428df8bae1dSRodney W. Grimes 
429df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
430df8bae1dSRodney W. Grimes 		return (EINVAL);
431fb99ab88SMatthew Dillon 
432fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
433fb99ab88SMatthew Dillon 
434227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX still needed ? */
435df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
436df8bae1dSRodney W. Grimes 		/*
437ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
438df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
439df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
440df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
441df8bae1dSRodney W. Grimes 		 */
442df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
4434cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value)) {
444c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
4454cf41af3SPoul-Henning Kamp 			if (timevalcmp(&aitv.it_value, &ctv, <))
4464cf41af3SPoul-Henning Kamp 				timevalclear(&aitv.it_value);
447df8bae1dSRodney W. Grimes 			else
448227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
449227ee8a1SPoul-Henning Kamp 		}
450fb99ab88SMatthew Dillon 	} else {
451df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
452fb99ab88SMatthew Dillon 	}
453df8bae1dSRodney W. Grimes 	splx(s);
45401609114SAlfred Perlstein 	error = copyout(&aitv, uap->itv, sizeof (struct itimerval));
455fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
456fb99ab88SMatthew Dillon 	return(error);
457df8bae1dSRodney W. Grimes }
458df8bae1dSRodney W. Grimes 
459d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
460df8bae1dSRodney W. Grimes struct setitimer_args {
461df8bae1dSRodney W. Grimes 	u_int	which;
462df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
463df8bae1dSRodney W. Grimes };
464d2d3e875SBruce Evans #endif
465fb99ab88SMatthew Dillon /*
466fb99ab88SMatthew Dillon  * MPSAFE
467fb99ab88SMatthew Dillon  */
468df8bae1dSRodney W. Grimes /* ARGSUSED */
46926f9a767SRodney W. Grimes int
47091afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
471df8bae1dSRodney W. Grimes {
472b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
473df8bae1dSRodney W. Grimes 	struct itimerval aitv;
474227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
47591afe087SPoul-Henning Kamp 	struct itimerval *itvp;
476fb99ab88SMatthew Dillon 	int s, error = 0;
477df8bae1dSRodney W. Grimes 
478df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
479df8bae1dSRodney W. Grimes 		return (EINVAL);
480df8bae1dSRodney W. Grimes 	itvp = uap->itv;
48101609114SAlfred Perlstein 	if (itvp && (error = copyin(itvp, &aitv, sizeof(struct itimerval))))
482df8bae1dSRodney W. Grimes 		return (error);
483fb99ab88SMatthew Dillon 
484fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
485fb99ab88SMatthew Dillon 
4860808c591SBruce Evans 	if ((uap->itv = uap->oitv) &&
487b40ce416SJulian Elischer 	    (error = getitimer(td, (struct getitimer_args *)uap))) {
488fb99ab88SMatthew Dillon 		goto done2;
489fb99ab88SMatthew Dillon 	}
490fb99ab88SMatthew Dillon 	if (itvp == 0) {
491fb99ab88SMatthew Dillon 		error = 0;
492fb99ab88SMatthew Dillon 		goto done2;
493fb99ab88SMatthew Dillon 	}
494fb99ab88SMatthew Dillon 	if (itimerfix(&aitv.it_value)) {
495fb99ab88SMatthew Dillon 		error = EINVAL;
496fb99ab88SMatthew Dillon 		goto done2;
497fb99ab88SMatthew Dillon 	}
498fb99ab88SMatthew Dillon 	if (!timevalisset(&aitv.it_value)) {
4994cf41af3SPoul-Henning Kamp 		timevalclear(&aitv.it_interval);
500fb99ab88SMatthew Dillon 	} else if (itimerfix(&aitv.it_interval)) {
501fb99ab88SMatthew Dillon 		error = EINVAL;
502fb99ab88SMatthew Dillon 		goto done2;
503fb99ab88SMatthew Dillon 	}
504227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX: still needed ? */
505df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
5064cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
5074f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
5084cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value))
5094f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
5104f559836SJake Burkholder 			    realitexpire, p);
511c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
512bfe6c9faSPoul-Henning Kamp 		timevaladd(&aitv.it_value, &ctv);
513df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
514fb99ab88SMatthew Dillon 	} else {
515df8bae1dSRodney W. Grimes 		p->p_stats->p_timer[uap->which] = aitv;
516fb99ab88SMatthew Dillon 	}
517df8bae1dSRodney W. Grimes 	splx(s);
518fb99ab88SMatthew Dillon done2:
519fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
520fb99ab88SMatthew Dillon 	return (error);
521df8bae1dSRodney W. Grimes }
522df8bae1dSRodney W. Grimes 
523df8bae1dSRodney W. Grimes /*
524df8bae1dSRodney W. Grimes  * Real interval timer expired:
525df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
526df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
527df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
528df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
529df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
530c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
5319207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
5329207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
5339207f00aSBruce Evans  * interrupt even when we're delayed.
534df8bae1dSRodney W. Grimes  */
535df8bae1dSRodney W. Grimes void
53691afe087SPoul-Henning Kamp realitexpire(void *arg)
537df8bae1dSRodney W. Grimes {
53891afe087SPoul-Henning Kamp 	struct proc *p;
539bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
540df8bae1dSRodney W. Grimes 	int s;
541df8bae1dSRodney W. Grimes 
542df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
54337824023SJohn Baldwin 	PROC_LOCK(p);
544df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
5454cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
5464cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
54737824023SJohn Baldwin 		PROC_UNLOCK(p);
548df8bae1dSRodney W. Grimes 		return;
549df8bae1dSRodney W. Grimes 	}
550df8bae1dSRodney W. Grimes 	for (;;) {
551227ee8a1SPoul-Henning Kamp 		s = splclock(); /* XXX: still neeeded ? */
552df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
553df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
554c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
5554cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
556bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
557bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
5584f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
5594f559836SJake Burkholder 			    realitexpire, p);
560df8bae1dSRodney W. Grimes 			splx(s);
56137824023SJohn Baldwin 			PROC_UNLOCK(p);
562df8bae1dSRodney W. Grimes 			return;
563df8bae1dSRodney W. Grimes 		}
564df8bae1dSRodney W. Grimes 		splx(s);
565df8bae1dSRodney W. Grimes 	}
56637824023SJohn Baldwin 	/*NOTREACHED*/
567df8bae1dSRodney W. Grimes }
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes /*
570df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
571df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
572df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
573df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
574df8bae1dSRodney W. Grimes  */
57526f9a767SRodney W. Grimes int
57691afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
577df8bae1dSRodney W. Grimes {
578df8bae1dSRodney W. Grimes 
579df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
580df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
581df8bae1dSRodney W. Grimes 		return (EINVAL);
582df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
583df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
584df8bae1dSRodney W. Grimes 	return (0);
585df8bae1dSRodney W. Grimes }
586df8bae1dSRodney W. Grimes 
587df8bae1dSRodney W. Grimes /*
588df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
589df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
590df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
591df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
592df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
593df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
594df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
595df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
596df8bae1dSRodney W. Grimes  */
59726f9a767SRodney W. Grimes int
59891afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
599df8bae1dSRodney W. Grimes {
600df8bae1dSRodney W. Grimes 
601df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
602df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
603df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
604df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
605df8bae1dSRodney W. Grimes 			goto expire;
606df8bae1dSRodney W. Grimes 		}
607df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
608df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
609df8bae1dSRodney W. Grimes 	}
610df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
611df8bae1dSRodney W. Grimes 	usec = 0;
6124cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
613df8bae1dSRodney W. Grimes 		return (1);
614df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
615df8bae1dSRodney W. Grimes expire:
6164cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
617df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
618df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
619df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
620df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
621df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
622df8bae1dSRodney W. Grimes 		}
623df8bae1dSRodney W. Grimes 	} else
624df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
625df8bae1dSRodney W. Grimes 	return (0);
626df8bae1dSRodney W. Grimes }
627df8bae1dSRodney W. Grimes 
628df8bae1dSRodney W. Grimes /*
629df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
630df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
631df8bae1dSRodney W. Grimes  * results which are before the beginning,
632df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
633df8bae1dSRodney W. Grimes  * Caveat emptor.
634df8bae1dSRodney W. Grimes  */
63526f9a767SRodney W. Grimes void
63691afe087SPoul-Henning Kamp timevaladd(struct timeval *t1, struct timeval *t2)
637df8bae1dSRodney W. Grimes {
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
640df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
641df8bae1dSRodney W. Grimes 	timevalfix(t1);
642df8bae1dSRodney W. Grimes }
643df8bae1dSRodney W. Grimes 
64426f9a767SRodney W. Grimes void
64591afe087SPoul-Henning Kamp timevalsub(struct timeval *t1, struct timeval *t2)
646df8bae1dSRodney W. Grimes {
647df8bae1dSRodney W. Grimes 
648df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
649df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
650df8bae1dSRodney W. Grimes 	timevalfix(t1);
651df8bae1dSRodney W. Grimes }
652df8bae1dSRodney W. Grimes 
65387b6de2bSPoul-Henning Kamp static void
65491afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
655df8bae1dSRodney W. Grimes {
656df8bae1dSRodney W. Grimes 
657df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
658df8bae1dSRodney W. Grimes 		t1->tv_sec--;
659df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
660df8bae1dSRodney W. Grimes 	}
661df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
662df8bae1dSRodney W. Grimes 		t1->tv_sec++;
663df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
664df8bae1dSRodney W. Grimes 	}
665df8bae1dSRodney W. Grimes }
66691974ce1SSam Leffler 
66791974ce1SSam Leffler /*
668addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
66991974ce1SSam Leffler  */
67091974ce1SSam Leffler int
67191974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
67291974ce1SSam Leffler {
67391974ce1SSam Leffler 	struct timeval tv, delta;
67491974ce1SSam Leffler 	int rv = 0;
67591974ce1SSam Leffler 
676addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
677addea9d4SSam Leffler 	delta = tv;
678addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
67991974ce1SSam Leffler 
68091974ce1SSam Leffler 	/*
68191974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
68291974ce1SSam Leffler 	 * even if interval is huge.
68391974ce1SSam Leffler 	 */
68491974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
68591974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
68691974ce1SSam Leffler 		*lasttime = tv;
68791974ce1SSam Leffler 		rv = 1;
68891974ce1SSam Leffler 	}
68991974ce1SSam Leffler 
69091974ce1SSam Leffler 	return (rv);
69191974ce1SSam Leffler }
69291974ce1SSam Leffler 
69391974ce1SSam Leffler /*
69491974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
695addea9d4SSam Leffler  *
696addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
697addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
698addea9d4SSam Leffler  *
699addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
700addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
701addea9d4SSam Leffler  * clock ticks for minimal overhead.
70291974ce1SSam Leffler  */
70391974ce1SSam Leffler int
70491974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
70591974ce1SSam Leffler {
706addea9d4SSam Leffler 	int now;
70791974ce1SSam Leffler 
70891974ce1SSam Leffler 	/*
709addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
710addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
711addea9d4SSam Leffler 	 * lasttime.
71291974ce1SSam Leffler 	 */
713addea9d4SSam Leffler 	now = ticks;
714addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
715addea9d4SSam Leffler 		lasttime->tv_sec = now;
716addea9d4SSam Leffler 		*curpps = 1;
717addea9d4SSam Leffler 		return (1);
718addea9d4SSam Leffler 	} else {
719addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
720addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
721addea9d4SSam Leffler 	}
72291974ce1SSam Leffler }
723