Home | History | Annotate | Line # | Download | only in kern
kern_resource.c revision 1.161
      1  1.161     rmind /*	$NetBSD: kern_resource.c,v 1.161 2011/05/01 01:15:18 rmind Exp $	*/
      2   1.20       cgd 
      3   1.17       cgd /*-
      4   1.19       cgd  * Copyright (c) 1982, 1986, 1991, 1993
      5   1.19       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.17       cgd  * (c) UNIX System Laboratories, Inc.
      7   1.17       cgd  * All or some portions of this file are derived from material licensed
      8   1.17       cgd  * to the University of California by American Telephone and Telegraph
      9   1.17       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10   1.17       cgd  * the permission of UNIX System Laboratories, Inc.
     11   1.17       cgd  *
     12   1.17       cgd  * Redistribution and use in source and binary forms, with or without
     13   1.17       cgd  * modification, are permitted provided that the following conditions
     14   1.17       cgd  * are met:
     15   1.17       cgd  * 1. Redistributions of source code must retain the above copyright
     16   1.17       cgd  *    notice, this list of conditions and the following disclaimer.
     17   1.17       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.17       cgd  *    notice, this list of conditions and the following disclaimer in the
     19   1.17       cgd  *    documentation and/or other materials provided with the distribution.
     20   1.72       agc  * 3. Neither the name of the University nor the names of its contributors
     21   1.17       cgd  *    may be used to endorse or promote products derived from this software
     22   1.17       cgd  *    without specific prior written permission.
     23   1.17       cgd  *
     24   1.17       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.17       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.17       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.17       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.17       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.17       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.17       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.17       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.17       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.17       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.17       cgd  * SUCH DAMAGE.
     35   1.17       cgd  *
     36   1.45      fvdl  *	@(#)kern_resource.c	8.8 (Berkeley) 2/14/95
     37   1.17       cgd  */
     38   1.61     lukem 
     39   1.61     lukem #include <sys/cdefs.h>
     40  1.161     rmind __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.161 2011/05/01 01:15:18 rmind Exp $");
     41   1.44       mrg 
     42   1.17       cgd #include <sys/param.h>
     43   1.22       cgd #include <sys/systm.h>
     44   1.17       cgd #include <sys/kernel.h>
     45   1.19       cgd #include <sys/file.h>
     46   1.17       cgd #include <sys/resourcevar.h>
     47   1.17       cgd #include <sys/malloc.h>
     48  1.132      yamt #include <sys/kmem.h>
     49  1.100      yamt #include <sys/namei.h>
     50   1.49   thorpej #include <sys/pool.h>
     51   1.17       cgd #include <sys/proc.h>
     52   1.74    atatat #include <sys/sysctl.h>
     53  1.129      yamt #include <sys/timevar.h>
     54  1.101      elad #include <sys/kauth.h>
     55  1.125        ad #include <sys/atomic.h>
     56   1.22       cgd #include <sys/mount.h>
     57   1.22       cgd #include <sys/syscallargs.h>
     58  1.136        ad #include <sys/atomic.h>
     59   1.17       cgd 
     60   1.43       mrg #include <uvm/uvm_extern.h>
     61   1.43       mrg 
     62   1.17       cgd /*
     63   1.60       eeh  * Maximum process data and stack limits.
     64   1.60       eeh  * They are variables so they are patchable.
     65   1.60       eeh  */
     66   1.60       eeh rlim_t maxdmap = MAXDSIZ;
     67   1.60       eeh rlim_t maxsmap = MAXSSIZ;
     68   1.60       eeh 
     69  1.134     rmind static pool_cache_t	plimit_cache;
     70  1.134     rmind static pool_cache_t	pstats_cache;
     71  1.130        ad 
     72  1.154      elad static kauth_listener_t	resource_listener;
     73  1.153      elad 
     74  1.156     pooka static void sysctl_proc_setup(void);
     75  1.156     pooka 
     76  1.153      elad static int
     77  1.154      elad resource_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
     78  1.153      elad     void *arg0, void *arg1, void *arg2, void *arg3)
     79  1.153      elad {
     80  1.153      elad 	struct proc *p;
     81  1.153      elad 	int result;
     82  1.153      elad 
     83  1.153      elad 	result = KAUTH_RESULT_DEFER;
     84  1.153      elad 	p = arg0;
     85  1.153      elad 
     86  1.154      elad 	switch (action) {
     87  1.154      elad 	case KAUTH_PROCESS_NICE:
     88  1.154      elad 		if (kauth_cred_geteuid(cred) != kauth_cred_geteuid(p->p_cred) &&
     89  1.154      elad                     kauth_cred_getuid(cred) != kauth_cred_geteuid(p->p_cred)) {
     90  1.154      elad                         break;
     91  1.154      elad                 }
     92  1.153      elad 
     93  1.154      elad                 if ((u_long)arg1 >= p->p_nice)
     94  1.154      elad                         result = KAUTH_RESULT_ALLOW;
     95  1.153      elad 
     96  1.154      elad 		break;
     97  1.154      elad 
     98  1.154      elad 	case KAUTH_PROCESS_RLIMIT: {
     99  1.154      elad 		enum kauth_process_req req;
    100  1.153      elad 
    101  1.154      elad 		req = (enum kauth_process_req)(unsigned long)arg1;
    102  1.153      elad 
    103  1.154      elad 		switch (req) {
    104  1.154      elad 		case KAUTH_REQ_PROCESS_RLIMIT_GET:
    105  1.153      elad 			result = KAUTH_RESULT_ALLOW;
    106  1.154      elad 			break;
    107  1.154      elad 
    108  1.154      elad 		case KAUTH_REQ_PROCESS_RLIMIT_SET: {
    109  1.154      elad 			struct rlimit *new_rlimit;
    110  1.154      elad 			u_long which;
    111  1.154      elad 
    112  1.154      elad 			if ((p != curlwp->l_proc) &&
    113  1.154      elad 			    (proc_uidmatch(cred, p->p_cred) != 0))
    114  1.154      elad 				break;
    115  1.154      elad 
    116  1.154      elad 			new_rlimit = arg2;
    117  1.154      elad 			which = (u_long)arg3;
    118  1.154      elad 
    119  1.154      elad 			if (new_rlimit->rlim_max <= p->p_rlimit[which].rlim_max)
    120  1.154      elad 				result = KAUTH_RESULT_ALLOW;
    121  1.154      elad 
    122  1.154      elad 			break;
    123  1.154      elad 			}
    124  1.154      elad 
    125  1.154      elad 		default:
    126  1.154      elad 			break;
    127  1.154      elad 		}
    128  1.154      elad 
    129  1.154      elad 		break;
    130  1.154      elad 	}
    131  1.154      elad 
    132  1.154      elad 	default:
    133  1.154      elad 		break;
    134  1.153      elad 	}
    135  1.153      elad 
    136  1.153      elad 	return result;
    137  1.153      elad }
    138  1.153      elad 
    139  1.130        ad void
    140  1.130        ad resource_init(void)
    141  1.130        ad {
    142  1.130        ad 
    143  1.130        ad 	plimit_cache = pool_cache_init(sizeof(struct plimit), 0, 0, 0,
    144  1.130        ad 	    "plimitpl", NULL, IPL_NONE, NULL, NULL, NULL);
    145  1.130        ad 	pstats_cache = pool_cache_init(sizeof(struct pstats), 0, 0, 0,
    146  1.130        ad 	    "pstatspl", NULL, IPL_NONE, NULL, NULL, NULL);
    147  1.153      elad 
    148  1.154      elad 	resource_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    149  1.154      elad 	    resource_listener_cb, NULL);
    150  1.156     pooka 
    151  1.156     pooka 	sysctl_proc_setup();
    152  1.130        ad }
    153  1.130        ad 
    154   1.60       eeh /*
    155   1.17       cgd  * Resource controls and accounting.
    156   1.17       cgd  */
    157   1.17       cgd 
    158   1.25       cgd int
    159  1.134     rmind sys_getpriority(struct lwp *l, const struct sys_getpriority_args *uap,
    160  1.134     rmind     register_t *retval)
    161   1.30   thorpej {
    162  1.128       dsl 	/* {
    163   1.22       cgd 		syscallarg(int) which;
    164   1.81    kleink 		syscallarg(id_t) who;
    165  1.128       dsl 	} */
    166   1.68   thorpej 	struct proc *curp = l->l_proc, *p;
    167   1.54  augustss 	int low = NZERO + PRIO_MAX + 1;
    168  1.113        ad 	int who = SCARG(uap, who);
    169   1.17       cgd 
    170  1.138        ad 	mutex_enter(proc_lock);
    171   1.22       cgd 	switch (SCARG(uap, which)) {
    172   1.17       cgd 	case PRIO_PROCESS:
    173  1.157     rmind 		p = who ? proc_find(who) : curp;;
    174  1.113        ad 		if (p != NULL)
    175  1.113        ad 			low = p->p_nice;
    176   1.17       cgd 		break;
    177   1.17       cgd 
    178   1.17       cgd 	case PRIO_PGRP: {
    179   1.54  augustss 		struct pgrp *pg;
    180   1.17       cgd 
    181  1.113        ad 		if (who == 0)
    182   1.17       cgd 			pg = curp->p_pgrp;
    183  1.157     rmind 		else if ((pg = pgrp_find(who)) == NULL)
    184   1.17       cgd 			break;
    185   1.64      matt 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    186   1.17       cgd 			if (p->p_nice < low)
    187   1.17       cgd 				low = p->p_nice;
    188   1.17       cgd 		}
    189   1.17       cgd 		break;
    190   1.17       cgd 	}
    191   1.17       cgd 
    192   1.17       cgd 	case PRIO_USER:
    193  1.113        ad 		if (who == 0)
    194  1.113        ad 			who = (int)kauth_cred_geteuid(l->l_cred);
    195   1.86      yamt 		PROCLIST_FOREACH(p, &allproc) {
    196  1.139        ad 			mutex_enter(p->p_lock);
    197  1.102        ad 			if (kauth_cred_geteuid(p->p_cred) ==
    198  1.113        ad 			    (uid_t)who && p->p_nice < low)
    199   1.17       cgd 				low = p->p_nice;
    200  1.139        ad 			mutex_exit(p->p_lock);
    201   1.64      matt 		}
    202   1.17       cgd 		break;
    203   1.17       cgd 
    204   1.17       cgd 	default:
    205  1.138        ad 		mutex_exit(proc_lock);
    206   1.17       cgd 		return (EINVAL);
    207   1.17       cgd 	}
    208  1.138        ad 	mutex_exit(proc_lock);
    209  1.113        ad 
    210   1.37        ws 	if (low == NZERO + PRIO_MAX + 1)
    211   1.17       cgd 		return (ESRCH);
    212   1.37        ws 	*retval = low - NZERO;
    213   1.17       cgd 	return (0);
    214   1.17       cgd }
    215   1.17       cgd 
    216   1.17       cgd /* ARGSUSED */
    217   1.25       cgd int
    218  1.134     rmind sys_setpriority(struct lwp *l, const struct sys_setpriority_args *uap,
    219  1.134     rmind     register_t *retval)
    220   1.30   thorpej {
    221  1.128       dsl 	/* {
    222   1.22       cgd 		syscallarg(int) which;
    223   1.81    kleink 		syscallarg(id_t) who;
    224   1.22       cgd 		syscallarg(int) prio;
    225  1.128       dsl 	} */
    226   1.68   thorpej 	struct proc *curp = l->l_proc, *p;
    227   1.17       cgd 	int found = 0, error = 0;
    228  1.113        ad 	int who = SCARG(uap, who);
    229   1.17       cgd 
    230  1.138        ad 	mutex_enter(proc_lock);
    231   1.22       cgd 	switch (SCARG(uap, which)) {
    232   1.17       cgd 	case PRIO_PROCESS:
    233  1.157     rmind 		p = who ? proc_find(who) : curp;
    234  1.157     rmind 		if (p != NULL) {
    235  1.139        ad 			mutex_enter(p->p_lock);
    236  1.113        ad 			error = donice(l, p, SCARG(uap, prio));
    237  1.139        ad 			mutex_exit(p->p_lock);
    238  1.145     njoly 			found++;
    239  1.113        ad 		}
    240   1.17       cgd 		break;
    241   1.17       cgd 
    242   1.17       cgd 	case PRIO_PGRP: {
    243   1.54  augustss 		struct pgrp *pg;
    244   1.87     perry 
    245  1.113        ad 		if (who == 0)
    246   1.17       cgd 			pg = curp->p_pgrp;
    247  1.157     rmind 		else if ((pg = pgrp_find(who)) == NULL)
    248   1.17       cgd 			break;
    249   1.64      matt 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    250  1.139        ad 			mutex_enter(p->p_lock);
    251  1.102        ad 			error = donice(l, p, SCARG(uap, prio));
    252  1.139        ad 			mutex_exit(p->p_lock);
    253   1.17       cgd 			found++;
    254   1.17       cgd 		}
    255   1.17       cgd 		break;
    256   1.17       cgd 	}
    257   1.17       cgd 
    258   1.17       cgd 	case PRIO_USER:
    259  1.113        ad 		if (who == 0)
    260  1.113        ad 			who = (int)kauth_cred_geteuid(l->l_cred);
    261   1.86      yamt 		PROCLIST_FOREACH(p, &allproc) {
    262  1.139        ad 			mutex_enter(p->p_lock);
    263  1.102        ad 			if (kauth_cred_geteuid(p->p_cred) ==
    264  1.102        ad 			    (uid_t)SCARG(uap, who)) {
    265  1.102        ad 				error = donice(l, p, SCARG(uap, prio));
    266   1.17       cgd 				found++;
    267   1.17       cgd 			}
    268  1.139        ad 			mutex_exit(p->p_lock);
    269   1.64      matt 		}
    270   1.17       cgd 		break;
    271   1.17       cgd 
    272   1.17       cgd 	default:
    273  1.144     njoly 		mutex_exit(proc_lock);
    274  1.144     njoly 		return EINVAL;
    275   1.17       cgd 	}
    276  1.138        ad 	mutex_exit(proc_lock);
    277   1.17       cgd 	if (found == 0)
    278   1.17       cgd 		return (ESRCH);
    279   1.17       cgd 	return (error);
    280   1.17       cgd }
    281   1.17       cgd 
    282  1.113        ad /*
    283  1.113        ad  * Renice a process.
    284  1.113        ad  *
    285  1.113        ad  * Call with the target process' credentials locked.
    286  1.113        ad  */
    287   1.25       cgd int
    288  1.102        ad donice(struct lwp *l, struct proc *chgp, int n)
    289   1.17       cgd {
    290  1.102        ad 	kauth_cred_t cred = l->l_cred;
    291  1.113        ad 
    292  1.139        ad 	KASSERT(mutex_owned(chgp->p_lock));
    293   1.17       cgd 
    294  1.152      elad 	if (kauth_cred_geteuid(cred) && kauth_cred_getuid(cred) &&
    295  1.152      elad 	    kauth_cred_geteuid(cred) != kauth_cred_geteuid(chgp->p_cred) &&
    296  1.152      elad 	    kauth_cred_getuid(cred) != kauth_cred_geteuid(chgp->p_cred))
    297  1.152      elad 		return (EPERM);
    298  1.152      elad 
    299   1.17       cgd 	if (n > PRIO_MAX)
    300   1.17       cgd 		n = PRIO_MAX;
    301   1.17       cgd 	if (n < PRIO_MIN)
    302   1.17       cgd 		n = PRIO_MIN;
    303   1.37        ws 	n += NZERO;
    304  1.112      elad 	if (kauth_authorize_process(cred, KAUTH_PROCESS_NICE, chgp,
    305  1.112      elad 	    KAUTH_ARG(n), NULL, NULL))
    306   1.17       cgd 		return (EACCES);
    307  1.117      yamt 	sched_nice(chgp, n);
    308   1.17       cgd 	return (0);
    309   1.17       cgd }
    310   1.17       cgd 
    311   1.17       cgd /* ARGSUSED */
    312   1.25       cgd int
    313  1.134     rmind sys_setrlimit(struct lwp *l, const struct sys_setrlimit_args *uap,
    314  1.134     rmind     register_t *retval)
    315   1.30   thorpej {
    316  1.128       dsl 	/* {
    317   1.42   mycroft 		syscallarg(int) which;
    318   1.39       cgd 		syscallarg(const struct rlimit *) rlp;
    319  1.128       dsl 	} */
    320   1.42   mycroft 	int which = SCARG(uap, which);
    321   1.19       cgd 	struct rlimit alim;
    322   1.17       cgd 	int error;
    323   1.17       cgd 
    324   1.46     perry 	error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
    325   1.33  christos 	if (error)
    326   1.17       cgd 		return (error);
    327  1.102        ad 	return (dosetrlimit(l, l->l_proc, which, &alim));
    328   1.17       cgd }
    329   1.17       cgd 
    330   1.17       cgd int
    331  1.102        ad dosetrlimit(struct lwp *l, struct proc *p, int which, struct rlimit *limp)
    332   1.17       cgd {
    333   1.54  augustss 	struct rlimit *alimp;
    334   1.17       cgd 	int error;
    335   1.17       cgd 
    336   1.67    itojun 	if ((u_int)which >= RLIM_NLIMITS)
    337   1.17       cgd 		return (EINVAL);
    338   1.38  matthias 
    339   1.62  jdolecek 	if (limp->rlim_cur > limp->rlim_max) {
    340   1.62  jdolecek 		/*
    341   1.62  jdolecek 		 * This is programming error. According to SUSv2, we should
    342   1.62  jdolecek 		 * return error in this case.
    343   1.62  jdolecek 		 */
    344   1.62  jdolecek 		return (EINVAL);
    345   1.62  jdolecek 	}
    346  1.122       dsl 
    347  1.122       dsl 	alimp = &p->p_rlimit[which];
    348  1.122       dsl 	/* if we don't change the value, no need to limcopy() */
    349  1.122       dsl 	if (limp->rlim_cur == alimp->rlim_cur &&
    350  1.122       dsl 	    limp->rlim_max == alimp->rlim_max)
    351  1.122       dsl 		return 0;
    352  1.122       dsl 
    353  1.112      elad 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
    354  1.131      elad 	    p, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_SET), limp, KAUTH_ARG(which));
    355  1.111      elad 	if (error)
    356  1.122       dsl 		return (error);
    357   1.62  jdolecek 
    358  1.161     rmind 	lim_privatise(p);
    359  1.122       dsl 	/* p->p_limit is now unchangeable */
    360  1.122       dsl 	alimp = &p->p_rlimit[which];
    361   1.17       cgd 
    362   1.17       cgd 	switch (which) {
    363   1.17       cgd 
    364   1.17       cgd 	case RLIMIT_DATA:
    365   1.19       cgd 		if (limp->rlim_cur > maxdmap)
    366   1.19       cgd 			limp->rlim_cur = maxdmap;
    367   1.19       cgd 		if (limp->rlim_max > maxdmap)
    368   1.19       cgd 			limp->rlim_max = maxdmap;
    369   1.17       cgd 		break;
    370   1.17       cgd 
    371   1.17       cgd 	case RLIMIT_STACK:
    372   1.19       cgd 		if (limp->rlim_cur > maxsmap)
    373   1.19       cgd 			limp->rlim_cur = maxsmap;
    374   1.19       cgd 		if (limp->rlim_max > maxsmap)
    375   1.19       cgd 			limp->rlim_max = maxsmap;
    376   1.62  jdolecek 
    377   1.62  jdolecek 		/*
    378   1.62  jdolecek 		 * Return EINVAL if the new stack size limit is lower than
    379   1.62  jdolecek 		 * current usage. Otherwise, the process would get SIGSEGV the
    380   1.62  jdolecek 		 * moment it would try to access anything on it's current stack.
    381   1.62  jdolecek 		 * This conforms to SUSv2.
    382   1.62  jdolecek 		 */
    383   1.62  jdolecek 		if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE
    384  1.113        ad 		    || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE) {
    385   1.62  jdolecek 			return (EINVAL);
    386  1.113        ad 		}
    387   1.40     enami 
    388   1.17       cgd 		/*
    389   1.40     enami 		 * Stack is allocated to the max at exec time with
    390   1.40     enami 		 * only "rlim_cur" bytes accessible (In other words,
    391   1.40     enami 		 * allocates stack dividing two contiguous regions at
    392   1.40     enami 		 * "rlim_cur" bytes boundary).
    393   1.40     enami 		 *
    394   1.40     enami 		 * Since allocation is done in terms of page, roundup
    395   1.40     enami 		 * "rlim_cur" (otherwise, contiguous regions
    396   1.40     enami 		 * overlap).  If stack limit is going up make more
    397   1.40     enami 		 * accessible, if going down make inaccessible.
    398   1.17       cgd 		 */
    399   1.40     enami 		limp->rlim_cur = round_page(limp->rlim_cur);
    400   1.17       cgd 		if (limp->rlim_cur != alimp->rlim_cur) {
    401   1.48       eeh 			vaddr_t addr;
    402   1.48       eeh 			vsize_t size;
    403   1.17       cgd 			vm_prot_t prot;
    404   1.17       cgd 
    405   1.17       cgd 			if (limp->rlim_cur > alimp->rlim_cur) {
    406   1.73       chs 				prot = VM_PROT_READ | VM_PROT_WRITE;
    407   1.17       cgd 				size = limp->rlim_cur - alimp->rlim_cur;
    408   1.91      fvdl 				addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
    409   1.91      fvdl 				    limp->rlim_cur;
    410   1.17       cgd 			} else {
    411   1.17       cgd 				prot = VM_PROT_NONE;
    412   1.17       cgd 				size = alimp->rlim_cur - limp->rlim_cur;
    413   1.91      fvdl 				addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
    414   1.91      fvdl 				     alimp->rlim_cur;
    415   1.17       cgd 			}
    416   1.43       mrg 			(void) uvm_map_protect(&p->p_vmspace->vm_map,
    417  1.114   thorpej 			    addr, addr+size, prot, false);
    418   1.17       cgd 		}
    419   1.17       cgd 		break;
    420   1.19       cgd 
    421   1.19       cgd 	case RLIMIT_NOFILE:
    422   1.19       cgd 		if (limp->rlim_cur > maxfiles)
    423   1.19       cgd 			limp->rlim_cur = maxfiles;
    424   1.19       cgd 		if (limp->rlim_max > maxfiles)
    425   1.19       cgd 			limp->rlim_max = maxfiles;
    426   1.19       cgd 		break;
    427   1.19       cgd 
    428   1.19       cgd 	case RLIMIT_NPROC:
    429   1.19       cgd 		if (limp->rlim_cur > maxproc)
    430   1.19       cgd 			limp->rlim_cur = maxproc;
    431   1.19       cgd 		if (limp->rlim_max > maxproc)
    432   1.19       cgd 			limp->rlim_max = maxproc;
    433   1.19       cgd 		break;
    434   1.17       cgd 	}
    435  1.122       dsl 
    436  1.122       dsl 	mutex_enter(&p->p_limit->pl_lock);
    437   1.17       cgd 	*alimp = *limp;
    438  1.122       dsl 	mutex_exit(&p->p_limit->pl_lock);
    439   1.17       cgd 	return (0);
    440   1.17       cgd }
    441   1.17       cgd 
    442   1.17       cgd /* ARGSUSED */
    443   1.25       cgd int
    444  1.134     rmind sys_getrlimit(struct lwp *l, const struct sys_getrlimit_args *uap,
    445  1.134     rmind     register_t *retval)
    446   1.30   thorpej {
    447  1.128       dsl 	/* {
    448   1.42   mycroft 		syscallarg(int) which;
    449   1.22       cgd 		syscallarg(struct rlimit *) rlp;
    450  1.128       dsl 	} */
    451   1.68   thorpej 	struct proc *p = l->l_proc;
    452   1.42   mycroft 	int which = SCARG(uap, which);
    453  1.119        ad 	struct rlimit rl;
    454   1.17       cgd 
    455   1.67    itojun 	if ((u_int)which >= RLIM_NLIMITS)
    456   1.17       cgd 		return (EINVAL);
    457  1.119        ad 
    458  1.139        ad 	mutex_enter(p->p_lock);
    459  1.119        ad 	memcpy(&rl, &p->p_rlimit[which], sizeof(rl));
    460  1.139        ad 	mutex_exit(p->p_lock);
    461  1.119        ad 
    462  1.119        ad 	return copyout(&rl, SCARG(uap, rlp), sizeof(rl));
    463   1.17       cgd }
    464   1.17       cgd 
    465   1.17       cgd /*
    466   1.17       cgd  * Transform the running time and tick information in proc p into user,
    467   1.17       cgd  * system, and interrupt time usage.
    468  1.113        ad  *
    469  1.139        ad  * Should be called with p->p_lock held unless called from exit1().
    470   1.17       cgd  */
    471   1.25       cgd void
    472   1.98   thorpej calcru(struct proc *p, struct timeval *up, struct timeval *sp,
    473  1.113        ad     struct timeval *ip, struct timeval *rp)
    474   1.17       cgd {
    475  1.129      yamt 	uint64_t u, st, ut, it, tot;
    476   1.68   thorpej 	struct lwp *l;
    477  1.129      yamt 	struct bintime tm;
    478  1.129      yamt 	struct timeval tv;
    479   1.17       cgd 
    480  1.113        ad 	mutex_spin_enter(&p->p_stmutex);
    481   1.17       cgd 	st = p->p_sticks;
    482   1.17       cgd 	ut = p->p_uticks;
    483   1.17       cgd 	it = p->p_iticks;
    484  1.113        ad 	mutex_spin_exit(&p->p_stmutex);
    485   1.17       cgd 
    486  1.129      yamt 	tm = p->p_rtime;
    487  1.113        ad 
    488   1.70       dsl 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    489  1.113        ad 		lwp_lock(l);
    490  1.129      yamt 		bintime_add(&tm, &l->l_rtime);
    491  1.142        ad 		if ((l->l_pflag & LP_RUNNING) != 0) {
    492  1.129      yamt 			struct bintime diff;
    493   1.68   thorpej 			/*
    494   1.68   thorpej 			 * Adjust for the current time slice.  This is
    495   1.68   thorpej 			 * actually fairly important since the error
    496   1.68   thorpej 			 * here is on the order of a time quantum,
    497   1.68   thorpej 			 * which is much greater than the sampling
    498   1.87     perry 			 * error.
    499   1.68   thorpej 			 */
    500  1.129      yamt 			binuptime(&diff);
    501  1.129      yamt 			bintime_sub(&diff, &l->l_stime);
    502  1.129      yamt 			bintime_add(&tm, &diff);
    503   1.68   thorpej 		}
    504  1.113        ad 		lwp_unlock(l);
    505   1.17       cgd 	}
    506   1.69       dsl 
    507   1.69       dsl 	tot = st + ut + it;
    508  1.129      yamt 	bintime2timeval(&tm, &tv);
    509  1.129      yamt 	u = (uint64_t)tv.tv_sec * 1000000ul + tv.tv_usec;
    510   1.70       dsl 
    511   1.69       dsl 	if (tot == 0) {
    512   1.69       dsl 		/* No ticks, so can't use to share time out, split 50-50 */
    513   1.70       dsl 		st = ut = u / 2;
    514   1.70       dsl 	} else {
    515   1.70       dsl 		st = (u * st) / tot;
    516   1.70       dsl 		ut = (u * ut) / tot;
    517   1.69       dsl 	}
    518  1.113        ad 	if (sp != NULL) {
    519  1.113        ad 		sp->tv_sec = st / 1000000;
    520  1.113        ad 		sp->tv_usec = st % 1000000;
    521  1.113        ad 	}
    522  1.113        ad 	if (up != NULL) {
    523  1.113        ad 		up->tv_sec = ut / 1000000;
    524  1.113        ad 		up->tv_usec = ut % 1000000;
    525  1.113        ad 	}
    526   1.17       cgd 	if (ip != NULL) {
    527   1.70       dsl 		if (it != 0)
    528   1.70       dsl 			it = (u * it) / tot;
    529   1.17       cgd 		ip->tv_sec = it / 1000000;
    530   1.17       cgd 		ip->tv_usec = it % 1000000;
    531   1.17       cgd 	}
    532  1.113        ad 	if (rp != NULL) {
    533  1.129      yamt 		*rp = tv;
    534  1.113        ad 	}
    535   1.17       cgd }
    536   1.17       cgd 
    537   1.17       cgd /* ARGSUSED */
    538   1.25       cgd int
    539  1.148  christos sys___getrusage50(struct lwp *l, const struct sys___getrusage50_args *uap,
    540  1.134     rmind     register_t *retval)
    541   1.30   thorpej {
    542  1.128       dsl 	/* {
    543   1.22       cgd 		syscallarg(int) who;
    544   1.22       cgd 		syscallarg(struct rusage *) rusage;
    545  1.128       dsl 	} */
    546  1.119        ad 	struct rusage ru;
    547   1.68   thorpej 	struct proc *p = l->l_proc;
    548   1.17       cgd 
    549   1.22       cgd 	switch (SCARG(uap, who)) {
    550   1.19       cgd 	case RUSAGE_SELF:
    551  1.139        ad 		mutex_enter(p->p_lock);
    552  1.119        ad 		memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
    553  1.119        ad 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
    554  1.137        ad 		rulwps(p, &ru);
    555  1.139        ad 		mutex_exit(p->p_lock);
    556   1.17       cgd 		break;
    557   1.17       cgd 
    558   1.17       cgd 	case RUSAGE_CHILDREN:
    559  1.139        ad 		mutex_enter(p->p_lock);
    560  1.119        ad 		memcpy(&ru, &p->p_stats->p_cru, sizeof(ru));
    561  1.139        ad 		mutex_exit(p->p_lock);
    562   1.17       cgd 		break;
    563   1.17       cgd 
    564   1.17       cgd 	default:
    565  1.119        ad 		return EINVAL;
    566   1.17       cgd 	}
    567  1.119        ad 
    568  1.119        ad 	return copyout(&ru, SCARG(uap, rusage), sizeof(ru));
    569   1.17       cgd }
    570   1.17       cgd 
    571   1.25       cgd void
    572   1.98   thorpej ruadd(struct rusage *ru, struct rusage *ru2)
    573   1.17       cgd {
    574   1.54  augustss 	long *ip, *ip2;
    575   1.54  augustss 	int i;
    576   1.17       cgd 
    577   1.27   mycroft 	timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
    578   1.27   mycroft 	timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
    579   1.17       cgd 	if (ru->ru_maxrss < ru2->ru_maxrss)
    580   1.17       cgd 		ru->ru_maxrss = ru2->ru_maxrss;
    581   1.17       cgd 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
    582   1.17       cgd 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
    583   1.17       cgd 		*ip++ += *ip2++;
    584   1.17       cgd }
    585   1.17       cgd 
    586  1.137        ad void
    587  1.137        ad rulwps(proc_t *p, struct rusage *ru)
    588  1.137        ad {
    589  1.137        ad 	lwp_t *l;
    590  1.137        ad 
    591  1.139        ad 	KASSERT(mutex_owned(p->p_lock));
    592  1.137        ad 
    593  1.137        ad 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    594  1.137        ad 		ruadd(ru, &l->l_ru);
    595  1.137        ad 		ru->ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
    596  1.137        ad 		ru->ru_nivcsw += l->l_nivcsw;
    597  1.137        ad 	}
    598  1.137        ad }
    599  1.137        ad 
    600   1.17       cgd /*
    601  1.161     rmind  * lim_copy: make a copy of the plimit structure.
    602  1.113        ad  *
    603  1.161     rmind  * We use copy-on-write after fork, and copy when a limit is changed.
    604   1.17       cgd  */
    605   1.17       cgd struct plimit *
    606  1.122       dsl lim_copy(struct plimit *lim)
    607   1.17       cgd {
    608  1.122       dsl 	struct plimit *newlim;
    609  1.113        ad 	char *corename;
    610  1.122       dsl 	size_t alen, len;
    611   1.17       cgd 
    612  1.130        ad 	newlim = pool_cache_get(plimit_cache, PR_WAITOK);
    613  1.121       dsl 	mutex_init(&newlim->pl_lock, MUTEX_DEFAULT, IPL_NONE);
    614  1.161     rmind 	newlim->pl_writeable = false;
    615  1.121       dsl 	newlim->pl_refcnt = 1;
    616  1.122       dsl 	newlim->pl_sv_limit = NULL;
    617  1.122       dsl 
    618  1.122       dsl 	mutex_enter(&lim->pl_lock);
    619  1.122       dsl 	memcpy(newlim->pl_rlimit, lim->pl_rlimit,
    620  1.122       dsl 	    sizeof(struct rlimit) * RLIM_NLIMITS);
    621   1.83        pk 
    622  1.161     rmind 	/*
    623  1.161     rmind 	 * Note: the common case is a use of default core name.
    624  1.161     rmind 	 */
    625  1.122       dsl 	alen = 0;
    626  1.122       dsl 	corename = NULL;
    627  1.113        ad 	for (;;) {
    628  1.122       dsl 		if (lim->pl_corename == defcorename) {
    629  1.122       dsl 			newlim->pl_corename = defcorename;
    630  1.161     rmind 			newlim->pl_cnlen = 0;
    631  1.122       dsl 			break;
    632  1.122       dsl 		}
    633  1.161     rmind 		len = lim->pl_cnlen;
    634  1.161     rmind 		if (len == alen) {
    635  1.122       dsl 			newlim->pl_corename = corename;
    636  1.161     rmind 			newlim->pl_cnlen = len;
    637  1.122       dsl 			memcpy(corename, lim->pl_corename, len);
    638  1.122       dsl 			corename = NULL;
    639  1.122       dsl 			break;
    640  1.122       dsl 		}
    641  1.122       dsl 		mutex_exit(&lim->pl_lock);
    642  1.161     rmind 		if (corename) {
    643  1.161     rmind 			kmem_free(corename, alen);
    644  1.161     rmind 		}
    645  1.122       dsl 		alen = len;
    646  1.161     rmind 		corename = kmem_alloc(alen, KM_SLEEP);
    647  1.121       dsl 		mutex_enter(&lim->pl_lock);
    648  1.122       dsl 	}
    649  1.122       dsl 	mutex_exit(&lim->pl_lock);
    650  1.161     rmind 
    651  1.161     rmind 	if (corename) {
    652  1.161     rmind 		kmem_free(corename, alen);
    653  1.161     rmind 	}
    654  1.122       dsl 	return newlim;
    655  1.122       dsl }
    656  1.122       dsl 
    657  1.122       dsl void
    658  1.122       dsl lim_addref(struct plimit *lim)
    659  1.122       dsl {
    660  1.125        ad 	atomic_inc_uint(&lim->pl_refcnt);
    661  1.122       dsl }
    662  1.113        ad 
    663  1.122       dsl /*
    664  1.161     rmind  * lim_privatise: give a process its own private plimit structure.
    665  1.122       dsl  */
    666  1.122       dsl void
    667  1.161     rmind lim_privatise(proc_t *p)
    668  1.122       dsl {
    669  1.161     rmind 	struct plimit *lim = p->p_limit, *newlim;
    670  1.122       dsl 
    671  1.161     rmind 	if (lim->pl_writeable) {
    672  1.122       dsl 		return;
    673  1.122       dsl 	}
    674  1.122       dsl 
    675  1.122       dsl 	newlim = lim_copy(lim);
    676  1.113        ad 
    677  1.139        ad 	mutex_enter(p->p_lock);
    678  1.161     rmind 	if (p->p_limit->pl_writeable) {
    679  1.161     rmind 		/* Other thread won the race. */
    680  1.139        ad 		mutex_exit(p->p_lock);
    681  1.159     rmind 		lim_free(newlim);
    682  1.122       dsl 		return;
    683  1.113        ad 	}
    684   1.83        pk 
    685  1.122       dsl 	/*
    686  1.161     rmind 	 * Since p->p_limit can be accessed without locked held,
    687  1.161     rmind 	 * old limit structure must not be deleted yet.
    688  1.122       dsl 	 */
    689  1.122       dsl 	newlim->pl_sv_limit = p->p_limit;
    690  1.161     rmind 	newlim->pl_writeable = true;
    691  1.122       dsl 	p->p_limit = newlim;
    692  1.139        ad 	mutex_exit(p->p_lock);
    693   1.32   mycroft }
    694   1.32   mycroft 
    695   1.32   mycroft void
    696  1.160     rmind lim_setcorename(proc_t *p, char *name, size_t len)
    697  1.160     rmind {
    698  1.160     rmind 	struct plimit *lim;
    699  1.160     rmind 	char *oname;
    700  1.161     rmind 	size_t olen;
    701  1.160     rmind 
    702  1.161     rmind 	lim_privatise(p);
    703  1.160     rmind 	lim = p->p_limit;
    704  1.160     rmind 
    705  1.160     rmind 	mutex_enter(&lim->pl_lock);
    706  1.160     rmind 	oname = lim->pl_corename;
    707  1.161     rmind 	olen = lim->pl_cnlen;
    708  1.160     rmind 	lim->pl_corename = name;
    709  1.161     rmind 	lim->pl_cnlen = len;
    710  1.160     rmind 	mutex_exit(&lim->pl_lock);
    711  1.160     rmind 
    712  1.160     rmind 	if (oname != defcorename) {
    713  1.161     rmind 		kmem_free(oname, olen);
    714  1.160     rmind 	}
    715  1.160     rmind }
    716  1.160     rmind 
    717  1.160     rmind void
    718  1.159     rmind lim_free(struct plimit *lim)
    719   1.32   mycroft {
    720  1.122       dsl 	struct plimit *sv_lim;
    721   1.85    kleink 
    722  1.122       dsl 	do {
    723  1.159     rmind 		if (atomic_dec_uint_nv(&lim->pl_refcnt) > 0) {
    724  1.122       dsl 			return;
    725  1.159     rmind 		}
    726  1.159     rmind 		if (lim->pl_corename != defcorename) {
    727  1.161     rmind 			kmem_free(lim->pl_corename, lim->pl_cnlen);
    728  1.159     rmind 		}
    729  1.122       dsl 		sv_lim = lim->pl_sv_limit;
    730  1.122       dsl 		mutex_destroy(&lim->pl_lock);
    731  1.130        ad 		pool_cache_put(plimit_cache, lim);
    732  1.122       dsl 	} while ((lim = sv_lim) != NULL);
    733   1.68   thorpej }
    734   1.68   thorpej 
    735   1.68   thorpej struct pstats *
    736   1.98   thorpej pstatscopy(struct pstats *ps)
    737   1.68   thorpej {
    738   1.87     perry 
    739   1.68   thorpej 	struct pstats *newps;
    740   1.68   thorpej 
    741  1.130        ad 	newps = pool_cache_get(pstats_cache, PR_WAITOK);
    742   1.68   thorpej 
    743   1.68   thorpej 	memset(&newps->pstat_startzero, 0,
    744  1.115  christos 	(unsigned) ((char *)&newps->pstat_endzero -
    745  1.115  christos 		    (char *)&newps->pstat_startzero));
    746   1.68   thorpej 	memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
    747  1.115  christos 	((char *)&newps->pstat_endcopy -
    748  1.115  christos 	 (char *)&newps->pstat_startcopy));
    749   1.68   thorpej 
    750   1.68   thorpej 	return (newps);
    751   1.68   thorpej 
    752   1.68   thorpej }
    753   1.68   thorpej 
    754   1.68   thorpej void
    755   1.98   thorpej pstatsfree(struct pstats *ps)
    756   1.68   thorpej {
    757   1.68   thorpej 
    758  1.130        ad 	pool_cache_put(pstats_cache, ps);
    759   1.74    atatat }
    760   1.74    atatat 
    761   1.74    atatat /*
    762   1.74    atatat  * sysctl interface in five parts
    763   1.74    atatat  */
    764   1.74    atatat 
    765   1.74    atatat /*
    766  1.157     rmind  * sysctl_proc_findproc: a routine for sysctl proc subtree helpers that
    767  1.157     rmind  * need to pick a valid process by PID.
    768  1.157     rmind  *
    769  1.157     rmind  * => Hold a reference on the process, on success.
    770   1.74    atatat  */
    771   1.74    atatat static int
    772  1.157     rmind sysctl_proc_findproc(lwp_t *l, pid_t pid, proc_t **p2)
    773   1.74    atatat {
    774  1.157     rmind 	proc_t *p;
    775  1.157     rmind 	int error;
    776   1.74    atatat 
    777  1.157     rmind 	if (pid == PROC_CURPROC) {
    778  1.157     rmind 		p = l->l_proc;
    779  1.157     rmind 	} else {
    780  1.157     rmind 		mutex_enter(proc_lock);
    781  1.157     rmind 		p = proc_find(pid);
    782  1.157     rmind 		if (p == NULL) {
    783  1.157     rmind 			mutex_exit(proc_lock);
    784  1.157     rmind 			return ESRCH;
    785  1.157     rmind 		}
    786  1.157     rmind 	}
    787  1.157     rmind 	error = rw_tryenter(&p->p_reflock, RW_READER) ? 0 : EBUSY;
    788  1.157     rmind 	if (pid != PROC_CURPROC) {
    789  1.157     rmind 		mutex_exit(proc_lock);
    790  1.157     rmind 	}
    791  1.157     rmind 	*p2 = p;
    792  1.157     rmind 	return error;
    793   1.74    atatat }
    794   1.74    atatat 
    795   1.74    atatat /*
    796  1.158     rmind  * sysctl_proc_corename: helper routine to get or set the core file name
    797  1.158     rmind  * for a process specified by PID.
    798   1.74    atatat  */
    799   1.74    atatat static int
    800   1.74    atatat sysctl_proc_corename(SYSCTLFN_ARGS)
    801   1.74    atatat {
    802  1.158     rmind 	struct proc *p;
    803   1.83        pk 	struct plimit *lim;
    804  1.158     rmind 	char *cnbuf, *cname;
    805  1.157     rmind 	struct sysctlnode node;
    806  1.158     rmind 	size_t len;
    807  1.158     rmind 	int error;
    808   1.74    atatat 
    809  1.158     rmind 	/* First, validate the request. */
    810  1.158     rmind 	if (namelen != 0 || name[-1] != PROC_PID_CORENAME)
    811  1.158     rmind 		return EINVAL;
    812   1.74    atatat 
    813  1.157     rmind 	/* Find the process.  Hold a reference (p_reflock), if found. */
    814  1.158     rmind 	error = sysctl_proc_findproc(l, (pid_t)name[-2], &p);
    815   1.74    atatat 	if (error)
    816  1.157     rmind 		return error;
    817   1.74    atatat 
    818  1.131      elad 	/* XXX-elad */
    819  1.158     rmind 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, p,
    820  1.131      elad 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    821  1.157     rmind 	if (error) {
    822  1.158     rmind 		rw_exit(&p->p_reflock);
    823  1.157     rmind 		return error;
    824  1.157     rmind 	}
    825  1.111      elad 
    826  1.158     rmind 	cnbuf = PNBUF_GET();
    827  1.158     rmind 
    828  1.131      elad 	if (newp == NULL) {
    829  1.158     rmind 		/* Get case: copy the core name into the buffer. */
    830  1.131      elad 		error = kauth_authorize_process(l->l_cred,
    831  1.158     rmind 		    KAUTH_PROCESS_CORENAME, p,
    832  1.131      elad 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_GET), NULL, NULL);
    833  1.157     rmind 		if (error) {
    834  1.158     rmind 			goto done;
    835  1.158     rmind 		}
    836  1.158     rmind 		lim = p->p_limit;
    837  1.158     rmind 		mutex_enter(&lim->pl_lock);
    838  1.158     rmind 		strlcpy(cnbuf, lim->pl_corename, MAXPATHLEN);
    839  1.158     rmind 		mutex_exit(&lim->pl_lock);
    840  1.158     rmind 	} else {
    841  1.158     rmind 		/* Set case: just use the temporary buffer. */
    842  1.158     rmind 		error = kauth_authorize_process(l->l_cred,
    843  1.158     rmind 		    KAUTH_PROCESS_CORENAME, p,
    844  1.158     rmind 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_SET), cnbuf, NULL);
    845  1.158     rmind 		if (error) {
    846  1.158     rmind 			goto done;
    847  1.157     rmind 		}
    848  1.131      elad 	}
    849  1.131      elad 
    850   1.74    atatat 	node = *rnode;
    851  1.158     rmind 	node.sysctl_data = cnbuf;
    852   1.74    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    853   1.74    atatat 
    854  1.158     rmind 	/* Return if error, or if we are only retrieving the core name. */
    855  1.158     rmind 	if (error || newp == NULL) {
    856  1.100      yamt 		goto done;
    857  1.157     rmind 	}
    858  1.103      elad 
    859   1.74    atatat 	/*
    860  1.158     rmind 	 * Validate new core name.  It must be either "core", "/core",
    861  1.158     rmind 	 * or end in ".core".
    862   1.74    atatat 	 */
    863  1.158     rmind 	len = strlen(cnbuf);
    864  1.158     rmind 	if ((len < 4 || strcmp(cnbuf + len - 4, "core") != 0) ||
    865  1.158     rmind 	    (len > 4 && cnbuf[len - 5] != '/' && cnbuf[len - 5] != '.')) {
    866  1.100      yamt 		error = EINVAL;
    867  1.100      yamt 		goto done;
    868  1.100      yamt 	}
    869   1.74    atatat 
    870  1.158     rmind 	/* Allocate, copy and set the new core name for plimit structure. */
    871  1.161     rmind 	cname = kmem_alloc(++len, KM_NOSLEEP);
    872  1.158     rmind 	if (cname == NULL) {
    873  1.100      yamt 		error = ENOMEM;
    874  1.100      yamt 		goto done;
    875  1.100      yamt 	}
    876  1.158     rmind 	memcpy(cname, cnbuf, len);
    877  1.160     rmind 	lim_setcorename(p, cname, len);
    878  1.100      yamt done:
    879  1.158     rmind 	rw_exit(&p->p_reflock);
    880  1.158     rmind 	PNBUF_PUT(cnbuf);
    881  1.100      yamt 	return error;
    882   1.74    atatat }
    883   1.74    atatat 
    884   1.74    atatat /*
    885   1.74    atatat  * sysctl helper routine for checking/setting a process's stop flags,
    886   1.74    atatat  * one for fork and one for exec.
    887   1.74    atatat  */
    888   1.74    atatat static int
    889   1.74    atatat sysctl_proc_stop(SYSCTLFN_ARGS)
    890   1.74    atatat {
    891  1.102        ad 	struct proc *ptmp;
    892   1.74    atatat 	int i, f, error = 0;
    893   1.74    atatat 	struct sysctlnode node;
    894   1.74    atatat 
    895   1.74    atatat 	if (namelen != 0)
    896   1.74    atatat 		return (EINVAL);
    897   1.74    atatat 
    898  1.157     rmind 	/* Find the process.  Hold a reference (p_reflock), if found. */
    899  1.157     rmind 	error = sysctl_proc_findproc(l, (pid_t)name[-2], &ptmp);
    900   1.74    atatat 	if (error)
    901  1.157     rmind 		return error;
    902   1.74    atatat 
    903  1.131      elad 	/* XXX-elad */
    904  1.131      elad 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
    905  1.131      elad 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    906  1.111      elad 	if (error)
    907  1.157     rmind 		goto out;
    908  1.111      elad 
    909   1.74    atatat 	switch (rnode->sysctl_num) {
    910   1.74    atatat 	case PROC_PID_STOPFORK:
    911  1.113        ad 		f = PS_STOPFORK;
    912   1.74    atatat 		break;
    913   1.74    atatat 	case PROC_PID_STOPEXEC:
    914  1.113        ad 		f = PS_STOPEXEC;
    915   1.74    atatat 		break;
    916   1.74    atatat 	case PROC_PID_STOPEXIT:
    917  1.113        ad 		f = PS_STOPEXIT;
    918   1.74    atatat 		break;
    919   1.74    atatat 	default:
    920  1.157     rmind 		error = EINVAL;
    921  1.157     rmind 		goto out;
    922   1.74    atatat 	}
    923   1.74    atatat 
    924   1.74    atatat 	i = (ptmp->p_flag & f) ? 1 : 0;
    925   1.74    atatat 	node = *rnode;
    926   1.74    atatat 	node.sysctl_data = &i;
    927   1.74    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    928   1.74    atatat 	if (error || newp == NULL)
    929  1.157     rmind 		goto out;
    930   1.74    atatat 
    931  1.139        ad 	mutex_enter(ptmp->p_lock);
    932  1.111      elad 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_STOPFLAG,
    933  1.111      elad 	    ptmp, KAUTH_ARG(f), NULL, NULL);
    934  1.143     rmind 	if (!error) {
    935  1.143     rmind 		if (i) {
    936  1.143     rmind 			ptmp->p_sflag |= f;
    937  1.143     rmind 		} else {
    938  1.143     rmind 			ptmp->p_sflag &= ~f;
    939  1.143     rmind 		}
    940  1.143     rmind 	}
    941  1.139        ad 	mutex_exit(ptmp->p_lock);
    942  1.157     rmind out:
    943  1.157     rmind 	rw_exit(&ptmp->p_reflock);
    944  1.143     rmind 	return error;
    945   1.74    atatat }
    946   1.74    atatat 
    947   1.74    atatat /*
    948   1.74    atatat  * sysctl helper routine for a process's rlimits as exposed by sysctl.
    949   1.74    atatat  */
    950   1.74    atatat static int
    951   1.74    atatat sysctl_proc_plimit(SYSCTLFN_ARGS)
    952   1.74    atatat {
    953  1.102        ad 	struct proc *ptmp;
    954   1.74    atatat 	u_int limitno;
    955   1.74    atatat 	int which, error = 0;
    956   1.74    atatat         struct rlimit alim;
    957   1.74    atatat 	struct sysctlnode node;
    958   1.74    atatat 
    959   1.74    atatat 	if (namelen != 0)
    960   1.74    atatat 		return (EINVAL);
    961   1.74    atatat 
    962   1.74    atatat 	which = name[-1];
    963   1.74    atatat 	if (which != PROC_PID_LIMIT_TYPE_SOFT &&
    964   1.74    atatat 	    which != PROC_PID_LIMIT_TYPE_HARD)
    965   1.74    atatat 		return (EINVAL);
    966   1.74    atatat 
    967   1.74    atatat 	limitno = name[-2] - 1;
    968   1.74    atatat 	if (limitno >= RLIM_NLIMITS)
    969   1.74    atatat 		return (EINVAL);
    970   1.74    atatat 
    971   1.74    atatat 	if (name[-3] != PROC_PID_LIMIT)
    972   1.74    atatat 		return (EINVAL);
    973   1.74    atatat 
    974  1.157     rmind 	/* Find the process.  Hold a reference (p_reflock), if found. */
    975  1.157     rmind 	error = sysctl_proc_findproc(l, (pid_t)name[-4], &ptmp);
    976   1.74    atatat 	if (error)
    977  1.157     rmind 		return error;
    978   1.74    atatat 
    979  1.131      elad 	/* XXX-elad */
    980  1.131      elad 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
    981  1.131      elad 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    982  1.111      elad 	if (error)
    983  1.157     rmind 		goto out;
    984  1.111      elad 
    985  1.131      elad 	/* Check if we can view limits. */
    986  1.131      elad 	if (newp == NULL) {
    987  1.131      elad 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
    988  1.131      elad 		    ptmp, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_GET), &alim,
    989  1.131      elad 		    KAUTH_ARG(which));
    990  1.131      elad 		if (error)
    991  1.157     rmind 			goto out;
    992  1.131      elad 	}
    993  1.131      elad 
    994   1.74    atatat 	node = *rnode;
    995   1.74    atatat 	memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
    996   1.74    atatat 	if (which == PROC_PID_LIMIT_TYPE_HARD)
    997   1.74    atatat 		node.sysctl_data = &alim.rlim_max;
    998   1.74    atatat 	else
    999   1.74    atatat 		node.sysctl_data = &alim.rlim_cur;
   1000   1.74    atatat 
   1001   1.74    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1002  1.157     rmind 	if (error || newp == NULL) {
   1003  1.157     rmind 		goto out;
   1004  1.157     rmind 	}
   1005  1.157     rmind 	error = dosetrlimit(l, ptmp, limitno, &alim);
   1006  1.157     rmind out:
   1007  1.157     rmind 	rw_exit(&ptmp->p_reflock);
   1008  1.157     rmind 	return error;
   1009   1.74    atatat }
   1010   1.74    atatat 
   1011  1.156     pooka static struct sysctllog *proc_sysctllog;
   1012  1.156     pooka 
   1013   1.74    atatat /*
   1014   1.74    atatat  * and finally, the actually glue that sticks it to the tree
   1015   1.74    atatat  */
   1016  1.156     pooka static void
   1017  1.156     pooka sysctl_proc_setup()
   1018   1.74    atatat {
   1019   1.74    atatat 
   1020  1.156     pooka 	sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
   1021   1.76    atatat 		       CTLFLAG_PERMANENT,
   1022   1.74    atatat 		       CTLTYPE_NODE, "proc", NULL,
   1023   1.74    atatat 		       NULL, 0, NULL, 0,
   1024   1.74    atatat 		       CTL_PROC, CTL_EOL);
   1025  1.156     pooka 	sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
   1026   1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
   1027   1.78    atatat 		       CTLTYPE_NODE, "curproc",
   1028   1.78    atatat 		       SYSCTL_DESCR("Per-process settings"),
   1029   1.74    atatat 		       NULL, 0, NULL, 0,
   1030   1.74    atatat 		       CTL_PROC, PROC_CURPROC, CTL_EOL);
   1031   1.74    atatat 
   1032  1.156     pooka 	sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
   1033  1.103      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
   1034   1.78    atatat 		       CTLTYPE_STRING, "corename",
   1035   1.78    atatat 		       SYSCTL_DESCR("Core file name"),
   1036   1.74    atatat 		       sysctl_proc_corename, 0, NULL, MAXPATHLEN,
   1037   1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
   1038  1.156     pooka 	sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
   1039   1.76    atatat 		       CTLFLAG_PERMANENT,
   1040   1.78    atatat 		       CTLTYPE_NODE, "rlimit",
   1041   1.78    atatat 		       SYSCTL_DESCR("Process limits"),
   1042   1.74    atatat 		       NULL, 0, NULL, 0,
   1043   1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
   1044   1.74    atatat 
   1045   1.74    atatat #define create_proc_plimit(s, n) do {					\
   1046  1.156     pooka 	sysctl_createv(&proc_sysctllog, 0, NULL, NULL,			\
   1047   1.76    atatat 		       CTLFLAG_PERMANENT,				\
   1048   1.78    atatat 		       CTLTYPE_NODE, s,					\
   1049   1.78    atatat 		       SYSCTL_DESCR("Process " s " limits"),		\
   1050   1.74    atatat 		       NULL, 0, NULL, 0,				\
   1051   1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
   1052   1.74    atatat 		       CTL_EOL);					\
   1053  1.156     pooka 	sysctl_createv(&proc_sysctllog, 0, NULL, NULL,			\
   1054   1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
   1055   1.78    atatat 		       CTLTYPE_QUAD, "soft",				\
   1056   1.78    atatat 		       SYSCTL_DESCR("Process soft " s " limit"),	\
   1057   1.74    atatat 		       sysctl_proc_plimit, 0, NULL, 0,			\
   1058   1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
   1059   1.74    atatat 		       PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL);		\
   1060  1.156     pooka 	sysctl_createv(&proc_sysctllog, 0, NULL, NULL,			\
   1061   1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
   1062   1.78    atatat 		       CTLTYPE_QUAD, "hard",				\
   1063   1.78    atatat 		       SYSCTL_DESCR("Process hard " s " limit"),	\
   1064   1.74    atatat 		       sysctl_proc_plimit, 0, NULL, 0,			\
   1065   1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
   1066   1.74    atatat 		       PROC_PID_LIMIT_TYPE_HARD, CTL_EOL);		\
   1067   1.74    atatat 	} while (0/*CONSTCOND*/)
   1068   1.74    atatat 
   1069   1.74    atatat 	create_proc_plimit("cputime",		PROC_PID_LIMIT_CPU);
   1070   1.74    atatat 	create_proc_plimit("filesize",		PROC_PID_LIMIT_FSIZE);
   1071   1.74    atatat 	create_proc_plimit("datasize",		PROC_PID_LIMIT_DATA);
   1072   1.74    atatat 	create_proc_plimit("stacksize",		PROC_PID_LIMIT_STACK);
   1073   1.74    atatat 	create_proc_plimit("coredumpsize",	PROC_PID_LIMIT_CORE);
   1074   1.74    atatat 	create_proc_plimit("memoryuse",		PROC_PID_LIMIT_RSS);
   1075   1.74    atatat 	create_proc_plimit("memorylocked",	PROC_PID_LIMIT_MEMLOCK);
   1076   1.74    atatat 	create_proc_plimit("maxproc",		PROC_PID_LIMIT_NPROC);
   1077   1.74    atatat 	create_proc_plimit("descriptors",	PROC_PID_LIMIT_NOFILE);
   1078   1.79  christos 	create_proc_plimit("sbsize",		PROC_PID_LIMIT_SBSIZE);
   1079  1.151       mrg 	create_proc_plimit("vmemoryuse",	PROC_PID_LIMIT_AS);
   1080   1.74    atatat 
   1081   1.74    atatat #undef create_proc_plimit
   1082   1.74    atatat 
   1083  1.156     pooka 	sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
   1084   1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
   1085   1.78    atatat 		       CTLTYPE_INT, "stopfork",
   1086   1.78    atatat 		       SYSCTL_DESCR("Stop process at fork(2)"),
   1087   1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
   1088   1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
   1089  1.156     pooka 	sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
   1090   1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
   1091   1.78    atatat 		       CTLTYPE_INT, "stopexec",
   1092   1.78    atatat 		       SYSCTL_DESCR("Stop process at execve(2)"),
   1093   1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
   1094   1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
   1095  1.156     pooka 	sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
   1096   1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
   1097   1.78    atatat 		       CTLTYPE_INT, "stopexit",
   1098   1.78    atatat 		       SYSCTL_DESCR("Stop process before completing exit"),
   1099   1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
   1100   1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
   1101   1.17       cgd }
   1102