Home | History | Annotate | Line # | Download | only in kern
kern_resource.c revision 1.139.2.4
      1  1.139.2.4      yamt /*	$NetBSD: kern_resource.c,v 1.139.2.4 2010/03/11 15:04:17 yamt 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.139.2.4      yamt __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.139.2.4 2010/03/11 15:04:17 yamt 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.139.2.4      yamt static kauth_listener_t	resource_listener;
     73  1.139.2.4      yamt 
     74  1.139.2.4      yamt static int
     75  1.139.2.4      yamt resource_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
     76  1.139.2.4      yamt     void *arg0, void *arg1, void *arg2, void *arg3)
     77  1.139.2.4      yamt {
     78  1.139.2.4      yamt 	struct proc *p;
     79  1.139.2.4      yamt 	int result;
     80  1.139.2.4      yamt 
     81  1.139.2.4      yamt 	result = KAUTH_RESULT_DEFER;
     82  1.139.2.4      yamt 	p = arg0;
     83  1.139.2.4      yamt 
     84  1.139.2.4      yamt 	switch (action) {
     85  1.139.2.4      yamt 	case KAUTH_PROCESS_NICE:
     86  1.139.2.4      yamt 		if (kauth_cred_geteuid(cred) != kauth_cred_geteuid(p->p_cred) &&
     87  1.139.2.4      yamt                     kauth_cred_getuid(cred) != kauth_cred_geteuid(p->p_cred)) {
     88  1.139.2.4      yamt                         break;
     89  1.139.2.4      yamt                 }
     90  1.139.2.4      yamt 
     91  1.139.2.4      yamt                 if ((u_long)arg1 >= p->p_nice)
     92  1.139.2.4      yamt                         result = KAUTH_RESULT_ALLOW;
     93  1.139.2.4      yamt 
     94  1.139.2.4      yamt 		break;
     95  1.139.2.4      yamt 
     96  1.139.2.4      yamt 	case KAUTH_PROCESS_RLIMIT: {
     97  1.139.2.4      yamt 		enum kauth_process_req req;
     98  1.139.2.4      yamt 
     99  1.139.2.4      yamt 		req = (enum kauth_process_req)(unsigned long)arg1;
    100  1.139.2.4      yamt 
    101  1.139.2.4      yamt 		switch (req) {
    102  1.139.2.4      yamt 		case KAUTH_REQ_PROCESS_RLIMIT_GET:
    103  1.139.2.4      yamt 			result = KAUTH_RESULT_ALLOW;
    104  1.139.2.4      yamt 			break;
    105  1.139.2.4      yamt 
    106  1.139.2.4      yamt 		case KAUTH_REQ_PROCESS_RLIMIT_SET: {
    107  1.139.2.4      yamt 			struct rlimit *new_rlimit;
    108  1.139.2.4      yamt 			u_long which;
    109  1.139.2.4      yamt 
    110  1.139.2.4      yamt 			if ((p != curlwp->l_proc) &&
    111  1.139.2.4      yamt 			    (proc_uidmatch(cred, p->p_cred) != 0))
    112  1.139.2.4      yamt 				break;
    113  1.139.2.4      yamt 
    114  1.139.2.4      yamt 			new_rlimit = arg2;
    115  1.139.2.4      yamt 			which = (u_long)arg3;
    116  1.139.2.4      yamt 
    117  1.139.2.4      yamt 			if (new_rlimit->rlim_max <= p->p_rlimit[which].rlim_max)
    118  1.139.2.4      yamt 				result = KAUTH_RESULT_ALLOW;
    119  1.139.2.4      yamt 
    120  1.139.2.4      yamt 			break;
    121  1.139.2.4      yamt 			}
    122  1.139.2.4      yamt 
    123  1.139.2.4      yamt 		default:
    124  1.139.2.4      yamt 			break;
    125  1.139.2.4      yamt 		}
    126  1.139.2.4      yamt 
    127  1.139.2.4      yamt 		break;
    128  1.139.2.4      yamt 	}
    129  1.139.2.4      yamt 
    130  1.139.2.4      yamt 	default:
    131  1.139.2.4      yamt 		break;
    132  1.139.2.4      yamt 	}
    133  1.139.2.4      yamt 
    134  1.139.2.4      yamt 	return result;
    135  1.139.2.4      yamt }
    136  1.139.2.4      yamt 
    137      1.130        ad void
    138      1.130        ad resource_init(void)
    139      1.130        ad {
    140      1.130        ad 
    141      1.130        ad 	plimit_cache = pool_cache_init(sizeof(struct plimit), 0, 0, 0,
    142      1.130        ad 	    "plimitpl", NULL, IPL_NONE, NULL, NULL, NULL);
    143      1.130        ad 	pstats_cache = pool_cache_init(sizeof(struct pstats), 0, 0, 0,
    144      1.130        ad 	    "pstatspl", NULL, IPL_NONE, NULL, NULL, NULL);
    145  1.139.2.4      yamt 
    146  1.139.2.4      yamt 	resource_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    147  1.139.2.4      yamt 	    resource_listener_cb, NULL);
    148      1.130        ad }
    149      1.130        ad 
    150       1.60       eeh /*
    151       1.17       cgd  * Resource controls and accounting.
    152       1.17       cgd  */
    153       1.17       cgd 
    154       1.25       cgd int
    155      1.134     rmind sys_getpriority(struct lwp *l, const struct sys_getpriority_args *uap,
    156      1.134     rmind     register_t *retval)
    157       1.30   thorpej {
    158      1.128       dsl 	/* {
    159       1.22       cgd 		syscallarg(int) which;
    160       1.81    kleink 		syscallarg(id_t) who;
    161      1.128       dsl 	} */
    162       1.68   thorpej 	struct proc *curp = l->l_proc, *p;
    163       1.54  augustss 	int low = NZERO + PRIO_MAX + 1;
    164      1.113        ad 	int who = SCARG(uap, who);
    165       1.17       cgd 
    166      1.138        ad 	mutex_enter(proc_lock);
    167       1.22       cgd 	switch (SCARG(uap, which)) {
    168       1.17       cgd 	case PRIO_PROCESS:
    169      1.113        ad 		if (who == 0)
    170       1.17       cgd 			p = curp;
    171       1.17       cgd 		else
    172      1.113        ad 			p = p_find(who, PFIND_LOCKED);
    173      1.113        ad 		if (p != NULL)
    174      1.113        ad 			low = p->p_nice;
    175       1.17       cgd 		break;
    176       1.17       cgd 
    177       1.17       cgd 	case PRIO_PGRP: {
    178       1.54  augustss 		struct pgrp *pg;
    179       1.17       cgd 
    180      1.113        ad 		if (who == 0)
    181       1.17       cgd 			pg = curp->p_pgrp;
    182      1.113        ad 		else if ((pg = pg_find(who, PFIND_LOCKED)) == NULL)
    183       1.17       cgd 			break;
    184       1.64      matt 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    185       1.17       cgd 			if (p->p_nice < low)
    186       1.17       cgd 				low = p->p_nice;
    187       1.17       cgd 		}
    188       1.17       cgd 		break;
    189       1.17       cgd 	}
    190       1.17       cgd 
    191       1.17       cgd 	case PRIO_USER:
    192      1.113        ad 		if (who == 0)
    193      1.113        ad 			who = (int)kauth_cred_geteuid(l->l_cred);
    194       1.86      yamt 		PROCLIST_FOREACH(p, &allproc) {
    195      1.139        ad 			mutex_enter(p->p_lock);
    196      1.102        ad 			if (kauth_cred_geteuid(p->p_cred) ==
    197      1.113        ad 			    (uid_t)who && p->p_nice < low)
    198       1.17       cgd 				low = p->p_nice;
    199      1.139        ad 			mutex_exit(p->p_lock);
    200       1.64      matt 		}
    201       1.17       cgd 		break;
    202       1.17       cgd 
    203       1.17       cgd 	default:
    204      1.138        ad 		mutex_exit(proc_lock);
    205       1.17       cgd 		return (EINVAL);
    206       1.17       cgd 	}
    207      1.138        ad 	mutex_exit(proc_lock);
    208      1.113        ad 
    209       1.37        ws 	if (low == NZERO + PRIO_MAX + 1)
    210       1.17       cgd 		return (ESRCH);
    211       1.37        ws 	*retval = low - NZERO;
    212       1.17       cgd 	return (0);
    213       1.17       cgd }
    214       1.17       cgd 
    215       1.17       cgd /* ARGSUSED */
    216       1.25       cgd int
    217      1.134     rmind sys_setpriority(struct lwp *l, const struct sys_setpriority_args *uap,
    218      1.134     rmind     register_t *retval)
    219       1.30   thorpej {
    220      1.128       dsl 	/* {
    221       1.22       cgd 		syscallarg(int) which;
    222       1.81    kleink 		syscallarg(id_t) who;
    223       1.22       cgd 		syscallarg(int) prio;
    224      1.128       dsl 	} */
    225       1.68   thorpej 	struct proc *curp = l->l_proc, *p;
    226       1.17       cgd 	int found = 0, error = 0;
    227      1.113        ad 	int who = SCARG(uap, who);
    228       1.17       cgd 
    229      1.138        ad 	mutex_enter(proc_lock);
    230       1.22       cgd 	switch (SCARG(uap, which)) {
    231       1.17       cgd 	case PRIO_PROCESS:
    232      1.113        ad 		if (who == 0)
    233       1.17       cgd 			p = curp;
    234       1.17       cgd 		else
    235      1.113        ad 			p = p_find(who, PFIND_LOCKED);
    236      1.113        ad 		if (p != 0) {
    237      1.139        ad 			mutex_enter(p->p_lock);
    238      1.113        ad 			error = donice(l, p, SCARG(uap, prio));
    239      1.139        ad 			mutex_exit(p->p_lock);
    240  1.139.2.2      yamt 			found++;
    241      1.113        ad 		}
    242       1.17       cgd 		break;
    243       1.17       cgd 
    244       1.17       cgd 	case PRIO_PGRP: {
    245       1.54  augustss 		struct pgrp *pg;
    246       1.87     perry 
    247      1.113        ad 		if (who == 0)
    248       1.17       cgd 			pg = curp->p_pgrp;
    249      1.113        ad 		else if ((pg = pg_find(who, PFIND_LOCKED)) == NULL)
    250       1.17       cgd 			break;
    251       1.64      matt 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    252      1.139        ad 			mutex_enter(p->p_lock);
    253      1.102        ad 			error = donice(l, p, SCARG(uap, prio));
    254      1.139        ad 			mutex_exit(p->p_lock);
    255       1.17       cgd 			found++;
    256       1.17       cgd 		}
    257       1.17       cgd 		break;
    258       1.17       cgd 	}
    259       1.17       cgd 
    260       1.17       cgd 	case PRIO_USER:
    261      1.113        ad 		if (who == 0)
    262      1.113        ad 			who = (int)kauth_cred_geteuid(l->l_cred);
    263       1.86      yamt 		PROCLIST_FOREACH(p, &allproc) {
    264      1.139        ad 			mutex_enter(p->p_lock);
    265      1.102        ad 			if (kauth_cred_geteuid(p->p_cred) ==
    266      1.102        ad 			    (uid_t)SCARG(uap, who)) {
    267      1.102        ad 				error = donice(l, p, SCARG(uap, prio));
    268       1.17       cgd 				found++;
    269       1.17       cgd 			}
    270      1.139        ad 			mutex_exit(p->p_lock);
    271       1.64      matt 		}
    272       1.17       cgd 		break;
    273       1.17       cgd 
    274       1.17       cgd 	default:
    275  1.139.2.2      yamt 		mutex_exit(proc_lock);
    276  1.139.2.2      yamt 		return EINVAL;
    277       1.17       cgd 	}
    278      1.138        ad 	mutex_exit(proc_lock);
    279       1.17       cgd 	if (found == 0)
    280       1.17       cgd 		return (ESRCH);
    281       1.17       cgd 	return (error);
    282       1.17       cgd }
    283       1.17       cgd 
    284      1.113        ad /*
    285      1.113        ad  * Renice a process.
    286      1.113        ad  *
    287      1.113        ad  * Call with the target process' credentials locked.
    288      1.113        ad  */
    289       1.25       cgd int
    290      1.102        ad donice(struct lwp *l, struct proc *chgp, int n)
    291       1.17       cgd {
    292      1.102        ad 	kauth_cred_t cred = l->l_cred;
    293      1.113        ad 
    294      1.139        ad 	KASSERT(mutex_owned(chgp->p_lock));
    295       1.17       cgd 
    296  1.139.2.3      yamt 	if (kauth_cred_geteuid(cred) && kauth_cred_getuid(cred) &&
    297  1.139.2.3      yamt 	    kauth_cred_geteuid(cred) != kauth_cred_geteuid(chgp->p_cred) &&
    298  1.139.2.3      yamt 	    kauth_cred_getuid(cred) != kauth_cred_geteuid(chgp->p_cred))
    299  1.139.2.3      yamt 		return (EPERM);
    300  1.139.2.3      yamt 
    301       1.17       cgd 	if (n > PRIO_MAX)
    302       1.17       cgd 		n = PRIO_MAX;
    303       1.17       cgd 	if (n < PRIO_MIN)
    304       1.17       cgd 		n = PRIO_MIN;
    305       1.37        ws 	n += NZERO;
    306      1.112      elad 	if (kauth_authorize_process(cred, KAUTH_PROCESS_NICE, chgp,
    307      1.112      elad 	    KAUTH_ARG(n), NULL, NULL))
    308       1.17       cgd 		return (EACCES);
    309      1.117      yamt 	sched_nice(chgp, n);
    310       1.17       cgd 	return (0);
    311       1.17       cgd }
    312       1.17       cgd 
    313       1.17       cgd /* ARGSUSED */
    314       1.25       cgd int
    315      1.134     rmind sys_setrlimit(struct lwp *l, const struct sys_setrlimit_args *uap,
    316      1.134     rmind     register_t *retval)
    317       1.30   thorpej {
    318      1.128       dsl 	/* {
    319       1.42   mycroft 		syscallarg(int) which;
    320       1.39       cgd 		syscallarg(const struct rlimit *) rlp;
    321      1.128       dsl 	} */
    322       1.42   mycroft 	int which = SCARG(uap, which);
    323       1.19       cgd 	struct rlimit alim;
    324       1.17       cgd 	int error;
    325       1.17       cgd 
    326       1.46     perry 	error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
    327       1.33  christos 	if (error)
    328       1.17       cgd 		return (error);
    329      1.102        ad 	return (dosetrlimit(l, l->l_proc, which, &alim));
    330       1.17       cgd }
    331       1.17       cgd 
    332       1.17       cgd int
    333      1.102        ad dosetrlimit(struct lwp *l, struct proc *p, int which, struct rlimit *limp)
    334       1.17       cgd {
    335       1.54  augustss 	struct rlimit *alimp;
    336       1.17       cgd 	int error;
    337       1.17       cgd 
    338       1.67    itojun 	if ((u_int)which >= RLIM_NLIMITS)
    339       1.17       cgd 		return (EINVAL);
    340       1.38  matthias 
    341       1.62  jdolecek 	if (limp->rlim_cur > limp->rlim_max) {
    342       1.62  jdolecek 		/*
    343       1.62  jdolecek 		 * This is programming error. According to SUSv2, we should
    344       1.62  jdolecek 		 * return error in this case.
    345       1.62  jdolecek 		 */
    346       1.62  jdolecek 		return (EINVAL);
    347       1.62  jdolecek 	}
    348      1.122       dsl 
    349      1.122       dsl 	alimp = &p->p_rlimit[which];
    350      1.122       dsl 	/* if we don't change the value, no need to limcopy() */
    351      1.122       dsl 	if (limp->rlim_cur == alimp->rlim_cur &&
    352      1.122       dsl 	    limp->rlim_max == alimp->rlim_max)
    353      1.122       dsl 		return 0;
    354      1.122       dsl 
    355      1.112      elad 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
    356      1.131      elad 	    p, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_SET), limp, KAUTH_ARG(which));
    357      1.111      elad 	if (error)
    358      1.122       dsl 		return (error);
    359       1.62  jdolecek 
    360      1.122       dsl 	lim_privatise(p, false);
    361      1.122       dsl 	/* p->p_limit is now unchangeable */
    362      1.122       dsl 	alimp = &p->p_rlimit[which];
    363       1.17       cgd 
    364       1.17       cgd 	switch (which) {
    365       1.17       cgd 
    366       1.17       cgd 	case RLIMIT_DATA:
    367       1.19       cgd 		if (limp->rlim_cur > maxdmap)
    368       1.19       cgd 			limp->rlim_cur = maxdmap;
    369       1.19       cgd 		if (limp->rlim_max > maxdmap)
    370       1.19       cgd 			limp->rlim_max = maxdmap;
    371       1.17       cgd 		break;
    372       1.17       cgd 
    373       1.17       cgd 	case RLIMIT_STACK:
    374       1.19       cgd 		if (limp->rlim_cur > maxsmap)
    375       1.19       cgd 			limp->rlim_cur = maxsmap;
    376       1.19       cgd 		if (limp->rlim_max > maxsmap)
    377       1.19       cgd 			limp->rlim_max = maxsmap;
    378       1.62  jdolecek 
    379       1.62  jdolecek 		/*
    380       1.62  jdolecek 		 * Return EINVAL if the new stack size limit is lower than
    381       1.62  jdolecek 		 * current usage. Otherwise, the process would get SIGSEGV the
    382       1.62  jdolecek 		 * moment it would try to access anything on it's current stack.
    383       1.62  jdolecek 		 * This conforms to SUSv2.
    384       1.62  jdolecek 		 */
    385       1.62  jdolecek 		if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE
    386      1.113        ad 		    || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE) {
    387       1.62  jdolecek 			return (EINVAL);
    388      1.113        ad 		}
    389       1.40     enami 
    390       1.17       cgd 		/*
    391       1.40     enami 		 * Stack is allocated to the max at exec time with
    392       1.40     enami 		 * only "rlim_cur" bytes accessible (In other words,
    393       1.40     enami 		 * allocates stack dividing two contiguous regions at
    394       1.40     enami 		 * "rlim_cur" bytes boundary).
    395       1.40     enami 		 *
    396       1.40     enami 		 * Since allocation is done in terms of page, roundup
    397       1.40     enami 		 * "rlim_cur" (otherwise, contiguous regions
    398       1.40     enami 		 * overlap).  If stack limit is going up make more
    399       1.40     enami 		 * accessible, if going down make inaccessible.
    400       1.17       cgd 		 */
    401       1.40     enami 		limp->rlim_cur = round_page(limp->rlim_cur);
    402       1.17       cgd 		if (limp->rlim_cur != alimp->rlim_cur) {
    403       1.48       eeh 			vaddr_t addr;
    404       1.48       eeh 			vsize_t size;
    405       1.17       cgd 			vm_prot_t prot;
    406       1.17       cgd 
    407       1.17       cgd 			if (limp->rlim_cur > alimp->rlim_cur) {
    408       1.73       chs 				prot = VM_PROT_READ | VM_PROT_WRITE;
    409       1.17       cgd 				size = limp->rlim_cur - alimp->rlim_cur;
    410       1.91      fvdl 				addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
    411       1.91      fvdl 				    limp->rlim_cur;
    412       1.17       cgd 			} else {
    413       1.17       cgd 				prot = VM_PROT_NONE;
    414       1.17       cgd 				size = alimp->rlim_cur - limp->rlim_cur;
    415       1.91      fvdl 				addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
    416       1.91      fvdl 				     alimp->rlim_cur;
    417       1.17       cgd 			}
    418       1.43       mrg 			(void) uvm_map_protect(&p->p_vmspace->vm_map,
    419      1.114   thorpej 			    addr, addr+size, prot, false);
    420       1.17       cgd 		}
    421       1.17       cgd 		break;
    422       1.19       cgd 
    423       1.19       cgd 	case RLIMIT_NOFILE:
    424       1.19       cgd 		if (limp->rlim_cur > maxfiles)
    425       1.19       cgd 			limp->rlim_cur = maxfiles;
    426       1.19       cgd 		if (limp->rlim_max > maxfiles)
    427       1.19       cgd 			limp->rlim_max = maxfiles;
    428       1.19       cgd 		break;
    429       1.19       cgd 
    430       1.19       cgd 	case RLIMIT_NPROC:
    431       1.19       cgd 		if (limp->rlim_cur > maxproc)
    432       1.19       cgd 			limp->rlim_cur = maxproc;
    433       1.19       cgd 		if (limp->rlim_max > maxproc)
    434       1.19       cgd 			limp->rlim_max = maxproc;
    435       1.19       cgd 		break;
    436       1.17       cgd 	}
    437      1.122       dsl 
    438      1.122       dsl 	mutex_enter(&p->p_limit->pl_lock);
    439       1.17       cgd 	*alimp = *limp;
    440      1.122       dsl 	mutex_exit(&p->p_limit->pl_lock);
    441       1.17       cgd 	return (0);
    442       1.17       cgd }
    443       1.17       cgd 
    444       1.17       cgd /* ARGSUSED */
    445       1.25       cgd int
    446      1.134     rmind sys_getrlimit(struct lwp *l, const struct sys_getrlimit_args *uap,
    447      1.134     rmind     register_t *retval)
    448       1.30   thorpej {
    449      1.128       dsl 	/* {
    450       1.42   mycroft 		syscallarg(int) which;
    451       1.22       cgd 		syscallarg(struct rlimit *) rlp;
    452      1.128       dsl 	} */
    453       1.68   thorpej 	struct proc *p = l->l_proc;
    454       1.42   mycroft 	int which = SCARG(uap, which);
    455      1.119        ad 	struct rlimit rl;
    456       1.17       cgd 
    457       1.67    itojun 	if ((u_int)which >= RLIM_NLIMITS)
    458       1.17       cgd 		return (EINVAL);
    459      1.119        ad 
    460      1.139        ad 	mutex_enter(p->p_lock);
    461      1.119        ad 	memcpy(&rl, &p->p_rlimit[which], sizeof(rl));
    462      1.139        ad 	mutex_exit(p->p_lock);
    463      1.119        ad 
    464      1.119        ad 	return copyout(&rl, SCARG(uap, rlp), sizeof(rl));
    465       1.17       cgd }
    466       1.17       cgd 
    467       1.17       cgd /*
    468       1.17       cgd  * Transform the running time and tick information in proc p into user,
    469       1.17       cgd  * system, and interrupt time usage.
    470      1.113        ad  *
    471      1.139        ad  * Should be called with p->p_lock held unless called from exit1().
    472       1.17       cgd  */
    473       1.25       cgd void
    474       1.98   thorpej calcru(struct proc *p, struct timeval *up, struct timeval *sp,
    475      1.113        ad     struct timeval *ip, struct timeval *rp)
    476       1.17       cgd {
    477      1.129      yamt 	uint64_t u, st, ut, it, tot;
    478       1.68   thorpej 	struct lwp *l;
    479      1.129      yamt 	struct bintime tm;
    480      1.129      yamt 	struct timeval tv;
    481       1.17       cgd 
    482      1.113        ad 	mutex_spin_enter(&p->p_stmutex);
    483       1.17       cgd 	st = p->p_sticks;
    484       1.17       cgd 	ut = p->p_uticks;
    485       1.17       cgd 	it = p->p_iticks;
    486      1.113        ad 	mutex_spin_exit(&p->p_stmutex);
    487       1.17       cgd 
    488      1.129      yamt 	tm = p->p_rtime;
    489      1.113        ad 
    490       1.70       dsl 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    491      1.113        ad 		lwp_lock(l);
    492      1.129      yamt 		bintime_add(&tm, &l->l_rtime);
    493  1.139.2.2      yamt 		if ((l->l_pflag & LP_RUNNING) != 0) {
    494      1.129      yamt 			struct bintime diff;
    495       1.68   thorpej 			/*
    496       1.68   thorpej 			 * Adjust for the current time slice.  This is
    497       1.68   thorpej 			 * actually fairly important since the error
    498       1.68   thorpej 			 * here is on the order of a time quantum,
    499       1.68   thorpej 			 * which is much greater than the sampling
    500       1.87     perry 			 * error.
    501       1.68   thorpej 			 */
    502      1.129      yamt 			binuptime(&diff);
    503      1.129      yamt 			bintime_sub(&diff, &l->l_stime);
    504      1.129      yamt 			bintime_add(&tm, &diff);
    505       1.68   thorpej 		}
    506      1.113        ad 		lwp_unlock(l);
    507       1.17       cgd 	}
    508       1.69       dsl 
    509       1.69       dsl 	tot = st + ut + it;
    510      1.129      yamt 	bintime2timeval(&tm, &tv);
    511      1.129      yamt 	u = (uint64_t)tv.tv_sec * 1000000ul + tv.tv_usec;
    512       1.70       dsl 
    513       1.69       dsl 	if (tot == 0) {
    514       1.69       dsl 		/* No ticks, so can't use to share time out, split 50-50 */
    515       1.70       dsl 		st = ut = u / 2;
    516       1.70       dsl 	} else {
    517       1.70       dsl 		st = (u * st) / tot;
    518       1.70       dsl 		ut = (u * ut) / tot;
    519       1.69       dsl 	}
    520      1.113        ad 	if (sp != NULL) {
    521      1.113        ad 		sp->tv_sec = st / 1000000;
    522      1.113        ad 		sp->tv_usec = st % 1000000;
    523      1.113        ad 	}
    524      1.113        ad 	if (up != NULL) {
    525      1.113        ad 		up->tv_sec = ut / 1000000;
    526      1.113        ad 		up->tv_usec = ut % 1000000;
    527      1.113        ad 	}
    528       1.17       cgd 	if (ip != NULL) {
    529       1.70       dsl 		if (it != 0)
    530       1.70       dsl 			it = (u * it) / tot;
    531       1.17       cgd 		ip->tv_sec = it / 1000000;
    532       1.17       cgd 		ip->tv_usec = it % 1000000;
    533       1.17       cgd 	}
    534      1.113        ad 	if (rp != NULL) {
    535      1.129      yamt 		*rp = tv;
    536      1.113        ad 	}
    537       1.17       cgd }
    538       1.17       cgd 
    539       1.17       cgd /* ARGSUSED */
    540       1.25       cgd int
    541  1.139.2.2      yamt sys___getrusage50(struct lwp *l, const struct sys___getrusage50_args *uap,
    542      1.134     rmind     register_t *retval)
    543       1.30   thorpej {
    544      1.128       dsl 	/* {
    545       1.22       cgd 		syscallarg(int) who;
    546       1.22       cgd 		syscallarg(struct rusage *) rusage;
    547      1.128       dsl 	} */
    548      1.119        ad 	struct rusage ru;
    549       1.68   thorpej 	struct proc *p = l->l_proc;
    550       1.17       cgd 
    551       1.22       cgd 	switch (SCARG(uap, who)) {
    552       1.19       cgd 	case RUSAGE_SELF:
    553      1.139        ad 		mutex_enter(p->p_lock);
    554      1.119        ad 		memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
    555      1.119        ad 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
    556      1.137        ad 		rulwps(p, &ru);
    557      1.139        ad 		mutex_exit(p->p_lock);
    558       1.17       cgd 		break;
    559       1.17       cgd 
    560       1.17       cgd 	case RUSAGE_CHILDREN:
    561      1.139        ad 		mutex_enter(p->p_lock);
    562      1.119        ad 		memcpy(&ru, &p->p_stats->p_cru, sizeof(ru));
    563      1.139        ad 		mutex_exit(p->p_lock);
    564       1.17       cgd 		break;
    565       1.17       cgd 
    566       1.17       cgd 	default:
    567      1.119        ad 		return EINVAL;
    568       1.17       cgd 	}
    569      1.119        ad 
    570      1.119        ad 	return copyout(&ru, SCARG(uap, rusage), sizeof(ru));
    571       1.17       cgd }
    572       1.17       cgd 
    573       1.25       cgd void
    574       1.98   thorpej ruadd(struct rusage *ru, struct rusage *ru2)
    575       1.17       cgd {
    576       1.54  augustss 	long *ip, *ip2;
    577       1.54  augustss 	int i;
    578       1.17       cgd 
    579       1.27   mycroft 	timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
    580       1.27   mycroft 	timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
    581       1.17       cgd 	if (ru->ru_maxrss < ru2->ru_maxrss)
    582       1.17       cgd 		ru->ru_maxrss = ru2->ru_maxrss;
    583       1.17       cgd 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
    584       1.17       cgd 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
    585       1.17       cgd 		*ip++ += *ip2++;
    586       1.17       cgd }
    587       1.17       cgd 
    588      1.137        ad void
    589      1.137        ad rulwps(proc_t *p, struct rusage *ru)
    590      1.137        ad {
    591      1.137        ad 	lwp_t *l;
    592      1.137        ad 
    593      1.139        ad 	KASSERT(mutex_owned(p->p_lock));
    594      1.137        ad 
    595      1.137        ad 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    596      1.137        ad 		ruadd(ru, &l->l_ru);
    597      1.137        ad 		ru->ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
    598      1.137        ad 		ru->ru_nivcsw += l->l_nivcsw;
    599      1.137        ad 	}
    600      1.137        ad }
    601      1.137        ad 
    602       1.17       cgd /*
    603       1.17       cgd  * Make a copy of the plimit structure.
    604       1.17       cgd  * We share these structures copy-on-write after fork,
    605       1.17       cgd  * and copy when a limit is changed.
    606      1.113        ad  *
    607      1.122       dsl  * Unfortunately (due to PL_SHAREMOD) it is possibly for the structure
    608      1.122       dsl  * we are copying to change beneath our feet!
    609       1.17       cgd  */
    610       1.17       cgd struct plimit *
    611      1.122       dsl lim_copy(struct plimit *lim)
    612       1.17       cgd {
    613      1.122       dsl 	struct plimit *newlim;
    614      1.113        ad 	char *corename;
    615      1.122       dsl 	size_t alen, len;
    616       1.17       cgd 
    617      1.130        ad 	newlim = pool_cache_get(plimit_cache, PR_WAITOK);
    618      1.121       dsl 	mutex_init(&newlim->pl_lock, MUTEX_DEFAULT, IPL_NONE);
    619      1.121       dsl 	newlim->pl_flags = 0;
    620      1.121       dsl 	newlim->pl_refcnt = 1;
    621      1.122       dsl 	newlim->pl_sv_limit = NULL;
    622      1.122       dsl 
    623      1.122       dsl 	mutex_enter(&lim->pl_lock);
    624      1.122       dsl 	memcpy(newlim->pl_rlimit, lim->pl_rlimit,
    625      1.122       dsl 	    sizeof(struct rlimit) * RLIM_NLIMITS);
    626       1.83        pk 
    627      1.122       dsl 	alen = 0;
    628      1.122       dsl 	corename = NULL;
    629      1.113        ad 	for (;;) {
    630      1.122       dsl 		if (lim->pl_corename == defcorename) {
    631      1.122       dsl 			newlim->pl_corename = defcorename;
    632      1.122       dsl 			break;
    633      1.122       dsl 		}
    634      1.122       dsl 		len = strlen(lim->pl_corename) + 1;
    635      1.122       dsl 		if (len <= alen) {
    636      1.122       dsl 			newlim->pl_corename = corename;
    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.122       dsl 		if (corename != NULL)
    643      1.122       dsl 			free(corename, M_TEMP);
    644      1.122       dsl 		alen = len;
    645      1.122       dsl 		corename = malloc(alen, M_TEMP, M_WAITOK);
    646      1.121       dsl 		mutex_enter(&lim->pl_lock);
    647      1.122       dsl 	}
    648      1.122       dsl 	mutex_exit(&lim->pl_lock);
    649      1.122       dsl 	if (corename != NULL)
    650      1.122       dsl 		free(corename, M_TEMP);
    651      1.122       dsl 	return newlim;
    652      1.122       dsl }
    653      1.122       dsl 
    654      1.122       dsl void
    655      1.122       dsl lim_addref(struct plimit *lim)
    656      1.122       dsl {
    657      1.125        ad 	atomic_inc_uint(&lim->pl_refcnt);
    658      1.122       dsl }
    659      1.113        ad 
    660      1.122       dsl /*
    661      1.122       dsl  * Give a process it's own private plimit structure.
    662      1.122       dsl  * This will only be shared (in fork) if modifications are to be shared.
    663      1.122       dsl  */
    664      1.122       dsl void
    665      1.122       dsl lim_privatise(struct proc *p, bool set_shared)
    666      1.122       dsl {
    667      1.122       dsl 	struct plimit *lim, *newlim;
    668      1.122       dsl 
    669      1.122       dsl 	lim = p->p_limit;
    670      1.122       dsl 	if (lim->pl_flags & PL_WRITEABLE) {
    671      1.122       dsl 		if (set_shared)
    672      1.122       dsl 			lim->pl_flags |= PL_SHAREMOD;
    673      1.122       dsl 		return;
    674      1.122       dsl 	}
    675      1.122       dsl 
    676      1.122       dsl 	if (set_shared && lim->pl_flags & PL_SHAREMOD)
    677      1.122       dsl 		return;
    678      1.122       dsl 
    679      1.122       dsl 	newlim = lim_copy(lim);
    680      1.113        ad 
    681      1.139        ad 	mutex_enter(p->p_lock);
    682      1.122       dsl 	if (p->p_limit->pl_flags & PL_WRITEABLE) {
    683      1.122       dsl 		/* Someone crept in while we were busy */
    684      1.139        ad 		mutex_exit(p->p_lock);
    685      1.122       dsl 		limfree(newlim);
    686      1.122       dsl 		if (set_shared)
    687      1.122       dsl 			p->p_limit->pl_flags |= PL_SHAREMOD;
    688      1.122       dsl 		return;
    689      1.113        ad 	}
    690       1.83        pk 
    691      1.122       dsl 	/*
    692      1.122       dsl 	 * Since most accesses to p->p_limit aren't locked, we must not
    693      1.122       dsl 	 * delete the old limit structure yet.
    694      1.122       dsl 	 */
    695      1.122       dsl 	newlim->pl_sv_limit = p->p_limit;
    696      1.122       dsl 	newlim->pl_flags |= PL_WRITEABLE;
    697      1.122       dsl 	if (set_shared)
    698      1.122       dsl 		newlim->pl_flags |= PL_SHAREMOD;
    699      1.122       dsl 	p->p_limit = newlim;
    700      1.139        ad 	mutex_exit(p->p_lock);
    701       1.32   mycroft }
    702       1.32   mycroft 
    703       1.32   mycroft void
    704       1.98   thorpej limfree(struct plimit *lim)
    705       1.32   mycroft {
    706      1.122       dsl 	struct plimit *sv_lim;
    707       1.85    kleink 
    708      1.122       dsl 	do {
    709      1.125        ad 		if (atomic_dec_uint_nv(&lim->pl_refcnt) > 0)
    710      1.122       dsl 			return;
    711      1.122       dsl 		if (lim->pl_corename != defcorename)
    712      1.122       dsl 			free(lim->pl_corename, M_TEMP);
    713      1.122       dsl 		sv_lim = lim->pl_sv_limit;
    714      1.122       dsl 		mutex_destroy(&lim->pl_lock);
    715      1.130        ad 		pool_cache_put(plimit_cache, lim);
    716      1.122       dsl 	} while ((lim = sv_lim) != NULL);
    717       1.68   thorpej }
    718       1.68   thorpej 
    719       1.68   thorpej struct pstats *
    720       1.98   thorpej pstatscopy(struct pstats *ps)
    721       1.68   thorpej {
    722       1.87     perry 
    723       1.68   thorpej 	struct pstats *newps;
    724       1.68   thorpej 
    725      1.130        ad 	newps = pool_cache_get(pstats_cache, PR_WAITOK);
    726       1.68   thorpej 
    727       1.68   thorpej 	memset(&newps->pstat_startzero, 0,
    728      1.115  christos 	(unsigned) ((char *)&newps->pstat_endzero -
    729      1.115  christos 		    (char *)&newps->pstat_startzero));
    730       1.68   thorpej 	memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
    731      1.115  christos 	((char *)&newps->pstat_endcopy -
    732      1.115  christos 	 (char *)&newps->pstat_startcopy));
    733       1.68   thorpej 
    734       1.68   thorpej 	return (newps);
    735       1.68   thorpej 
    736       1.68   thorpej }
    737       1.68   thorpej 
    738       1.68   thorpej void
    739       1.98   thorpej pstatsfree(struct pstats *ps)
    740       1.68   thorpej {
    741       1.68   thorpej 
    742      1.130        ad 	pool_cache_put(pstats_cache, ps);
    743       1.74    atatat }
    744       1.74    atatat 
    745       1.74    atatat /*
    746       1.74    atatat  * sysctl interface in five parts
    747       1.74    atatat  */
    748       1.74    atatat 
    749       1.74    atatat /*
    750       1.74    atatat  * a routine for sysctl proc subtree helpers that need to pick a valid
    751       1.74    atatat  * process by pid.
    752       1.74    atatat  */
    753       1.74    atatat static int
    754      1.102        ad sysctl_proc_findproc(struct lwp *l, struct proc **p2, pid_t pid)
    755       1.74    atatat {
    756       1.74    atatat 	struct proc *ptmp;
    757      1.101      elad 	int error = 0;
    758       1.74    atatat 
    759       1.74    atatat 	if (pid == PROC_CURPROC)
    760      1.102        ad 		ptmp = l->l_proc;
    761       1.74    atatat 	else if ((ptmp = pfind(pid)) == NULL)
    762       1.74    atatat 		error = ESRCH;
    763       1.74    atatat 
    764       1.74    atatat 	*p2 = ptmp;
    765       1.74    atatat 	return (error);
    766       1.74    atatat }
    767       1.74    atatat 
    768       1.74    atatat /*
    769       1.74    atatat  * sysctl helper routine for setting a process's specific corefile
    770       1.74    atatat  * name.  picks the process based on the given pid and checks the
    771       1.74    atatat  * correctness of the new value.
    772       1.74    atatat  */
    773       1.74    atatat static int
    774       1.74    atatat sysctl_proc_corename(SYSCTLFN_ARGS)
    775       1.74    atatat {
    776      1.102        ad 	struct proc *ptmp;
    777       1.83        pk 	struct plimit *lim;
    778       1.74    atatat 	int error = 0, len;
    779      1.100      yamt 	char *cname;
    780      1.122       dsl 	char *ocore;
    781      1.100      yamt 	char *tmp;
    782       1.74    atatat 	struct sysctlnode node;
    783       1.74    atatat 
    784       1.74    atatat 	/*
    785       1.74    atatat 	 * is this all correct?
    786       1.74    atatat 	 */
    787       1.74    atatat 	if (namelen != 0)
    788       1.74    atatat 		return (EINVAL);
    789       1.74    atatat 	if (name[-1] != PROC_PID_CORENAME)
    790       1.74    atatat 		return (EINVAL);
    791       1.74    atatat 
    792       1.74    atatat 	/*
    793       1.74    atatat 	 * whom are we tweaking?
    794       1.74    atatat 	 */
    795      1.102        ad 	error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-2]);
    796       1.74    atatat 	if (error)
    797       1.74    atatat 		return (error);
    798       1.74    atatat 
    799      1.131      elad 	/* XXX-elad */
    800      1.131      elad 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
    801      1.131      elad 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    802      1.111      elad 	if (error)
    803      1.111      elad 		return (error);
    804      1.111      elad 
    805      1.131      elad 	if (newp == NULL) {
    806      1.131      elad 		error = kauth_authorize_process(l->l_cred,
    807      1.131      elad 		    KAUTH_PROCESS_CORENAME, ptmp,
    808      1.131      elad 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_GET), NULL, NULL);
    809      1.131      elad 		if (error)
    810      1.131      elad 			return (error);
    811      1.131      elad 	}
    812      1.131      elad 
    813       1.74    atatat 	/*
    814       1.74    atatat 	 * let them modify a temporary copy of the core name
    815       1.74    atatat 	 */
    816      1.122       dsl 	cname = PNBUF_GET();
    817      1.122       dsl 	lim = ptmp->p_limit;
    818      1.122       dsl 	mutex_enter(&lim->pl_lock);
    819      1.122       dsl 	strlcpy(cname, lim->pl_corename, MAXPATHLEN);
    820      1.122       dsl 	mutex_exit(&lim->pl_lock);
    821      1.122       dsl 
    822       1.74    atatat 	node = *rnode;
    823       1.74    atatat 	node.sysctl_data = cname;
    824       1.74    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    825       1.74    atatat 
    826       1.74    atatat 	/*
    827       1.74    atatat 	 * if that failed, or they have nothing new to say, or we've
    828       1.74    atatat 	 * heard it before...
    829       1.74    atatat 	 */
    830      1.122       dsl 	if (error || newp == NULL)
    831      1.122       dsl 		goto done;
    832      1.122       dsl 	lim = ptmp->p_limit;
    833      1.122       dsl 	mutex_enter(&lim->pl_lock);
    834      1.122       dsl 	error = strcmp(cname, lim->pl_corename);
    835      1.122       dsl 	mutex_exit(&lim->pl_lock);
    836      1.122       dsl 	if (error == 0)
    837      1.122       dsl 		/* Unchanged */
    838      1.100      yamt 		goto done;
    839       1.74    atatat 
    840      1.111      elad 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CORENAME,
    841      1.131      elad 	    ptmp, KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_SET), cname, NULL);
    842      1.111      elad 	if (error)
    843      1.111      elad 		return (error);
    844      1.103      elad 
    845       1.74    atatat 	/*
    846       1.74    atatat 	 * no error yet and cname now has the new core name in it.
    847       1.74    atatat 	 * let's see if it looks acceptable.  it must be either "core"
    848       1.74    atatat 	 * or end in ".core" or "/core".
    849       1.74    atatat 	 */
    850       1.74    atatat 	len = strlen(cname);
    851      1.100      yamt 	if (len < 4) {
    852      1.100      yamt 		error = EINVAL;
    853      1.100      yamt 	} else if (strcmp(cname + len - 4, "core") != 0) {
    854      1.100      yamt 		error = EINVAL;
    855      1.100      yamt 	} else if (len > 4 && cname[len - 5] != '/' && cname[len - 5] != '.') {
    856      1.100      yamt 		error = EINVAL;
    857      1.100      yamt 	}
    858      1.100      yamt 	if (error != 0) {
    859      1.100      yamt 		goto done;
    860      1.100      yamt 	}
    861       1.74    atatat 
    862       1.74    atatat 	/*
    863       1.74    atatat 	 * hmm...looks good.  now...where do we put it?
    864       1.74    atatat 	 */
    865       1.74    atatat 	tmp = malloc(len + 1, M_TEMP, M_WAITOK|M_CANFAIL);
    866      1.100      yamt 	if (tmp == NULL) {
    867      1.100      yamt 		error = ENOMEM;
    868      1.100      yamt 		goto done;
    869      1.100      yamt 	}
    870      1.122       dsl 	memcpy(tmp, cname, len + 1);
    871       1.74    atatat 
    872      1.122       dsl 	lim_privatise(ptmp, false);
    873       1.83        pk 	lim = ptmp->p_limit;
    874      1.122       dsl 	mutex_enter(&lim->pl_lock);
    875      1.122       dsl 	ocore = lim->pl_corename;
    876       1.83        pk 	lim->pl_corename = tmp;
    877      1.122       dsl 	mutex_exit(&lim->pl_lock);
    878      1.122       dsl 	if (ocore != defcorename)
    879      1.122       dsl 		free(ocore, M_TEMP);
    880      1.122       dsl 
    881      1.100      yamt done:
    882      1.100      yamt 	PNBUF_PUT(cname);
    883      1.100      yamt 	return error;
    884       1.74    atatat }
    885       1.74    atatat 
    886       1.74    atatat /*
    887       1.74    atatat  * sysctl helper routine for checking/setting a process's stop flags,
    888       1.74    atatat  * one for fork and one for exec.
    889       1.74    atatat  */
    890       1.74    atatat static int
    891       1.74    atatat sysctl_proc_stop(SYSCTLFN_ARGS)
    892       1.74    atatat {
    893      1.102        ad 	struct proc *ptmp;
    894       1.74    atatat 	int i, f, error = 0;
    895       1.74    atatat 	struct sysctlnode node;
    896       1.74    atatat 
    897       1.74    atatat 	if (namelen != 0)
    898       1.74    atatat 		return (EINVAL);
    899       1.74    atatat 
    900      1.102        ad 	error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-2]);
    901       1.74    atatat 	if (error)
    902       1.74    atatat 		return (error);
    903       1.74    atatat 
    904      1.131      elad 	/* XXX-elad */
    905      1.131      elad 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
    906      1.131      elad 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    907      1.111      elad 	if (error)
    908      1.111      elad 		return (error);
    909      1.111      elad 
    910       1.74    atatat 	switch (rnode->sysctl_num) {
    911       1.74    atatat 	case PROC_PID_STOPFORK:
    912      1.113        ad 		f = PS_STOPFORK;
    913       1.74    atatat 		break;
    914       1.74    atatat 	case PROC_PID_STOPEXEC:
    915      1.113        ad 		f = PS_STOPEXEC;
    916       1.74    atatat 		break;
    917       1.74    atatat 	case PROC_PID_STOPEXIT:
    918      1.113        ad 		f = PS_STOPEXIT;
    919       1.74    atatat 		break;
    920       1.74    atatat 	default:
    921       1.74    atatat 		return (EINVAL);
    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.74    atatat 		return (error);
    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.139.2.2      yamt 	if (!error) {
    935  1.139.2.2      yamt 		if (i) {
    936  1.139.2.2      yamt 			ptmp->p_sflag |= f;
    937  1.139.2.2      yamt 		} else {
    938  1.139.2.2      yamt 			ptmp->p_sflag &= ~f;
    939  1.139.2.2      yamt 		}
    940  1.139.2.2      yamt 	}
    941      1.139        ad 	mutex_exit(ptmp->p_lock);
    942       1.74    atatat 
    943  1.139.2.2      yamt 	return error;
    944       1.74    atatat }
    945       1.74    atatat 
    946       1.74    atatat /*
    947       1.74    atatat  * sysctl helper routine for a process's rlimits as exposed by sysctl.
    948       1.74    atatat  */
    949       1.74    atatat static int
    950       1.74    atatat sysctl_proc_plimit(SYSCTLFN_ARGS)
    951       1.74    atatat {
    952      1.102        ad 	struct proc *ptmp;
    953       1.74    atatat 	u_int limitno;
    954       1.74    atatat 	int which, error = 0;
    955       1.74    atatat         struct rlimit alim;
    956       1.74    atatat 	struct sysctlnode node;
    957       1.74    atatat 
    958       1.74    atatat 	if (namelen != 0)
    959       1.74    atatat 		return (EINVAL);
    960       1.74    atatat 
    961       1.74    atatat 	which = name[-1];
    962       1.74    atatat 	if (which != PROC_PID_LIMIT_TYPE_SOFT &&
    963       1.74    atatat 	    which != PROC_PID_LIMIT_TYPE_HARD)
    964       1.74    atatat 		return (EINVAL);
    965       1.74    atatat 
    966       1.74    atatat 	limitno = name[-2] - 1;
    967       1.74    atatat 	if (limitno >= RLIM_NLIMITS)
    968       1.74    atatat 		return (EINVAL);
    969       1.74    atatat 
    970       1.74    atatat 	if (name[-3] != PROC_PID_LIMIT)
    971       1.74    atatat 		return (EINVAL);
    972       1.74    atatat 
    973      1.102        ad 	error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-4]);
    974       1.74    atatat 	if (error)
    975       1.74    atatat 		return (error);
    976       1.74    atatat 
    977      1.131      elad 	/* XXX-elad */
    978      1.131      elad 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
    979      1.131      elad 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    980      1.111      elad 	if (error)
    981      1.111      elad 		return (error);
    982      1.111      elad 
    983      1.131      elad 	/* Check if we can view limits. */
    984      1.131      elad 	if (newp == NULL) {
    985      1.131      elad 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
    986      1.131      elad 		    ptmp, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_GET), &alim,
    987      1.131      elad 		    KAUTH_ARG(which));
    988      1.131      elad 		if (error)
    989      1.131      elad 			return (error);
    990      1.131      elad 	}
    991      1.131      elad 
    992       1.74    atatat 	node = *rnode;
    993       1.74    atatat 	memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
    994       1.74    atatat 	if (which == PROC_PID_LIMIT_TYPE_HARD)
    995       1.74    atatat 		node.sysctl_data = &alim.rlim_max;
    996       1.74    atatat 	else
    997       1.74    atatat 		node.sysctl_data = &alim.rlim_cur;
    998       1.74    atatat 
    999       1.74    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1000       1.74    atatat 	if (error || newp == NULL)
   1001       1.74    atatat 		return (error);
   1002       1.74    atatat 
   1003      1.102        ad 	return (dosetrlimit(l, ptmp, limitno, &alim));
   1004       1.74    atatat }
   1005       1.74    atatat 
   1006       1.74    atatat /*
   1007       1.74    atatat  * and finally, the actually glue that sticks it to the tree
   1008       1.74    atatat  */
   1009       1.74    atatat SYSCTL_SETUP(sysctl_proc_setup, "sysctl proc subtree setup")
   1010       1.74    atatat {
   1011       1.74    atatat 
   1012       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1013       1.76    atatat 		       CTLFLAG_PERMANENT,
   1014       1.74    atatat 		       CTLTYPE_NODE, "proc", NULL,
   1015       1.74    atatat 		       NULL, 0, NULL, 0,
   1016       1.74    atatat 		       CTL_PROC, CTL_EOL);
   1017       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1018       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
   1019       1.78    atatat 		       CTLTYPE_NODE, "curproc",
   1020       1.78    atatat 		       SYSCTL_DESCR("Per-process settings"),
   1021       1.74    atatat 		       NULL, 0, NULL, 0,
   1022       1.74    atatat 		       CTL_PROC, PROC_CURPROC, CTL_EOL);
   1023       1.74    atatat 
   1024       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1025      1.103      elad 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
   1026       1.78    atatat 		       CTLTYPE_STRING, "corename",
   1027       1.78    atatat 		       SYSCTL_DESCR("Core file name"),
   1028       1.74    atatat 		       sysctl_proc_corename, 0, NULL, MAXPATHLEN,
   1029       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
   1030       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1031       1.76    atatat 		       CTLFLAG_PERMANENT,
   1032       1.78    atatat 		       CTLTYPE_NODE, "rlimit",
   1033       1.78    atatat 		       SYSCTL_DESCR("Process limits"),
   1034       1.74    atatat 		       NULL, 0, NULL, 0,
   1035       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
   1036       1.74    atatat 
   1037       1.74    atatat #define create_proc_plimit(s, n) do {					\
   1038       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,				\
   1039       1.76    atatat 		       CTLFLAG_PERMANENT,				\
   1040       1.78    atatat 		       CTLTYPE_NODE, s,					\
   1041       1.78    atatat 		       SYSCTL_DESCR("Process " s " limits"),		\
   1042       1.74    atatat 		       NULL, 0, NULL, 0,				\
   1043       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
   1044       1.74    atatat 		       CTL_EOL);					\
   1045       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,				\
   1046       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
   1047       1.78    atatat 		       CTLTYPE_QUAD, "soft",				\
   1048       1.78    atatat 		       SYSCTL_DESCR("Process soft " s " limit"),	\
   1049       1.74    atatat 		       sysctl_proc_plimit, 0, NULL, 0,			\
   1050       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
   1051       1.74    atatat 		       PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL);		\
   1052       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,				\
   1053       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
   1054       1.78    atatat 		       CTLTYPE_QUAD, "hard",				\
   1055       1.78    atatat 		       SYSCTL_DESCR("Process hard " s " limit"),	\
   1056       1.74    atatat 		       sysctl_proc_plimit, 0, NULL, 0,			\
   1057       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
   1058       1.74    atatat 		       PROC_PID_LIMIT_TYPE_HARD, CTL_EOL);		\
   1059       1.74    atatat 	} while (0/*CONSTCOND*/)
   1060       1.74    atatat 
   1061       1.74    atatat 	create_proc_plimit("cputime",		PROC_PID_LIMIT_CPU);
   1062       1.74    atatat 	create_proc_plimit("filesize",		PROC_PID_LIMIT_FSIZE);
   1063       1.74    atatat 	create_proc_plimit("datasize",		PROC_PID_LIMIT_DATA);
   1064       1.74    atatat 	create_proc_plimit("stacksize",		PROC_PID_LIMIT_STACK);
   1065       1.74    atatat 	create_proc_plimit("coredumpsize",	PROC_PID_LIMIT_CORE);
   1066       1.74    atatat 	create_proc_plimit("memoryuse",		PROC_PID_LIMIT_RSS);
   1067       1.74    atatat 	create_proc_plimit("memorylocked",	PROC_PID_LIMIT_MEMLOCK);
   1068       1.74    atatat 	create_proc_plimit("maxproc",		PROC_PID_LIMIT_NPROC);
   1069       1.74    atatat 	create_proc_plimit("descriptors",	PROC_PID_LIMIT_NOFILE);
   1070       1.79  christos 	create_proc_plimit("sbsize",		PROC_PID_LIMIT_SBSIZE);
   1071  1.139.2.2      yamt 	create_proc_plimit("vmemoryuse",	PROC_PID_LIMIT_AS);
   1072       1.74    atatat 
   1073       1.74    atatat #undef create_proc_plimit
   1074       1.74    atatat 
   1075       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1076       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
   1077       1.78    atatat 		       CTLTYPE_INT, "stopfork",
   1078       1.78    atatat 		       SYSCTL_DESCR("Stop process at fork(2)"),
   1079       1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
   1080       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
   1081       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1082       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
   1083       1.78    atatat 		       CTLTYPE_INT, "stopexec",
   1084       1.78    atatat 		       SYSCTL_DESCR("Stop process at execve(2)"),
   1085       1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
   1086       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
   1087       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1088       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
   1089       1.78    atatat 		       CTLTYPE_INT, "stopexit",
   1090       1.78    atatat 		       SYSCTL_DESCR("Stop process before completing exit"),
   1091       1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
   1092       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
   1093       1.17       cgd }
   1094