xref: /onnv-gate/usr/src/cmd/avs/dscfg/dscfg.c (revision 7836:4e95154b5b7a)
1*7836SJohn.Forte@Sun.COM /*
2*7836SJohn.Forte@Sun.COM  * CDDL HEADER START
3*7836SJohn.Forte@Sun.COM  *
4*7836SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
5*7836SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
6*7836SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
7*7836SJohn.Forte@Sun.COM  *
8*7836SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7836SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*7836SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
11*7836SJohn.Forte@Sun.COM  * and limitations under the License.
12*7836SJohn.Forte@Sun.COM  *
13*7836SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*7836SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7836SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*7836SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*7836SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7836SJohn.Forte@Sun.COM  *
19*7836SJohn.Forte@Sun.COM  * CDDL HEADER END
20*7836SJohn.Forte@Sun.COM  */
21*7836SJohn.Forte@Sun.COM /*
22*7836SJohn.Forte@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*7836SJohn.Forte@Sun.COM  * Use is subject to license terms.
24*7836SJohn.Forte@Sun.COM  */
25*7836SJohn.Forte@Sun.COM 
26*7836SJohn.Forte@Sun.COM #include <stdio.h>
27*7836SJohn.Forte@Sun.COM #include <sys/types.h>
28*7836SJohn.Forte@Sun.COM #include <sys/wait.h>
29*7836SJohn.Forte@Sun.COM #include <sys/vtoc.h>
30*7836SJohn.Forte@Sun.COM #include <sys/stat.h>
31*7836SJohn.Forte@Sun.COM #include <stdio.h>
32*7836SJohn.Forte@Sun.COM #include <sys/mnttab.h>
33*7836SJohn.Forte@Sun.COM #include <errno.h>
34*7836SJohn.Forte@Sun.COM #include <limits.h>
35*7836SJohn.Forte@Sun.COM #include <fcntl.h>
36*7836SJohn.Forte@Sun.COM #include <string.h>
37*7836SJohn.Forte@Sun.COM #include <strings.h>
38*7836SJohn.Forte@Sun.COM #include <stdlib.h>
39*7836SJohn.Forte@Sun.COM #include <unistd.h>
40*7836SJohn.Forte@Sun.COM #include <time.h>
41*7836SJohn.Forte@Sun.COM 
42*7836SJohn.Forte@Sun.COM #include <locale.h>
43*7836SJohn.Forte@Sun.COM #include <langinfo.h>
44*7836SJohn.Forte@Sun.COM #include <libintl.h>
45*7836SJohn.Forte@Sun.COM #include <stdarg.h>
46*7836SJohn.Forte@Sun.COM #include <netdb.h>
47*7836SJohn.Forte@Sun.COM #include <ctype.h>
48*7836SJohn.Forte@Sun.COM #include <assert.h>
49*7836SJohn.Forte@Sun.COM 
50*7836SJohn.Forte@Sun.COM #include <sys/nsctl/cfg_impl.h>
51*7836SJohn.Forte@Sun.COM #include <sys/nsctl/cfg.h>
52*7836SJohn.Forte@Sun.COM 
53*7836SJohn.Forte@Sun.COM #include <sys/unistat/spcs_s.h>
54*7836SJohn.Forte@Sun.COM #include <sys/unistat/spcs_s_u.h>
55*7836SJohn.Forte@Sun.COM #include <sys/unistat/spcs_errors.h>
56*7836SJohn.Forte@Sun.COM 
57*7836SJohn.Forte@Sun.COM #ifdef DEBUG
58*7836SJohn.Forte@Sun.COM #include <sys/nsctl/dsw.h>
59*7836SJohn.Forte@Sun.COM #endif
60*7836SJohn.Forte@Sun.COM 
61*7836SJohn.Forte@Sun.COM #define	DEFAULT_PARSER_LOC "/etc/dscfg_format"
62*7836SJohn.Forte@Sun.COM 
63*7836SJohn.Forte@Sun.COM int Cflg;
64*7836SJohn.Forte@Sun.COM int Dflg;
65*7836SJohn.Forte@Sun.COM int Lflg;
66*7836SJohn.Forte@Sun.COM int aflg;
67*7836SJohn.Forte@Sun.COM int iflg;
68*7836SJohn.Forte@Sun.COM int lflg;
69*7836SJohn.Forte@Sun.COM int nflg;
70*7836SJohn.Forte@Sun.COM int pflg;
71*7836SJohn.Forte@Sun.COM int rflg;
72*7836SJohn.Forte@Sun.COM int sflg;
73*7836SJohn.Forte@Sun.COM int uflg;
74*7836SJohn.Forte@Sun.COM 
75*7836SJohn.Forte@Sun.COM int verbose;
76*7836SJohn.Forte@Sun.COM int noflags;
77*7836SJohn.Forte@Sun.COM int errflg;
78*7836SJohn.Forte@Sun.COM int mustcommit;
79*7836SJohn.Forte@Sun.COM char *locname;		/* config location  from cfg_location */
80*7836SJohn.Forte@Sun.COM char *cmdname;
81*7836SJohn.Forte@Sun.COM 
82*7836SJohn.Forte@Sun.COM #define	MAX_FILENAME	80
83*7836SJohn.Forte@Sun.COM 
84*7836SJohn.Forte@Sun.COM char output_file[MAX_FILENAME];	/* specified output file */
85*7836SJohn.Forte@Sun.COM char altroot[MAX_FILENAME];	/* specifed root location */
86*7836SJohn.Forte@Sun.COM char config_file[MAX_FILENAME];	/* specified configuration file */
87*7836SJohn.Forte@Sun.COM char input_file[MAX_FILENAME];	/* specified input file */
88*7836SJohn.Forte@Sun.COM char logical_host[MAX_FILENAME]; /* specified  cluster node */
89*7836SJohn.Forte@Sun.COM char device_group[MAX_FILENAME]; /* specified device group name */
90*7836SJohn.Forte@Sun.COM 
91*7836SJohn.Forte@Sun.COM #define	IS_NOT_CLUSTER	1
92*7836SJohn.Forte@Sun.COM #define	IS_CLUSTER	2
93*7836SJohn.Forte@Sun.COM 
94*7836SJohn.Forte@Sun.COM void cfg_invalidate_hsizes(int, const char *);
95*7836SJohn.Forte@Sun.COM static int check_cluster();
96*7836SJohn.Forte@Sun.COM 
97*7836SJohn.Forte@Sun.COM void
usage(char * errmsg)98*7836SJohn.Forte@Sun.COM usage(char *errmsg)
99*7836SJohn.Forte@Sun.COM {
100*7836SJohn.Forte@Sun.COM 	if (errmsg)
101*7836SJohn.Forte@Sun.COM 		(void) fprintf(stderr, "%s: %s\n", cmdname, errmsg);
102*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr,
103*7836SJohn.Forte@Sun.COM 	    gettext("dscfg \t\t\t\tDisplay location of "
104*7836SJohn.Forte@Sun.COM 		"local configuration database\n"));
105*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -l -s path\t\t"
106*7836SJohn.Forte@Sun.COM 		"List contents of configuration database\n"));
107*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext(
108*7836SJohn.Forte@Sun.COM 		"\t\t\t\tlocated at path specified\n"));
109*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -i\t\t\t"
110*7836SJohn.Forte@Sun.COM 		"Initialize configuration database\n"));
111*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr,
112*7836SJohn.Forte@Sun.COM 	    gettext("dscfg -i -p "
113*7836SJohn.Forte@Sun.COM #ifdef DEBUG
114*7836SJohn.Forte@Sun.COM "[-n] "
115*7836SJohn.Forte@Sun.COM #endif
116*7836SJohn.Forte@Sun.COM 	    "/etc/dscfg_format\tFormat configuration database\n"));
117*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr,
118*7836SJohn.Forte@Sun.COM 	    gettext("dscfg -a file\t\t\tRestore configuration "
119*7836SJohn.Forte@Sun.COM 	    "database from file\n"));
120*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("\t\t\t\tspecified\n"));
121*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr,
122*7836SJohn.Forte@Sun.COM 		gettext("dscfg -l\t\t\tList contents of configuration database"
123*7836SJohn.Forte@Sun.COM 		"\n"));
124*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr,
125*7836SJohn.Forte@Sun.COM 		gettext("dscfg -L\t\t\tDisplay configuration database's\n"));
126*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("\t\t\t\tlock status\n"));
127*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -h\t\t\tUsage message\n"));
128*7836SJohn.Forte@Sun.COM 	if (check_cluster() != IS_NOT_CLUSTER) {
129*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("\nSun Cluster Usage\n"));
130*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("******************\n"));
131*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr,
132*7836SJohn.Forte@Sun.COM 		gettext("dscfg -s path\t\t\tSet cluster "
133*7836SJohn.Forte@Sun.COM 		"configuration database at DID\n"));
134*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("\t\t\t\tpath specified\n"));
135*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -D device_group\t\t"
136*7836SJohn.Forte@Sun.COM 		"Check status of cluster device group\n"));
137*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -C -\t\t\t"
138*7836SJohn.Forte@Sun.COM 		"Display location of cluster configuration\n"));
139*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("\t\t\t\tdatabase\n"));
140*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -l -s DID_device\t\tList "
141*7836SJohn.Forte@Sun.COM 		"the contents of cluster configuration\n"));
142*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("\t\t\t\tdatabase\n"));
143*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -C - -i\t\t\tInitialize "
144*7836SJohn.Forte@Sun.COM 		"cluster configuration database\n"));
145*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -C - -i -p "
146*7836SJohn.Forte@Sun.COM 		"/etc/dscfg_format Format cluster configuration database\n"));
147*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -C - -a file\t\t"
148*7836SJohn.Forte@Sun.COM 		"Restore cluster configuration database from\n"));
149*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("\t\t\t\tfile specified\n"));
150*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -C - -l\t\t\t"
151*7836SJohn.Forte@Sun.COM 		"List contents of local configuration database\n"));
152*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -C device_group -l\t"
153*7836SJohn.Forte@Sun.COM 		"List configuration database by device group\n"));
154*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("dscfg -C \"-\" -l\t\t\t"
155*7836SJohn.Forte@Sun.COM 		"List configuration database excluding\n"));
156*7836SJohn.Forte@Sun.COM 	(void) fprintf(stderr, gettext("\t\t\t\tdevice groups\n"));
157*7836SJohn.Forte@Sun.COM 	}
158*7836SJohn.Forte@Sun.COM }
159*7836SJohn.Forte@Sun.COM 
160*7836SJohn.Forte@Sun.COM int
parse_parse_config(CFGFILE * cfg)161*7836SJohn.Forte@Sun.COM parse_parse_config(CFGFILE *cfg)
162*7836SJohn.Forte@Sun.COM {
163*7836SJohn.Forte@Sun.COM 	FILE *fp;
164*7836SJohn.Forte@Sun.COM 	char	inbuf[CFG_MAX_BUF];
165*7836SJohn.Forte@Sun.COM 	char	*buff;
166*7836SJohn.Forte@Sun.COM 	int	rc;
167*7836SJohn.Forte@Sun.COM 
168*7836SJohn.Forte@Sun.COM 	/*
169*7836SJohn.Forte@Sun.COM 	 * Open parser config file, use default if none specified
170*7836SJohn.Forte@Sun.COM 	 */
171*7836SJohn.Forte@Sun.COM 	buff = (input_file[0]) ? input_file : DEFAULT_PARSER_LOC;
172*7836SJohn.Forte@Sun.COM 	if ((fp = fopen(buff, "r")) == NULL) {
173*7836SJohn.Forte@Sun.COM 		(void) fprintf(stderr,
174*7836SJohn.Forte@Sun.COM 			gettext("parser config file (%s) not found\n"), buff);
175*7836SJohn.Forte@Sun.COM 		return (-1);
176*7836SJohn.Forte@Sun.COM 	}
177*7836SJohn.Forte@Sun.COM 
178*7836SJohn.Forte@Sun.COM 	/*
179*7836SJohn.Forte@Sun.COM 	 * start at begining of configration database
180*7836SJohn.Forte@Sun.COM 	 */
181*7836SJohn.Forte@Sun.COM 	cfg_rewind(cfg, CFG_SEC_ALL);
182*7836SJohn.Forte@Sun.COM 
183*7836SJohn.Forte@Sun.COM 	while (((buff = fgets(inbuf, (sizeof (inbuf) - 1), fp)) != NULL)) {
184*7836SJohn.Forte@Sun.COM 		if (*buff == '#' || *buff == '%')
185*7836SJohn.Forte@Sun.COM 			continue;
186*7836SJohn.Forte@Sun.COM 		/* overwrite newline */
187*7836SJohn.Forte@Sun.COM 		buff[strlen(buff) - 1] = '\0';
188*7836SJohn.Forte@Sun.COM 		rc = cfg_update_parser_config(cfg, buff, CFG_PARSE_CONF);
189*7836SJohn.Forte@Sun.COM 		if (rc < 0) {
190*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr,
191*7836SJohn.Forte@Sun.COM 			    gettext("update parser config rc %d key %s\n"),
192*7836SJohn.Forte@Sun.COM 			    rc, buff);
193*7836SJohn.Forte@Sun.COM 			(void) fclose(fp);
194*7836SJohn.Forte@Sun.COM 			return (-1);
195*7836SJohn.Forte@Sun.COM 		}
196*7836SJohn.Forte@Sun.COM 	}
197*7836SJohn.Forte@Sun.COM 	(void) fclose(fp);
198*7836SJohn.Forte@Sun.COM 	return (1);
199*7836SJohn.Forte@Sun.COM }
200*7836SJohn.Forte@Sun.COM 
201*7836SJohn.Forte@Sun.COM void
parse_text_config(CFGFILE * cfg)202*7836SJohn.Forte@Sun.COM parse_text_config(CFGFILE *cfg)
203*7836SJohn.Forte@Sun.COM {
204*7836SJohn.Forte@Sun.COM 	FILE *fp;
205*7836SJohn.Forte@Sun.COM 	char	inbuf[CFG_MAX_BUF];
206*7836SJohn.Forte@Sun.COM 	char	*buff;
207*7836SJohn.Forte@Sun.COM 	char	*key;
208*7836SJohn.Forte@Sun.COM 	char	*p;
209*7836SJohn.Forte@Sun.COM 	int	rc;
210*7836SJohn.Forte@Sun.COM 
211*7836SJohn.Forte@Sun.COM 	if ((fp = fopen(input_file, "r")) == NULL) {
212*7836SJohn.Forte@Sun.COM 		(void) fprintf(stderr,
213*7836SJohn.Forte@Sun.COM 			    gettext("Unable to open text config %s\n"),
214*7836SJohn.Forte@Sun.COM 				input_file);
215*7836SJohn.Forte@Sun.COM 		exit(2);
216*7836SJohn.Forte@Sun.COM 	}
217*7836SJohn.Forte@Sun.COM 	bzero(inbuf, sizeof (inbuf));
218*7836SJohn.Forte@Sun.COM 	cfg_rewind(cfg, CFG_SEC_CONF);
219*7836SJohn.Forte@Sun.COM 	while (((buff = fgets(inbuf, (sizeof (inbuf) - 1), fp)) != NULL)) {
220*7836SJohn.Forte@Sun.COM 		if (*buff == '#')
221*7836SJohn.Forte@Sun.COM 			continue;
222*7836SJohn.Forte@Sun.COM 		/* overwrite newline */
223*7836SJohn.Forte@Sun.COM 		buff[strlen(buff) - 1] = '\0';
224*7836SJohn.Forte@Sun.COM 		key = strtok(buff, ":");
225*7836SJohn.Forte@Sun.COM 		if (!key) {
226*7836SJohn.Forte@Sun.COM 			continue;
227*7836SJohn.Forte@Sun.COM 		}
228*7836SJohn.Forte@Sun.COM 		p = &buff[strlen(key)+2];
229*7836SJohn.Forte@Sun.COM 		while (*p && isspace(*p)) {
230*7836SJohn.Forte@Sun.COM 			++p;
231*7836SJohn.Forte@Sun.COM 		}
232*7836SJohn.Forte@Sun.COM 		if (!*p) {
233*7836SJohn.Forte@Sun.COM 			continue;
234*7836SJohn.Forte@Sun.COM 		}
235*7836SJohn.Forte@Sun.COM 		rc = cfg_put_cstring(cfg, key, p, strlen(p));
236*7836SJohn.Forte@Sun.COM 		if (rc < 0) {
237*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr,
238*7836SJohn.Forte@Sun.COM 			    gettext("update text config failed rc %d key %s"),
239*7836SJohn.Forte@Sun.COM 			    rc, buff);
240*7836SJohn.Forte@Sun.COM 			return;
241*7836SJohn.Forte@Sun.COM 		}
242*7836SJohn.Forte@Sun.COM 		bzero(inbuf, sizeof (inbuf));
243*7836SJohn.Forte@Sun.COM 	}
244*7836SJohn.Forte@Sun.COM 	(void) fclose(fp);
245*7836SJohn.Forte@Sun.COM }
246*7836SJohn.Forte@Sun.COM void
dump_status(CFGFILE * cfg)247*7836SJohn.Forte@Sun.COM dump_status(CFGFILE *cfg)
248*7836SJohn.Forte@Sun.COM {
249*7836SJohn.Forte@Sun.COM 	cfp_t *cfp = FP_SUN_CLUSTER(cfg);
250*7836SJohn.Forte@Sun.COM 
251*7836SJohn.Forte@Sun.COM 	/*
252*7836SJohn.Forte@Sun.COM 	 * WARNING will robinson
253*7836SJohn.Forte@Sun.COM 	 * The following is using a non-exported internal interface
254*7836SJohn.Forte@Sun.COM 	 * to libcfg
255*7836SJohn.Forte@Sun.COM 	 * You may not use any of the following fields in MS software
256*7836SJohn.Forte@Sun.COM 	 */
257*7836SJohn.Forte@Sun.COM 	if (!locname)
258*7836SJohn.Forte@Sun.COM 		exit(2);
259*7836SJohn.Forte@Sun.COM 	if (!verbose)
260*7836SJohn.Forte@Sun.COM 		(void) printf("%s\n", locname);
261*7836SJohn.Forte@Sun.COM 	else {
262*7836SJohn.Forte@Sun.COM #ifdef DEBUG
263*7836SJohn.Forte@Sun.COM 		(void) printf(gettext("Configuration location: %s\n"), locname);
264*7836SJohn.Forte@Sun.COM 		(void) printf(
265*7836SJohn.Forte@Sun.COM 		    gettext("Header info:\n\t\t\tmagic: %x\tstate: %x\n"),
266*7836SJohn.Forte@Sun.COM 		    cfp->cf_head->h_magic, cfp->cf_head->h_state);
267*7836SJohn.Forte@Sun.COM 		(void) printf(
268*7836SJohn.Forte@Sun.COM 		    gettext("Parser section:\t\t"
269*7836SJohn.Forte@Sun.COM 		    "Start: %x\tsize: %d offset: %d\n"),
270*7836SJohn.Forte@Sun.COM 		    cfp->cf_mapped, cfp->cf_head->h_parsesize,
271*7836SJohn.Forte@Sun.COM 		    cfp->cf_head->h_parseoff);
272*7836SJohn.Forte@Sun.COM 		(void) printf(
273*7836SJohn.Forte@Sun.COM 		    gettext("Config section:\t\t"
274*7836SJohn.Forte@Sun.COM 		    "Start: %x\tsize:%d\tacsize: %d\n"),
275*7836SJohn.Forte@Sun.COM 		    cfp->cf_head->h_cparse, cfp->cf_head->h_csize,
276*7836SJohn.Forte@Sun.COM 		    cfp->cf_head->h_acsize);
277*7836SJohn.Forte@Sun.COM 		(void) printf("\t\t\tccopy1: %s\tccopy2: %s\n",
278*7836SJohn.Forte@Sun.COM 			cfp->cf_head->h_ccopy1,
279*7836SJohn.Forte@Sun.COM 			cfp->cf_head->h_ccopy2);
280*7836SJohn.Forte@Sun.COM 		(void) printf(
281*7836SJohn.Forte@Sun.COM 		    gettext("Sequence:\t\tseq1: %d\t\tseq2: %d\n"),
282*7836SJohn.Forte@Sun.COM 			cfp->cf_head->h_seq1, cfp->cf_head->h_seq2);
283*7836SJohn.Forte@Sun.COM #endif
284*7836SJohn.Forte@Sun.COM 	}
285*7836SJohn.Forte@Sun.COM }
286*7836SJohn.Forte@Sun.COM 
287*7836SJohn.Forte@Sun.COM void
dump_lockstat(CFGFILE * cfg)288*7836SJohn.Forte@Sun.COM dump_lockstat(CFGFILE *cfg)
289*7836SJohn.Forte@Sun.COM {
290*7836SJohn.Forte@Sun.COM 	pid_t pid;
291*7836SJohn.Forte@Sun.COM 	CFGLOCK lock;
292*7836SJohn.Forte@Sun.COM 	char	ps_str[1024];
293*7836SJohn.Forte@Sun.COM 
294*7836SJohn.Forte@Sun.COM 	if (cfg_get_lock(cfg, &lock, &pid) == TRUE) {
295*7836SJohn.Forte@Sun.COM 		(void) printf("%s %ld\n",
296*7836SJohn.Forte@Sun.COM 		    lock == CFG_RDLOCK ?
297*7836SJohn.Forte@Sun.COM 			    gettext("Read locked by process id") :
298*7836SJohn.Forte@Sun.COM 			    gettext("Write locked by process id"),
299*7836SJohn.Forte@Sun.COM 		    pid);
300*7836SJohn.Forte@Sun.COM 		(void) sprintf(ps_str, "ps -p %ld", pid);
301*7836SJohn.Forte@Sun.COM 		system(ps_str);
302*7836SJohn.Forte@Sun.COM 	} else
303*7836SJohn.Forte@Sun.COM 		(void) printf("%s\n", gettext("Not locked."));
304*7836SJohn.Forte@Sun.COM }
305*7836SJohn.Forte@Sun.COM 
306*7836SJohn.Forte@Sun.COM 
307*7836SJohn.Forte@Sun.COM /*
308*7836SJohn.Forte@Sun.COM  * dump current configuration section to stdout
309*7836SJohn.Forte@Sun.COM  */
310*7836SJohn.Forte@Sun.COM 
311*7836SJohn.Forte@Sun.COM void
print_config(CFGFILE * cfg)312*7836SJohn.Forte@Sun.COM print_config(CFGFILE *cfg)
313*7836SJohn.Forte@Sun.COM {
314*7836SJohn.Forte@Sun.COM 	time_t tloc = 0;
315*7836SJohn.Forte@Sun.COM 	int set = 0;
316*7836SJohn.Forte@Sun.COM 	char pconfig[CFG_MAX_BUF];
317*7836SJohn.Forte@Sun.COM 	char key[CFG_MAX_KEY];
318*7836SJohn.Forte@Sun.COM 	char buf[CFG_MAX_BUF];
319*7836SJohn.Forte@Sun.COM 	char *cp, pbuf[CFG_MAX_BUF];
320*7836SJohn.Forte@Sun.COM 	FILE *fp;
321*7836SJohn.Forte@Sun.COM 	int rc;
322*7836SJohn.Forte@Sun.COM 	int end;
323*7836SJohn.Forte@Sun.COM 
324*7836SJohn.Forte@Sun.COM 	(void) snprintf(pconfig, sizeof (pconfig),
325*7836SJohn.Forte@Sun.COM 	    "%s%s", altroot, DEFAULT_PARSER_LOC);
326*7836SJohn.Forte@Sun.COM 	if ((fp = fopen(pconfig, "r")) == NULL) {
327*7836SJohn.Forte@Sun.COM 		(void) fprintf(stderr,
328*7836SJohn.Forte@Sun.COM 		    gettext("dscfg: unable to open "
329*7836SJohn.Forte@Sun.COM 		    "parser configuration (%s): %s\n"),
330*7836SJohn.Forte@Sun.COM 		    pconfig, strerror(errno));
331*7836SJohn.Forte@Sun.COM 		exit(1);
332*7836SJohn.Forte@Sun.COM 	}
333*7836SJohn.Forte@Sun.COM 
334*7836SJohn.Forte@Sun.COM 	(void) time(&tloc);
335*7836SJohn.Forte@Sun.COM 	(void) printf(gettext("# Consolidated Dataservice Configuration\n"));
336*7836SJohn.Forte@Sun.COM 	(void) printf(gettext("# Do not edit out whitespace or dashes\n"));
337*7836SJohn.Forte@Sun.COM 	(void) printf(gettext("# File created on: %s"), ctime(&tloc));
338*7836SJohn.Forte@Sun.COM 
339*7836SJohn.Forte@Sun.COM 	while (fgets(pbuf, (sizeof (pbuf) - 1), fp) != NULL) {
340*7836SJohn.Forte@Sun.COM 		if (pbuf[0] == '#') {
341*7836SJohn.Forte@Sun.COM 			/* comment */
342*7836SJohn.Forte@Sun.COM 			continue;
343*7836SJohn.Forte@Sun.COM 		}
344*7836SJohn.Forte@Sun.COM 		/* force a NULL terminator */
345*7836SJohn.Forte@Sun.COM 		pbuf[sizeof (pbuf) - 1] = '\0';
346*7836SJohn.Forte@Sun.COM 
347*7836SJohn.Forte@Sun.COM 		if (pbuf[0] == '%') {
348*7836SJohn.Forte@Sun.COM 			/*
349*7836SJohn.Forte@Sun.COM 			 * descriptive text
350*7836SJohn.Forte@Sun.COM 			 * - print it (with comment leader) and move on
351*7836SJohn.Forte@Sun.COM 			 */
352*7836SJohn.Forte@Sun.COM 			(void) printf("#%s", &pbuf[1]);
353*7836SJohn.Forte@Sun.COM 			continue;
354*7836SJohn.Forte@Sun.COM 		}
355*7836SJohn.Forte@Sun.COM 
356*7836SJohn.Forte@Sun.COM 		/*
357*7836SJohn.Forte@Sun.COM 		 * truncate the parser config in pbuf[] to just the tag
358*7836SJohn.Forte@Sun.COM 		 */
359*7836SJohn.Forte@Sun.COM 		cp = strchr(pbuf, '.');
360*7836SJohn.Forte@Sun.COM 		if (cp != NULL) {
361*7836SJohn.Forte@Sun.COM 			*cp = '\0';
362*7836SJohn.Forte@Sun.COM 		}
363*7836SJohn.Forte@Sun.COM 
364*7836SJohn.Forte@Sun.COM 		set = 1;
365*7836SJohn.Forte@Sun.COM 		/*CONSTCOND*/
366*7836SJohn.Forte@Sun.COM 		while (1) {
367*7836SJohn.Forte@Sun.COM 			bzero(buf, CFG_MAX_BUF);
368*7836SJohn.Forte@Sun.COM 			(void) snprintf(key,
369*7836SJohn.Forte@Sun.COM 			    sizeof (key), "%s.set%d", pbuf, set);
370*7836SJohn.Forte@Sun.COM 			rc = cfg_get_cstring(cfg, key, buf, CFG_MAX_BUF);
371*7836SJohn.Forte@Sun.COM 			if (rc < 0) {
372*7836SJohn.Forte@Sun.COM 				break;
373*7836SJohn.Forte@Sun.COM 			}
374*7836SJohn.Forte@Sun.COM 			/* trim trailing space if necessary */
375*7836SJohn.Forte@Sun.COM 			end = strlen(buf) - 1;
376*7836SJohn.Forte@Sun.COM 			if (buf[end] == ' ')
377*7836SJohn.Forte@Sun.COM 				buf[end] = '\0';
378*7836SJohn.Forte@Sun.COM 
379*7836SJohn.Forte@Sun.COM 			(void) printf("%s:%s\n", pbuf, buf);
380*7836SJohn.Forte@Sun.COM 			set++;
381*7836SJohn.Forte@Sun.COM 		}
382*7836SJohn.Forte@Sun.COM 	}
383*7836SJohn.Forte@Sun.COM 
384*7836SJohn.Forte@Sun.COM 	(void) fclose(fp);
385*7836SJohn.Forte@Sun.COM }
386*7836SJohn.Forte@Sun.COM 
387*7836SJohn.Forte@Sun.COM int
make_new_config(const char * fileloc)388*7836SJohn.Forte@Sun.COM make_new_config(const char *fileloc)
389*7836SJohn.Forte@Sun.COM {
390*7836SJohn.Forte@Sun.COM 	int fd;
391*7836SJohn.Forte@Sun.COM 	int rc;
392*7836SJohn.Forte@Sun.COM 	int skip;
393*7836SJohn.Forte@Sun.COM 
394*7836SJohn.Forte@Sun.COM 	char buf[CFG_MAX_BUF];
395*7836SJohn.Forte@Sun.COM 	/*CONSTCOND*/
396*7836SJohn.Forte@Sun.COM 	assert((sizeof (buf) % 512) == 0);
397*7836SJohn.Forte@Sun.COM 
398*7836SJohn.Forte@Sun.COM 	bzero(buf, CFG_MAX_BUF);
399*7836SJohn.Forte@Sun.COM 
400*7836SJohn.Forte@Sun.COM 	if ((fd = open(fileloc, O_RDWR | O_CREAT, 0640)) == -1) {
401*7836SJohn.Forte@Sun.COM 		return (-1);
402*7836SJohn.Forte@Sun.COM 	}
403*7836SJohn.Forte@Sun.COM 
404*7836SJohn.Forte@Sun.COM 	/* if this is a device, we may have to skip the vtoc */
405*7836SJohn.Forte@Sun.COM 	if ((skip = cfg_shldskip_vtoc(fd, fileloc)) == -1) {
406*7836SJohn.Forte@Sun.COM 		(void) fprintf(stderr,
407*7836SJohn.Forte@Sun.COM 			gettext("dscfg: unable to read vtoc on (%s)\n"),
408*7836SJohn.Forte@Sun.COM 				fileloc);
409*7836SJohn.Forte@Sun.COM 		return (-1);
410*7836SJohn.Forte@Sun.COM 	} else if (skip) {
411*7836SJohn.Forte@Sun.COM 		do {
412*7836SJohn.Forte@Sun.COM 			rc = lseek(fd, CFG_VTOC_SKIP, SEEK_SET);
413*7836SJohn.Forte@Sun.COM 		} while (rc == -1 && errno == EINTR);
414*7836SJohn.Forte@Sun.COM 
415*7836SJohn.Forte@Sun.COM 		if (rc == -1) {
416*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr, gettext("dscfg: seek error"));
417*7836SJohn.Forte@Sun.COM 			return (-1);
418*7836SJohn.Forte@Sun.COM 		}
419*7836SJohn.Forte@Sun.COM 	}
420*7836SJohn.Forte@Sun.COM 
421*7836SJohn.Forte@Sun.COM 	do {
422*7836SJohn.Forte@Sun.COM 		rc = write(fd, buf, sizeof (buf));
423*7836SJohn.Forte@Sun.COM 	} while (rc == -1 && errno == EINTR);
424*7836SJohn.Forte@Sun.COM 
425*7836SJohn.Forte@Sun.COM 	close(fd);
426*7836SJohn.Forte@Sun.COM 
427*7836SJohn.Forte@Sun.COM 	return ((rc < 0) ? 0 : 1);
428*7836SJohn.Forte@Sun.COM }
429*7836SJohn.Forte@Sun.COM 
430*7836SJohn.Forte@Sun.COM /*
431*7836SJohn.Forte@Sun.COM  * dscfg
432*7836SJohn.Forte@Sun.COM  * configure or return dataservice persistent configuration
433*7836SJohn.Forte@Sun.COM  *
434*7836SJohn.Forte@Sun.COM  * options
435*7836SJohn.Forte@Sun.COM  *		-i initialize file for first time
436*7836SJohn.Forte@Sun.COM  *		-l dump current configuration to stdout in ascii
437*7836SJohn.Forte@Sun.COM  *		-a add
438*7836SJohn.Forte@Sun.COM  *		-C node	Set resource filter
439*7836SJohn.Forte@Sun.COM  *		-p parser config specified input file
440*7836SJohn.Forte@Sun.COM  *		-s set partition location or filename in default location
441*7836SJohn.Forte@Sun.COM  *		-L print configuration lock status
442*7836SJohn.Forte@Sun.COM  *		-u upgrade
443*7836SJohn.Forte@Sun.COM  *		-r prepend bootdir to beginning of path for cfg_open
444*7836SJohn.Forte@Sun.COM  *	no options    status
445*7836SJohn.Forte@Sun.COM  *
446*7836SJohn.Forte@Sun.COM  *
447*7836SJohn.Forte@Sun.COM  */
448*7836SJohn.Forte@Sun.COM #ifdef lint
449*7836SJohn.Forte@Sun.COM int
dscfg_lintmain(int argc,char * argv[])450*7836SJohn.Forte@Sun.COM dscfg_lintmain(int argc, char *argv[])
451*7836SJohn.Forte@Sun.COM #else
452*7836SJohn.Forte@Sun.COM int
453*7836SJohn.Forte@Sun.COM main(int argc, char *argv[])
454*7836SJohn.Forte@Sun.COM #endif
455*7836SJohn.Forte@Sun.COM {
456*7836SJohn.Forte@Sun.COM 	CFGFILE *cfg;
457*7836SJohn.Forte@Sun.COM 	extern char *optarg;
458*7836SJohn.Forte@Sun.COM 	char *loc;
459*7836SJohn.Forte@Sun.COM 	int offset = 0;
460*7836SJohn.Forte@Sun.COM 	int rc;
461*7836SJohn.Forte@Sun.COM 	char c;
462*7836SJohn.Forte@Sun.COM 	int local;
463*7836SJohn.Forte@Sun.COM 	int action_counts = 0;
464*7836SJohn.Forte@Sun.COM 
465*7836SJohn.Forte@Sun.COM 	bzero(input_file, sizeof (input_file));
466*7836SJohn.Forte@Sun.COM 	(void) setlocale(LC_ALL, "");
467*7836SJohn.Forte@Sun.COM 	(void) textdomain("dscfg");
468*7836SJohn.Forte@Sun.COM 	logical_host[0] = '\0';
469*7836SJohn.Forte@Sun.COM 	cmdname = argv[0];
470*7836SJohn.Forte@Sun.COM #ifdef DEBUG
471*7836SJohn.Forte@Sun.COM 	while ((c = getopt(argc, argv, "a:C:dD:ilLp:r:s:hvn")) != EOF) {
472*7836SJohn.Forte@Sun.COM #else
473*7836SJohn.Forte@Sun.COM 	while ((c = getopt(argc, argv, "a:C:dD:ilLp:r:s:h")) != EOF) {
474*7836SJohn.Forte@Sun.COM #endif
475*7836SJohn.Forte@Sun.COM 		switch (c) {
476*7836SJohn.Forte@Sun.COM 			case 'a':
477*7836SJohn.Forte@Sun.COM 				aflg++;
478*7836SJohn.Forte@Sun.COM 				strcpy(input_file, optarg);
479*7836SJohn.Forte@Sun.COM 				mustcommit++;
480*7836SJohn.Forte@Sun.COM 				action_counts++;
481*7836SJohn.Forte@Sun.COM 				break;
482*7836SJohn.Forte@Sun.COM 			case 'C':
483*7836SJohn.Forte@Sun.COM 				Cflg++;
484*7836SJohn.Forte@Sun.COM 				strcpy(logical_host, optarg);
485*7836SJohn.Forte@Sun.COM 				if (logical_host && *logical_host == '-')
486*7836SJohn.Forte@Sun.COM 				    if (argc == 3)
487*7836SJohn.Forte@Sun.COM 					action_counts++;
488*7836SJohn.Forte@Sun.COM 				break;
489*7836SJohn.Forte@Sun.COM 			case 'D':
490*7836SJohn.Forte@Sun.COM 				Dflg++;
491*7836SJohn.Forte@Sun.COM 				strcpy(device_group, optarg);
492*7836SJohn.Forte@Sun.COM 				action_counts++;
493*7836SJohn.Forte@Sun.COM 				break;
494*7836SJohn.Forte@Sun.COM 			case 'i':
495*7836SJohn.Forte@Sun.COM 				iflg++;
496*7836SJohn.Forte@Sun.COM 				mustcommit++;
497*7836SJohn.Forte@Sun.COM 				action_counts++;
498*7836SJohn.Forte@Sun.COM 				break;
499*7836SJohn.Forte@Sun.COM 			case 'l':
500*7836SJohn.Forte@Sun.COM 				lflg++;
501*7836SJohn.Forte@Sun.COM 				action_counts++;
502*7836SJohn.Forte@Sun.COM 				break;
503*7836SJohn.Forte@Sun.COM 			case 'L':
504*7836SJohn.Forte@Sun.COM 				Lflg++;
505*7836SJohn.Forte@Sun.COM 				action_counts++;
506*7836SJohn.Forte@Sun.COM 				break;
507*7836SJohn.Forte@Sun.COM 			case 'p':
508*7836SJohn.Forte@Sun.COM 				pflg++;
509*7836SJohn.Forte@Sun.COM 				strcpy(input_file, optarg);
510*7836SJohn.Forte@Sun.COM 				mustcommit++;
511*7836SJohn.Forte@Sun.COM 				break;
512*7836SJohn.Forte@Sun.COM 			case 's':
513*7836SJohn.Forte@Sun.COM 				sflg++;
514*7836SJohn.Forte@Sun.COM 				strcpy(config_file, optarg);
515*7836SJohn.Forte@Sun.COM 				action_counts++;
516*7836SJohn.Forte@Sun.COM 				break;
517*7836SJohn.Forte@Sun.COM 			case 'h':
518*7836SJohn.Forte@Sun.COM 				usage(NULL);
519*7836SJohn.Forte@Sun.COM 				exit(0);
520*7836SJohn.Forte@Sun.COM 			/*NOTREACHED*/
521*7836SJohn.Forte@Sun.COM #ifdef DEBUG
522*7836SJohn.Forte@Sun.COM 			case 'v':
523*7836SJohn.Forte@Sun.COM 				verbose++;
524*7836SJohn.Forte@Sun.COM 				action_counts++;
525*7836SJohn.Forte@Sun.COM 				break;
526*7836SJohn.Forte@Sun.COM #endif
527*7836SJohn.Forte@Sun.COM #ifdef UPGRADE
528*7836SJohn.Forte@Sun.COM 			case 'u':
529*7836SJohn.Forte@Sun.COM 				uflg++;
530*7836SJohn.Forte@Sun.COM 				action_counts++;
531*7836SJohn.Forte@Sun.COM 				break;
532*7836SJohn.Forte@Sun.COM #endif
533*7836SJohn.Forte@Sun.COM 
534*7836SJohn.Forte@Sun.COM 			case 'r':
535*7836SJohn.Forte@Sun.COM 				rflg++;
536*7836SJohn.Forte@Sun.COM 				strcpy(altroot, optarg);
537*7836SJohn.Forte@Sun.COM 				break;
538*7836SJohn.Forte@Sun.COM 
539*7836SJohn.Forte@Sun.COM 			case 'n':
540*7836SJohn.Forte@Sun.COM 				nflg++;
541*7836SJohn.Forte@Sun.COM 				break;
542*7836SJohn.Forte@Sun.COM 
543*7836SJohn.Forte@Sun.COM 			default:
544*7836SJohn.Forte@Sun.COM 				usage(NULL);
545*7836SJohn.Forte@Sun.COM 				exit(1);
546*7836SJohn.Forte@Sun.COM 				break;
547*7836SJohn.Forte@Sun.COM 		};
548*7836SJohn.Forte@Sun.COM 	}
549*7836SJohn.Forte@Sun.COM 
550*7836SJohn.Forte@Sun.COM 	switch (action_counts) {
551*7836SJohn.Forte@Sun.COM 	    case 0:
552*7836SJohn.Forte@Sun.COM 		if (argc > 1) {
553*7836SJohn.Forte@Sun.COM 		    if (pflg)
554*7836SJohn.Forte@Sun.COM 			usage(gettext(
555*7836SJohn.Forte@Sun.COM 			    "-p option must be used in conjunction with -i"));
556*7836SJohn.Forte@Sun.COM 		    else
557*7836SJohn.Forte@Sun.COM 			usage(gettext("must specify an action flag"));
558*7836SJohn.Forte@Sun.COM 		    exit(1);
559*7836SJohn.Forte@Sun.COM 		}
560*7836SJohn.Forte@Sun.COM 		break;
561*7836SJohn.Forte@Sun.COM 	    case 1:
562*7836SJohn.Forte@Sun.COM 		break;
563*7836SJohn.Forte@Sun.COM 	    case 2:
564*7836SJohn.Forte@Sun.COM 		if (lflg && sflg)
565*7836SJohn.Forte@Sun.COM 			break;
566*7836SJohn.Forte@Sun.COM 		else {
567*7836SJohn.Forte@Sun.COM 		    usage(gettext("too many action flags"));
568*7836SJohn.Forte@Sun.COM 		    exit(1);
569*7836SJohn.Forte@Sun.COM 		    break;
570*7836SJohn.Forte@Sun.COM 		}
571*7836SJohn.Forte@Sun.COM 	    default:
572*7836SJohn.Forte@Sun.COM 		usage(gettext("too many action flags"));
573*7836SJohn.Forte@Sun.COM 		exit(1);
574*7836SJohn.Forte@Sun.COM 		break;
575*7836SJohn.Forte@Sun.COM 	}
576*7836SJohn.Forte@Sun.COM 
577*7836SJohn.Forte@Sun.COM 	if (argc == 1 || (argc == 2 && verbose) || (argc == 3 && (rflg|Cflg)))
578*7836SJohn.Forte@Sun.COM 		noflags++;
579*7836SJohn.Forte@Sun.COM 
580*7836SJohn.Forte@Sun.COM 	if (Dflg) {
581*7836SJohn.Forte@Sun.COM 		/*
582*7836SJohn.Forte@Sun.COM 		 * Determine if the value specified is a device group
583*7836SJohn.Forte@Sun.COM 		 * that is active on this node
584*7836SJohn.Forte@Sun.COM 		 */
585*7836SJohn.Forte@Sun.COM 		char *other_node;
586*7836SJohn.Forte@Sun.COM 		if ((cfg_issuncluster() > 0) && (strlen(device_group) > 0)) {
587*7836SJohn.Forte@Sun.COM 		    local = cfg_dgname_islocal(device_group, &other_node);
588*7836SJohn.Forte@Sun.COM 		    if (local == 0)
589*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr, gettext(
590*7836SJohn.Forte@Sun.COM 			    "Device group %s active on %s\n"),
591*7836SJohn.Forte@Sun.COM 			    device_group, other_node);
592*7836SJohn.Forte@Sun.COM 		    else if (local == 1)
593*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr, gettext(
594*7836SJohn.Forte@Sun.COM 			    "Device group %s active on this node\n"),
595*7836SJohn.Forte@Sun.COM 			    device_group);
596*7836SJohn.Forte@Sun.COM 		    else
597*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr, gettext(
598*7836SJohn.Forte@Sun.COM 			    "Device group %s not found\n"), device_group);
599*7836SJohn.Forte@Sun.COM 		    return (local);
600*7836SJohn.Forte@Sun.COM 		} else {
601*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr, gettext(
602*7836SJohn.Forte@Sun.COM 			    "dscfg -D is only allowed in "
603*7836SJohn.Forte@Sun.COM 			    "Sun Cluster OE\n"));
604*7836SJohn.Forte@Sun.COM 			return (0);
605*7836SJohn.Forte@Sun.COM 		}
606*7836SJohn.Forte@Sun.COM 	}
607*7836SJohn.Forte@Sun.COM 
608*7836SJohn.Forte@Sun.COM 	if (sflg && !lflg) {
609*7836SJohn.Forte@Sun.COM 		/*
610*7836SJohn.Forte@Sun.COM 		 * Only allow setting location on a non-sun cluster system
611*7836SJohn.Forte@Sun.COM 		 * if the cluster reference file is already present.
612*7836SJohn.Forte@Sun.COM 		 */
613*7836SJohn.Forte@Sun.COM 		struct stat dscfg_stat = {0};
614*7836SJohn.Forte@Sun.COM 		if (cfg_issuncluster() <= 0) {
615*7836SJohn.Forte@Sun.COM 			if (stat(CFG_CLUSTER_LOCATION, &dscfg_stat) != 0) {
616*7836SJohn.Forte@Sun.COM 				if (dscfg_stat.st_blocks == 0) {
617*7836SJohn.Forte@Sun.COM 					(void) fprintf(stderr, gettext(
618*7836SJohn.Forte@Sun.COM 						"dscfg -s is only allowed in "
619*7836SJohn.Forte@Sun.COM 						"Sun Cluster OE\n"));
620*7836SJohn.Forte@Sun.COM 					exit(1);
621*7836SJohn.Forte@Sun.COM 				}
622*7836SJohn.Forte@Sun.COM 			}
623*7836SJohn.Forte@Sun.COM 		}
624*7836SJohn.Forte@Sun.COM 
625*7836SJohn.Forte@Sun.COM 		spcs_log("dscfg", NULL, gettext("dscfg -s %s"), config_file);
626*7836SJohn.Forte@Sun.COM 		locname = cfg_location(config_file, CFG_LOC_SET_CLUSTER,
627*7836SJohn.Forte@Sun.COM 		    rflg ? altroot : NULL);
628*7836SJohn.Forte@Sun.COM 		if (locname == NULL) {
629*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr, gettext("dscfg: %s\n"),
630*7836SJohn.Forte@Sun.COM 			    cfg_error(NULL));
631*7836SJohn.Forte@Sun.COM 			exit(1);
632*7836SJohn.Forte@Sun.COM 		} else
633*7836SJohn.Forte@Sun.COM 			exit(0);
634*7836SJohn.Forte@Sun.COM 
635*7836SJohn.Forte@Sun.COM 	} else if (sflg && lflg) {
636*7836SJohn.Forte@Sun.COM 		/* s used with l for temporarily peeking at a dscfg database */
637*7836SJohn.Forte@Sun.COM 		loc = config_file;
638*7836SJohn.Forte@Sun.COM 	} else {
639*7836SJohn.Forte@Sun.COM 		locname = cfg_location(NULL,
640*7836SJohn.Forte@Sun.COM 			Cflg ? CFG_LOC_GET_CLUSTER : CFG_LOC_GET_LOCAL,
641*7836SJohn.Forte@Sun.COM 			rflg ? altroot : NULL);
642*7836SJohn.Forte@Sun.COM 		if (Cflg && (locname == NULL)) {
643*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr, gettext(
644*7836SJohn.Forte@Sun.COM 			    "dscfg: cluster config not set: %s\n"),
645*7836SJohn.Forte@Sun.COM 			    cfg_error(NULL));
646*7836SJohn.Forte@Sun.COM 			return (1);
647*7836SJohn.Forte@Sun.COM 		}
648*7836SJohn.Forte@Sun.COM 		loc = rflg ? locname : NULL;
649*7836SJohn.Forte@Sun.COM 	}
650*7836SJohn.Forte@Sun.COM 
651*7836SJohn.Forte@Sun.COM 	/*
652*7836SJohn.Forte@Sun.COM 	 * the following hack forces the configuration file to initialize
653*7836SJohn.Forte@Sun.COM 	 */
654*7836SJohn.Forte@Sun.COM 	if (iflg && !pflg) {
655*7836SJohn.Forte@Sun.COM 		int fild;
656*7836SJohn.Forte@Sun.COM 		int c;
657*7836SJohn.Forte@Sun.COM 		char buf[CFG_MAX_BUF] = {0};
658*7836SJohn.Forte@Sun.COM 		cfp_t *cfp;
659*7836SJohn.Forte@Sun.COM 
660*7836SJohn.Forte@Sun.COM 		if (!nflg) {
661*7836SJohn.Forte@Sun.COM 			(void) printf(
662*7836SJohn.Forte@Sun.COM 			    gettext("WARNING: This option will erase your "
663*7836SJohn.Forte@Sun.COM 				"Availability Suite configuration\n"));
664*7836SJohn.Forte@Sun.COM 			(void) printf(
665*7836SJohn.Forte@Sun.COM 			    gettext("Do you want to continue? (Y/N) [N] "));
666*7836SJohn.Forte@Sun.COM 
667*7836SJohn.Forte@Sun.COM 			c = getchar();
668*7836SJohn.Forte@Sun.COM 			switch (c) {
669*7836SJohn.Forte@Sun.COM 			case 'y':
670*7836SJohn.Forte@Sun.COM 			case 'Y': break;
671*7836SJohn.Forte@Sun.COM 			case 'n':
672*7836SJohn.Forte@Sun.COM 			case 'N':
673*7836SJohn.Forte@Sun.COM 			case '\n':
674*7836SJohn.Forte@Sun.COM 				(void) fprintf(stderr, gettext(
675*7836SJohn.Forte@Sun.COM 				"dscfg: configuration not initialized\n"));
676*7836SJohn.Forte@Sun.COM 				exit(1);
677*7836SJohn.Forte@Sun.COM 			default:
678*7836SJohn.Forte@Sun.COM 				(void) fprintf(stderr, gettext(
679*7836SJohn.Forte@Sun.COM 				"dscfg: %d is not a valid response\n"), c);
680*7836SJohn.Forte@Sun.COM 				exit(1);
681*7836SJohn.Forte@Sun.COM 			}
682*7836SJohn.Forte@Sun.COM 		}
683*7836SJohn.Forte@Sun.COM 
684*7836SJohn.Forte@Sun.COM 		spcs_log("dscfg", NULL, gettext("dscfg -i"));
685*7836SJohn.Forte@Sun.COM 
686*7836SJohn.Forte@Sun.COM 		if ((cfg = cfg_open(loc)) == NULL) {
687*7836SJohn.Forte@Sun.COM 			/* this is not a good config, or non-existent so.. */
688*7836SJohn.Forte@Sun.COM 			if (!make_new_config(locname)) {
689*7836SJohn.Forte@Sun.COM 				(void) fprintf(stderr, gettext("dscfg: %s\n"),
690*7836SJohn.Forte@Sun.COM 				    cfg_error(NULL));
691*7836SJohn.Forte@Sun.COM 				exit(1);
692*7836SJohn.Forte@Sun.COM 			}
693*7836SJohn.Forte@Sun.COM 			if ((cfg = cfg_open(loc)) == NULL) {
694*7836SJohn.Forte@Sun.COM 				(void) fprintf(stderr, gettext("dscfg: %s\n"),
695*7836SJohn.Forte@Sun.COM 				    cfg_error(NULL));
696*7836SJohn.Forte@Sun.COM 				exit(1);
697*7836SJohn.Forte@Sun.COM 			}
698*7836SJohn.Forte@Sun.COM 		}
699*7836SJohn.Forte@Sun.COM 
700*7836SJohn.Forte@Sun.COM 		/*
701*7836SJohn.Forte@Sun.COM 		 * Set cluster node if specified
702*7836SJohn.Forte@Sun.COM 		 */
703*7836SJohn.Forte@Sun.COM 		if (Cflg)
704*7836SJohn.Forte@Sun.COM 			cfg_resource(cfg, logical_host);
705*7836SJohn.Forte@Sun.COM 
706*7836SJohn.Forte@Sun.COM 		if (cfg_is_cfg(cfg) != 1) {
707*7836SJohn.Forte@Sun.COM 			if (!make_new_config(locname)) {
708*7836SJohn.Forte@Sun.COM 				(void) fprintf(stderr, gettext("dscfg: unable "
709*7836SJohn.Forte@Sun.COM 				    " to create new config \n"));
710*7836SJohn.Forte@Sun.COM 				exit(1);
711*7836SJohn.Forte@Sun.COM 			}
712*7836SJohn.Forte@Sun.COM 		}
713*7836SJohn.Forte@Sun.COM 
714*7836SJohn.Forte@Sun.COM 		if (!cfg_lock(cfg, CFG_WRLOCK)) {
715*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr, gettext("dscfg: %s\n"),
716*7836SJohn.Forte@Sun.COM 			    cfg_error(NULL));
717*7836SJohn.Forte@Sun.COM 			exit(1);
718*7836SJohn.Forte@Sun.COM 		}
719*7836SJohn.Forte@Sun.COM 
720*7836SJohn.Forte@Sun.COM 		cfp = FP_SUN_CLUSTER(cfg);
721*7836SJohn.Forte@Sun.COM 		if ((fild = cfp->cf_fd) == 0) {
722*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr,
723*7836SJohn.Forte@Sun.COM 				gettext("dscfg: failure to access %s "
724*7836SJohn.Forte@Sun.COM 				"configuration database: %s\n"),
725*7836SJohn.Forte@Sun.COM 				(Cflg) ? gettext("cluster") : gettext("local"),
726*7836SJohn.Forte@Sun.COM 			cfg_error(NULL));
727*7836SJohn.Forte@Sun.COM 			exit(1);
728*7836SJohn.Forte@Sun.COM 		}
729*7836SJohn.Forte@Sun.COM 
730*7836SJohn.Forte@Sun.COM 		if (cfg_shldskip_vtoc(fild, locname) > 0)
731*7836SJohn.Forte@Sun.COM 			offset += CFG_VTOC_SKIP;
732*7836SJohn.Forte@Sun.COM 
733*7836SJohn.Forte@Sun.COM 		lseek(fild, offset, SEEK_SET);
734*7836SJohn.Forte@Sun.COM 		write(fild, buf, sizeof (buf));
735*7836SJohn.Forte@Sun.COM 		cfg_invalidate_hsizes(fild, locname);
736*7836SJohn.Forte@Sun.COM 
737*7836SJohn.Forte@Sun.COM 		cfg_close(cfg);
738*7836SJohn.Forte@Sun.COM 		exit(0);
739*7836SJohn.Forte@Sun.COM 	}
740*7836SJohn.Forte@Sun.COM 
741*7836SJohn.Forte@Sun.COM 	if (pflg && !iflg) {
742*7836SJohn.Forte@Sun.COM 		usage(gettext("-p option must be used in conjunction with -i"));
743*7836SJohn.Forte@Sun.COM 		exit(1);
744*7836SJohn.Forte@Sun.COM 
745*7836SJohn.Forte@Sun.COM 	}
746*7836SJohn.Forte@Sun.COM 
747*7836SJohn.Forte@Sun.COM 	if (uflg) {
748*7836SJohn.Forte@Sun.COM 		char cmd[CFG_MAX_BUF];
749*7836SJohn.Forte@Sun.COM 		if (rflg)
750*7836SJohn.Forte@Sun.COM 			(void) snprintf(cmd, sizeof (cmd),
751*7836SJohn.Forte@Sun.COM 			    "%s/usr/sbin/dscfg -r %s -l >"
752*7836SJohn.Forte@Sun.COM 			    " %s/var/tmp/.dscfg.bak", altroot,
753*7836SJohn.Forte@Sun.COM 			    altroot, altroot);
754*7836SJohn.Forte@Sun.COM 		else
755*7836SJohn.Forte@Sun.COM 			(void) snprintf(cmd, sizeof (cmd),
756*7836SJohn.Forte@Sun.COM 			    "/usr/sbin/dscfg -l >"
757*7836SJohn.Forte@Sun.COM 			    " /var/tmp/.dscfg.bak");
758*7836SJohn.Forte@Sun.COM 
759*7836SJohn.Forte@Sun.COM 		if (system(cmd) != 0) {
760*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr,
761*7836SJohn.Forte@Sun.COM 			    "dscfg: unable to create backup\n");
762*7836SJohn.Forte@Sun.COM 			exit(1);
763*7836SJohn.Forte@Sun.COM 		}
764*7836SJohn.Forte@Sun.COM 
765*7836SJohn.Forte@Sun.COM 		if ((cfg = cfg_open(loc)) == NULL) {
766*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr, gettext("dscfg: %s\n"),
767*7836SJohn.Forte@Sun.COM 			    cfg_error(NULL));
768*7836SJohn.Forte@Sun.COM 			exit(2);
769*7836SJohn.Forte@Sun.COM 		}
770*7836SJohn.Forte@Sun.COM 
771*7836SJohn.Forte@Sun.COM 		if (!cfg_lock(cfg, CFG_UPGRADE)) {
772*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr,
773*7836SJohn.Forte@Sun.COM 			    gettext("dscfg: upgrade failed\n"));
774*7836SJohn.Forte@Sun.COM 			cfg_close(cfg);
775*7836SJohn.Forte@Sun.COM 			exit(1);
776*7836SJohn.Forte@Sun.COM 		}
777*7836SJohn.Forte@Sun.COM 
778*7836SJohn.Forte@Sun.COM 		cfg_close(cfg);
779*7836SJohn.Forte@Sun.COM 		exit(0);
780*7836SJohn.Forte@Sun.COM 	}
781*7836SJohn.Forte@Sun.COM 
782*7836SJohn.Forte@Sun.COM 	if ((cfg = cfg_open(loc)) == NULL) {
783*7836SJohn.Forte@Sun.COM 		(void) fprintf(stderr, gettext("dscfg: %s\n"), cfg_error(NULL));
784*7836SJohn.Forte@Sun.COM 		exit(2);
785*7836SJohn.Forte@Sun.COM 	}
786*7836SJohn.Forte@Sun.COM 
787*7836SJohn.Forte@Sun.COM 	/*
788*7836SJohn.Forte@Sun.COM 	 * Set cluster node if specified
789*7836SJohn.Forte@Sun.COM 	 */
790*7836SJohn.Forte@Sun.COM 	if (Cflg)
791*7836SJohn.Forte@Sun.COM 		cfg_resource(cfg, logical_host);
792*7836SJohn.Forte@Sun.COM 
793*7836SJohn.Forte@Sun.COM 	if ((!pflg) && (!noflags)) {
794*7836SJohn.Forte@Sun.COM 		if (cfg_is_cfg(cfg) != 1) {
795*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr,
796*7836SJohn.Forte@Sun.COM 				gettext("dscfg: %s\n"), cfg_error(NULL));
797*7836SJohn.Forte@Sun.COM 			cfg_close(cfg);
798*7836SJohn.Forte@Sun.COM 			exit(1);
799*7836SJohn.Forte@Sun.COM 		}
800*7836SJohn.Forte@Sun.COM 	}
801*7836SJohn.Forte@Sun.COM 
802*7836SJohn.Forte@Sun.COM 	if (Lflg) {
803*7836SJohn.Forte@Sun.COM 		dump_lockstat(cfg);
804*7836SJohn.Forte@Sun.COM 		cfg_close(cfg);
805*7836SJohn.Forte@Sun.COM 		exit(0);
806*7836SJohn.Forte@Sun.COM 	}
807*7836SJohn.Forte@Sun.COM 
808*7836SJohn.Forte@Sun.COM 	if (noflags) {
809*7836SJohn.Forte@Sun.COM 		dump_status(cfg);
810*7836SJohn.Forte@Sun.COM 		cfg_close(cfg);
811*7836SJohn.Forte@Sun.COM 		exit(0);
812*7836SJohn.Forte@Sun.COM 	}
813*7836SJohn.Forte@Sun.COM 
814*7836SJohn.Forte@Sun.COM 	if (!cfg_lock(cfg, mustcommit? CFG_WRLOCK : CFG_RDLOCK)) {
815*7836SJohn.Forte@Sun.COM 		(void) fprintf(stderr, gettext("cfg_lock: lock failed\n"));
816*7836SJohn.Forte@Sun.COM 		cfg_close(cfg);
817*7836SJohn.Forte@Sun.COM 		exit(1);
818*7836SJohn.Forte@Sun.COM 	}
819*7836SJohn.Forte@Sun.COM 
820*7836SJohn.Forte@Sun.COM 	if (lflg) {
821*7836SJohn.Forte@Sun.COM 		print_config(cfg);
822*7836SJohn.Forte@Sun.COM 		cfg_close(cfg);
823*7836SJohn.Forte@Sun.COM 		exit(0);
824*7836SJohn.Forte@Sun.COM 	}
825*7836SJohn.Forte@Sun.COM 
826*7836SJohn.Forte@Sun.COM 	/*
827*7836SJohn.Forte@Sun.COM 	 * initialize configuration
828*7836SJohn.Forte@Sun.COM 	 */
829*7836SJohn.Forte@Sun.COM 	if (iflg) {
830*7836SJohn.Forte@Sun.COM 		spcs_log("dscfg", NULL, gettext("dscfg -i -p %s"), input_file);
831*7836SJohn.Forte@Sun.COM 
832*7836SJohn.Forte@Sun.COM 		if (!pflg) {
833*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr,
834*7836SJohn.Forte@Sun.COM 			    gettext("dscfg: cannot init without "
835*7836SJohn.Forte@Sun.COM 			    "parser configuration file\n"));
836*7836SJohn.Forte@Sun.COM 			cfg_close(cfg);
837*7836SJohn.Forte@Sun.COM 			exit(1);
838*7836SJohn.Forte@Sun.COM 		} else if (parse_parse_config(cfg) < 0) {
839*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr, gettext("dscfg: cannot load "
840*7836SJohn.Forte@Sun.COM 				    "parser configuration file\n"));
841*7836SJohn.Forte@Sun.COM 			cfg_close(cfg);
842*7836SJohn.Forte@Sun.COM 			exit(1);
843*7836SJohn.Forte@Sun.COM 		}
844*7836SJohn.Forte@Sun.COM 	}
845*7836SJohn.Forte@Sun.COM 
846*7836SJohn.Forte@Sun.COM 	/*
847*7836SJohn.Forte@Sun.COM 	 * read asci config file and write
848*7836SJohn.Forte@Sun.COM 	 */
849*7836SJohn.Forte@Sun.COM 	if (aflg) {
850*7836SJohn.Forte@Sun.COM 		spcs_log("dscfg", NULL, gettext("dscfg -a %s"), input_file);
851*7836SJohn.Forte@Sun.COM 		parse_text_config(cfg);
852*7836SJohn.Forte@Sun.COM 	}
853*7836SJohn.Forte@Sun.COM 
854*7836SJohn.Forte@Sun.COM 	if (mustcommit) {
855*7836SJohn.Forte@Sun.COM 		rc = cfg_commit(cfg);
856*7836SJohn.Forte@Sun.COM 		if (rc < 0) {
857*7836SJohn.Forte@Sun.COM 			int sev = 0;
858*7836SJohn.Forte@Sun.COM 			(void) fprintf(stderr, gettext("dscfg: %s\n"),
859*7836SJohn.Forte@Sun.COM 			    cfg_error(&sev));
860*7836SJohn.Forte@Sun.COM 			if (sev == CFG_EFATAL) {
861*7836SJohn.Forte@Sun.COM 				cfg_close(cfg);
862*7836SJohn.Forte@Sun.COM 				exit(2);
863*7836SJohn.Forte@Sun.COM 			}
864*7836SJohn.Forte@Sun.COM 		}
865*7836SJohn.Forte@Sun.COM 	}
866*7836SJohn.Forte@Sun.COM 
867*7836SJohn.Forte@Sun.COM 	cfg_close(cfg);
868*7836SJohn.Forte@Sun.COM 	return (0);
869*7836SJohn.Forte@Sun.COM }
870*7836SJohn.Forte@Sun.COM 
871*7836SJohn.Forte@Sun.COM static int
872*7836SJohn.Forte@Sun.COM check_cluster()
873*7836SJohn.Forte@Sun.COM {
874*7836SJohn.Forte@Sun.COM 	static int is_cluster = -1;
875*7836SJohn.Forte@Sun.COM 	int rc;
876*7836SJohn.Forte@Sun.COM 
877*7836SJohn.Forte@Sun.COM 	if (is_cluster != -1)
878*7836SJohn.Forte@Sun.COM 	    return (is_cluster);
879*7836SJohn.Forte@Sun.COM 	rc = cfg_iscluster();
880*7836SJohn.Forte@Sun.COM 	if (rc > 0) {
881*7836SJohn.Forte@Sun.COM 	    is_cluster = IS_CLUSTER;
882*7836SJohn.Forte@Sun.COM 	    return (is_cluster);
883*7836SJohn.Forte@Sun.COM 	} else if (rc == 0) {
884*7836SJohn.Forte@Sun.COM 	    is_cluster = IS_NOT_CLUSTER;
885*7836SJohn.Forte@Sun.COM 	    return (is_cluster);
886*7836SJohn.Forte@Sun.COM 	} else {
887*7836SJohn.Forte@Sun.COM 	    (void) fprintf(stderr,
888*7836SJohn.Forte@Sun.COM 		gettext("dscfg: unable to determin environment\n"));
889*7836SJohn.Forte@Sun.COM 	    /*NOTREACHED*/
890*7836SJohn.Forte@Sun.COM 	}
891*7836SJohn.Forte@Sun.COM 
892*7836SJohn.Forte@Sun.COM 	/* gcc */
893*7836SJohn.Forte@Sun.COM 	return (is_cluster);
894*7836SJohn.Forte@Sun.COM }
895