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_resource.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: kern_resource.c,v 1.87.2.1 2005/09/18 20:09:50 tron Exp $      */
    2 
    3 /*-
    4  * Copyright (c) 1982, 1986, 1991, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  * (c) UNIX System Laboratories, Inc.
    7  * All or some portions of this file are derived from material licensed
    8  * to the University of California by American Telephone and Telegraph
    9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   10  * the permission of UNIX System Laboratories, Inc.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  *
   36  *      @(#)kern_resource.c     8.8 (Berkeley) 2/14/95
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.87.2.1 2005/09/18 20:09:50 tron Exp $");
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/kernel.h>
   45 #include <sys/file.h>
   46 #include <sys/resourcevar.h>
   47 #include <sys/malloc.h>
   48 #include <sys/pool.h>
   49 #include <sys/proc.h>
   50 #include <sys/sysctl.h>
   51 
   52 #include <sys/mount.h>
   53 #include <sys/sa.h>
   54 #include <sys/syscallargs.h>
   55 
   56 #include <uvm/uvm_extern.h>
   57 
   58 /*
   59  * Maximum process data and stack limits.
   60  * They are variables so they are patchable.
   61  */
   62 rlim_t maxdmap = MAXDSIZ;
   63 rlim_t maxsmap = MAXSSIZ;
   64 
   65 struct uihashhead *uihashtbl;
   66 u_long uihash;          /* size of hash table - 1 */
   67 
   68 static struct uidinfo *getuidinfo(uid_t);
   69 static void freeuidinfo(struct uidinfo *);
   70 static struct uidinfo *allocuidinfo(uid_t);
   71 
   72 /*
   73  * Resource controls and accounting.
   74  */
   75 
   76 int
   77 sys_getpriority(l, v, retval)
   78         struct lwp *l;
   79         void *v;
   80         register_t *retval;
   81 {
   82         struct sys_getpriority_args /* {
   83                 syscallarg(int) which;
   84                 syscallarg(id_t) who;
   85         } */ *uap = v;
   86         struct proc *curp = l->l_proc, *p;
   87         int low = NZERO + PRIO_MAX + 1;
   88 
   89         switch (SCARG(uap, which)) {
   90 
   91         case PRIO_PROCESS:
   92                 if (SCARG(uap, who) == 0)
   93                         p = curp;
   94                 else
   95                         p = pfind(SCARG(uap, who));
   96                 if (p == 0)
   97                         break;
   98                 low = p->p_nice;
   99                 break;
  100 
  101         case PRIO_PGRP: {
  102                 struct pgrp *pg;
  103 
  104                 if (SCARG(uap, who) == 0)
  105                         pg = curp->p_pgrp;
  106                 else if ((pg = pgfind(SCARG(uap, who))) == NULL)
  107                         break;
  108                 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
  109                         if (p->p_nice < low)
  110                                 low = p->p_nice;
  111                 }
  112                 break;
  113         }
  114 
  115         case PRIO_USER:
  116                 if (SCARG(uap, who) == 0)
  117                         SCARG(uap, who) = curp->p_ucred->cr_uid;
  118                 proclist_lock_read();
  119                 PROCLIST_FOREACH(p, &allproc) {
  120                         if (p->p_ucred->cr_uid == (uid_t) SCARG(uap, who) &&
  121                             p->p_nice < low)
  122                                 low = p->p_nice;
  123                 }
  124                 proclist_unlock_read();
  125                 break;
  126 
  127         default:
  128                 return (EINVAL);
  129         }
  130         if (low == NZERO + PRIO_MAX + 1)
  131                 return (ESRCH);
  132         *retval = low - NZERO;
  133         return (0);
  134 }
  135 
  136 /* ARGSUSED */
  137 int
  138 sys_setpriority(l, v, retval)
  139         struct lwp *l;
  140         void *v;
  141         register_t *retval;
  142 {
  143         struct sys_setpriority_args /* {
  144                 syscallarg(int) which;
  145                 syscallarg(id_t) who;
  146                 syscallarg(int) prio;
  147         } */ *uap = v;
  148         struct proc *curp = l->l_proc, *p;
  149         int found = 0, error = 0;
  150 
  151         switch (SCARG(uap, which)) {
  152 
  153         case PRIO_PROCESS:
  154                 if (SCARG(uap, who) == 0)
  155                         p = curp;
  156                 else
  157                         p = pfind(SCARG(uap, who));
  158                 if (p == 0)
  159                         break;
  160                 error = donice(curp, p, SCARG(uap, prio));
  161                 found++;
  162                 break;
  163 
  164         case PRIO_PGRP: {
  165                 struct pgrp *pg;
  166 
  167                 if (SCARG(uap, who) == 0)
  168                         pg = curp->p_pgrp;
  169                 else if ((pg = pgfind(SCARG(uap, who))) == NULL)
  170                         break;
  171                 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
  172                         error = donice(curp, p, SCARG(uap, prio));
  173                         found++;
  174                 }
  175                 break;
  176         }
  177 
  178         case PRIO_USER:
  179                 if (SCARG(uap, who) == 0)
  180                         SCARG(uap, who) = curp->p_ucred->cr_uid;
  181                 proclist_lock_read();
  182                 PROCLIST_FOREACH(p, &allproc) {
  183                         if (p->p_ucred->cr_uid == (uid_t) SCARG(uap, who)) {
  184                                 error = donice(curp, p, SCARG(uap, prio));
  185                                 found++;
  186                         }
  187                 }
  188                 proclist_unlock_read();
  189                 break;
  190 
  191         default:
  192                 return (EINVAL);
  193         }
  194         if (found == 0)
  195                 return (ESRCH);
  196         return (error);
  197 }
  198 
  199 int
  200 donice(curp, chgp, n)
  201         struct proc *curp, *chgp;
  202         int n;
  203 {
  204         struct pcred *pcred = curp->p_cred;
  205         int s;
  206 
  207         if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
  208             pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
  209             pcred->p_ruid != chgp->p_ucred->cr_uid)
  210                 return (EPERM);
  211         if (n > PRIO_MAX)
  212                 n = PRIO_MAX;
  213         if (n < PRIO_MIN)
  214                 n = PRIO_MIN;
  215         n += NZERO;
  216         if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
  217                 return (EACCES);
  218         chgp->p_nice = n;
  219         SCHED_LOCK(s);
  220         (void)resetprocpriority(chgp);
  221         SCHED_UNLOCK(s);
  222         return (0);
  223 }
  224 
  225 /* ARGSUSED */
  226 int
  227 sys_setrlimit(l, v, retval)
  228         struct lwp *l;
  229         void *v;
  230         register_t *retval;
  231 {
  232         struct sys_setrlimit_args /* {
  233                 syscallarg(int) which;
  234                 syscallarg(const struct rlimit *) rlp;
  235         } */ *uap = v;
  236         struct proc *p = l->l_proc;
  237         int which = SCARG(uap, which);
  238         struct rlimit alim;
  239         int error;
  240 
  241         error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
  242         if (error)
  243                 return (error);
  244         return (dosetrlimit(p, p->p_cred, which, &alim));
  245 }
  246 
  247 int
  248 dosetrlimit(p, cred, which, limp)
  249         struct proc *p;
  250         struct  pcred *cred;
  251         int which;
  252         struct rlimit *limp;
  253 {
  254         struct rlimit *alimp;
  255         struct plimit *oldplim;
  256         int error;
  257 
  258         if ((u_int)which >= RLIM_NLIMITS)
  259                 return (EINVAL);
  260 
  261         if (limp->rlim_cur < 0 || limp->rlim_max < 0)
  262                 return (EINVAL);
  263 
  264         alimp = &p->p_rlimit[which];
  265         /* if we don't change the value, no need to limcopy() */
  266         if (limp->rlim_cur == alimp->rlim_cur &&
  267             limp->rlim_max == alimp->rlim_max)
  268                 return 0;
  269 
  270         if (limp->rlim_cur > limp->rlim_max) {
  271                 /*
  272                  * This is programming error. According to SUSv2, we should
  273                  * return error in this case.
  274                  */
  275                 return (EINVAL);
  276         }
  277         if (limp->rlim_max > alimp->rlim_max
  278             && (error = suser(cred->pc_ucred, &p->p_acflag)) != 0)
  279                         return (error);
  280 
  281         if (p->p_limit->p_refcnt > 1 &&
  282             (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
  283                 p->p_limit = limcopy(oldplim = p->p_limit);
  284                 limfree(oldplim);
  285                 alimp = &p->p_rlimit[which];
  286         }
  287 
  288         switch (which) {
  289 
  290         case RLIMIT_DATA:
  291                 if (limp->rlim_cur > maxdmap)
  292                         limp->rlim_cur = maxdmap;
  293                 if (limp->rlim_max > maxdmap)
  294                         limp->rlim_max = maxdmap;
  295                 break;
  296 
  297         case RLIMIT_STACK:
  298                 if (limp->rlim_cur > maxsmap)
  299                         limp->rlim_cur = maxsmap;
  300                 if (limp->rlim_max > maxsmap)
  301                         limp->rlim_max = maxsmap;
  302 
  303                 /*
  304                  * Return EINVAL if the new stack size limit is lower than
  305                  * current usage. Otherwise, the process would get SIGSEGV the
  306                  * moment it would try to access anything on it's current stack.
  307                  * This conforms to SUSv2.
  308                  */
  309                 if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE
  310                     || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE)
  311                         return (EINVAL);
  312 
  313                 /*
  314                  * Stack is allocated to the max at exec time with
  315                  * only "rlim_cur" bytes accessible (In other words,
  316                  * allocates stack dividing two contiguous regions at
  317                  * "rlim_cur" bytes boundary).
  318                  *
  319                  * Since allocation is done in terms of page, roundup
  320                  * "rlim_cur" (otherwise, contiguous regions
  321                  * overlap).  If stack limit is going up make more
  322                  * accessible, if going down make inaccessible.
  323                  */
  324                 limp->rlim_cur = round_page(limp->rlim_cur);
  325                 if (limp->rlim_cur != alimp->rlim_cur) {
  326                         vaddr_t addr;
  327                         vsize_t size;
  328                         vm_prot_t prot;
  329 
  330                         if (limp->rlim_cur > alimp->rlim_cur) {
  331                                 prot = VM_PROT_READ | VM_PROT_WRITE;
  332                                 size = limp->rlim_cur - alimp->rlim_cur;
  333                                 addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
  334                                     limp->rlim_cur;
  335                         } else {
  336                                 prot = VM_PROT_NONE;
  337                                 size = alimp->rlim_cur - limp->rlim_cur;
  338                                 addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
  339                                      alimp->rlim_cur;
  340                         }
  341                         (void) uvm_map_protect(&p->p_vmspace->vm_map,
  342                                               addr, addr+size, prot, FALSE);
  343                 }
  344                 break;
  345 
  346         case RLIMIT_NOFILE:
  347                 if (limp->rlim_cur > maxfiles)
  348                         limp->rlim_cur = maxfiles;
  349                 if (limp->rlim_max > maxfiles)
  350                         limp->rlim_max = maxfiles;
  351                 break;
  352 
  353         case RLIMIT_NPROC:
  354                 if (limp->rlim_cur > maxproc)
  355                         limp->rlim_cur = maxproc;
  356                 if (limp->rlim_max > maxproc)
  357                         limp->rlim_max = maxproc;
  358                 break;
  359         }
  360         *alimp = *limp;
  361         return (0);
  362 }
  363 
  364 /* ARGSUSED */
  365 int
  366 sys_getrlimit(l, v, retval)
  367         struct lwp *l;
  368         void *v;
  369         register_t *retval;
  370 {
  371         struct sys_getrlimit_args /* {
  372                 syscallarg(int) which;
  373                 syscallarg(struct rlimit *) rlp;
  374         } */ *uap = v;
  375         struct proc *p = l->l_proc;
  376         int which = SCARG(uap, which);
  377 
  378         if ((u_int)which >= RLIM_NLIMITS)
  379                 return (EINVAL);
  380         return (copyout(&p->p_rlimit[which], SCARG(uap, rlp),
  381             sizeof(struct rlimit)));
  382 }
  383 
  384 /*
  385  * Transform the running time and tick information in proc p into user,
  386  * system, and interrupt time usage.
  387  */
  388 void
  389 calcru(p, up, sp, ip)
  390         struct proc *p;
  391         struct timeval *up;
  392         struct timeval *sp;
  393         struct timeval *ip;
  394 {
  395         u_quad_t u, st, ut, it, tot;
  396         unsigned long sec;
  397         long usec;
  398         int s;
  399         struct timeval tv;
  400         struct lwp *l;
  401 
  402         s = splstatclock();
  403         st = p->p_sticks;
  404         ut = p->p_uticks;
  405         it = p->p_iticks;
  406         splx(s);
  407 
  408         sec = p->p_rtime.tv_sec;
  409         usec = p->p_rtime.tv_usec;
  410         LIST_FOREACH(l, &p->p_lwps, l_sibling) {
  411                 if (l->l_stat == LSONPROC) {
  412                         struct schedstate_percpu *spc;
  413 
  414                         KDASSERT(l->l_cpu != NULL);
  415                         spc = &l->l_cpu->ci_schedstate;
  416 
  417                         /*
  418                          * Adjust for the current time slice.  This is
  419                          * actually fairly important since the error
  420                          * here is on the order of a time quantum,
  421                          * which is much greater than the sampling
  422                          * error.
  423                          */
  424                         microtime(&tv);
  425                         sec += tv.tv_sec - spc->spc_runtime.tv_sec;
  426                         usec += tv.tv_usec - spc->spc_runtime.tv_usec;
  427                 }
  428         }
  429 
  430         tot = st + ut + it;
  431         u = sec * 1000000ull + usec;
  432 
  433         if (tot == 0) {
  434                 /* No ticks, so can't use to share time out, split 50-50 */
  435                 st = ut = u / 2;
  436         } else {
  437                 st = (u * st) / tot;
  438                 ut = (u * ut) / tot;
  439         }
  440         sp->tv_sec = st / 1000000;
  441         sp->tv_usec = st % 1000000;
  442         up->tv_sec = ut / 1000000;
  443         up->tv_usec = ut % 1000000;
  444         if (ip != NULL) {
  445                 if (it != 0)
  446                         it = (u * it) / tot;
  447                 ip->tv_sec = it / 1000000;
  448                 ip->tv_usec = it % 1000000;
  449         }
  450 }
  451 
  452 /* ARGSUSED */
  453 int
  454 sys_getrusage(l, v, retval)
  455         struct lwp *l;
  456         void *v;
  457         register_t *retval;
  458 {
  459         struct sys_getrusage_args /* {
  460                 syscallarg(int) who;
  461                 syscallarg(struct rusage *) rusage;
  462         } */ *uap = v;
  463         struct rusage *rup;
  464         struct proc *p = l->l_proc;
  465 
  466         switch (SCARG(uap, who)) {
  467 
  468         case RUSAGE_SELF:
  469                 rup = &p->p_stats->p_ru;
  470                 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
  471                 break;
  472 
  473         case RUSAGE_CHILDREN:
  474                 rup = &p->p_stats->p_cru;
  475                 break;
  476 
  477         default:
  478                 return (EINVAL);
  479         }
  480         return (copyout(rup, SCARG(uap, rusage), sizeof(struct rusage)));
  481 }
  482 
  483 void
  484 ruadd(ru, ru2)
  485         struct rusage *ru, *ru2;
  486 {
  487         long *ip, *ip2;
  488         int i;
  489 
  490         timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
  491         timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
  492         if (ru->ru_maxrss < ru2->ru_maxrss)
  493                 ru->ru_maxrss = ru2->ru_maxrss;
  494         ip = &ru->ru_first; ip2 = &ru2->ru_first;
  495         for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
  496                 *ip++ += *ip2++;
  497 }
  498 
  499 /*
  500  * Make a copy of the plimit structure.
  501  * We share these structures copy-on-write after fork,
  502  * and copy when a limit is changed.
  503  */
  504 struct plimit *
  505 limcopy(lim)
  506         struct plimit *lim;
  507 {
  508         struct plimit *newlim;
  509         size_t l = 0;
  510 
  511         simple_lock(&lim->p_slock);
  512         if (lim->pl_corename != defcorename)
  513                 l = strlen(lim->pl_corename) + 1;
  514         simple_unlock(&lim->p_slock);
  515 
  516         newlim = pool_get(&plimit_pool, PR_WAITOK);
  517         simple_lock_init(&newlim->p_slock);
  518         newlim->p_lflags = 0;
  519         newlim->p_refcnt = 1;
  520         newlim->pl_corename = (l != 0)
  521                 ? malloc(l, M_TEMP, M_WAITOK)
  522                 : defcorename;
  523 
  524         simple_lock(&lim->p_slock);
  525         memcpy(newlim->pl_rlimit, lim->pl_rlimit,
  526             sizeof(struct rlimit) * RLIM_NLIMITS);
  527 
  528         if (l != 0)
  529                 strlcpy(newlim->pl_corename, lim->pl_corename, l);
  530         simple_unlock(&lim->p_slock);
  531 
  532         return (newlim);
  533 }
  534 
  535 void
  536 limfree(lim)
  537         struct plimit *lim;
  538 {
  539         int n;
  540 
  541         simple_lock(&lim->p_slock);
  542         n = --lim->p_refcnt;
  543         simple_unlock(&lim->p_slock);
  544         if (n > 0)
  545                 return;
  546 #ifdef DIAGNOSTIC
  547         if (n < 0)
  548                 panic("limfree");
  549 #endif
  550         if (lim->pl_corename != defcorename)
  551                 free(lim->pl_corename, M_TEMP);
  552         pool_put(&plimit_pool, lim);
  553 }
  554 
  555 struct pstats *
  556 pstatscopy(ps)
  557         struct pstats *ps;
  558 {
  559 
  560         struct pstats *newps;
  561 
  562         newps = pool_get(&pstats_pool, PR_WAITOK);
  563 
  564         memset(&newps->pstat_startzero, 0,
  565         (unsigned) ((caddr_t)&newps->pstat_endzero -
  566                     (caddr_t)&newps->pstat_startzero));
  567         memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
  568         ((caddr_t)&newps->pstat_endcopy -
  569          (caddr_t)&newps->pstat_startcopy));
  570 
  571         return (newps);
  572 
  573 }
  574 
  575 void
  576 pstatsfree(ps)
  577         struct pstats *ps;
  578 {
  579 
  580         pool_put(&pstats_pool, ps);
  581 }
  582 
  583 /*
  584  * sysctl interface in five parts
  585  */
  586 
  587 /*
  588  * a routine for sysctl proc subtree helpers that need to pick a valid
  589  * process by pid.
  590  */
  591 static int
  592 sysctl_proc_findproc(struct proc *p, struct proc **p2, pid_t pid)
  593 {
  594         struct proc *ptmp;
  595         int i, error = 0;
  596 
  597         if (pid == PROC_CURPROC)
  598                 ptmp = p;
  599         else if ((ptmp = pfind(pid)) == NULL)
  600                 error = ESRCH;
  601         else {
  602                 /*
  603                  * suid proc of ours or proc not ours
  604                  */
  605                 if (p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
  606                     p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
  607                         error = suser(p->p_ucred, &p->p_acflag);
  608 
  609                 /*
  610                  * sgid proc has sgid back to us temporarily
  611                  */
  612                 else if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
  613                         error = suser(p->p_ucred, &p->p_acflag);
  614 
  615                 /*
  616                  * our rgid must be in target's group list (ie,
  617                  * sub-processes started by a sgid process)
  618                  */
  619                 else {
  620                         for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
  621                                 if (p->p_ucred->cr_groups[i] ==
  622                                     ptmp->p_cred->p_rgid)
  623                                         break;
  624                         }
  625                         if (i == p->p_ucred->cr_ngroups)
  626                                 error = suser(p->p_ucred, &p->p_acflag);
  627                 }
  628         }
  629 
  630         *p2 = ptmp;
  631         return (error);
  632 }
  633 
  634 /*
  635  * sysctl helper routine for setting a process's specific corefile
  636  * name.  picks the process based on the given pid and checks the
  637  * correctness of the new value.
  638  */
  639 static int
  640 sysctl_proc_corename(SYSCTLFN_ARGS)
  641 {
  642         struct proc *ptmp, *p;
  643         struct plimit *lim;
  644         int error = 0, len;
  645         char cname[MAXPATHLEN], *tmp;
  646         struct sysctlnode node;
  647 
  648         /*
  649          * is this all correct?
  650          */
  651         if (namelen != 0)
  652                 return (EINVAL);
  653         if (name[-1] != PROC_PID_CORENAME)
  654                 return (EINVAL);
  655 
  656         /*
  657          * whom are we tweaking?
  658          */
  659         p = l->l_proc;
  660         error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-2]);
  661         if (error)
  662                 return (error);
  663 
  664         /*
  665          * let them modify a temporary copy of the core name
  666          */
  667         node = *rnode;
  668         strlcpy(cname, ptmp->p_limit->pl_corename, sizeof(cname));
  669         node.sysctl_data = cname;
  670         error = sysctl_lookup(SYSCTLFN_CALL(&node));
  671 
  672         /*
  673          * if that failed, or they have nothing new to say, or we've
  674          * heard it before...
  675          */
  676         if (error || newp == NULL ||
  677             strcmp(cname, ptmp->p_limit->pl_corename) == 0)
  678                 return (error);
  679 
  680         /*
  681          * no error yet and cname now has the new core name in it.
  682          * let's see if it looks acceptable.  it must be either "core"
  683          * or end in ".core" or "/core".
  684          */
  685         len = strlen(cname);
  686         if (len < 4)
  687                 return (EINVAL);
  688         if (strcmp(cname + len - 4, "core") != 0)
  689                 return (EINVAL);
  690         if (len > 4 && cname[len - 5] != '/' && cname[len - 5] != '.')
  691                 return (EINVAL);
  692 
  693         /*
  694          * hmm...looks good.  now...where do we put it?
  695          */
  696         tmp = malloc(len + 1, M_TEMP, M_WAITOK|M_CANFAIL);
  697         if (tmp == NULL)
  698                 return (ENOMEM);
  699         strlcpy(tmp, cname, len + 1);
  700 
  701         lim = ptmp->p_limit;
  702         if (lim->p_refcnt > 1 && (lim->p_lflags & PL_SHAREMOD) == 0) {
  703                 ptmp->p_limit = limcopy(lim);
  704                 limfree(lim);
  705                 lim = ptmp->p_limit;
  706         }
  707         if (lim->pl_corename != defcorename)
  708                 free(lim->pl_corename, M_TEMP);
  709         lim->pl_corename = tmp;
  710 
  711         return (error);
  712 }
  713 
  714 /*
  715  * sysctl helper routine for checking/setting a process's stop flags,
  716  * one for fork and one for exec.
  717  */
  718 static int
  719 sysctl_proc_stop(SYSCTLFN_ARGS)
  720 {
  721         struct proc *p, *ptmp;
  722         int i, f, error = 0;
  723         struct sysctlnode node;
  724 
  725         if (namelen != 0)
  726                 return (EINVAL);
  727 
  728         p = l->l_proc;
  729         error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-2]);
  730         if (error)
  731                 return (error);
  732 
  733         switch (rnode->sysctl_num) {
  734         case PROC_PID_STOPFORK:
  735                 f = P_STOPFORK;
  736                 break;
  737         case PROC_PID_STOPEXEC:
  738                 f = P_STOPEXEC;
  739                 break;
  740         case PROC_PID_STOPEXIT:
  741                 f = P_STOPEXIT;
  742                 break;
  743         default:
  744                 return (EINVAL);
  745         }
  746 
  747         i = (ptmp->p_flag & f) ? 1 : 0;
  748         node = *rnode;
  749         node.sysctl_data = &i;
  750         error = sysctl_lookup(SYSCTLFN_CALL(&node));
  751         if (error || newp == NULL)
  752                 return (error);
  753 
  754         if (i)
  755                 ptmp->p_flag |= f;
  756         else
  757                 ptmp->p_flag &= ~f;
  758 
  759         return (0);
  760 }
  761 
  762 /*
  763  * sysctl helper routine for a process's rlimits as exposed by sysctl.
  764  */
  765 static int
  766 sysctl_proc_plimit(SYSCTLFN_ARGS)
  767 {
  768         struct proc *ptmp, *p;
  769         u_int limitno;
  770         int which, error = 0;
  771         struct rlimit alim;
  772         struct sysctlnode node;
  773 
  774         if (namelen != 0)
  775                 return (EINVAL);
  776 
  777         which = name[-1];
  778         if (which != PROC_PID_LIMIT_TYPE_SOFT &&
  779             which != PROC_PID_LIMIT_TYPE_HARD)
  780                 return (EINVAL);
  781 
  782         limitno = name[-2] - 1;
  783         if (limitno >= RLIM_NLIMITS)
  784                 return (EINVAL);
  785 
  786         if (name[-3] != PROC_PID_LIMIT)
  787                 return (EINVAL);
  788 
  789         p = l->l_proc;
  790         error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-4]);
  791         if (error)
  792                 return (error);
  793 
  794         node = *rnode;
  795         memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
  796         if (which == PROC_PID_LIMIT_TYPE_HARD)
  797                 node.sysctl_data = &alim.rlim_max;
  798         else
  799                 node.sysctl_data = &alim.rlim_cur;
  800 
  801         error = sysctl_lookup(SYSCTLFN_CALL(&node));
  802         if (error || newp == NULL)
  803                 return (error);
  804 
  805         return (dosetrlimit(ptmp, p->p_cred, limitno, &alim));
  806 }
  807 
  808 /*
  809  * and finally, the actually glue that sticks it to the tree
  810  */
  811 SYSCTL_SETUP(sysctl_proc_setup, "sysctl proc subtree setup")
  812 {
  813 
  814         sysctl_createv(clog, 0, NULL, NULL,
  815                        CTLFLAG_PERMANENT,
  816                        CTLTYPE_NODE, "proc", NULL,
  817                        NULL, 0, NULL, 0,
  818                        CTL_PROC, CTL_EOL);
  819         sysctl_createv(clog, 0, NULL, NULL,
  820                        CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
  821                        CTLTYPE_NODE, "curproc",
  822                        SYSCTL_DESCR("Per-process settings"),
  823                        NULL, 0, NULL, 0,
  824                        CTL_PROC, PROC_CURPROC, CTL_EOL);
  825 
  826         sysctl_createv(clog, 0, NULL, NULL,
  827                        CTLFLAG_PERMANENT|CTLFLAG_READONLY2|CTLFLAG_ANYWRITE,
  828                        CTLTYPE_STRING, "corename",
  829                        SYSCTL_DESCR("Core file name"),
  830                        sysctl_proc_corename, 0, NULL, MAXPATHLEN,
  831                        CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
  832         sysctl_createv(clog, 0, NULL, NULL,
  833                        CTLFLAG_PERMANENT,
  834                        CTLTYPE_NODE, "rlimit",
  835                        SYSCTL_DESCR("Process limits"),
  836                        NULL, 0, NULL, 0,
  837                        CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
  838 
  839 #define create_proc_plimit(s, n) do {                                   \
  840         sysctl_createv(clog, 0, NULL, NULL,                             \
  841                        CTLFLAG_PERMANENT,                               \
  842                        CTLTYPE_NODE, s,                                 \
  843                        SYSCTL_DESCR("Process " s " limits"),            \
  844                        NULL, 0, NULL, 0,                                \
  845                        CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,       \
  846                        CTL_EOL);                                        \
  847         sysctl_createv(clog, 0, NULL, NULL,                             \
  848                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
  849                        CTLTYPE_QUAD, "soft",                            \
  850                        SYSCTL_DESCR("Process soft " s " limit"),        \
  851                        sysctl_proc_plimit, 0, NULL, 0,                  \
  852                        CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,       \
  853                        PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL);              \
  854         sysctl_createv(clog, 0, NULL, NULL,                             \
  855                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
  856                        CTLTYPE_QUAD, "hard",                            \
  857                        SYSCTL_DESCR("Process hard " s " limit"),        \
  858                        sysctl_proc_plimit, 0, NULL, 0,                  \
  859                        CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,       \
  860                        PROC_PID_LIMIT_TYPE_HARD, CTL_EOL);              \
  861         } while (0/*CONSTCOND*/)
  862 
  863         create_proc_plimit("cputime",           PROC_PID_LIMIT_CPU);
  864         create_proc_plimit("filesize",          PROC_PID_LIMIT_FSIZE);
  865         create_proc_plimit("datasize",          PROC_PID_LIMIT_DATA);
  866         create_proc_plimit("stacksize",         PROC_PID_LIMIT_STACK);
  867         create_proc_plimit("coredumpsize",      PROC_PID_LIMIT_CORE);
  868         create_proc_plimit("memoryuse",         PROC_PID_LIMIT_RSS);
  869         create_proc_plimit("memorylocked",      PROC_PID_LIMIT_MEMLOCK);
  870         create_proc_plimit("maxproc",           PROC_PID_LIMIT_NPROC);
  871         create_proc_plimit("descriptors",       PROC_PID_LIMIT_NOFILE);
  872         create_proc_plimit("sbsize",            PROC_PID_LIMIT_SBSIZE);
  873 
  874 #undef create_proc_plimit
  875 
  876         sysctl_createv(clog, 0, NULL, NULL,
  877                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
  878                        CTLTYPE_INT, "stopfork",
  879                        SYSCTL_DESCR("Stop process at fork(2)"),
  880                        sysctl_proc_stop, 0, NULL, 0,
  881                        CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
  882         sysctl_createv(clog, 0, NULL, NULL,
  883                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
  884                        CTLTYPE_INT, "stopexec",
  885                        SYSCTL_DESCR("Stop process at execve(2)"),
  886                        sysctl_proc_stop, 0, NULL, 0,
  887                        CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
  888         sysctl_createv(clog, 0, NULL, NULL,
  889                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
  890                        CTLTYPE_INT, "stopexit",
  891                        SYSCTL_DESCR("Stop process before completing exit"),
  892                        sysctl_proc_stop, 0, NULL, 0,
  893                        CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
  894 }
  895 
  896 static struct uidinfo *
  897 getuidinfo(uid_t uid)
  898 {
  899         struct uidinfo *uip;
  900         struct uihashhead *uipp;
  901 
  902         uipp = UIHASH(uid);
  903 
  904         LIST_FOREACH(uip, uipp, ui_hash)
  905                 if (uip->ui_uid == uid)
  906                         return uip;
  907         return NULL;
  908 }
  909 
  910 static void
  911 freeuidinfo(struct uidinfo *uip)
  912 {
  913         LIST_REMOVE(uip, ui_hash);
  914         FREE(uip, M_PROC);
  915 }
  916 
  917 static struct uidinfo *
  918 allocuidinfo(uid_t uid)
  919 {
  920         struct uidinfo *uip;
  921         struct uihashhead *uipp;
  922 
  923         uipp = UIHASH(uid);
  924         MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
  925         LIST_INSERT_HEAD(uipp, uip, ui_hash);
  926         uip->ui_uid = uid;
  927         uip->ui_proccnt = 0;
  928         uip->ui_sbsize = 0;
  929         return uip;
  930 }
  931 
  932 /*
  933  * Change the count associated with number of processes
  934  * a given user is using.
  935  */
  936 int
  937 chgproccnt(uid_t uid, int diff)
  938 {
  939         struct uidinfo *uip;
  940 
  941         if (diff == 0)
  942                 return 0;
  943 
  944         if ((uip = getuidinfo(uid)) != NULL) {
  945                 uip->ui_proccnt += diff;
  946                 KASSERT(uip->ui_proccnt >= 0);
  947                 if (uip->ui_proccnt > 0)
  948                         return uip->ui_proccnt;
  949                 else {
  950                         if (uip->ui_sbsize == 0)
  951                                 freeuidinfo(uip);
  952                         return 0;
  953                 }
  954         } else {
  955                 if (diff < 0)
  956                         panic("chgproccnt: lost user %lu", (unsigned long)uid);
  957                 uip = allocuidinfo(uid);
  958                 uip->ui_proccnt = diff;
  959                 return uip->ui_proccnt;
  960         }
  961 }
  962 
  963 int
  964 chgsbsize(uid_t uid, u_long *hiwat, u_long to, rlim_t max)
  965 {
  966         *hiwat = to;
  967         return 1;
  968 #ifdef notyet
  969         struct uidinfo *uip;
  970         rlim_t nsb;
  971         int rv = 0;
  972 
  973         if ((uip = getuidinfo(uid)) == NULL)
  974                 uip = allocuidinfo(uid);
  975         nsb = uip->ui_sbsize + to - *hiwat;
  976         if (to > *hiwat && nsb > max)
  977                 goto done;
  978         *hiwat = to;
  979         uip->ui_sbsize = nsb;
  980         rv = 1;
  981         KASSERT(uip->ui_sbsize >= 0);
  982 done:
  983         if (uip->ui_sbsize == 0 && uip->ui_proccnt == 0)
  984                 freeuidinfo(uip);
  985         return rv;
  986 #endif
  987 }

Cache object: 4827eb8e039c9a92d8dfd2918d1d0aad


[ 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.