Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.1.1.3
      1      1.1   cgd /*
      2  1.1.1.2  fvdl  * Copyright (c) 1982, 1986, 1989, 1993
      3  1.1.1.2  fvdl  *	The Regents of the University of California.  All rights reserved.
      4      1.1   cgd  *
      5      1.1   cgd  * Redistribution and use in source and binary forms, with or without
      6      1.1   cgd  * modification, are permitted provided that the following conditions
      7      1.1   cgd  * are met:
      8      1.1   cgd  * 1. Redistributions of source code must retain the above copyright
      9      1.1   cgd  *    notice, this list of conditions and the following disclaimer.
     10      1.1   cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1   cgd  *    notice, this list of conditions and the following disclaimer in the
     12      1.1   cgd  *    documentation and/or other materials provided with the distribution.
     13      1.1   cgd  * 3. All advertising materials mentioning features or use of this software
     14      1.1   cgd  *    must display the following acknowledgement:
     15      1.1   cgd  *	This product includes software developed by the University of
     16      1.1   cgd  *	California, Berkeley and its contributors.
     17      1.1   cgd  * 4. Neither the name of the University nor the names of its contributors
     18      1.1   cgd  *    may be used to endorse or promote products derived from this software
     19      1.1   cgd  *    without specific prior written permission.
     20      1.1   cgd  *
     21      1.1   cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1   cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1   cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1   cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1   cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1   cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1   cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1   cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1   cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1   cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1   cgd  * SUCH DAMAGE.
     32      1.1   cgd  *
     33  1.1.1.3  fvdl  *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
     34      1.1   cgd  */
     35      1.1   cgd 
     36  1.1.1.2  fvdl #include <sys/param.h>
     37  1.1.1.2  fvdl #include <sys/resourcevar.h>
     38  1.1.1.2  fvdl #include <sys/kernel.h>
     39  1.1.1.2  fvdl #include <sys/systm.h>
     40  1.1.1.2  fvdl #include <sys/proc.h>
     41  1.1.1.2  fvdl #include <sys/vnode.h>
     42      1.1   cgd 
     43  1.1.1.3  fvdl #include <sys/mount.h>
     44  1.1.1.3  fvdl #include <sys/syscallargs.h>
     45  1.1.1.3  fvdl 
     46  1.1.1.2  fvdl #include <machine/cpu.h>
     47      1.1   cgd 
     48      1.1   cgd /*
     49      1.1   cgd  * Time of day and interval timer support.
     50      1.1   cgd  *
     51      1.1   cgd  * These routines provide the kernel entry points to get and set
     52      1.1   cgd  * the time-of-day and per-process interval timers.  Subroutines
     53      1.1   cgd  * here provide support for adding and subtracting timeval structures
     54      1.1   cgd  * and decrementing interval timers, optionally reloading the interval
     55      1.1   cgd  * timers when they expire.
     56      1.1   cgd  */
     57      1.1   cgd 
     58      1.1   cgd /* ARGSUSED */
     59  1.1.1.3  fvdl int
     60      1.1   cgd gettimeofday(p, uap, retval)
     61      1.1   cgd 	struct proc *p;
     62  1.1.1.3  fvdl 	register struct gettimeofday_args /* {
     63  1.1.1.3  fvdl 		syscallarg(struct timeval *) tp;
     64  1.1.1.3  fvdl 		syscallarg(struct timezone *) tzp;
     65  1.1.1.3  fvdl 	} */ *uap;
     66  1.1.1.3  fvdl 	register_t *retval;
     67      1.1   cgd {
     68      1.1   cgd 	struct timeval atv;
     69      1.1   cgd 	int error = 0;
     70      1.1   cgd 
     71  1.1.1.3  fvdl 	if (SCARG(uap, tp)) {
     72      1.1   cgd 		microtime(&atv);
     73  1.1.1.3  fvdl 		if (error = copyout((caddr_t)&atv, (caddr_t)SCARG(uap, tp),
     74      1.1   cgd 		    sizeof (atv)))
     75      1.1   cgd 			return (error);
     76      1.1   cgd 	}
     77  1.1.1.3  fvdl 	if (SCARG(uap, tzp))
     78  1.1.1.3  fvdl 		error = copyout((caddr_t)&tz, (caddr_t)SCARG(uap, tzp),
     79      1.1   cgd 		    sizeof (tz));
     80      1.1   cgd 	return (error);
     81      1.1   cgd }
     82      1.1   cgd 
     83      1.1   cgd /* ARGSUSED */
     84  1.1.1.3  fvdl int
     85      1.1   cgd settimeofday(p, uap, retval)
     86      1.1   cgd 	struct proc *p;
     87  1.1.1.3  fvdl 	struct settimeofday_args /* {
     88  1.1.1.3  fvdl 		syscallarg(struct timeval *) tv;
     89  1.1.1.3  fvdl 		syscallarg(struct timezone *) tzp;
     90  1.1.1.3  fvdl 	} */ *uap;
     91  1.1.1.3  fvdl 	register_t *retval;
     92      1.1   cgd {
     93  1.1.1.2  fvdl 	struct timeval atv, delta;
     94      1.1   cgd 	struct timezone atz;
     95      1.1   cgd 	int error, s;
     96      1.1   cgd 
     97      1.1   cgd 	if (error = suser(p->p_ucred, &p->p_acflag))
     98      1.1   cgd 		return (error);
     99  1.1.1.2  fvdl 	/* Verify all parameters before changing time. */
    100  1.1.1.3  fvdl 	if (SCARG(uap, tv) && (error = copyin((caddr_t)SCARG(uap, tv),
    101  1.1.1.3  fvdl 	    (caddr_t)&atv, sizeof(atv))))
    102  1.1.1.2  fvdl 		return (error);
    103  1.1.1.3  fvdl 	if (SCARG(uap, tzp) && (error = copyin((caddr_t)SCARG(uap, tzp),
    104  1.1.1.3  fvdl 	    (caddr_t)&atz, sizeof(atz))))
    105  1.1.1.2  fvdl 		return (error);
    106  1.1.1.3  fvdl 	if (SCARG(uap, tv)) {
    107  1.1.1.3  fvdl 		/*
    108  1.1.1.3  fvdl 		 * If the system is secure, we do not allow the time to be
    109  1.1.1.3  fvdl 		 * set to an earlier value (it may be slowed using adjtime,
    110  1.1.1.3  fvdl 		 * but not set back). This feature prevent interlopers from
    111  1.1.1.3  fvdl 		 * setting arbitrary time stamps on files.
    112  1.1.1.3  fvdl 		 */
    113  1.1.1.3  fvdl 		if (securelevel > 0 && timercmp(&atv, &time, <))
    114  1.1.1.3  fvdl 			return (EPERM);
    115      1.1   cgd 		/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
    116  1.1.1.2  fvdl 		s = splclock();
    117  1.1.1.2  fvdl 		/* nb. delta.tv_usec may be < 0, but this is OK here */
    118  1.1.1.2  fvdl 		delta.tv_sec = atv.tv_sec - time.tv_sec;
    119  1.1.1.2  fvdl 		delta.tv_usec = atv.tv_usec - time.tv_usec;
    120  1.1.1.2  fvdl 		time = atv;
    121  1.1.1.2  fvdl 		(void) splsoftclock();
    122  1.1.1.2  fvdl 		timevaladd(&boottime, &delta);
    123  1.1.1.2  fvdl 		timevalfix(&boottime);
    124  1.1.1.2  fvdl 		timevaladd(&runtime, &delta);
    125  1.1.1.2  fvdl 		timevalfix(&runtime);
    126  1.1.1.3  fvdl #		ifdef NFS
    127  1.1.1.3  fvdl 			lease_updatetime(delta.tv_sec);
    128  1.1.1.3  fvdl #		endif
    129  1.1.1.2  fvdl 		splx(s);
    130      1.1   cgd 		resettodr();
    131      1.1   cgd 	}
    132  1.1.1.3  fvdl 	if (SCARG(uap, tzp))
    133      1.1   cgd 		tz = atz;
    134  1.1.1.2  fvdl 	return (0);
    135      1.1   cgd }
    136      1.1   cgd 
    137      1.1   cgd extern	int tickadj;			/* "standard" clock skew, us./tick */
    138      1.1   cgd int	tickdelta;			/* current clock skew, us. per tick */
    139      1.1   cgd long	timedelta;			/* unapplied time correction, us. */
    140      1.1   cgd long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
    141      1.1   cgd 
    142      1.1   cgd /* ARGSUSED */
    143  1.1.1.3  fvdl int
    144      1.1   cgd adjtime(p, uap, retval)
    145      1.1   cgd 	struct proc *p;
    146  1.1.1.3  fvdl 	register struct adjtime_args /* {
    147  1.1.1.3  fvdl 		syscallarg(struct timeval *) delta;
    148  1.1.1.3  fvdl 		syscallarg(struct timeval *) olddelta;
    149  1.1.1.3  fvdl 	} */ *uap;
    150  1.1.1.3  fvdl 	register_t *retval;
    151      1.1   cgd {
    152  1.1.1.2  fvdl 	struct timeval atv;
    153  1.1.1.2  fvdl 	register long ndelta, ntickdelta, odelta;
    154      1.1   cgd 	int s, error;
    155      1.1   cgd 
    156      1.1   cgd 	if (error = suser(p->p_ucred, &p->p_acflag))
    157      1.1   cgd 		return (error);
    158  1.1.1.3  fvdl 	if (error = copyin((caddr_t)SCARG(uap, delta), (caddr_t)&atv,
    159  1.1.1.3  fvdl 	    sizeof(struct timeval)))
    160      1.1   cgd 		return (error);
    161      1.1   cgd 
    162  1.1.1.2  fvdl 	/*
    163  1.1.1.2  fvdl 	 * Compute the total correction and the rate at which to apply it.
    164  1.1.1.2  fvdl 	 * Round the adjustment down to a whole multiple of the per-tick
    165  1.1.1.2  fvdl 	 * delta, so that after some number of incremental changes in
    166  1.1.1.2  fvdl 	 * hardclock(), tickdelta will become zero, lest the correction
    167  1.1.1.2  fvdl 	 * overshoot and start taking us away from the desired final time.
    168  1.1.1.2  fvdl 	 */
    169  1.1.1.2  fvdl 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
    170  1.1.1.2  fvdl 	if (ndelta > bigadj)
    171  1.1.1.2  fvdl 		ntickdelta = 10 * tickadj;
    172  1.1.1.2  fvdl 	else
    173  1.1.1.2  fvdl 		ntickdelta = tickadj;
    174  1.1.1.2  fvdl 	if (ndelta % ntickdelta)
    175  1.1.1.2  fvdl 		ndelta = ndelta / ntickdelta * ntickdelta;
    176  1.1.1.2  fvdl 
    177  1.1.1.2  fvdl 	/*
    178  1.1.1.2  fvdl 	 * To make hardclock()'s job easier, make the per-tick delta negative
    179  1.1.1.2  fvdl 	 * if we want time to run slower; then hardclock can simply compute
    180  1.1.1.2  fvdl 	 * tick + tickdelta, and subtract tickdelta from timedelta.
    181  1.1.1.2  fvdl 	 */
    182  1.1.1.2  fvdl 	if (ndelta < 0)
    183  1.1.1.2  fvdl 		ntickdelta = -ntickdelta;
    184      1.1   cgd 	s = splclock();
    185  1.1.1.2  fvdl 	odelta = timedelta;
    186      1.1   cgd 	timedelta = ndelta;
    187  1.1.1.2  fvdl 	tickdelta = ntickdelta;
    188      1.1   cgd 	splx(s);
    189      1.1   cgd 
    190  1.1.1.3  fvdl 	if (SCARG(uap, olddelta)) {
    191  1.1.1.2  fvdl 		atv.tv_sec = odelta / 1000000;
    192  1.1.1.2  fvdl 		atv.tv_usec = odelta % 1000000;
    193  1.1.1.3  fvdl 		(void) copyout((caddr_t)&atv, (caddr_t)SCARG(uap, olddelta),
    194  1.1.1.2  fvdl 		    sizeof(struct timeval));
    195  1.1.1.2  fvdl 	}
    196      1.1   cgd 	return (0);
    197      1.1   cgd }
    198      1.1   cgd 
    199      1.1   cgd /*
    200      1.1   cgd  * Get value of an interval timer.  The process virtual and
    201      1.1   cgd  * profiling virtual time timers are kept in the p_stats area, since
    202      1.1   cgd  * they can be swapped out.  These are kept internally in the
    203      1.1   cgd  * way they are specified externally: in time until they expire.
    204      1.1   cgd  *
    205      1.1   cgd  * The real time interval timer is kept in the process table slot
    206      1.1   cgd  * for the process, and its value (it_value) is kept as an
    207      1.1   cgd  * absolute time rather than as a delta, so that it is easy to keep
    208      1.1   cgd  * periodic real-time signals from drifting.
    209      1.1   cgd  *
    210      1.1   cgd  * Virtual time timers are processed in the hardclock() routine of
    211      1.1   cgd  * kern_clock.c.  The real time timer is processed by a timeout
    212      1.1   cgd  * routine, called from the softclock() routine.  Since a callout
    213      1.1   cgd  * may be delayed in real time due to interrupt processing in the system,
    214      1.1   cgd  * it is possible for the real time timeout routine (realitexpire, given below),
    215      1.1   cgd  * to be delayed in real time past when it is supposed to occur.  It
    216      1.1   cgd  * does not suffice, therefore, to reload the real timer .it_value from the
    217      1.1   cgd  * real time timers .it_interval.  Rather, we compute the next time in
    218      1.1   cgd  * absolute time the timer should go off.
    219      1.1   cgd  */
    220      1.1   cgd /* ARGSUSED */
    221  1.1.1.3  fvdl int
    222      1.1   cgd getitimer(p, uap, retval)
    223      1.1   cgd 	struct proc *p;
    224  1.1.1.3  fvdl 	register struct getitimer_args /* {
    225  1.1.1.3  fvdl 		syscallarg(u_int) which;
    226  1.1.1.3  fvdl 		syscallarg(struct itimerval *) itv;
    227  1.1.1.3  fvdl 	} */ *uap;
    228  1.1.1.3  fvdl 	register_t *retval;
    229      1.1   cgd {
    230      1.1   cgd 	struct itimerval aitv;
    231      1.1   cgd 	int s;
    232      1.1   cgd 
    233  1.1.1.3  fvdl 	if (SCARG(uap, which) > ITIMER_PROF)
    234      1.1   cgd 		return (EINVAL);
    235      1.1   cgd 	s = splclock();
    236  1.1.1.3  fvdl 	if (SCARG(uap, which) == ITIMER_REAL) {
    237      1.1   cgd 		/*
    238  1.1.1.3  fvdl 		 * Convert from absolute to relative time in .it_value
    239      1.1   cgd 		 * part of real time timer.  If time for real time timer
    240      1.1   cgd 		 * has passed return 0, else return difference between
    241      1.1   cgd 		 * current time and time for the timer to go off.
    242      1.1   cgd 		 */
    243      1.1   cgd 		aitv = p->p_realtimer;
    244      1.1   cgd 		if (timerisset(&aitv.it_value))
    245      1.1   cgd 			if (timercmp(&aitv.it_value, &time, <))
    246      1.1   cgd 				timerclear(&aitv.it_value);
    247      1.1   cgd 			else
    248  1.1.1.2  fvdl 				timevalsub(&aitv.it_value,
    249  1.1.1.2  fvdl 				    (struct timeval *)&time);
    250      1.1   cgd 	} else
    251  1.1.1.3  fvdl 		aitv = p->p_stats->p_timer[SCARG(uap, which)];
    252      1.1   cgd 	splx(s);
    253  1.1.1.3  fvdl 	return (copyout((caddr_t)&aitv, (caddr_t)SCARG(uap, itv),
    254      1.1   cgd 	    sizeof (struct itimerval)));
    255      1.1   cgd }
    256      1.1   cgd 
    257      1.1   cgd /* ARGSUSED */
    258  1.1.1.3  fvdl int
    259      1.1   cgd setitimer(p, uap, retval)
    260      1.1   cgd 	struct proc *p;
    261  1.1.1.3  fvdl 	register struct setitimer_args /* {
    262  1.1.1.3  fvdl 		syscallarg(u_int) which;
    263  1.1.1.3  fvdl 		syscallarg(struct itimerval *) itv;
    264  1.1.1.3  fvdl 		syscallarg(struct itimerval *) oitv;
    265  1.1.1.3  fvdl 	} */ *uap;
    266  1.1.1.3  fvdl 	register_t *retval;
    267      1.1   cgd {
    268      1.1   cgd 	struct itimerval aitv;
    269      1.1   cgd 	register struct itimerval *itvp;
    270      1.1   cgd 	int s, error;
    271      1.1   cgd 
    272  1.1.1.3  fvdl 	if (SCARG(uap, which) > ITIMER_PROF)
    273      1.1   cgd 		return (EINVAL);
    274  1.1.1.3  fvdl 	itvp = SCARG(uap, itv);
    275      1.1   cgd 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
    276      1.1   cgd 	    sizeof(struct itimerval))))
    277      1.1   cgd 		return (error);
    278  1.1.1.3  fvdl 	if ((SCARG(uap, itv) = SCARG(uap, oitv)) &&
    279  1.1.1.3  fvdl 	    (error = getitimer(p, uap, retval)))
    280      1.1   cgd 		return (error);
    281      1.1   cgd 	if (itvp == 0)
    282      1.1   cgd 		return (0);
    283      1.1   cgd 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
    284      1.1   cgd 		return (EINVAL);
    285      1.1   cgd 	s = splclock();
    286  1.1.1.3  fvdl 	if (SCARG(uap, which) == ITIMER_REAL) {
    287      1.1   cgd 		untimeout(realitexpire, (caddr_t)p);
    288      1.1   cgd 		if (timerisset(&aitv.it_value)) {
    289  1.1.1.2  fvdl 			timevaladd(&aitv.it_value, (struct timeval *)&time);
    290      1.1   cgd 			timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
    291      1.1   cgd 		}
    292      1.1   cgd 		p->p_realtimer = aitv;
    293      1.1   cgd 	} else
    294  1.1.1.3  fvdl 		p->p_stats->p_timer[SCARG(uap, which)] = aitv;
    295      1.1   cgd 	splx(s);
    296      1.1   cgd 	return (0);
    297      1.1   cgd }
    298      1.1   cgd 
    299      1.1   cgd /*
    300      1.1   cgd  * Real interval timer expired:
    301      1.1   cgd  * send process whose timer expired an alarm signal.
    302      1.1   cgd  * If time is not set up to reload, then just return.
    303      1.1   cgd  * Else compute next time timer should go off which is > current time.
    304      1.1   cgd  * This is where delay in processing this timeout causes multiple
    305      1.1   cgd  * SIGALRM calls to be compressed into one.
    306      1.1   cgd  */
    307  1.1.1.2  fvdl void
    308  1.1.1.2  fvdl realitexpire(arg)
    309  1.1.1.2  fvdl 	void *arg;
    310      1.1   cgd {
    311  1.1.1.2  fvdl 	register struct proc *p;
    312      1.1   cgd 	int s;
    313      1.1   cgd 
    314  1.1.1.2  fvdl 	p = (struct proc *)arg;
    315      1.1   cgd 	psignal(p, SIGALRM);
    316      1.1   cgd 	if (!timerisset(&p->p_realtimer.it_interval)) {
    317      1.1   cgd 		timerclear(&p->p_realtimer.it_value);
    318      1.1   cgd 		return;
    319      1.1   cgd 	}
    320      1.1   cgd 	for (;;) {
    321      1.1   cgd 		s = splclock();
    322      1.1   cgd 		timevaladd(&p->p_realtimer.it_value,
    323      1.1   cgd 		    &p->p_realtimer.it_interval);
    324      1.1   cgd 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
    325      1.1   cgd 			timeout(realitexpire, (caddr_t)p,
    326      1.1   cgd 			    hzto(&p->p_realtimer.it_value));
    327      1.1   cgd 			splx(s);
    328      1.1   cgd 			return;
    329      1.1   cgd 		}
    330      1.1   cgd 		splx(s);
    331      1.1   cgd 	}
    332      1.1   cgd }
    333      1.1   cgd 
    334      1.1   cgd /*
    335      1.1   cgd  * Check that a proposed value to load into the .it_value or
    336      1.1   cgd  * .it_interval part of an interval timer is acceptable, and
    337      1.1   cgd  * fix it to have at least minimal value (i.e. if it is less
    338      1.1   cgd  * than the resolution of the clock, round it up.)
    339      1.1   cgd  */
    340  1.1.1.3  fvdl int
    341      1.1   cgd itimerfix(tv)
    342      1.1   cgd 	struct timeval *tv;
    343      1.1   cgd {
    344      1.1   cgd 
    345      1.1   cgd 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
    346      1.1   cgd 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    347      1.1   cgd 		return (EINVAL);
    348      1.1   cgd 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    349      1.1   cgd 		tv->tv_usec = tick;
    350      1.1   cgd 	return (0);
    351      1.1   cgd }
    352      1.1   cgd 
    353      1.1   cgd /*
    354      1.1   cgd  * Decrement an interval timer by a specified number
    355      1.1   cgd  * of microseconds, which must be less than a second,
    356      1.1   cgd  * i.e. < 1000000.  If the timer expires, then reload
    357      1.1   cgd  * it.  In this case, carry over (usec - old value) to
    358  1.1.1.2  fvdl  * reduce the value reloaded into the timer so that
    359      1.1   cgd  * the timer does not drift.  This routine assumes
    360      1.1   cgd  * that it is called in a context where the timers
    361      1.1   cgd  * on which it is operating cannot change in value.
    362      1.1   cgd  */
    363  1.1.1.3  fvdl int
    364      1.1   cgd itimerdecr(itp, usec)
    365      1.1   cgd 	register struct itimerval *itp;
    366      1.1   cgd 	int usec;
    367      1.1   cgd {
    368      1.1   cgd 
    369      1.1   cgd 	if (itp->it_value.tv_usec < usec) {
    370      1.1   cgd 		if (itp->it_value.tv_sec == 0) {
    371      1.1   cgd 			/* expired, and already in next interval */
    372      1.1   cgd 			usec -= itp->it_value.tv_usec;
    373      1.1   cgd 			goto expire;
    374      1.1   cgd 		}
    375      1.1   cgd 		itp->it_value.tv_usec += 1000000;
    376      1.1   cgd 		itp->it_value.tv_sec--;
    377      1.1   cgd 	}
    378      1.1   cgd 	itp->it_value.tv_usec -= usec;
    379      1.1   cgd 	usec = 0;
    380      1.1   cgd 	if (timerisset(&itp->it_value))
    381      1.1   cgd 		return (1);
    382      1.1   cgd 	/* expired, exactly at end of interval */
    383      1.1   cgd expire:
    384      1.1   cgd 	if (timerisset(&itp->it_interval)) {
    385      1.1   cgd 		itp->it_value = itp->it_interval;
    386      1.1   cgd 		itp->it_value.tv_usec -= usec;
    387      1.1   cgd 		if (itp->it_value.tv_usec < 0) {
    388      1.1   cgd 			itp->it_value.tv_usec += 1000000;
    389      1.1   cgd 			itp->it_value.tv_sec--;
    390      1.1   cgd 		}
    391      1.1   cgd 	} else
    392      1.1   cgd 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
    393      1.1   cgd 	return (0);
    394      1.1   cgd }
    395      1.1   cgd 
    396      1.1   cgd /*
    397      1.1   cgd  * Add and subtract routines for timevals.
    398      1.1   cgd  * N.B.: subtract routine doesn't deal with
    399      1.1   cgd  * results which are before the beginning,
    400      1.1   cgd  * it just gets very confused in this case.
    401      1.1   cgd  * Caveat emptor.
    402      1.1   cgd  */
    403      1.1   cgd timevaladd(t1, t2)
    404      1.1   cgd 	struct timeval *t1, *t2;
    405      1.1   cgd {
    406      1.1   cgd 
    407      1.1   cgd 	t1->tv_sec += t2->tv_sec;
    408      1.1   cgd 	t1->tv_usec += t2->tv_usec;
    409      1.1   cgd 	timevalfix(t1);
    410      1.1   cgd }
    411      1.1   cgd 
    412      1.1   cgd timevalsub(t1, t2)
    413      1.1   cgd 	struct timeval *t1, *t2;
    414      1.1   cgd {
    415      1.1   cgd 
    416      1.1   cgd 	t1->tv_sec -= t2->tv_sec;
    417      1.1   cgd 	t1->tv_usec -= t2->tv_usec;
    418      1.1   cgd 	timevalfix(t1);
    419      1.1   cgd }
    420      1.1   cgd 
    421      1.1   cgd timevalfix(t1)
    422      1.1   cgd 	struct timeval *t1;
    423      1.1   cgd {
    424      1.1   cgd 
    425      1.1   cgd 	if (t1->tv_usec < 0) {
    426      1.1   cgd 		t1->tv_sec--;
    427      1.1   cgd 		t1->tv_usec += 1000000;
    428      1.1   cgd 	}
    429      1.1   cgd 	if (t1->tv_usec >= 1000000) {
    430      1.1   cgd 		t1->tv_sec++;
    431      1.1   cgd 		t1->tv_usec -= 1000000;
    432      1.1   cgd 	}
    433      1.1   cgd }
    434