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