xref: /titanic_50/usr/src/cmd/power/handlers.c (revision c42872d4489d6f0fbccfabe2a62f3c976ee1e5d6)
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
5*c42872d4Smh27603  * Common Development and Distribution License (the "License").
6*c42872d4Smh27603  * 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 /*
22*c42872d4Smh27603  * Copyright 2006 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>
337c478bd9Sstevel@tonic-gate #include <syslog.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define	STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str)
387c478bd9Sstevel@tonic-gate #define	LASTBYTE(str) (str + strlen(str) - 1)
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static char nerr_fmt[] = "number is out of range (%s)\n";
417c478bd9Sstevel@tonic-gate static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n";
427c478bd9Sstevel@tonic-gate static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n";
437c478bd9Sstevel@tonic-gate static char bad_thresh_fmt[] = "bad threshold(s)\n";
447c478bd9Sstevel@tonic-gate static char stat_fmt[] = "cannot stat \"%s\", %s\n";
457c478bd9Sstevel@tonic-gate static char always_on[] = "always-on";
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * When lines in a config file (usually "/etc/power.conf") start with
497c478bd9Sstevel@tonic-gate  * a recognized keyword, a "handler" routine is called for specific
507c478bd9Sstevel@tonic-gate  * CPR or PM -related action(s).  Each routine returns a status code
517c478bd9Sstevel@tonic-gate  * indicating whether all tasks were successful; if any errors occured,
527c478bd9Sstevel@tonic-gate  * future CPR or PM updates are skipped.  Following are the handler
537c478bd9Sstevel@tonic-gate  * routines for all keywords:
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
58*c42872d4Smh27603  * Check for valid cpupm behavior and communicate it to the kernel.
59*c42872d4Smh27603  */
60*c42872d4Smh27603 int
61*c42872d4Smh27603 cpupm(void)
62*c42872d4Smh27603 {
63*c42872d4Smh27603 	struct btoc {
64*c42872d4Smh27603 		char *behavior;
65*c42872d4Smh27603 		int cmd;
66*c42872d4Smh27603 		int Errno;
67*c42872d4Smh27603 	};
68*c42872d4Smh27603 	static struct btoc blist[] = {
69*c42872d4Smh27603 		"disable",	PM_STOP_CPUPM, EINVAL,
70*c42872d4Smh27603 		"enable",	PM_START_CPUPM, EBUSY,
71*c42872d4Smh27603 		NULL,		0, 0
72*c42872d4Smh27603 	};
73*c42872d4Smh27603 	struct btoc *bp;
74*c42872d4Smh27603 	char *behavior;
75*c42872d4Smh27603 
76*c42872d4Smh27603 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
77*c42872d4Smh27603 		if (strcmp(behavior, bp->behavior) == 0)
78*c42872d4Smh27603 			break;
79*c42872d4Smh27603 	}
80*c42872d4Smh27603 	if (bp->cmd == 0) {
81*c42872d4Smh27603 		mesg(MERR, "invalid cpupm behavior \"%s\"\n", behavior);
82*c42872d4Smh27603 		return (NOUP);
83*c42872d4Smh27603 	}
84*c42872d4Smh27603 	if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
85*c42872d4Smh27603 		mesg(MERR, "cpupm %s failed, %s\n",
86*c42872d4Smh27603 		    behavior, strerror(errno));
87*c42872d4Smh27603 		return (NOUP);
88*c42872d4Smh27603 	}
89*c42872d4Smh27603 	return (OKUP);
90*c42872d4Smh27603 }
91*c42872d4Smh27603 
92*c42872d4Smh27603 
93*c42872d4Smh27603 /*
947c478bd9Sstevel@tonic-gate  * Check for valid autopm behavior and save after ioctl success.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate int
977c478bd9Sstevel@tonic-gate autopm(void)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	struct btoc {
1007c478bd9Sstevel@tonic-gate 		char *behavior;
1017c478bd9Sstevel@tonic-gate 		int cmd, Errno, isdef;
1027c478bd9Sstevel@tonic-gate 	};
1037c478bd9Sstevel@tonic-gate 	static struct btoc blist[] = {
1047c478bd9Sstevel@tonic-gate 		"default",	PM_START_PM,	EBUSY,	1,
1057c478bd9Sstevel@tonic-gate 		"disable",	PM_STOP_PM,	EINVAL,	0,
1067c478bd9Sstevel@tonic-gate 		"enable",	PM_START_PM,	EBUSY,	0,
1077c478bd9Sstevel@tonic-gate 		NULL,		0,		0,	0,
1087c478bd9Sstevel@tonic-gate 	};
1097c478bd9Sstevel@tonic-gate 	struct btoc *bp;
1107c478bd9Sstevel@tonic-gate 	char *behavior;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
1137c478bd9Sstevel@tonic-gate 		if (strcmp(behavior, bp->behavior) == 0)
1147c478bd9Sstevel@tonic-gate 			break;
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 	if (bp->cmd == 0) {
1177c478bd9Sstevel@tonic-gate 		mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior);
1187c478bd9Sstevel@tonic-gate 		return (NOUP);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * for "default" behavior, do not enable autopm if not ESTAR_V3
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	if (!bp->isdef || (estar_vers == ESTAR_V3)) {
1257c478bd9Sstevel@tonic-gate 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
1267c478bd9Sstevel@tonic-gate 			mesg(MERR, "autopm %s failed, %s\n",
1277c478bd9Sstevel@tonic-gate 			    behavior, strerror(errno));
1287c478bd9Sstevel@tonic-gate 			return (NOUP);
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	(void) strcpy(new_cc.apm_behavior, behavior);
1327c478bd9Sstevel@tonic-gate 	return (OKUP);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static int
1377c478bd9Sstevel@tonic-gate gethm(char *src, int *hour, int *min)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	if (sscanf(src, "%d:%d", hour, min) != 2) {
1407c478bd9Sstevel@tonic-gate 		mesg(MERR, "bad time format (%s)\n", src);
1417c478bd9Sstevel@tonic-gate 		return (-1);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	return (0);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static void
1487c478bd9Sstevel@tonic-gate strcpy_limit(char *dst, char *src, size_t limit, char *info)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	if (strlcpy(dst, src, limit) >= limit)
1517c478bd9Sstevel@tonic-gate 		mesg(MEXIT, "%s is too long (%s)\n", info, src);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Convert autoshutdown idle and start/finish times;
1577c478bd9Sstevel@tonic-gate  * check and record autoshutdown behavior.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate int
1607c478bd9Sstevel@tonic-gate autosd(void)
1617c478bd9Sstevel@tonic-gate {
1621dc065c6Smh27603 	char **bp, *behavior;
1631dc065c6Smh27603 	char *unrec = gettext("unrecognized autoshutdown behavior");
1647c478bd9Sstevel@tonic-gate 	static char *blist[] = {
1657c478bd9Sstevel@tonic-gate 		"autowakeup", "default", "noshutdown",
1667c478bd9Sstevel@tonic-gate 		"shutdown", "unconfigured", NULL
1677c478bd9Sstevel@tonic-gate 	};
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	new_cc.as_idle = atoi(LINEARG(1));
1707c478bd9Sstevel@tonic-gate 	if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) ||
1717c478bd9Sstevel@tonic-gate 	    gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm))
1727c478bd9Sstevel@tonic-gate 		return (NOUP);
1731dc065c6Smh27603 	mesg(MDEBUG, "idle %d, start %d:%02d, finish %d:%02d\n",
1747c478bd9Sstevel@tonic-gate 	    new_cc.as_idle, new_cc.as_sh, new_cc.as_sm,
1757c478bd9Sstevel@tonic-gate 	    new_cc.as_fh, new_cc.as_fm);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	for (behavior = LINEARG(4), bp = blist; *bp; bp++) {
1787c478bd9Sstevel@tonic-gate 		if (strcmp(behavior, *bp) == 0)
1797c478bd9Sstevel@tonic-gate 			break;
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	if (*bp == NULL) {
1827c478bd9Sstevel@tonic-gate 		mesg(MERR, "%s: \"%s\"\n", unrec, behavior);
1837c478bd9Sstevel@tonic-gate 		return (NOUP);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 	STRCPYLIM(new_cc.as_behavior, *bp, unrec);
1867c478bd9Sstevel@tonic-gate 	return (OKUP);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * Check for a real device and try to resolve to a full path.
1927c478bd9Sstevel@tonic-gate  * The orig/resolved path may be modified into a prom pathname,
1937c478bd9Sstevel@tonic-gate  * and an allocated copy of the result is stored at *destp;
1947c478bd9Sstevel@tonic-gate  * the caller will need to free that space.  Returns 1 for any
1957c478bd9Sstevel@tonic-gate  * error, otherwise 0; also sets *errp after an alloc error.
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate static int
1987c478bd9Sstevel@tonic-gate devpath(char **destp, char *src, int *errp)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	struct stat stbuf;
2017c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX];
2027c478bd9Sstevel@tonic-gate 	char *cp, *dstr;
2037c478bd9Sstevel@tonic-gate 	int devok, dcs = 0;
2047c478bd9Sstevel@tonic-gate 	size_t len;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * When there's a real device, try to resolve the path
2087c478bd9Sstevel@tonic-gate 	 * and trim the leading "/devices" component.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) {
2117c478bd9Sstevel@tonic-gate 		if (realpath(src, buf) == NULL) {
2127c478bd9Sstevel@tonic-gate 			mesg(MERR, "realpath cannot resolve \"%s\"\n",
2137c478bd9Sstevel@tonic-gate 			    src, strerror(errno));
2147c478bd9Sstevel@tonic-gate 			return (1);
2157c478bd9Sstevel@tonic-gate 		}
2167c478bd9Sstevel@tonic-gate 		src = buf;
2177c478bd9Sstevel@tonic-gate 		dstr = "/devices";
2187c478bd9Sstevel@tonic-gate 		len = strlen(dstr);
2197c478bd9Sstevel@tonic-gate 		dcs = (strncmp(src, dstr, len) == 0);
2207c478bd9Sstevel@tonic-gate 		if (dcs)
2217c478bd9Sstevel@tonic-gate 			src += len;
2227c478bd9Sstevel@tonic-gate 	} else
2237c478bd9Sstevel@tonic-gate 		mesg(MDEBUG, stat_fmt, src, strerror(errno));
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/*
2267c478bd9Sstevel@tonic-gate 	 * When the path has ":anything", display an error for
2277c478bd9Sstevel@tonic-gate 	 * a non-device or truncate a resolved+modifed path.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	if (cp = strchr(src, ':')) {
2307c478bd9Sstevel@tonic-gate 		if (devok == 0) {
2317c478bd9Sstevel@tonic-gate 			mesg(MERR, "physical path may not contain "
2327c478bd9Sstevel@tonic-gate 			    "a minor string (%s)\n", src);
2337c478bd9Sstevel@tonic-gate 			return (1);
2347c478bd9Sstevel@tonic-gate 		} else if (dcs)
2357c478bd9Sstevel@tonic-gate 			*cp = '\0';
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if ((*destp = strdup(src)) == NULL) {
2397c478bd9Sstevel@tonic-gate 		*errp = NOUP;
2407c478bd9Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, src, strerror(errno));
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	return (*destp == NULL);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * Call pm ioctl request(s) to set property/device dependencies.
2487c478bd9Sstevel@tonic-gate  */
2497c478bd9Sstevel@tonic-gate static int
2507c478bd9Sstevel@tonic-gate dev_dep_common(int isprop)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	int cmd, argn, upval = OKUP;
2537c478bd9Sstevel@tonic-gate 	char *src, *first, **destp;
2547c478bd9Sstevel@tonic-gate 	pm_req_t pmreq;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
2577c478bd9Sstevel@tonic-gate 	src = LINEARG(1);
2587c478bd9Sstevel@tonic-gate 	if (isprop) {
2597c478bd9Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT_PROPERTY;
2607c478bd9Sstevel@tonic-gate 		first = NULL;
2617c478bd9Sstevel@tonic-gate 		pmreq.pmreq_kept = src;
2627c478bd9Sstevel@tonic-gate 	} else {
2637c478bd9Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT;
2647c478bd9Sstevel@tonic-gate 		if (devpath(&first, src, &upval))
2657c478bd9Sstevel@tonic-gate 			return (upval);
2667c478bd9Sstevel@tonic-gate 		pmreq.pmreq_kept = first;
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 	destp = &pmreq.pmreq_keeper;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * Now loop through any dependents.
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
2747c478bd9Sstevel@tonic-gate 		if (devpath(destp, src, &upval)) {
2757c478bd9Sstevel@tonic-gate 			if (upval != OKUP)
2767c478bd9Sstevel@tonic-gate 				return (upval);
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 		if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) {
2807c478bd9Sstevel@tonic-gate 			mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n"
2817c478bd9Sstevel@tonic-gate 			    "kept \"%s\", keeper \"%s\"\n",
2827c478bd9Sstevel@tonic-gate 			    cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper);
2837c478bd9Sstevel@tonic-gate 			mesg(MERR, "cannot set \"%s\" dependency "
2847c478bd9Sstevel@tonic-gate 			    "for \"%s\", %s\n", pmreq.pmreq_keeper,
2857c478bd9Sstevel@tonic-gate 			    pmreq.pmreq_kept, strerror(errno));
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 		free(*destp);
2887c478bd9Sstevel@tonic-gate 		*destp = NULL;
2897c478bd9Sstevel@tonic-gate 		if (upval != OKUP)
2907c478bd9Sstevel@tonic-gate 			break;
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	free(first);
2947c478bd9Sstevel@tonic-gate 	return (upval);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate int
2997c478bd9Sstevel@tonic-gate ddprop(void)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	return (dev_dep_common(1));
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate int
3067c478bd9Sstevel@tonic-gate devdep(void)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	return (dev_dep_common(0));
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * Convert a numeric string (with a possible trailing scaling byte)
3147c478bd9Sstevel@tonic-gate  * into an integer.  Returns a converted value and *nerrp unchanged,
3157c478bd9Sstevel@tonic-gate  * or 0 with *nerrp set to 1 for a conversion error.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate static int
3187c478bd9Sstevel@tonic-gate get_scaled_value(char *str, int *nerrp)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	longlong_t svalue = 0, factor = 1;
3217c478bd9Sstevel@tonic-gate 	char *sp;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	errno = 0;
3247c478bd9Sstevel@tonic-gate 	svalue = strtol(str, &sp, 0);
3257c478bd9Sstevel@tonic-gate 	if (errno || (*str != '-' && (*str < '0' || *str > '9')))
3267c478bd9Sstevel@tonic-gate 		*nerrp = 1;
3277c478bd9Sstevel@tonic-gate 	else if (sp && *sp != '\0') {
3287c478bd9Sstevel@tonic-gate 		if (*sp == 'h')
3297c478bd9Sstevel@tonic-gate 			factor = 3600;
3307c478bd9Sstevel@tonic-gate 		else if (*sp == 'm')
3317c478bd9Sstevel@tonic-gate 			factor = 60;
3327c478bd9Sstevel@tonic-gate 		else if (*sp != 's')
3337c478bd9Sstevel@tonic-gate 			*nerrp = 1;
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	/* any bytes following sp are ignored */
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (*nerrp == 0) {
3387c478bd9Sstevel@tonic-gate 		svalue *= factor;
3397c478bd9Sstevel@tonic-gate 		if (svalue < INT_MIN || svalue > INT_MAX)
3407c478bd9Sstevel@tonic-gate 			*nerrp = 1;
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 	if (*nerrp)
3437c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, str);
3447c478bd9Sstevel@tonic-gate 	mesg(MDEBUG, "got scaled value %d\n", (int)svalue);
3457c478bd9Sstevel@tonic-gate 	return ((int)svalue);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * Increment the count of threshold values,
3517c478bd9Sstevel@tonic-gate  * reallocate *vlistp and append another element.
3527c478bd9Sstevel@tonic-gate  * Returns 1 on error, otherwise 0.
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate static int
3557c478bd9Sstevel@tonic-gate vlist_append(int **vlistp, int *vcntp, int value)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	(*vcntp)++;
3587c478bd9Sstevel@tonic-gate 	if (*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp)))
3597c478bd9Sstevel@tonic-gate 		*(*vlistp + *vcntp - 1) = value;
3607c478bd9Sstevel@tonic-gate 	else
3617c478bd9Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, "threshold list", strerror(errno));
3627c478bd9Sstevel@tonic-gate 	return (*vlistp == NULL);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate  * Convert a single threshold string or paren groups of thresh's as
3687c478bd9Sstevel@tonic-gate  * described below.  All thresh's are saved to an allocated list at
3697c478bd9Sstevel@tonic-gate  * *vlistp; the caller will need to free that space.  On return:
3707c478bd9Sstevel@tonic-gate  * *vcntp is the count of the vlist array, and vlist is either
3717c478bd9Sstevel@tonic-gate  * a single thresh or N groups of thresh's with a trailing zero:
3727c478bd9Sstevel@tonic-gate  * (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0.
3737c478bd9Sstevel@tonic-gate  * Returns 0 when all conversions were OK, and 1 for any syntax,
3747c478bd9Sstevel@tonic-gate  * conversion, or alloc error.
3757c478bd9Sstevel@tonic-gate  */
3767c478bd9Sstevel@tonic-gate static int
3777c478bd9Sstevel@tonic-gate get_thresh(int **vlistp, int *vcntp)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	int argn, value, gci, grp_cnt = 0, paren = 0, nerr = 0;
3807c478bd9Sstevel@tonic-gate 	char *rp, *src;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
3837c478bd9Sstevel@tonic-gate 		if (*src == LPAREN) {
3847c478bd9Sstevel@tonic-gate 			gci = *vcntp;
3857c478bd9Sstevel@tonic-gate 			if (nerr = vlist_append(vlistp, vcntp, 0))
3867c478bd9Sstevel@tonic-gate 				break;
3877c478bd9Sstevel@tonic-gate 			paren = 1;
3887c478bd9Sstevel@tonic-gate 			src++;
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 		if (*(rp = LASTBYTE(src)) == RPAREN) {
3917c478bd9Sstevel@tonic-gate 			if (paren) {
3927c478bd9Sstevel@tonic-gate 				grp_cnt = *vcntp - gci;
3937c478bd9Sstevel@tonic-gate 				*(*vlistp + gci) = grp_cnt;
3947c478bd9Sstevel@tonic-gate 				paren = 0;
3957c478bd9Sstevel@tonic-gate 				*rp = '\0';
3967c478bd9Sstevel@tonic-gate 			} else {
3977c478bd9Sstevel@tonic-gate 				nerr = 1;
3987c478bd9Sstevel@tonic-gate 				break;
3997c478bd9Sstevel@tonic-gate 			}
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		value = get_scaled_value(src, &nerr);
4037c478bd9Sstevel@tonic-gate 		if (nerr || (nerr = vlist_append(vlistp, vcntp, value)))
4047c478bd9Sstevel@tonic-gate 			break;
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (nerr == 0 && grp_cnt)
4087c478bd9Sstevel@tonic-gate 		nerr = vlist_append(vlistp, vcntp, 0);
4097c478bd9Sstevel@tonic-gate 	return (nerr);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate /*
4147c478bd9Sstevel@tonic-gate  * Set device thresholds from (3) formats:
4157c478bd9Sstevel@tonic-gate  * 	path	"always-on"
4167c478bd9Sstevel@tonic-gate  * 	path	time-spec: [0-9]+[{h,m,s}]
4177c478bd9Sstevel@tonic-gate  *	path	(ts1 ts2 ...)+
4187c478bd9Sstevel@tonic-gate  */
4197c478bd9Sstevel@tonic-gate int
4207c478bd9Sstevel@tonic-gate devthr(void)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	int cmd, upval = OKUP, nthresh = 0, *vlist = NULL;
4237c478bd9Sstevel@tonic-gate 	pm_req_t pmreq;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
4267c478bd9Sstevel@tonic-gate 	if (devpath(&pmreq.physpath, LINEARG(1), &upval))
4277c478bd9Sstevel@tonic-gate 		return (upval);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if (strcmp(LINEARG(2), always_on) == 0) {
4307c478bd9Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
4317c478bd9Sstevel@tonic-gate 		pmreq.value = INT_MAX;
4327c478bd9Sstevel@tonic-gate 	} else if (get_thresh(&vlist, &nthresh)) {
4337c478bd9Sstevel@tonic-gate 		mesg(MERR, bad_thresh_fmt);
4347c478bd9Sstevel@tonic-gate 		upval = NOUP;
4357c478bd9Sstevel@tonic-gate 	} else if (nthresh == 1) {
4367c478bd9Sstevel@tonic-gate 		pmreq.value = *vlist;
4377c478bd9Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
4387c478bd9Sstevel@tonic-gate 	} else {
4397c478bd9Sstevel@tonic-gate 		pmreq.data = vlist;
4407c478bd9Sstevel@tonic-gate 		pmreq.datasize = (nthresh * sizeof (*vlist));
4417c478bd9Sstevel@tonic-gate 		cmd = PM_SET_COMPONENT_THRESHOLDS;
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1)
4457c478bd9Sstevel@tonic-gate 		mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno));
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	free(vlist);
4487c478bd9Sstevel@tonic-gate 	free(pmreq.physpath);
4497c478bd9Sstevel@tonic-gate 	return (upval);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate static int
4547c478bd9Sstevel@tonic-gate scan_int(char *src, int *dst)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	long lval;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	errno = 0;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	lval = strtol(LINEARG(1), NULL, 0);
4617c478bd9Sstevel@tonic-gate 	if (errno || lval > INT_MAX || lval < 0) {
4627c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
4637c478bd9Sstevel@tonic-gate 		return (NOUP);
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	*dst = (int)lval;
4677c478bd9Sstevel@tonic-gate 	return (OKUP);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate static int
4717c478bd9Sstevel@tonic-gate scan_float(char *src, float *dst)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	float fval;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	errno = 0;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	fval = strtof(src, NULL);
4787c478bd9Sstevel@tonic-gate 	if (errno || fval < 0.0) {
4797c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
4807c478bd9Sstevel@tonic-gate 		return (NOUP);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	*dst = fval;
4847c478bd9Sstevel@tonic-gate 	return (OKUP);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate int
4897c478bd9Sstevel@tonic-gate dreads(void)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.diskreads_thold));
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * Set pathname for idlecheck;
4977c478bd9Sstevel@tonic-gate  * an overflowed pathname is treated as a fatal error.
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate int
5007c478bd9Sstevel@tonic-gate idlechk(void)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path");
5037c478bd9Sstevel@tonic-gate 	return (OKUP);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate int
5087c478bd9Sstevel@tonic-gate loadavg(void)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	return (scan_float(LINEARG(1), &new_cc.loadaverage_thold));
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate int
5157c478bd9Sstevel@tonic-gate nfsreq(void)
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold));
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate #ifdef sparc
5227c478bd9Sstevel@tonic-gate static char open_fmt[] = "cannot open \"%s\", %s\n";
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate  * Verify the filesystem type for a regular statefile is "ufs"
5267c478bd9Sstevel@tonic-gate  * or verify a block device is not in use as a mounted filesytem.
5277c478bd9Sstevel@tonic-gate  * Returns 1 if any error, otherwise 0.
5287c478bd9Sstevel@tonic-gate  */
5297c478bd9Sstevel@tonic-gate static int
5307c478bd9Sstevel@tonic-gate check_mount(char *sfile, dev_t sfdev, int ufs)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	char *src, *err_fmt = NULL, *mnttab = MNTTAB;
5337c478bd9Sstevel@tonic-gate 	int rgent, match = 0;
5347c478bd9Sstevel@tonic-gate 	struct extmnttab ent;
5357c478bd9Sstevel@tonic-gate 	FILE *fp;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
5387c478bd9Sstevel@tonic-gate 		mesg(MERR, open_fmt, mnttab, strerror(errno));
5397c478bd9Sstevel@tonic-gate 		return (1);
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * Search for a matching dev_t;
5447c478bd9Sstevel@tonic-gate 	 * ignore non-ufs filesystems for a regular statefile.
5457c478bd9Sstevel@tonic-gate 	 */
5467c478bd9Sstevel@tonic-gate 	while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) {
5477c478bd9Sstevel@tonic-gate 		if (rgent > 0) {
5487c478bd9Sstevel@tonic-gate 			mesg(MERR, "error reading \"%s\"\n", mnttab);
5497c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
5507c478bd9Sstevel@tonic-gate 			return (1);
5517c478bd9Sstevel@tonic-gate 		} else if (ufs && strcmp(ent.mnt_fstype, "ufs"))
5527c478bd9Sstevel@tonic-gate 			continue;
5537c478bd9Sstevel@tonic-gate 		else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) {
5547c478bd9Sstevel@tonic-gate 			match = 1;
5557c478bd9Sstevel@tonic-gate 			break;
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	/*
5617c478bd9Sstevel@tonic-gate 	 * No match is needed for a block device statefile,
5627c478bd9Sstevel@tonic-gate 	 * a match is needed for a regular statefile.
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	if (match == 0) {
5657c478bd9Sstevel@tonic-gate 		if (new_cc.cf_type == CFT_SPEC)
5667c478bd9Sstevel@tonic-gate 			STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile");
5677c478bd9Sstevel@tonic-gate 		else
5687c478bd9Sstevel@tonic-gate 			err_fmt = "cannot find ufs mount point for \"%s\"\n";
5697c478bd9Sstevel@tonic-gate 	} else if (new_cc.cf_type == CFT_UFS) {
5707c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry");
5717c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special");
5727c478bd9Sstevel@tonic-gate 		while (*(sfile + 1) == '/') sfile++;
5737c478bd9Sstevel@tonic-gate 		src = sfile + strlen(ent.mnt_mountp);
5747c478bd9Sstevel@tonic-gate 		while (*src == '/') src++;
5757c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_path, src, "statefile path");
5767c478bd9Sstevel@tonic-gate 	} else
5777c478bd9Sstevel@tonic-gate 		err_fmt = "statefile device \"%s\" is a mounted filesystem\n";
5787c478bd9Sstevel@tonic-gate 	if (err_fmt)
5797c478bd9Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
5807c478bd9Sstevel@tonic-gate 	return (err_fmt != NULL);
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate  * Convert a Unix device to a prom device and save on success,
5867c478bd9Sstevel@tonic-gate  * log any ioctl/conversion error.
5877c478bd9Sstevel@tonic-gate  */
5887c478bd9Sstevel@tonic-gate static int
5897c478bd9Sstevel@tonic-gate utop(void)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate 	union obpbuf {
5927c478bd9Sstevel@tonic-gate 		char	buf[OBP_MAXPATHLEN + sizeof (uint_t)];
5937c478bd9Sstevel@tonic-gate 		struct	openpromio oppio;
5947c478bd9Sstevel@tonic-gate 	};
5957c478bd9Sstevel@tonic-gate 	union obpbuf oppbuf;
5967c478bd9Sstevel@tonic-gate 	struct openpromio *opp;
5977c478bd9Sstevel@tonic-gate 	char *promdev = "/dev/openprom";
5987c478bd9Sstevel@tonic-gate 	int fd, upval;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if ((fd = open(promdev, O_RDONLY)) == -1) {
6017c478bd9Sstevel@tonic-gate 		mesg(MERR, open_fmt, promdev, strerror(errno));
6027c478bd9Sstevel@tonic-gate 		return (NOUP);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	opp = &oppbuf.oppio;
6067c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPATHLEN;
6077c478bd9Sstevel@tonic-gate 	strcpy_limit(opp->oprom_array, new_cc.cf_devfs,
6087c478bd9Sstevel@tonic-gate 	    OBP_MAXPATHLEN, "statefile device");
6097c478bd9Sstevel@tonic-gate 	upval = ioctl(fd, OPROMDEV2PROMNAME, opp);
6107c478bd9Sstevel@tonic-gate 	(void) close(fd);
6117c478bd9Sstevel@tonic-gate 	if (upval == OKUP)
6127c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_dev_prom, opp->oprom_array, "prom device");
6137c478bd9Sstevel@tonic-gate 	else {
6147c478bd9Sstevel@tonic-gate 		openlog("pmconfig", 0, LOG_DAEMON);
6157c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE,
6167c478bd9Sstevel@tonic-gate 		    gettext("cannot convert \"%s\" to prom device"),
6177c478bd9Sstevel@tonic-gate 		    new_cc.cf_devfs);
6187c478bd9Sstevel@tonic-gate 		closelog();
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	return (upval);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate  * Check for a valid statefile pathname, inode and mount status.
6277c478bd9Sstevel@tonic-gate  */
6287c478bd9Sstevel@tonic-gate int
6297c478bd9Sstevel@tonic-gate sfpath(void)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	static int statefile;
6327c478bd9Sstevel@tonic-gate 	char *err_fmt = NULL;
6337c478bd9Sstevel@tonic-gate 	char *sfile, *sp, ch;
6347c478bd9Sstevel@tonic-gate 	struct stat stbuf;
6357c478bd9Sstevel@tonic-gate 	int dir = 0;
6367c478bd9Sstevel@tonic-gate 	dev_t dev;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	if (statefile) {
6397c478bd9Sstevel@tonic-gate 		mesg(MERR, "ignored redundant statefile entry\n");
6407c478bd9Sstevel@tonic-gate 		return (OKUP);
6417c478bd9Sstevel@tonic-gate 	} else if (ua_err) {
6427c478bd9Sstevel@tonic-gate 		if (ua_err != ENOTSUP)
6437c478bd9Sstevel@tonic-gate 			mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n",
6447c478bd9Sstevel@tonic-gate 			    strerror(ua_err));
6457c478bd9Sstevel@tonic-gate 		return (NOUP);
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * Check for an absolute path and trim any trailing '/'.
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate 	sfile = LINEARG(1);
6527c478bd9Sstevel@tonic-gate 	if (*sfile != '/') {
6537c478bd9Sstevel@tonic-gate 		mesg(MERR, "statefile requires an absolute path\n");
6547c478bd9Sstevel@tonic-gate 		return (NOUP);
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 	for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--)
6577c478bd9Sstevel@tonic-gate 		*sp = '\0';
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	/*
6607c478bd9Sstevel@tonic-gate 	 * If the statefile doesn't exist, the leading path must be a dir.
6617c478bd9Sstevel@tonic-gate 	 */
6627c478bd9Sstevel@tonic-gate 	if (stat(sfile, &stbuf) == -1) {
6637c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
6647c478bd9Sstevel@tonic-gate 			dir = 1;
6657c478bd9Sstevel@tonic-gate 			if ((sp = strrchr(sfile, '/')) == sfile)
6667c478bd9Sstevel@tonic-gate 				sp++;
6677c478bd9Sstevel@tonic-gate 			ch = *sp;
6687c478bd9Sstevel@tonic-gate 			*sp = '\0';
6697c478bd9Sstevel@tonic-gate 			if (stat(sfile, &stbuf) == -1)
6707c478bd9Sstevel@tonic-gate 				err_fmt = stat_fmt;
6717c478bd9Sstevel@tonic-gate 			*sp = ch;
6727c478bd9Sstevel@tonic-gate 		} else
6737c478bd9Sstevel@tonic-gate 			err_fmt = stat_fmt;
6747c478bd9Sstevel@tonic-gate 		if (err_fmt) {
6757c478bd9Sstevel@tonic-gate 			mesg(MERR, err_fmt, sfile, strerror(errno));
6767c478bd9Sstevel@tonic-gate 			return (NOUP);
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/*
6817c478bd9Sstevel@tonic-gate 	 * Check for regular/dir/block types, set cf_type and dev.
6827c478bd9Sstevel@tonic-gate 	 */
6837c478bd9Sstevel@tonic-gate 	if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) {
6847c478bd9Sstevel@tonic-gate 		new_cc.cf_type = CFT_UFS;
6857c478bd9Sstevel@tonic-gate 		dev = stbuf.st_dev;
6867c478bd9Sstevel@tonic-gate 	} else if (S_ISBLK(stbuf.st_mode)) {
6877c478bd9Sstevel@tonic-gate 		if (minor(stbuf.st_rdev) != 2) {
6887c478bd9Sstevel@tonic-gate 			new_cc.cf_type = CFT_SPEC;
6897c478bd9Sstevel@tonic-gate 			dev = stbuf.st_rdev;
6907c478bd9Sstevel@tonic-gate 		} else
6917c478bd9Sstevel@tonic-gate 			err_fmt = "statefile device cannot be slice 2 (%s)\n"
6927c478bd9Sstevel@tonic-gate 			    "would clobber the disk label and boot-block\n";
6937c478bd9Sstevel@tonic-gate 	} else
6947c478bd9Sstevel@tonic-gate 		err_fmt = "bad file type for \"%s\"\n"
6957c478bd9Sstevel@tonic-gate 		    "statefile must be a regular file or block device\n";
6967c478bd9Sstevel@tonic-gate 	if (err_fmt) {
6977c478bd9Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
6987c478bd9Sstevel@tonic-gate 		return (NOUP);
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)) || utop())
7027c478bd9Sstevel@tonic-gate 		return (NOUP);
7037c478bd9Sstevel@tonic-gate 	new_cc.cf_magic = CPR_CONFIG_MAGIC;
7047c478bd9Sstevel@tonic-gate 	statefile = 1;
7057c478bd9Sstevel@tonic-gate 	return (OKUP);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate #endif /* sparc */
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /*
711*c42872d4Smh27603  * Common function to set a system or cpu threshold.
7127c478bd9Sstevel@tonic-gate  */
713*c42872d4Smh27603 static int
714*c42872d4Smh27603 cmnthr(int req)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	int value, nerr = 0, upval = OKUP;
7177c478bd9Sstevel@tonic-gate 	char *thresh = LINEARG(1);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	if (strcmp(thresh, always_on) == 0)
7207c478bd9Sstevel@tonic-gate 		value = INT_MAX;
7217c478bd9Sstevel@tonic-gate 	else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) {
7227c478bd9Sstevel@tonic-gate 		mesg(MERR, "%s must be a positive value\n", LINEARG(0));
7237c478bd9Sstevel@tonic-gate 		upval = NOUP;
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	if (upval == OKUP)
726*c42872d4Smh27603 		(void) ioctl(pm_fd, req, value);
7277c478bd9Sstevel@tonic-gate 	return (upval);
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 
731*c42872d4Smh27603 /*
732*c42872d4Smh27603  * Try setting system threshold.
733*c42872d4Smh27603  */
734*c42872d4Smh27603 int
735*c42872d4Smh27603 systhr(void)
736*c42872d4Smh27603 {
737*c42872d4Smh27603 	return (cmnthr(PM_SET_SYSTEM_THRESHOLD));
738*c42872d4Smh27603 }
739*c42872d4Smh27603 
740*c42872d4Smh27603 
741*c42872d4Smh27603 /*
742*c42872d4Smh27603  * Try setting cpu threshold.
743*c42872d4Smh27603  */
744*c42872d4Smh27603 int
745*c42872d4Smh27603 cputhr(void)
746*c42872d4Smh27603 {
747*c42872d4Smh27603 	return (cmnthr(PM_SET_CPU_THRESHOLD));
748*c42872d4Smh27603 }
749*c42872d4Smh27603 
750*c42872d4Smh27603 
7517c478bd9Sstevel@tonic-gate int
7527c478bd9Sstevel@tonic-gate tchars(void)
7537c478bd9Sstevel@tonic-gate {
7547c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.ttychars_thold));
7557c478bd9Sstevel@tonic-gate }
756