xref: /freebsd/sys/kern/kern_time.c (revision 78c85e8dfc1c4a90a3ecb51acf35c33fe1faf331)
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  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
354b8d5f2dSRobert Watson #include "opt_mac.h"
364b8d5f2dSRobert Watson 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38e96c1fdcSPeter Wemm #include <sys/systm.h>
39fb919e4dSMark Murray #include <sys/lock.h>
40fb919e4dSMark Murray #include <sys/mutex.h>
41d2d3e875SBruce Evans #include <sys/sysproto.h>
42df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
43797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
44df8bae1dSRodney W. Grimes #include <sys/kernel.h>
454b8d5f2dSRobert Watson #include <sys/mac.h>
4694c8fcd8SPeter Wemm #include <sys/sysent.h>
47df8bae1dSRodney W. Grimes #include <sys/proc.h>
48708e7684SPeter Wemm #include <sys/time.h>
4991266b96SPoul-Henning Kamp #include <sys/timetc.h>
50df8bae1dSRodney W. Grimes #include <sys/vnode.h>
51fb919e4dSMark Murray 
525b870b7bSPeter Wemm #include <vm/vm.h>
535b870b7bSPeter Wemm #include <vm/vm_extern.h>
54df8bae1dSRodney W. Grimes 
5591f1c2b3SPoul-Henning Kamp int tz_minuteswest;
5691f1c2b3SPoul-Henning Kamp int tz_dsttime;
57ac7e6123SDavid Greenman 
58df8bae1dSRodney W. Grimes /*
59df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
62df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
63df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
64df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
65df8bae1dSRodney W. Grimes  * timers when they expire.
66df8bae1dSRodney W. Grimes  */
67df8bae1dSRodney W. Grimes 
684d77a549SAlfred Perlstein static int	nanosleep1(struct thread *td, struct timespec *rqt,
694d77a549SAlfred Perlstein 		    struct timespec *rmt);
707edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
714d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
724d77a549SAlfred Perlstein static void	no_lease_updatetime(int);
7394c8fcd8SPeter Wemm 
741b09ae77SPoul-Henning Kamp static void
751b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
761b09ae77SPoul-Henning Kamp 	int deltat;
771b09ae77SPoul-Henning Kamp {
781b09ae77SPoul-Henning Kamp }
791b09ae77SPoul-Henning Kamp 
804d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
811b09ae77SPoul-Henning Kamp 
8294c8fcd8SPeter Wemm static int
8391afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
8494c8fcd8SPeter Wemm {
85fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
86c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
877ec73f64SPoul-Henning Kamp 	struct timespec ts;
8894c8fcd8SPeter Wemm 	int s;
8994c8fcd8SPeter Wemm 
90708e7684SPeter Wemm 	s = splclock();
919c8fff87SBruce Evans 	microtime(&tv1);
9200af9731SPoul-Henning Kamp 	delta = *tv;
9300af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
9494c8fcd8SPeter Wemm 
9594c8fcd8SPeter Wemm 	/*
969c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
97fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
98fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
99fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
100fcae3aa6SNick Sayer 	 * back to the past.
101c0bd94a7SNick Sayer 	 *
102c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
103c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
104c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
10594c8fcd8SPeter Wemm 	 */
1067edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
107fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1083f92429aSMatt Jacob 			/*
109c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1103f92429aSMatt Jacob 			 */
111fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
112fcae3aa6SNick Sayer 				maxtime = tv1;
113fcae3aa6SNick Sayer 			tv2 = *tv;
114fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
115fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1163f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
117fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
118fcae3aa6SNick Sayer 			}
1193f92429aSMatt Jacob 		} else {
120c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
121c0bd94a7SNick Sayer 				splx(s);
122c0bd94a7SNick Sayer 				return (EPERM);
123c0bd94a7SNick Sayer 			}
124c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
125c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
126c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
127c0bd94a7SNick Sayer 			}
128c0bd94a7SNick Sayer 			laststep = *tv;
129fcae3aa6SNick Sayer 		}
1309c8fff87SBruce Evans 	}
1319c8fff87SBruce Evans 
1327ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1337ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1347edfb592SJohn Baldwin 	mtx_lock(&Giant);
13591266b96SPoul-Henning Kamp 	tc_setclock(&ts);
13694c8fcd8SPeter Wemm 	(void) splsoftclock();
13794c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
13894c8fcd8SPeter Wemm 	splx(s);
13994c8fcd8SPeter Wemm 	resettodr();
1407edfb592SJohn Baldwin 	mtx_unlock(&Giant);
14194c8fcd8SPeter Wemm 	return (0);
14294c8fcd8SPeter Wemm }
14394c8fcd8SPeter Wemm 
14494c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
14594c8fcd8SPeter Wemm struct clock_gettime_args {
14694c8fcd8SPeter Wemm 	clockid_t clock_id;
14794c8fcd8SPeter Wemm 	struct	timespec *tp;
14894c8fcd8SPeter Wemm };
14994c8fcd8SPeter Wemm #endif
150708e7684SPeter Wemm 
151fb99ab88SMatthew Dillon /*
152fb99ab88SMatthew Dillon  * MPSAFE
153fb99ab88SMatthew Dillon  */
15494c8fcd8SPeter Wemm /* ARGSUSED */
15594c8fcd8SPeter Wemm int
15691afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
15794c8fcd8SPeter Wemm {
15894c8fcd8SPeter Wemm 	struct timespec ats;
159de0a9241SKelly Yancey 	struct timeval sys, user;
16078c85e8dSJohn Baldwin 	struct proc *p;
16194c8fcd8SPeter Wemm 
16278c85e8dSJohn Baldwin 	p = td->td_proc;
163b8817154SKelly Yancey 	switch (uap->clock_id) {
164b8817154SKelly Yancey 	case CLOCK_REALTIME:
1657ec73f64SPoul-Henning Kamp 		nanotime(&ats);
166b8817154SKelly Yancey 		break;
167b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
16878c85e8dSJohn Baldwin 		PROC_LOCK(p);
16978c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
17078c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
171b8817154SKelly Yancey 		TIMEVAL_TO_TIMESPEC(&user, &ats);
172b8817154SKelly Yancey 		break;
173b8817154SKelly Yancey 	case CLOCK_PROF:
17478c85e8dSJohn Baldwin 		PROC_LOCK(p);
17578c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
17678c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
177de0a9241SKelly Yancey 		timevaladd(&user, &sys);
178de0a9241SKelly Yancey 		TIMEVAL_TO_TIMESPEC(&user, &ats);
179de0a9241SKelly Yancey 		break;
180de0a9241SKelly Yancey 	case CLOCK_MONOTONIC:
181de0a9241SKelly Yancey 		nanouptime(&ats);
182b8817154SKelly Yancey 		break;
183b8817154SKelly Yancey 	default:
1845cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
185b8817154SKelly Yancey 	}
186d1e405c5SAlfred Perlstein 	return (copyout(&ats, uap->tp, sizeof(ats)));
18794c8fcd8SPeter Wemm }
18894c8fcd8SPeter Wemm 
18994c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
19094c8fcd8SPeter Wemm struct clock_settime_args {
19194c8fcd8SPeter Wemm 	clockid_t clock_id;
19294c8fcd8SPeter Wemm 	const struct	timespec *tp;
19394c8fcd8SPeter Wemm };
19494c8fcd8SPeter Wemm #endif
195708e7684SPeter Wemm 
196fb99ab88SMatthew Dillon /*
197fb99ab88SMatthew Dillon  * MPSAFE
198fb99ab88SMatthew Dillon  */
19994c8fcd8SPeter Wemm /* ARGSUSED */
20094c8fcd8SPeter Wemm int
20191afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
20294c8fcd8SPeter Wemm {
20394c8fcd8SPeter Wemm 	struct timeval atv;
20494c8fcd8SPeter Wemm 	struct timespec ats;
20594c8fcd8SPeter Wemm 	int error;
20694c8fcd8SPeter Wemm 
2074b8d5f2dSRobert Watson #ifdef MAC
2084b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
2094b8d5f2dSRobert Watson 	if (error)
2104b8d5f2dSRobert Watson 		return (error);
2114b8d5f2dSRobert Watson #endif
21244731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
2137edfb592SJohn Baldwin 		return (error);
214d1e405c5SAlfred Perlstein 	if (uap->clock_id != CLOCK_REALTIME)
2157edfb592SJohn Baldwin 		return (EINVAL);
216d1e405c5SAlfred Perlstein 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
2177edfb592SJohn Baldwin 		return (error);
2187edfb592SJohn Baldwin 	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
2197edfb592SJohn Baldwin 		return (EINVAL);
220a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
22194c8fcd8SPeter Wemm 	TIMESPEC_TO_TIMEVAL(&atv, &ats);
2227edfb592SJohn Baldwin 	error = settime(td, &atv);
22394c8fcd8SPeter Wemm 	return (error);
22494c8fcd8SPeter Wemm }
22594c8fcd8SPeter Wemm 
22694c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
22794c8fcd8SPeter Wemm struct clock_getres_args {
22894c8fcd8SPeter Wemm 	clockid_t clock_id;
22994c8fcd8SPeter Wemm 	struct	timespec *tp;
23094c8fcd8SPeter Wemm };
23194c8fcd8SPeter Wemm #endif
232708e7684SPeter Wemm 
23394c8fcd8SPeter Wemm int
23491afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
23594c8fcd8SPeter Wemm {
23694c8fcd8SPeter Wemm 	struct timespec ts;
23794c8fcd8SPeter Wemm 
23894c8fcd8SPeter Wemm 	ts.tv_sec = 0;
239b8817154SKelly Yancey 	switch (uap->clock_id) {
240b8817154SKelly Yancey 	case CLOCK_REALTIME:
241b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
242ac0653dcSBruce Evans 		/*
243ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
244ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
245ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
246ac0653dcSBruce Evans 		 */
247ac0653dcSBruce Evans 		ts.tv_nsec = 1000000000 / tc_getfrequency() + 1;
248b8817154SKelly Yancey 		break;
249b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
250b8817154SKelly Yancey 	case CLOCK_PROF:
251b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
252b8817154SKelly Yancey 		ts.tv_nsec = (1000000000 + hz - 1) / hz;
253b8817154SKelly Yancey 		break;
254b8817154SKelly Yancey 	default:
255b8817154SKelly Yancey 		return (EINVAL);
25694c8fcd8SPeter Wemm 	}
257de0a9241SKelly Yancey 	if (uap->tp == NULL)
258de0a9241SKelly Yancey 		return (0);
259de0a9241SKelly Yancey 	return (copyout(&ts, uap->tp, sizeof(ts)));
26094c8fcd8SPeter Wemm }
26194c8fcd8SPeter Wemm 
26294c8fcd8SPeter Wemm static int nanowait;
26394c8fcd8SPeter Wemm 
2645b870b7bSPeter Wemm static int
26591afe087SPoul-Henning Kamp nanosleep1(struct thread *td, struct timespec *rqt, struct timespec *rmt)
2665b870b7bSPeter Wemm {
2675704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
26833841826SPoul-Henning Kamp 	struct timeval tv;
26933841826SPoul-Henning Kamp 	int error;
2705b870b7bSPeter Wemm 
2717d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
272708e7684SPeter Wemm 		return (EINVAL);
273d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
2747d7fb492SBruce Evans 		return (0);
275c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
27600af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
27733841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
27833841826SPoul-Henning Kamp 	for (;;) {
27933841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
28033841826SPoul-Henning Kamp 		    tvtohz(&tv));
281c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
28233841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
28333841826SPoul-Henning Kamp 			if (error == ERESTART)
28494c8fcd8SPeter Wemm 				error = EINTR;
28533841826SPoul-Henning Kamp 			if (rmt != NULL) {
28633841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
28733841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
28833841826SPoul-Henning Kamp 					timespecclear(&ts);
28900af9731SPoul-Henning Kamp 				*rmt = ts;
29000af9731SPoul-Henning Kamp 			}
29133841826SPoul-Henning Kamp 			return (error);
29233841826SPoul-Henning Kamp 		}
29333841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
29433841826SPoul-Henning Kamp 			return (0);
2955704ba6aSPoul-Henning Kamp 		ts3 = ts;
2965704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
2975704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
29833841826SPoul-Henning Kamp 	}
2995b870b7bSPeter Wemm }
30094c8fcd8SPeter Wemm 
3015b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
3025b870b7bSPeter Wemm struct nanosleep_args {
3035b870b7bSPeter Wemm 	struct	timespec *rqtp;
3045b870b7bSPeter Wemm 	struct	timespec *rmtp;
3055b870b7bSPeter Wemm };
3065b870b7bSPeter Wemm #endif
3075b870b7bSPeter Wemm 
308fb99ab88SMatthew Dillon /*
309fb99ab88SMatthew Dillon  * MPSAFE
310fb99ab88SMatthew Dillon  */
3115b870b7bSPeter Wemm /* ARGSUSED */
3125b870b7bSPeter Wemm int
31391afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
3145b870b7bSPeter Wemm {
3155b870b7bSPeter Wemm 	struct timespec rmt, rqt;
316fb99ab88SMatthew Dillon 	int error;
3175b870b7bSPeter Wemm 
318d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
3195b870b7bSPeter Wemm 	if (error)
3205b870b7bSPeter Wemm 		return (error);
321fb99ab88SMatthew Dillon 
32231f3e2adSAlfred Perlstein 	if (uap->rmtp &&
32331f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
32431f3e2adSAlfred Perlstein 			return (EFAULT);
325b40ce416SJulian Elischer 	error = nanosleep1(td, &rqt, &rmt);
326d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
327fb99ab88SMatthew Dillon 		int error2;
328fb99ab88SMatthew Dillon 
329d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
33031f3e2adSAlfred Perlstein 		if (error2)
331fb99ab88SMatthew Dillon 			error = error2;
332708e7684SPeter Wemm 	}
333708e7684SPeter Wemm 	return (error);
33494c8fcd8SPeter Wemm }
33594c8fcd8SPeter Wemm 
3365b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
337df8bae1dSRodney W. Grimes struct gettimeofday_args {
338df8bae1dSRodney W. Grimes 	struct	timeval *tp;
339df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
340df8bae1dSRodney W. Grimes };
341d2d3e875SBruce Evans #endif
342fb99ab88SMatthew Dillon /*
343fb99ab88SMatthew Dillon  * MPSAFE
344fb99ab88SMatthew Dillon  */
345df8bae1dSRodney W. Grimes /* ARGSUSED */
34626f9a767SRodney W. Grimes int
34791afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
348df8bae1dSRodney W. Grimes {
349df8bae1dSRodney W. Grimes 	struct timeval atv;
350411c25edSTim J. Robbins 	struct timezone rtz;
351df8bae1dSRodney W. Grimes 	int error = 0;
352df8bae1dSRodney W. Grimes 
353df8bae1dSRodney W. Grimes 	if (uap->tp) {
354df8bae1dSRodney W. Grimes 		microtime(&atv);
35501609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
356df8bae1dSRodney W. Grimes 	}
35721dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
35891f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
35991f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
360411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
36121dcdb38SPoul-Henning Kamp 	}
362df8bae1dSRodney W. Grimes 	return (error);
363df8bae1dSRodney W. Grimes }
364df8bae1dSRodney W. Grimes 
365d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
366df8bae1dSRodney W. Grimes struct settimeofday_args {
367df8bae1dSRodney W. Grimes 	struct	timeval *tv;
368df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
369df8bae1dSRodney W. Grimes };
370d2d3e875SBruce Evans #endif
371fb99ab88SMatthew Dillon /*
372fb99ab88SMatthew Dillon  * MPSAFE
373fb99ab88SMatthew Dillon  */
374df8bae1dSRodney W. Grimes /* ARGSUSED */
37526f9a767SRodney W. Grimes int
37691afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
377df8bae1dSRodney W. Grimes {
378708e7684SPeter Wemm 	struct timeval atv;
379df8bae1dSRodney W. Grimes 	struct timezone atz;
380fb99ab88SMatthew Dillon 	int error = 0;
381fb99ab88SMatthew Dillon 
3824b8d5f2dSRobert Watson #ifdef MAC
3834b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
3844b8d5f2dSRobert Watson 	if (error)
3854b8d5f2dSRobert Watson 		return (error);
3864b8d5f2dSRobert Watson #endif
38744731cabSJohn Baldwin 	if ((error = suser(td)))
3887edfb592SJohn Baldwin 		return (error);
389df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
390708e7684SPeter Wemm 	if (uap->tv) {
39101609114SAlfred Perlstein 		if ((error = copyin(uap->tv, &atv, sizeof(atv))))
3927edfb592SJohn Baldwin 			return (error);
3937edfb592SJohn Baldwin 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
3947edfb592SJohn Baldwin 			return (EINVAL);
395708e7684SPeter Wemm 	}
396df8bae1dSRodney W. Grimes 	if (uap->tzp &&
39701609114SAlfred Perlstein 	    (error = copyin(uap->tzp, &atz, sizeof(atz))))
3987edfb592SJohn Baldwin 		return (error);
3997edfb592SJohn Baldwin 
4007edfb592SJohn Baldwin 	if (uap->tv && (error = settime(td, &atv)))
4017edfb592SJohn Baldwin 		return (error);
4027edfb592SJohn Baldwin 	if (uap->tzp) {
40391f1c2b3SPoul-Henning Kamp 		tz_minuteswest = atz.tz_minuteswest;
40491f1c2b3SPoul-Henning Kamp 		tz_dsttime = atz.tz_dsttime;
4057edfb592SJohn Baldwin 	}
406fb99ab88SMatthew Dillon 	return (error);
407df8bae1dSRodney W. Grimes }
408df8bae1dSRodney W. Grimes /*
409df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
410df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
411df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
412df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
413df8bae1dSRodney W. Grimes  *
414df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
415df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
416df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
417df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
418df8bae1dSRodney W. Grimes  *
419df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
420df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
421df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
422df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
423df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
424df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
425df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
426df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
427df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
428df8bae1dSRodney W. Grimes  */
429d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
430df8bae1dSRodney W. Grimes struct getitimer_args {
431df8bae1dSRodney W. Grimes 	u_int	which;
432df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
433df8bae1dSRodney W. Grimes };
434d2d3e875SBruce Evans #endif
435fb99ab88SMatthew Dillon /*
436fb99ab88SMatthew Dillon  * MPSAFE
437fb99ab88SMatthew Dillon  */
43826f9a767SRodney W. Grimes int
43991afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
440df8bae1dSRodney W. Grimes {
441b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
442227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
443df8bae1dSRodney W. Grimes 	struct itimerval aitv;
444df8bae1dSRodney W. Grimes 
445df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
446df8bae1dSRodney W. Grimes 		return (EINVAL);
447fb99ab88SMatthew Dillon 
448df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
449df8bae1dSRodney W. Grimes 		/*
450ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
451df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
452df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
453df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
454df8bae1dSRodney W. Grimes 		 */
45596d7f8efSTim J. Robbins 		PROC_LOCK(p);
456df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
45796d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
4584cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value)) {
459c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
4604cf41af3SPoul-Henning Kamp 			if (timevalcmp(&aitv.it_value, &ctv, <))
4614cf41af3SPoul-Henning Kamp 				timevalclear(&aitv.it_value);
462df8bae1dSRodney W. Grimes 			else
463227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
464227ee8a1SPoul-Henning Kamp 		}
465fb99ab88SMatthew Dillon 	} else {
46696d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
467df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
46896d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
469fb99ab88SMatthew Dillon 	}
470411c25edSTim J. Robbins 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
471df8bae1dSRodney W. Grimes }
472df8bae1dSRodney W. Grimes 
473d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
474df8bae1dSRodney W. Grimes struct setitimer_args {
475df8bae1dSRodney W. Grimes 	u_int	which;
476df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
477df8bae1dSRodney W. Grimes };
478d2d3e875SBruce Evans #endif
479fb99ab88SMatthew Dillon /*
480fb99ab88SMatthew Dillon  * MPSAFE
481fb99ab88SMatthew Dillon  */
48226f9a767SRodney W. Grimes int
48391afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
484df8bae1dSRodney W. Grimes {
485b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
48696d7f8efSTim J. Robbins 	struct itimerval aitv, oitv;
487227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
48896d7f8efSTim J. Robbins 	int error;
48996d7f8efSTim J. Robbins 
49096d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
49196d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
49296d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
49396d7f8efSTim J. Robbins 	}
494df8bae1dSRodney W. Grimes 
495df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
496df8bae1dSRodney W. Grimes 		return (EINVAL);
49796d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
498df8bae1dSRodney W. Grimes 		return (error);
49996d7f8efSTim J. Robbins 	if (itimerfix(&aitv.it_value))
50096d7f8efSTim J. Robbins 		return (EINVAL);
50196d7f8efSTim J. Robbins 	if (!timevalisset(&aitv.it_value))
5024cf41af3SPoul-Henning Kamp 		timevalclear(&aitv.it_interval);
50396d7f8efSTim J. Robbins 	else if (itimerfix(&aitv.it_interval))
50496d7f8efSTim J. Robbins 		return (EINVAL);
50596d7f8efSTim J. Robbins 
506df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
50796d7f8efSTim J. Robbins 		PROC_LOCK(p);
5084cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
5094f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
51025b4d3a8SJohn Baldwin 		getmicrouptime(&ctv);
51125b4d3a8SJohn Baldwin 		if (timevalisset(&aitv.it_value)) {
5124f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
5134f559836SJake Burkholder 			    realitexpire, p);
514bfe6c9faSPoul-Henning Kamp 			timevaladd(&aitv.it_value, &ctv);
51525b4d3a8SJohn Baldwin 		}
51696d7f8efSTim J. Robbins 		oitv = p->p_realtimer;
517df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
51896d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
51996d7f8efSTim J. Robbins 		if (timevalisset(&oitv.it_value)) {
52096d7f8efSTim J. Robbins 			if (timevalcmp(&oitv.it_value, &ctv, <))
52196d7f8efSTim J. Robbins 				timevalclear(&oitv.it_value);
52296d7f8efSTim J. Robbins 			else
52396d7f8efSTim J. Robbins 				timevalsub(&oitv.it_value, &ctv);
524fb99ab88SMatthew Dillon 		}
52596d7f8efSTim J. Robbins 	} else {
52696d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
52796d7f8efSTim J. Robbins 		oitv = p->p_stats->p_timer[uap->which];
52896d7f8efSTim J. Robbins 		p->p_stats->p_timer[uap->which] = aitv;
52996d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
53096d7f8efSTim J. Robbins 	}
53196d7f8efSTim J. Robbins 	if (uap->oitv == NULL)
53296d7f8efSTim J. Robbins 		return (0);
53396d7f8efSTim J. Robbins 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
534df8bae1dSRodney W. Grimes }
535df8bae1dSRodney W. Grimes 
536df8bae1dSRodney W. Grimes /*
537df8bae1dSRodney W. Grimes  * Real interval timer expired:
538df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
539df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
540df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
541df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
542df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
543c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
5449207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
5459207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
5469207f00aSBruce Evans  * interrupt even when we're delayed.
547df8bae1dSRodney W. Grimes  */
548df8bae1dSRodney W. Grimes void
54991afe087SPoul-Henning Kamp realitexpire(void *arg)
550df8bae1dSRodney W. Grimes {
55191afe087SPoul-Henning Kamp 	struct proc *p;
552bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
553df8bae1dSRodney W. Grimes 
554df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
55537824023SJohn Baldwin 	PROC_LOCK(p);
556df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
5574cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
5584cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
5595499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
5605499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
56137824023SJohn Baldwin 		PROC_UNLOCK(p);
562df8bae1dSRodney W. Grimes 		return;
563df8bae1dSRodney W. Grimes 	}
564df8bae1dSRodney W. Grimes 	for (;;) {
565df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
566df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
567c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
5684cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
569bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
570bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
5714f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
5724f559836SJake Burkholder 			    realitexpire, p);
57337824023SJohn Baldwin 			PROC_UNLOCK(p);
574df8bae1dSRodney W. Grimes 			return;
575df8bae1dSRodney W. Grimes 		}
576df8bae1dSRodney W. Grimes 	}
57737824023SJohn Baldwin 	/*NOTREACHED*/
578df8bae1dSRodney W. Grimes }
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes /*
581df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
582df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
583df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
584df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
585df8bae1dSRodney W. Grimes  */
58626f9a767SRodney W. Grimes int
58791afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
588df8bae1dSRodney W. Grimes {
589df8bae1dSRodney W. Grimes 
590df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
591df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
592df8bae1dSRodney W. Grimes 		return (EINVAL);
593df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
594df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
595df8bae1dSRodney W. Grimes 	return (0);
596df8bae1dSRodney W. Grimes }
597df8bae1dSRodney W. Grimes 
598df8bae1dSRodney W. Grimes /*
599df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
600df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
601df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
602df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
603df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
604df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
605df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
606df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
607df8bae1dSRodney W. Grimes  */
60826f9a767SRodney W. Grimes int
60991afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
610df8bae1dSRodney W. Grimes {
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
613df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
614df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
615df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
616df8bae1dSRodney W. Grimes 			goto expire;
617df8bae1dSRodney W. Grimes 		}
618df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
619df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
620df8bae1dSRodney W. Grimes 	}
621df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
622df8bae1dSRodney W. Grimes 	usec = 0;
6234cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
624df8bae1dSRodney W. Grimes 		return (1);
625df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
626df8bae1dSRodney W. Grimes expire:
6274cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
628df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
629df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
630df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
631df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
632df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
633df8bae1dSRodney W. Grimes 		}
634df8bae1dSRodney W. Grimes 	} else
635df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
636df8bae1dSRodney W. Grimes 	return (0);
637df8bae1dSRodney W. Grimes }
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes /*
640df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
641df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
642df8bae1dSRodney W. Grimes  * results which are before the beginning,
643df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
644df8bae1dSRodney W. Grimes  * Caveat emptor.
645df8bae1dSRodney W. Grimes  */
64626f9a767SRodney W. Grimes void
6476ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
648df8bae1dSRodney W. Grimes {
649df8bae1dSRodney W. Grimes 
650df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
651df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
652df8bae1dSRodney W. Grimes 	timevalfix(t1);
653df8bae1dSRodney W. Grimes }
654df8bae1dSRodney W. Grimes 
65526f9a767SRodney W. Grimes void
6566ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
657df8bae1dSRodney W. Grimes {
658df8bae1dSRodney W. Grimes 
659df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
660df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
661df8bae1dSRodney W. Grimes 	timevalfix(t1);
662df8bae1dSRodney W. Grimes }
663df8bae1dSRodney W. Grimes 
66487b6de2bSPoul-Henning Kamp static void
66591afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
666df8bae1dSRodney W. Grimes {
667df8bae1dSRodney W. Grimes 
668df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
669df8bae1dSRodney W. Grimes 		t1->tv_sec--;
670df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
671df8bae1dSRodney W. Grimes 	}
672df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
673df8bae1dSRodney W. Grimes 		t1->tv_sec++;
674df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
675df8bae1dSRodney W. Grimes 	}
676df8bae1dSRodney W. Grimes }
67791974ce1SSam Leffler 
67891974ce1SSam Leffler /*
679addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
68091974ce1SSam Leffler  */
68191974ce1SSam Leffler int
68291974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
68391974ce1SSam Leffler {
68491974ce1SSam Leffler 	struct timeval tv, delta;
68591974ce1SSam Leffler 	int rv = 0;
68691974ce1SSam Leffler 
687addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
688addea9d4SSam Leffler 	delta = tv;
689addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
69091974ce1SSam Leffler 
69191974ce1SSam Leffler 	/*
69291974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
69391974ce1SSam Leffler 	 * even if interval is huge.
69491974ce1SSam Leffler 	 */
69591974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
69691974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
69791974ce1SSam Leffler 		*lasttime = tv;
69891974ce1SSam Leffler 		rv = 1;
69991974ce1SSam Leffler 	}
70091974ce1SSam Leffler 
70191974ce1SSam Leffler 	return (rv);
70291974ce1SSam Leffler }
70391974ce1SSam Leffler 
70491974ce1SSam Leffler /*
70591974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
706addea9d4SSam Leffler  *
707addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
708addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
709addea9d4SSam Leffler  *
710893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
711893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
712893bec80SSam Leffler  * limiting.
713893bec80SSam Leffler  *
714addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
715addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
716addea9d4SSam Leffler  * clock ticks for minimal overhead.
71791974ce1SSam Leffler  */
71891974ce1SSam Leffler int
71991974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
72091974ce1SSam Leffler {
721addea9d4SSam Leffler 	int now;
72291974ce1SSam Leffler 
72391974ce1SSam Leffler 	/*
724addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
725addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
726addea9d4SSam Leffler 	 * lasttime.
72791974ce1SSam Leffler 	 */
728addea9d4SSam Leffler 	now = ticks;
729addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
730addea9d4SSam Leffler 		lasttime->tv_sec = now;
731addea9d4SSam Leffler 		*curpps = 1;
732893bec80SSam Leffler 		return (maxpps != 0);
733addea9d4SSam Leffler 	} else {
734addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
735addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
736addea9d4SSam Leffler 	}
73791974ce1SSam Leffler }
738