xref: /onnv-gate/usr/src/cmd/rcap/rcapadm/rcapadm.c (revision 13093:48f2dbca79a2)
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  */
21*13093SRoger.Faulkner@Oracle.COM 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/stat.h>
280Sstevel@tonic-gate #include <sys/wait.h>
290Sstevel@tonic-gate #include <fcntl.h>
300Sstevel@tonic-gate #include <errno.h>
310Sstevel@tonic-gate #include <signal.h>
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <strings.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <libscf.h>
370Sstevel@tonic-gate #include <libscf_priv.h>
380Sstevel@tonic-gate #include <libintl.h>
390Sstevel@tonic-gate #include <locale.h>
403247Sgjelinek #include <zone.h>
413247Sgjelinek #include <libzonecfg.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include "utils.h"
440Sstevel@tonic-gate #include "rcapd.h"
450Sstevel@tonic-gate #include "rcapd_conf.h"
460Sstevel@tonic-gate #include "rcapd_stat.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static void
usage()490Sstevel@tonic-gate usage()
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	(void) fprintf(stderr,
520Sstevel@tonic-gate 	    gettext("usage: rcapadm\n"
530Sstevel@tonic-gate 	    "               [-E|-D]                                "
540Sstevel@tonic-gate 	    "# enable/disable rcapd\n"
550Sstevel@tonic-gate 	    "               [-n]                                   "
560Sstevel@tonic-gate 	    "# don't start/stop rcapd\n"
570Sstevel@tonic-gate 	    "               [-i <scan|sample|report|config>=value] "
580Sstevel@tonic-gate 	    "# set intervals\n"
590Sstevel@tonic-gate 	    "               [-c <percent>]                         "
600Sstevel@tonic-gate 	    "# set memory cap\n"
610Sstevel@tonic-gate 	    "                                                      "
623247Sgjelinek 	    "# enforcement threshold\n"
633247Sgjelinek 	    "               [-z <zonename> -m <max-rss>]               "
643247Sgjelinek 	    "# update zone memory cap\n"));
650Sstevel@tonic-gate 	exit(E_USAGE);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static rcfg_t conf;
690Sstevel@tonic-gate static int enable = -1;
700Sstevel@tonic-gate static int disable = -1;
710Sstevel@tonic-gate static int pressure = -1;
720Sstevel@tonic-gate static int no_starting_stopping = -1;
730Sstevel@tonic-gate static int scan_interval = -1;
740Sstevel@tonic-gate static int report_interval = -1;
750Sstevel@tonic-gate static int config_interval = -1;
760Sstevel@tonic-gate static int sample_interval = -1;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static char *subopt_v[] = {
790Sstevel@tonic-gate 	"scan",
800Sstevel@tonic-gate 	"sample",
810Sstevel@tonic-gate 	"report",
820Sstevel@tonic-gate 	"config",
830Sstevel@tonic-gate 	NULL
840Sstevel@tonic-gate };
850Sstevel@tonic-gate 
860Sstevel@tonic-gate typedef enum {
870Sstevel@tonic-gate 	OPT_SCAN = 0,
880Sstevel@tonic-gate 	OPT_SAMPLE,
890Sstevel@tonic-gate 	OPT_REPORT,
900Sstevel@tonic-gate 	OPT_CONFIG
910Sstevel@tonic-gate } subopt_idx_t;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate static void
print_state(void)940Sstevel@tonic-gate print_state(void)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	scf_simple_prop_t *persistent_prop = NULL;
970Sstevel@tonic-gate 	scf_simple_prop_t *temporary_prop = NULL;
980Sstevel@tonic-gate 	uint8_t *persistent = NULL;
990Sstevel@tonic-gate 	uint8_t *temporary = NULL;
1000Sstevel@tonic-gate 	scf_handle_t *h;
1010Sstevel@tonic-gate 	/* LINTED: conditionally assigned and used in function */
1020Sstevel@tonic-gate 	ssize_t numvals;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if ((h = scf_handle_create(SCF_VERSION)) == NULL ||
1050Sstevel@tonic-gate 	    scf_handle_bind(h) != 0)
1060Sstevel@tonic-gate 		goto out;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if ((persistent_prop = scf_simple_prop_get(h, RCAP_FMRI,
1090Sstevel@tonic-gate 	    SCF_PG_GENERAL, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
1100Sstevel@tonic-gate 	    scf_simple_prop_numvalues(persistent_prop)) > 0)
1110Sstevel@tonic-gate 		persistent = scf_simple_prop_next_boolean(persistent_prop);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if ((temporary_prop = scf_simple_prop_get(h, RCAP_FMRI,
1140Sstevel@tonic-gate 	    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
1150Sstevel@tonic-gate 	    scf_simple_prop_numvalues(temporary_prop)) > 0)
1160Sstevel@tonic-gate 		temporary = scf_simple_prop_next_boolean(temporary_prop);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate out:
1190Sstevel@tonic-gate 	if (!persistent)
1200Sstevel@tonic-gate 		(void) printf(gettext("                                      "
1210Sstevel@tonic-gate 		    "state: unknown"));
1220Sstevel@tonic-gate 	else if (temporary && *temporary != *persistent)
1230Sstevel@tonic-gate 		(void) printf(gettext("                                      "
1240Sstevel@tonic-gate 		    "state: %s (%s at next boot)\n"), *temporary ?
1250Sstevel@tonic-gate 		    gettext("enabled") : gettext("disabled"), *persistent ?
1260Sstevel@tonic-gate 		    gettext("enabled") : gettext("disabled"));
1270Sstevel@tonic-gate 	else
1280Sstevel@tonic-gate 		(void) printf(gettext("                                      "
1290Sstevel@tonic-gate 		    "state: %s\n"), *persistent ? gettext("enabled") :
130*13093SRoger.Faulkner@Oracle.COM 		    gettext("disabled"));
1310Sstevel@tonic-gate 
1324119Stn143363 	(void) printf(gettext("           memory cap enforcement"
1334119Stn143363 	    " threshold: %d%%\n"), conf.rcfg_memory_cap_enforcement_pressure);
1344119Stn143363 	(void) printf(gettext("                    process scan rate"
1354119Stn143363 	    " (sec): %d\n"), conf.rcfg_proc_walk_interval);
1364119Stn143363 	(void) printf(gettext("                 reconfiguration rate"
1374119Stn143363 	    " (sec): %d\n"), conf.rcfg_reconfiguration_interval);
1384119Stn143363 	(void) printf(gettext("                          report rate"
1394119Stn143363 	    " (sec): %d\n"), conf.rcfg_report_interval);
1404119Stn143363 	(void) printf(gettext("                    RSS sampling rate"
1414119Stn143363 	    " (sec): %d\n"), conf.rcfg_rss_sample_interval);
1424119Stn143363 
1430Sstevel@tonic-gate 	scf_simple_prop_free(temporary_prop);
1440Sstevel@tonic-gate 	scf_simple_prop_free(persistent_prop);
1450Sstevel@tonic-gate 	scf_handle_destroy(h);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1483247Sgjelinek /*
1493247Sgjelinek  * Update the in-kernel memory cap for the specified zone.
1503247Sgjelinek  */
1513247Sgjelinek static int
update_zone_mcap(char * zonename,char * maxrss)1523247Sgjelinek update_zone_mcap(char *zonename, char *maxrss)
1533247Sgjelinek {
1543247Sgjelinek 	zoneid_t zone_id;
1553247Sgjelinek 	uint64_t num;
1563247Sgjelinek 
1573247Sgjelinek 	if (getzoneid() != GLOBAL_ZONEID || zonecfg_in_alt_root())
1583247Sgjelinek 		return (E_SUCCESS);
1593247Sgjelinek 
1603247Sgjelinek 	/* get the running zone from the kernel */
1613247Sgjelinek 	if ((zone_id = getzoneidbyname(zonename)) == -1) {
1623247Sgjelinek 		(void) fprintf(stderr, gettext("zone '%s' must be running\n"),
1633247Sgjelinek 		    zonename);
1643247Sgjelinek 		return (E_ERROR);
1653247Sgjelinek 	}
1663247Sgjelinek 
1673247Sgjelinek 	if (zonecfg_str_to_bytes(maxrss, &num) == -1) {
1683247Sgjelinek 		(void) fprintf(stderr, gettext("invalid max-rss value\n"));
1693247Sgjelinek 		return (E_ERROR);
1703247Sgjelinek 	}
1713247Sgjelinek 
1723247Sgjelinek 	if (zone_setattr(zone_id, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
1733247Sgjelinek 		(void) fprintf(stderr, gettext("could not set memory "
1743247Sgjelinek 		    "cap for zone '%s'\n"), zonename);
1753247Sgjelinek 		return (E_ERROR);
1763247Sgjelinek 	}
1773247Sgjelinek 
1783247Sgjelinek 	return (E_SUCCESS);
1793247Sgjelinek }
1803247Sgjelinek 
1810Sstevel@tonic-gate int
main(int argc,char * argv[])1820Sstevel@tonic-gate main(int argc, char *argv[])
1830Sstevel@tonic-gate {
1842517Stn143363 	char *subopts, *optval;
1850Sstevel@tonic-gate 	int modified = 0;
1863247Sgjelinek 	boolean_t refresh = B_FALSE;
1872517Stn143363 	int opt;
1883247Sgjelinek 	char *zonename;
1893247Sgjelinek 	char *maxrss = NULL;
1900Sstevel@tonic-gate 
191*13093SRoger.Faulkner@Oracle.COM 	(void) setpname("rcapadm");
1920Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1930Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1940Sstevel@tonic-gate 
1953247Sgjelinek 	while ((opt = getopt(argc, argv, "DEc:i:m:nz:")) != EOF) {
1960Sstevel@tonic-gate 		switch (opt) {
1970Sstevel@tonic-gate 		case 'n':
1980Sstevel@tonic-gate 			no_starting_stopping = 1;
1990Sstevel@tonic-gate 			break;
2000Sstevel@tonic-gate 		case 'c':
2010Sstevel@tonic-gate 			if ((pressure = xatoi(optarg)) < 0 ||
2020Sstevel@tonic-gate 			    pressure > 100 ||
2030Sstevel@tonic-gate 			    errno == EINVAL)
2040Sstevel@tonic-gate 				usage();
2050Sstevel@tonic-gate 			modified++;
2060Sstevel@tonic-gate 			break;
2070Sstevel@tonic-gate 		case 'E':
2080Sstevel@tonic-gate 			enable = 1;
2090Sstevel@tonic-gate 			disable = 0;
2100Sstevel@tonic-gate 			break;
2110Sstevel@tonic-gate 		case 'D':
2120Sstevel@tonic-gate 			disable = 1;
2130Sstevel@tonic-gate 			enable = 0;
2140Sstevel@tonic-gate 			break;
2150Sstevel@tonic-gate 		case 'i':
2160Sstevel@tonic-gate 			subopts = optarg;
2170Sstevel@tonic-gate 			while (*subopts != '\0') {
2180Sstevel@tonic-gate 				switch (getsubopt(&subopts, subopt_v,
2190Sstevel@tonic-gate 				    &optval)) {
2200Sstevel@tonic-gate 					case OPT_SCAN:
2210Sstevel@tonic-gate 						if (optval == NULL ||
2220Sstevel@tonic-gate 						    (scan_interval =
2230Sstevel@tonic-gate 						    xatoi(optval)) <= 0)
2240Sstevel@tonic-gate 							usage();
2250Sstevel@tonic-gate 						break;
2260Sstevel@tonic-gate 					case OPT_SAMPLE:
2270Sstevel@tonic-gate 						if (optval == NULL ||
2280Sstevel@tonic-gate 						    (sample_interval =
2290Sstevel@tonic-gate 						    xatoi(optval)) <= 0)
2300Sstevel@tonic-gate 							usage();
2310Sstevel@tonic-gate 						break;
2320Sstevel@tonic-gate 					case OPT_REPORT:
2330Sstevel@tonic-gate 						if (optval == NULL ||
2340Sstevel@tonic-gate 						    (report_interval =
2350Sstevel@tonic-gate 						    xatoi(optval)) < 0)
2360Sstevel@tonic-gate 							usage();
2370Sstevel@tonic-gate 						break;
2380Sstevel@tonic-gate 					case OPT_CONFIG:
2390Sstevel@tonic-gate 						if (optval == NULL ||
2400Sstevel@tonic-gate 						    (config_interval =
2410Sstevel@tonic-gate 						    xatoi(optval)) < 0)
2420Sstevel@tonic-gate 							usage();
2430Sstevel@tonic-gate 						break;
2440Sstevel@tonic-gate 					default:
2450Sstevel@tonic-gate 						usage();
2460Sstevel@tonic-gate 				}
2470Sstevel@tonic-gate 			}
2480Sstevel@tonic-gate 			modified++;
2490Sstevel@tonic-gate 			break;
2503247Sgjelinek 		case 'm':
2513247Sgjelinek 			maxrss = optarg;
2523247Sgjelinek 			break;
2533247Sgjelinek 		case 'z':
2543247Sgjelinek 			refresh = B_TRUE;
2553247Sgjelinek 			zonename = optarg;
2563247Sgjelinek 			break;
2570Sstevel@tonic-gate 		default:
2580Sstevel@tonic-gate 			usage();
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 
2623247Sgjelinek 	/* the -z & -m options must be used together */
2633247Sgjelinek 	if (argc > optind || (refresh && maxrss == NULL) ||
2643247Sgjelinek 	    (!refresh && maxrss != NULL))
2653247Sgjelinek 		usage();
2663247Sgjelinek 
2673247Sgjelinek 	if (refresh && (no_starting_stopping > 0 || modified))
2680Sstevel@tonic-gate 		usage();
2690Sstevel@tonic-gate 
2704119Stn143363 	/*
2714119Stn143363 	 * disable/enable before reading configuration from the repository
2724119Stn143363 	 * which may fail and prevents the disabling/enabling to complete.
2734119Stn143363 	 */
2744119Stn143363 	if (disable > 0) {
2754119Stn143363 		if (smf_disable_instance(RCAP_FMRI, no_starting_stopping > 0
2764119Stn143363 		    ? SMF_AT_NEXT_BOOT : 0) != 0)
2774119Stn143363 			die(gettext("cannot disable service: %s\n"),
2784119Stn143363 			    scf_strerror(scf_error()));
2794119Stn143363 	}
2804119Stn143363 
2814119Stn143363 	if (enable > 0) {
2824119Stn143363 		if (smf_enable_instance(RCAP_FMRI, no_starting_stopping > 0
2834119Stn143363 		    ? SMF_AT_NEXT_BOOT : 0) != 0)
2844119Stn143363 			die(gettext("cannot enable service: %s\n"),
2854119Stn143363 			    scf_strerror(scf_error()));
2864119Stn143363 	}
2874119Stn143363 
2884119Stn143363 	if (rcfg_read(&conf, NULL) != E_SUCCESS) {
2894119Stn143363 		/*
2904119Stn143363 		 * If instance is enabled, put it in maintenance since we
2914119Stn143363 		 * failed to read configuration from the repository or
2924119Stn143363 		 * create statistics file.
2934119Stn143363 		 */
2944119Stn143363 		if (strcmp(smf_get_state(RCAP_FMRI),
2954119Stn143363 		    SCF_STATE_STRING_DISABLED) != 0)
2964119Stn143363 			(void) smf_maintain_instance(RCAP_FMRI, 0);
2974119Stn143363 
2984119Stn143363 		die(gettext("resource caps not configured\n"));
2990Sstevel@tonic-gate 	} else {
3004119Stn143363 		/* Done reading configuration */
3010Sstevel@tonic-gate 		if (strcmp(conf.rcfg_mode_name, "project") != 0) {
3020Sstevel@tonic-gate 			warn(gettext("%s mode specification ignored -- using"
3030Sstevel@tonic-gate 			    " project mode\n"), conf.rcfg_mode_name);
3040Sstevel@tonic-gate 			conf.rcfg_mode_name = "project";
3050Sstevel@tonic-gate 			conf.rcfg_mode = rctype_project;
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3093247Sgjelinek 	if (refresh)
3103247Sgjelinek 		return (update_zone_mcap(zonename, maxrss));
3113247Sgjelinek 
3120Sstevel@tonic-gate 	if (modified) {
3130Sstevel@tonic-gate 		if (pressure >= 0)
3140Sstevel@tonic-gate 			conf.rcfg_memory_cap_enforcement_pressure = pressure;
3150Sstevel@tonic-gate 		if (config_interval >= 0)
3160Sstevel@tonic-gate 			conf.rcfg_reconfiguration_interval = config_interval;
3170Sstevel@tonic-gate 		if (scan_interval >= 0)
3180Sstevel@tonic-gate 			conf.rcfg_proc_walk_interval = scan_interval;
3190Sstevel@tonic-gate 		if (report_interval >= 0)
3200Sstevel@tonic-gate 			conf.rcfg_report_interval = report_interval;
3210Sstevel@tonic-gate 		if (sample_interval >= 0)
3220Sstevel@tonic-gate 			conf.rcfg_rss_sample_interval = sample_interval;
3230Sstevel@tonic-gate 
3242517Stn143363 		/*
3254119Stn143363 		 * Modify configuration with the new parameter(s). The
3264119Stn143363 		 * function will exit if it fails.
3272517Stn143363 		 */
3284119Stn143363 		if ((modify_config(&conf)) != 0)
3294119Stn143363 			die(gettext("Error updating repository \n"));
3300Sstevel@tonic-gate 
3314119Stn143363 		if (smf_refresh_instance(RCAP_FMRI) != 0)
3324119Stn143363 			die(gettext("cannot refresh service: %s\n"),
3330Sstevel@tonic-gate 			    scf_strerror(scf_error()));
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/*
3370Sstevel@tonic-gate 	 * Display current configuration
3380Sstevel@tonic-gate 	 */
3390Sstevel@tonic-gate 	print_state();
3400Sstevel@tonic-gate 	return (E_SUCCESS);
3410Sstevel@tonic-gate }
342