xref: /titanic_53/usr/src/cmd/power/handlers.c (revision 88ecc943b4eb72f7c4fbbd8435997b85ef171fc3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c42872d4Smh27603  * Common Development and Distribution License (the "License").
6c42872d4Smh27603  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220e751525SEric Saxe  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include "pmconfig.h"
277c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
287c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
297c478bd9Sstevel@tonic-gate #include <sys/openpromio.h>
307c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
31e7cbe64fSgw25295 #include <sys/vtoc.h>
32e7cbe64fSgw25295 #include <sys/efi_partition.h>
337c478bd9Sstevel@tonic-gate #include <syslog.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
352df1fe9cSrandyf #include <sys/pm.h>
362df1fe9cSrandyf #include <kstat.h>
372df1fe9cSrandyf #include <sys/smbios.h>
38e7cbe64fSgw25295 #include <libzfs.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str)
427c478bd9Sstevel@tonic-gate #define	LASTBYTE(str) (str + strlen(str) - 1)
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static char nerr_fmt[] = "number is out of range (%s)\n";
457c478bd9Sstevel@tonic-gate static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n";
467c478bd9Sstevel@tonic-gate static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n";
477c478bd9Sstevel@tonic-gate static char bad_thresh_fmt[] = "bad threshold(s)\n";
487c478bd9Sstevel@tonic-gate static char stat_fmt[] = "cannot stat \"%s\", %s\n";
497c478bd9Sstevel@tonic-gate static char always_on[] = "always-on";
507c478bd9Sstevel@tonic-gate 
512df1fe9cSrandyf #define	PM_DEFAULT_ALGORITHM -1
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * When lines in a config file (usually "/etc/power.conf") start with
547c478bd9Sstevel@tonic-gate  * a recognized keyword, a "handler" routine is called for specific
557c478bd9Sstevel@tonic-gate  * CPR or PM -related action(s).  Each routine returns a status code
567c478bd9Sstevel@tonic-gate  * indicating whether all tasks were successful; if any errors occured,
577c478bd9Sstevel@tonic-gate  * future CPR or PM updates are skipped.  Following are the handler
587c478bd9Sstevel@tonic-gate  * routines for all keywords:
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 
622df1fe9cSrandyf static char pm_cmd_string[32];
632df1fe9cSrandyf 
642df1fe9cSrandyf static char *
652df1fe9cSrandyf pm_map(int cmd)
662df1fe9cSrandyf {
672df1fe9cSrandyf 	pm_req_t req;
682df1fe9cSrandyf 
692df1fe9cSrandyf 	req.value = cmd;
702df1fe9cSrandyf 	req.data = (void *)pm_cmd_string;
712df1fe9cSrandyf 	req.datasize = sizeof (pm_cmd_string);
722df1fe9cSrandyf 
732df1fe9cSrandyf 	if (ioctl(pm_fd, PM_GET_CMD_NAME, &req) < 0) {
7425aedaedSmh27603 		perror(gettext("PM_GET_CMD_NAME failed:"));
752df1fe9cSrandyf 		return ("??");
762df1fe9cSrandyf 	}
772df1fe9cSrandyf 	return (pm_cmd_string);
782df1fe9cSrandyf }
792df1fe9cSrandyf 
802df1fe9cSrandyf static int
812df1fe9cSrandyf isonlist(char *listname, const char *man, const char *prod)
822df1fe9cSrandyf {
832df1fe9cSrandyf 	pm_searchargs_t sl;
842df1fe9cSrandyf 	int ret;
852df1fe9cSrandyf 
862df1fe9cSrandyf 	sl.pms_listname = listname;
872df1fe9cSrandyf 	sl.pms_manufacturer = (char *)man;
882df1fe9cSrandyf 	sl.pms_product = (char *)prod;
892df1fe9cSrandyf 	ret = ioctl(pm_fd, PM_SEARCH_LIST, &sl);
902df1fe9cSrandyf 	mesg(MDEBUG, "PM_SEARCH_LIST %s for %s,%s returns %d\n",
912df1fe9cSrandyf 	    listname, man, prod, ret);
922df1fe9cSrandyf 	return (ret == 0);
932df1fe9cSrandyf }
942df1fe9cSrandyf 
952df1fe9cSrandyf static int
962df1fe9cSrandyf do_ioctl(int ioctl_cmd, char *keyword, char *behavior, int suppress)
972df1fe9cSrandyf {
982df1fe9cSrandyf 	mesg(MDEBUG, "doing ioctl %s for %s ", pm_map(ioctl_cmd), keyword);
992df1fe9cSrandyf 	if (ioctl(pm_fd, ioctl_cmd, NULL) == -1) {
1002df1fe9cSrandyf 		int suppressed = suppress == -1 || suppress == errno;
1012df1fe9cSrandyf 		if (!suppressed) {
1022df1fe9cSrandyf 			mesg(MERR, "%s %s failed, %s\n", keyword, behavior,
1032df1fe9cSrandyf 			    strerror(errno));
1042df1fe9cSrandyf 			return (NOUP);
1052df1fe9cSrandyf 		} else {
10625aedaedSmh27603 			mesg(MDEBUG, "%s %s failed, %s\n", keyword, behavior,
10725aedaedSmh27603 			    strerror(errno));
1082df1fe9cSrandyf 			return (OKUP);
1092df1fe9cSrandyf 		}
1102df1fe9cSrandyf 	}
1112df1fe9cSrandyf 	mesg(MDEBUG, "succeeded\n");
1122df1fe9cSrandyf 	return (OKUP);
1132df1fe9cSrandyf }
1142df1fe9cSrandyf 
1157c478bd9Sstevel@tonic-gate /*
116c42872d4Smh27603  * Check for valid cpupm behavior and communicate it to the kernel.
117c42872d4Smh27603  */
118c42872d4Smh27603 int
119c42872d4Smh27603 cpupm(void)
120c42872d4Smh27603 {
1210e751525SEric Saxe 	struct bmtoc {
122c42872d4Smh27603 		char *behavior;
1230e751525SEric Saxe 		char *mode;
124c42872d4Smh27603 		int cmd;
125c42872d4Smh27603 		int Errno;
126c42872d4Smh27603 	};
127c42872d4Smh27603 
1280e751525SEric Saxe 	static struct bmtoc bmlist[] = {
1290e751525SEric Saxe 		"disable",	"\0",		PM_STOP_CPUPM,		EINVAL,
1300e751525SEric Saxe 		"enable",	"poll-mode",	PM_START_CPUPM_POLL,	EBUSY,
1310e751525SEric Saxe 		"enable",	"event-mode",	PM_START_CPUPM_EV,	EBUSY,
1320e751525SEric Saxe 		"enable",	"\0",		PM_START_CPUPM,		EBUSY,
1330e751525SEric Saxe 		NULL,		0,		0,			0
1340e751525SEric Saxe 	};
1350e751525SEric Saxe 	struct bmtoc *bp;
1360e751525SEric Saxe 	char *behavior;
1370e751525SEric Saxe 	char *mode;
1380e751525SEric Saxe 
1390e751525SEric Saxe 	behavior = LINEARG(1);
1400e751525SEric Saxe 	if ((mode = LINEARG(2)) == NULL)
1410e751525SEric Saxe 		mode = "\0";
1420e751525SEric Saxe 
1430e751525SEric Saxe 	for (bp = bmlist; bp->cmd; bp++) {
1440e751525SEric Saxe 		if (strcmp(behavior, bp->behavior) == 0 &&
1450e751525SEric Saxe 		    strcmp(mode, bp->mode) == 0) {
146c42872d4Smh27603 			break;
147c42872d4Smh27603 		}
1480e751525SEric Saxe 	}
149c42872d4Smh27603 	if (bp->cmd == 0) {
1500e751525SEric Saxe 		if (LINEARG(2) == NULL) {
151c42872d4Smh27603 			mesg(MERR, "invalid cpupm behavior \"%s\"\n", behavior);
1520e751525SEric Saxe 		} else {
1530e751525SEric Saxe 			mesg(MERR, "invalid cpupm behavior \"%s %s\"\n",
1540e751525SEric Saxe 			    behavior, mode);
1550e751525SEric Saxe 		}
156c42872d4Smh27603 		return (NOUP);
157c42872d4Smh27603 	}
158c42872d4Smh27603 	if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
159c42872d4Smh27603 		mesg(MERR, "cpupm %s failed, %s\n",
160c42872d4Smh27603 		    behavior, strerror(errno));
161c42872d4Smh27603 		return (NOUP);
162c42872d4Smh27603 	}
163c42872d4Smh27603 	return (OKUP);
164c42872d4Smh27603 }
165c42872d4Smh27603 
1660e751525SEric Saxe /*
1670e751525SEric Saxe  * Check for valid cpu_deep_idle option and communicate it to the kernel.
1680e751525SEric Saxe  */
1690e751525SEric Saxe int
1700e751525SEric Saxe cpuidle(void)
1710e751525SEric Saxe {
1720e751525SEric Saxe 	struct btoc {
1730e751525SEric Saxe 		char *behavior;
1740e751525SEric Saxe 		int cmd;
1750e751525SEric Saxe 		int Errno;
1760e751525SEric Saxe 	};
1770e751525SEric Saxe 	static struct btoc blist[] = {
1780e751525SEric Saxe 		"disable",	PM_DISABLE_CPU_DEEP_IDLE, EINVAL,
1790e751525SEric Saxe 		"enable",	PM_ENABLE_CPU_DEEP_IDLE, EBUSY,
1800e751525SEric Saxe 		"default",	PM_DEFAULT_CPU_DEEP_IDLE, EBUSY,
1810e751525SEric Saxe 		NULL,		0, 0
1820e751525SEric Saxe 	};
1830e751525SEric Saxe 	struct btoc *bp;
1840e751525SEric Saxe 	char *behavior;
1850e751525SEric Saxe 
1860e751525SEric Saxe 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
1870e751525SEric Saxe 		if (strcmp(behavior, bp->behavior) == 0)
1880e751525SEric Saxe 			break;
1890e751525SEric Saxe 	}
1900e751525SEric Saxe 	if (bp->cmd == 0) {
1910e751525SEric Saxe 		mesg(MERR, "invalid cpu_deep_idle behavior \"%s\"\n", behavior);
1920e751525SEric Saxe 		return (NOUP);
1930e751525SEric Saxe 	}
1940e751525SEric Saxe 	if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
1950e751525SEric Saxe 		mesg(MERR, "cpu_deep_idle %s failed, %s\n",
1960e751525SEric Saxe 		    behavior, strerror(errno));
1970e751525SEric Saxe 		return (NOUP);
1980e751525SEric Saxe 	}
1990e751525SEric Saxe 	return (OKUP);
2000e751525SEric Saxe }
201c42872d4Smh27603 
202c42872d4Smh27603 /*
2032df1fe9cSrandyf  * Two decisions are identical except for the list names and ioctl commands
2042df1fe9cSrandyf  * inputs: whitelist, blacklist, yes, no
2052df1fe9cSrandyf  * if (! ("S3" kstat exists))
2062df1fe9cSrandyf  *	return (no)
2072df1fe9cSrandyf  * if (SystemInformation.Manufacturer == "Sun Microsystems" &&
2082df1fe9cSrandyf  *    (Pref_PM_Profile == Workstation || Pref_PM_Profile == Desktop)) {
2092df1fe9cSrandyf  *	if (platform on blacklist)
2102df1fe9cSrandyf  *		return (no)
2112df1fe9cSrandyf  *	return (yes)
2122df1fe9cSrandyf  * } else {
2132df1fe9cSrandyf  *	if (platform on whitelist)
2142df1fe9cSrandyf  *		return (yes)
2152df1fe9cSrandyf  *	return (no)
2162df1fe9cSrandyf  * }
2172df1fe9cSrandyf  */
2182df1fe9cSrandyf 
2192df1fe9cSrandyf int
2202df1fe9cSrandyf S3_helper(char *whitelist, char *blacklist, int yes, int no, char *keyword,
2212df1fe9cSrandyf 	char *behavior, int *didyes, int suppress)
2222df1fe9cSrandyf {
2232df1fe9cSrandyf 	int oflags = SMB_O_NOCKSUM | SMB_O_NOVERS;
2242df1fe9cSrandyf 	smbios_hdl_t *shp;
2252df1fe9cSrandyf 	smbios_system_t sys;
2262df1fe9cSrandyf 	id_t id;
2272df1fe9cSrandyf 	int ret;
2282df1fe9cSrandyf 	kstat_ctl_t *kc;
2292df1fe9cSrandyf 	kstat_t *ksp;
2302df1fe9cSrandyf 	kstat_named_t *dp;
2312df1fe9cSrandyf 	smbios_info_t info;
2322df1fe9cSrandyf 	int preferred_pm_profile = 0;
2332df1fe9cSrandyf 	char yesstr[32], nostr[32];	/* DEBUG */
2342df1fe9cSrandyf 
2352df1fe9cSrandyf 	*didyes = 0;
2362df1fe9cSrandyf 
2372df1fe9cSrandyf 	strncpy(yesstr, pm_map(yes), sizeof (yesstr));
2382df1fe9cSrandyf 	strncpy(nostr, pm_map(no), sizeof (nostr));
2392df1fe9cSrandyf 	mesg(MDEBUG, "S3_helper(%s, %s, %s, %s, %s, %s)\n", whitelist,
2402df1fe9cSrandyf 	    blacklist, yesstr, nostr, keyword, behavior);
2412df1fe9cSrandyf 	if ((kc = kstat_open()) == NULL) {
2422df1fe9cSrandyf 		mesg(MDEBUG, "kstat_open failed\n");
2432df1fe9cSrandyf 		return (OKUP);
2442df1fe9cSrandyf 	}
2452df1fe9cSrandyf 	ksp = kstat_lookup(kc, "acpi", -1, "acpi");
2462df1fe9cSrandyf 	if (ksp == NULL) {
2472df1fe9cSrandyf 		mesg(MDEBUG, "kstat_lookup 'acpi', -1, 'acpi' failed\n");
2482df1fe9cSrandyf 		kstat_close(kc);
2492df1fe9cSrandyf 		return (OKUP);
2502df1fe9cSrandyf 	}
2512df1fe9cSrandyf 	(void) kstat_read(kc, ksp,  NULL);
2522df1fe9cSrandyf 	dp = kstat_data_lookup(ksp, "S3");
2532df1fe9cSrandyf 	if (dp == NULL || dp->value.l == 0) {
2542df1fe9cSrandyf 		mesg(MDEBUG, "kstat_data_lookup 'S3' fails\n");
2552df1fe9cSrandyf 		if (dp != NULL)
2562df1fe9cSrandyf 			mesg(MDEBUG, "value.l %lx\n", dp->value.l);
2572df1fe9cSrandyf 		kstat_close(kc);
2582df1fe9cSrandyf 		return (do_ioctl(no, keyword, behavior, suppress));
2592df1fe9cSrandyf 	}
2602df1fe9cSrandyf 	mesg(MDEBUG, "kstat indicates S3 support (%lx)\n", dp->value.l);
2612df1fe9cSrandyf 
2622df1fe9cSrandyf 	if (!whitelist_only) {
2632df1fe9cSrandyf 		/*
2642df1fe9cSrandyf 		 * We still have an ACPI ksp, search it again for
2652df1fe9cSrandyf 		 * 'preferred_pm_profile' (needs to be valid if we don't
2662df1fe9cSrandyf 		 * aren't only using a whitelist).
2672df1fe9cSrandyf 		 */
2682df1fe9cSrandyf 		dp = kstat_data_lookup(ksp, "preferred_pm_profile");
2692df1fe9cSrandyf 		if (dp == NULL) {
2702df1fe9cSrandyf 			mesg(MDEBUG, "kstat_data_lookup 'ppmp fails\n");
2712df1fe9cSrandyf 			kstat_close(kc);
2722df1fe9cSrandyf 			return (do_ioctl(no, keyword, behavior, suppress));
2732df1fe9cSrandyf 		}
27425aedaedSmh27603 		mesg(MDEBUG, "kstat indicates preferred_pm_profile is %lx\n",
2752df1fe9cSrandyf 		    dp->value.l);
2762df1fe9cSrandyf 		preferred_pm_profile = dp->value.l;
2772df1fe9cSrandyf 	}
2782df1fe9cSrandyf 	kstat_close(kc);
2792df1fe9cSrandyf 
2802df1fe9cSrandyf 	if ((shp = smbios_open(NULL,
2812df1fe9cSrandyf 	    SMB_VERSION, oflags, &ret)) == NULL) {
2822df1fe9cSrandyf 		/* we promised not to complain */
2832df1fe9cSrandyf 		/* we bail leaving it to the kernel default */
2842df1fe9cSrandyf 		mesg(MDEBUG, "smbios_open failed %d\n", errno);
2852df1fe9cSrandyf 		return (OKUP);
2862df1fe9cSrandyf 	}
2872df1fe9cSrandyf 	if ((id = smbios_info_system(shp, &sys)) == SMB_ERR) {
2882df1fe9cSrandyf 		mesg(MDEBUG, "smbios_info_system failed %d\n", errno);
2892df1fe9cSrandyf 		smbios_close(shp);
2902df1fe9cSrandyf 		return (OKUP);
2912df1fe9cSrandyf 	}
2922df1fe9cSrandyf 	if (smbios_info_common(shp, id, &info) == SMB_ERR) {
2932df1fe9cSrandyf 		mesg(MDEBUG, "smbios_info_common failed %d\n", errno);
2942df1fe9cSrandyf 		smbios_close(shp);
2952df1fe9cSrandyf 		return (OKUP);
2962df1fe9cSrandyf 	}
2972df1fe9cSrandyf 	mesg(MDEBUG, "Manufacturer: %s\n", info.smbi_manufacturer);
2982df1fe9cSrandyf 	mesg(MDEBUG, "Product: %s\n", info.smbi_product);
2992df1fe9cSrandyf 	smbios_close(shp);
3002df1fe9cSrandyf 
3012df1fe9cSrandyf 	if (!whitelist_only) {
3022df1fe9cSrandyf #define	PPP_DESKTOP 1
3032df1fe9cSrandyf #define	PPP_WORKSTATION 3
3042df1fe9cSrandyf 		if (strcmp(info.smbi_manufacturer, "Sun Microsystems") == 0 &&
3052df1fe9cSrandyf 		    (preferred_pm_profile == PPP_DESKTOP ||
3062df1fe9cSrandyf 		    preferred_pm_profile == PPP_WORKSTATION)) {
3072df1fe9cSrandyf 			if (isonlist(blacklist,
3082df1fe9cSrandyf 			    info.smbi_manufacturer, info.smbi_product)) {
3092df1fe9cSrandyf 				return (do_ioctl(no, keyword, behavior,
3102df1fe9cSrandyf 				    suppress));
3112df1fe9cSrandyf 			} else {
3122df1fe9cSrandyf 				ret = do_ioctl(yes, keyword, behavior,
3132df1fe9cSrandyf 				    suppress);
3142df1fe9cSrandyf 				*didyes = (ret == OKUP);
3152df1fe9cSrandyf 				return (ret);
3162df1fe9cSrandyf 			}
3172df1fe9cSrandyf 		}
3182df1fe9cSrandyf 	}
3192df1fe9cSrandyf 	if (isonlist(whitelist,
3202df1fe9cSrandyf 	    info.smbi_manufacturer, info.smbi_product)) {
3212df1fe9cSrandyf 		ret = do_ioctl(yes, keyword, behavior, suppress);
3222df1fe9cSrandyf 		*didyes = (ret == OKUP);
3232df1fe9cSrandyf 		return (ret);
3242df1fe9cSrandyf 	} else {
3252df1fe9cSrandyf 		return (do_ioctl(no, keyword, behavior, suppress));
3262df1fe9cSrandyf 	}
3272df1fe9cSrandyf }
3282df1fe9cSrandyf 
3292df1fe9cSrandyf int
3302df1fe9cSrandyf S3sup(void)	/* S3-support keyword handler */
3312df1fe9cSrandyf {
3322df1fe9cSrandyf 	struct btoc {
3332df1fe9cSrandyf 		char *behavior;
3342df1fe9cSrandyf 		int cmd;
3352df1fe9cSrandyf 	};
3362df1fe9cSrandyf 	static struct btoc blist[] = {
3372df1fe9cSrandyf 		"default",	PM_DEFAULT_ALGORITHM,
3382df1fe9cSrandyf 		"enable",	PM_ENABLE_S3,
3392df1fe9cSrandyf 		"disable",	PM_DISABLE_S3,
3402df1fe9cSrandyf 		NULL,		0
3412df1fe9cSrandyf 	};
3422df1fe9cSrandyf 	struct btoc *bp;
3432df1fe9cSrandyf 	char *behavior;
3442df1fe9cSrandyf 	int dontcare;
3452df1fe9cSrandyf 
3462df1fe9cSrandyf 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
3472df1fe9cSrandyf 		if (strcmp(behavior, bp->behavior) == 0)
3482df1fe9cSrandyf 			break;
3492df1fe9cSrandyf 	}
3502df1fe9cSrandyf 	if (bp->cmd == 0) {
3512df1fe9cSrandyf 		mesg(MERR, "invalid S3-support behavior \"%s\"\n", behavior);
3522df1fe9cSrandyf 		return (NOUP);
3532df1fe9cSrandyf 	}
3542df1fe9cSrandyf 
3552df1fe9cSrandyf 
3562df1fe9cSrandyf 	switch (bp->cmd) {
3572df1fe9cSrandyf 
3582df1fe9cSrandyf 	case PM_ENABLE_S3:
3592df1fe9cSrandyf 	case PM_DISABLE_S3:
3602df1fe9cSrandyf 		return (do_ioctl(bp->cmd, "S3-support", behavior, EBUSY));
3612df1fe9cSrandyf 
3622df1fe9cSrandyf 	case PM_DEFAULT_ALGORITHM:
3632df1fe9cSrandyf 		/*
3642df1fe9cSrandyf 		 * we suppress errors in the "default" case because we
3652df1fe9cSrandyf 		 * already did an invisible default call, so we know we'll
3662df1fe9cSrandyf 		 * get EBUSY
3672df1fe9cSrandyf 		 */
3682df1fe9cSrandyf 		return (S3_helper("S3-support-enable", "S3-support-disable",
3692df1fe9cSrandyf 		    PM_ENABLE_S3, PM_DISABLE_S3, "S3-support", behavior,
3702df1fe9cSrandyf 		    &dontcare, EBUSY));
3712df1fe9cSrandyf 
3722df1fe9cSrandyf 	default:
3732df1fe9cSrandyf 		mesg(MERR, "S3-support %s failed, %s\n", behavior,
3742df1fe9cSrandyf 		    strerror(errno));
3752df1fe9cSrandyf 		return (NOUP);
3762df1fe9cSrandyf 	}
3772df1fe9cSrandyf }
3782df1fe9cSrandyf 
3792df1fe9cSrandyf /*
3802df1fe9cSrandyf  * Check for valid autoS3 behavior and save after ioctl success.
3812df1fe9cSrandyf  */
3822df1fe9cSrandyf int
3832df1fe9cSrandyf autoS3(void)
3842df1fe9cSrandyf {
3852df1fe9cSrandyf 	struct btoc {
3862df1fe9cSrandyf 		char *behavior;
3872df1fe9cSrandyf 		int cmd;
3882df1fe9cSrandyf 	};
3892df1fe9cSrandyf 	static struct btoc blist[] = {
3902df1fe9cSrandyf 		"default",	PM_DEFAULT_ALGORITHM,
3912df1fe9cSrandyf 		"disable",	PM_STOP_AUTOS3,
3922df1fe9cSrandyf 		"enable",	PM_START_AUTOS3,
3932df1fe9cSrandyf 		NULL,		0
3942df1fe9cSrandyf 	};
3952df1fe9cSrandyf 	struct btoc *bp;
3962df1fe9cSrandyf 	char *behavior;
3972df1fe9cSrandyf 	int dontcare;
3982df1fe9cSrandyf 
3992df1fe9cSrandyf 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
4002df1fe9cSrandyf 		if (strcmp(behavior, bp->behavior) == 0)
4012df1fe9cSrandyf 			break;
4022df1fe9cSrandyf 	}
4032df1fe9cSrandyf 	if (bp->cmd == 0) {
4042df1fe9cSrandyf 		mesg(MERR, "invalid autoS3 behavior \"%s\"\n", behavior);
4052df1fe9cSrandyf 		return (NOUP);
4062df1fe9cSrandyf 	}
4072df1fe9cSrandyf 
4082df1fe9cSrandyf 	switch (bp->cmd) {
4092df1fe9cSrandyf 	default:
4102df1fe9cSrandyf 		mesg(MERR, "autoS3 %s failed, %s\n",
4112df1fe9cSrandyf 		    behavior, strerror(errno));
4122df1fe9cSrandyf 		mesg(MDEBUG, "unknown command\n", bp->cmd);
4132df1fe9cSrandyf 		return (OKUP);
4142df1fe9cSrandyf 
4152df1fe9cSrandyf 	case PM_STOP_AUTOS3:
4162df1fe9cSrandyf 	case PM_START_AUTOS3:
4172df1fe9cSrandyf 		return (do_ioctl(bp->cmd, "autoS3", behavior, EBUSY));
4182df1fe9cSrandyf 
4192df1fe9cSrandyf 	case PM_DEFAULT_ALGORITHM:
4202df1fe9cSrandyf 		return (S3_helper("S3-autoenable", "S3-autodisable",
4212df1fe9cSrandyf 		    PM_START_AUTOS3, PM_STOP_AUTOS3, "autoS3", behavior,
4222df1fe9cSrandyf 		    &dontcare, EBUSY));
4232df1fe9cSrandyf 	}
4242df1fe9cSrandyf }
4252df1fe9cSrandyf 
4262df1fe9cSrandyf 
4272df1fe9cSrandyf /*
4287c478bd9Sstevel@tonic-gate  * Check for valid autopm behavior and save after ioctl success.
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate int
4317c478bd9Sstevel@tonic-gate autopm(void)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	struct btoc {
4347c478bd9Sstevel@tonic-gate 		char *behavior;
4357c478bd9Sstevel@tonic-gate 		int cmd, Errno, isdef;
4367c478bd9Sstevel@tonic-gate 	};
4377c478bd9Sstevel@tonic-gate 	static struct btoc blist[] = {
4382df1fe9cSrandyf 		"default",	PM_START_PM,	-1,	1,
4397c478bd9Sstevel@tonic-gate 		"disable",	PM_STOP_PM,	EINVAL,	0,
4407c478bd9Sstevel@tonic-gate 		"enable",	PM_START_PM,	EBUSY,	0,
4417c478bd9Sstevel@tonic-gate 		NULL,		0,		0,	0,
4427c478bd9Sstevel@tonic-gate 	};
4437c478bd9Sstevel@tonic-gate 	struct btoc *bp;
4447c478bd9Sstevel@tonic-gate 	char *behavior;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
4477c478bd9Sstevel@tonic-gate 		if (strcmp(behavior, bp->behavior) == 0)
4487c478bd9Sstevel@tonic-gate 			break;
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 	if (bp->cmd == 0) {
4517c478bd9Sstevel@tonic-gate 		mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior);
4527c478bd9Sstevel@tonic-gate 		return (NOUP);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/*
4567c478bd9Sstevel@tonic-gate 	 * for "default" behavior, do not enable autopm if not ESTAR_V3
4577c478bd9Sstevel@tonic-gate 	 */
4582df1fe9cSrandyf #if defined(__sparc)
4597c478bd9Sstevel@tonic-gate 	if (!bp->isdef || (estar_vers == ESTAR_V3)) {
4607c478bd9Sstevel@tonic-gate 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
4617c478bd9Sstevel@tonic-gate 			mesg(MERR, "autopm %s failed, %s\n",
4627c478bd9Sstevel@tonic-gate 			    behavior, strerror(errno));
4637c478bd9Sstevel@tonic-gate 			return (NOUP);
4647c478bd9Sstevel@tonic-gate 		}
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 	(void) strcpy(new_cc.apm_behavior, behavior);
4677c478bd9Sstevel@tonic-gate 	return (OKUP);
4682df1fe9cSrandyf #endif
4692df1fe9cSrandyf #if defined(__x86)
4702df1fe9cSrandyf 	if (!bp->isdef) {
4712df1fe9cSrandyf 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
4722df1fe9cSrandyf 			mesg(MERR, "autopm %s failed, %s\n",
4732df1fe9cSrandyf 			    behavior, strerror(errno));
4742df1fe9cSrandyf 			return (NOUP);
4752df1fe9cSrandyf 		}
4762df1fe9cSrandyf 		mesg(MDEBUG, "autopm %s succeeded\n", behavior);
4772df1fe9cSrandyf 
4782df1fe9cSrandyf 		return (OKUP);
4792df1fe9cSrandyf 	} else {
4802df1fe9cSrandyf 		int didenable;
4812df1fe9cSrandyf 		int ret = S3_helper("autopm-enable", "autopm-disable",
4822df1fe9cSrandyf 		    PM_START_PM, PM_STOP_PM, "autopm", behavior, &didenable,
4832df1fe9cSrandyf 		    bp->Errno);
4842df1fe9cSrandyf 		if (didenable) {
4852df1fe9cSrandyf 			/* tell powerd to attach all devices */
4862df1fe9cSrandyf 			new_cc.is_autopm_default = 1;
4872df1fe9cSrandyf 			(void) strcpy(new_cc.apm_behavior, behavior);
4882df1fe9cSrandyf 		}
4892df1fe9cSrandyf 		return (ret);
4902df1fe9cSrandyf 	}
4912df1fe9cSrandyf #endif
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate static int
4967c478bd9Sstevel@tonic-gate gethm(char *src, int *hour, int *min)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	if (sscanf(src, "%d:%d", hour, min) != 2) {
4997c478bd9Sstevel@tonic-gate 		mesg(MERR, "bad time format (%s)\n", src);
5007c478bd9Sstevel@tonic-gate 		return (-1);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 	return (0);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate static void
5077c478bd9Sstevel@tonic-gate strcpy_limit(char *dst, char *src, size_t limit, char *info)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	if (strlcpy(dst, src, limit) >= limit)
5107c478bd9Sstevel@tonic-gate 		mesg(MEXIT, "%s is too long (%s)\n", info, src);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate  * Convert autoshutdown idle and start/finish times;
5167c478bd9Sstevel@tonic-gate  * check and record autoshutdown behavior.
5177c478bd9Sstevel@tonic-gate  */
5187c478bd9Sstevel@tonic-gate int
5197c478bd9Sstevel@tonic-gate autosd(void)
5207c478bd9Sstevel@tonic-gate {
5211dc065c6Smh27603 	char **bp, *behavior;
5221dc065c6Smh27603 	char *unrec = gettext("unrecognized autoshutdown behavior");
5237c478bd9Sstevel@tonic-gate 	static char *blist[] = {
5247c478bd9Sstevel@tonic-gate 		"autowakeup", "default", "noshutdown",
5257c478bd9Sstevel@tonic-gate 		"shutdown", "unconfigured", NULL
5267c478bd9Sstevel@tonic-gate 	};
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	new_cc.as_idle = atoi(LINEARG(1));
5297c478bd9Sstevel@tonic-gate 	if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) ||
5307c478bd9Sstevel@tonic-gate 	    gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm))
5317c478bd9Sstevel@tonic-gate 		return (NOUP);
5321dc065c6Smh27603 	mesg(MDEBUG, "idle %d, start %d:%02d, finish %d:%02d\n",
5337c478bd9Sstevel@tonic-gate 	    new_cc.as_idle, new_cc.as_sh, new_cc.as_sm,
5347c478bd9Sstevel@tonic-gate 	    new_cc.as_fh, new_cc.as_fm);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	for (behavior = LINEARG(4), bp = blist; *bp; bp++) {
5377c478bd9Sstevel@tonic-gate 		if (strcmp(behavior, *bp) == 0)
5387c478bd9Sstevel@tonic-gate 			break;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 	if (*bp == NULL) {
5417c478bd9Sstevel@tonic-gate 		mesg(MERR, "%s: \"%s\"\n", unrec, behavior);
5427c478bd9Sstevel@tonic-gate 		return (NOUP);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	STRCPYLIM(new_cc.as_behavior, *bp, unrec);
5457c478bd9Sstevel@tonic-gate 	return (OKUP);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate /*
5507c478bd9Sstevel@tonic-gate  * Check for a real device and try to resolve to a full path.
5517c478bd9Sstevel@tonic-gate  * The orig/resolved path may be modified into a prom pathname,
5527c478bd9Sstevel@tonic-gate  * and an allocated copy of the result is stored at *destp;
5537c478bd9Sstevel@tonic-gate  * the caller will need to free that space.  Returns 1 for any
5547c478bd9Sstevel@tonic-gate  * error, otherwise 0; also sets *errp after an alloc error.
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate static int
5577c478bd9Sstevel@tonic-gate devpath(char **destp, char *src, int *errp)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	struct stat stbuf;
5607c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX];
5617c478bd9Sstevel@tonic-gate 	char *cp, *dstr;
5627c478bd9Sstevel@tonic-gate 	int devok, dcs = 0;
5637c478bd9Sstevel@tonic-gate 	size_t len;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	/*
5667c478bd9Sstevel@tonic-gate 	 * When there's a real device, try to resolve the path
5677c478bd9Sstevel@tonic-gate 	 * and trim the leading "/devices" component.
5687c478bd9Sstevel@tonic-gate 	 */
5697c478bd9Sstevel@tonic-gate 	if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) {
5707c478bd9Sstevel@tonic-gate 		if (realpath(src, buf) == NULL) {
5717c478bd9Sstevel@tonic-gate 			mesg(MERR, "realpath cannot resolve \"%s\"\n",
5727c478bd9Sstevel@tonic-gate 			    src, strerror(errno));
5737c478bd9Sstevel@tonic-gate 			return (1);
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 		src = buf;
5767c478bd9Sstevel@tonic-gate 		dstr = "/devices";
5777c478bd9Sstevel@tonic-gate 		len = strlen(dstr);
5787c478bd9Sstevel@tonic-gate 		dcs = (strncmp(src, dstr, len) == 0);
5797c478bd9Sstevel@tonic-gate 		if (dcs)
5807c478bd9Sstevel@tonic-gate 			src += len;
5817c478bd9Sstevel@tonic-gate 	} else
5827c478bd9Sstevel@tonic-gate 		mesg(MDEBUG, stat_fmt, src, strerror(errno));
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * When the path has ":anything", display an error for
5867c478bd9Sstevel@tonic-gate 	 * a non-device or truncate a resolved+modifed path.
5877c478bd9Sstevel@tonic-gate 	 */
5887c478bd9Sstevel@tonic-gate 	if (cp = strchr(src, ':')) {
5897c478bd9Sstevel@tonic-gate 		if (devok == 0) {
5907c478bd9Sstevel@tonic-gate 			mesg(MERR, "physical path may not contain "
5917c478bd9Sstevel@tonic-gate 			    "a minor string (%s)\n", src);
5927c478bd9Sstevel@tonic-gate 			return (1);
5937c478bd9Sstevel@tonic-gate 		} else if (dcs)
5947c478bd9Sstevel@tonic-gate 			*cp = '\0';
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if ((*destp = strdup(src)) == NULL) {
5987c478bd9Sstevel@tonic-gate 		*errp = NOUP;
5997c478bd9Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, src, strerror(errno));
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 	return (*destp == NULL);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  * Call pm ioctl request(s) to set property/device dependencies.
6077c478bd9Sstevel@tonic-gate  */
6087c478bd9Sstevel@tonic-gate static int
6097c478bd9Sstevel@tonic-gate dev_dep_common(int isprop)
6107c478bd9Sstevel@tonic-gate {
6117c478bd9Sstevel@tonic-gate 	int cmd, argn, upval = OKUP;
6127c478bd9Sstevel@tonic-gate 	char *src, *first, **destp;
6137c478bd9Sstevel@tonic-gate 	pm_req_t pmreq;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
6167c478bd9Sstevel@tonic-gate 	src = LINEARG(1);
6177c478bd9Sstevel@tonic-gate 	if (isprop) {
6187c478bd9Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT_PROPERTY;
6197c478bd9Sstevel@tonic-gate 		first = NULL;
6207c478bd9Sstevel@tonic-gate 		pmreq.pmreq_kept = src;
6217c478bd9Sstevel@tonic-gate 	} else {
6227c478bd9Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT;
6237c478bd9Sstevel@tonic-gate 		if (devpath(&first, src, &upval))
6247c478bd9Sstevel@tonic-gate 			return (upval);
6257c478bd9Sstevel@tonic-gate 		pmreq.pmreq_kept = first;
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 	destp = &pmreq.pmreq_keeper;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * Now loop through any dependents.
6317c478bd9Sstevel@tonic-gate 	 */
6327c478bd9Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
6337c478bd9Sstevel@tonic-gate 		if (devpath(destp, src, &upval)) {
6347c478bd9Sstevel@tonic-gate 			if (upval != OKUP)
6357c478bd9Sstevel@tonic-gate 				return (upval);
6367c478bd9Sstevel@tonic-gate 			break;
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 		if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) {
6397c478bd9Sstevel@tonic-gate 			mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n"
6407c478bd9Sstevel@tonic-gate 			    "kept \"%s\", keeper \"%s\"\n",
6417c478bd9Sstevel@tonic-gate 			    cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper);
6427c478bd9Sstevel@tonic-gate 			mesg(MERR, "cannot set \"%s\" dependency "
6437c478bd9Sstevel@tonic-gate 			    "for \"%s\", %s\n", pmreq.pmreq_keeper,
6447c478bd9Sstevel@tonic-gate 			    pmreq.pmreq_kept, strerror(errno));
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate 		free(*destp);
6477c478bd9Sstevel@tonic-gate 		*destp = NULL;
6487c478bd9Sstevel@tonic-gate 		if (upval != OKUP)
6497c478bd9Sstevel@tonic-gate 			break;
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	free(first);
6537c478bd9Sstevel@tonic-gate 	return (upval);
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate int
6587c478bd9Sstevel@tonic-gate ddprop(void)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	return (dev_dep_common(1));
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate int
6657c478bd9Sstevel@tonic-gate devdep(void)
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	return (dev_dep_common(0));
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate /*
6727c478bd9Sstevel@tonic-gate  * Convert a numeric string (with a possible trailing scaling byte)
6737c478bd9Sstevel@tonic-gate  * into an integer.  Returns a converted value and *nerrp unchanged,
6747c478bd9Sstevel@tonic-gate  * or 0 with *nerrp set to 1 for a conversion error.
6757c478bd9Sstevel@tonic-gate  */
6767c478bd9Sstevel@tonic-gate static int
6777c478bd9Sstevel@tonic-gate get_scaled_value(char *str, int *nerrp)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	longlong_t svalue = 0, factor = 1;
6807c478bd9Sstevel@tonic-gate 	char *sp;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	errno = 0;
6837c478bd9Sstevel@tonic-gate 	svalue = strtol(str, &sp, 0);
6847c478bd9Sstevel@tonic-gate 	if (errno || (*str != '-' && (*str < '0' || *str > '9')))
6857c478bd9Sstevel@tonic-gate 		*nerrp = 1;
6867c478bd9Sstevel@tonic-gate 	else if (sp && *sp != '\0') {
6877c478bd9Sstevel@tonic-gate 		if (*sp == 'h')
6887c478bd9Sstevel@tonic-gate 			factor = 3600;
6897c478bd9Sstevel@tonic-gate 		else if (*sp == 'm')
6907c478bd9Sstevel@tonic-gate 			factor = 60;
6917c478bd9Sstevel@tonic-gate 		else if (*sp != 's')
6927c478bd9Sstevel@tonic-gate 			*nerrp = 1;
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 	/* any bytes following sp are ignored */
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if (*nerrp == 0) {
6977c478bd9Sstevel@tonic-gate 		svalue *= factor;
6987c478bd9Sstevel@tonic-gate 		if (svalue < INT_MIN || svalue > INT_MAX)
6997c478bd9Sstevel@tonic-gate 			*nerrp = 1;
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 	if (*nerrp)
7027c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, str);
7037c478bd9Sstevel@tonic-gate 	mesg(MDEBUG, "got scaled value %d\n", (int)svalue);
7047c478bd9Sstevel@tonic-gate 	return ((int)svalue);
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate /*
7097c478bd9Sstevel@tonic-gate  * Increment the count of threshold values,
7107c478bd9Sstevel@tonic-gate  * reallocate *vlistp and append another element.
7117c478bd9Sstevel@tonic-gate  * Returns 1 on error, otherwise 0.
7127c478bd9Sstevel@tonic-gate  */
7137c478bd9Sstevel@tonic-gate static int
7147c478bd9Sstevel@tonic-gate vlist_append(int **vlistp, int *vcntp, int value)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	(*vcntp)++;
7177c478bd9Sstevel@tonic-gate 	if (*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp)))
7187c478bd9Sstevel@tonic-gate 		*(*vlistp + *vcntp - 1) = value;
7197c478bd9Sstevel@tonic-gate 	else
7207c478bd9Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, "threshold list", strerror(errno));
7217c478bd9Sstevel@tonic-gate 	return (*vlistp == NULL);
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate /*
7267c478bd9Sstevel@tonic-gate  * Convert a single threshold string or paren groups of thresh's as
7277c478bd9Sstevel@tonic-gate  * described below.  All thresh's are saved to an allocated list at
7287c478bd9Sstevel@tonic-gate  * *vlistp; the caller will need to free that space.  On return:
7297c478bd9Sstevel@tonic-gate  * *vcntp is the count of the vlist array, and vlist is either
7307c478bd9Sstevel@tonic-gate  * a single thresh or N groups of thresh's with a trailing zero:
7317c478bd9Sstevel@tonic-gate  * (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0.
7327c478bd9Sstevel@tonic-gate  * Returns 0 when all conversions were OK, and 1 for any syntax,
7337c478bd9Sstevel@tonic-gate  * conversion, or alloc error.
7347c478bd9Sstevel@tonic-gate  */
7357c478bd9Sstevel@tonic-gate static int
7367c478bd9Sstevel@tonic-gate get_thresh(int **vlistp, int *vcntp)
7377c478bd9Sstevel@tonic-gate {
7387c478bd9Sstevel@tonic-gate 	int argn, value, gci, grp_cnt = 0, paren = 0, nerr = 0;
7397c478bd9Sstevel@tonic-gate 	char *rp, *src;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
7427c478bd9Sstevel@tonic-gate 		if (*src == LPAREN) {
7437c478bd9Sstevel@tonic-gate 			gci = *vcntp;
7447c478bd9Sstevel@tonic-gate 			if (nerr = vlist_append(vlistp, vcntp, 0))
7457c478bd9Sstevel@tonic-gate 				break;
7467c478bd9Sstevel@tonic-gate 			paren = 1;
7477c478bd9Sstevel@tonic-gate 			src++;
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 		if (*(rp = LASTBYTE(src)) == RPAREN) {
7507c478bd9Sstevel@tonic-gate 			if (paren) {
7517c478bd9Sstevel@tonic-gate 				grp_cnt = *vcntp - gci;
7527c478bd9Sstevel@tonic-gate 				*(*vlistp + gci) = grp_cnt;
7537c478bd9Sstevel@tonic-gate 				paren = 0;
7547c478bd9Sstevel@tonic-gate 				*rp = '\0';
7557c478bd9Sstevel@tonic-gate 			} else {
7567c478bd9Sstevel@tonic-gate 				nerr = 1;
7577c478bd9Sstevel@tonic-gate 				break;
7587c478bd9Sstevel@tonic-gate 			}
7597c478bd9Sstevel@tonic-gate 		}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		value = get_scaled_value(src, &nerr);
7627c478bd9Sstevel@tonic-gate 		if (nerr || (nerr = vlist_append(vlistp, vcntp, value)))
7637c478bd9Sstevel@tonic-gate 			break;
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if (nerr == 0 && grp_cnt)
7677c478bd9Sstevel@tonic-gate 		nerr = vlist_append(vlistp, vcntp, 0);
7687c478bd9Sstevel@tonic-gate 	return (nerr);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate /*
7737c478bd9Sstevel@tonic-gate  * Set device thresholds from (3) formats:
7747c478bd9Sstevel@tonic-gate  * 	path	"always-on"
7757c478bd9Sstevel@tonic-gate  * 	path	time-spec: [0-9]+[{h,m,s}]
7767c478bd9Sstevel@tonic-gate  *	path	(ts1 ts2 ...)+
7777c478bd9Sstevel@tonic-gate  */
7787c478bd9Sstevel@tonic-gate int
7797c478bd9Sstevel@tonic-gate devthr(void)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	int cmd, upval = OKUP, nthresh = 0, *vlist = NULL;
7827c478bd9Sstevel@tonic-gate 	pm_req_t pmreq;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
7857c478bd9Sstevel@tonic-gate 	if (devpath(&pmreq.physpath, LINEARG(1), &upval))
7867c478bd9Sstevel@tonic-gate 		return (upval);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	if (strcmp(LINEARG(2), always_on) == 0) {
7897c478bd9Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
7907c478bd9Sstevel@tonic-gate 		pmreq.value = INT_MAX;
7917c478bd9Sstevel@tonic-gate 	} else if (get_thresh(&vlist, &nthresh)) {
7927c478bd9Sstevel@tonic-gate 		mesg(MERR, bad_thresh_fmt);
7937c478bd9Sstevel@tonic-gate 		upval = NOUP;
7947c478bd9Sstevel@tonic-gate 	} else if (nthresh == 1) {
7957c478bd9Sstevel@tonic-gate 		pmreq.value = *vlist;
7967c478bd9Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
7977c478bd9Sstevel@tonic-gate 	} else {
7987c478bd9Sstevel@tonic-gate 		pmreq.data = vlist;
7997c478bd9Sstevel@tonic-gate 		pmreq.datasize = (nthresh * sizeof (*vlist));
8007c478bd9Sstevel@tonic-gate 		cmd = PM_SET_COMPONENT_THRESHOLDS;
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1)
8047c478bd9Sstevel@tonic-gate 		mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno));
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	free(vlist);
8077c478bd9Sstevel@tonic-gate 	free(pmreq.physpath);
8087c478bd9Sstevel@tonic-gate 	return (upval);
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate static int
8137c478bd9Sstevel@tonic-gate scan_int(char *src, int *dst)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	long lval;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	errno = 0;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	lval = strtol(LINEARG(1), NULL, 0);
8207c478bd9Sstevel@tonic-gate 	if (errno || lval > INT_MAX || lval < 0) {
8217c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
8227c478bd9Sstevel@tonic-gate 		return (NOUP);
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	*dst = (int)lval;
8267c478bd9Sstevel@tonic-gate 	return (OKUP);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate static int
8307c478bd9Sstevel@tonic-gate scan_float(char *src, float *dst)
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	float fval;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	errno = 0;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	fval = strtof(src, NULL);
8377c478bd9Sstevel@tonic-gate 	if (errno || fval < 0.0) {
8387c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
8397c478bd9Sstevel@tonic-gate 		return (NOUP);
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	*dst = fval;
8437c478bd9Sstevel@tonic-gate 	return (OKUP);
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate int
8487c478bd9Sstevel@tonic-gate dreads(void)
8497c478bd9Sstevel@tonic-gate {
8507c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.diskreads_thold));
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate /*
8557c478bd9Sstevel@tonic-gate  * Set pathname for idlecheck;
8567c478bd9Sstevel@tonic-gate  * an overflowed pathname is treated as a fatal error.
8577c478bd9Sstevel@tonic-gate  */
8587c478bd9Sstevel@tonic-gate int
8597c478bd9Sstevel@tonic-gate idlechk(void)
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate 	STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path");
8627c478bd9Sstevel@tonic-gate 	return (OKUP);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate int
8677c478bd9Sstevel@tonic-gate loadavg(void)
8687c478bd9Sstevel@tonic-gate {
8697c478bd9Sstevel@tonic-gate 	return (scan_float(LINEARG(1), &new_cc.loadaverage_thold));
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate int
8747c478bd9Sstevel@tonic-gate nfsreq(void)
8757c478bd9Sstevel@tonic-gate {
8767c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold));
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate #ifdef sparc
8807c478bd9Sstevel@tonic-gate static char open_fmt[] = "cannot open \"%s\", %s\n";
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate /*
8837c478bd9Sstevel@tonic-gate  * Verify the filesystem type for a regular statefile is "ufs"
8847c478bd9Sstevel@tonic-gate  * or verify a block device is not in use as a mounted filesytem.
8857c478bd9Sstevel@tonic-gate  * Returns 1 if any error, otherwise 0.
8867c478bd9Sstevel@tonic-gate  */
8877c478bd9Sstevel@tonic-gate static int
8887c478bd9Sstevel@tonic-gate check_mount(char *sfile, dev_t sfdev, int ufs)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	char *src, *err_fmt = NULL, *mnttab = MNTTAB;
8917c478bd9Sstevel@tonic-gate 	int rgent, match = 0;
892e7cbe64fSgw25295 	struct mnttab zroot = { 0 };
893e7cbe64fSgw25295 	struct mnttab entry;
8947c478bd9Sstevel@tonic-gate 	struct extmnttab ent;
8957c478bd9Sstevel@tonic-gate 	FILE *fp;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
8987c478bd9Sstevel@tonic-gate 		mesg(MERR, open_fmt, mnttab, strerror(errno));
8997c478bd9Sstevel@tonic-gate 		return (1);
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 
902e7cbe64fSgw25295 	if (ufs) {
903e7cbe64fSgw25295 		zroot.mnt_mountp = "/";
904e7cbe64fSgw25295 		zroot.mnt_fstype = "zfs";
905e7cbe64fSgw25295 		if (getmntany(fp, &entry, &zroot) == 0) {
906e7cbe64fSgw25295 			err_fmt = "ufs statefile with zfs root is not"
907e7cbe64fSgw25295 			    " supported\n";
908e7cbe64fSgw25295 			mesg(MERR, err_fmt, sfile);
909e7cbe64fSgw25295 			fclose(fp);
910e7cbe64fSgw25295 			return (1);
911e7cbe64fSgw25295 		}
912e7cbe64fSgw25295 		resetmnttab(fp);
913e7cbe64fSgw25295 	}
9147c478bd9Sstevel@tonic-gate 	/*
9157c478bd9Sstevel@tonic-gate 	 * Search for a matching dev_t;
9167c478bd9Sstevel@tonic-gate 	 * ignore non-ufs filesystems for a regular statefile.
9177c478bd9Sstevel@tonic-gate 	 */
9187c478bd9Sstevel@tonic-gate 	while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) {
9197c478bd9Sstevel@tonic-gate 		if (rgent > 0) {
9207c478bd9Sstevel@tonic-gate 			mesg(MERR, "error reading \"%s\"\n", mnttab);
9217c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
9227c478bd9Sstevel@tonic-gate 			return (1);
9237c478bd9Sstevel@tonic-gate 		} else if (ufs && strcmp(ent.mnt_fstype, "ufs"))
9247c478bd9Sstevel@tonic-gate 			continue;
9257c478bd9Sstevel@tonic-gate 		else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) {
9267c478bd9Sstevel@tonic-gate 			match = 1;
9277c478bd9Sstevel@tonic-gate 			break;
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	/*
9327c478bd9Sstevel@tonic-gate 	 * No match is needed for a block device statefile,
9337c478bd9Sstevel@tonic-gate 	 * a match is needed for a regular statefile.
9347c478bd9Sstevel@tonic-gate 	 */
9357c478bd9Sstevel@tonic-gate 	if (match == 0) {
936e7cbe64fSgw25295 		if (new_cc.cf_type != CFT_UFS)
9377c478bd9Sstevel@tonic-gate 			STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile");
9387c478bd9Sstevel@tonic-gate 		else
9397c478bd9Sstevel@tonic-gate 			err_fmt = "cannot find ufs mount point for \"%s\"\n";
9407c478bd9Sstevel@tonic-gate 	} else if (new_cc.cf_type == CFT_UFS) {
9417c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry");
9427c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special");
9437c478bd9Sstevel@tonic-gate 		while (*(sfile + 1) == '/') sfile++;
9447c478bd9Sstevel@tonic-gate 		src = sfile + strlen(ent.mnt_mountp);
9457c478bd9Sstevel@tonic-gate 		while (*src == '/') src++;
9467c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_path, src, "statefile path");
9477c478bd9Sstevel@tonic-gate 	} else
9487c478bd9Sstevel@tonic-gate 		err_fmt = "statefile device \"%s\" is a mounted filesystem\n";
949e7cbe64fSgw25295 	(void) fclose(fp);
9507c478bd9Sstevel@tonic-gate 	if (err_fmt)
9517c478bd9Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
9527c478bd9Sstevel@tonic-gate 	return (err_fmt != NULL);
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate /*
9577c478bd9Sstevel@tonic-gate  * Convert a Unix device to a prom device and save on success,
9587c478bd9Sstevel@tonic-gate  * log any ioctl/conversion error.
9597c478bd9Sstevel@tonic-gate  */
9607c478bd9Sstevel@tonic-gate static int
961e7cbe64fSgw25295 utop(char *fs_name, char *prom_name)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate 	union obpbuf {
9647c478bd9Sstevel@tonic-gate 		char	buf[OBP_MAXPATHLEN + sizeof (uint_t)];
9657c478bd9Sstevel@tonic-gate 		struct	openpromio oppio;
9667c478bd9Sstevel@tonic-gate 	};
9677c478bd9Sstevel@tonic-gate 	union obpbuf oppbuf;
9687c478bd9Sstevel@tonic-gate 	struct openpromio *opp;
9697c478bd9Sstevel@tonic-gate 	char *promdev = "/dev/openprom";
9707c478bd9Sstevel@tonic-gate 	int fd, upval;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	if ((fd = open(promdev, O_RDONLY)) == -1) {
9737c478bd9Sstevel@tonic-gate 		mesg(MERR, open_fmt, promdev, strerror(errno));
9747c478bd9Sstevel@tonic-gate 		return (NOUP);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	opp = &oppbuf.oppio;
9787c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPATHLEN;
979e7cbe64fSgw25295 	strcpy_limit(opp->oprom_array, fs_name,
9807c478bd9Sstevel@tonic-gate 	    OBP_MAXPATHLEN, "statefile device");
9817c478bd9Sstevel@tonic-gate 	upval = ioctl(fd, OPROMDEV2PROMNAME, opp);
9827c478bd9Sstevel@tonic-gate 	(void) close(fd);
983e7cbe64fSgw25295 	if (upval == OKUP) {
984e7cbe64fSgw25295 		strcpy_limit(prom_name, opp->oprom_array, OBP_MAXPATHLEN,
985e7cbe64fSgw25295 		    "prom device");
986e7cbe64fSgw25295 	} else {
9877c478bd9Sstevel@tonic-gate 		openlog("pmconfig", 0, LOG_DAEMON);
9887c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE,
9897c478bd9Sstevel@tonic-gate 		    gettext("cannot convert \"%s\" to prom device"),
990e7cbe64fSgw25295 		    fs_name);
9917c478bd9Sstevel@tonic-gate 		closelog();
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	return (upval);
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate 
997e7cbe64fSgw25295 /*
998e7cbe64fSgw25295  * given the path to a zvol, return the cXtYdZ name
999e7cbe64fSgw25295  * returns < 0 on error, 0 if it isn't a zvol, > 1 on success
1000e7cbe64fSgw25295  */
1001e7cbe64fSgw25295 static int
1002e7cbe64fSgw25295 ztop(char *arg, char *diskname)
1003e7cbe64fSgw25295 {
1004e7cbe64fSgw25295 	zpool_handle_t *zpool_handle;
1005e7cbe64fSgw25295 	nvlist_t *config, *nvroot;
1006e7cbe64fSgw25295 	nvlist_t **child;
1007e7cbe64fSgw25295 	uint_t children;
1008e7cbe64fSgw25295 	libzfs_handle_t *lzfs;
1009e7cbe64fSgw25295 	char *vname;
1010e7cbe64fSgw25295 	char *p;
1011e7cbe64fSgw25295 	char pool_name[MAXPATHLEN];
1012e7cbe64fSgw25295 
1013e7cbe64fSgw25295 	if (strncmp(arg, "/dev/zvol/dsk/", 14)) {
1014e7cbe64fSgw25295 		return (0);
1015e7cbe64fSgw25295 	}
1016e7cbe64fSgw25295 	arg += 14;
1017e7cbe64fSgw25295 	strncpy(pool_name, arg, MAXPATHLEN);
1018e7cbe64fSgw25295 	if (p = strchr(pool_name, '/'))
1019e7cbe64fSgw25295 		*p = '\0';
1020e7cbe64fSgw25295 	STRCPYLIM(new_cc.cf_fs, p + 1, "statefile path");
1021e7cbe64fSgw25295 
1022e7cbe64fSgw25295 	if ((lzfs = libzfs_init()) == NULL) {
1023e7cbe64fSgw25295 		mesg(MERR, "failed to initialize ZFS library\n");
1024e7cbe64fSgw25295 		return (-1);
1025e7cbe64fSgw25295 	}
1026e7cbe64fSgw25295 	if ((zpool_handle = zpool_open(lzfs, pool_name)) == NULL) {
1027e7cbe64fSgw25295 		mesg(MERR, "couldn't open pool '%s'\n", pool_name);
1028e7cbe64fSgw25295 		libzfs_fini(lzfs);
1029e7cbe64fSgw25295 		return (-1);
1030e7cbe64fSgw25295 	}
1031e7cbe64fSgw25295 	config = zpool_get_config(zpool_handle, NULL);
1032e7cbe64fSgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1033e7cbe64fSgw25295 	    &nvroot) != 0) {
1034e7cbe64fSgw25295 		zpool_close(zpool_handle);
1035e7cbe64fSgw25295 		libzfs_fini(lzfs);
1036e7cbe64fSgw25295 		return (-1);
1037e7cbe64fSgw25295 	}
1038e7cbe64fSgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1039e7cbe64fSgw25295 	    &child, &children) == 0);
1040e7cbe64fSgw25295 	if (children != 1) {
1041e7cbe64fSgw25295 		mesg(MERR, "expected one vdev, got %d\n", children);
1042e7cbe64fSgw25295 		zpool_close(zpool_handle);
1043e7cbe64fSgw25295 		libzfs_fini(lzfs);
1044e7cbe64fSgw25295 		return (-1);
1045e7cbe64fSgw25295 	}
1046*88ecc943SGeorge Wilson 	vname = zpool_vdev_name(lzfs, zpool_handle, child[0], B_FALSE);
1047e7cbe64fSgw25295 	if (vname == NULL) {
1048e7cbe64fSgw25295 		mesg(MERR, "couldn't determine vdev name\n");
1049e7cbe64fSgw25295 		zpool_close(zpool_handle);
1050e7cbe64fSgw25295 		libzfs_fini(lzfs);
1051e7cbe64fSgw25295 		return (-1);
1052e7cbe64fSgw25295 	}
1053e7cbe64fSgw25295 	strcpy(diskname, "/dev/dsk/");
1054e7cbe64fSgw25295 	strcat(diskname, vname);
1055e7cbe64fSgw25295 	free(vname);
1056e7cbe64fSgw25295 	zpool_close(zpool_handle);
1057e7cbe64fSgw25295 	libzfs_fini(lzfs);
1058e7cbe64fSgw25295 	return (1);
1059e7cbe64fSgw25295 }
1060e7cbe64fSgw25295 
1061e7cbe64fSgw25295 /*
1062e7cbe64fSgw25295  * returns NULL if the slice is good (e.g. does not start at block
1063e7cbe64fSgw25295  * zero, or a string describing the error if it doesn't
1064e7cbe64fSgw25295  */
1065e7cbe64fSgw25295 static boolean_t
1066e7cbe64fSgw25295 is_good_slice(char *sfile, char **err)
1067e7cbe64fSgw25295 {
1068e7cbe64fSgw25295 	int fd, rc;
1069e7cbe64fSgw25295 	struct vtoc vtoc;
1070e7cbe64fSgw25295 	dk_gpt_t *gpt;
1071e7cbe64fSgw25295 	char rdskname[MAXPATHLEN];
1072e7cbe64fSgw25295 	char *x, *y;
1073e7cbe64fSgw25295 
1074e7cbe64fSgw25295 	*err = NULL;
1075e7cbe64fSgw25295 	/* convert from dsk to rdsk */
1076e7cbe64fSgw25295 	STRCPYLIM(rdskname, sfile, "disk name");
1077e7cbe64fSgw25295 	x = strstr(rdskname, "dsk/");
1078e7cbe64fSgw25295 	y = strstr(sfile, "dsk/");
1079e7cbe64fSgw25295 	if (x != NULL) {
1080e7cbe64fSgw25295 		*x++ = 'r';
1081e7cbe64fSgw25295 		strcpy(x, y);
1082e7cbe64fSgw25295 	}
1083e7cbe64fSgw25295 
1084e7cbe64fSgw25295 	if ((fd = open(rdskname, O_RDONLY)) == -1) {
1085e7cbe64fSgw25295 		*err = "could not open '%s'\n";
1086e7cbe64fSgw25295 	} else if ((rc = read_vtoc(fd, &vtoc)) >= 0) {
1087e7cbe64fSgw25295 		/*
1088e7cbe64fSgw25295 		 * we got a slice number; now check the block
1089e7cbe64fSgw25295 		 * number where the slice starts
1090e7cbe64fSgw25295 		 */
1091e7cbe64fSgw25295 		if (vtoc.v_part[rc].p_start < 2)
1092e7cbe64fSgw25295 			*err = "using '%s' would clobber the disk label\n";
1093e7cbe64fSgw25295 		close(fd);
1094e7cbe64fSgw25295 		return (*err ? B_FALSE : B_TRUE);
1095e7cbe64fSgw25295 	} else if ((rc == VT_ENOTSUP) &&
1096e7cbe64fSgw25295 	    (efi_alloc_and_read(fd, &gpt)) >= 0) {
1097e7cbe64fSgw25295 		/* EFI slices don't clobber the disk label */
1098e7cbe64fSgw25295 		free(gpt);
1099e7cbe64fSgw25295 		close(fd);
1100e7cbe64fSgw25295 		return (B_TRUE);
1101e7cbe64fSgw25295 	} else
1102e7cbe64fSgw25295 		*err = "could not read partition table from '%s'\n";
1103e7cbe64fSgw25295 	return (B_FALSE);
1104e7cbe64fSgw25295 }
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate /*
11077c478bd9Sstevel@tonic-gate  * Check for a valid statefile pathname, inode and mount status.
11087c478bd9Sstevel@tonic-gate  */
11097c478bd9Sstevel@tonic-gate int
11107c478bd9Sstevel@tonic-gate sfpath(void)
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate 	static int statefile;
11137c478bd9Sstevel@tonic-gate 	char *err_fmt = NULL;
11147c478bd9Sstevel@tonic-gate 	char *sfile, *sp, ch;
1115e7cbe64fSgw25295 	char diskname[256];
11167c478bd9Sstevel@tonic-gate 	struct stat stbuf;
11177c478bd9Sstevel@tonic-gate 	int dir = 0;
11187c478bd9Sstevel@tonic-gate 	dev_t dev;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	if (statefile) {
11217c478bd9Sstevel@tonic-gate 		mesg(MERR, "ignored redundant statefile entry\n");
11227c478bd9Sstevel@tonic-gate 		return (OKUP);
11237c478bd9Sstevel@tonic-gate 	} else if (ua_err) {
11247c478bd9Sstevel@tonic-gate 		if (ua_err != ENOTSUP)
11257c478bd9Sstevel@tonic-gate 			mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n",
11267c478bd9Sstevel@tonic-gate 			    strerror(ua_err));
11277c478bd9Sstevel@tonic-gate 		return (NOUP);
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	/*
11317c478bd9Sstevel@tonic-gate 	 * Check for an absolute path and trim any trailing '/'.
11327c478bd9Sstevel@tonic-gate 	 */
11337c478bd9Sstevel@tonic-gate 	sfile = LINEARG(1);
11347c478bd9Sstevel@tonic-gate 	if (*sfile != '/') {
11357c478bd9Sstevel@tonic-gate 		mesg(MERR, "statefile requires an absolute path\n");
11367c478bd9Sstevel@tonic-gate 		return (NOUP);
11377c478bd9Sstevel@tonic-gate 	}
11387c478bd9Sstevel@tonic-gate 	for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--)
11397c478bd9Sstevel@tonic-gate 		*sp = '\0';
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	/*
11427c478bd9Sstevel@tonic-gate 	 * If the statefile doesn't exist, the leading path must be a dir.
11437c478bd9Sstevel@tonic-gate 	 */
11447c478bd9Sstevel@tonic-gate 	if (stat(sfile, &stbuf) == -1) {
11457c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
11467c478bd9Sstevel@tonic-gate 			dir = 1;
11477c478bd9Sstevel@tonic-gate 			if ((sp = strrchr(sfile, '/')) == sfile)
11487c478bd9Sstevel@tonic-gate 				sp++;
11497c478bd9Sstevel@tonic-gate 			ch = *sp;
11507c478bd9Sstevel@tonic-gate 			*sp = '\0';
11517c478bd9Sstevel@tonic-gate 			if (stat(sfile, &stbuf) == -1)
11527c478bd9Sstevel@tonic-gate 				err_fmt = stat_fmt;
11537c478bd9Sstevel@tonic-gate 			*sp = ch;
11547c478bd9Sstevel@tonic-gate 		} else
11557c478bd9Sstevel@tonic-gate 			err_fmt = stat_fmt;
11567c478bd9Sstevel@tonic-gate 		if (err_fmt) {
11577c478bd9Sstevel@tonic-gate 			mesg(MERR, err_fmt, sfile, strerror(errno));
11587c478bd9Sstevel@tonic-gate 			return (NOUP);
11597c478bd9Sstevel@tonic-gate 		}
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	/*
11637c478bd9Sstevel@tonic-gate 	 * Check for regular/dir/block types, set cf_type and dev.
11647c478bd9Sstevel@tonic-gate 	 */
11657c478bd9Sstevel@tonic-gate 	if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) {
11667c478bd9Sstevel@tonic-gate 		new_cc.cf_type = CFT_UFS;
11677c478bd9Sstevel@tonic-gate 		dev = stbuf.st_dev;
11687c478bd9Sstevel@tonic-gate 	} else if (S_ISBLK(stbuf.st_mode)) {
1169e7cbe64fSgw25295 		if (is_good_slice(sfile, &err_fmt)) {
1170e7cbe64fSgw25295 			switch (ztop(sfile, diskname)) {
1171e7cbe64fSgw25295 				case 1:
1172e7cbe64fSgw25295 					new_cc.cf_type = CFT_ZVOL;
1173e7cbe64fSgw25295 					break;
1174e7cbe64fSgw25295 				case 0:
11757c478bd9Sstevel@tonic-gate 					new_cc.cf_type = CFT_SPEC;
1176e7cbe64fSgw25295 					break;
1177e7cbe64fSgw25295 				case -1:
1178e7cbe64fSgw25295 				default:
1179e7cbe64fSgw25295 					return (NOUP);
1180e7cbe64fSgw25295 			}
11817c478bd9Sstevel@tonic-gate 			dev = stbuf.st_rdev;
1182e7cbe64fSgw25295 		}
11837c478bd9Sstevel@tonic-gate 	} else
11847c478bd9Sstevel@tonic-gate 		err_fmt = "bad file type for \"%s\"\n"
11857c478bd9Sstevel@tonic-gate 		    "statefile must be a regular file or block device\n";
11867c478bd9Sstevel@tonic-gate 	if (err_fmt) {
11877c478bd9Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
11887c478bd9Sstevel@tonic-gate 		return (NOUP);
11897c478bd9Sstevel@tonic-gate 	}
1190e7cbe64fSgw25295 	if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)))
11917c478bd9Sstevel@tonic-gate 		return (NOUP);
1192e7cbe64fSgw25295 	if (new_cc.cf_type == CFT_ZVOL) {
1193e7cbe64fSgw25295 		if (utop(diskname, new_cc.cf_dev_prom))
1194e7cbe64fSgw25295 			return (NOUP);
1195e7cbe64fSgw25295 	} else if (utop(new_cc.cf_devfs, new_cc.cf_dev_prom)) {
1196e7cbe64fSgw25295 		return (NOUP);
1197e7cbe64fSgw25295 	}
11987c478bd9Sstevel@tonic-gate 	new_cc.cf_magic = CPR_CONFIG_MAGIC;
11997c478bd9Sstevel@tonic-gate 	statefile = 1;
12007c478bd9Sstevel@tonic-gate 	return (OKUP);
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate #endif /* sparc */
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate /*
1206c42872d4Smh27603  * Common function to set a system or cpu threshold.
12077c478bd9Sstevel@tonic-gate  */
1208c42872d4Smh27603 static int
1209c42872d4Smh27603 cmnthr(int req)
12107c478bd9Sstevel@tonic-gate {
12117c478bd9Sstevel@tonic-gate 	int value, nerr = 0, upval = OKUP;
12127c478bd9Sstevel@tonic-gate 	char *thresh = LINEARG(1);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	if (strcmp(thresh, always_on) == 0)
12157c478bd9Sstevel@tonic-gate 		value = INT_MAX;
12167c478bd9Sstevel@tonic-gate 	else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) {
12177c478bd9Sstevel@tonic-gate 		mesg(MERR, "%s must be a positive value\n", LINEARG(0));
12187c478bd9Sstevel@tonic-gate 		upval = NOUP;
12197c478bd9Sstevel@tonic-gate 	}
12207c478bd9Sstevel@tonic-gate 	if (upval == OKUP)
1221c42872d4Smh27603 		(void) ioctl(pm_fd, req, value);
12227c478bd9Sstevel@tonic-gate 	return (upval);
12237c478bd9Sstevel@tonic-gate }
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 
1226c42872d4Smh27603 /*
1227c42872d4Smh27603  * Try setting system threshold.
1228c42872d4Smh27603  */
1229c42872d4Smh27603 int
1230c42872d4Smh27603 systhr(void)
1231c42872d4Smh27603 {
1232c42872d4Smh27603 	return (cmnthr(PM_SET_SYSTEM_THRESHOLD));
1233c42872d4Smh27603 }
1234c42872d4Smh27603 
1235c42872d4Smh27603 
1236c42872d4Smh27603 /*
1237c42872d4Smh27603  * Try setting cpu threshold.
1238c42872d4Smh27603  */
1239c42872d4Smh27603 int
1240c42872d4Smh27603 cputhr(void)
1241c42872d4Smh27603 {
1242c42872d4Smh27603 	return (cmnthr(PM_SET_CPU_THRESHOLD));
1243c42872d4Smh27603 }
1244c42872d4Smh27603 
1245c42872d4Smh27603 
12467c478bd9Sstevel@tonic-gate int
12477c478bd9Sstevel@tonic-gate tchars(void)
12487c478bd9Sstevel@tonic-gate {
12497c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.ttychars_thold));
12507c478bd9Sstevel@tonic-gate }
1251