1*d75e6a5dStn143363 /* 2*d75e6a5dStn143363 * CDDL HEADER START 3*d75e6a5dStn143363 * 4*d75e6a5dStn143363 * The contents of this file are subject to the terms of the 5*d75e6a5dStn143363 * Common Development and Distribution License (the "License"). 6*d75e6a5dStn143363 * You may not use this file except in compliance with the License. 7*d75e6a5dStn143363 * 8*d75e6a5dStn143363 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*d75e6a5dStn143363 * or http://www.opensolaris.org/os/licensing. 10*d75e6a5dStn143363 * See the License for the specific language governing permissions 11*d75e6a5dStn143363 * and limitations under the License. 12*d75e6a5dStn143363 * 13*d75e6a5dStn143363 * When distributing Covered Code, include this CDDL HEADER in each 14*d75e6a5dStn143363 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*d75e6a5dStn143363 * If applicable, add the following below this CDDL HEADER, with the 16*d75e6a5dStn143363 * fields enclosed by brackets "[]" replaced with your own identifying 17*d75e6a5dStn143363 * information: Portions Copyright [yyyy] [name of copyright owner] 18*d75e6a5dStn143363 * 19*d75e6a5dStn143363 * CDDL HEADER END 20*d75e6a5dStn143363 * 21*d75e6a5dStn143363 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 22*d75e6a5dStn143363 * Use is subject to license terms. 23*d75e6a5dStn143363 */ 24*d75e6a5dStn143363 25*d75e6a5dStn143363 #pragma ident "%Z%%M% %I% %E% SMI" 26*d75e6a5dStn143363 27*d75e6a5dStn143363 #include <sys/types.h> 28*d75e6a5dStn143363 #include <sys/stat.h> 29*d75e6a5dStn143363 #include <stdio.h> 30*d75e6a5dStn143363 #include <stdlib.h> 31*d75e6a5dStn143363 #include <errno.h> 32*d75e6a5dStn143363 #include <strings.h> 33*d75e6a5dStn143363 #include <fcntl.h> 34*d75e6a5dStn143363 #include <unistd.h> 35*d75e6a5dStn143363 #include <libscf.h> 36*d75e6a5dStn143363 #include <libscf_priv.h> 37*d75e6a5dStn143363 #include <libuutil.h> 38*d75e6a5dStn143363 #include "rcapd.h" 39*d75e6a5dStn143363 #include "rcapd_conf.h" 40*d75e6a5dStn143363 #include "rcapd_stat.h" 41*d75e6a5dStn143363 #include "utils.h" 42*d75e6a5dStn143363 43*d75e6a5dStn143363 /* 44*d75e6a5dStn143363 * Read configuration and set the fields of an rcfg_t correspondingly. 45*d75e6a5dStn143363 * Verify that the statistics file is writable, with the optional 46*d75e6a5dStn143363 * verify_stat_file_creation() callback. 47*d75e6a5dStn143363 */ 48*d75e6a5dStn143363 int 49*d75e6a5dStn143363 rcfg_read(rcfg_t *_rcfg, int(*verify_stat_file_creation)(void)) 50*d75e6a5dStn143363 { 51*d75e6a5dStn143363 scf_simple_handle_t *simple_h; 52*d75e6a5dStn143363 uint64_t count_val; 53*d75e6a5dStn143363 int ret = E_ERROR; 54*d75e6a5dStn143363 55*d75e6a5dStn143363 rcfg_init(_rcfg); 56*d75e6a5dStn143363 57*d75e6a5dStn143363 if ((simple_h = scf_general_pg_setup(RCAP_FMRI, CONFIG_PG)) 58*d75e6a5dStn143363 == NULL) { 59*d75e6a5dStn143363 warn(gettext("SMF initialization problem: %s\n"), 60*d75e6a5dStn143363 scf_strerror(scf_error())); 61*d75e6a5dStn143363 goto err; 62*d75e6a5dStn143363 } 63*d75e6a5dStn143363 64*d75e6a5dStn143363 if (scf_read_count_property(simple_h, PRESSURE, &count_val) 65*d75e6a5dStn143363 == SCF_FAILED) { 66*d75e6a5dStn143363 warn(gettext("Configuration property '%s' " 67*d75e6a5dStn143363 "not found. \n"), PRESSURE); 68*d75e6a5dStn143363 goto err; 69*d75e6a5dStn143363 } else { 70*d75e6a5dStn143363 if (count_val > 100) 71*d75e6a5dStn143363 _rcfg->rcfg_memory_cap_enforcement_pressure = 100; 72*d75e6a5dStn143363 else 73*d75e6a5dStn143363 _rcfg->rcfg_memory_cap_enforcement_pressure 74*d75e6a5dStn143363 = count_val; 75*d75e6a5dStn143363 76*d75e6a5dStn143363 debug("cap max pressure: %d%%\n", 77*d75e6a5dStn143363 _rcfg->rcfg_memory_cap_enforcement_pressure); 78*d75e6a5dStn143363 } 79*d75e6a5dStn143363 80*d75e6a5dStn143363 if (scf_read_count_property(simple_h, RECONFIG_INT, &count_val) 81*d75e6a5dStn143363 == SCF_FAILED) { 82*d75e6a5dStn143363 warn(gettext("Configuration property '%s' " 83*d75e6a5dStn143363 "not found. \n"), RECONFIG_INT); 84*d75e6a5dStn143363 goto err; 85*d75e6a5dStn143363 } else { 86*d75e6a5dStn143363 _rcfg->rcfg_reconfiguration_interval = count_val; 87*d75e6a5dStn143363 debug("reconfiguration interval: %d seconds\n", 88*d75e6a5dStn143363 _rcfg->rcfg_reconfiguration_interval); 89*d75e6a5dStn143363 } 90*d75e6a5dStn143363 91*d75e6a5dStn143363 if (scf_read_count_property(simple_h, REPORT_INT, &count_val) 92*d75e6a5dStn143363 == SCF_FAILED) { 93*d75e6a5dStn143363 warn(gettext("Configuration property '%s' " 94*d75e6a5dStn143363 "not found. \n"), REPORT_INT); 95*d75e6a5dStn143363 goto err; 96*d75e6a5dStn143363 } else { 97*d75e6a5dStn143363 _rcfg->rcfg_report_interval = count_val; 98*d75e6a5dStn143363 debug("report interval: %d seconds\n", 99*d75e6a5dStn143363 _rcfg->rcfg_report_interval); 100*d75e6a5dStn143363 } 101*d75e6a5dStn143363 102*d75e6a5dStn143363 if (scf_read_count_property(simple_h, RSS_SAMPLE_INT, &count_val) 103*d75e6a5dStn143363 == SCF_FAILED) { 104*d75e6a5dStn143363 warn(gettext("Configuration property '%s' " 105*d75e6a5dStn143363 "not found. \n"), RSS_SAMPLE_INT); 106*d75e6a5dStn143363 goto err; 107*d75e6a5dStn143363 } else { 108*d75e6a5dStn143363 _rcfg->rcfg_rss_sample_interval = count_val; 109*d75e6a5dStn143363 debug("RSS sample interval: %d seconds\n", 110*d75e6a5dStn143363 _rcfg->rcfg_rss_sample_interval); 111*d75e6a5dStn143363 } 112*d75e6a5dStn143363 113*d75e6a5dStn143363 if (scf_read_count_property(simple_h, WALK_INT, &count_val) 114*d75e6a5dStn143363 == SCF_FAILED) { 115*d75e6a5dStn143363 warn(gettext("Configuration property '%s' " 116*d75e6a5dStn143363 "not found. \n"), WALK_INT); 117*d75e6a5dStn143363 goto err; 118*d75e6a5dStn143363 } else { 119*d75e6a5dStn143363 _rcfg->rcfg_proc_walk_interval = count_val; 120*d75e6a5dStn143363 debug("proc_walk interval: %d seconds\n", 121*d75e6a5dStn143363 _rcfg->rcfg_proc_walk_interval); 122*d75e6a5dStn143363 } 123*d75e6a5dStn143363 124*d75e6a5dStn143363 if (_rcfg->rcfg_mode_name == NULL) { 125*d75e6a5dStn143363 /* 126*d75e6a5dStn143363 * Set project mode, by default. 127*d75e6a5dStn143363 */ 128*d75e6a5dStn143363 _rcfg->rcfg_mode = rctype_project; 129*d75e6a5dStn143363 _rcfg->rcfg_mode_name = "project"; 130*d75e6a5dStn143363 debug("mode: %s\n", _rcfg->rcfg_mode_name); 131*d75e6a5dStn143363 } 132*d75e6a5dStn143363 133*d75e6a5dStn143363 if (verify_stat_file_creation != 0 && verify_stat_file_creation() 134*d75e6a5dStn143363 != 0) { 135*d75e6a5dStn143363 warn(gettext("cannot create statistics file, " "%s"), 136*d75e6a5dStn143363 _rcfg->rcfg_stat_file); 137*d75e6a5dStn143363 goto err; 138*d75e6a5dStn143363 } 139*d75e6a5dStn143363 140*d75e6a5dStn143363 debug("done parsing\n"); 141*d75e6a5dStn143363 ret = E_SUCCESS; 142*d75e6a5dStn143363 goto out; 143*d75e6a5dStn143363 144*d75e6a5dStn143363 err: 145*d75e6a5dStn143363 if (scf_error() != SCF_ERROR_NONE) { 146*d75e6a5dStn143363 warn(gettext("Unexpected libscf error: %s. \n"), 147*d75e6a5dStn143363 scf_strerror(scf_error())); 148*d75e6a5dStn143363 } 149*d75e6a5dStn143363 150*d75e6a5dStn143363 out: 151*d75e6a5dStn143363 scf_simple_handle_destroy(simple_h); 152*d75e6a5dStn143363 return (ret); 153*d75e6a5dStn143363 } 154*d75e6a5dStn143363 155*d75e6a5dStn143363 void 156*d75e6a5dStn143363 rcfg_init(rcfg_t *rcfg) 157*d75e6a5dStn143363 { 158*d75e6a5dStn143363 bzero(rcfg, sizeof (*rcfg)); 159*d75e6a5dStn143363 (void) strcpy(rcfg->rcfg_stat_file, STAT_FILE_DEFAULT); 160*d75e6a5dStn143363 } 161*d75e6a5dStn143363 162*d75e6a5dStn143363 /* 163*d75e6a5dStn143363 * Modify configuration in repository given the rcfg_t structure. 164*d75e6a5dStn143363 */ 165*d75e6a5dStn143363 int 166*d75e6a5dStn143363 modify_config(rcfg_t *conf) 167*d75e6a5dStn143363 { 168*d75e6a5dStn143363 scf_simple_handle_t *simple_h; 169*d75e6a5dStn143363 scf_transaction_t *tx = NULL; 170*d75e6a5dStn143363 int rval, ret = E_ERROR; 171*d75e6a5dStn143363 172*d75e6a5dStn143363 if ((simple_h = scf_general_pg_setup(RCAP_FMRI, CONFIG_PG)) 173*d75e6a5dStn143363 == NULL) { 174*d75e6a5dStn143363 warn(gettext("SMF initialization problem: %s\n"), 175*d75e6a5dStn143363 scf_strerror(scf_error())); 176*d75e6a5dStn143363 goto out; 177*d75e6a5dStn143363 } 178*d75e6a5dStn143363 179*d75e6a5dStn143363 if ((tx = scf_transaction_setup(simple_h)) == NULL) { 180*d75e6a5dStn143363 warn(gettext("SMF initialization problem: %s\n"), 181*d75e6a5dStn143363 scf_strerror(scf_error())); 182*d75e6a5dStn143363 goto out; 183*d75e6a5dStn143363 } 184*d75e6a5dStn143363 185*d75e6a5dStn143363 do { 186*d75e6a5dStn143363 if (scf_set_count_property(tx, PRESSURE, 187*d75e6a5dStn143363 conf->rcfg_memory_cap_enforcement_pressure, 0) 188*d75e6a5dStn143363 != SCF_SUCCESS) { 189*d75e6a5dStn143363 warn(gettext("Couldn't set '%s' property. \n"), 190*d75e6a5dStn143363 PRESSURE); 191*d75e6a5dStn143363 goto out; 192*d75e6a5dStn143363 } 193*d75e6a5dStn143363 194*d75e6a5dStn143363 if (scf_set_count_property(tx, RECONFIG_INT, 195*d75e6a5dStn143363 conf->rcfg_reconfiguration_interval, 0) != SCF_SUCCESS) { 196*d75e6a5dStn143363 warn(gettext("Couldn't set '%s' property. \n"), 197*d75e6a5dStn143363 RECONFIG_INT); 198*d75e6a5dStn143363 goto out; 199*d75e6a5dStn143363 } 200*d75e6a5dStn143363 201*d75e6a5dStn143363 if (scf_set_count_property(tx, RSS_SAMPLE_INT, 202*d75e6a5dStn143363 conf->rcfg_rss_sample_interval, 0) != SCF_SUCCESS) { 203*d75e6a5dStn143363 warn(gettext("Couldn't set '%s' property. \n"), 204*d75e6a5dStn143363 RSS_SAMPLE_INT); 205*d75e6a5dStn143363 goto out; 206*d75e6a5dStn143363 } 207*d75e6a5dStn143363 208*d75e6a5dStn143363 if (scf_set_count_property(tx, REPORT_INT, 209*d75e6a5dStn143363 conf->rcfg_report_interval, 0) != SCF_SUCCESS) { 210*d75e6a5dStn143363 warn(gettext("Couldn't set '%s' property. \n"), 211*d75e6a5dStn143363 REPORT_INT); 212*d75e6a5dStn143363 goto out; 213*d75e6a5dStn143363 } 214*d75e6a5dStn143363 215*d75e6a5dStn143363 if (scf_set_count_property(tx, WALK_INT, 216*d75e6a5dStn143363 conf->rcfg_proc_walk_interval, 0) != SCF_SUCCESS) { 217*d75e6a5dStn143363 warn(gettext("Couldn't set '%s' property. \n"), 218*d75e6a5dStn143363 WALK_INT); 219*d75e6a5dStn143363 goto out; 220*d75e6a5dStn143363 } 221*d75e6a5dStn143363 222*d75e6a5dStn143363 if ((rval = scf_transaction_commit(tx)) == -1) 223*d75e6a5dStn143363 goto out; 224*d75e6a5dStn143363 225*d75e6a5dStn143363 if (rval == 0) { 226*d75e6a5dStn143363 if (scf_transaction_restart(simple_h, tx) 227*d75e6a5dStn143363 != SCF_SUCCESS) { 228*d75e6a5dStn143363 warn(gettext("SMF initialization problem: " 229*d75e6a5dStn143363 "%s\n"), scf_strerror(scf_error())); 230*d75e6a5dStn143363 goto out; 231*d75e6a5dStn143363 } 232*d75e6a5dStn143363 } 233*d75e6a5dStn143363 } while (rval == 0); 234*d75e6a5dStn143363 235*d75e6a5dStn143363 ret = E_SUCCESS; 236*d75e6a5dStn143363 237*d75e6a5dStn143363 out: 238*d75e6a5dStn143363 if (tx != NULL) { 239*d75e6a5dStn143363 scf_transaction_destroy_children(tx); 240*d75e6a5dStn143363 scf_transaction_destroy(tx); 241*d75e6a5dStn143363 } 242*d75e6a5dStn143363 scf_simple_handle_destroy(simple_h); 243*d75e6a5dStn143363 return (ret); 244*d75e6a5dStn143363 } 245