FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_time.c
1 /*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_mac.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/sysproto.h>
42 #include <sys/resourcevar.h>
43 #include <sys/signalvar.h>
44 #include <sys/kernel.h>
45 #include <sys/mac.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysent.h>
48 #include <sys/proc.h>
49 #include <sys/time.h>
50 #include <sys/timetc.h>
51 #include <sys/vnode.h>
52
53 #include <vm/vm.h>
54 #include <vm/vm_extern.h>
55
56 int tz_minuteswest;
57 int tz_dsttime;
58
59 /*
60 * Time of day and interval timer support.
61 *
62 * These routines provide the kernel entry points to get and set
63 * the time-of-day and per-process interval timers. Subroutines
64 * here provide support for adding and subtracting timeval structures
65 * and decrementing interval timers, optionally reloading the interval
66 * timers when they expire.
67 */
68
69 static int settime(struct thread *, struct timeval *);
70 static void timevalfix(struct timeval *);
71 static void no_lease_updatetime(int);
72
73 static void
74 no_lease_updatetime(deltat)
75 int deltat;
76 {
77 }
78
79 void (*lease_updatetime)(int) = no_lease_updatetime;
80
81 static int
82 settime(struct thread *td, struct timeval *tv)
83 {
84 struct timeval delta, tv1, tv2;
85 static struct timeval maxtime, laststep;
86 struct timespec ts;
87 int s;
88
89 s = splclock();
90 microtime(&tv1);
91 delta = *tv;
92 timevalsub(&delta, &tv1);
93
94 /*
95 * If the system is secure, we do not allow the time to be
96 * set to a value earlier than 1 second less than the highest
97 * time we have yet seen. The worst a miscreant can do in
98 * this circumstance is "freeze" time. He couldn't go
99 * back to the past.
100 *
101 * We similarly do not allow the clock to be stepped more
102 * than one second, nor more than once per second. This allows
103 * a miscreant to make the clock march double-time, but no worse.
104 */
105 if (securelevel_gt(td->td_ucred, 1) != 0) {
106 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
107 /*
108 * Update maxtime to latest time we've seen.
109 */
110 if (tv1.tv_sec > maxtime.tv_sec)
111 maxtime = tv1;
112 tv2 = *tv;
113 timevalsub(&tv2, &maxtime);
114 if (tv2.tv_sec < -1) {
115 tv->tv_sec = maxtime.tv_sec - 1;
116 printf("Time adjustment clamped to -1 second\n");
117 }
118 } else {
119 if (tv1.tv_sec == laststep.tv_sec) {
120 splx(s);
121 return (EPERM);
122 }
123 if (delta.tv_sec > 1) {
124 tv->tv_sec = tv1.tv_sec + 1;
125 printf("Time adjustment clamped to +1 second\n");
126 }
127 laststep = *tv;
128 }
129 }
130
131 ts.tv_sec = tv->tv_sec;
132 ts.tv_nsec = tv->tv_usec * 1000;
133 mtx_lock(&Giant);
134 tc_setclock(&ts);
135 (void) splsoftclock();
136 lease_updatetime(delta.tv_sec);
137 splx(s);
138 resettodr();
139 mtx_unlock(&Giant);
140 return (0);
141 }
142
143 #ifndef _SYS_SYSPROTO_H_
144 struct clock_gettime_args {
145 clockid_t clock_id;
146 struct timespec *tp;
147 };
148 #endif
149
150 /*
151 * MPSAFE
152 */
153 /* ARGSUSED */
154 int
155 clock_gettime(struct thread *td, struct clock_gettime_args *uap)
156 {
157 struct timespec ats;
158 struct timeval sys, user;
159
160 switch (uap->clock_id) {
161 case CLOCK_REALTIME:
162 nanotime(&ats);
163 break;
164 case CLOCK_VIRTUAL:
165 mtx_lock_spin(&sched_lock);
166 calcru(td->td_proc, &user, &sys, NULL);
167 mtx_unlock_spin(&sched_lock);
168 TIMEVAL_TO_TIMESPEC(&user, &ats);
169 break;
170 case CLOCK_PROF:
171 mtx_lock_spin(&sched_lock);
172 calcru(td->td_proc, &user, &sys, NULL);
173 mtx_unlock_spin(&sched_lock);
174 timevaladd(&user, &sys);
175 TIMEVAL_TO_TIMESPEC(&user, &ats);
176 break;
177 case CLOCK_MONOTONIC:
178 nanouptime(&ats);
179 break;
180 default:
181 return (EINVAL);
182 }
183 return (copyout(&ats, uap->tp, sizeof(ats)));
184 }
185
186 #ifndef _SYS_SYSPROTO_H_
187 struct clock_settime_args {
188 clockid_t clock_id;
189 const struct timespec *tp;
190 };
191 #endif
192
193 /*
194 * MPSAFE
195 */
196 /* ARGSUSED */
197 int
198 clock_settime(struct thread *td, struct clock_settime_args *uap)
199 {
200 struct timeval atv;
201 struct timespec ats;
202 int error;
203
204 #ifdef MAC
205 error = mac_check_system_settime(td->td_ucred);
206 if (error)
207 return (error);
208 #endif
209 if ((error = suser(td)) != 0)
210 return (error);
211 if (uap->clock_id != CLOCK_REALTIME)
212 return (EINVAL);
213 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
214 return (error);
215 if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
216 return (EINVAL);
217 /* XXX Don't convert nsec->usec and back */
218 TIMESPEC_TO_TIMEVAL(&atv, &ats);
219 error = settime(td, &atv);
220 return (error);
221 }
222
223 #ifndef _SYS_SYSPROTO_H_
224 struct clock_getres_args {
225 clockid_t clock_id;
226 struct timespec *tp;
227 };
228 #endif
229
230 int
231 clock_getres(struct thread *td, struct clock_getres_args *uap)
232 {
233 struct timespec ts;
234
235 ts.tv_sec = 0;
236 switch (uap->clock_id) {
237 case CLOCK_REALTIME:
238 case CLOCK_MONOTONIC:
239 /*
240 * Round up the result of the division cheaply by adding 1.
241 * Rounding up is especially important if rounding down
242 * would give 0. Perfect rounding is unimportant.
243 */
244 ts.tv_nsec = 1000000000 / tc_getfrequency() + 1;
245 break;
246 case CLOCK_VIRTUAL:
247 case CLOCK_PROF:
248 /* Accurately round up here because we can do so cheaply. */
249 ts.tv_nsec = (1000000000 + hz - 1) / hz;
250 break;
251 default:
252 return (EINVAL);
253 }
254 if (uap->tp == NULL)
255 return (0);
256 return (copyout(&ts, uap->tp, sizeof(ts)));
257 }
258
259 static int nanowait;
260
261 int
262 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
263 {
264 struct timespec ts, ts2, ts3;
265 struct timeval tv;
266 int error;
267
268 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
269 return (EINVAL);
270 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
271 return (0);
272 getnanouptime(&ts);
273 timespecadd(&ts, rqt);
274 TIMESPEC_TO_TIMEVAL(&tv, rqt);
275 for (;;) {
276 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
277 tvtohz(&tv));
278 getnanouptime(&ts2);
279 if (error != EWOULDBLOCK) {
280 if (error == ERESTART)
281 error = EINTR;
282 if (rmt != NULL) {
283 timespecsub(&ts, &ts2);
284 if (ts.tv_sec < 0)
285 timespecclear(&ts);
286 *rmt = ts;
287 }
288 return (error);
289 }
290 if (timespeccmp(&ts2, &ts, >=))
291 return (0);
292 ts3 = ts;
293 timespecsub(&ts3, &ts2);
294 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
295 }
296 }
297
298 #ifndef _SYS_SYSPROTO_H_
299 struct nanosleep_args {
300 struct timespec *rqtp;
301 struct timespec *rmtp;
302 };
303 #endif
304
305 /*
306 * MPSAFE
307 */
308 /* ARGSUSED */
309 int
310 nanosleep(struct thread *td, struct nanosleep_args *uap)
311 {
312 struct timespec rmt, rqt;
313 int error;
314
315 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
316 if (error)
317 return (error);
318
319 if (uap->rmtp &&
320 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
321 return (EFAULT);
322 error = kern_nanosleep(td, &rqt, &rmt);
323 if (error && uap->rmtp) {
324 int error2;
325
326 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
327 if (error2)
328 error = error2;
329 }
330 return (error);
331 }
332
333 #ifndef _SYS_SYSPROTO_H_
334 struct gettimeofday_args {
335 struct timeval *tp;
336 struct timezone *tzp;
337 };
338 #endif
339 /*
340 * MPSAFE
341 */
342 /* ARGSUSED */
343 int
344 gettimeofday(struct thread *td, struct gettimeofday_args *uap)
345 {
346 struct timeval atv;
347 struct timezone rtz;
348 int error = 0;
349
350 if (uap->tp) {
351 microtime(&atv);
352 error = copyout(&atv, uap->tp, sizeof (atv));
353 }
354 if (error == 0 && uap->tzp != NULL) {
355 rtz.tz_minuteswest = tz_minuteswest;
356 rtz.tz_dsttime = tz_dsttime;
357 error = copyout(&rtz, uap->tzp, sizeof (rtz));
358 }
359 return (error);
360 }
361
362 #ifndef _SYS_SYSPROTO_H_
363 struct settimeofday_args {
364 struct timeval *tv;
365 struct timezone *tzp;
366 };
367 #endif
368 /*
369 * MPSAFE
370 */
371 /* ARGSUSED */
372 int
373 settimeofday(struct thread *td, struct settimeofday_args *uap)
374 {
375 struct timeval atv, *tvp;
376 struct timezone atz, *tzp;
377 int error;
378
379 if (uap->tv) {
380 error = copyin(uap->tv, &atv, sizeof(atv));
381 if (error)
382 return (error);
383 tvp = &atv;
384 } else
385 tvp = NULL;
386 if (uap->tzp) {
387 error = copyin(uap->tzp, &atz, sizeof(atz));
388 if (error)
389 return (error);
390 tzp = &atz;
391 } else
392 tzp = NULL;
393 return (kern_settimeofday(td, tvp, tzp));
394 }
395
396 int
397 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
398 {
399 int error;
400
401 #ifdef MAC
402 error = mac_check_system_settime(td->td_ucred);
403 if (error)
404 return (error);
405 #endif
406 error = suser(td);
407 if (error)
408 return (error);
409 /* Verify all parameters before changing time. */
410 if (tv) {
411 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
412 return (EINVAL);
413 error = settime(td, tv);
414 }
415 if (tzp && error == 0) {
416 tz_minuteswest = tzp->tz_minuteswest;
417 tz_dsttime = tzp->tz_dsttime;
418 }
419 return (error);
420 }
421
422 /*
423 * Get value of an interval timer. The process virtual and
424 * profiling virtual time timers are kept in the p_stats area, since
425 * they can be swapped out. These are kept internally in the
426 * way they are specified externally: in time until they expire.
427 *
428 * The real time interval timer is kept in the process table slot
429 * for the process, and its value (it_value) is kept as an
430 * absolute time rather than as a delta, so that it is easy to keep
431 * periodic real-time signals from drifting.
432 *
433 * Virtual time timers are processed in the hardclock() routine of
434 * kern_clock.c. The real time timer is processed by a timeout
435 * routine, called from the softclock() routine. Since a callout
436 * may be delayed in real time due to interrupt processing in the system,
437 * it is possible for the real time timeout routine (realitexpire, given below),
438 * to be delayed in real time past when it is supposed to occur. It
439 * does not suffice, therefore, to reload the real timer .it_value from the
440 * real time timers .it_interval. Rather, we compute the next time in
441 * absolute time the timer should go off.
442 */
443 #ifndef _SYS_SYSPROTO_H_
444 struct getitimer_args {
445 u_int which;
446 struct itimerval *itv;
447 };
448 #endif
449 /*
450 * MPSAFE
451 */
452 int
453 getitimer(struct thread *td, struct getitimer_args *uap)
454 {
455 struct itimerval aitv;
456 int error;
457
458 error = kern_getitimer(td, uap->which, &aitv);
459 if (error != 0)
460 return (error);
461 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
462 }
463
464 int
465 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
466 {
467 struct proc *p = td->td_proc;
468 struct timeval ctv;
469
470 if (which > ITIMER_PROF)
471 return (EINVAL);
472
473 if (which == ITIMER_REAL) {
474 /*
475 * Convert from absolute to relative time in .it_value
476 * part of real time timer. If time for real time timer
477 * has passed return 0, else return difference between
478 * current time and time for the timer to go off.
479 */
480 PROC_LOCK(p);
481 *aitv = p->p_realtimer;
482 PROC_UNLOCK(p);
483 if (timevalisset(&aitv->it_value)) {
484 getmicrouptime(&ctv);
485 if (timevalcmp(&aitv->it_value, &ctv, <))
486 timevalclear(&aitv->it_value);
487 else
488 timevalsub(&aitv->it_value, &ctv);
489 }
490 } else {
491 mtx_lock_spin(&sched_lock);
492 *aitv = p->p_stats->p_timer[which];
493 mtx_unlock_spin(&sched_lock);
494 }
495 return (0);
496 }
497
498 #ifndef _SYS_SYSPROTO_H_
499 struct setitimer_args {
500 u_int which;
501 struct itimerval *itv, *oitv;
502 };
503 #endif
504
505 /*
506 * MPSAFE
507 */
508 int
509 setitimer(struct thread *td, struct setitimer_args *uap)
510 {
511 struct itimerval aitv, oitv;
512 int error;
513
514 if (uap->itv == NULL) {
515 uap->itv = uap->oitv;
516 return (getitimer(td, (struct getitimer_args *)uap));
517 }
518
519 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
520 return (error);
521 error = kern_setitimer(td, uap->which, &aitv, &oitv);
522 if (error != 0 || uap->oitv == NULL)
523 return (error);
524 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
525 }
526
527 int
528 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
529 struct itimerval *oitv)
530 {
531 struct proc *p = td->td_proc;
532 struct timeval ctv;
533
534 if (aitv == NULL)
535 return (kern_getitimer(td, which, oitv));
536
537 if (which > ITIMER_PROF)
538 return (EINVAL);
539 if (itimerfix(&aitv->it_value))
540 return (EINVAL);
541 if (!timevalisset(&aitv->it_value))
542 timevalclear(&aitv->it_interval);
543 else if (itimerfix(&aitv->it_interval))
544 return (EINVAL);
545
546 if (which == ITIMER_REAL) {
547 PROC_LOCK(p);
548 if (timevalisset(&p->p_realtimer.it_value))
549 callout_stop(&p->p_itcallout);
550 getmicrouptime(&ctv);
551 if (timevalisset(&aitv->it_value)) {
552 callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
553 realitexpire, p);
554 timevaladd(&aitv->it_value, &ctv);
555 }
556 *oitv = p->p_realtimer;
557 p->p_realtimer = *aitv;
558 PROC_UNLOCK(p);
559 if (timevalisset(&oitv->it_value)) {
560 if (timevalcmp(&oitv->it_value, &ctv, <))
561 timevalclear(&oitv->it_value);
562 else
563 timevalsub(&oitv->it_value, &ctv);
564 }
565 } else {
566 mtx_lock_spin(&sched_lock);
567 *oitv = p->p_stats->p_timer[which];
568 p->p_stats->p_timer[which] = *aitv;
569 mtx_unlock_spin(&sched_lock);
570 }
571 return (0);
572 }
573
574 /*
575 * Real interval timer expired:
576 * send process whose timer expired an alarm signal.
577 * If time is not set up to reload, then just return.
578 * Else compute next time timer should go off which is > current time.
579 * This is where delay in processing this timeout causes multiple
580 * SIGALRM calls to be compressed into one.
581 * tvtohz() always adds 1 to allow for the time until the next clock
582 * interrupt being strictly less than 1 clock tick, but we don't want
583 * that here since we want to appear to be in sync with the clock
584 * interrupt even when we're delayed.
585 */
586 void
587 realitexpire(void *arg)
588 {
589 struct proc *p;
590 struct timeval ctv, ntv;
591
592 p = (struct proc *)arg;
593 PROC_LOCK(p);
594 psignal(p, SIGALRM);
595 if (!timevalisset(&p->p_realtimer.it_interval)) {
596 timevalclear(&p->p_realtimer.it_value);
597 if (p->p_flag & P_WEXIT)
598 wakeup(&p->p_itcallout);
599 PROC_UNLOCK(p);
600 return;
601 }
602 for (;;) {
603 timevaladd(&p->p_realtimer.it_value,
604 &p->p_realtimer.it_interval);
605 getmicrouptime(&ctv);
606 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
607 ntv = p->p_realtimer.it_value;
608 timevalsub(&ntv, &ctv);
609 callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
610 realitexpire, p);
611 PROC_UNLOCK(p);
612 return;
613 }
614 }
615 /*NOTREACHED*/
616 }
617
618 /*
619 * Check that a proposed value to load into the .it_value or
620 * .it_interval part of an interval timer is acceptable, and
621 * fix it to have at least minimal value (i.e. if it is less
622 * than the resolution of the clock, round it up.)
623 */
624 int
625 itimerfix(struct timeval *tv)
626 {
627
628 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
629 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
630 return (EINVAL);
631 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
632 tv->tv_usec = tick;
633 return (0);
634 }
635
636 /*
637 * Decrement an interval timer by a specified number
638 * of microseconds, which must be less than a second,
639 * i.e. < 1000000. If the timer expires, then reload
640 * it. In this case, carry over (usec - old value) to
641 * reduce the value reloaded into the timer so that
642 * the timer does not drift. This routine assumes
643 * that it is called in a context where the timers
644 * on which it is operating cannot change in value.
645 */
646 int
647 itimerdecr(struct itimerval *itp, int usec)
648 {
649
650 if (itp->it_value.tv_usec < usec) {
651 if (itp->it_value.tv_sec == 0) {
652 /* expired, and already in next interval */
653 usec -= itp->it_value.tv_usec;
654 goto expire;
655 }
656 itp->it_value.tv_usec += 1000000;
657 itp->it_value.tv_sec--;
658 }
659 itp->it_value.tv_usec -= usec;
660 usec = 0;
661 if (timevalisset(&itp->it_value))
662 return (1);
663 /* expired, exactly at end of interval */
664 expire:
665 if (timevalisset(&itp->it_interval)) {
666 itp->it_value = itp->it_interval;
667 itp->it_value.tv_usec -= usec;
668 if (itp->it_value.tv_usec < 0) {
669 itp->it_value.tv_usec += 1000000;
670 itp->it_value.tv_sec--;
671 }
672 } else
673 itp->it_value.tv_usec = 0; /* sec is already 0 */
674 return (0);
675 }
676
677 /*
678 * Add and subtract routines for timevals.
679 * N.B.: subtract routine doesn't deal with
680 * results which are before the beginning,
681 * it just gets very confused in this case.
682 * Caveat emptor.
683 */
684 void
685 timevaladd(struct timeval *t1, const struct timeval *t2)
686 {
687
688 t1->tv_sec += t2->tv_sec;
689 t1->tv_usec += t2->tv_usec;
690 timevalfix(t1);
691 }
692
693 void
694 timevalsub(struct timeval *t1, const struct timeval *t2)
695 {
696
697 t1->tv_sec -= t2->tv_sec;
698 t1->tv_usec -= t2->tv_usec;
699 timevalfix(t1);
700 }
701
702 static void
703 timevalfix(struct timeval *t1)
704 {
705
706 if (t1->tv_usec < 0) {
707 t1->tv_sec--;
708 t1->tv_usec += 1000000;
709 }
710 if (t1->tv_usec >= 1000000) {
711 t1->tv_sec++;
712 t1->tv_usec -= 1000000;
713 }
714 }
715
716 /*
717 * ratecheck(): simple time-based rate-limit checking.
718 */
719 int
720 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
721 {
722 struct timeval tv, delta;
723 int rv = 0;
724
725 getmicrouptime(&tv); /* NB: 10ms precision */
726 delta = tv;
727 timevalsub(&delta, lasttime);
728
729 /*
730 * check for 0,0 is so that the message will be seen at least once,
731 * even if interval is huge.
732 */
733 if (timevalcmp(&delta, mininterval, >=) ||
734 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
735 *lasttime = tv;
736 rv = 1;
737 }
738
739 return (rv);
740 }
741
742 /*
743 * ppsratecheck(): packets (or events) per second limitation.
744 *
745 * Return 0 if the limit is to be enforced (e.g. the caller
746 * should drop a packet because of the rate limitation).
747 *
748 * maxpps of 0 always causes zero to be returned. maxpps of -1
749 * always causes 1 to be returned; this effectively defeats rate
750 * limiting.
751 *
752 * Note that we maintain the struct timeval for compatibility
753 * with other bsd systems. We reuse the storage and just monitor
754 * clock ticks for minimal overhead.
755 */
756 int
757 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
758 {
759 int now;
760
761 /*
762 * Reset the last time and counter if this is the first call
763 * or more than a second has passed since the last update of
764 * lasttime.
765 */
766 now = ticks;
767 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
768 lasttime->tv_sec = now;
769 *curpps = 1;
770 return (maxpps != 0);
771 } else {
772 (*curpps)++; /* NB: ignore potential overflow */
773 return (maxpps < 0 || *curpps < maxpps);
774 }
775 }
Cache object: 57cf7c65c6214abee4490f30827a99bb
|