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