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

Cache object: bc2218b8b9f9992d173fe144cbf5de50


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