xref: /freebsd/sys/kern/kern_time.c (revision b8817154c3132e53b68c50b26d7672572f39b5bc)
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;
159b8817154SKelly Yancey 	struct timeval user, sys;
16094c8fcd8SPeter Wemm 
161b8817154SKelly Yancey 	switch (uap->clock_id) {
162b8817154SKelly Yancey 	case CLOCK_REALTIME:
1637ec73f64SPoul-Henning Kamp 		nanotime(&ats);
164b8817154SKelly Yancey 		break;
165b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
1668f5ef1a9SPoul-Henning Kamp 		nanouptime(&ats);
167b8817154SKelly Yancey 		break;
168b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
169b8817154SKelly Yancey 		calcru(td->td_proc, &user, &sys, NULL);
170b8817154SKelly Yancey 		TIMEVAL_TO_TIMESPEC(&user, &ats);
171b8817154SKelly Yancey 		break;
172b8817154SKelly Yancey 	case CLOCK_PROF:
173b8817154SKelly Yancey 		calcru(td->td_proc, &user, &sys, NULL);
174b8817154SKelly Yancey 		ats.tv_sec = user.tv_sec + sys.tv_sec;
175b8817154SKelly Yancey 		ats.tv_nsec = (user.tv_usec + sys.tv_usec) * 1000;
176b8817154SKelly Yancey 		if (ats.tv_nsec > 1000000000) {
177b8817154SKelly Yancey 			ats.tv_sec++;
178b8817154SKelly Yancey 			ats.tv_nsec -= 1000000000;
179b8817154SKelly Yancey 		}
180b8817154SKelly Yancey 		break;
181b8817154SKelly Yancey 	default:
1825cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
183b8817154SKelly Yancey 	}
184d1e405c5SAlfred Perlstein 	return (copyout(&ats, uap->tp, sizeof(ats)));
18594c8fcd8SPeter Wemm }
18694c8fcd8SPeter Wemm 
18794c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
18894c8fcd8SPeter Wemm struct clock_settime_args {
18994c8fcd8SPeter Wemm 	clockid_t clock_id;
19094c8fcd8SPeter Wemm 	const struct	timespec *tp;
19194c8fcd8SPeter Wemm };
19294c8fcd8SPeter Wemm #endif
193708e7684SPeter Wemm 
194fb99ab88SMatthew Dillon /*
195fb99ab88SMatthew Dillon  * MPSAFE
196fb99ab88SMatthew Dillon  */
19794c8fcd8SPeter Wemm /* ARGSUSED */
19894c8fcd8SPeter Wemm int
19991afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
20094c8fcd8SPeter Wemm {
20194c8fcd8SPeter Wemm 	struct timeval atv;
20294c8fcd8SPeter Wemm 	struct timespec ats;
20394c8fcd8SPeter Wemm 	int error;
20494c8fcd8SPeter Wemm 
2054b8d5f2dSRobert Watson #ifdef MAC
2064b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
2074b8d5f2dSRobert Watson 	if (error)
2084b8d5f2dSRobert Watson 		return (error);
2094b8d5f2dSRobert Watson #endif
21044731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
2117edfb592SJohn Baldwin 		return (error);
212d1e405c5SAlfred Perlstein 	if (uap->clock_id != CLOCK_REALTIME)
2137edfb592SJohn Baldwin 		return (EINVAL);
214d1e405c5SAlfred Perlstein 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
2157edfb592SJohn Baldwin 		return (error);
2167edfb592SJohn Baldwin 	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
2177edfb592SJohn Baldwin 		return (EINVAL);
218a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
21994c8fcd8SPeter Wemm 	TIMESPEC_TO_TIMEVAL(&atv, &ats);
2207edfb592SJohn Baldwin 	error = settime(td, &atv);
22194c8fcd8SPeter Wemm 	return (error);
22294c8fcd8SPeter Wemm }
22394c8fcd8SPeter Wemm 
22494c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
22594c8fcd8SPeter Wemm struct clock_getres_args {
22694c8fcd8SPeter Wemm 	clockid_t clock_id;
22794c8fcd8SPeter Wemm 	struct	timespec *tp;
22894c8fcd8SPeter Wemm };
22994c8fcd8SPeter Wemm #endif
230708e7684SPeter Wemm 
23194c8fcd8SPeter Wemm int
23291afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
23394c8fcd8SPeter Wemm {
23494c8fcd8SPeter Wemm 	struct timespec ts;
235708e7684SPeter Wemm 	int error;
23694c8fcd8SPeter Wemm 
23794c8fcd8SPeter Wemm 	ts.tv_sec = 0;
238b8817154SKelly Yancey 
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 	}
257b8817154SKelly Yancey 
258b8817154SKelly Yancey 	error = 0;
259b8817154SKelly Yancey 	if (uap->tp)
260b8817154SKelly Yancey 		error = copyout(&ts, uap->tp, sizeof(ts));
261b8817154SKelly Yancey 
262708e7684SPeter Wemm 	return (error);
26394c8fcd8SPeter Wemm }
26494c8fcd8SPeter Wemm 
26594c8fcd8SPeter Wemm static int nanowait;
26694c8fcd8SPeter Wemm 
2675b870b7bSPeter Wemm static int
26891afe087SPoul-Henning Kamp nanosleep1(struct thread *td, struct timespec *rqt, struct timespec *rmt)
2695b870b7bSPeter Wemm {
2705704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
27133841826SPoul-Henning Kamp 	struct timeval tv;
27233841826SPoul-Henning Kamp 	int error;
2735b870b7bSPeter Wemm 
2747d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
275708e7684SPeter Wemm 		return (EINVAL);
276d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
2777d7fb492SBruce Evans 		return (0);
278c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
27900af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
28033841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
28133841826SPoul-Henning Kamp 	for (;;) {
28233841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
28333841826SPoul-Henning Kamp 		    tvtohz(&tv));
284c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
28533841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
28633841826SPoul-Henning Kamp 			if (error == ERESTART)
28794c8fcd8SPeter Wemm 				error = EINTR;
28833841826SPoul-Henning Kamp 			if (rmt != NULL) {
28933841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
29033841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
29133841826SPoul-Henning Kamp 					timespecclear(&ts);
29200af9731SPoul-Henning Kamp 				*rmt = ts;
29300af9731SPoul-Henning Kamp 			}
29433841826SPoul-Henning Kamp 			return (error);
29533841826SPoul-Henning Kamp 		}
29633841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
29733841826SPoul-Henning Kamp 			return (0);
2985704ba6aSPoul-Henning Kamp 		ts3 = ts;
2995704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
3005704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
30133841826SPoul-Henning Kamp 	}
3025b870b7bSPeter Wemm }
30394c8fcd8SPeter Wemm 
3045b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
3055b870b7bSPeter Wemm struct nanosleep_args {
3065b870b7bSPeter Wemm 	struct	timespec *rqtp;
3075b870b7bSPeter Wemm 	struct	timespec *rmtp;
3085b870b7bSPeter Wemm };
3095b870b7bSPeter Wemm #endif
3105b870b7bSPeter Wemm 
311fb99ab88SMatthew Dillon /*
312fb99ab88SMatthew Dillon  * MPSAFE
313fb99ab88SMatthew Dillon  */
3145b870b7bSPeter Wemm /* ARGSUSED */
3155b870b7bSPeter Wemm int
31691afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
3175b870b7bSPeter Wemm {
3185b870b7bSPeter Wemm 	struct timespec rmt, rqt;
319fb99ab88SMatthew Dillon 	int error;
3205b870b7bSPeter Wemm 
321d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
3225b870b7bSPeter Wemm 	if (error)
3235b870b7bSPeter Wemm 		return (error);
324fb99ab88SMatthew Dillon 
32531f3e2adSAlfred Perlstein 	if (uap->rmtp &&
32631f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
32731f3e2adSAlfred Perlstein 			return (EFAULT);
328b40ce416SJulian Elischer 	error = nanosleep1(td, &rqt, &rmt);
329d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
330fb99ab88SMatthew Dillon 		int error2;
331fb99ab88SMatthew Dillon 
332d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
33331f3e2adSAlfred Perlstein 		if (error2)
334fb99ab88SMatthew Dillon 			error = error2;
335708e7684SPeter Wemm 	}
336708e7684SPeter Wemm 	return (error);
33794c8fcd8SPeter Wemm }
33894c8fcd8SPeter Wemm 
3395b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
340df8bae1dSRodney W. Grimes struct gettimeofday_args {
341df8bae1dSRodney W. Grimes 	struct	timeval *tp;
342df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
343df8bae1dSRodney W. Grimes };
344d2d3e875SBruce Evans #endif
345fb99ab88SMatthew Dillon /*
346fb99ab88SMatthew Dillon  * MPSAFE
347fb99ab88SMatthew Dillon  */
348df8bae1dSRodney W. Grimes /* ARGSUSED */
34926f9a767SRodney W. Grimes int
35091afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
351df8bae1dSRodney W. Grimes {
352df8bae1dSRodney W. Grimes 	struct timeval atv;
353411c25edSTim J. Robbins 	struct timezone rtz;
354df8bae1dSRodney W. Grimes 	int error = 0;
355df8bae1dSRodney W. Grimes 
356df8bae1dSRodney W. Grimes 	if (uap->tp) {
357df8bae1dSRodney W. Grimes 		microtime(&atv);
35801609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
359df8bae1dSRodney W. Grimes 	}
36021dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
36191f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
36291f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
363411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
36421dcdb38SPoul-Henning Kamp 	}
365df8bae1dSRodney W. Grimes 	return (error);
366df8bae1dSRodney W. Grimes }
367df8bae1dSRodney W. Grimes 
368d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
369df8bae1dSRodney W. Grimes struct settimeofday_args {
370df8bae1dSRodney W. Grimes 	struct	timeval *tv;
371df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
372df8bae1dSRodney W. Grimes };
373d2d3e875SBruce Evans #endif
374fb99ab88SMatthew Dillon /*
375fb99ab88SMatthew Dillon  * MPSAFE
376fb99ab88SMatthew Dillon  */
377df8bae1dSRodney W. Grimes /* ARGSUSED */
37826f9a767SRodney W. Grimes int
37991afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
380df8bae1dSRodney W. Grimes {
381708e7684SPeter Wemm 	struct timeval atv;
382df8bae1dSRodney W. Grimes 	struct timezone atz;
383fb99ab88SMatthew Dillon 	int error = 0;
384fb99ab88SMatthew Dillon 
3854b8d5f2dSRobert Watson #ifdef MAC
3864b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
3874b8d5f2dSRobert Watson 	if (error)
3884b8d5f2dSRobert Watson 		return (error);
3894b8d5f2dSRobert Watson #endif
39044731cabSJohn Baldwin 	if ((error = suser(td)))
3917edfb592SJohn Baldwin 		return (error);
392df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
393708e7684SPeter Wemm 	if (uap->tv) {
39401609114SAlfred Perlstein 		if ((error = copyin(uap->tv, &atv, sizeof(atv))))
3957edfb592SJohn Baldwin 			return (error);
3967edfb592SJohn Baldwin 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
3977edfb592SJohn Baldwin 			return (EINVAL);
398708e7684SPeter Wemm 	}
399df8bae1dSRodney W. Grimes 	if (uap->tzp &&
40001609114SAlfred Perlstein 	    (error = copyin(uap->tzp, &atz, sizeof(atz))))
4017edfb592SJohn Baldwin 		return (error);
4027edfb592SJohn Baldwin 
4037edfb592SJohn Baldwin 	if (uap->tv && (error = settime(td, &atv)))
4047edfb592SJohn Baldwin 		return (error);
4057edfb592SJohn Baldwin 	if (uap->tzp) {
40691f1c2b3SPoul-Henning Kamp 		tz_minuteswest = atz.tz_minuteswest;
40791f1c2b3SPoul-Henning Kamp 		tz_dsttime = atz.tz_dsttime;
4087edfb592SJohn Baldwin 	}
409fb99ab88SMatthew Dillon 	return (error);
410df8bae1dSRodney W. Grimes }
411df8bae1dSRodney W. Grimes /*
412df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
413df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
414df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
415df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
416df8bae1dSRodney W. Grimes  *
417df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
418df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
419df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
420df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
421df8bae1dSRodney W. Grimes  *
422df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
423df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
424df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
425df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
426df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
427df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
428df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
429df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
430df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
431df8bae1dSRodney W. Grimes  */
432d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
433df8bae1dSRodney W. Grimes struct getitimer_args {
434df8bae1dSRodney W. Grimes 	u_int	which;
435df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
436df8bae1dSRodney W. Grimes };
437d2d3e875SBruce Evans #endif
438fb99ab88SMatthew Dillon /*
439fb99ab88SMatthew Dillon  * MPSAFE
440fb99ab88SMatthew Dillon  */
44126f9a767SRodney W. Grimes int
44291afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
443df8bae1dSRodney W. Grimes {
444b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
445227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
446df8bae1dSRodney W. Grimes 	struct itimerval aitv;
447df8bae1dSRodney W. Grimes 
448df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
449df8bae1dSRodney W. Grimes 		return (EINVAL);
450fb99ab88SMatthew Dillon 
451df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
452df8bae1dSRodney W. Grimes 		/*
453ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
454df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
455df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
456df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
457df8bae1dSRodney W. Grimes 		 */
45896d7f8efSTim J. Robbins 		PROC_LOCK(p);
459df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
46096d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
4614cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value)) {
462c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
4634cf41af3SPoul-Henning Kamp 			if (timevalcmp(&aitv.it_value, &ctv, <))
4644cf41af3SPoul-Henning Kamp 				timevalclear(&aitv.it_value);
465df8bae1dSRodney W. Grimes 			else
466227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
467227ee8a1SPoul-Henning Kamp 		}
468fb99ab88SMatthew Dillon 	} else {
46996d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
470df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
47196d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
472fb99ab88SMatthew Dillon 	}
473411c25edSTim J. Robbins 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
474df8bae1dSRodney W. Grimes }
475df8bae1dSRodney W. Grimes 
476d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
477df8bae1dSRodney W. Grimes struct setitimer_args {
478df8bae1dSRodney W. Grimes 	u_int	which;
479df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
480df8bae1dSRodney W. Grimes };
481d2d3e875SBruce Evans #endif
482fb99ab88SMatthew Dillon /*
483fb99ab88SMatthew Dillon  * MPSAFE
484fb99ab88SMatthew Dillon  */
48526f9a767SRodney W. Grimes int
48691afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
487df8bae1dSRodney W. Grimes {
488b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
48996d7f8efSTim J. Robbins 	struct itimerval aitv, oitv;
490227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
49196d7f8efSTim J. Robbins 	int error;
49296d7f8efSTim J. Robbins 
49396d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
49496d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
49596d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
49696d7f8efSTim J. Robbins 	}
497df8bae1dSRodney W. Grimes 
498df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
499df8bae1dSRodney W. Grimes 		return (EINVAL);
50096d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
501df8bae1dSRodney W. Grimes 		return (error);
50296d7f8efSTim J. Robbins 	if (itimerfix(&aitv.it_value))
50396d7f8efSTim J. Robbins 		return (EINVAL);
50496d7f8efSTim J. Robbins 	if (!timevalisset(&aitv.it_value))
5054cf41af3SPoul-Henning Kamp 		timevalclear(&aitv.it_interval);
50696d7f8efSTim J. Robbins 	else if (itimerfix(&aitv.it_interval))
50796d7f8efSTim J. Robbins 		return (EINVAL);
50896d7f8efSTim J. Robbins 
509df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
51096d7f8efSTim J. Robbins 		PROC_LOCK(p);
5114cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
5124f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
51325b4d3a8SJohn Baldwin 		getmicrouptime(&ctv);
51425b4d3a8SJohn Baldwin 		if (timevalisset(&aitv.it_value)) {
5154f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
5164f559836SJake Burkholder 			    realitexpire, p);
517bfe6c9faSPoul-Henning Kamp 			timevaladd(&aitv.it_value, &ctv);
51825b4d3a8SJohn Baldwin 		}
51996d7f8efSTim J. Robbins 		oitv = p->p_realtimer;
520df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
52196d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
52296d7f8efSTim J. Robbins 		if (timevalisset(&oitv.it_value)) {
52396d7f8efSTim J. Robbins 			if (timevalcmp(&oitv.it_value, &ctv, <))
52496d7f8efSTim J. Robbins 				timevalclear(&oitv.it_value);
52596d7f8efSTim J. Robbins 			else
52696d7f8efSTim J. Robbins 				timevalsub(&oitv.it_value, &ctv);
527fb99ab88SMatthew Dillon 		}
52896d7f8efSTim J. Robbins 	} else {
52996d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
53096d7f8efSTim J. Robbins 		oitv = p->p_stats->p_timer[uap->which];
53196d7f8efSTim J. Robbins 		p->p_stats->p_timer[uap->which] = aitv;
53296d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
53396d7f8efSTim J. Robbins 	}
53496d7f8efSTim J. Robbins 	if (uap->oitv == NULL)
53596d7f8efSTim J. Robbins 		return (0);
53696d7f8efSTim J. Robbins 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
537df8bae1dSRodney W. Grimes }
538df8bae1dSRodney W. Grimes 
539df8bae1dSRodney W. Grimes /*
540df8bae1dSRodney W. Grimes  * Real interval timer expired:
541df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
542df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
543df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
544df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
545df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
546c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
5479207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
5489207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
5499207f00aSBruce Evans  * interrupt even when we're delayed.
550df8bae1dSRodney W. Grimes  */
551df8bae1dSRodney W. Grimes void
55291afe087SPoul-Henning Kamp realitexpire(void *arg)
553df8bae1dSRodney W. Grimes {
55491afe087SPoul-Henning Kamp 	struct proc *p;
555bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
55837824023SJohn Baldwin 	PROC_LOCK(p);
559df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
5604cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
5614cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
5625499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
5635499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
56437824023SJohn Baldwin 		PROC_UNLOCK(p);
565df8bae1dSRodney W. Grimes 		return;
566df8bae1dSRodney W. Grimes 	}
567df8bae1dSRodney W. Grimes 	for (;;) {
568df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
569df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
570c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
5714cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
572bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
573bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
5744f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
5754f559836SJake Burkholder 			    realitexpire, p);
57637824023SJohn Baldwin 			PROC_UNLOCK(p);
577df8bae1dSRodney W. Grimes 			return;
578df8bae1dSRodney W. Grimes 		}
579df8bae1dSRodney W. Grimes 	}
58037824023SJohn Baldwin 	/*NOTREACHED*/
581df8bae1dSRodney W. Grimes }
582df8bae1dSRodney W. Grimes 
583df8bae1dSRodney W. Grimes /*
584df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
585df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
586df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
587df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
588df8bae1dSRodney W. Grimes  */
58926f9a767SRodney W. Grimes int
59091afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
591df8bae1dSRodney W. Grimes {
592df8bae1dSRodney W. Grimes 
593df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
594df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
595df8bae1dSRodney W. Grimes 		return (EINVAL);
596df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
597df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
598df8bae1dSRodney W. Grimes 	return (0);
599df8bae1dSRodney W. Grimes }
600df8bae1dSRodney W. Grimes 
601df8bae1dSRodney W. Grimes /*
602df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
603df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
604df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
605df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
606df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
607df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
608df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
609df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
610df8bae1dSRodney W. Grimes  */
61126f9a767SRodney W. Grimes int
61291afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
613df8bae1dSRodney W. Grimes {
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
616df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
617df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
618df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
619df8bae1dSRodney W. Grimes 			goto expire;
620df8bae1dSRodney W. Grimes 		}
621df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
622df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
623df8bae1dSRodney W. Grimes 	}
624df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
625df8bae1dSRodney W. Grimes 	usec = 0;
6264cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
627df8bae1dSRodney W. Grimes 		return (1);
628df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
629df8bae1dSRodney W. Grimes expire:
6304cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
631df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
632df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
633df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
634df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
635df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
636df8bae1dSRodney W. Grimes 		}
637df8bae1dSRodney W. Grimes 	} else
638df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
639df8bae1dSRodney W. Grimes 	return (0);
640df8bae1dSRodney W. Grimes }
641df8bae1dSRodney W. Grimes 
642df8bae1dSRodney W. Grimes /*
643df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
644df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
645df8bae1dSRodney W. Grimes  * results which are before the beginning,
646df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
647df8bae1dSRodney W. Grimes  * Caveat emptor.
648df8bae1dSRodney W. Grimes  */
64926f9a767SRodney W. Grimes void
6506ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
651df8bae1dSRodney W. Grimes {
652df8bae1dSRodney W. Grimes 
653df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
654df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
655df8bae1dSRodney W. Grimes 	timevalfix(t1);
656df8bae1dSRodney W. Grimes }
657df8bae1dSRodney W. Grimes 
65826f9a767SRodney W. Grimes void
6596ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
660df8bae1dSRodney W. Grimes {
661df8bae1dSRodney W. Grimes 
662df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
663df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
664df8bae1dSRodney W. Grimes 	timevalfix(t1);
665df8bae1dSRodney W. Grimes }
666df8bae1dSRodney W. Grimes 
66787b6de2bSPoul-Henning Kamp static void
66891afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
669df8bae1dSRodney W. Grimes {
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
672df8bae1dSRodney W. Grimes 		t1->tv_sec--;
673df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
674df8bae1dSRodney W. Grimes 	}
675df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
676df8bae1dSRodney W. Grimes 		t1->tv_sec++;
677df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
678df8bae1dSRodney W. Grimes 	}
679df8bae1dSRodney W. Grimes }
68091974ce1SSam Leffler 
68191974ce1SSam Leffler /*
682addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
68391974ce1SSam Leffler  */
68491974ce1SSam Leffler int
68591974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
68691974ce1SSam Leffler {
68791974ce1SSam Leffler 	struct timeval tv, delta;
68891974ce1SSam Leffler 	int rv = 0;
68991974ce1SSam Leffler 
690addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
691addea9d4SSam Leffler 	delta = tv;
692addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
69391974ce1SSam Leffler 
69491974ce1SSam Leffler 	/*
69591974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
69691974ce1SSam Leffler 	 * even if interval is huge.
69791974ce1SSam Leffler 	 */
69891974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
69991974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
70091974ce1SSam Leffler 		*lasttime = tv;
70191974ce1SSam Leffler 		rv = 1;
70291974ce1SSam Leffler 	}
70391974ce1SSam Leffler 
70491974ce1SSam Leffler 	return (rv);
70591974ce1SSam Leffler }
70691974ce1SSam Leffler 
70791974ce1SSam Leffler /*
70891974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
709addea9d4SSam Leffler  *
710addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
711addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
712addea9d4SSam Leffler  *
713893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
714893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
715893bec80SSam Leffler  * limiting.
716893bec80SSam Leffler  *
717addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
718addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
719addea9d4SSam Leffler  * clock ticks for minimal overhead.
72091974ce1SSam Leffler  */
72191974ce1SSam Leffler int
72291974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
72391974ce1SSam Leffler {
724addea9d4SSam Leffler 	int now;
72591974ce1SSam Leffler 
72691974ce1SSam Leffler 	/*
727addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
728addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
729addea9d4SSam Leffler 	 * lasttime.
73091974ce1SSam Leffler 	 */
731addea9d4SSam Leffler 	now = ticks;
732addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
733addea9d4SSam Leffler 		lasttime->tv_sec = now;
734addea9d4SSam Leffler 		*curpps = 1;
735893bec80SSam Leffler 		return (maxpps != 0);
736addea9d4SSam Leffler 	} else {
737addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
738addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
739addea9d4SSam Leffler 	}
74091974ce1SSam Leffler }
741