The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_time.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    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  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)kern_time.c 8.1 (Berkeley) 6/10/93
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD$");
   34 
   35 #include "opt_mac.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/lock.h>
   40 #include <sys/mutex.h>
   41 #include <sys/sysproto.h>
   42 #include <sys/resourcevar.h>
   43 #include <sys/signalvar.h>
   44 #include <sys/kernel.h>
   45 #include <sys/mac.h>
   46 #include <sys/syscallsubr.h>
   47 #include <sys/sysent.h>
   48 #include <sys/proc.h>
   49 #include <sys/time.h>
   50 #include <sys/timetc.h>
   51 #include <sys/vnode.h>
   52 
   53 #include <vm/vm.h>
   54 #include <vm/vm_extern.h>
   55 
   56 int tz_minuteswest;
   57 int tz_dsttime;
   58 
   59 /*
   60  * Time of day and interval timer support.
   61  *
   62  * These routines provide the kernel entry points to get and set
   63  * the time-of-day and per-process interval timers.  Subroutines
   64  * here provide support for adding and subtracting timeval structures
   65  * and decrementing interval timers, optionally reloading the interval
   66  * timers when they expire.
   67  */
   68 
   69 static int      settime(struct thread *, struct timeval *);
   70 static void     timevalfix(struct timeval *);
   71 static void     no_lease_updatetime(int);
   72 
   73 static void 
   74 no_lease_updatetime(deltat)
   75         int deltat;
   76 {
   77 }
   78 
   79 void (*lease_updatetime)(int)  = no_lease_updatetime;
   80 
   81 static int
   82 settime(struct thread *td, struct timeval *tv)
   83 {
   84         struct timeval delta, tv1, tv2;
   85         static struct timeval maxtime, laststep;
   86         struct timespec ts;
   87         int s;
   88 
   89         s = splclock();
   90         microtime(&tv1);
   91         delta = *tv;
   92         timevalsub(&delta, &tv1);
   93 
   94         /*
   95          * If the system is secure, we do not allow the time to be 
   96          * set to a value earlier than 1 second less than the highest
   97          * time we have yet seen. The worst a miscreant can do in
   98          * this circumstance is "freeze" time. He couldn't go
   99          * back to the past.
  100          *
  101          * We similarly do not allow the clock to be stepped more
  102          * than one second, nor more than once per second. This allows
  103          * a miscreant to make the clock march double-time, but no worse.
  104          */
  105         if (securelevel_gt(td->td_ucred, 1) != 0) {
  106                 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
  107                         /*
  108                          * Update maxtime to latest time we've seen.
  109                          */
  110                         if (tv1.tv_sec > maxtime.tv_sec)
  111                                 maxtime = tv1;
  112                         tv2 = *tv;
  113                         timevalsub(&tv2, &maxtime);
  114                         if (tv2.tv_sec < -1) {
  115                                 tv->tv_sec = maxtime.tv_sec - 1;
  116                                 printf("Time adjustment clamped to -1 second\n");
  117                         }
  118                 } else {
  119                         if (tv1.tv_sec == laststep.tv_sec) {
  120                                 splx(s);
  121                                 return (EPERM);
  122                         }
  123                         if (delta.tv_sec > 1) {
  124                                 tv->tv_sec = tv1.tv_sec + 1;
  125                                 printf("Time adjustment clamped to +1 second\n");
  126                         }
  127                         laststep = *tv;
  128                 }
  129         }
  130 
  131         ts.tv_sec = tv->tv_sec;
  132         ts.tv_nsec = tv->tv_usec * 1000;
  133         mtx_lock(&Giant);
  134         tc_setclock(&ts);
  135         (void) splsoftclock();
  136         lease_updatetime(delta.tv_sec);
  137         splx(s);
  138         resettodr();
  139         mtx_unlock(&Giant);
  140         return (0);
  141 }
  142 
  143 #ifndef _SYS_SYSPROTO_H_
  144 struct clock_gettime_args {
  145         clockid_t clock_id;
  146         struct  timespec *tp;
  147 };
  148 #endif
  149 
  150 /*
  151  * MPSAFE
  152  */
  153 /* ARGSUSED */
  154 int
  155 clock_gettime(struct thread *td, struct clock_gettime_args *uap)
  156 {
  157         struct timespec ats;
  158         int error;
  159 
  160         error = kern_clock_gettime(td, uap->clock_id, &ats);
  161         if (error == 0)
  162                 error = copyout(&ats, uap->tp, sizeof(ats));
  163 
  164         return (error);
  165 }
  166 
  167 int
  168 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
  169 {
  170         struct timeval sys, user;
  171         struct proc *p;
  172 
  173         p = td->td_proc;
  174         switch (clock_id) {
  175         case CLOCK_REALTIME:
  176                 nanotime(ats);
  177                 break;
  178         case CLOCK_VIRTUAL:
  179                 PROC_LOCK(p);
  180                 calcru(p, &user, &sys);
  181                 PROC_UNLOCK(p);
  182                 TIMEVAL_TO_TIMESPEC(&user, ats);
  183                 break;
  184         case CLOCK_PROF:
  185                 PROC_LOCK(p);
  186                 calcru(p, &user, &sys);
  187                 PROC_UNLOCK(p);
  188                 timevaladd(&user, &sys);
  189                 TIMEVAL_TO_TIMESPEC(&user, ats);
  190                 break;
  191         case CLOCK_MONOTONIC:
  192                 nanouptime(ats);
  193                 break;
  194         default:
  195                 return (EINVAL);
  196         }
  197         return (0);
  198 }
  199 
  200 #ifndef _SYS_SYSPROTO_H_
  201 struct clock_settime_args {
  202         clockid_t clock_id;
  203         const struct    timespec *tp;
  204 };
  205 #endif
  206 
  207 /*
  208  * MPSAFE
  209  */
  210 /* ARGSUSED */
  211 int
  212 clock_settime(struct thread *td, struct clock_settime_args *uap)
  213 {
  214         struct timespec ats;
  215         int error;
  216 
  217         if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
  218                 return (error);
  219         return (kern_clock_settime(td, uap->clock_id, &ats));
  220 }
  221 
  222 int
  223 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
  224 {
  225         struct timeval atv;
  226         int error;
  227 
  228 #ifdef MAC
  229         error = mac_check_system_settime(td->td_ucred);
  230         if (error)
  231                 return (error);
  232 #endif
  233         if ((error = suser(td)) != 0)
  234                 return (error);
  235         if (clock_id != CLOCK_REALTIME)
  236                 return (EINVAL);
  237         if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
  238                 return (EINVAL);
  239         /* XXX Don't convert nsec->usec and back */
  240         TIMESPEC_TO_TIMEVAL(&atv, ats);
  241         error = settime(td, &atv);
  242         return (error);
  243 }
  244 
  245 #ifndef _SYS_SYSPROTO_H_
  246 struct clock_getres_args {
  247         clockid_t clock_id;
  248         struct  timespec *tp;
  249 };
  250 #endif
  251 
  252 int
  253 clock_getres(struct thread *td, struct clock_getres_args *uap)
  254 {
  255         struct timespec ts;
  256         int error;
  257 
  258         if (uap->tp == NULL)
  259                 return (0);
  260 
  261         error = kern_clock_getres(td, uap->clock_id, &ts);
  262         if (error == 0)
  263                 error = copyout(&ts, uap->tp, sizeof(ts));
  264         return (error);
  265 }
  266 
  267 int
  268 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
  269 {
  270 
  271         ts->tv_sec = 0;
  272         switch (clock_id) {
  273         case CLOCK_REALTIME:
  274         case CLOCK_MONOTONIC:
  275                 /*
  276                  * Round up the result of the division cheaply by adding 1.
  277                  * Rounding up is especially important if rounding down
  278                  * would give 0.  Perfect rounding is unimportant.
  279                  */
  280                 ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
  281                 break;
  282         case CLOCK_VIRTUAL:
  283         case CLOCK_PROF:
  284                 /* Accurately round up here because we can do so cheaply. */
  285                 ts->tv_nsec = (1000000000 + hz - 1) / hz;
  286                 break;
  287         default:
  288                 return (EINVAL);
  289         }
  290         return (0);
  291 }
  292 
  293 static int nanowait;
  294 
  295 int
  296 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
  297 {
  298         struct timespec ts, ts2, ts3;
  299         struct timeval tv;
  300         int error;
  301 
  302         if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
  303                 return (EINVAL);
  304         if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
  305                 return (0);
  306         getnanouptime(&ts);
  307         timespecadd(&ts, rqt);
  308         TIMESPEC_TO_TIMEVAL(&tv, rqt);
  309         for (;;) {
  310                 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
  311                     tvtohz(&tv));
  312                 getnanouptime(&ts2);
  313                 if (error != EWOULDBLOCK) {
  314                         if (error == ERESTART)
  315                                 error = EINTR;
  316                         if (rmt != NULL) {
  317                                 timespecsub(&ts, &ts2);
  318                                 if (ts.tv_sec < 0)
  319                                         timespecclear(&ts);
  320                                 *rmt = ts;
  321                         }
  322                         return (error);
  323                 }
  324                 if (timespeccmp(&ts2, &ts, >=))
  325                         return (0);
  326                 ts3 = ts;
  327                 timespecsub(&ts3, &ts2);
  328                 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
  329         }
  330 }
  331 
  332 #ifndef _SYS_SYSPROTO_H_
  333 struct nanosleep_args {
  334         struct  timespec *rqtp;
  335         struct  timespec *rmtp;
  336 };
  337 #endif
  338 
  339 /* 
  340  * MPSAFE
  341  */
  342 /* ARGSUSED */
  343 int
  344 nanosleep(struct thread *td, struct nanosleep_args *uap)
  345 {
  346         struct timespec rmt, rqt;
  347         int error;
  348 
  349         error = copyin(uap->rqtp, &rqt, sizeof(rqt));
  350         if (error)
  351                 return (error);
  352 
  353         if (uap->rmtp &&
  354             !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
  355                         return (EFAULT);
  356         error = kern_nanosleep(td, &rqt, &rmt);
  357         if (error && uap->rmtp) {
  358                 int error2;
  359 
  360                 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
  361                 if (error2)
  362                         error = error2;
  363         }
  364         return (error);
  365 }
  366 
  367 #ifndef _SYS_SYSPROTO_H_
  368 struct gettimeofday_args {
  369         struct  timeval *tp;
  370         struct  timezone *tzp;
  371 };
  372 #endif
  373 /*
  374  * MPSAFE
  375  */
  376 /* ARGSUSED */
  377 int
  378 gettimeofday(struct thread *td, struct gettimeofday_args *uap)
  379 {
  380         struct timeval atv;
  381         struct timezone rtz;
  382         int error = 0;
  383 
  384         if (uap->tp) {
  385                 microtime(&atv);
  386                 error = copyout(&atv, uap->tp, sizeof (atv));
  387         }
  388         if (error == 0 && uap->tzp != NULL) {
  389                 rtz.tz_minuteswest = tz_minuteswest;
  390                 rtz.tz_dsttime = tz_dsttime;
  391                 error = copyout(&rtz, uap->tzp, sizeof (rtz));
  392         }
  393         return (error);
  394 }
  395 
  396 #ifndef _SYS_SYSPROTO_H_
  397 struct settimeofday_args {
  398         struct  timeval *tv;
  399         struct  timezone *tzp;
  400 };
  401 #endif
  402 /*
  403  * MPSAFE
  404  */
  405 /* ARGSUSED */
  406 int
  407 settimeofday(struct thread *td, struct settimeofday_args *uap)
  408 {
  409         struct timeval atv, *tvp;
  410         struct timezone atz, *tzp;
  411         int error;
  412 
  413         if (uap->tv) {
  414                 error = copyin(uap->tv, &atv, sizeof(atv));
  415                 if (error)
  416                         return (error);
  417                 tvp = &atv;
  418         } else
  419                 tvp = NULL;
  420         if (uap->tzp) {
  421                 error = copyin(uap->tzp, &atz, sizeof(atz));
  422                 if (error)
  423                         return (error);
  424                 tzp = &atz;
  425         } else
  426                 tzp = NULL;
  427         return (kern_settimeofday(td, tvp, tzp));
  428 }
  429 
  430 int
  431 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
  432 {
  433         int error;
  434 
  435 #ifdef MAC
  436         error = mac_check_system_settime(td->td_ucred);
  437         if (error)
  438                 return (error);
  439 #endif
  440         error = suser(td);
  441         if (error)
  442                 return (error);
  443         /* Verify all parameters before changing time. */
  444         if (tv) {
  445                 if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
  446                         return (EINVAL);
  447                 error = settime(td, tv);
  448         }
  449         if (tzp && error == 0) {
  450                 tz_minuteswest = tzp->tz_minuteswest;
  451                 tz_dsttime = tzp->tz_dsttime;
  452         }
  453         return (error);
  454 }
  455 
  456 /*
  457  * Get value of an interval timer.  The process virtual and
  458  * profiling virtual time timers are kept in the p_stats area, since
  459  * they can be swapped out.  These are kept internally in the
  460  * way they are specified externally: in time until they expire.
  461  *
  462  * The real time interval timer is kept in the process table slot
  463  * for the process, and its value (it_value) is kept as an
  464  * absolute time rather than as a delta, so that it is easy to keep
  465  * periodic real-time signals from drifting.
  466  *
  467  * Virtual time timers are processed in the hardclock() routine of
  468  * kern_clock.c.  The real time timer is processed by a timeout
  469  * routine, called from the softclock() routine.  Since a callout
  470  * may be delayed in real time due to interrupt processing in the system,
  471  * it is possible for the real time timeout routine (realitexpire, given below),
  472  * to be delayed in real time past when it is supposed to occur.  It
  473  * does not suffice, therefore, to reload the real timer .it_value from the
  474  * real time timers .it_interval.  Rather, we compute the next time in
  475  * absolute time the timer should go off.
  476  */
  477 #ifndef _SYS_SYSPROTO_H_
  478 struct getitimer_args {
  479         u_int   which;
  480         struct  itimerval *itv;
  481 };
  482 #endif
  483 /*
  484  * MPSAFE
  485  */
  486 int
  487 getitimer(struct thread *td, struct getitimer_args *uap)
  488 {
  489         struct itimerval aitv;
  490         int error;
  491 
  492         error = kern_getitimer(td, uap->which, &aitv);
  493         if (error != 0)
  494                 return (error);
  495         return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
  496 }
  497 
  498 int
  499 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
  500 {
  501         struct proc *p = td->td_proc;
  502         struct timeval ctv;
  503 
  504         if (which > ITIMER_PROF)
  505                 return (EINVAL);
  506 
  507         if (which == ITIMER_REAL) {
  508                 /*
  509                  * Convert from absolute to relative time in .it_value
  510                  * part of real time timer.  If time for real time timer
  511                  * has passed return 0, else return difference between
  512                  * current time and time for the timer to go off.
  513                  */
  514                 PROC_LOCK(p);
  515                 *aitv = p->p_realtimer;
  516                 PROC_UNLOCK(p);
  517                 if (timevalisset(&aitv->it_value)) {
  518                         getmicrouptime(&ctv);
  519                         if (timevalcmp(&aitv->it_value, &ctv, <))
  520                                 timevalclear(&aitv->it_value);
  521                         else
  522                                 timevalsub(&aitv->it_value, &ctv);
  523                 }
  524         } else {
  525                 mtx_lock_spin(&sched_lock);
  526                 *aitv = p->p_stats->p_timer[which];
  527                 mtx_unlock_spin(&sched_lock);
  528         }
  529         return (0);
  530 }
  531 
  532 #ifndef _SYS_SYSPROTO_H_
  533 struct setitimer_args {
  534         u_int   which;
  535         struct  itimerval *itv, *oitv;
  536 };
  537 #endif
  538 
  539 /*
  540  * MPSAFE
  541  */
  542 int
  543 setitimer(struct thread *td, struct setitimer_args *uap)
  544 {
  545         struct itimerval aitv, oitv;
  546         int error;
  547 
  548         if (uap->itv == NULL) {
  549                 uap->itv = uap->oitv;
  550                 return (getitimer(td, (struct getitimer_args *)uap));
  551         }
  552 
  553         if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
  554                 return (error);
  555         error = kern_setitimer(td, uap->which, &aitv, &oitv);
  556         if (error != 0 || uap->oitv == NULL)
  557                 return (error);
  558         return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
  559 }
  560 
  561 int
  562 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
  563     struct itimerval *oitv)
  564 {
  565         struct proc *p = td->td_proc;
  566         struct timeval ctv;
  567 
  568         if (aitv == NULL)
  569                 return (kern_getitimer(td, which, oitv));
  570 
  571         if (which > ITIMER_PROF)
  572                 return (EINVAL);
  573         if (itimerfix(&aitv->it_value))
  574                 return (EINVAL);
  575         if (!timevalisset(&aitv->it_value))
  576                 timevalclear(&aitv->it_interval);
  577         else if (itimerfix(&aitv->it_interval))
  578                 return (EINVAL);
  579 
  580         if (which == ITIMER_REAL) {
  581                 PROC_LOCK(p);
  582                 if (timevalisset(&p->p_realtimer.it_value))
  583                         callout_stop(&p->p_itcallout);
  584                 getmicrouptime(&ctv);
  585                 if (timevalisset(&aitv->it_value)) {
  586                         callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
  587                             realitexpire, p);
  588                         timevaladd(&aitv->it_value, &ctv);
  589                 }
  590                 *oitv = p->p_realtimer;
  591                 p->p_realtimer = *aitv;
  592                 PROC_UNLOCK(p);
  593                 if (timevalisset(&oitv->it_value)) {
  594                         if (timevalcmp(&oitv->it_value, &ctv, <))
  595                                 timevalclear(&oitv->it_value);
  596                         else
  597                                 timevalsub(&oitv->it_value, &ctv);
  598                 }
  599         } else {
  600                 mtx_lock_spin(&sched_lock);
  601                 *oitv = p->p_stats->p_timer[which];
  602                 p->p_stats->p_timer[which] = *aitv;
  603                 mtx_unlock_spin(&sched_lock);
  604         }
  605         return (0);
  606 }
  607 
  608 /*
  609  * Real interval timer expired:
  610  * send process whose timer expired an alarm signal.
  611  * If time is not set up to reload, then just return.
  612  * Else compute next time timer should go off which is > current time.
  613  * This is where delay in processing this timeout causes multiple
  614  * SIGALRM calls to be compressed into one.
  615  * tvtohz() always adds 1 to allow for the time until the next clock
  616  * interrupt being strictly less than 1 clock tick, but we don't want
  617  * that here since we want to appear to be in sync with the clock
  618  * interrupt even when we're delayed.
  619  */
  620 void
  621 realitexpire(void *arg)
  622 {
  623         struct proc *p;
  624         struct timeval ctv, ntv;
  625 
  626         p = (struct proc *)arg;
  627         PROC_LOCK(p);
  628         psignal(p, SIGALRM);
  629         if (!timevalisset(&p->p_realtimer.it_interval)) {
  630                 timevalclear(&p->p_realtimer.it_value);
  631                 if (p->p_flag & P_WEXIT)
  632                         wakeup(&p->p_itcallout);
  633                 PROC_UNLOCK(p);
  634                 return;
  635         }
  636         for (;;) {
  637                 timevaladd(&p->p_realtimer.it_value,
  638                     &p->p_realtimer.it_interval);
  639                 getmicrouptime(&ctv);
  640                 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
  641                         ntv = p->p_realtimer.it_value;
  642                         timevalsub(&ntv, &ctv);
  643                         callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
  644                             realitexpire, p);
  645                         PROC_UNLOCK(p);
  646                         return;
  647                 }
  648         }
  649         /*NOTREACHED*/
  650 }
  651 
  652 /*
  653  * Check that a proposed value to load into the .it_value or
  654  * .it_interval part of an interval timer is acceptable, and
  655  * fix it to have at least minimal value (i.e. if it is less
  656  * than the resolution of the clock, round it up.)
  657  */
  658 int
  659 itimerfix(struct timeval *tv)
  660 {
  661 
  662         if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
  663             tv->tv_usec < 0 || tv->tv_usec >= 1000000)
  664                 return (EINVAL);
  665         if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
  666                 tv->tv_usec = tick;
  667         return (0);
  668 }
  669 
  670 /*
  671  * Decrement an interval timer by a specified number
  672  * of microseconds, which must be less than a second,
  673  * i.e. < 1000000.  If the timer expires, then reload
  674  * it.  In this case, carry over (usec - old value) to
  675  * reduce the value reloaded into the timer so that
  676  * the timer does not drift.  This routine assumes
  677  * that it is called in a context where the timers
  678  * on which it is operating cannot change in value.
  679  */
  680 int
  681 itimerdecr(struct itimerval *itp, int usec)
  682 {
  683 
  684         if (itp->it_value.tv_usec < usec) {
  685                 if (itp->it_value.tv_sec == 0) {
  686                         /* expired, and already in next interval */
  687                         usec -= itp->it_value.tv_usec;
  688                         goto expire;
  689                 }
  690                 itp->it_value.tv_usec += 1000000;
  691                 itp->it_value.tv_sec--;
  692         }
  693         itp->it_value.tv_usec -= usec;
  694         usec = 0;
  695         if (timevalisset(&itp->it_value))
  696                 return (1);
  697         /* expired, exactly at end of interval */
  698 expire:
  699         if (timevalisset(&itp->it_interval)) {
  700                 itp->it_value = itp->it_interval;
  701                 itp->it_value.tv_usec -= usec;
  702                 if (itp->it_value.tv_usec < 0) {
  703                         itp->it_value.tv_usec += 1000000;
  704                         itp->it_value.tv_sec--;
  705                 }
  706         } else
  707                 itp->it_value.tv_usec = 0;              /* sec is already 0 */
  708         return (0);
  709 }
  710 
  711 /*
  712  * Add and subtract routines for timevals.
  713  * N.B.: subtract routine doesn't deal with
  714  * results which are before the beginning,
  715  * it just gets very confused in this case.
  716  * Caveat emptor.
  717  */
  718 void
  719 timevaladd(struct timeval *t1, const struct timeval *t2)
  720 {
  721 
  722         t1->tv_sec += t2->tv_sec;
  723         t1->tv_usec += t2->tv_usec;
  724         timevalfix(t1);
  725 }
  726 
  727 void
  728 timevalsub(struct timeval *t1, const struct timeval *t2)
  729 {
  730 
  731         t1->tv_sec -= t2->tv_sec;
  732         t1->tv_usec -= t2->tv_usec;
  733         timevalfix(t1);
  734 }
  735 
  736 static void
  737 timevalfix(struct timeval *t1)
  738 {
  739 
  740         if (t1->tv_usec < 0) {
  741                 t1->tv_sec--;
  742                 t1->tv_usec += 1000000;
  743         }
  744         if (t1->tv_usec >= 1000000) {
  745                 t1->tv_sec++;
  746                 t1->tv_usec -= 1000000;
  747         }
  748 }
  749 
  750 /*
  751  * ratecheck(): simple time-based rate-limit checking.
  752  */
  753 int
  754 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
  755 {
  756         struct timeval tv, delta;
  757         int rv = 0;
  758 
  759         getmicrouptime(&tv);            /* NB: 10ms precision */
  760         delta = tv;
  761         timevalsub(&delta, lasttime);
  762 
  763         /*
  764          * check for 0,0 is so that the message will be seen at least once,
  765          * even if interval is huge.
  766          */
  767         if (timevalcmp(&delta, mininterval, >=) ||
  768             (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
  769                 *lasttime = tv;
  770                 rv = 1;
  771         }
  772 
  773         return (rv);
  774 }
  775 
  776 /*
  777  * ppsratecheck(): packets (or events) per second limitation.
  778  *
  779  * Return 0 if the limit is to be enforced (e.g. the caller
  780  * should drop a packet because of the rate limitation).
  781  *
  782  * maxpps of 0 always causes zero to be returned.  maxpps of -1
  783  * always causes 1 to be returned; this effectively defeats rate
  784  * limiting.
  785  *
  786  * Note that we maintain the struct timeval for compatibility
  787  * with other bsd systems.  We reuse the storage and just monitor
  788  * clock ticks for minimal overhead.  
  789  */
  790 int
  791 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
  792 {
  793         int now;
  794 
  795         /*
  796          * Reset the last time and counter if this is the first call
  797          * or more than a second has passed since the last update of
  798          * lasttime.
  799          */
  800         now = ticks;
  801         if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
  802                 lasttime->tv_sec = now;
  803                 *curpps = 1;
  804                 return (maxpps != 0);
  805         } else {
  806                 (*curpps)++;            /* NB: ignore potential overflow */
  807                 return (maxpps < 0 || *curpps < maxpps);
  808         }
  809 }

Cache object: 1fb64fa6f788cffb4cbb23e3d39f8293


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.