kern_time.c revision 1.146.2.1 1 /* $NetBSD: kern_time.c,v 1.146.2.1 2008/05/10 23:49:04 wrstuden Exp $ */
2
3 /*-
4 * Copyright (c) 2000, 2004, 2005, 2007, 2008 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.146.2.1 2008/05/10 23:49:04 wrstuden Exp $");
65
66 #include <sys/param.h>
67 #include <sys/resourcevar.h>
68 #include <sys/kernel.h>
69 #include <sys/systm.h>
70 #include <sys/proc.h>
71 #include <sys/vnode.h>
72 #include <sys/signalvar.h>
73 #include <sys/syslog.h>
74 #include <sys/timetc.h>
75 #include <sys/timex.h>
76 #include <sys/kauth.h>
77 #include <sys/mount.h>
78 #include <sys/sa.h>
79 #include <sys/syscallargs.h>
80 #include <sys/cpu.h>
81
82 #include <uvm/uvm_extern.h>
83
84 static void timer_intr(void *);
85 static void itimerfire(struct ptimer *);
86 static void itimerfree(struct ptimers *, int);
87
88 kmutex_t time_lock;
89 kmutex_t timer_lock;
90
91 static void *timer_sih;
92 static TAILQ_HEAD(, ptimer) timer_queue;
93
94 POOL_INIT(ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
95 &pool_allocator_nointr, IPL_NONE);
96 POOL_INIT(ptimers_pool, sizeof(struct ptimers), 0, 0, 0, "ptimerspl",
97 &pool_allocator_nointr, IPL_NONE);
98
99 /*
100 * Initialize timekeeping.
101 */
102 void
103 time_init(void)
104 {
105
106 mutex_init(&time_lock, MUTEX_DEFAULT, IPL_NONE);
107 }
108
109 void
110 time_init2(void)
111 {
112
113 TAILQ_INIT(&timer_queue);
114 mutex_init(&timer_lock, MUTEX_DEFAULT, IPL_SCHED);
115 timer_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
116 timer_intr, NULL);
117 }
118
119 /* Time of day and interval timer support.
120 *
121 * These routines provide the kernel entry points to get and set
122 * the time-of-day and per-process interval timers. Subroutines
123 * here provide support for adding and subtracting timeval structures
124 * and decrementing interval timers, optionally reloading the interval
125 * timers when they expire.
126 */
127
128 /* This function is used by clock_settime and settimeofday */
129 static int
130 settime1(struct proc *p, struct timespec *ts, bool check_kauth)
131 {
132 struct timeval delta, tv;
133 struct timeval now;
134 struct timespec ts1;
135 struct bintime btdelta;
136 lwp_t *l;
137 int s;
138
139 TIMESPEC_TO_TIMEVAL(&tv, ts);
140
141 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
142 s = splclock();
143 microtime(&now);
144 timersub(&tv, &now, &delta);
145
146 if (check_kauth && kauth_authorize_system(kauth_cred_get(),
147 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, ts, &delta,
148 KAUTH_ARG(check_kauth ? false : true)) != 0) {
149 splx(s);
150 return (EPERM);
151 }
152
153 #ifdef notyet
154 if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
155 splx(s);
156 return (EPERM);
157 }
158 #endif
159
160 TIMEVAL_TO_TIMESPEC(&tv, &ts1);
161 tc_setclock(&ts1);
162
163 timeradd(&boottime, &delta, &boottime);
164
165 /*
166 * XXXSMP: There is a short race between setting the time above
167 * and adjusting LWP's run times. Fixing this properly means
168 * pausing all CPUs while we adjust the clock.
169 */
170 timeval2bintime(&delta, &btdelta);
171 mutex_enter(proc_lock);
172 LIST_FOREACH(l, &alllwp, l_list) {
173 lwp_lock(l);
174 bintime_add(&l->l_stime, &btdelta);
175 lwp_unlock(l);
176 }
177 mutex_exit(proc_lock);
178 resettodr();
179 splx(s);
180
181 return (0);
182 }
183
184 int
185 settime(struct proc *p, struct timespec *ts)
186 {
187 return (settime1(p, ts, true));
188 }
189
190 /* ARGSUSED */
191 int
192 sys_clock_gettime(struct lwp *l, const struct sys_clock_gettime_args *uap,
193 register_t *retval)
194 {
195 /* {
196 syscallarg(clockid_t) clock_id;
197 syscallarg(struct timespec *) tp;
198 } */
199 clockid_t clock_id;
200 struct timespec ats;
201
202 clock_id = SCARG(uap, clock_id);
203 switch (clock_id) {
204 case CLOCK_REALTIME:
205 nanotime(&ats);
206 break;
207 case CLOCK_MONOTONIC:
208 nanouptime(&ats);
209 break;
210 default:
211 return (EINVAL);
212 }
213
214 return copyout(&ats, SCARG(uap, tp), sizeof(ats));
215 }
216
217 /* ARGSUSED */
218 int
219 sys_clock_settime(struct lwp *l, const struct sys_clock_settime_args *uap,
220 register_t *retval)
221 {
222 /* {
223 syscallarg(clockid_t) clock_id;
224 syscallarg(const struct timespec *) tp;
225 } */
226
227 return clock_settime1(l->l_proc, SCARG(uap, clock_id), SCARG(uap, tp),
228 true);
229 }
230
231
232 int
233 clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp,
234 bool check_kauth)
235 {
236 struct timespec ats;
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 if ((error = settime1(p, &ats, check_kauth)) != 0)
245 return (error);
246 break;
247 case CLOCK_MONOTONIC:
248 return (EINVAL); /* read-only clock */
249 default:
250 return (EINVAL);
251 }
252
253 return 0;
254 }
255
256 int
257 sys_clock_getres(struct lwp *l, const struct sys_clock_getres_args *uap,
258 register_t *retval)
259 {
260 /* {
261 syscallarg(clockid_t) clock_id;
262 syscallarg(struct timespec *) tp;
263 } */
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 if (tc_getfrequency() > 1000000000)
274 ts.tv_nsec = 1;
275 else
276 ts.tv_nsec = 1000000000 / tc_getfrequency();
277 break;
278 default:
279 return (EINVAL);
280 }
281
282 if (SCARG(uap, tp))
283 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
284
285 return error;
286 }
287
288 /* ARGSUSED */
289 int
290 sys_nanosleep(struct lwp *l, const struct sys_nanosleep_args *uap,
291 register_t *retval)
292 {
293 /* {
294 syscallarg(struct timespec *) rqtp;
295 syscallarg(struct timespec *) rmtp;
296 } */
297 struct timespec rmt, rqt;
298 int error, error1;
299
300 error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
301 if (error)
302 return (error);
303
304 error = nanosleep1(l, &rqt, SCARG(uap, rmtp) ? &rmt : NULL);
305 if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
306 return error;
307
308 error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
309 return error1 ? error1 : error;
310 }
311
312 int
313 nanosleep1(struct lwp *l, struct timespec *rqt, struct timespec *rmt)
314 {
315 struct timespec rmtstart;
316 int error, timo;
317
318 if (itimespecfix(rqt))
319 return (EINVAL);
320
321 timo = tstohz(rqt);
322 /*
323 * Avoid inadvertantly sleeping forever
324 */
325 if (timo == 0)
326 timo = 1;
327 getnanouptime(&rmtstart);
328 again:
329 error = kpause("nanoslp", true, timo, NULL);
330 if (rmt != NULL || error == 0) {
331 struct timespec rmtend;
332 struct timespec t0;
333 struct timespec *t;
334
335 getnanouptime(&rmtend);
336 t = (rmt != NULL) ? rmt : &t0;
337 timespecsub(&rmtend, &rmtstart, t);
338 timespecsub(rqt, t, t);
339 if (t->tv_sec < 0)
340 timespecclear(t);
341 if (error == 0) {
342 timo = tstohz(t);
343 if (timo > 0)
344 goto again;
345 }
346 }
347
348 if (error == ERESTART)
349 error = EINTR;
350 if (error == EWOULDBLOCK)
351 error = 0;
352
353 return error;
354 }
355
356 /* ARGSUSED */
357 int
358 sys_gettimeofday(struct lwp *l, const struct sys_gettimeofday_args *uap,
359 register_t *retval)
360 {
361 /* {
362 syscallarg(struct timeval *) tp;
363 syscallarg(void *) tzp; really "struct timezone *";
364 } */
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(struct lwp *l, const struct sys_settimeofday_args *uap,
390 register_t *retval)
391 {
392 /* {
393 syscallarg(const struct timeval *) tv;
394 syscallarg(const void *) tzp; really "const struct timezone *";
395 } */
396
397 return settimeofday1(SCARG(uap, tv), true, SCARG(uap, tzp), l, true);
398 }
399
400 int
401 settimeofday1(const struct timeval *utv, bool userspace,
402 const void *utzp, struct lwp *l, bool check_kauth)
403 {
404 struct timeval atv;
405 struct timespec ts;
406 int error;
407
408 /* Verify all parameters before changing time. */
409
410 /*
411 * NetBSD has no kernel notion of time zone, and only an
412 * obsolete program would try to set it, so we log a warning.
413 */
414 if (utzp)
415 log(LOG_WARNING, "pid %d attempted to set the "
416 "(obsolete) kernel time zone\n", l->l_proc->p_pid);
417
418 if (utv == NULL)
419 return 0;
420
421 if (userspace) {
422 if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
423 return error;
424 utv = &atv;
425 }
426
427 TIMEVAL_TO_TIMESPEC(utv, &ts);
428 return settime1(l->l_proc, &ts, check_kauth);
429 }
430
431 int time_adjusted; /* set if an adjustment is made */
432
433 /* ARGSUSED */
434 int
435 sys_adjtime(struct lwp *l, const struct sys_adjtime_args *uap,
436 register_t *retval)
437 {
438 /* {
439 syscallarg(const struct timeval *) delta;
440 syscallarg(struct timeval *) olddelta;
441 } */
442 int error;
443
444 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
445 KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
446 return (error);
447
448 return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), l->l_proc);
449 }
450
451 int
452 adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
453 {
454 struct timeval atv;
455 int error = 0;
456
457 extern int64_t time_adjtime; /* in kern_ntptime.c */
458
459 if (olddelta) {
460 mutex_spin_enter(&timecounter_lock);
461 atv.tv_sec = time_adjtime / 1000000;
462 atv.tv_usec = time_adjtime % 1000000;
463 mutex_spin_exit(&timecounter_lock);
464 if (atv.tv_usec < 0) {
465 atv.tv_usec += 1000000;
466 atv.tv_sec--;
467 }
468 error = copyout(&atv, olddelta, sizeof(struct timeval));
469 if (error)
470 return (error);
471 }
472
473 if (delta) {
474 error = copyin(delta, &atv, sizeof(struct timeval));
475 if (error)
476 return (error);
477
478 mutex_spin_enter(&timecounter_lock);
479 time_adjtime = (int64_t)atv.tv_sec * 1000000 +
480 atv.tv_usec;
481 if (time_adjtime) {
482 /* We need to save the system time during shutdown */
483 time_adjusted |= 1;
484 }
485 mutex_spin_exit(&timecounter_lock);
486 }
487
488 return error;
489 }
490
491 /*
492 * Interval timer support. Both the BSD getitimer() family and the POSIX
493 * timer_*() family of routines are supported.
494 *
495 * All timers are kept in an array pointed to by p_timers, which is
496 * allocated on demand - many processes don't use timers at all. The
497 * first three elements in this array are reserved for the BSD timers:
498 * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
499 * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
500 * syscall.
501 *
502 * Realtime timers are kept in the ptimer structure as an absolute
503 * time; virtual time timers are kept as a linked list of deltas.
504 * Virtual time timers are processed in the hardclock() routine of
505 * kern_clock.c. The real time timer is processed by a callout
506 * routine, called from the softclock() routine. Since a callout may
507 * be delayed in real time due to interrupt processing in the system,
508 * it is possible for the real time timeout routine (realtimeexpire,
509 * given below), to be delayed in real time past when it is supposed
510 * to occur. It does not suffice, therefore, to reload the real timer
511 * .it_value from the real time timers .it_interval. Rather, we
512 * compute the next time in absolute time the timer should go off. */
513
514 /* Allocate a POSIX realtime timer. */
515 int
516 sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap,
517 register_t *retval)
518 {
519 /* {
520 syscallarg(clockid_t) clock_id;
521 syscallarg(struct sigevent *) evp;
522 syscallarg(timer_t *) timerid;
523 } */
524
525 return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
526 SCARG(uap, evp), copyin, l);
527 }
528
529 int
530 timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
531 copyin_t fetch_event, struct lwp *l)
532 {
533 int error;
534 timer_t timerid;
535 struct ptimers *pts;
536 struct ptimer *pt;
537 struct proc *p;
538
539 p = l->l_proc;
540
541 if (id < CLOCK_REALTIME || id > CLOCK_PROF)
542 return (EINVAL);
543
544 if ((pts = p->p_timers) == NULL)
545 pts = timers_alloc(p);
546
547 pt = pool_get(&ptimer_pool, PR_WAITOK);
548 if (evp != NULL) {
549 if (((error =
550 (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
551 ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
552 (pt->pt_ev.sigev_notify > SIGEV_SA))) {
553 pool_put(&ptimer_pool, pt);
554 return (error ? error : EINVAL);
555 }
556 }
557
558 /* Find a free timer slot, skipping those reserved for setitimer(). */
559 mutex_spin_enter(&timer_lock);
560 for (timerid = 3; timerid < TIMER_MAX; timerid++)
561 if (pts->pts_timers[timerid] == NULL)
562 break;
563 if (timerid == TIMER_MAX) {
564 mutex_spin_exit(&timer_lock);
565 pool_put(&ptimer_pool, pt);
566 return EAGAIN;
567 }
568 if (evp == NULL) {
569 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
570 switch (id) {
571 case CLOCK_REALTIME:
572 pt->pt_ev.sigev_signo = SIGALRM;
573 break;
574 case CLOCK_VIRTUAL:
575 pt->pt_ev.sigev_signo = SIGVTALRM;
576 break;
577 case CLOCK_PROF:
578 pt->pt_ev.sigev_signo = SIGPROF;
579 break;
580 }
581 pt->pt_ev.sigev_value.sival_int = timerid;
582 }
583 pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
584 pt->pt_info.ksi_errno = 0;
585 pt->pt_info.ksi_code = 0;
586 pt->pt_info.ksi_pid = p->p_pid;
587 pt->pt_info.ksi_uid = kauth_cred_getuid(l->l_cred);
588 pt->pt_info.ksi_value = pt->pt_ev.sigev_value;
589 pt->pt_type = id;
590 pt->pt_proc = p;
591 pt->pt_overruns = 0;
592 pt->pt_poverruns = 0;
593 pt->pt_entry = timerid;
594 pt->pt_queued = false;
595 pt->pt_active = 0;
596 timerclear(&pt->pt_time.it_value);
597 callout_init(&pt->pt_ch, 0);
598 pts->pts_timers[timerid] = pt;
599 mutex_spin_exit(&timer_lock);
600
601 return copyout(&timerid, tid, sizeof(timerid));
602 }
603
604 /* Delete a POSIX realtime timer */
605 int
606 sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap,
607 register_t *retval)
608 {
609 /* {
610 syscallarg(timer_t) timerid;
611 } */
612 struct proc *p = l->l_proc;
613 timer_t timerid;
614 struct ptimers *pts;
615 struct ptimer *pt, *ptn;
616
617 timerid = SCARG(uap, timerid);
618 pts = p->p_timers;
619
620 if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
621 return (EINVAL);
622
623 mutex_spin_enter(&timer_lock);
624 if ((pt = pts->pts_timers[timerid]) == NULL) {
625 mutex_spin_exit(&timer_lock);
626 return (EINVAL);
627 }
628 if (pt->pt_active) {
629 ptn = LIST_NEXT(pt, pt_list);
630 LIST_REMOVE(pt, pt_list);
631 for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
632 timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
633 &ptn->pt_time.it_value);
634 pt->pt_active = 0;
635 }
636 itimerfree(pts, timerid);
637
638 return (0);
639 }
640
641 /*
642 * Set up the given timer. The value in pt->pt_time.it_value is taken
643 * to be an absolute time for CLOCK_REALTIME timers and a relative
644 * time for virtual timers.
645 * Must be called at splclock().
646 */
647 void
648 timer_settime(struct ptimer *pt)
649 {
650 struct ptimer *ptn, *pptn;
651 struct ptlist *ptl;
652
653 KASSERT(mutex_owned(&timer_lock));
654
655 if (pt->pt_type == CLOCK_REALTIME) {
656 callout_stop(&pt->pt_ch);
657 if (timerisset(&pt->pt_time.it_value)) {
658 /*
659 * Don't need to check hzto() return value, here.
660 * callout_reset() does it for us.
661 */
662 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
663 realtimerexpire, pt);
664 }
665 } else {
666 if (pt->pt_active) {
667 ptn = LIST_NEXT(pt, pt_list);
668 LIST_REMOVE(pt, pt_list);
669 for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
670 timeradd(&pt->pt_time.it_value,
671 &ptn->pt_time.it_value,
672 &ptn->pt_time.it_value);
673 }
674 if (timerisset(&pt->pt_time.it_value)) {
675 if (pt->pt_type == CLOCK_VIRTUAL)
676 ptl = &pt->pt_proc->p_timers->pts_virtual;
677 else
678 ptl = &pt->pt_proc->p_timers->pts_prof;
679
680 for (ptn = LIST_FIRST(ptl), pptn = NULL;
681 ptn && timercmp(&pt->pt_time.it_value,
682 &ptn->pt_time.it_value, >);
683 pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
684 timersub(&pt->pt_time.it_value,
685 &ptn->pt_time.it_value,
686 &pt->pt_time.it_value);
687
688 if (pptn)
689 LIST_INSERT_AFTER(pptn, pt, pt_list);
690 else
691 LIST_INSERT_HEAD(ptl, pt, pt_list);
692
693 for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
694 timersub(&ptn->pt_time.it_value,
695 &pt->pt_time.it_value,
696 &ptn->pt_time.it_value);
697
698 pt->pt_active = 1;
699 } else
700 pt->pt_active = 0;
701 }
702 }
703
704 void
705 timer_gettime(struct ptimer *pt, struct itimerval *aitv)
706 {
707 struct timeval now;
708 struct ptimer *ptn;
709
710 KASSERT(mutex_owned(&timer_lock));
711
712 *aitv = pt->pt_time;
713 if (pt->pt_type == CLOCK_REALTIME) {
714 /*
715 * Convert from absolute to relative time in .it_value
716 * part of real time timer. If time for real time
717 * timer has passed return 0, else return difference
718 * between current time and time for the timer to go
719 * off.
720 */
721 if (timerisset(&aitv->it_value)) {
722 getmicrotime(&now);
723 if (timercmp(&aitv->it_value, &now, <))
724 timerclear(&aitv->it_value);
725 else
726 timersub(&aitv->it_value, &now,
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, const struct sys_timer_settime_args *uap,
747 register_t *retval)
748 {
749 /* {
750 syscallarg(timer_t) timerid;
751 syscallarg(int) flags;
752 syscallarg(const struct itimerspec *) value;
753 syscallarg(struct itimerspec *) ovalue;
754 } */
755 int error;
756 struct itimerspec value, ovalue, *ovp = NULL;
757
758 if ((error = copyin(SCARG(uap, value), &value,
759 sizeof(struct itimerspec))) != 0)
760 return (error);
761
762 if (SCARG(uap, ovalue))
763 ovp = &ovalue;
764
765 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
766 SCARG(uap, flags), l->l_proc)) != 0)
767 return error;
768
769 if (ovp)
770 return copyout(&ovalue, SCARG(uap, ovalue),
771 sizeof(struct itimerspec));
772 return 0;
773 }
774
775 int
776 dotimer_settime(int timerid, struct itimerspec *value,
777 struct itimerspec *ovalue, int flags, struct proc *p)
778 {
779 struct timeval now;
780 struct itimerval val, oval;
781 struct ptimers *pts;
782 struct ptimer *pt;
783
784 pts = p->p_timers;
785
786 if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
787 return EINVAL;
788 TIMESPEC_TO_TIMEVAL(&val.it_value, &value->it_value);
789 TIMESPEC_TO_TIMEVAL(&val.it_interval, &value->it_interval);
790 if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
791 return (EINVAL);
792
793 mutex_spin_enter(&timer_lock);
794 if ((pt = pts->pts_timers[timerid]) == NULL) {
795 mutex_spin_exit(&timer_lock);
796 return (EINVAL);
797 }
798
799 oval = pt->pt_time;
800 pt->pt_time = val;
801
802 /*
803 * If we've been passed a relative time for a realtime timer,
804 * convert it to absolute; if an absolute time for a virtual
805 * timer, convert it to relative and make sure we don't set it
806 * to zero, which would cancel the timer, or let it go
807 * negative, which would confuse the comparison tests.
808 */
809 if (timerisset(&pt->pt_time.it_value)) {
810 if (pt->pt_type == CLOCK_REALTIME) {
811 if ((flags & TIMER_ABSTIME) == 0) {
812 getmicrotime(&now);
813 timeradd(&pt->pt_time.it_value, &now,
814 &pt->pt_time.it_value);
815 }
816 } else {
817 if ((flags & TIMER_ABSTIME) != 0) {
818 getmicrotime(&now);
819 timersub(&pt->pt_time.it_value, &now,
820 &pt->pt_time.it_value);
821 if (!timerisset(&pt->pt_time.it_value) ||
822 pt->pt_time.it_value.tv_sec < 0) {
823 pt->pt_time.it_value.tv_sec = 0;
824 pt->pt_time.it_value.tv_usec = 1;
825 }
826 }
827 }
828 }
829
830 timer_settime(pt);
831 mutex_spin_exit(&timer_lock);
832
833 if (ovalue) {
834 TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue->it_value);
835 TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue->it_interval);
836 }
837
838 return (0);
839 }
840
841 /* Return the time remaining until a POSIX timer fires. */
842 int
843 sys_timer_gettime(struct lwp *l, const struct sys_timer_gettime_args *uap,
844 register_t *retval)
845 {
846 /* {
847 syscallarg(timer_t) timerid;
848 syscallarg(struct itimerspec *) value;
849 } */
850 struct itimerspec its;
851 int error;
852
853 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
854 &its)) != 0)
855 return error;
856
857 return copyout(&its, SCARG(uap, value), sizeof(its));
858 }
859
860 int
861 dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
862 {
863 struct ptimer *pt;
864 struct ptimers *pts;
865 struct itimerval aitv;
866
867 pts = p->p_timers;
868 if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
869 return (EINVAL);
870 mutex_spin_enter(&timer_lock);
871 if ((pt = pts->pts_timers[timerid]) == NULL) {
872 mutex_spin_exit(&timer_lock);
873 return (EINVAL);
874 }
875 timer_gettime(pt, &aitv);
876 mutex_spin_exit(&timer_lock);
877
878 TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its->it_interval);
879 TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its->it_value);
880
881 return 0;
882 }
883
884 /*
885 * Return the count of the number of times a periodic timer expired
886 * while a notification was already pending. The counter is reset when
887 * a timer expires and a notification can be posted.
888 */
889 int
890 sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap,
891 register_t *retval)
892 {
893 /* {
894 syscallarg(timer_t) timerid;
895 } */
896 struct proc *p = l->l_proc;
897 struct ptimers *pts;
898 int timerid;
899 struct ptimer *pt;
900
901 timerid = SCARG(uap, timerid);
902
903 pts = p->p_timers;
904 if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
905 return (EINVAL);
906 mutex_spin_enter(&timer_lock);
907 if ((pt = pts->pts_timers[timerid]) == NULL) {
908 mutex_spin_exit(&timer_lock);
909 return (EINVAL);
910 }
911 *retval = pt->pt_poverruns;
912 mutex_spin_exit(&timer_lock);
913
914 return (0);
915 }
916
917 /*
918 * Real interval timer expired:
919 * send process whose timer expired an alarm signal.
920 * If time is not set up to reload, then just return.
921 * Else compute next time timer should go off which is > current time.
922 * This is where delay in processing this timeout causes multiple
923 * SIGALRM calls to be compressed into one.
924 */
925 void
926 realtimerexpire(void *arg)
927 {
928 struct timeval now;
929 struct ptimer *pt;
930
931 pt = arg;
932
933 mutex_spin_enter(&timer_lock);
934 itimerfire(pt);
935
936 if (!timerisset(&pt->pt_time.it_interval)) {
937 timerclear(&pt->pt_time.it_value);
938 mutex_spin_exit(&timer_lock);
939 return;
940 }
941 for (;;) {
942 timeradd(&pt->pt_time.it_value,
943 &pt->pt_time.it_interval, &pt->pt_time.it_value);
944 getmicrotime(&now);
945 if (timercmp(&pt->pt_time.it_value, &now, >)) {
946 /*
947 * Don't need to check hzto() return value, here.
948 * callout_reset() does it for us.
949 */
950 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
951 realtimerexpire, pt);
952 mutex_spin_exit(&timer_lock);
953 return;
954 }
955 mutex_spin_exit(&timer_lock);
956 pt->pt_overruns++;
957 mutex_spin_enter(&timer_lock);
958 }
959 }
960
961 /* BSD routine to get the value of an interval timer. */
962 /* ARGSUSED */
963 int
964 sys_getitimer(struct lwp *l, const struct sys_getitimer_args *uap,
965 register_t *retval)
966 {
967 /* {
968 syscallarg(int) which;
969 syscallarg(struct itimerval *) itv;
970 } */
971 struct proc *p = l->l_proc;
972 struct itimerval aitv;
973 int error;
974
975 error = dogetitimer(p, SCARG(uap, which), &aitv);
976 if (error)
977 return error;
978 return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
979 }
980
981 int
982 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
983 {
984 struct ptimers *pts;
985 struct ptimer *pt;
986
987 if ((u_int)which > ITIMER_PROF)
988 return (EINVAL);
989
990 mutex_spin_enter(&timer_lock);
991 pts = p->p_timers;
992 if (pts == NULL || (pt = pts->pts_timers[which]) == NULL) {
993 timerclear(&itvp->it_value);
994 timerclear(&itvp->it_interval);
995 } else
996 timer_gettime(pt, itvp);
997 mutex_spin_exit(&timer_lock);
998
999 return 0;
1000 }
1001
1002 /* BSD routine to set/arm an interval timer. */
1003 /* ARGSUSED */
1004 int
1005 sys_setitimer(struct lwp *l, const struct sys_setitimer_args *uap,
1006 register_t *retval)
1007 {
1008 /* {
1009 syscallarg(int) which;
1010 syscallarg(const struct itimerval *) itv;
1011 syscallarg(struct itimerval *) oitv;
1012 } */
1013 struct proc *p = l->l_proc;
1014 int which = SCARG(uap, which);
1015 struct sys_getitimer_args getargs;
1016 const struct itimerval *itvp;
1017 struct itimerval aitv;
1018 int error;
1019
1020 if ((u_int)which > ITIMER_PROF)
1021 return (EINVAL);
1022 itvp = SCARG(uap, itv);
1023 if (itvp &&
1024 (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
1025 return (error);
1026 if (SCARG(uap, oitv) != NULL) {
1027 SCARG(&getargs, which) = which;
1028 SCARG(&getargs, itv) = SCARG(uap, oitv);
1029 if ((error = sys_getitimer(l, &getargs, retval)) != 0)
1030 return (error);
1031 }
1032 if (itvp == 0)
1033 return (0);
1034
1035 return dosetitimer(p, which, &aitv);
1036 }
1037
1038 int
1039 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
1040 {
1041 struct timeval now;
1042 struct ptimers *pts;
1043 struct ptimer *pt, *spare;
1044
1045 if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1046 return (EINVAL);
1047
1048 /*
1049 * Don't bother allocating data structures if the process just
1050 * wants to clear the timer.
1051 */
1052 spare = NULL;
1053 pts = p->p_timers;
1054 retry:
1055 if (!timerisset(&itvp->it_value) && (pts == NULL ||
1056 pts->pts_timers[which] == NULL))
1057 return (0);
1058 if (pts == NULL)
1059 pts = timers_alloc(p);
1060 mutex_spin_enter(&timer_lock);
1061 pt = pts->pts_timers[which];
1062 if (pt == NULL) {
1063 if (spare == NULL) {
1064 mutex_spin_exit(&timer_lock);
1065 spare = pool_get(&ptimer_pool, PR_WAITOK);
1066 goto retry;
1067 }
1068 pt = spare;
1069 spare = NULL;
1070 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1071 pt->pt_ev.sigev_value.sival_int = which;
1072 pt->pt_overruns = 0;
1073 pt->pt_proc = p;
1074 pt->pt_type = which;
1075 pt->pt_entry = which;
1076 pt->pt_active = 0;
1077 pt->pt_queued = false;
1078 callout_init(&pt->pt_ch, CALLOUT_MPSAFE);
1079 switch (which) {
1080 case ITIMER_REAL:
1081 pt->pt_ev.sigev_signo = SIGALRM;
1082 break;
1083 case ITIMER_VIRTUAL:
1084 pt->pt_ev.sigev_signo = SIGVTALRM;
1085 break;
1086 case ITIMER_PROF:
1087 pt->pt_ev.sigev_signo = SIGPROF;
1088 break;
1089 }
1090 pts->pts_timers[which] = pt;
1091 }
1092 pt->pt_time = *itvp;
1093
1094 if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
1095 /* Convert to absolute time */
1096 /* XXX need to wrap in splclock for timecounters case? */
1097 getmicrotime(&now);
1098 timeradd(&pt->pt_time.it_value, &now, &pt->pt_time.it_value);
1099 }
1100 timer_settime(pt);
1101 mutex_spin_exit(&timer_lock);
1102 if (spare != NULL)
1103 pool_put(&ptimer_pool, spare);
1104
1105 return (0);
1106 }
1107
1108 /* Utility routines to manage the array of pointers to timers. */
1109 struct ptimers *
1110 timers_alloc(struct proc *p)
1111 {
1112 struct ptimers *pts;
1113 int i;
1114
1115 pts = pool_get(&ptimers_pool, PR_WAITOK);
1116 LIST_INIT(&pts->pts_virtual);
1117 LIST_INIT(&pts->pts_prof);
1118 for (i = 0; i < TIMER_MAX; i++)
1119 pts->pts_timers[i] = NULL;
1120 pts->pts_fired = 0;
1121 mutex_spin_enter(&timer_lock);
1122 if (p->p_timers == NULL) {
1123 p->p_timers = pts;
1124 mutex_spin_exit(&timer_lock);
1125 return pts;
1126 }
1127 mutex_spin_exit(&timer_lock);
1128 pool_put(&ptimers_pool, pts);
1129 return p->p_timers;
1130 }
1131
1132 /*
1133 * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1134 * then clean up all timers and free all the data structures. If
1135 * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1136 * by timer_create(), not the BSD setitimer() timers, and only free the
1137 * structure if none of those remain.
1138 */
1139 void
1140 timers_free(struct proc *p, int which)
1141 {
1142 struct ptimers *pts;
1143 struct ptimer *ptn;
1144 struct timeval tv;
1145 int i;
1146
1147 if (p->p_timers == NULL)
1148 return;
1149
1150 pts = p->p_timers;
1151 mutex_spin_enter(&timer_lock);
1152 if (which == TIMERS_ALL) {
1153 p->p_timers = NULL;
1154 i = 0;
1155 } else {
1156 timerclear(&tv);
1157 for (ptn = LIST_FIRST(&pts->pts_virtual);
1158 ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1159 ptn = LIST_NEXT(ptn, pt_list))
1160 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1161 LIST_FIRST(&pts->pts_virtual) = NULL;
1162 if (ptn) {
1163 timeradd(&tv, &ptn->pt_time.it_value,
1164 &ptn->pt_time.it_value);
1165 LIST_INSERT_HEAD(&pts->pts_virtual, ptn, pt_list);
1166 }
1167 timerclear(&tv);
1168 for (ptn = LIST_FIRST(&pts->pts_prof);
1169 ptn && ptn != pts->pts_timers[ITIMER_PROF];
1170 ptn = LIST_NEXT(ptn, pt_list))
1171 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1172 LIST_FIRST(&pts->pts_prof) = NULL;
1173 if (ptn) {
1174 timeradd(&tv, &ptn->pt_time.it_value,
1175 &ptn->pt_time.it_value);
1176 LIST_INSERT_HEAD(&pts->pts_prof, ptn, pt_list);
1177 }
1178 i = 3;
1179 }
1180 for ( ; i < TIMER_MAX; i++) {
1181 if (pts->pts_timers[i] != NULL) {
1182 itimerfree(pts, i);
1183 mutex_spin_enter(&timer_lock);
1184 }
1185 }
1186 if (pts->pts_timers[0] == NULL && pts->pts_timers[1] == NULL &&
1187 pts->pts_timers[2] == NULL) {
1188 p->p_timers = NULL;
1189 mutex_spin_exit(&timer_lock);
1190 pool_put(&ptimers_pool, pts);
1191 } else
1192 mutex_spin_exit(&timer_lock);
1193 }
1194
1195 static void
1196 itimerfree(struct ptimers *pts, int index)
1197 {
1198 struct ptimer *pt;
1199
1200 KASSERT(mutex_owned(&timer_lock));
1201
1202 pt = pts->pts_timers[index];
1203 pts->pts_timers[index] = NULL;
1204 if (pt->pt_type == CLOCK_REALTIME)
1205 callout_halt(&pt->pt_ch, &timer_lock);
1206 else if (pt->pt_queued)
1207 TAILQ_REMOVE(&timer_queue, pt, pt_chain);
1208 mutex_spin_exit(&timer_lock);
1209 callout_destroy(&pt->pt_ch);
1210 pool_put(&ptimer_pool, pt);
1211 }
1212
1213 /*
1214 * Decrement an interval timer by a specified number
1215 * of microseconds, which must be less than a second,
1216 * i.e. < 1000000. If the timer expires, then reload
1217 * it. In this case, carry over (usec - old value) to
1218 * reduce the value reloaded into the timer so that
1219 * the timer does not drift. This routine assumes
1220 * that it is called in a context where the timers
1221 * on which it is operating cannot change in value.
1222 */
1223 static int
1224 itimerdecr(struct ptimer *pt, int usec)
1225 {
1226 struct itimerval *itp;
1227
1228 KASSERT(mutex_owned(&timer_lock));
1229
1230 itp = &pt->pt_time;
1231 if (itp->it_value.tv_usec < usec) {
1232 if (itp->it_value.tv_sec == 0) {
1233 /* expired, and already in next interval */
1234 usec -= itp->it_value.tv_usec;
1235 goto expire;
1236 }
1237 itp->it_value.tv_usec += 1000000;
1238 itp->it_value.tv_sec--;
1239 }
1240 itp->it_value.tv_usec -= usec;
1241 usec = 0;
1242 if (timerisset(&itp->it_value))
1243 return (1);
1244 /* expired, exactly at end of interval */
1245 expire:
1246 if (timerisset(&itp->it_interval)) {
1247 itp->it_value = itp->it_interval;
1248 itp->it_value.tv_usec -= usec;
1249 if (itp->it_value.tv_usec < 0) {
1250 itp->it_value.tv_usec += 1000000;
1251 itp->it_value.tv_sec--;
1252 }
1253 timer_settime(pt);
1254 } else
1255 itp->it_value.tv_usec = 0; /* sec is already 0 */
1256 return (0);
1257 }
1258
1259 static void
1260 itimerfire(struct ptimer *pt)
1261 {
1262
1263 KASSERT(mutex_owned(&timer_lock));
1264
1265 /*
1266 * XXX Can overrun, but we don't do signal queueing yet, anyway.
1267 * XXX Relying on the clock interrupt is stupid.
1268 */
1269 if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL || pt->pt_queued)
1270 return;
1271 TAILQ_INSERT_TAIL(&timer_queue, pt, pt_chain);
1272 pt->pt_queued = true;
1273 softint_schedule(timer_sih);
1274 }
1275
1276 void
1277 timer_tick(lwp_t *l, bool user)
1278 {
1279 struct ptimers *pts;
1280 struct ptimer *pt;
1281 proc_t *p;
1282
1283 p = l->l_proc;
1284 if (p->p_timers == NULL)
1285 return;
1286
1287 mutex_spin_enter(&timer_lock);
1288 if ((pts = l->l_proc->p_timers) != NULL) {
1289 /*
1290 * Run current process's virtual and profile time, as needed.
1291 */
1292 if (user && (pt = LIST_FIRST(&pts->pts_virtual)) != NULL)
1293 if (itimerdecr(pt, tick) == 0)
1294 itimerfire(pt);
1295 if ((pt = LIST_FIRST(&pts->pts_prof)) != NULL)
1296 if (itimerdecr(pt, tick) == 0)
1297 itimerfire(pt);
1298 }
1299 mutex_spin_exit(&timer_lock);
1300 }
1301
1302 static void
1303 timer_intr(void *cookie)
1304 {
1305 ksiginfo_t ksi;
1306 struct ptimer *pt;
1307 proc_t *p;
1308
1309 mutex_spin_enter(&timer_lock);
1310 while ((pt = TAILQ_FIRST(&timer_queue)) != NULL) {
1311 TAILQ_REMOVE(&timer_queue, pt, pt_chain);
1312 KASSERT(pt->pt_queued);
1313 pt->pt_queued = false;
1314
1315 if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL)
1316 continue;
1317 p = pt->pt_proc;
1318 if (pt->pt_proc->p_timers == NULL) {
1319 /* Process is dying. */
1320 continue;
1321 }
1322 if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo)) {
1323 pt->pt_overruns++;
1324 continue;
1325 }
1326
1327 KSI_INIT(&ksi);
1328 ksi.ksi_signo = pt->pt_ev.sigev_signo;
1329 ksi.ksi_code = SI_TIMER;
1330 ksi.ksi_value = pt->pt_ev.sigev_value;
1331 pt->pt_poverruns = pt->pt_overruns;
1332 pt->pt_overruns = 0;
1333 mutex_spin_exit(&timer_lock);
1334
1335 mutex_enter(proc_lock);
1336 kpsignal(p, &ksi, NULL);
1337 mutex_exit(proc_lock);
1338
1339 mutex_spin_enter(&timer_lock);
1340 }
1341 mutex_spin_exit(&timer_lock);
1342 }
1343
1344 /*
1345 * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
1346 * for usage and rationale.
1347 */
1348 int
1349 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1350 {
1351 struct timeval tv, delta;
1352 int rv = 0;
1353
1354 getmicrouptime(&tv);
1355 timersub(&tv, lasttime, &delta);
1356
1357 /*
1358 * check for 0,0 is so that the message will be seen at least once,
1359 * even if interval is huge.
1360 */
1361 if (timercmp(&delta, mininterval, >=) ||
1362 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1363 *lasttime = tv;
1364 rv = 1;
1365 }
1366
1367 return (rv);
1368 }
1369
1370 /*
1371 * ppsratecheck(): packets (or events) per second limitation.
1372 */
1373 int
1374 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1375 {
1376 struct timeval tv, delta;
1377 int rv;
1378
1379 getmicrouptime(&tv);
1380 timersub(&tv, lasttime, &delta);
1381
1382 /*
1383 * check for 0,0 is so that the message will be seen at least once.
1384 * if more than one second have passed since the last update of
1385 * lasttime, reset the counter.
1386 *
1387 * we do increment *curpps even in *curpps < maxpps case, as some may
1388 * try to use *curpps for stat purposes as well.
1389 */
1390 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
1391 delta.tv_sec >= 1) {
1392 *lasttime = tv;
1393 *curpps = 0;
1394 }
1395 if (maxpps < 0)
1396 rv = 1;
1397 else if (*curpps < maxpps)
1398 rv = 1;
1399 else
1400 rv = 0;
1401
1402 #if 1 /*DIAGNOSTIC?*/
1403 /* be careful about wrap-around */
1404 if (*curpps + 1 > *curpps)
1405 *curpps = *curpps + 1;
1406 #else
1407 /*
1408 * assume that there's not too many calls to this function.
1409 * not sure if the assumption holds, as it depends on *caller's*
1410 * behavior, not the behavior of this function.
1411 * IMHO it is wrong to make assumption on the caller's behavior,
1412 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1413 */
1414 *curpps = *curpps + 1;
1415 #endif
1416
1417 return (rv);
1418 }
1419