xref: /freebsd/sys/kern/kern_time.c (revision 5cb3dc8fa301328202f071114e33acb70b9d6d6c)
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>
4894c8fcd8SPeter Wemm #include <sys/sysent.h>
49df8bae1dSRodney W. Grimes #include <sys/proc.h>
50708e7684SPeter Wemm #include <sys/time.h>
5191266b96SPoul-Henning Kamp #include <sys/timetc.h>
52df8bae1dSRodney W. Grimes #include <sys/vnode.h>
53fb919e4dSMark Murray 
545b870b7bSPeter Wemm #include <vm/vm.h>
555b870b7bSPeter Wemm #include <vm/vm_extern.h>
56df8bae1dSRodney W. Grimes 
5791f1c2b3SPoul-Henning Kamp int tz_minuteswest;
5891f1c2b3SPoul-Henning Kamp int tz_dsttime;
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 
1625cb3dc8fSPoul-Henning Kamp 	if (uap->clock_id == CLOCK_REALTIME)
1637ec73f64SPoul-Henning Kamp 		nanotime(&ats);
1645cb3dc8fSPoul-Henning Kamp 	else if (uap->clock_id == CLOCK_MONOTONIC)
1658f5ef1a9SPoul-Henning Kamp 		nanouptime(&ats);
1665cb3dc8fSPoul-Henning Kamp 	else
1675cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
168d1e405c5SAlfred Perlstein 	return (copyout(&ats, 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
18391afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
18494c8fcd8SPeter Wemm {
18594c8fcd8SPeter Wemm 	struct timeval atv;
18694c8fcd8SPeter Wemm 	struct timespec ats;
18794c8fcd8SPeter Wemm 	int error;
18894c8fcd8SPeter Wemm 
1894b8d5f2dSRobert Watson #ifdef MAC
1904b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
1914b8d5f2dSRobert Watson 	if (error)
1924b8d5f2dSRobert Watson 		return (error);
1934b8d5f2dSRobert Watson #endif
19444731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
1957edfb592SJohn Baldwin 		return (error);
196d1e405c5SAlfred Perlstein 	if (uap->clock_id != CLOCK_REALTIME)
1977edfb592SJohn Baldwin 		return (EINVAL);
198d1e405c5SAlfred Perlstein 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
1997edfb592SJohn Baldwin 		return (error);
2007edfb592SJohn Baldwin 	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
2017edfb592SJohn Baldwin 		return (EINVAL);
202a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
20394c8fcd8SPeter Wemm 	TIMESPEC_TO_TIMEVAL(&atv, &ats);
2047edfb592SJohn Baldwin 	error = settime(td, &atv);
20594c8fcd8SPeter Wemm 	return (error);
20694c8fcd8SPeter Wemm }
20794c8fcd8SPeter Wemm 
20894c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
20994c8fcd8SPeter Wemm struct clock_getres_args {
21094c8fcd8SPeter Wemm 	clockid_t clock_id;
21194c8fcd8SPeter Wemm 	struct	timespec *tp;
21294c8fcd8SPeter Wemm };
21394c8fcd8SPeter Wemm #endif
214708e7684SPeter Wemm 
21594c8fcd8SPeter Wemm int
21691afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
21794c8fcd8SPeter Wemm {
21894c8fcd8SPeter Wemm 	struct timespec ts;
219708e7684SPeter Wemm 	int error;
22094c8fcd8SPeter Wemm 
221d1e405c5SAlfred Perlstein 	if (uap->clock_id != CLOCK_REALTIME)
22294c8fcd8SPeter Wemm 		return (EINVAL);
223708e7684SPeter Wemm 	error = 0;
224d1e405c5SAlfred Perlstein 	if (uap->tp) {
22594c8fcd8SPeter Wemm 		ts.tv_sec = 0;
226ac0653dcSBruce Evans 		/*
227ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
228ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
229ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
230ac0653dcSBruce Evans 		 */
231ac0653dcSBruce Evans 		ts.tv_nsec = 1000000000 / tc_getfrequency() + 1;
232d1e405c5SAlfred Perlstein 		error = copyout(&ts, uap->tp, sizeof(ts));
23394c8fcd8SPeter Wemm 	}
234708e7684SPeter Wemm 	return (error);
23594c8fcd8SPeter Wemm }
23694c8fcd8SPeter Wemm 
23794c8fcd8SPeter Wemm static int nanowait;
23894c8fcd8SPeter Wemm 
2395b870b7bSPeter Wemm static int
24091afe087SPoul-Henning Kamp nanosleep1(struct thread *td, struct timespec *rqt, struct timespec *rmt)
2415b870b7bSPeter Wemm {
2425704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
24333841826SPoul-Henning Kamp 	struct timeval tv;
24433841826SPoul-Henning Kamp 	int error;
2455b870b7bSPeter Wemm 
2467d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
247708e7684SPeter Wemm 		return (EINVAL);
248d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
2497d7fb492SBruce Evans 		return (0);
250c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
25100af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
25233841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
25333841826SPoul-Henning Kamp 	for (;;) {
25433841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
25533841826SPoul-Henning Kamp 		    tvtohz(&tv));
256c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
25733841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
25833841826SPoul-Henning Kamp 			if (error == ERESTART)
25994c8fcd8SPeter Wemm 				error = EINTR;
26033841826SPoul-Henning Kamp 			if (rmt != NULL) {
26133841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
26233841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
26333841826SPoul-Henning Kamp 					timespecclear(&ts);
26400af9731SPoul-Henning Kamp 				*rmt = ts;
26500af9731SPoul-Henning Kamp 			}
26633841826SPoul-Henning Kamp 			return (error);
26733841826SPoul-Henning Kamp 		}
26833841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
26933841826SPoul-Henning Kamp 			return (0);
2705704ba6aSPoul-Henning Kamp 		ts3 = ts;
2715704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
2725704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
27333841826SPoul-Henning Kamp 	}
2745b870b7bSPeter Wemm }
27594c8fcd8SPeter Wemm 
2765b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
2775b870b7bSPeter Wemm struct nanosleep_args {
2785b870b7bSPeter Wemm 	struct	timespec *rqtp;
2795b870b7bSPeter Wemm 	struct	timespec *rmtp;
2805b870b7bSPeter Wemm };
2815b870b7bSPeter Wemm #endif
2825b870b7bSPeter Wemm 
283fb99ab88SMatthew Dillon /*
284fb99ab88SMatthew Dillon  * MPSAFE
285fb99ab88SMatthew Dillon  */
2865b870b7bSPeter Wemm /* ARGSUSED */
2875b870b7bSPeter Wemm int
28891afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
2895b870b7bSPeter Wemm {
2905b870b7bSPeter Wemm 	struct timespec rmt, rqt;
291fb99ab88SMatthew Dillon 	int error;
2925b870b7bSPeter Wemm 
293d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
2945b870b7bSPeter Wemm 	if (error)
2955b870b7bSPeter Wemm 		return (error);
296fb99ab88SMatthew Dillon 
29731f3e2adSAlfred Perlstein 	if (uap->rmtp &&
29831f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
29931f3e2adSAlfred Perlstein 			return (EFAULT);
300b40ce416SJulian Elischer 	error = nanosleep1(td, &rqt, &rmt);
301d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
302fb99ab88SMatthew Dillon 		int error2;
303fb99ab88SMatthew Dillon 
304d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
30531f3e2adSAlfred Perlstein 		if (error2)
306fb99ab88SMatthew Dillon 			error = error2;
307708e7684SPeter Wemm 	}
308708e7684SPeter Wemm 	return (error);
30994c8fcd8SPeter Wemm }
31094c8fcd8SPeter Wemm 
3115b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
312df8bae1dSRodney W. Grimes struct gettimeofday_args {
313df8bae1dSRodney W. Grimes 	struct	timeval *tp;
314df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
315df8bae1dSRodney W. Grimes };
316d2d3e875SBruce Evans #endif
317fb99ab88SMatthew Dillon /*
318fb99ab88SMatthew Dillon  * MPSAFE
319fb99ab88SMatthew Dillon  */
320df8bae1dSRodney W. Grimes /* ARGSUSED */
32126f9a767SRodney W. Grimes int
32291afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
323df8bae1dSRodney W. Grimes {
324df8bae1dSRodney W. Grimes 	struct timeval atv;
325411c25edSTim J. Robbins 	struct timezone rtz;
326df8bae1dSRodney W. Grimes 	int error = 0;
327df8bae1dSRodney W. Grimes 
328df8bae1dSRodney W. Grimes 	if (uap->tp) {
329df8bae1dSRodney W. Grimes 		microtime(&atv);
33001609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
331df8bae1dSRodney W. Grimes 	}
33221dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
33391f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
33491f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
335411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
33621dcdb38SPoul-Henning Kamp 	}
337df8bae1dSRodney W. Grimes 	return (error);
338df8bae1dSRodney W. Grimes }
339df8bae1dSRodney W. Grimes 
340d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
341df8bae1dSRodney W. Grimes struct settimeofday_args {
342df8bae1dSRodney W. Grimes 	struct	timeval *tv;
343df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
344df8bae1dSRodney W. Grimes };
345d2d3e875SBruce Evans #endif
346fb99ab88SMatthew Dillon /*
347fb99ab88SMatthew Dillon  * MPSAFE
348fb99ab88SMatthew Dillon  */
349df8bae1dSRodney W. Grimes /* ARGSUSED */
35026f9a767SRodney W. Grimes int
35191afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
352df8bae1dSRodney W. Grimes {
353708e7684SPeter Wemm 	struct timeval atv;
354df8bae1dSRodney W. Grimes 	struct timezone atz;
355fb99ab88SMatthew Dillon 	int error = 0;
356fb99ab88SMatthew Dillon 
3574b8d5f2dSRobert Watson #ifdef MAC
3584b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
3594b8d5f2dSRobert Watson 	if (error)
3604b8d5f2dSRobert Watson 		return (error);
3614b8d5f2dSRobert Watson #endif
36244731cabSJohn Baldwin 	if ((error = suser(td)))
3637edfb592SJohn Baldwin 		return (error);
364df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
365708e7684SPeter Wemm 	if (uap->tv) {
36601609114SAlfred Perlstein 		if ((error = copyin(uap->tv, &atv, sizeof(atv))))
3677edfb592SJohn Baldwin 			return (error);
3687edfb592SJohn Baldwin 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
3697edfb592SJohn Baldwin 			return (EINVAL);
370708e7684SPeter Wemm 	}
371df8bae1dSRodney W. Grimes 	if (uap->tzp &&
37201609114SAlfred Perlstein 	    (error = copyin(uap->tzp, &atz, sizeof(atz))))
3737edfb592SJohn Baldwin 		return (error);
3747edfb592SJohn Baldwin 
3757edfb592SJohn Baldwin 	if (uap->tv && (error = settime(td, &atv)))
3767edfb592SJohn Baldwin 		return (error);
3777edfb592SJohn Baldwin 	if (uap->tzp) {
37891f1c2b3SPoul-Henning Kamp 		tz_minuteswest = atz.tz_minuteswest;
37991f1c2b3SPoul-Henning Kamp 		tz_dsttime = atz.tz_dsttime;
3807edfb592SJohn Baldwin 	}
381fb99ab88SMatthew Dillon 	return (error);
382df8bae1dSRodney W. Grimes }
383df8bae1dSRodney W. Grimes /*
384df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
385df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
386df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
387df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
388df8bae1dSRodney W. Grimes  *
389df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
390df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
391df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
392df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
393df8bae1dSRodney W. Grimes  *
394df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
395df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
396df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
397df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
398df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
399df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
400df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
401df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
402df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
403df8bae1dSRodney W. Grimes  */
404d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
405df8bae1dSRodney W. Grimes struct getitimer_args {
406df8bae1dSRodney W. Grimes 	u_int	which;
407df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
408df8bae1dSRodney W. Grimes };
409d2d3e875SBruce Evans #endif
410fb99ab88SMatthew Dillon /*
411fb99ab88SMatthew Dillon  * MPSAFE
412fb99ab88SMatthew Dillon  */
41326f9a767SRodney W. Grimes int
41491afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
415df8bae1dSRodney W. Grimes {
416b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
417227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
418df8bae1dSRodney W. Grimes 	struct itimerval aitv;
419df8bae1dSRodney W. Grimes 
420df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
421df8bae1dSRodney W. Grimes 		return (EINVAL);
422fb99ab88SMatthew Dillon 
423df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
424df8bae1dSRodney W. Grimes 		/*
425ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
426df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
427df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
428df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
429df8bae1dSRodney W. Grimes 		 */
43096d7f8efSTim J. Robbins 		PROC_LOCK(p);
431df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
43296d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
4334cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value)) {
434c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
4354cf41af3SPoul-Henning Kamp 			if (timevalcmp(&aitv.it_value, &ctv, <))
4364cf41af3SPoul-Henning Kamp 				timevalclear(&aitv.it_value);
437df8bae1dSRodney W. Grimes 			else
438227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
439227ee8a1SPoul-Henning Kamp 		}
440fb99ab88SMatthew Dillon 	} else {
44196d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
442df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
44396d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
444fb99ab88SMatthew Dillon 	}
445411c25edSTim J. Robbins 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
446df8bae1dSRodney W. Grimes }
447df8bae1dSRodney W. Grimes 
448d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
449df8bae1dSRodney W. Grimes struct setitimer_args {
450df8bae1dSRodney W. Grimes 	u_int	which;
451df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
452df8bae1dSRodney W. Grimes };
453d2d3e875SBruce Evans #endif
454fb99ab88SMatthew Dillon /*
455fb99ab88SMatthew Dillon  * MPSAFE
456fb99ab88SMatthew Dillon  */
45726f9a767SRodney W. Grimes int
45891afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
459df8bae1dSRodney W. Grimes {
460b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
46196d7f8efSTim J. Robbins 	struct itimerval aitv, oitv;
462227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
46396d7f8efSTim J. Robbins 	int error;
46496d7f8efSTim J. Robbins 
46596d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
46696d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
46796d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
46896d7f8efSTim J. Robbins 	}
469df8bae1dSRodney W. Grimes 
470df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
471df8bae1dSRodney W. Grimes 		return (EINVAL);
47296d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
473df8bae1dSRodney W. Grimes 		return (error);
47496d7f8efSTim J. Robbins 	if (itimerfix(&aitv.it_value))
47596d7f8efSTim J. Robbins 		return (EINVAL);
47696d7f8efSTim J. Robbins 	if (!timevalisset(&aitv.it_value))
4774cf41af3SPoul-Henning Kamp 		timevalclear(&aitv.it_interval);
47896d7f8efSTim J. Robbins 	else if (itimerfix(&aitv.it_interval))
47996d7f8efSTim J. Robbins 		return (EINVAL);
48096d7f8efSTim J. Robbins 
481df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
48296d7f8efSTim J. Robbins 		PROC_LOCK(p);
4834cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
4844f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
4854cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value))
4864f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
4874f559836SJake Burkholder 			    realitexpire, p);
488c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
489bfe6c9faSPoul-Henning Kamp 		timevaladd(&aitv.it_value, &ctv);
49096d7f8efSTim J. Robbins 		oitv = p->p_realtimer;
491df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
49296d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
49396d7f8efSTim J. Robbins 		if (timevalisset(&oitv.it_value)) {
49496d7f8efSTim J. Robbins 			if (timevalcmp(&oitv.it_value, &ctv, <))
49596d7f8efSTim J. Robbins 				timevalclear(&oitv.it_value);
49696d7f8efSTim J. Robbins 			else
49796d7f8efSTim J. Robbins 				timevalsub(&oitv.it_value, &ctv);
498fb99ab88SMatthew Dillon 		}
49996d7f8efSTim J. Robbins 	} else {
50096d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
50196d7f8efSTim J. Robbins 		oitv = p->p_stats->p_timer[uap->which];
50296d7f8efSTim J. Robbins 		p->p_stats->p_timer[uap->which] = aitv;
50396d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
50496d7f8efSTim J. Robbins 	}
50596d7f8efSTim J. Robbins 	if (uap->oitv == NULL)
50696d7f8efSTim J. Robbins 		return (0);
50796d7f8efSTim J. Robbins 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
508df8bae1dSRodney W. Grimes }
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes /*
511df8bae1dSRodney W. Grimes  * Real interval timer expired:
512df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
513df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
514df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
515df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
516df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
517c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
5189207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
5199207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
5209207f00aSBruce Evans  * interrupt even when we're delayed.
521df8bae1dSRodney W. Grimes  */
522df8bae1dSRodney W. Grimes void
52391afe087SPoul-Henning Kamp realitexpire(void *arg)
524df8bae1dSRodney W. Grimes {
52591afe087SPoul-Henning Kamp 	struct proc *p;
526bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
527df8bae1dSRodney W. Grimes 
528df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
52937824023SJohn Baldwin 	PROC_LOCK(p);
530df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
5314cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
5324cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
53337824023SJohn Baldwin 		PROC_UNLOCK(p);
534df8bae1dSRodney W. Grimes 		return;
535df8bae1dSRodney W. Grimes 	}
536df8bae1dSRodney W. Grimes 	for (;;) {
537df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
538df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
539c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
5404cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
541bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
542bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
5434f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
5444f559836SJake Burkholder 			    realitexpire, p);
54537824023SJohn Baldwin 			PROC_UNLOCK(p);
546df8bae1dSRodney W. Grimes 			return;
547df8bae1dSRodney W. Grimes 		}
548df8bae1dSRodney W. Grimes 	}
54937824023SJohn Baldwin 	/*NOTREACHED*/
550df8bae1dSRodney W. Grimes }
551df8bae1dSRodney W. Grimes 
552df8bae1dSRodney W. Grimes /*
553df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
554df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
555df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
556df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
557df8bae1dSRodney W. Grimes  */
55826f9a767SRodney W. Grimes int
55991afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
560df8bae1dSRodney W. Grimes {
561df8bae1dSRodney W. Grimes 
562df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
563df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
564df8bae1dSRodney W. Grimes 		return (EINVAL);
565df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
566df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
567df8bae1dSRodney W. Grimes 	return (0);
568df8bae1dSRodney W. Grimes }
569df8bae1dSRodney W. Grimes 
570df8bae1dSRodney W. Grimes /*
571df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
572df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
573df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
574df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
575df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
576df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
577df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
578df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
579df8bae1dSRodney W. Grimes  */
58026f9a767SRodney W. Grimes int
58191afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
582df8bae1dSRodney W. Grimes {
583df8bae1dSRodney W. Grimes 
584df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
585df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
586df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
587df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
588df8bae1dSRodney W. Grimes 			goto expire;
589df8bae1dSRodney W. Grimes 		}
590df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
591df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
592df8bae1dSRodney W. Grimes 	}
593df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
594df8bae1dSRodney W. Grimes 	usec = 0;
5954cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
596df8bae1dSRodney W. Grimes 		return (1);
597df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
598df8bae1dSRodney W. Grimes expire:
5994cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
600df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
601df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
602df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
603df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
604df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
605df8bae1dSRodney W. Grimes 		}
606df8bae1dSRodney W. Grimes 	} else
607df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
608df8bae1dSRodney W. Grimes 	return (0);
609df8bae1dSRodney W. Grimes }
610df8bae1dSRodney W. Grimes 
611df8bae1dSRodney W. Grimes /*
612df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
613df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
614df8bae1dSRodney W. Grimes  * results which are before the beginning,
615df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
616df8bae1dSRodney W. Grimes  * Caveat emptor.
617df8bae1dSRodney W. Grimes  */
61826f9a767SRodney W. Grimes void
61991afe087SPoul-Henning Kamp timevaladd(struct timeval *t1, struct timeval *t2)
620df8bae1dSRodney W. Grimes {
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
623df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
624df8bae1dSRodney W. Grimes 	timevalfix(t1);
625df8bae1dSRodney W. Grimes }
626df8bae1dSRodney W. Grimes 
62726f9a767SRodney W. Grimes void
62891afe087SPoul-Henning Kamp timevalsub(struct timeval *t1, struct timeval *t2)
629df8bae1dSRodney W. Grimes {
630df8bae1dSRodney W. Grimes 
631df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
632df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
633df8bae1dSRodney W. Grimes 	timevalfix(t1);
634df8bae1dSRodney W. Grimes }
635df8bae1dSRodney W. Grimes 
63687b6de2bSPoul-Henning Kamp static void
63791afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
638df8bae1dSRodney W. Grimes {
639df8bae1dSRodney W. Grimes 
640df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
641df8bae1dSRodney W. Grimes 		t1->tv_sec--;
642df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
643df8bae1dSRodney W. Grimes 	}
644df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
645df8bae1dSRodney W. Grimes 		t1->tv_sec++;
646df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
647df8bae1dSRodney W. Grimes 	}
648df8bae1dSRodney W. Grimes }
64991974ce1SSam Leffler 
65091974ce1SSam Leffler /*
651addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
65291974ce1SSam Leffler  */
65391974ce1SSam Leffler int
65491974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
65591974ce1SSam Leffler {
65691974ce1SSam Leffler 	struct timeval tv, delta;
65791974ce1SSam Leffler 	int rv = 0;
65891974ce1SSam Leffler 
659addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
660addea9d4SSam Leffler 	delta = tv;
661addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
66291974ce1SSam Leffler 
66391974ce1SSam Leffler 	/*
66491974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
66591974ce1SSam Leffler 	 * even if interval is huge.
66691974ce1SSam Leffler 	 */
66791974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
66891974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
66991974ce1SSam Leffler 		*lasttime = tv;
67091974ce1SSam Leffler 		rv = 1;
67191974ce1SSam Leffler 	}
67291974ce1SSam Leffler 
67391974ce1SSam Leffler 	return (rv);
67491974ce1SSam Leffler }
67591974ce1SSam Leffler 
67691974ce1SSam Leffler /*
67791974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
678addea9d4SSam Leffler  *
679addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
680addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
681addea9d4SSam Leffler  *
682addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
683addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
684addea9d4SSam Leffler  * clock ticks for minimal overhead.
68591974ce1SSam Leffler  */
68691974ce1SSam Leffler int
68791974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
68891974ce1SSam Leffler {
689addea9d4SSam Leffler 	int now;
69091974ce1SSam Leffler 
69191974ce1SSam Leffler 	/*
692addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
693addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
694addea9d4SSam Leffler 	 * lasttime.
69591974ce1SSam Leffler 	 */
696addea9d4SSam Leffler 	now = ticks;
697addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
698addea9d4SSam Leffler 		lasttime->tv_sec = now;
699addea9d4SSam Leffler 		*curpps = 1;
700addea9d4SSam Leffler 		return (1);
701addea9d4SSam Leffler 	} else {
702addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
703addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
704addea9d4SSam Leffler 	}
70591974ce1SSam Leffler }
706