xref: /freebsd/sys/kern/kern_time.c (revision 677b542ea243380af64822b30e4ef5f7a6d978ee)
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
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
36677b542eSDavid E. O'Brien #include <sys/cdefs.h>
37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
38677b542eSDavid E. O'Brien 
394b8d5f2dSRobert Watson #include "opt_mac.h"
404b8d5f2dSRobert Watson 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42e96c1fdcSPeter Wemm #include <sys/systm.h>
43fb919e4dSMark Murray #include <sys/lock.h>
44fb919e4dSMark Murray #include <sys/mutex.h>
45d2d3e875SBruce Evans #include <sys/sysproto.h>
46df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
47797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
48df8bae1dSRodney W. Grimes #include <sys/kernel.h>
494b8d5f2dSRobert Watson #include <sys/mac.h>
5094c8fcd8SPeter Wemm #include <sys/sysent.h>
51df8bae1dSRodney W. Grimes #include <sys/proc.h>
52708e7684SPeter Wemm #include <sys/time.h>
5391266b96SPoul-Henning Kamp #include <sys/timetc.h>
54df8bae1dSRodney W. Grimes #include <sys/vnode.h>
55fb919e4dSMark Murray 
565b870b7bSPeter Wemm #include <vm/vm.h>
575b870b7bSPeter Wemm #include <vm/vm_extern.h>
58df8bae1dSRodney W. Grimes 
5991f1c2b3SPoul-Henning Kamp int tz_minuteswest;
6091f1c2b3SPoul-Henning Kamp int tz_dsttime;
61ac7e6123SDavid Greenman 
62df8bae1dSRodney W. Grimes /*
63df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
64df8bae1dSRodney W. Grimes  *
65df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
66df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
67df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
68df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
69df8bae1dSRodney W. Grimes  * timers when they expire.
70df8bae1dSRodney W. Grimes  */
71df8bae1dSRodney W. Grimes 
724d77a549SAlfred Perlstein static int	nanosleep1(struct thread *td, struct timespec *rqt,
734d77a549SAlfred Perlstein 		    struct timespec *rmt);
747edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
754d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
764d77a549SAlfred Perlstein static void	no_lease_updatetime(int);
7794c8fcd8SPeter Wemm 
781b09ae77SPoul-Henning Kamp static void
791b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
801b09ae77SPoul-Henning Kamp 	int deltat;
811b09ae77SPoul-Henning Kamp {
821b09ae77SPoul-Henning Kamp }
831b09ae77SPoul-Henning Kamp 
844d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
851b09ae77SPoul-Henning Kamp 
8694c8fcd8SPeter Wemm static int
8791afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
8894c8fcd8SPeter Wemm {
89fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
90c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
917ec73f64SPoul-Henning Kamp 	struct timespec ts;
9294c8fcd8SPeter Wemm 	int s;
9394c8fcd8SPeter Wemm 
94708e7684SPeter Wemm 	s = splclock();
959c8fff87SBruce Evans 	microtime(&tv1);
9600af9731SPoul-Henning Kamp 	delta = *tv;
9700af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
9894c8fcd8SPeter Wemm 
9994c8fcd8SPeter Wemm 	/*
1009c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
101fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
102fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
103fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
104fcae3aa6SNick Sayer 	 * back to the past.
105c0bd94a7SNick Sayer 	 *
106c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
107c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
108c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
10994c8fcd8SPeter Wemm 	 */
1107edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
111fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1123f92429aSMatt Jacob 			/*
113c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1143f92429aSMatt Jacob 			 */
115fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
116fcae3aa6SNick Sayer 				maxtime = tv1;
117fcae3aa6SNick Sayer 			tv2 = *tv;
118fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
119fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1203f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
121fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
122fcae3aa6SNick Sayer 			}
1233f92429aSMatt Jacob 		} else {
124c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
125c0bd94a7SNick Sayer 				splx(s);
126c0bd94a7SNick Sayer 				return (EPERM);
127c0bd94a7SNick Sayer 			}
128c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
129c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
130c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
131c0bd94a7SNick Sayer 			}
132c0bd94a7SNick Sayer 			laststep = *tv;
133fcae3aa6SNick Sayer 		}
1349c8fff87SBruce Evans 	}
1359c8fff87SBruce Evans 
1367ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1377ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1387edfb592SJohn Baldwin 	mtx_lock(&Giant);
13991266b96SPoul-Henning Kamp 	tc_setclock(&ts);
14094c8fcd8SPeter Wemm 	(void) splsoftclock();
14194c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
14294c8fcd8SPeter Wemm 	splx(s);
14394c8fcd8SPeter Wemm 	resettodr();
1447edfb592SJohn Baldwin 	mtx_unlock(&Giant);
14594c8fcd8SPeter Wemm 	return (0);
14694c8fcd8SPeter Wemm }
14794c8fcd8SPeter Wemm 
14894c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
14994c8fcd8SPeter Wemm struct clock_gettime_args {
15094c8fcd8SPeter Wemm 	clockid_t clock_id;
15194c8fcd8SPeter Wemm 	struct	timespec *tp;
15294c8fcd8SPeter Wemm };
15394c8fcd8SPeter Wemm #endif
154708e7684SPeter Wemm 
155fb99ab88SMatthew Dillon /*
156fb99ab88SMatthew Dillon  * MPSAFE
157fb99ab88SMatthew Dillon  */
15894c8fcd8SPeter Wemm /* ARGSUSED */
15994c8fcd8SPeter Wemm int
16091afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
16194c8fcd8SPeter Wemm {
16294c8fcd8SPeter Wemm 	struct timespec ats;
16394c8fcd8SPeter Wemm 
1645cb3dc8fSPoul-Henning Kamp 	if (uap->clock_id == CLOCK_REALTIME)
1657ec73f64SPoul-Henning Kamp 		nanotime(&ats);
1665cb3dc8fSPoul-Henning Kamp 	else if (uap->clock_id == CLOCK_MONOTONIC)
1678f5ef1a9SPoul-Henning Kamp 		nanouptime(&ats);
1685cb3dc8fSPoul-Henning Kamp 	else
1695cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
170d1e405c5SAlfred Perlstein 	return (copyout(&ats, uap->tp, sizeof(ats)));
17194c8fcd8SPeter Wemm }
17294c8fcd8SPeter Wemm 
17394c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
17494c8fcd8SPeter Wemm struct clock_settime_args {
17594c8fcd8SPeter Wemm 	clockid_t clock_id;
17694c8fcd8SPeter Wemm 	const struct	timespec *tp;
17794c8fcd8SPeter Wemm };
17894c8fcd8SPeter Wemm #endif
179708e7684SPeter Wemm 
180fb99ab88SMatthew Dillon /*
181fb99ab88SMatthew Dillon  * MPSAFE
182fb99ab88SMatthew Dillon  */
18394c8fcd8SPeter Wemm /* ARGSUSED */
18494c8fcd8SPeter Wemm int
18591afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
18694c8fcd8SPeter Wemm {
18794c8fcd8SPeter Wemm 	struct timeval atv;
18894c8fcd8SPeter Wemm 	struct timespec ats;
18994c8fcd8SPeter Wemm 	int error;
19094c8fcd8SPeter Wemm 
1914b8d5f2dSRobert Watson #ifdef MAC
1924b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
1934b8d5f2dSRobert Watson 	if (error)
1944b8d5f2dSRobert Watson 		return (error);
1954b8d5f2dSRobert Watson #endif
19644731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
1977edfb592SJohn Baldwin 		return (error);
198d1e405c5SAlfred Perlstein 	if (uap->clock_id != CLOCK_REALTIME)
1997edfb592SJohn Baldwin 		return (EINVAL);
200d1e405c5SAlfred Perlstein 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
2017edfb592SJohn Baldwin 		return (error);
2027edfb592SJohn Baldwin 	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
2037edfb592SJohn Baldwin 		return (EINVAL);
204a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
20594c8fcd8SPeter Wemm 	TIMESPEC_TO_TIMEVAL(&atv, &ats);
2067edfb592SJohn Baldwin 	error = settime(td, &atv);
20794c8fcd8SPeter Wemm 	return (error);
20894c8fcd8SPeter Wemm }
20994c8fcd8SPeter Wemm 
21094c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
21194c8fcd8SPeter Wemm struct clock_getres_args {
21294c8fcd8SPeter Wemm 	clockid_t clock_id;
21394c8fcd8SPeter Wemm 	struct	timespec *tp;
21494c8fcd8SPeter Wemm };
21594c8fcd8SPeter Wemm #endif
216708e7684SPeter Wemm 
21794c8fcd8SPeter Wemm int
21891afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
21994c8fcd8SPeter Wemm {
22094c8fcd8SPeter Wemm 	struct timespec ts;
221708e7684SPeter Wemm 	int error;
22294c8fcd8SPeter Wemm 
223d1e405c5SAlfred Perlstein 	if (uap->clock_id != CLOCK_REALTIME)
22494c8fcd8SPeter Wemm 		return (EINVAL);
225708e7684SPeter Wemm 	error = 0;
226d1e405c5SAlfred Perlstein 	if (uap->tp) {
22794c8fcd8SPeter Wemm 		ts.tv_sec = 0;
228ac0653dcSBruce Evans 		/*
229ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
230ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
231ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
232ac0653dcSBruce Evans 		 */
233ac0653dcSBruce Evans 		ts.tv_nsec = 1000000000 / tc_getfrequency() + 1;
234d1e405c5SAlfred Perlstein 		error = copyout(&ts, uap->tp, sizeof(ts));
23594c8fcd8SPeter Wemm 	}
236708e7684SPeter Wemm 	return (error);
23794c8fcd8SPeter Wemm }
23894c8fcd8SPeter Wemm 
23994c8fcd8SPeter Wemm static int nanowait;
24094c8fcd8SPeter Wemm 
2415b870b7bSPeter Wemm static int
24291afe087SPoul-Henning Kamp nanosleep1(struct thread *td, struct timespec *rqt, struct timespec *rmt)
2435b870b7bSPeter Wemm {
2445704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
24533841826SPoul-Henning Kamp 	struct timeval tv;
24633841826SPoul-Henning Kamp 	int error;
2475b870b7bSPeter Wemm 
2487d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
249708e7684SPeter Wemm 		return (EINVAL);
250d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
2517d7fb492SBruce Evans 		return (0);
252c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
25300af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
25433841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
25533841826SPoul-Henning Kamp 	for (;;) {
25633841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
25733841826SPoul-Henning Kamp 		    tvtohz(&tv));
258c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
25933841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
26033841826SPoul-Henning Kamp 			if (error == ERESTART)
26194c8fcd8SPeter Wemm 				error = EINTR;
26233841826SPoul-Henning Kamp 			if (rmt != NULL) {
26333841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
26433841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
26533841826SPoul-Henning Kamp 					timespecclear(&ts);
26600af9731SPoul-Henning Kamp 				*rmt = ts;
26700af9731SPoul-Henning Kamp 			}
26833841826SPoul-Henning Kamp 			return (error);
26933841826SPoul-Henning Kamp 		}
27033841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
27133841826SPoul-Henning Kamp 			return (0);
2725704ba6aSPoul-Henning Kamp 		ts3 = ts;
2735704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
2745704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
27533841826SPoul-Henning Kamp 	}
2765b870b7bSPeter Wemm }
27794c8fcd8SPeter Wemm 
2785b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
2795b870b7bSPeter Wemm struct nanosleep_args {
2805b870b7bSPeter Wemm 	struct	timespec *rqtp;
2815b870b7bSPeter Wemm 	struct	timespec *rmtp;
2825b870b7bSPeter Wemm };
2835b870b7bSPeter Wemm #endif
2845b870b7bSPeter Wemm 
285fb99ab88SMatthew Dillon /*
286fb99ab88SMatthew Dillon  * MPSAFE
287fb99ab88SMatthew Dillon  */
2885b870b7bSPeter Wemm /* ARGSUSED */
2895b870b7bSPeter Wemm int
29091afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
2915b870b7bSPeter Wemm {
2925b870b7bSPeter Wemm 	struct timespec rmt, rqt;
293fb99ab88SMatthew Dillon 	int error;
2945b870b7bSPeter Wemm 
295d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
2965b870b7bSPeter Wemm 	if (error)
2975b870b7bSPeter Wemm 		return (error);
298fb99ab88SMatthew Dillon 
29931f3e2adSAlfred Perlstein 	if (uap->rmtp &&
30031f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
30131f3e2adSAlfred Perlstein 			return (EFAULT);
302b40ce416SJulian Elischer 	error = nanosleep1(td, &rqt, &rmt);
303d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
304fb99ab88SMatthew Dillon 		int error2;
305fb99ab88SMatthew Dillon 
306d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
30731f3e2adSAlfred Perlstein 		if (error2)
308fb99ab88SMatthew Dillon 			error = error2;
309708e7684SPeter Wemm 	}
310708e7684SPeter Wemm 	return (error);
31194c8fcd8SPeter Wemm }
31294c8fcd8SPeter Wemm 
3135b870b7bSPeter Wemm #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
319fb99ab88SMatthew Dillon /*
320fb99ab88SMatthew Dillon  * MPSAFE
321fb99ab88SMatthew Dillon  */
322df8bae1dSRodney W. Grimes /* ARGSUSED */
32326f9a767SRodney W. Grimes int
32491afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
325df8bae1dSRodney W. Grimes {
326df8bae1dSRodney W. Grimes 	struct timeval atv;
327411c25edSTim J. Robbins 	struct timezone rtz;
328df8bae1dSRodney W. Grimes 	int error = 0;
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes 	if (uap->tp) {
331df8bae1dSRodney W. Grimes 		microtime(&atv);
33201609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
333df8bae1dSRodney W. Grimes 	}
33421dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
33591f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
33691f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
337411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
33821dcdb38SPoul-Henning Kamp 	}
339df8bae1dSRodney W. Grimes 	return (error);
340df8bae1dSRodney W. Grimes }
341df8bae1dSRodney W. Grimes 
342d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
343df8bae1dSRodney W. Grimes struct settimeofday_args {
344df8bae1dSRodney W. Grimes 	struct	timeval *tv;
345df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
346df8bae1dSRodney W. Grimes };
347d2d3e875SBruce Evans #endif
348fb99ab88SMatthew Dillon /*
349fb99ab88SMatthew Dillon  * MPSAFE
350fb99ab88SMatthew Dillon  */
351df8bae1dSRodney W. Grimes /* ARGSUSED */
35226f9a767SRodney W. Grimes int
35391afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
354df8bae1dSRodney W. Grimes {
355708e7684SPeter Wemm 	struct timeval atv;
356df8bae1dSRodney W. Grimes 	struct timezone atz;
357fb99ab88SMatthew Dillon 	int error = 0;
358fb99ab88SMatthew Dillon 
3594b8d5f2dSRobert Watson #ifdef MAC
3604b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
3614b8d5f2dSRobert Watson 	if (error)
3624b8d5f2dSRobert Watson 		return (error);
3634b8d5f2dSRobert Watson #endif
36444731cabSJohn Baldwin 	if ((error = suser(td)))
3657edfb592SJohn Baldwin 		return (error);
366df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
367708e7684SPeter Wemm 	if (uap->tv) {
36801609114SAlfred Perlstein 		if ((error = copyin(uap->tv, &atv, sizeof(atv))))
3697edfb592SJohn Baldwin 			return (error);
3707edfb592SJohn Baldwin 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
3717edfb592SJohn Baldwin 			return (EINVAL);
372708e7684SPeter Wemm 	}
373df8bae1dSRodney W. Grimes 	if (uap->tzp &&
37401609114SAlfred Perlstein 	    (error = copyin(uap->tzp, &atz, sizeof(atz))))
3757edfb592SJohn Baldwin 		return (error);
3767edfb592SJohn Baldwin 
3777edfb592SJohn Baldwin 	if (uap->tv && (error = settime(td, &atv)))
3787edfb592SJohn Baldwin 		return (error);
3797edfb592SJohn Baldwin 	if (uap->tzp) {
38091f1c2b3SPoul-Henning Kamp 		tz_minuteswest = atz.tz_minuteswest;
38191f1c2b3SPoul-Henning Kamp 		tz_dsttime = atz.tz_dsttime;
3827edfb592SJohn Baldwin 	}
383fb99ab88SMatthew Dillon 	return (error);
384df8bae1dSRodney W. Grimes }
385df8bae1dSRodney W. Grimes /*
386df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
387df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
388df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
389df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
390df8bae1dSRodney W. Grimes  *
391df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
392df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
393df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
394df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
395df8bae1dSRodney W. Grimes  *
396df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
397df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
398df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
399df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
400df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
401df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
402df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
403df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
404df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
405df8bae1dSRodney W. Grimes  */
406d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
407df8bae1dSRodney W. Grimes struct getitimer_args {
408df8bae1dSRodney W. Grimes 	u_int	which;
409df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
410df8bae1dSRodney W. Grimes };
411d2d3e875SBruce Evans #endif
412fb99ab88SMatthew Dillon /*
413fb99ab88SMatthew Dillon  * MPSAFE
414fb99ab88SMatthew Dillon  */
41526f9a767SRodney W. Grimes int
41691afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
417df8bae1dSRodney W. Grimes {
418b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
419227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
420df8bae1dSRodney W. Grimes 	struct itimerval aitv;
421df8bae1dSRodney W. Grimes 
422df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
423df8bae1dSRodney W. Grimes 		return (EINVAL);
424fb99ab88SMatthew Dillon 
425df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
426df8bae1dSRodney W. Grimes 		/*
427ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
428df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
429df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
430df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
431df8bae1dSRodney W. Grimes 		 */
43296d7f8efSTim J. Robbins 		PROC_LOCK(p);
433df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
43496d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
4354cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value)) {
436c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
4374cf41af3SPoul-Henning Kamp 			if (timevalcmp(&aitv.it_value, &ctv, <))
4384cf41af3SPoul-Henning Kamp 				timevalclear(&aitv.it_value);
439df8bae1dSRodney W. Grimes 			else
440227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
441227ee8a1SPoul-Henning Kamp 		}
442fb99ab88SMatthew Dillon 	} else {
44396d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
444df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
44596d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
446fb99ab88SMatthew Dillon 	}
447411c25edSTim J. Robbins 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
448df8bae1dSRodney W. Grimes }
449df8bae1dSRodney W. Grimes 
450d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
451df8bae1dSRodney W. Grimes struct setitimer_args {
452df8bae1dSRodney W. Grimes 	u_int	which;
453df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
454df8bae1dSRodney W. Grimes };
455d2d3e875SBruce Evans #endif
456fb99ab88SMatthew Dillon /*
457fb99ab88SMatthew Dillon  * MPSAFE
458fb99ab88SMatthew Dillon  */
45926f9a767SRodney W. Grimes int
46091afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
461df8bae1dSRodney W. Grimes {
462b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
46396d7f8efSTim J. Robbins 	struct itimerval aitv, oitv;
464227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
46596d7f8efSTim J. Robbins 	int error;
46696d7f8efSTim J. Robbins 
46796d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
46896d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
46996d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
47096d7f8efSTim J. Robbins 	}
471df8bae1dSRodney W. Grimes 
472df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
473df8bae1dSRodney W. Grimes 		return (EINVAL);
47496d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
475df8bae1dSRodney W. Grimes 		return (error);
47696d7f8efSTim J. Robbins 	if (itimerfix(&aitv.it_value))
47796d7f8efSTim J. Robbins 		return (EINVAL);
47896d7f8efSTim J. Robbins 	if (!timevalisset(&aitv.it_value))
4794cf41af3SPoul-Henning Kamp 		timevalclear(&aitv.it_interval);
48096d7f8efSTim J. Robbins 	else if (itimerfix(&aitv.it_interval))
48196d7f8efSTim J. Robbins 		return (EINVAL);
48296d7f8efSTim J. Robbins 
483df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
48496d7f8efSTim J. Robbins 		PROC_LOCK(p);
4854cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
4864f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
48725b4d3a8SJohn Baldwin 		getmicrouptime(&ctv);
48825b4d3a8SJohn Baldwin 		if (timevalisset(&aitv.it_value)) {
4894f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
4904f559836SJake Burkholder 			    realitexpire, p);
491bfe6c9faSPoul-Henning Kamp 			timevaladd(&aitv.it_value, &ctv);
49225b4d3a8SJohn Baldwin 		}
49396d7f8efSTim J. Robbins 		oitv = p->p_realtimer;
494df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
49596d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
49696d7f8efSTim J. Robbins 		if (timevalisset(&oitv.it_value)) {
49796d7f8efSTim J. Robbins 			if (timevalcmp(&oitv.it_value, &ctv, <))
49896d7f8efSTim J. Robbins 				timevalclear(&oitv.it_value);
49996d7f8efSTim J. Robbins 			else
50096d7f8efSTim J. Robbins 				timevalsub(&oitv.it_value, &ctv);
501fb99ab88SMatthew Dillon 		}
50296d7f8efSTim J. Robbins 	} else {
50396d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
50496d7f8efSTim J. Robbins 		oitv = p->p_stats->p_timer[uap->which];
50596d7f8efSTim J. Robbins 		p->p_stats->p_timer[uap->which] = aitv;
50696d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
50796d7f8efSTim J. Robbins 	}
50896d7f8efSTim J. Robbins 	if (uap->oitv == NULL)
50996d7f8efSTim J. Robbins 		return (0);
51096d7f8efSTim J. Robbins 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
511df8bae1dSRodney W. Grimes }
512df8bae1dSRodney W. Grimes 
513df8bae1dSRodney W. Grimes /*
514df8bae1dSRodney W. Grimes  * Real interval timer expired:
515df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
516df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
517df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
518df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
519df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
520c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
5219207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
5229207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
5239207f00aSBruce Evans  * interrupt even when we're delayed.
524df8bae1dSRodney W. Grimes  */
525df8bae1dSRodney W. Grimes void
52691afe087SPoul-Henning Kamp realitexpire(void *arg)
527df8bae1dSRodney W. Grimes {
52891afe087SPoul-Henning Kamp 	struct proc *p;
529bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
530df8bae1dSRodney W. Grimes 
531df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
53237824023SJohn Baldwin 	PROC_LOCK(p);
533df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
5344cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
5354cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
5365499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
5375499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
53837824023SJohn Baldwin 		PROC_UNLOCK(p);
539df8bae1dSRodney W. Grimes 		return;
540df8bae1dSRodney W. Grimes 	}
541df8bae1dSRodney W. Grimes 	for (;;) {
542df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
543df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
544c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
5454cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
546bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
547bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
5484f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
5494f559836SJake Burkholder 			    realitexpire, p);
55037824023SJohn Baldwin 			PROC_UNLOCK(p);
551df8bae1dSRodney W. Grimes 			return;
552df8bae1dSRodney W. Grimes 		}
553df8bae1dSRodney W. Grimes 	}
55437824023SJohn Baldwin 	/*NOTREACHED*/
555df8bae1dSRodney W. Grimes }
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes /*
558df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
559df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
560df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
561df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
562df8bae1dSRodney W. Grimes  */
56326f9a767SRodney W. Grimes int
56491afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
565df8bae1dSRodney W. Grimes {
566df8bae1dSRodney W. Grimes 
567df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
568df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
569df8bae1dSRodney W. Grimes 		return (EINVAL);
570df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
571df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
572df8bae1dSRodney W. Grimes 	return (0);
573df8bae1dSRodney W. Grimes }
574df8bae1dSRodney W. Grimes 
575df8bae1dSRodney W. Grimes /*
576df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
577df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
578df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
579df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
580df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
581df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
582df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
583df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
584df8bae1dSRodney W. Grimes  */
58526f9a767SRodney W. Grimes int
58691afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
587df8bae1dSRodney W. Grimes {
588df8bae1dSRodney W. Grimes 
589df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
590df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
591df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
592df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
593df8bae1dSRodney W. Grimes 			goto expire;
594df8bae1dSRodney W. Grimes 		}
595df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
596df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
597df8bae1dSRodney W. Grimes 	}
598df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
599df8bae1dSRodney W. Grimes 	usec = 0;
6004cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
601df8bae1dSRodney W. Grimes 		return (1);
602df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
603df8bae1dSRodney W. Grimes expire:
6044cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
605df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
606df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
607df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
608df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
609df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
610df8bae1dSRodney W. Grimes 		}
611df8bae1dSRodney W. Grimes 	} else
612df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
613df8bae1dSRodney W. Grimes 	return (0);
614df8bae1dSRodney W. Grimes }
615df8bae1dSRodney W. Grimes 
616df8bae1dSRodney W. Grimes /*
617df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
618df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
619df8bae1dSRodney W. Grimes  * results which are before the beginning,
620df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
621df8bae1dSRodney W. Grimes  * Caveat emptor.
622df8bae1dSRodney W. Grimes  */
62326f9a767SRodney W. Grimes void
62491afe087SPoul-Henning Kamp timevaladd(struct timeval *t1, struct timeval *t2)
625df8bae1dSRodney W. Grimes {
626df8bae1dSRodney W. Grimes 
627df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
628df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
629df8bae1dSRodney W. Grimes 	timevalfix(t1);
630df8bae1dSRodney W. Grimes }
631df8bae1dSRodney W. Grimes 
63226f9a767SRodney W. Grimes void
63391afe087SPoul-Henning Kamp timevalsub(struct timeval *t1, struct timeval *t2)
634df8bae1dSRodney W. Grimes {
635df8bae1dSRodney W. Grimes 
636df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
637df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
638df8bae1dSRodney W. Grimes 	timevalfix(t1);
639df8bae1dSRodney W. Grimes }
640df8bae1dSRodney W. Grimes 
64187b6de2bSPoul-Henning Kamp static void
64291afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
643df8bae1dSRodney W. Grimes {
644df8bae1dSRodney W. Grimes 
645df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
646df8bae1dSRodney W. Grimes 		t1->tv_sec--;
647df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
648df8bae1dSRodney W. Grimes 	}
649df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
650df8bae1dSRodney W. Grimes 		t1->tv_sec++;
651df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
652df8bae1dSRodney W. Grimes 	}
653df8bae1dSRodney W. Grimes }
65491974ce1SSam Leffler 
65591974ce1SSam Leffler /*
656addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
65791974ce1SSam Leffler  */
65891974ce1SSam Leffler int
65991974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
66091974ce1SSam Leffler {
66191974ce1SSam Leffler 	struct timeval tv, delta;
66291974ce1SSam Leffler 	int rv = 0;
66391974ce1SSam Leffler 
664addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
665addea9d4SSam Leffler 	delta = tv;
666addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
66791974ce1SSam Leffler 
66891974ce1SSam Leffler 	/*
66991974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
67091974ce1SSam Leffler 	 * even if interval is huge.
67191974ce1SSam Leffler 	 */
67291974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
67391974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
67491974ce1SSam Leffler 		*lasttime = tv;
67591974ce1SSam Leffler 		rv = 1;
67691974ce1SSam Leffler 	}
67791974ce1SSam Leffler 
67891974ce1SSam Leffler 	return (rv);
67991974ce1SSam Leffler }
68091974ce1SSam Leffler 
68191974ce1SSam Leffler /*
68291974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
683addea9d4SSam Leffler  *
684addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
685addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
686addea9d4SSam Leffler  *
687893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
688893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
689893bec80SSam Leffler  * limiting.
690893bec80SSam Leffler  *
691addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
692addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
693addea9d4SSam Leffler  * clock ticks for minimal overhead.
69491974ce1SSam Leffler  */
69591974ce1SSam Leffler int
69691974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
69791974ce1SSam Leffler {
698addea9d4SSam Leffler 	int now;
69991974ce1SSam Leffler 
70091974ce1SSam Leffler 	/*
701addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
702addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
703addea9d4SSam Leffler 	 * lasttime.
70491974ce1SSam Leffler 	 */
705addea9d4SSam Leffler 	now = ticks;
706addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
707addea9d4SSam Leffler 		lasttime->tv_sec = now;
708addea9d4SSam Leffler 		*curpps = 1;
709893bec80SSam Leffler 		return (maxpps != 0);
710addea9d4SSam Leffler 	} else {
711addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
712addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
713addea9d4SSam Leffler 	}
71491974ce1SSam Leffler }
715