xref: /titanic_50/usr/src/cmd/prctl/prctl.c (revision c3ea284015697c0da5651ef400f3dc6bc430b290)
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
5*c3ea2840SMenno Lageman  * Common Development and Distribution License (the "License").
6*c3ea2840SMenno Lageman  * 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 /*
22*c3ea2840SMenno 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 
277c478bd9Sstevel@tonic-gate #include <unistd.h>
287c478bd9Sstevel@tonic-gate #include <rctl.h>
297c478bd9Sstevel@tonic-gate #include <libproc.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <locale.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <strings.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <project.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <dirent.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
437c478bd9Sstevel@tonic-gate #include <priv.h>
447c478bd9Sstevel@tonic-gate #include <zone.h>
457c478bd9Sstevel@tonic-gate #include "utils.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /* Valid user actions */
487c478bd9Sstevel@tonic-gate #define	ACTION_DISABLE		0x01
497c478bd9Sstevel@tonic-gate #define	ACTION_ENABLE		0x02
507c478bd9Sstevel@tonic-gate #define	ACTION_SET		0x04
517c478bd9Sstevel@tonic-gate #define	ACTION_REPLACE		0x08
527c478bd9Sstevel@tonic-gate #define	ACTION_DELETE		0x10
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	PRCTL_VALUE_WIDTH	4
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* Maximum string length for deferred errors */
577c478bd9Sstevel@tonic-gate #define	GLOBAL_ERR_SZ		1024
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* allow important process values to be passed together easily */
607c478bd9Sstevel@tonic-gate typedef struct pr_info_handle {
617c478bd9Sstevel@tonic-gate 	struct ps_prochandle *pr;
627c478bd9Sstevel@tonic-gate 	pid_t pid;
637c478bd9Sstevel@tonic-gate 	psinfo_t psinfo;
647c478bd9Sstevel@tonic-gate 	taskid_t taskid;
657c478bd9Sstevel@tonic-gate 	projid_t projid;
667c478bd9Sstevel@tonic-gate 	char *projname;
677c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
687c478bd9Sstevel@tonic-gate 	char *zonename;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate } pr_info_handle_t;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /* Structures for list of resource controls */
737c478bd9Sstevel@tonic-gate typedef struct prctl_value {
747c478bd9Sstevel@tonic-gate 	rctlblk_t *rblk;
757c478bd9Sstevel@tonic-gate 	struct prctl_value *next;
767c478bd9Sstevel@tonic-gate } prctl_value_t;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate typedef struct prctl_list {
797c478bd9Sstevel@tonic-gate 	char *name;
807c478bd9Sstevel@tonic-gate 	prctl_value_t *val_list;
817c478bd9Sstevel@tonic-gate 	struct prctl_list *next;
827c478bd9Sstevel@tonic-gate } prctl_list_t;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static	volatile int	interrupt;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static	prctl_list_t	*global_rctl_list_head = NULL;
877c478bd9Sstevel@tonic-gate static	prctl_list_t	*global_rctl_list_tail = NULL;
887c478bd9Sstevel@tonic-gate static	char		global_error[GLOBAL_ERR_SZ];
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* global variables that contain commmand line option info */
917c478bd9Sstevel@tonic-gate static	int	arg_operation = 0;
927c478bd9Sstevel@tonic-gate static	int	arg_force = 0;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /* String and type from -i */
967c478bd9Sstevel@tonic-gate static	rctl_entity_t	arg_entity_type = RCENTITY_PROCESS;
977c478bd9Sstevel@tonic-gate static	char	*arg_entity_string = NULL;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* -n argument */
1007c478bd9Sstevel@tonic-gate static  char	*arg_name = NULL;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static	rctl_entity_t	arg_name_entity = 0;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /* -t argument value */
1057c478bd9Sstevel@tonic-gate static	int	arg_priv = 0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /* -v argument string */
1087c478bd9Sstevel@tonic-gate static	char	*arg_valuestring = NULL;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /* global flags of rctl name passed to -n */
1117c478bd9Sstevel@tonic-gate static	int	arg_global_flags = 0;
1127c478bd9Sstevel@tonic-gate static	rctl_qty_t	arg_global_max;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* appropriate scaling variables determined by rctl unit type */
1157c478bd9Sstevel@tonic-gate scale_t		*arg_scale;
1167c478bd9Sstevel@tonic-gate static	char	*arg_unit = NULL;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /* -v argument string converted to uint64_t */
1197c478bd9Sstevel@tonic-gate static	uint64_t arg_value = 0;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /* if -v argument is scaled value, points to "K", "M", "G", ... */
1227c478bd9Sstevel@tonic-gate static  char	*arg_modifier = NULL;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /* -e/-d argument string */
1257c478bd9Sstevel@tonic-gate static	char	*arg_action_string = NULL;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /* Set to RCTL_LOCAL_SIGNAL|DENY based on arg_action_string */
1287c478bd9Sstevel@tonic-gate static	int	arg_action = 0;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /* if -e/-d arg is signal=XXX, set to signal number of XXX */
1317c478bd9Sstevel@tonic-gate static	int	arg_signal = 0;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /* -p arg if -p is specified */
1347c478bd9Sstevel@tonic-gate static	int	arg_pid = -1;
1357c478bd9Sstevel@tonic-gate static	char	*arg_pid_string = NULL;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /* Set to 1 if -P is specified */
1387c478bd9Sstevel@tonic-gate static	int	arg_parseable_mode = 0;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /* interupt handler */
1417c478bd9Sstevel@tonic-gate static	void	intr(int);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static	int	get_rctls(struct ps_prochandle *);
1447c478bd9Sstevel@tonic-gate static	int	store_rctls(const char *rctlname, void *walk_data);
1457c478bd9Sstevel@tonic-gate static	prctl_value_t	*store_value_entry(rctlblk_t *rblk, prctl_list_t *list);
1467c478bd9Sstevel@tonic-gate static	prctl_list_t	*store_list_entry(const char *name);
1477c478bd9Sstevel@tonic-gate static	void	free_lists();
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate static	int	change_action(rctlblk_t *blk);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static	int	prctl_setrctl(struct ps_prochandle *Pr, const char *name,
1527c478bd9Sstevel@tonic-gate 			rctlblk_t *, rctlblk_t *, uint_t);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static	int match_rctl(struct ps_prochandle *Pr, rctlblk_t **rctl, char *name,
1557c478bd9Sstevel@tonic-gate 			char *valuestringin, int valuein, rctl_priv_t privin,
1567c478bd9Sstevel@tonic-gate 			int pidin);
1577c478bd9Sstevel@tonic-gate static	int	match_rctl_blk(rctlblk_t *rctl, char *valuestringin,
1587c478bd9Sstevel@tonic-gate 			uint64_t valuein,
1597c478bd9Sstevel@tonic-gate 			rctl_priv_t privin, int pidin);
1607c478bd9Sstevel@tonic-gate static	pid_t	regrab_process(pid_t pid, pr_info_handle_t *p, int, int *gret);
1617c478bd9Sstevel@tonic-gate static	pid_t	grab_process_by_id(char *idname, rctl_entity_t type,
1627c478bd9Sstevel@tonic-gate 			pr_info_handle_t *p, int, int *gret);
1637c478bd9Sstevel@tonic-gate static	int	grab_process(pr_info_handle_t *p, int *gret);
1647c478bd9Sstevel@tonic-gate static	void	release_process(struct ps_prochandle *Pr);
1657c478bd9Sstevel@tonic-gate static	void	preserve_error(char *format, ...);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static	void	print_rctls(pr_info_handle_t *p);
1687c478bd9Sstevel@tonic-gate static	void	print_priv(rctl_priv_t local_priv, char *format);
1697c478bd9Sstevel@tonic-gate static	void	print_local_action(int action, int *signalp, char *format);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static const char USAGE[] = ""
1727c478bd9Sstevel@tonic-gate "usage:\n"
1737c478bd9Sstevel@tonic-gate "    Report resource control values and actions:\n"
1747c478bd9Sstevel@tonic-gate "	prctl [-P] [-t [basic | privileged | system]\n"
1757c478bd9Sstevel@tonic-gate "	[-n name] [-i process | task | project | zone] id ...\n"
1767c478bd9Sstevel@tonic-gate "	-P space delimited output\n"
1777c478bd9Sstevel@tonic-gate "	-t privilege level of rctl values to get\n"
1787c478bd9Sstevel@tonic-gate "	-n name of resource control values to get\n"
1797c478bd9Sstevel@tonic-gate "	-i idtype of operand list\n"
1807c478bd9Sstevel@tonic-gate "    Manipulate resource control values:\n"
1817c478bd9Sstevel@tonic-gate "	prctl [-t [basic | privileged | system]\n"
1827c478bd9Sstevel@tonic-gate "	-n name [-srx] [-v value] [-p pid ] [-e | -d action]\n"
1837c478bd9Sstevel@tonic-gate "	[-i process | task | project | zone] id ...\n"
1847c478bd9Sstevel@tonic-gate "	-t privilege level of rctl value to set/replace/delete/modify\n"
1857c478bd9Sstevel@tonic-gate "	-n name of resource control to set/replace/delete/modify\n"
1867c478bd9Sstevel@tonic-gate "	-s set new resource control value\n"
1877c478bd9Sstevel@tonic-gate "	-r replace first rctl value of matching privilege\n"
1887c478bd9Sstevel@tonic-gate "	-x delete first rctl value of matching privilege, value, and \n"
1897c478bd9Sstevel@tonic-gate "	   recipient pid\n"
1907c478bd9Sstevel@tonic-gate "	-v value of rctl to set/replace/delete/modify\n"
1917c478bd9Sstevel@tonic-gate "	-p recipient pid of rctl to set/replace/delete/modify\n"
1927c478bd9Sstevel@tonic-gate "	-e enable action of first rctl value of matching privilege,\n"
1937c478bd9Sstevel@tonic-gate "	   value, and recipient pid\n"
1947c478bd9Sstevel@tonic-gate "	-d disable action of first rctl value of matching privilege,\n"
1957c478bd9Sstevel@tonic-gate "	   value, and recipient pid\n"
1967c478bd9Sstevel@tonic-gate "	-i idtype of operand list\n";
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static void
2007c478bd9Sstevel@tonic-gate usage()
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(USAGE));
2037c478bd9Sstevel@tonic-gate 	exit(2);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate int
2077c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	int flags;
2107c478bd9Sstevel@tonic-gate 	int opt, errflg = 0;
2117c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblkA = NULL;
2127c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblkB = NULL;
2137c478bd9Sstevel@tonic-gate 	rctlblk_t *tmp = NULL;
2147c478bd9Sstevel@tonic-gate 	pid_t pid;
2157c478bd9Sstevel@tonic-gate 	char *target_id;
2167c478bd9Sstevel@tonic-gate 	int search_type;
2177c478bd9Sstevel@tonic-gate 	int signal;
2187c478bd9Sstevel@tonic-gate 	int localaction;
2197c478bd9Sstevel@tonic-gate 	int printed = 0;
2207c478bd9Sstevel@tonic-gate 	int gret;
2217c478bd9Sstevel@tonic-gate 	char *end;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2247c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2257c478bd9Sstevel@tonic-gate 	(void) setprogname(argv[0]);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "sPp:Fd:e:i:n:rt:v:x")) != EOF) {
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		switch (opt) {
2307c478bd9Sstevel@tonic-gate 		case 'F':	/* force grabbing (no O_EXCL) */
2317c478bd9Sstevel@tonic-gate 			arg_force = PGRAB_FORCE;
2327c478bd9Sstevel@tonic-gate 			break;
2337c478bd9Sstevel@tonic-gate 		case 'i':	/* id type for arguments */
2347c478bd9Sstevel@tonic-gate 			arg_entity_string = optarg;
2357c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "process") == 0 ||
2367c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "pid") == 0)
2377c478bd9Sstevel@tonic-gate 				arg_entity_type = RCENTITY_PROCESS;
2387c478bd9Sstevel@tonic-gate 			else if (strcmp(optarg, "project") == 0 ||
2397c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "projid") == 0)
2407c478bd9Sstevel@tonic-gate 				arg_entity_type = RCENTITY_PROJECT;
2417c478bd9Sstevel@tonic-gate 			else if (strcmp(optarg, "task") == 0 ||
2427c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "taskid") == 0)
2437c478bd9Sstevel@tonic-gate 				arg_entity_type = RCENTITY_TASK;
2447c478bd9Sstevel@tonic-gate 			else if (strcmp(optarg, "zone") == 0 ||
2457c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "zoneid") == 0)
2467c478bd9Sstevel@tonic-gate 				arg_entity_type = RCENTITY_ZONE;
2477c478bd9Sstevel@tonic-gate 			else {
2487c478bd9Sstevel@tonic-gate 				warn(gettext("unknown idtype %s"), optarg);
2497c478bd9Sstevel@tonic-gate 				errflg = 1;
2507c478bd9Sstevel@tonic-gate 			}
2517c478bd9Sstevel@tonic-gate 			break;
2527c478bd9Sstevel@tonic-gate 		case 'd':
2537c478bd9Sstevel@tonic-gate 			arg_action_string = optarg;
2547c478bd9Sstevel@tonic-gate 			arg_operation |= ACTION_DISABLE;
2557c478bd9Sstevel@tonic-gate 			break;
2567c478bd9Sstevel@tonic-gate 		case 'e':
2577c478bd9Sstevel@tonic-gate 			arg_action_string = optarg;
2587c478bd9Sstevel@tonic-gate 			arg_operation |= ACTION_ENABLE;
2597c478bd9Sstevel@tonic-gate 			break;
2607c478bd9Sstevel@tonic-gate 		case 'n':	/* name of rctl */
2617c478bd9Sstevel@tonic-gate 			arg_name = optarg;
2627c478bd9Sstevel@tonic-gate 			if (strncmp(optarg, "process.",
2637c478bd9Sstevel@tonic-gate 			    strlen("process.")) == 0)
2647c478bd9Sstevel@tonic-gate 				arg_name_entity = RCENTITY_PROCESS;
2657c478bd9Sstevel@tonic-gate 			else if (strncmp(optarg, "project.",
2667c478bd9Sstevel@tonic-gate 			    strlen("project.")) == 0)
2677c478bd9Sstevel@tonic-gate 				arg_name_entity = RCENTITY_PROJECT;
2687c478bd9Sstevel@tonic-gate 			else if (strncmp(optarg, "task.",
2697c478bd9Sstevel@tonic-gate 			    strlen("task.")) == 0)
2707c478bd9Sstevel@tonic-gate 				arg_name_entity = RCENTITY_TASK;
2717c478bd9Sstevel@tonic-gate 			else if (strncmp(optarg, "zone.",
2727c478bd9Sstevel@tonic-gate 			    strlen("zone.")) == 0)
2737c478bd9Sstevel@tonic-gate 				arg_name_entity = RCENTITY_ZONE;
2747c478bd9Sstevel@tonic-gate 			break;
2757c478bd9Sstevel@tonic-gate 		case 'r':
2767c478bd9Sstevel@tonic-gate 			arg_operation |= ACTION_REPLACE;
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 		case 't':	/* rctl type */
2797c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "basic") == 0)
2807c478bd9Sstevel@tonic-gate 				arg_priv = RCPRIV_BASIC;
2817c478bd9Sstevel@tonic-gate 			else if (strcmp(optarg, "privileged") == 0)
2827c478bd9Sstevel@tonic-gate 				arg_priv = RCPRIV_PRIVILEGED;
2837c478bd9Sstevel@tonic-gate 			else if (strcmp(optarg, "priv") == 0)
2847c478bd9Sstevel@tonic-gate 				arg_priv = RCPRIV_PRIVILEGED;
2857c478bd9Sstevel@tonic-gate 			else if (strcmp(optarg, "system") == 0)
2867c478bd9Sstevel@tonic-gate 				arg_priv = RCPRIV_SYSTEM;
2877c478bd9Sstevel@tonic-gate 			else {
2887c478bd9Sstevel@tonic-gate 				warn(gettext("unknown privilege %s"), optarg);
2897c478bd9Sstevel@tonic-gate 				errflg = 1;
2907c478bd9Sstevel@tonic-gate 			}
2917c478bd9Sstevel@tonic-gate 			break;
2927c478bd9Sstevel@tonic-gate 		case 'v':	/* value */
2937c478bd9Sstevel@tonic-gate 			arg_valuestring = optarg;
2947c478bd9Sstevel@tonic-gate 			break;
2957c478bd9Sstevel@tonic-gate 		case 's':
2967c478bd9Sstevel@tonic-gate 			arg_operation |= ACTION_SET;
2977c478bd9Sstevel@tonic-gate 			break;
2987c478bd9Sstevel@tonic-gate 		case 'x':	/* delete */
2997c478bd9Sstevel@tonic-gate 			arg_operation |= ACTION_DELETE;
3007c478bd9Sstevel@tonic-gate 			break;
3017c478bd9Sstevel@tonic-gate 		case 'p':
3027c478bd9Sstevel@tonic-gate 			errno = 0;
3037c478bd9Sstevel@tonic-gate 			/* Stick with -1 if arg is "-" */
3047c478bd9Sstevel@tonic-gate 			if (strcmp("-", optarg) == 0)
3057c478bd9Sstevel@tonic-gate 				break;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 			arg_pid_string = optarg;
3087c478bd9Sstevel@tonic-gate 			arg_pid = strtoul(optarg, &end, 10);
3097c478bd9Sstevel@tonic-gate 			if (errno || *end != '\0' || end == optarg) {
3107c478bd9Sstevel@tonic-gate 				warn(gettext("invalid pid %s"), optarg);
3117c478bd9Sstevel@tonic-gate 				errflg = 1;
3127c478bd9Sstevel@tonic-gate 				break;
3137c478bd9Sstevel@tonic-gate 			}
3147c478bd9Sstevel@tonic-gate 			break;
3157c478bd9Sstevel@tonic-gate 		case 'P':
3167c478bd9Sstevel@tonic-gate 			arg_parseable_mode = 1;
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 		default:
3197c478bd9Sstevel@tonic-gate 			warn(gettext("unknown option"));
3207c478bd9Sstevel@tonic-gate 			errflg = 1;
3217c478bd9Sstevel@tonic-gate 			break;
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 	argc -= optind;
3257c478bd9Sstevel@tonic-gate 	argv += optind;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if (argc < 1) {
3287c478bd9Sstevel@tonic-gate 		warn(gettext("no arguments specified"));
3297c478bd9Sstevel@tonic-gate 		errflg = 1;
3307c478bd9Sstevel@tonic-gate 		goto done_parse;
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 	/* if -v is specified without -r, -x, -d, or -e, -s is implied */
3337c478bd9Sstevel@tonic-gate 	if (arg_valuestring &&
3347c478bd9Sstevel@tonic-gate 	    (!(arg_operation & (ACTION_REPLACE | ACTION_DELETE |
3357c478bd9Sstevel@tonic-gate 	    ACTION_DISABLE | ACTION_ENABLE)))) {
3367c478bd9Sstevel@tonic-gate 		arg_operation |= ACTION_SET;
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 	/* operations require -n */
3397c478bd9Sstevel@tonic-gate 	if (arg_operation && (arg_name == NULL)) {
3407c478bd9Sstevel@tonic-gate 		warn(gettext("-n is required with -s, -r, -x, -e, or -d"));
3417c478bd9Sstevel@tonic-gate 		errflg = 1;
3427c478bd9Sstevel@tonic-gate 		goto done_parse;
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	/* enable and disable are exclusive */
3457c478bd9Sstevel@tonic-gate 	if ((arg_operation & ACTION_ENABLE) &&
3467c478bd9Sstevel@tonic-gate 	    (arg_operation & ACTION_DISABLE)) {
3477c478bd9Sstevel@tonic-gate 		warn(gettext("options -d and -e are exclusive"));
3487c478bd9Sstevel@tonic-gate 		errflg = 1;
3497c478bd9Sstevel@tonic-gate 		goto done_parse;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 	/* -s, -r, and -x are exclusive */
3527c478bd9Sstevel@tonic-gate 	flags = arg_operation &
3537c478bd9Sstevel@tonic-gate 	    (ACTION_REPLACE | ACTION_SET | ACTION_DELETE);
3547c478bd9Sstevel@tonic-gate 	if (flags & (flags - 1)) {
3557c478bd9Sstevel@tonic-gate 		warn(gettext("options -s, -r, and -x are exclusive"));
3567c478bd9Sstevel@tonic-gate 		errflg = 1;
3577c478bd9Sstevel@tonic-gate 		goto done_parse;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 	/* -e or -d makes no sense with -x */
3607c478bd9Sstevel@tonic-gate 	if ((arg_operation & ACTION_DELETE) &
3617c478bd9Sstevel@tonic-gate 	    (arg_operation & (ACTION_ENABLE | ACTION_DISABLE))) {
3627c478bd9Sstevel@tonic-gate 		warn(gettext("options -e or -d  not allowed with -x"));
3637c478bd9Sstevel@tonic-gate 		errflg = 1;
3647c478bd9Sstevel@tonic-gate 		goto done_parse;
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	/* if -r is specified -v must be as well */
3677c478bd9Sstevel@tonic-gate 	if ((arg_operation & ACTION_REPLACE) && (!arg_valuestring)) {
3687c478bd9Sstevel@tonic-gate 		warn(gettext("option -r requires use of option -v"));
3697c478bd9Sstevel@tonic-gate 		errflg = 1;
3707c478bd9Sstevel@tonic-gate 		goto done_parse;
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	/* if -s is specified -v must be as well */
3737c478bd9Sstevel@tonic-gate 	if ((arg_operation & ACTION_SET) && (!arg_valuestring)) {
3747c478bd9Sstevel@tonic-gate 		warn(gettext("option -s requires use of option -v"));
3757c478bd9Sstevel@tonic-gate 		errflg = 1;
3767c478bd9Sstevel@tonic-gate 		goto done_parse;
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 	/* Specifying a recipient pid on a non-basic rctl makes no sense */
3797c478bd9Sstevel@tonic-gate 	if (arg_pid != -1 && arg_priv > RCPRIV_BASIC) {
3807c478bd9Sstevel@tonic-gate 		warn(gettext("option -p not allowed on non-basic rctl"));
3817c478bd9Sstevel@tonic-gate 		errflg = 1;
3827c478bd9Sstevel@tonic-gate 		goto done_parse;
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 	/* Specifying a recipient pid on a privileged rctl makes no sense */
3857c478bd9Sstevel@tonic-gate 	if (arg_pid != -1 &&
3867c478bd9Sstevel@tonic-gate 	    arg_priv == RCPRIV_PRIVILEGED) {
3877c478bd9Sstevel@tonic-gate 		warn(gettext("option -p not allowed with privileged rctl"));
3887c478bd9Sstevel@tonic-gate 		errflg = 1;
3897c478bd9Sstevel@tonic-gate 		goto done_parse;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 	if (arg_operation) {
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		/* do additional checks if there is an operation */
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		if (arg_parseable_mode == 1) {
3967c478bd9Sstevel@tonic-gate 			warn(gettext("-P not valid when manipulating "
3977c478bd9Sstevel@tonic-gate 			    "resource control values"));
3987c478bd9Sstevel@tonic-gate 			errflg = 1;
3997c478bd9Sstevel@tonic-gate 			goto done_parse;
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 		/* get rctl global flags to determine if actions are valid */
4027c478bd9Sstevel@tonic-gate 		if ((rctlblkA = calloc(1, rctlblk_size())) == NULL) {
4037c478bd9Sstevel@tonic-gate 			warn(gettext("malloc failed: %s"),
4047c478bd9Sstevel@tonic-gate 			    strerror(errno));
4057c478bd9Sstevel@tonic-gate 			errflg = 1;
4067c478bd9Sstevel@tonic-gate 			goto done_parse;
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 		if ((rctlblkB = calloc(1, rctlblk_size())) == NULL) {
4097c478bd9Sstevel@tonic-gate 			warn(gettext("malloc failed: %s"),
4107c478bd9Sstevel@tonic-gate 			    strerror(errno));
4117c478bd9Sstevel@tonic-gate 			errflg = 1;
4127c478bd9Sstevel@tonic-gate 			goto done_parse;
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 		/* get system rctl to get global flags and max value */
4157c478bd9Sstevel@tonic-gate 		if (getrctl(arg_name, NULL, rctlblkA, RCTL_FIRST)) {
4167c478bd9Sstevel@tonic-gate 			warn(gettext("failed to get resource control "
4177c478bd9Sstevel@tonic-gate 			    "for %s: %s"), arg_name, strerror(errno));
4187c478bd9Sstevel@tonic-gate 			errflg = 1;
4197c478bd9Sstevel@tonic-gate 			goto done_parse;
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 		while (getrctl(arg_name, rctlblkA, rctlblkB, RCTL_NEXT) == 0) {
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 			/* allow user interrupt */
4247c478bd9Sstevel@tonic-gate 			if (interrupt) {
4257c478bd9Sstevel@tonic-gate 				errflg = 1;
4267c478bd9Sstevel@tonic-gate 				goto done_parse;
4277c478bd9Sstevel@tonic-gate 			}
4287c478bd9Sstevel@tonic-gate 			tmp = rctlblkB;
4297c478bd9Sstevel@tonic-gate 			rctlblkB = rctlblkA;
4307c478bd9Sstevel@tonic-gate 			rctlblkA = tmp;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 			if (rctlblk_get_privilege(rctlblkA) ==
4337c478bd9Sstevel@tonic-gate 			    RCPRIV_SYSTEM) {
4347c478bd9Sstevel@tonic-gate 				break;
4357c478bd9Sstevel@tonic-gate 			}
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 		if (rctlblk_get_privilege(rctlblkA) != RCPRIV_SYSTEM) {
4387c478bd9Sstevel@tonic-gate 			warn(gettext("failed to get system resource control "
4397c478bd9Sstevel@tonic-gate 			    "for %s: %s"), arg_name, strerror(errno));
4407c478bd9Sstevel@tonic-gate 			errflg = 1;
4417c478bd9Sstevel@tonic-gate 			goto done_parse;
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 		/* figure out the correct scale and unit for this rctl */
4447c478bd9Sstevel@tonic-gate 		arg_global_flags = rctlblk_get_global_flags(rctlblkA);
4457c478bd9Sstevel@tonic-gate 		arg_global_max = rctlblk_get_value(rctlblkA);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		if (arg_global_flags & RCTL_GLOBAL_BYTES) {
4487c478bd9Sstevel@tonic-gate 			arg_unit = SCALED_UNIT_BYTES;
4497c478bd9Sstevel@tonic-gate 			arg_scale = scale_binary;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		} else if (arg_global_flags & RCTL_GLOBAL_SECONDS) {
4527c478bd9Sstevel@tonic-gate 			arg_unit = SCALED_UNIT_SECONDS;
4537c478bd9Sstevel@tonic-gate 			arg_scale = scale_metric;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		} else {
4567c478bd9Sstevel@tonic-gate 			arg_unit = SCALED_UNIT_NONE;
4577c478bd9Sstevel@tonic-gate 			arg_scale = scale_metric;
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 		/* parse -v value string */
4607c478bd9Sstevel@tonic-gate 		if (arg_valuestring) {
4617c478bd9Sstevel@tonic-gate 			if (scaledtouint64(arg_valuestring,
4627c478bd9Sstevel@tonic-gate 			    &arg_value, NULL, &arg_modifier, NULL,
4637c478bd9Sstevel@tonic-gate 			    arg_scale, arg_unit,
4647c478bd9Sstevel@tonic-gate 			    SCALED_ALL_FLAGS)) {
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 				warn(gettext("invalid -v value %s"),
4677c478bd9Sstevel@tonic-gate 				    arg_valuestring);
4687c478bd9Sstevel@tonic-gate 				errflg = 1;
4697c478bd9Sstevel@tonic-gate 				goto done_parse;
4707c478bd9Sstevel@tonic-gate 			}
4717c478bd9Sstevel@tonic-gate 			if (arg_value > arg_global_max) {
4727c478bd9Sstevel@tonic-gate 				warn(gettext("-v value %s exceeds system "
4737c478bd9Sstevel@tonic-gate 				    "limit for resource control: %s"),
4747c478bd9Sstevel@tonic-gate 				    arg_valuestring, arg_name);
4757c478bd9Sstevel@tonic-gate 				errflg = 1;
4767c478bd9Sstevel@tonic-gate 				goto done_parse;
4777c478bd9Sstevel@tonic-gate 			}
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 		/* parse action */
4807c478bd9Sstevel@tonic-gate 		if (arg_action_string) {
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 			char *sigchr;
4837c478bd9Sstevel@tonic-gate 			char *iter;
4847c478bd9Sstevel@tonic-gate 
485*c3ea2840SMenno Lageman 			if ((strcmp(arg_action_string, "signal") == 0) ||
486*c3ea2840SMenno Lageman 			    (strcmp(arg_action_string, "sig") == 0)) {
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 				if (arg_operation & ACTION_ENABLE) {
4897c478bd9Sstevel@tonic-gate 					warn(gettext(
4907c478bd9Sstevel@tonic-gate 					    "signal name or number must be "
4917c478bd9Sstevel@tonic-gate 					    "specified with -e"));
4927c478bd9Sstevel@tonic-gate 					errflg = 1;
4937c478bd9Sstevel@tonic-gate 					goto done_parse;
4947c478bd9Sstevel@tonic-gate 				}
4957c478bd9Sstevel@tonic-gate 				arg_action = RCTL_LOCAL_SIGNAL;
4967c478bd9Sstevel@tonic-gate 				arg_signal = -1;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 			} else if ((strncmp(arg_action_string,
4997c478bd9Sstevel@tonic-gate 			    "signal=", strlen("signal=")) == 0) ||
5007c478bd9Sstevel@tonic-gate 			    (strncmp(arg_action_string,
5017c478bd9Sstevel@tonic-gate 			    "sig=", strlen("sig=")) == 0)) {
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 				arg_action = RCTL_LOCAL_SIGNAL;
5047c478bd9Sstevel@tonic-gate 				sigchr = strrchr(arg_action_string, '=');
5057c478bd9Sstevel@tonic-gate 				sigchr++;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 				iter = sigchr;
5087c478bd9Sstevel@tonic-gate 				while (*iter) {
5097c478bd9Sstevel@tonic-gate 					*iter = toupper(*iter);
5107c478bd9Sstevel@tonic-gate 					iter++;
5117c478bd9Sstevel@tonic-gate 				}
5127c478bd9Sstevel@tonic-gate 				if (strncmp("SIG", sigchr, 3) == 0)
5137c478bd9Sstevel@tonic-gate 					sigchr += 3;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 				if (str2sig(sigchr, &arg_signal) != 0) {
5177c478bd9Sstevel@tonic-gate 					warn(gettext("signal invalid"));
5187c478bd9Sstevel@tonic-gate 					errflg = 1;
5197c478bd9Sstevel@tonic-gate 					goto done_parse;
5207c478bd9Sstevel@tonic-gate 				}
5217c478bd9Sstevel@tonic-gate 			} else if (strcmp(arg_action_string, "deny") == 0) {
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 				arg_action = RCTL_LOCAL_DENY;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 			} else if (strcmp(arg_action_string, "all") == 0) {
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 				if (arg_operation & ACTION_ENABLE) {
5287c478bd9Sstevel@tonic-gate 					warn(gettext(
5297c478bd9Sstevel@tonic-gate 					    "cannot use action 'all' with -e"));
5307c478bd9Sstevel@tonic-gate 					errflg = 1;
5317c478bd9Sstevel@tonic-gate 					goto done_parse;
5327c478bd9Sstevel@tonic-gate 				}
5337c478bd9Sstevel@tonic-gate 				arg_action = RCTL_LOCAL_DENY |
5347c478bd9Sstevel@tonic-gate 				    RCTL_LOCAL_SIGNAL;
5357c478bd9Sstevel@tonic-gate 				arg_signal = -1;
5367c478bd9Sstevel@tonic-gate 				goto done_parse;
5377c478bd9Sstevel@tonic-gate 			} else {
5387c478bd9Sstevel@tonic-gate 				warn(gettext("action invalid"));
5397c478bd9Sstevel@tonic-gate 				errflg = 1;
5407c478bd9Sstevel@tonic-gate 				goto done_parse;
5417c478bd9Sstevel@tonic-gate 			}
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		/* cannot manipulate system rctls */
5447c478bd9Sstevel@tonic-gate 		if (arg_priv == RCPRIV_SYSTEM) {
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 			warn(gettext("cannot modify system values"));
5477c478bd9Sstevel@tonic-gate 			errflg = 1;
5487c478bd9Sstevel@tonic-gate 			goto done_parse;
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 		/* validate that the privilege is allowed */
5517c478bd9Sstevel@tonic-gate 		if ((arg_priv == RCPRIV_BASIC) &&
5527c478bd9Sstevel@tonic-gate 		    (arg_global_flags & RCTL_GLOBAL_NOBASIC)) {
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 			warn(gettext("basic values not allowed on rctl %s"),
5557c478bd9Sstevel@tonic-gate 			    arg_name);
5567c478bd9Sstevel@tonic-gate 			errflg = 1;
5577c478bd9Sstevel@tonic-gate 			goto done_parse;
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 		/* validate that actions are appropriate for given rctl */
5607c478bd9Sstevel@tonic-gate 		if ((arg_operation & ACTION_ENABLE) &&
5617c478bd9Sstevel@tonic-gate 		    (arg_action & RCTL_LOCAL_DENY) &&
5627c478bd9Sstevel@tonic-gate 		    (arg_global_flags & RCTL_GLOBAL_DENY_NEVER)) {
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 			warn(gettext("unable to enable deny on rctl with "
5657c478bd9Sstevel@tonic-gate 			    "global flag 'no-deny'"));
5667c478bd9Sstevel@tonic-gate 			errflg = 1;
5677c478bd9Sstevel@tonic-gate 			goto done_parse;
5687c478bd9Sstevel@tonic-gate 		}
5697c478bd9Sstevel@tonic-gate 		if ((arg_operation & ACTION_DISABLE) &&
5707c478bd9Sstevel@tonic-gate 		    (arg_action & RCTL_LOCAL_DENY) &&
5717c478bd9Sstevel@tonic-gate 		    (arg_global_flags & RCTL_GLOBAL_DENY_ALWAYS)) {
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 			warn(gettext("unable to disable deny on rctl with "
5747c478bd9Sstevel@tonic-gate 			    "global flag 'deny'"));
5757c478bd9Sstevel@tonic-gate 			errflg = 1;
5767c478bd9Sstevel@tonic-gate 			goto done_parse;
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 		if ((arg_operation & ACTION_ENABLE) &&
5797c478bd9Sstevel@tonic-gate 		    (arg_action & RCTL_LOCAL_SIGNAL) &&
5807c478bd9Sstevel@tonic-gate 		    (arg_global_flags & RCTL_GLOBAL_SIGNAL_NEVER)) {
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 			warn(gettext("unable to enable signal on rctl with "
5837c478bd9Sstevel@tonic-gate 			    "global flag 'no-signal'"));
5847c478bd9Sstevel@tonic-gate 			errflg = 1;
5857c478bd9Sstevel@tonic-gate 			goto done_parse;
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 		/* now set defaults for options not supplied */
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		/*
5907c478bd9Sstevel@tonic-gate 		 * default privilege to basic if this is a seting an rctl
5917c478bd9Sstevel@tonic-gate 		 * operation
5927c478bd9Sstevel@tonic-gate 		 */
5937c478bd9Sstevel@tonic-gate 		if (arg_operation & ACTION_SET) {
5947c478bd9Sstevel@tonic-gate 			if (arg_priv == 0) {
5957c478bd9Sstevel@tonic-gate 				arg_priv = RCPRIV_BASIC;
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 		/*
5997c478bd9Sstevel@tonic-gate 		 * -p is required when set a basic task,
6007c478bd9Sstevel@tonic-gate 		 * project or zone rctl
6017c478bd9Sstevel@tonic-gate 		 */
6027c478bd9Sstevel@tonic-gate 		if ((arg_pid == -1) &&
6037c478bd9Sstevel@tonic-gate 		    (arg_priv == RCPRIV_BASIC) &&
6047c478bd9Sstevel@tonic-gate 		    (arg_entity_type != RCENTITY_PROCESS) &&
6057c478bd9Sstevel@tonic-gate 		    (arg_operation & ACTION_SET) &&
6067c478bd9Sstevel@tonic-gate 		    (arg_name) &&
6077c478bd9Sstevel@tonic-gate 		    (arg_name_entity == RCENTITY_TASK ||
6087c478bd9Sstevel@tonic-gate 		    arg_name_entity == RCENTITY_PROJECT ||
6097c478bd9Sstevel@tonic-gate 		    arg_name_entity == RCENTITY_ZONE)) {
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 			warn(gettext("-p pid required when setting or "
6127c478bd9Sstevel@tonic-gate 			    "replacing task or project rctl"));
6137c478bd9Sstevel@tonic-gate 			errflg = 1;
6147c478bd9Sstevel@tonic-gate 			goto done_parse;
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 	} else {
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 		/* validate for list mode */
6197c478bd9Sstevel@tonic-gate 		/* -p is not valid in list mode */
6207c478bd9Sstevel@tonic-gate 		if (arg_pid != -1) {
6217c478bd9Sstevel@tonic-gate 			warn(gettext("-p pid requires -s, -r, -x, -e, or -d"));
6227c478bd9Sstevel@tonic-gate 			errflg = 1;
6237c478bd9Sstevel@tonic-gate 			goto done_parse;
6247c478bd9Sstevel@tonic-gate 		}
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 	/* getting/setting process rctl on task or project is error */
6277c478bd9Sstevel@tonic-gate 	if ((arg_name && (arg_name_entity == RCENTITY_PROCESS)) &&
6287c478bd9Sstevel@tonic-gate 	    ((arg_entity_type == RCENTITY_TASK) ||
6297c478bd9Sstevel@tonic-gate 	    (arg_entity_type == RCENTITY_PROJECT))) {
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 		warn(gettext("cannot get/set process rctl on task "
6327c478bd9Sstevel@tonic-gate 		    "or project"));
6337c478bd9Sstevel@tonic-gate 		errflg = 1;
6347c478bd9Sstevel@tonic-gate 		goto done_parse;
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 	/* getting/setting task rctl on project is error */
6377c478bd9Sstevel@tonic-gate 	if ((arg_name && (arg_name_entity == RCENTITY_TASK)) &&
6387c478bd9Sstevel@tonic-gate 	    (arg_entity_type == RCENTITY_PROJECT)) {
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		warn(gettext("cannot get/set task rctl on project"));
6417c478bd9Sstevel@tonic-gate 		errflg = 1;
6427c478bd9Sstevel@tonic-gate 		goto done_parse;
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate done_parse:
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/* free any rctlblk's that we may have allocated */
6487c478bd9Sstevel@tonic-gate 	if (rctlblkA) {
6497c478bd9Sstevel@tonic-gate 		free(rctlblkA);
6507c478bd9Sstevel@tonic-gate 		rctlblkA = NULL;
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate 	if (rctlblkB) {
6537c478bd9Sstevel@tonic-gate 		free(rctlblkB);
6547c478bd9Sstevel@tonic-gate 		rctlblkB = NULL;
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 	if (errflg)
6577c478bd9Sstevel@tonic-gate 		usage();
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	/* catch signals from terminal */
6607c478bd9Sstevel@tonic-gate 	if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
6617c478bd9Sstevel@tonic-gate 		(void) sigset(SIGHUP, intr);
6627c478bd9Sstevel@tonic-gate 	if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
6637c478bd9Sstevel@tonic-gate 		(void) sigset(SIGINT, intr);
6647c478bd9Sstevel@tonic-gate 	if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
6657c478bd9Sstevel@tonic-gate 		(void) sigset(SIGQUIT, intr);
6667c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, intr);
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	while (--argc >= 0 && !interrupt) {
6697c478bd9Sstevel@tonic-gate 		pr_info_handle_t p;
6707c478bd9Sstevel@tonic-gate 		char *arg = *argv++;
6717c478bd9Sstevel@tonic-gate 		int intarg;
6727c478bd9Sstevel@tonic-gate 		char *end;
6737c478bd9Sstevel@tonic-gate 		errflg = 0;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		gret = 0;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		/* Store int version of arg */
6787c478bd9Sstevel@tonic-gate 		errno = 0;
6797c478bd9Sstevel@tonic-gate 		intarg = strtoul(arg, &end, 10);
6807c478bd9Sstevel@tonic-gate 		if (errno || *end != '\0' || end == arg) {
6817c478bd9Sstevel@tonic-gate 			intarg = -1;
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		/*
6857c478bd9Sstevel@tonic-gate 		 * -p defaults to arg if basic and collective rctl
6867c478bd9Sstevel@tonic-gate 		 * and -i process is specified
6877c478bd9Sstevel@tonic-gate 		 */
6887c478bd9Sstevel@tonic-gate 		if ((arg_pid == -1) &&
6897c478bd9Sstevel@tonic-gate 		    (arg_priv == RCPRIV_BASIC) &&
6907c478bd9Sstevel@tonic-gate 		    (arg_entity_type == RCENTITY_PROCESS) &&
6917c478bd9Sstevel@tonic-gate 		    (arg_name) &&
6927c478bd9Sstevel@tonic-gate 		    (arg_name_entity == RCENTITY_TASK ||
6937c478bd9Sstevel@tonic-gate 		    arg_name_entity == RCENTITY_PROJECT)) {
6947c478bd9Sstevel@tonic-gate 			arg_pid_string = arg;
6957c478bd9Sstevel@tonic-gate 			errno = 0;
6967c478bd9Sstevel@tonic-gate 			arg_pid = intarg;
6977c478bd9Sstevel@tonic-gate 		}
6987c478bd9Sstevel@tonic-gate 		/* Specifying a recipient pid and -i pid is redundent */
6997c478bd9Sstevel@tonic-gate 		if (arg_pid != -1 && arg_entity_type == RCENTITY_PROCESS &&
7007c478bd9Sstevel@tonic-gate 		    arg_pid != intarg) {
7017c478bd9Sstevel@tonic-gate 			warn(gettext("option -p pid must match -i process"));
7027c478bd9Sstevel@tonic-gate 			errflg = 1;
7037c478bd9Sstevel@tonic-gate 			continue;
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 		/* use recipient pid if we have one */
7067c478bd9Sstevel@tonic-gate 		if (arg_pid_string != NULL) {
7077c478bd9Sstevel@tonic-gate 			target_id = arg_pid_string;
7087c478bd9Sstevel@tonic-gate 			search_type = RCENTITY_PROCESS;
7097c478bd9Sstevel@tonic-gate 		} else {
7107c478bd9Sstevel@tonic-gate 			target_id = arg;
7117c478bd9Sstevel@tonic-gate 			search_type = arg_entity_type;
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);	/* process-at-a-time */
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 		if (arg_operation != 0) {
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 			if ((pid = grab_process_by_id(target_id,
7187c478bd9Sstevel@tonic-gate 			    search_type, &p, arg_priv, &gret)) < 0) {
7197c478bd9Sstevel@tonic-gate 				/*
7207c478bd9Sstevel@tonic-gate 				 * Mark that an error occurred so that the
7217c478bd9Sstevel@tonic-gate 				 * return value can be set, but continue
7227c478bd9Sstevel@tonic-gate 				 * on with other processes
7237c478bd9Sstevel@tonic-gate 				 */
7247c478bd9Sstevel@tonic-gate 				errflg = 1;
7257c478bd9Sstevel@tonic-gate 				continue;
7267c478bd9Sstevel@tonic-gate 			}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 			/*
7297c478bd9Sstevel@tonic-gate 			 * At this point, the victim process is held.
7307c478bd9Sstevel@tonic-gate 			 * Do not call any Pgrab-unsafe functions until
7317c478bd9Sstevel@tonic-gate 			 * the process is released via release_process().
7327c478bd9Sstevel@tonic-gate 			 */
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 			errflg = get_rctls(p.pr);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 			if (arg_operation & ACTION_DELETE) {
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 				/* match by privilege, value, and pid */
7397c478bd9Sstevel@tonic-gate 				if (match_rctl(p.pr, &rctlblkA, arg_name,
7407c478bd9Sstevel@tonic-gate 				    arg_valuestring, arg_value, arg_priv,
7417c478bd9Sstevel@tonic-gate 				    arg_pid) != 0 || rctlblkA == NULL) {
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 					if (interrupt)
7447c478bd9Sstevel@tonic-gate 						goto out;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 					preserve_error(gettext("no matching "
7477c478bd9Sstevel@tonic-gate 					    "resource control found for "
7487c478bd9Sstevel@tonic-gate 					    "deletion"));
7497c478bd9Sstevel@tonic-gate 					errflg = 1;
7507c478bd9Sstevel@tonic-gate 					goto out;
7517c478bd9Sstevel@tonic-gate 				}
7527c478bd9Sstevel@tonic-gate 				/*
7537c478bd9Sstevel@tonic-gate 				 * grab correct process.  This is neccessary
7547c478bd9Sstevel@tonic-gate 				 * if the recipient pid does not match the
7557c478bd9Sstevel@tonic-gate 				 * one we grabbed
7567c478bd9Sstevel@tonic-gate 				 */
7577c478bd9Sstevel@tonic-gate 				pid = regrab_process(
7587c478bd9Sstevel@tonic-gate 				    rctlblk_get_recipient_pid(rctlblkA),
7597c478bd9Sstevel@tonic-gate 				    &p, arg_priv, &gret);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 				if (pid < 0) {
7627c478bd9Sstevel@tonic-gate 					errflg = 1;
7637c478bd9Sstevel@tonic-gate 					goto out;
7647c478bd9Sstevel@tonic-gate 				}
7657c478bd9Sstevel@tonic-gate 				if (prctl_setrctl(p.pr, arg_name, NULL,
7667c478bd9Sstevel@tonic-gate 				    rctlblkA, RCTL_DELETE) != 0) {
7677c478bd9Sstevel@tonic-gate 					errflg = 1;
7687c478bd9Sstevel@tonic-gate 					goto out;
7697c478bd9Sstevel@tonic-gate 				}
7707c478bd9Sstevel@tonic-gate 			} else if (arg_operation & ACTION_SET) {
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 				/* match by privilege, value, and pid */
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 				if (match_rctl(p.pr, &rctlblkA, arg_name,
7757c478bd9Sstevel@tonic-gate 				    arg_valuestring, arg_value, arg_priv,
7767c478bd9Sstevel@tonic-gate 				    arg_pid) == 0) {
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 					if (interrupt)
7797c478bd9Sstevel@tonic-gate 						goto out;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 					preserve_error(gettext("resource "
7827c478bd9Sstevel@tonic-gate 					    "control already exists"));
7837c478bd9Sstevel@tonic-gate 					errflg = 1;
7847c478bd9Sstevel@tonic-gate 					goto out;
7857c478bd9Sstevel@tonic-gate 				}
7867c478bd9Sstevel@tonic-gate 				rctlblkB = calloc(1, rctlblk_size());
7877c478bd9Sstevel@tonic-gate 				if (rctlblkB == NULL) {
7887c478bd9Sstevel@tonic-gate 					preserve_error(gettext(
7897c478bd9Sstevel@tonic-gate 					    "malloc failed"), strerror(errno));
7907c478bd9Sstevel@tonic-gate 					errflg = 1;
7917c478bd9Sstevel@tonic-gate 					goto out;
7927c478bd9Sstevel@tonic-gate 				}
7937c478bd9Sstevel@tonic-gate 				rctlblk_set_value(rctlblkB, arg_value);
7947c478bd9Sstevel@tonic-gate 				rctlblk_set_privilege(rctlblkB, arg_priv);
7957c478bd9Sstevel@tonic-gate 				if (change_action(rctlblkB)) {
7967c478bd9Sstevel@tonic-gate 					errflg = 1;
7977c478bd9Sstevel@tonic-gate 					goto out;
7987c478bd9Sstevel@tonic-gate 				}
7997c478bd9Sstevel@tonic-gate 				if (prctl_setrctl(p.pr, arg_name, NULL,
8007c478bd9Sstevel@tonic-gate 				    rctlblkB, RCTL_INSERT) != 0) {
8017c478bd9Sstevel@tonic-gate 					errflg = 1;
8027c478bd9Sstevel@tonic-gate 					goto out;
8037c478bd9Sstevel@tonic-gate 				}
8047c478bd9Sstevel@tonic-gate 			} else if (arg_operation & ACTION_REPLACE) {
8057c478bd9Sstevel@tonic-gate 				/*
8067c478bd9Sstevel@tonic-gate 				 * match rctl for deletion by privilege and
8077c478bd9Sstevel@tonic-gate 				 * pid only
8087c478bd9Sstevel@tonic-gate 				 */
8097c478bd9Sstevel@tonic-gate 				if (match_rctl(p.pr, &rctlblkA, arg_name,
8107c478bd9Sstevel@tonic-gate 				    NULL, 0, arg_priv,
8117c478bd9Sstevel@tonic-gate 				    arg_pid) != 0 || rctlblkA == NULL) {
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 					if (interrupt)
8147c478bd9Sstevel@tonic-gate 						goto out;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 					preserve_error(gettext("no matching "
8177c478bd9Sstevel@tonic-gate 					    "resource control to replace"));
8187c478bd9Sstevel@tonic-gate 					errflg = 1;
8197c478bd9Sstevel@tonic-gate 					goto out;
8207c478bd9Sstevel@tonic-gate 				}
8217c478bd9Sstevel@tonic-gate 				/*
8227c478bd9Sstevel@tonic-gate 				 * grab correct process.  This is neccessary
8237c478bd9Sstevel@tonic-gate 				 * if the recipient pid does not match the
8247c478bd9Sstevel@tonic-gate 				 * one we grabbed
8257c478bd9Sstevel@tonic-gate 				 */
8267c478bd9Sstevel@tonic-gate 				pid = regrab_process(
8277c478bd9Sstevel@tonic-gate 				    rctlblk_get_recipient_pid(rctlblkA),
8287c478bd9Sstevel@tonic-gate 				    &p, arg_priv, &gret);
8297c478bd9Sstevel@tonic-gate 				if (pid < 0) {
8307c478bd9Sstevel@tonic-gate 					errflg = 1;
8317c478bd9Sstevel@tonic-gate 					goto out;
8327c478bd9Sstevel@tonic-gate 				}
8337c478bd9Sstevel@tonic-gate 				pid = rctlblk_get_recipient_pid(rctlblkA);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 				/*
8367c478bd9Sstevel@tonic-gate 				 * match by privilege, value and pid to
8377c478bd9Sstevel@tonic-gate 				 * check if new rctl  already exists
8387c478bd9Sstevel@tonic-gate 				 */
8397c478bd9Sstevel@tonic-gate 				if (match_rctl(p.pr, &rctlblkB, arg_name,
8407c478bd9Sstevel@tonic-gate 				    arg_valuestring, arg_value, arg_priv,
8417c478bd9Sstevel@tonic-gate 				    pid) < 0) {
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 					if (interrupt)
8447c478bd9Sstevel@tonic-gate 						goto out;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 					preserve_error(gettext(
8477c478bd9Sstevel@tonic-gate 					    "Internal Error"));
8487c478bd9Sstevel@tonic-gate 					errflg = 1;
8497c478bd9Sstevel@tonic-gate 					goto out;
8507c478bd9Sstevel@tonic-gate 				}
8517c478bd9Sstevel@tonic-gate 				/*
8527c478bd9Sstevel@tonic-gate 				 * If rctl already exists, and it does not
8537c478bd9Sstevel@tonic-gate 				 *  match the one that we will delete, than
8547c478bd9Sstevel@tonic-gate 				 *  the replace will fail.
8557c478bd9Sstevel@tonic-gate 				 */
8567c478bd9Sstevel@tonic-gate 				if (rctlblkB != NULL &&
857*c3ea2840SMenno Lageman 				    arg_value != rctlblk_get_value(rctlblkA)) {
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 					preserve_error(gettext("replacement "
8607c478bd9Sstevel@tonic-gate 					    "resource control already "
8617c478bd9Sstevel@tonic-gate 					    "exists"));
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 					errflg = 1;
8647c478bd9Sstevel@tonic-gate 					goto out;
8657c478bd9Sstevel@tonic-gate 				}
8667c478bd9Sstevel@tonic-gate 				/* create new rctl */
8677c478bd9Sstevel@tonic-gate 				rctlblkB = calloc(1, rctlblk_size());
8687c478bd9Sstevel@tonic-gate 				if (rctlblkB == NULL) {
8697c478bd9Sstevel@tonic-gate 					preserve_error(gettext(
8707c478bd9Sstevel@tonic-gate 					    "malloc failed"), strerror(errno));
8717c478bd9Sstevel@tonic-gate 					errflg = 1;
8727c478bd9Sstevel@tonic-gate 					goto out;
8737c478bd9Sstevel@tonic-gate 				}
874*c3ea2840SMenno Lageman 				localaction =
875*c3ea2840SMenno Lageman 				    rctlblk_get_local_action(rctlblkA, &signal);
876*c3ea2840SMenno Lageman 				rctlblk_set_local_action(rctlblkB, localaction,
877*c3ea2840SMenno Lageman 				    signal);
8787c478bd9Sstevel@tonic-gate 				rctlblk_set_value(rctlblkB, arg_value);
8797c478bd9Sstevel@tonic-gate 				rctlblk_set_privilege(rctlblkB,
8807c478bd9Sstevel@tonic-gate 				    rctlblk_get_privilege(rctlblkA));
8817c478bd9Sstevel@tonic-gate 				if (change_action(rctlblkB)) {
8827c478bd9Sstevel@tonic-gate 					errflg = 1;
8837c478bd9Sstevel@tonic-gate 					goto out;
8847c478bd9Sstevel@tonic-gate 				}
8857c478bd9Sstevel@tonic-gate 				/* do replacement */
8867c478bd9Sstevel@tonic-gate 				if (prctl_setrctl(p.pr, arg_name, rctlblkA,
8877c478bd9Sstevel@tonic-gate 				    rctlblkB, RCTL_REPLACE) != 0) {
8887c478bd9Sstevel@tonic-gate 					errflg = 1;
8897c478bd9Sstevel@tonic-gate 					goto out;
8907c478bd9Sstevel@tonic-gate 				}
8917c478bd9Sstevel@tonic-gate 			} else if (arg_operation &
8927c478bd9Sstevel@tonic-gate 			    (ACTION_ENABLE | ACTION_DISABLE)) {
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 				rctlblkB = calloc(1, rctlblk_size());
8957c478bd9Sstevel@tonic-gate 				if (rctlblkB == NULL) {
8967c478bd9Sstevel@tonic-gate 					preserve_error(gettext(
8977c478bd9Sstevel@tonic-gate 					    "malloc failed"), strerror(errno));
8987c478bd9Sstevel@tonic-gate 					errflg = 1;
8997c478bd9Sstevel@tonic-gate 					goto out;
9007c478bd9Sstevel@tonic-gate 				}
9017c478bd9Sstevel@tonic-gate 				/* match by privilege, value, and pid */
9027c478bd9Sstevel@tonic-gate 				if (match_rctl(p.pr, &rctlblkA, arg_name,
9037c478bd9Sstevel@tonic-gate 				    arg_valuestring, arg_value, arg_priv,
9047c478bd9Sstevel@tonic-gate 				    arg_pid) != 0) {
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 					if (interrupt)
9077c478bd9Sstevel@tonic-gate 						goto out;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 					/* if no match, just set new rctl */
9107c478bd9Sstevel@tonic-gate 					if (arg_priv == 0)
9117c478bd9Sstevel@tonic-gate 						arg_priv = RCPRIV_BASIC;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 					if ((arg_priv == RCPRIV_BASIC) &&
9147c478bd9Sstevel@tonic-gate 					    (arg_entity_type !=
9157c478bd9Sstevel@tonic-gate 					    RCENTITY_PROCESS) &&
9167c478bd9Sstevel@tonic-gate 					    (arg_pid_string == NULL)) {
9177c478bd9Sstevel@tonic-gate 						preserve_error(gettext(
9187c478bd9Sstevel@tonic-gate 						    "-p required when setting "
9197c478bd9Sstevel@tonic-gate 						    "basic rctls"));
9207c478bd9Sstevel@tonic-gate 						errflg = 1;
9217c478bd9Sstevel@tonic-gate 						goto out;
9227c478bd9Sstevel@tonic-gate 					}
9237c478bd9Sstevel@tonic-gate 					rctlblk_set_value(rctlblkB,
9247c478bd9Sstevel@tonic-gate 					    arg_value);
9257c478bd9Sstevel@tonic-gate 					rctlblk_set_privilege(
9267c478bd9Sstevel@tonic-gate 					    rctlblkB, arg_priv);
9277c478bd9Sstevel@tonic-gate 					if (change_action(rctlblkB)) {
9287c478bd9Sstevel@tonic-gate 						errflg = 1;
9297c478bd9Sstevel@tonic-gate 						goto out;
9307c478bd9Sstevel@tonic-gate 					}
9317c478bd9Sstevel@tonic-gate 					if (prctl_setrctl(p.pr,
9327c478bd9Sstevel@tonic-gate 					    arg_name, NULL, rctlblkB,
9337c478bd9Sstevel@tonic-gate 					    RCTL_INSERT) != 0) {
9347c478bd9Sstevel@tonic-gate 						errflg = 1;
9357c478bd9Sstevel@tonic-gate 						goto out;
9367c478bd9Sstevel@tonic-gate 					}
9377c478bd9Sstevel@tonic-gate 					goto out;
9387c478bd9Sstevel@tonic-gate 				}
9397c478bd9Sstevel@tonic-gate 				if (rctlblkA == NULL) {
9407c478bd9Sstevel@tonic-gate 					preserve_error(gettext("no matching "
9417c478bd9Sstevel@tonic-gate 					    "resource control found"));
9427c478bd9Sstevel@tonic-gate 					errflg = 1;
9437c478bd9Sstevel@tonic-gate 					goto out;
9447c478bd9Sstevel@tonic-gate 				}
9457c478bd9Sstevel@tonic-gate 				/*
9467c478bd9Sstevel@tonic-gate 				 * grab correct process.  This is neccessary
9477c478bd9Sstevel@tonic-gate 				 * if the recipient pid does not match the
9487c478bd9Sstevel@tonic-gate 				 * one we grabbed
9497c478bd9Sstevel@tonic-gate 				 */
9507c478bd9Sstevel@tonic-gate 				pid = regrab_process(
9517c478bd9Sstevel@tonic-gate 				    rctlblk_get_recipient_pid(rctlblkA),
9527c478bd9Sstevel@tonic-gate 				    &p, arg_priv, &gret);
9537c478bd9Sstevel@tonic-gate 				if (pid < 0) {
9547c478bd9Sstevel@tonic-gate 					errflg = 1;
9557c478bd9Sstevel@tonic-gate 					goto out;
9567c478bd9Sstevel@tonic-gate 				}
9577c478bd9Sstevel@tonic-gate 				localaction =
9587c478bd9Sstevel@tonic-gate 				    rctlblk_get_local_action(rctlblkA,
9597c478bd9Sstevel@tonic-gate 				    &signal);
9607c478bd9Sstevel@tonic-gate 				rctlblk_set_local_action(rctlblkB, localaction,
9617c478bd9Sstevel@tonic-gate 				    signal);
9627c478bd9Sstevel@tonic-gate 				rctlblk_set_privilege(rctlblkB,
9637c478bd9Sstevel@tonic-gate 				    rctlblk_get_privilege(rctlblkA));
9647c478bd9Sstevel@tonic-gate 				rctlblk_set_value(rctlblkB,
9657c478bd9Sstevel@tonic-gate 				    rctlblk_get_value(rctlblkA));
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 				if (change_action(rctlblkB)) {
9687c478bd9Sstevel@tonic-gate 					errflg = 1;
9697c478bd9Sstevel@tonic-gate 					goto out;
9707c478bd9Sstevel@tonic-gate 				}
9717c478bd9Sstevel@tonic-gate 				if (prctl_setrctl(p.pr, arg_name, rctlblkA,
9727c478bd9Sstevel@tonic-gate 				    rctlblkB, RCTL_REPLACE) != 0) {
9737c478bd9Sstevel@tonic-gate 					errflg = 1;
9747c478bd9Sstevel@tonic-gate 					goto out;
9757c478bd9Sstevel@tonic-gate 				}
9767c478bd9Sstevel@tonic-gate 			}
9777c478bd9Sstevel@tonic-gate out:
9787c478bd9Sstevel@tonic-gate 			release_process(p.pr);
9797c478bd9Sstevel@tonic-gate 			if (rctlblkA)
9807c478bd9Sstevel@tonic-gate 				free(rctlblkA);
9817c478bd9Sstevel@tonic-gate 			if (rctlblkB)
9827c478bd9Sstevel@tonic-gate 				free(rctlblkB);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 			/* Print any errors that occurred */
9857c478bd9Sstevel@tonic-gate 			if (errflg && *global_error != '\0') {
9867c478bd9Sstevel@tonic-gate 				proc_unctrl_psinfo(&(p.psinfo));
9877c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%d:\t%.70s\n",
9887c478bd9Sstevel@tonic-gate 				    (int)p.pid, p.psinfo.pr_psargs);
9897c478bd9Sstevel@tonic-gate 				warn("%s\n", global_error);
9907c478bd9Sstevel@tonic-gate 				break;
9917c478bd9Sstevel@tonic-gate 			}
9927c478bd9Sstevel@tonic-gate 		} else {
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 			struct project projent;
9957c478bd9Sstevel@tonic-gate 			char buf[PROJECT_BUFSZ];
9967c478bd9Sstevel@tonic-gate 			char zonename[ZONENAME_MAX];
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 			/*
9997c478bd9Sstevel@tonic-gate 			 * Hack to allow the user to specify a system
10007c478bd9Sstevel@tonic-gate 			 * process.
10017c478bd9Sstevel@tonic-gate 			 */
10027c478bd9Sstevel@tonic-gate 			gret = G_SYS;
10037c478bd9Sstevel@tonic-gate 			pid = grab_process_by_id(
10047c478bd9Sstevel@tonic-gate 			    target_id, search_type, &p, RCPRIV_BASIC, &gret);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 			/*
10077c478bd9Sstevel@tonic-gate 			 * Print system process if user chose specifically
10087c478bd9Sstevel@tonic-gate 			 * to inspect a system process.
10097c478bd9Sstevel@tonic-gate 			 */
10107c478bd9Sstevel@tonic-gate 			if (arg_entity_type == RCENTITY_PROCESS &&
10117c478bd9Sstevel@tonic-gate 			    pid < 0 &&
10127c478bd9Sstevel@tonic-gate 			    gret == G_SYS) {
10137c478bd9Sstevel@tonic-gate 				/*
10147c478bd9Sstevel@tonic-gate 				 * Add blank lines between output for
10157c478bd9Sstevel@tonic-gate 				 * operands.
10167c478bd9Sstevel@tonic-gate 				 */
10177c478bd9Sstevel@tonic-gate 				if (printed) {
10187c478bd9Sstevel@tonic-gate 					(void) fprintf(stdout, "\n");
10197c478bd9Sstevel@tonic-gate 				}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 				proc_unctrl_psinfo(&(p.psinfo));
10227c478bd9Sstevel@tonic-gate 				(void) printf(
10237c478bd9Sstevel@tonic-gate 				    "process: %d: %s [ system process ]\n",
10247c478bd9Sstevel@tonic-gate 				    (int)p.pid, p.psinfo.pr_psargs);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 				printed = 1;
10277c478bd9Sstevel@tonic-gate 				continue;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 			} else if (pid < 0) {
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 				/*
10327c478bd9Sstevel@tonic-gate 				 * Mark that an error occurred so that the
10337c478bd9Sstevel@tonic-gate 				 * return value can be set, but continue
10347c478bd9Sstevel@tonic-gate 				 * on with other processes
10357c478bd9Sstevel@tonic-gate 				 */
10367c478bd9Sstevel@tonic-gate 				errflg = 1;
10377c478bd9Sstevel@tonic-gate 				continue;
10387c478bd9Sstevel@tonic-gate 			}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 			errflg = get_rctls(p.pr);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 			release_process(p.pr);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 			/* handle user interrupt of getting rctls */
10457c478bd9Sstevel@tonic-gate 			if (interrupt)
10467c478bd9Sstevel@tonic-gate 				break;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 			/* add blank lines between output for operands */
10497c478bd9Sstevel@tonic-gate 			if (printed) {
10507c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "\n");
10517c478bd9Sstevel@tonic-gate 			}
10527c478bd9Sstevel@tonic-gate 			/* First print any errors */
10537c478bd9Sstevel@tonic-gate 			if (errflg) {
10547c478bd9Sstevel@tonic-gate 				warn("%s\n", global_error);
10557c478bd9Sstevel@tonic-gate 				free_lists();
10567c478bd9Sstevel@tonic-gate 				break;
10577c478bd9Sstevel@tonic-gate 			}
10587c478bd9Sstevel@tonic-gate 			if (getprojbyid(p.projid, &projent, buf,
10597c478bd9Sstevel@tonic-gate 			    sizeof (buf))) {
10607c478bd9Sstevel@tonic-gate 				p.projname = projent.pj_name;
10617c478bd9Sstevel@tonic-gate 			} else {
10627c478bd9Sstevel@tonic-gate 				p.projname = "";
10637c478bd9Sstevel@tonic-gate 			}
10647c478bd9Sstevel@tonic-gate 			if (getzonenamebyid(p.zoneid, zonename,
10657c478bd9Sstevel@tonic-gate 			    sizeof (zonename)) > 0) {
10667c478bd9Sstevel@tonic-gate 				p.zonename = zonename;
10677c478bd9Sstevel@tonic-gate 			} else {
10687c478bd9Sstevel@tonic-gate 				p.zonename = "";
10697c478bd9Sstevel@tonic-gate 			}
10707c478bd9Sstevel@tonic-gate 			print_rctls(&p);
10717c478bd9Sstevel@tonic-gate 			printed = 1;
10727c478bd9Sstevel@tonic-gate 			/* Free the resource control lists */
10737c478bd9Sstevel@tonic-gate 			free_lists();
10747c478bd9Sstevel@tonic-gate 		}
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 	if (interrupt)
10777c478bd9Sstevel@tonic-gate 		errflg = 1;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	/*
10807c478bd9Sstevel@tonic-gate 	 * return error if one occurred
10817c478bd9Sstevel@tonic-gate 	 */
10827c478bd9Sstevel@tonic-gate 	return (errflg);
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate static void
10877c478bd9Sstevel@tonic-gate intr(int sig)
10887c478bd9Sstevel@tonic-gate {
10897c478bd9Sstevel@tonic-gate 	interrupt = sig;
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate /*
10937c478bd9Sstevel@tonic-gate  * get_rctls(struct ps_prochandle *, const char *)
10947c478bd9Sstevel@tonic-gate  *
10957c478bd9Sstevel@tonic-gate  * If controlname is given, store only controls for that named
10967c478bd9Sstevel@tonic-gate  * resource. If controlname is NULL, store all controls for all
10977c478bd9Sstevel@tonic-gate  * resources.
10987c478bd9Sstevel@tonic-gate  *
10997c478bd9Sstevel@tonic-gate  * This function is Pgrab-safe.
11007c478bd9Sstevel@tonic-gate  */
11017c478bd9Sstevel@tonic-gate static int
11027c478bd9Sstevel@tonic-gate get_rctls(struct ps_prochandle *Pr)
11037c478bd9Sstevel@tonic-gate {
11047c478bd9Sstevel@tonic-gate 	int ret = 0;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	if (arg_name == NULL) {
11077c478bd9Sstevel@tonic-gate 		if (rctl_walk(store_rctls, Pr) != 0)
11087c478bd9Sstevel@tonic-gate 			ret = 1;
11097c478bd9Sstevel@tonic-gate 	} else {
11107c478bd9Sstevel@tonic-gate 		ret = store_rctls(arg_name, Pr);
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate 	return (ret);
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate /*
11167c478bd9Sstevel@tonic-gate  * store_rctls(const char *, void *)
11177c478bd9Sstevel@tonic-gate  *
11187c478bd9Sstevel@tonic-gate  * Store resource controls for the given name in a linked list.
11197c478bd9Sstevel@tonic-gate  * Honor the user's options, and store only the ones they are
11207c478bd9Sstevel@tonic-gate  * interested in. If priv is not 0, show only controls that match
11217c478bd9Sstevel@tonic-gate  * the given privilege.
11227c478bd9Sstevel@tonic-gate  *
11237c478bd9Sstevel@tonic-gate  * This function is Pgrab-safe
11247c478bd9Sstevel@tonic-gate  */
11257c478bd9Sstevel@tonic-gate static int
11267c478bd9Sstevel@tonic-gate store_rctls(const char *rctlname, void *walk_data)
11277c478bd9Sstevel@tonic-gate {
11287c478bd9Sstevel@tonic-gate 	struct ps_prochandle *Pr = walk_data;
11297c478bd9Sstevel@tonic-gate 	rctlblk_t *rblk2, *rblk_tmp, *rblk1 = NULL;
11307c478bd9Sstevel@tonic-gate 	prctl_list_t *list = NULL;
11317c478bd9Sstevel@tonic-gate 	rctl_priv_t rblk_priv;
11327c478bd9Sstevel@tonic-gate 	rctl_entity_t rblk_entity;
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	if (((rblk1 = calloc(1, rctlblk_size())) == NULL) ||
11357c478bd9Sstevel@tonic-gate 	    ((rblk2 = calloc(1, rctlblk_size())) == NULL)) {
11367c478bd9Sstevel@tonic-gate 		if (rblk1 != NULL)
11377c478bd9Sstevel@tonic-gate 			free(rblk1);
11387c478bd9Sstevel@tonic-gate 		preserve_error(gettext("malloc failed: %s"),
11397c478bd9Sstevel@tonic-gate 		    strerror(errno));
11407c478bd9Sstevel@tonic-gate 		return (1);
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 	if (pr_getrctl(Pr, rctlname, NULL, rblk1, RCTL_FIRST)) {
11437c478bd9Sstevel@tonic-gate 		preserve_error(gettext("failed to get resource control "
11447c478bd9Sstevel@tonic-gate 		    "for %s: %s"), rctlname, strerror(errno));
11457c478bd9Sstevel@tonic-gate 		free(rblk1);
11467c478bd9Sstevel@tonic-gate 		free(rblk2);
11477c478bd9Sstevel@tonic-gate 		return (1);
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 	/* Store control if it matches privilege and enity type criteria */
11507c478bd9Sstevel@tonic-gate 	rblk_priv = rctlblk_get_privilege(rblk1);
11517c478bd9Sstevel@tonic-gate 	rblk_entity = 0;
11527c478bd9Sstevel@tonic-gate 	if (strncmp(rctlname, "process.",
11537c478bd9Sstevel@tonic-gate 	    strlen("process.")) == 0)
11547c478bd9Sstevel@tonic-gate 		rblk_entity = RCENTITY_PROCESS;
11557c478bd9Sstevel@tonic-gate 	else if (strncmp(rctlname, "project.",
11567c478bd9Sstevel@tonic-gate 	    strlen("project.")) == 0)
11577c478bd9Sstevel@tonic-gate 		rblk_entity = RCENTITY_PROJECT;
11587c478bd9Sstevel@tonic-gate 	else if (strncmp(rctlname, "task.",
11597c478bd9Sstevel@tonic-gate 	    strlen("task.")) == 0)
11607c478bd9Sstevel@tonic-gate 		rblk_entity = RCENTITY_TASK;
11617c478bd9Sstevel@tonic-gate 	else if (strncmp(rctlname, "zone.",
11627c478bd9Sstevel@tonic-gate 	    strlen("zone.")) == 0)
11637c478bd9Sstevel@tonic-gate 		rblk_entity = RCENTITY_ZONE;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	if (((arg_priv == 0) || (rblk_priv == arg_priv)) &&
11667c478bd9Sstevel@tonic-gate 	    ((arg_name == NULL) ||
1167*c3ea2840SMenno Lageman 	    strncmp(rctlname, arg_name, strlen(arg_name)) == 0) &&
1168*c3ea2840SMenno Lageman 	    (arg_entity_string == NULL || rblk_entity >= arg_entity_type)) {
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 		/* Once we know we have some controls, store the name */
11717c478bd9Sstevel@tonic-gate 		if ((list = store_list_entry(rctlname)) == NULL) {
11727c478bd9Sstevel@tonic-gate 			free(rblk1);
11737c478bd9Sstevel@tonic-gate 			free(rblk2);
11747c478bd9Sstevel@tonic-gate 			return (1);
11757c478bd9Sstevel@tonic-gate 		}
11767c478bd9Sstevel@tonic-gate 		if (store_value_entry(rblk1, list) == NULL) {
11777c478bd9Sstevel@tonic-gate 			free(rblk1);
11787c478bd9Sstevel@tonic-gate 			free(rblk2);
11797c478bd9Sstevel@tonic-gate 			return (1);
11807c478bd9Sstevel@tonic-gate 		}
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	while (pr_getrctl(Pr, rctlname, rblk1, rblk2, RCTL_NEXT) == 0) {
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 		/*
11857c478bd9Sstevel@tonic-gate 		 * in case this is stuck for some reason, allow manual
11867c478bd9Sstevel@tonic-gate 		 * interrupt
11877c478bd9Sstevel@tonic-gate 		 */
11887c478bd9Sstevel@tonic-gate 		if (interrupt) {
11897c478bd9Sstevel@tonic-gate 			free(rblk1);
11907c478bd9Sstevel@tonic-gate 			free(rblk2);
11917c478bd9Sstevel@tonic-gate 			return (1);
11927c478bd9Sstevel@tonic-gate 		}
11937c478bd9Sstevel@tonic-gate 		rblk_priv = rctlblk_get_privilege(rblk2);
11947c478bd9Sstevel@tonic-gate 		/*
11957c478bd9Sstevel@tonic-gate 		 * Store control if it matches privilege and entity type
11967c478bd9Sstevel@tonic-gate 		 * criteria
11977c478bd9Sstevel@tonic-gate 		 */
11987c478bd9Sstevel@tonic-gate 		if (((arg_priv == 0) || (rblk_priv == arg_priv)) &&
11997c478bd9Sstevel@tonic-gate 		    ((arg_name == NULL) ||
1200*c3ea2840SMenno Lageman 		    strncmp(rctlname, arg_name, strlen(arg_name)) == 0) &&
12017c478bd9Sstevel@tonic-gate 		    (arg_entity_string == NULL ||
12027c478bd9Sstevel@tonic-gate 		    rblk_entity == arg_entity_type)) {
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 			/* May not have created the list yet. */
12057c478bd9Sstevel@tonic-gate 			if (list == NULL) {
12067c478bd9Sstevel@tonic-gate 				if ((list = store_list_entry(rctlname))
12077c478bd9Sstevel@tonic-gate 				    == NULL) {
12087c478bd9Sstevel@tonic-gate 					free(rblk1);
12097c478bd9Sstevel@tonic-gate 					free(rblk2);
12107c478bd9Sstevel@tonic-gate 					return (1);
12117c478bd9Sstevel@tonic-gate 				}
12127c478bd9Sstevel@tonic-gate 			}
12137c478bd9Sstevel@tonic-gate 			if (store_value_entry(rblk2, list) == NULL) {
12147c478bd9Sstevel@tonic-gate 				free(rblk1);
12157c478bd9Sstevel@tonic-gate 				free(rblk2);
12167c478bd9Sstevel@tonic-gate 				return (1);
12177c478bd9Sstevel@tonic-gate 			}
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 		rblk_tmp = rblk1;
12207c478bd9Sstevel@tonic-gate 		rblk1 = rblk2;
12217c478bd9Sstevel@tonic-gate 		rblk2 = rblk_tmp;
12227c478bd9Sstevel@tonic-gate 	}
12237c478bd9Sstevel@tonic-gate 	free(rblk1);
12247c478bd9Sstevel@tonic-gate 	free(rblk2);
12257c478bd9Sstevel@tonic-gate 	return (0);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate /*
12297c478bd9Sstevel@tonic-gate  * store_value_entry(rctlblk_t *, prctl_list_t *)
12307c478bd9Sstevel@tonic-gate  *
12317c478bd9Sstevel@tonic-gate  * Store an rblk for a given resource control into the global list.
12327c478bd9Sstevel@tonic-gate  *
12337c478bd9Sstevel@tonic-gate  * This function is Pgrab-safe.
12347c478bd9Sstevel@tonic-gate  */
12357c478bd9Sstevel@tonic-gate prctl_value_t *
12367c478bd9Sstevel@tonic-gate store_value_entry(rctlblk_t *rblk, prctl_list_t *list)
12377c478bd9Sstevel@tonic-gate {
12387c478bd9Sstevel@tonic-gate 	prctl_value_t *e = calloc(1, sizeof (prctl_value_t));
12397c478bd9Sstevel@tonic-gate 	rctlblk_t *store_blk = calloc(1, rctlblk_size());
12407c478bd9Sstevel@tonic-gate 	prctl_value_t *iter = list->val_list;
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	if (e == NULL || store_blk == NULL) {
12437c478bd9Sstevel@tonic-gate 		preserve_error(gettext("malloc failed %s"),
12447c478bd9Sstevel@tonic-gate 		    strerror(errno));
12457c478bd9Sstevel@tonic-gate 		if (e != NULL)
12467c478bd9Sstevel@tonic-gate 			free(e);
12477c478bd9Sstevel@tonic-gate 		if (store_blk != NULL)
12487c478bd9Sstevel@tonic-gate 			free(store_blk);
12497c478bd9Sstevel@tonic-gate 		return (NULL);
12507c478bd9Sstevel@tonic-gate 	}
12517c478bd9Sstevel@tonic-gate 	if (iter == NULL)
12527c478bd9Sstevel@tonic-gate 		list->val_list = e;
12537c478bd9Sstevel@tonic-gate 	else {
12547c478bd9Sstevel@tonic-gate 		while (iter->next != NULL) {
12557c478bd9Sstevel@tonic-gate 			iter = iter->next;
12567c478bd9Sstevel@tonic-gate 		}
12577c478bd9Sstevel@tonic-gate 		iter->next = e;
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 	bcopy(rblk, store_blk, rctlblk_size());
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	e->rblk = store_blk;
12627c478bd9Sstevel@tonic-gate 	e->next = NULL;
12637c478bd9Sstevel@tonic-gate 	return (e);
12647c478bd9Sstevel@tonic-gate }
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate /*
12677c478bd9Sstevel@tonic-gate  * store_list_entry(const char *)
12687c478bd9Sstevel@tonic-gate  *
12697c478bd9Sstevel@tonic-gate  * Store a new resource control value in the global list. No checking
12707c478bd9Sstevel@tonic-gate  * for duplicates done.
12717c478bd9Sstevel@tonic-gate  *
12727c478bd9Sstevel@tonic-gate  * This function is Pgrab-safe.
12737c478bd9Sstevel@tonic-gate  */
12747c478bd9Sstevel@tonic-gate prctl_list_t *
12757c478bd9Sstevel@tonic-gate store_list_entry(const char *name)
12767c478bd9Sstevel@tonic-gate {
12777c478bd9Sstevel@tonic-gate 	prctl_list_t *e = calloc(1, sizeof (prctl_list_t));
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	if (e == NULL) {
12807c478bd9Sstevel@tonic-gate 		preserve_error(gettext("malloc failed %s"),
12817c478bd9Sstevel@tonic-gate 		    strerror(errno));
12827c478bd9Sstevel@tonic-gate 		return (NULL);
12837c478bd9Sstevel@tonic-gate 	}
12847c478bd9Sstevel@tonic-gate 	if ((e->name = calloc(1, strlen(name) + 1)) == NULL) {
12857c478bd9Sstevel@tonic-gate 		preserve_error(gettext("malloc failed %s"),
12867c478bd9Sstevel@tonic-gate 		    strerror(errno));
12877c478bd9Sstevel@tonic-gate 		free(e);
12887c478bd9Sstevel@tonic-gate 		return (NULL);
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 	(void) strcpy(e->name, name);
12917c478bd9Sstevel@tonic-gate 	e->val_list = NULL;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	if (global_rctl_list_head == NULL) {
12947c478bd9Sstevel@tonic-gate 		global_rctl_list_head = e;
12957c478bd9Sstevel@tonic-gate 		global_rctl_list_tail = e;
12967c478bd9Sstevel@tonic-gate 	} else {
12977c478bd9Sstevel@tonic-gate 		global_rctl_list_tail->next = e;
12987c478bd9Sstevel@tonic-gate 		global_rctl_list_tail = e;
12997c478bd9Sstevel@tonic-gate 	}
13007c478bd9Sstevel@tonic-gate 	e->next = NULL;
13017c478bd9Sstevel@tonic-gate 	return (e);
13027c478bd9Sstevel@tonic-gate }
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate /*
13057c478bd9Sstevel@tonic-gate  * free_lists()
13067c478bd9Sstevel@tonic-gate  *
13077c478bd9Sstevel@tonic-gate  * Free all resource control blocks and values from the global lists.
13087c478bd9Sstevel@tonic-gate  *
13097c478bd9Sstevel@tonic-gate  * This function is Pgrab-safe.
13107c478bd9Sstevel@tonic-gate  */
13117c478bd9Sstevel@tonic-gate void
13127c478bd9Sstevel@tonic-gate free_lists()
13137c478bd9Sstevel@tonic-gate {
13147c478bd9Sstevel@tonic-gate 	prctl_list_t *new_list, *old_list = global_rctl_list_head;
13157c478bd9Sstevel@tonic-gate 	prctl_value_t *old_val, *new_val;
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	while (old_list != NULL) {
13187c478bd9Sstevel@tonic-gate 		old_val = old_list->val_list;
13197c478bd9Sstevel@tonic-gate 		while (old_val != NULL) {
13207c478bd9Sstevel@tonic-gate 			free(old_val->rblk);
13217c478bd9Sstevel@tonic-gate 			new_val = old_val->next;
13227c478bd9Sstevel@tonic-gate 			free(old_val);
13237c478bd9Sstevel@tonic-gate 			old_val = new_val;
13247c478bd9Sstevel@tonic-gate 		}
13257c478bd9Sstevel@tonic-gate 		free(old_list->name);
13267c478bd9Sstevel@tonic-gate 		new_list = old_list->next;
13277c478bd9Sstevel@tonic-gate 		free(old_list);
13287c478bd9Sstevel@tonic-gate 		old_list = new_list;
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 	global_rctl_list_head = NULL;
13317c478bd9Sstevel@tonic-gate 	global_rctl_list_tail = NULL;
13327c478bd9Sstevel@tonic-gate }
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate void
13357c478bd9Sstevel@tonic-gate print_heading()
13367c478bd9Sstevel@tonic-gate {
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	/* print headings */
13397c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%-8s%-16s%-9s%-7s%-28s%10s\n",
13407c478bd9Sstevel@tonic-gate 	    "NAME", "PRIVILEGE", "VALUE",
13417c478bd9Sstevel@tonic-gate 	    "FLAG", "ACTION", "RECIPIENT");
13427c478bd9Sstevel@tonic-gate }
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate /*
13457c478bd9Sstevel@tonic-gate  * print_rctls()
13467c478bd9Sstevel@tonic-gate  *
13477c478bd9Sstevel@tonic-gate  * Print all resource controls from the global list that was
13487c478bd9Sstevel@tonic-gate  * previously populated by store_rctls.
13497c478bd9Sstevel@tonic-gate  */
13507c478bd9Sstevel@tonic-gate void
13517c478bd9Sstevel@tonic-gate print_rctls(pr_info_handle_t *p)
13527c478bd9Sstevel@tonic-gate {
13537c478bd9Sstevel@tonic-gate 	prctl_list_t *iter_list = global_rctl_list_head;
13547c478bd9Sstevel@tonic-gate 	prctl_value_t *iter_val;
13557c478bd9Sstevel@tonic-gate 	rctl_qty_t  rblk_value;
13567c478bd9Sstevel@tonic-gate 	rctl_priv_t rblk_priv;
13577c478bd9Sstevel@tonic-gate 	uint_t local_action;
13587c478bd9Sstevel@tonic-gate 	int signal, local_flags, global_flags;
13597c478bd9Sstevel@tonic-gate 	pid_t pid;
13607c478bd9Sstevel@tonic-gate 	char rctl_valuestring[SCALED_STRLEN];
13617c478bd9Sstevel@tonic-gate 	char *unit = NULL;
13627c478bd9Sstevel@tonic-gate 	scale_t *scale;
13637c478bd9Sstevel@tonic-gate 	char *string;
13647c478bd9Sstevel@tonic-gate 	int doneheading = 0;
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	if (iter_list == NULL)
13677c478bd9Sstevel@tonic-gate 		return;
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 	while (iter_list != NULL) {
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 		if (doneheading == 0 &&
13727c478bd9Sstevel@tonic-gate 		    arg_entity_type == RCENTITY_PROCESS) {
13737c478bd9Sstevel@tonic-gate 			proc_unctrl_psinfo(&(p->psinfo));
13747c478bd9Sstevel@tonic-gate 			doneheading = 1;
13757c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout,
13767c478bd9Sstevel@tonic-gate 			    "process: %d: %.70s\n", (int)p->pid,
13777c478bd9Sstevel@tonic-gate 			    p->psinfo.pr_psargs);
13787c478bd9Sstevel@tonic-gate 			if (!arg_parseable_mode)
13797c478bd9Sstevel@tonic-gate 				print_heading();
13807c478bd9Sstevel@tonic-gate 		}
13817c478bd9Sstevel@tonic-gate 		if (doneheading == 0 &&
13827c478bd9Sstevel@tonic-gate 		    arg_entity_type == RCENTITY_TASK) {
13837c478bd9Sstevel@tonic-gate 			doneheading = 1;
13847c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "task: %d\n", (int)p->taskid);
13857c478bd9Sstevel@tonic-gate 			if (!arg_parseable_mode)
13867c478bd9Sstevel@tonic-gate 				print_heading();
13877c478bd9Sstevel@tonic-gate 		}
13887c478bd9Sstevel@tonic-gate 		if (doneheading == 0 &&
13897c478bd9Sstevel@tonic-gate 		    arg_entity_type == RCENTITY_PROJECT) {
13907c478bd9Sstevel@tonic-gate 			if (!arg_parseable_mode && doneheading)
13917c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "\n");
13927c478bd9Sstevel@tonic-gate 			doneheading = 1;
13937c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout,
13947c478bd9Sstevel@tonic-gate 			    "project: %d: %.70s\n", (int)p->projid,
13957c478bd9Sstevel@tonic-gate 			    p->projname);
13967c478bd9Sstevel@tonic-gate 			if (!arg_parseable_mode)
13977c478bd9Sstevel@tonic-gate 				print_heading();
13987c478bd9Sstevel@tonic-gate 		}
13997c478bd9Sstevel@tonic-gate 		if (doneheading == 0 &&
14007c478bd9Sstevel@tonic-gate 		    arg_entity_type == RCENTITY_ZONE) {
14017c478bd9Sstevel@tonic-gate 			doneheading = 1;
14027c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout,
14037c478bd9Sstevel@tonic-gate 			    "zone: %d: %.70s\n", (int)p->zoneid,
14047c478bd9Sstevel@tonic-gate 			    p->zonename);
14057c478bd9Sstevel@tonic-gate 			if (!arg_parseable_mode)
14067c478bd9Sstevel@tonic-gate 				print_heading();
14077c478bd9Sstevel@tonic-gate 		}
14087c478bd9Sstevel@tonic-gate 		/* only print name once in normal output */
14097c478bd9Sstevel@tonic-gate 		if (!arg_parseable_mode)
14107c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s\n", iter_list->name);
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 		iter_val = iter_list->val_list;
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 		/* if for some reason there are no values, skip */
14157c478bd9Sstevel@tonic-gate 		if (iter_val == 0)
14167c478bd9Sstevel@tonic-gate 			continue;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 		/* get the global flags the first rctl only */
14207c478bd9Sstevel@tonic-gate 		global_flags = rctlblk_get_global_flags(iter_val->rblk);
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 		if (global_flags & RCTL_GLOBAL_BYTES) {
14247c478bd9Sstevel@tonic-gate 			unit = SCALED_UNIT_BYTES;
14257c478bd9Sstevel@tonic-gate 			scale = scale_binary;
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 		} else if (global_flags & RCTL_GLOBAL_SECONDS) {
14287c478bd9Sstevel@tonic-gate 			unit = SCALED_UNIT_SECONDS;
14297c478bd9Sstevel@tonic-gate 			scale = scale_metric;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 		} else {
14327c478bd9Sstevel@tonic-gate 			unit = SCALED_UNIT_NONE;
14337c478bd9Sstevel@tonic-gate 			scale = scale_metric;
14347c478bd9Sstevel@tonic-gate 		}
14357c478bd9Sstevel@tonic-gate 		/* iterate over an print all control values */
14367c478bd9Sstevel@tonic-gate 		while (iter_val != NULL) {
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 			/* print name or empty name field */
14397c478bd9Sstevel@tonic-gate 			if (!arg_parseable_mode)
14407c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%8s", "");
14417c478bd9Sstevel@tonic-gate 			else
14427c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%s ", iter_list->name);
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 			rblk_priv = rctlblk_get_privilege(iter_val->rblk);
14467c478bd9Sstevel@tonic-gate 			if (!arg_parseable_mode)
14477c478bd9Sstevel@tonic-gate 				print_priv(rblk_priv, "%-16s");
14487c478bd9Sstevel@tonic-gate 			else
14497c478bd9Sstevel@tonic-gate 				print_priv(rblk_priv, "%s ");
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 			rblk_value = rctlblk_get_value(iter_val->rblk);
14527c478bd9Sstevel@tonic-gate 			if (arg_parseable_mode) {
14537c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%llu ", rblk_value);
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 			} else {
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 				(void) uint64toscaled(rblk_value, 4, "E",
14587c478bd9Sstevel@tonic-gate 				    rctl_valuestring, NULL, NULL,
14597c478bd9Sstevel@tonic-gate 				    scale, NULL, 0);
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%5s",
14627c478bd9Sstevel@tonic-gate 				    rctl_valuestring);
14637c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%-4s", unit);
14647c478bd9Sstevel@tonic-gate 			}
14657c478bd9Sstevel@tonic-gate 			local_flags = rctlblk_get_local_flags(iter_val->rblk);
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 			if (local_flags & RCTL_LOCAL_MAXIMAL) {
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 				if (global_flags & RCTL_GLOBAL_INFINITE) {
14707c478bd9Sstevel@tonic-gate 					string = "inf";
14717c478bd9Sstevel@tonic-gate 				} else {
14727c478bd9Sstevel@tonic-gate 					string = "max";
14737c478bd9Sstevel@tonic-gate 				}
14747c478bd9Sstevel@tonic-gate 			} else {
14757c478bd9Sstevel@tonic-gate 				string = "-";
14767c478bd9Sstevel@tonic-gate 			}
14777c478bd9Sstevel@tonic-gate 			if (arg_parseable_mode)
14787c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%s ", string);
14797c478bd9Sstevel@tonic-gate 			else
14807c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%4s%3s",
14817c478bd9Sstevel@tonic-gate 				    string, "");
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 			local_action = rctlblk_get_local_action(iter_val->rblk,
14857c478bd9Sstevel@tonic-gate 			    &signal);
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 			if (arg_parseable_mode)
14887c478bd9Sstevel@tonic-gate 				print_local_action(local_action, &signal,
14897c478bd9Sstevel@tonic-gate 				    "%s ");
14907c478bd9Sstevel@tonic-gate 			else
14917c478bd9Sstevel@tonic-gate 				print_local_action(local_action, &signal,
14927c478bd9Sstevel@tonic-gate 				    "%-28s");
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 			pid = rctlblk_get_recipient_pid(iter_val->rblk);
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 			if (arg_parseable_mode) {
14977c478bd9Sstevel@tonic-gate 				if (pid < 0) {
14987c478bd9Sstevel@tonic-gate 					(void) fprintf(stdout, "%s\n", "-");
14997c478bd9Sstevel@tonic-gate 				} else {
15007c478bd9Sstevel@tonic-gate 					(void) fprintf(stdout, "%d\n",
15017c478bd9Sstevel@tonic-gate 					    (int)pid);
15027c478bd9Sstevel@tonic-gate 				}
15037c478bd9Sstevel@tonic-gate 			} else {
15047c478bd9Sstevel@tonic-gate 				if (pid < 0) {
15057c478bd9Sstevel@tonic-gate 					(void) fprintf(stdout, "%10s\n", "-");
15067c478bd9Sstevel@tonic-gate 				} else {
15077c478bd9Sstevel@tonic-gate 					(void) fprintf(stdout, "%10d\n",
15087c478bd9Sstevel@tonic-gate 					    (int)pid);
15097c478bd9Sstevel@tonic-gate 				}
15107c478bd9Sstevel@tonic-gate 			}
15117c478bd9Sstevel@tonic-gate 			iter_val = iter_val->next;
15127c478bd9Sstevel@tonic-gate 		}
15137c478bd9Sstevel@tonic-gate 		iter_list = iter_list->next;
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate }
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate /*
15187c478bd9Sstevel@tonic-gate  *
15197c478bd9Sstevel@tonic-gate  * match_rctl
15207c478bd9Sstevel@tonic-gate  *
15217c478bd9Sstevel@tonic-gate  * find the first rctl with matching name, value, priv, and recipient pid
15227c478bd9Sstevel@tonic-gate  */
15237c478bd9Sstevel@tonic-gate int
15247c478bd9Sstevel@tonic-gate match_rctl(struct ps_prochandle *Pr, rctlblk_t **rctl, char *name,
15257c478bd9Sstevel@tonic-gate     char *valuestringin, int valuein, rctl_priv_t privin, int pidin)
15267c478bd9Sstevel@tonic-gate {
15277c478bd9Sstevel@tonic-gate 	rctlblk_t *next;
15287c478bd9Sstevel@tonic-gate 	rctlblk_t *last;
15297c478bd9Sstevel@tonic-gate 	rctlblk_t *tmp;
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	*rctl = NULL;
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	next = calloc(1, rctlblk_size());
15347c478bd9Sstevel@tonic-gate 	last = calloc(1, rctlblk_size());
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	if ((last == NULL) || (next == NULL)) {
15377c478bd9Sstevel@tonic-gate 		preserve_error(gettext("malloc failed"), strerror(errno));
15387c478bd9Sstevel@tonic-gate 		return (-1);
15397c478bd9Sstevel@tonic-gate 	}
15407c478bd9Sstevel@tonic-gate 	/*
15417c478bd9Sstevel@tonic-gate 	 * For this resource name, now iterate through all
15427c478bd9Sstevel@tonic-gate 	 * the  controls, looking for a match to the
15437c478bd9Sstevel@tonic-gate 	 * user-specified input.
15447c478bd9Sstevel@tonic-gate 	 */
15457c478bd9Sstevel@tonic-gate 	if (pr_getrctl(Pr, name, NULL, next, RCTL_FIRST)) {
15467c478bd9Sstevel@tonic-gate 		preserve_error(gettext("failed to get resource control "
15477c478bd9Sstevel@tonic-gate 		    "for %s: %s"), name, strerror(errno));
15487c478bd9Sstevel@tonic-gate 		return (-1);
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 	if (match_rctl_blk(next, valuestringin, valuein, privin, pidin) == 1) {
15517c478bd9Sstevel@tonic-gate 		free(last);
15527c478bd9Sstevel@tonic-gate 		*rctl = next;
15537c478bd9Sstevel@tonic-gate 		return (0);
15547c478bd9Sstevel@tonic-gate 	}
15557c478bd9Sstevel@tonic-gate 	tmp = next;
15567c478bd9Sstevel@tonic-gate 	next = last;
15577c478bd9Sstevel@tonic-gate 	last = tmp;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	while (pr_getrctl(Pr, name, last, next,	RCTL_NEXT) == 0) {
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 		/* allow user interrupt */
15627c478bd9Sstevel@tonic-gate 		if (interrupt)
15637c478bd9Sstevel@tonic-gate 			break;
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 		if (match_rctl_blk(next, valuestringin, valuein, privin, pidin)
15667c478bd9Sstevel@tonic-gate 		    == 1) {
15677c478bd9Sstevel@tonic-gate 			free(last);
15687c478bd9Sstevel@tonic-gate 			*rctl = next;
15697c478bd9Sstevel@tonic-gate 			return (0);
15707c478bd9Sstevel@tonic-gate 		}
15717c478bd9Sstevel@tonic-gate 		tmp = next;
15727c478bd9Sstevel@tonic-gate 		next = last;
15737c478bd9Sstevel@tonic-gate 		last = tmp;
15747c478bd9Sstevel@tonic-gate 	}
15757c478bd9Sstevel@tonic-gate 	free(next);
15767c478bd9Sstevel@tonic-gate 	free(last);
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	return (1);
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate /*
15827c478bd9Sstevel@tonic-gate  * int match_rctl_blk(rctlblk_t *, char *, uint64, rctl_priv_t, int pid)
15837c478bd9Sstevel@tonic-gate  *
15847c478bd9Sstevel@tonic-gate  * Input
15857c478bd9Sstevel@tonic-gate  *   Must supply a valid rctl, value, privilege, and pid to match on.
15867c478bd9Sstevel@tonic-gate  *   If valuestring is NULL, then valuestring and valuein will not be used
15877c478bd9Sstevel@tonic-gate  *   If privilege type is 0 it will not be used.
15887c478bd9Sstevel@tonic-gate  *   If pid is -1 it will not be used.
15897c478bd9Sstevel@tonic-gate  *
15907c478bd9Sstevel@tonic-gate  * Return values
15917c478bd9Sstevel@tonic-gate  *   Returns 1 if a matching rctl given matches the parameters specified, and
15927c478bd9Sstevel@tonic-gate  *   0 if they do not.
15937c478bd9Sstevel@tonic-gate  *
15947c478bd9Sstevel@tonic-gate  * This function is Pgrab-safe.
15957c478bd9Sstevel@tonic-gate  */
15967c478bd9Sstevel@tonic-gate int
15977c478bd9Sstevel@tonic-gate match_rctl_blk(rctlblk_t *rctl, char *valuestringin,
15987c478bd9Sstevel@tonic-gate     uint64_t valuein, rctl_priv_t privin, int pidin)
15997c478bd9Sstevel@tonic-gate {
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	rctl_qty_t value;
16027c478bd9Sstevel@tonic-gate 	rctl_priv_t priv;
16037c478bd9Sstevel@tonic-gate 	pid_t pid;
16047c478bd9Sstevel@tonic-gate 	int valuematch = 1;
16057c478bd9Sstevel@tonic-gate 	int privmatch = 1;
16067c478bd9Sstevel@tonic-gate 	int pidmatch = 1;
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	value = rctlblk_get_value(rctl);
16097c478bd9Sstevel@tonic-gate 	priv = rctlblk_get_privilege(rctl);
16107c478bd9Sstevel@tonic-gate 	pid = rctlblk_get_recipient_pid(rctl);
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	if (valuestringin) {
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 		if (arg_modifier == NULL) {
16157c478bd9Sstevel@tonic-gate 			valuematch = (valuein == value);
16167c478bd9Sstevel@tonic-gate 		} else {
16177c478bd9Sstevel@tonic-gate 			valuematch = scaledequint64(valuestringin, value,
16187c478bd9Sstevel@tonic-gate 			    PRCTL_VALUE_WIDTH,
16197c478bd9Sstevel@tonic-gate 			    arg_scale, arg_unit,
16207c478bd9Sstevel@tonic-gate 			    SCALED_ALL_FLAGS);
16217c478bd9Sstevel@tonic-gate 		}
16227c478bd9Sstevel@tonic-gate 	}
16237c478bd9Sstevel@tonic-gate 	if (privin != 0) {
16247c478bd9Sstevel@tonic-gate 		privmatch = (privin == priv);
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 	if (pidin != -1) {
16277c478bd9Sstevel@tonic-gate 		pidmatch = (pidin == pid);
16287c478bd9Sstevel@tonic-gate 	}
16297c478bd9Sstevel@tonic-gate 	return (valuematch && privmatch && pidmatch);
16307c478bd9Sstevel@tonic-gate }
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate static int
16337c478bd9Sstevel@tonic-gate change_action(rctlblk_t *blk)
16347c478bd9Sstevel@tonic-gate {
16357c478bd9Sstevel@tonic-gate 	int signal = 0;
16367c478bd9Sstevel@tonic-gate 	int action;
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	action = rctlblk_get_local_action(blk, &signal);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	if (arg_operation & ACTION_ENABLE) {
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 		if (arg_action & RCTL_LOCAL_SIGNAL) {
16437c478bd9Sstevel@tonic-gate 			signal = arg_signal;
16447c478bd9Sstevel@tonic-gate 		}
16457c478bd9Sstevel@tonic-gate 		action = action | arg_action;
16467c478bd9Sstevel@tonic-gate 		/* add local action */
16477c478bd9Sstevel@tonic-gate 		rctlblk_set_local_action(blk, action, signal);
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	} else if (arg_operation & ACTION_DISABLE) {
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 		/*
16527c478bd9Sstevel@tonic-gate 		 * if deleting signal and signal number is specified,
16537c478bd9Sstevel@tonic-gate 		 * then signal number must match
16547c478bd9Sstevel@tonic-gate 		 */
16557c478bd9Sstevel@tonic-gate 		if ((arg_action & RCTL_LOCAL_SIGNAL) &&
16567c478bd9Sstevel@tonic-gate 		    (arg_signal != -1)) {
16577c478bd9Sstevel@tonic-gate 			if (arg_signal != signal) {
16587c478bd9Sstevel@tonic-gate 				preserve_error(gettext("signal name or number "
16597c478bd9Sstevel@tonic-gate 				    "does not match existing action"));
16607c478bd9Sstevel@tonic-gate 				return (-1);
16617c478bd9Sstevel@tonic-gate 			}
16627c478bd9Sstevel@tonic-gate 		}
16637c478bd9Sstevel@tonic-gate 		/* remove local action */
16647c478bd9Sstevel@tonic-gate 		action = action & (~arg_action);
16657c478bd9Sstevel@tonic-gate 		rctlblk_set_local_action(blk, RCTL_LOCAL_NOACTION, 0);
16667c478bd9Sstevel@tonic-gate 		rctlblk_set_local_action(blk, action, signal);
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 	/* enable deny if it must be enabled */
16697c478bd9Sstevel@tonic-gate 	if (arg_global_flags & RCTL_GLOBAL_DENY_ALWAYS) {
16707c478bd9Sstevel@tonic-gate 		rctlblk_set_local_action(blk, RCTL_LOCAL_DENY | action,
16717c478bd9Sstevel@tonic-gate 		    signal);
16727c478bd9Sstevel@tonic-gate 	}
16737c478bd9Sstevel@tonic-gate 	return (0);
16747c478bd9Sstevel@tonic-gate }
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate /*
16777c478bd9Sstevel@tonic-gate  * prctl_setrctl
16787c478bd9Sstevel@tonic-gate  *
16797c478bd9Sstevel@tonic-gate  * Input
16807c478bd9Sstevel@tonic-gate  *   This function expects that input has been validated. In the
16817c478bd9Sstevel@tonic-gate  *   case of a replace operation, both old_rblk and new_rblk must
16827c478bd9Sstevel@tonic-gate  *   be valid resource controls. If a resource control is being
16837c478bd9Sstevel@tonic-gate  *   created, only new_rblk must be supplied. If a resource control
16847c478bd9Sstevel@tonic-gate  *   is being deleted, only new_rblk must be supplied.
16857c478bd9Sstevel@tonic-gate  *
16867c478bd9Sstevel@tonic-gate  * If the privilege is a priviliged type, at this time, the process
16877c478bd9Sstevel@tonic-gate  * tries to take on superuser privileges.
16887c478bd9Sstevel@tonic-gate  */
16897c478bd9Sstevel@tonic-gate int
16907c478bd9Sstevel@tonic-gate prctl_setrctl(struct ps_prochandle *Pr, const char *name,
16917c478bd9Sstevel@tonic-gate     rctlblk_t *old_rblk, rctlblk_t *new_rblk, uint_t flags)
16927c478bd9Sstevel@tonic-gate {
16937c478bd9Sstevel@tonic-gate 	int ret = 0;
16947c478bd9Sstevel@tonic-gate 	rctl_priv_t rblk_priv;
16957c478bd9Sstevel@tonic-gate 	psinfo_t psinfo;
16967c478bd9Sstevel@tonic-gate 	zoneid_t oldzoneid = GLOBAL_ZONEID;
16977c478bd9Sstevel@tonic-gate 	prpriv_t *old_prpriv = NULL, *new_prpriv = NULL;
16987c478bd9Sstevel@tonic-gate 	priv_set_t *eset, *pset;
16997c478bd9Sstevel@tonic-gate 	boolean_t relinquish_failed = B_FALSE;
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	rblk_priv = rctlblk_get_privilege(new_rblk);
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	if (rblk_priv == RCPRIV_SYSTEM) {
17047c478bd9Sstevel@tonic-gate 		preserve_error(gettext("cannot modify system values"));
17057c478bd9Sstevel@tonic-gate 		return (1);
17067c478bd9Sstevel@tonic-gate 	}
17077c478bd9Sstevel@tonic-gate 	if (rblk_priv == RCPRIV_PRIVILEGED) {
17087c478bd9Sstevel@tonic-gate 		new_prpriv = proc_get_priv(Pstatus(Pr)->pr_pid);
17097c478bd9Sstevel@tonic-gate 		if (new_prpriv == NULL) {
17107c478bd9Sstevel@tonic-gate 			preserve_error(gettext("cannot get process privileges "
17117c478bd9Sstevel@tonic-gate 			    "for pid %d: %s"), Pstatus(Pr)->pr_pid,
17127c478bd9Sstevel@tonic-gate 			    strerror(errno));
17137c478bd9Sstevel@tonic-gate 			return (1);
17147c478bd9Sstevel@tonic-gate 		}
17157c478bd9Sstevel@tonic-gate 		/*
17167c478bd9Sstevel@tonic-gate 		 * We only have to change the process privileges if it doesn't
17177c478bd9Sstevel@tonic-gate 		 * already have PRIV_SYS_RESOURCE.  In addition, we want to make
17187c478bd9Sstevel@tonic-gate 		 * sure that we don't leave a process with elevated privileges,
17197c478bd9Sstevel@tonic-gate 		 * so we make sure the process dies if we exit unexpectedly.
17207c478bd9Sstevel@tonic-gate 		 */
17217c478bd9Sstevel@tonic-gate 		eset = (priv_set_t *)
17227c478bd9Sstevel@tonic-gate 		    &new_prpriv->pr_sets[new_prpriv->pr_setsize *
17237c478bd9Sstevel@tonic-gate 		    priv_getsetbyname(PRIV_EFFECTIVE)];
17247c478bd9Sstevel@tonic-gate 		pset = (priv_set_t *)
17257c478bd9Sstevel@tonic-gate 		    &new_prpriv->pr_sets[new_prpriv->pr_setsize *
17267c478bd9Sstevel@tonic-gate 		    priv_getsetbyname(PRIV_PERMITTED)];
17277c478bd9Sstevel@tonic-gate 		if (!priv_ismember(eset, PRIV_SYS_RESOURCE)) {
17287c478bd9Sstevel@tonic-gate 			/* Keep track of original privileges */
17297c478bd9Sstevel@tonic-gate 			old_prpriv = proc_get_priv(Pstatus(Pr)->pr_pid);
17307c478bd9Sstevel@tonic-gate 			if (old_prpriv == NULL) {
17317c478bd9Sstevel@tonic-gate 				preserve_error(gettext("cannot get process "
17327c478bd9Sstevel@tonic-gate 				    "privileges for pid %d: %s"),
17337c478bd9Sstevel@tonic-gate 				    Pstatus(Pr)->pr_pid, strerror(errno));
17347c478bd9Sstevel@tonic-gate 				free(new_prpriv);
17357c478bd9Sstevel@tonic-gate 				return (1);
17367c478bd9Sstevel@tonic-gate 			}
17377c478bd9Sstevel@tonic-gate 			(void) priv_addset(eset, PRIV_SYS_RESOURCE);
17387c478bd9Sstevel@tonic-gate 			(void) priv_addset(pset, PRIV_SYS_RESOURCE);
17397c478bd9Sstevel@tonic-gate 			if (Psetflags(Pr, PR_KLC) != 0 ||
17407c478bd9Sstevel@tonic-gate 			    Psetpriv(Pr, new_prpriv) != 0) {
17417c478bd9Sstevel@tonic-gate 				preserve_error(gettext("cannot set process "
17427c478bd9Sstevel@tonic-gate 				    "privileges for pid %d: %s"),
17437c478bd9Sstevel@tonic-gate 				    Pstatus(Pr)->pr_pid, strerror(errno));
17447c478bd9Sstevel@tonic-gate 				(void) Punsetflags(Pr, PR_KLC);
17457c478bd9Sstevel@tonic-gate 				free(new_prpriv);
17467c478bd9Sstevel@tonic-gate 				free(old_prpriv);
17477c478bd9Sstevel@tonic-gate 				return (1);
17487c478bd9Sstevel@tonic-gate 			}
17497c478bd9Sstevel@tonic-gate 		}
17507c478bd9Sstevel@tonic-gate 		/*
17517c478bd9Sstevel@tonic-gate 		 * If this is a zone.* rctl, it requires more than
17527c478bd9Sstevel@tonic-gate 		 * PRIV_SYS_RESOURCE: it wants the process to have global-zone
17537c478bd9Sstevel@tonic-gate 		 * credentials.  We temporarily grant non-global zone processes
17547c478bd9Sstevel@tonic-gate 		 * these credentials, and make sure the process dies if we exit
17557c478bd9Sstevel@tonic-gate 		 * unexpectedly.
17567c478bd9Sstevel@tonic-gate 		 */
17577c478bd9Sstevel@tonic-gate 		if (arg_name &&
17587c478bd9Sstevel@tonic-gate 		    arg_name_entity == RCENTITY_ZONE &&
17597c478bd9Sstevel@tonic-gate 		    getzoneid() == GLOBAL_ZONEID &&
17607c478bd9Sstevel@tonic-gate 		    proc_get_psinfo(Pstatus(Pr)->pr_pid, &psinfo) == 0 &&
17617c478bd9Sstevel@tonic-gate 		    (oldzoneid = psinfo.pr_zoneid) != GLOBAL_ZONEID) {
17627c478bd9Sstevel@tonic-gate 			/*
17637c478bd9Sstevel@tonic-gate 			 * We need to give this process superuser
17647c478bd9Sstevel@tonic-gate 			 * ("super-zone") privileges.
17657c478bd9Sstevel@tonic-gate 			 *
17667c478bd9Sstevel@tonic-gate 			 * Must never return without setting this back!
17677c478bd9Sstevel@tonic-gate 			 */
17687c478bd9Sstevel@tonic-gate 			if (Psetflags(Pr, PR_KLC) != 0 ||
17697c478bd9Sstevel@tonic-gate 			    Psetzoneid(Pr, GLOBAL_ZONEID) < 0) {
17707c478bd9Sstevel@tonic-gate 				preserve_error(gettext(
17717c478bd9Sstevel@tonic-gate 				    "cannot set global-zone "
17727c478bd9Sstevel@tonic-gate 				    "privileges for pid %d: %s"),
17737c478bd9Sstevel@tonic-gate 				    Pstatus(Pr)->pr_pid, strerror(errno));
17747c478bd9Sstevel@tonic-gate 				/*
17757c478bd9Sstevel@tonic-gate 				 * We couldn't set the zoneid to begin with, so
17767c478bd9Sstevel@tonic-gate 				 * there's no point in warning the user about
17777c478bd9Sstevel@tonic-gate 				 * trying to un-set it.
17787c478bd9Sstevel@tonic-gate 				 */
17797c478bd9Sstevel@tonic-gate 				oldzoneid = GLOBAL_ZONEID;
17807c478bd9Sstevel@tonic-gate 				ret = 1;
17817c478bd9Sstevel@tonic-gate 				goto bail;
17827c478bd9Sstevel@tonic-gate 			}
17837c478bd9Sstevel@tonic-gate 		}
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 	/* Now, actually populate the rctlblk in the kernel */
17867c478bd9Sstevel@tonic-gate 	if (flags == RCTL_REPLACE) {
17877c478bd9Sstevel@tonic-gate 		/*
17887c478bd9Sstevel@tonic-gate 		 * Replace should be a delete followed by an insert. This
17897c478bd9Sstevel@tonic-gate 		 * allows us to replace rctl value blocks which match in
17907c478bd9Sstevel@tonic-gate 		 * privilege and value, but have updated actions, etc.
17917c478bd9Sstevel@tonic-gate 		 * setrctl() doesn't allow a direct replace, but we
17927c478bd9Sstevel@tonic-gate 		 * should do the right thing for the user in the command.
17937c478bd9Sstevel@tonic-gate 		 */
17947c478bd9Sstevel@tonic-gate 		if (pr_setrctl(Pr, name, NULL,
17957c478bd9Sstevel@tonic-gate 		    old_rblk, RCTL_DELETE)) {
17967c478bd9Sstevel@tonic-gate 			preserve_error(gettext("failed to delete resource "
17977c478bd9Sstevel@tonic-gate 			    "control %s for pid %d: %s"), name,
17987c478bd9Sstevel@tonic-gate 			    Pstatus(Pr)->pr_pid, strerror(errno));
17997c478bd9Sstevel@tonic-gate 			ret = 1;
18007c478bd9Sstevel@tonic-gate 			goto bail;
18017c478bd9Sstevel@tonic-gate 		}
18027c478bd9Sstevel@tonic-gate 		if (pr_setrctl(Pr, name, NULL,
18037c478bd9Sstevel@tonic-gate 		    new_rblk, RCTL_INSERT)) {
18047c478bd9Sstevel@tonic-gate 			preserve_error(gettext("failed to insert resource "
18057c478bd9Sstevel@tonic-gate 			    "control %s for pid %d: %s"), name,
18067c478bd9Sstevel@tonic-gate 			    Pstatus(Pr)->pr_pid, strerror(errno));
18077c478bd9Sstevel@tonic-gate 			ret = 1;
18087c478bd9Sstevel@tonic-gate 			goto bail;
18097c478bd9Sstevel@tonic-gate 		}
18107c478bd9Sstevel@tonic-gate 	} else if (flags == RCTL_INSERT) {
18117c478bd9Sstevel@tonic-gate 		if (pr_setrctl(Pr, name, NULL,
18127c478bd9Sstevel@tonic-gate 		    new_rblk, RCTL_INSERT)) {
18137c478bd9Sstevel@tonic-gate 			preserve_error(gettext("failed to create resource "
18147c478bd9Sstevel@tonic-gate 			    "control %s for pid %d: %s"), name,
18157c478bd9Sstevel@tonic-gate 			    Pstatus(Pr)->pr_pid, strerror(errno));
18167c478bd9Sstevel@tonic-gate 			ret = 1;
18177c478bd9Sstevel@tonic-gate 			goto bail;
18187c478bd9Sstevel@tonic-gate 		}
18197c478bd9Sstevel@tonic-gate 	} else if (flags == RCTL_DELETE) {
18207c478bd9Sstevel@tonic-gate 		if (pr_setrctl(Pr, name, NULL,
18217c478bd9Sstevel@tonic-gate 		    new_rblk, RCTL_DELETE)) {
18227c478bd9Sstevel@tonic-gate 			preserve_error(gettext("failed to delete resource "
18237c478bd9Sstevel@tonic-gate 			    "control %s for pid %d: %s"), name,
18247c478bd9Sstevel@tonic-gate 			    Pstatus(Pr)->pr_pid, strerror(errno));
18257c478bd9Sstevel@tonic-gate 			ret = 1;
18267c478bd9Sstevel@tonic-gate 			goto bail;
18277c478bd9Sstevel@tonic-gate 		}
18287c478bd9Sstevel@tonic-gate 	}
18297c478bd9Sstevel@tonic-gate bail:
18307c478bd9Sstevel@tonic-gate 	if (oldzoneid != GLOBAL_ZONEID) {
18317c478bd9Sstevel@tonic-gate 		if (Psetzoneid(Pr, oldzoneid) != 0)
18327c478bd9Sstevel@tonic-gate 			relinquish_failed = B_TRUE;
18337c478bd9Sstevel@tonic-gate 	}
18347c478bd9Sstevel@tonic-gate 	if (old_prpriv != NULL) {
18357c478bd9Sstevel@tonic-gate 		if (Psetpriv(Pr, old_prpriv) != 0)
18367c478bd9Sstevel@tonic-gate 			relinquish_failed = B_TRUE;
18377c478bd9Sstevel@tonic-gate 		free(old_prpriv);
18387c478bd9Sstevel@tonic-gate 	}
18397c478bd9Sstevel@tonic-gate 	if (relinquish_failed) {
18407c478bd9Sstevel@tonic-gate 		/*
18417c478bd9Sstevel@tonic-gate 		 * If this failed, we can't leave a process hanging
18427c478bd9Sstevel@tonic-gate 		 * around with elevated privileges, so we'll have to
18437c478bd9Sstevel@tonic-gate 		 * release the process from libproc, knowing that it
18447c478bd9Sstevel@tonic-gate 		 * will be killed (since we set PR_KLC).
18457c478bd9Sstevel@tonic-gate 		 */
18467c478bd9Sstevel@tonic-gate 		Pdestroy_agent(Pr);
18477c478bd9Sstevel@tonic-gate 		preserve_error(gettext("cannot relinquish privileges "
18487c478bd9Sstevel@tonic-gate 		    "for pid %d. The process was killed."),
18497c478bd9Sstevel@tonic-gate 		    Pstatus(Pr)->pr_pid);
18507c478bd9Sstevel@tonic-gate 	} else {
18517c478bd9Sstevel@tonic-gate 		if (Punsetflags(Pr, PR_KLC) != 0)
18527c478bd9Sstevel@tonic-gate 			preserve_error(gettext("cannot relinquish privileges "
18537c478bd9Sstevel@tonic-gate 			    "for pid %d. The process was killed."),
18547c478bd9Sstevel@tonic-gate 			    Pstatus(Pr)->pr_pid);
18557c478bd9Sstevel@tonic-gate 	}
18567c478bd9Sstevel@tonic-gate 	if (new_prpriv != NULL)
18577c478bd9Sstevel@tonic-gate 		free(new_prpriv);
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	return (ret);
18607c478bd9Sstevel@tonic-gate }
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate void
18637c478bd9Sstevel@tonic-gate print_priv(rctl_priv_t local_priv, char *format)
18647c478bd9Sstevel@tonic-gate {
18657c478bd9Sstevel@tonic-gate 	char pstring[11];
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	switch (local_priv) {
18687c478bd9Sstevel@tonic-gate 	case RCPRIV_BASIC:
18697c478bd9Sstevel@tonic-gate 		(void) strcpy(pstring, "basic");
18707c478bd9Sstevel@tonic-gate 		break;
18717c478bd9Sstevel@tonic-gate 	case RCPRIV_PRIVILEGED:
18727c478bd9Sstevel@tonic-gate 		(void) strcpy(pstring, "privileged");
18737c478bd9Sstevel@tonic-gate 		break;
18747c478bd9Sstevel@tonic-gate 	case RCPRIV_SYSTEM:
18757c478bd9Sstevel@tonic-gate 		(void) strcpy(pstring, "system");
18767c478bd9Sstevel@tonic-gate 		break;
18777c478bd9Sstevel@tonic-gate 	default:
18787c478bd9Sstevel@tonic-gate 		(void) sprintf(pstring, "%d", local_priv);
18797c478bd9Sstevel@tonic-gate 		break;
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 	/* LINTED */
18827c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, format, pstring);
18837c478bd9Sstevel@tonic-gate }
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate void
18867c478bd9Sstevel@tonic-gate print_local_action(int action, int *signalp, char *format)
18877c478bd9Sstevel@tonic-gate {
18887c478bd9Sstevel@tonic-gate 	char sig[SIG2STR_MAX];
18897c478bd9Sstevel@tonic-gate 	char sigstring[SIG2STR_MAX + 7];
18907c478bd9Sstevel@tonic-gate 	char astring[5 + SIG2STR_MAX + 7];
18917c478bd9Sstevel@tonic-gate 	int set = 0;
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	astring[0] = '\0';
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	if (action == RCTL_LOCAL_NOACTION) {
18967c478bd9Sstevel@tonic-gate 		(void) strcat(astring, "none");
18977c478bd9Sstevel@tonic-gate 		set++;
18987c478bd9Sstevel@tonic-gate 	}
18997c478bd9Sstevel@tonic-gate 	if (action & RCTL_LOCAL_DENY) {
19007c478bd9Sstevel@tonic-gate 		(void) strcat(astring, "deny");
19017c478bd9Sstevel@tonic-gate 		set++;
19027c478bd9Sstevel@tonic-gate 	}
19037c478bd9Sstevel@tonic-gate 	if ((action & RCTL_LOCAL_DENY) &&
19047c478bd9Sstevel@tonic-gate 	    (action & RCTL_LOCAL_SIGNAL)) {
19057c478bd9Sstevel@tonic-gate 		(void) strcat(astring, ",");
19067c478bd9Sstevel@tonic-gate 	}
19077c478bd9Sstevel@tonic-gate 	if (action & RCTL_LOCAL_SIGNAL) {
19087c478bd9Sstevel@tonic-gate 		if (sig2str(*signalp, sig))
19097c478bd9Sstevel@tonic-gate 			(void) snprintf(sigstring, sizeof (astring),
19107c478bd9Sstevel@tonic-gate 			    "signal=%d", *signalp);
19117c478bd9Sstevel@tonic-gate 		else
19127c478bd9Sstevel@tonic-gate 			(void) snprintf(sigstring, sizeof (astring),
19137c478bd9Sstevel@tonic-gate 			    "signal=%s", sig);
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 		(void) strcat(astring, sigstring);
19167c478bd9Sstevel@tonic-gate 		set++;
19177c478bd9Sstevel@tonic-gate 	}
19187c478bd9Sstevel@tonic-gate 	if (set)
19197c478bd9Sstevel@tonic-gate 		/* LINTED */
19207c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, format, astring);
19217c478bd9Sstevel@tonic-gate 	else
19227c478bd9Sstevel@tonic-gate 		/* LINTED */
19237c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, format, action);
19247c478bd9Sstevel@tonic-gate }
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate /*
19277c478bd9Sstevel@tonic-gate  * This function is used to grab the process matching the recipient pid
19287c478bd9Sstevel@tonic-gate  */
19297c478bd9Sstevel@tonic-gate pid_t
19307c478bd9Sstevel@tonic-gate regrab_process(pid_t pid, pr_info_handle_t *p, int priv, int *gret)
19317c478bd9Sstevel@tonic-gate {
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	char pidstring[24];
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	gret = 0;
19367c478bd9Sstevel@tonic-gate 	if (pid == -1)
19377c478bd9Sstevel@tonic-gate 		return (p->pid);
19387c478bd9Sstevel@tonic-gate 	if (p->pid == pid)
19397c478bd9Sstevel@tonic-gate 		return (p->pid);
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	release_process(p->pr);
19427c478bd9Sstevel@tonic-gate 	(void) memset(p, 0, sizeof (*p));
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	(void) snprintf(pidstring, 24, "%d", pid);
19457c478bd9Sstevel@tonic-gate 	return (grab_process_by_id(
19467c478bd9Sstevel@tonic-gate 	    pidstring, RCENTITY_PROCESS, p, priv, gret));
19477c478bd9Sstevel@tonic-gate }
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate /*
19507c478bd9Sstevel@tonic-gate  * int grab_process_by_id(char *, rctl_entity_t, pr_info_handle_t *, int, int *)
19517c478bd9Sstevel@tonic-gate  *
19527c478bd9Sstevel@tonic-gate  * Input
19537c478bd9Sstevel@tonic-gate  *    Supply a non-NULL string containing:
19547c478bd9Sstevel@tonic-gate  *	- logical project/zone name or project/zone number if type is
19557c478bd9Sstevel@tonic-gate  *	RCENTITY_PROJECT or RCENTITY_ZONE
19567c478bd9Sstevel@tonic-gate  *	- task number if type is RCENTITY_TYPE
19577c478bd9Sstevel@tonic-gate  *	- a pid if type is RCENTITY_PID
19587c478bd9Sstevel@tonic-gate  *    Also supply an un-allocated prochandle, and an allocated info_handle.
19597c478bd9Sstevel@tonic-gate  *    This function assumes that the type is set.
19607c478bd9Sstevel@tonic-gate  *    If priv is not RCPRIV_BASIC, the grabbed process is required to have
19617c478bd9Sstevel@tonic-gate  *    PRIV_SYS_RESOURCE in it's limit set.
19627c478bd9Sstevel@tonic-gate  *
19637c478bd9Sstevel@tonic-gate  * Return Values
19647c478bd9Sstevel@tonic-gate  *    Returns 0 on success and 1 on failure. If there is a process
19657c478bd9Sstevel@tonic-gate  *    running under the specified id, success is returned, and
19667c478bd9Sstevel@tonic-gate  *    Pr is pointed to the process. Success will be returned and Pr
19677c478bd9Sstevel@tonic-gate  *    set to NULL if the matching process is our own.
19687c478bd9Sstevel@tonic-gate  *    If success is returned, psinfo will be valid, and pid will
19697c478bd9Sstevel@tonic-gate  *    be the process number. The process will also be held at the
19707c478bd9Sstevel@tonic-gate  *    end, so release_process should be used by the caller.
19717c478bd9Sstevel@tonic-gate  *
19727c478bd9Sstevel@tonic-gate  * This function assumes that signals are caught already so that libproc
19737c478bd9Sstevel@tonic-gate  * can be safely used.
19747c478bd9Sstevel@tonic-gate  *
19757c478bd9Sstevel@tonic-gate  * Return Values
19767c478bd9Sstevel@tonic-gate  *	pid - Process found and grabbed
19777c478bd9Sstevel@tonic-gate  *	-1 - Error
19787c478bd9Sstevel@tonic-gate  */
19797c478bd9Sstevel@tonic-gate pid_t
19807c478bd9Sstevel@tonic-gate grab_process_by_id(char *idname, rctl_entity_t type, pr_info_handle_t *p,
19817c478bd9Sstevel@tonic-gate     int priv, int *gret)
19827c478bd9Sstevel@tonic-gate {
19837c478bd9Sstevel@tonic-gate 	char prbuf[PROJECT_BUFSZ];
19847c478bd9Sstevel@tonic-gate 	projid_t projid;
19857c478bd9Sstevel@tonic-gate 	taskid_t taskid;
19867c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
19877c478bd9Sstevel@tonic-gate 	zoneid_t zone_self;
19887c478bd9Sstevel@tonic-gate 	struct project proj;
19897c478bd9Sstevel@tonic-gate 	DIR *dirp;
19907c478bd9Sstevel@tonic-gate 	struct dirent *dentp;
19917c478bd9Sstevel@tonic-gate 	int found = 0;
19927c478bd9Sstevel@tonic-gate 	int pid_self;
19937c478bd9Sstevel@tonic-gate 	int ret;
19947c478bd9Sstevel@tonic-gate 	int gret_in;
19957c478bd9Sstevel@tonic-gate 	int intidname;
19967c478bd9Sstevel@tonic-gate 	char *end;
19977c478bd9Sstevel@tonic-gate 	prpriv_t *prpriv;
19987c478bd9Sstevel@tonic-gate 	priv_set_t *prset;
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	gret_in = *gret;
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	/* get our pid se we do not try to operate on self */
20037c478bd9Sstevel@tonic-gate 	pid_self = getpid();
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	/* Store integer version of id */
20067c478bd9Sstevel@tonic-gate 	intidname = strtoul(idname, &end, 10);
20077c478bd9Sstevel@tonic-gate 	if (errno || *end != '\0' || end == idname) {
20087c478bd9Sstevel@tonic-gate 		intidname = -1;
20097c478bd9Sstevel@tonic-gate 	}
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 	/*
20127c478bd9Sstevel@tonic-gate 	 * get our zoneid so we don't try to operate on a project in
20137c478bd9Sstevel@tonic-gate 	 * another zone
20147c478bd9Sstevel@tonic-gate 	 */
20157c478bd9Sstevel@tonic-gate 	zone_self = getzoneid();
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	if (idname == NULL || strcmp(idname, "") == 0) {
20187c478bd9Sstevel@tonic-gate 		warn(gettext("id name cannot be nuint64\n"));
20197c478bd9Sstevel@tonic-gate 		return (-1);
20207c478bd9Sstevel@tonic-gate 	}
20217c478bd9Sstevel@tonic-gate 	/*
20227c478bd9Sstevel@tonic-gate 	 * Set up zoneid, projid or taskid, as appropriate, so that comparisons
20237c478bd9Sstevel@tonic-gate 	 * can be done later with the input.
20247c478bd9Sstevel@tonic-gate 	 */
20257c478bd9Sstevel@tonic-gate 	if (type == RCENTITY_ZONE) {
20267c478bd9Sstevel@tonic-gate 		if (zone_get_id(idname, &zoneid) != 0) {
20277c478bd9Sstevel@tonic-gate 			warn(gettext("%s: unknown zone\n"), idname);
20287c478bd9Sstevel@tonic-gate 			return (-1);
20297c478bd9Sstevel@tonic-gate 		}
20307c478bd9Sstevel@tonic-gate 	} else if (type == RCENTITY_PROJECT) {
20317c478bd9Sstevel@tonic-gate 		if (getprojbyname(idname, &proj, prbuf, PROJECT_BUFSZ)
20327c478bd9Sstevel@tonic-gate 		    == NULL) {
20337c478bd9Sstevel@tonic-gate 			if (getprojbyid(intidname, &proj, prbuf,
20347c478bd9Sstevel@tonic-gate 			    PROJECT_BUFSZ) == NULL) {
20357c478bd9Sstevel@tonic-gate 				warn(gettext("%s: cannot find project\n"),
20367c478bd9Sstevel@tonic-gate 				    idname);
20377c478bd9Sstevel@tonic-gate 				return (-1);
20387c478bd9Sstevel@tonic-gate 			}
20397c478bd9Sstevel@tonic-gate 		}
20407c478bd9Sstevel@tonic-gate 		projid = proj.pj_projid;
20417c478bd9Sstevel@tonic-gate 	} else if (type == RCENTITY_TASK) {
20427c478bd9Sstevel@tonic-gate 		taskid = (taskid_t)atol(idname);
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate 	/*
20457c478bd9Sstevel@tonic-gate 	 * Projects and tasks need to search through /proc for
20467c478bd9Sstevel@tonic-gate 	 * a parent process.
20477c478bd9Sstevel@tonic-gate 	 */
20487c478bd9Sstevel@tonic-gate 	if (type == RCENTITY_ZONE || type == RCENTITY_PROJECT ||
20497c478bd9Sstevel@tonic-gate 	    type == RCENTITY_TASK) {
20507c478bd9Sstevel@tonic-gate 		if ((dirp = opendir("/proc")) == NULL) {
20517c478bd9Sstevel@tonic-gate 			warn(gettext("%s: cannot open /proc directory\n"),
20527c478bd9Sstevel@tonic-gate 			    idname);
20537c478bd9Sstevel@tonic-gate 			return (-1);
20547c478bd9Sstevel@tonic-gate 		}
20557c478bd9Sstevel@tonic-gate 		/*
20567c478bd9Sstevel@tonic-gate 		 * Look through all processes in /proc. For each process,
20577c478bd9Sstevel@tonic-gate 		 * check if the pr_projid in their psinfo matches the
20587c478bd9Sstevel@tonic-gate 		 * specified id.
20597c478bd9Sstevel@tonic-gate 		 */
20607c478bd9Sstevel@tonic-gate 		while (dentp = readdir(dirp)) {
20617c478bd9Sstevel@tonic-gate 			p->pid = atoi(dentp->d_name);
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 			/* Skip self */
20647c478bd9Sstevel@tonic-gate 			if (p->pid == pid_self)
20657c478bd9Sstevel@tonic-gate 				continue;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 			if (proc_get_psinfo(p->pid, &(p->psinfo)) != 0)
20687c478bd9Sstevel@tonic-gate 				continue;
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 			/* Skip process if it is not what we are looking for */
20717c478bd9Sstevel@tonic-gate 			if (type == RCENTITY_ZONE &&
20727c478bd9Sstevel@tonic-gate 			    (p->psinfo).pr_zoneid != zoneid) {
20737c478bd9Sstevel@tonic-gate 				continue;
2074*c3ea2840SMenno Lageman 			} else if (type == RCENTITY_PROJECT &&
20757c478bd9Sstevel@tonic-gate 			    ((p->psinfo).pr_projid != projid ||
20767c478bd9Sstevel@tonic-gate 			    (p->psinfo).pr_zoneid != zone_self)) {
20777c478bd9Sstevel@tonic-gate 				continue;
20787c478bd9Sstevel@tonic-gate 			} else if (type == RCENTITY_TASK &&
20797c478bd9Sstevel@tonic-gate 			    (p->psinfo).pr_taskid != taskid) {
20807c478bd9Sstevel@tonic-gate 				continue;
20817c478bd9Sstevel@tonic-gate 			}
20827c478bd9Sstevel@tonic-gate 			/* attempt to grab process */
20837c478bd9Sstevel@tonic-gate 			if (grab_process(p, gret) != 0)
20847c478bd9Sstevel@tonic-gate 				continue;
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 			/*
20877c478bd9Sstevel@tonic-gate 			 * Re-confirm that this process is still running as
20887c478bd9Sstevel@tonic-gate 			 * part	of the specified project or task.  If it
20897c478bd9Sstevel@tonic-gate 			 * doesn't match, release the process and return an
20907c478bd9Sstevel@tonic-gate 			 * error. This should only be done if the Pr struct is
20917c478bd9Sstevel@tonic-gate 			 * not NULL.
20927c478bd9Sstevel@tonic-gate 			 */
20937c478bd9Sstevel@tonic-gate 			if (type == RCENTITY_PROJECT) {
20947c478bd9Sstevel@tonic-gate 				if (pr_getprojid(p->pr) != projid ||
20957c478bd9Sstevel@tonic-gate 				    pr_getzoneid(p->pr) != zone_self) {
20967c478bd9Sstevel@tonic-gate 					release_process(p->pr);
20977c478bd9Sstevel@tonic-gate 					continue;
20987c478bd9Sstevel@tonic-gate 				}
20997c478bd9Sstevel@tonic-gate 			} else if (type == RCENTITY_TASK) {
21007c478bd9Sstevel@tonic-gate 				if (pr_gettaskid(p->pr) != taskid) {
21017c478bd9Sstevel@tonic-gate 					release_process(p->pr);
21027c478bd9Sstevel@tonic-gate 					continue;
21037c478bd9Sstevel@tonic-gate 				}
21047c478bd9Sstevel@tonic-gate 			} else if (type == RCENTITY_ZONE) {
21057c478bd9Sstevel@tonic-gate 				if (pr_getzoneid(p->pr) != zoneid) {
21067c478bd9Sstevel@tonic-gate 					release_process(p->pr);
21077c478bd9Sstevel@tonic-gate 					continue;
21087c478bd9Sstevel@tonic-gate 				}
21097c478bd9Sstevel@tonic-gate 			}
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 			/*
21127c478bd9Sstevel@tonic-gate 			 * If we are setting a privileged resource control,
21137c478bd9Sstevel@tonic-gate 			 * verify that process has PRIV_SYS_RESOURCE in it's
21147c478bd9Sstevel@tonic-gate 			 * limit set.  If it does not, then we will not be
21157c478bd9Sstevel@tonic-gate 			 * able to give this process the privilege it needs
21167c478bd9Sstevel@tonic-gate 			 * to set the resource control.
21177c478bd9Sstevel@tonic-gate 			 */
21187c478bd9Sstevel@tonic-gate 			if (priv != RCPRIV_BASIC) {
21197c478bd9Sstevel@tonic-gate 				prpriv = proc_get_priv(p->pid);
21207c478bd9Sstevel@tonic-gate 				if (prpriv == NULL) {
21217c478bd9Sstevel@tonic-gate 					release_process(p->pr);
21227c478bd9Sstevel@tonic-gate 					continue;
21237c478bd9Sstevel@tonic-gate 				}
21247c478bd9Sstevel@tonic-gate 				prset = (priv_set_t *)
21257c478bd9Sstevel@tonic-gate 				    &prpriv->pr_sets[prpriv->pr_setsize *
21267c478bd9Sstevel@tonic-gate 				    priv_getsetbyname(PRIV_LIMIT)];
21277c478bd9Sstevel@tonic-gate 				if (!priv_ismember(prset, PRIV_SYS_RESOURCE)) {
21287c478bd9Sstevel@tonic-gate 					release_process(p->pr);
21297c478bd9Sstevel@tonic-gate 					continue;
21307c478bd9Sstevel@tonic-gate 				}
21317c478bd9Sstevel@tonic-gate 			}
21327c478bd9Sstevel@tonic-gate 			found = 1;
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 			p->taskid = pr_gettaskid(p->pr);
21357c478bd9Sstevel@tonic-gate 			p->projid = pr_getprojid(p->pr);
21367c478bd9Sstevel@tonic-gate 			p->zoneid = pr_getzoneid(p->pr);
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 			break;
21397c478bd9Sstevel@tonic-gate 		}
21407c478bd9Sstevel@tonic-gate 		(void) closedir(dirp);
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 		if (found == 0) {
21437c478bd9Sstevel@tonic-gate 			warn(gettext("%s: No controllable process found in "
21447c478bd9Sstevel@tonic-gate 			    "task, project, or zone.\n"), idname);
21457c478bd9Sstevel@tonic-gate 			return (-1);
21467c478bd9Sstevel@tonic-gate 		}
21477c478bd9Sstevel@tonic-gate 		return (p->pid);
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	} else if (type == RCENTITY_PROCESS) {
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 		/* fail if self */
21527c478bd9Sstevel@tonic-gate 		if (p->pid == pid_self) {
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 			warn(gettext("%s: cannot control self"), idname);
21557c478bd9Sstevel@tonic-gate 			return (-1);
21567c478bd9Sstevel@tonic-gate 		}
21577c478bd9Sstevel@tonic-gate 		/*
21587c478bd9Sstevel@tonic-gate 		 * Process types need to be set up with the correct pid
21597c478bd9Sstevel@tonic-gate 		 * and psinfo structure.
21607c478bd9Sstevel@tonic-gate 		 */
21617c478bd9Sstevel@tonic-gate 		if ((p->pid = proc_arg_psinfo(idname, PR_ARG_PIDS,
21627c478bd9Sstevel@tonic-gate 		    &(p->psinfo), gret)) == -1) {
21637c478bd9Sstevel@tonic-gate 			warn(gettext("%s: cannot examine: %s"), idname,
21647c478bd9Sstevel@tonic-gate 			    Pgrab_error(*gret));
21657c478bd9Sstevel@tonic-gate 			return (-1);
21667c478bd9Sstevel@tonic-gate 		}
21677c478bd9Sstevel@tonic-gate 		/* grab process */
21687c478bd9Sstevel@tonic-gate 		ret = grab_process(p, gret);
21697c478bd9Sstevel@tonic-gate 		if (ret == 1) {
21707c478bd9Sstevel@tonic-gate 			/* Don't print error if G_SYS is allowed */
21717c478bd9Sstevel@tonic-gate 			if (gret_in == G_SYS && *gret == G_SYS) {
21727c478bd9Sstevel@tonic-gate 				return (-1);
21737c478bd9Sstevel@tonic-gate 			} else {
21747c478bd9Sstevel@tonic-gate 				warn(gettext("%s: cannot control: %s"), idname,
21757c478bd9Sstevel@tonic-gate 				    Pgrab_error(*gret));
21767c478bd9Sstevel@tonic-gate 				return (-1);
21777c478bd9Sstevel@tonic-gate 			}
21787c478bd9Sstevel@tonic-gate 		} else if (ret == 2) {
21797c478bd9Sstevel@tonic-gate 			ret = errno;
21807c478bd9Sstevel@tonic-gate 			warn(gettext("%s: cannot control: %s"), idname,
21817c478bd9Sstevel@tonic-gate 			    strerror(ret));
21827c478bd9Sstevel@tonic-gate 			return (-1);
21837c478bd9Sstevel@tonic-gate 		}
21847c478bd9Sstevel@tonic-gate 		p->taskid = pr_gettaskid(p->pr);
21857c478bd9Sstevel@tonic-gate 		p->projid = pr_getprojid(p->pr);
21867c478bd9Sstevel@tonic-gate 		p->zoneid = pr_getzoneid(p->pr);
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 		return (p->pid);
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	} else {
21917c478bd9Sstevel@tonic-gate 		warn(gettext("%s: unknown resource entity type %d\n"), idname,
21927c478bd9Sstevel@tonic-gate 		    type);
21937c478bd9Sstevel@tonic-gate 		return (-1);
21947c478bd9Sstevel@tonic-gate 	}
21957c478bd9Sstevel@tonic-gate }
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate /*
21987c478bd9Sstevel@tonic-gate  * Do the work required to manipulate a process through libproc.
21997c478bd9Sstevel@tonic-gate  * If grab_process() returns no errors (0), then release_process()
22007c478bd9Sstevel@tonic-gate  * must eventually be called.
22017c478bd9Sstevel@tonic-gate  *
22027c478bd9Sstevel@tonic-gate  * Return values:
22037c478bd9Sstevel@tonic-gate  *	0 Successful creation of agent thread
22047c478bd9Sstevel@tonic-gate  *	1 Error grabbing
22057c478bd9Sstevel@tonic-gate  *	2 Error creating agent
22067c478bd9Sstevel@tonic-gate  */
22077c478bd9Sstevel@tonic-gate int
22087c478bd9Sstevel@tonic-gate grab_process(pr_info_handle_t *p, int *gret)
22097c478bd9Sstevel@tonic-gate {
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 	if ((p->pr = Pgrab(p->pid, arg_force, gret)) != NULL) {
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 		if (Psetflags(p->pr, PR_RLC) != 0) {
22147c478bd9Sstevel@tonic-gate 			Prelease(p->pr, 0);
22157c478bd9Sstevel@tonic-gate 			return (1);
22167c478bd9Sstevel@tonic-gate 		}
22177c478bd9Sstevel@tonic-gate 		if (Pcreate_agent(p->pr) == 0) {
22187c478bd9Sstevel@tonic-gate 			return (0);
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 		} else {
22217c478bd9Sstevel@tonic-gate 			Prelease(p->pr, 0);
22227c478bd9Sstevel@tonic-gate 			return (2);
22237c478bd9Sstevel@tonic-gate 		}
22247c478bd9Sstevel@tonic-gate 	} else {
22257c478bd9Sstevel@tonic-gate 		return (1);
22267c478bd9Sstevel@tonic-gate 	}
22277c478bd9Sstevel@tonic-gate }
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate /*
22307c478bd9Sstevel@tonic-gate  * Release the specified process. This destroys the agent
22317c478bd9Sstevel@tonic-gate  * and releases the process. If the process is NULL, nothing
22327c478bd9Sstevel@tonic-gate  * is done. This function should only be called if grab_process()
22337c478bd9Sstevel@tonic-gate  * has previously been called and returned success.
22347c478bd9Sstevel@tonic-gate  *
22357c478bd9Sstevel@tonic-gate  * This function is Pgrab-safe.
22367c478bd9Sstevel@tonic-gate  */
22377c478bd9Sstevel@tonic-gate void
22387c478bd9Sstevel@tonic-gate release_process(struct ps_prochandle *Pr)
22397c478bd9Sstevel@tonic-gate {
22407c478bd9Sstevel@tonic-gate 	if (Pr == NULL)
22417c478bd9Sstevel@tonic-gate 		return;
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 	Pdestroy_agent(Pr);
22447c478bd9Sstevel@tonic-gate 	Prelease(Pr, 0);
22457c478bd9Sstevel@tonic-gate }
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate /*
22487c478bd9Sstevel@tonic-gate  * preserve_error(char *, ...)
22497c478bd9Sstevel@tonic-gate  *
22507c478bd9Sstevel@tonic-gate  * preserve_error() should be called rather than warn() by any
22517c478bd9Sstevel@tonic-gate  * function that is called while the victim process is held by Pgrab.
22527c478bd9Sstevel@tonic-gate  * It will save the error until the process has been un-controlled
22537c478bd9Sstevel@tonic-gate  * and output is reasonable again.
22547c478bd9Sstevel@tonic-gate  *
22557c478bd9Sstevel@tonic-gate  * Note that multiple errors are not stored. Any error in these
22567c478bd9Sstevel@tonic-gate  * sections should be critical and return immediately.
22577c478bd9Sstevel@tonic-gate  *
22587c478bd9Sstevel@tonic-gate  * This function is Pgrab-safe.
22597c478bd9Sstevel@tonic-gate  *
22607c478bd9Sstevel@tonic-gate  * Since this function may copy untrusted command line arguments to
22617c478bd9Sstevel@tonic-gate  * global_error, security practices require that global_error never be
22627c478bd9Sstevel@tonic-gate  * printed directly.  Use printf("%s\n", global_error) or equivalent.
22637c478bd9Sstevel@tonic-gate  */
22647c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
22657c478bd9Sstevel@tonic-gate void
22667c478bd9Sstevel@tonic-gate preserve_error(char *format, ...)
22677c478bd9Sstevel@tonic-gate {
22687c478bd9Sstevel@tonic-gate 	va_list alist;
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	va_start(alist, format);
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	/*
22737c478bd9Sstevel@tonic-gate 	 * GLOBAL_ERR_SZ is pretty big. If the error is longer
22747c478bd9Sstevel@tonic-gate 	 * than that, just truncate it, rather than chance missing
22757c478bd9Sstevel@tonic-gate 	 * the error altogether.
22767c478bd9Sstevel@tonic-gate 	 */
22777c478bd9Sstevel@tonic-gate 	(void) vsnprintf(global_error, GLOBAL_ERR_SZ-1, format, alist);
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	va_end(alist);
22807c478bd9Sstevel@tonic-gate }
2281