xref: /onnv-gate/usr/src/cmd/power/parse.c (revision 0)
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 2003 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 "pmconfig.h"
30*0Sstevel@tonic-gate #include <deflt.h>
31*0Sstevel@tonic-gate #include <pwd.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #ifdef sparc
34*0Sstevel@tonic-gate #include <libdevinfo.h>
35*0Sstevel@tonic-gate static char sf_cmt[] = "# Statefile\t\tPath\n";
36*0Sstevel@tonic-gate #endif
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate static char as_cmt[] =
39*0Sstevel@tonic-gate 	"# Auto-Shutdown\t\tIdle(min)\tStart/Finish(hh:mm)\tBehavior\n";
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate char **line_args;
42*0Sstevel@tonic-gate int lineno = 0;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * cpr and pm combined permission/update status
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate prmup_t cpr_status = { 0, OKUP, "cpr" };
48*0Sstevel@tonic-gate prmup_t pm_status  = { 0, OKUP, "pm" };
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /*
52*0Sstevel@tonic-gate  * For config file parsing to work correctly/efficiently, this table
53*0Sstevel@tonic-gate  * needs to be sorted by .keyword and any longer string like "device"
54*0Sstevel@tonic-gate  * must appear before a substring like "dev".
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate static cinfo_t conftab[] = {
57*0Sstevel@tonic-gate 	"autopm",		autopm,  &pm_status,	NULL,	2, 0, 1,
58*0Sstevel@tonic-gate 	"autoshutdown",		autosd,  &cpr_status,	as_cmt,	5, 0, 1,
59*0Sstevel@tonic-gate 	"device-dependency-property",
60*0Sstevel@tonic-gate 				ddprop,  &pm_status,	NULL,	3, 1, 1,
61*0Sstevel@tonic-gate 	"device-dependency",	devdep,  &pm_status,	NULL,	3, 1, 1,
62*0Sstevel@tonic-gate 	"device-thresholds",	devthr,  &pm_status,	NULL,	3, 1, 1,
63*0Sstevel@tonic-gate 	"diskreads",		dreads,  &cpr_status,	NULL,	2, 0, 1,
64*0Sstevel@tonic-gate 	"idlecheck",		idlechk, &cpr_status,	NULL,	2, 0, 0,
65*0Sstevel@tonic-gate 	"loadaverage",		loadavg, &cpr_status,	NULL,	2, 0, 1,
66*0Sstevel@tonic-gate 	"nfsreqs",		nfsreq,  &cpr_status,	NULL,	2, 0, 1,
67*0Sstevel@tonic-gate #ifdef  sparc
68*0Sstevel@tonic-gate 	"statefile",		sfpath,  &cpr_status,	sf_cmt,	2, 0, 0,
69*0Sstevel@tonic-gate #endif
70*0Sstevel@tonic-gate 	"system-threshold",	systhr,  &pm_status,	NULL,	2, 0, 1,
71*0Sstevel@tonic-gate 	"ttychars",		tchars,  &cpr_status,	NULL,	2, 0, 1,
72*0Sstevel@tonic-gate 	NULL,			NULL,	 NULL,		NULL,	0, 0, 0,
73*0Sstevel@tonic-gate };
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate /*
77*0Sstevel@tonic-gate  * Set cpr/pm permission from default file info.
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate static void
80*0Sstevel@tonic-gate set_perm(char *defstr, char *user, int *perm, int cons)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	char *dinfo, *tk;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	/*
85*0Sstevel@tonic-gate 	 * /etc/default/power entries are:
86*0Sstevel@tonic-gate 	 *	all			(all users + root)
87*0Sstevel@tonic-gate 	 *	-			(none + root)
88*0Sstevel@tonic-gate 	 *	<user1[, user2...>	(list users + root)
89*0Sstevel@tonic-gate 	 *	console-owner		(console onwer + root)
90*0Sstevel@tonic-gate 	 * Any error in reading/parsing the file limits the
91*0Sstevel@tonic-gate 	 * access requirement to root.
92*0Sstevel@tonic-gate 	 */
93*0Sstevel@tonic-gate 	dinfo = defread(defstr);
94*0Sstevel@tonic-gate 	mesg(MDEBUG, "set_perm: \"%s\", value \"%s\"\n",
95*0Sstevel@tonic-gate 	    defstr, dinfo ? dinfo : "NULL");
96*0Sstevel@tonic-gate 	if (dinfo == NULL)
97*0Sstevel@tonic-gate 		return;
98*0Sstevel@tonic-gate 	else if (strcmp(dinfo, "all") == 0)
99*0Sstevel@tonic-gate 		*perm = 1;
100*0Sstevel@tonic-gate 	else if (strcmp(dinfo, "console-owner") == 0)
101*0Sstevel@tonic-gate 		*perm = cons;
102*0Sstevel@tonic-gate 	else if (user != NULL &&
103*0Sstevel@tonic-gate 	    (*dinfo == '<') && (tk = strrchr(++dinfo, '>'))) {
104*0Sstevel@tonic-gate 		/* Scan dinfo for a matching user. */
105*0Sstevel@tonic-gate 		for (*tk = '\0'; tk = strtok(dinfo, ", "); dinfo = NULL) {
106*0Sstevel@tonic-gate 			mesg(MDEBUG, "match_user: cmp (\"%s\", \"%s\")\n",
107*0Sstevel@tonic-gate 			    tk, user);
108*0Sstevel@tonic-gate 			if (strcmp(tk, user) == 0) {
109*0Sstevel@tonic-gate 				*perm = 1;
110*0Sstevel@tonic-gate 				break;
111*0Sstevel@tonic-gate 			}
112*0Sstevel@tonic-gate 		}
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate /*
118*0Sstevel@tonic-gate  * Lookup cpr/pm user permissions in "/etc/default/power".
119*0Sstevel@tonic-gate  */
120*0Sstevel@tonic-gate void
121*0Sstevel@tonic-gate lookup_perms(void)
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate 	struct passwd *pent;
124*0Sstevel@tonic-gate 	struct stat stbuf;
125*0Sstevel@tonic-gate 	int cons_perm;
126*0Sstevel@tonic-gate 	char *user;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	if ((ruid = getuid()) == 0) {
129*0Sstevel@tonic-gate 		cpr_status.perm = pm_status.perm = 1;
130*0Sstevel@tonic-gate 		return;
131*0Sstevel@tonic-gate 	} else if ((pent = getpwuid(ruid)) != NULL) {
132*0Sstevel@tonic-gate 		user = pent->pw_name;
133*0Sstevel@tonic-gate 	} else {
134*0Sstevel@tonic-gate 		user = NULL;
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	if (defopen("/etc/default/power") == -1)
138*0Sstevel@tonic-gate 		return;
139*0Sstevel@tonic-gate 	if (stat("/dev/console", &stbuf) == -1)
140*0Sstevel@tonic-gate 		cons_perm = 0;
141*0Sstevel@tonic-gate 	else
142*0Sstevel@tonic-gate 		cons_perm = (ruid == stbuf.st_uid);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	set_perm("PMCHANGEPERM=", user, &pm_status.perm, cons_perm);
145*0Sstevel@tonic-gate 	set_perm("CPRCHANGEPERM=", user, &cpr_status.perm, cons_perm);
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	(void) defopen(NULL);
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate #ifdef sparc
152*0Sstevel@tonic-gate /*
153*0Sstevel@tonic-gate  * Lookup energystar-v[23] property and set estar_vers.
154*0Sstevel@tonic-gate  */
155*0Sstevel@tonic-gate void
156*0Sstevel@tonic-gate lookup_estar_vers(void)
157*0Sstevel@tonic-gate {
158*0Sstevel@tonic-gate 	char es_prop[] = "energystar-v?", *fmt = "%s init/access error\n";
159*0Sstevel@tonic-gate 	di_prom_handle_t ph;
160*0Sstevel@tonic-gate 	di_node_t node;
161*0Sstevel@tonic-gate 	uchar_t *prop_data;
162*0Sstevel@tonic-gate 	int last;
163*0Sstevel@tonic-gate 	char ch;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	if ((node = di_init("/", DINFOPROP)) == DI_NODE_NIL) {
166*0Sstevel@tonic-gate 		mesg(MERR, fmt, "di_init");
167*0Sstevel@tonic-gate 		return;
168*0Sstevel@tonic-gate 	} else if ((ph = di_prom_init()) == DI_PROM_HANDLE_NIL) {
169*0Sstevel@tonic-gate 		mesg(MERR, fmt, "di_prom_init");
170*0Sstevel@tonic-gate 		di_fini(node);
171*0Sstevel@tonic-gate 		return;
172*0Sstevel@tonic-gate 	}
173*0Sstevel@tonic-gate 	last = strlen(es_prop) - 1;
174*0Sstevel@tonic-gate 	for (ch = ESTAR_V2; ch <= ESTAR_V3; ch++) {
175*0Sstevel@tonic-gate 		es_prop[last] = ch;
176*0Sstevel@tonic-gate 		if (di_prom_prop_lookup_bytes(ph, node,
177*0Sstevel@tonic-gate 		    es_prop, &prop_data) == 0) {
178*0Sstevel@tonic-gate 			mesg(MDEBUG, "get_estar_vers: %s prop found\n",
179*0Sstevel@tonic-gate 			    es_prop);
180*0Sstevel@tonic-gate 			estar_vers = ch;
181*0Sstevel@tonic-gate 			break;
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 	di_prom_fini(ph);
185*0Sstevel@tonic-gate 	di_fini(node);
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate #endif /* sparc */
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate /*
191*0Sstevel@tonic-gate  * limit open() to the real user
192*0Sstevel@tonic-gate  */
193*0Sstevel@tonic-gate static int
194*0Sstevel@tonic-gate pmc_open(char *name, int oflag)
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate 	uid_t euid;
197*0Sstevel@tonic-gate 	int fd;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	euid = geteuid();
200*0Sstevel@tonic-gate 	if (seteuid(ruid) == -1)
201*0Sstevel@tonic-gate 		mesg(MEXIT, "cannot reset euid to %d, %s\n",
202*0Sstevel@tonic-gate 		    ruid, strerror(errno));
203*0Sstevel@tonic-gate 	fd = open(name, oflag);
204*0Sstevel@tonic-gate 	(void) seteuid(euid);
205*0Sstevel@tonic-gate 	return (fd);
206*0Sstevel@tonic-gate }
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate /*
210*0Sstevel@tonic-gate  * Alloc space and read a config file; caller needs to free the space.
211*0Sstevel@tonic-gate  */
212*0Sstevel@tonic-gate static char *
213*0Sstevel@tonic-gate get_conf_data(char *name)
214*0Sstevel@tonic-gate {
215*0Sstevel@tonic-gate 	struct stat stbuf;
216*0Sstevel@tonic-gate 	ssize_t nread;
217*0Sstevel@tonic-gate 	size_t size;
218*0Sstevel@tonic-gate 	char *buf;
219*0Sstevel@tonic-gate 	int fd;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	if ((fd = pmc_open(name, O_RDONLY)) == -1)
222*0Sstevel@tonic-gate 		mesg(MEXIT, "cannot open %s\n", name);
223*0Sstevel@tonic-gate 	else if (fstat(fd, &stbuf) == -1)
224*0Sstevel@tonic-gate 		mesg(MEXIT, "cannot stat %s\n", name);
225*0Sstevel@tonic-gate 	size = (size_t)stbuf.st_size;
226*0Sstevel@tonic-gate 	def_src = (stbuf.st_ino == def_info.st_ino &&
227*0Sstevel@tonic-gate 	    stbuf.st_dev == def_info.st_dev);
228*0Sstevel@tonic-gate 	if ((buf = malloc(size + 1)) == NULL)
229*0Sstevel@tonic-gate 		mesg(MEXIT, "cannot allocate %u for \"%s\"\n", size + 1, name);
230*0Sstevel@tonic-gate 	nread = read(fd, buf, size);
231*0Sstevel@tonic-gate 	(void) close(fd);
232*0Sstevel@tonic-gate 	if (nread != (ssize_t)size)
233*0Sstevel@tonic-gate 		mesg(MEXIT, "read error, expect %u, got %d, file \"%s\"\n",
234*0Sstevel@tonic-gate 		    size, nread, name);
235*0Sstevel@tonic-gate 	*(buf + size) = '\0';
236*0Sstevel@tonic-gate 	return (buf);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate /*
241*0Sstevel@tonic-gate  * Add an arg to line_args, adding space if needed.
242*0Sstevel@tonic-gate  */
243*0Sstevel@tonic-gate static void
244*0Sstevel@tonic-gate newarg(char *arg, int index)
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate 	static int alcnt;
247*0Sstevel@tonic-gate 	size_t size;
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	if ((index + 1) > alcnt) {
250*0Sstevel@tonic-gate 		alcnt += 4;
251*0Sstevel@tonic-gate 		size = alcnt * sizeof (*line_args);
252*0Sstevel@tonic-gate 		if ((line_args = realloc(line_args, size)) == NULL)
253*0Sstevel@tonic-gate 			mesg(MEXIT, "cannot alloc %u for line args\n", size);
254*0Sstevel@tonic-gate 	}
255*0Sstevel@tonic-gate 	*(line_args + index) = arg;
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate /*
260*0Sstevel@tonic-gate  * Convert blank-delimited words into an arg vector and return
261*0Sstevel@tonic-gate  * the arg count; character strings get null-terminated in place.
262*0Sstevel@tonic-gate  */
263*0Sstevel@tonic-gate static int
264*0Sstevel@tonic-gate build_args(char *cline, char *tail)
265*0Sstevel@tonic-gate {
266*0Sstevel@tonic-gate 	extern int debug;
267*0Sstevel@tonic-gate 	char **vec, *arg, *cp;
268*0Sstevel@tonic-gate 	int cnt = 0;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	/*
271*0Sstevel@tonic-gate 	 * Search logic: look for "\\\n" as a continuation marker,
272*0Sstevel@tonic-gate 	 * treat any other "\\*" as ordinary arg data, scan until a
273*0Sstevel@tonic-gate 	 * white-space delimiter is found, and if the arg has length,
274*0Sstevel@tonic-gate 	 * null-terminate and save arg to line_args.  The scan includes
275*0Sstevel@tonic-gate 	 * tail so the last arg is found without any special-case code.
276*0Sstevel@tonic-gate 	 */
277*0Sstevel@tonic-gate 	for (arg = cp = cline; cp <= tail; cp++) {
278*0Sstevel@tonic-gate 		if (*cp == '\\') {
279*0Sstevel@tonic-gate 			if (*(cp + 1) && *(cp + 1) != '\n') {
280*0Sstevel@tonic-gate 				cp++;
281*0Sstevel@tonic-gate 				continue;
282*0Sstevel@tonic-gate 			}
283*0Sstevel@tonic-gate 		} else if (strchr(" \t\n", *cp) == NULL)
284*0Sstevel@tonic-gate 			continue;
285*0Sstevel@tonic-gate 		if (cp - arg) {
286*0Sstevel@tonic-gate 			*cp = '\0';
287*0Sstevel@tonic-gate 			newarg(arg, cnt++);
288*0Sstevel@tonic-gate 		}
289*0Sstevel@tonic-gate 		arg = cp + 1;
290*0Sstevel@tonic-gate 	}
291*0Sstevel@tonic-gate 	newarg(NULL, cnt);
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	if (debug) {
294*0Sstevel@tonic-gate 		mesg(MDEBUG, "\nline %d, found %d args:\n", lineno, cnt);
295*0Sstevel@tonic-gate 		for (vec = line_args; *vec; vec++)
296*0Sstevel@tonic-gate 			mesg(MDEBUG, "    \"%s\"\n", *vec);
297*0Sstevel@tonic-gate 	}
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	return (cnt);
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate /*
304*0Sstevel@tonic-gate  * Match leading keyword from a conf line and
305*0Sstevel@tonic-gate  * return a reference to a config info struct.
306*0Sstevel@tonic-gate  */
307*0Sstevel@tonic-gate static cinfo_t *
308*0Sstevel@tonic-gate get_cinfo(void)
309*0Sstevel@tonic-gate {
310*0Sstevel@tonic-gate 	cinfo_t *cip, *info = NULL;
311*0Sstevel@tonic-gate 	char *keyword;
312*0Sstevel@tonic-gate 	int chr_diff;
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	/*
315*0Sstevel@tonic-gate 	 * Scan the config table for a matching keyword; since the table
316*0Sstevel@tonic-gate 	 * is sorted by keyword strings, a few optimizations can be done:
317*0Sstevel@tonic-gate 	 * first compare only the first byte of the keyword, skip any
318*0Sstevel@tonic-gate 	 * table string that starts with a lower ASCII value, compare the
319*0Sstevel@tonic-gate 	 * full string only when the first byte matches, and stop checking
320*0Sstevel@tonic-gate 	 * if the table string starts with a higher ASCII value.
321*0Sstevel@tonic-gate 	 */
322*0Sstevel@tonic-gate 	keyword = LINEARG(0);
323*0Sstevel@tonic-gate 	for (cip = conftab; cip->keyword; cip++) {
324*0Sstevel@tonic-gate 		chr_diff = (int)(*cip->keyword - *keyword);
325*0Sstevel@tonic-gate #if 0
326*0Sstevel@tonic-gate 		mesg(MDEBUG, "line %d, ('%c' - '%c') = %d\n",
327*0Sstevel@tonic-gate 		    lineno, *cip->keyword, *line, chr_diff);
328*0Sstevel@tonic-gate #endif
329*0Sstevel@tonic-gate 		if (chr_diff < 0)
330*0Sstevel@tonic-gate 			continue;
331*0Sstevel@tonic-gate 		else if (chr_diff == 0) {
332*0Sstevel@tonic-gate 			if (strcmp(keyword, cip->keyword) == 0) {
333*0Sstevel@tonic-gate 				info = cip;
334*0Sstevel@tonic-gate 				break;
335*0Sstevel@tonic-gate 			}
336*0Sstevel@tonic-gate 		} else
337*0Sstevel@tonic-gate 			break;
338*0Sstevel@tonic-gate 	}
339*0Sstevel@tonic-gate 	return (info);
340*0Sstevel@tonic-gate }
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate /*
344*0Sstevel@tonic-gate  * Find the end of a [possibly continued] conf line
345*0Sstevel@tonic-gate  * and record the real/lf-delimited line count at *lcnt.
346*0Sstevel@tonic-gate  */
347*0Sstevel@tonic-gate static char *
348*0Sstevel@tonic-gate find_line_end(char *line, int *lcnt)
349*0Sstevel@tonic-gate {
350*0Sstevel@tonic-gate 	char *next, *lf;
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	*lcnt = 0;
353*0Sstevel@tonic-gate 	next = line;
354*0Sstevel@tonic-gate 	while (lf = strchr(next, '\n')) {
355*0Sstevel@tonic-gate 		(*lcnt)++;
356*0Sstevel@tonic-gate 		if (lf == line || (*(lf - 1) != '\\') || *(lf + 1) == '\0')
357*0Sstevel@tonic-gate 			break;
358*0Sstevel@tonic-gate 		next = lf + 1;
359*0Sstevel@tonic-gate 	}
360*0Sstevel@tonic-gate 	return (lf);
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate /*
365*0Sstevel@tonic-gate  * Parse the named conf file and for each conf line
366*0Sstevel@tonic-gate  * call the action routine or conftab handler routine.
367*0Sstevel@tonic-gate  */
368*0Sstevel@tonic-gate void
369*0Sstevel@tonic-gate parse_conf_file(char *name, vact_t action)
370*0Sstevel@tonic-gate {
371*0Sstevel@tonic-gate 	char *file_buf, *cline, *line, *lend;
372*0Sstevel@tonic-gate 	cinfo_t *cip;
373*0Sstevel@tonic-gate 	int linc, cnt;
374*0Sstevel@tonic-gate 	size_t llen;
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	file_buf = get_conf_data(name);
377*0Sstevel@tonic-gate 	mesg(MDEBUG, "\nnow parsing \"%s\"...\n", name);
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	lineno = 1;
380*0Sstevel@tonic-gate 	line = file_buf;
381*0Sstevel@tonic-gate 	while (lend = find_line_end(line, &linc)) {
382*0Sstevel@tonic-gate 		/*
383*0Sstevel@tonic-gate 		 * Each line should start with valid data
384*0Sstevel@tonic-gate 		 * but leading white-space can be ignored
385*0Sstevel@tonic-gate 		 */
386*0Sstevel@tonic-gate 		while (line < lend) {
387*0Sstevel@tonic-gate 			if (*line != ' ' && *line != '\t')
388*0Sstevel@tonic-gate 				break;
389*0Sstevel@tonic-gate 			line++;
390*0Sstevel@tonic-gate 		}
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 		/*
393*0Sstevel@tonic-gate 		 * Copy line into allocated space and null-terminate
394*0Sstevel@tonic-gate 		 * without the trailing line-feed.
395*0Sstevel@tonic-gate 		 */
396*0Sstevel@tonic-gate 		if ((llen = (lend - line)) != 0) {
397*0Sstevel@tonic-gate 			if ((cline = malloc(llen + 1)) == NULL)
398*0Sstevel@tonic-gate 				mesg(MEXIT, "cannot alloc %u bytes "
399*0Sstevel@tonic-gate 				    "for line copy\n", llen);
400*0Sstevel@tonic-gate 			(void) memcpy(cline, line, llen);
401*0Sstevel@tonic-gate 			*(cline + llen) = '\0';
402*0Sstevel@tonic-gate 		} else
403*0Sstevel@tonic-gate 			cline = NULL;
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 		/*
406*0Sstevel@tonic-gate 		 * For blank and comment lines: possibly show a debug
407*0Sstevel@tonic-gate 		 * message and otherwise ignore them.  For other lines:
408*0Sstevel@tonic-gate 		 * parse into an arg vector and try to match the first
409*0Sstevel@tonic-gate 		 * arg with conftab keywords.  When a match is found,
410*0Sstevel@tonic-gate 		 * check for exact or minimum arg count, and call the
411*0Sstevel@tonic-gate 		 * action or handler routine; if handler does not return
412*0Sstevel@tonic-gate 		 * OKUP, set the referenced update value to NOUP so that
413*0Sstevel@tonic-gate 		 * later CPR or PM updates are skipped.
414*0Sstevel@tonic-gate 		 */
415*0Sstevel@tonic-gate 		if (llen == 0)
416*0Sstevel@tonic-gate 			mesg(MDEBUG, "\nline %d, blank...\n", lineno);
417*0Sstevel@tonic-gate 		else if (*line == '#')
418*0Sstevel@tonic-gate 			mesg(MDEBUG, "\nline %d, comment...\n", lineno);
419*0Sstevel@tonic-gate 		else if (cnt = build_args(cline, cline + llen)) {
420*0Sstevel@tonic-gate 			if ((cip = get_cinfo()) == NULL) {
421*0Sstevel@tonic-gate 				mesg(MEXIT, "unrecognized keyword \"%s\"\n",
422*0Sstevel@tonic-gate 				    LINEARG(0));
423*0Sstevel@tonic-gate 			} else if (cnt != cip->argc &&
424*0Sstevel@tonic-gate 			    (cip->any == 0 || cnt < cip->argc)) {
425*0Sstevel@tonic-gate 				mesg(MEXIT, "found %d args, expect %d%s\n",
426*0Sstevel@tonic-gate 				    cnt, cip->argc, cip->any ? "+" : "");
427*0Sstevel@tonic-gate 			} else if (action)
428*0Sstevel@tonic-gate 				(*action)(line, llen + 1, cip);
429*0Sstevel@tonic-gate 			else if (cip->status->perm && (def_src || cip->alt)) {
430*0Sstevel@tonic-gate 				if ((*cip->handler)() != OKUP)
431*0Sstevel@tonic-gate 					cip->status->update = NOUP;
432*0Sstevel@tonic-gate 			} else {
433*0Sstevel@tonic-gate 				mesg(MDEBUG,
434*0Sstevel@tonic-gate 				    "==> handler skipped: %s_perm %d, "
435*0Sstevel@tonic-gate 				    "def_src %d, alt %d\n", cip->status->set,
436*0Sstevel@tonic-gate 				    cip->status->perm, def_src, cip->alt);
437*0Sstevel@tonic-gate 			}
438*0Sstevel@tonic-gate 		}
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 		if (cline)
441*0Sstevel@tonic-gate 			free(cline);
442*0Sstevel@tonic-gate 		line = lend + 1;
443*0Sstevel@tonic-gate 		lineno += linc;
444*0Sstevel@tonic-gate 	}
445*0Sstevel@tonic-gate 	lineno = 0;
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	free(file_buf);
448*0Sstevel@tonic-gate }
449