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 #include <sys/types.h>
26*d75e6a5dStn143363 #include <sys/stat.h>
27*d75e6a5dStn143363 #include <stdio.h>
28*d75e6a5dStn143363 #include <stdlib.h>
29*d75e6a5dStn143363 #include <errno.h>
30*d75e6a5dStn143363 #include <strings.h>
31*d75e6a5dStn143363 #include <fcntl.h>
32*d75e6a5dStn143363 #include <unistd.h>
33*d75e6a5dStn143363 #include <libscf.h>
34*d75e6a5dStn143363 #include <libscf_priv.h>
35*d75e6a5dStn143363 #include <libuutil.h>
36*d75e6a5dStn143363 #include "rcapd.h"
37*d75e6a5dStn143363 #include "rcapd_conf.h"
38*d75e6a5dStn143363 #include "rcapd_stat.h"
39*d75e6a5dStn143363 #include "utils.h"
40*d75e6a5dStn143363
41*d75e6a5dStn143363 /*
42*d75e6a5dStn143363 * Read configuration and set the fields of an rcfg_t correspondingly.
43*d75e6a5dStn143363 * Verify that the statistics file is writable, with the optional
44*d75e6a5dStn143363 * verify_stat_file_creation() callback.
45*d75e6a5dStn143363 */
46*d75e6a5dStn143363 int
rcfg_read(rcfg_t * _rcfg,int (* verify_stat_file_creation)(void))47*d75e6a5dStn143363 rcfg_read(rcfg_t *_rcfg, int(*verify_stat_file_creation)(void))
48*d75e6a5dStn143363 {
49*d75e6a5dStn143363 scf_simple_handle_t *simple_h;
50*d75e6a5dStn143363 uint64_t count_val;
51*d75e6a5dStn143363 int ret = E_ERROR;
52*d75e6a5dStn143363
53*d75e6a5dStn143363 rcfg_init(_rcfg);
54*d75e6a5dStn143363
55*d75e6a5dStn143363 if ((simple_h = scf_general_pg_setup(RCAP_FMRI, CONFIG_PG))
56*d75e6a5dStn143363 == NULL) {
57*d75e6a5dStn143363 warn(gettext("SMF initialization problem: %s\n"),
58*d75e6a5dStn143363 scf_strerror(scf_error()));
59*d75e6a5dStn143363 goto err;
60*d75e6a5dStn143363 }
61*d75e6a5dStn143363
62*d75e6a5dStn143363 if (scf_read_count_property(simple_h, PRESSURE, &count_val)
63*d75e6a5dStn143363 == SCF_FAILED) {
64*d75e6a5dStn143363 warn(gettext("Configuration property '%s' "
65*d75e6a5dStn143363 "not found. \n"), PRESSURE);
66*d75e6a5dStn143363 goto err;
67*d75e6a5dStn143363 } else {
68*d75e6a5dStn143363 if (count_val > 100)
69*d75e6a5dStn143363 _rcfg->rcfg_memory_cap_enforcement_pressure = 100;
70*d75e6a5dStn143363 else
71*d75e6a5dStn143363 _rcfg->rcfg_memory_cap_enforcement_pressure
72*d75e6a5dStn143363 = count_val;
73*d75e6a5dStn143363
74*d75e6a5dStn143363 debug("cap max pressure: %d%%\n",
75*d75e6a5dStn143363 _rcfg->rcfg_memory_cap_enforcement_pressure);
76*d75e6a5dStn143363 }
77*d75e6a5dStn143363
78*d75e6a5dStn143363 if (scf_read_count_property(simple_h, RECONFIG_INT, &count_val)
79*d75e6a5dStn143363 == SCF_FAILED) {
80*d75e6a5dStn143363 warn(gettext("Configuration property '%s' "
81*d75e6a5dStn143363 "not found. \n"), RECONFIG_INT);
82*d75e6a5dStn143363 goto err;
83*d75e6a5dStn143363 } else {
84*d75e6a5dStn143363 _rcfg->rcfg_reconfiguration_interval = count_val;
85*d75e6a5dStn143363 debug("reconfiguration interval: %d seconds\n",
86*d75e6a5dStn143363 _rcfg->rcfg_reconfiguration_interval);
87*d75e6a5dStn143363 }
88*d75e6a5dStn143363
89*d75e6a5dStn143363 if (scf_read_count_property(simple_h, REPORT_INT, &count_val)
90*d75e6a5dStn143363 == SCF_FAILED) {
91*d75e6a5dStn143363 warn(gettext("Configuration property '%s' "
92*d75e6a5dStn143363 "not found. \n"), REPORT_INT);
93*d75e6a5dStn143363 goto err;
94*d75e6a5dStn143363 } else {
95*d75e6a5dStn143363 _rcfg->rcfg_report_interval = count_val;
96*d75e6a5dStn143363 debug("report interval: %d seconds\n",
97*d75e6a5dStn143363 _rcfg->rcfg_report_interval);
98*d75e6a5dStn143363 }
99*d75e6a5dStn143363
100*d75e6a5dStn143363 if (scf_read_count_property(simple_h, RSS_SAMPLE_INT, &count_val)
101*d75e6a5dStn143363 == SCF_FAILED) {
102*d75e6a5dStn143363 warn(gettext("Configuration property '%s' "
103*d75e6a5dStn143363 "not found. \n"), RSS_SAMPLE_INT);
104*d75e6a5dStn143363 goto err;
105*d75e6a5dStn143363 } else {
106*d75e6a5dStn143363 _rcfg->rcfg_rss_sample_interval = count_val;
107*d75e6a5dStn143363 debug("RSS sample interval: %d seconds\n",
108*d75e6a5dStn143363 _rcfg->rcfg_rss_sample_interval);
109*d75e6a5dStn143363 }
110*d75e6a5dStn143363
111*d75e6a5dStn143363 if (scf_read_count_property(simple_h, WALK_INT, &count_val)
112*d75e6a5dStn143363 == SCF_FAILED) {
113*d75e6a5dStn143363 warn(gettext("Configuration property '%s' "
114*d75e6a5dStn143363 "not found. \n"), WALK_INT);
115*d75e6a5dStn143363 goto err;
116*d75e6a5dStn143363 } else {
117*d75e6a5dStn143363 _rcfg->rcfg_proc_walk_interval = count_val;
118*d75e6a5dStn143363 debug("proc_walk interval: %d seconds\n",
119*d75e6a5dStn143363 _rcfg->rcfg_proc_walk_interval);
120*d75e6a5dStn143363 }
121*d75e6a5dStn143363
122*d75e6a5dStn143363 if (_rcfg->rcfg_mode_name == NULL) {
123*d75e6a5dStn143363 /*
124*d75e6a5dStn143363 * Set project mode, by default.
125*d75e6a5dStn143363 */
126*d75e6a5dStn143363 _rcfg->rcfg_mode = rctype_project;
127*d75e6a5dStn143363 _rcfg->rcfg_mode_name = "project";
128*d75e6a5dStn143363 debug("mode: %s\n", _rcfg->rcfg_mode_name);
129*d75e6a5dStn143363 }
130*d75e6a5dStn143363
131*d75e6a5dStn143363 if (verify_stat_file_creation != 0 && verify_stat_file_creation()
132*d75e6a5dStn143363 != 0) {
133*d75e6a5dStn143363 warn(gettext("cannot create statistics file, " "%s"),
134*d75e6a5dStn143363 _rcfg->rcfg_stat_file);
135*d75e6a5dStn143363 goto err;
136*d75e6a5dStn143363 }
137*d75e6a5dStn143363
138*d75e6a5dStn143363 debug("done parsing\n");
139*d75e6a5dStn143363 ret = E_SUCCESS;
140*d75e6a5dStn143363 goto out;
141*d75e6a5dStn143363
142*d75e6a5dStn143363 err:
143*d75e6a5dStn143363 if (scf_error() != SCF_ERROR_NONE) {
144*d75e6a5dStn143363 warn(gettext("Unexpected libscf error: %s. \n"),
145*d75e6a5dStn143363 scf_strerror(scf_error()));
146*d75e6a5dStn143363 }
147*d75e6a5dStn143363
148*d75e6a5dStn143363 out:
149*d75e6a5dStn143363 scf_simple_handle_destroy(simple_h);
150*d75e6a5dStn143363 return (ret);
151*d75e6a5dStn143363 }
152*d75e6a5dStn143363
153*d75e6a5dStn143363 void
rcfg_init(rcfg_t * rcfg)154*d75e6a5dStn143363 rcfg_init(rcfg_t *rcfg)
155*d75e6a5dStn143363 {
156*d75e6a5dStn143363 bzero(rcfg, sizeof (*rcfg));
157*d75e6a5dStn143363 (void) strcpy(rcfg->rcfg_stat_file, STAT_FILE_DEFAULT);
158*d75e6a5dStn143363 }
159*d75e6a5dStn143363
160*d75e6a5dStn143363 /*
161*d75e6a5dStn143363 * Modify configuration in repository given the rcfg_t structure.
162*d75e6a5dStn143363 */
163*d75e6a5dStn143363 int
modify_config(rcfg_t * conf)164*d75e6a5dStn143363 modify_config(rcfg_t *conf)
165*d75e6a5dStn143363 {
166*d75e6a5dStn143363 scf_simple_handle_t *simple_h;
167*d75e6a5dStn143363 scf_transaction_t *tx = NULL;
168*d75e6a5dStn143363 int rval, ret = E_ERROR;
169*d75e6a5dStn143363
170*d75e6a5dStn143363 if ((simple_h = scf_general_pg_setup(RCAP_FMRI, CONFIG_PG))
171*d75e6a5dStn143363 == NULL) {
172*d75e6a5dStn143363 warn(gettext("SMF initialization problem: %s\n"),
173*d75e6a5dStn143363 scf_strerror(scf_error()));
174*d75e6a5dStn143363 goto out;
175*d75e6a5dStn143363 }
176*d75e6a5dStn143363
177*d75e6a5dStn143363 if ((tx = scf_transaction_setup(simple_h)) == NULL) {
178*d75e6a5dStn143363 warn(gettext("SMF initialization problem: %s\n"),
179*d75e6a5dStn143363 scf_strerror(scf_error()));
180*d75e6a5dStn143363 goto out;
181*d75e6a5dStn143363 }
182*d75e6a5dStn143363
183*d75e6a5dStn143363 do {
184*d75e6a5dStn143363 if (scf_set_count_property(tx, PRESSURE,
185*d75e6a5dStn143363 conf->rcfg_memory_cap_enforcement_pressure, 0)
186*d75e6a5dStn143363 != SCF_SUCCESS) {
187*d75e6a5dStn143363 warn(gettext("Couldn't set '%s' property. \n"),
188*d75e6a5dStn143363 PRESSURE);
189*d75e6a5dStn143363 goto out;
190*d75e6a5dStn143363 }
191*d75e6a5dStn143363
192*d75e6a5dStn143363 if (scf_set_count_property(tx, RECONFIG_INT,
193*d75e6a5dStn143363 conf->rcfg_reconfiguration_interval, 0) != SCF_SUCCESS) {
194*d75e6a5dStn143363 warn(gettext("Couldn't set '%s' property. \n"),
195*d75e6a5dStn143363 RECONFIG_INT);
196*d75e6a5dStn143363 goto out;
197*d75e6a5dStn143363 }
198*d75e6a5dStn143363
199*d75e6a5dStn143363 if (scf_set_count_property(tx, RSS_SAMPLE_INT,
200*d75e6a5dStn143363 conf->rcfg_rss_sample_interval, 0) != SCF_SUCCESS) {
201*d75e6a5dStn143363 warn(gettext("Couldn't set '%s' property. \n"),
202*d75e6a5dStn143363 RSS_SAMPLE_INT);
203*d75e6a5dStn143363 goto out;
204*d75e6a5dStn143363 }
205*d75e6a5dStn143363
206*d75e6a5dStn143363 if (scf_set_count_property(tx, REPORT_INT,
207*d75e6a5dStn143363 conf->rcfg_report_interval, 0) != SCF_SUCCESS) {
208*d75e6a5dStn143363 warn(gettext("Couldn't set '%s' property. \n"),
209*d75e6a5dStn143363 REPORT_INT);
210*d75e6a5dStn143363 goto out;
211*d75e6a5dStn143363 }
212*d75e6a5dStn143363
213*d75e6a5dStn143363 if (scf_set_count_property(tx, WALK_INT,
214*d75e6a5dStn143363 conf->rcfg_proc_walk_interval, 0) != SCF_SUCCESS) {
215*d75e6a5dStn143363 warn(gettext("Couldn't set '%s' property. \n"),
216*d75e6a5dStn143363 WALK_INT);
217*d75e6a5dStn143363 goto out;
218*d75e6a5dStn143363 }
219*d75e6a5dStn143363
220*d75e6a5dStn143363 if ((rval = scf_transaction_commit(tx)) == -1)
221*d75e6a5dStn143363 goto out;
222*d75e6a5dStn143363
223*d75e6a5dStn143363 if (rval == 0) {
224*d75e6a5dStn143363 if (scf_transaction_restart(simple_h, tx)
225*d75e6a5dStn143363 != SCF_SUCCESS) {
226*d75e6a5dStn143363 warn(gettext("SMF initialization problem: "
227*d75e6a5dStn143363 "%s\n"), scf_strerror(scf_error()));
228*d75e6a5dStn143363 goto out;
229*d75e6a5dStn143363 }
230*d75e6a5dStn143363 }
231*d75e6a5dStn143363 } while (rval == 0);
232*d75e6a5dStn143363
233*d75e6a5dStn143363 ret = E_SUCCESS;
234*d75e6a5dStn143363
235*d75e6a5dStn143363 out:
236*d75e6a5dStn143363 if (tx != NULL) {
237*d75e6a5dStn143363 scf_transaction_destroy_children(tx);
238*d75e6a5dStn143363 scf_transaction_destroy(tx);
239*d75e6a5dStn143363 }
240*d75e6a5dStn143363 scf_simple_handle_destroy(simple_h);
241*d75e6a5dStn143363 return (ret);
242*d75e6a5dStn143363 }
243