xref: /onnv-gate/usr/src/cmd/rcap/rcapadm/rcapadm.c (revision 2517:89c2fb48dc84)
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
5*2517Stn143363  * Common Development and Distribution License (the "License").
6*2517Stn143363  * 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*2517Stn143363  * Copyright 2006 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>
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 #define	RCAP_FMRI		"system/rcap:default"
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 	    "                                                      "
640Sstevel@tonic-gate 	    "# enforcement threshold\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 static char *fname = RCAPD_DEFAULT_CONF_FILE;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static char *subopt_v[] = {
800Sstevel@tonic-gate 	"scan",
810Sstevel@tonic-gate 	"sample",
820Sstevel@tonic-gate 	"report",
830Sstevel@tonic-gate 	"config",
840Sstevel@tonic-gate 	NULL
850Sstevel@tonic-gate };
860Sstevel@tonic-gate 
870Sstevel@tonic-gate typedef enum {
880Sstevel@tonic-gate 	OPT_SCAN = 0,
890Sstevel@tonic-gate 	OPT_SAMPLE,
900Sstevel@tonic-gate 	OPT_REPORT,
910Sstevel@tonic-gate 	OPT_CONFIG
920Sstevel@tonic-gate } subopt_idx_t;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static void
950Sstevel@tonic-gate print_state(void)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	scf_simple_prop_t *persistent_prop = NULL;
980Sstevel@tonic-gate 	scf_simple_prop_t *temporary_prop = NULL;
990Sstevel@tonic-gate 	uint8_t *persistent = NULL;
1000Sstevel@tonic-gate 	uint8_t *temporary = NULL;
1010Sstevel@tonic-gate 	scf_handle_t *h;
1020Sstevel@tonic-gate 	/* LINTED: conditionally assigned and used in function */
1030Sstevel@tonic-gate 	ssize_t numvals;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	if ((h = scf_handle_create(SCF_VERSION)) == NULL ||
1060Sstevel@tonic-gate 	    scf_handle_bind(h) != 0)
1070Sstevel@tonic-gate 		goto out;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if ((persistent_prop = scf_simple_prop_get(h, RCAP_FMRI,
1100Sstevel@tonic-gate 	    SCF_PG_GENERAL, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
1110Sstevel@tonic-gate 	    scf_simple_prop_numvalues(persistent_prop)) > 0)
1120Sstevel@tonic-gate 		persistent = scf_simple_prop_next_boolean(persistent_prop);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if ((temporary_prop = scf_simple_prop_get(h, RCAP_FMRI,
1150Sstevel@tonic-gate 	    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
1160Sstevel@tonic-gate 	    scf_simple_prop_numvalues(temporary_prop)) > 0)
1170Sstevel@tonic-gate 		temporary = scf_simple_prop_next_boolean(temporary_prop);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate out:
1200Sstevel@tonic-gate 	if (!persistent)
1210Sstevel@tonic-gate 		(void) printf(gettext("                                      "
1220Sstevel@tonic-gate 		    "state: unknown"));
1230Sstevel@tonic-gate 	else if (temporary && *temporary != *persistent)
1240Sstevel@tonic-gate 		(void) printf(gettext("                                      "
1250Sstevel@tonic-gate 		    "state: %s (%s at next boot)\n"), *temporary ?
1260Sstevel@tonic-gate 		    gettext("enabled") : gettext("disabled"), *persistent ?
1270Sstevel@tonic-gate 		    gettext("enabled") : gettext("disabled"));
1280Sstevel@tonic-gate 	else
1290Sstevel@tonic-gate 		(void) printf(gettext("                                      "
1300Sstevel@tonic-gate 		    "state: %s\n"), *persistent ? gettext("enabled") :
1310Sstevel@tonic-gate 			gettext("disabled"));
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	scf_simple_prop_free(temporary_prop);
1340Sstevel@tonic-gate 	scf_simple_prop_free(persistent_prop);
1350Sstevel@tonic-gate 	scf_handle_destroy(h);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate int
1390Sstevel@tonic-gate main(int argc, char *argv[])
1400Sstevel@tonic-gate {
141*2517Stn143363 	char *subopts, *optval;
1420Sstevel@tonic-gate 	int modified = 0;
143*2517Stn143363 	int opt;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	(void) setprogname("rcapadm");
1460Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1470Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "DEc:i:n")) != EOF) {
1500Sstevel@tonic-gate 		switch (opt) {
1510Sstevel@tonic-gate 		case 'n':
1520Sstevel@tonic-gate 			no_starting_stopping = 1;
1530Sstevel@tonic-gate 			break;
1540Sstevel@tonic-gate 		case 'c':
1550Sstevel@tonic-gate 			if ((pressure = xatoi(optarg)) < 0 ||
1560Sstevel@tonic-gate 			    pressure > 100 ||
1570Sstevel@tonic-gate 			    errno == EINVAL)
1580Sstevel@tonic-gate 				usage();
1590Sstevel@tonic-gate 			modified++;
1600Sstevel@tonic-gate 			break;
1610Sstevel@tonic-gate 		case 'E':
1620Sstevel@tonic-gate 			enable = 1;
1630Sstevel@tonic-gate 			disable = 0;
1640Sstevel@tonic-gate 			modified++;
1650Sstevel@tonic-gate 			break;
1660Sstevel@tonic-gate 		case 'D':
1670Sstevel@tonic-gate 			disable = 1;
1680Sstevel@tonic-gate 			enable = 0;
1690Sstevel@tonic-gate 			modified++;
1700Sstevel@tonic-gate 			break;
1710Sstevel@tonic-gate 		case 'i':
1720Sstevel@tonic-gate 			subopts = optarg;
1730Sstevel@tonic-gate 			while (*subopts != '\0') {
1740Sstevel@tonic-gate 				switch (getsubopt(&subopts, subopt_v,
1750Sstevel@tonic-gate 				    &optval)) {
1760Sstevel@tonic-gate 					case OPT_SCAN:
1770Sstevel@tonic-gate 						if (optval == NULL ||
1780Sstevel@tonic-gate 						    (scan_interval =
1790Sstevel@tonic-gate 						    xatoi(optval)) <= 0)
1800Sstevel@tonic-gate 							usage();
1810Sstevel@tonic-gate 						break;
1820Sstevel@tonic-gate 					case OPT_SAMPLE:
1830Sstevel@tonic-gate 						if (optval == NULL ||
1840Sstevel@tonic-gate 						    (sample_interval =
1850Sstevel@tonic-gate 						    xatoi(optval)) <= 0)
1860Sstevel@tonic-gate 							usage();
1870Sstevel@tonic-gate 						break;
1880Sstevel@tonic-gate 					case OPT_REPORT:
1890Sstevel@tonic-gate 						if (optval == NULL ||
1900Sstevel@tonic-gate 						    (report_interval =
1910Sstevel@tonic-gate 						    xatoi(optval)) < 0)
1920Sstevel@tonic-gate 							usage();
1930Sstevel@tonic-gate 						break;
1940Sstevel@tonic-gate 					case OPT_CONFIG:
1950Sstevel@tonic-gate 						if (optval == NULL ||
1960Sstevel@tonic-gate 						    (config_interval =
1970Sstevel@tonic-gate 						    xatoi(optval)) < 0)
1980Sstevel@tonic-gate 							usage();
1990Sstevel@tonic-gate 						break;
2000Sstevel@tonic-gate 					default:
2010Sstevel@tonic-gate 						usage();
2020Sstevel@tonic-gate 				}
2030Sstevel@tonic-gate 			}
2040Sstevel@tonic-gate 			modified++;
2050Sstevel@tonic-gate 			break;
2060Sstevel@tonic-gate 		default:
2070Sstevel@tonic-gate 			usage();
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (argc > optind)
2120Sstevel@tonic-gate 		usage();
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if (rcfg_read(fname, -1, &conf, NULL) < 0) {
2150Sstevel@tonic-gate 		if (!(errno == ENOENT && modified)) {
2160Sstevel@tonic-gate 			die(gettext("resource caps not configured\n"));
2170Sstevel@tonic-gate 			return (E_ERROR);
2180Sstevel@tonic-gate 		}
2190Sstevel@tonic-gate 		rcfg_init(&conf);
2200Sstevel@tonic-gate 		conf.rcfg_mode_name = "project";
2210Sstevel@tonic-gate 	} else {
2220Sstevel@tonic-gate 		/*
2230Sstevel@tonic-gate 		 * The configuration file has been read.  Warn that any lnode
2240Sstevel@tonic-gate 		 * (or non-project) mode specification (by an SRM
2250Sstevel@tonic-gate 		 * 1.3 configuration file, for example) is ignored.
2260Sstevel@tonic-gate 		 */
2270Sstevel@tonic-gate 		if (strcmp(conf.rcfg_mode_name, "project") != 0) {
2280Sstevel@tonic-gate 			warn(gettext("%s mode specification ignored -- using"
2290Sstevel@tonic-gate 			    " project mode\n"), conf.rcfg_mode_name);
2300Sstevel@tonic-gate 			conf.rcfg_mode_name = "project";
2310Sstevel@tonic-gate 			conf.rcfg_mode = rctype_project;
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (modified) {
2360Sstevel@tonic-gate 		if (pressure >= 0)
2370Sstevel@tonic-gate 			conf.rcfg_memory_cap_enforcement_pressure = pressure;
2380Sstevel@tonic-gate 		if (config_interval >= 0)
2390Sstevel@tonic-gate 			conf.rcfg_reconfiguration_interval = config_interval;
2400Sstevel@tonic-gate 		if (scan_interval >= 0)
2410Sstevel@tonic-gate 			conf.rcfg_proc_walk_interval = scan_interval;
2420Sstevel@tonic-gate 		if (report_interval >= 0)
2430Sstevel@tonic-gate 			conf.rcfg_report_interval = report_interval;
2440Sstevel@tonic-gate 		if (sample_interval >= 0)
2450Sstevel@tonic-gate 			conf.rcfg_rss_sample_interval = sample_interval;
2460Sstevel@tonic-gate 
247*2517Stn143363 		/*
248*2517Stn143363 		 * Create config file with the new parameter(s). The
249*2517Stn143363 		 * create_config_file will exit if it fails.
250*2517Stn143363 		 */
251*2517Stn143363 		create_config_file(&conf);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		if (enable > 0 && smf_enable_instance(RCAP_FMRI,
2540Sstevel@tonic-gate 		    no_starting_stopping > 0 ? SMF_AT_NEXT_BOOT : 0) != 0)
2550Sstevel@tonic-gate 			die(gettext("cannot enable service: %s\n"),
2560Sstevel@tonic-gate 			    scf_strerror(scf_error()));
2570Sstevel@tonic-gate 		else if (disable > 0 && smf_disable_instance(RCAP_FMRI,
2580Sstevel@tonic-gate 		    no_starting_stopping > 0 ? SMF_AT_NEXT_BOOT : 0) != 0)
2590Sstevel@tonic-gate 			die(gettext("cannot disable service: %s\n"),
2600Sstevel@tonic-gate 			    scf_strerror(scf_error()));
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 		return (E_SUCCESS);
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	/*
2660Sstevel@tonic-gate 	 * Display current configuration
2670Sstevel@tonic-gate 	 */
2680Sstevel@tonic-gate 	print_state();
2690Sstevel@tonic-gate 	(void) printf(gettext("           memory cap enforcement"
2700Sstevel@tonic-gate 	    " threshold: %d%%\n"), conf.rcfg_memory_cap_enforcement_pressure);
2710Sstevel@tonic-gate 	(void) printf(gettext("                    process scan rate"
2720Sstevel@tonic-gate 	    " (sec): %d\n"), conf.rcfg_proc_walk_interval);
2730Sstevel@tonic-gate 	(void) printf(gettext("                 reconfiguration rate"
2740Sstevel@tonic-gate 	    " (sec): %d\n"), conf.rcfg_reconfiguration_interval);
2750Sstevel@tonic-gate 	(void) printf(gettext("                          report rate"
2760Sstevel@tonic-gate 	    " (sec): %d\n"), conf.rcfg_report_interval);
2770Sstevel@tonic-gate 	(void) printf(gettext("                    RSS sampling rate"
2780Sstevel@tonic-gate 	    " (sec): %d\n"), conf.rcfg_rss_sample_interval);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	return (E_SUCCESS);
2810Sstevel@tonic-gate }
282