kern_time.c revision 1.61.10.2 1 /* $NetBSD: kern_time.c,v 1.61.10.2 2005/12/07 10:43:07 tron Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christopher G. Demetriou.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1982, 1986, 1989, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
72 */
73
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.61.10.2 2005/12/07 10:43:07 tron Exp $");
76
77 #include "fs_nfs.h"
78 #include "opt_nfs.h"
79 #include "opt_nfsserver.h"
80
81 #include <sys/param.h>
82 #include <sys/resourcevar.h>
83 #include <sys/kernel.h>
84 #include <sys/systm.h>
85 #include <sys/proc.h>
86 #include <sys/vnode.h>
87 #include <sys/signalvar.h>
88 #include <sys/syslog.h>
89
90 #include <sys/mount.h>
91 #include <sys/syscallargs.h>
92
93 #include <uvm/uvm_extern.h>
94
95 #if defined(NFS) || defined(NFSSERVER)
96 #include <nfs/rpcv2.h>
97 #include <nfs/nfsproto.h>
98 #include <nfs/nfs_var.h>
99 #endif
100
101 #include <machine/cpu.h>
102
103 /*
104 * Time of day and interval timer support.
105 *
106 * These routines provide the kernel entry points to get and set
107 * the time-of-day and per-process interval timers. Subroutines
108 * here provide support for adding and subtracting timeval structures
109 * and decrementing interval timers, optionally reloading the interval
110 * timers when they expire.
111 */
112
113 /* This function is used by clock_settime and settimeofday */
114 int
115 settime(tv)
116 struct timeval *tv;
117 {
118 struct timeval delta;
119 struct cpu_info *ci;
120 int s;
121
122 /*
123 * Don't allow the time to be set forward so far it will wrap
124 * and become negative, thus allowing an attacker to bypass
125 * the next check below. The cutoff is 1 year before rollover
126 * occurs, so even if the attacker uses adjtime(2) to move
127 * the time past the cutoff, it will take a very long time
128 * to get to the wrap point.
129 *
130 * XXX: we check against INT_MAX since on 64-bit
131 * platforms, sizeof(int) != sizeof(long) and
132 * time_t is 32 bits even when atv.tv_sec is 64 bits.
133 */
134 if (tv->tv_sec > INT_MAX - 365*24*60*60) {
135 struct proc *p = curproc;
136 struct proc *pp = p->p_pptr;
137 log(LOG_WARNING, "pid %d (%s) "
138 "invoked by uid %d ppid %d (%s) "
139 "tried to set clock forward to %ld\n",
140 p->p_pid, p->p_comm, pp->p_ucred->cr_uid,
141 pp->p_pid, pp->p_comm, (long)tv->tv_sec);
142 return (EPERM);
143 }
144 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
145 s = splclock();
146 timersub(tv, &time, &delta);
147 if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1) {
148 splx(s);
149 return (EPERM);
150 }
151 #ifdef notyet
152 if ((delta.tv_sec < 86400) && securelevel > 0) {
153 splx(s);
154 return (EPERM);
155 }
156 #endif
157 time = *tv;
158 (void) spllowersoftclock();
159 timeradd(&boottime, &delta, &boottime);
160 /*
161 * XXXSMP
162 * This is wrong. We should traverse a list of all
163 * CPUs and add the delta to the runtime of those
164 * CPUs which have a process on them.
165 */
166 ci = curcpu();
167 timeradd(&ci->ci_schedstate.spc_runtime, &delta,
168 &ci->ci_schedstate.spc_runtime);
169 # if (defined(NFS) && !defined (NFS_V2_ONLY)) || defined(NFSSERVER)
170 nqnfs_lease_updatetime(delta.tv_sec);
171 # endif
172 splx(s);
173 resettodr();
174 return (0);
175 }
176
177 /* ARGSUSED */
178 int
179 sys_clock_gettime(p, v, retval)
180 struct proc *p;
181 void *v;
182 register_t *retval;
183 {
184 struct sys_clock_gettime_args /* {
185 syscallarg(clockid_t) clock_id;
186 syscallarg(struct timespec *) tp;
187 } */ *uap = v;
188 clockid_t clock_id;
189 struct timeval atv;
190 struct timespec ats;
191 int s;
192
193 clock_id = SCARG(uap, clock_id);
194 switch (clock_id) {
195 case CLOCK_REALTIME:
196 microtime(&atv);
197 TIMEVAL_TO_TIMESPEC(&atv,&ats);
198 break;
199 case CLOCK_MONOTONIC:
200 /* XXX "hz" granularity */
201 s = splclock();
202 atv = mono_time;
203 splx(s);
204 TIMEVAL_TO_TIMESPEC(&atv,&ats);
205 break;
206 default:
207 return (EINVAL);
208 }
209
210 return copyout(&ats, SCARG(uap, tp), sizeof(ats));
211 }
212
213 /* ARGSUSED */
214 int
215 sys_clock_settime(p, v, retval)
216 struct proc *p;
217 void *v;
218 register_t *retval;
219 {
220 struct sys_clock_settime_args /* {
221 syscallarg(clockid_t) clock_id;
222 syscallarg(const struct timespec *) tp;
223 } */ *uap = v;
224 int error;
225
226 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
227 return (error);
228
229 return (clock_settime1(SCARG(uap, clock_id), SCARG(uap, tp)));
230 }
231
232
233 int
234 clock_settime1(clock_id, tp)
235 clockid_t clock_id;
236 const struct timespec *tp;
237 {
238 struct timespec ats;
239 struct timeval atv;
240 int error;
241
242 if ((error = copyin(tp, &ats, sizeof(ats))) != 0)
243 return (error);
244
245 switch (clock_id) {
246 case CLOCK_REALTIME:
247 TIMESPEC_TO_TIMEVAL(&atv, &ats);
248 if ((error = settime(&atv)) != 0)
249 return (error);
250 break;
251 case CLOCK_MONOTONIC:
252 return (EINVAL); /* read-only clock */
253 default:
254 return (EINVAL);
255 }
256
257 return 0;
258 }
259
260 int
261 sys_clock_getres(p, v, retval)
262 struct proc *p;
263 void *v;
264 register_t *retval;
265 {
266 struct sys_clock_getres_args /* {
267 syscallarg(clockid_t) clock_id;
268 syscallarg(struct timespec *) tp;
269 } */ *uap = v;
270 clockid_t clock_id;
271 struct timespec ts;
272 int error = 0;
273
274 clock_id = SCARG(uap, clock_id);
275 switch (clock_id) {
276 case CLOCK_REALTIME:
277 case CLOCK_MONOTONIC:
278 ts.tv_sec = 0;
279 ts.tv_nsec = 1000000000 / hz;
280 break;
281 default:
282 return (EINVAL);
283 }
284
285 if (SCARG(uap, tp))
286 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
287
288 return error;
289 }
290
291 /* ARGSUSED */
292 int
293 sys_nanosleep(p, v, retval)
294 struct proc *p;
295 void *v;
296 register_t *retval;
297 {
298 static int nanowait;
299 struct sys_nanosleep_args/* {
300 syscallarg(struct timespec *) rqtp;
301 syscallarg(struct timespec *) rmtp;
302 } */ *uap = v;
303 struct timespec rqt;
304 struct timespec rmt;
305 struct timeval atv, utv;
306 int error, s, timo;
307
308 error = copyin((caddr_t)SCARG(uap, rqtp), (caddr_t)&rqt,
309 sizeof(struct timespec));
310 if (error)
311 return (error);
312
313 TIMESPEC_TO_TIMEVAL(&atv,&rqt)
314 if (itimerfix(&atv) || atv.tv_sec > 1000000000)
315 return (EINVAL);
316
317 s = splclock();
318 timeradd(&atv,&time,&atv);
319 timo = hzto(&atv);
320 /*
321 * Avoid inadvertantly sleeping forever
322 */
323 if (timo == 0)
324 timo = 1;
325 splx(s);
326
327 error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
328 if (error == ERESTART)
329 error = EINTR;
330 if (error == EWOULDBLOCK)
331 error = 0;
332
333 if (SCARG(uap, rmtp)) {
334 int error;
335
336 s = splclock();
337 utv = time;
338 splx(s);
339
340 timersub(&atv, &utv, &utv);
341 if (utv.tv_sec < 0)
342 timerclear(&utv);
343
344 TIMEVAL_TO_TIMESPEC(&utv,&rmt);
345 error = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
346 sizeof(rmt));
347 if (error)
348 return (error);
349 }
350
351 return error;
352 }
353
354 /* ARGSUSED */
355 int
356 sys_gettimeofday(p, v, retval)
357 struct proc *p;
358 void *v;
359 register_t *retval;
360 {
361 struct sys_gettimeofday_args /* {
362 syscallarg(struct timeval *) tp;
363 syscallarg(struct timezone *) tzp;
364 } */ *uap = v;
365 struct timeval atv;
366 int error = 0;
367 struct timezone tzfake;
368
369 if (SCARG(uap, tp)) {
370 microtime(&atv);
371 error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
372 if (error)
373 return (error);
374 }
375 if (SCARG(uap, tzp)) {
376 /*
377 * NetBSD has no kernel notion of time zone, so we just
378 * fake up a timezone struct and return it if demanded.
379 */
380 tzfake.tz_minuteswest = 0;
381 tzfake.tz_dsttime = 0;
382 error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
383 }
384 return (error);
385 }
386
387 /* ARGSUSED */
388 int
389 sys_settimeofday(p, v, retval)
390 struct proc *p;
391 void *v;
392 register_t *retval;
393 {
394 struct sys_settimeofday_args /* {
395 syscallarg(const struct timeval *) tv;
396 syscallarg(const struct timezone *) tzp;
397 } */ *uap = v;
398 int error;
399
400 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
401 return (error);
402
403 return settimeofday1(SCARG(uap, tv), SCARG(uap, tzp), p);
404 }
405
406 int
407 settimeofday1(utv, utzp, p)
408 const struct timeval *utv;
409 const struct timezone *utzp;
410 struct proc *p;
411 {
412 struct timeval atv;
413 struct timezone atz;
414 struct timeval *tv = NULL;
415 struct timezone *tzp = NULL;
416 int error;
417
418 /* Verify all parameters before changing time. */
419 if (utv) {
420 if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
421 return (error);
422 tv = &atv;
423 }
424 /* XXX since we don't use tz, probably no point in doing copyin. */
425 if (utzp) {
426 if ((error = copyin(utzp, &atz, sizeof(atz))) != 0)
427 return (error);
428 tzp = &atz;
429 }
430
431 if (tv)
432 if ((error = settime(tv)) != 0)
433 return (error);
434 /*
435 * NetBSD has no kernel notion of time zone, and only an
436 * obsolete program would try to set it, so we log a warning.
437 */
438 if (tzp)
439 log(LOG_WARNING, "pid %d attempted to set the "
440 "(obsolete) kernel time zone\n", p->p_pid);
441 return (0);
442 }
443
444 int tickdelta; /* current clock skew, us. per tick */
445 long timedelta; /* unapplied time correction, us. */
446 long bigadj = 1000000; /* use 10x skew above bigadj us. */
447
448 /* ARGSUSED */
449 int
450 sys_adjtime(p, v, retval)
451 struct proc *p;
452 void *v;
453 register_t *retval;
454 {
455 struct sys_adjtime_args /* {
456 syscallarg(const struct timeval *) delta;
457 syscallarg(struct timeval *) olddelta;
458 } */ *uap = v;
459 int error;
460
461 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
462 return (error);
463
464 return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), p);
465 }
466
467 int
468 adjtime1(delta, olddelta, p)
469 const struct timeval *delta;
470 struct timeval *olddelta;
471 struct proc *p;
472 {
473 struct timeval atv;
474 struct timeval *oatv = NULL;
475 long ndelta, ntickdelta, odelta;
476 int error;
477 int s;
478
479 error = copyin(delta, &atv, sizeof(struct timeval));
480 if (error)
481 return (error);
482
483 if (olddelta != NULL) {
484 if (uvm_useracc((caddr_t)olddelta,
485 sizeof(struct timeval), B_WRITE) == FALSE)
486 return (EFAULT);
487 oatv = olddelta;
488 }
489
490 /*
491 * Compute the total correction and the rate at which to apply it.
492 * Round the adjustment down to a whole multiple of the per-tick
493 * delta, so that after some number of incremental changes in
494 * hardclock(), tickdelta will become zero, lest the correction
495 * overshoot and start taking us away from the desired final time.
496 */
497 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
498 if (ndelta > bigadj || ndelta < -bigadj)
499 ntickdelta = 10 * tickadj;
500 else
501 ntickdelta = tickadj;
502 if (ndelta % ntickdelta)
503 ndelta = ndelta / ntickdelta * ntickdelta;
504
505 /*
506 * To make hardclock()'s job easier, make the per-tick delta negative
507 * if we want time to run slower; then hardclock can simply compute
508 * tick + tickdelta, and subtract tickdelta from timedelta.
509 */
510 if (ndelta < 0)
511 ntickdelta = -ntickdelta;
512 s = splclock();
513 odelta = timedelta;
514 timedelta = ndelta;
515 tickdelta = ntickdelta;
516 splx(s);
517
518 if (olddelta) {
519 atv.tv_sec = odelta / 1000000;
520 atv.tv_usec = odelta % 1000000;
521 (void) copyout(&atv, olddelta, sizeof(struct timeval));
522 }
523 return (0);
524 }
525
526 /*
527 * Get value of an interval timer. The process virtual and
528 * profiling virtual time timers are kept in the p_stats area, since
529 * they can be swapped out. These are kept internally in the
530 * way they are specified externally: in time until they expire.
531 *
532 * The real time interval timer is kept in the process table slot
533 * for the process, and its value (it_value) is kept as an
534 * absolute time rather than as a delta, so that it is easy to keep
535 * periodic real-time signals from drifting.
536 *
537 * Virtual time timers are processed in the hardclock() routine of
538 * kern_clock.c. The real time timer is processed by a timeout
539 * routine, called from the softclock() routine. Since a callout
540 * may be delayed in real time due to interrupt processing in the system,
541 * it is possible for the real time timeout routine (realitexpire, given below),
542 * to be delayed in real time past when it is supposed to occur. It
543 * does not suffice, therefore, to reload the real timer .it_value from the
544 * real time timers .it_interval. Rather, we compute the next time in
545 * absolute time the timer should go off.
546 */
547 /* ARGSUSED */
548 int
549 sys_getitimer(p, v, retval)
550 struct proc *p;
551 void *v;
552 register_t *retval;
553 {
554 struct sys_getitimer_args /* {
555 syscallarg(int) which;
556 syscallarg(struct itimerval *) itv;
557 } */ *uap = v;
558 int which = SCARG(uap, which);
559 struct itimerval aitv;
560 int s;
561
562 if ((u_int)which > ITIMER_PROF)
563 return (EINVAL);
564 s = splclock();
565 if (which == ITIMER_REAL) {
566 /*
567 * Convert from absolute to relative time in .it_value
568 * part of real time timer. If time for real time timer
569 * has passed return 0, else return difference between
570 * current time and time for the timer to go off.
571 */
572 aitv = p->p_realtimer;
573 if (timerisset(&aitv.it_value)) {
574 if (timercmp(&aitv.it_value, &time, <))
575 timerclear(&aitv.it_value);
576 else
577 timersub(&aitv.it_value, &time, &aitv.it_value);
578 }
579 } else
580 aitv = p->p_stats->p_timer[which];
581 splx(s);
582 return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
583 }
584
585 /* ARGSUSED */
586 int
587 sys_setitimer(p, v, retval)
588 struct proc *p;
589 void *v;
590 register_t *retval;
591 {
592 struct sys_setitimer_args /* {
593 syscallarg(int) which;
594 syscallarg(const struct itimerval *) itv;
595 syscallarg(struct itimerval *) oitv;
596 } */ *uap = v;
597 int which = SCARG(uap, which);
598 struct sys_getitimer_args getargs;
599 struct itimerval aitv;
600 const struct itimerval *itvp;
601 int s, error;
602
603 if ((u_int)which > ITIMER_PROF)
604 return (EINVAL);
605 itvp = SCARG(uap, itv);
606 if (itvp &&
607 (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
608 return (error);
609 if (SCARG(uap, oitv) != NULL) {
610 SCARG(&getargs, which) = which;
611 SCARG(&getargs, itv) = SCARG(uap, oitv);
612 if ((error = sys_getitimer(p, &getargs, retval)) != 0)
613 return (error);
614 }
615 if (itvp == 0)
616 return (0);
617 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
618 return (EINVAL);
619 s = splclock();
620 if (which == ITIMER_REAL) {
621 callout_stop(&p->p_realit_ch);
622 if (timerisset(&aitv.it_value)) {
623 /*
624 * Don't need to check hzto() return value, here.
625 * callout_reset() does it for us.
626 */
627 timeradd(&aitv.it_value, &time, &aitv.it_value);
628 callout_reset(&p->p_realit_ch, hzto(&aitv.it_value),
629 realitexpire, p);
630 }
631 p->p_realtimer = aitv;
632 } else
633 p->p_stats->p_timer[which] = aitv;
634 splx(s);
635 return (0);
636 }
637
638 /*
639 * Real interval timer expired:
640 * send process whose timer expired an alarm signal.
641 * If time is not set up to reload, then just return.
642 * Else compute next time timer should go off which is > current time.
643 * This is where delay in processing this timeout causes multiple
644 * SIGALRM calls to be compressed into one.
645 */
646 void
647 realitexpire(arg)
648 void *arg;
649 {
650 struct proc *p;
651 int s;
652
653 p = (struct proc *)arg;
654 psignal(p, SIGALRM);
655 if (!timerisset(&p->p_realtimer.it_interval)) {
656 timerclear(&p->p_realtimer.it_value);
657 return;
658 }
659 for (;;) {
660 s = splclock();
661 timeradd(&p->p_realtimer.it_value,
662 &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
663 if (timercmp(&p->p_realtimer.it_value, &time, >)) {
664 /*
665 * Don't need to check hzto() return value, here.
666 * callout_reset() does it for us.
667 */
668 callout_reset(&p->p_realit_ch,
669 hzto(&p->p_realtimer.it_value), realitexpire, p);
670 splx(s);
671 return;
672 }
673 splx(s);
674 }
675 }
676
677 /*
678 * Check that a proposed value to load into the .it_value or
679 * .it_interval part of an interval timer is acceptable, and
680 * fix it to have at least minimal value (i.e. if it is less
681 * than the resolution of the clock, round it up.)
682 */
683 int
684 itimerfix(tv)
685 struct timeval *tv;
686 {
687
688 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
689 return (EINVAL);
690 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
691 tv->tv_usec = tick;
692 return (0);
693 }
694
695 /*
696 * Decrement an interval timer by a specified number
697 * of microseconds, which must be less than a second,
698 * i.e. < 1000000. If the timer expires, then reload
699 * it. In this case, carry over (usec - old value) to
700 * reduce the value reloaded into the timer so that
701 * the timer does not drift. This routine assumes
702 * that it is called in a context where the timers
703 * on which it is operating cannot change in value.
704 */
705 int
706 itimerdecr(itp, usec)
707 struct itimerval *itp;
708 int usec;
709 {
710
711 if (itp->it_value.tv_usec < usec) {
712 if (itp->it_value.tv_sec == 0) {
713 /* expired, and already in next interval */
714 usec -= itp->it_value.tv_usec;
715 goto expire;
716 }
717 itp->it_value.tv_usec += 1000000;
718 itp->it_value.tv_sec--;
719 }
720 itp->it_value.tv_usec -= usec;
721 usec = 0;
722 if (timerisset(&itp->it_value))
723 return (1);
724 /* expired, exactly at end of interval */
725 expire:
726 if (timerisset(&itp->it_interval)) {
727 itp->it_value = itp->it_interval;
728 itp->it_value.tv_usec -= usec;
729 if (itp->it_value.tv_usec < 0) {
730 itp->it_value.tv_usec += 1000000;
731 itp->it_value.tv_sec--;
732 }
733 } else
734 itp->it_value.tv_usec = 0; /* sec is already 0 */
735 return (0);
736 }
737
738 /*
739 * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
740 * for usage and rationale.
741 */
742 int
743 ratecheck(lasttime, mininterval)
744 struct timeval *lasttime;
745 const struct timeval *mininterval;
746 {
747 struct timeval tv, delta;
748 int s, rv = 0;
749
750 s = splclock();
751 tv = mono_time;
752 splx(s);
753
754 timersub(&tv, lasttime, &delta);
755
756 /*
757 * check for 0,0 is so that the message will be seen at least once,
758 * even if interval is huge.
759 */
760 if (timercmp(&delta, mininterval, >=) ||
761 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
762 *lasttime = tv;
763 rv = 1;
764 }
765
766 return (rv);
767 }
768
769 /*
770 * ppsratecheck(): packets (or events) per second limitation.
771 */
772 int
773 ppsratecheck(lasttime, curpps, maxpps)
774 struct timeval *lasttime;
775 int *curpps;
776 int maxpps; /* maximum pps allowed */
777 {
778 struct timeval tv, delta;
779 int s, rv;
780
781 s = splclock();
782 tv = mono_time;
783 splx(s);
784
785 timersub(&tv, lasttime, &delta);
786
787 /*
788 * check for 0,0 is so that the message will be seen at least once.
789 * if more than one second have passed since the last update of
790 * lasttime, reset the counter.
791 *
792 * we do increment *curpps even in *curpps < maxpps case, as some may
793 * try to use *curpps for stat purposes as well.
794 */
795 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
796 delta.tv_sec >= 1) {
797 *lasttime = tv;
798 *curpps = 0;
799 rv = 1;
800 } else if (maxpps < 0)
801 rv = 1;
802 else if (*curpps < maxpps)
803 rv = 1;
804 else
805 rv = 0;
806
807 #if 1 /*DIAGNOSTIC?*/
808 /* be careful about wrap-around */
809 if (*curpps + 1 > *curpps)
810 *curpps = *curpps + 1;
811 #else
812 /*
813 * assume that there's not too many calls to this function.
814 * not sure if the assumption holds, as it depends on *caller's*
815 * behavior, not the behavior of this function.
816 * IMHO it is wrong to make assumption on the caller's behavior,
817 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
818 */
819 *curpps = *curpps + 1;
820 #endif
821
822 return (rv);
823 }
824