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