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 struct proc *p; 160 161 p = td->td_proc; 162 switch (uap->clock_id) { 163 case CLOCK_REALTIME: 164 nanotime(&ats); 165 break; 166 case CLOCK_VIRTUAL: 167 PROC_LOCK(p); 168 calcru(p, &user, &sys); 169 PROC_UNLOCK(p); 170 TIMEVAL_TO_TIMESPEC(&user, &ats); 171 break; 172 case CLOCK_PROF: 173 PROC_LOCK(p); 174 calcru(p, &user, &sys); 175 PROC_UNLOCK(p); 176 timevaladd(&user, &sys); 177 TIMEVAL_TO_TIMESPEC(&user, &ats); 178 break; 179 case CLOCK_MONOTONIC: 180 nanouptime(&ats); 181 break; 182 default: 183 return (EINVAL); 184 } 185 return (copyout(&ats, uap->tp, sizeof(ats))); 186 } 187 188 #ifndef _SYS_SYSPROTO_H_ 189 struct clock_settime_args { 190 clockid_t clock_id; 191 const struct timespec *tp; 192 }; 193 #endif 194 195 /* 196 * MPSAFE 197 */ 198 /* ARGSUSED */ 199 int 200 clock_settime(struct thread *td, struct clock_settime_args *uap) 201 { 202 struct timeval atv; 203 struct timespec ats; 204 int error; 205 206 #ifdef MAC 207 error = mac_check_system_settime(td->td_ucred); 208 if (error) 209 return (error); 210 #endif 211 if ((error = suser(td)) != 0) 212 return (error); 213 if (uap->clock_id != CLOCK_REALTIME) 214 return (EINVAL); 215 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0) 216 return (error); 217 if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000) 218 return (EINVAL); 219 /* XXX Don't convert nsec->usec and back */ 220 TIMESPEC_TO_TIMEVAL(&atv, &ats); 221 error = settime(td, &atv); 222 return (error); 223 } 224 225 #ifndef _SYS_SYSPROTO_H_ 226 struct clock_getres_args { 227 clockid_t clock_id; 228 struct timespec *tp; 229 }; 230 #endif 231 232 int 233 clock_getres(struct thread *td, struct clock_getres_args *uap) 234 { 235 struct timespec ts; 236 237 ts.tv_sec = 0; 238 switch (uap->clock_id) { 239 case CLOCK_REALTIME: 240 case CLOCK_MONOTONIC: 241 /* 242 * Round up the result of the division cheaply by adding 1. 243 * Rounding up is especially important if rounding down 244 * would give 0. Perfect rounding is unimportant. 245 */ 246 ts.tv_nsec = 1000000000 / tc_getfrequency() + 1; 247 break; 248 case CLOCK_VIRTUAL: 249 case CLOCK_PROF: 250 /* Accurately round up here because we can do so cheaply. */ 251 ts.tv_nsec = (1000000000 + hz - 1) / hz; 252 break; 253 default: 254 return (EINVAL); 255 } 256 if (uap->tp == NULL) 257 return (0); 258 return (copyout(&ts, uap->tp, sizeof(ts))); 259 } 260 261 static int nanowait; 262 263 int 264 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt) 265 { 266 struct timespec ts, ts2, ts3; 267 struct timeval tv; 268 int error; 269 270 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 271 return (EINVAL); 272 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0)) 273 return (0); 274 getnanouptime(&ts); 275 timespecadd(&ts, rqt); 276 TIMESPEC_TO_TIMEVAL(&tv, rqt); 277 for (;;) { 278 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", 279 tvtohz(&tv)); 280 getnanouptime(&ts2); 281 if (error != EWOULDBLOCK) { 282 if (error == ERESTART) 283 error = EINTR; 284 if (rmt != NULL) { 285 timespecsub(&ts, &ts2); 286 if (ts.tv_sec < 0) 287 timespecclear(&ts); 288 *rmt = ts; 289 } 290 return (error); 291 } 292 if (timespeccmp(&ts2, &ts, >=)) 293 return (0); 294 ts3 = ts; 295 timespecsub(&ts3, &ts2); 296 TIMESPEC_TO_TIMEVAL(&tv, &ts3); 297 } 298 } 299 300 #ifndef _SYS_SYSPROTO_H_ 301 struct nanosleep_args { 302 struct timespec *rqtp; 303 struct timespec *rmtp; 304 }; 305 #endif 306 307 /* 308 * MPSAFE 309 */ 310 /* ARGSUSED */ 311 int 312 nanosleep(struct thread *td, struct nanosleep_args *uap) 313 { 314 struct timespec rmt, rqt; 315 int error; 316 317 error = copyin(uap->rqtp, &rqt, sizeof(rqt)); 318 if (error) 319 return (error); 320 321 if (uap->rmtp && 322 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 323 return (EFAULT); 324 error = kern_nanosleep(td, &rqt, &rmt); 325 if (error && uap->rmtp) { 326 int error2; 327 328 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt)); 329 if (error2) 330 error = error2; 331 } 332 return (error); 333 } 334 335 #ifndef _SYS_SYSPROTO_H_ 336 struct gettimeofday_args { 337 struct timeval *tp; 338 struct timezone *tzp; 339 }; 340 #endif 341 /* 342 * MPSAFE 343 */ 344 /* ARGSUSED */ 345 int 346 gettimeofday(struct thread *td, struct gettimeofday_args *uap) 347 { 348 struct timeval atv; 349 struct timezone rtz; 350 int error = 0; 351 352 if (uap->tp) { 353 microtime(&atv); 354 error = copyout(&atv, uap->tp, sizeof (atv)); 355 } 356 if (error == 0 && uap->tzp != NULL) { 357 rtz.tz_minuteswest = tz_minuteswest; 358 rtz.tz_dsttime = tz_dsttime; 359 error = copyout(&rtz, uap->tzp, sizeof (rtz)); 360 } 361 return (error); 362 } 363 364 #ifndef _SYS_SYSPROTO_H_ 365 struct settimeofday_args { 366 struct timeval *tv; 367 struct timezone *tzp; 368 }; 369 #endif 370 /* 371 * MPSAFE 372 */ 373 /* ARGSUSED */ 374 int 375 settimeofday(struct thread *td, struct settimeofday_args *uap) 376 { 377 struct timeval atv; 378 struct timezone atz; 379 int error = 0; 380 381 #ifdef MAC 382 error = mac_check_system_settime(td->td_ucred); 383 if (error) 384 return (error); 385 #endif 386 if ((error = suser(td))) 387 return (error); 388 /* Verify all parameters before changing time. */ 389 if (uap->tv) { 390 if ((error = copyin(uap->tv, &atv, sizeof(atv)))) 391 return (error); 392 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000) 393 return (EINVAL); 394 } 395 if (uap->tzp && 396 (error = copyin(uap->tzp, &atz, sizeof(atz)))) 397 return (error); 398 399 if (uap->tv && (error = settime(td, &atv))) 400 return (error); 401 if (uap->tzp) { 402 tz_minuteswest = atz.tz_minuteswest; 403 tz_dsttime = atz.tz_dsttime; 404 } 405 return (error); 406 } 407 /* 408 * Get value of an interval timer. The process virtual and 409 * profiling virtual time timers are kept in the p_stats area, since 410 * they can be swapped out. These are kept internally in the 411 * way they are specified externally: in time until they expire. 412 * 413 * The real time interval timer is kept in the process table slot 414 * for the process, and its value (it_value) is kept as an 415 * absolute time rather than as a delta, so that it is easy to keep 416 * periodic real-time signals from drifting. 417 * 418 * Virtual time timers are processed in the hardclock() routine of 419 * kern_clock.c. The real time timer is processed by a timeout 420 * routine, called from the softclock() routine. Since a callout 421 * may be delayed in real time due to interrupt processing in the system, 422 * it is possible for the real time timeout routine (realitexpire, given below), 423 * to be delayed in real time past when it is supposed to occur. It 424 * does not suffice, therefore, to reload the real timer .it_value from the 425 * real time timers .it_interval. Rather, we compute the next time in 426 * absolute time the timer should go off. 427 */ 428 #ifndef _SYS_SYSPROTO_H_ 429 struct getitimer_args { 430 u_int which; 431 struct itimerval *itv; 432 }; 433 #endif 434 /* 435 * MPSAFE 436 */ 437 int 438 getitimer(struct thread *td, struct getitimer_args *uap) 439 { 440 struct itimerval aitv; 441 int error; 442 443 error = kern_getitimer(td, uap->which, &aitv); 444 if (error != 0) 445 return (error); 446 return (copyout(&aitv, uap->itv, sizeof (struct itimerval))); 447 } 448 449 int 450 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv) 451 { 452 struct proc *p = td->td_proc; 453 struct timeval ctv; 454 455 if (which > ITIMER_PROF) 456 return (EINVAL); 457 458 if (which == ITIMER_REAL) { 459 /* 460 * Convert from absolute to relative time in .it_value 461 * part of real time timer. If time for real time timer 462 * has passed return 0, else return difference between 463 * current time and time for the timer to go off. 464 */ 465 PROC_LOCK(p); 466 *aitv = p->p_realtimer; 467 PROC_UNLOCK(p); 468 if (timevalisset(&aitv->it_value)) { 469 getmicrouptime(&ctv); 470 if (timevalcmp(&aitv->it_value, &ctv, <)) 471 timevalclear(&aitv->it_value); 472 else 473 timevalsub(&aitv->it_value, &ctv); 474 } 475 } else { 476 mtx_lock_spin(&sched_lock); 477 *aitv = p->p_stats->p_timer[which]; 478 mtx_unlock_spin(&sched_lock); 479 } 480 return (0); 481 } 482 483 #ifndef _SYS_SYSPROTO_H_ 484 struct setitimer_args { 485 u_int which; 486 struct itimerval *itv, *oitv; 487 }; 488 #endif 489 490 /* 491 * MPSAFE 492 */ 493 int 494 setitimer(struct thread *td, struct setitimer_args *uap) 495 { 496 struct itimerval aitv, oitv; 497 int error; 498 499 if (uap->itv == NULL) { 500 uap->itv = uap->oitv; 501 return (getitimer(td, (struct getitimer_args *)uap)); 502 } 503 504 if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval)))) 505 return (error); 506 error = kern_setitimer(td, uap->which, &aitv, &oitv); 507 if (error != 0 || uap->oitv == NULL) 508 return (error); 509 return (copyout(&oitv, uap->oitv, sizeof(struct itimerval))); 510 } 511 512 int 513 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv, 514 struct itimerval *oitv) 515 { 516 struct proc *p = td->td_proc; 517 struct timeval ctv; 518 519 if (aitv == NULL) 520 return (kern_getitimer(td, which, oitv)); 521 522 if (which > ITIMER_PROF) 523 return (EINVAL); 524 if (itimerfix(&aitv->it_value)) 525 return (EINVAL); 526 if (!timevalisset(&aitv->it_value)) 527 timevalclear(&aitv->it_interval); 528 else if (itimerfix(&aitv->it_interval)) 529 return (EINVAL); 530 531 if (which == ITIMER_REAL) { 532 PROC_LOCK(p); 533 if (timevalisset(&p->p_realtimer.it_value)) 534 callout_stop(&p->p_itcallout); 535 getmicrouptime(&ctv); 536 if (timevalisset(&aitv->it_value)) { 537 callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value), 538 realitexpire, p); 539 timevaladd(&aitv->it_value, &ctv); 540 } 541 *oitv = p->p_realtimer; 542 p->p_realtimer = *aitv; 543 PROC_UNLOCK(p); 544 if (timevalisset(&oitv->it_value)) { 545 if (timevalcmp(&oitv->it_value, &ctv, <)) 546 timevalclear(&oitv->it_value); 547 else 548 timevalsub(&oitv->it_value, &ctv); 549 } 550 } else { 551 mtx_lock_spin(&sched_lock); 552 *oitv = p->p_stats->p_timer[which]; 553 p->p_stats->p_timer[which] = *aitv; 554 mtx_unlock_spin(&sched_lock); 555 } 556 return (0); 557 } 558 559 /* 560 * Real interval timer expired: 561 * send process whose timer expired an alarm signal. 562 * If time is not set up to reload, then just return. 563 * Else compute next time timer should go off which is > current time. 564 * This is where delay in processing this timeout causes multiple 565 * SIGALRM calls to be compressed into one. 566 * tvtohz() always adds 1 to allow for the time until the next clock 567 * interrupt being strictly less than 1 clock tick, but we don't want 568 * that here since we want to appear to be in sync with the clock 569 * interrupt even when we're delayed. 570 */ 571 void 572 realitexpire(void *arg) 573 { 574 struct proc *p; 575 struct timeval ctv, ntv; 576 577 p = (struct proc *)arg; 578 PROC_LOCK(p); 579 psignal(p, SIGALRM); 580 if (!timevalisset(&p->p_realtimer.it_interval)) { 581 timevalclear(&p->p_realtimer.it_value); 582 if (p->p_flag & P_WEXIT) 583 wakeup(&p->p_itcallout); 584 PROC_UNLOCK(p); 585 return; 586 } 587 for (;;) { 588 timevaladd(&p->p_realtimer.it_value, 589 &p->p_realtimer.it_interval); 590 getmicrouptime(&ctv); 591 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) { 592 ntv = p->p_realtimer.it_value; 593 timevalsub(&ntv, &ctv); 594 callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1, 595 realitexpire, p); 596 PROC_UNLOCK(p); 597 return; 598 } 599 } 600 /*NOTREACHED*/ 601 } 602 603 /* 604 * Check that a proposed value to load into the .it_value or 605 * .it_interval part of an interval timer is acceptable, and 606 * fix it to have at least minimal value (i.e. if it is less 607 * than the resolution of the clock, round it up.) 608 */ 609 int 610 itimerfix(struct timeval *tv) 611 { 612 613 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 614 tv->tv_usec < 0 || tv->tv_usec >= 1000000) 615 return (EINVAL); 616 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 617 tv->tv_usec = tick; 618 return (0); 619 } 620 621 /* 622 * Decrement an interval timer by a specified number 623 * of microseconds, which must be less than a second, 624 * i.e. < 1000000. If the timer expires, then reload 625 * it. In this case, carry over (usec - old value) to 626 * reduce the value reloaded into the timer so that 627 * the timer does not drift. This routine assumes 628 * that it is called in a context where the timers 629 * on which it is operating cannot change in value. 630 */ 631 int 632 itimerdecr(struct itimerval *itp, int usec) 633 { 634 635 if (itp->it_value.tv_usec < usec) { 636 if (itp->it_value.tv_sec == 0) { 637 /* expired, and already in next interval */ 638 usec -= itp->it_value.tv_usec; 639 goto expire; 640 } 641 itp->it_value.tv_usec += 1000000; 642 itp->it_value.tv_sec--; 643 } 644 itp->it_value.tv_usec -= usec; 645 usec = 0; 646 if (timevalisset(&itp->it_value)) 647 return (1); 648 /* expired, exactly at end of interval */ 649 expire: 650 if (timevalisset(&itp->it_interval)) { 651 itp->it_value = itp->it_interval; 652 itp->it_value.tv_usec -= usec; 653 if (itp->it_value.tv_usec < 0) { 654 itp->it_value.tv_usec += 1000000; 655 itp->it_value.tv_sec--; 656 } 657 } else 658 itp->it_value.tv_usec = 0; /* sec is already 0 */ 659 return (0); 660 } 661 662 /* 663 * Add and subtract routines for timevals. 664 * N.B.: subtract routine doesn't deal with 665 * results which are before the beginning, 666 * it just gets very confused in this case. 667 * Caveat emptor. 668 */ 669 void 670 timevaladd(struct timeval *t1, const struct timeval *t2) 671 { 672 673 t1->tv_sec += t2->tv_sec; 674 t1->tv_usec += t2->tv_usec; 675 timevalfix(t1); 676 } 677 678 void 679 timevalsub(struct timeval *t1, const struct timeval *t2) 680 { 681 682 t1->tv_sec -= t2->tv_sec; 683 t1->tv_usec -= t2->tv_usec; 684 timevalfix(t1); 685 } 686 687 static void 688 timevalfix(struct timeval *t1) 689 { 690 691 if (t1->tv_usec < 0) { 692 t1->tv_sec--; 693 t1->tv_usec += 1000000; 694 } 695 if (t1->tv_usec >= 1000000) { 696 t1->tv_sec++; 697 t1->tv_usec -= 1000000; 698 } 699 } 700 701 /* 702 * ratecheck(): simple time-based rate-limit checking. 703 */ 704 int 705 ratecheck(struct timeval *lasttime, const struct timeval *mininterval) 706 { 707 struct timeval tv, delta; 708 int rv = 0; 709 710 getmicrouptime(&tv); /* NB: 10ms precision */ 711 delta = tv; 712 timevalsub(&delta, lasttime); 713 714 /* 715 * check for 0,0 is so that the message will be seen at least once, 716 * even if interval is huge. 717 */ 718 if (timevalcmp(&delta, mininterval, >=) || 719 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 720 *lasttime = tv; 721 rv = 1; 722 } 723 724 return (rv); 725 } 726 727 /* 728 * ppsratecheck(): packets (or events) per second limitation. 729 * 730 * Return 0 if the limit is to be enforced (e.g. the caller 731 * should drop a packet because of the rate limitation). 732 * 733 * maxpps of 0 always causes zero to be returned. maxpps of -1 734 * always causes 1 to be returned; this effectively defeats rate 735 * limiting. 736 * 737 * Note that we maintain the struct timeval for compatibility 738 * with other bsd systems. We reuse the storage and just monitor 739 * clock ticks for minimal overhead. 740 */ 741 int 742 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 743 { 744 int now; 745 746 /* 747 * Reset the last time and counter if this is the first call 748 * or more than a second has passed since the last update of 749 * lasttime. 750 */ 751 now = ticks; 752 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) { 753 lasttime->tv_sec = now; 754 *curpps = 1; 755 return (maxpps != 0); 756 } else { 757 (*curpps)++; /* NB: ignore potential overflow */ 758 return (maxpps < 0 || *curpps < maxpps); 759 } 760 } 761