xref: /onnv-gate/usr/src/cmd/power/handlers.c (revision 6423)
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 /*
226171Smh27603  * Copyright 2008 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>
33*6423Sgw25295 #include <sys/vtoc.h>
34*6423Sgw25295 #include <sys/efi_partition.h>
350Sstevel@tonic-gate #include <syslog.h>
360Sstevel@tonic-gate #include <stdlib.h>
375295Srandyf #include <sys/pm.h>
385295Srandyf #include <kstat.h>
395295Srandyf #include <sys/smbios.h>
40*6423Sgw25295 #include <libzfs.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #define	STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str)
440Sstevel@tonic-gate #define	LASTBYTE(str) (str + strlen(str) - 1)
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static char nerr_fmt[] = "number is out of range (%s)\n";
470Sstevel@tonic-gate static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n";
480Sstevel@tonic-gate static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n";
490Sstevel@tonic-gate static char bad_thresh_fmt[] = "bad threshold(s)\n";
500Sstevel@tonic-gate static char stat_fmt[] = "cannot stat \"%s\", %s\n";
510Sstevel@tonic-gate static char always_on[] = "always-on";
520Sstevel@tonic-gate 
535295Srandyf #define	PM_DEFAULT_ALGORITHM -1
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * When lines in a config file (usually "/etc/power.conf") start with
560Sstevel@tonic-gate  * a recognized keyword, a "handler" routine is called for specific
570Sstevel@tonic-gate  * CPR or PM -related action(s).  Each routine returns a status code
580Sstevel@tonic-gate  * indicating whether all tasks were successful; if any errors occured,
590Sstevel@tonic-gate  * future CPR or PM updates are skipped.  Following are the handler
600Sstevel@tonic-gate  * routines for all keywords:
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 
645295Srandyf static char pm_cmd_string[32];
655295Srandyf 
665295Srandyf static char *
675295Srandyf pm_map(int cmd)
685295Srandyf {
695295Srandyf 	pm_req_t req;
705295Srandyf 
715295Srandyf 	req.value = cmd;
725295Srandyf 	req.data = (void *)pm_cmd_string;
735295Srandyf 	req.datasize = sizeof (pm_cmd_string);
745295Srandyf 
755295Srandyf 	if (ioctl(pm_fd, PM_GET_CMD_NAME, &req) < 0) {
766171Smh27603 		perror(gettext("PM_GET_CMD_NAME failed:"));
775295Srandyf 		return ("??");
785295Srandyf 	}
795295Srandyf 	return (pm_cmd_string);
805295Srandyf }
815295Srandyf 
825295Srandyf static int
835295Srandyf isonlist(char *listname, const char *man, const char *prod)
845295Srandyf {
855295Srandyf 	pm_searchargs_t sl;
865295Srandyf 	int ret;
875295Srandyf 
885295Srandyf 	sl.pms_listname = listname;
895295Srandyf 	sl.pms_manufacturer = (char *)man;
905295Srandyf 	sl.pms_product = (char *)prod;
915295Srandyf 	ret = ioctl(pm_fd, PM_SEARCH_LIST, &sl);
925295Srandyf 	mesg(MDEBUG, "PM_SEARCH_LIST %s for %s,%s returns %d\n",
935295Srandyf 	    listname, man, prod, ret);
945295Srandyf 	return (ret == 0);
955295Srandyf }
965295Srandyf 
975295Srandyf static int
985295Srandyf do_ioctl(int ioctl_cmd, char *keyword, char *behavior, int suppress)
995295Srandyf {
1005295Srandyf 	mesg(MDEBUG, "doing ioctl %s for %s ", pm_map(ioctl_cmd), keyword);
1015295Srandyf 	if (ioctl(pm_fd, ioctl_cmd, NULL) == -1) {
1025295Srandyf 		int suppressed = suppress == -1 || suppress == errno;
1035295Srandyf 		if (!suppressed) {
1045295Srandyf 			mesg(MERR, "%s %s failed, %s\n", keyword, behavior,
1055295Srandyf 			    strerror(errno));
1065295Srandyf 			return (NOUP);
1075295Srandyf 		} else {
1086171Smh27603 			mesg(MDEBUG, "%s %s failed, %s\n", keyword, behavior,
1096171Smh27603 			    strerror(errno));
1105295Srandyf 			return (OKUP);
1115295Srandyf 		}
1125295Srandyf 	}
1135295Srandyf 	mesg(MDEBUG, "succeeded\n");
1145295Srandyf 	return (OKUP);
1155295Srandyf }
1165295Srandyf 
1170Sstevel@tonic-gate /*
1183028Smh27603  * Check for valid cpupm behavior and communicate it to the kernel.
1193028Smh27603  */
1203028Smh27603 int
1213028Smh27603 cpupm(void)
1223028Smh27603 {
1233028Smh27603 	struct btoc {
1243028Smh27603 		char *behavior;
1253028Smh27603 		int cmd;
1263028Smh27603 		int Errno;
1273028Smh27603 	};
1283028Smh27603 	static struct btoc blist[] = {
1293028Smh27603 		"disable",	PM_STOP_CPUPM, EINVAL,
1303028Smh27603 		"enable",	PM_START_CPUPM, EBUSY,
1313028Smh27603 		NULL,		0, 0
1323028Smh27603 	};
1333028Smh27603 	struct btoc *bp;
1343028Smh27603 	char *behavior;
1353028Smh27603 
1363028Smh27603 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
1373028Smh27603 		if (strcmp(behavior, bp->behavior) == 0)
1383028Smh27603 			break;
1393028Smh27603 	}
1403028Smh27603 	if (bp->cmd == 0) {
1413028Smh27603 		mesg(MERR, "invalid cpupm behavior \"%s\"\n", behavior);
1423028Smh27603 		return (NOUP);
1433028Smh27603 	}
1443028Smh27603 	if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
1453028Smh27603 		mesg(MERR, "cpupm %s failed, %s\n",
1463028Smh27603 		    behavior, strerror(errno));
1473028Smh27603 		return (NOUP);
1483028Smh27603 	}
1493028Smh27603 	return (OKUP);
1503028Smh27603 }
1513028Smh27603 
1523028Smh27603 
1533028Smh27603 /*
1545295Srandyf  * Two decisions are identical except for the list names and ioctl commands
1555295Srandyf  * inputs: whitelist, blacklist, yes, no
1565295Srandyf  * if (! ("S3" kstat exists))
1575295Srandyf  *	return (no)
1585295Srandyf  * if (SystemInformation.Manufacturer == "Sun Microsystems" &&
1595295Srandyf  *    (Pref_PM_Profile == Workstation || Pref_PM_Profile == Desktop)) {
1605295Srandyf  *	if (platform on blacklist)
1615295Srandyf  *		return (no)
1625295Srandyf  *	return (yes)
1635295Srandyf  * } else {
1645295Srandyf  *	if (platform on whitelist)
1655295Srandyf  *		return (yes)
1665295Srandyf  *	return (no)
1675295Srandyf  * }
1685295Srandyf  */
1695295Srandyf 
1705295Srandyf int
1715295Srandyf S3_helper(char *whitelist, char *blacklist, int yes, int no, char *keyword,
1725295Srandyf 	char *behavior, int *didyes, int suppress)
1735295Srandyf {
1745295Srandyf 	int oflags = SMB_O_NOCKSUM | SMB_O_NOVERS;
1755295Srandyf 	smbios_hdl_t *shp;
1765295Srandyf 	smbios_system_t sys;
1775295Srandyf 	id_t id;
1785295Srandyf 	int ret;
1795295Srandyf 	kstat_ctl_t *kc;
1805295Srandyf 	kstat_t *ksp;
1815295Srandyf 	kstat_named_t *dp;
1825295Srandyf 	smbios_info_t info;
1835295Srandyf 	int preferred_pm_profile = 0;
1845295Srandyf 	char yesstr[32], nostr[32];	/* DEBUG */
1855295Srandyf 
1865295Srandyf 	*didyes = 0;
1875295Srandyf 
1885295Srandyf 	strncpy(yesstr, pm_map(yes), sizeof (yesstr));
1895295Srandyf 	strncpy(nostr, pm_map(no), sizeof (nostr));
1905295Srandyf 	mesg(MDEBUG, "S3_helper(%s, %s, %s, %s, %s, %s)\n", whitelist,
1915295Srandyf 	    blacklist, yesstr, nostr, keyword, behavior);
1925295Srandyf 	if ((kc = kstat_open()) == NULL) {
1935295Srandyf 		mesg(MDEBUG, "kstat_open failed\n");
1945295Srandyf 		return (OKUP);
1955295Srandyf 	}
1965295Srandyf 	ksp = kstat_lookup(kc, "acpi", -1, "acpi");
1975295Srandyf 	if (ksp == NULL) {
1985295Srandyf 		mesg(MDEBUG, "kstat_lookup 'acpi', -1, 'acpi' failed\n");
1995295Srandyf 		kstat_close(kc);
2005295Srandyf 		return (OKUP);
2015295Srandyf 	}
2025295Srandyf 	(void) kstat_read(kc, ksp,  NULL);
2035295Srandyf 	dp = kstat_data_lookup(ksp, "S3");
2045295Srandyf 	if (dp == NULL || dp->value.l == 0) {
2055295Srandyf 		mesg(MDEBUG, "kstat_data_lookup 'S3' fails\n");
2065295Srandyf 		if (dp != NULL)
2075295Srandyf 			mesg(MDEBUG, "value.l %lx\n", dp->value.l);
2085295Srandyf 		kstat_close(kc);
2095295Srandyf 		return (do_ioctl(no, keyword, behavior, suppress));
2105295Srandyf 	}
2115295Srandyf 	mesg(MDEBUG, "kstat indicates S3 support (%lx)\n", dp->value.l);
2125295Srandyf 
2135295Srandyf 	if (!whitelist_only) {
2145295Srandyf 		/*
2155295Srandyf 		 * We still have an ACPI ksp, search it again for
2165295Srandyf 		 * 'preferred_pm_profile' (needs to be valid if we don't
2175295Srandyf 		 * aren't only using a whitelist).
2185295Srandyf 		 */
2195295Srandyf 		dp = kstat_data_lookup(ksp, "preferred_pm_profile");
2205295Srandyf 		if (dp == NULL) {
2215295Srandyf 			mesg(MDEBUG, "kstat_data_lookup 'ppmp fails\n");
2225295Srandyf 			kstat_close(kc);
2235295Srandyf 			return (do_ioctl(no, keyword, behavior, suppress));
2245295Srandyf 		}
2256171Smh27603 		mesg(MDEBUG, "kstat indicates preferred_pm_profile is %lx\n",
2265295Srandyf 		    dp->value.l);
2275295Srandyf 		preferred_pm_profile = dp->value.l;
2285295Srandyf 	}
2295295Srandyf 	kstat_close(kc);
2305295Srandyf 
2315295Srandyf 	if ((shp = smbios_open(NULL,
2325295Srandyf 	    SMB_VERSION, oflags, &ret)) == NULL) {
2335295Srandyf 		/* we promised not to complain */
2345295Srandyf 		/* we bail leaving it to the kernel default */
2355295Srandyf 		mesg(MDEBUG, "smbios_open failed %d\n", errno);
2365295Srandyf 		return (OKUP);
2375295Srandyf 	}
2385295Srandyf 	if ((id = smbios_info_system(shp, &sys)) == SMB_ERR) {
2395295Srandyf 		mesg(MDEBUG, "smbios_info_system failed %d\n", errno);
2405295Srandyf 		smbios_close(shp);
2415295Srandyf 		return (OKUP);
2425295Srandyf 	}
2435295Srandyf 	if (smbios_info_common(shp, id, &info) == SMB_ERR) {
2445295Srandyf 		mesg(MDEBUG, "smbios_info_common failed %d\n", errno);
2455295Srandyf 		smbios_close(shp);
2465295Srandyf 		return (OKUP);
2475295Srandyf 	}
2485295Srandyf 	mesg(MDEBUG, "Manufacturer: %s\n", info.smbi_manufacturer);
2495295Srandyf 	mesg(MDEBUG, "Product: %s\n", info.smbi_product);
2505295Srandyf 	smbios_close(shp);
2515295Srandyf 
2525295Srandyf 	if (!whitelist_only) {
2535295Srandyf #define	PPP_DESKTOP 1
2545295Srandyf #define	PPP_WORKSTATION 3
2555295Srandyf 		if (strcmp(info.smbi_manufacturer, "Sun Microsystems") == 0 &&
2565295Srandyf 		    (preferred_pm_profile == PPP_DESKTOP ||
2575295Srandyf 		    preferred_pm_profile == PPP_WORKSTATION)) {
2585295Srandyf 			if (isonlist(blacklist,
2595295Srandyf 			    info.smbi_manufacturer, info.smbi_product)) {
2605295Srandyf 				return (do_ioctl(no, keyword, behavior,
2615295Srandyf 				    suppress));
2625295Srandyf 			} else {
2635295Srandyf 				ret = do_ioctl(yes, keyword, behavior,
2645295Srandyf 				    suppress);
2655295Srandyf 				*didyes = (ret == OKUP);
2665295Srandyf 				return (ret);
2675295Srandyf 			}
2685295Srandyf 		}
2695295Srandyf 	}
2705295Srandyf 	if (isonlist(whitelist,
2715295Srandyf 	    info.smbi_manufacturer, info.smbi_product)) {
2725295Srandyf 		ret = do_ioctl(yes, keyword, behavior, suppress);
2735295Srandyf 		*didyes = (ret == OKUP);
2745295Srandyf 		return (ret);
2755295Srandyf 	} else {
2765295Srandyf 		return (do_ioctl(no, keyword, behavior, suppress));
2775295Srandyf 	}
2785295Srandyf }
2795295Srandyf 
2805295Srandyf int
2815295Srandyf S3sup(void)	/* S3-support keyword handler */
2825295Srandyf {
2835295Srandyf 	struct btoc {
2845295Srandyf 		char *behavior;
2855295Srandyf 		int cmd;
2865295Srandyf 	};
2875295Srandyf 	static struct btoc blist[] = {
2885295Srandyf 		"default",	PM_DEFAULT_ALGORITHM,
2895295Srandyf 		"enable",	PM_ENABLE_S3,
2905295Srandyf 		"disable",	PM_DISABLE_S3,
2915295Srandyf 		NULL,		0
2925295Srandyf 	};
2935295Srandyf 	struct btoc *bp;
2945295Srandyf 	char *behavior;
2955295Srandyf 	int dontcare;
2965295Srandyf 
2975295Srandyf 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
2985295Srandyf 		if (strcmp(behavior, bp->behavior) == 0)
2995295Srandyf 			break;
3005295Srandyf 	}
3015295Srandyf 	if (bp->cmd == 0) {
3025295Srandyf 		mesg(MERR, "invalid S3-support behavior \"%s\"\n", behavior);
3035295Srandyf 		return (NOUP);
3045295Srandyf 	}
3055295Srandyf 
3065295Srandyf 
3075295Srandyf 	switch (bp->cmd) {
3085295Srandyf 
3095295Srandyf 	case PM_ENABLE_S3:
3105295Srandyf 	case PM_DISABLE_S3:
3115295Srandyf 		return (do_ioctl(bp->cmd, "S3-support", behavior, EBUSY));
3125295Srandyf 
3135295Srandyf 	case PM_DEFAULT_ALGORITHM:
3145295Srandyf 		/*
3155295Srandyf 		 * we suppress errors in the "default" case because we
3165295Srandyf 		 * already did an invisible default call, so we know we'll
3175295Srandyf 		 * get EBUSY
3185295Srandyf 		 */
3195295Srandyf 		return (S3_helper("S3-support-enable", "S3-support-disable",
3205295Srandyf 		    PM_ENABLE_S3, PM_DISABLE_S3, "S3-support", behavior,
3215295Srandyf 		    &dontcare, EBUSY));
3225295Srandyf 
3235295Srandyf 	default:
3245295Srandyf 		mesg(MERR, "S3-support %s failed, %s\n", behavior,
3255295Srandyf 		    strerror(errno));
3265295Srandyf 		return (NOUP);
3275295Srandyf 	}
3285295Srandyf }
3295295Srandyf 
3305295Srandyf /*
3315295Srandyf  * Check for valid autoS3 behavior and save after ioctl success.
3325295Srandyf  */
3335295Srandyf int
3345295Srandyf autoS3(void)
3355295Srandyf {
3365295Srandyf 	struct btoc {
3375295Srandyf 		char *behavior;
3385295Srandyf 		int cmd;
3395295Srandyf 	};
3405295Srandyf 	static struct btoc blist[] = {
3415295Srandyf 		"default",	PM_DEFAULT_ALGORITHM,
3425295Srandyf 		"disable",	PM_STOP_AUTOS3,
3435295Srandyf 		"enable",	PM_START_AUTOS3,
3445295Srandyf 		NULL,		0
3455295Srandyf 	};
3465295Srandyf 	struct btoc *bp;
3475295Srandyf 	char *behavior;
3485295Srandyf 	int dontcare;
3495295Srandyf 
3505295Srandyf 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
3515295Srandyf 		if (strcmp(behavior, bp->behavior) == 0)
3525295Srandyf 			break;
3535295Srandyf 	}
3545295Srandyf 	if (bp->cmd == 0) {
3555295Srandyf 		mesg(MERR, "invalid autoS3 behavior \"%s\"\n", behavior);
3565295Srandyf 		return (NOUP);
3575295Srandyf 	}
3585295Srandyf 
3595295Srandyf 	switch (bp->cmd) {
3605295Srandyf 	default:
3615295Srandyf 		mesg(MERR, "autoS3 %s failed, %s\n",
3625295Srandyf 		    behavior, strerror(errno));
3635295Srandyf 		mesg(MDEBUG, "unknown command\n", bp->cmd);
3645295Srandyf 		return (OKUP);
3655295Srandyf 
3665295Srandyf 	case PM_STOP_AUTOS3:
3675295Srandyf 	case PM_START_AUTOS3:
3685295Srandyf 		return (do_ioctl(bp->cmd, "autoS3", behavior, EBUSY));
3695295Srandyf 
3705295Srandyf 	case PM_DEFAULT_ALGORITHM:
3715295Srandyf 		return (S3_helper("S3-autoenable", "S3-autodisable",
3725295Srandyf 		    PM_START_AUTOS3, PM_STOP_AUTOS3, "autoS3", behavior,
3735295Srandyf 		    &dontcare, EBUSY));
3745295Srandyf 	}
3755295Srandyf }
3765295Srandyf 
3775295Srandyf 
3785295Srandyf /*
3790Sstevel@tonic-gate  * Check for valid autopm behavior and save after ioctl success.
3800Sstevel@tonic-gate  */
3810Sstevel@tonic-gate int
3820Sstevel@tonic-gate autopm(void)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	struct btoc {
3850Sstevel@tonic-gate 		char *behavior;
3860Sstevel@tonic-gate 		int cmd, Errno, isdef;
3870Sstevel@tonic-gate 	};
3880Sstevel@tonic-gate 	static struct btoc blist[] = {
3895295Srandyf 		"default",	PM_START_PM,	-1,	1,
3900Sstevel@tonic-gate 		"disable",	PM_STOP_PM,	EINVAL,	0,
3910Sstevel@tonic-gate 		"enable",	PM_START_PM,	EBUSY,	0,
3920Sstevel@tonic-gate 		NULL,		0,		0,	0,
3930Sstevel@tonic-gate 	};
3940Sstevel@tonic-gate 	struct btoc *bp;
3950Sstevel@tonic-gate 	char *behavior;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
3980Sstevel@tonic-gate 		if (strcmp(behavior, bp->behavior) == 0)
3990Sstevel@tonic-gate 			break;
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 	if (bp->cmd == 0) {
4020Sstevel@tonic-gate 		mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior);
4030Sstevel@tonic-gate 		return (NOUP);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	/*
4070Sstevel@tonic-gate 	 * for "default" behavior, do not enable autopm if not ESTAR_V3
4080Sstevel@tonic-gate 	 */
4095295Srandyf #if defined(__sparc)
4100Sstevel@tonic-gate 	if (!bp->isdef || (estar_vers == ESTAR_V3)) {
4110Sstevel@tonic-gate 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
4120Sstevel@tonic-gate 			mesg(MERR, "autopm %s failed, %s\n",
4130Sstevel@tonic-gate 			    behavior, strerror(errno));
4140Sstevel@tonic-gate 			return (NOUP);
4150Sstevel@tonic-gate 		}
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 	(void) strcpy(new_cc.apm_behavior, behavior);
4180Sstevel@tonic-gate 	return (OKUP);
4195295Srandyf #endif
4205295Srandyf #if defined(__x86)
4215295Srandyf 	if (!bp->isdef) {
4225295Srandyf 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
4235295Srandyf 			mesg(MERR, "autopm %s failed, %s\n",
4245295Srandyf 			    behavior, strerror(errno));
4255295Srandyf 			return (NOUP);
4265295Srandyf 		}
4275295Srandyf 		mesg(MDEBUG, "autopm %s succeeded\n", behavior);
4285295Srandyf 
4295295Srandyf 		return (OKUP);
4305295Srandyf 	} else {
4315295Srandyf 		int didenable;
4325295Srandyf 		int ret = S3_helper("autopm-enable", "autopm-disable",
4335295Srandyf 		    PM_START_PM, PM_STOP_PM, "autopm", behavior, &didenable,
4345295Srandyf 		    bp->Errno);
4355295Srandyf 		if (didenable) {
4365295Srandyf 			/* tell powerd to attach all devices */
4375295Srandyf 			new_cc.is_autopm_default = 1;
4385295Srandyf 			(void) strcpy(new_cc.apm_behavior, behavior);
4395295Srandyf 		}
4405295Srandyf 		return (ret);
4415295Srandyf 	}
4425295Srandyf #endif
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate static int
4470Sstevel@tonic-gate gethm(char *src, int *hour, int *min)
4480Sstevel@tonic-gate {
4490Sstevel@tonic-gate 	if (sscanf(src, "%d:%d", hour, min) != 2) {
4500Sstevel@tonic-gate 		mesg(MERR, "bad time format (%s)\n", src);
4510Sstevel@tonic-gate 		return (-1);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	return (0);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate static void
4580Sstevel@tonic-gate strcpy_limit(char *dst, char *src, size_t limit, char *info)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	if (strlcpy(dst, src, limit) >= limit)
4610Sstevel@tonic-gate 		mesg(MEXIT, "%s is too long (%s)\n", info, src);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate  * Convert autoshutdown idle and start/finish times;
4670Sstevel@tonic-gate  * check and record autoshutdown behavior.
4680Sstevel@tonic-gate  */
4690Sstevel@tonic-gate int
4700Sstevel@tonic-gate autosd(void)
4710Sstevel@tonic-gate {
472763Smh27603 	char **bp, *behavior;
473763Smh27603 	char *unrec = gettext("unrecognized autoshutdown behavior");
4740Sstevel@tonic-gate 	static char *blist[] = {
4750Sstevel@tonic-gate 		"autowakeup", "default", "noshutdown",
4760Sstevel@tonic-gate 		"shutdown", "unconfigured", NULL
4770Sstevel@tonic-gate 	};
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	new_cc.as_idle = atoi(LINEARG(1));
4800Sstevel@tonic-gate 	if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) ||
4810Sstevel@tonic-gate 	    gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm))
4820Sstevel@tonic-gate 		return (NOUP);
483763Smh27603 	mesg(MDEBUG, "idle %d, start %d:%02d, finish %d:%02d\n",
4840Sstevel@tonic-gate 	    new_cc.as_idle, new_cc.as_sh, new_cc.as_sm,
4850Sstevel@tonic-gate 	    new_cc.as_fh, new_cc.as_fm);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	for (behavior = LINEARG(4), bp = blist; *bp; bp++) {
4880Sstevel@tonic-gate 		if (strcmp(behavior, *bp) == 0)
4890Sstevel@tonic-gate 			break;
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 	if (*bp == NULL) {
4920Sstevel@tonic-gate 		mesg(MERR, "%s: \"%s\"\n", unrec, behavior);
4930Sstevel@tonic-gate 		return (NOUP);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 	STRCPYLIM(new_cc.as_behavior, *bp, unrec);
4960Sstevel@tonic-gate 	return (OKUP);
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  * Check for a real device and try to resolve to a full path.
5020Sstevel@tonic-gate  * The orig/resolved path may be modified into a prom pathname,
5030Sstevel@tonic-gate  * and an allocated copy of the result is stored at *destp;
5040Sstevel@tonic-gate  * the caller will need to free that space.  Returns 1 for any
5050Sstevel@tonic-gate  * error, otherwise 0; also sets *errp after an alloc error.
5060Sstevel@tonic-gate  */
5070Sstevel@tonic-gate static int
5080Sstevel@tonic-gate devpath(char **destp, char *src, int *errp)
5090Sstevel@tonic-gate {
5100Sstevel@tonic-gate 	struct stat stbuf;
5110Sstevel@tonic-gate 	char buf[PATH_MAX];
5120Sstevel@tonic-gate 	char *cp, *dstr;
5130Sstevel@tonic-gate 	int devok, dcs = 0;
5140Sstevel@tonic-gate 	size_t len;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/*
5170Sstevel@tonic-gate 	 * When there's a real device, try to resolve the path
5180Sstevel@tonic-gate 	 * and trim the leading "/devices" component.
5190Sstevel@tonic-gate 	 */
5200Sstevel@tonic-gate 	if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) {
5210Sstevel@tonic-gate 		if (realpath(src, buf) == NULL) {
5220Sstevel@tonic-gate 			mesg(MERR, "realpath cannot resolve \"%s\"\n",
5230Sstevel@tonic-gate 			    src, strerror(errno));
5240Sstevel@tonic-gate 			return (1);
5250Sstevel@tonic-gate 		}
5260Sstevel@tonic-gate 		src = buf;
5270Sstevel@tonic-gate 		dstr = "/devices";
5280Sstevel@tonic-gate 		len = strlen(dstr);
5290Sstevel@tonic-gate 		dcs = (strncmp(src, dstr, len) == 0);
5300Sstevel@tonic-gate 		if (dcs)
5310Sstevel@tonic-gate 			src += len;
5320Sstevel@tonic-gate 	} else
5330Sstevel@tonic-gate 		mesg(MDEBUG, stat_fmt, src, strerror(errno));
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/*
5360Sstevel@tonic-gate 	 * When the path has ":anything", display an error for
5370Sstevel@tonic-gate 	 * a non-device or truncate a resolved+modifed path.
5380Sstevel@tonic-gate 	 */
5390Sstevel@tonic-gate 	if (cp = strchr(src, ':')) {
5400Sstevel@tonic-gate 		if (devok == 0) {
5410Sstevel@tonic-gate 			mesg(MERR, "physical path may not contain "
5420Sstevel@tonic-gate 			    "a minor string (%s)\n", src);
5430Sstevel@tonic-gate 			return (1);
5440Sstevel@tonic-gate 		} else if (dcs)
5450Sstevel@tonic-gate 			*cp = '\0';
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	if ((*destp = strdup(src)) == NULL) {
5490Sstevel@tonic-gate 		*errp = NOUP;
5500Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, src, strerror(errno));
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 	return (*destp == NULL);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate /*
5570Sstevel@tonic-gate  * Call pm ioctl request(s) to set property/device dependencies.
5580Sstevel@tonic-gate  */
5590Sstevel@tonic-gate static int
5600Sstevel@tonic-gate dev_dep_common(int isprop)
5610Sstevel@tonic-gate {
5620Sstevel@tonic-gate 	int cmd, argn, upval = OKUP;
5630Sstevel@tonic-gate 	char *src, *first, **destp;
5640Sstevel@tonic-gate 	pm_req_t pmreq;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
5670Sstevel@tonic-gate 	src = LINEARG(1);
5680Sstevel@tonic-gate 	if (isprop) {
5690Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT_PROPERTY;
5700Sstevel@tonic-gate 		first = NULL;
5710Sstevel@tonic-gate 		pmreq.pmreq_kept = src;
5720Sstevel@tonic-gate 	} else {
5730Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT;
5740Sstevel@tonic-gate 		if (devpath(&first, src, &upval))
5750Sstevel@tonic-gate 			return (upval);
5760Sstevel@tonic-gate 		pmreq.pmreq_kept = first;
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 	destp = &pmreq.pmreq_keeper;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	/*
5810Sstevel@tonic-gate 	 * Now loop through any dependents.
5820Sstevel@tonic-gate 	 */
5830Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
5840Sstevel@tonic-gate 		if (devpath(destp, src, &upval)) {
5850Sstevel@tonic-gate 			if (upval != OKUP)
5860Sstevel@tonic-gate 				return (upval);
5870Sstevel@tonic-gate 			break;
5880Sstevel@tonic-gate 		}
5890Sstevel@tonic-gate 		if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) {
5900Sstevel@tonic-gate 			mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n"
5910Sstevel@tonic-gate 			    "kept \"%s\", keeper \"%s\"\n",
5920Sstevel@tonic-gate 			    cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper);
5930Sstevel@tonic-gate 			mesg(MERR, "cannot set \"%s\" dependency "
5940Sstevel@tonic-gate 			    "for \"%s\", %s\n", pmreq.pmreq_keeper,
5950Sstevel@tonic-gate 			    pmreq.pmreq_kept, strerror(errno));
5960Sstevel@tonic-gate 		}
5970Sstevel@tonic-gate 		free(*destp);
5980Sstevel@tonic-gate 		*destp = NULL;
5990Sstevel@tonic-gate 		if (upval != OKUP)
6000Sstevel@tonic-gate 			break;
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	free(first);
6040Sstevel@tonic-gate 	return (upval);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate int
6090Sstevel@tonic-gate ddprop(void)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate 	return (dev_dep_common(1));
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate int
6160Sstevel@tonic-gate devdep(void)
6170Sstevel@tonic-gate {
6180Sstevel@tonic-gate 	return (dev_dep_common(0));
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate /*
6230Sstevel@tonic-gate  * Convert a numeric string (with a possible trailing scaling byte)
6240Sstevel@tonic-gate  * into an integer.  Returns a converted value and *nerrp unchanged,
6250Sstevel@tonic-gate  * or 0 with *nerrp set to 1 for a conversion error.
6260Sstevel@tonic-gate  */
6270Sstevel@tonic-gate static int
6280Sstevel@tonic-gate get_scaled_value(char *str, int *nerrp)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate 	longlong_t svalue = 0, factor = 1;
6310Sstevel@tonic-gate 	char *sp;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	errno = 0;
6340Sstevel@tonic-gate 	svalue = strtol(str, &sp, 0);
6350Sstevel@tonic-gate 	if (errno || (*str != '-' && (*str < '0' || *str > '9')))
6360Sstevel@tonic-gate 		*nerrp = 1;
6370Sstevel@tonic-gate 	else if (sp && *sp != '\0') {
6380Sstevel@tonic-gate 		if (*sp == 'h')
6390Sstevel@tonic-gate 			factor = 3600;
6400Sstevel@tonic-gate 		else if (*sp == 'm')
6410Sstevel@tonic-gate 			factor = 60;
6420Sstevel@tonic-gate 		else if (*sp != 's')
6430Sstevel@tonic-gate 			*nerrp = 1;
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 	/* any bytes following sp are ignored */
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	if (*nerrp == 0) {
6480Sstevel@tonic-gate 		svalue *= factor;
6490Sstevel@tonic-gate 		if (svalue < INT_MIN || svalue > INT_MAX)
6500Sstevel@tonic-gate 			*nerrp = 1;
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 	if (*nerrp)
6530Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, str);
6540Sstevel@tonic-gate 	mesg(MDEBUG, "got scaled value %d\n", (int)svalue);
6550Sstevel@tonic-gate 	return ((int)svalue);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate  * Increment the count of threshold values,
6610Sstevel@tonic-gate  * reallocate *vlistp and append another element.
6620Sstevel@tonic-gate  * Returns 1 on error, otherwise 0.
6630Sstevel@tonic-gate  */
6640Sstevel@tonic-gate static int
6650Sstevel@tonic-gate vlist_append(int **vlistp, int *vcntp, int value)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate 	(*vcntp)++;
6680Sstevel@tonic-gate 	if (*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp)))
6690Sstevel@tonic-gate 		*(*vlistp + *vcntp - 1) = value;
6700Sstevel@tonic-gate 	else
6710Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, "threshold list", strerror(errno));
6720Sstevel@tonic-gate 	return (*vlistp == NULL);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate /*
6770Sstevel@tonic-gate  * Convert a single threshold string or paren groups of thresh's as
6780Sstevel@tonic-gate  * described below.  All thresh's are saved to an allocated list at
6790Sstevel@tonic-gate  * *vlistp; the caller will need to free that space.  On return:
6800Sstevel@tonic-gate  * *vcntp is the count of the vlist array, and vlist is either
6810Sstevel@tonic-gate  * a single thresh or N groups of thresh's with a trailing zero:
6820Sstevel@tonic-gate  * (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0.
6830Sstevel@tonic-gate  * Returns 0 when all conversions were OK, and 1 for any syntax,
6840Sstevel@tonic-gate  * conversion, or alloc error.
6850Sstevel@tonic-gate  */
6860Sstevel@tonic-gate static int
6870Sstevel@tonic-gate get_thresh(int **vlistp, int *vcntp)
6880Sstevel@tonic-gate {
6890Sstevel@tonic-gate 	int argn, value, gci, grp_cnt = 0, paren = 0, nerr = 0;
6900Sstevel@tonic-gate 	char *rp, *src;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
6930Sstevel@tonic-gate 		if (*src == LPAREN) {
6940Sstevel@tonic-gate 			gci = *vcntp;
6950Sstevel@tonic-gate 			if (nerr = vlist_append(vlistp, vcntp, 0))
6960Sstevel@tonic-gate 				break;
6970Sstevel@tonic-gate 			paren = 1;
6980Sstevel@tonic-gate 			src++;
6990Sstevel@tonic-gate 		}
7000Sstevel@tonic-gate 		if (*(rp = LASTBYTE(src)) == RPAREN) {
7010Sstevel@tonic-gate 			if (paren) {
7020Sstevel@tonic-gate 				grp_cnt = *vcntp - gci;
7030Sstevel@tonic-gate 				*(*vlistp + gci) = grp_cnt;
7040Sstevel@tonic-gate 				paren = 0;
7050Sstevel@tonic-gate 				*rp = '\0';
7060Sstevel@tonic-gate 			} else {
7070Sstevel@tonic-gate 				nerr = 1;
7080Sstevel@tonic-gate 				break;
7090Sstevel@tonic-gate 			}
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		value = get_scaled_value(src, &nerr);
7130Sstevel@tonic-gate 		if (nerr || (nerr = vlist_append(vlistp, vcntp, value)))
7140Sstevel@tonic-gate 			break;
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	if (nerr == 0 && grp_cnt)
7180Sstevel@tonic-gate 		nerr = vlist_append(vlistp, vcntp, 0);
7190Sstevel@tonic-gate 	return (nerr);
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate /*
7240Sstevel@tonic-gate  * Set device thresholds from (3) formats:
7250Sstevel@tonic-gate  * 	path	"always-on"
7260Sstevel@tonic-gate  * 	path	time-spec: [0-9]+[{h,m,s}]
7270Sstevel@tonic-gate  *	path	(ts1 ts2 ...)+
7280Sstevel@tonic-gate  */
7290Sstevel@tonic-gate int
7300Sstevel@tonic-gate devthr(void)
7310Sstevel@tonic-gate {
7320Sstevel@tonic-gate 	int cmd, upval = OKUP, nthresh = 0, *vlist = NULL;
7330Sstevel@tonic-gate 	pm_req_t pmreq;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
7360Sstevel@tonic-gate 	if (devpath(&pmreq.physpath, LINEARG(1), &upval))
7370Sstevel@tonic-gate 		return (upval);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	if (strcmp(LINEARG(2), always_on) == 0) {
7400Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
7410Sstevel@tonic-gate 		pmreq.value = INT_MAX;
7420Sstevel@tonic-gate 	} else if (get_thresh(&vlist, &nthresh)) {
7430Sstevel@tonic-gate 		mesg(MERR, bad_thresh_fmt);
7440Sstevel@tonic-gate 		upval = NOUP;
7450Sstevel@tonic-gate 	} else if (nthresh == 1) {
7460Sstevel@tonic-gate 		pmreq.value = *vlist;
7470Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
7480Sstevel@tonic-gate 	} else {
7490Sstevel@tonic-gate 		pmreq.data = vlist;
7500Sstevel@tonic-gate 		pmreq.datasize = (nthresh * sizeof (*vlist));
7510Sstevel@tonic-gate 		cmd = PM_SET_COMPONENT_THRESHOLDS;
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1)
7550Sstevel@tonic-gate 		mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno));
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	free(vlist);
7580Sstevel@tonic-gate 	free(pmreq.physpath);
7590Sstevel@tonic-gate 	return (upval);
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate static int
7640Sstevel@tonic-gate scan_int(char *src, int *dst)
7650Sstevel@tonic-gate {
7660Sstevel@tonic-gate 	long lval;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	errno = 0;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	lval = strtol(LINEARG(1), NULL, 0);
7710Sstevel@tonic-gate 	if (errno || lval > INT_MAX || lval < 0) {
7720Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
7730Sstevel@tonic-gate 		return (NOUP);
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	*dst = (int)lval;
7770Sstevel@tonic-gate 	return (OKUP);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate static int
7810Sstevel@tonic-gate scan_float(char *src, float *dst)
7820Sstevel@tonic-gate {
7830Sstevel@tonic-gate 	float fval;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	errno = 0;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	fval = strtof(src, NULL);
7880Sstevel@tonic-gate 	if (errno || fval < 0.0) {
7890Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
7900Sstevel@tonic-gate 		return (NOUP);
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	*dst = fval;
7940Sstevel@tonic-gate 	return (OKUP);
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate int
7990Sstevel@tonic-gate dreads(void)
8000Sstevel@tonic-gate {
8010Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.diskreads_thold));
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate /*
8060Sstevel@tonic-gate  * Set pathname for idlecheck;
8070Sstevel@tonic-gate  * an overflowed pathname is treated as a fatal error.
8080Sstevel@tonic-gate  */
8090Sstevel@tonic-gate int
8100Sstevel@tonic-gate idlechk(void)
8110Sstevel@tonic-gate {
8120Sstevel@tonic-gate 	STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path");
8130Sstevel@tonic-gate 	return (OKUP);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate int
8180Sstevel@tonic-gate loadavg(void)
8190Sstevel@tonic-gate {
8200Sstevel@tonic-gate 	return (scan_float(LINEARG(1), &new_cc.loadaverage_thold));
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate int
8250Sstevel@tonic-gate nfsreq(void)
8260Sstevel@tonic-gate {
8270Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold));
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate #ifdef sparc
8310Sstevel@tonic-gate static char open_fmt[] = "cannot open \"%s\", %s\n";
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate /*
8340Sstevel@tonic-gate  * Verify the filesystem type for a regular statefile is "ufs"
8350Sstevel@tonic-gate  * or verify a block device is not in use as a mounted filesytem.
8360Sstevel@tonic-gate  * Returns 1 if any error, otherwise 0.
8370Sstevel@tonic-gate  */
8380Sstevel@tonic-gate static int
8390Sstevel@tonic-gate check_mount(char *sfile, dev_t sfdev, int ufs)
8400Sstevel@tonic-gate {
8410Sstevel@tonic-gate 	char *src, *err_fmt = NULL, *mnttab = MNTTAB;
8420Sstevel@tonic-gate 	int rgent, match = 0;
843*6423Sgw25295 	struct mnttab zroot = { 0 };
844*6423Sgw25295 	struct mnttab entry;
8450Sstevel@tonic-gate 	struct extmnttab ent;
8460Sstevel@tonic-gate 	FILE *fp;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
8490Sstevel@tonic-gate 		mesg(MERR, open_fmt, mnttab, strerror(errno));
8500Sstevel@tonic-gate 		return (1);
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
853*6423Sgw25295 	if (ufs) {
854*6423Sgw25295 		zroot.mnt_mountp = "/";
855*6423Sgw25295 		zroot.mnt_fstype = "zfs";
856*6423Sgw25295 		if (getmntany(fp, &entry, &zroot) == 0) {
857*6423Sgw25295 			err_fmt = "ufs statefile with zfs root is not"
858*6423Sgw25295 			    " supported\n";
859*6423Sgw25295 			mesg(MERR, err_fmt, sfile);
860*6423Sgw25295 			fclose(fp);
861*6423Sgw25295 			return (1);
862*6423Sgw25295 		}
863*6423Sgw25295 		resetmnttab(fp);
864*6423Sgw25295 	}
8650Sstevel@tonic-gate 	/*
8660Sstevel@tonic-gate 	 * Search for a matching dev_t;
8670Sstevel@tonic-gate 	 * ignore non-ufs filesystems for a regular statefile.
8680Sstevel@tonic-gate 	 */
8690Sstevel@tonic-gate 	while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) {
8700Sstevel@tonic-gate 		if (rgent > 0) {
8710Sstevel@tonic-gate 			mesg(MERR, "error reading \"%s\"\n", mnttab);
8720Sstevel@tonic-gate 			(void) fclose(fp);
8730Sstevel@tonic-gate 			return (1);
8740Sstevel@tonic-gate 		} else if (ufs && strcmp(ent.mnt_fstype, "ufs"))
8750Sstevel@tonic-gate 			continue;
8760Sstevel@tonic-gate 		else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) {
8770Sstevel@tonic-gate 			match = 1;
8780Sstevel@tonic-gate 			break;
8790Sstevel@tonic-gate 		}
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	/*
8830Sstevel@tonic-gate 	 * No match is needed for a block device statefile,
8840Sstevel@tonic-gate 	 * a match is needed for a regular statefile.
8850Sstevel@tonic-gate 	 */
8860Sstevel@tonic-gate 	if (match == 0) {
887*6423Sgw25295 		if (new_cc.cf_type != CFT_UFS)
8880Sstevel@tonic-gate 			STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile");
8890Sstevel@tonic-gate 		else
8900Sstevel@tonic-gate 			err_fmt = "cannot find ufs mount point for \"%s\"\n";
8910Sstevel@tonic-gate 	} else if (new_cc.cf_type == CFT_UFS) {
8920Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry");
8930Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special");
8940Sstevel@tonic-gate 		while (*(sfile + 1) == '/') sfile++;
8950Sstevel@tonic-gate 		src = sfile + strlen(ent.mnt_mountp);
8960Sstevel@tonic-gate 		while (*src == '/') src++;
8970Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_path, src, "statefile path");
8980Sstevel@tonic-gate 	} else
8990Sstevel@tonic-gate 		err_fmt = "statefile device \"%s\" is a mounted filesystem\n";
900*6423Sgw25295 	(void) fclose(fp);
9010Sstevel@tonic-gate 	if (err_fmt)
9020Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
9030Sstevel@tonic-gate 	return (err_fmt != NULL);
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate /*
9080Sstevel@tonic-gate  * Convert a Unix device to a prom device and save on success,
9090Sstevel@tonic-gate  * log any ioctl/conversion error.
9100Sstevel@tonic-gate  */
9110Sstevel@tonic-gate static int
912*6423Sgw25295 utop(char *fs_name, char *prom_name)
9130Sstevel@tonic-gate {
9140Sstevel@tonic-gate 	union obpbuf {
9150Sstevel@tonic-gate 		char	buf[OBP_MAXPATHLEN + sizeof (uint_t)];
9160Sstevel@tonic-gate 		struct	openpromio oppio;
9170Sstevel@tonic-gate 	};
9180Sstevel@tonic-gate 	union obpbuf oppbuf;
9190Sstevel@tonic-gate 	struct openpromio *opp;
9200Sstevel@tonic-gate 	char *promdev = "/dev/openprom";
9210Sstevel@tonic-gate 	int fd, upval;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	if ((fd = open(promdev, O_RDONLY)) == -1) {
9240Sstevel@tonic-gate 		mesg(MERR, open_fmt, promdev, strerror(errno));
9250Sstevel@tonic-gate 		return (NOUP);
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	opp = &oppbuf.oppio;
9290Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPATHLEN;
930*6423Sgw25295 	strcpy_limit(opp->oprom_array, fs_name,
9310Sstevel@tonic-gate 	    OBP_MAXPATHLEN, "statefile device");
9320Sstevel@tonic-gate 	upval = ioctl(fd, OPROMDEV2PROMNAME, opp);
9330Sstevel@tonic-gate 	(void) close(fd);
934*6423Sgw25295 	if (upval == OKUP) {
935*6423Sgw25295 		strcpy_limit(prom_name, opp->oprom_array, OBP_MAXPATHLEN,
936*6423Sgw25295 		    "prom device");
937*6423Sgw25295 	} else {
9380Sstevel@tonic-gate 		openlog("pmconfig", 0, LOG_DAEMON);
9390Sstevel@tonic-gate 		syslog(LOG_NOTICE,
9400Sstevel@tonic-gate 		    gettext("cannot convert \"%s\" to prom device"),
941*6423Sgw25295 		    fs_name);
9420Sstevel@tonic-gate 		closelog();
9430Sstevel@tonic-gate 	}
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	return (upval);
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate 
948*6423Sgw25295 /*
949*6423Sgw25295  * given the path to a zvol, return the cXtYdZ name
950*6423Sgw25295  * returns < 0 on error, 0 if it isn't a zvol, > 1 on success
951*6423Sgw25295  */
952*6423Sgw25295 static int
953*6423Sgw25295 ztop(char *arg, char *diskname)
954*6423Sgw25295 {
955*6423Sgw25295 	zpool_handle_t *zpool_handle;
956*6423Sgw25295 	nvlist_t *config, *nvroot;
957*6423Sgw25295 	nvlist_t **child;
958*6423Sgw25295 	uint_t children;
959*6423Sgw25295 	libzfs_handle_t *lzfs;
960*6423Sgw25295 	char *vname;
961*6423Sgw25295 	char *p;
962*6423Sgw25295 	char pool_name[MAXPATHLEN];
963*6423Sgw25295 
964*6423Sgw25295 	if (strncmp(arg, "/dev/zvol/dsk/", 14)) {
965*6423Sgw25295 		return (0);
966*6423Sgw25295 	}
967*6423Sgw25295 	arg += 14;
968*6423Sgw25295 	strncpy(pool_name, arg, MAXPATHLEN);
969*6423Sgw25295 	if (p = strchr(pool_name, '/'))
970*6423Sgw25295 		*p = '\0';
971*6423Sgw25295 	STRCPYLIM(new_cc.cf_fs, p + 1, "statefile path");
972*6423Sgw25295 
973*6423Sgw25295 	if ((lzfs = libzfs_init()) == NULL) {
974*6423Sgw25295 		mesg(MERR, "failed to initialize ZFS library\n");
975*6423Sgw25295 		return (-1);
976*6423Sgw25295 	}
977*6423Sgw25295 	if ((zpool_handle = zpool_open(lzfs, pool_name)) == NULL) {
978*6423Sgw25295 		mesg(MERR, "couldn't open pool '%s'\n", pool_name);
979*6423Sgw25295 		libzfs_fini(lzfs);
980*6423Sgw25295 		return (-1);
981*6423Sgw25295 	}
982*6423Sgw25295 	config = zpool_get_config(zpool_handle, NULL);
983*6423Sgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
984*6423Sgw25295 	    &nvroot) != 0) {
985*6423Sgw25295 		zpool_close(zpool_handle);
986*6423Sgw25295 		libzfs_fini(lzfs);
987*6423Sgw25295 		return (-1);
988*6423Sgw25295 	}
989*6423Sgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
990*6423Sgw25295 	    &child, &children) == 0);
991*6423Sgw25295 	if (children != 1) {
992*6423Sgw25295 		mesg(MERR, "expected one vdev, got %d\n", children);
993*6423Sgw25295 		zpool_close(zpool_handle);
994*6423Sgw25295 		libzfs_fini(lzfs);
995*6423Sgw25295 		return (-1);
996*6423Sgw25295 	}
997*6423Sgw25295 	vname = zpool_vdev_name(lzfs, zpool_handle, child[0]);
998*6423Sgw25295 	if (vname == NULL) {
999*6423Sgw25295 		mesg(MERR, "couldn't determine vdev name\n");
1000*6423Sgw25295 		zpool_close(zpool_handle);
1001*6423Sgw25295 		libzfs_fini(lzfs);
1002*6423Sgw25295 		return (-1);
1003*6423Sgw25295 	}
1004*6423Sgw25295 	strcpy(diskname, "/dev/dsk/");
1005*6423Sgw25295 	strcat(diskname, vname);
1006*6423Sgw25295 	free(vname);
1007*6423Sgw25295 	zpool_close(zpool_handle);
1008*6423Sgw25295 	libzfs_fini(lzfs);
1009*6423Sgw25295 	return (1);
1010*6423Sgw25295 }
1011*6423Sgw25295 
1012*6423Sgw25295 /*
1013*6423Sgw25295  * returns NULL if the slice is good (e.g. does not start at block
1014*6423Sgw25295  * zero, or a string describing the error if it doesn't
1015*6423Sgw25295  */
1016*6423Sgw25295 static boolean_t
1017*6423Sgw25295 is_good_slice(char *sfile, char **err)
1018*6423Sgw25295 {
1019*6423Sgw25295 	int fd, rc;
1020*6423Sgw25295 	struct vtoc vtoc;
1021*6423Sgw25295 	dk_gpt_t *gpt;
1022*6423Sgw25295 	char rdskname[MAXPATHLEN];
1023*6423Sgw25295 	char *x, *y;
1024*6423Sgw25295 
1025*6423Sgw25295 	*err = NULL;
1026*6423Sgw25295 	/* convert from dsk to rdsk */
1027*6423Sgw25295 	STRCPYLIM(rdskname, sfile, "disk name");
1028*6423Sgw25295 	x = strstr(rdskname, "dsk/");
1029*6423Sgw25295 	y = strstr(sfile, "dsk/");
1030*6423Sgw25295 	if (x != NULL) {
1031*6423Sgw25295 		*x++ = 'r';
1032*6423Sgw25295 		strcpy(x, y);
1033*6423Sgw25295 	}
1034*6423Sgw25295 
1035*6423Sgw25295 	if ((fd = open(rdskname, O_RDONLY)) == -1) {
1036*6423Sgw25295 		*err = "could not open '%s'\n";
1037*6423Sgw25295 	} else if ((rc = read_vtoc(fd, &vtoc)) >= 0) {
1038*6423Sgw25295 		/*
1039*6423Sgw25295 		 * we got a slice number; now check the block
1040*6423Sgw25295 		 * number where the slice starts
1041*6423Sgw25295 		 */
1042*6423Sgw25295 		if (vtoc.v_part[rc].p_start < 2)
1043*6423Sgw25295 			*err = "using '%s' would clobber the disk label\n";
1044*6423Sgw25295 		close(fd);
1045*6423Sgw25295 		return (*err ? B_FALSE : B_TRUE);
1046*6423Sgw25295 	} else if ((rc == VT_ENOTSUP) &&
1047*6423Sgw25295 	    (efi_alloc_and_read(fd, &gpt)) >= 0) {
1048*6423Sgw25295 		/* EFI slices don't clobber the disk label */
1049*6423Sgw25295 		free(gpt);
1050*6423Sgw25295 		close(fd);
1051*6423Sgw25295 		return (B_TRUE);
1052*6423Sgw25295 	} else
1053*6423Sgw25295 		*err = "could not read partition table from '%s'\n";
1054*6423Sgw25295 	return (B_FALSE);
1055*6423Sgw25295 }
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate /*
10580Sstevel@tonic-gate  * Check for a valid statefile pathname, inode and mount status.
10590Sstevel@tonic-gate  */
10600Sstevel@tonic-gate int
10610Sstevel@tonic-gate sfpath(void)
10620Sstevel@tonic-gate {
10630Sstevel@tonic-gate 	static int statefile;
10640Sstevel@tonic-gate 	char *err_fmt = NULL;
10650Sstevel@tonic-gate 	char *sfile, *sp, ch;
1066*6423Sgw25295 	char diskname[256];
10670Sstevel@tonic-gate 	struct stat stbuf;
10680Sstevel@tonic-gate 	int dir = 0;
10690Sstevel@tonic-gate 	dev_t dev;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	if (statefile) {
10720Sstevel@tonic-gate 		mesg(MERR, "ignored redundant statefile entry\n");
10730Sstevel@tonic-gate 		return (OKUP);
10740Sstevel@tonic-gate 	} else if (ua_err) {
10750Sstevel@tonic-gate 		if (ua_err != ENOTSUP)
10760Sstevel@tonic-gate 			mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n",
10770Sstevel@tonic-gate 			    strerror(ua_err));
10780Sstevel@tonic-gate 		return (NOUP);
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	/*
10820Sstevel@tonic-gate 	 * Check for an absolute path and trim any trailing '/'.
10830Sstevel@tonic-gate 	 */
10840Sstevel@tonic-gate 	sfile = LINEARG(1);
10850Sstevel@tonic-gate 	if (*sfile != '/') {
10860Sstevel@tonic-gate 		mesg(MERR, "statefile requires an absolute path\n");
10870Sstevel@tonic-gate 		return (NOUP);
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 	for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--)
10900Sstevel@tonic-gate 		*sp = '\0';
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	/*
10930Sstevel@tonic-gate 	 * If the statefile doesn't exist, the leading path must be a dir.
10940Sstevel@tonic-gate 	 */
10950Sstevel@tonic-gate 	if (stat(sfile, &stbuf) == -1) {
10960Sstevel@tonic-gate 		if (errno == ENOENT) {
10970Sstevel@tonic-gate 			dir = 1;
10980Sstevel@tonic-gate 			if ((sp = strrchr(sfile, '/')) == sfile)
10990Sstevel@tonic-gate 				sp++;
11000Sstevel@tonic-gate 			ch = *sp;
11010Sstevel@tonic-gate 			*sp = '\0';
11020Sstevel@tonic-gate 			if (stat(sfile, &stbuf) == -1)
11030Sstevel@tonic-gate 				err_fmt = stat_fmt;
11040Sstevel@tonic-gate 			*sp = ch;
11050Sstevel@tonic-gate 		} else
11060Sstevel@tonic-gate 			err_fmt = stat_fmt;
11070Sstevel@tonic-gate 		if (err_fmt) {
11080Sstevel@tonic-gate 			mesg(MERR, err_fmt, sfile, strerror(errno));
11090Sstevel@tonic-gate 			return (NOUP);
11100Sstevel@tonic-gate 		}
11110Sstevel@tonic-gate 	}
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	/*
11140Sstevel@tonic-gate 	 * Check for regular/dir/block types, set cf_type and dev.
11150Sstevel@tonic-gate 	 */
11160Sstevel@tonic-gate 	if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) {
11170Sstevel@tonic-gate 		new_cc.cf_type = CFT_UFS;
11180Sstevel@tonic-gate 		dev = stbuf.st_dev;
11190Sstevel@tonic-gate 	} else if (S_ISBLK(stbuf.st_mode)) {
1120*6423Sgw25295 		if (is_good_slice(sfile, &err_fmt)) {
1121*6423Sgw25295 			switch (ztop(sfile, diskname)) {
1122*6423Sgw25295 				case 1:
1123*6423Sgw25295 					new_cc.cf_type = CFT_ZVOL;
1124*6423Sgw25295 					break;
1125*6423Sgw25295 				case 0:
1126*6423Sgw25295 					new_cc.cf_type = CFT_SPEC;
1127*6423Sgw25295 					break;
1128*6423Sgw25295 				case -1:
1129*6423Sgw25295 				default:
1130*6423Sgw25295 					return (NOUP);
1131*6423Sgw25295 			}
11320Sstevel@tonic-gate 			dev = stbuf.st_rdev;
1133*6423Sgw25295 		}
11340Sstevel@tonic-gate 	} else
11350Sstevel@tonic-gate 		err_fmt = "bad file type for \"%s\"\n"
11360Sstevel@tonic-gate 		    "statefile must be a regular file or block device\n";
11370Sstevel@tonic-gate 	if (err_fmt) {
11380Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
11390Sstevel@tonic-gate 		return (NOUP);
11400Sstevel@tonic-gate 	}
1141*6423Sgw25295 	if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)))
11420Sstevel@tonic-gate 		return (NOUP);
1143*6423Sgw25295 	if (new_cc.cf_type == CFT_ZVOL) {
1144*6423Sgw25295 		if (utop(diskname, new_cc.cf_dev_prom))
1145*6423Sgw25295 			return (NOUP);
1146*6423Sgw25295 	} else if (utop(new_cc.cf_devfs, new_cc.cf_dev_prom)) {
1147*6423Sgw25295 		return (NOUP);
1148*6423Sgw25295 	}
11490Sstevel@tonic-gate 	new_cc.cf_magic = CPR_CONFIG_MAGIC;
11500Sstevel@tonic-gate 	statefile = 1;
11510Sstevel@tonic-gate 	return (OKUP);
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate #endif /* sparc */
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate /*
11573028Smh27603  * Common function to set a system or cpu threshold.
11580Sstevel@tonic-gate  */
11593028Smh27603 static int
11603028Smh27603 cmnthr(int req)
11610Sstevel@tonic-gate {
11620Sstevel@tonic-gate 	int value, nerr = 0, upval = OKUP;
11630Sstevel@tonic-gate 	char *thresh = LINEARG(1);
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	if (strcmp(thresh, always_on) == 0)
11660Sstevel@tonic-gate 		value = INT_MAX;
11670Sstevel@tonic-gate 	else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) {
11680Sstevel@tonic-gate 		mesg(MERR, "%s must be a positive value\n", LINEARG(0));
11690Sstevel@tonic-gate 		upval = NOUP;
11700Sstevel@tonic-gate 	}
11710Sstevel@tonic-gate 	if (upval == OKUP)
11723028Smh27603 		(void) ioctl(pm_fd, req, value);
11730Sstevel@tonic-gate 	return (upval);
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 
11773028Smh27603 /*
11783028Smh27603  * Try setting system threshold.
11793028Smh27603  */
11803028Smh27603 int
11813028Smh27603 systhr(void)
11823028Smh27603 {
11833028Smh27603 	return (cmnthr(PM_SET_SYSTEM_THRESHOLD));
11843028Smh27603 }
11853028Smh27603 
11863028Smh27603 
11873028Smh27603 /*
11883028Smh27603  * Try setting cpu threshold.
11893028Smh27603  */
11903028Smh27603 int
11913028Smh27603 cputhr(void)
11923028Smh27603 {
11933028Smh27603 	return (cmnthr(PM_SET_CPU_THRESHOLD));
11943028Smh27603 }
11953028Smh27603 
11963028Smh27603 
11970Sstevel@tonic-gate int
11980Sstevel@tonic-gate tchars(void)
11990Sstevel@tonic-gate {
12000Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.ttychars_thold));
12010Sstevel@tonic-gate }
1202