xref: /titanic_51/usr/src/cmd/power/handlers.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
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 /*
2225aedaedSmh27603  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include "pmconfig.h"
297c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
307c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
317c478bd9Sstevel@tonic-gate #include <sys/openpromio.h>
327c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
33*e7cbe64fSgw25295 #include <sys/vtoc.h>
34*e7cbe64fSgw25295 #include <sys/efi_partition.h>
357c478bd9Sstevel@tonic-gate #include <syslog.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
372df1fe9cSrandyf #include <sys/pm.h>
382df1fe9cSrandyf #include <kstat.h>
392df1fe9cSrandyf #include <sys/smbios.h>
40*e7cbe64fSgw25295 #include <libzfs.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str)
447c478bd9Sstevel@tonic-gate #define	LASTBYTE(str) (str + strlen(str) - 1)
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static char nerr_fmt[] = "number is out of range (%s)\n";
477c478bd9Sstevel@tonic-gate static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n";
487c478bd9Sstevel@tonic-gate static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n";
497c478bd9Sstevel@tonic-gate static char bad_thresh_fmt[] = "bad threshold(s)\n";
507c478bd9Sstevel@tonic-gate static char stat_fmt[] = "cannot stat \"%s\", %s\n";
517c478bd9Sstevel@tonic-gate static char always_on[] = "always-on";
527c478bd9Sstevel@tonic-gate 
532df1fe9cSrandyf #define	PM_DEFAULT_ALGORITHM -1
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * When lines in a config file (usually "/etc/power.conf") start with
567c478bd9Sstevel@tonic-gate  * a recognized keyword, a "handler" routine is called for specific
577c478bd9Sstevel@tonic-gate  * CPR or PM -related action(s).  Each routine returns a status code
587c478bd9Sstevel@tonic-gate  * indicating whether all tasks were successful; if any errors occured,
597c478bd9Sstevel@tonic-gate  * future CPR or PM updates are skipped.  Following are the handler
607c478bd9Sstevel@tonic-gate  * routines for all keywords:
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 
642df1fe9cSrandyf static char pm_cmd_string[32];
652df1fe9cSrandyf 
662df1fe9cSrandyf static char *
672df1fe9cSrandyf pm_map(int cmd)
682df1fe9cSrandyf {
692df1fe9cSrandyf 	pm_req_t req;
702df1fe9cSrandyf 
712df1fe9cSrandyf 	req.value = cmd;
722df1fe9cSrandyf 	req.data = (void *)pm_cmd_string;
732df1fe9cSrandyf 	req.datasize = sizeof (pm_cmd_string);
742df1fe9cSrandyf 
752df1fe9cSrandyf 	if (ioctl(pm_fd, PM_GET_CMD_NAME, &req) < 0) {
7625aedaedSmh27603 		perror(gettext("PM_GET_CMD_NAME failed:"));
772df1fe9cSrandyf 		return ("??");
782df1fe9cSrandyf 	}
792df1fe9cSrandyf 	return (pm_cmd_string);
802df1fe9cSrandyf }
812df1fe9cSrandyf 
822df1fe9cSrandyf static int
832df1fe9cSrandyf isonlist(char *listname, const char *man, const char *prod)
842df1fe9cSrandyf {
852df1fe9cSrandyf 	pm_searchargs_t sl;
862df1fe9cSrandyf 	int ret;
872df1fe9cSrandyf 
882df1fe9cSrandyf 	sl.pms_listname = listname;
892df1fe9cSrandyf 	sl.pms_manufacturer = (char *)man;
902df1fe9cSrandyf 	sl.pms_product = (char *)prod;
912df1fe9cSrandyf 	ret = ioctl(pm_fd, PM_SEARCH_LIST, &sl);
922df1fe9cSrandyf 	mesg(MDEBUG, "PM_SEARCH_LIST %s for %s,%s returns %d\n",
932df1fe9cSrandyf 	    listname, man, prod, ret);
942df1fe9cSrandyf 	return (ret == 0);
952df1fe9cSrandyf }
962df1fe9cSrandyf 
972df1fe9cSrandyf static int
982df1fe9cSrandyf do_ioctl(int ioctl_cmd, char *keyword, char *behavior, int suppress)
992df1fe9cSrandyf {
1002df1fe9cSrandyf 	mesg(MDEBUG, "doing ioctl %s for %s ", pm_map(ioctl_cmd), keyword);
1012df1fe9cSrandyf 	if (ioctl(pm_fd, ioctl_cmd, NULL) == -1) {
1022df1fe9cSrandyf 		int suppressed = suppress == -1 || suppress == errno;
1032df1fe9cSrandyf 		if (!suppressed) {
1042df1fe9cSrandyf 			mesg(MERR, "%s %s failed, %s\n", keyword, behavior,
1052df1fe9cSrandyf 			    strerror(errno));
1062df1fe9cSrandyf 			return (NOUP);
1072df1fe9cSrandyf 		} else {
10825aedaedSmh27603 			mesg(MDEBUG, "%s %s failed, %s\n", keyword, behavior,
10925aedaedSmh27603 			    strerror(errno));
1102df1fe9cSrandyf 			return (OKUP);
1112df1fe9cSrandyf 		}
1122df1fe9cSrandyf 	}
1132df1fe9cSrandyf 	mesg(MDEBUG, "succeeded\n");
1142df1fe9cSrandyf 	return (OKUP);
1152df1fe9cSrandyf }
1162df1fe9cSrandyf 
1177c478bd9Sstevel@tonic-gate /*
118c42872d4Smh27603  * Check for valid cpupm behavior and communicate it to the kernel.
119c42872d4Smh27603  */
120c42872d4Smh27603 int
121c42872d4Smh27603 cpupm(void)
122c42872d4Smh27603 {
123c42872d4Smh27603 	struct btoc {
124c42872d4Smh27603 		char *behavior;
125c42872d4Smh27603 		int cmd;
126c42872d4Smh27603 		int Errno;
127c42872d4Smh27603 	};
128c42872d4Smh27603 	static struct btoc blist[] = {
129c42872d4Smh27603 		"disable",	PM_STOP_CPUPM, EINVAL,
130c42872d4Smh27603 		"enable",	PM_START_CPUPM, EBUSY,
131c42872d4Smh27603 		NULL,		0, 0
132c42872d4Smh27603 	};
133c42872d4Smh27603 	struct btoc *bp;
134c42872d4Smh27603 	char *behavior;
135c42872d4Smh27603 
136c42872d4Smh27603 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
137c42872d4Smh27603 		if (strcmp(behavior, bp->behavior) == 0)
138c42872d4Smh27603 			break;
139c42872d4Smh27603 	}
140c42872d4Smh27603 	if (bp->cmd == 0) {
141c42872d4Smh27603 		mesg(MERR, "invalid cpupm behavior \"%s\"\n", behavior);
142c42872d4Smh27603 		return (NOUP);
143c42872d4Smh27603 	}
144c42872d4Smh27603 	if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
145c42872d4Smh27603 		mesg(MERR, "cpupm %s failed, %s\n",
146c42872d4Smh27603 		    behavior, strerror(errno));
147c42872d4Smh27603 		return (NOUP);
148c42872d4Smh27603 	}
149c42872d4Smh27603 	return (OKUP);
150c42872d4Smh27603 }
151c42872d4Smh27603 
152c42872d4Smh27603 
153c42872d4Smh27603 /*
1542df1fe9cSrandyf  * Two decisions are identical except for the list names and ioctl commands
1552df1fe9cSrandyf  * inputs: whitelist, blacklist, yes, no
1562df1fe9cSrandyf  * if (! ("S3" kstat exists))
1572df1fe9cSrandyf  *	return (no)
1582df1fe9cSrandyf  * if (SystemInformation.Manufacturer == "Sun Microsystems" &&
1592df1fe9cSrandyf  *    (Pref_PM_Profile == Workstation || Pref_PM_Profile == Desktop)) {
1602df1fe9cSrandyf  *	if (platform on blacklist)
1612df1fe9cSrandyf  *		return (no)
1622df1fe9cSrandyf  *	return (yes)
1632df1fe9cSrandyf  * } else {
1642df1fe9cSrandyf  *	if (platform on whitelist)
1652df1fe9cSrandyf  *		return (yes)
1662df1fe9cSrandyf  *	return (no)
1672df1fe9cSrandyf  * }
1682df1fe9cSrandyf  */
1692df1fe9cSrandyf 
1702df1fe9cSrandyf int
1712df1fe9cSrandyf S3_helper(char *whitelist, char *blacklist, int yes, int no, char *keyword,
1722df1fe9cSrandyf 	char *behavior, int *didyes, int suppress)
1732df1fe9cSrandyf {
1742df1fe9cSrandyf 	int oflags = SMB_O_NOCKSUM | SMB_O_NOVERS;
1752df1fe9cSrandyf 	smbios_hdl_t *shp;
1762df1fe9cSrandyf 	smbios_system_t sys;
1772df1fe9cSrandyf 	id_t id;
1782df1fe9cSrandyf 	int ret;
1792df1fe9cSrandyf 	kstat_ctl_t *kc;
1802df1fe9cSrandyf 	kstat_t *ksp;
1812df1fe9cSrandyf 	kstat_named_t *dp;
1822df1fe9cSrandyf 	smbios_info_t info;
1832df1fe9cSrandyf 	int preferred_pm_profile = 0;
1842df1fe9cSrandyf 	char yesstr[32], nostr[32];	/* DEBUG */
1852df1fe9cSrandyf 
1862df1fe9cSrandyf 	*didyes = 0;
1872df1fe9cSrandyf 
1882df1fe9cSrandyf 	strncpy(yesstr, pm_map(yes), sizeof (yesstr));
1892df1fe9cSrandyf 	strncpy(nostr, pm_map(no), sizeof (nostr));
1902df1fe9cSrandyf 	mesg(MDEBUG, "S3_helper(%s, %s, %s, %s, %s, %s)\n", whitelist,
1912df1fe9cSrandyf 	    blacklist, yesstr, nostr, keyword, behavior);
1922df1fe9cSrandyf 	if ((kc = kstat_open()) == NULL) {
1932df1fe9cSrandyf 		mesg(MDEBUG, "kstat_open failed\n");
1942df1fe9cSrandyf 		return (OKUP);
1952df1fe9cSrandyf 	}
1962df1fe9cSrandyf 	ksp = kstat_lookup(kc, "acpi", -1, "acpi");
1972df1fe9cSrandyf 	if (ksp == NULL) {
1982df1fe9cSrandyf 		mesg(MDEBUG, "kstat_lookup 'acpi', -1, 'acpi' failed\n");
1992df1fe9cSrandyf 		kstat_close(kc);
2002df1fe9cSrandyf 		return (OKUP);
2012df1fe9cSrandyf 	}
2022df1fe9cSrandyf 	(void) kstat_read(kc, ksp,  NULL);
2032df1fe9cSrandyf 	dp = kstat_data_lookup(ksp, "S3");
2042df1fe9cSrandyf 	if (dp == NULL || dp->value.l == 0) {
2052df1fe9cSrandyf 		mesg(MDEBUG, "kstat_data_lookup 'S3' fails\n");
2062df1fe9cSrandyf 		if (dp != NULL)
2072df1fe9cSrandyf 			mesg(MDEBUG, "value.l %lx\n", dp->value.l);
2082df1fe9cSrandyf 		kstat_close(kc);
2092df1fe9cSrandyf 		return (do_ioctl(no, keyword, behavior, suppress));
2102df1fe9cSrandyf 	}
2112df1fe9cSrandyf 	mesg(MDEBUG, "kstat indicates S3 support (%lx)\n", dp->value.l);
2122df1fe9cSrandyf 
2132df1fe9cSrandyf 	if (!whitelist_only) {
2142df1fe9cSrandyf 		/*
2152df1fe9cSrandyf 		 * We still have an ACPI ksp, search it again for
2162df1fe9cSrandyf 		 * 'preferred_pm_profile' (needs to be valid if we don't
2172df1fe9cSrandyf 		 * aren't only using a whitelist).
2182df1fe9cSrandyf 		 */
2192df1fe9cSrandyf 		dp = kstat_data_lookup(ksp, "preferred_pm_profile");
2202df1fe9cSrandyf 		if (dp == NULL) {
2212df1fe9cSrandyf 			mesg(MDEBUG, "kstat_data_lookup 'ppmp fails\n");
2222df1fe9cSrandyf 			kstat_close(kc);
2232df1fe9cSrandyf 			return (do_ioctl(no, keyword, behavior, suppress));
2242df1fe9cSrandyf 		}
22525aedaedSmh27603 		mesg(MDEBUG, "kstat indicates preferred_pm_profile is %lx\n",
2262df1fe9cSrandyf 		    dp->value.l);
2272df1fe9cSrandyf 		preferred_pm_profile = dp->value.l;
2282df1fe9cSrandyf 	}
2292df1fe9cSrandyf 	kstat_close(kc);
2302df1fe9cSrandyf 
2312df1fe9cSrandyf 	if ((shp = smbios_open(NULL,
2322df1fe9cSrandyf 	    SMB_VERSION, oflags, &ret)) == NULL) {
2332df1fe9cSrandyf 		/* we promised not to complain */
2342df1fe9cSrandyf 		/* we bail leaving it to the kernel default */
2352df1fe9cSrandyf 		mesg(MDEBUG, "smbios_open failed %d\n", errno);
2362df1fe9cSrandyf 		return (OKUP);
2372df1fe9cSrandyf 	}
2382df1fe9cSrandyf 	if ((id = smbios_info_system(shp, &sys)) == SMB_ERR) {
2392df1fe9cSrandyf 		mesg(MDEBUG, "smbios_info_system failed %d\n", errno);
2402df1fe9cSrandyf 		smbios_close(shp);
2412df1fe9cSrandyf 		return (OKUP);
2422df1fe9cSrandyf 	}
2432df1fe9cSrandyf 	if (smbios_info_common(shp, id, &info) == SMB_ERR) {
2442df1fe9cSrandyf 		mesg(MDEBUG, "smbios_info_common failed %d\n", errno);
2452df1fe9cSrandyf 		smbios_close(shp);
2462df1fe9cSrandyf 		return (OKUP);
2472df1fe9cSrandyf 	}
2482df1fe9cSrandyf 	mesg(MDEBUG, "Manufacturer: %s\n", info.smbi_manufacturer);
2492df1fe9cSrandyf 	mesg(MDEBUG, "Product: %s\n", info.smbi_product);
2502df1fe9cSrandyf 	smbios_close(shp);
2512df1fe9cSrandyf 
2522df1fe9cSrandyf 	if (!whitelist_only) {
2532df1fe9cSrandyf #define	PPP_DESKTOP 1
2542df1fe9cSrandyf #define	PPP_WORKSTATION 3
2552df1fe9cSrandyf 		if (strcmp(info.smbi_manufacturer, "Sun Microsystems") == 0 &&
2562df1fe9cSrandyf 		    (preferred_pm_profile == PPP_DESKTOP ||
2572df1fe9cSrandyf 		    preferred_pm_profile == PPP_WORKSTATION)) {
2582df1fe9cSrandyf 			if (isonlist(blacklist,
2592df1fe9cSrandyf 			    info.smbi_manufacturer, info.smbi_product)) {
2602df1fe9cSrandyf 				return (do_ioctl(no, keyword, behavior,
2612df1fe9cSrandyf 				    suppress));
2622df1fe9cSrandyf 			} else {
2632df1fe9cSrandyf 				ret = do_ioctl(yes, keyword, behavior,
2642df1fe9cSrandyf 				    suppress);
2652df1fe9cSrandyf 				*didyes = (ret == OKUP);
2662df1fe9cSrandyf 				return (ret);
2672df1fe9cSrandyf 			}
2682df1fe9cSrandyf 		}
2692df1fe9cSrandyf 	}
2702df1fe9cSrandyf 	if (isonlist(whitelist,
2712df1fe9cSrandyf 	    info.smbi_manufacturer, info.smbi_product)) {
2722df1fe9cSrandyf 		ret = do_ioctl(yes, keyword, behavior, suppress);
2732df1fe9cSrandyf 		*didyes = (ret == OKUP);
2742df1fe9cSrandyf 		return (ret);
2752df1fe9cSrandyf 	} else {
2762df1fe9cSrandyf 		return (do_ioctl(no, keyword, behavior, suppress));
2772df1fe9cSrandyf 	}
2782df1fe9cSrandyf }
2792df1fe9cSrandyf 
2802df1fe9cSrandyf int
2812df1fe9cSrandyf S3sup(void)	/* S3-support keyword handler */
2822df1fe9cSrandyf {
2832df1fe9cSrandyf 	struct btoc {
2842df1fe9cSrandyf 		char *behavior;
2852df1fe9cSrandyf 		int cmd;
2862df1fe9cSrandyf 	};
2872df1fe9cSrandyf 	static struct btoc blist[] = {
2882df1fe9cSrandyf 		"default",	PM_DEFAULT_ALGORITHM,
2892df1fe9cSrandyf 		"enable",	PM_ENABLE_S3,
2902df1fe9cSrandyf 		"disable",	PM_DISABLE_S3,
2912df1fe9cSrandyf 		NULL,		0
2922df1fe9cSrandyf 	};
2932df1fe9cSrandyf 	struct btoc *bp;
2942df1fe9cSrandyf 	char *behavior;
2952df1fe9cSrandyf 	int dontcare;
2962df1fe9cSrandyf 
2972df1fe9cSrandyf 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
2982df1fe9cSrandyf 		if (strcmp(behavior, bp->behavior) == 0)
2992df1fe9cSrandyf 			break;
3002df1fe9cSrandyf 	}
3012df1fe9cSrandyf 	if (bp->cmd == 0) {
3022df1fe9cSrandyf 		mesg(MERR, "invalid S3-support behavior \"%s\"\n", behavior);
3032df1fe9cSrandyf 		return (NOUP);
3042df1fe9cSrandyf 	}
3052df1fe9cSrandyf 
3062df1fe9cSrandyf 
3072df1fe9cSrandyf 	switch (bp->cmd) {
3082df1fe9cSrandyf 
3092df1fe9cSrandyf 	case PM_ENABLE_S3:
3102df1fe9cSrandyf 	case PM_DISABLE_S3:
3112df1fe9cSrandyf 		return (do_ioctl(bp->cmd, "S3-support", behavior, EBUSY));
3122df1fe9cSrandyf 
3132df1fe9cSrandyf 	case PM_DEFAULT_ALGORITHM:
3142df1fe9cSrandyf 		/*
3152df1fe9cSrandyf 		 * we suppress errors in the "default" case because we
3162df1fe9cSrandyf 		 * already did an invisible default call, so we know we'll
3172df1fe9cSrandyf 		 * get EBUSY
3182df1fe9cSrandyf 		 */
3192df1fe9cSrandyf 		return (S3_helper("S3-support-enable", "S3-support-disable",
3202df1fe9cSrandyf 		    PM_ENABLE_S3, PM_DISABLE_S3, "S3-support", behavior,
3212df1fe9cSrandyf 		    &dontcare, EBUSY));
3222df1fe9cSrandyf 
3232df1fe9cSrandyf 	default:
3242df1fe9cSrandyf 		mesg(MERR, "S3-support %s failed, %s\n", behavior,
3252df1fe9cSrandyf 		    strerror(errno));
3262df1fe9cSrandyf 		return (NOUP);
3272df1fe9cSrandyf 	}
3282df1fe9cSrandyf }
3292df1fe9cSrandyf 
3302df1fe9cSrandyf /*
3312df1fe9cSrandyf  * Check for valid autoS3 behavior and save after ioctl success.
3322df1fe9cSrandyf  */
3332df1fe9cSrandyf int
3342df1fe9cSrandyf autoS3(void)
3352df1fe9cSrandyf {
3362df1fe9cSrandyf 	struct btoc {
3372df1fe9cSrandyf 		char *behavior;
3382df1fe9cSrandyf 		int cmd;
3392df1fe9cSrandyf 	};
3402df1fe9cSrandyf 	static struct btoc blist[] = {
3412df1fe9cSrandyf 		"default",	PM_DEFAULT_ALGORITHM,
3422df1fe9cSrandyf 		"disable",	PM_STOP_AUTOS3,
3432df1fe9cSrandyf 		"enable",	PM_START_AUTOS3,
3442df1fe9cSrandyf 		NULL,		0
3452df1fe9cSrandyf 	};
3462df1fe9cSrandyf 	struct btoc *bp;
3472df1fe9cSrandyf 	char *behavior;
3482df1fe9cSrandyf 	int dontcare;
3492df1fe9cSrandyf 
3502df1fe9cSrandyf 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
3512df1fe9cSrandyf 		if (strcmp(behavior, bp->behavior) == 0)
3522df1fe9cSrandyf 			break;
3532df1fe9cSrandyf 	}
3542df1fe9cSrandyf 	if (bp->cmd == 0) {
3552df1fe9cSrandyf 		mesg(MERR, "invalid autoS3 behavior \"%s\"\n", behavior);
3562df1fe9cSrandyf 		return (NOUP);
3572df1fe9cSrandyf 	}
3582df1fe9cSrandyf 
3592df1fe9cSrandyf 	switch (bp->cmd) {
3602df1fe9cSrandyf 	default:
3612df1fe9cSrandyf 		mesg(MERR, "autoS3 %s failed, %s\n",
3622df1fe9cSrandyf 		    behavior, strerror(errno));
3632df1fe9cSrandyf 		mesg(MDEBUG, "unknown command\n", bp->cmd);
3642df1fe9cSrandyf 		return (OKUP);
3652df1fe9cSrandyf 
3662df1fe9cSrandyf 	case PM_STOP_AUTOS3:
3672df1fe9cSrandyf 	case PM_START_AUTOS3:
3682df1fe9cSrandyf 		return (do_ioctl(bp->cmd, "autoS3", behavior, EBUSY));
3692df1fe9cSrandyf 
3702df1fe9cSrandyf 	case PM_DEFAULT_ALGORITHM:
3712df1fe9cSrandyf 		return (S3_helper("S3-autoenable", "S3-autodisable",
3722df1fe9cSrandyf 		    PM_START_AUTOS3, PM_STOP_AUTOS3, "autoS3", behavior,
3732df1fe9cSrandyf 		    &dontcare, EBUSY));
3742df1fe9cSrandyf 	}
3752df1fe9cSrandyf }
3762df1fe9cSrandyf 
3772df1fe9cSrandyf 
3782df1fe9cSrandyf /*
3797c478bd9Sstevel@tonic-gate  * Check for valid autopm behavior and save after ioctl success.
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate int
3827c478bd9Sstevel@tonic-gate autopm(void)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	struct btoc {
3857c478bd9Sstevel@tonic-gate 		char *behavior;
3867c478bd9Sstevel@tonic-gate 		int cmd, Errno, isdef;
3877c478bd9Sstevel@tonic-gate 	};
3887c478bd9Sstevel@tonic-gate 	static struct btoc blist[] = {
3892df1fe9cSrandyf 		"default",	PM_START_PM,	-1,	1,
3907c478bd9Sstevel@tonic-gate 		"disable",	PM_STOP_PM,	EINVAL,	0,
3917c478bd9Sstevel@tonic-gate 		"enable",	PM_START_PM,	EBUSY,	0,
3927c478bd9Sstevel@tonic-gate 		NULL,		0,		0,	0,
3937c478bd9Sstevel@tonic-gate 	};
3947c478bd9Sstevel@tonic-gate 	struct btoc *bp;
3957c478bd9Sstevel@tonic-gate 	char *behavior;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
3987c478bd9Sstevel@tonic-gate 		if (strcmp(behavior, bp->behavior) == 0)
3997c478bd9Sstevel@tonic-gate 			break;
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 	if (bp->cmd == 0) {
4027c478bd9Sstevel@tonic-gate 		mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior);
4037c478bd9Sstevel@tonic-gate 		return (NOUP);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/*
4077c478bd9Sstevel@tonic-gate 	 * for "default" behavior, do not enable autopm if not ESTAR_V3
4087c478bd9Sstevel@tonic-gate 	 */
4092df1fe9cSrandyf #if defined(__sparc)
4107c478bd9Sstevel@tonic-gate 	if (!bp->isdef || (estar_vers == ESTAR_V3)) {
4117c478bd9Sstevel@tonic-gate 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
4127c478bd9Sstevel@tonic-gate 			mesg(MERR, "autopm %s failed, %s\n",
4137c478bd9Sstevel@tonic-gate 			    behavior, strerror(errno));
4147c478bd9Sstevel@tonic-gate 			return (NOUP);
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 	(void) strcpy(new_cc.apm_behavior, behavior);
4187c478bd9Sstevel@tonic-gate 	return (OKUP);
4192df1fe9cSrandyf #endif
4202df1fe9cSrandyf #if defined(__x86)
4212df1fe9cSrandyf 	if (!bp->isdef) {
4222df1fe9cSrandyf 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
4232df1fe9cSrandyf 			mesg(MERR, "autopm %s failed, %s\n",
4242df1fe9cSrandyf 			    behavior, strerror(errno));
4252df1fe9cSrandyf 			return (NOUP);
4262df1fe9cSrandyf 		}
4272df1fe9cSrandyf 		mesg(MDEBUG, "autopm %s succeeded\n", behavior);
4282df1fe9cSrandyf 
4292df1fe9cSrandyf 		return (OKUP);
4302df1fe9cSrandyf 	} else {
4312df1fe9cSrandyf 		int didenable;
4322df1fe9cSrandyf 		int ret = S3_helper("autopm-enable", "autopm-disable",
4332df1fe9cSrandyf 		    PM_START_PM, PM_STOP_PM, "autopm", behavior, &didenable,
4342df1fe9cSrandyf 		    bp->Errno);
4352df1fe9cSrandyf 		if (didenable) {
4362df1fe9cSrandyf 			/* tell powerd to attach all devices */
4372df1fe9cSrandyf 			new_cc.is_autopm_default = 1;
4382df1fe9cSrandyf 			(void) strcpy(new_cc.apm_behavior, behavior);
4392df1fe9cSrandyf 		}
4402df1fe9cSrandyf 		return (ret);
4412df1fe9cSrandyf 	}
4422df1fe9cSrandyf #endif
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate static int
4477c478bd9Sstevel@tonic-gate gethm(char *src, int *hour, int *min)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	if (sscanf(src, "%d:%d", hour, min) != 2) {
4507c478bd9Sstevel@tonic-gate 		mesg(MERR, "bad time format (%s)\n", src);
4517c478bd9Sstevel@tonic-gate 		return (-1);
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 	return (0);
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate static void
4587c478bd9Sstevel@tonic-gate strcpy_limit(char *dst, char *src, size_t limit, char *info)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	if (strlcpy(dst, src, limit) >= limit)
4617c478bd9Sstevel@tonic-gate 		mesg(MEXIT, "%s is too long (%s)\n", info, src);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate  * Convert autoshutdown idle and start/finish times;
4677c478bd9Sstevel@tonic-gate  * check and record autoshutdown behavior.
4687c478bd9Sstevel@tonic-gate  */
4697c478bd9Sstevel@tonic-gate int
4707c478bd9Sstevel@tonic-gate autosd(void)
4717c478bd9Sstevel@tonic-gate {
4721dc065c6Smh27603 	char **bp, *behavior;
4731dc065c6Smh27603 	char *unrec = gettext("unrecognized autoshutdown behavior");
4747c478bd9Sstevel@tonic-gate 	static char *blist[] = {
4757c478bd9Sstevel@tonic-gate 		"autowakeup", "default", "noshutdown",
4767c478bd9Sstevel@tonic-gate 		"shutdown", "unconfigured", NULL
4777c478bd9Sstevel@tonic-gate 	};
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	new_cc.as_idle = atoi(LINEARG(1));
4807c478bd9Sstevel@tonic-gate 	if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) ||
4817c478bd9Sstevel@tonic-gate 	    gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm))
4827c478bd9Sstevel@tonic-gate 		return (NOUP);
4831dc065c6Smh27603 	mesg(MDEBUG, "idle %d, start %d:%02d, finish %d:%02d\n",
4847c478bd9Sstevel@tonic-gate 	    new_cc.as_idle, new_cc.as_sh, new_cc.as_sm,
4857c478bd9Sstevel@tonic-gate 	    new_cc.as_fh, new_cc.as_fm);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	for (behavior = LINEARG(4), bp = blist; *bp; bp++) {
4887c478bd9Sstevel@tonic-gate 		if (strcmp(behavior, *bp) == 0)
4897c478bd9Sstevel@tonic-gate 			break;
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 	if (*bp == NULL) {
4927c478bd9Sstevel@tonic-gate 		mesg(MERR, "%s: \"%s\"\n", unrec, behavior);
4937c478bd9Sstevel@tonic-gate 		return (NOUP);
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	STRCPYLIM(new_cc.as_behavior, *bp, unrec);
4967c478bd9Sstevel@tonic-gate 	return (OKUP);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * Check for a real device and try to resolve to a full path.
5027c478bd9Sstevel@tonic-gate  * The orig/resolved path may be modified into a prom pathname,
5037c478bd9Sstevel@tonic-gate  * and an allocated copy of the result is stored at *destp;
5047c478bd9Sstevel@tonic-gate  * the caller will need to free that space.  Returns 1 for any
5057c478bd9Sstevel@tonic-gate  * error, otherwise 0; also sets *errp after an alloc error.
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate static int
5087c478bd9Sstevel@tonic-gate devpath(char **destp, char *src, int *errp)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	struct stat stbuf;
5117c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX];
5127c478bd9Sstevel@tonic-gate 	char *cp, *dstr;
5137c478bd9Sstevel@tonic-gate 	int devok, dcs = 0;
5147c478bd9Sstevel@tonic-gate 	size_t len;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	/*
5177c478bd9Sstevel@tonic-gate 	 * When there's a real device, try to resolve the path
5187c478bd9Sstevel@tonic-gate 	 * and trim the leading "/devices" component.
5197c478bd9Sstevel@tonic-gate 	 */
5207c478bd9Sstevel@tonic-gate 	if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) {
5217c478bd9Sstevel@tonic-gate 		if (realpath(src, buf) == NULL) {
5227c478bd9Sstevel@tonic-gate 			mesg(MERR, "realpath cannot resolve \"%s\"\n",
5237c478bd9Sstevel@tonic-gate 			    src, strerror(errno));
5247c478bd9Sstevel@tonic-gate 			return (1);
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 		src = buf;
5277c478bd9Sstevel@tonic-gate 		dstr = "/devices";
5287c478bd9Sstevel@tonic-gate 		len = strlen(dstr);
5297c478bd9Sstevel@tonic-gate 		dcs = (strncmp(src, dstr, len) == 0);
5307c478bd9Sstevel@tonic-gate 		if (dcs)
5317c478bd9Sstevel@tonic-gate 			src += len;
5327c478bd9Sstevel@tonic-gate 	} else
5337c478bd9Sstevel@tonic-gate 		mesg(MDEBUG, stat_fmt, src, strerror(errno));
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/*
5367c478bd9Sstevel@tonic-gate 	 * When the path has ":anything", display an error for
5377c478bd9Sstevel@tonic-gate 	 * a non-device or truncate a resolved+modifed path.
5387c478bd9Sstevel@tonic-gate 	 */
5397c478bd9Sstevel@tonic-gate 	if (cp = strchr(src, ':')) {
5407c478bd9Sstevel@tonic-gate 		if (devok == 0) {
5417c478bd9Sstevel@tonic-gate 			mesg(MERR, "physical path may not contain "
5427c478bd9Sstevel@tonic-gate 			    "a minor string (%s)\n", src);
5437c478bd9Sstevel@tonic-gate 			return (1);
5447c478bd9Sstevel@tonic-gate 		} else if (dcs)
5457c478bd9Sstevel@tonic-gate 			*cp = '\0';
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	if ((*destp = strdup(src)) == NULL) {
5497c478bd9Sstevel@tonic-gate 		*errp = NOUP;
5507c478bd9Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, src, strerror(errno));
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	return (*destp == NULL);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * Call pm ioctl request(s) to set property/device dependencies.
5587c478bd9Sstevel@tonic-gate  */
5597c478bd9Sstevel@tonic-gate static int
5607c478bd9Sstevel@tonic-gate dev_dep_common(int isprop)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	int cmd, argn, upval = OKUP;
5637c478bd9Sstevel@tonic-gate 	char *src, *first, **destp;
5647c478bd9Sstevel@tonic-gate 	pm_req_t pmreq;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
5677c478bd9Sstevel@tonic-gate 	src = LINEARG(1);
5687c478bd9Sstevel@tonic-gate 	if (isprop) {
5697c478bd9Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT_PROPERTY;
5707c478bd9Sstevel@tonic-gate 		first = NULL;
5717c478bd9Sstevel@tonic-gate 		pmreq.pmreq_kept = src;
5727c478bd9Sstevel@tonic-gate 	} else {
5737c478bd9Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT;
5747c478bd9Sstevel@tonic-gate 		if (devpath(&first, src, &upval))
5757c478bd9Sstevel@tonic-gate 			return (upval);
5767c478bd9Sstevel@tonic-gate 		pmreq.pmreq_kept = first;
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 	destp = &pmreq.pmreq_keeper;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/*
5817c478bd9Sstevel@tonic-gate 	 * Now loop through any dependents.
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
5847c478bd9Sstevel@tonic-gate 		if (devpath(destp, src, &upval)) {
5857c478bd9Sstevel@tonic-gate 			if (upval != OKUP)
5867c478bd9Sstevel@tonic-gate 				return (upval);
5877c478bd9Sstevel@tonic-gate 			break;
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 		if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) {
5907c478bd9Sstevel@tonic-gate 			mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n"
5917c478bd9Sstevel@tonic-gate 			    "kept \"%s\", keeper \"%s\"\n",
5927c478bd9Sstevel@tonic-gate 			    cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper);
5937c478bd9Sstevel@tonic-gate 			mesg(MERR, "cannot set \"%s\" dependency "
5947c478bd9Sstevel@tonic-gate 			    "for \"%s\", %s\n", pmreq.pmreq_keeper,
5957c478bd9Sstevel@tonic-gate 			    pmreq.pmreq_kept, strerror(errno));
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 		free(*destp);
5987c478bd9Sstevel@tonic-gate 		*destp = NULL;
5997c478bd9Sstevel@tonic-gate 		if (upval != OKUP)
6007c478bd9Sstevel@tonic-gate 			break;
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	free(first);
6047c478bd9Sstevel@tonic-gate 	return (upval);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate int
6097c478bd9Sstevel@tonic-gate ddprop(void)
6107c478bd9Sstevel@tonic-gate {
6117c478bd9Sstevel@tonic-gate 	return (dev_dep_common(1));
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate int
6167c478bd9Sstevel@tonic-gate devdep(void)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	return (dev_dep_common(0));
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate /*
6237c478bd9Sstevel@tonic-gate  * Convert a numeric string (with a possible trailing scaling byte)
6247c478bd9Sstevel@tonic-gate  * into an integer.  Returns a converted value and *nerrp unchanged,
6257c478bd9Sstevel@tonic-gate  * or 0 with *nerrp set to 1 for a conversion error.
6267c478bd9Sstevel@tonic-gate  */
6277c478bd9Sstevel@tonic-gate static int
6287c478bd9Sstevel@tonic-gate get_scaled_value(char *str, int *nerrp)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate 	longlong_t svalue = 0, factor = 1;
6317c478bd9Sstevel@tonic-gate 	char *sp;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	errno = 0;
6347c478bd9Sstevel@tonic-gate 	svalue = strtol(str, &sp, 0);
6357c478bd9Sstevel@tonic-gate 	if (errno || (*str != '-' && (*str < '0' || *str > '9')))
6367c478bd9Sstevel@tonic-gate 		*nerrp = 1;
6377c478bd9Sstevel@tonic-gate 	else if (sp && *sp != '\0') {
6387c478bd9Sstevel@tonic-gate 		if (*sp == 'h')
6397c478bd9Sstevel@tonic-gate 			factor = 3600;
6407c478bd9Sstevel@tonic-gate 		else if (*sp == 'm')
6417c478bd9Sstevel@tonic-gate 			factor = 60;
6427c478bd9Sstevel@tonic-gate 		else if (*sp != 's')
6437c478bd9Sstevel@tonic-gate 			*nerrp = 1;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	/* any bytes following sp are ignored */
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	if (*nerrp == 0) {
6487c478bd9Sstevel@tonic-gate 		svalue *= factor;
6497c478bd9Sstevel@tonic-gate 		if (svalue < INT_MIN || svalue > INT_MAX)
6507c478bd9Sstevel@tonic-gate 			*nerrp = 1;
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate 	if (*nerrp)
6537c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, str);
6547c478bd9Sstevel@tonic-gate 	mesg(MDEBUG, "got scaled value %d\n", (int)svalue);
6557c478bd9Sstevel@tonic-gate 	return ((int)svalue);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate  * Increment the count of threshold values,
6617c478bd9Sstevel@tonic-gate  * reallocate *vlistp and append another element.
6627c478bd9Sstevel@tonic-gate  * Returns 1 on error, otherwise 0.
6637c478bd9Sstevel@tonic-gate  */
6647c478bd9Sstevel@tonic-gate static int
6657c478bd9Sstevel@tonic-gate vlist_append(int **vlistp, int *vcntp, int value)
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	(*vcntp)++;
6687c478bd9Sstevel@tonic-gate 	if (*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp)))
6697c478bd9Sstevel@tonic-gate 		*(*vlistp + *vcntp - 1) = value;
6707c478bd9Sstevel@tonic-gate 	else
6717c478bd9Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, "threshold list", strerror(errno));
6727c478bd9Sstevel@tonic-gate 	return (*vlistp == NULL);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate  * Convert a single threshold string or paren groups of thresh's as
6787c478bd9Sstevel@tonic-gate  * described below.  All thresh's are saved to an allocated list at
6797c478bd9Sstevel@tonic-gate  * *vlistp; the caller will need to free that space.  On return:
6807c478bd9Sstevel@tonic-gate  * *vcntp is the count of the vlist array, and vlist is either
6817c478bd9Sstevel@tonic-gate  * a single thresh or N groups of thresh's with a trailing zero:
6827c478bd9Sstevel@tonic-gate  * (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0.
6837c478bd9Sstevel@tonic-gate  * Returns 0 when all conversions were OK, and 1 for any syntax,
6847c478bd9Sstevel@tonic-gate  * conversion, or alloc error.
6857c478bd9Sstevel@tonic-gate  */
6867c478bd9Sstevel@tonic-gate static int
6877c478bd9Sstevel@tonic-gate get_thresh(int **vlistp, int *vcntp)
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate 	int argn, value, gci, grp_cnt = 0, paren = 0, nerr = 0;
6907c478bd9Sstevel@tonic-gate 	char *rp, *src;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
6937c478bd9Sstevel@tonic-gate 		if (*src == LPAREN) {
6947c478bd9Sstevel@tonic-gate 			gci = *vcntp;
6957c478bd9Sstevel@tonic-gate 			if (nerr = vlist_append(vlistp, vcntp, 0))
6967c478bd9Sstevel@tonic-gate 				break;
6977c478bd9Sstevel@tonic-gate 			paren = 1;
6987c478bd9Sstevel@tonic-gate 			src++;
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 		if (*(rp = LASTBYTE(src)) == RPAREN) {
7017c478bd9Sstevel@tonic-gate 			if (paren) {
7027c478bd9Sstevel@tonic-gate 				grp_cnt = *vcntp - gci;
7037c478bd9Sstevel@tonic-gate 				*(*vlistp + gci) = grp_cnt;
7047c478bd9Sstevel@tonic-gate 				paren = 0;
7057c478bd9Sstevel@tonic-gate 				*rp = '\0';
7067c478bd9Sstevel@tonic-gate 			} else {
7077c478bd9Sstevel@tonic-gate 				nerr = 1;
7087c478bd9Sstevel@tonic-gate 				break;
7097c478bd9Sstevel@tonic-gate 			}
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		value = get_scaled_value(src, &nerr);
7137c478bd9Sstevel@tonic-gate 		if (nerr || (nerr = vlist_append(vlistp, vcntp, value)))
7147c478bd9Sstevel@tonic-gate 			break;
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	if (nerr == 0 && grp_cnt)
7187c478bd9Sstevel@tonic-gate 		nerr = vlist_append(vlistp, vcntp, 0);
7197c478bd9Sstevel@tonic-gate 	return (nerr);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate /*
7247c478bd9Sstevel@tonic-gate  * Set device thresholds from (3) formats:
7257c478bd9Sstevel@tonic-gate  * 	path	"always-on"
7267c478bd9Sstevel@tonic-gate  * 	path	time-spec: [0-9]+[{h,m,s}]
7277c478bd9Sstevel@tonic-gate  *	path	(ts1 ts2 ...)+
7287c478bd9Sstevel@tonic-gate  */
7297c478bd9Sstevel@tonic-gate int
7307c478bd9Sstevel@tonic-gate devthr(void)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate 	int cmd, upval = OKUP, nthresh = 0, *vlist = NULL;
7337c478bd9Sstevel@tonic-gate 	pm_req_t pmreq;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
7367c478bd9Sstevel@tonic-gate 	if (devpath(&pmreq.physpath, LINEARG(1), &upval))
7377c478bd9Sstevel@tonic-gate 		return (upval);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (strcmp(LINEARG(2), always_on) == 0) {
7407c478bd9Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
7417c478bd9Sstevel@tonic-gate 		pmreq.value = INT_MAX;
7427c478bd9Sstevel@tonic-gate 	} else if (get_thresh(&vlist, &nthresh)) {
7437c478bd9Sstevel@tonic-gate 		mesg(MERR, bad_thresh_fmt);
7447c478bd9Sstevel@tonic-gate 		upval = NOUP;
7457c478bd9Sstevel@tonic-gate 	} else if (nthresh == 1) {
7467c478bd9Sstevel@tonic-gate 		pmreq.value = *vlist;
7477c478bd9Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
7487c478bd9Sstevel@tonic-gate 	} else {
7497c478bd9Sstevel@tonic-gate 		pmreq.data = vlist;
7507c478bd9Sstevel@tonic-gate 		pmreq.datasize = (nthresh * sizeof (*vlist));
7517c478bd9Sstevel@tonic-gate 		cmd = PM_SET_COMPONENT_THRESHOLDS;
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1)
7557c478bd9Sstevel@tonic-gate 		mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno));
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	free(vlist);
7587c478bd9Sstevel@tonic-gate 	free(pmreq.physpath);
7597c478bd9Sstevel@tonic-gate 	return (upval);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate static int
7647c478bd9Sstevel@tonic-gate scan_int(char *src, int *dst)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	long lval;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	errno = 0;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	lval = strtol(LINEARG(1), NULL, 0);
7717c478bd9Sstevel@tonic-gate 	if (errno || lval > INT_MAX || lval < 0) {
7727c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
7737c478bd9Sstevel@tonic-gate 		return (NOUP);
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	*dst = (int)lval;
7777c478bd9Sstevel@tonic-gate 	return (OKUP);
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate static int
7817c478bd9Sstevel@tonic-gate scan_float(char *src, float *dst)
7827c478bd9Sstevel@tonic-gate {
7837c478bd9Sstevel@tonic-gate 	float fval;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	errno = 0;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	fval = strtof(src, NULL);
7887c478bd9Sstevel@tonic-gate 	if (errno || fval < 0.0) {
7897c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
7907c478bd9Sstevel@tonic-gate 		return (NOUP);
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	*dst = fval;
7947c478bd9Sstevel@tonic-gate 	return (OKUP);
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate int
7997c478bd9Sstevel@tonic-gate dreads(void)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.diskreads_thold));
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate  * Set pathname for idlecheck;
8077c478bd9Sstevel@tonic-gate  * an overflowed pathname is treated as a fatal error.
8087c478bd9Sstevel@tonic-gate  */
8097c478bd9Sstevel@tonic-gate int
8107c478bd9Sstevel@tonic-gate idlechk(void)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path");
8137c478bd9Sstevel@tonic-gate 	return (OKUP);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate int
8187c478bd9Sstevel@tonic-gate loadavg(void)
8197c478bd9Sstevel@tonic-gate {
8207c478bd9Sstevel@tonic-gate 	return (scan_float(LINEARG(1), &new_cc.loadaverage_thold));
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate int
8257c478bd9Sstevel@tonic-gate nfsreq(void)
8267c478bd9Sstevel@tonic-gate {
8277c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold));
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate #ifdef sparc
8317c478bd9Sstevel@tonic-gate static char open_fmt[] = "cannot open \"%s\", %s\n";
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate /*
8347c478bd9Sstevel@tonic-gate  * Verify the filesystem type for a regular statefile is "ufs"
8357c478bd9Sstevel@tonic-gate  * or verify a block device is not in use as a mounted filesytem.
8367c478bd9Sstevel@tonic-gate  * Returns 1 if any error, otherwise 0.
8377c478bd9Sstevel@tonic-gate  */
8387c478bd9Sstevel@tonic-gate static int
8397c478bd9Sstevel@tonic-gate check_mount(char *sfile, dev_t sfdev, int ufs)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	char *src, *err_fmt = NULL, *mnttab = MNTTAB;
8427c478bd9Sstevel@tonic-gate 	int rgent, match = 0;
843*e7cbe64fSgw25295 	struct mnttab zroot = { 0 };
844*e7cbe64fSgw25295 	struct mnttab entry;
8457c478bd9Sstevel@tonic-gate 	struct extmnttab ent;
8467c478bd9Sstevel@tonic-gate 	FILE *fp;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
8497c478bd9Sstevel@tonic-gate 		mesg(MERR, open_fmt, mnttab, strerror(errno));
8507c478bd9Sstevel@tonic-gate 		return (1);
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 
853*e7cbe64fSgw25295 	if (ufs) {
854*e7cbe64fSgw25295 		zroot.mnt_mountp = "/";
855*e7cbe64fSgw25295 		zroot.mnt_fstype = "zfs";
856*e7cbe64fSgw25295 		if (getmntany(fp, &entry, &zroot) == 0) {
857*e7cbe64fSgw25295 			err_fmt = "ufs statefile with zfs root is not"
858*e7cbe64fSgw25295 			    " supported\n";
859*e7cbe64fSgw25295 			mesg(MERR, err_fmt, sfile);
860*e7cbe64fSgw25295 			fclose(fp);
861*e7cbe64fSgw25295 			return (1);
862*e7cbe64fSgw25295 		}
863*e7cbe64fSgw25295 		resetmnttab(fp);
864*e7cbe64fSgw25295 	}
8657c478bd9Sstevel@tonic-gate 	/*
8667c478bd9Sstevel@tonic-gate 	 * Search for a matching dev_t;
8677c478bd9Sstevel@tonic-gate 	 * ignore non-ufs filesystems for a regular statefile.
8687c478bd9Sstevel@tonic-gate 	 */
8697c478bd9Sstevel@tonic-gate 	while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) {
8707c478bd9Sstevel@tonic-gate 		if (rgent > 0) {
8717c478bd9Sstevel@tonic-gate 			mesg(MERR, "error reading \"%s\"\n", mnttab);
8727c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
8737c478bd9Sstevel@tonic-gate 			return (1);
8747c478bd9Sstevel@tonic-gate 		} else if (ufs && strcmp(ent.mnt_fstype, "ufs"))
8757c478bd9Sstevel@tonic-gate 			continue;
8767c478bd9Sstevel@tonic-gate 		else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) {
8777c478bd9Sstevel@tonic-gate 			match = 1;
8787c478bd9Sstevel@tonic-gate 			break;
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/*
8837c478bd9Sstevel@tonic-gate 	 * No match is needed for a block device statefile,
8847c478bd9Sstevel@tonic-gate 	 * a match is needed for a regular statefile.
8857c478bd9Sstevel@tonic-gate 	 */
8867c478bd9Sstevel@tonic-gate 	if (match == 0) {
887*e7cbe64fSgw25295 		if (new_cc.cf_type != CFT_UFS)
8887c478bd9Sstevel@tonic-gate 			STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile");
8897c478bd9Sstevel@tonic-gate 		else
8907c478bd9Sstevel@tonic-gate 			err_fmt = "cannot find ufs mount point for \"%s\"\n";
8917c478bd9Sstevel@tonic-gate 	} else if (new_cc.cf_type == CFT_UFS) {
8927c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry");
8937c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special");
8947c478bd9Sstevel@tonic-gate 		while (*(sfile + 1) == '/') sfile++;
8957c478bd9Sstevel@tonic-gate 		src = sfile + strlen(ent.mnt_mountp);
8967c478bd9Sstevel@tonic-gate 		while (*src == '/') src++;
8977c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_path, src, "statefile path");
8987c478bd9Sstevel@tonic-gate 	} else
8997c478bd9Sstevel@tonic-gate 		err_fmt = "statefile device \"%s\" is a mounted filesystem\n";
900*e7cbe64fSgw25295 	(void) fclose(fp);
9017c478bd9Sstevel@tonic-gate 	if (err_fmt)
9027c478bd9Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
9037c478bd9Sstevel@tonic-gate 	return (err_fmt != NULL);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate /*
9087c478bd9Sstevel@tonic-gate  * Convert a Unix device to a prom device and save on success,
9097c478bd9Sstevel@tonic-gate  * log any ioctl/conversion error.
9107c478bd9Sstevel@tonic-gate  */
9117c478bd9Sstevel@tonic-gate static int
912*e7cbe64fSgw25295 utop(char *fs_name, char *prom_name)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate 	union obpbuf {
9157c478bd9Sstevel@tonic-gate 		char	buf[OBP_MAXPATHLEN + sizeof (uint_t)];
9167c478bd9Sstevel@tonic-gate 		struct	openpromio oppio;
9177c478bd9Sstevel@tonic-gate 	};
9187c478bd9Sstevel@tonic-gate 	union obpbuf oppbuf;
9197c478bd9Sstevel@tonic-gate 	struct openpromio *opp;
9207c478bd9Sstevel@tonic-gate 	char *promdev = "/dev/openprom";
9217c478bd9Sstevel@tonic-gate 	int fd, upval;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	if ((fd = open(promdev, O_RDONLY)) == -1) {
9247c478bd9Sstevel@tonic-gate 		mesg(MERR, open_fmt, promdev, strerror(errno));
9257c478bd9Sstevel@tonic-gate 		return (NOUP);
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	opp = &oppbuf.oppio;
9297c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPATHLEN;
930*e7cbe64fSgw25295 	strcpy_limit(opp->oprom_array, fs_name,
9317c478bd9Sstevel@tonic-gate 	    OBP_MAXPATHLEN, "statefile device");
9327c478bd9Sstevel@tonic-gate 	upval = ioctl(fd, OPROMDEV2PROMNAME, opp);
9337c478bd9Sstevel@tonic-gate 	(void) close(fd);
934*e7cbe64fSgw25295 	if (upval == OKUP) {
935*e7cbe64fSgw25295 		strcpy_limit(prom_name, opp->oprom_array, OBP_MAXPATHLEN,
936*e7cbe64fSgw25295 		    "prom device");
937*e7cbe64fSgw25295 	} else {
9387c478bd9Sstevel@tonic-gate 		openlog("pmconfig", 0, LOG_DAEMON);
9397c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE,
9407c478bd9Sstevel@tonic-gate 		    gettext("cannot convert \"%s\" to prom device"),
941*e7cbe64fSgw25295 		    fs_name);
9427c478bd9Sstevel@tonic-gate 		closelog();
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	return (upval);
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate 
948*e7cbe64fSgw25295 /*
949*e7cbe64fSgw25295  * given the path to a zvol, return the cXtYdZ name
950*e7cbe64fSgw25295  * returns < 0 on error, 0 if it isn't a zvol, > 1 on success
951*e7cbe64fSgw25295  */
952*e7cbe64fSgw25295 static int
953*e7cbe64fSgw25295 ztop(char *arg, char *diskname)
954*e7cbe64fSgw25295 {
955*e7cbe64fSgw25295 	zpool_handle_t *zpool_handle;
956*e7cbe64fSgw25295 	nvlist_t *config, *nvroot;
957*e7cbe64fSgw25295 	nvlist_t **child;
958*e7cbe64fSgw25295 	uint_t children;
959*e7cbe64fSgw25295 	libzfs_handle_t *lzfs;
960*e7cbe64fSgw25295 	char *vname;
961*e7cbe64fSgw25295 	char *p;
962*e7cbe64fSgw25295 	char pool_name[MAXPATHLEN];
963*e7cbe64fSgw25295 
964*e7cbe64fSgw25295 	if (strncmp(arg, "/dev/zvol/dsk/", 14)) {
965*e7cbe64fSgw25295 		return (0);
966*e7cbe64fSgw25295 	}
967*e7cbe64fSgw25295 	arg += 14;
968*e7cbe64fSgw25295 	strncpy(pool_name, arg, MAXPATHLEN);
969*e7cbe64fSgw25295 	if (p = strchr(pool_name, '/'))
970*e7cbe64fSgw25295 		*p = '\0';
971*e7cbe64fSgw25295 	STRCPYLIM(new_cc.cf_fs, p + 1, "statefile path");
972*e7cbe64fSgw25295 
973*e7cbe64fSgw25295 	if ((lzfs = libzfs_init()) == NULL) {
974*e7cbe64fSgw25295 		mesg(MERR, "failed to initialize ZFS library\n");
975*e7cbe64fSgw25295 		return (-1);
976*e7cbe64fSgw25295 	}
977*e7cbe64fSgw25295 	if ((zpool_handle = zpool_open(lzfs, pool_name)) == NULL) {
978*e7cbe64fSgw25295 		mesg(MERR, "couldn't open pool '%s'\n", pool_name);
979*e7cbe64fSgw25295 		libzfs_fini(lzfs);
980*e7cbe64fSgw25295 		return (-1);
981*e7cbe64fSgw25295 	}
982*e7cbe64fSgw25295 	config = zpool_get_config(zpool_handle, NULL);
983*e7cbe64fSgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
984*e7cbe64fSgw25295 	    &nvroot) != 0) {
985*e7cbe64fSgw25295 		zpool_close(zpool_handle);
986*e7cbe64fSgw25295 		libzfs_fini(lzfs);
987*e7cbe64fSgw25295 		return (-1);
988*e7cbe64fSgw25295 	}
989*e7cbe64fSgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
990*e7cbe64fSgw25295 	    &child, &children) == 0);
991*e7cbe64fSgw25295 	if (children != 1) {
992*e7cbe64fSgw25295 		mesg(MERR, "expected one vdev, got %d\n", children);
993*e7cbe64fSgw25295 		zpool_close(zpool_handle);
994*e7cbe64fSgw25295 		libzfs_fini(lzfs);
995*e7cbe64fSgw25295 		return (-1);
996*e7cbe64fSgw25295 	}
997*e7cbe64fSgw25295 	vname = zpool_vdev_name(lzfs, zpool_handle, child[0]);
998*e7cbe64fSgw25295 	if (vname == NULL) {
999*e7cbe64fSgw25295 		mesg(MERR, "couldn't determine vdev name\n");
1000*e7cbe64fSgw25295 		zpool_close(zpool_handle);
1001*e7cbe64fSgw25295 		libzfs_fini(lzfs);
1002*e7cbe64fSgw25295 		return (-1);
1003*e7cbe64fSgw25295 	}
1004*e7cbe64fSgw25295 	strcpy(diskname, "/dev/dsk/");
1005*e7cbe64fSgw25295 	strcat(diskname, vname);
1006*e7cbe64fSgw25295 	free(vname);
1007*e7cbe64fSgw25295 	zpool_close(zpool_handle);
1008*e7cbe64fSgw25295 	libzfs_fini(lzfs);
1009*e7cbe64fSgw25295 	return (1);
1010*e7cbe64fSgw25295 }
1011*e7cbe64fSgw25295 
1012*e7cbe64fSgw25295 /*
1013*e7cbe64fSgw25295  * returns NULL if the slice is good (e.g. does not start at block
1014*e7cbe64fSgw25295  * zero, or a string describing the error if it doesn't
1015*e7cbe64fSgw25295  */
1016*e7cbe64fSgw25295 static boolean_t
1017*e7cbe64fSgw25295 is_good_slice(char *sfile, char **err)
1018*e7cbe64fSgw25295 {
1019*e7cbe64fSgw25295 	int fd, rc;
1020*e7cbe64fSgw25295 	struct vtoc vtoc;
1021*e7cbe64fSgw25295 	dk_gpt_t *gpt;
1022*e7cbe64fSgw25295 	char rdskname[MAXPATHLEN];
1023*e7cbe64fSgw25295 	char *x, *y;
1024*e7cbe64fSgw25295 
1025*e7cbe64fSgw25295 	*err = NULL;
1026*e7cbe64fSgw25295 	/* convert from dsk to rdsk */
1027*e7cbe64fSgw25295 	STRCPYLIM(rdskname, sfile, "disk name");
1028*e7cbe64fSgw25295 	x = strstr(rdskname, "dsk/");
1029*e7cbe64fSgw25295 	y = strstr(sfile, "dsk/");
1030*e7cbe64fSgw25295 	if (x != NULL) {
1031*e7cbe64fSgw25295 		*x++ = 'r';
1032*e7cbe64fSgw25295 		strcpy(x, y);
1033*e7cbe64fSgw25295 	}
1034*e7cbe64fSgw25295 
1035*e7cbe64fSgw25295 	if ((fd = open(rdskname, O_RDONLY)) == -1) {
1036*e7cbe64fSgw25295 		*err = "could not open '%s'\n";
1037*e7cbe64fSgw25295 	} else if ((rc = read_vtoc(fd, &vtoc)) >= 0) {
1038*e7cbe64fSgw25295 		/*
1039*e7cbe64fSgw25295 		 * we got a slice number; now check the block
1040*e7cbe64fSgw25295 		 * number where the slice starts
1041*e7cbe64fSgw25295 		 */
1042*e7cbe64fSgw25295 		if (vtoc.v_part[rc].p_start < 2)
1043*e7cbe64fSgw25295 			*err = "using '%s' would clobber the disk label\n";
1044*e7cbe64fSgw25295 		close(fd);
1045*e7cbe64fSgw25295 		return (*err ? B_FALSE : B_TRUE);
1046*e7cbe64fSgw25295 	} else if ((rc == VT_ENOTSUP) &&
1047*e7cbe64fSgw25295 	    (efi_alloc_and_read(fd, &gpt)) >= 0) {
1048*e7cbe64fSgw25295 		/* EFI slices don't clobber the disk label */
1049*e7cbe64fSgw25295 		free(gpt);
1050*e7cbe64fSgw25295 		close(fd);
1051*e7cbe64fSgw25295 		return (B_TRUE);
1052*e7cbe64fSgw25295 	} else
1053*e7cbe64fSgw25295 		*err = "could not read partition table from '%s'\n";
1054*e7cbe64fSgw25295 	return (B_FALSE);
1055*e7cbe64fSgw25295 }
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate /*
10587c478bd9Sstevel@tonic-gate  * Check for a valid statefile pathname, inode and mount status.
10597c478bd9Sstevel@tonic-gate  */
10607c478bd9Sstevel@tonic-gate int
10617c478bd9Sstevel@tonic-gate sfpath(void)
10627c478bd9Sstevel@tonic-gate {
10637c478bd9Sstevel@tonic-gate 	static int statefile;
10647c478bd9Sstevel@tonic-gate 	char *err_fmt = NULL;
10657c478bd9Sstevel@tonic-gate 	char *sfile, *sp, ch;
1066*e7cbe64fSgw25295 	char diskname[256];
10677c478bd9Sstevel@tonic-gate 	struct stat stbuf;
10687c478bd9Sstevel@tonic-gate 	int dir = 0;
10697c478bd9Sstevel@tonic-gate 	dev_t dev;
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	if (statefile) {
10727c478bd9Sstevel@tonic-gate 		mesg(MERR, "ignored redundant statefile entry\n");
10737c478bd9Sstevel@tonic-gate 		return (OKUP);
10747c478bd9Sstevel@tonic-gate 	} else if (ua_err) {
10757c478bd9Sstevel@tonic-gate 		if (ua_err != ENOTSUP)
10767c478bd9Sstevel@tonic-gate 			mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n",
10777c478bd9Sstevel@tonic-gate 			    strerror(ua_err));
10787c478bd9Sstevel@tonic-gate 		return (NOUP);
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	/*
10827c478bd9Sstevel@tonic-gate 	 * Check for an absolute path and trim any trailing '/'.
10837c478bd9Sstevel@tonic-gate 	 */
10847c478bd9Sstevel@tonic-gate 	sfile = LINEARG(1);
10857c478bd9Sstevel@tonic-gate 	if (*sfile != '/') {
10867c478bd9Sstevel@tonic-gate 		mesg(MERR, "statefile requires an absolute path\n");
10877c478bd9Sstevel@tonic-gate 		return (NOUP);
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 	for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--)
10907c478bd9Sstevel@tonic-gate 		*sp = '\0';
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/*
10937c478bd9Sstevel@tonic-gate 	 * If the statefile doesn't exist, the leading path must be a dir.
10947c478bd9Sstevel@tonic-gate 	 */
10957c478bd9Sstevel@tonic-gate 	if (stat(sfile, &stbuf) == -1) {
10967c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
10977c478bd9Sstevel@tonic-gate 			dir = 1;
10987c478bd9Sstevel@tonic-gate 			if ((sp = strrchr(sfile, '/')) == sfile)
10997c478bd9Sstevel@tonic-gate 				sp++;
11007c478bd9Sstevel@tonic-gate 			ch = *sp;
11017c478bd9Sstevel@tonic-gate 			*sp = '\0';
11027c478bd9Sstevel@tonic-gate 			if (stat(sfile, &stbuf) == -1)
11037c478bd9Sstevel@tonic-gate 				err_fmt = stat_fmt;
11047c478bd9Sstevel@tonic-gate 			*sp = ch;
11057c478bd9Sstevel@tonic-gate 		} else
11067c478bd9Sstevel@tonic-gate 			err_fmt = stat_fmt;
11077c478bd9Sstevel@tonic-gate 		if (err_fmt) {
11087c478bd9Sstevel@tonic-gate 			mesg(MERR, err_fmt, sfile, strerror(errno));
11097c478bd9Sstevel@tonic-gate 			return (NOUP);
11107c478bd9Sstevel@tonic-gate 		}
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/*
11147c478bd9Sstevel@tonic-gate 	 * Check for regular/dir/block types, set cf_type and dev.
11157c478bd9Sstevel@tonic-gate 	 */
11167c478bd9Sstevel@tonic-gate 	if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) {
11177c478bd9Sstevel@tonic-gate 		new_cc.cf_type = CFT_UFS;
11187c478bd9Sstevel@tonic-gate 		dev = stbuf.st_dev;
11197c478bd9Sstevel@tonic-gate 	} else if (S_ISBLK(stbuf.st_mode)) {
1120*e7cbe64fSgw25295 		if (is_good_slice(sfile, &err_fmt)) {
1121*e7cbe64fSgw25295 			switch (ztop(sfile, diskname)) {
1122*e7cbe64fSgw25295 				case 1:
1123*e7cbe64fSgw25295 					new_cc.cf_type = CFT_ZVOL;
1124*e7cbe64fSgw25295 					break;
1125*e7cbe64fSgw25295 				case 0:
11267c478bd9Sstevel@tonic-gate 					new_cc.cf_type = CFT_SPEC;
1127*e7cbe64fSgw25295 					break;
1128*e7cbe64fSgw25295 				case -1:
1129*e7cbe64fSgw25295 				default:
1130*e7cbe64fSgw25295 					return (NOUP);
1131*e7cbe64fSgw25295 			}
11327c478bd9Sstevel@tonic-gate 			dev = stbuf.st_rdev;
1133*e7cbe64fSgw25295 		}
11347c478bd9Sstevel@tonic-gate 	} else
11357c478bd9Sstevel@tonic-gate 		err_fmt = "bad file type for \"%s\"\n"
11367c478bd9Sstevel@tonic-gate 		    "statefile must be a regular file or block device\n";
11377c478bd9Sstevel@tonic-gate 	if (err_fmt) {
11387c478bd9Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
11397c478bd9Sstevel@tonic-gate 		return (NOUP);
11407c478bd9Sstevel@tonic-gate 	}
1141*e7cbe64fSgw25295 	if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)))
11427c478bd9Sstevel@tonic-gate 		return (NOUP);
1143*e7cbe64fSgw25295 	if (new_cc.cf_type == CFT_ZVOL) {
1144*e7cbe64fSgw25295 		if (utop(diskname, new_cc.cf_dev_prom))
1145*e7cbe64fSgw25295 			return (NOUP);
1146*e7cbe64fSgw25295 	} else if (utop(new_cc.cf_devfs, new_cc.cf_dev_prom)) {
1147*e7cbe64fSgw25295 		return (NOUP);
1148*e7cbe64fSgw25295 	}
11497c478bd9Sstevel@tonic-gate 	new_cc.cf_magic = CPR_CONFIG_MAGIC;
11507c478bd9Sstevel@tonic-gate 	statefile = 1;
11517c478bd9Sstevel@tonic-gate 	return (OKUP);
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate #endif /* sparc */
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate /*
1157c42872d4Smh27603  * Common function to set a system or cpu threshold.
11587c478bd9Sstevel@tonic-gate  */
1159c42872d4Smh27603 static int
1160c42872d4Smh27603 cmnthr(int req)
11617c478bd9Sstevel@tonic-gate {
11627c478bd9Sstevel@tonic-gate 	int value, nerr = 0, upval = OKUP;
11637c478bd9Sstevel@tonic-gate 	char *thresh = LINEARG(1);
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	if (strcmp(thresh, always_on) == 0)
11667c478bd9Sstevel@tonic-gate 		value = INT_MAX;
11677c478bd9Sstevel@tonic-gate 	else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) {
11687c478bd9Sstevel@tonic-gate 		mesg(MERR, "%s must be a positive value\n", LINEARG(0));
11697c478bd9Sstevel@tonic-gate 		upval = NOUP;
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 	if (upval == OKUP)
1172c42872d4Smh27603 		(void) ioctl(pm_fd, req, value);
11737c478bd9Sstevel@tonic-gate 	return (upval);
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 
1177c42872d4Smh27603 /*
1178c42872d4Smh27603  * Try setting system threshold.
1179c42872d4Smh27603  */
1180c42872d4Smh27603 int
1181c42872d4Smh27603 systhr(void)
1182c42872d4Smh27603 {
1183c42872d4Smh27603 	return (cmnthr(PM_SET_SYSTEM_THRESHOLD));
1184c42872d4Smh27603 }
1185c42872d4Smh27603 
1186c42872d4Smh27603 
1187c42872d4Smh27603 /*
1188c42872d4Smh27603  * Try setting cpu threshold.
1189c42872d4Smh27603  */
1190c42872d4Smh27603 int
1191c42872d4Smh27603 cputhr(void)
1192c42872d4Smh27603 {
1193c42872d4Smh27603 	return (cmnthr(PM_SET_CPU_THRESHOLD));
1194c42872d4Smh27603 }
1195c42872d4Smh27603 
1196c42872d4Smh27603 
11977c478bd9Sstevel@tonic-gate int
11987c478bd9Sstevel@tonic-gate tchars(void)
11997c478bd9Sstevel@tonic-gate {
12007c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.ttychars_thold));
12017c478bd9Sstevel@tonic-gate }
1202