xref: /freebsd/sys/kern/kern_time.c (revision ce834215a70ff69e7e222827437116eee2f9ac6f)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
34  * $Id: kern_time.c,v 1.28 1997/06/01 09:05:19 peter Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <sys/sysproto.h>
39 #include <sys/resourcevar.h>
40 #include <sys/signalvar.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/sysent.h>
44 #include <sys/proc.h>
45 #include <sys/signal.h>
46 #include <sys/time.h>
47 #include <sys/vnode.h>
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/vm_extern.h>
51 
52 struct timezone tz;
53 
54 /*
55  * Time of day and interval timer support.
56  *
57  * These routines provide the kernel entry points to get and set
58  * the time-of-day and per-process interval timers.  Subroutines
59  * here provide support for adding and subtracting timeval structures
60  * and decrementing interval timers, optionally reloading the interval
61  * timers when they expire.
62  */
63 
64 static int	settime __P((struct timeval *));
65 static void	timevalfix __P((struct timeval *));
66 static int	nanosleep1 __P((struct proc *p, struct timespec *rqt,
67 		    struct timespec *rmt));
68 
69 static int
70 settime(tv)
71 	struct timeval *tv;
72 {
73 	struct timeval delta;
74 	struct proc *p;
75 	int s;
76 
77 	/*
78 	 * Must not set clock backwards in highly secure mode.
79 	 */
80 	s = splclock();
81 	delta.tv_sec = tv->tv_sec - time.tv_sec;
82 	delta.tv_usec = tv->tv_usec - time.tv_usec;
83 	splx(s);
84 	timevalfix(&delta);
85 	if (delta.tv_sec < 0 && securelevel > 1)
86 		return (EPERM);
87 
88 	s = splclock();
89 	/*
90 	 * Recalculate delta directly to minimize clock interrupt
91 	 * latency.  Fix it after the ipl has been lowered.
92 	 */
93 	delta.tv_sec = tv->tv_sec - time.tv_sec;
94 	delta.tv_usec = tv->tv_usec - time.tv_usec;
95 	time = *tv;
96 	/*
97 	 * XXX should arrange for microtime() to agree with *tv if
98 	 * it is called now.  As it is, it may add up to about
99 	 * `tick' unwanted usec.
100 	 * Another problem is that clock interrupts may occur at
101 	 * other than multiples of `tick'.  It's not worth fixing
102 	 * this here, since the problem is also caused by tick
103 	 * adjustments.
104 	 */
105 	(void) splsoftclock();
106 	timevalfix(&delta);
107 	timevaladd(&boottime, &delta);
108 	timevaladd(&runtime, &delta);
109 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
110 		if (timerisset(&p->p_realtimer.it_value))
111 			timevaladd(&p->p_realtimer.it_value, &delta);
112 		if (p->p_sleepend)
113 			timevaladd(p->p_sleepend, &delta);
114 	}
115 #ifdef NFS
116 	lease_updatetime(delta.tv_sec);
117 #endif
118 	splx(s);
119 	resettodr();
120 	return (0);
121 }
122 
123 #ifndef _SYS_SYSPROTO_H_
124 struct clock_gettime_args {
125 	clockid_t clock_id;
126 	struct	timespec *tp;
127 };
128 #endif
129 
130 /* ARGSUSED */
131 int
132 clock_gettime(p, uap, retval)
133 	struct proc *p;
134 	struct clock_gettime_args *uap;
135 	register_t *retval;
136 {
137 	struct timeval atv;
138 	struct timespec ats;
139 
140 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
141 		return (EINVAL);
142 	microtime(&atv);
143 	TIMEVAL_TO_TIMESPEC(&atv, &ats);
144 	return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
145 }
146 
147 #ifndef _SYS_SYSPROTO_H_
148 struct clock_settime_args {
149 	clockid_t clock_id;
150 	const struct	timespec *tp;
151 };
152 #endif
153 
154 /* ARGSUSED */
155 int
156 clock_settime(p, uap, retval)
157 	struct proc *p;
158 	struct clock_settime_args *uap;
159 	register_t *retval;
160 {
161 	struct timeval atv;
162 	struct timespec ats;
163 	int error;
164 
165 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
166 		return (error);
167 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
168 		return (EINVAL);
169 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
170 		return (error);
171 	if (atv.tv_usec < 0 || ats.tv_nsec >= 1000000000)
172 		return (EINVAL);
173 	TIMESPEC_TO_TIMEVAL(&atv, &ats);
174 	if ((error = settime(&atv)))
175 		return (error);
176 	return (0);
177 }
178 
179 #ifndef _SYS_SYSPROTO_H_
180 struct clock_getres_args {
181 	clockid_t clock_id;
182 	struct	timespec *tp;
183 };
184 #endif
185 
186 int
187 clock_getres(p, uap, retval)
188 	struct proc *p;
189 	struct clock_getres_args *uap;
190 	register_t *retval;
191 {
192 	struct timespec ts;
193 	int error;
194 
195 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
196 		return (EINVAL);
197 	error = 0;
198 	if (SCARG(uap, tp)) {
199 		ts.tv_sec = 0;
200 		ts.tv_nsec = 1000000000 / hz;
201 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
202 	}
203 	return (error);
204 }
205 
206 static int nanowait;
207 
208 static int
209 nanosleep1(p, rqt, rmt)
210 	struct proc *p;
211 	struct timespec *rqt, *rmt;
212 {
213 	struct timeval atv, utv;
214 	int error, s, timo;
215 
216 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
217 		return (EINVAL);
218 	TIMESPEC_TO_TIMEVAL(&atv, rqt)
219 	if (itimerfix(&atv))
220 		return (EINVAL);
221 
222 	/*
223 	 * XXX this is not as careful as settimeofday() about minimising
224 	 * interrupt latency.  The hzto() interface is inconvenient as usual.
225 	 */
226 	s = splclock();
227 	timevaladd(&atv, &time);
228 	timo = hzto(&atv);
229 	splx(s);
230 
231 	p->p_sleepend = &atv;
232 	error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", timo);
233 	p->p_sleepend = NULL;
234 
235 	if (error == ERESTART)
236 		error = EINTR;
237 	if (error == EWOULDBLOCK)
238 		error = 0;
239 	if (rmt != NULL) {
240 		/*-
241 		 * XXX this is unnecessary and possibly wrong if the timeout
242 		 * expired.  Then the remaining time should be zero.  If the
243 		 * calculation gives a nonzero value, then we have a bug.
244 		 * (1) if settimeofday() was called, then the calculation is
245 		 *     probably wrong, since `time' has probably become
246 		 *     inconsistent with the ending time `atv'.
247 		 *     XXX (1) should be fixed now with p->p_sleepend;
248 		 * (2) otherwise, our calculation of `timo' was wrong, perhaps
249 		 *     due to `tick' being wrong when hzto() was called or
250 		 *     changing afterwards (it can be wrong or change due to
251 		 *     hzto() not knowing about adjtime(2) or tickadj(8)).
252 		 *     Then we should be sleeping again instead instead of
253 		 *     returning.  Rounding up in hzto() probably fixes this
254 		 *     problem for small timeouts, but the absolute error may
255 		 *     be large for large timeouts.
256 		 */
257 		s = splclock();
258 		utv = time;
259 		splx(s);
260 		timevalsub(&atv, &utv);
261 		if (atv.tv_sec < 0)
262 			timerclear(&atv);
263 		TIMEVAL_TO_TIMESPEC(&atv, rmt);
264 	}
265 	return (error);
266 }
267 
268 #ifndef _SYS_SYSPROTO_H_
269 struct nanosleep_args {
270 	struct	timespec *rqtp;
271 	struct	timespec *rmtp;
272 };
273 #endif
274 
275 /* ARGSUSED */
276 int
277 nanosleep(p, uap, retval)
278 	struct proc *p;
279 	struct nanosleep_args *uap;
280 	register_t *retval;
281 {
282 	struct timespec rmt, rqt;
283 	int error, error2;
284 
285 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
286 	if (error)
287 		return (error);
288 	if (SCARG(uap, rmtp))
289 		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), B_WRITE))
290 			return (EFAULT);
291 
292 	error = nanosleep1(p, &rqt, &rmt);
293 
294 	if (SCARG(uap, rmtp)) {
295 		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
296 		if (error2)	/* XXX shouldn't happen, did useracc() above */
297 			return (error2);
298 	}
299 	return (error);
300 }
301 
302 #ifndef _SYS_SYSPROTO_H_
303 struct signanosleep_args {
304 	struct	timespec *rqtp;
305 	struct	timespec *rmtp;
306 	sigset_t *mask;
307 };
308 #endif
309 
310 /* ARGSUSED */
311 int
312 signanosleep(p, uap, retval)
313 	struct proc *p;
314 	struct signanosleep_args *uap;
315 	register_t *retval;
316 {
317 	struct timespec rmt, rqt;
318 	int error, error2;
319 	struct sigacts *ps = p->p_sigacts;
320 	sigset_t mask;
321 
322 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
323 	if (error)
324 		return (error);
325 	if (SCARG(uap, rmtp))
326 		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), B_WRITE))
327 			return (EFAULT);
328 	error = copyin(SCARG(uap, mask), &mask, sizeof(mask));
329 	if (error)
330 		return (error);
331 
332 	/* See kern_sig.c:sigsuspend() for explanation */
333 	ps->ps_oldmask = p->p_sigmask;
334 	ps->ps_flags |= SAS_OLDMASK;
335 	p->p_sigmask = mask &~ sigcantmask;
336 
337 	error = nanosleep1(p, &rqt, &rmt);
338 
339 	p->p_sigmask = ps->ps_oldmask;	/* in case timeout rather than sig */
340 	ps->ps_flags &= ~SAS_OLDMASK;
341 
342 	if (SCARG(uap, rmtp)) {
343 		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
344 		if (error2)	/* XXX shouldn't happen, did useracc() above */
345 			return (error2);
346 	}
347 	return (error);
348 
349 }
350 
351 #ifndef _SYS_SYSPROTO_H_
352 struct gettimeofday_args {
353 	struct	timeval *tp;
354 	struct	timezone *tzp;
355 };
356 #endif
357 /* ARGSUSED */
358 int
359 gettimeofday(p, uap, retval)
360 	struct proc *p;
361 	register struct gettimeofday_args *uap;
362 	int *retval;
363 {
364 	struct timeval atv;
365 	int error = 0;
366 
367 	if (uap->tp) {
368 		microtime(&atv);
369 		if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
370 		    sizeof (atv))))
371 			return (error);
372 	}
373 	if (uap->tzp)
374 		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
375 		    sizeof (tz));
376 	return (error);
377 }
378 
379 #ifndef _SYS_SYSPROTO_H_
380 struct settimeofday_args {
381 	struct	timeval *tv;
382 	struct	timezone *tzp;
383 };
384 #endif
385 /* ARGSUSED */
386 int
387 settimeofday(p, uap, retval)
388 	struct proc *p;
389 	struct settimeofday_args *uap;
390 	int *retval;
391 {
392 	struct timeval atv;
393 	struct timezone atz;
394 	int error;
395 
396 	if ((error = suser(p->p_ucred, &p->p_acflag)))
397 		return (error);
398 	/* Verify all parameters before changing time. */
399 	if (uap->tv) {
400 		if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
401 		    sizeof(atv))))
402 			return (error);
403 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
404 			return (EINVAL);
405 	}
406 	if (uap->tzp &&
407 	    (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
408 		return (error);
409 	if (uap->tv && (error = settime(&atv)))
410 		return (error);
411 	if (uap->tzp)
412 		tz = atz;
413 	return (0);
414 }
415 
416 int	tickdelta;			/* current clock skew, us. per tick */
417 long	timedelta;			/* unapplied time correction, us. */
418 static long	bigadj = 1000000;	/* use 10x skew above bigadj us. */
419 
420 #ifndef _SYS_SYSPROTO_H_
421 struct adjtime_args {
422 	struct timeval *delta;
423 	struct timeval *olddelta;
424 };
425 #endif
426 /* ARGSUSED */
427 int
428 adjtime(p, uap, retval)
429 	struct proc *p;
430 	register struct adjtime_args *uap;
431 	int *retval;
432 {
433 	struct timeval atv;
434 	register long ndelta, ntickdelta, odelta;
435 	int s, error;
436 
437 	if ((error = suser(p->p_ucred, &p->p_acflag)))
438 		return (error);
439 	if ((error =
440 	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
441 		return (error);
442 
443 	/*
444 	 * Compute the total correction and the rate at which to apply it.
445 	 * Round the adjustment down to a whole multiple of the per-tick
446 	 * delta, so that after some number of incremental changes in
447 	 * hardclock(), tickdelta will become zero, lest the correction
448 	 * overshoot and start taking us away from the desired final time.
449 	 */
450 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
451 	if (ndelta > bigadj || ndelta < -bigadj)
452 		ntickdelta = 10 * tickadj;
453 	else
454 		ntickdelta = tickadj;
455 	if (ndelta % ntickdelta)
456 		ndelta = ndelta / ntickdelta * ntickdelta;
457 
458 	/*
459 	 * To make hardclock()'s job easier, make the per-tick delta negative
460 	 * if we want time to run slower; then hardclock can simply compute
461 	 * tick + tickdelta, and subtract tickdelta from timedelta.
462 	 */
463 	if (ndelta < 0)
464 		ntickdelta = -ntickdelta;
465 	s = splclock();
466 	odelta = timedelta;
467 	timedelta = ndelta;
468 	tickdelta = ntickdelta;
469 	splx(s);
470 
471 	if (uap->olddelta) {
472 		atv.tv_sec = odelta / 1000000;
473 		atv.tv_usec = odelta % 1000000;
474 		(void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
475 		    sizeof(struct timeval));
476 	}
477 	return (0);
478 }
479 
480 /*
481  * Get value of an interval timer.  The process virtual and
482  * profiling virtual time timers are kept in the p_stats area, since
483  * they can be swapped out.  These are kept internally in the
484  * way they are specified externally: in time until they expire.
485  *
486  * The real time interval timer is kept in the process table slot
487  * for the process, and its value (it_value) is kept as an
488  * absolute time rather than as a delta, so that it is easy to keep
489  * periodic real-time signals from drifting.
490  *
491  * Virtual time timers are processed in the hardclock() routine of
492  * kern_clock.c.  The real time timer is processed by a timeout
493  * routine, called from the softclock() routine.  Since a callout
494  * may be delayed in real time due to interrupt processing in the system,
495  * it is possible for the real time timeout routine (realitexpire, given below),
496  * to be delayed in real time past when it is supposed to occur.  It
497  * does not suffice, therefore, to reload the real timer .it_value from the
498  * real time timers .it_interval.  Rather, we compute the next time in
499  * absolute time the timer should go off.
500  */
501 #ifndef _SYS_SYSPROTO_H_
502 struct getitimer_args {
503 	u_int	which;
504 	struct	itimerval *itv;
505 };
506 #endif
507 /* ARGSUSED */
508 int
509 getitimer(p, uap, retval)
510 	struct proc *p;
511 	register struct getitimer_args *uap;
512 	int *retval;
513 {
514 	struct itimerval aitv;
515 	int s;
516 
517 	if (uap->which > ITIMER_PROF)
518 		return (EINVAL);
519 	s = splclock();
520 	if (uap->which == ITIMER_REAL) {
521 		/*
522 		 * Convert from absoulte to relative time in .it_value
523 		 * part of real time timer.  If time for real time timer
524 		 * has passed return 0, else return difference between
525 		 * current time and time for the timer to go off.
526 		 */
527 		aitv = p->p_realtimer;
528 		if (timerisset(&aitv.it_value))
529 			if (timercmp(&aitv.it_value, &time, <))
530 				timerclear(&aitv.it_value);
531 			else
532 				timevalsub(&aitv.it_value, &time);
533 	} else
534 		aitv = p->p_stats->p_timer[uap->which];
535 	splx(s);
536 	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
537 	    sizeof (struct itimerval)));
538 }
539 
540 #ifndef _SYS_SYSPROTO_H_
541 struct setitimer_args {
542 	u_int	which;
543 	struct	itimerval *itv, *oitv;
544 };
545 #endif
546 /* ARGSUSED */
547 int
548 setitimer(p, uap, retval)
549 	struct proc *p;
550 	register struct setitimer_args *uap;
551 	int *retval;
552 {
553 	struct itimerval aitv;
554 	register struct itimerval *itvp;
555 	int s, error;
556 
557 	if (uap->which > ITIMER_PROF)
558 		return (EINVAL);
559 	itvp = uap->itv;
560 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
561 	    sizeof(struct itimerval))))
562 		return (error);
563 	if ((uap->itv = uap->oitv) &&
564 	    (error = getitimer(p, (struct getitimer_args *)uap, retval)))
565 		return (error);
566 	if (itvp == 0)
567 		return (0);
568 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
569 		return (EINVAL);
570 	s = splclock();
571 	if (uap->which == ITIMER_REAL) {
572 		untimeout(realitexpire, (caddr_t)p);
573 		if (timerisset(&aitv.it_value)) {
574 			timevaladd(&aitv.it_value, &time);
575 			timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
576 		}
577 		p->p_realtimer = aitv;
578 	} else
579 		p->p_stats->p_timer[uap->which] = aitv;
580 	splx(s);
581 	return (0);
582 }
583 
584 /*
585  * Real interval timer expired:
586  * send process whose timer expired an alarm signal.
587  * If time is not set up to reload, then just return.
588  * Else compute next time timer should go off which is > current time.
589  * This is where delay in processing this timeout causes multiple
590  * SIGALRM calls to be compressed into one.
591  * hzto() always adds 1 to allow for the time until the next clock
592  * interrupt being strictly less than 1 clock tick, but we don't want
593  * that here since we want to appear to be in sync with the clock
594  * interrupt even when we're delayed.
595  */
596 void
597 realitexpire(arg)
598 	void *arg;
599 {
600 	register struct proc *p;
601 	int s;
602 
603 	p = (struct proc *)arg;
604 	psignal(p, SIGALRM);
605 	if (!timerisset(&p->p_realtimer.it_interval)) {
606 		timerclear(&p->p_realtimer.it_value);
607 		return;
608 	}
609 	for (;;) {
610 		s = splclock();
611 		timevaladd(&p->p_realtimer.it_value,
612 		    &p->p_realtimer.it_interval);
613 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
614 			timeout(realitexpire, (caddr_t)p,
615 			    hzto(&p->p_realtimer.it_value) - 1);
616 			splx(s);
617 			return;
618 		}
619 		splx(s);
620 	}
621 }
622 
623 /*
624  * Check that a proposed value to load into the .it_value or
625  * .it_interval part of an interval timer is acceptable, and
626  * fix it to have at least minimal value (i.e. if it is less
627  * than the resolution of the clock, round it up.)
628  */
629 int
630 itimerfix(tv)
631 	struct timeval *tv;
632 {
633 
634 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
635 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
636 		return (EINVAL);
637 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
638 		tv->tv_usec = tick;
639 	return (0);
640 }
641 
642 /*
643  * Decrement an interval timer by a specified number
644  * of microseconds, which must be less than a second,
645  * i.e. < 1000000.  If the timer expires, then reload
646  * it.  In this case, carry over (usec - old value) to
647  * reduce the value reloaded into the timer so that
648  * the timer does not drift.  This routine assumes
649  * that it is called in a context where the timers
650  * on which it is operating cannot change in value.
651  */
652 int
653 itimerdecr(itp, usec)
654 	register struct itimerval *itp;
655 	int usec;
656 {
657 
658 	if (itp->it_value.tv_usec < usec) {
659 		if (itp->it_value.tv_sec == 0) {
660 			/* expired, and already in next interval */
661 			usec -= itp->it_value.tv_usec;
662 			goto expire;
663 		}
664 		itp->it_value.tv_usec += 1000000;
665 		itp->it_value.tv_sec--;
666 	}
667 	itp->it_value.tv_usec -= usec;
668 	usec = 0;
669 	if (timerisset(&itp->it_value))
670 		return (1);
671 	/* expired, exactly at end of interval */
672 expire:
673 	if (timerisset(&itp->it_interval)) {
674 		itp->it_value = itp->it_interval;
675 		itp->it_value.tv_usec -= usec;
676 		if (itp->it_value.tv_usec < 0) {
677 			itp->it_value.tv_usec += 1000000;
678 			itp->it_value.tv_sec--;
679 		}
680 	} else
681 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
682 	return (0);
683 }
684 
685 /*
686  * Add and subtract routines for timevals.
687  * N.B.: subtract routine doesn't deal with
688  * results which are before the beginning,
689  * it just gets very confused in this case.
690  * Caveat emptor.
691  */
692 void
693 timevaladd(t1, t2)
694 	struct timeval *t1, *t2;
695 {
696 
697 	t1->tv_sec += t2->tv_sec;
698 	t1->tv_usec += t2->tv_usec;
699 	timevalfix(t1);
700 }
701 
702 void
703 timevalsub(t1, t2)
704 	struct timeval *t1, *t2;
705 {
706 
707 	t1->tv_sec -= t2->tv_sec;
708 	t1->tv_usec -= t2->tv_usec;
709 	timevalfix(t1);
710 }
711 
712 static void
713 timevalfix(t1)
714 	struct timeval *t1;
715 {
716 
717 	if (t1->tv_usec < 0) {
718 		t1->tv_sec--;
719 		t1->tv_usec += 1000000;
720 	}
721 	if (t1->tv_usec >= 1000000) {
722 		t1->tv_sec++;
723 		t1->tv_usec -= 1000000;
724 	}
725 }
726