FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_time.c
1 /* $NetBSD: kern_time.c,v 1.88.2.3 2005/12/07 09:53:55 tron Exp $ */
2
3 /*-
4 * Copyright (c) 2000, 2004, 2005 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. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.88.2.3 2005/12/07 09:53:55 tron Exp $");
72
73 #include "fs_nfs.h"
74 #include "opt_nfs.h"
75 #include "opt_nfsserver.h"
76
77 #include <sys/param.h>
78 #include <sys/resourcevar.h>
79 #include <sys/kernel.h>
80 #include <sys/systm.h>
81 #include <sys/malloc.h>
82 #include <sys/proc.h>
83 #include <sys/sa.h>
84 #include <sys/savar.h>
85 #include <sys/vnode.h>
86 #include <sys/signalvar.h>
87 #include <sys/syslog.h>
88
89 #include <sys/mount.h>
90 #include <sys/syscallargs.h>
91
92 #include <uvm/uvm_extern.h>
93
94 #if defined(NFS) || defined(NFSSERVER)
95 #include <nfs/rpcv2.h>
96 #include <nfs/nfsproto.h>
97 #include <nfs/nfs_var.h>
98 #endif
99
100 #include <machine/cpu.h>
101
102 static void timerupcall(struct lwp *, void *);
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(struct timeval *tv)
116 {
117 struct timeval delta;
118 struct cpu_info *ci;
119 int s;
120
121 /*
122 * Don't allow the time to be set forward so far it will wrap
123 * and become negative, thus allowing an attacker to bypass
124 * the next check below. The cutoff is 1 year before rollover
125 * occurs, so even if the attacker uses adjtime(2) to move
126 * the time past the cutoff, it will take a very long time
127 * to get to the wrap point.
128 *
129 * XXX: we check against INT_MAX since on 64-bit
130 * platforms, sizeof(int) != sizeof(long) and
131 * time_t is 32 bits even when atv.tv_sec is 64 bits.
132 */
133 if (tv->tv_sec > INT_MAX - 365*24*60*60) {
134 struct proc *p = curproc;
135 struct proc *pp = p->p_pptr;
136 log(LOG_WARNING, "pid %d (%s) "
137 "invoked by uid %d ppid %d (%s) "
138 "tried to set clock forward to %ld\n",
139 p->p_pid, p->p_comm, pp->p_ucred->cr_uid,
140 pp->p_pid, pp->p_comm, (long)tv->tv_sec);
141 return (EPERM);
142 }
143 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
144 s = splclock();
145 timersub(tv, &time, &delta);
146 if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1) {
147 splx(s);
148 return (EPERM);
149 }
150 #ifdef notyet
151 if ((delta.tv_sec < 86400) && securelevel > 0) {
152 splx(s);
153 return (EPERM);
154 }
155 #endif
156 time = *tv;
157 (void) spllowersoftclock();
158 timeradd(&boottime, &delta, &boottime);
159 /*
160 * XXXSMP
161 * This is wrong. We should traverse a list of all
162 * CPUs and add the delta to the runtime of those
163 * CPUs which have a process on them.
164 */
165 ci = curcpu();
166 timeradd(&ci->ci_schedstate.spc_runtime, &delta,
167 &ci->ci_schedstate.spc_runtime);
168 # if (defined(NFS) && !defined (NFS_V2_ONLY)) || defined(NFSSERVER)
169 nqnfs_lease_updatetime(delta.tv_sec);
170 # endif
171 splx(s);
172 resettodr();
173 return (0);
174 }
175
176 /* ARGSUSED */
177 int
178 sys_clock_gettime(struct lwp *l, void *v, register_t *retval)
179 {
180 struct sys_clock_gettime_args /* {
181 syscallarg(clockid_t) clock_id;
182 syscallarg(struct timespec *) tp;
183 } */ *uap = v;
184 clockid_t clock_id;
185 struct timeval atv;
186 struct timespec ats;
187 int s;
188
189 clock_id = SCARG(uap, clock_id);
190 switch (clock_id) {
191 case CLOCK_REALTIME:
192 microtime(&atv);
193 TIMEVAL_TO_TIMESPEC(&atv,&ats);
194 break;
195 case CLOCK_MONOTONIC:
196 /* XXX "hz" granularity */
197 s = splclock();
198 atv = mono_time;
199 splx(s);
200 TIMEVAL_TO_TIMESPEC(&atv,&ats);
201 break;
202 default:
203 return (EINVAL);
204 }
205
206 return copyout(&ats, SCARG(uap, tp), sizeof(ats));
207 }
208
209 /* ARGSUSED */
210 int
211 sys_clock_settime(l, v, retval)
212 struct lwp *l;
213 void *v;
214 register_t *retval;
215 {
216 struct sys_clock_settime_args /* {
217 syscallarg(clockid_t) clock_id;
218 syscallarg(const struct timespec *) tp;
219 } */ *uap = v;
220 struct proc *p = l->l_proc;
221 int error;
222
223 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
224 return (error);
225
226 return (clock_settime1(SCARG(uap, clock_id), SCARG(uap, tp)));
227 }
228
229
230 int
231 clock_settime1(clock_id, tp)
232 clockid_t clock_id;
233 const struct timespec *tp;
234 {
235 struct timespec ats;
236 struct timeval atv;
237 int error;
238
239 if ((error = copyin(tp, &ats, sizeof(ats))) != 0)
240 return (error);
241
242 switch (clock_id) {
243 case CLOCK_REALTIME:
244 TIMESPEC_TO_TIMEVAL(&atv, &ats);
245 if ((error = settime(&atv)) != 0)
246 return (error);
247 break;
248 case CLOCK_MONOTONIC:
249 return (EINVAL); /* read-only clock */
250 default:
251 return (EINVAL);
252 }
253
254 return 0;
255 }
256
257 int
258 sys_clock_getres(struct lwp *l, void *v, register_t *retval)
259 {
260 struct sys_clock_getres_args /* {
261 syscallarg(clockid_t) clock_id;
262 syscallarg(struct timespec *) tp;
263 } */ *uap = v;
264 clockid_t clock_id;
265 struct timespec ts;
266 int error = 0;
267
268 clock_id = SCARG(uap, clock_id);
269 switch (clock_id) {
270 case CLOCK_REALTIME:
271 case CLOCK_MONOTONIC:
272 ts.tv_sec = 0;
273 ts.tv_nsec = 1000000000 / hz;
274 break;
275 default:
276 return (EINVAL);
277 }
278
279 if (SCARG(uap, tp))
280 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
281
282 return error;
283 }
284
285 /* ARGSUSED */
286 int
287 sys_nanosleep(struct lwp *l, void *v, register_t *retval)
288 {
289 static int nanowait;
290 struct sys_nanosleep_args/* {
291 syscallarg(struct timespec *) rqtp;
292 syscallarg(struct timespec *) rmtp;
293 } */ *uap = v;
294 struct timespec rqt;
295 struct timespec rmt;
296 struct timeval atv, utv;
297 int error, s, timo;
298
299 error = copyin((caddr_t)SCARG(uap, rqtp), (caddr_t)&rqt,
300 sizeof(struct timespec));
301 if (error)
302 return (error);
303
304 TIMESPEC_TO_TIMEVAL(&atv,&rqt);
305 if (itimerfix(&atv))
306 return (EINVAL);
307
308 s = splclock();
309 timeradd(&atv,&time,&atv);
310 timo = hzto(&atv);
311 /*
312 * Avoid inadvertantly sleeping forever
313 */
314 if (timo == 0)
315 timo = 1;
316 splx(s);
317
318 error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
319 if (error == ERESTART)
320 error = EINTR;
321 if (error == EWOULDBLOCK)
322 error = 0;
323
324 if (SCARG(uap, rmtp)) {
325 int error;
326
327 s = splclock();
328 utv = time;
329 splx(s);
330
331 timersub(&atv, &utv, &utv);
332 if (utv.tv_sec < 0)
333 timerclear(&utv);
334
335 TIMEVAL_TO_TIMESPEC(&utv,&rmt);
336 error = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
337 sizeof(rmt));
338 if (error)
339 return (error);
340 }
341
342 return error;
343 }
344
345 /* ARGSUSED */
346 int
347 sys_gettimeofday(struct lwp *l, void *v, register_t *retval)
348 {
349 struct sys_gettimeofday_args /* {
350 syscallarg(struct timeval *) tp;
351 syscallarg(void *) tzp; really "struct timezone *"
352 } */ *uap = v;
353 struct timeval atv;
354 int error = 0;
355 struct timezone tzfake;
356
357 if (SCARG(uap, tp)) {
358 microtime(&atv);
359 error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
360 if (error)
361 return (error);
362 }
363 if (SCARG(uap, tzp)) {
364 /*
365 * NetBSD has no kernel notion of time zone, so we just
366 * fake up a timezone struct and return it if demanded.
367 */
368 tzfake.tz_minuteswest = 0;
369 tzfake.tz_dsttime = 0;
370 error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
371 }
372 return (error);
373 }
374
375 /* ARGSUSED */
376 int
377 sys_settimeofday(struct lwp *l, void *v, register_t *retval)
378 {
379 struct sys_settimeofday_args /* {
380 syscallarg(const struct timeval *) tv;
381 syscallarg(const void *) tzp; really "const struct timezone *"
382 } */ *uap = v;
383 struct proc *p = l->l_proc;
384 int error;
385
386 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
387 return (error);
388
389 return settimeofday1(SCARG(uap, tv), SCARG(uap, tzp), p);
390 }
391
392 int
393 settimeofday1(utv, utzp, p)
394 const struct timeval *utv;
395 const struct timezone *utzp;
396 struct proc *p;
397 {
398 struct timeval atv;
399 struct timezone atz;
400 struct timeval *tv = NULL;
401 struct timezone *tzp = NULL;
402 int error;
403
404 /* Verify all parameters before changing time. */
405 if (utv) {
406 if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
407 return (error);
408 tv = &atv;
409 }
410 /* XXX since we don't use tz, probably no point in doing copyin. */
411 if (utzp) {
412 if ((error = copyin(utzp, &atz, sizeof(atz))) != 0)
413 return (error);
414 tzp = &atz;
415 }
416
417 if (tv)
418 if ((error = settime(tv)) != 0)
419 return (error);
420 /*
421 * NetBSD has no kernel notion of time zone, and only an
422 * obsolete program would try to set it, so we log a warning.
423 */
424 if (tzp)
425 log(LOG_WARNING, "pid %d attempted to set the "
426 "(obsolete) kernel time zone\n", p->p_pid);
427 return (0);
428 }
429
430 int tickdelta; /* current clock skew, us. per tick */
431 long timedelta; /* unapplied time correction, us. */
432 long bigadj = 1000000; /* use 10x skew above bigadj us. */
433 int time_adjusted; /* set if an adjustment is made */
434
435 /* ARGSUSED */
436 int
437 sys_adjtime(struct lwp *l, void *v, register_t *retval)
438 {
439 struct sys_adjtime_args /* {
440 syscallarg(const struct timeval *) delta;
441 syscallarg(struct timeval *) olddelta;
442 } */ *uap = v;
443 struct proc *p = l->l_proc;
444 int error;
445
446 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
447 return (error);
448
449 return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), p);
450 }
451
452 int
453 adjtime1(delta, olddelta, p)
454 const struct timeval *delta;
455 struct timeval *olddelta;
456 struct proc *p;
457 {
458 struct timeval atv;
459 long ndelta, ntickdelta, odelta;
460 int error;
461 int s;
462
463 error = copyin(delta, &atv, sizeof(struct timeval));
464 if (error)
465 return (error);
466
467 /*
468 * Compute the total correction and the rate at which to apply it.
469 * Round the adjustment down to a whole multiple of the per-tick
470 * delta, so that after some number of incremental changes in
471 * hardclock(), tickdelta will become zero, lest the correction
472 * overshoot and start taking us away from the desired final time.
473 */
474 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
475 if (ndelta > bigadj || ndelta < -bigadj)
476 ntickdelta = 10 * tickadj;
477 else
478 ntickdelta = tickadj;
479 if (ndelta % ntickdelta)
480 ndelta = ndelta / ntickdelta * ntickdelta;
481
482 /*
483 * To make hardclock()'s job easier, make the per-tick delta negative
484 * if we want time to run slower; then hardclock can simply compute
485 * tick + tickdelta, and subtract tickdelta from timedelta.
486 */
487 if (ndelta < 0)
488 ntickdelta = -ntickdelta;
489 if (ndelta != 0)
490 /* We need to save the system clock time during shutdown */
491 time_adjusted |= 1;
492 s = splclock();
493 odelta = timedelta;
494 timedelta = ndelta;
495 tickdelta = ntickdelta;
496 splx(s);
497
498 if (olddelta) {
499 atv.tv_sec = odelta / 1000000;
500 atv.tv_usec = odelta % 1000000;
501 error = copyout(&atv, olddelta, sizeof(struct timeval));
502 }
503 return error;
504 }
505
506 /*
507 * Interval timer support. Both the BSD getitimer() family and the POSIX
508 * timer_*() family of routines are supported.
509 *
510 * All timers are kept in an array pointed to by p_timers, which is
511 * allocated on demand - many processes don't use timers at all. The
512 * first three elements in this array are reserved for the BSD timers:
513 * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
514 * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
515 * syscall.
516 *
517 * Realtime timers are kept in the ptimer structure as an absolute
518 * time; virtual time timers are kept as a linked list of deltas.
519 * Virtual time timers are processed in the hardclock() routine of
520 * kern_clock.c. The real time timer is processed by a callout
521 * routine, called from the softclock() routine. Since a callout may
522 * be delayed in real time due to interrupt processing in the system,
523 * it is possible for the real time timeout routine (realtimeexpire,
524 * given below), to be delayed in real time past when it is supposed
525 * to occur. It does not suffice, therefore, to reload the real timer
526 * .it_value from the real time timers .it_interval. Rather, we
527 * compute the next time in absolute time the timer should go off. */
528
529 /* Allocate a POSIX realtime timer. */
530 int
531 sys_timer_create(struct lwp *l, void *v, register_t *retval)
532 {
533 struct sys_timer_create_args /* {
534 syscallarg(clockid_t) clock_id;
535 syscallarg(struct sigevent *) evp;
536 syscallarg(timer_t *) timerid;
537 } */ *uap = v;
538 struct proc *p = l->l_proc;
539 clockid_t id;
540 struct sigevent *evp;
541 struct ptimer *pt;
542 timer_t timerid;
543 int error;
544
545 id = SCARG(uap, clock_id);
546 if (id < CLOCK_REALTIME ||
547 id > CLOCK_PROF)
548 return (EINVAL);
549
550 if (p->p_timers == NULL)
551 timers_alloc(p);
552
553 /* Find a free timer slot, skipping those reserved for setitimer(). */
554 for (timerid = 3; timerid < TIMER_MAX; timerid++)
555 if (p->p_timers->pts_timers[timerid] == NULL)
556 break;
557
558 if (timerid == TIMER_MAX)
559 return EAGAIN;
560
561 pt = pool_get(&ptimer_pool, PR_WAITOK);
562 evp = SCARG(uap, evp);
563 if (evp) {
564 if (((error =
565 copyin(evp, &pt->pt_ev, sizeof (pt->pt_ev))) != 0) ||
566 ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
567 (pt->pt_ev.sigev_notify > SIGEV_SA))) {
568 pool_put(&ptimer_pool, pt);
569 return (error ? error : EINVAL);
570 }
571 } else {
572 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
573 switch (id) {
574 case CLOCK_REALTIME:
575 pt->pt_ev.sigev_signo = SIGALRM;
576 break;
577 case CLOCK_VIRTUAL:
578 pt->pt_ev.sigev_signo = SIGVTALRM;
579 break;
580 case CLOCK_PROF:
581 pt->pt_ev.sigev_signo = SIGPROF;
582 break;
583 }
584 pt->pt_ev.sigev_value.sival_int = timerid;
585 }
586 pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
587 pt->pt_info.ksi_errno = 0;
588 pt->pt_info.ksi_code = 0;
589 pt->pt_info.ksi_pid = p->p_pid;
590 pt->pt_info.ksi_uid = p->p_cred->p_ruid;
591 pt->pt_info.ksi_sigval = pt->pt_ev.sigev_value;
592
593 pt->pt_type = id;
594 pt->pt_proc = p;
595 pt->pt_overruns = 0;
596 pt->pt_poverruns = 0;
597 pt->pt_entry = timerid;
598 timerclear(&pt->pt_time.it_value);
599 if (id == CLOCK_REALTIME)
600 callout_init(&pt->pt_ch);
601 else
602 pt->pt_active = 0;
603
604 p->p_timers->pts_timers[timerid] = pt;
605
606 return copyout(&timerid, SCARG(uap, timerid), sizeof(timerid));
607 }
608
609
610 /* Delete a POSIX realtime timer */
611 int
612 sys_timer_delete(struct lwp *l, void *v, register_t *retval)
613 {
614 struct sys_timer_delete_args /* {
615 syscallarg(timer_t) timerid;
616 } */ *uap = v;
617 struct proc *p = l->l_proc;
618 timer_t timerid;
619 struct ptimer *pt, *ptn;
620 int s;
621
622 timerid = SCARG(uap, timerid);
623
624 if ((p->p_timers == NULL) ||
625 (timerid < 2) || (timerid >= TIMER_MAX) ||
626 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
627 return (EINVAL);
628
629 if (pt->pt_type == CLOCK_REALTIME)
630 callout_stop(&pt->pt_ch);
631 else if (pt->pt_active) {
632 s = splclock();
633 ptn = LIST_NEXT(pt, pt_list);
634 LIST_REMOVE(pt, pt_list);
635 for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
636 timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
637 &ptn->pt_time.it_value);
638 splx(s);
639 }
640
641 p->p_timers->pts_timers[timerid] = NULL;
642 pool_put(&ptimer_pool, pt);
643
644 return (0);
645 }
646
647 /*
648 * Set up the given timer. The value in pt->pt_time.it_value is taken
649 * to be an absolute time for CLOCK_REALTIME timers and a relative
650 * time for virtual timers.
651 * Must be called at splclock().
652 */
653 void
654 timer_settime(struct ptimer *pt)
655 {
656 struct ptimer *ptn, *pptn;
657 struct ptlist *ptl;
658
659 if (pt->pt_type == CLOCK_REALTIME) {
660 callout_stop(&pt->pt_ch);
661 if (timerisset(&pt->pt_time.it_value)) {
662 /*
663 * Don't need to check hzto() return value, here.
664 * callout_reset() does it for us.
665 */
666 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
667 realtimerexpire, pt);
668 }
669 } else {
670 if (pt->pt_active) {
671 ptn = LIST_NEXT(pt, pt_list);
672 LIST_REMOVE(pt, pt_list);
673 for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
674 timeradd(&pt->pt_time.it_value,
675 &ptn->pt_time.it_value,
676 &ptn->pt_time.it_value);
677 }
678 if (timerisset(&pt->pt_time.it_value)) {
679 if (pt->pt_type == CLOCK_VIRTUAL)
680 ptl = &pt->pt_proc->p_timers->pts_virtual;
681 else
682 ptl = &pt->pt_proc->p_timers->pts_prof;
683
684 for (ptn = LIST_FIRST(ptl), pptn = NULL;
685 ptn && timercmp(&pt->pt_time.it_value,
686 &ptn->pt_time.it_value, >);
687 pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
688 timersub(&pt->pt_time.it_value,
689 &ptn->pt_time.it_value,
690 &pt->pt_time.it_value);
691
692 if (pptn)
693 LIST_INSERT_AFTER(pptn, pt, pt_list);
694 else
695 LIST_INSERT_HEAD(ptl, pt, pt_list);
696
697 for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
698 timersub(&ptn->pt_time.it_value,
699 &pt->pt_time.it_value,
700 &ptn->pt_time.it_value);
701
702 pt->pt_active = 1;
703 } else
704 pt->pt_active = 0;
705 }
706 }
707
708 void
709 timer_gettime(struct ptimer *pt, struct itimerval *aitv)
710 {
711 struct ptimer *ptn;
712
713 *aitv = pt->pt_time;
714 if (pt->pt_type == CLOCK_REALTIME) {
715 /*
716 * Convert from absolute to relative time in .it_value
717 * part of real time timer. If time for real time
718 * timer has passed return 0, else return difference
719 * between current time and time for the timer to go
720 * off.
721 */
722 if (timerisset(&aitv->it_value)) {
723 if (timercmp(&aitv->it_value, &time, <))
724 timerclear(&aitv->it_value);
725 else
726 timersub(&aitv->it_value, &time,
727 &aitv->it_value);
728 }
729 } else if (pt->pt_active) {
730 if (pt->pt_type == CLOCK_VIRTUAL)
731 ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
732 else
733 ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
734 for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
735 timeradd(&aitv->it_value,
736 &ptn->pt_time.it_value, &aitv->it_value);
737 KASSERT(ptn != NULL); /* pt should be findable on the list */
738 } else
739 timerclear(&aitv->it_value);
740 }
741
742
743
744 /* Set and arm a POSIX realtime timer */
745 int
746 sys_timer_settime(struct lwp *l, void *v, register_t *retval)
747 {
748 struct sys_timer_settime_args /* {
749 syscallarg(timer_t) timerid;
750 syscallarg(int) flags;
751 syscallarg(const struct itimerspec *) value;
752 syscallarg(struct itimerspec *) ovalue;
753 } */ *uap = v;
754 struct proc *p = l->l_proc;
755 int error, s, timerid;
756 struct itimerval val, oval;
757 struct itimerspec value, ovalue;
758 struct ptimer *pt;
759
760 timerid = SCARG(uap, timerid);
761
762 if ((p->p_timers == NULL) ||
763 (timerid < 2) || (timerid >= TIMER_MAX) ||
764 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
765 return (EINVAL);
766
767 if ((error = copyin(SCARG(uap, value), &value,
768 sizeof(struct itimerspec))) != 0)
769 return (error);
770
771 TIMESPEC_TO_TIMEVAL(&val.it_value, &value.it_value);
772 TIMESPEC_TO_TIMEVAL(&val.it_interval, &value.it_interval);
773 if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
774 return (EINVAL);
775
776 oval = pt->pt_time;
777 pt->pt_time = val;
778
779 s = splclock();
780 /*
781 * If we've been passed a relative time for a realtime timer,
782 * convert it to absolute; if an absolute time for a virtual
783 * timer, convert it to relative and make sure we don't set it
784 * to zero, which would cancel the timer, or let it go
785 * negative, which would confuse the comparison tests.
786 */
787 if (timerisset(&pt->pt_time.it_value)) {
788 if (pt->pt_type == CLOCK_REALTIME) {
789 if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0)
790 timeradd(&pt->pt_time.it_value, &time,
791 &pt->pt_time.it_value);
792 } else {
793 if ((SCARG(uap, flags) & TIMER_ABSTIME) != 0) {
794 timersub(&pt->pt_time.it_value, &time,
795 &pt->pt_time.it_value);
796 if (!timerisset(&pt->pt_time.it_value) ||
797 pt->pt_time.it_value.tv_sec < 0) {
798 pt->pt_time.it_value.tv_sec = 0;
799 pt->pt_time.it_value.tv_usec = 1;
800 }
801 }
802 }
803 }
804
805 timer_settime(pt);
806 splx(s);
807
808 if (SCARG(uap, ovalue)) {
809 TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue.it_value);
810 TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue.it_interval);
811 return copyout(&ovalue, SCARG(uap, ovalue),
812 sizeof(struct itimerspec));
813 }
814
815 return (0);
816 }
817
818 /* Return the time remaining until a POSIX timer fires. */
819 int
820 sys_timer_gettime(struct lwp *l, void *v, register_t *retval)
821 {
822 struct sys_timer_gettime_args /* {
823 syscallarg(timer_t) timerid;
824 syscallarg(struct itimerspec *) value;
825 } */ *uap = v;
826 struct itimerval aitv;
827 struct itimerspec its;
828 struct proc *p = l->l_proc;
829 int s, timerid;
830 struct ptimer *pt;
831
832 timerid = SCARG(uap, timerid);
833
834 if ((p->p_timers == NULL) ||
835 (timerid < 2) || (timerid >= TIMER_MAX) ||
836 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
837 return (EINVAL);
838
839 s = splclock();
840 timer_gettime(pt, &aitv);
841 splx(s);
842
843 TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its.it_interval);
844 TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its.it_value);
845
846 return copyout(&its, SCARG(uap, value), sizeof(its));
847 }
848
849 /*
850 * Return the count of the number of times a periodic timer expired
851 * while a notification was already pending. The counter is reset when
852 * a timer expires and a notification can be posted.
853 */
854 int
855 sys_timer_getoverrun(struct lwp *l, void *v, register_t *retval)
856 {
857 struct sys_timer_getoverrun_args /* {
858 syscallarg(timer_t) timerid;
859 } */ *uap = v;
860 struct proc *p = l->l_proc;
861 int timerid;
862 struct ptimer *pt;
863
864 timerid = SCARG(uap, timerid);
865
866 if ((p->p_timers == NULL) ||
867 (timerid < 2) || (timerid >= TIMER_MAX) ||
868 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
869 return (EINVAL);
870
871 *retval = pt->pt_poverruns;
872
873 return (0);
874 }
875
876 /* Glue function that triggers an upcall; called from userret(). */
877 static void
878 timerupcall(struct lwp *l, void *arg)
879 {
880 struct ptimers *pt = (struct ptimers *)arg;
881 unsigned int i, fired, done;
882
883 KDASSERT(l->l_proc->p_sa);
884 /* Bail out if we do not own the virtual processor */
885 if (l->l_savp->savp_lwp != l)
886 return ;
887
888 KERNEL_PROC_LOCK(l);
889
890 fired = pt->pts_fired;
891 done = 0;
892 while ((i = ffs(fired)) != 0) {
893 siginfo_t *si;
894 int mask = 1 << --i;
895 int f;
896
897 f = l->l_flag & L_SA;
898 l->l_flag &= ~L_SA;
899 si = siginfo_alloc(PR_WAITOK);
900 si->_info = pt->pts_timers[i]->pt_info.ksi_info;
901 if (sa_upcall(l, SA_UPCALL_SIGEV | SA_UPCALL_DEFER, NULL, l,
902 sizeof(*si), si, siginfo_free) != 0) {
903 siginfo_free(si);
904 /* XXX What do we do here?? */
905 } else
906 done |= mask;
907 fired &= ~mask;
908 l->l_flag |= f;
909 }
910 pt->pts_fired &= ~done;
911 if (pt->pts_fired == 0)
912 l->l_proc->p_userret = NULL;
913
914 KERNEL_PROC_UNLOCK(l);
915 }
916
917
918 /*
919 * Real interval timer expired:
920 * send process whose timer expired an alarm signal.
921 * If time is not set up to reload, then just return.
922 * Else compute next time timer should go off which is > current time.
923 * This is where delay in processing this timeout causes multiple
924 * SIGALRM calls to be compressed into one.
925 */
926 void
927 realtimerexpire(void *arg)
928 {
929 struct ptimer *pt;
930 int s;
931
932 pt = (struct ptimer *)arg;
933
934 itimerfire(pt);
935
936 if (!timerisset(&pt->pt_time.it_interval)) {
937 timerclear(&pt->pt_time.it_value);
938 return;
939 }
940 for (;;) {
941 s = splclock();
942 timeradd(&pt->pt_time.it_value,
943 &pt->pt_time.it_interval, &pt->pt_time.it_value);
944 if (timercmp(&pt->pt_time.it_value, &time, >)) {
945 /*
946 * Don't need to check hzto() return value, here.
947 * callout_reset() does it for us.
948 */
949 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
950 realtimerexpire, pt);
951 splx(s);
952 return;
953 }
954 splx(s);
955 pt->pt_overruns++;
956 }
957 }
958
959 /* BSD routine to get the value of an interval timer. */
960 /* ARGSUSED */
961 int
962 sys_getitimer(struct lwp *l, void *v, register_t *retval)
963 {
964 struct sys_getitimer_args /* {
965 syscallarg(int) which;
966 syscallarg(struct itimerval *) itv;
967 } */ *uap = v;
968 struct proc *p = l->l_proc;
969 struct itimerval aitv;
970 int s, which;
971
972 which = SCARG(uap, which);
973
974 if ((u_int)which > ITIMER_PROF)
975 return (EINVAL);
976
977 if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
978 timerclear(&aitv.it_value);
979 timerclear(&aitv.it_interval);
980 } else {
981 s = splclock();
982 timer_gettime(p->p_timers->pts_timers[which], &aitv);
983 splx(s);
984 }
985
986 return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
987
988 }
989
990 /* BSD routine to set/arm an interval timer. */
991 /* ARGSUSED */
992 int
993 sys_setitimer(struct lwp *l, void *v, register_t *retval)
994 {
995 struct sys_setitimer_args /* {
996 syscallarg(int) which;
997 syscallarg(const struct itimerval *) itv;
998 syscallarg(struct itimerval *) oitv;
999 } */ *uap = v;
1000 struct proc *p = l->l_proc;
1001 int which = SCARG(uap, which);
1002 struct sys_getitimer_args getargs;
1003 struct itimerval aitv;
1004 const struct itimerval *itvp;
1005 struct ptimer *pt;
1006 int s, error;
1007
1008 if ((u_int)which > ITIMER_PROF)
1009 return (EINVAL);
1010 itvp = SCARG(uap, itv);
1011 if (itvp &&
1012 (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
1013 return (error);
1014 if (SCARG(uap, oitv) != NULL) {
1015 SCARG(&getargs, which) = which;
1016 SCARG(&getargs, itv) = SCARG(uap, oitv);
1017 if ((error = sys_getitimer(l, &getargs, retval)) != 0)
1018 return (error);
1019 }
1020 if (itvp == 0)
1021 return (0);
1022 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
1023 return (EINVAL);
1024
1025 /*
1026 * Don't bother allocating data structures if the process just
1027 * wants to clear the timer.
1028 */
1029 if (!timerisset(&aitv.it_value) &&
1030 ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
1031 return (0);
1032
1033 if (p->p_timers == NULL)
1034 timers_alloc(p);
1035 if (p->p_timers->pts_timers[which] == NULL) {
1036 pt = pool_get(&ptimer_pool, PR_WAITOK);
1037 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1038 pt->pt_ev.sigev_value.sival_int = which;
1039 pt->pt_overruns = 0;
1040 pt->pt_proc = p;
1041 pt->pt_type = which;
1042 pt->pt_entry = which;
1043 switch (which) {
1044 case ITIMER_REAL:
1045 callout_init(&pt->pt_ch);
1046 pt->pt_ev.sigev_signo = SIGALRM;
1047 break;
1048 case ITIMER_VIRTUAL:
1049 pt->pt_active = 0;
1050 pt->pt_ev.sigev_signo = SIGVTALRM;
1051 break;
1052 case ITIMER_PROF:
1053 pt->pt_active = 0;
1054 pt->pt_ev.sigev_signo = SIGPROF;
1055 break;
1056 }
1057 } else
1058 pt = p->p_timers->pts_timers[which];
1059
1060 pt->pt_time = aitv;
1061 p->p_timers->pts_timers[which] = pt;
1062
1063 s = splclock();
1064 if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
1065 /* Convert to absolute time */
1066 timeradd(&pt->pt_time.it_value, &time, &pt->pt_time.it_value);
1067 }
1068 timer_settime(pt);
1069 splx(s);
1070
1071 return (0);
1072 }
1073
1074 /* Utility routines to manage the array of pointers to timers. */
1075 void
1076 timers_alloc(struct proc *p)
1077 {
1078 int i;
1079 struct ptimers *pts;
1080
1081 pts = malloc(sizeof (struct ptimers), M_SUBPROC, 0);
1082 LIST_INIT(&pts->pts_virtual);
1083 LIST_INIT(&pts->pts_prof);
1084 for (i = 0; i < TIMER_MAX; i++)
1085 pts->pts_timers[i] = NULL;
1086 pts->pts_fired = 0;
1087 p->p_timers = pts;
1088 }
1089
1090 /*
1091 * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1092 * then clean up all timers and free all the data structures. If
1093 * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1094 * by timer_create(), not the BSD setitimer() timers, and only free the
1095 * structure if none of those remain.
1096 */
1097 void
1098 timers_free(struct proc *p, int which)
1099 {
1100 int i, s;
1101 struct ptimers *pts;
1102 struct ptimer *pt, *ptn;
1103 struct timeval tv;
1104
1105 if (p->p_timers) {
1106 pts = p->p_timers;
1107 if (which == TIMERS_ALL)
1108 i = 0;
1109 else {
1110 s = splclock();
1111 timerclear(&tv);
1112 for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
1113 ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1114 ptn = LIST_NEXT(ptn, pt_list))
1115 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1116 LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
1117 if (ptn) {
1118 timeradd(&tv, &ptn->pt_time.it_value,
1119 &ptn->pt_time.it_value);
1120 LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
1121 ptn, pt_list);
1122 }
1123
1124 timerclear(&tv);
1125 for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
1126 ptn && ptn != pts->pts_timers[ITIMER_PROF];
1127 ptn = LIST_NEXT(ptn, pt_list))
1128 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1129 LIST_FIRST(&p->p_timers->pts_prof) = NULL;
1130 if (ptn) {
1131 timeradd(&tv, &ptn->pt_time.it_value,
1132 &ptn->pt_time.it_value);
1133 LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
1134 pt_list);
1135 }
1136 splx(s);
1137 i = 3;
1138 }
1139 for ( ; i < TIMER_MAX; i++)
1140 if ((pt = pts->pts_timers[i]) != NULL) {
1141 if (pt->pt_type == CLOCK_REALTIME)
1142 callout_stop(&pt->pt_ch);
1143 pts->pts_timers[i] = NULL;
1144 pool_put(&ptimer_pool, pt);
1145 }
1146 if ((pts->pts_timers[0] == NULL) &&
1147 (pts->pts_timers[1] == NULL) &&
1148 (pts->pts_timers[2] == NULL)) {
1149 p->p_timers = NULL;
1150 free(pts, M_SUBPROC);
1151 }
1152 }
1153 }
1154
1155 /*
1156 * Check that a proposed value to load into the .it_value or
1157 * .it_interval part of an interval timer is acceptable, and
1158 * fix it to have at least minimal value (i.e. if it is less
1159 * than the resolution of the clock, round it up.)
1160 */
1161 int
1162 itimerfix(struct timeval *tv)
1163 {
1164
1165 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
1166 return (EINVAL);
1167 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
1168 tv->tv_usec = tick;
1169 return (0);
1170 }
1171
1172 /*
1173 * Decrement an interval timer by a specified number
1174 * of microseconds, which must be less than a second,
1175 * i.e. < 1000000. If the timer expires, then reload
1176 * it. In this case, carry over (usec - old value) to
1177 * reduce the value reloaded into the timer so that
1178 * the timer does not drift. This routine assumes
1179 * that it is called in a context where the timers
1180 * on which it is operating cannot change in value.
1181 */
1182 int
1183 itimerdecr(struct ptimer *pt, int usec)
1184 {
1185 struct itimerval *itp;
1186
1187 itp = &pt->pt_time;
1188 if (itp->it_value.tv_usec < usec) {
1189 if (itp->it_value.tv_sec == 0) {
1190 /* expired, and already in next interval */
1191 usec -= itp->it_value.tv_usec;
1192 goto expire;
1193 }
1194 itp->it_value.tv_usec += 1000000;
1195 itp->it_value.tv_sec--;
1196 }
1197 itp->it_value.tv_usec -= usec;
1198 usec = 0;
1199 if (timerisset(&itp->it_value))
1200 return (1);
1201 /* expired, exactly at end of interval */
1202 expire:
1203 if (timerisset(&itp->it_interval)) {
1204 itp->it_value = itp->it_interval;
1205 itp->it_value.tv_usec -= usec;
1206 if (itp->it_value.tv_usec < 0) {
1207 itp->it_value.tv_usec += 1000000;
1208 itp->it_value.tv_sec--;
1209 }
1210 timer_settime(pt);
1211 } else
1212 itp->it_value.tv_usec = 0; /* sec is already 0 */
1213 return (0);
1214 }
1215
1216 void
1217 itimerfire(struct ptimer *pt)
1218 {
1219 struct proc *p = pt->pt_proc;
1220 struct sadata_vp *vp;
1221 int s;
1222 unsigned int i;
1223
1224 if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
1225 /*
1226 * No RT signal infrastructure exists at this time;
1227 * just post the signal number and throw away the
1228 * value.
1229 */
1230 if (sigismember(&p->p_sigctx.ps_siglist, pt->pt_ev.sigev_signo))
1231 pt->pt_overruns++;
1232 else {
1233 ksiginfo_t ksi;
1234 (void)memset(&ksi, 0, sizeof(ksi));
1235 ksi.ksi_signo = pt->pt_ev.sigev_signo;
1236 ksi.ksi_code = SI_TIMER;
1237 ksi.ksi_sigval = pt->pt_ev.sigev_value;
1238 pt->pt_poverruns = pt->pt_overruns;
1239 pt->pt_overruns = 0;
1240 kpsignal(p, &ksi, NULL);
1241 }
1242 } else if (pt->pt_ev.sigev_notify == SIGEV_SA && (p->p_flag & P_SA)) {
1243 /* Cause the process to generate an upcall when it returns. */
1244
1245 if (p->p_userret == NULL) {
1246 /*
1247 * XXX stop signals can be processed inside tsleep,
1248 * which can be inside sa_yield's inner loop, which
1249 * makes testing for sa_idle alone insuffucent to
1250 * determine if we really should call setrunnable.
1251 */
1252 pt->pt_poverruns = pt->pt_overruns;
1253 pt->pt_overruns = 0;
1254 i = 1 << pt->pt_entry;
1255 p->p_timers->pts_fired = i;
1256 p->p_userret = timerupcall;
1257 p->p_userret_arg = p->p_timers;
1258
1259 SCHED_LOCK(s);
1260 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1261 if (vp->savp_lwp->l_flag & L_SA_IDLE) {
1262 vp->savp_lwp->l_flag &= ~L_SA_IDLE;
1263 sched_wakeup(vp->savp_lwp);
1264 break;
1265 }
1266 }
1267 SCHED_UNLOCK(s);
1268 } else if (p->p_userret == timerupcall) {
1269 i = 1 << pt->pt_entry;
1270 if ((p->p_timers->pts_fired & i) == 0) {
1271 pt->pt_poverruns = pt->pt_overruns;
1272 pt->pt_overruns = 0;
1273 p->p_timers->pts_fired |= i;
1274 } else
1275 pt->pt_overruns++;
1276 } else {
1277 pt->pt_overruns++;
1278 if ((p->p_flag & P_WEXIT) == 0)
1279 printf("itimerfire(%d): overrun %d on timer %x (userret is %p)\n",
1280 p->p_pid, pt->pt_overruns,
1281 pt->pt_ev.sigev_value.sival_int,
1282 p->p_userret);
1283 }
1284 }
1285
1286 }
1287
1288 /*
1289 * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
1290 * for usage and rationale.
1291 */
1292 int
1293 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1294 {
1295 struct timeval tv, delta;
1296 int s, rv = 0;
1297
1298 s = splclock();
1299 tv = mono_time;
1300 splx(s);
1301
1302 timersub(&tv, lasttime, &delta);
1303
1304 /*
1305 * check for 0,0 is so that the message will be seen at least once,
1306 * even if interval is huge.
1307 */
1308 if (timercmp(&delta, mininterval, >=) ||
1309 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1310 *lasttime = tv;
1311 rv = 1;
1312 }
1313
1314 return (rv);
1315 }
1316
1317 /*
1318 * ppsratecheck(): packets (or events) per second limitation.
1319 */
1320 int
1321 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1322 {
1323 struct timeval tv, delta;
1324 int s, rv;
1325
1326 s = splclock();
1327 tv = mono_time;
1328 splx(s);
1329
1330 timersub(&tv, lasttime, &delta);
1331
1332 /*
1333 * check for 0,0 is so that the message will be seen at least once.
1334 * if more than one second have passed since the last update of
1335 * lasttime, reset the counter.
1336 *
1337 * we do increment *curpps even in *curpps < maxpps case, as some may
1338 * try to use *curpps for stat purposes as well.
1339 */
1340 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
1341 delta.tv_sec >= 1) {
1342 *lasttime = tv;
1343 *curpps = 0;
1344 }
1345 if (maxpps < 0)
1346 rv = 1;
1347 else if (*curpps < maxpps)
1348 rv = 1;
1349 else
1350 rv = 0;
1351
1352 #if 1 /*DIAGNOSTIC?*/
1353 /* be careful about wrap-around */
1354 if (*curpps + 1 > *curpps)
1355 *curpps = *curpps + 1;
1356 #else
1357 /*
1358 * assume that there's not too many calls to this function.
1359 * not sure if the assumption holds, as it depends on *caller's*
1360 * behavior, not the behavior of this function.
1361 * IMHO it is wrong to make assumption on the caller's behavior,
1362 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1363 */
1364 *curpps = *curpps + 1;
1365 #endif
1366
1367 return (rv);
1368 }
Cache object: b1409a0b402e22c3cb79a603830f3e11
|