xref: /onnv-gate/usr/src/cmd/power/parse.c (revision 9057:8c24c292edab)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53028Smh27603  * Common Development and Distribution License (the "License").
63028Smh27603  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228906SEric.Saxe@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include "pmconfig.h"
270Sstevel@tonic-gate #include <deflt.h>
280Sstevel@tonic-gate #include <pwd.h>
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #ifdef sparc
310Sstevel@tonic-gate #include <libdevinfo.h>
320Sstevel@tonic-gate static char sf_cmt[] = "# Statefile\t\tPath\n";
330Sstevel@tonic-gate #endif
340Sstevel@tonic-gate 
350Sstevel@tonic-gate static char as_cmt[] =
360Sstevel@tonic-gate 	"# Auto-Shutdown\t\tIdle(min)\tStart/Finish(hh:mm)\tBehavior\n";
370Sstevel@tonic-gate 
380Sstevel@tonic-gate char **line_args;
390Sstevel@tonic-gate int lineno = 0;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * cpr and pm combined permission/update status
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate prmup_t cpr_status = { 0, OKUP, "cpr" };
450Sstevel@tonic-gate prmup_t pm_status  = { 0, OKUP, "pm" };
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * For config file parsing to work correctly/efficiently, this table
500Sstevel@tonic-gate  * needs to be sorted by .keyword and any longer string like "device"
510Sstevel@tonic-gate  * must appear before a substring like "dev".
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate static cinfo_t conftab[] = {
545295Srandyf 	"S3-support",		S3sup,   &pm_status,	NULL,	2, 0, 1,
555295Srandyf 	"autoS3",		autoS3,  &pm_status,	NULL,	2, 0, 1,
560Sstevel@tonic-gate 	"autopm",		autopm,  &pm_status,	NULL,	2, 0, 1,
570Sstevel@tonic-gate 	"autoshutdown",		autosd,  &cpr_status,	as_cmt,	5, 0, 1,
583028Smh27603 	"cpu-threshold",	cputhr,  &pm_status,	NULL,	2, 0, 1,
598906SEric.Saxe@Sun.COM 	"cpu_deep_idle",	cpuidle, &pm_status,	NULL,	2, 0, 1,
608906SEric.Saxe@Sun.COM 	"cpupm",		cpupm,   &pm_status,	NULL,	2, 1, 1,
610Sstevel@tonic-gate 	"device-dependency-property",
620Sstevel@tonic-gate 				ddprop,  &pm_status,	NULL,	3, 1, 1,
630Sstevel@tonic-gate 	"device-dependency",	devdep,  &pm_status,	NULL,	3, 1, 1,
640Sstevel@tonic-gate 	"device-thresholds",	devthr,  &pm_status,	NULL,	3, 1, 1,
650Sstevel@tonic-gate 	"diskreads",		dreads,  &cpr_status,	NULL,	2, 0, 1,
660Sstevel@tonic-gate 	"idlecheck",		idlechk, &cpr_status,	NULL,	2, 0, 0,
670Sstevel@tonic-gate 	"loadaverage",		loadavg, &cpr_status,	NULL,	2, 0, 1,
680Sstevel@tonic-gate 	"nfsreqs",		nfsreq,  &cpr_status,	NULL,	2, 0, 1,
690Sstevel@tonic-gate #ifdef  sparc
700Sstevel@tonic-gate 	"statefile",		sfpath,  &cpr_status,	sf_cmt,	2, 0, 0,
710Sstevel@tonic-gate #endif
720Sstevel@tonic-gate 	"system-threshold",	systhr,  &pm_status,	NULL,	2, 0, 1,
730Sstevel@tonic-gate 	"ttychars",		tchars,  &cpr_status,	NULL,	2, 0, 1,
740Sstevel@tonic-gate 	NULL,			NULL,	 NULL,		NULL,	0, 0, 0,
750Sstevel@tonic-gate };
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * Set cpr/pm permission from default file info.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate static void
set_perm(char * defstr,char * user,int * perm,int cons)820Sstevel@tonic-gate set_perm(char *defstr, char *user, int *perm, int cons)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	char *dinfo, *tk;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * /etc/default/power entries are:
880Sstevel@tonic-gate 	 *	all			(all users + root)
890Sstevel@tonic-gate 	 *	-			(none + root)
900Sstevel@tonic-gate 	 *	<user1[, user2...>	(list users + root)
910Sstevel@tonic-gate 	 *	console-owner		(console onwer + root)
920Sstevel@tonic-gate 	 * Any error in reading/parsing the file limits the
930Sstevel@tonic-gate 	 * access requirement to root.
940Sstevel@tonic-gate 	 */
950Sstevel@tonic-gate 	dinfo = defread(defstr);
960Sstevel@tonic-gate 	mesg(MDEBUG, "set_perm: \"%s\", value \"%s\"\n",
970Sstevel@tonic-gate 	    defstr, dinfo ? dinfo : "NULL");
980Sstevel@tonic-gate 	if (dinfo == NULL)
990Sstevel@tonic-gate 		return;
1000Sstevel@tonic-gate 	else if (strcmp(dinfo, "all") == 0)
1010Sstevel@tonic-gate 		*perm = 1;
1020Sstevel@tonic-gate 	else if (strcmp(dinfo, "console-owner") == 0)
1030Sstevel@tonic-gate 		*perm = cons;
1040Sstevel@tonic-gate 	else if (user != NULL &&
1050Sstevel@tonic-gate 	    (*dinfo == '<') && (tk = strrchr(++dinfo, '>'))) {
1060Sstevel@tonic-gate 		/* Scan dinfo for a matching user. */
1070Sstevel@tonic-gate 		for (*tk = '\0'; tk = strtok(dinfo, ", "); dinfo = NULL) {
1080Sstevel@tonic-gate 			mesg(MDEBUG, "match_user: cmp (\"%s\", \"%s\")\n",
1090Sstevel@tonic-gate 			    tk, user);
1100Sstevel@tonic-gate 			if (strcmp(tk, user) == 0) {
1110Sstevel@tonic-gate 				*perm = 1;
1120Sstevel@tonic-gate 				break;
1130Sstevel@tonic-gate 			}
1140Sstevel@tonic-gate 		}
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * Lookup cpr/pm user permissions in "/etc/default/power".
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate void
lookup_perms(void)1230Sstevel@tonic-gate lookup_perms(void)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	struct passwd *pent;
1260Sstevel@tonic-gate 	struct stat stbuf;
1270Sstevel@tonic-gate 	int cons_perm;
1280Sstevel@tonic-gate 	char *user;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	if ((ruid = getuid()) == 0) {
1310Sstevel@tonic-gate 		cpr_status.perm = pm_status.perm = 1;
1320Sstevel@tonic-gate 		return;
1330Sstevel@tonic-gate 	} else if ((pent = getpwuid(ruid)) != NULL) {
1340Sstevel@tonic-gate 		user = pent->pw_name;
1350Sstevel@tonic-gate 	} else {
1360Sstevel@tonic-gate 		user = NULL;
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if (defopen("/etc/default/power") == -1)
1400Sstevel@tonic-gate 		return;
1410Sstevel@tonic-gate 	if (stat("/dev/console", &stbuf) == -1)
1420Sstevel@tonic-gate 		cons_perm = 0;
1430Sstevel@tonic-gate 	else
1440Sstevel@tonic-gate 		cons_perm = (ruid == stbuf.st_uid);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	set_perm("PMCHANGEPERM=", user, &pm_status.perm, cons_perm);
1470Sstevel@tonic-gate 	set_perm("CPRCHANGEPERM=", user, &cpr_status.perm, cons_perm);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	(void) defopen(NULL);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate #ifdef sparc
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  * Lookup energystar-v[23] property and set estar_vers.
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate void
lookup_estar_vers(void)1580Sstevel@tonic-gate lookup_estar_vers(void)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	char es_prop[] = "energystar-v?", *fmt = "%s init/access error\n";
1610Sstevel@tonic-gate 	di_prom_handle_t ph;
1620Sstevel@tonic-gate 	di_node_t node;
1630Sstevel@tonic-gate 	uchar_t *prop_data;
1640Sstevel@tonic-gate 	int last;
1650Sstevel@tonic-gate 	char ch;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if ((node = di_init("/", DINFOPROP)) == DI_NODE_NIL) {
1680Sstevel@tonic-gate 		mesg(MERR, fmt, "di_init");
1690Sstevel@tonic-gate 		return;
1700Sstevel@tonic-gate 	} else if ((ph = di_prom_init()) == DI_PROM_HANDLE_NIL) {
1710Sstevel@tonic-gate 		mesg(MERR, fmt, "di_prom_init");
1720Sstevel@tonic-gate 		di_fini(node);
1730Sstevel@tonic-gate 		return;
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 	last = strlen(es_prop) - 1;
1760Sstevel@tonic-gate 	for (ch = ESTAR_V2; ch <= ESTAR_V3; ch++) {
1770Sstevel@tonic-gate 		es_prop[last] = ch;
1780Sstevel@tonic-gate 		if (di_prom_prop_lookup_bytes(ph, node,
1790Sstevel@tonic-gate 		    es_prop, &prop_data) == 0) {
1800Sstevel@tonic-gate 			mesg(MDEBUG, "get_estar_vers: %s prop found\n",
1810Sstevel@tonic-gate 			    es_prop);
1820Sstevel@tonic-gate 			estar_vers = ch;
1830Sstevel@tonic-gate 			break;
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 	di_prom_fini(ph);
1870Sstevel@tonic-gate 	di_fini(node);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate #endif /* sparc */
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate  * limit open() to the real user
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate static int
pmc_open(char * name,int oflag)1960Sstevel@tonic-gate pmc_open(char *name, int oflag)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	uid_t euid;
1990Sstevel@tonic-gate 	int fd;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	euid = geteuid();
2020Sstevel@tonic-gate 	if (seteuid(ruid) == -1)
2030Sstevel@tonic-gate 		mesg(MEXIT, "cannot reset euid to %d, %s\n",
2040Sstevel@tonic-gate 		    ruid, strerror(errno));
2050Sstevel@tonic-gate 	fd = open(name, oflag);
2060Sstevel@tonic-gate 	(void) seteuid(euid);
2070Sstevel@tonic-gate 	return (fd);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * Alloc space and read a config file; caller needs to free the space.
2130Sstevel@tonic-gate  */
2140Sstevel@tonic-gate static char *
get_conf_data(char * name)2150Sstevel@tonic-gate get_conf_data(char *name)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	struct stat stbuf;
2180Sstevel@tonic-gate 	ssize_t nread;
2190Sstevel@tonic-gate 	size_t size;
2200Sstevel@tonic-gate 	char *buf;
2210Sstevel@tonic-gate 	int fd;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if ((fd = pmc_open(name, O_RDONLY)) == -1)
2240Sstevel@tonic-gate 		mesg(MEXIT, "cannot open %s\n", name);
2250Sstevel@tonic-gate 	else if (fstat(fd, &stbuf) == -1)
2260Sstevel@tonic-gate 		mesg(MEXIT, "cannot stat %s\n", name);
2270Sstevel@tonic-gate 	size = (size_t)stbuf.st_size;
2280Sstevel@tonic-gate 	def_src = (stbuf.st_ino == def_info.st_ino &&
2290Sstevel@tonic-gate 	    stbuf.st_dev == def_info.st_dev);
2300Sstevel@tonic-gate 	if ((buf = malloc(size + 1)) == NULL)
2310Sstevel@tonic-gate 		mesg(MEXIT, "cannot allocate %u for \"%s\"\n", size + 1, name);
2320Sstevel@tonic-gate 	nread = read(fd, buf, size);
2330Sstevel@tonic-gate 	(void) close(fd);
2340Sstevel@tonic-gate 	if (nread != (ssize_t)size)
2350Sstevel@tonic-gate 		mesg(MEXIT, "read error, expect %u, got %d, file \"%s\"\n",
2360Sstevel@tonic-gate 		    size, nread, name);
2370Sstevel@tonic-gate 	*(buf + size) = '\0';
2380Sstevel@tonic-gate 	return (buf);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate  * Add an arg to line_args, adding space if needed.
2440Sstevel@tonic-gate  */
2450Sstevel@tonic-gate static void
newarg(char * arg,int index)2460Sstevel@tonic-gate newarg(char *arg, int index)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	static int alcnt;
2490Sstevel@tonic-gate 	size_t size;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if ((index + 1) > alcnt) {
2520Sstevel@tonic-gate 		alcnt += 4;
2530Sstevel@tonic-gate 		size = alcnt * sizeof (*line_args);
2540Sstevel@tonic-gate 		if ((line_args = realloc(line_args, size)) == NULL)
2550Sstevel@tonic-gate 			mesg(MEXIT, "cannot alloc %u for line args\n", size);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 	*(line_args + index) = arg;
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  * Convert blank-delimited words into an arg vector and return
2630Sstevel@tonic-gate  * the arg count; character strings get null-terminated in place.
2640Sstevel@tonic-gate  */
2650Sstevel@tonic-gate static int
build_args(char * cline,char * tail)2660Sstevel@tonic-gate build_args(char *cline, char *tail)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	extern int debug;
2690Sstevel@tonic-gate 	char **vec, *arg, *cp;
2700Sstevel@tonic-gate 	int cnt = 0;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * Search logic: look for "\\\n" as a continuation marker,
2740Sstevel@tonic-gate 	 * treat any other "\\*" as ordinary arg data, scan until a
2750Sstevel@tonic-gate 	 * white-space delimiter is found, and if the arg has length,
2760Sstevel@tonic-gate 	 * null-terminate and save arg to line_args.  The scan includes
2770Sstevel@tonic-gate 	 * tail so the last arg is found without any special-case code.
2780Sstevel@tonic-gate 	 */
2790Sstevel@tonic-gate 	for (arg = cp = cline; cp <= tail; cp++) {
2800Sstevel@tonic-gate 		if (*cp == '\\') {
2810Sstevel@tonic-gate 			if (*(cp + 1) && *(cp + 1) != '\n') {
2820Sstevel@tonic-gate 				cp++;
2830Sstevel@tonic-gate 				continue;
2840Sstevel@tonic-gate 			}
2850Sstevel@tonic-gate 		} else if (strchr(" \t\n", *cp) == NULL)
2860Sstevel@tonic-gate 			continue;
2870Sstevel@tonic-gate 		if (cp - arg) {
2880Sstevel@tonic-gate 			*cp = '\0';
2890Sstevel@tonic-gate 			newarg(arg, cnt++);
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate 		arg = cp + 1;
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 	newarg(NULL, cnt);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (debug) {
2960Sstevel@tonic-gate 		mesg(MDEBUG, "\nline %d, found %d args:\n", lineno, cnt);
2970Sstevel@tonic-gate 		for (vec = line_args; *vec; vec++)
2980Sstevel@tonic-gate 			mesg(MDEBUG, "    \"%s\"\n", *vec);
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	return (cnt);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate  * Match leading keyword from a conf line and
3070Sstevel@tonic-gate  * return a reference to a config info struct.
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate static cinfo_t *
get_cinfo(void)3100Sstevel@tonic-gate get_cinfo(void)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	cinfo_t *cip, *info = NULL;
3130Sstevel@tonic-gate 	char *keyword;
3140Sstevel@tonic-gate 	int chr_diff;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	/*
3170Sstevel@tonic-gate 	 * Scan the config table for a matching keyword; since the table
3180Sstevel@tonic-gate 	 * is sorted by keyword strings, a few optimizations can be done:
3190Sstevel@tonic-gate 	 * first compare only the first byte of the keyword, skip any
3200Sstevel@tonic-gate 	 * table string that starts with a lower ASCII value, compare the
3210Sstevel@tonic-gate 	 * full string only when the first byte matches, and stop checking
3220Sstevel@tonic-gate 	 * if the table string starts with a higher ASCII value.
3230Sstevel@tonic-gate 	 */
3240Sstevel@tonic-gate 	keyword = LINEARG(0);
3250Sstevel@tonic-gate 	for (cip = conftab; cip->keyword; cip++) {
3260Sstevel@tonic-gate 		chr_diff = (int)(*cip->keyword - *keyword);
3270Sstevel@tonic-gate #if 0
3280Sstevel@tonic-gate 		mesg(MDEBUG, "line %d, ('%c' - '%c') = %d\n",
3290Sstevel@tonic-gate 		    lineno, *cip->keyword, *line, chr_diff);
3300Sstevel@tonic-gate #endif
3310Sstevel@tonic-gate 		if (chr_diff < 0)
3320Sstevel@tonic-gate 			continue;
3330Sstevel@tonic-gate 		else if (chr_diff == 0) {
3340Sstevel@tonic-gate 			if (strcmp(keyword, cip->keyword) == 0) {
3350Sstevel@tonic-gate 				info = cip;
3360Sstevel@tonic-gate 				break;
3370Sstevel@tonic-gate 			}
3380Sstevel@tonic-gate 		} else
3390Sstevel@tonic-gate 			break;
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 	return (info);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * Find the end of a [possibly continued] conf line
3470Sstevel@tonic-gate  * and record the real/lf-delimited line count at *lcnt.
3480Sstevel@tonic-gate  */
3490Sstevel@tonic-gate static char *
find_line_end(char * line,int * lcnt)3500Sstevel@tonic-gate find_line_end(char *line, int *lcnt)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	char *next, *lf;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	*lcnt = 0;
3550Sstevel@tonic-gate 	next = line;
3560Sstevel@tonic-gate 	while (lf = strchr(next, '\n')) {
3570Sstevel@tonic-gate 		(*lcnt)++;
3580Sstevel@tonic-gate 		if (lf == line || (*(lf - 1) != '\\') || *(lf + 1) == '\0')
3590Sstevel@tonic-gate 			break;
3600Sstevel@tonic-gate 		next = lf + 1;
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 	return (lf);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate  * Parse the named conf file and for each conf line
3680Sstevel@tonic-gate  * call the action routine or conftab handler routine.
3690Sstevel@tonic-gate  */
3700Sstevel@tonic-gate void
parse_conf_file(char * name,vact_t action,boolean_t first_parse)371*9057SSeth.Goldberg@Sun.COM parse_conf_file(char *name, vact_t action, boolean_t first_parse)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	char *file_buf, *cline, *line, *lend;
3740Sstevel@tonic-gate 	cinfo_t *cip;
3750Sstevel@tonic-gate 	int linc, cnt;
3760Sstevel@tonic-gate 	size_t llen;
3775295Srandyf 	int dontcare;
3785295Srandyf 
3795295Srandyf 	/*
380*9057SSeth.Goldberg@Sun.COM 	 * Do the "implied default" for autoS3, but only before we
381*9057SSeth.Goldberg@Sun.COM 	 * start parsing the first conf file.
3825295Srandyf 	 */
383*9057SSeth.Goldberg@Sun.COM 	if (first_parse) {
384*9057SSeth.Goldberg@Sun.COM 		(void) S3_helper("S3-support-enable", "S3-support-disable",
385*9057SSeth.Goldberg@Sun.COM 		    PM_ENABLE_S3, PM_DISABLE_S3, "S3-support", "default",
386*9057SSeth.Goldberg@Sun.COM 		    &dontcare, -1);
387*9057SSeth.Goldberg@Sun.COM 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	file_buf = get_conf_data(name);
3900Sstevel@tonic-gate 	mesg(MDEBUG, "\nnow parsing \"%s\"...\n", name);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	lineno = 1;
3930Sstevel@tonic-gate 	line = file_buf;
3940Sstevel@tonic-gate 	while (lend = find_line_end(line, &linc)) {
3950Sstevel@tonic-gate 		/*
3960Sstevel@tonic-gate 		 * Each line should start with valid data
3970Sstevel@tonic-gate 		 * but leading white-space can be ignored
3980Sstevel@tonic-gate 		 */
3990Sstevel@tonic-gate 		while (line < lend) {
4000Sstevel@tonic-gate 			if (*line != ' ' && *line != '\t')
4010Sstevel@tonic-gate 				break;
4020Sstevel@tonic-gate 			line++;
4030Sstevel@tonic-gate 		}
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 		/*
4060Sstevel@tonic-gate 		 * Copy line into allocated space and null-terminate
4070Sstevel@tonic-gate 		 * without the trailing line-feed.
4080Sstevel@tonic-gate 		 */
4090Sstevel@tonic-gate 		if ((llen = (lend - line)) != 0) {
4100Sstevel@tonic-gate 			if ((cline = malloc(llen + 1)) == NULL)
4110Sstevel@tonic-gate 				mesg(MEXIT, "cannot alloc %u bytes "
4120Sstevel@tonic-gate 				    "for line copy\n", llen);
4130Sstevel@tonic-gate 			(void) memcpy(cline, line, llen);
4140Sstevel@tonic-gate 			*(cline + llen) = '\0';
4150Sstevel@tonic-gate 		} else
4160Sstevel@tonic-gate 			cline = NULL;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 		/*
4190Sstevel@tonic-gate 		 * For blank and comment lines: possibly show a debug
4200Sstevel@tonic-gate 		 * message and otherwise ignore them.  For other lines:
4210Sstevel@tonic-gate 		 * parse into an arg vector and try to match the first
4220Sstevel@tonic-gate 		 * arg with conftab keywords.  When a match is found,
4230Sstevel@tonic-gate 		 * check for exact or minimum arg count, and call the
4240Sstevel@tonic-gate 		 * action or handler routine; if handler does not return
4250Sstevel@tonic-gate 		 * OKUP, set the referenced update value to NOUP so that
4260Sstevel@tonic-gate 		 * later CPR or PM updates are skipped.
4270Sstevel@tonic-gate 		 */
4280Sstevel@tonic-gate 		if (llen == 0)
4290Sstevel@tonic-gate 			mesg(MDEBUG, "\nline %d, blank...\n", lineno);
4300Sstevel@tonic-gate 		else if (*line == '#')
4310Sstevel@tonic-gate 			mesg(MDEBUG, "\nline %d, comment...\n", lineno);
4320Sstevel@tonic-gate 		else if (cnt = build_args(cline, cline + llen)) {
4330Sstevel@tonic-gate 			if ((cip = get_cinfo()) == NULL) {
4340Sstevel@tonic-gate 				mesg(MEXIT, "unrecognized keyword \"%s\"\n",
4350Sstevel@tonic-gate 				    LINEARG(0));
4360Sstevel@tonic-gate 			} else if (cnt != cip->argc &&
4370Sstevel@tonic-gate 			    (cip->any == 0 || cnt < cip->argc)) {
4380Sstevel@tonic-gate 				mesg(MEXIT, "found %d args, expect %d%s\n",
4390Sstevel@tonic-gate 				    cnt, cip->argc, cip->any ? "+" : "");
4400Sstevel@tonic-gate 			} else if (action)
4410Sstevel@tonic-gate 				(*action)(line, llen + 1, cip);
4420Sstevel@tonic-gate 			else if (cip->status->perm && (def_src || cip->alt)) {
4430Sstevel@tonic-gate 				if ((*cip->handler)() != OKUP)
4440Sstevel@tonic-gate 					cip->status->update = NOUP;
4450Sstevel@tonic-gate 			} else {
4460Sstevel@tonic-gate 				mesg(MDEBUG,
4470Sstevel@tonic-gate 				    "==> handler skipped: %s_perm %d, "
4480Sstevel@tonic-gate 				    "def_src %d, alt %d\n", cip->status->set,
4490Sstevel@tonic-gate 				    cip->status->perm, def_src, cip->alt);
4500Sstevel@tonic-gate 			}
4510Sstevel@tonic-gate 		}
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 		if (cline)
4540Sstevel@tonic-gate 			free(cline);
4550Sstevel@tonic-gate 		line = lend + 1;
4560Sstevel@tonic-gate 		lineno += linc;
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 	lineno = 0;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	free(file_buf);
4615295Srandyf 
4625295Srandyf 	if (verify) {
4635295Srandyf 		int ret = ioctl(pm_fd, PM_GET_PM_STATE, NULL);
4645295Srandyf 		if (ret < 0) {
4655295Srandyf 			mesg(MDEBUG, "Cannot get PM state: %s\n",
4665295Srandyf 			    strerror(errno));
4675295Srandyf 		}
4685295Srandyf 		switch (ret) {
4695295Srandyf 		case PM_SYSTEM_PM_ENABLED:
4705295Srandyf 			mesg(MDEBUG, "Autopm Enabled\n");
4715295Srandyf 			break;
4725295Srandyf 		case PM_SYSTEM_PM_DISABLED:
4735295Srandyf 			mesg(MDEBUG, "Autopm Disabled\n");
4745295Srandyf 			break;
4755295Srandyf 		}
4765295Srandyf 		ret = ioctl(pm_fd, PM_GET_S3_SUPPORT_STATE, NULL);
4775295Srandyf 		if (ret < 0) {
4785295Srandyf 			mesg(MDEBUG, "Cannot get PM state: %s\n",
4795295Srandyf 			    strerror(errno));
4805295Srandyf 		}
4815295Srandyf 		switch (ret) {
4825295Srandyf 		case PM_S3_SUPPORT_ENABLED:
4835295Srandyf 			mesg(MDEBUG, "S3 support Enabled\n");
4845295Srandyf 			break;
4855295Srandyf 		case PM_S3_SUPPORT_DISABLED:
4865295Srandyf 			mesg(MDEBUG, "S3 support Disabled\n");
4875295Srandyf 			break;
4885295Srandyf 		}
4895295Srandyf 		ret = ioctl(pm_fd, PM_GET_AUTOS3_STATE, NULL);
4905295Srandyf 		if (ret < 0) {
4915295Srandyf 			mesg(MDEBUG, "Cannot get PM state: %s\n",
4925295Srandyf 			    strerror(errno));
4935295Srandyf 		}
4945295Srandyf 		switch (ret) {
4955295Srandyf 		case PM_AUTOS3_ENABLED:
4965295Srandyf 			mesg(MDEBUG, "AutoS3 Enabled\n");
4975295Srandyf 			break;
4985295Srandyf 		case PM_AUTOS3_DISABLED:
4995295Srandyf 			mesg(MDEBUG, "AutoS3  Disabled\n");
5005295Srandyf 			break;
5015295Srandyf 		}
5025295Srandyf 	}
5030Sstevel@tonic-gate }
504