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 5f1710550Stn143363 * Common Development and Distribution License (the "License"). 6f1710550Stn143363 * 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*d75e6a5dStn143363 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <sys/stat.h> 307c478bd9Sstevel@tonic-gate #include <sys/wait.h> 317c478bd9Sstevel@tonic-gate #include <fcntl.h> 327c478bd9Sstevel@tonic-gate #include <errno.h> 337c478bd9Sstevel@tonic-gate #include <signal.h> 347c478bd9Sstevel@tonic-gate #include <stdio.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <strings.h> 377c478bd9Sstevel@tonic-gate #include <unistd.h> 387c478bd9Sstevel@tonic-gate #include <libscf.h> 397c478bd9Sstevel@tonic-gate #include <libscf_priv.h> 407c478bd9Sstevel@tonic-gate #include <libintl.h> 417c478bd9Sstevel@tonic-gate #include <locale.h> 420209230bSgjelinek #include <zone.h> 430209230bSgjelinek #include <libzonecfg.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #include "utils.h" 467c478bd9Sstevel@tonic-gate #include "rcapd.h" 477c478bd9Sstevel@tonic-gate #include "rcapd_conf.h" 487c478bd9Sstevel@tonic-gate #include "rcapd_stat.h" 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate static void 517c478bd9Sstevel@tonic-gate usage() 527c478bd9Sstevel@tonic-gate { 537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 547c478bd9Sstevel@tonic-gate gettext("usage: rcapadm\n" 557c478bd9Sstevel@tonic-gate " [-E|-D] " 567c478bd9Sstevel@tonic-gate "# enable/disable rcapd\n" 577c478bd9Sstevel@tonic-gate " [-n] " 587c478bd9Sstevel@tonic-gate "# don't start/stop rcapd\n" 597c478bd9Sstevel@tonic-gate " [-i <scan|sample|report|config>=value] " 607c478bd9Sstevel@tonic-gate "# set intervals\n" 617c478bd9Sstevel@tonic-gate " [-c <percent>] " 627c478bd9Sstevel@tonic-gate "# set memory cap\n" 637c478bd9Sstevel@tonic-gate " " 640209230bSgjelinek "# enforcement threshold\n" 650209230bSgjelinek " [-z <zonename> -m <max-rss>] " 660209230bSgjelinek "# update zone memory cap\n")); 677c478bd9Sstevel@tonic-gate exit(E_USAGE); 687c478bd9Sstevel@tonic-gate } 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate static rcfg_t conf; 717c478bd9Sstevel@tonic-gate static int enable = -1; 727c478bd9Sstevel@tonic-gate static int disable = -1; 737c478bd9Sstevel@tonic-gate static int pressure = -1; 747c478bd9Sstevel@tonic-gate static int no_starting_stopping = -1; 757c478bd9Sstevel@tonic-gate static int scan_interval = -1; 767c478bd9Sstevel@tonic-gate static int report_interval = -1; 777c478bd9Sstevel@tonic-gate static int config_interval = -1; 787c478bd9Sstevel@tonic-gate static int sample_interval = -1; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate static char *subopt_v[] = { 817c478bd9Sstevel@tonic-gate "scan", 827c478bd9Sstevel@tonic-gate "sample", 837c478bd9Sstevel@tonic-gate "report", 847c478bd9Sstevel@tonic-gate "config", 857c478bd9Sstevel@tonic-gate NULL 867c478bd9Sstevel@tonic-gate }; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate typedef enum { 897c478bd9Sstevel@tonic-gate OPT_SCAN = 0, 907c478bd9Sstevel@tonic-gate OPT_SAMPLE, 917c478bd9Sstevel@tonic-gate OPT_REPORT, 927c478bd9Sstevel@tonic-gate OPT_CONFIG 937c478bd9Sstevel@tonic-gate } subopt_idx_t; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate static void 967c478bd9Sstevel@tonic-gate print_state(void) 977c478bd9Sstevel@tonic-gate { 987c478bd9Sstevel@tonic-gate scf_simple_prop_t *persistent_prop = NULL; 997c478bd9Sstevel@tonic-gate scf_simple_prop_t *temporary_prop = NULL; 1007c478bd9Sstevel@tonic-gate uint8_t *persistent = NULL; 1017c478bd9Sstevel@tonic-gate uint8_t *temporary = NULL; 1027c478bd9Sstevel@tonic-gate scf_handle_t *h; 1037c478bd9Sstevel@tonic-gate /* LINTED: conditionally assigned and used in function */ 1047c478bd9Sstevel@tonic-gate ssize_t numvals; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate if ((h = scf_handle_create(SCF_VERSION)) == NULL || 1077c478bd9Sstevel@tonic-gate scf_handle_bind(h) != 0) 1087c478bd9Sstevel@tonic-gate goto out; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate if ((persistent_prop = scf_simple_prop_get(h, RCAP_FMRI, 1117c478bd9Sstevel@tonic-gate SCF_PG_GENERAL, SCF_PROPERTY_ENABLED)) != NULL && (numvals = 1127c478bd9Sstevel@tonic-gate scf_simple_prop_numvalues(persistent_prop)) > 0) 1137c478bd9Sstevel@tonic-gate persistent = scf_simple_prop_next_boolean(persistent_prop); 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate if ((temporary_prop = scf_simple_prop_get(h, RCAP_FMRI, 1167c478bd9Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED)) != NULL && (numvals = 1177c478bd9Sstevel@tonic-gate scf_simple_prop_numvalues(temporary_prop)) > 0) 1187c478bd9Sstevel@tonic-gate temporary = scf_simple_prop_next_boolean(temporary_prop); 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate out: 1217c478bd9Sstevel@tonic-gate if (!persistent) 1227c478bd9Sstevel@tonic-gate (void) printf(gettext(" " 1237c478bd9Sstevel@tonic-gate "state: unknown")); 1247c478bd9Sstevel@tonic-gate else if (temporary && *temporary != *persistent) 1257c478bd9Sstevel@tonic-gate (void) printf(gettext(" " 1267c478bd9Sstevel@tonic-gate "state: %s (%s at next boot)\n"), *temporary ? 1277c478bd9Sstevel@tonic-gate gettext("enabled") : gettext("disabled"), *persistent ? 1287c478bd9Sstevel@tonic-gate gettext("enabled") : gettext("disabled")); 1297c478bd9Sstevel@tonic-gate else 1307c478bd9Sstevel@tonic-gate (void) printf(gettext(" " 1317c478bd9Sstevel@tonic-gate "state: %s\n"), *persistent ? gettext("enabled") : 1327c478bd9Sstevel@tonic-gate gettext("disabled")); 1337c478bd9Sstevel@tonic-gate 134*d75e6a5dStn143363 (void) printf(gettext(" memory cap enforcement" 135*d75e6a5dStn143363 " threshold: %d%%\n"), conf.rcfg_memory_cap_enforcement_pressure); 136*d75e6a5dStn143363 (void) printf(gettext(" process scan rate" 137*d75e6a5dStn143363 " (sec): %d\n"), conf.rcfg_proc_walk_interval); 138*d75e6a5dStn143363 (void) printf(gettext(" reconfiguration rate" 139*d75e6a5dStn143363 " (sec): %d\n"), conf.rcfg_reconfiguration_interval); 140*d75e6a5dStn143363 (void) printf(gettext(" report rate" 141*d75e6a5dStn143363 " (sec): %d\n"), conf.rcfg_report_interval); 142*d75e6a5dStn143363 (void) printf(gettext(" RSS sampling rate" 143*d75e6a5dStn143363 " (sec): %d\n"), conf.rcfg_rss_sample_interval); 144*d75e6a5dStn143363 1457c478bd9Sstevel@tonic-gate scf_simple_prop_free(temporary_prop); 1467c478bd9Sstevel@tonic-gate scf_simple_prop_free(persistent_prop); 1477c478bd9Sstevel@tonic-gate scf_handle_destroy(h); 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate 1500209230bSgjelinek /* 1510209230bSgjelinek * Update the in-kernel memory cap for the specified zone. 1520209230bSgjelinek */ 1530209230bSgjelinek static int 1540209230bSgjelinek update_zone_mcap(char *zonename, char *maxrss) 1550209230bSgjelinek { 1560209230bSgjelinek zoneid_t zone_id; 1570209230bSgjelinek uint64_t num; 1580209230bSgjelinek 1590209230bSgjelinek if (getzoneid() != GLOBAL_ZONEID || zonecfg_in_alt_root()) 1600209230bSgjelinek return (E_SUCCESS); 1610209230bSgjelinek 1620209230bSgjelinek /* get the running zone from the kernel */ 1630209230bSgjelinek if ((zone_id = getzoneidbyname(zonename)) == -1) { 1640209230bSgjelinek (void) fprintf(stderr, gettext("zone '%s' must be running\n"), 1650209230bSgjelinek zonename); 1660209230bSgjelinek return (E_ERROR); 1670209230bSgjelinek } 1680209230bSgjelinek 1690209230bSgjelinek if (zonecfg_str_to_bytes(maxrss, &num) == -1) { 1700209230bSgjelinek (void) fprintf(stderr, gettext("invalid max-rss value\n")); 1710209230bSgjelinek return (E_ERROR); 1720209230bSgjelinek } 1730209230bSgjelinek 1740209230bSgjelinek if (zone_setattr(zone_id, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 1750209230bSgjelinek (void) fprintf(stderr, gettext("could not set memory " 1760209230bSgjelinek "cap for zone '%s'\n"), zonename); 1770209230bSgjelinek return (E_ERROR); 1780209230bSgjelinek } 1790209230bSgjelinek 1800209230bSgjelinek return (E_SUCCESS); 1810209230bSgjelinek } 1820209230bSgjelinek 1837c478bd9Sstevel@tonic-gate int 1847c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1857c478bd9Sstevel@tonic-gate { 186f1710550Stn143363 char *subopts, *optval; 1877c478bd9Sstevel@tonic-gate int modified = 0; 1880209230bSgjelinek boolean_t refresh = B_FALSE; 189f1710550Stn143363 int opt; 1900209230bSgjelinek char *zonename; 1910209230bSgjelinek char *maxrss = NULL; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate (void) setprogname("rcapadm"); 1947c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1957c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1967c478bd9Sstevel@tonic-gate 1970209230bSgjelinek while ((opt = getopt(argc, argv, "DEc:i:m:nz:")) != EOF) { 1987c478bd9Sstevel@tonic-gate switch (opt) { 1997c478bd9Sstevel@tonic-gate case 'n': 2007c478bd9Sstevel@tonic-gate no_starting_stopping = 1; 2017c478bd9Sstevel@tonic-gate break; 2027c478bd9Sstevel@tonic-gate case 'c': 2037c478bd9Sstevel@tonic-gate if ((pressure = xatoi(optarg)) < 0 || 2047c478bd9Sstevel@tonic-gate pressure > 100 || 2057c478bd9Sstevel@tonic-gate errno == EINVAL) 2067c478bd9Sstevel@tonic-gate usage(); 2077c478bd9Sstevel@tonic-gate modified++; 2087c478bd9Sstevel@tonic-gate break; 2097c478bd9Sstevel@tonic-gate case 'E': 2107c478bd9Sstevel@tonic-gate enable = 1; 2117c478bd9Sstevel@tonic-gate disable = 0; 2127c478bd9Sstevel@tonic-gate break; 2137c478bd9Sstevel@tonic-gate case 'D': 2147c478bd9Sstevel@tonic-gate disable = 1; 2157c478bd9Sstevel@tonic-gate enable = 0; 2167c478bd9Sstevel@tonic-gate break; 2177c478bd9Sstevel@tonic-gate case 'i': 2187c478bd9Sstevel@tonic-gate subopts = optarg; 2197c478bd9Sstevel@tonic-gate while (*subopts != '\0') { 2207c478bd9Sstevel@tonic-gate switch (getsubopt(&subopts, subopt_v, 2217c478bd9Sstevel@tonic-gate &optval)) { 2227c478bd9Sstevel@tonic-gate case OPT_SCAN: 2237c478bd9Sstevel@tonic-gate if (optval == NULL || 2247c478bd9Sstevel@tonic-gate (scan_interval = 2257c478bd9Sstevel@tonic-gate xatoi(optval)) <= 0) 2267c478bd9Sstevel@tonic-gate usage(); 2277c478bd9Sstevel@tonic-gate break; 2287c478bd9Sstevel@tonic-gate case OPT_SAMPLE: 2297c478bd9Sstevel@tonic-gate if (optval == NULL || 2307c478bd9Sstevel@tonic-gate (sample_interval = 2317c478bd9Sstevel@tonic-gate xatoi(optval)) <= 0) 2327c478bd9Sstevel@tonic-gate usage(); 2337c478bd9Sstevel@tonic-gate break; 2347c478bd9Sstevel@tonic-gate case OPT_REPORT: 2357c478bd9Sstevel@tonic-gate if (optval == NULL || 2367c478bd9Sstevel@tonic-gate (report_interval = 2377c478bd9Sstevel@tonic-gate xatoi(optval)) < 0) 2387c478bd9Sstevel@tonic-gate usage(); 2397c478bd9Sstevel@tonic-gate break; 2407c478bd9Sstevel@tonic-gate case OPT_CONFIG: 2417c478bd9Sstevel@tonic-gate if (optval == NULL || 2427c478bd9Sstevel@tonic-gate (config_interval = 2437c478bd9Sstevel@tonic-gate xatoi(optval)) < 0) 2447c478bd9Sstevel@tonic-gate usage(); 2457c478bd9Sstevel@tonic-gate break; 2467c478bd9Sstevel@tonic-gate default: 2477c478bd9Sstevel@tonic-gate usage(); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate modified++; 2517c478bd9Sstevel@tonic-gate break; 2520209230bSgjelinek case 'm': 2530209230bSgjelinek maxrss = optarg; 2540209230bSgjelinek break; 2550209230bSgjelinek case 'z': 2560209230bSgjelinek refresh = B_TRUE; 2570209230bSgjelinek zonename = optarg; 2580209230bSgjelinek break; 2597c478bd9Sstevel@tonic-gate default: 2607c478bd9Sstevel@tonic-gate usage(); 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate } 2637c478bd9Sstevel@tonic-gate 2640209230bSgjelinek /* the -z & -m options must be used together */ 2650209230bSgjelinek if (argc > optind || (refresh && maxrss == NULL) || 2660209230bSgjelinek (!refresh && maxrss != NULL)) 2670209230bSgjelinek usage(); 2680209230bSgjelinek 2690209230bSgjelinek if (refresh && (no_starting_stopping > 0 || modified)) 2707c478bd9Sstevel@tonic-gate usage(); 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate /* 273*d75e6a5dStn143363 * disable/enable before reading configuration from the repository 274*d75e6a5dStn143363 * which may fail and prevents the disabling/enabling to complete. 2757c478bd9Sstevel@tonic-gate */ 276*d75e6a5dStn143363 if (disable > 0) { 277*d75e6a5dStn143363 if (smf_disable_instance(RCAP_FMRI, no_starting_stopping > 0 278*d75e6a5dStn143363 ? SMF_AT_NEXT_BOOT : 0) != 0) 279*d75e6a5dStn143363 die(gettext("cannot disable service: %s\n"), 280*d75e6a5dStn143363 scf_strerror(scf_error())); 281*d75e6a5dStn143363 } 282*d75e6a5dStn143363 283*d75e6a5dStn143363 if (enable > 0) { 284*d75e6a5dStn143363 if (smf_enable_instance(RCAP_FMRI, no_starting_stopping > 0 285*d75e6a5dStn143363 ? SMF_AT_NEXT_BOOT : 0) != 0) 286*d75e6a5dStn143363 die(gettext("cannot enable service: %s\n"), 287*d75e6a5dStn143363 scf_strerror(scf_error())); 288*d75e6a5dStn143363 } 289*d75e6a5dStn143363 290*d75e6a5dStn143363 if (rcfg_read(&conf, NULL) != E_SUCCESS) { 291*d75e6a5dStn143363 /* 292*d75e6a5dStn143363 * If instance is enabled, put it in maintenance since we 293*d75e6a5dStn143363 * failed to read configuration from the repository or 294*d75e6a5dStn143363 * create statistics file. 295*d75e6a5dStn143363 */ 296*d75e6a5dStn143363 if (strcmp(smf_get_state(RCAP_FMRI), 297*d75e6a5dStn143363 SCF_STATE_STRING_DISABLED) != 0) 298*d75e6a5dStn143363 (void) smf_maintain_instance(RCAP_FMRI, 0); 299*d75e6a5dStn143363 300*d75e6a5dStn143363 die(gettext("resource caps not configured\n")); 301*d75e6a5dStn143363 } else { 302*d75e6a5dStn143363 /* Done reading configuration */ 3037c478bd9Sstevel@tonic-gate if (strcmp(conf.rcfg_mode_name, "project") != 0) { 3047c478bd9Sstevel@tonic-gate warn(gettext("%s mode specification ignored -- using" 3057c478bd9Sstevel@tonic-gate " project mode\n"), conf.rcfg_mode_name); 3067c478bd9Sstevel@tonic-gate conf.rcfg_mode_name = "project"; 3077c478bd9Sstevel@tonic-gate conf.rcfg_mode = rctype_project; 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate 3110209230bSgjelinek if (refresh) 3120209230bSgjelinek return (update_zone_mcap(zonename, maxrss)); 3130209230bSgjelinek 3147c478bd9Sstevel@tonic-gate if (modified) { 3157c478bd9Sstevel@tonic-gate if (pressure >= 0) 3167c478bd9Sstevel@tonic-gate conf.rcfg_memory_cap_enforcement_pressure = pressure; 3177c478bd9Sstevel@tonic-gate if (config_interval >= 0) 3187c478bd9Sstevel@tonic-gate conf.rcfg_reconfiguration_interval = config_interval; 3197c478bd9Sstevel@tonic-gate if (scan_interval >= 0) 3207c478bd9Sstevel@tonic-gate conf.rcfg_proc_walk_interval = scan_interval; 3217c478bd9Sstevel@tonic-gate if (report_interval >= 0) 3227c478bd9Sstevel@tonic-gate conf.rcfg_report_interval = report_interval; 3237c478bd9Sstevel@tonic-gate if (sample_interval >= 0) 3247c478bd9Sstevel@tonic-gate conf.rcfg_rss_sample_interval = sample_interval; 3257c478bd9Sstevel@tonic-gate 326f1710550Stn143363 /* 327*d75e6a5dStn143363 * Modify configuration with the new parameter(s). The 328*d75e6a5dStn143363 * function will exit if it fails. 329f1710550Stn143363 */ 330*d75e6a5dStn143363 if ((modify_config(&conf)) != 0) 331*d75e6a5dStn143363 die(gettext("Error updating repository \n")); 3327c478bd9Sstevel@tonic-gate 333*d75e6a5dStn143363 if (smf_refresh_instance(RCAP_FMRI) != 0) 334*d75e6a5dStn143363 die(gettext("cannot refresh service: %s\n"), 3357c478bd9Sstevel@tonic-gate scf_strerror(scf_error())); 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate /* 3397c478bd9Sstevel@tonic-gate * Display current configuration 3407c478bd9Sstevel@tonic-gate */ 3417c478bd9Sstevel@tonic-gate print_state(); 3427c478bd9Sstevel@tonic-gate return (E_SUCCESS); 3437c478bd9Sstevel@tonic-gate } 344