10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52517Stn143363 * Common Development and Distribution License (the "License"). 62517Stn143363 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*4119Stn143363 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/types.h> 290Sstevel@tonic-gate #include <sys/stat.h> 300Sstevel@tonic-gate #include <sys/wait.h> 310Sstevel@tonic-gate #include <fcntl.h> 320Sstevel@tonic-gate #include <errno.h> 330Sstevel@tonic-gate #include <signal.h> 340Sstevel@tonic-gate #include <stdio.h> 350Sstevel@tonic-gate #include <stdlib.h> 360Sstevel@tonic-gate #include <strings.h> 370Sstevel@tonic-gate #include <unistd.h> 380Sstevel@tonic-gate #include <libscf.h> 390Sstevel@tonic-gate #include <libscf_priv.h> 400Sstevel@tonic-gate #include <libintl.h> 410Sstevel@tonic-gate #include <locale.h> 423247Sgjelinek #include <zone.h> 433247Sgjelinek #include <libzonecfg.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate #include "utils.h" 460Sstevel@tonic-gate #include "rcapd.h" 470Sstevel@tonic-gate #include "rcapd_conf.h" 480Sstevel@tonic-gate #include "rcapd_stat.h" 490Sstevel@tonic-gate 500Sstevel@tonic-gate static void 510Sstevel@tonic-gate usage() 520Sstevel@tonic-gate { 530Sstevel@tonic-gate (void) fprintf(stderr, 540Sstevel@tonic-gate gettext("usage: rcapadm\n" 550Sstevel@tonic-gate " [-E|-D] " 560Sstevel@tonic-gate "# enable/disable rcapd\n" 570Sstevel@tonic-gate " [-n] " 580Sstevel@tonic-gate "# don't start/stop rcapd\n" 590Sstevel@tonic-gate " [-i <scan|sample|report|config>=value] " 600Sstevel@tonic-gate "# set intervals\n" 610Sstevel@tonic-gate " [-c <percent>] " 620Sstevel@tonic-gate "# set memory cap\n" 630Sstevel@tonic-gate " " 643247Sgjelinek "# enforcement threshold\n" 653247Sgjelinek " [-z <zonename> -m <max-rss>] " 663247Sgjelinek "# update zone memory cap\n")); 670Sstevel@tonic-gate exit(E_USAGE); 680Sstevel@tonic-gate } 690Sstevel@tonic-gate 700Sstevel@tonic-gate static rcfg_t conf; 710Sstevel@tonic-gate static int enable = -1; 720Sstevel@tonic-gate static int disable = -1; 730Sstevel@tonic-gate static int pressure = -1; 740Sstevel@tonic-gate static int no_starting_stopping = -1; 750Sstevel@tonic-gate static int scan_interval = -1; 760Sstevel@tonic-gate static int report_interval = -1; 770Sstevel@tonic-gate static int config_interval = -1; 780Sstevel@tonic-gate static int sample_interval = -1; 790Sstevel@tonic-gate 800Sstevel@tonic-gate static char *subopt_v[] = { 810Sstevel@tonic-gate "scan", 820Sstevel@tonic-gate "sample", 830Sstevel@tonic-gate "report", 840Sstevel@tonic-gate "config", 850Sstevel@tonic-gate NULL 860Sstevel@tonic-gate }; 870Sstevel@tonic-gate 880Sstevel@tonic-gate typedef enum { 890Sstevel@tonic-gate OPT_SCAN = 0, 900Sstevel@tonic-gate OPT_SAMPLE, 910Sstevel@tonic-gate OPT_REPORT, 920Sstevel@tonic-gate OPT_CONFIG 930Sstevel@tonic-gate } subopt_idx_t; 940Sstevel@tonic-gate 950Sstevel@tonic-gate static void 960Sstevel@tonic-gate print_state(void) 970Sstevel@tonic-gate { 980Sstevel@tonic-gate scf_simple_prop_t *persistent_prop = NULL; 990Sstevel@tonic-gate scf_simple_prop_t *temporary_prop = NULL; 1000Sstevel@tonic-gate uint8_t *persistent = NULL; 1010Sstevel@tonic-gate uint8_t *temporary = NULL; 1020Sstevel@tonic-gate scf_handle_t *h; 1030Sstevel@tonic-gate /* LINTED: conditionally assigned and used in function */ 1040Sstevel@tonic-gate ssize_t numvals; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate if ((h = scf_handle_create(SCF_VERSION)) == NULL || 1070Sstevel@tonic-gate scf_handle_bind(h) != 0) 1080Sstevel@tonic-gate goto out; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate if ((persistent_prop = scf_simple_prop_get(h, RCAP_FMRI, 1110Sstevel@tonic-gate SCF_PG_GENERAL, SCF_PROPERTY_ENABLED)) != NULL && (numvals = 1120Sstevel@tonic-gate scf_simple_prop_numvalues(persistent_prop)) > 0) 1130Sstevel@tonic-gate persistent = scf_simple_prop_next_boolean(persistent_prop); 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate if ((temporary_prop = scf_simple_prop_get(h, RCAP_FMRI, 1160Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED)) != NULL && (numvals = 1170Sstevel@tonic-gate scf_simple_prop_numvalues(temporary_prop)) > 0) 1180Sstevel@tonic-gate temporary = scf_simple_prop_next_boolean(temporary_prop); 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate out: 1210Sstevel@tonic-gate if (!persistent) 1220Sstevel@tonic-gate (void) printf(gettext(" " 1230Sstevel@tonic-gate "state: unknown")); 1240Sstevel@tonic-gate else if (temporary && *temporary != *persistent) 1250Sstevel@tonic-gate (void) printf(gettext(" " 1260Sstevel@tonic-gate "state: %s (%s at next boot)\n"), *temporary ? 1270Sstevel@tonic-gate gettext("enabled") : gettext("disabled"), *persistent ? 1280Sstevel@tonic-gate gettext("enabled") : gettext("disabled")); 1290Sstevel@tonic-gate else 1300Sstevel@tonic-gate (void) printf(gettext(" " 1310Sstevel@tonic-gate "state: %s\n"), *persistent ? gettext("enabled") : 1320Sstevel@tonic-gate gettext("disabled")); 1330Sstevel@tonic-gate 134*4119Stn143363 (void) printf(gettext(" memory cap enforcement" 135*4119Stn143363 " threshold: %d%%\n"), conf.rcfg_memory_cap_enforcement_pressure); 136*4119Stn143363 (void) printf(gettext(" process scan rate" 137*4119Stn143363 " (sec): %d\n"), conf.rcfg_proc_walk_interval); 138*4119Stn143363 (void) printf(gettext(" reconfiguration rate" 139*4119Stn143363 " (sec): %d\n"), conf.rcfg_reconfiguration_interval); 140*4119Stn143363 (void) printf(gettext(" report rate" 141*4119Stn143363 " (sec): %d\n"), conf.rcfg_report_interval); 142*4119Stn143363 (void) printf(gettext(" RSS sampling rate" 143*4119Stn143363 " (sec): %d\n"), conf.rcfg_rss_sample_interval); 144*4119Stn143363 1450Sstevel@tonic-gate scf_simple_prop_free(temporary_prop); 1460Sstevel@tonic-gate scf_simple_prop_free(persistent_prop); 1470Sstevel@tonic-gate scf_handle_destroy(h); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1503247Sgjelinek /* 1513247Sgjelinek * Update the in-kernel memory cap for the specified zone. 1523247Sgjelinek */ 1533247Sgjelinek static int 1543247Sgjelinek update_zone_mcap(char *zonename, char *maxrss) 1553247Sgjelinek { 1563247Sgjelinek zoneid_t zone_id; 1573247Sgjelinek uint64_t num; 1583247Sgjelinek 1593247Sgjelinek if (getzoneid() != GLOBAL_ZONEID || zonecfg_in_alt_root()) 1603247Sgjelinek return (E_SUCCESS); 1613247Sgjelinek 1623247Sgjelinek /* get the running zone from the kernel */ 1633247Sgjelinek if ((zone_id = getzoneidbyname(zonename)) == -1) { 1643247Sgjelinek (void) fprintf(stderr, gettext("zone '%s' must be running\n"), 1653247Sgjelinek zonename); 1663247Sgjelinek return (E_ERROR); 1673247Sgjelinek } 1683247Sgjelinek 1693247Sgjelinek if (zonecfg_str_to_bytes(maxrss, &num) == -1) { 1703247Sgjelinek (void) fprintf(stderr, gettext("invalid max-rss value\n")); 1713247Sgjelinek return (E_ERROR); 1723247Sgjelinek } 1733247Sgjelinek 1743247Sgjelinek if (zone_setattr(zone_id, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 1753247Sgjelinek (void) fprintf(stderr, gettext("could not set memory " 1763247Sgjelinek "cap for zone '%s'\n"), zonename); 1773247Sgjelinek return (E_ERROR); 1783247Sgjelinek } 1793247Sgjelinek 1803247Sgjelinek return (E_SUCCESS); 1813247Sgjelinek } 1823247Sgjelinek 1830Sstevel@tonic-gate int 1840Sstevel@tonic-gate main(int argc, char *argv[]) 1850Sstevel@tonic-gate { 1862517Stn143363 char *subopts, *optval; 1870Sstevel@tonic-gate int modified = 0; 1883247Sgjelinek boolean_t refresh = B_FALSE; 1892517Stn143363 int opt; 1903247Sgjelinek char *zonename; 1913247Sgjelinek char *maxrss = NULL; 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate (void) setprogname("rcapadm"); 1940Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1950Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1960Sstevel@tonic-gate 1973247Sgjelinek while ((opt = getopt(argc, argv, "DEc:i:m:nz:")) != EOF) { 1980Sstevel@tonic-gate switch (opt) { 1990Sstevel@tonic-gate case 'n': 2000Sstevel@tonic-gate no_starting_stopping = 1; 2010Sstevel@tonic-gate break; 2020Sstevel@tonic-gate case 'c': 2030Sstevel@tonic-gate if ((pressure = xatoi(optarg)) < 0 || 2040Sstevel@tonic-gate pressure > 100 || 2050Sstevel@tonic-gate errno == EINVAL) 2060Sstevel@tonic-gate usage(); 2070Sstevel@tonic-gate modified++; 2080Sstevel@tonic-gate break; 2090Sstevel@tonic-gate case 'E': 2100Sstevel@tonic-gate enable = 1; 2110Sstevel@tonic-gate disable = 0; 2120Sstevel@tonic-gate break; 2130Sstevel@tonic-gate case 'D': 2140Sstevel@tonic-gate disable = 1; 2150Sstevel@tonic-gate enable = 0; 2160Sstevel@tonic-gate break; 2170Sstevel@tonic-gate case 'i': 2180Sstevel@tonic-gate subopts = optarg; 2190Sstevel@tonic-gate while (*subopts != '\0') { 2200Sstevel@tonic-gate switch (getsubopt(&subopts, subopt_v, 2210Sstevel@tonic-gate &optval)) { 2220Sstevel@tonic-gate case OPT_SCAN: 2230Sstevel@tonic-gate if (optval == NULL || 2240Sstevel@tonic-gate (scan_interval = 2250Sstevel@tonic-gate xatoi(optval)) <= 0) 2260Sstevel@tonic-gate usage(); 2270Sstevel@tonic-gate break; 2280Sstevel@tonic-gate case OPT_SAMPLE: 2290Sstevel@tonic-gate if (optval == NULL || 2300Sstevel@tonic-gate (sample_interval = 2310Sstevel@tonic-gate xatoi(optval)) <= 0) 2320Sstevel@tonic-gate usage(); 2330Sstevel@tonic-gate break; 2340Sstevel@tonic-gate case OPT_REPORT: 2350Sstevel@tonic-gate if (optval == NULL || 2360Sstevel@tonic-gate (report_interval = 2370Sstevel@tonic-gate xatoi(optval)) < 0) 2380Sstevel@tonic-gate usage(); 2390Sstevel@tonic-gate break; 2400Sstevel@tonic-gate case OPT_CONFIG: 2410Sstevel@tonic-gate if (optval == NULL || 2420Sstevel@tonic-gate (config_interval = 2430Sstevel@tonic-gate xatoi(optval)) < 0) 2440Sstevel@tonic-gate usage(); 2450Sstevel@tonic-gate break; 2460Sstevel@tonic-gate default: 2470Sstevel@tonic-gate usage(); 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate modified++; 2510Sstevel@tonic-gate break; 2523247Sgjelinek case 'm': 2533247Sgjelinek maxrss = optarg; 2543247Sgjelinek break; 2553247Sgjelinek case 'z': 2563247Sgjelinek refresh = B_TRUE; 2573247Sgjelinek zonename = optarg; 2583247Sgjelinek break; 2590Sstevel@tonic-gate default: 2600Sstevel@tonic-gate usage(); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate 2643247Sgjelinek /* the -z & -m options must be used together */ 2653247Sgjelinek if (argc > optind || (refresh && maxrss == NULL) || 2663247Sgjelinek (!refresh && maxrss != NULL)) 2673247Sgjelinek usage(); 2683247Sgjelinek 2693247Sgjelinek if (refresh && (no_starting_stopping > 0 || modified)) 2700Sstevel@tonic-gate usage(); 2710Sstevel@tonic-gate 272*4119Stn143363 /* 273*4119Stn143363 * disable/enable before reading configuration from the repository 274*4119Stn143363 * which may fail and prevents the disabling/enabling to complete. 275*4119Stn143363 */ 276*4119Stn143363 if (disable > 0) { 277*4119Stn143363 if (smf_disable_instance(RCAP_FMRI, no_starting_stopping > 0 278*4119Stn143363 ? SMF_AT_NEXT_BOOT : 0) != 0) 279*4119Stn143363 die(gettext("cannot disable service: %s\n"), 280*4119Stn143363 scf_strerror(scf_error())); 281*4119Stn143363 } 282*4119Stn143363 283*4119Stn143363 if (enable > 0) { 284*4119Stn143363 if (smf_enable_instance(RCAP_FMRI, no_starting_stopping > 0 285*4119Stn143363 ? SMF_AT_NEXT_BOOT : 0) != 0) 286*4119Stn143363 die(gettext("cannot enable service: %s\n"), 287*4119Stn143363 scf_strerror(scf_error())); 288*4119Stn143363 } 289*4119Stn143363 290*4119Stn143363 if (rcfg_read(&conf, NULL) != E_SUCCESS) { 291*4119Stn143363 /* 292*4119Stn143363 * If instance is enabled, put it in maintenance since we 293*4119Stn143363 * failed to read configuration from the repository or 294*4119Stn143363 * create statistics file. 295*4119Stn143363 */ 296*4119Stn143363 if (strcmp(smf_get_state(RCAP_FMRI), 297*4119Stn143363 SCF_STATE_STRING_DISABLED) != 0) 298*4119Stn143363 (void) smf_maintain_instance(RCAP_FMRI, 0); 299*4119Stn143363 300*4119Stn143363 die(gettext("resource caps not configured\n")); 3010Sstevel@tonic-gate } else { 302*4119Stn143363 /* Done reading configuration */ 3030Sstevel@tonic-gate if (strcmp(conf.rcfg_mode_name, "project") != 0) { 3040Sstevel@tonic-gate warn(gettext("%s mode specification ignored -- using" 3050Sstevel@tonic-gate " project mode\n"), conf.rcfg_mode_name); 3060Sstevel@tonic-gate conf.rcfg_mode_name = "project"; 3070Sstevel@tonic-gate conf.rcfg_mode = rctype_project; 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate 3113247Sgjelinek if (refresh) 3123247Sgjelinek return (update_zone_mcap(zonename, maxrss)); 3133247Sgjelinek 3140Sstevel@tonic-gate if (modified) { 3150Sstevel@tonic-gate if (pressure >= 0) 3160Sstevel@tonic-gate conf.rcfg_memory_cap_enforcement_pressure = pressure; 3170Sstevel@tonic-gate if (config_interval >= 0) 3180Sstevel@tonic-gate conf.rcfg_reconfiguration_interval = config_interval; 3190Sstevel@tonic-gate if (scan_interval >= 0) 3200Sstevel@tonic-gate conf.rcfg_proc_walk_interval = scan_interval; 3210Sstevel@tonic-gate if (report_interval >= 0) 3220Sstevel@tonic-gate conf.rcfg_report_interval = report_interval; 3230Sstevel@tonic-gate if (sample_interval >= 0) 3240Sstevel@tonic-gate conf.rcfg_rss_sample_interval = sample_interval; 3250Sstevel@tonic-gate 3262517Stn143363 /* 327*4119Stn143363 * Modify configuration with the new parameter(s). The 328*4119Stn143363 * function will exit if it fails. 3292517Stn143363 */ 330*4119Stn143363 if ((modify_config(&conf)) != 0) 331*4119Stn143363 die(gettext("Error updating repository \n")); 3320Sstevel@tonic-gate 333*4119Stn143363 if (smf_refresh_instance(RCAP_FMRI) != 0) 334*4119Stn143363 die(gettext("cannot refresh service: %s\n"), 3350Sstevel@tonic-gate scf_strerror(scf_error())); 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate /* 3390Sstevel@tonic-gate * Display current configuration 3400Sstevel@tonic-gate */ 3410Sstevel@tonic-gate print_state(); 3420Sstevel@tonic-gate return (E_SUCCESS); 3430Sstevel@tonic-gate } 344