xref: /titanic_53/usr/src/cmd/rcap/rcapadm/rcapadm.c (revision f1710550bd8341486e7494e781335ba875c9b12c)
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
5*f1710550Stn143363  * Common Development and Distribution License (the "License").
6*f1710550Stn143363  * 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*f1710550Stn143363  * Copyright 2006 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>
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 #define	RCAP_FMRI		"system/rcap:default"
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 	    "                                                      "
647c478bd9Sstevel@tonic-gate 	    "# enforcement threshold\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 static char *fname = RCAPD_DEFAULT_CONF_FILE;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static char *subopt_v[] = {
807c478bd9Sstevel@tonic-gate 	"scan",
817c478bd9Sstevel@tonic-gate 	"sample",
827c478bd9Sstevel@tonic-gate 	"report",
837c478bd9Sstevel@tonic-gate 	"config",
847c478bd9Sstevel@tonic-gate 	NULL
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate typedef enum {
887c478bd9Sstevel@tonic-gate 	OPT_SCAN = 0,
897c478bd9Sstevel@tonic-gate 	OPT_SAMPLE,
907c478bd9Sstevel@tonic-gate 	OPT_REPORT,
917c478bd9Sstevel@tonic-gate 	OPT_CONFIG
927c478bd9Sstevel@tonic-gate } subopt_idx_t;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static void
957c478bd9Sstevel@tonic-gate print_state(void)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	scf_simple_prop_t *persistent_prop = NULL;
987c478bd9Sstevel@tonic-gate 	scf_simple_prop_t *temporary_prop = NULL;
997c478bd9Sstevel@tonic-gate 	uint8_t *persistent = NULL;
1007c478bd9Sstevel@tonic-gate 	uint8_t *temporary = NULL;
1017c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
1027c478bd9Sstevel@tonic-gate 	/* LINTED: conditionally assigned and used in function */
1037c478bd9Sstevel@tonic-gate 	ssize_t numvals;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if ((h = scf_handle_create(SCF_VERSION)) == NULL ||
1067c478bd9Sstevel@tonic-gate 	    scf_handle_bind(h) != 0)
1077c478bd9Sstevel@tonic-gate 		goto out;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if ((persistent_prop = scf_simple_prop_get(h, RCAP_FMRI,
1107c478bd9Sstevel@tonic-gate 	    SCF_PG_GENERAL, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
1117c478bd9Sstevel@tonic-gate 	    scf_simple_prop_numvalues(persistent_prop)) > 0)
1127c478bd9Sstevel@tonic-gate 		persistent = scf_simple_prop_next_boolean(persistent_prop);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if ((temporary_prop = scf_simple_prop_get(h, RCAP_FMRI,
1157c478bd9Sstevel@tonic-gate 	    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
1167c478bd9Sstevel@tonic-gate 	    scf_simple_prop_numvalues(temporary_prop)) > 0)
1177c478bd9Sstevel@tonic-gate 		temporary = scf_simple_prop_next_boolean(temporary_prop);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate out:
1207c478bd9Sstevel@tonic-gate 	if (!persistent)
1217c478bd9Sstevel@tonic-gate 		(void) printf(gettext("                                      "
1227c478bd9Sstevel@tonic-gate 		    "state: unknown"));
1237c478bd9Sstevel@tonic-gate 	else if (temporary && *temporary != *persistent)
1247c478bd9Sstevel@tonic-gate 		(void) printf(gettext("                                      "
1257c478bd9Sstevel@tonic-gate 		    "state: %s (%s at next boot)\n"), *temporary ?
1267c478bd9Sstevel@tonic-gate 		    gettext("enabled") : gettext("disabled"), *persistent ?
1277c478bd9Sstevel@tonic-gate 		    gettext("enabled") : gettext("disabled"));
1287c478bd9Sstevel@tonic-gate 	else
1297c478bd9Sstevel@tonic-gate 		(void) printf(gettext("                                      "
1307c478bd9Sstevel@tonic-gate 		    "state: %s\n"), *persistent ? gettext("enabled") :
1317c478bd9Sstevel@tonic-gate 			gettext("disabled"));
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(temporary_prop);
1347c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(persistent_prop);
1357c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate int
1397c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1407c478bd9Sstevel@tonic-gate {
141*f1710550Stn143363 	char *subopts, *optval;
1427c478bd9Sstevel@tonic-gate 	int modified = 0;
143*f1710550Stn143363 	int opt;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	(void) setprogname("rcapadm");
1467c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1477c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "DEc:i:n")) != EOF) {
1507c478bd9Sstevel@tonic-gate 		switch (opt) {
1517c478bd9Sstevel@tonic-gate 		case 'n':
1527c478bd9Sstevel@tonic-gate 			no_starting_stopping = 1;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		case 'c':
1557c478bd9Sstevel@tonic-gate 			if ((pressure = xatoi(optarg)) < 0 ||
1567c478bd9Sstevel@tonic-gate 			    pressure > 100 ||
1577c478bd9Sstevel@tonic-gate 			    errno == EINVAL)
1587c478bd9Sstevel@tonic-gate 				usage();
1597c478bd9Sstevel@tonic-gate 			modified++;
1607c478bd9Sstevel@tonic-gate 			break;
1617c478bd9Sstevel@tonic-gate 		case 'E':
1627c478bd9Sstevel@tonic-gate 			enable = 1;
1637c478bd9Sstevel@tonic-gate 			disable = 0;
1647c478bd9Sstevel@tonic-gate 			modified++;
1657c478bd9Sstevel@tonic-gate 			break;
1667c478bd9Sstevel@tonic-gate 		case 'D':
1677c478bd9Sstevel@tonic-gate 			disable = 1;
1687c478bd9Sstevel@tonic-gate 			enable = 0;
1697c478bd9Sstevel@tonic-gate 			modified++;
1707c478bd9Sstevel@tonic-gate 			break;
1717c478bd9Sstevel@tonic-gate 		case 'i':
1727c478bd9Sstevel@tonic-gate 			subopts = optarg;
1737c478bd9Sstevel@tonic-gate 			while (*subopts != '\0') {
1747c478bd9Sstevel@tonic-gate 				switch (getsubopt(&subopts, subopt_v,
1757c478bd9Sstevel@tonic-gate 				    &optval)) {
1767c478bd9Sstevel@tonic-gate 					case OPT_SCAN:
1777c478bd9Sstevel@tonic-gate 						if (optval == NULL ||
1787c478bd9Sstevel@tonic-gate 						    (scan_interval =
1797c478bd9Sstevel@tonic-gate 						    xatoi(optval)) <= 0)
1807c478bd9Sstevel@tonic-gate 							usage();
1817c478bd9Sstevel@tonic-gate 						break;
1827c478bd9Sstevel@tonic-gate 					case OPT_SAMPLE:
1837c478bd9Sstevel@tonic-gate 						if (optval == NULL ||
1847c478bd9Sstevel@tonic-gate 						    (sample_interval =
1857c478bd9Sstevel@tonic-gate 						    xatoi(optval)) <= 0)
1867c478bd9Sstevel@tonic-gate 							usage();
1877c478bd9Sstevel@tonic-gate 						break;
1887c478bd9Sstevel@tonic-gate 					case OPT_REPORT:
1897c478bd9Sstevel@tonic-gate 						if (optval == NULL ||
1907c478bd9Sstevel@tonic-gate 						    (report_interval =
1917c478bd9Sstevel@tonic-gate 						    xatoi(optval)) < 0)
1927c478bd9Sstevel@tonic-gate 							usage();
1937c478bd9Sstevel@tonic-gate 						break;
1947c478bd9Sstevel@tonic-gate 					case OPT_CONFIG:
1957c478bd9Sstevel@tonic-gate 						if (optval == NULL ||
1967c478bd9Sstevel@tonic-gate 						    (config_interval =
1977c478bd9Sstevel@tonic-gate 						    xatoi(optval)) < 0)
1987c478bd9Sstevel@tonic-gate 							usage();
1997c478bd9Sstevel@tonic-gate 						break;
2007c478bd9Sstevel@tonic-gate 					default:
2017c478bd9Sstevel@tonic-gate 						usage();
2027c478bd9Sstevel@tonic-gate 				}
2037c478bd9Sstevel@tonic-gate 			}
2047c478bd9Sstevel@tonic-gate 			modified++;
2057c478bd9Sstevel@tonic-gate 			break;
2067c478bd9Sstevel@tonic-gate 		default:
2077c478bd9Sstevel@tonic-gate 			usage();
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (argc > optind)
2127c478bd9Sstevel@tonic-gate 		usage();
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (rcfg_read(fname, -1, &conf, NULL) < 0) {
2157c478bd9Sstevel@tonic-gate 		if (!(errno == ENOENT && modified)) {
2167c478bd9Sstevel@tonic-gate 			die(gettext("resource caps not configured\n"));
2177c478bd9Sstevel@tonic-gate 			return (E_ERROR);
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 		rcfg_init(&conf);
2207c478bd9Sstevel@tonic-gate 		conf.rcfg_mode_name = "project";
2217c478bd9Sstevel@tonic-gate 	} else {
2227c478bd9Sstevel@tonic-gate 		/*
2237c478bd9Sstevel@tonic-gate 		 * The configuration file has been read.  Warn that any lnode
2247c478bd9Sstevel@tonic-gate 		 * (or non-project) mode specification (by an SRM
2257c478bd9Sstevel@tonic-gate 		 * 1.3 configuration file, for example) is ignored.
2267c478bd9Sstevel@tonic-gate 		 */
2277c478bd9Sstevel@tonic-gate 		if (strcmp(conf.rcfg_mode_name, "project") != 0) {
2287c478bd9Sstevel@tonic-gate 			warn(gettext("%s mode specification ignored -- using"
2297c478bd9Sstevel@tonic-gate 			    " project mode\n"), conf.rcfg_mode_name);
2307c478bd9Sstevel@tonic-gate 			conf.rcfg_mode_name = "project";
2317c478bd9Sstevel@tonic-gate 			conf.rcfg_mode = rctype_project;
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (modified) {
2367c478bd9Sstevel@tonic-gate 		if (pressure >= 0)
2377c478bd9Sstevel@tonic-gate 			conf.rcfg_memory_cap_enforcement_pressure = pressure;
2387c478bd9Sstevel@tonic-gate 		if (config_interval >= 0)
2397c478bd9Sstevel@tonic-gate 			conf.rcfg_reconfiguration_interval = config_interval;
2407c478bd9Sstevel@tonic-gate 		if (scan_interval >= 0)
2417c478bd9Sstevel@tonic-gate 			conf.rcfg_proc_walk_interval = scan_interval;
2427c478bd9Sstevel@tonic-gate 		if (report_interval >= 0)
2437c478bd9Sstevel@tonic-gate 			conf.rcfg_report_interval = report_interval;
2447c478bd9Sstevel@tonic-gate 		if (sample_interval >= 0)
2457c478bd9Sstevel@tonic-gate 			conf.rcfg_rss_sample_interval = sample_interval;
2467c478bd9Sstevel@tonic-gate 
247*f1710550Stn143363 		/*
248*f1710550Stn143363 		 * Create config file with the new parameter(s). The
249*f1710550Stn143363 		 * create_config_file will exit if it fails.
250*f1710550Stn143363 		 */
251*f1710550Stn143363 		create_config_file(&conf);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		if (enable > 0 && smf_enable_instance(RCAP_FMRI,
2547c478bd9Sstevel@tonic-gate 		    no_starting_stopping > 0 ? SMF_AT_NEXT_BOOT : 0) != 0)
2557c478bd9Sstevel@tonic-gate 			die(gettext("cannot enable service: %s\n"),
2567c478bd9Sstevel@tonic-gate 			    scf_strerror(scf_error()));
2577c478bd9Sstevel@tonic-gate 		else if (disable > 0 && smf_disable_instance(RCAP_FMRI,
2587c478bd9Sstevel@tonic-gate 		    no_starting_stopping > 0 ? SMF_AT_NEXT_BOOT : 0) != 0)
2597c478bd9Sstevel@tonic-gate 			die(gettext("cannot disable service: %s\n"),
2607c478bd9Sstevel@tonic-gate 			    scf_strerror(scf_error()));
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		return (E_SUCCESS);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * Display current configuration
2677c478bd9Sstevel@tonic-gate 	 */
2687c478bd9Sstevel@tonic-gate 	print_state();
2697c478bd9Sstevel@tonic-gate 	(void) printf(gettext("           memory cap enforcement"
2707c478bd9Sstevel@tonic-gate 	    " threshold: %d%%\n"), conf.rcfg_memory_cap_enforcement_pressure);
2717c478bd9Sstevel@tonic-gate 	(void) printf(gettext("                    process scan rate"
2727c478bd9Sstevel@tonic-gate 	    " (sec): %d\n"), conf.rcfg_proc_walk_interval);
2737c478bd9Sstevel@tonic-gate 	(void) printf(gettext("                 reconfiguration rate"
2747c478bd9Sstevel@tonic-gate 	    " (sec): %d\n"), conf.rcfg_reconfiguration_interval);
2757c478bd9Sstevel@tonic-gate 	(void) printf(gettext("                          report rate"
2767c478bd9Sstevel@tonic-gate 	    " (sec): %d\n"), conf.rcfg_report_interval);
2777c478bd9Sstevel@tonic-gate 	(void) printf(gettext("                    RSS sampling rate"
2787c478bd9Sstevel@tonic-gate 	    " (sec): %d\n"), conf.rcfg_rss_sample_interval);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (E_SUCCESS);
2817c478bd9Sstevel@tonic-gate }
282