xref: /freebsd/sys/kern/kern_time.c (revision 7fdf2c856f3f34fcf25ff3d85cf0f9462d0ff1f0)
19454b2d8SWarner Losh /*-
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 
687edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
694d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
704d77a549SAlfred Perlstein static void	no_lease_updatetime(int);
7194c8fcd8SPeter Wemm 
721b09ae77SPoul-Henning Kamp static void
731b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
741b09ae77SPoul-Henning Kamp 	int deltat;
751b09ae77SPoul-Henning Kamp {
761b09ae77SPoul-Henning Kamp }
771b09ae77SPoul-Henning Kamp 
784d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
791b09ae77SPoul-Henning Kamp 
8094c8fcd8SPeter Wemm static int
8191afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
8294c8fcd8SPeter Wemm {
83fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
84c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
857ec73f64SPoul-Henning Kamp 	struct timespec ts;
8694c8fcd8SPeter Wemm 	int s;
8794c8fcd8SPeter Wemm 
88708e7684SPeter Wemm 	s = splclock();
899c8fff87SBruce Evans 	microtime(&tv1);
9000af9731SPoul-Henning Kamp 	delta = *tv;
9100af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
9294c8fcd8SPeter Wemm 
9394c8fcd8SPeter Wemm 	/*
949c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
95fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
96fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
97fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
98fcae3aa6SNick Sayer 	 * back to the past.
99c0bd94a7SNick Sayer 	 *
100c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
101c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
102c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
10394c8fcd8SPeter Wemm 	 */
1047edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
105fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1063f92429aSMatt Jacob 			/*
107c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1083f92429aSMatt Jacob 			 */
109fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
110fcae3aa6SNick Sayer 				maxtime = tv1;
111fcae3aa6SNick Sayer 			tv2 = *tv;
112fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
113fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1143f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
115fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
116fcae3aa6SNick Sayer 			}
1173f92429aSMatt Jacob 		} else {
118c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
119c0bd94a7SNick Sayer 				splx(s);
120c0bd94a7SNick Sayer 				return (EPERM);
121c0bd94a7SNick Sayer 			}
122c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
123c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
124c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
125c0bd94a7SNick Sayer 			}
126c0bd94a7SNick Sayer 			laststep = *tv;
127fcae3aa6SNick Sayer 		}
1289c8fff87SBruce Evans 	}
1299c8fff87SBruce Evans 
1307ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1317ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1327edfb592SJohn Baldwin 	mtx_lock(&Giant);
13391266b96SPoul-Henning Kamp 	tc_setclock(&ts);
13494c8fcd8SPeter Wemm 	(void) splsoftclock();
13594c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
13694c8fcd8SPeter Wemm 	splx(s);
13794c8fcd8SPeter Wemm 	resettodr();
1387edfb592SJohn Baldwin 	mtx_unlock(&Giant);
13994c8fcd8SPeter Wemm 	return (0);
14094c8fcd8SPeter Wemm }
14194c8fcd8SPeter Wemm 
14294c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
14394c8fcd8SPeter Wemm struct clock_gettime_args {
14494c8fcd8SPeter Wemm 	clockid_t clock_id;
14594c8fcd8SPeter Wemm 	struct	timespec *tp;
14694c8fcd8SPeter Wemm };
14794c8fcd8SPeter Wemm #endif
148708e7684SPeter Wemm 
149fb99ab88SMatthew Dillon /*
150fb99ab88SMatthew Dillon  * MPSAFE
151fb99ab88SMatthew Dillon  */
15294c8fcd8SPeter Wemm /* ARGSUSED */
15394c8fcd8SPeter Wemm int
15491afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
15594c8fcd8SPeter Wemm {
15694c8fcd8SPeter Wemm 	struct timespec ats;
157de0a9241SKelly Yancey 	struct timeval sys, user;
15878c85e8dSJohn Baldwin 	struct proc *p;
15994c8fcd8SPeter Wemm 
16078c85e8dSJohn Baldwin 	p = td->td_proc;
161b8817154SKelly Yancey 	switch (uap->clock_id) {
162b8817154SKelly Yancey 	case CLOCK_REALTIME:
1637ec73f64SPoul-Henning Kamp 		nanotime(&ats);
164b8817154SKelly Yancey 		break;
165b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
16678c85e8dSJohn Baldwin 		PROC_LOCK(p);
16778c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
16878c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
169b8817154SKelly Yancey 		TIMEVAL_TO_TIMESPEC(&user, &ats);
170b8817154SKelly Yancey 		break;
171b8817154SKelly Yancey 	case CLOCK_PROF:
17278c85e8dSJohn Baldwin 		PROC_LOCK(p);
17378c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
17478c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
175de0a9241SKelly Yancey 		timevaladd(&user, &sys);
176de0a9241SKelly Yancey 		TIMEVAL_TO_TIMESPEC(&user, &ats);
177de0a9241SKelly Yancey 		break;
178de0a9241SKelly Yancey 	case CLOCK_MONOTONIC:
179de0a9241SKelly Yancey 		nanouptime(&ats);
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;
23594c8fcd8SPeter Wemm 
23694c8fcd8SPeter Wemm 	ts.tv_sec = 0;
237b8817154SKelly Yancey 	switch (uap->clock_id) {
238b8817154SKelly Yancey 	case CLOCK_REALTIME:
239b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
240ac0653dcSBruce Evans 		/*
241ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
242ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
243ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
244ac0653dcSBruce Evans 		 */
245ac0653dcSBruce Evans 		ts.tv_nsec = 1000000000 / tc_getfrequency() + 1;
246b8817154SKelly Yancey 		break;
247b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
248b8817154SKelly Yancey 	case CLOCK_PROF:
249b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
250b8817154SKelly Yancey 		ts.tv_nsec = (1000000000 + hz - 1) / hz;
251b8817154SKelly Yancey 		break;
252b8817154SKelly Yancey 	default:
253b8817154SKelly Yancey 		return (EINVAL);
25494c8fcd8SPeter Wemm 	}
255de0a9241SKelly Yancey 	if (uap->tp == NULL)
256de0a9241SKelly Yancey 		return (0);
257de0a9241SKelly Yancey 	return (copyout(&ts, uap->tp, sizeof(ts)));
25894c8fcd8SPeter Wemm }
25994c8fcd8SPeter Wemm 
26094c8fcd8SPeter Wemm static int nanowait;
26194c8fcd8SPeter Wemm 
2627fdf2c85SPaul Saab int
2637fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
2645b870b7bSPeter Wemm {
2655704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
26633841826SPoul-Henning Kamp 	struct timeval tv;
26733841826SPoul-Henning Kamp 	int error;
2685b870b7bSPeter Wemm 
2697d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
270708e7684SPeter Wemm 		return (EINVAL);
271d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
2727d7fb492SBruce Evans 		return (0);
273c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
27400af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
27533841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
27633841826SPoul-Henning Kamp 	for (;;) {
27733841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
27833841826SPoul-Henning Kamp 		    tvtohz(&tv));
279c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
28033841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
28133841826SPoul-Henning Kamp 			if (error == ERESTART)
28294c8fcd8SPeter Wemm 				error = EINTR;
28333841826SPoul-Henning Kamp 			if (rmt != NULL) {
28433841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
28533841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
28633841826SPoul-Henning Kamp 					timespecclear(&ts);
28700af9731SPoul-Henning Kamp 				*rmt = ts;
28800af9731SPoul-Henning Kamp 			}
28933841826SPoul-Henning Kamp 			return (error);
29033841826SPoul-Henning Kamp 		}
29133841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
29233841826SPoul-Henning Kamp 			return (0);
2935704ba6aSPoul-Henning Kamp 		ts3 = ts;
2945704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
2955704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
29633841826SPoul-Henning Kamp 	}
2975b870b7bSPeter Wemm }
29894c8fcd8SPeter Wemm 
2995b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
3005b870b7bSPeter Wemm struct nanosleep_args {
3015b870b7bSPeter Wemm 	struct	timespec *rqtp;
3025b870b7bSPeter Wemm 	struct	timespec *rmtp;
3035b870b7bSPeter Wemm };
3045b870b7bSPeter Wemm #endif
3055b870b7bSPeter Wemm 
306fb99ab88SMatthew Dillon /*
307fb99ab88SMatthew Dillon  * MPSAFE
308fb99ab88SMatthew Dillon  */
3095b870b7bSPeter Wemm /* ARGSUSED */
3105b870b7bSPeter Wemm int
31191afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
3125b870b7bSPeter Wemm {
3135b870b7bSPeter Wemm 	struct timespec rmt, rqt;
314fb99ab88SMatthew Dillon 	int error;
3155b870b7bSPeter Wemm 
316d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
3175b870b7bSPeter Wemm 	if (error)
3185b870b7bSPeter Wemm 		return (error);
319fb99ab88SMatthew Dillon 
32031f3e2adSAlfred Perlstein 	if (uap->rmtp &&
32131f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
32231f3e2adSAlfred Perlstein 			return (EFAULT);
3237fdf2c85SPaul Saab 	error = kern_nanosleep(td, &rqt, &rmt);
324d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
325fb99ab88SMatthew Dillon 		int error2;
326fb99ab88SMatthew Dillon 
327d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
32831f3e2adSAlfred Perlstein 		if (error2)
329fb99ab88SMatthew Dillon 			error = error2;
330708e7684SPeter Wemm 	}
331708e7684SPeter Wemm 	return (error);
33294c8fcd8SPeter Wemm }
33394c8fcd8SPeter Wemm 
3345b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
335df8bae1dSRodney W. Grimes struct gettimeofday_args {
336df8bae1dSRodney W. Grimes 	struct	timeval *tp;
337df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
338df8bae1dSRodney W. Grimes };
339d2d3e875SBruce Evans #endif
340fb99ab88SMatthew Dillon /*
341fb99ab88SMatthew Dillon  * MPSAFE
342fb99ab88SMatthew Dillon  */
343df8bae1dSRodney W. Grimes /* ARGSUSED */
34426f9a767SRodney W. Grimes int
34591afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
346df8bae1dSRodney W. Grimes {
347df8bae1dSRodney W. Grimes 	struct timeval atv;
348411c25edSTim J. Robbins 	struct timezone rtz;
349df8bae1dSRodney W. Grimes 	int error = 0;
350df8bae1dSRodney W. Grimes 
351df8bae1dSRodney W. Grimes 	if (uap->tp) {
352df8bae1dSRodney W. Grimes 		microtime(&atv);
35301609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
354df8bae1dSRodney W. Grimes 	}
35521dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
35691f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
35791f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
358411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
35921dcdb38SPoul-Henning Kamp 	}
360df8bae1dSRodney W. Grimes 	return (error);
361df8bae1dSRodney W. Grimes }
362df8bae1dSRodney W. Grimes 
363d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
364df8bae1dSRodney W. Grimes struct settimeofday_args {
365df8bae1dSRodney W. Grimes 	struct	timeval *tv;
366df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
367df8bae1dSRodney W. Grimes };
368d2d3e875SBruce Evans #endif
369fb99ab88SMatthew Dillon /*
370fb99ab88SMatthew Dillon  * MPSAFE
371fb99ab88SMatthew Dillon  */
372df8bae1dSRodney W. Grimes /* ARGSUSED */
37326f9a767SRodney W. Grimes int
37491afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
375df8bae1dSRodney W. Grimes {
376708e7684SPeter Wemm 	struct timeval atv;
377df8bae1dSRodney W. Grimes 	struct timezone atz;
378fb99ab88SMatthew Dillon 	int error = 0;
379fb99ab88SMatthew Dillon 
3804b8d5f2dSRobert Watson #ifdef MAC
3814b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
3824b8d5f2dSRobert Watson 	if (error)
3834b8d5f2dSRobert Watson 		return (error);
3844b8d5f2dSRobert Watson #endif
38544731cabSJohn Baldwin 	if ((error = suser(td)))
3867edfb592SJohn Baldwin 		return (error);
387df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
388708e7684SPeter Wemm 	if (uap->tv) {
38901609114SAlfred Perlstein 		if ((error = copyin(uap->tv, &atv, sizeof(atv))))
3907edfb592SJohn Baldwin 			return (error);
3917edfb592SJohn Baldwin 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
3927edfb592SJohn Baldwin 			return (EINVAL);
393708e7684SPeter Wemm 	}
394df8bae1dSRodney W. Grimes 	if (uap->tzp &&
39501609114SAlfred Perlstein 	    (error = copyin(uap->tzp, &atz, sizeof(atz))))
3967edfb592SJohn Baldwin 		return (error);
3977edfb592SJohn Baldwin 
3987edfb592SJohn Baldwin 	if (uap->tv && (error = settime(td, &atv)))
3997edfb592SJohn Baldwin 		return (error);
4007edfb592SJohn Baldwin 	if (uap->tzp) {
40191f1c2b3SPoul-Henning Kamp 		tz_minuteswest = atz.tz_minuteswest;
40291f1c2b3SPoul-Henning Kamp 		tz_dsttime = atz.tz_dsttime;
4037edfb592SJohn Baldwin 	}
404fb99ab88SMatthew Dillon 	return (error);
405df8bae1dSRodney W. Grimes }
406df8bae1dSRodney W. Grimes /*
407df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
408df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
409df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
410df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
411df8bae1dSRodney W. Grimes  *
412df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
413df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
414df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
415df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
416df8bae1dSRodney W. Grimes  *
417df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
418df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
419df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
420df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
421df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
422df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
423df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
424df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
425df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
426df8bae1dSRodney W. Grimes  */
427d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
428df8bae1dSRodney W. Grimes struct getitimer_args {
429df8bae1dSRodney W. Grimes 	u_int	which;
430df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
431df8bae1dSRodney W. Grimes };
432d2d3e875SBruce Evans #endif
433fb99ab88SMatthew Dillon /*
434fb99ab88SMatthew Dillon  * MPSAFE
435fb99ab88SMatthew Dillon  */
43626f9a767SRodney W. Grimes int
43791afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
438df8bae1dSRodney W. Grimes {
439b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
440227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
441df8bae1dSRodney W. Grimes 	struct itimerval aitv;
442df8bae1dSRodney W. Grimes 
443df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
444df8bae1dSRodney W. Grimes 		return (EINVAL);
445fb99ab88SMatthew Dillon 
446df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
447df8bae1dSRodney W. Grimes 		/*
448ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
449df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
450df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
451df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
452df8bae1dSRodney W. Grimes 		 */
45396d7f8efSTim J. Robbins 		PROC_LOCK(p);
454df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
45596d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
4564cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value)) {
457c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
4584cf41af3SPoul-Henning Kamp 			if (timevalcmp(&aitv.it_value, &ctv, <))
4594cf41af3SPoul-Henning Kamp 				timevalclear(&aitv.it_value);
460df8bae1dSRodney W. Grimes 			else
461227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
462227ee8a1SPoul-Henning Kamp 		}
463fb99ab88SMatthew Dillon 	} else {
46496d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
465df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
46696d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
467fb99ab88SMatthew Dillon 	}
468411c25edSTim J. Robbins 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
469df8bae1dSRodney W. Grimes }
470df8bae1dSRodney W. Grimes 
471d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
472df8bae1dSRodney W. Grimes struct setitimer_args {
473df8bae1dSRodney W. Grimes 	u_int	which;
474df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
475df8bae1dSRodney W. Grimes };
476d2d3e875SBruce Evans #endif
477fb99ab88SMatthew Dillon /*
478fb99ab88SMatthew Dillon  * MPSAFE
479fb99ab88SMatthew Dillon  */
48026f9a767SRodney W. Grimes int
48191afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
482df8bae1dSRodney W. Grimes {
483b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
48496d7f8efSTim J. Robbins 	struct itimerval aitv, oitv;
485227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
48696d7f8efSTim J. Robbins 	int error;
48796d7f8efSTim J. Robbins 
48896d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
48996d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
49096d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
49196d7f8efSTim J. Robbins 	}
492df8bae1dSRodney W. Grimes 
493df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
494df8bae1dSRodney W. Grimes 		return (EINVAL);
49596d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
496df8bae1dSRodney W. Grimes 		return (error);
49796d7f8efSTim J. Robbins 	if (itimerfix(&aitv.it_value))
49896d7f8efSTim J. Robbins 		return (EINVAL);
49996d7f8efSTim J. Robbins 	if (!timevalisset(&aitv.it_value))
5004cf41af3SPoul-Henning Kamp 		timevalclear(&aitv.it_interval);
50196d7f8efSTim J. Robbins 	else if (itimerfix(&aitv.it_interval))
50296d7f8efSTim J. Robbins 		return (EINVAL);
50396d7f8efSTim J. Robbins 
504df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
50596d7f8efSTim J. Robbins 		PROC_LOCK(p);
5064cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
5074f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
50825b4d3a8SJohn Baldwin 		getmicrouptime(&ctv);
50925b4d3a8SJohn Baldwin 		if (timevalisset(&aitv.it_value)) {
5104f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
5114f559836SJake Burkholder 			    realitexpire, p);
512bfe6c9faSPoul-Henning Kamp 			timevaladd(&aitv.it_value, &ctv);
51325b4d3a8SJohn Baldwin 		}
51496d7f8efSTim J. Robbins 		oitv = p->p_realtimer;
515df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
51696d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
51796d7f8efSTim J. Robbins 		if (timevalisset(&oitv.it_value)) {
51896d7f8efSTim J. Robbins 			if (timevalcmp(&oitv.it_value, &ctv, <))
51996d7f8efSTim J. Robbins 				timevalclear(&oitv.it_value);
52096d7f8efSTim J. Robbins 			else
52196d7f8efSTim J. Robbins 				timevalsub(&oitv.it_value, &ctv);
522fb99ab88SMatthew Dillon 		}
52396d7f8efSTim J. Robbins 	} else {
52496d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
52596d7f8efSTim J. Robbins 		oitv = p->p_stats->p_timer[uap->which];
52696d7f8efSTim J. Robbins 		p->p_stats->p_timer[uap->which] = aitv;
52796d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
52896d7f8efSTim J. Robbins 	}
52996d7f8efSTim J. Robbins 	if (uap->oitv == NULL)
53096d7f8efSTim J. Robbins 		return (0);
53196d7f8efSTim J. Robbins 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
532df8bae1dSRodney W. Grimes }
533df8bae1dSRodney W. Grimes 
534df8bae1dSRodney W. Grimes /*
535df8bae1dSRodney W. Grimes  * Real interval timer expired:
536df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
537df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
538df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
539df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
540df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
541c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
5429207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
5439207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
5449207f00aSBruce Evans  * interrupt even when we're delayed.
545df8bae1dSRodney W. Grimes  */
546df8bae1dSRodney W. Grimes void
54791afe087SPoul-Henning Kamp realitexpire(void *arg)
548df8bae1dSRodney W. Grimes {
54991afe087SPoul-Henning Kamp 	struct proc *p;
550bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
551df8bae1dSRodney W. Grimes 
552df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
55337824023SJohn Baldwin 	PROC_LOCK(p);
554df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
5554cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
5564cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
5575499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
5585499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
55937824023SJohn Baldwin 		PROC_UNLOCK(p);
560df8bae1dSRodney W. Grimes 		return;
561df8bae1dSRodney W. Grimes 	}
562df8bae1dSRodney W. Grimes 	for (;;) {
563df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
564df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
565c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
5664cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
567bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
568bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
5694f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
5704f559836SJake Burkholder 			    realitexpire, p);
57137824023SJohn Baldwin 			PROC_UNLOCK(p);
572df8bae1dSRodney W. Grimes 			return;
573df8bae1dSRodney W. Grimes 		}
574df8bae1dSRodney W. Grimes 	}
57537824023SJohn Baldwin 	/*NOTREACHED*/
576df8bae1dSRodney W. Grimes }
577df8bae1dSRodney W. Grimes 
578df8bae1dSRodney W. Grimes /*
579df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
580df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
581df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
582df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
583df8bae1dSRodney W. Grimes  */
58426f9a767SRodney W. Grimes int
58591afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
586df8bae1dSRodney W. Grimes {
587df8bae1dSRodney W. Grimes 
588df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
589df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
590df8bae1dSRodney W. Grimes 		return (EINVAL);
591df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
592df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
593df8bae1dSRodney W. Grimes 	return (0);
594df8bae1dSRodney W. Grimes }
595df8bae1dSRodney W. Grimes 
596df8bae1dSRodney W. Grimes /*
597df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
598df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
599df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
600df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
601df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
602df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
603df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
604df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
605df8bae1dSRodney W. Grimes  */
60626f9a767SRodney W. Grimes int
60791afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
608df8bae1dSRodney W. Grimes {
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
611df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
612df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
613df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
614df8bae1dSRodney W. Grimes 			goto expire;
615df8bae1dSRodney W. Grimes 		}
616df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
617df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
618df8bae1dSRodney W. Grimes 	}
619df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
620df8bae1dSRodney W. Grimes 	usec = 0;
6214cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
622df8bae1dSRodney W. Grimes 		return (1);
623df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
624df8bae1dSRodney W. Grimes expire:
6254cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
626df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
627df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
628df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
629df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
630df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
631df8bae1dSRodney W. Grimes 		}
632df8bae1dSRodney W. Grimes 	} else
633df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
634df8bae1dSRodney W. Grimes 	return (0);
635df8bae1dSRodney W. Grimes }
636df8bae1dSRodney W. Grimes 
637df8bae1dSRodney W. Grimes /*
638df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
639df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
640df8bae1dSRodney W. Grimes  * results which are before the beginning,
641df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
642df8bae1dSRodney W. Grimes  * Caveat emptor.
643df8bae1dSRodney W. Grimes  */
64426f9a767SRodney W. Grimes void
6456ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
646df8bae1dSRodney W. Grimes {
647df8bae1dSRodney W. Grimes 
648df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
649df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
650df8bae1dSRodney W. Grimes 	timevalfix(t1);
651df8bae1dSRodney W. Grimes }
652df8bae1dSRodney W. Grimes 
65326f9a767SRodney W. Grimes void
6546ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
655df8bae1dSRodney W. Grimes {
656df8bae1dSRodney W. Grimes 
657df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
658df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
659df8bae1dSRodney W. Grimes 	timevalfix(t1);
660df8bae1dSRodney W. Grimes }
661df8bae1dSRodney W. Grimes 
66287b6de2bSPoul-Henning Kamp static void
66391afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
664df8bae1dSRodney W. Grimes {
665df8bae1dSRodney W. Grimes 
666df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
667df8bae1dSRodney W. Grimes 		t1->tv_sec--;
668df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
669df8bae1dSRodney W. Grimes 	}
670df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
671df8bae1dSRodney W. Grimes 		t1->tv_sec++;
672df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
673df8bae1dSRodney W. Grimes 	}
674df8bae1dSRodney W. Grimes }
67591974ce1SSam Leffler 
67691974ce1SSam Leffler /*
677addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
67891974ce1SSam Leffler  */
67991974ce1SSam Leffler int
68091974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
68191974ce1SSam Leffler {
68291974ce1SSam Leffler 	struct timeval tv, delta;
68391974ce1SSam Leffler 	int rv = 0;
68491974ce1SSam Leffler 
685addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
686addea9d4SSam Leffler 	delta = tv;
687addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
68891974ce1SSam Leffler 
68991974ce1SSam Leffler 	/*
69091974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
69191974ce1SSam Leffler 	 * even if interval is huge.
69291974ce1SSam Leffler 	 */
69391974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
69491974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
69591974ce1SSam Leffler 		*lasttime = tv;
69691974ce1SSam Leffler 		rv = 1;
69791974ce1SSam Leffler 	}
69891974ce1SSam Leffler 
69991974ce1SSam Leffler 	return (rv);
70091974ce1SSam Leffler }
70191974ce1SSam Leffler 
70291974ce1SSam Leffler /*
70391974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
704addea9d4SSam Leffler  *
705addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
706addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
707addea9d4SSam Leffler  *
708893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
709893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
710893bec80SSam Leffler  * limiting.
711893bec80SSam Leffler  *
712addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
713addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
714addea9d4SSam Leffler  * clock ticks for minimal overhead.
71591974ce1SSam Leffler  */
71691974ce1SSam Leffler int
71791974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
71891974ce1SSam Leffler {
719addea9d4SSam Leffler 	int now;
72091974ce1SSam Leffler 
72191974ce1SSam Leffler 	/*
722addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
723addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
724addea9d4SSam Leffler 	 * lasttime.
72591974ce1SSam Leffler 	 */
726addea9d4SSam Leffler 	now = ticks;
727addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
728addea9d4SSam Leffler 		lasttime->tv_sec = now;
729addea9d4SSam Leffler 		*curpps = 1;
730893bec80SSam Leffler 		return (maxpps != 0);
731addea9d4SSam Leffler 	} else {
732addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
733addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
734addea9d4SSam Leffler 	}
73591974ce1SSam Leffler }
736