xref: /onnv-gate/usr/src/cmd/power/handlers.c (revision 3028:ecc075a2fc74)
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
5*3028Smh27603  * Common Development and Distribution License (the "License").
6*3028Smh27603  * 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 /*
22*3028Smh27603  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include "pmconfig.h"
290Sstevel@tonic-gate #include <sys/mkdev.h>
300Sstevel@tonic-gate #include <sys/syslog.h>
310Sstevel@tonic-gate #include <sys/openpromio.h>
320Sstevel@tonic-gate #include <sys/mnttab.h>
330Sstevel@tonic-gate #include <syslog.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #define	STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str)
380Sstevel@tonic-gate #define	LASTBYTE(str) (str + strlen(str) - 1)
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static char nerr_fmt[] = "number is out of range (%s)\n";
410Sstevel@tonic-gate static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n";
420Sstevel@tonic-gate static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n";
430Sstevel@tonic-gate static char bad_thresh_fmt[] = "bad threshold(s)\n";
440Sstevel@tonic-gate static char stat_fmt[] = "cannot stat \"%s\", %s\n";
450Sstevel@tonic-gate static char always_on[] = "always-on";
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * When lines in a config file (usually "/etc/power.conf") start with
490Sstevel@tonic-gate  * a recognized keyword, a "handler" routine is called for specific
500Sstevel@tonic-gate  * CPR or PM -related action(s).  Each routine returns a status code
510Sstevel@tonic-gate  * indicating whether all tasks were successful; if any errors occured,
520Sstevel@tonic-gate  * future CPR or PM updates are skipped.  Following are the handler
530Sstevel@tonic-gate  * routines for all keywords:
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
58*3028Smh27603  * Check for valid cpupm behavior and communicate it to the kernel.
59*3028Smh27603  */
60*3028Smh27603 int
61*3028Smh27603 cpupm(void)
62*3028Smh27603 {
63*3028Smh27603 	struct btoc {
64*3028Smh27603 		char *behavior;
65*3028Smh27603 		int cmd;
66*3028Smh27603 		int Errno;
67*3028Smh27603 	};
68*3028Smh27603 	static struct btoc blist[] = {
69*3028Smh27603 		"disable",	PM_STOP_CPUPM, EINVAL,
70*3028Smh27603 		"enable",	PM_START_CPUPM, EBUSY,
71*3028Smh27603 		NULL,		0, 0
72*3028Smh27603 	};
73*3028Smh27603 	struct btoc *bp;
74*3028Smh27603 	char *behavior;
75*3028Smh27603 
76*3028Smh27603 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
77*3028Smh27603 		if (strcmp(behavior, bp->behavior) == 0)
78*3028Smh27603 			break;
79*3028Smh27603 	}
80*3028Smh27603 	if (bp->cmd == 0) {
81*3028Smh27603 		mesg(MERR, "invalid cpupm behavior \"%s\"\n", behavior);
82*3028Smh27603 		return (NOUP);
83*3028Smh27603 	}
84*3028Smh27603 	if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
85*3028Smh27603 		mesg(MERR, "cpupm %s failed, %s\n",
86*3028Smh27603 		    behavior, strerror(errno));
87*3028Smh27603 		return (NOUP);
88*3028Smh27603 	}
89*3028Smh27603 	return (OKUP);
90*3028Smh27603 }
91*3028Smh27603 
92*3028Smh27603 
93*3028Smh27603 /*
940Sstevel@tonic-gate  * Check for valid autopm behavior and save after ioctl success.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate int
970Sstevel@tonic-gate autopm(void)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	struct btoc {
1000Sstevel@tonic-gate 		char *behavior;
1010Sstevel@tonic-gate 		int cmd, Errno, isdef;
1020Sstevel@tonic-gate 	};
1030Sstevel@tonic-gate 	static struct btoc blist[] = {
1040Sstevel@tonic-gate 		"default",	PM_START_PM,	EBUSY,	1,
1050Sstevel@tonic-gate 		"disable",	PM_STOP_PM,	EINVAL,	0,
1060Sstevel@tonic-gate 		"enable",	PM_START_PM,	EBUSY,	0,
1070Sstevel@tonic-gate 		NULL,		0,		0,	0,
1080Sstevel@tonic-gate 	};
1090Sstevel@tonic-gate 	struct btoc *bp;
1100Sstevel@tonic-gate 	char *behavior;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
1130Sstevel@tonic-gate 		if (strcmp(behavior, bp->behavior) == 0)
1140Sstevel@tonic-gate 			break;
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 	if (bp->cmd == 0) {
1170Sstevel@tonic-gate 		mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior);
1180Sstevel@tonic-gate 		return (NOUP);
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	/*
1220Sstevel@tonic-gate 	 * for "default" behavior, do not enable autopm if not ESTAR_V3
1230Sstevel@tonic-gate 	 */
1240Sstevel@tonic-gate 	if (!bp->isdef || (estar_vers == ESTAR_V3)) {
1250Sstevel@tonic-gate 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
1260Sstevel@tonic-gate 			mesg(MERR, "autopm %s failed, %s\n",
1270Sstevel@tonic-gate 			    behavior, strerror(errno));
1280Sstevel@tonic-gate 			return (NOUP);
1290Sstevel@tonic-gate 		}
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 	(void) strcpy(new_cc.apm_behavior, behavior);
1320Sstevel@tonic-gate 	return (OKUP);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static int
1370Sstevel@tonic-gate gethm(char *src, int *hour, int *min)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	if (sscanf(src, "%d:%d", hour, min) != 2) {
1400Sstevel@tonic-gate 		mesg(MERR, "bad time format (%s)\n", src);
1410Sstevel@tonic-gate 		return (-1);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 	return (0);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static void
1480Sstevel@tonic-gate strcpy_limit(char *dst, char *src, size_t limit, char *info)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	if (strlcpy(dst, src, limit) >= limit)
1510Sstevel@tonic-gate 		mesg(MEXIT, "%s is too long (%s)\n", info, src);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * Convert autoshutdown idle and start/finish times;
1570Sstevel@tonic-gate  * check and record autoshutdown behavior.
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate int
1600Sstevel@tonic-gate autosd(void)
1610Sstevel@tonic-gate {
162763Smh27603 	char **bp, *behavior;
163763Smh27603 	char *unrec = gettext("unrecognized autoshutdown behavior");
1640Sstevel@tonic-gate 	static char *blist[] = {
1650Sstevel@tonic-gate 		"autowakeup", "default", "noshutdown",
1660Sstevel@tonic-gate 		"shutdown", "unconfigured", NULL
1670Sstevel@tonic-gate 	};
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	new_cc.as_idle = atoi(LINEARG(1));
1700Sstevel@tonic-gate 	if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) ||
1710Sstevel@tonic-gate 	    gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm))
1720Sstevel@tonic-gate 		return (NOUP);
173763Smh27603 	mesg(MDEBUG, "idle %d, start %d:%02d, finish %d:%02d\n",
1740Sstevel@tonic-gate 	    new_cc.as_idle, new_cc.as_sh, new_cc.as_sm,
1750Sstevel@tonic-gate 	    new_cc.as_fh, new_cc.as_fm);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	for (behavior = LINEARG(4), bp = blist; *bp; bp++) {
1780Sstevel@tonic-gate 		if (strcmp(behavior, *bp) == 0)
1790Sstevel@tonic-gate 			break;
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 	if (*bp == NULL) {
1820Sstevel@tonic-gate 		mesg(MERR, "%s: \"%s\"\n", unrec, behavior);
1830Sstevel@tonic-gate 		return (NOUP);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 	STRCPYLIM(new_cc.as_behavior, *bp, unrec);
1860Sstevel@tonic-gate 	return (OKUP);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate  * Check for a real device and try to resolve to a full path.
1920Sstevel@tonic-gate  * The orig/resolved path may be modified into a prom pathname,
1930Sstevel@tonic-gate  * and an allocated copy of the result is stored at *destp;
1940Sstevel@tonic-gate  * the caller will need to free that space.  Returns 1 for any
1950Sstevel@tonic-gate  * error, otherwise 0; also sets *errp after an alloc error.
1960Sstevel@tonic-gate  */
1970Sstevel@tonic-gate static int
1980Sstevel@tonic-gate devpath(char **destp, char *src, int *errp)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	struct stat stbuf;
2010Sstevel@tonic-gate 	char buf[PATH_MAX];
2020Sstevel@tonic-gate 	char *cp, *dstr;
2030Sstevel@tonic-gate 	int devok, dcs = 0;
2040Sstevel@tonic-gate 	size_t len;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	/*
2070Sstevel@tonic-gate 	 * When there's a real device, try to resolve the path
2080Sstevel@tonic-gate 	 * and trim the leading "/devices" component.
2090Sstevel@tonic-gate 	 */
2100Sstevel@tonic-gate 	if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) {
2110Sstevel@tonic-gate 		if (realpath(src, buf) == NULL) {
2120Sstevel@tonic-gate 			mesg(MERR, "realpath cannot resolve \"%s\"\n",
2130Sstevel@tonic-gate 			    src, strerror(errno));
2140Sstevel@tonic-gate 			return (1);
2150Sstevel@tonic-gate 		}
2160Sstevel@tonic-gate 		src = buf;
2170Sstevel@tonic-gate 		dstr = "/devices";
2180Sstevel@tonic-gate 		len = strlen(dstr);
2190Sstevel@tonic-gate 		dcs = (strncmp(src, dstr, len) == 0);
2200Sstevel@tonic-gate 		if (dcs)
2210Sstevel@tonic-gate 			src += len;
2220Sstevel@tonic-gate 	} else
2230Sstevel@tonic-gate 		mesg(MDEBUG, stat_fmt, src, strerror(errno));
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/*
2260Sstevel@tonic-gate 	 * When the path has ":anything", display an error for
2270Sstevel@tonic-gate 	 * a non-device or truncate a resolved+modifed path.
2280Sstevel@tonic-gate 	 */
2290Sstevel@tonic-gate 	if (cp = strchr(src, ':')) {
2300Sstevel@tonic-gate 		if (devok == 0) {
2310Sstevel@tonic-gate 			mesg(MERR, "physical path may not contain "
2320Sstevel@tonic-gate 			    "a minor string (%s)\n", src);
2330Sstevel@tonic-gate 			return (1);
2340Sstevel@tonic-gate 		} else if (dcs)
2350Sstevel@tonic-gate 			*cp = '\0';
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if ((*destp = strdup(src)) == NULL) {
2390Sstevel@tonic-gate 		*errp = NOUP;
2400Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, src, strerror(errno));
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 	return (*destp == NULL);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * Call pm ioctl request(s) to set property/device dependencies.
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate static int
2500Sstevel@tonic-gate dev_dep_common(int isprop)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	int cmd, argn, upval = OKUP;
2530Sstevel@tonic-gate 	char *src, *first, **destp;
2540Sstevel@tonic-gate 	pm_req_t pmreq;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
2570Sstevel@tonic-gate 	src = LINEARG(1);
2580Sstevel@tonic-gate 	if (isprop) {
2590Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT_PROPERTY;
2600Sstevel@tonic-gate 		first = NULL;
2610Sstevel@tonic-gate 		pmreq.pmreq_kept = src;
2620Sstevel@tonic-gate 	} else {
2630Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT;
2640Sstevel@tonic-gate 		if (devpath(&first, src, &upval))
2650Sstevel@tonic-gate 			return (upval);
2660Sstevel@tonic-gate 		pmreq.pmreq_kept = first;
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 	destp = &pmreq.pmreq_keeper;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	/*
2710Sstevel@tonic-gate 	 * Now loop through any dependents.
2720Sstevel@tonic-gate 	 */
2730Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
2740Sstevel@tonic-gate 		if (devpath(destp, src, &upval)) {
2750Sstevel@tonic-gate 			if (upval != OKUP)
2760Sstevel@tonic-gate 				return (upval);
2770Sstevel@tonic-gate 			break;
2780Sstevel@tonic-gate 		}
2790Sstevel@tonic-gate 		if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) {
2800Sstevel@tonic-gate 			mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n"
2810Sstevel@tonic-gate 			    "kept \"%s\", keeper \"%s\"\n",
2820Sstevel@tonic-gate 			    cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper);
2830Sstevel@tonic-gate 			mesg(MERR, "cannot set \"%s\" dependency "
2840Sstevel@tonic-gate 			    "for \"%s\", %s\n", pmreq.pmreq_keeper,
2850Sstevel@tonic-gate 			    pmreq.pmreq_kept, strerror(errno));
2860Sstevel@tonic-gate 		}
2870Sstevel@tonic-gate 		free(*destp);
2880Sstevel@tonic-gate 		*destp = NULL;
2890Sstevel@tonic-gate 		if (upval != OKUP)
2900Sstevel@tonic-gate 			break;
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	free(first);
2940Sstevel@tonic-gate 	return (upval);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate int
2990Sstevel@tonic-gate ddprop(void)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	return (dev_dep_common(1));
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate int
3060Sstevel@tonic-gate devdep(void)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	return (dev_dep_common(0));
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * Convert a numeric string (with a possible trailing scaling byte)
3140Sstevel@tonic-gate  * into an integer.  Returns a converted value and *nerrp unchanged,
3150Sstevel@tonic-gate  * or 0 with *nerrp set to 1 for a conversion error.
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate static int
3180Sstevel@tonic-gate get_scaled_value(char *str, int *nerrp)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	longlong_t svalue = 0, factor = 1;
3210Sstevel@tonic-gate 	char *sp;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	errno = 0;
3240Sstevel@tonic-gate 	svalue = strtol(str, &sp, 0);
3250Sstevel@tonic-gate 	if (errno || (*str != '-' && (*str < '0' || *str > '9')))
3260Sstevel@tonic-gate 		*nerrp = 1;
3270Sstevel@tonic-gate 	else if (sp && *sp != '\0') {
3280Sstevel@tonic-gate 		if (*sp == 'h')
3290Sstevel@tonic-gate 			factor = 3600;
3300Sstevel@tonic-gate 		else if (*sp == 'm')
3310Sstevel@tonic-gate 			factor = 60;
3320Sstevel@tonic-gate 		else if (*sp != 's')
3330Sstevel@tonic-gate 			*nerrp = 1;
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 	/* any bytes following sp are ignored */
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if (*nerrp == 0) {
3380Sstevel@tonic-gate 		svalue *= factor;
3390Sstevel@tonic-gate 		if (svalue < INT_MIN || svalue > INT_MAX)
3400Sstevel@tonic-gate 			*nerrp = 1;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 	if (*nerrp)
3430Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, str);
3440Sstevel@tonic-gate 	mesg(MDEBUG, "got scaled value %d\n", (int)svalue);
3450Sstevel@tonic-gate 	return ((int)svalue);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * Increment the count of threshold values,
3510Sstevel@tonic-gate  * reallocate *vlistp and append another element.
3520Sstevel@tonic-gate  * Returns 1 on error, otherwise 0.
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate static int
3550Sstevel@tonic-gate vlist_append(int **vlistp, int *vcntp, int value)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	(*vcntp)++;
3580Sstevel@tonic-gate 	if (*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp)))
3590Sstevel@tonic-gate 		*(*vlistp + *vcntp - 1) = value;
3600Sstevel@tonic-gate 	else
3610Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, "threshold list", strerror(errno));
3620Sstevel@tonic-gate 	return (*vlistp == NULL);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate  * Convert a single threshold string or paren groups of thresh's as
3680Sstevel@tonic-gate  * described below.  All thresh's are saved to an allocated list at
3690Sstevel@tonic-gate  * *vlistp; the caller will need to free that space.  On return:
3700Sstevel@tonic-gate  * *vcntp is the count of the vlist array, and vlist is either
3710Sstevel@tonic-gate  * a single thresh or N groups of thresh's with a trailing zero:
3720Sstevel@tonic-gate  * (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0.
3730Sstevel@tonic-gate  * Returns 0 when all conversions were OK, and 1 for any syntax,
3740Sstevel@tonic-gate  * conversion, or alloc error.
3750Sstevel@tonic-gate  */
3760Sstevel@tonic-gate static int
3770Sstevel@tonic-gate get_thresh(int **vlistp, int *vcntp)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	int argn, value, gci, grp_cnt = 0, paren = 0, nerr = 0;
3800Sstevel@tonic-gate 	char *rp, *src;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
3830Sstevel@tonic-gate 		if (*src == LPAREN) {
3840Sstevel@tonic-gate 			gci = *vcntp;
3850Sstevel@tonic-gate 			if (nerr = vlist_append(vlistp, vcntp, 0))
3860Sstevel@tonic-gate 				break;
3870Sstevel@tonic-gate 			paren = 1;
3880Sstevel@tonic-gate 			src++;
3890Sstevel@tonic-gate 		}
3900Sstevel@tonic-gate 		if (*(rp = LASTBYTE(src)) == RPAREN) {
3910Sstevel@tonic-gate 			if (paren) {
3920Sstevel@tonic-gate 				grp_cnt = *vcntp - gci;
3930Sstevel@tonic-gate 				*(*vlistp + gci) = grp_cnt;
3940Sstevel@tonic-gate 				paren = 0;
3950Sstevel@tonic-gate 				*rp = '\0';
3960Sstevel@tonic-gate 			} else {
3970Sstevel@tonic-gate 				nerr = 1;
3980Sstevel@tonic-gate 				break;
3990Sstevel@tonic-gate 			}
4000Sstevel@tonic-gate 		}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 		value = get_scaled_value(src, &nerr);
4030Sstevel@tonic-gate 		if (nerr || (nerr = vlist_append(vlistp, vcntp, value)))
4040Sstevel@tonic-gate 			break;
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	if (nerr == 0 && grp_cnt)
4080Sstevel@tonic-gate 		nerr = vlist_append(vlistp, vcntp, 0);
4090Sstevel@tonic-gate 	return (nerr);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate  * Set device thresholds from (3) formats:
4150Sstevel@tonic-gate  * 	path	"always-on"
4160Sstevel@tonic-gate  * 	path	time-spec: [0-9]+[{h,m,s}]
4170Sstevel@tonic-gate  *	path	(ts1 ts2 ...)+
4180Sstevel@tonic-gate  */
4190Sstevel@tonic-gate int
4200Sstevel@tonic-gate devthr(void)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	int cmd, upval = OKUP, nthresh = 0, *vlist = NULL;
4230Sstevel@tonic-gate 	pm_req_t pmreq;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
4260Sstevel@tonic-gate 	if (devpath(&pmreq.physpath, LINEARG(1), &upval))
4270Sstevel@tonic-gate 		return (upval);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if (strcmp(LINEARG(2), always_on) == 0) {
4300Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
4310Sstevel@tonic-gate 		pmreq.value = INT_MAX;
4320Sstevel@tonic-gate 	} else if (get_thresh(&vlist, &nthresh)) {
4330Sstevel@tonic-gate 		mesg(MERR, bad_thresh_fmt);
4340Sstevel@tonic-gate 		upval = NOUP;
4350Sstevel@tonic-gate 	} else if (nthresh == 1) {
4360Sstevel@tonic-gate 		pmreq.value = *vlist;
4370Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
4380Sstevel@tonic-gate 	} else {
4390Sstevel@tonic-gate 		pmreq.data = vlist;
4400Sstevel@tonic-gate 		pmreq.datasize = (nthresh * sizeof (*vlist));
4410Sstevel@tonic-gate 		cmd = PM_SET_COMPONENT_THRESHOLDS;
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1)
4450Sstevel@tonic-gate 		mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno));
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	free(vlist);
4480Sstevel@tonic-gate 	free(pmreq.physpath);
4490Sstevel@tonic-gate 	return (upval);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate static int
4540Sstevel@tonic-gate scan_int(char *src, int *dst)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	long lval;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	errno = 0;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	lval = strtol(LINEARG(1), NULL, 0);
4610Sstevel@tonic-gate 	if (errno || lval > INT_MAX || lval < 0) {
4620Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
4630Sstevel@tonic-gate 		return (NOUP);
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	*dst = (int)lval;
4670Sstevel@tonic-gate 	return (OKUP);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate static int
4710Sstevel@tonic-gate scan_float(char *src, float *dst)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate 	float fval;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	errno = 0;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	fval = strtof(src, NULL);
4780Sstevel@tonic-gate 	if (errno || fval < 0.0) {
4790Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
4800Sstevel@tonic-gate 		return (NOUP);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	*dst = fval;
4840Sstevel@tonic-gate 	return (OKUP);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate int
4890Sstevel@tonic-gate dreads(void)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.diskreads_thold));
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate  * Set pathname for idlecheck;
4970Sstevel@tonic-gate  * an overflowed pathname is treated as a fatal error.
4980Sstevel@tonic-gate  */
4990Sstevel@tonic-gate int
5000Sstevel@tonic-gate idlechk(void)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate 	STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path");
5030Sstevel@tonic-gate 	return (OKUP);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate int
5080Sstevel@tonic-gate loadavg(void)
5090Sstevel@tonic-gate {
5100Sstevel@tonic-gate 	return (scan_float(LINEARG(1), &new_cc.loadaverage_thold));
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate int
5150Sstevel@tonic-gate nfsreq(void)
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold));
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate #ifdef sparc
5220Sstevel@tonic-gate static char open_fmt[] = "cannot open \"%s\", %s\n";
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate  * Verify the filesystem type for a regular statefile is "ufs"
5260Sstevel@tonic-gate  * or verify a block device is not in use as a mounted filesytem.
5270Sstevel@tonic-gate  * Returns 1 if any error, otherwise 0.
5280Sstevel@tonic-gate  */
5290Sstevel@tonic-gate static int
5300Sstevel@tonic-gate check_mount(char *sfile, dev_t sfdev, int ufs)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	char *src, *err_fmt = NULL, *mnttab = MNTTAB;
5330Sstevel@tonic-gate 	int rgent, match = 0;
5340Sstevel@tonic-gate 	struct extmnttab ent;
5350Sstevel@tonic-gate 	FILE *fp;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
5380Sstevel@tonic-gate 		mesg(MERR, open_fmt, mnttab, strerror(errno));
5390Sstevel@tonic-gate 		return (1);
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/*
5430Sstevel@tonic-gate 	 * Search for a matching dev_t;
5440Sstevel@tonic-gate 	 * ignore non-ufs filesystems for a regular statefile.
5450Sstevel@tonic-gate 	 */
5460Sstevel@tonic-gate 	while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) {
5470Sstevel@tonic-gate 		if (rgent > 0) {
5480Sstevel@tonic-gate 			mesg(MERR, "error reading \"%s\"\n", mnttab);
5490Sstevel@tonic-gate 			(void) fclose(fp);
5500Sstevel@tonic-gate 			return (1);
5510Sstevel@tonic-gate 		} else if (ufs && strcmp(ent.mnt_fstype, "ufs"))
5520Sstevel@tonic-gate 			continue;
5530Sstevel@tonic-gate 		else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) {
5540Sstevel@tonic-gate 			match = 1;
5550Sstevel@tonic-gate 			break;
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 	(void) fclose(fp);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	/*
5610Sstevel@tonic-gate 	 * No match is needed for a block device statefile,
5620Sstevel@tonic-gate 	 * a match is needed for a regular statefile.
5630Sstevel@tonic-gate 	 */
5640Sstevel@tonic-gate 	if (match == 0) {
5650Sstevel@tonic-gate 		if (new_cc.cf_type == CFT_SPEC)
5660Sstevel@tonic-gate 			STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile");
5670Sstevel@tonic-gate 		else
5680Sstevel@tonic-gate 			err_fmt = "cannot find ufs mount point for \"%s\"\n";
5690Sstevel@tonic-gate 	} else if (new_cc.cf_type == CFT_UFS) {
5700Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry");
5710Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special");
5720Sstevel@tonic-gate 		while (*(sfile + 1) == '/') sfile++;
5730Sstevel@tonic-gate 		src = sfile + strlen(ent.mnt_mountp);
5740Sstevel@tonic-gate 		while (*src == '/') src++;
5750Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_path, src, "statefile path");
5760Sstevel@tonic-gate 	} else
5770Sstevel@tonic-gate 		err_fmt = "statefile device \"%s\" is a mounted filesystem\n";
5780Sstevel@tonic-gate 	if (err_fmt)
5790Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
5800Sstevel@tonic-gate 	return (err_fmt != NULL);
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate  * Convert a Unix device to a prom device and save on success,
5860Sstevel@tonic-gate  * log any ioctl/conversion error.
5870Sstevel@tonic-gate  */
5880Sstevel@tonic-gate static int
5890Sstevel@tonic-gate utop(void)
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate 	union obpbuf {
5920Sstevel@tonic-gate 		char	buf[OBP_MAXPATHLEN + sizeof (uint_t)];
5930Sstevel@tonic-gate 		struct	openpromio oppio;
5940Sstevel@tonic-gate 	};
5950Sstevel@tonic-gate 	union obpbuf oppbuf;
5960Sstevel@tonic-gate 	struct openpromio *opp;
5970Sstevel@tonic-gate 	char *promdev = "/dev/openprom";
5980Sstevel@tonic-gate 	int fd, upval;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	if ((fd = open(promdev, O_RDONLY)) == -1) {
6010Sstevel@tonic-gate 		mesg(MERR, open_fmt, promdev, strerror(errno));
6020Sstevel@tonic-gate 		return (NOUP);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	opp = &oppbuf.oppio;
6060Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPATHLEN;
6070Sstevel@tonic-gate 	strcpy_limit(opp->oprom_array, new_cc.cf_devfs,
6080Sstevel@tonic-gate 	    OBP_MAXPATHLEN, "statefile device");
6090Sstevel@tonic-gate 	upval = ioctl(fd, OPROMDEV2PROMNAME, opp);
6100Sstevel@tonic-gate 	(void) close(fd);
6110Sstevel@tonic-gate 	if (upval == OKUP)
6120Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_dev_prom, opp->oprom_array, "prom device");
6130Sstevel@tonic-gate 	else {
6140Sstevel@tonic-gate 		openlog("pmconfig", 0, LOG_DAEMON);
6150Sstevel@tonic-gate 		syslog(LOG_NOTICE,
6160Sstevel@tonic-gate 		    gettext("cannot convert \"%s\" to prom device"),
6170Sstevel@tonic-gate 		    new_cc.cf_devfs);
6180Sstevel@tonic-gate 		closelog();
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	return (upval);
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate  * Check for a valid statefile pathname, inode and mount status.
6270Sstevel@tonic-gate  */
6280Sstevel@tonic-gate int
6290Sstevel@tonic-gate sfpath(void)
6300Sstevel@tonic-gate {
6310Sstevel@tonic-gate 	static int statefile;
6320Sstevel@tonic-gate 	char *err_fmt = NULL;
6330Sstevel@tonic-gate 	char *sfile, *sp, ch;
6340Sstevel@tonic-gate 	struct stat stbuf;
6350Sstevel@tonic-gate 	int dir = 0;
6360Sstevel@tonic-gate 	dev_t dev;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	if (statefile) {
6390Sstevel@tonic-gate 		mesg(MERR, "ignored redundant statefile entry\n");
6400Sstevel@tonic-gate 		return (OKUP);
6410Sstevel@tonic-gate 	} else if (ua_err) {
6420Sstevel@tonic-gate 		if (ua_err != ENOTSUP)
6430Sstevel@tonic-gate 			mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n",
6440Sstevel@tonic-gate 			    strerror(ua_err));
6450Sstevel@tonic-gate 		return (NOUP);
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	/*
6490Sstevel@tonic-gate 	 * Check for an absolute path and trim any trailing '/'.
6500Sstevel@tonic-gate 	 */
6510Sstevel@tonic-gate 	sfile = LINEARG(1);
6520Sstevel@tonic-gate 	if (*sfile != '/') {
6530Sstevel@tonic-gate 		mesg(MERR, "statefile requires an absolute path\n");
6540Sstevel@tonic-gate 		return (NOUP);
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 	for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--)
6570Sstevel@tonic-gate 		*sp = '\0';
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/*
6600Sstevel@tonic-gate 	 * If the statefile doesn't exist, the leading path must be a dir.
6610Sstevel@tonic-gate 	 */
6620Sstevel@tonic-gate 	if (stat(sfile, &stbuf) == -1) {
6630Sstevel@tonic-gate 		if (errno == ENOENT) {
6640Sstevel@tonic-gate 			dir = 1;
6650Sstevel@tonic-gate 			if ((sp = strrchr(sfile, '/')) == sfile)
6660Sstevel@tonic-gate 				sp++;
6670Sstevel@tonic-gate 			ch = *sp;
6680Sstevel@tonic-gate 			*sp = '\0';
6690Sstevel@tonic-gate 			if (stat(sfile, &stbuf) == -1)
6700Sstevel@tonic-gate 				err_fmt = stat_fmt;
6710Sstevel@tonic-gate 			*sp = ch;
6720Sstevel@tonic-gate 		} else
6730Sstevel@tonic-gate 			err_fmt = stat_fmt;
6740Sstevel@tonic-gate 		if (err_fmt) {
6750Sstevel@tonic-gate 			mesg(MERR, err_fmt, sfile, strerror(errno));
6760Sstevel@tonic-gate 			return (NOUP);
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/*
6810Sstevel@tonic-gate 	 * Check for regular/dir/block types, set cf_type and dev.
6820Sstevel@tonic-gate 	 */
6830Sstevel@tonic-gate 	if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) {
6840Sstevel@tonic-gate 		new_cc.cf_type = CFT_UFS;
6850Sstevel@tonic-gate 		dev = stbuf.st_dev;
6860Sstevel@tonic-gate 	} else if (S_ISBLK(stbuf.st_mode)) {
6870Sstevel@tonic-gate 		if (minor(stbuf.st_rdev) != 2) {
6880Sstevel@tonic-gate 			new_cc.cf_type = CFT_SPEC;
6890Sstevel@tonic-gate 			dev = stbuf.st_rdev;
6900Sstevel@tonic-gate 		} else
6910Sstevel@tonic-gate 			err_fmt = "statefile device cannot be slice 2 (%s)\n"
6920Sstevel@tonic-gate 			    "would clobber the disk label and boot-block\n";
6930Sstevel@tonic-gate 	} else
6940Sstevel@tonic-gate 		err_fmt = "bad file type for \"%s\"\n"
6950Sstevel@tonic-gate 		    "statefile must be a regular file or block device\n";
6960Sstevel@tonic-gate 	if (err_fmt) {
6970Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
6980Sstevel@tonic-gate 		return (NOUP);
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)) || utop())
7020Sstevel@tonic-gate 		return (NOUP);
7030Sstevel@tonic-gate 	new_cc.cf_magic = CPR_CONFIG_MAGIC;
7040Sstevel@tonic-gate 	statefile = 1;
7050Sstevel@tonic-gate 	return (OKUP);
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate #endif /* sparc */
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate /*
711*3028Smh27603  * Common function to set a system or cpu threshold.
7120Sstevel@tonic-gate  */
713*3028Smh27603 static int
714*3028Smh27603 cmnthr(int req)
7150Sstevel@tonic-gate {
7160Sstevel@tonic-gate 	int value, nerr = 0, upval = OKUP;
7170Sstevel@tonic-gate 	char *thresh = LINEARG(1);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	if (strcmp(thresh, always_on) == 0)
7200Sstevel@tonic-gate 		value = INT_MAX;
7210Sstevel@tonic-gate 	else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) {
7220Sstevel@tonic-gate 		mesg(MERR, "%s must be a positive value\n", LINEARG(0));
7230Sstevel@tonic-gate 		upval = NOUP;
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 	if (upval == OKUP)
726*3028Smh27603 		(void) ioctl(pm_fd, req, value);
7270Sstevel@tonic-gate 	return (upval);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 
731*3028Smh27603 /*
732*3028Smh27603  * Try setting system threshold.
733*3028Smh27603  */
734*3028Smh27603 int
735*3028Smh27603 systhr(void)
736*3028Smh27603 {
737*3028Smh27603 	return (cmnthr(PM_SET_SYSTEM_THRESHOLD));
738*3028Smh27603 }
739*3028Smh27603 
740*3028Smh27603 
741*3028Smh27603 /*
742*3028Smh27603  * Try setting cpu threshold.
743*3028Smh27603  */
744*3028Smh27603 int
745*3028Smh27603 cputhr(void)
746*3028Smh27603 {
747*3028Smh27603 	return (cmnthr(PM_SET_CPU_THRESHOLD));
748*3028Smh27603 }
749*3028Smh27603 
750*3028Smh27603 
7510Sstevel@tonic-gate int
7520Sstevel@tonic-gate tchars(void)
7530Sstevel@tonic-gate {
7540Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.ttychars_thold));
7550Sstevel@tonic-gate }
756