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