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 5c3ea2840SMenno Lageman * Common Development and Distribution License (the "License"). 6c3ea2840SMenno 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*23a1cceaSRoger A. Faulkner /* 23*23a1cceaSRoger A. Faulkner * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. 24*23a1cceaSRoger A. Faulkner */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <unistd.h> 277c478bd9Sstevel@tonic-gate #include <rctl.h> 287c478bd9Sstevel@tonic-gate #include <libproc.h> 297c478bd9Sstevel@tonic-gate #include <stdio.h> 307c478bd9Sstevel@tonic-gate #include <libintl.h> 317c478bd9Sstevel@tonic-gate #include <locale.h> 327c478bd9Sstevel@tonic-gate #include <string.h> 337c478bd9Sstevel@tonic-gate #include <signal.h> 347c478bd9Sstevel@tonic-gate #include <strings.h> 357c478bd9Sstevel@tonic-gate #include <ctype.h> 367c478bd9Sstevel@tonic-gate #include <project.h> 377c478bd9Sstevel@tonic-gate #include <sys/types.h> 387c478bd9Sstevel@tonic-gate #include <dirent.h> 397c478bd9Sstevel@tonic-gate #include <errno.h> 407c478bd9Sstevel@tonic-gate #include <stdlib.h> 417c478bd9Sstevel@tonic-gate #include <sys/varargs.h> 427c478bd9Sstevel@tonic-gate #include <priv.h> 437c478bd9Sstevel@tonic-gate #include <zone.h> 447c478bd9Sstevel@tonic-gate #include "utils.h" 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* Valid user actions */ 477c478bd9Sstevel@tonic-gate #define ACTION_DISABLE 0x01 487c478bd9Sstevel@tonic-gate #define ACTION_ENABLE 0x02 497c478bd9Sstevel@tonic-gate #define ACTION_SET 0x04 507c478bd9Sstevel@tonic-gate #define ACTION_REPLACE 0x08 517c478bd9Sstevel@tonic-gate #define ACTION_DELETE 0x10 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #define PRCTL_VALUE_WIDTH 4 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate /* Maximum string length for deferred errors */ 567c478bd9Sstevel@tonic-gate #define GLOBAL_ERR_SZ 1024 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate /* allow important process values to be passed together easily */ 597c478bd9Sstevel@tonic-gate typedef struct pr_info_handle { 607c478bd9Sstevel@tonic-gate struct ps_prochandle *pr; 617c478bd9Sstevel@tonic-gate pid_t pid; 627c478bd9Sstevel@tonic-gate psinfo_t psinfo; 637c478bd9Sstevel@tonic-gate taskid_t taskid; 647c478bd9Sstevel@tonic-gate projid_t projid; 657c478bd9Sstevel@tonic-gate char *projname; 667c478bd9Sstevel@tonic-gate zoneid_t zoneid; 677c478bd9Sstevel@tonic-gate char *zonename; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate } pr_info_handle_t; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* Structures for list of resource controls */ 727c478bd9Sstevel@tonic-gate typedef struct prctl_value { 737c478bd9Sstevel@tonic-gate rctlblk_t *rblk; 747c478bd9Sstevel@tonic-gate struct prctl_value *next; 757c478bd9Sstevel@tonic-gate } prctl_value_t; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate typedef struct prctl_list { 787c478bd9Sstevel@tonic-gate char *name; 7912835672SMenno Lageman rctl_qty_t *usage; 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); 225*23a1cceaSRoger A. Faulkner (void) setpname(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 485c3ea2840SMenno Lageman if ((strcmp(arg_action_string, "signal") == 0) || 486c3ea2840SMenno 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 && 857c3ea2840SMenno 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 } 874c3ea2840SMenno Lageman localaction = 875c3ea2840SMenno Lageman rctlblk_get_local_action(rctlblkA, &signal); 876c3ea2840SMenno Lageman rctlblk_set_local_action(rctlblkB, localaction, 877c3ea2840SMenno 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) || 1167c3ea2840SMenno Lageman strncmp(rctlname, arg_name, strlen(arg_name)) == 0) && 1168c3ea2840SMenno 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) || 1200c3ea2840SMenno 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 } 122312835672SMenno Lageman 122412835672SMenno Lageman /* 122512835672SMenno Lageman * Get the current usage for the resource control if it matched the 122612835672SMenno Lageman * privilege and entity type criteria. 122712835672SMenno Lageman */ 122812835672SMenno Lageman if (list != NULL) { 122912835672SMenno Lageman if (pr_getrctl(Pr, rctlname, NULL, rblk2, RCTL_USAGE) == 0) { 123012835672SMenno Lageman list->usage = (rctl_qty_t *)malloc(sizeof (rctl_qty_t)); 123112835672SMenno Lageman if (list->usage == NULL) { 123212835672SMenno Lageman preserve_error(gettext("malloc failed: %s"), 123312835672SMenno Lageman strerror(errno)); 123412835672SMenno Lageman free(rblk1); 123512835672SMenno Lageman free(rblk2); 123612835672SMenno Lageman return (1); 123712835672SMenno Lageman } 123812835672SMenno Lageman *list->usage = rctlblk_get_value(rblk2); 123912835672SMenno Lageman } else { 124012835672SMenno Lageman list->usage = NULL; 124112835672SMenno Lageman if (errno != ENOTSUP) { 124212835672SMenno Lageman preserve_error(gettext("failed to get " 124312835672SMenno Lageman "resource control usage for %s: %s"), 124412835672SMenno Lageman rctlname, strerror(errno)); 124512835672SMenno Lageman free(rblk1); 124612835672SMenno Lageman free(rblk2); 124712835672SMenno Lageman return (1); 124812835672SMenno Lageman } 124912835672SMenno Lageman } 125012835672SMenno Lageman } 12517c478bd9Sstevel@tonic-gate free(rblk1); 12527c478bd9Sstevel@tonic-gate free(rblk2); 12537c478bd9Sstevel@tonic-gate return (0); 12547c478bd9Sstevel@tonic-gate } 12557c478bd9Sstevel@tonic-gate 12567c478bd9Sstevel@tonic-gate /* 12577c478bd9Sstevel@tonic-gate * store_value_entry(rctlblk_t *, prctl_list_t *) 12587c478bd9Sstevel@tonic-gate * 12597c478bd9Sstevel@tonic-gate * Store an rblk for a given resource control into the global list. 12607c478bd9Sstevel@tonic-gate * 12617c478bd9Sstevel@tonic-gate * This function is Pgrab-safe. 12627c478bd9Sstevel@tonic-gate */ 12637c478bd9Sstevel@tonic-gate prctl_value_t * 12647c478bd9Sstevel@tonic-gate store_value_entry(rctlblk_t *rblk, prctl_list_t *list) 12657c478bd9Sstevel@tonic-gate { 12667c478bd9Sstevel@tonic-gate prctl_value_t *e = calloc(1, sizeof (prctl_value_t)); 12677c478bd9Sstevel@tonic-gate rctlblk_t *store_blk = calloc(1, rctlblk_size()); 12687c478bd9Sstevel@tonic-gate prctl_value_t *iter = list->val_list; 12697c478bd9Sstevel@tonic-gate 12707c478bd9Sstevel@tonic-gate if (e == NULL || store_blk == NULL) { 12717c478bd9Sstevel@tonic-gate preserve_error(gettext("malloc failed %s"), 12727c478bd9Sstevel@tonic-gate strerror(errno)); 12737c478bd9Sstevel@tonic-gate if (e != NULL) 12747c478bd9Sstevel@tonic-gate free(e); 12757c478bd9Sstevel@tonic-gate if (store_blk != NULL) 12767c478bd9Sstevel@tonic-gate free(store_blk); 12777c478bd9Sstevel@tonic-gate return (NULL); 12787c478bd9Sstevel@tonic-gate } 12797c478bd9Sstevel@tonic-gate if (iter == NULL) 12807c478bd9Sstevel@tonic-gate list->val_list = e; 12817c478bd9Sstevel@tonic-gate else { 12827c478bd9Sstevel@tonic-gate while (iter->next != NULL) { 12837c478bd9Sstevel@tonic-gate iter = iter->next; 12847c478bd9Sstevel@tonic-gate } 12857c478bd9Sstevel@tonic-gate iter->next = e; 12867c478bd9Sstevel@tonic-gate } 12877c478bd9Sstevel@tonic-gate bcopy(rblk, store_blk, rctlblk_size()); 12887c478bd9Sstevel@tonic-gate 12897c478bd9Sstevel@tonic-gate e->rblk = store_blk; 12907c478bd9Sstevel@tonic-gate e->next = NULL; 12917c478bd9Sstevel@tonic-gate return (e); 12927c478bd9Sstevel@tonic-gate } 12937c478bd9Sstevel@tonic-gate 12947c478bd9Sstevel@tonic-gate /* 12957c478bd9Sstevel@tonic-gate * store_list_entry(const char *) 12967c478bd9Sstevel@tonic-gate * 12977c478bd9Sstevel@tonic-gate * Store a new resource control value in the global list. No checking 12987c478bd9Sstevel@tonic-gate * for duplicates done. 12997c478bd9Sstevel@tonic-gate * 13007c478bd9Sstevel@tonic-gate * This function is Pgrab-safe. 13017c478bd9Sstevel@tonic-gate */ 13027c478bd9Sstevel@tonic-gate prctl_list_t * 13037c478bd9Sstevel@tonic-gate store_list_entry(const char *name) 13047c478bd9Sstevel@tonic-gate { 13057c478bd9Sstevel@tonic-gate prctl_list_t *e = calloc(1, sizeof (prctl_list_t)); 13067c478bd9Sstevel@tonic-gate 13077c478bd9Sstevel@tonic-gate if (e == NULL) { 13087c478bd9Sstevel@tonic-gate preserve_error(gettext("malloc failed %s"), 13097c478bd9Sstevel@tonic-gate strerror(errno)); 13107c478bd9Sstevel@tonic-gate return (NULL); 13117c478bd9Sstevel@tonic-gate } 13127c478bd9Sstevel@tonic-gate if ((e->name = calloc(1, strlen(name) + 1)) == NULL) { 13137c478bd9Sstevel@tonic-gate preserve_error(gettext("malloc failed %s"), 13147c478bd9Sstevel@tonic-gate strerror(errno)); 13157c478bd9Sstevel@tonic-gate free(e); 13167c478bd9Sstevel@tonic-gate return (NULL); 13177c478bd9Sstevel@tonic-gate } 13187c478bd9Sstevel@tonic-gate (void) strcpy(e->name, name); 13197c478bd9Sstevel@tonic-gate e->val_list = NULL; 13207c478bd9Sstevel@tonic-gate 13217c478bd9Sstevel@tonic-gate if (global_rctl_list_head == NULL) { 13227c478bd9Sstevel@tonic-gate global_rctl_list_head = e; 13237c478bd9Sstevel@tonic-gate global_rctl_list_tail = e; 13247c478bd9Sstevel@tonic-gate } else { 13257c478bd9Sstevel@tonic-gate global_rctl_list_tail->next = e; 13267c478bd9Sstevel@tonic-gate global_rctl_list_tail = e; 13277c478bd9Sstevel@tonic-gate } 13287c478bd9Sstevel@tonic-gate e->next = NULL; 13297c478bd9Sstevel@tonic-gate return (e); 13307c478bd9Sstevel@tonic-gate } 13317c478bd9Sstevel@tonic-gate 13327c478bd9Sstevel@tonic-gate /* 13337c478bd9Sstevel@tonic-gate * free_lists() 13347c478bd9Sstevel@tonic-gate * 13357c478bd9Sstevel@tonic-gate * Free all resource control blocks and values from the global lists. 13367c478bd9Sstevel@tonic-gate * 13377c478bd9Sstevel@tonic-gate * This function is Pgrab-safe. 13387c478bd9Sstevel@tonic-gate */ 13397c478bd9Sstevel@tonic-gate void 13407c478bd9Sstevel@tonic-gate free_lists() 13417c478bd9Sstevel@tonic-gate { 13427c478bd9Sstevel@tonic-gate prctl_list_t *new_list, *old_list = global_rctl_list_head; 13437c478bd9Sstevel@tonic-gate prctl_value_t *old_val, *new_val; 13447c478bd9Sstevel@tonic-gate 13457c478bd9Sstevel@tonic-gate while (old_list != NULL) { 13467c478bd9Sstevel@tonic-gate old_val = old_list->val_list; 13477c478bd9Sstevel@tonic-gate while (old_val != NULL) { 13487c478bd9Sstevel@tonic-gate free(old_val->rblk); 13497c478bd9Sstevel@tonic-gate new_val = old_val->next; 13507c478bd9Sstevel@tonic-gate free(old_val); 13517c478bd9Sstevel@tonic-gate old_val = new_val; 13527c478bd9Sstevel@tonic-gate } 13537c478bd9Sstevel@tonic-gate free(old_list->name); 135412835672SMenno Lageman free(old_list->usage); 13557c478bd9Sstevel@tonic-gate new_list = old_list->next; 13567c478bd9Sstevel@tonic-gate free(old_list); 13577c478bd9Sstevel@tonic-gate old_list = new_list; 13587c478bd9Sstevel@tonic-gate } 13597c478bd9Sstevel@tonic-gate global_rctl_list_head = NULL; 13607c478bd9Sstevel@tonic-gate global_rctl_list_tail = NULL; 13617c478bd9Sstevel@tonic-gate } 13627c478bd9Sstevel@tonic-gate 13637c478bd9Sstevel@tonic-gate void 13647c478bd9Sstevel@tonic-gate print_heading() 13657c478bd9Sstevel@tonic-gate { 13667c478bd9Sstevel@tonic-gate 13677c478bd9Sstevel@tonic-gate /* print headings */ 13687c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%-8s%-16s%-9s%-7s%-28s%10s\n", 13697c478bd9Sstevel@tonic-gate "NAME", "PRIVILEGE", "VALUE", 13707c478bd9Sstevel@tonic-gate "FLAG", "ACTION", "RECIPIENT"); 13717c478bd9Sstevel@tonic-gate } 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate /* 13747c478bd9Sstevel@tonic-gate * print_rctls() 13757c478bd9Sstevel@tonic-gate * 13767c478bd9Sstevel@tonic-gate * Print all resource controls from the global list that was 13777c478bd9Sstevel@tonic-gate * previously populated by store_rctls. 13787c478bd9Sstevel@tonic-gate */ 13797c478bd9Sstevel@tonic-gate void 13807c478bd9Sstevel@tonic-gate print_rctls(pr_info_handle_t *p) 13817c478bd9Sstevel@tonic-gate { 13827c478bd9Sstevel@tonic-gate prctl_list_t *iter_list = global_rctl_list_head; 13837c478bd9Sstevel@tonic-gate prctl_value_t *iter_val; 13847c478bd9Sstevel@tonic-gate rctl_qty_t rblk_value; 13857c478bd9Sstevel@tonic-gate rctl_priv_t rblk_priv; 13867c478bd9Sstevel@tonic-gate uint_t local_action; 13877c478bd9Sstevel@tonic-gate int signal, local_flags, global_flags; 13887c478bd9Sstevel@tonic-gate pid_t pid; 13897c478bd9Sstevel@tonic-gate char rctl_valuestring[SCALED_STRLEN]; 13907c478bd9Sstevel@tonic-gate char *unit = NULL; 13917c478bd9Sstevel@tonic-gate scale_t *scale; 13927c478bd9Sstevel@tonic-gate char *string; 13937c478bd9Sstevel@tonic-gate int doneheading = 0; 13947c478bd9Sstevel@tonic-gate 13957c478bd9Sstevel@tonic-gate if (iter_list == NULL) 13967c478bd9Sstevel@tonic-gate return; 13977c478bd9Sstevel@tonic-gate 13987c478bd9Sstevel@tonic-gate while (iter_list != NULL) { 13997c478bd9Sstevel@tonic-gate 14007c478bd9Sstevel@tonic-gate if (doneheading == 0 && 14017c478bd9Sstevel@tonic-gate arg_entity_type == RCENTITY_PROCESS) { 14027c478bd9Sstevel@tonic-gate proc_unctrl_psinfo(&(p->psinfo)); 14037c478bd9Sstevel@tonic-gate doneheading = 1; 14047c478bd9Sstevel@tonic-gate (void) fprintf(stdout, 14057c478bd9Sstevel@tonic-gate "process: %d: %.70s\n", (int)p->pid, 14067c478bd9Sstevel@tonic-gate p->psinfo.pr_psargs); 14077c478bd9Sstevel@tonic-gate if (!arg_parseable_mode) 14087c478bd9Sstevel@tonic-gate print_heading(); 14097c478bd9Sstevel@tonic-gate } 14107c478bd9Sstevel@tonic-gate if (doneheading == 0 && 14117c478bd9Sstevel@tonic-gate arg_entity_type == RCENTITY_TASK) { 14127c478bd9Sstevel@tonic-gate doneheading = 1; 14137c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "task: %d\n", (int)p->taskid); 14147c478bd9Sstevel@tonic-gate if (!arg_parseable_mode) 14157c478bd9Sstevel@tonic-gate print_heading(); 14167c478bd9Sstevel@tonic-gate } 14177c478bd9Sstevel@tonic-gate if (doneheading == 0 && 14187c478bd9Sstevel@tonic-gate arg_entity_type == RCENTITY_PROJECT) { 14197c478bd9Sstevel@tonic-gate if (!arg_parseable_mode && doneheading) 14207c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "\n"); 14217c478bd9Sstevel@tonic-gate doneheading = 1; 14227c478bd9Sstevel@tonic-gate (void) fprintf(stdout, 14237c478bd9Sstevel@tonic-gate "project: %d: %.70s\n", (int)p->projid, 14247c478bd9Sstevel@tonic-gate p->projname); 14257c478bd9Sstevel@tonic-gate if (!arg_parseable_mode) 14267c478bd9Sstevel@tonic-gate print_heading(); 14277c478bd9Sstevel@tonic-gate } 14287c478bd9Sstevel@tonic-gate if (doneheading == 0 && 14297c478bd9Sstevel@tonic-gate arg_entity_type == RCENTITY_ZONE) { 14307c478bd9Sstevel@tonic-gate doneheading = 1; 14317c478bd9Sstevel@tonic-gate (void) fprintf(stdout, 14327c478bd9Sstevel@tonic-gate "zone: %d: %.70s\n", (int)p->zoneid, 14337c478bd9Sstevel@tonic-gate p->zonename); 14347c478bd9Sstevel@tonic-gate if (!arg_parseable_mode) 14357c478bd9Sstevel@tonic-gate print_heading(); 14367c478bd9Sstevel@tonic-gate } 14377c478bd9Sstevel@tonic-gate /* only print name once in normal output */ 14387c478bd9Sstevel@tonic-gate if (!arg_parseable_mode) 14397c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", iter_list->name); 14407c478bd9Sstevel@tonic-gate 14417c478bd9Sstevel@tonic-gate iter_val = iter_list->val_list; 14427c478bd9Sstevel@tonic-gate 14437c478bd9Sstevel@tonic-gate /* if for some reason there are no values, skip */ 14447c478bd9Sstevel@tonic-gate if (iter_val == 0) 14457c478bd9Sstevel@tonic-gate continue; 14467c478bd9Sstevel@tonic-gate 14477c478bd9Sstevel@tonic-gate 14487c478bd9Sstevel@tonic-gate /* get the global flags the first rctl only */ 14497c478bd9Sstevel@tonic-gate global_flags = rctlblk_get_global_flags(iter_val->rblk); 14507c478bd9Sstevel@tonic-gate 14517c478bd9Sstevel@tonic-gate 14527c478bd9Sstevel@tonic-gate if (global_flags & RCTL_GLOBAL_BYTES) { 14537c478bd9Sstevel@tonic-gate unit = SCALED_UNIT_BYTES; 14547c478bd9Sstevel@tonic-gate scale = scale_binary; 14557c478bd9Sstevel@tonic-gate 14567c478bd9Sstevel@tonic-gate } else if (global_flags & RCTL_GLOBAL_SECONDS) { 14577c478bd9Sstevel@tonic-gate unit = SCALED_UNIT_SECONDS; 14587c478bd9Sstevel@tonic-gate scale = scale_metric; 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate } else { 14617c478bd9Sstevel@tonic-gate unit = SCALED_UNIT_NONE; 14627c478bd9Sstevel@tonic-gate scale = scale_metric; 14637c478bd9Sstevel@tonic-gate } 146412835672SMenno Lageman 146512835672SMenno Lageman /* print the current usage for the rctl if available */ 146612835672SMenno Lageman if (iter_list->usage != NULL) { 146712835672SMenno Lageman rblk_value = *(iter_list->usage); 146812835672SMenno Lageman if (!arg_parseable_mode) { 146912835672SMenno Lageman (void) uint64toscaled(rblk_value, 4, "E", 147012835672SMenno Lageman rctl_valuestring, NULL, NULL, 147112835672SMenno Lageman scale, NULL, 0); 147212835672SMenno Lageman 147312835672SMenno Lageman (void) fprintf(stdout, "%8s%-16s%5s%-4s\n", 147412835672SMenno Lageman "", "usage", rctl_valuestring, unit); 147512835672SMenno Lageman } else { 147612835672SMenno Lageman (void) fprintf(stdout, "%s %s %llu - - -\n", 147712835672SMenno Lageman iter_list->name, "usage", rblk_value); 147812835672SMenno Lageman } 147912835672SMenno Lageman } 148012835672SMenno Lageman 14817c478bd9Sstevel@tonic-gate /* iterate over an print all control values */ 14827c478bd9Sstevel@tonic-gate while (iter_val != NULL) { 14837c478bd9Sstevel@tonic-gate 14847c478bd9Sstevel@tonic-gate /* print name or empty name field */ 14857c478bd9Sstevel@tonic-gate if (!arg_parseable_mode) 14867c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%8s", ""); 14877c478bd9Sstevel@tonic-gate else 14887c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s ", iter_list->name); 14897c478bd9Sstevel@tonic-gate 14907c478bd9Sstevel@tonic-gate 14917c478bd9Sstevel@tonic-gate rblk_priv = rctlblk_get_privilege(iter_val->rblk); 14927c478bd9Sstevel@tonic-gate if (!arg_parseable_mode) 14937c478bd9Sstevel@tonic-gate print_priv(rblk_priv, "%-16s"); 14947c478bd9Sstevel@tonic-gate else 14957c478bd9Sstevel@tonic-gate print_priv(rblk_priv, "%s "); 14967c478bd9Sstevel@tonic-gate 14977c478bd9Sstevel@tonic-gate rblk_value = rctlblk_get_value(iter_val->rblk); 14987c478bd9Sstevel@tonic-gate if (arg_parseable_mode) { 14997c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%llu ", rblk_value); 15007c478bd9Sstevel@tonic-gate 15017c478bd9Sstevel@tonic-gate } else { 15027c478bd9Sstevel@tonic-gate 15037c478bd9Sstevel@tonic-gate (void) uint64toscaled(rblk_value, 4, "E", 15047c478bd9Sstevel@tonic-gate rctl_valuestring, NULL, NULL, 15057c478bd9Sstevel@tonic-gate scale, NULL, 0); 15067c478bd9Sstevel@tonic-gate 15077c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%5s", 15087c478bd9Sstevel@tonic-gate rctl_valuestring); 15097c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%-4s", unit); 15107c478bd9Sstevel@tonic-gate } 15117c478bd9Sstevel@tonic-gate local_flags = rctlblk_get_local_flags(iter_val->rblk); 15127c478bd9Sstevel@tonic-gate 15137c478bd9Sstevel@tonic-gate if (local_flags & RCTL_LOCAL_MAXIMAL) { 15147c478bd9Sstevel@tonic-gate 15157c478bd9Sstevel@tonic-gate if (global_flags & RCTL_GLOBAL_INFINITE) { 15167c478bd9Sstevel@tonic-gate string = "inf"; 15177c478bd9Sstevel@tonic-gate } else { 15187c478bd9Sstevel@tonic-gate string = "max"; 15197c478bd9Sstevel@tonic-gate } 15207c478bd9Sstevel@tonic-gate } else { 15217c478bd9Sstevel@tonic-gate string = "-"; 15227c478bd9Sstevel@tonic-gate } 15237c478bd9Sstevel@tonic-gate if (arg_parseable_mode) 15247c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s ", string); 15257c478bd9Sstevel@tonic-gate else 15267c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%4s%3s", 15277c478bd9Sstevel@tonic-gate string, ""); 15287c478bd9Sstevel@tonic-gate 15297c478bd9Sstevel@tonic-gate 15307c478bd9Sstevel@tonic-gate local_action = rctlblk_get_local_action(iter_val->rblk, 15317c478bd9Sstevel@tonic-gate &signal); 15327c478bd9Sstevel@tonic-gate 15337c478bd9Sstevel@tonic-gate if (arg_parseable_mode) 15347c478bd9Sstevel@tonic-gate print_local_action(local_action, &signal, 15357c478bd9Sstevel@tonic-gate "%s "); 15367c478bd9Sstevel@tonic-gate else 15377c478bd9Sstevel@tonic-gate print_local_action(local_action, &signal, 15387c478bd9Sstevel@tonic-gate "%-28s"); 15397c478bd9Sstevel@tonic-gate 15407c478bd9Sstevel@tonic-gate pid = rctlblk_get_recipient_pid(iter_val->rblk); 15417c478bd9Sstevel@tonic-gate 15427c478bd9Sstevel@tonic-gate if (arg_parseable_mode) { 15437c478bd9Sstevel@tonic-gate if (pid < 0) { 15447c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", "-"); 15457c478bd9Sstevel@tonic-gate } else { 15467c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%d\n", 15477c478bd9Sstevel@tonic-gate (int)pid); 15487c478bd9Sstevel@tonic-gate } 15497c478bd9Sstevel@tonic-gate } else { 15507c478bd9Sstevel@tonic-gate if (pid < 0) { 15517c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%10s\n", "-"); 15527c478bd9Sstevel@tonic-gate } else { 15537c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%10d\n", 15547c478bd9Sstevel@tonic-gate (int)pid); 15557c478bd9Sstevel@tonic-gate } 15567c478bd9Sstevel@tonic-gate } 15577c478bd9Sstevel@tonic-gate iter_val = iter_val->next; 15587c478bd9Sstevel@tonic-gate } 15597c478bd9Sstevel@tonic-gate iter_list = iter_list->next; 15607c478bd9Sstevel@tonic-gate } 15617c478bd9Sstevel@tonic-gate } 15627c478bd9Sstevel@tonic-gate 15637c478bd9Sstevel@tonic-gate /* 15647c478bd9Sstevel@tonic-gate * 15657c478bd9Sstevel@tonic-gate * match_rctl 15667c478bd9Sstevel@tonic-gate * 15677c478bd9Sstevel@tonic-gate * find the first rctl with matching name, value, priv, and recipient pid 15687c478bd9Sstevel@tonic-gate */ 15697c478bd9Sstevel@tonic-gate int 15707c478bd9Sstevel@tonic-gate match_rctl(struct ps_prochandle *Pr, rctlblk_t **rctl, char *name, 15717c478bd9Sstevel@tonic-gate char *valuestringin, int valuein, rctl_priv_t privin, int pidin) 15727c478bd9Sstevel@tonic-gate { 15737c478bd9Sstevel@tonic-gate rctlblk_t *next; 15747c478bd9Sstevel@tonic-gate rctlblk_t *last; 15757c478bd9Sstevel@tonic-gate rctlblk_t *tmp; 15767c478bd9Sstevel@tonic-gate 15777c478bd9Sstevel@tonic-gate *rctl = NULL; 15787c478bd9Sstevel@tonic-gate 15797c478bd9Sstevel@tonic-gate next = calloc(1, rctlblk_size()); 15807c478bd9Sstevel@tonic-gate last = calloc(1, rctlblk_size()); 15817c478bd9Sstevel@tonic-gate 15827c478bd9Sstevel@tonic-gate if ((last == NULL) || (next == NULL)) { 15837c478bd9Sstevel@tonic-gate preserve_error(gettext("malloc failed"), strerror(errno)); 15847c478bd9Sstevel@tonic-gate return (-1); 15857c478bd9Sstevel@tonic-gate } 15867c478bd9Sstevel@tonic-gate /* 15877c478bd9Sstevel@tonic-gate * For this resource name, now iterate through all 15887c478bd9Sstevel@tonic-gate * the controls, looking for a match to the 15897c478bd9Sstevel@tonic-gate * user-specified input. 15907c478bd9Sstevel@tonic-gate */ 15917c478bd9Sstevel@tonic-gate if (pr_getrctl(Pr, name, NULL, next, RCTL_FIRST)) { 15927c478bd9Sstevel@tonic-gate preserve_error(gettext("failed to get resource control " 15937c478bd9Sstevel@tonic-gate "for %s: %s"), name, strerror(errno)); 15947c478bd9Sstevel@tonic-gate return (-1); 15957c478bd9Sstevel@tonic-gate } 15967c478bd9Sstevel@tonic-gate if (match_rctl_blk(next, valuestringin, valuein, privin, pidin) == 1) { 15977c478bd9Sstevel@tonic-gate free(last); 15987c478bd9Sstevel@tonic-gate *rctl = next; 15997c478bd9Sstevel@tonic-gate return (0); 16007c478bd9Sstevel@tonic-gate } 16017c478bd9Sstevel@tonic-gate tmp = next; 16027c478bd9Sstevel@tonic-gate next = last; 16037c478bd9Sstevel@tonic-gate last = tmp; 16047c478bd9Sstevel@tonic-gate 16057c478bd9Sstevel@tonic-gate while (pr_getrctl(Pr, name, last, next, RCTL_NEXT) == 0) { 16067c478bd9Sstevel@tonic-gate 16077c478bd9Sstevel@tonic-gate /* allow user interrupt */ 16087c478bd9Sstevel@tonic-gate if (interrupt) 16097c478bd9Sstevel@tonic-gate break; 16107c478bd9Sstevel@tonic-gate 16117c478bd9Sstevel@tonic-gate if (match_rctl_blk(next, valuestringin, valuein, privin, pidin) 16127c478bd9Sstevel@tonic-gate == 1) { 16137c478bd9Sstevel@tonic-gate free(last); 16147c478bd9Sstevel@tonic-gate *rctl = next; 16157c478bd9Sstevel@tonic-gate return (0); 16167c478bd9Sstevel@tonic-gate } 16177c478bd9Sstevel@tonic-gate tmp = next; 16187c478bd9Sstevel@tonic-gate next = last; 16197c478bd9Sstevel@tonic-gate last = tmp; 16207c478bd9Sstevel@tonic-gate } 16217c478bd9Sstevel@tonic-gate free(next); 16227c478bd9Sstevel@tonic-gate free(last); 16237c478bd9Sstevel@tonic-gate 16247c478bd9Sstevel@tonic-gate return (1); 16257c478bd9Sstevel@tonic-gate } 16267c478bd9Sstevel@tonic-gate 16277c478bd9Sstevel@tonic-gate /* 16287c478bd9Sstevel@tonic-gate * int match_rctl_blk(rctlblk_t *, char *, uint64, rctl_priv_t, int pid) 16297c478bd9Sstevel@tonic-gate * 16307c478bd9Sstevel@tonic-gate * Input 16317c478bd9Sstevel@tonic-gate * Must supply a valid rctl, value, privilege, and pid to match on. 16327c478bd9Sstevel@tonic-gate * If valuestring is NULL, then valuestring and valuein will not be used 16337c478bd9Sstevel@tonic-gate * If privilege type is 0 it will not be used. 16347c478bd9Sstevel@tonic-gate * If pid is -1 it will not be used. 16357c478bd9Sstevel@tonic-gate * 16367c478bd9Sstevel@tonic-gate * Return values 16377c478bd9Sstevel@tonic-gate * Returns 1 if a matching rctl given matches the parameters specified, and 16387c478bd9Sstevel@tonic-gate * 0 if they do not. 16397c478bd9Sstevel@tonic-gate * 16407c478bd9Sstevel@tonic-gate * This function is Pgrab-safe. 16417c478bd9Sstevel@tonic-gate */ 16427c478bd9Sstevel@tonic-gate int 16437c478bd9Sstevel@tonic-gate match_rctl_blk(rctlblk_t *rctl, char *valuestringin, 16447c478bd9Sstevel@tonic-gate uint64_t valuein, rctl_priv_t privin, int pidin) 16457c478bd9Sstevel@tonic-gate { 16467c478bd9Sstevel@tonic-gate 16477c478bd9Sstevel@tonic-gate rctl_qty_t value; 16487c478bd9Sstevel@tonic-gate rctl_priv_t priv; 16497c478bd9Sstevel@tonic-gate pid_t pid; 16507c478bd9Sstevel@tonic-gate int valuematch = 1; 16517c478bd9Sstevel@tonic-gate int privmatch = 1; 16527c478bd9Sstevel@tonic-gate int pidmatch = 1; 16537c478bd9Sstevel@tonic-gate 16547c478bd9Sstevel@tonic-gate value = rctlblk_get_value(rctl); 16557c478bd9Sstevel@tonic-gate priv = rctlblk_get_privilege(rctl); 16567c478bd9Sstevel@tonic-gate pid = rctlblk_get_recipient_pid(rctl); 16577c478bd9Sstevel@tonic-gate 16587c478bd9Sstevel@tonic-gate if (valuestringin) { 16597c478bd9Sstevel@tonic-gate 16607c478bd9Sstevel@tonic-gate if (arg_modifier == NULL) { 16617c478bd9Sstevel@tonic-gate valuematch = (valuein == value); 16627c478bd9Sstevel@tonic-gate } else { 16637c478bd9Sstevel@tonic-gate valuematch = scaledequint64(valuestringin, value, 16647c478bd9Sstevel@tonic-gate PRCTL_VALUE_WIDTH, 16657c478bd9Sstevel@tonic-gate arg_scale, arg_unit, 16667c478bd9Sstevel@tonic-gate SCALED_ALL_FLAGS); 16677c478bd9Sstevel@tonic-gate } 16687c478bd9Sstevel@tonic-gate } 16697c478bd9Sstevel@tonic-gate if (privin != 0) { 16707c478bd9Sstevel@tonic-gate privmatch = (privin == priv); 16717c478bd9Sstevel@tonic-gate } 16727c478bd9Sstevel@tonic-gate if (pidin != -1) { 16737c478bd9Sstevel@tonic-gate pidmatch = (pidin == pid); 16747c478bd9Sstevel@tonic-gate } 16757c478bd9Sstevel@tonic-gate return (valuematch && privmatch && pidmatch); 16767c478bd9Sstevel@tonic-gate } 16777c478bd9Sstevel@tonic-gate 16787c478bd9Sstevel@tonic-gate static int 16797c478bd9Sstevel@tonic-gate change_action(rctlblk_t *blk) 16807c478bd9Sstevel@tonic-gate { 16817c478bd9Sstevel@tonic-gate int signal = 0; 16827c478bd9Sstevel@tonic-gate int action; 16837c478bd9Sstevel@tonic-gate 16847c478bd9Sstevel@tonic-gate action = rctlblk_get_local_action(blk, &signal); 16857c478bd9Sstevel@tonic-gate 16867c478bd9Sstevel@tonic-gate if (arg_operation & ACTION_ENABLE) { 16877c478bd9Sstevel@tonic-gate 16887c478bd9Sstevel@tonic-gate if (arg_action & RCTL_LOCAL_SIGNAL) { 16897c478bd9Sstevel@tonic-gate signal = arg_signal; 16907c478bd9Sstevel@tonic-gate } 16917c478bd9Sstevel@tonic-gate action = action | arg_action; 16927c478bd9Sstevel@tonic-gate /* add local action */ 16937c478bd9Sstevel@tonic-gate rctlblk_set_local_action(blk, action, signal); 16947c478bd9Sstevel@tonic-gate 16957c478bd9Sstevel@tonic-gate } else if (arg_operation & ACTION_DISABLE) { 16967c478bd9Sstevel@tonic-gate 16977c478bd9Sstevel@tonic-gate /* 16987c478bd9Sstevel@tonic-gate * if deleting signal and signal number is specified, 16997c478bd9Sstevel@tonic-gate * then signal number must match 17007c478bd9Sstevel@tonic-gate */ 17017c478bd9Sstevel@tonic-gate if ((arg_action & RCTL_LOCAL_SIGNAL) && 17027c478bd9Sstevel@tonic-gate (arg_signal != -1)) { 17037c478bd9Sstevel@tonic-gate if (arg_signal != signal) { 17047c478bd9Sstevel@tonic-gate preserve_error(gettext("signal name or number " 17057c478bd9Sstevel@tonic-gate "does not match existing action")); 17067c478bd9Sstevel@tonic-gate return (-1); 17077c478bd9Sstevel@tonic-gate } 17087c478bd9Sstevel@tonic-gate } 17097c478bd9Sstevel@tonic-gate /* remove local action */ 17107c478bd9Sstevel@tonic-gate action = action & (~arg_action); 17117c478bd9Sstevel@tonic-gate rctlblk_set_local_action(blk, RCTL_LOCAL_NOACTION, 0); 17127c478bd9Sstevel@tonic-gate rctlblk_set_local_action(blk, action, signal); 17137c478bd9Sstevel@tonic-gate } 17147c478bd9Sstevel@tonic-gate /* enable deny if it must be enabled */ 17157c478bd9Sstevel@tonic-gate if (arg_global_flags & RCTL_GLOBAL_DENY_ALWAYS) { 17167c478bd9Sstevel@tonic-gate rctlblk_set_local_action(blk, RCTL_LOCAL_DENY | action, 17177c478bd9Sstevel@tonic-gate signal); 17187c478bd9Sstevel@tonic-gate } 17197c478bd9Sstevel@tonic-gate return (0); 17207c478bd9Sstevel@tonic-gate } 17217c478bd9Sstevel@tonic-gate 17227c478bd9Sstevel@tonic-gate /* 17237c478bd9Sstevel@tonic-gate * prctl_setrctl 17247c478bd9Sstevel@tonic-gate * 17257c478bd9Sstevel@tonic-gate * Input 17267c478bd9Sstevel@tonic-gate * This function expects that input has been validated. In the 17277c478bd9Sstevel@tonic-gate * case of a replace operation, both old_rblk and new_rblk must 17287c478bd9Sstevel@tonic-gate * be valid resource controls. If a resource control is being 17297c478bd9Sstevel@tonic-gate * created, only new_rblk must be supplied. If a resource control 17307c478bd9Sstevel@tonic-gate * is being deleted, only new_rblk must be supplied. 17317c478bd9Sstevel@tonic-gate * 17327c478bd9Sstevel@tonic-gate * If the privilege is a priviliged type, at this time, the process 17337c478bd9Sstevel@tonic-gate * tries to take on superuser privileges. 17347c478bd9Sstevel@tonic-gate */ 17357c478bd9Sstevel@tonic-gate int 17367c478bd9Sstevel@tonic-gate prctl_setrctl(struct ps_prochandle *Pr, const char *name, 17377c478bd9Sstevel@tonic-gate rctlblk_t *old_rblk, rctlblk_t *new_rblk, uint_t flags) 17387c478bd9Sstevel@tonic-gate { 17397c478bd9Sstevel@tonic-gate int ret = 0; 17407c478bd9Sstevel@tonic-gate rctl_priv_t rblk_priv; 17417c478bd9Sstevel@tonic-gate psinfo_t psinfo; 17427c478bd9Sstevel@tonic-gate zoneid_t oldzoneid = GLOBAL_ZONEID; 17437c478bd9Sstevel@tonic-gate prpriv_t *old_prpriv = NULL, *new_prpriv = NULL; 17447c478bd9Sstevel@tonic-gate priv_set_t *eset, *pset; 17457c478bd9Sstevel@tonic-gate boolean_t relinquish_failed = B_FALSE; 17467c478bd9Sstevel@tonic-gate 17477c478bd9Sstevel@tonic-gate rblk_priv = rctlblk_get_privilege(new_rblk); 17487c478bd9Sstevel@tonic-gate 17497c478bd9Sstevel@tonic-gate if (rblk_priv == RCPRIV_SYSTEM) { 17507c478bd9Sstevel@tonic-gate preserve_error(gettext("cannot modify system values")); 17517c478bd9Sstevel@tonic-gate return (1); 17527c478bd9Sstevel@tonic-gate } 17537c478bd9Sstevel@tonic-gate if (rblk_priv == RCPRIV_PRIVILEGED) { 17547c478bd9Sstevel@tonic-gate new_prpriv = proc_get_priv(Pstatus(Pr)->pr_pid); 17557c478bd9Sstevel@tonic-gate if (new_prpriv == NULL) { 17567c478bd9Sstevel@tonic-gate preserve_error(gettext("cannot get process privileges " 17577c478bd9Sstevel@tonic-gate "for pid %d: %s"), Pstatus(Pr)->pr_pid, 17587c478bd9Sstevel@tonic-gate strerror(errno)); 17597c478bd9Sstevel@tonic-gate return (1); 17607c478bd9Sstevel@tonic-gate } 17617c478bd9Sstevel@tonic-gate /* 17627c478bd9Sstevel@tonic-gate * We only have to change the process privileges if it doesn't 17637c478bd9Sstevel@tonic-gate * already have PRIV_SYS_RESOURCE. In addition, we want to make 17647c478bd9Sstevel@tonic-gate * sure that we don't leave a process with elevated privileges, 17657c478bd9Sstevel@tonic-gate * so we make sure the process dies if we exit unexpectedly. 17667c478bd9Sstevel@tonic-gate */ 17677c478bd9Sstevel@tonic-gate eset = (priv_set_t *) 17687c478bd9Sstevel@tonic-gate &new_prpriv->pr_sets[new_prpriv->pr_setsize * 17697c478bd9Sstevel@tonic-gate priv_getsetbyname(PRIV_EFFECTIVE)]; 17707c478bd9Sstevel@tonic-gate pset = (priv_set_t *) 17717c478bd9Sstevel@tonic-gate &new_prpriv->pr_sets[new_prpriv->pr_setsize * 17727c478bd9Sstevel@tonic-gate priv_getsetbyname(PRIV_PERMITTED)]; 17737c478bd9Sstevel@tonic-gate if (!priv_ismember(eset, PRIV_SYS_RESOURCE)) { 17747c478bd9Sstevel@tonic-gate /* Keep track of original privileges */ 17757c478bd9Sstevel@tonic-gate old_prpriv = proc_get_priv(Pstatus(Pr)->pr_pid); 17767c478bd9Sstevel@tonic-gate if (old_prpriv == NULL) { 17777c478bd9Sstevel@tonic-gate preserve_error(gettext("cannot get process " 17787c478bd9Sstevel@tonic-gate "privileges for pid %d: %s"), 17797c478bd9Sstevel@tonic-gate Pstatus(Pr)->pr_pid, strerror(errno)); 17807c478bd9Sstevel@tonic-gate free(new_prpriv); 17817c478bd9Sstevel@tonic-gate return (1); 17827c478bd9Sstevel@tonic-gate } 17837c478bd9Sstevel@tonic-gate (void) priv_addset(eset, PRIV_SYS_RESOURCE); 17847c478bd9Sstevel@tonic-gate (void) priv_addset(pset, PRIV_SYS_RESOURCE); 17857c478bd9Sstevel@tonic-gate if (Psetflags(Pr, PR_KLC) != 0 || 17867c478bd9Sstevel@tonic-gate Psetpriv(Pr, new_prpriv) != 0) { 17877c478bd9Sstevel@tonic-gate preserve_error(gettext("cannot set process " 17887c478bd9Sstevel@tonic-gate "privileges for pid %d: %s"), 17897c478bd9Sstevel@tonic-gate Pstatus(Pr)->pr_pid, strerror(errno)); 17907c478bd9Sstevel@tonic-gate (void) Punsetflags(Pr, PR_KLC); 17917c478bd9Sstevel@tonic-gate free(new_prpriv); 17927c478bd9Sstevel@tonic-gate free(old_prpriv); 17937c478bd9Sstevel@tonic-gate return (1); 17947c478bd9Sstevel@tonic-gate } 17957c478bd9Sstevel@tonic-gate } 17967c478bd9Sstevel@tonic-gate /* 17977c478bd9Sstevel@tonic-gate * If this is a zone.* rctl, it requires more than 17987c478bd9Sstevel@tonic-gate * PRIV_SYS_RESOURCE: it wants the process to have global-zone 17997c478bd9Sstevel@tonic-gate * credentials. We temporarily grant non-global zone processes 18007c478bd9Sstevel@tonic-gate * these credentials, and make sure the process dies if we exit 18017c478bd9Sstevel@tonic-gate * unexpectedly. 18027c478bd9Sstevel@tonic-gate */ 18037c478bd9Sstevel@tonic-gate if (arg_name && 18047c478bd9Sstevel@tonic-gate arg_name_entity == RCENTITY_ZONE && 18057c478bd9Sstevel@tonic-gate getzoneid() == GLOBAL_ZONEID && 18067c478bd9Sstevel@tonic-gate proc_get_psinfo(Pstatus(Pr)->pr_pid, &psinfo) == 0 && 18077c478bd9Sstevel@tonic-gate (oldzoneid = psinfo.pr_zoneid) != GLOBAL_ZONEID) { 18087c478bd9Sstevel@tonic-gate /* 18097c478bd9Sstevel@tonic-gate * We need to give this process superuser 18107c478bd9Sstevel@tonic-gate * ("super-zone") privileges. 18117c478bd9Sstevel@tonic-gate * 18127c478bd9Sstevel@tonic-gate * Must never return without setting this back! 18137c478bd9Sstevel@tonic-gate */ 18147c478bd9Sstevel@tonic-gate if (Psetflags(Pr, PR_KLC) != 0 || 18157c478bd9Sstevel@tonic-gate Psetzoneid(Pr, GLOBAL_ZONEID) < 0) { 18167c478bd9Sstevel@tonic-gate preserve_error(gettext( 18177c478bd9Sstevel@tonic-gate "cannot set global-zone " 18187c478bd9Sstevel@tonic-gate "privileges for pid %d: %s"), 18197c478bd9Sstevel@tonic-gate Pstatus(Pr)->pr_pid, strerror(errno)); 18207c478bd9Sstevel@tonic-gate /* 18217c478bd9Sstevel@tonic-gate * We couldn't set the zoneid to begin with, so 18227c478bd9Sstevel@tonic-gate * there's no point in warning the user about 18237c478bd9Sstevel@tonic-gate * trying to un-set it. 18247c478bd9Sstevel@tonic-gate */ 18257c478bd9Sstevel@tonic-gate oldzoneid = GLOBAL_ZONEID; 18267c478bd9Sstevel@tonic-gate ret = 1; 18277c478bd9Sstevel@tonic-gate goto bail; 18287c478bd9Sstevel@tonic-gate } 18297c478bd9Sstevel@tonic-gate } 18307c478bd9Sstevel@tonic-gate } 18317c478bd9Sstevel@tonic-gate /* Now, actually populate the rctlblk in the kernel */ 18327c478bd9Sstevel@tonic-gate if (flags == RCTL_REPLACE) { 18337c478bd9Sstevel@tonic-gate /* 18347c478bd9Sstevel@tonic-gate * Replace should be a delete followed by an insert. This 18357c478bd9Sstevel@tonic-gate * allows us to replace rctl value blocks which match in 18367c478bd9Sstevel@tonic-gate * privilege and value, but have updated actions, etc. 18377c478bd9Sstevel@tonic-gate * setrctl() doesn't allow a direct replace, but we 18387c478bd9Sstevel@tonic-gate * should do the right thing for the user in the command. 18397c478bd9Sstevel@tonic-gate */ 18407c478bd9Sstevel@tonic-gate if (pr_setrctl(Pr, name, NULL, 18417c478bd9Sstevel@tonic-gate old_rblk, RCTL_DELETE)) { 18427c478bd9Sstevel@tonic-gate preserve_error(gettext("failed to delete resource " 18437c478bd9Sstevel@tonic-gate "control %s for pid %d: %s"), name, 18447c478bd9Sstevel@tonic-gate Pstatus(Pr)->pr_pid, strerror(errno)); 18457c478bd9Sstevel@tonic-gate ret = 1; 18467c478bd9Sstevel@tonic-gate goto bail; 18477c478bd9Sstevel@tonic-gate } 18487c478bd9Sstevel@tonic-gate if (pr_setrctl(Pr, name, NULL, 18497c478bd9Sstevel@tonic-gate new_rblk, RCTL_INSERT)) { 18507c478bd9Sstevel@tonic-gate preserve_error(gettext("failed to insert resource " 18517c478bd9Sstevel@tonic-gate "control %s for pid %d: %s"), name, 18527c478bd9Sstevel@tonic-gate Pstatus(Pr)->pr_pid, strerror(errno)); 18537c478bd9Sstevel@tonic-gate ret = 1; 18547c478bd9Sstevel@tonic-gate goto bail; 18557c478bd9Sstevel@tonic-gate } 18567c478bd9Sstevel@tonic-gate } else if (flags == RCTL_INSERT) { 18577c478bd9Sstevel@tonic-gate if (pr_setrctl(Pr, name, NULL, 18587c478bd9Sstevel@tonic-gate new_rblk, RCTL_INSERT)) { 18597c478bd9Sstevel@tonic-gate preserve_error(gettext("failed to create resource " 18607c478bd9Sstevel@tonic-gate "control %s for pid %d: %s"), name, 18617c478bd9Sstevel@tonic-gate Pstatus(Pr)->pr_pid, strerror(errno)); 18627c478bd9Sstevel@tonic-gate ret = 1; 18637c478bd9Sstevel@tonic-gate goto bail; 18647c478bd9Sstevel@tonic-gate } 18657c478bd9Sstevel@tonic-gate } else if (flags == RCTL_DELETE) { 18667c478bd9Sstevel@tonic-gate if (pr_setrctl(Pr, name, NULL, 18677c478bd9Sstevel@tonic-gate new_rblk, RCTL_DELETE)) { 18687c478bd9Sstevel@tonic-gate preserve_error(gettext("failed to delete resource " 18697c478bd9Sstevel@tonic-gate "control %s for pid %d: %s"), name, 18707c478bd9Sstevel@tonic-gate Pstatus(Pr)->pr_pid, strerror(errno)); 18717c478bd9Sstevel@tonic-gate ret = 1; 18727c478bd9Sstevel@tonic-gate goto bail; 18737c478bd9Sstevel@tonic-gate } 18747c478bd9Sstevel@tonic-gate } 18757c478bd9Sstevel@tonic-gate bail: 18767c478bd9Sstevel@tonic-gate if (oldzoneid != GLOBAL_ZONEID) { 18777c478bd9Sstevel@tonic-gate if (Psetzoneid(Pr, oldzoneid) != 0) 18787c478bd9Sstevel@tonic-gate relinquish_failed = B_TRUE; 18797c478bd9Sstevel@tonic-gate } 18807c478bd9Sstevel@tonic-gate if (old_prpriv != NULL) { 18817c478bd9Sstevel@tonic-gate if (Psetpriv(Pr, old_prpriv) != 0) 18827c478bd9Sstevel@tonic-gate relinquish_failed = B_TRUE; 18837c478bd9Sstevel@tonic-gate free(old_prpriv); 18847c478bd9Sstevel@tonic-gate } 18857c478bd9Sstevel@tonic-gate if (relinquish_failed) { 18867c478bd9Sstevel@tonic-gate /* 18877c478bd9Sstevel@tonic-gate * If this failed, we can't leave a process hanging 18887c478bd9Sstevel@tonic-gate * around with elevated privileges, so we'll have to 18897c478bd9Sstevel@tonic-gate * release the process from libproc, knowing that it 18907c478bd9Sstevel@tonic-gate * will be killed (since we set PR_KLC). 18917c478bd9Sstevel@tonic-gate */ 18927c478bd9Sstevel@tonic-gate Pdestroy_agent(Pr); 18937c478bd9Sstevel@tonic-gate preserve_error(gettext("cannot relinquish privileges " 18947c478bd9Sstevel@tonic-gate "for pid %d. The process was killed."), 18957c478bd9Sstevel@tonic-gate Pstatus(Pr)->pr_pid); 18967c478bd9Sstevel@tonic-gate } else { 18977c478bd9Sstevel@tonic-gate if (Punsetflags(Pr, PR_KLC) != 0) 18987c478bd9Sstevel@tonic-gate preserve_error(gettext("cannot relinquish privileges " 18997c478bd9Sstevel@tonic-gate "for pid %d. The process was killed."), 19007c478bd9Sstevel@tonic-gate Pstatus(Pr)->pr_pid); 19017c478bd9Sstevel@tonic-gate } 19027c478bd9Sstevel@tonic-gate if (new_prpriv != NULL) 19037c478bd9Sstevel@tonic-gate free(new_prpriv); 19047c478bd9Sstevel@tonic-gate 19057c478bd9Sstevel@tonic-gate return (ret); 19067c478bd9Sstevel@tonic-gate } 19077c478bd9Sstevel@tonic-gate 19087c478bd9Sstevel@tonic-gate void 19097c478bd9Sstevel@tonic-gate print_priv(rctl_priv_t local_priv, char *format) 19107c478bd9Sstevel@tonic-gate { 19117c478bd9Sstevel@tonic-gate char pstring[11]; 19127c478bd9Sstevel@tonic-gate 19137c478bd9Sstevel@tonic-gate switch (local_priv) { 19147c478bd9Sstevel@tonic-gate case RCPRIV_BASIC: 19157c478bd9Sstevel@tonic-gate (void) strcpy(pstring, "basic"); 19167c478bd9Sstevel@tonic-gate break; 19177c478bd9Sstevel@tonic-gate case RCPRIV_PRIVILEGED: 19187c478bd9Sstevel@tonic-gate (void) strcpy(pstring, "privileged"); 19197c478bd9Sstevel@tonic-gate break; 19207c478bd9Sstevel@tonic-gate case RCPRIV_SYSTEM: 19217c478bd9Sstevel@tonic-gate (void) strcpy(pstring, "system"); 19227c478bd9Sstevel@tonic-gate break; 19237c478bd9Sstevel@tonic-gate default: 19247c478bd9Sstevel@tonic-gate (void) sprintf(pstring, "%d", local_priv); 19257c478bd9Sstevel@tonic-gate break; 19267c478bd9Sstevel@tonic-gate } 19277c478bd9Sstevel@tonic-gate /* LINTED */ 19287c478bd9Sstevel@tonic-gate (void) fprintf(stdout, format, pstring); 19297c478bd9Sstevel@tonic-gate } 19307c478bd9Sstevel@tonic-gate 19317c478bd9Sstevel@tonic-gate void 19327c478bd9Sstevel@tonic-gate print_local_action(int action, int *signalp, char *format) 19337c478bd9Sstevel@tonic-gate { 19347c478bd9Sstevel@tonic-gate char sig[SIG2STR_MAX]; 19357c478bd9Sstevel@tonic-gate char sigstring[SIG2STR_MAX + 7]; 19367c478bd9Sstevel@tonic-gate char astring[5 + SIG2STR_MAX + 7]; 19377c478bd9Sstevel@tonic-gate int set = 0; 19387c478bd9Sstevel@tonic-gate 19397c478bd9Sstevel@tonic-gate astring[0] = '\0'; 19407c478bd9Sstevel@tonic-gate 19417c478bd9Sstevel@tonic-gate if (action == RCTL_LOCAL_NOACTION) { 19427c478bd9Sstevel@tonic-gate (void) strcat(astring, "none"); 19437c478bd9Sstevel@tonic-gate set++; 19447c478bd9Sstevel@tonic-gate } 19457c478bd9Sstevel@tonic-gate if (action & RCTL_LOCAL_DENY) { 19467c478bd9Sstevel@tonic-gate (void) strcat(astring, "deny"); 19477c478bd9Sstevel@tonic-gate set++; 19487c478bd9Sstevel@tonic-gate } 19497c478bd9Sstevel@tonic-gate if ((action & RCTL_LOCAL_DENY) && 19507c478bd9Sstevel@tonic-gate (action & RCTL_LOCAL_SIGNAL)) { 19517c478bd9Sstevel@tonic-gate (void) strcat(astring, ","); 19527c478bd9Sstevel@tonic-gate } 19537c478bd9Sstevel@tonic-gate if (action & RCTL_LOCAL_SIGNAL) { 19547c478bd9Sstevel@tonic-gate if (sig2str(*signalp, sig)) 19557c478bd9Sstevel@tonic-gate (void) snprintf(sigstring, sizeof (astring), 19567c478bd9Sstevel@tonic-gate "signal=%d", *signalp); 19577c478bd9Sstevel@tonic-gate else 19587c478bd9Sstevel@tonic-gate (void) snprintf(sigstring, sizeof (astring), 19597c478bd9Sstevel@tonic-gate "signal=%s", sig); 19607c478bd9Sstevel@tonic-gate 19617c478bd9Sstevel@tonic-gate (void) strcat(astring, sigstring); 19627c478bd9Sstevel@tonic-gate set++; 19637c478bd9Sstevel@tonic-gate } 19647c478bd9Sstevel@tonic-gate if (set) 19657c478bd9Sstevel@tonic-gate /* LINTED */ 19667c478bd9Sstevel@tonic-gate (void) fprintf(stdout, format, astring); 19677c478bd9Sstevel@tonic-gate else 19687c478bd9Sstevel@tonic-gate /* LINTED */ 19697c478bd9Sstevel@tonic-gate (void) fprintf(stdout, format, action); 19707c478bd9Sstevel@tonic-gate } 19717c478bd9Sstevel@tonic-gate 19727c478bd9Sstevel@tonic-gate /* 19737c478bd9Sstevel@tonic-gate * This function is used to grab the process matching the recipient pid 19747c478bd9Sstevel@tonic-gate */ 19757c478bd9Sstevel@tonic-gate pid_t 19767c478bd9Sstevel@tonic-gate regrab_process(pid_t pid, pr_info_handle_t *p, int priv, int *gret) 19777c478bd9Sstevel@tonic-gate { 19787c478bd9Sstevel@tonic-gate 19797c478bd9Sstevel@tonic-gate char pidstring[24]; 19807c478bd9Sstevel@tonic-gate 19817c478bd9Sstevel@tonic-gate gret = 0; 19827c478bd9Sstevel@tonic-gate if (pid == -1) 19837c478bd9Sstevel@tonic-gate return (p->pid); 19847c478bd9Sstevel@tonic-gate if (p->pid == pid) 19857c478bd9Sstevel@tonic-gate return (p->pid); 19867c478bd9Sstevel@tonic-gate 19877c478bd9Sstevel@tonic-gate release_process(p->pr); 19887c478bd9Sstevel@tonic-gate (void) memset(p, 0, sizeof (*p)); 19897c478bd9Sstevel@tonic-gate 19907c478bd9Sstevel@tonic-gate (void) snprintf(pidstring, 24, "%d", pid); 19917c478bd9Sstevel@tonic-gate return (grab_process_by_id( 19927c478bd9Sstevel@tonic-gate pidstring, RCENTITY_PROCESS, p, priv, gret)); 19937c478bd9Sstevel@tonic-gate } 19947c478bd9Sstevel@tonic-gate 19957c478bd9Sstevel@tonic-gate /* 19967c478bd9Sstevel@tonic-gate * int grab_process_by_id(char *, rctl_entity_t, pr_info_handle_t *, int, int *) 19977c478bd9Sstevel@tonic-gate * 19987c478bd9Sstevel@tonic-gate * Input 19997c478bd9Sstevel@tonic-gate * Supply a non-NULL string containing: 20007c478bd9Sstevel@tonic-gate * - logical project/zone name or project/zone number if type is 20017c478bd9Sstevel@tonic-gate * RCENTITY_PROJECT or RCENTITY_ZONE 20027c478bd9Sstevel@tonic-gate * - task number if type is RCENTITY_TYPE 20037c478bd9Sstevel@tonic-gate * - a pid if type is RCENTITY_PID 20047c478bd9Sstevel@tonic-gate * Also supply an un-allocated prochandle, and an allocated info_handle. 20057c478bd9Sstevel@tonic-gate * This function assumes that the type is set. 20067c478bd9Sstevel@tonic-gate * If priv is not RCPRIV_BASIC, the grabbed process is required to have 20077c478bd9Sstevel@tonic-gate * PRIV_SYS_RESOURCE in it's limit set. 20087c478bd9Sstevel@tonic-gate * 20097c478bd9Sstevel@tonic-gate * Return Values 20107c478bd9Sstevel@tonic-gate * Returns 0 on success and 1 on failure. If there is a process 20117c478bd9Sstevel@tonic-gate * running under the specified id, success is returned, and 20127c478bd9Sstevel@tonic-gate * Pr is pointed to the process. Success will be returned and Pr 20137c478bd9Sstevel@tonic-gate * set to NULL if the matching process is our own. 20147c478bd9Sstevel@tonic-gate * If success is returned, psinfo will be valid, and pid will 20157c478bd9Sstevel@tonic-gate * be the process number. The process will also be held at the 20167c478bd9Sstevel@tonic-gate * end, so release_process should be used by the caller. 20177c478bd9Sstevel@tonic-gate * 20187c478bd9Sstevel@tonic-gate * This function assumes that signals are caught already so that libproc 20197c478bd9Sstevel@tonic-gate * can be safely used. 20207c478bd9Sstevel@tonic-gate * 20217c478bd9Sstevel@tonic-gate * Return Values 20227c478bd9Sstevel@tonic-gate * pid - Process found and grabbed 20237c478bd9Sstevel@tonic-gate * -1 - Error 20247c478bd9Sstevel@tonic-gate */ 20257c478bd9Sstevel@tonic-gate pid_t 20267c478bd9Sstevel@tonic-gate grab_process_by_id(char *idname, rctl_entity_t type, pr_info_handle_t *p, 20277c478bd9Sstevel@tonic-gate int priv, int *gret) 20287c478bd9Sstevel@tonic-gate { 20297c478bd9Sstevel@tonic-gate char prbuf[PROJECT_BUFSZ]; 20307c478bd9Sstevel@tonic-gate projid_t projid; 20317c478bd9Sstevel@tonic-gate taskid_t taskid; 20327c478bd9Sstevel@tonic-gate zoneid_t zoneid; 20337c478bd9Sstevel@tonic-gate zoneid_t zone_self; 20347c478bd9Sstevel@tonic-gate struct project proj; 20357c478bd9Sstevel@tonic-gate DIR *dirp; 20367c478bd9Sstevel@tonic-gate struct dirent *dentp; 20377c478bd9Sstevel@tonic-gate int found = 0; 20387c478bd9Sstevel@tonic-gate int pid_self; 20397c478bd9Sstevel@tonic-gate int ret; 20407c478bd9Sstevel@tonic-gate int gret_in; 20417c478bd9Sstevel@tonic-gate int intidname; 20427c478bd9Sstevel@tonic-gate char *end; 20437c478bd9Sstevel@tonic-gate prpriv_t *prpriv; 20447c478bd9Sstevel@tonic-gate priv_set_t *prset; 20457c478bd9Sstevel@tonic-gate 20467c478bd9Sstevel@tonic-gate gret_in = *gret; 20477c478bd9Sstevel@tonic-gate 20487c478bd9Sstevel@tonic-gate /* get our pid se we do not try to operate on self */ 20497c478bd9Sstevel@tonic-gate pid_self = getpid(); 20507c478bd9Sstevel@tonic-gate 20517c478bd9Sstevel@tonic-gate /* Store integer version of id */ 20527c478bd9Sstevel@tonic-gate intidname = strtoul(idname, &end, 10); 20537c478bd9Sstevel@tonic-gate if (errno || *end != '\0' || end == idname) { 20547c478bd9Sstevel@tonic-gate intidname = -1; 20557c478bd9Sstevel@tonic-gate } 20567c478bd9Sstevel@tonic-gate 20577c478bd9Sstevel@tonic-gate /* 20587c478bd9Sstevel@tonic-gate * get our zoneid so we don't try to operate on a project in 20597c478bd9Sstevel@tonic-gate * another zone 20607c478bd9Sstevel@tonic-gate */ 20617c478bd9Sstevel@tonic-gate zone_self = getzoneid(); 20627c478bd9Sstevel@tonic-gate 20637c478bd9Sstevel@tonic-gate if (idname == NULL || strcmp(idname, "") == 0) { 20647c478bd9Sstevel@tonic-gate warn(gettext("id name cannot be nuint64\n")); 20657c478bd9Sstevel@tonic-gate return (-1); 20667c478bd9Sstevel@tonic-gate } 20677c478bd9Sstevel@tonic-gate /* 20687c478bd9Sstevel@tonic-gate * Set up zoneid, projid or taskid, as appropriate, so that comparisons 20697c478bd9Sstevel@tonic-gate * can be done later with the input. 20707c478bd9Sstevel@tonic-gate */ 20717c478bd9Sstevel@tonic-gate if (type == RCENTITY_ZONE) { 20727c478bd9Sstevel@tonic-gate if (zone_get_id(idname, &zoneid) != 0) { 20737c478bd9Sstevel@tonic-gate warn(gettext("%s: unknown zone\n"), idname); 20747c478bd9Sstevel@tonic-gate return (-1); 20757c478bd9Sstevel@tonic-gate } 20767c478bd9Sstevel@tonic-gate } else if (type == RCENTITY_PROJECT) { 20777c478bd9Sstevel@tonic-gate if (getprojbyname(idname, &proj, prbuf, PROJECT_BUFSZ) 20787c478bd9Sstevel@tonic-gate == NULL) { 20797c478bd9Sstevel@tonic-gate if (getprojbyid(intidname, &proj, prbuf, 20807c478bd9Sstevel@tonic-gate PROJECT_BUFSZ) == NULL) { 20817c478bd9Sstevel@tonic-gate warn(gettext("%s: cannot find project\n"), 20827c478bd9Sstevel@tonic-gate idname); 20837c478bd9Sstevel@tonic-gate return (-1); 20847c478bd9Sstevel@tonic-gate } 20857c478bd9Sstevel@tonic-gate } 20867c478bd9Sstevel@tonic-gate projid = proj.pj_projid; 20877c478bd9Sstevel@tonic-gate } else if (type == RCENTITY_TASK) { 20887c478bd9Sstevel@tonic-gate taskid = (taskid_t)atol(idname); 20897c478bd9Sstevel@tonic-gate } 20907c478bd9Sstevel@tonic-gate /* 20917c478bd9Sstevel@tonic-gate * Projects and tasks need to search through /proc for 20927c478bd9Sstevel@tonic-gate * a parent process. 20937c478bd9Sstevel@tonic-gate */ 20947c478bd9Sstevel@tonic-gate if (type == RCENTITY_ZONE || type == RCENTITY_PROJECT || 20957c478bd9Sstevel@tonic-gate type == RCENTITY_TASK) { 20967c478bd9Sstevel@tonic-gate if ((dirp = opendir("/proc")) == NULL) { 20977c478bd9Sstevel@tonic-gate warn(gettext("%s: cannot open /proc directory\n"), 20987c478bd9Sstevel@tonic-gate idname); 20997c478bd9Sstevel@tonic-gate return (-1); 21007c478bd9Sstevel@tonic-gate } 21017c478bd9Sstevel@tonic-gate /* 21027c478bd9Sstevel@tonic-gate * Look through all processes in /proc. For each process, 21037c478bd9Sstevel@tonic-gate * check if the pr_projid in their psinfo matches the 21047c478bd9Sstevel@tonic-gate * specified id. 21057c478bd9Sstevel@tonic-gate */ 21067c478bd9Sstevel@tonic-gate while (dentp = readdir(dirp)) { 21077c478bd9Sstevel@tonic-gate p->pid = atoi(dentp->d_name); 21087c478bd9Sstevel@tonic-gate 21097c478bd9Sstevel@tonic-gate /* Skip self */ 21107c478bd9Sstevel@tonic-gate if (p->pid == pid_self) 21117c478bd9Sstevel@tonic-gate continue; 21127c478bd9Sstevel@tonic-gate 21137c478bd9Sstevel@tonic-gate if (proc_get_psinfo(p->pid, &(p->psinfo)) != 0) 21147c478bd9Sstevel@tonic-gate continue; 21157c478bd9Sstevel@tonic-gate 21167c478bd9Sstevel@tonic-gate /* Skip process if it is not what we are looking for */ 21177c478bd9Sstevel@tonic-gate if (type == RCENTITY_ZONE && 21187c478bd9Sstevel@tonic-gate (p->psinfo).pr_zoneid != zoneid) { 21197c478bd9Sstevel@tonic-gate continue; 2120c3ea2840SMenno Lageman } else if (type == RCENTITY_PROJECT && 21217c478bd9Sstevel@tonic-gate ((p->psinfo).pr_projid != projid || 21227c478bd9Sstevel@tonic-gate (p->psinfo).pr_zoneid != zone_self)) { 21237c478bd9Sstevel@tonic-gate continue; 21247c478bd9Sstevel@tonic-gate } else if (type == RCENTITY_TASK && 21257c478bd9Sstevel@tonic-gate (p->psinfo).pr_taskid != taskid) { 21267c478bd9Sstevel@tonic-gate continue; 21277c478bd9Sstevel@tonic-gate } 21287c478bd9Sstevel@tonic-gate /* attempt to grab process */ 21297c478bd9Sstevel@tonic-gate if (grab_process(p, gret) != 0) 21307c478bd9Sstevel@tonic-gate continue; 21317c478bd9Sstevel@tonic-gate 21327c478bd9Sstevel@tonic-gate /* 21337c478bd9Sstevel@tonic-gate * Re-confirm that this process is still running as 21347c478bd9Sstevel@tonic-gate * part of the specified project or task. If it 21357c478bd9Sstevel@tonic-gate * doesn't match, release the process and return an 21367c478bd9Sstevel@tonic-gate * error. This should only be done if the Pr struct is 21377c478bd9Sstevel@tonic-gate * not NULL. 21387c478bd9Sstevel@tonic-gate */ 21397c478bd9Sstevel@tonic-gate if (type == RCENTITY_PROJECT) { 21407c478bd9Sstevel@tonic-gate if (pr_getprojid(p->pr) != projid || 21417c478bd9Sstevel@tonic-gate pr_getzoneid(p->pr) != zone_self) { 21427c478bd9Sstevel@tonic-gate release_process(p->pr); 21437c478bd9Sstevel@tonic-gate continue; 21447c478bd9Sstevel@tonic-gate } 21457c478bd9Sstevel@tonic-gate } else if (type == RCENTITY_TASK) { 21467c478bd9Sstevel@tonic-gate if (pr_gettaskid(p->pr) != taskid) { 21477c478bd9Sstevel@tonic-gate release_process(p->pr); 21487c478bd9Sstevel@tonic-gate continue; 21497c478bd9Sstevel@tonic-gate } 21507c478bd9Sstevel@tonic-gate } else if (type == RCENTITY_ZONE) { 21517c478bd9Sstevel@tonic-gate if (pr_getzoneid(p->pr) != zoneid) { 21527c478bd9Sstevel@tonic-gate release_process(p->pr); 21537c478bd9Sstevel@tonic-gate continue; 21547c478bd9Sstevel@tonic-gate } 21557c478bd9Sstevel@tonic-gate } 21567c478bd9Sstevel@tonic-gate 21577c478bd9Sstevel@tonic-gate /* 21587c478bd9Sstevel@tonic-gate * If we are setting a privileged resource control, 21597c478bd9Sstevel@tonic-gate * verify that process has PRIV_SYS_RESOURCE in it's 21607c478bd9Sstevel@tonic-gate * limit set. If it does not, then we will not be 21617c478bd9Sstevel@tonic-gate * able to give this process the privilege it needs 21627c478bd9Sstevel@tonic-gate * to set the resource control. 21637c478bd9Sstevel@tonic-gate */ 21647c478bd9Sstevel@tonic-gate if (priv != RCPRIV_BASIC) { 21657c478bd9Sstevel@tonic-gate prpriv = proc_get_priv(p->pid); 21667c478bd9Sstevel@tonic-gate if (prpriv == NULL) { 21677c478bd9Sstevel@tonic-gate release_process(p->pr); 21687c478bd9Sstevel@tonic-gate continue; 21697c478bd9Sstevel@tonic-gate } 21707c478bd9Sstevel@tonic-gate prset = (priv_set_t *) 21717c478bd9Sstevel@tonic-gate &prpriv->pr_sets[prpriv->pr_setsize * 21727c478bd9Sstevel@tonic-gate priv_getsetbyname(PRIV_LIMIT)]; 21737c478bd9Sstevel@tonic-gate if (!priv_ismember(prset, PRIV_SYS_RESOURCE)) { 21747c478bd9Sstevel@tonic-gate release_process(p->pr); 21757c478bd9Sstevel@tonic-gate continue; 21767c478bd9Sstevel@tonic-gate } 21777c478bd9Sstevel@tonic-gate } 21787c478bd9Sstevel@tonic-gate found = 1; 21797c478bd9Sstevel@tonic-gate 21807c478bd9Sstevel@tonic-gate p->taskid = pr_gettaskid(p->pr); 21817c478bd9Sstevel@tonic-gate p->projid = pr_getprojid(p->pr); 21827c478bd9Sstevel@tonic-gate p->zoneid = pr_getzoneid(p->pr); 21837c478bd9Sstevel@tonic-gate 21847c478bd9Sstevel@tonic-gate break; 21857c478bd9Sstevel@tonic-gate } 21867c478bd9Sstevel@tonic-gate (void) closedir(dirp); 21877c478bd9Sstevel@tonic-gate 21887c478bd9Sstevel@tonic-gate if (found == 0) { 21897c478bd9Sstevel@tonic-gate warn(gettext("%s: No controllable process found in " 21907c478bd9Sstevel@tonic-gate "task, project, or zone.\n"), idname); 21917c478bd9Sstevel@tonic-gate return (-1); 21927c478bd9Sstevel@tonic-gate } 21937c478bd9Sstevel@tonic-gate return (p->pid); 21947c478bd9Sstevel@tonic-gate 21957c478bd9Sstevel@tonic-gate } else if (type == RCENTITY_PROCESS) { 21967c478bd9Sstevel@tonic-gate 21977c478bd9Sstevel@tonic-gate /* fail if self */ 21987c478bd9Sstevel@tonic-gate if (p->pid == pid_self) { 21997c478bd9Sstevel@tonic-gate 22007c478bd9Sstevel@tonic-gate warn(gettext("%s: cannot control self"), idname); 22017c478bd9Sstevel@tonic-gate return (-1); 22027c478bd9Sstevel@tonic-gate } 22037c478bd9Sstevel@tonic-gate /* 22047c478bd9Sstevel@tonic-gate * Process types need to be set up with the correct pid 22057c478bd9Sstevel@tonic-gate * and psinfo structure. 22067c478bd9Sstevel@tonic-gate */ 22077c478bd9Sstevel@tonic-gate if ((p->pid = proc_arg_psinfo(idname, PR_ARG_PIDS, 22087c478bd9Sstevel@tonic-gate &(p->psinfo), gret)) == -1) { 22097c478bd9Sstevel@tonic-gate warn(gettext("%s: cannot examine: %s"), idname, 22107c478bd9Sstevel@tonic-gate Pgrab_error(*gret)); 22117c478bd9Sstevel@tonic-gate return (-1); 22127c478bd9Sstevel@tonic-gate } 22137c478bd9Sstevel@tonic-gate /* grab process */ 22147c478bd9Sstevel@tonic-gate ret = grab_process(p, gret); 22157c478bd9Sstevel@tonic-gate if (ret == 1) { 22167c478bd9Sstevel@tonic-gate /* Don't print error if G_SYS is allowed */ 22177c478bd9Sstevel@tonic-gate if (gret_in == G_SYS && *gret == G_SYS) { 22187c478bd9Sstevel@tonic-gate return (-1); 22197c478bd9Sstevel@tonic-gate } else { 22207c478bd9Sstevel@tonic-gate warn(gettext("%s: cannot control: %s"), idname, 22217c478bd9Sstevel@tonic-gate Pgrab_error(*gret)); 22227c478bd9Sstevel@tonic-gate return (-1); 22237c478bd9Sstevel@tonic-gate } 22247c478bd9Sstevel@tonic-gate } else if (ret == 2) { 22257c478bd9Sstevel@tonic-gate ret = errno; 22267c478bd9Sstevel@tonic-gate warn(gettext("%s: cannot control: %s"), idname, 22277c478bd9Sstevel@tonic-gate strerror(ret)); 22287c478bd9Sstevel@tonic-gate return (-1); 22297c478bd9Sstevel@tonic-gate } 22307c478bd9Sstevel@tonic-gate p->taskid = pr_gettaskid(p->pr); 22317c478bd9Sstevel@tonic-gate p->projid = pr_getprojid(p->pr); 22327c478bd9Sstevel@tonic-gate p->zoneid = pr_getzoneid(p->pr); 22337c478bd9Sstevel@tonic-gate 22347c478bd9Sstevel@tonic-gate return (p->pid); 22357c478bd9Sstevel@tonic-gate 22367c478bd9Sstevel@tonic-gate } else { 22377c478bd9Sstevel@tonic-gate warn(gettext("%s: unknown resource entity type %d\n"), idname, 22387c478bd9Sstevel@tonic-gate type); 22397c478bd9Sstevel@tonic-gate return (-1); 22407c478bd9Sstevel@tonic-gate } 22417c478bd9Sstevel@tonic-gate } 22427c478bd9Sstevel@tonic-gate 22437c478bd9Sstevel@tonic-gate /* 22447c478bd9Sstevel@tonic-gate * Do the work required to manipulate a process through libproc. 22457c478bd9Sstevel@tonic-gate * If grab_process() returns no errors (0), then release_process() 22467c478bd9Sstevel@tonic-gate * must eventually be called. 22477c478bd9Sstevel@tonic-gate * 22487c478bd9Sstevel@tonic-gate * Return values: 22497c478bd9Sstevel@tonic-gate * 0 Successful creation of agent thread 22507c478bd9Sstevel@tonic-gate * 1 Error grabbing 22517c478bd9Sstevel@tonic-gate * 2 Error creating agent 22527c478bd9Sstevel@tonic-gate */ 22537c478bd9Sstevel@tonic-gate int 22547c478bd9Sstevel@tonic-gate grab_process(pr_info_handle_t *p, int *gret) 22557c478bd9Sstevel@tonic-gate { 22567c478bd9Sstevel@tonic-gate 22577c478bd9Sstevel@tonic-gate if ((p->pr = Pgrab(p->pid, arg_force, gret)) != NULL) { 22587c478bd9Sstevel@tonic-gate 22597c478bd9Sstevel@tonic-gate if (Psetflags(p->pr, PR_RLC) != 0) { 22607c478bd9Sstevel@tonic-gate Prelease(p->pr, 0); 22617c478bd9Sstevel@tonic-gate return (1); 22627c478bd9Sstevel@tonic-gate } 22637c478bd9Sstevel@tonic-gate if (Pcreate_agent(p->pr) == 0) { 22647c478bd9Sstevel@tonic-gate return (0); 22657c478bd9Sstevel@tonic-gate 22667c478bd9Sstevel@tonic-gate } else { 22677c478bd9Sstevel@tonic-gate Prelease(p->pr, 0); 22687c478bd9Sstevel@tonic-gate return (2); 22697c478bd9Sstevel@tonic-gate } 22707c478bd9Sstevel@tonic-gate } else { 22717c478bd9Sstevel@tonic-gate return (1); 22727c478bd9Sstevel@tonic-gate } 22737c478bd9Sstevel@tonic-gate } 22747c478bd9Sstevel@tonic-gate 22757c478bd9Sstevel@tonic-gate /* 22767c478bd9Sstevel@tonic-gate * Release the specified process. This destroys the agent 22777c478bd9Sstevel@tonic-gate * and releases the process. If the process is NULL, nothing 22787c478bd9Sstevel@tonic-gate * is done. This function should only be called if grab_process() 22797c478bd9Sstevel@tonic-gate * has previously been called and returned success. 22807c478bd9Sstevel@tonic-gate * 22817c478bd9Sstevel@tonic-gate * This function is Pgrab-safe. 22827c478bd9Sstevel@tonic-gate */ 22837c478bd9Sstevel@tonic-gate void 22847c478bd9Sstevel@tonic-gate release_process(struct ps_prochandle *Pr) 22857c478bd9Sstevel@tonic-gate { 22867c478bd9Sstevel@tonic-gate if (Pr == NULL) 22877c478bd9Sstevel@tonic-gate return; 22887c478bd9Sstevel@tonic-gate 22897c478bd9Sstevel@tonic-gate Pdestroy_agent(Pr); 22907c478bd9Sstevel@tonic-gate Prelease(Pr, 0); 22917c478bd9Sstevel@tonic-gate } 22927c478bd9Sstevel@tonic-gate 22937c478bd9Sstevel@tonic-gate /* 22947c478bd9Sstevel@tonic-gate * preserve_error(char *, ...) 22957c478bd9Sstevel@tonic-gate * 22967c478bd9Sstevel@tonic-gate * preserve_error() should be called rather than warn() by any 22977c478bd9Sstevel@tonic-gate * function that is called while the victim process is held by Pgrab. 22987c478bd9Sstevel@tonic-gate * It will save the error until the process has been un-controlled 22997c478bd9Sstevel@tonic-gate * and output is reasonable again. 23007c478bd9Sstevel@tonic-gate * 23017c478bd9Sstevel@tonic-gate * Note that multiple errors are not stored. Any error in these 23027c478bd9Sstevel@tonic-gate * sections should be critical and return immediately. 23037c478bd9Sstevel@tonic-gate * 23047c478bd9Sstevel@tonic-gate * This function is Pgrab-safe. 23057c478bd9Sstevel@tonic-gate * 23067c478bd9Sstevel@tonic-gate * Since this function may copy untrusted command line arguments to 23077c478bd9Sstevel@tonic-gate * global_error, security practices require that global_error never be 23087c478bd9Sstevel@tonic-gate * printed directly. Use printf("%s\n", global_error) or equivalent. 23097c478bd9Sstevel@tonic-gate */ 23107c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 23117c478bd9Sstevel@tonic-gate void 23127c478bd9Sstevel@tonic-gate preserve_error(char *format, ...) 23137c478bd9Sstevel@tonic-gate { 23147c478bd9Sstevel@tonic-gate va_list alist; 23157c478bd9Sstevel@tonic-gate 23167c478bd9Sstevel@tonic-gate va_start(alist, format); 23177c478bd9Sstevel@tonic-gate 23187c478bd9Sstevel@tonic-gate /* 23197c478bd9Sstevel@tonic-gate * GLOBAL_ERR_SZ is pretty big. If the error is longer 23207c478bd9Sstevel@tonic-gate * than that, just truncate it, rather than chance missing 23217c478bd9Sstevel@tonic-gate * the error altogether. 23227c478bd9Sstevel@tonic-gate */ 23237c478bd9Sstevel@tonic-gate (void) vsnprintf(global_error, GLOBAL_ERR_SZ-1, format, alist); 23247c478bd9Sstevel@tonic-gate 23257c478bd9Sstevel@tonic-gate va_end(alist); 23267c478bd9Sstevel@tonic-gate } 2327