xref: /titanic_51/usr/src/uts/common/syscall/rctlsys.c (revision a0f9c00cd82d49a710cee8aeb83ce42b5fb293a9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5532877c4Srd117015  * Common Development and Distribution License (the "License").
6532877c4Srd117015  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2255c01d4fSMenno Lageman  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
297c478bd9Sstevel@tonic-gate #include <sys/cred.h>
307c478bd9Sstevel@tonic-gate #include <sys/errno.h>
317c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
327c478bd9Sstevel@tonic-gate #include <sys/rctl_impl.h>
337c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
347c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/policy.h>
387c478bd9Sstevel@tonic-gate #include <sys/proc.h>
397c478bd9Sstevel@tonic-gate #include <sys/task.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * setrctl(2), getrctl(2), and private rctlsys(2*) system calls
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * Resource control block (rctlblk_ptr_t, rctl_opaque_t)
457c478bd9Sstevel@tonic-gate  *   The resource control system call interfaces present the resource control
467c478bd9Sstevel@tonic-gate  *   values and flags via the resource control block abstraction, made manifest
477c478bd9Sstevel@tonic-gate  *   via an opaque data type with strict type definitions.  Keeping the formal
487c478bd9Sstevel@tonic-gate  *   definitions in the rcontrol block allows us to be clever in the kernel,
497c478bd9Sstevel@tonic-gate  *   combining attributes where appropriate in the current implementation while
507c478bd9Sstevel@tonic-gate  *   preserving binary compatibility in the face of implementation changes.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	RBX_TO_BLK	0x1
547c478bd9Sstevel@tonic-gate #define	RBX_FROM_BLK	0x2
557c478bd9Sstevel@tonic-gate #define	RBX_VAL		0x4
567c478bd9Sstevel@tonic-gate #define	RBX_CTL		0x8
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static void
597c478bd9Sstevel@tonic-gate rctlsys_rblk_xfrm(rctl_opaque_t *blk, rctl_dict_entry_t *rde,
607c478bd9Sstevel@tonic-gate     rctl_val_t *val, int flags)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	if (flags & RBX_FROM_BLK) {
637c478bd9Sstevel@tonic-gate 		if (flags & RBX_VAL) {
647c478bd9Sstevel@tonic-gate 			/*
657c478bd9Sstevel@tonic-gate 			 * Firing time cannot be set.
667c478bd9Sstevel@tonic-gate 			 */
677c478bd9Sstevel@tonic-gate 			val->rcv_privilege = blk->rcq_privilege;
687c478bd9Sstevel@tonic-gate 			val->rcv_value = blk->rcq_value;
697c478bd9Sstevel@tonic-gate 			val->rcv_flagaction = blk->rcq_local_flagaction;
707c478bd9Sstevel@tonic-gate 			val->rcv_action_signal = blk->rcq_local_signal;
717c478bd9Sstevel@tonic-gate 			val->rcv_action_recip_pid =
727c478bd9Sstevel@tonic-gate 			    blk->rcq_local_recipient_pid;
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 		if (flags & RBX_CTL) {
757c478bd9Sstevel@tonic-gate 			rde->rcd_flagaction = blk->rcq_global_flagaction;
767c478bd9Sstevel@tonic-gate 			rde->rcd_syslog_level = blk->rcq_global_syslog_level;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 			/*
797c478bd9Sstevel@tonic-gate 			 * Because the strlog() interface supports fewer options
807c478bd9Sstevel@tonic-gate 			 * than are made available via the syslog() interface to
817c478bd9Sstevel@tonic-gate 			 * userland, we map the syslog level down to a smaller
827c478bd9Sstevel@tonic-gate 			 * set of distinct logging behaviours.
837c478bd9Sstevel@tonic-gate 			 */
847c478bd9Sstevel@tonic-gate 			rde->rcd_strlog_flags = 0;
857c478bd9Sstevel@tonic-gate 			switch (blk->rcq_global_syslog_level) {
867c478bd9Sstevel@tonic-gate 				case LOG_EMERG:
877c478bd9Sstevel@tonic-gate 				case LOG_ALERT:
887c478bd9Sstevel@tonic-gate 				case LOG_CRIT:
897c478bd9Sstevel@tonic-gate 					rde->rcd_strlog_flags |= SL_CONSOLE;
907c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
917c478bd9Sstevel@tonic-gate 				case LOG_ERR:
927c478bd9Sstevel@tonic-gate 					rde->rcd_strlog_flags |= SL_ERROR;
937c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
947c478bd9Sstevel@tonic-gate 				case LOG_WARNING:
957c478bd9Sstevel@tonic-gate 					rde->rcd_strlog_flags |= SL_WARN;
967c478bd9Sstevel@tonic-gate 					break;
977c478bd9Sstevel@tonic-gate 				case LOG_NOTICE:
987c478bd9Sstevel@tonic-gate 					rde->rcd_strlog_flags |= SL_CONSOLE;
997c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
1007c478bd9Sstevel@tonic-gate 				case LOG_INFO:	/* informational */
1017c478bd9Sstevel@tonic-gate 				case LOG_DEBUG:	/* debug-level messages */
1027c478bd9Sstevel@tonic-gate 				default:
1037c478bd9Sstevel@tonic-gate 					rde->rcd_strlog_flags |= SL_NOTE;
1047c478bd9Sstevel@tonic-gate 					break;
1057c478bd9Sstevel@tonic-gate 			}
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 	} else {
1087c478bd9Sstevel@tonic-gate 		bzero(blk,  sizeof (rctl_opaque_t));
1097c478bd9Sstevel@tonic-gate 		if (flags & RBX_VAL) {
1107c478bd9Sstevel@tonic-gate 			blk->rcq_privilege = val->rcv_privilege;
1117c478bd9Sstevel@tonic-gate 			blk->rcq_value = val->rcv_value;
1127c478bd9Sstevel@tonic-gate 			blk->rcq_enforced_value = rctl_model_value(rde,
1137c478bd9Sstevel@tonic-gate 			    curproc, val->rcv_value);
1147c478bd9Sstevel@tonic-gate 			blk->rcq_local_flagaction = val->rcv_flagaction;
1157c478bd9Sstevel@tonic-gate 			blk->rcq_local_signal = val->rcv_action_signal;
1167c478bd9Sstevel@tonic-gate 			blk->rcq_firing_time = val->rcv_firing_time;
1177c478bd9Sstevel@tonic-gate 			blk->rcq_local_recipient_pid =
1187c478bd9Sstevel@tonic-gate 			    val->rcv_action_recip_pid;
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 		if (flags & RBX_CTL) {
1217c478bd9Sstevel@tonic-gate 			blk->rcq_global_flagaction = rde->rcd_flagaction;
1227c478bd9Sstevel@tonic-gate 			blk->rcq_global_syslog_level = rde->rcd_syslog_level;
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * int rctl_invalid_value(rctl_dict_entry_t *, rctl_val_t *)
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  * Overview
1317c478bd9Sstevel@tonic-gate  *   Perform basic validation of proposed new resource control value against the
1327c478bd9Sstevel@tonic-gate  *   global properties set on the control.  Any system call operation presented
1337c478bd9Sstevel@tonic-gate  *   with an invalid resource control value should return -1 and set errno to
1347c478bd9Sstevel@tonic-gate  *   EINVAL.
1357c478bd9Sstevel@tonic-gate  *
1367c478bd9Sstevel@tonic-gate  * Return values
1377c478bd9Sstevel@tonic-gate  *   0 if valid, 1 if invalid.
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  * Caller's context
1407c478bd9Sstevel@tonic-gate  *   No restriction on context.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate int
1437c478bd9Sstevel@tonic-gate rctl_invalid_value(rctl_dict_entry_t *rde, rctl_val_t *rval)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	rctl_val_t *sys_rval;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (rval->rcv_privilege != RCPRIV_BASIC &&
1487c478bd9Sstevel@tonic-gate 	    rval->rcv_privilege != RCPRIV_PRIVILEGED &&
1497c478bd9Sstevel@tonic-gate 	    rval->rcv_privilege != RCPRIV_SYSTEM)
1507c478bd9Sstevel@tonic-gate 		return (1);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (rval->rcv_flagaction & ~RCTL_LOCAL_MASK)
1537c478bd9Sstevel@tonic-gate 		return (1);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (rval->rcv_privilege == RCPRIV_BASIC &&
1567c478bd9Sstevel@tonic-gate 	    (rde->rcd_flagaction & RCTL_GLOBAL_NOBASIC) != 0)
1577c478bd9Sstevel@tonic-gate 		return (1);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if ((rval->rcv_flagaction & RCTL_LOCAL_DENY) == 0 &&
1607c478bd9Sstevel@tonic-gate 	    (rde->rcd_flagaction & RCTL_GLOBAL_DENY_ALWAYS) != 0)
1617c478bd9Sstevel@tonic-gate 		return (1);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if ((rval->rcv_flagaction & RCTL_LOCAL_DENY) &&
1647c478bd9Sstevel@tonic-gate 	    (rde->rcd_flagaction & RCTL_GLOBAL_DENY_NEVER))
1657c478bd9Sstevel@tonic-gate 		return (1);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if ((rval->rcv_flagaction & RCTL_LOCAL_SIGNAL) &&
1687c478bd9Sstevel@tonic-gate 	    (rde->rcd_flagaction & RCTL_GLOBAL_SIGNAL_NEVER))
1697c478bd9Sstevel@tonic-gate 		return (1);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if ((rval->rcv_flagaction & RCTL_LOCAL_SIGNAL) &&
1727c478bd9Sstevel@tonic-gate 	    rval->rcv_action_signal == 0)
1737c478bd9Sstevel@tonic-gate 		return (1);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (rval->rcv_action_signal == SIGXCPU &&
1767c478bd9Sstevel@tonic-gate 	    (rde->rcd_flagaction & RCTL_GLOBAL_CPU_TIME) == 0)
1777c478bd9Sstevel@tonic-gate 		return (1);
1787c478bd9Sstevel@tonic-gate 	else if (rval->rcv_action_signal == SIGXFSZ &&
1797c478bd9Sstevel@tonic-gate 	    (rde->rcd_flagaction & RCTL_GLOBAL_FILE_SIZE) == 0)
1807c478bd9Sstevel@tonic-gate 		return (1);
1817c478bd9Sstevel@tonic-gate 	else if (rval->rcv_action_signal != SIGHUP &&
1827c478bd9Sstevel@tonic-gate 	    rval->rcv_action_signal != SIGABRT &&
1837c478bd9Sstevel@tonic-gate 	    rval->rcv_action_signal != SIGKILL &&
1847c478bd9Sstevel@tonic-gate 	    rval->rcv_action_signal != SIGTERM &&
1857c478bd9Sstevel@tonic-gate 	    rval->rcv_action_signal != SIGSTOP &&
1867c478bd9Sstevel@tonic-gate 	    rval->rcv_action_signal != SIGXCPU &&
1877c478bd9Sstevel@tonic-gate 	    rval->rcv_action_signal != SIGXFSZ &&
1887c478bd9Sstevel@tonic-gate 	    rval->rcv_action_signal != SIGXRES &&
1897c478bd9Sstevel@tonic-gate 	    rval->rcv_action_signal != 0)	/* That is, no signal is ok. */
1907c478bd9Sstevel@tonic-gate 		return (1);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	sys_rval = rde->rcd_default_value;
1937c478bd9Sstevel@tonic-gate 	while (sys_rval->rcv_privilege != RCPRIV_SYSTEM)
1947c478bd9Sstevel@tonic-gate 		sys_rval = sys_rval->rcv_next;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (rval->rcv_value > sys_rval->rcv_value)
1977c478bd9Sstevel@tonic-gate 		return (1);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	return (0);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * static long rctlsys_get(char *name, rctl_opaque_t *old_rblk,
2047c478bd9Sstevel@tonic-gate  *   rctl_opaque_t *new_rblk, int flags)
2057c478bd9Sstevel@tonic-gate  *
2067c478bd9Sstevel@tonic-gate  * Overview
2077c478bd9Sstevel@tonic-gate  *   rctlsys_get() is the implementation of the core logic of getrctl(2), the
20855c01d4fSMenno Lageman  *   public system call for fetching resource control values.  Three mutually
20955c01d4fSMenno Lageman  *   exclusive flag values are supported: RCTL_USAGE, RCTL_FIRST and RCTL_NEXT.
21055c01d4fSMenno Lageman  *   When RCTL_USAGE is presented, the current usage for the resource control
21155c01d4fSMenno Lageman  *   is returned in new_blk if the resource control provides an implementation
21255c01d4fSMenno Lageman  *   of the usage operation.  When RCTL_FIRST is presented, the value of
21355c01d4fSMenno Lageman  *   old_rblk is ignored, and the first value in the resource control value
21455c01d4fSMenno Lageman  *   sequence for the named control is transformed and placed in the user
21555c01d4fSMenno Lageman  *   memory location at new_rblk.  In the RCTL_NEXT case, the value of old_rblk
21655c01d4fSMenno Lageman  *   is examined, and the next value in the sequence is transformed and placed
21755c01d4fSMenno Lageman  *   at new_rblk.
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate static long
2207c478bd9Sstevel@tonic-gate rctlsys_get(char *name, rctl_opaque_t *old_rblk, rctl_opaque_t *new_rblk,
2217c478bd9Sstevel@tonic-gate     int flags)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	rctl_val_t *nval;
2247c478bd9Sstevel@tonic-gate 	rctl_opaque_t *nblk;
2257c478bd9Sstevel@tonic-gate 	rctl_hndl_t hndl;
2267c478bd9Sstevel@tonic-gate 	char *kname;
2277c478bd9Sstevel@tonic-gate 	size_t klen;
2287c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *krde;
2297c478bd9Sstevel@tonic-gate 	int ret;
2307c478bd9Sstevel@tonic-gate 	int action = flags & (~RCTLSYS_ACTION_MASK);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (flags & (~RCTLSYS_MASK))
2337c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (action != RCTL_FIRST && action != RCTL_NEXT &&
2367c478bd9Sstevel@tonic-gate 	    action != RCTL_USAGE)
2377c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (new_rblk == NULL || name == NULL)
2407c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	kname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2437c478bd9Sstevel@tonic-gate 	krde = kmem_alloc(sizeof (rctl_dict_entry_t), KM_SLEEP);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (copyinstr(name, kname, MAXPATHLEN, &klen) != 0) {
2467c478bd9Sstevel@tonic-gate 		kmem_free(kname, MAXPATHLEN);
2477c478bd9Sstevel@tonic-gate 		kmem_free(krde, sizeof (rctl_dict_entry_t));
2487c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if ((hndl = rctl_hndl_lookup(kname)) == -1) {
2527c478bd9Sstevel@tonic-gate 		kmem_free(kname, MAXPATHLEN);
2537c478bd9Sstevel@tonic-gate 		kmem_free(krde, sizeof (rctl_dict_entry_t));
2547c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (rctl_global_get(kname, krde) == -1) {
2587c478bd9Sstevel@tonic-gate 		kmem_free(kname, MAXPATHLEN);
2597c478bd9Sstevel@tonic-gate 		kmem_free(krde, sizeof (rctl_dict_entry_t));
2607c478bd9Sstevel@tonic-gate 		return (set_errno(ESRCH));
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	kmem_free(kname, MAXPATHLEN);
2647c478bd9Sstevel@tonic-gate 
26555c01d4fSMenno Lageman 	if (action != RCTL_USAGE)
2667c478bd9Sstevel@tonic-gate 		nval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if (action == RCTL_USAGE) {
26955c01d4fSMenno Lageman 		rctl_set_t *rset;
27055c01d4fSMenno Lageman 		rctl_t *rctl;
27155c01d4fSMenno Lageman 		rctl_qty_t usage;
27255c01d4fSMenno Lageman 
27355c01d4fSMenno Lageman 		mutex_enter(&curproc->p_lock);
27455c01d4fSMenno Lageman 		if ((rset = rctl_entity_obtain_rset(krde, curproc)) == NULL) {
27555c01d4fSMenno Lageman 			mutex_exit(&curproc->p_lock);
27655c01d4fSMenno Lageman 			kmem_free(krde, sizeof (rctl_dict_entry_t));
27755c01d4fSMenno Lageman 			return (set_errno(ESRCH));
27855c01d4fSMenno Lageman 		}
27955c01d4fSMenno Lageman 		mutex_enter(&rset->rcs_lock);
28055c01d4fSMenno Lageman 		if (rctl_set_find(rset, hndl, &rctl) == -1) {
28155c01d4fSMenno Lageman 			mutex_exit(&rset->rcs_lock);
28255c01d4fSMenno Lageman 			mutex_exit(&curproc->p_lock);
28355c01d4fSMenno Lageman 			kmem_free(krde, sizeof (rctl_dict_entry_t));
28455c01d4fSMenno Lageman 			return (set_errno(ESRCH));
28555c01d4fSMenno Lageman 		}
28655c01d4fSMenno Lageman 		if (RCTLOP_NO_USAGE(rctl)) {
28755c01d4fSMenno Lageman 			mutex_exit(&rset->rcs_lock);
28855c01d4fSMenno Lageman 			mutex_exit(&curproc->p_lock);
2897c478bd9Sstevel@tonic-gate 			kmem_free(krde, sizeof (rctl_dict_entry_t));
2907c478bd9Sstevel@tonic-gate 			return (set_errno(ENOTSUP));
29155c01d4fSMenno Lageman 		}
29255c01d4fSMenno Lageman 		usage = RCTLOP_GET_USAGE(rctl, curproc);
29355c01d4fSMenno Lageman 		mutex_exit(&rset->rcs_lock);
29455c01d4fSMenno Lageman 		mutex_exit(&curproc->p_lock);
29555c01d4fSMenno Lageman 
296*a0f9c00cSJosef 'Jeff' Sipek 		nblk = kmem_zalloc(sizeof (rctl_opaque_t), KM_SLEEP);
29755c01d4fSMenno Lageman 		nblk->rcq_value = usage;
29855c01d4fSMenno Lageman 
29955c01d4fSMenno Lageman 		ret = copyout(nblk, new_rblk, sizeof (rctl_opaque_t));
30055c01d4fSMenno Lageman 		kmem_free(nblk, sizeof (rctl_opaque_t));
30155c01d4fSMenno Lageman 		kmem_free(krde, sizeof (rctl_dict_entry_t));
30255c01d4fSMenno Lageman 		return (ret == 0 ? 0 : set_errno(EFAULT));
3037c478bd9Sstevel@tonic-gate 	} else if (action == RCTL_FIRST) {
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		mutex_enter(&curproc->p_lock);
3067c478bd9Sstevel@tonic-gate 		if (ret = rctl_local_get(hndl, NULL, nval, curproc)) {
3077c478bd9Sstevel@tonic-gate 			mutex_exit(&curproc->p_lock);
3087c478bd9Sstevel@tonic-gate 			kmem_cache_free(rctl_val_cache, nval);
3097c478bd9Sstevel@tonic-gate 			kmem_free(krde, sizeof (rctl_dict_entry_t));
3107c478bd9Sstevel@tonic-gate 			return (set_errno(ret));
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 		mutex_exit(&curproc->p_lock);
3137c478bd9Sstevel@tonic-gate 	} else {
3147c478bd9Sstevel@tonic-gate 		/*
3157c478bd9Sstevel@tonic-gate 		 * RCTL_NEXT
3167c478bd9Sstevel@tonic-gate 		 */
3177c478bd9Sstevel@tonic-gate 		rctl_val_t *oval;
3187c478bd9Sstevel@tonic-gate 		rctl_opaque_t *oblk;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		oblk = kmem_alloc(sizeof (rctl_opaque_t), KM_SLEEP);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		if (copyin(old_rblk, oblk, sizeof (rctl_opaque_t)) == -1) {
3237c478bd9Sstevel@tonic-gate 			kmem_cache_free(rctl_val_cache, nval);
3247c478bd9Sstevel@tonic-gate 			kmem_free(oblk, sizeof (rctl_opaque_t));
3257c478bd9Sstevel@tonic-gate 			kmem_free(krde, sizeof (rctl_dict_entry_t));
3267c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		oval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		rctlsys_rblk_xfrm(oblk, NULL, oval, RBX_FROM_BLK | RBX_VAL);
3327c478bd9Sstevel@tonic-gate 		mutex_enter(&curproc->p_lock);
3337c478bd9Sstevel@tonic-gate 		ret = rctl_local_get(hndl, oval, nval, curproc);
3347c478bd9Sstevel@tonic-gate 		mutex_exit(&curproc->p_lock);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		kmem_cache_free(rctl_val_cache, oval);
3377c478bd9Sstevel@tonic-gate 		kmem_free(oblk, sizeof (rctl_opaque_t));
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		if (ret != 0) {
3407c478bd9Sstevel@tonic-gate 			kmem_cache_free(rctl_val_cache, nval);
3417c478bd9Sstevel@tonic-gate 			kmem_free(krde, sizeof (rctl_dict_entry_t));
3427c478bd9Sstevel@tonic-gate 			return (set_errno(ret));
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	nblk = kmem_alloc(sizeof (rctl_opaque_t), KM_SLEEP);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	rctlsys_rblk_xfrm(nblk, krde, nval, RBX_TO_BLK | RBX_VAL | RBX_CTL);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	kmem_free(krde, sizeof (rctl_dict_entry_t));
3517c478bd9Sstevel@tonic-gate 	kmem_cache_free(rctl_val_cache, nval);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (copyout(nblk, new_rblk, sizeof (rctl_opaque_t)) == -1) {
3547c478bd9Sstevel@tonic-gate 		kmem_free(nblk, sizeof (rctl_opaque_t));
3557c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	kmem_free(nblk, sizeof (rctl_opaque_t));
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	return (0);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /*
3647c478bd9Sstevel@tonic-gate  * static long rctlsys_set(char *name, rctl_opaque_t *old_rblk,
3657c478bd9Sstevel@tonic-gate  *   rctl_opaque_t *new_rblk, int flags)
3667c478bd9Sstevel@tonic-gate  *
3677c478bd9Sstevel@tonic-gate  * Overview
3687c478bd9Sstevel@tonic-gate  *   rctlsys_set() is the implementation of the core login of setrctl(2), which
3697c478bd9Sstevel@tonic-gate  *   allows the establishment of resource control values.  Flags may take on any
3707c478bd9Sstevel@tonic-gate  *   of three exclusive values:  RCTL_INSERT, RCTL_DELETE, and RCTL_REPLACE.
3717c478bd9Sstevel@tonic-gate  *   RCTL_INSERT ignores old_rblk and inserts the value in the appropriate
3727c478bd9Sstevel@tonic-gate  *   position in the ordered sequence of resource control values.  RCTL_DELETE
3737c478bd9Sstevel@tonic-gate  *   ignores old_rblk and deletes the first resource control value matching
3747c478bd9Sstevel@tonic-gate  *   (value, priority) in the given resource block.  If no matching value is
3757c478bd9Sstevel@tonic-gate  *   found, -1 is returned and errno is set to ENOENT.  Finally, in the case of
3767c478bd9Sstevel@tonic-gate  *   RCTL_REPLACE, old_rblk is used to match (value, priority); the matching
3777c478bd9Sstevel@tonic-gate  *   resource control value in the sequence is replaced with the contents of
3787c478bd9Sstevel@tonic-gate  *   new_rblk.  Again, if no match is found, -1 is returned and errno is set to
3797c478bd9Sstevel@tonic-gate  *   ENOENT.
3807c478bd9Sstevel@tonic-gate  *
3817c478bd9Sstevel@tonic-gate  *   rctlsys_set() causes a cursor test, which can reactivate resource controls
3827c478bd9Sstevel@tonic-gate  *   that have previously fired.
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate static long
3857c478bd9Sstevel@tonic-gate rctlsys_set(char *name, rctl_opaque_t *old_rblk, rctl_opaque_t *new_rblk,
3867c478bd9Sstevel@tonic-gate     int flags)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	rctl_val_t *nval;
3897c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
3907c478bd9Sstevel@tonic-gate 	rctl_opaque_t *nblk;
3917c478bd9Sstevel@tonic-gate 	rctl_hndl_t hndl;
3927c478bd9Sstevel@tonic-gate 	char *kname;
3937c478bd9Sstevel@tonic-gate 	size_t klen;
3947c478bd9Sstevel@tonic-gate 	long ret = 0;
3957c478bd9Sstevel@tonic-gate 	proc_t *pp = NULL;
3967c478bd9Sstevel@tonic-gate 	pid_t pid;
3977c478bd9Sstevel@tonic-gate 	int action = flags & (~RCTLSYS_ACTION_MASK);
3987c478bd9Sstevel@tonic-gate 	rctl_val_t *oval;
3997c478bd9Sstevel@tonic-gate 	rctl_val_t *rval1;
4007c478bd9Sstevel@tonic-gate 	rctl_val_t *rval2;
4017c478bd9Sstevel@tonic-gate 	rctl_val_t *tval;
4027c478bd9Sstevel@tonic-gate 	rctl_opaque_t *oblk;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (flags & (~RCTLSYS_MASK))
4057c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (action != RCTL_INSERT &&
4087c478bd9Sstevel@tonic-gate 	    action != RCTL_DELETE &&
4097c478bd9Sstevel@tonic-gate 	    action != RCTL_REPLACE)
4107c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if (new_rblk == NULL || name == NULL)
4137c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	kname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4167c478bd9Sstevel@tonic-gate 	if (copyinstr(name, kname, MAXPATHLEN, &klen) != 0) {
4177c478bd9Sstevel@tonic-gate 		kmem_free(kname, MAXPATHLEN);
4187c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	if ((hndl = rctl_hndl_lookup(kname)) == -1) {
4227c478bd9Sstevel@tonic-gate 		kmem_free(kname, MAXPATHLEN);
4237c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	kmem_free(kname, MAXPATHLEN);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	rde = rctl_dict_lookup_hndl(hndl);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	nblk = kmem_alloc(sizeof (rctl_opaque_t), KM_SLEEP);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if (copyin(new_rblk, nblk, sizeof (rctl_opaque_t)) == -1) {
4337c478bd9Sstevel@tonic-gate 		kmem_free(nblk, sizeof (rctl_opaque_t));
4347c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	nval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	rctlsys_rblk_xfrm(nblk, NULL, nval, RBX_FROM_BLK | RBX_VAL);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if (rctl_invalid_value(rde, nval)) {
4427c478bd9Sstevel@tonic-gate 		kmem_free(nblk, sizeof (rctl_opaque_t));
4437c478bd9Sstevel@tonic-gate 		kmem_cache_free(rctl_val_cache, nval);
4447c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	/* allocate what we might need before potentially grabbing p_lock */
4487c478bd9Sstevel@tonic-gate 	oblk = kmem_alloc(sizeof (rctl_opaque_t), KM_SLEEP);
4497c478bd9Sstevel@tonic-gate 	oval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
4507c478bd9Sstevel@tonic-gate 	rval1 = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
4517c478bd9Sstevel@tonic-gate 	rval2 = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if (nval->rcv_privilege == RCPRIV_BASIC) {
4547c478bd9Sstevel@tonic-gate 		if (flags & RCTL_USE_RECIPIENT_PID) {
4557c478bd9Sstevel@tonic-gate 			pid = nval->rcv_action_recip_pid;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 			/* case for manipulating rctl values on other procs */
4587c478bd9Sstevel@tonic-gate 			if (pid != curproc->p_pid) {
4597c478bd9Sstevel@tonic-gate 				/* cannot be other pid on process rctls */
4607c478bd9Sstevel@tonic-gate 				if (rde->rcd_entity == RCENTITY_PROCESS) {
4617c478bd9Sstevel@tonic-gate 					ret = set_errno(EINVAL);
4627c478bd9Sstevel@tonic-gate 					goto rctlsys_out;
4637c478bd9Sstevel@tonic-gate 				}
4647c478bd9Sstevel@tonic-gate 				/*
4657c478bd9Sstevel@tonic-gate 				 * must have privilege to manipulate controls
4667c478bd9Sstevel@tonic-gate 				 * on other processes
4677c478bd9Sstevel@tonic-gate 				 */
4687c478bd9Sstevel@tonic-gate 				if (secpolicy_rctlsys(CRED(), B_FALSE) != 0) {
4697c478bd9Sstevel@tonic-gate 					ret = set_errno(EACCES);
4707c478bd9Sstevel@tonic-gate 					goto rctlsys_out;
4717c478bd9Sstevel@tonic-gate 				}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 				pid = nval->rcv_action_recip_pid;
4747c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
4757c478bd9Sstevel@tonic-gate 				pp = prfind(pid);
4767c478bd9Sstevel@tonic-gate 				if (!pp) {
4777c478bd9Sstevel@tonic-gate 					mutex_exit(&pidlock);
4787c478bd9Sstevel@tonic-gate 					ret = set_errno(ESRCH);
4797c478bd9Sstevel@tonic-gate 					goto rctlsys_out;
4807c478bd9Sstevel@tonic-gate 				}
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 				/*
4837c478bd9Sstevel@tonic-gate 				 * idle or zombie procs have either not yet
4847c478bd9Sstevel@tonic-gate 				 * set up their rctls or have already done
4857c478bd9Sstevel@tonic-gate 				 * their rctl_set_tearoff's.
4867c478bd9Sstevel@tonic-gate 				 */
4877c478bd9Sstevel@tonic-gate 				if (pp->p_stat == SZOMB ||
4887c478bd9Sstevel@tonic-gate 				    pp->p_stat == SIDL) {
4897c478bd9Sstevel@tonic-gate 					mutex_exit(&pidlock);
4907c478bd9Sstevel@tonic-gate 					ret = set_errno(ESRCH);
4917c478bd9Sstevel@tonic-gate 					goto rctlsys_out;
4927c478bd9Sstevel@tonic-gate 				}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 				/*
4957c478bd9Sstevel@tonic-gate 				 * hold this pp's p_lock to ensure that
4967c478bd9Sstevel@tonic-gate 				 * it does not do it's rctl_set_tearoff
4977c478bd9Sstevel@tonic-gate 				 * If we did not do this, we could
4987c478bd9Sstevel@tonic-gate 				 * potentially add rctls to the entity
4997c478bd9Sstevel@tonic-gate 				 * with a recipient that is a process
5007c478bd9Sstevel@tonic-gate 				 * that has exited.
5017c478bd9Sstevel@tonic-gate 				 */
5027c478bd9Sstevel@tonic-gate 				mutex_enter(&pp->p_lock);
5037c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 				/*
5067c478bd9Sstevel@tonic-gate 				 * We know that curproc's task, project,
5077c478bd9Sstevel@tonic-gate 				 * and zone pointers will not change
5087c478bd9Sstevel@tonic-gate 				 * because functions that change them
5097c478bd9Sstevel@tonic-gate 				 * call holdlwps(SHOLDFORK1) first.
5107c478bd9Sstevel@tonic-gate 				 */
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 				/*
5137c478bd9Sstevel@tonic-gate 				 * verify that the found pp is in the
5147c478bd9Sstevel@tonic-gate 				 * current task.  If it is, then it
5157c478bd9Sstevel@tonic-gate 				 * is also within the current project
5167c478bd9Sstevel@tonic-gate 				 * and zone.
5177c478bd9Sstevel@tonic-gate 				 */
5187c478bd9Sstevel@tonic-gate 				if (rde->rcd_entity == RCENTITY_TASK &&
5197c478bd9Sstevel@tonic-gate 				    pp->p_task != curproc->p_task) {
5207c478bd9Sstevel@tonic-gate 					ret = set_errno(ESRCH);
5217c478bd9Sstevel@tonic-gate 					goto rctlsys_out;
5227c478bd9Sstevel@tonic-gate 				}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 				ASSERT(pp->p_task->tk_proj ==
5257c478bd9Sstevel@tonic-gate 				    curproc->p_task->tk_proj);
5267c478bd9Sstevel@tonic-gate 				ASSERT(pp->p_zone == curproc->p_zone);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 				nval->rcv_action_recipient = pp;
5307c478bd9Sstevel@tonic-gate 				nval->rcv_action_recip_pid = pid;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 			} else {
5337c478bd9Sstevel@tonic-gate 				/* for manipulating rctl values on this proc */
5347c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
5357c478bd9Sstevel@tonic-gate 				pp = curproc;
5367c478bd9Sstevel@tonic-gate 				nval->rcv_action_recipient = curproc;
5377c478bd9Sstevel@tonic-gate 				nval->rcv_action_recip_pid = curproc->p_pid;
5387c478bd9Sstevel@tonic-gate 			}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		} else {
5417c478bd9Sstevel@tonic-gate 			/* RCTL_USE_RECIPIENT_PID not set, use this proc */
5427c478bd9Sstevel@tonic-gate 			mutex_enter(&curproc->p_lock);
5437c478bd9Sstevel@tonic-gate 			pp = curproc;
5447c478bd9Sstevel@tonic-gate 			nval->rcv_action_recipient = curproc;
5457c478bd9Sstevel@tonic-gate 			nval->rcv_action_recip_pid = curproc->p_pid;
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	} else {
5497c478bd9Sstevel@tonic-gate 		/* privileged controls have no recipient pid */
5507c478bd9Sstevel@tonic-gate 		mutex_enter(&curproc->p_lock);
5517c478bd9Sstevel@tonic-gate 		pp = curproc;
5527c478bd9Sstevel@tonic-gate 		nval->rcv_action_recipient = NULL;
5537c478bd9Sstevel@tonic-gate 		nval->rcv_action_recip_pid = -1;
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	nval->rcv_firing_time = 0;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	if (action == RCTL_REPLACE) {
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		if (copyin(old_rblk, oblk, sizeof (rctl_opaque_t)) == -1) {
5617c478bd9Sstevel@tonic-gate 			ret = set_errno(EFAULT);
5627c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		rctlsys_rblk_xfrm(oblk, NULL, oval, RBX_FROM_BLK | RBX_VAL);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		if (rctl_invalid_value(rde, oval)) {
5687c478bd9Sstevel@tonic-gate 			ret = set_errno(EINVAL);
5697c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 		if (oval->rcv_privilege == RCPRIV_BASIC) {
5737c478bd9Sstevel@tonic-gate 			if (!(flags & RCTL_USE_RECIPIENT_PID)) {
5747c478bd9Sstevel@tonic-gate 				oval->rcv_action_recipient = curproc;
5757c478bd9Sstevel@tonic-gate 				oval->rcv_action_recip_pid = curproc->p_pid;
5767c478bd9Sstevel@tonic-gate 			}
5777c478bd9Sstevel@tonic-gate 		} else {
5787c478bd9Sstevel@tonic-gate 			oval->rcv_action_recipient = NULL;
5797c478bd9Sstevel@tonic-gate 			oval->rcv_action_recip_pid = -1;
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		/*
5837c478bd9Sstevel@tonic-gate 		 * Find the real value we're attempting to replace on the
5847c478bd9Sstevel@tonic-gate 		 * sequence, rather than trusting the one delivered from
5857c478bd9Sstevel@tonic-gate 		 * userland.
5867c478bd9Sstevel@tonic-gate 		 */
5877c478bd9Sstevel@tonic-gate 		if (ret = rctl_local_get(hndl, NULL, rval1, pp)) {
5887c478bd9Sstevel@tonic-gate 			(void) set_errno(ret);
5897c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 		do {
5937c478bd9Sstevel@tonic-gate 			if (rval1->rcv_privilege == RCPRIV_SYSTEM ||
5947c478bd9Sstevel@tonic-gate 			    rctl_val_cmp(oval, rval1, 0) == 0)
5957c478bd9Sstevel@tonic-gate 				break;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 			tval = rval1;
5987c478bd9Sstevel@tonic-gate 			rval1 = rval2;
5997c478bd9Sstevel@tonic-gate 			rval2 = tval;
6007c478bd9Sstevel@tonic-gate 		} while (rctl_local_get(hndl, rval2, rval1, pp) == 0);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		if (rval1->rcv_privilege == RCPRIV_SYSTEM) {
6037c478bd9Sstevel@tonic-gate 			if (rctl_val_cmp(oval, rval1, 1) == 0)
6047c478bd9Sstevel@tonic-gate 				ret = set_errno(EPERM);
6057c478bd9Sstevel@tonic-gate 			else
6067c478bd9Sstevel@tonic-gate 				ret = set_errno(ESRCH);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		bcopy(rval1, oval, sizeof (rctl_val_t));
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		/*
6147c478bd9Sstevel@tonic-gate 		 * System controls are immutable.
6157c478bd9Sstevel@tonic-gate 		 */
6167c478bd9Sstevel@tonic-gate 		if (nval->rcv_privilege == RCPRIV_SYSTEM) {
6177c478bd9Sstevel@tonic-gate 			ret = set_errno(EPERM);
6187c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
6197c478bd9Sstevel@tonic-gate 		}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		/*
6227c478bd9Sstevel@tonic-gate 		 * Only privileged processes in the global zone can modify
6237c478bd9Sstevel@tonic-gate 		 * privileged rctls of type RCENTITY_ZONE; replacing privileged
6247c478bd9Sstevel@tonic-gate 		 * controls with basic ones are not allowed either.  Lowering a
6257c478bd9Sstevel@tonic-gate 		 * lowerable one might be OK for privileged processes in a
6267c478bd9Sstevel@tonic-gate 		 * non-global zone, but lowerable rctls probably don't make
6277c478bd9Sstevel@tonic-gate 		 * sense for zones (hence, not modifiable from within a zone).
6287c478bd9Sstevel@tonic-gate 		 */
6297c478bd9Sstevel@tonic-gate 		if (rde->rcd_entity == RCENTITY_ZONE &&
6307c478bd9Sstevel@tonic-gate 		    (nval->rcv_privilege == RCPRIV_PRIVILEGED ||
6317c478bd9Sstevel@tonic-gate 		    oval->rcv_privilege == RCPRIV_PRIVILEGED) &&
6327c478bd9Sstevel@tonic-gate 		    secpolicy_rctlsys(CRED(), B_TRUE) != 0) {
6337c478bd9Sstevel@tonic-gate 			ret = set_errno(EACCES);
6347c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		/*
6387c478bd9Sstevel@tonic-gate 		 * Must be privileged to replace a privileged control with
6397c478bd9Sstevel@tonic-gate 		 * a basic one.
6407c478bd9Sstevel@tonic-gate 		 */
6417c478bd9Sstevel@tonic-gate 		if (oval->rcv_privilege == RCPRIV_PRIVILEGED &&
6427c478bd9Sstevel@tonic-gate 		    nval->rcv_privilege != RCPRIV_PRIVILEGED &&
6437c478bd9Sstevel@tonic-gate 		    secpolicy_rctlsys(CRED(), B_FALSE) != 0) {
6447c478bd9Sstevel@tonic-gate 			ret = set_errno(EACCES);
6457c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		/*
6497c478bd9Sstevel@tonic-gate 		 * Must have lowerable global property for non-privileged
6507c478bd9Sstevel@tonic-gate 		 * to lower the value of a privileged control; otherwise must
6517c478bd9Sstevel@tonic-gate 		 * have sufficient privileges to modify privileged controls
6527c478bd9Sstevel@tonic-gate 		 * at all.
6537c478bd9Sstevel@tonic-gate 		 */
6547c478bd9Sstevel@tonic-gate 		if (oval->rcv_privilege == RCPRIV_PRIVILEGED &&
6557c478bd9Sstevel@tonic-gate 		    nval->rcv_privilege == RCPRIV_PRIVILEGED &&
6567c478bd9Sstevel@tonic-gate 		    ((((rde->rcd_flagaction & RCTL_GLOBAL_LOWERABLE) == 0) ||
6577c478bd9Sstevel@tonic-gate 		    oval->rcv_flagaction != nval->rcv_flagaction ||
6587c478bd9Sstevel@tonic-gate 		    oval->rcv_action_signal != nval->rcv_action_signal ||
6597c478bd9Sstevel@tonic-gate 		    oval->rcv_value < nval->rcv_value)) &&
6607c478bd9Sstevel@tonic-gate 		    secpolicy_rctlsys(CRED(), B_FALSE) != 0) {
6617c478bd9Sstevel@tonic-gate 			ret = set_errno(EACCES);
6627c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
6637c478bd9Sstevel@tonic-gate 		}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		if (ret = rctl_local_replace(hndl, oval, nval, pp)) {
6667c478bd9Sstevel@tonic-gate 			(void) set_errno(ret);
6677c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		/* ensure that nval is not freed */
6717c478bd9Sstevel@tonic-gate 		nval = NULL;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	} else if (action == RCTL_INSERT) {
6747c478bd9Sstevel@tonic-gate 		/*
6757c478bd9Sstevel@tonic-gate 		 * System controls are immutable.
6767c478bd9Sstevel@tonic-gate 		 */
6777c478bd9Sstevel@tonic-gate 		if (nval->rcv_privilege == RCPRIV_SYSTEM) {
6787c478bd9Sstevel@tonic-gate 			ret = set_errno(EPERM);
6797c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
6807c478bd9Sstevel@tonic-gate 		}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		/*
6837c478bd9Sstevel@tonic-gate 		 * Only privileged processes in the global zone may add
6847c478bd9Sstevel@tonic-gate 		 * privileged zone.* rctls.  Only privileged processes
6857c478bd9Sstevel@tonic-gate 		 * may add other privileged rctls.
6867c478bd9Sstevel@tonic-gate 		 */
6877c478bd9Sstevel@tonic-gate 		if (nval->rcv_privilege == RCPRIV_PRIVILEGED) {
6887c478bd9Sstevel@tonic-gate 			if ((rde->rcd_entity == RCENTITY_ZONE &&
6897c478bd9Sstevel@tonic-gate 			    secpolicy_rctlsys(CRED(), B_TRUE) != 0) ||
6907c478bd9Sstevel@tonic-gate 			    (rde->rcd_entity != RCENTITY_ZONE &&
6917c478bd9Sstevel@tonic-gate 			    secpolicy_rctlsys(CRED(), B_FALSE) != 0)) {
6927c478bd9Sstevel@tonic-gate 				ret = set_errno(EACCES);
6937c478bd9Sstevel@tonic-gate 				goto rctlsys_out;
6947c478bd9Sstevel@tonic-gate 			}
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 		/*
6987c478bd9Sstevel@tonic-gate 		 * Only one basic control is allowed per rctl.
6997c478bd9Sstevel@tonic-gate 		 * If a basic control is being inserted, delete
7007c478bd9Sstevel@tonic-gate 		 * any other basic control.
7017c478bd9Sstevel@tonic-gate 		 */
7027c478bd9Sstevel@tonic-gate 		if ((nval->rcv_privilege == RCPRIV_BASIC) &&
7037c478bd9Sstevel@tonic-gate 		    (rctl_local_get(hndl, NULL, rval1, pp) == 0)) {
7047c478bd9Sstevel@tonic-gate 			do {
7057c478bd9Sstevel@tonic-gate 				if (rval1->rcv_privilege == RCPRIV_BASIC &&
7067c478bd9Sstevel@tonic-gate 				    rval1->rcv_action_recipient == curproc) {
7077c478bd9Sstevel@tonic-gate 					(void) rctl_local_delete(hndl, rval1,
7087c478bd9Sstevel@tonic-gate 					    pp);
7097c478bd9Sstevel@tonic-gate 					if (rctl_local_get(hndl, NULL, rval1,
7107c478bd9Sstevel@tonic-gate 					    pp) != 0)
7117c478bd9Sstevel@tonic-gate 						break;
7127c478bd9Sstevel@tonic-gate 				}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 				tval = rval1;
7157c478bd9Sstevel@tonic-gate 				rval1 = rval2;
7167c478bd9Sstevel@tonic-gate 				rval2 = tval;
7177c478bd9Sstevel@tonic-gate 			} while (rctl_local_get(hndl, rval2, rval1, pp)
7187c478bd9Sstevel@tonic-gate 			    == 0);
7197c478bd9Sstevel@tonic-gate 		}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 		if (ret = rctl_local_insert(hndl, nval, pp)) {
7237c478bd9Sstevel@tonic-gate 			(void) set_errno(ret);
7247c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 		/* ensure that nval is not freed */
7287c478bd9Sstevel@tonic-gate 		nval = NULL;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	} else {
7317c478bd9Sstevel@tonic-gate 		/*
7327c478bd9Sstevel@tonic-gate 		 * RCTL_DELETE
7337c478bd9Sstevel@tonic-gate 		 */
7347c478bd9Sstevel@tonic-gate 		if (nval->rcv_privilege == RCPRIV_SYSTEM) {
7357c478bd9Sstevel@tonic-gate 			ret = set_errno(EPERM);
7367c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		if (nval->rcv_privilege == RCPRIV_PRIVILEGED) {
7407c478bd9Sstevel@tonic-gate 			if ((rde->rcd_entity == RCENTITY_ZONE &&
7417c478bd9Sstevel@tonic-gate 			    secpolicy_rctlsys(CRED(), B_TRUE) != 0) ||
7427c478bd9Sstevel@tonic-gate 			    (rde->rcd_entity != RCENTITY_ZONE &&
7437c478bd9Sstevel@tonic-gate 			    secpolicy_rctlsys(CRED(), B_FALSE) != 0)) {
7447c478bd9Sstevel@tonic-gate 				ret = set_errno(EACCES);
7457c478bd9Sstevel@tonic-gate 				goto rctlsys_out;
7467c478bd9Sstevel@tonic-gate 			}
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		if (ret = rctl_local_delete(hndl, nval, pp)) {
7507c478bd9Sstevel@tonic-gate 			(void) set_errno(ret);
7517c478bd9Sstevel@tonic-gate 			goto rctlsys_out;
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate rctlsys_out:
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (pp)
7587c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	kmem_free(nblk, sizeof (rctl_opaque_t));
7617c478bd9Sstevel@tonic-gate 	kmem_free(oblk, sizeof (rctl_opaque_t));
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/* only free nval if we did not rctl_local_insert it */
7647c478bd9Sstevel@tonic-gate 	if (nval)
7657c478bd9Sstevel@tonic-gate 		kmem_cache_free(rctl_val_cache, nval);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	kmem_cache_free(rctl_val_cache, oval);
7687c478bd9Sstevel@tonic-gate 	kmem_cache_free(rctl_val_cache, rval1);
7697c478bd9Sstevel@tonic-gate 	kmem_cache_free(rctl_val_cache, rval2);
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	return (ret);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate static long
7757c478bd9Sstevel@tonic-gate rctlsys_lst(char *ubuf, size_t ubufsz)
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate 	char *kbuf;
7787c478bd9Sstevel@tonic-gate 	size_t kbufsz;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	kbufsz = rctl_build_name_buf(&kbuf);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if (kbufsz <= ubufsz &&
7837c478bd9Sstevel@tonic-gate 	    copyout(kbuf, ubuf, kbufsz) != 0) {
7847c478bd9Sstevel@tonic-gate 		kmem_free(kbuf, kbufsz);
7857c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, kbufsz);
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	return (kbufsz);
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate static long
7947c478bd9Sstevel@tonic-gate rctlsys_ctl(char *name, rctl_opaque_t *rblk, int flags)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *krde;
7977c478bd9Sstevel@tonic-gate 	rctl_opaque_t *krblk;
7987c478bd9Sstevel@tonic-gate 	char *kname;
7997c478bd9Sstevel@tonic-gate 	size_t klen;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	kname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	if (name == NULL || copyinstr(name, kname, MAXPATHLEN, &klen) != 0) {
8047c478bd9Sstevel@tonic-gate 		kmem_free(kname, MAXPATHLEN);
8057c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	switch (flags) {
8097c478bd9Sstevel@tonic-gate 	case RCTLCTL_GET:
8107c478bd9Sstevel@tonic-gate 		krde = kmem_alloc(sizeof (rctl_dict_entry_t), KM_SLEEP);
8117c478bd9Sstevel@tonic-gate 		krblk = kmem_zalloc(sizeof (rctl_opaque_t), KM_SLEEP);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 		if (rctl_global_get(kname, krde) == -1) {
8147c478bd9Sstevel@tonic-gate 			kmem_free(krde, sizeof (rctl_dict_entry_t));
8157c478bd9Sstevel@tonic-gate 			kmem_free(krblk, sizeof (rctl_opaque_t));
8167c478bd9Sstevel@tonic-gate 			kmem_free(kname, MAXPATHLEN);
8177c478bd9Sstevel@tonic-gate 			return (set_errno(ESRCH));
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		rctlsys_rblk_xfrm(krblk, krde, NULL, RBX_TO_BLK | RBX_CTL);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 		if (copyout(krblk, rblk, sizeof (rctl_opaque_t)) != 0) {
8237c478bd9Sstevel@tonic-gate 			kmem_free(krde, sizeof (rctl_dict_entry_t));
8247c478bd9Sstevel@tonic-gate 			kmem_free(krblk, sizeof (rctl_opaque_t));
8257c478bd9Sstevel@tonic-gate 			kmem_free(kname, MAXPATHLEN);
8267c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 		kmem_free(krde, sizeof (rctl_dict_entry_t));
8307c478bd9Sstevel@tonic-gate 		kmem_free(krblk, sizeof (rctl_opaque_t));
8317c478bd9Sstevel@tonic-gate 		kmem_free(kname, MAXPATHLEN);
8327c478bd9Sstevel@tonic-gate 		break;
8337c478bd9Sstevel@tonic-gate 	case RCTLCTL_SET:
8347c478bd9Sstevel@tonic-gate 		if (secpolicy_rctlsys(CRED(), B_TRUE) != 0) {
8357c478bd9Sstevel@tonic-gate 			kmem_free(kname, MAXPATHLEN);
8367c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
8377c478bd9Sstevel@tonic-gate 		}
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		krde = kmem_alloc(sizeof (rctl_dict_entry_t), KM_SLEEP);
8407c478bd9Sstevel@tonic-gate 		krblk = kmem_zalloc(sizeof (rctl_opaque_t), KM_SLEEP);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		if (rctl_global_get(kname, krde) == -1) {
8437c478bd9Sstevel@tonic-gate 			kmem_free(krde, sizeof (rctl_dict_entry_t));
8447c478bd9Sstevel@tonic-gate 			kmem_free(krblk, sizeof (rctl_opaque_t));
8457c478bd9Sstevel@tonic-gate 			kmem_free(kname, MAXPATHLEN);
8467c478bd9Sstevel@tonic-gate 			return (set_errno(ESRCH));
8477c478bd9Sstevel@tonic-gate 		}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 		if (copyin(rblk, krblk, sizeof (rctl_opaque_t)) != 0) {
8507c478bd9Sstevel@tonic-gate 			kmem_free(krde, sizeof (rctl_dict_entry_t));
8517c478bd9Sstevel@tonic-gate 			kmem_free(krblk, sizeof (rctl_opaque_t));
8527c478bd9Sstevel@tonic-gate 			kmem_free(kname, MAXPATHLEN);
8537c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
8547c478bd9Sstevel@tonic-gate 		}
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 		rctlsys_rblk_xfrm(krblk, krde, NULL, RBX_FROM_BLK | RBX_CTL);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		if (rctl_global_set(kname, krde) == -1) {
8597c478bd9Sstevel@tonic-gate 			kmem_free(krde, sizeof (rctl_dict_entry_t));
8607c478bd9Sstevel@tonic-gate 			kmem_free(krblk, sizeof (rctl_opaque_t));
8617c478bd9Sstevel@tonic-gate 			kmem_free(kname, MAXPATHLEN);
8627c478bd9Sstevel@tonic-gate 			return (set_errno(ESRCH));
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		kmem_free(krde, sizeof (rctl_dict_entry_t));
8667c478bd9Sstevel@tonic-gate 		kmem_free(krblk, sizeof (rctl_opaque_t));
8677c478bd9Sstevel@tonic-gate 		kmem_free(kname, MAXPATHLEN);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		break;
8707c478bd9Sstevel@tonic-gate 	default:
8717c478bd9Sstevel@tonic-gate 		kmem_free(kname, MAXPATHLEN);
8727c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	return (0);
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
878532877c4Srd117015 /*
879532877c4Srd117015  * The arbitrary maximum number of rctl_opaque_t that we can pass to
880532877c4Srd117015  * rctl_projset().
881532877c4Srd117015  */
882532877c4Srd117015 #define	RCTL_PROJSET_MAXSIZE	1024
883532877c4Srd117015 
884532877c4Srd117015 static long
885532877c4Srd117015 rctlsys_projset(char *name, rctl_opaque_t *rblk, size_t size, int flags)
886532877c4Srd117015 {
887532877c4Srd117015 	rctl_dict_entry_t *krde;
888532877c4Srd117015 	rctl_opaque_t *krblk;
889532877c4Srd117015 	char *kname;
890532877c4Srd117015 	size_t klen;
891532877c4Srd117015 	rctl_hndl_t hndl;
892532877c4Srd117015 	rctl_val_t *new_values = NULL;
893532877c4Srd117015 	rctl_val_t *alloc_values = NULL;
894532877c4Srd117015 	rctl_val_t *new_val;
895532877c4Srd117015 	rctl_val_t *alloc_val;
896532877c4Srd117015 	int error = 0;
897532877c4Srd117015 	int count;
898532877c4Srd117015 
899532877c4Srd117015 	kname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
900532877c4Srd117015 
901532877c4Srd117015 	if (name == NULL || copyinstr(name, kname, MAXPATHLEN, &klen) != 0) {
902532877c4Srd117015 		kmem_free(kname, MAXPATHLEN);
903532877c4Srd117015 		return (set_errno(EFAULT));
904532877c4Srd117015 	}
905532877c4Srd117015 
906532877c4Srd117015 	if (size > RCTL_PROJSET_MAXSIZE) {
907532877c4Srd117015 		kmem_free(kname, MAXPATHLEN);
908532877c4Srd117015 		return (set_errno(EINVAL));
909532877c4Srd117015 	}
910532877c4Srd117015 
911532877c4Srd117015 	if ((hndl = rctl_hndl_lookup(kname)) == -1) {
912532877c4Srd117015 		kmem_free(kname, MAXPATHLEN);
913532877c4Srd117015 		return (set_errno(EINVAL));
914532877c4Srd117015 	}
915532877c4Srd117015 
916532877c4Srd117015 	krde = rctl_dict_lookup_hndl(hndl);
917532877c4Srd117015 
918532877c4Srd117015 	/* If not a project entity then exit */
919532877c4Srd117015 	if ((krde->rcd_entity != RCENTITY_PROJECT) || (size <= 0)) {
920532877c4Srd117015 		kmem_free(kname, MAXPATHLEN);
921532877c4Srd117015 		return (set_errno(EINVAL));
922532877c4Srd117015 	}
923532877c4Srd117015 
9246fa77e95Srd117015 	if (secpolicy_rctlsys(CRED(), B_FALSE) != 0) {
9256fa77e95Srd117015 		kmem_free(kname, MAXPATHLEN);
9266fa77e95Srd117015 		return (set_errno(EPERM));
9276fa77e95Srd117015 	}
9286fa77e95Srd117015 
929532877c4Srd117015 	/* Allocate an array large enough for all resource control blocks */
930532877c4Srd117015 	krblk = kmem_zalloc(sizeof (rctl_opaque_t) * size, KM_SLEEP);
931532877c4Srd117015 
932532877c4Srd117015 	if (copyin(rblk, krblk, sizeof (rctl_opaque_t) * size) == 0) {
933532877c4Srd117015 
934532877c4Srd117015 		for (count = 0; (count < size) && (error == 0); count++) {
935532877c4Srd117015 			new_val = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
936532877c4Srd117015 			alloc_val = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
937532877c4Srd117015 
938532877c4Srd117015 			rctlsys_rblk_xfrm(&krblk[count], NULL, new_val,
939532877c4Srd117015 			    RBX_FROM_BLK | RBX_VAL);
940532877c4Srd117015 
941532877c4Srd117015 			/*
942532877c4Srd117015 			 * Project entity resource control values should always
943532877c4Srd117015 			 * be privileged
944532877c4Srd117015 			 */
945532877c4Srd117015 			if (new_val->rcv_privilege != RCPRIV_PRIVILEGED) {
946532877c4Srd117015 				kmem_cache_free(rctl_val_cache, new_val);
947532877c4Srd117015 				kmem_cache_free(rctl_val_cache, alloc_val);
948532877c4Srd117015 
949532877c4Srd117015 				error = EPERM;
950532877c4Srd117015 			} else if (rctl_invalid_value(krde, new_val) == 0) {
951532877c4Srd117015 
952532877c4Srd117015 				/*
953532877c4Srd117015 				 * This is a project entity; we do not set
954532877c4Srd117015 				 * rcv_action_recipient or rcv_action_recip_pid
955532877c4Srd117015 				 */
956532877c4Srd117015 				new_val->rcv_action_recipient = NULL;
957532877c4Srd117015 				new_val->rcv_action_recip_pid = -1;
958532877c4Srd117015 				new_val->rcv_flagaction |= RCTL_LOCAL_PROJDB;
959532877c4Srd117015 				new_val->rcv_firing_time = 0;
960532877c4Srd117015 
961532877c4Srd117015 				new_val->rcv_prev = NULL;
962532877c4Srd117015 				new_val->rcv_next = new_values;
963532877c4Srd117015 				new_values = new_val;
964532877c4Srd117015 
965532877c4Srd117015 				/*
966532877c4Srd117015 				 * alloc_val is left largely uninitialized, it
967532877c4Srd117015 				 * is a pre-allocated rctl_val_t which is used
968532877c4Srd117015 				 * later in rctl_local_replace_all() /
969532877c4Srd117015 				 * rctl_local_insert_all().
970532877c4Srd117015 				 */
971532877c4Srd117015 				alloc_val->rcv_prev = NULL;
972532877c4Srd117015 				alloc_val->rcv_next = alloc_values;
973532877c4Srd117015 				alloc_values = alloc_val;
974532877c4Srd117015 			} else {
975532877c4Srd117015 				kmem_cache_free(rctl_val_cache, new_val);
976532877c4Srd117015 				kmem_cache_free(rctl_val_cache, alloc_val);
977532877c4Srd117015 
978532877c4Srd117015 				error = EINVAL;
979532877c4Srd117015 			}
980532877c4Srd117015 		}
981532877c4Srd117015 
982532877c4Srd117015 	} else {
983532877c4Srd117015 		error = EFAULT;
984532877c4Srd117015 	}
985532877c4Srd117015 
9860580618bSrd117015 	kmem_free(krblk, sizeof (rctl_opaque_t) * size);
987532877c4Srd117015 	kmem_free(kname, MAXPATHLEN);
988532877c4Srd117015 
989532877c4Srd117015 	if (error) {
990532877c4Srd117015 		/*
991532877c4Srd117015 		 * We will have the same number of items in the alloc_values
992532877c4Srd117015 		 * linked list, as we have in new_values.  However, we remain
993532877c4Srd117015 		 * cautious, and teardown the linked lists individually.
994532877c4Srd117015 		 */
995532877c4Srd117015 		while (new_values != NULL) {
996532877c4Srd117015 			new_val = new_values;
997532877c4Srd117015 			new_values = new_values->rcv_next;
998532877c4Srd117015 			kmem_cache_free(rctl_val_cache, new_val);
999532877c4Srd117015 		}
1000532877c4Srd117015 
1001532877c4Srd117015 		while (alloc_values != NULL) {
1002532877c4Srd117015 			alloc_val = alloc_values;
1003532877c4Srd117015 			alloc_values = alloc_values->rcv_next;
1004532877c4Srd117015 			kmem_cache_free(rctl_val_cache, alloc_val);
1005532877c4Srd117015 		}
1006532877c4Srd117015 
1007532877c4Srd117015 		return (set_errno(error));
1008532877c4Srd117015 	}
1009532877c4Srd117015 
1010532877c4Srd117015 	/*
1011532877c4Srd117015 	 * We take the p_lock here to maintain consistency with other functions
1012532877c4Srd117015 	 * - rctlsys_get() and rctlsys_set()
1013532877c4Srd117015 	 */
1014532877c4Srd117015 	mutex_enter(&curproc->p_lock);
1015532877c4Srd117015 	if (flags & TASK_PROJ_PURGE)  {
1016532877c4Srd117015 		(void) rctl_local_replace_all(hndl, new_values, alloc_values,
1017532877c4Srd117015 		    curproc);
1018532877c4Srd117015 	} else {
1019532877c4Srd117015 		(void) rctl_local_insert_all(hndl, new_values, alloc_values,
1020532877c4Srd117015 		    curproc);
1021532877c4Srd117015 	}
1022532877c4Srd117015 	mutex_exit(&curproc->p_lock);
1023532877c4Srd117015 
1024532877c4Srd117015 	return (0);
1025532877c4Srd117015 }
1026532877c4Srd117015 
10277c478bd9Sstevel@tonic-gate long
10287c478bd9Sstevel@tonic-gate rctlsys(int code, char *name, void *obuf, void *nbuf, size_t obufsz, int flags)
10297c478bd9Sstevel@tonic-gate {
10307c478bd9Sstevel@tonic-gate 	switch (code) {
10317c478bd9Sstevel@tonic-gate 	case 0:
10327c478bd9Sstevel@tonic-gate 		return (rctlsys_get(name, obuf, nbuf, flags));
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	case 1:
10357c478bd9Sstevel@tonic-gate 		return (rctlsys_set(name, obuf, nbuf, flags));
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	case 2:
10387c478bd9Sstevel@tonic-gate 		/*
10397c478bd9Sstevel@tonic-gate 		 * Private call for rctl_walk(3C).
10407c478bd9Sstevel@tonic-gate 		 */
10417c478bd9Sstevel@tonic-gate 		return (rctlsys_lst(obuf, obufsz));
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	case 3:
10447c478bd9Sstevel@tonic-gate 		/*
10457c478bd9Sstevel@tonic-gate 		 * Private code for rctladm(1M):  "rctlctl".
10467c478bd9Sstevel@tonic-gate 		 */
10477c478bd9Sstevel@tonic-gate 		return (rctlsys_ctl(name, obuf, flags));
1048532877c4Srd117015 	case 4:
1049532877c4Srd117015 		/*
1050532877c4Srd117015 		 * Private code for setproject(3PROJECT).
1051532877c4Srd117015 		 */
1052532877c4Srd117015 		return (rctlsys_projset(name, nbuf, obufsz, flags));
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	default:
10557c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate }
1058