1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/types.h> 30*0Sstevel@tonic-gate #include <sys/stat.h> 31*0Sstevel@tonic-gate #include <sys/wait.h> 32*0Sstevel@tonic-gate #include <fcntl.h> 33*0Sstevel@tonic-gate #include <errno.h> 34*0Sstevel@tonic-gate #include <signal.h> 35*0Sstevel@tonic-gate #include <stdio.h> 36*0Sstevel@tonic-gate #include <stdlib.h> 37*0Sstevel@tonic-gate #include <strings.h> 38*0Sstevel@tonic-gate #include <unistd.h> 39*0Sstevel@tonic-gate #include <libscf.h> 40*0Sstevel@tonic-gate #include <libscf_priv.h> 41*0Sstevel@tonic-gate #include <libintl.h> 42*0Sstevel@tonic-gate #include <locale.h> 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate #include "utils.h" 45*0Sstevel@tonic-gate #include "rcapd.h" 46*0Sstevel@tonic-gate #include "rcapd_conf.h" 47*0Sstevel@tonic-gate #include "rcapd_stat.h" 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate #define CFG_TEMPLATE_SUFFIX ".XXXXXX" /* suffix of mkstemp() arg */ 50*0Sstevel@tonic-gate #define RCAP_FMRI "system/rcap:default" 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate static void 53*0Sstevel@tonic-gate usage() 54*0Sstevel@tonic-gate { 55*0Sstevel@tonic-gate (void) fprintf(stderr, 56*0Sstevel@tonic-gate gettext("usage: rcapadm\n" 57*0Sstevel@tonic-gate " [-E|-D] " 58*0Sstevel@tonic-gate "# enable/disable rcapd\n" 59*0Sstevel@tonic-gate " [-n] " 60*0Sstevel@tonic-gate "# don't start/stop rcapd\n" 61*0Sstevel@tonic-gate " [-i <scan|sample|report|config>=value] " 62*0Sstevel@tonic-gate "# set intervals\n" 63*0Sstevel@tonic-gate " [-c <percent>] " 64*0Sstevel@tonic-gate "# set memory cap\n" 65*0Sstevel@tonic-gate " " 66*0Sstevel@tonic-gate "# enforcement threshold\n")); 67*0Sstevel@tonic-gate exit(E_USAGE); 68*0Sstevel@tonic-gate } 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate static rcfg_t conf; 71*0Sstevel@tonic-gate static int enable = -1; 72*0Sstevel@tonic-gate static int disable = -1; 73*0Sstevel@tonic-gate static int pressure = -1; 74*0Sstevel@tonic-gate static int no_starting_stopping = -1; 75*0Sstevel@tonic-gate static int scan_interval = -1; 76*0Sstevel@tonic-gate static int report_interval = -1; 77*0Sstevel@tonic-gate static int config_interval = -1; 78*0Sstevel@tonic-gate static int sample_interval = -1; 79*0Sstevel@tonic-gate static char *fname = RCAPD_DEFAULT_CONF_FILE; 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate static char *subopt_v[] = { 82*0Sstevel@tonic-gate "scan", 83*0Sstevel@tonic-gate "sample", 84*0Sstevel@tonic-gate "report", 85*0Sstevel@tonic-gate "config", 86*0Sstevel@tonic-gate NULL 87*0Sstevel@tonic-gate }; 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate typedef enum { 90*0Sstevel@tonic-gate OPT_SCAN = 0, 91*0Sstevel@tonic-gate OPT_SAMPLE, 92*0Sstevel@tonic-gate OPT_REPORT, 93*0Sstevel@tonic-gate OPT_CONFIG 94*0Sstevel@tonic-gate } subopt_idx_t; 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate static void 97*0Sstevel@tonic-gate print_state(void) 98*0Sstevel@tonic-gate { 99*0Sstevel@tonic-gate scf_simple_prop_t *persistent_prop = NULL; 100*0Sstevel@tonic-gate scf_simple_prop_t *temporary_prop = NULL; 101*0Sstevel@tonic-gate uint8_t *persistent = NULL; 102*0Sstevel@tonic-gate uint8_t *temporary = NULL; 103*0Sstevel@tonic-gate scf_handle_t *h; 104*0Sstevel@tonic-gate /* LINTED: conditionally assigned and used in function */ 105*0Sstevel@tonic-gate ssize_t numvals; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate if ((h = scf_handle_create(SCF_VERSION)) == NULL || 108*0Sstevel@tonic-gate scf_handle_bind(h) != 0) 109*0Sstevel@tonic-gate goto out; 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate if ((persistent_prop = scf_simple_prop_get(h, RCAP_FMRI, 112*0Sstevel@tonic-gate SCF_PG_GENERAL, SCF_PROPERTY_ENABLED)) != NULL && (numvals = 113*0Sstevel@tonic-gate scf_simple_prop_numvalues(persistent_prop)) > 0) 114*0Sstevel@tonic-gate persistent = scf_simple_prop_next_boolean(persistent_prop); 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate if ((temporary_prop = scf_simple_prop_get(h, RCAP_FMRI, 117*0Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED)) != NULL && (numvals = 118*0Sstevel@tonic-gate scf_simple_prop_numvalues(temporary_prop)) > 0) 119*0Sstevel@tonic-gate temporary = scf_simple_prop_next_boolean(temporary_prop); 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate out: 122*0Sstevel@tonic-gate if (!persistent) 123*0Sstevel@tonic-gate (void) printf(gettext(" " 124*0Sstevel@tonic-gate "state: unknown")); 125*0Sstevel@tonic-gate else if (temporary && *temporary != *persistent) 126*0Sstevel@tonic-gate (void) printf(gettext(" " 127*0Sstevel@tonic-gate "state: %s (%s at next boot)\n"), *temporary ? 128*0Sstevel@tonic-gate gettext("enabled") : gettext("disabled"), *persistent ? 129*0Sstevel@tonic-gate gettext("enabled") : gettext("disabled")); 130*0Sstevel@tonic-gate else 131*0Sstevel@tonic-gate (void) printf(gettext(" " 132*0Sstevel@tonic-gate "state: %s\n"), *persistent ? gettext("enabled") : 133*0Sstevel@tonic-gate gettext("disabled")); 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate scf_simple_prop_free(temporary_prop); 136*0Sstevel@tonic-gate scf_simple_prop_free(persistent_prop); 137*0Sstevel@tonic-gate scf_handle_destroy(h); 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate int 141*0Sstevel@tonic-gate main(int argc, char *argv[]) 142*0Sstevel@tonic-gate { 143*0Sstevel@tonic-gate char *subopts, *optval, *template; 144*0Sstevel@tonic-gate int modified = 0; 145*0Sstevel@tonic-gate FILE *fp; 146*0Sstevel@tonic-gate int fd, olderrno, opt; 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate (void) setprogname("rcapadm"); 149*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 150*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate while ((opt = getopt(argc, argv, "DEc:i:n")) != EOF) { 153*0Sstevel@tonic-gate switch (opt) { 154*0Sstevel@tonic-gate case 'n': 155*0Sstevel@tonic-gate no_starting_stopping = 1; 156*0Sstevel@tonic-gate break; 157*0Sstevel@tonic-gate case 'c': 158*0Sstevel@tonic-gate if ((pressure = xatoi(optarg)) < 0 || 159*0Sstevel@tonic-gate pressure > 100 || 160*0Sstevel@tonic-gate errno == EINVAL) 161*0Sstevel@tonic-gate usage(); 162*0Sstevel@tonic-gate modified++; 163*0Sstevel@tonic-gate break; 164*0Sstevel@tonic-gate case 'E': 165*0Sstevel@tonic-gate enable = 1; 166*0Sstevel@tonic-gate disable = 0; 167*0Sstevel@tonic-gate modified++; 168*0Sstevel@tonic-gate break; 169*0Sstevel@tonic-gate case 'D': 170*0Sstevel@tonic-gate disable = 1; 171*0Sstevel@tonic-gate enable = 0; 172*0Sstevel@tonic-gate modified++; 173*0Sstevel@tonic-gate break; 174*0Sstevel@tonic-gate case 'i': 175*0Sstevel@tonic-gate subopts = optarg; 176*0Sstevel@tonic-gate while (*subopts != '\0') { 177*0Sstevel@tonic-gate switch (getsubopt(&subopts, subopt_v, 178*0Sstevel@tonic-gate &optval)) { 179*0Sstevel@tonic-gate case OPT_SCAN: 180*0Sstevel@tonic-gate if (optval == NULL || 181*0Sstevel@tonic-gate (scan_interval = 182*0Sstevel@tonic-gate xatoi(optval)) <= 0) 183*0Sstevel@tonic-gate usage(); 184*0Sstevel@tonic-gate break; 185*0Sstevel@tonic-gate case OPT_SAMPLE: 186*0Sstevel@tonic-gate if (optval == NULL || 187*0Sstevel@tonic-gate (sample_interval = 188*0Sstevel@tonic-gate xatoi(optval)) <= 0) 189*0Sstevel@tonic-gate usage(); 190*0Sstevel@tonic-gate break; 191*0Sstevel@tonic-gate case OPT_REPORT: 192*0Sstevel@tonic-gate if (optval == NULL || 193*0Sstevel@tonic-gate (report_interval = 194*0Sstevel@tonic-gate xatoi(optval)) < 0) 195*0Sstevel@tonic-gate usage(); 196*0Sstevel@tonic-gate break; 197*0Sstevel@tonic-gate case OPT_CONFIG: 198*0Sstevel@tonic-gate if (optval == NULL || 199*0Sstevel@tonic-gate (config_interval = 200*0Sstevel@tonic-gate xatoi(optval)) < 0) 201*0Sstevel@tonic-gate usage(); 202*0Sstevel@tonic-gate break; 203*0Sstevel@tonic-gate default: 204*0Sstevel@tonic-gate usage(); 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate modified++; 208*0Sstevel@tonic-gate break; 209*0Sstevel@tonic-gate default: 210*0Sstevel@tonic-gate usage(); 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate if (argc > optind) 215*0Sstevel@tonic-gate usage(); 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate if (rcfg_read(fname, -1, &conf, NULL) < 0) { 218*0Sstevel@tonic-gate if (!(errno == ENOENT && modified)) { 219*0Sstevel@tonic-gate die(gettext("resource caps not configured\n")); 220*0Sstevel@tonic-gate return (E_ERROR); 221*0Sstevel@tonic-gate } 222*0Sstevel@tonic-gate rcfg_init(&conf); 223*0Sstevel@tonic-gate conf.rcfg_mode_name = "project"; 224*0Sstevel@tonic-gate conf.rcfg_reconfiguration_interval = 60; 225*0Sstevel@tonic-gate conf.rcfg_proc_walk_interval = 15; 226*0Sstevel@tonic-gate conf.rcfg_report_interval = 5; 227*0Sstevel@tonic-gate conf.rcfg_rss_sample_interval = 5; 228*0Sstevel@tonic-gate } else { 229*0Sstevel@tonic-gate /* 230*0Sstevel@tonic-gate * The configuration file has been read. Warn that any lnode 231*0Sstevel@tonic-gate * (or non-project) mode specification (by an SRM 232*0Sstevel@tonic-gate * 1.3 configuration file, for example) is ignored. 233*0Sstevel@tonic-gate */ 234*0Sstevel@tonic-gate if (strcmp(conf.rcfg_mode_name, "project") != 0) { 235*0Sstevel@tonic-gate warn(gettext("%s mode specification ignored -- using" 236*0Sstevel@tonic-gate " project mode\n"), conf.rcfg_mode_name); 237*0Sstevel@tonic-gate conf.rcfg_mode_name = "project"; 238*0Sstevel@tonic-gate conf.rcfg_mode = rctype_project; 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate } 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate if (modified) { 243*0Sstevel@tonic-gate if (pressure >= 0) 244*0Sstevel@tonic-gate conf.rcfg_memory_cap_enforcement_pressure = pressure; 245*0Sstevel@tonic-gate if (config_interval >= 0) 246*0Sstevel@tonic-gate conf.rcfg_reconfiguration_interval = config_interval; 247*0Sstevel@tonic-gate if (scan_interval >= 0) 248*0Sstevel@tonic-gate conf.rcfg_proc_walk_interval = scan_interval; 249*0Sstevel@tonic-gate if (report_interval >= 0) 250*0Sstevel@tonic-gate conf.rcfg_report_interval = report_interval; 251*0Sstevel@tonic-gate if (sample_interval >= 0) 252*0Sstevel@tonic-gate conf.rcfg_rss_sample_interval = sample_interval; 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate if ((template = malloc(strlen(fname) + 255*0Sstevel@tonic-gate strlen(CFG_TEMPLATE_SUFFIX) + 1)) == NULL) 256*0Sstevel@tonic-gate die(gettext("memory allocation failure")); 257*0Sstevel@tonic-gate (void) strcpy(template, fname); 258*0Sstevel@tonic-gate (void) strcpy(template + strlen(template), CFG_TEMPLATE_SUFFIX); 259*0Sstevel@tonic-gate if ((fd = mkstemp(template)) < 0) 260*0Sstevel@tonic-gate die("%s", template); 261*0Sstevel@tonic-gate if ((fp = fdopen(fd, "w")) == NULL) { 262*0Sstevel@tonic-gate olderrno = errno; 263*0Sstevel@tonic-gate (void) close(fd); 264*0Sstevel@tonic-gate (void) unlink(template); 265*0Sstevel@tonic-gate errno = olderrno; 266*0Sstevel@tonic-gate die("%s", template); 267*0Sstevel@tonic-gate return (E_ERROR); 268*0Sstevel@tonic-gate } 269*0Sstevel@tonic-gate (void) fputs("#\n# rcap.conf\n#\n" 270*0Sstevel@tonic-gate "# Configuration parameters for resource capping daemon.\n" 271*0Sstevel@tonic-gate "# Do NOT edit by hand -- use rcapadm(1m) instead.\n" 272*0Sstevel@tonic-gate "#\n", fp); 273*0Sstevel@tonic-gate (void) fprintf(fp, "RCAPD_MEMORY_CAP_ENFORCEMENT_PRESSURE " 274*0Sstevel@tonic-gate "= %d\n", conf.rcfg_memory_cap_enforcement_pressure); 275*0Sstevel@tonic-gate (void) fprintf(fp, "RCAPD_RECONFIGURATION_INTERVAL " 276*0Sstevel@tonic-gate "= %d\n", conf.rcfg_reconfiguration_interval); 277*0Sstevel@tonic-gate (void) fprintf(fp, "RCAPD_PROC_WALK_INTERVAL " 278*0Sstevel@tonic-gate "= %d\n", conf.rcfg_proc_walk_interval); 279*0Sstevel@tonic-gate (void) fprintf(fp, "RCAPD_REPORT_INTERVAL " 280*0Sstevel@tonic-gate "= %d\n", conf.rcfg_report_interval); 281*0Sstevel@tonic-gate (void) fprintf(fp, "RCAPD_RSS_SAMPLE_INTERVAL " 282*0Sstevel@tonic-gate "= %d\n", conf.rcfg_rss_sample_interval); 283*0Sstevel@tonic-gate if (fchmod(fd, 0644) != 0) { 284*0Sstevel@tonic-gate olderrno = errno; 285*0Sstevel@tonic-gate (void) close(fd); 286*0Sstevel@tonic-gate (void) fclose(fp); 287*0Sstevel@tonic-gate (void) unlink(template); 288*0Sstevel@tonic-gate errno = olderrno; 289*0Sstevel@tonic-gate die("%s", template); 290*0Sstevel@tonic-gate } 291*0Sstevel@tonic-gate if (rename(template, fname) != 0) { 292*0Sstevel@tonic-gate olderrno = errno; 293*0Sstevel@tonic-gate (void) close(fd); 294*0Sstevel@tonic-gate (void) fclose(fp); 295*0Sstevel@tonic-gate (void) unlink(template); 296*0Sstevel@tonic-gate errno = olderrno; 297*0Sstevel@tonic-gate die(gettext("cannot rename temporary file to %s"), 298*0Sstevel@tonic-gate fname); 299*0Sstevel@tonic-gate } 300*0Sstevel@tonic-gate (void) fclose(fp); 301*0Sstevel@tonic-gate (void) close(fd); 302*0Sstevel@tonic-gate free(template); 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gate if (enable > 0 && smf_enable_instance(RCAP_FMRI, 305*0Sstevel@tonic-gate no_starting_stopping > 0 ? SMF_AT_NEXT_BOOT : 0) != 0) 306*0Sstevel@tonic-gate die(gettext("cannot enable service: %s\n"), 307*0Sstevel@tonic-gate scf_strerror(scf_error())); 308*0Sstevel@tonic-gate else if (disable > 0 && smf_disable_instance(RCAP_FMRI, 309*0Sstevel@tonic-gate no_starting_stopping > 0 ? SMF_AT_NEXT_BOOT : 0) != 0) 310*0Sstevel@tonic-gate die(gettext("cannot disable service: %s\n"), 311*0Sstevel@tonic-gate scf_strerror(scf_error())); 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate return (E_SUCCESS); 314*0Sstevel@tonic-gate } 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gate /* 317*0Sstevel@tonic-gate * Display current configuration 318*0Sstevel@tonic-gate */ 319*0Sstevel@tonic-gate print_state(); 320*0Sstevel@tonic-gate (void) printf(gettext(" memory cap enforcement" 321*0Sstevel@tonic-gate " threshold: %d%%\n"), conf.rcfg_memory_cap_enforcement_pressure); 322*0Sstevel@tonic-gate (void) printf(gettext(" process scan rate" 323*0Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_proc_walk_interval); 324*0Sstevel@tonic-gate (void) printf(gettext(" reconfiguration rate" 325*0Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_reconfiguration_interval); 326*0Sstevel@tonic-gate (void) printf(gettext(" report rate" 327*0Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_report_interval); 328*0Sstevel@tonic-gate (void) printf(gettext(" RSS sampling rate" 329*0Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_rss_sample_interval); 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gate return (E_SUCCESS); 332*0Sstevel@tonic-gate } 333