xref: /titanic_51/usr/src/cmd/power/handlers.c (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
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 /*
22*2df1fe9cSrandyf  * Copyright 2007 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>
35*2df1fe9cSrandyf #include <sys/pm.h>
36*2df1fe9cSrandyf #include <kstat.h>
37*2df1fe9cSrandyf #include <sys/smbios.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str)
417c478bd9Sstevel@tonic-gate #define	LASTBYTE(str) (str + strlen(str) - 1)
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static char nerr_fmt[] = "number is out of range (%s)\n";
447c478bd9Sstevel@tonic-gate static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n";
457c478bd9Sstevel@tonic-gate static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n";
467c478bd9Sstevel@tonic-gate static char bad_thresh_fmt[] = "bad threshold(s)\n";
477c478bd9Sstevel@tonic-gate static char stat_fmt[] = "cannot stat \"%s\", %s\n";
487c478bd9Sstevel@tonic-gate static char always_on[] = "always-on";
497c478bd9Sstevel@tonic-gate 
50*2df1fe9cSrandyf #define	PM_DEFAULT_ALGORITHM -1
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * When lines in a config file (usually "/etc/power.conf") start with
537c478bd9Sstevel@tonic-gate  * a recognized keyword, a "handler" routine is called for specific
547c478bd9Sstevel@tonic-gate  * CPR or PM -related action(s).  Each routine returns a status code
557c478bd9Sstevel@tonic-gate  * indicating whether all tasks were successful; if any errors occured,
567c478bd9Sstevel@tonic-gate  * future CPR or PM updates are skipped.  Following are the handler
577c478bd9Sstevel@tonic-gate  * routines for all keywords:
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 
61*2df1fe9cSrandyf static char pm_cmd_string[32];
62*2df1fe9cSrandyf 
63*2df1fe9cSrandyf static char *
64*2df1fe9cSrandyf pm_map(int cmd)
65*2df1fe9cSrandyf {
66*2df1fe9cSrandyf 	pm_req_t req;
67*2df1fe9cSrandyf 
68*2df1fe9cSrandyf 	req.value = cmd;
69*2df1fe9cSrandyf 	req.data = (void *)pm_cmd_string;
70*2df1fe9cSrandyf 	req.datasize = sizeof (pm_cmd_string);
71*2df1fe9cSrandyf 
72*2df1fe9cSrandyf 	if (ioctl(pm_fd, PM_GET_CMD_NAME, &req) < 0) {
73*2df1fe9cSrandyf 		perror("PM cmd name lookup:");
74*2df1fe9cSrandyf 		return ("??");
75*2df1fe9cSrandyf 	}
76*2df1fe9cSrandyf 	return (pm_cmd_string);
77*2df1fe9cSrandyf }
78*2df1fe9cSrandyf 
79*2df1fe9cSrandyf static int
80*2df1fe9cSrandyf isonlist(char *listname, const char *man, const char *prod)
81*2df1fe9cSrandyf {
82*2df1fe9cSrandyf 	pm_searchargs_t sl;
83*2df1fe9cSrandyf 	int ret;
84*2df1fe9cSrandyf 
85*2df1fe9cSrandyf 	sl.pms_listname = listname;
86*2df1fe9cSrandyf 	sl.pms_manufacturer = (char *)man;
87*2df1fe9cSrandyf 	sl.pms_product = (char *)prod;
88*2df1fe9cSrandyf 	ret = ioctl(pm_fd, PM_SEARCH_LIST, &sl);
89*2df1fe9cSrandyf 	mesg(MDEBUG, "PM_SEARCH_LIST %s for %s,%s returns %d\n",
90*2df1fe9cSrandyf 	    listname, man, prod, ret);
91*2df1fe9cSrandyf 	return (ret == 0);
92*2df1fe9cSrandyf }
93*2df1fe9cSrandyf 
94*2df1fe9cSrandyf static int
95*2df1fe9cSrandyf do_ioctl(int ioctl_cmd, char *keyword, char *behavior, int suppress)
96*2df1fe9cSrandyf {
97*2df1fe9cSrandyf 	mesg(MDEBUG, "doing ioctl %s for %s ", pm_map(ioctl_cmd), keyword);
98*2df1fe9cSrandyf 	if (ioctl(pm_fd, ioctl_cmd, NULL) == -1) {
99*2df1fe9cSrandyf 		int suppressed = suppress == -1 || suppress == errno;
100*2df1fe9cSrandyf 		mesg(MDEBUG, "%s failed, %s (%ssuppressed)\n", behavior,
101*2df1fe9cSrandyf 		    strerror(errno), (suppressed ? "" : "not "));
102*2df1fe9cSrandyf 		if (!suppressed) {
103*2df1fe9cSrandyf 			mesg(MERR, "%s %s failed, %s\n", keyword, behavior,
104*2df1fe9cSrandyf 			    strerror(errno));
105*2df1fe9cSrandyf 			return (NOUP);
106*2df1fe9cSrandyf 		} else {
107*2df1fe9cSrandyf 			return (OKUP);
108*2df1fe9cSrandyf 		}
109*2df1fe9cSrandyf 	}
110*2df1fe9cSrandyf 	mesg(MDEBUG, "succeeded\n");
111*2df1fe9cSrandyf 	return (OKUP);
112*2df1fe9cSrandyf }
113*2df1fe9cSrandyf 
1147c478bd9Sstevel@tonic-gate /*
115c42872d4Smh27603  * Check for valid cpupm behavior and communicate it to the kernel.
116c42872d4Smh27603  */
117c42872d4Smh27603 int
118c42872d4Smh27603 cpupm(void)
119c42872d4Smh27603 {
120c42872d4Smh27603 	struct btoc {
121c42872d4Smh27603 		char *behavior;
122c42872d4Smh27603 		int cmd;
123c42872d4Smh27603 		int Errno;
124c42872d4Smh27603 	};
125c42872d4Smh27603 	static struct btoc blist[] = {
126c42872d4Smh27603 		"disable",	PM_STOP_CPUPM, EINVAL,
127c42872d4Smh27603 		"enable",	PM_START_CPUPM, EBUSY,
128c42872d4Smh27603 		NULL,		0, 0
129c42872d4Smh27603 	};
130c42872d4Smh27603 	struct btoc *bp;
131c42872d4Smh27603 	char *behavior;
132c42872d4Smh27603 
133c42872d4Smh27603 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
134c42872d4Smh27603 		if (strcmp(behavior, bp->behavior) == 0)
135c42872d4Smh27603 			break;
136c42872d4Smh27603 	}
137c42872d4Smh27603 	if (bp->cmd == 0) {
138c42872d4Smh27603 		mesg(MERR, "invalid cpupm behavior \"%s\"\n", behavior);
139c42872d4Smh27603 		return (NOUP);
140c42872d4Smh27603 	}
141c42872d4Smh27603 	if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
142c42872d4Smh27603 		mesg(MERR, "cpupm %s failed, %s\n",
143c42872d4Smh27603 		    behavior, strerror(errno));
144c42872d4Smh27603 		return (NOUP);
145c42872d4Smh27603 	}
146c42872d4Smh27603 	return (OKUP);
147c42872d4Smh27603 }
148c42872d4Smh27603 
149c42872d4Smh27603 
150c42872d4Smh27603 /*
151*2df1fe9cSrandyf  * Two decisions are identical except for the list names and ioctl commands
152*2df1fe9cSrandyf  * inputs: whitelist, blacklist, yes, no
153*2df1fe9cSrandyf  * if (! ("S3" kstat exists))
154*2df1fe9cSrandyf  *	return (no)
155*2df1fe9cSrandyf  * if (SystemInformation.Manufacturer == "Sun Microsystems" &&
156*2df1fe9cSrandyf  *    (Pref_PM_Profile == Workstation || Pref_PM_Profile == Desktop)) {
157*2df1fe9cSrandyf  *	if (platform on blacklist)
158*2df1fe9cSrandyf  *		return (no)
159*2df1fe9cSrandyf  *	return (yes)
160*2df1fe9cSrandyf  * } else {
161*2df1fe9cSrandyf  *	if (platform on whitelist)
162*2df1fe9cSrandyf  *		return (yes)
163*2df1fe9cSrandyf  *	return (no)
164*2df1fe9cSrandyf  * }
165*2df1fe9cSrandyf  */
166*2df1fe9cSrandyf 
167*2df1fe9cSrandyf int
168*2df1fe9cSrandyf S3_helper(char *whitelist, char *blacklist, int yes, int no, char *keyword,
169*2df1fe9cSrandyf 	char *behavior, int *didyes, int suppress)
170*2df1fe9cSrandyf {
171*2df1fe9cSrandyf 	int oflags = SMB_O_NOCKSUM | SMB_O_NOVERS;
172*2df1fe9cSrandyf 	smbios_hdl_t *shp;
173*2df1fe9cSrandyf 	smbios_system_t sys;
174*2df1fe9cSrandyf 	id_t id;
175*2df1fe9cSrandyf 	int ret;
176*2df1fe9cSrandyf 	kstat_ctl_t *kc;
177*2df1fe9cSrandyf 	kstat_t *ksp;
178*2df1fe9cSrandyf 	kstat_named_t *dp;
179*2df1fe9cSrandyf 	smbios_info_t info;
180*2df1fe9cSrandyf 	int preferred_pm_profile = 0;
181*2df1fe9cSrandyf 	char yesstr[32], nostr[32];	/* DEBUG */
182*2df1fe9cSrandyf 
183*2df1fe9cSrandyf 	*didyes = 0;
184*2df1fe9cSrandyf 
185*2df1fe9cSrandyf 	strncpy(yesstr, pm_map(yes), sizeof (yesstr));
186*2df1fe9cSrandyf 	strncpy(nostr, pm_map(no), sizeof (nostr));
187*2df1fe9cSrandyf 	mesg(MDEBUG, "S3_helper(%s, %s, %s, %s, %s, %s)\n", whitelist,
188*2df1fe9cSrandyf 	    blacklist, yesstr, nostr, keyword, behavior);
189*2df1fe9cSrandyf 	if ((kc = kstat_open()) == NULL) {
190*2df1fe9cSrandyf 		mesg(MDEBUG, "kstat_open failed\n");
191*2df1fe9cSrandyf 		return (OKUP);
192*2df1fe9cSrandyf 	}
193*2df1fe9cSrandyf 	ksp = kstat_lookup(kc, "acpi", -1, "acpi");
194*2df1fe9cSrandyf 	if (ksp == NULL) {
195*2df1fe9cSrandyf 		mesg(MDEBUG, "kstat_lookup 'acpi', -1, 'acpi' failed\n");
196*2df1fe9cSrandyf 		kstat_close(kc);
197*2df1fe9cSrandyf 		return (OKUP);
198*2df1fe9cSrandyf 	}
199*2df1fe9cSrandyf 	(void) kstat_read(kc, ksp,  NULL);
200*2df1fe9cSrandyf 	dp = kstat_data_lookup(ksp, "S3");
201*2df1fe9cSrandyf 	if (dp == NULL || dp->value.l == 0) {
202*2df1fe9cSrandyf 		mesg(MDEBUG, "kstat_data_lookup 'S3' fails\n");
203*2df1fe9cSrandyf 		if (dp != NULL)
204*2df1fe9cSrandyf 			mesg(MDEBUG, "value.l %lx\n", dp->value.l);
205*2df1fe9cSrandyf 		kstat_close(kc);
206*2df1fe9cSrandyf 		return (do_ioctl(no, keyword, behavior, suppress));
207*2df1fe9cSrandyf 	}
208*2df1fe9cSrandyf 	mesg(MDEBUG, "kstat indicates S3 support (%lx)\n", dp->value.l);
209*2df1fe9cSrandyf 
210*2df1fe9cSrandyf 	if (!whitelist_only) {
211*2df1fe9cSrandyf 		/*
212*2df1fe9cSrandyf 		 * We still have an ACPI ksp, search it again for
213*2df1fe9cSrandyf 		 * 'preferred_pm_profile' (needs to be valid if we don't
214*2df1fe9cSrandyf 		 * aren't only using a whitelist).
215*2df1fe9cSrandyf 		 */
216*2df1fe9cSrandyf 		dp = kstat_data_lookup(ksp, "preferred_pm_profile");
217*2df1fe9cSrandyf 		if (dp == NULL) {
218*2df1fe9cSrandyf 			mesg(MDEBUG, "kstat_data_lookup 'ppmp fails\n");
219*2df1fe9cSrandyf 			kstat_close(kc);
220*2df1fe9cSrandyf 			return (do_ioctl(no, keyword, behavior, suppress));
221*2df1fe9cSrandyf 		}
222*2df1fe9cSrandyf 		mesg(MDEBUG, "kstat indicates preffered_pm_profile is %lx\n",
223*2df1fe9cSrandyf 		    dp->value.l);
224*2df1fe9cSrandyf 		preferred_pm_profile = dp->value.l;
225*2df1fe9cSrandyf 	}
226*2df1fe9cSrandyf 	kstat_close(kc);
227*2df1fe9cSrandyf 
228*2df1fe9cSrandyf 	if ((shp = smbios_open(NULL,
229*2df1fe9cSrandyf 	    SMB_VERSION, oflags, &ret)) == NULL) {
230*2df1fe9cSrandyf 		/* we promised not to complain */
231*2df1fe9cSrandyf 		/* we bail leaving it to the kernel default */
232*2df1fe9cSrandyf 		mesg(MDEBUG, "smbios_open failed %d\n", errno);
233*2df1fe9cSrandyf 		return (OKUP);
234*2df1fe9cSrandyf 	}
235*2df1fe9cSrandyf 	if ((id = smbios_info_system(shp, &sys)) == SMB_ERR) {
236*2df1fe9cSrandyf 		mesg(MDEBUG, "smbios_info_system failed %d\n", errno);
237*2df1fe9cSrandyf 		smbios_close(shp);
238*2df1fe9cSrandyf 		return (OKUP);
239*2df1fe9cSrandyf 	}
240*2df1fe9cSrandyf 	if (smbios_info_common(shp, id, &info) == SMB_ERR) {
241*2df1fe9cSrandyf 		mesg(MDEBUG, "smbios_info_common failed %d\n", errno);
242*2df1fe9cSrandyf 		smbios_close(shp);
243*2df1fe9cSrandyf 		return (OKUP);
244*2df1fe9cSrandyf 	}
245*2df1fe9cSrandyf 	mesg(MDEBUG, "Manufacturer: %s\n", info.smbi_manufacturer);
246*2df1fe9cSrandyf 	mesg(MDEBUG, "Product: %s\n", info.smbi_product);
247*2df1fe9cSrandyf 	smbios_close(shp);
248*2df1fe9cSrandyf 
249*2df1fe9cSrandyf 	if (!whitelist_only) {
250*2df1fe9cSrandyf #define	PPP_DESKTOP 1
251*2df1fe9cSrandyf #define	PPP_WORKSTATION 3
252*2df1fe9cSrandyf 		if (strcmp(info.smbi_manufacturer, "Sun Microsystems") == 0 &&
253*2df1fe9cSrandyf 		    (preferred_pm_profile == PPP_DESKTOP ||
254*2df1fe9cSrandyf 		    preferred_pm_profile == PPP_WORKSTATION)) {
255*2df1fe9cSrandyf 			if (isonlist(blacklist,
256*2df1fe9cSrandyf 			    info.smbi_manufacturer, info.smbi_product)) {
257*2df1fe9cSrandyf 				return (do_ioctl(no, keyword, behavior,
258*2df1fe9cSrandyf 				    suppress));
259*2df1fe9cSrandyf 			} else {
260*2df1fe9cSrandyf 				ret = do_ioctl(yes, keyword, behavior,
261*2df1fe9cSrandyf 				    suppress);
262*2df1fe9cSrandyf 				*didyes = (ret == OKUP);
263*2df1fe9cSrandyf 				return (ret);
264*2df1fe9cSrandyf 			}
265*2df1fe9cSrandyf 		}
266*2df1fe9cSrandyf 	}
267*2df1fe9cSrandyf 	if (isonlist(whitelist,
268*2df1fe9cSrandyf 	    info.smbi_manufacturer, info.smbi_product)) {
269*2df1fe9cSrandyf 		ret = do_ioctl(yes, keyword, behavior, suppress);
270*2df1fe9cSrandyf 		*didyes = (ret == OKUP);
271*2df1fe9cSrandyf 		return (ret);
272*2df1fe9cSrandyf 	} else {
273*2df1fe9cSrandyf 		return (do_ioctl(no, keyword, behavior, suppress));
274*2df1fe9cSrandyf 	}
275*2df1fe9cSrandyf }
276*2df1fe9cSrandyf 
277*2df1fe9cSrandyf int
278*2df1fe9cSrandyf S3sup(void)	/* S3-support keyword handler */
279*2df1fe9cSrandyf {
280*2df1fe9cSrandyf 	struct btoc {
281*2df1fe9cSrandyf 		char *behavior;
282*2df1fe9cSrandyf 		int cmd;
283*2df1fe9cSrandyf 	};
284*2df1fe9cSrandyf 	static struct btoc blist[] = {
285*2df1fe9cSrandyf 		"default",	PM_DEFAULT_ALGORITHM,
286*2df1fe9cSrandyf 		"enable",	PM_ENABLE_S3,
287*2df1fe9cSrandyf 		"disable",	PM_DISABLE_S3,
288*2df1fe9cSrandyf 		NULL,		0
289*2df1fe9cSrandyf 	};
290*2df1fe9cSrandyf 	struct btoc *bp;
291*2df1fe9cSrandyf 	char *behavior;
292*2df1fe9cSrandyf 	int dontcare;
293*2df1fe9cSrandyf 
294*2df1fe9cSrandyf 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
295*2df1fe9cSrandyf 		if (strcmp(behavior, bp->behavior) == 0)
296*2df1fe9cSrandyf 			break;
297*2df1fe9cSrandyf 	}
298*2df1fe9cSrandyf 	if (bp->cmd == 0) {
299*2df1fe9cSrandyf 		mesg(MERR, "invalid S3-support behavior \"%s\"\n", behavior);
300*2df1fe9cSrandyf 		return (NOUP);
301*2df1fe9cSrandyf 	}
302*2df1fe9cSrandyf 
303*2df1fe9cSrandyf 
304*2df1fe9cSrandyf 	switch (bp->cmd) {
305*2df1fe9cSrandyf 
306*2df1fe9cSrandyf 	case PM_ENABLE_S3:
307*2df1fe9cSrandyf 	case PM_DISABLE_S3:
308*2df1fe9cSrandyf 		return (do_ioctl(bp->cmd, "S3-support", behavior, EBUSY));
309*2df1fe9cSrandyf 
310*2df1fe9cSrandyf 	case PM_DEFAULT_ALGORITHM:
311*2df1fe9cSrandyf 		/*
312*2df1fe9cSrandyf 		 * we suppress errors in the "default" case because we
313*2df1fe9cSrandyf 		 * already did an invisible default call, so we know we'll
314*2df1fe9cSrandyf 		 * get EBUSY
315*2df1fe9cSrandyf 		 */
316*2df1fe9cSrandyf 		return (S3_helper("S3-support-enable", "S3-support-disable",
317*2df1fe9cSrandyf 		    PM_ENABLE_S3, PM_DISABLE_S3, "S3-support", behavior,
318*2df1fe9cSrandyf 		    &dontcare, EBUSY));
319*2df1fe9cSrandyf 
320*2df1fe9cSrandyf 	default:
321*2df1fe9cSrandyf 		mesg(MERR, "S3-support %s failed, %s\n", behavior,
322*2df1fe9cSrandyf 		    strerror(errno));
323*2df1fe9cSrandyf 		return (NOUP);
324*2df1fe9cSrandyf 	}
325*2df1fe9cSrandyf }
326*2df1fe9cSrandyf 
327*2df1fe9cSrandyf /*
328*2df1fe9cSrandyf  * Check for valid autoS3 behavior and save after ioctl success.
329*2df1fe9cSrandyf  */
330*2df1fe9cSrandyf int
331*2df1fe9cSrandyf autoS3(void)
332*2df1fe9cSrandyf {
333*2df1fe9cSrandyf 	struct btoc {
334*2df1fe9cSrandyf 		char *behavior;
335*2df1fe9cSrandyf 		int cmd;
336*2df1fe9cSrandyf 	};
337*2df1fe9cSrandyf 	static struct btoc blist[] = {
338*2df1fe9cSrandyf 		"default",	PM_DEFAULT_ALGORITHM,
339*2df1fe9cSrandyf 		"disable",	PM_STOP_AUTOS3,
340*2df1fe9cSrandyf 		"enable",	PM_START_AUTOS3,
341*2df1fe9cSrandyf 		NULL,		0
342*2df1fe9cSrandyf 	};
343*2df1fe9cSrandyf 	struct btoc *bp;
344*2df1fe9cSrandyf 	char *behavior;
345*2df1fe9cSrandyf 	int dontcare;
346*2df1fe9cSrandyf 
347*2df1fe9cSrandyf 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
348*2df1fe9cSrandyf 		if (strcmp(behavior, bp->behavior) == 0)
349*2df1fe9cSrandyf 			break;
350*2df1fe9cSrandyf 	}
351*2df1fe9cSrandyf 	if (bp->cmd == 0) {
352*2df1fe9cSrandyf 		mesg(MERR, "invalid autoS3 behavior \"%s\"\n", behavior);
353*2df1fe9cSrandyf 		return (NOUP);
354*2df1fe9cSrandyf 	}
355*2df1fe9cSrandyf 
356*2df1fe9cSrandyf 	switch (bp->cmd) {
357*2df1fe9cSrandyf 	default:
358*2df1fe9cSrandyf 		mesg(MERR, "autoS3 %s failed, %s\n",
359*2df1fe9cSrandyf 		    behavior, strerror(errno));
360*2df1fe9cSrandyf 		mesg(MDEBUG, "unknown command\n", bp->cmd);
361*2df1fe9cSrandyf 		return (OKUP);
362*2df1fe9cSrandyf 
363*2df1fe9cSrandyf 	case PM_STOP_AUTOS3:
364*2df1fe9cSrandyf 	case PM_START_AUTOS3:
365*2df1fe9cSrandyf 		return (do_ioctl(bp->cmd, "autoS3", behavior, EBUSY));
366*2df1fe9cSrandyf 
367*2df1fe9cSrandyf 	case PM_DEFAULT_ALGORITHM:
368*2df1fe9cSrandyf 		return (S3_helper("S3-autoenable", "S3-autodisable",
369*2df1fe9cSrandyf 		    PM_START_AUTOS3, PM_STOP_AUTOS3, "autoS3", behavior,
370*2df1fe9cSrandyf 		    &dontcare, EBUSY));
371*2df1fe9cSrandyf 	}
372*2df1fe9cSrandyf }
373*2df1fe9cSrandyf 
374*2df1fe9cSrandyf 
375*2df1fe9cSrandyf /*
3767c478bd9Sstevel@tonic-gate  * Check for valid autopm behavior and save after ioctl success.
3777c478bd9Sstevel@tonic-gate  */
3787c478bd9Sstevel@tonic-gate int
3797c478bd9Sstevel@tonic-gate autopm(void)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	struct btoc {
3827c478bd9Sstevel@tonic-gate 		char *behavior;
3837c478bd9Sstevel@tonic-gate 		int cmd, Errno, isdef;
3847c478bd9Sstevel@tonic-gate 	};
3857c478bd9Sstevel@tonic-gate 	static struct btoc blist[] = {
386*2df1fe9cSrandyf 		"default",	PM_START_PM,	-1,	1,
3877c478bd9Sstevel@tonic-gate 		"disable",	PM_STOP_PM,	EINVAL,	0,
3887c478bd9Sstevel@tonic-gate 		"enable",	PM_START_PM,	EBUSY,	0,
3897c478bd9Sstevel@tonic-gate 		NULL,		0,		0,	0,
3907c478bd9Sstevel@tonic-gate 	};
3917c478bd9Sstevel@tonic-gate 	struct btoc *bp;
3927c478bd9Sstevel@tonic-gate 	char *behavior;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
3957c478bd9Sstevel@tonic-gate 		if (strcmp(behavior, bp->behavior) == 0)
3967c478bd9Sstevel@tonic-gate 			break;
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 	if (bp->cmd == 0) {
3997c478bd9Sstevel@tonic-gate 		mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior);
4007c478bd9Sstevel@tonic-gate 		return (NOUP);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/*
4047c478bd9Sstevel@tonic-gate 	 * for "default" behavior, do not enable autopm if not ESTAR_V3
4057c478bd9Sstevel@tonic-gate 	 */
406*2df1fe9cSrandyf #if defined(__sparc)
4077c478bd9Sstevel@tonic-gate 	if (!bp->isdef || (estar_vers == ESTAR_V3)) {
4087c478bd9Sstevel@tonic-gate 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
4097c478bd9Sstevel@tonic-gate 			mesg(MERR, "autopm %s failed, %s\n",
4107c478bd9Sstevel@tonic-gate 			    behavior, strerror(errno));
4117c478bd9Sstevel@tonic-gate 			return (NOUP);
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 	(void) strcpy(new_cc.apm_behavior, behavior);
4157c478bd9Sstevel@tonic-gate 	return (OKUP);
416*2df1fe9cSrandyf #endif
417*2df1fe9cSrandyf #if defined(__x86)
418*2df1fe9cSrandyf 	if (!bp->isdef) {
419*2df1fe9cSrandyf 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
420*2df1fe9cSrandyf 			mesg(MERR, "autopm %s failed, %s\n",
421*2df1fe9cSrandyf 			    behavior, strerror(errno));
422*2df1fe9cSrandyf 			return (NOUP);
423*2df1fe9cSrandyf 		}
424*2df1fe9cSrandyf 		mesg(MDEBUG, "autopm %s succeeded\n", behavior);
425*2df1fe9cSrandyf 
426*2df1fe9cSrandyf 		return (OKUP);
427*2df1fe9cSrandyf 	} else {
428*2df1fe9cSrandyf 		int didenable;
429*2df1fe9cSrandyf 		int ret = S3_helper("autopm-enable", "autopm-disable",
430*2df1fe9cSrandyf 		    PM_START_PM, PM_STOP_PM, "autopm", behavior, &didenable,
431*2df1fe9cSrandyf 		    bp->Errno);
432*2df1fe9cSrandyf 		if (didenable) {
433*2df1fe9cSrandyf 			/* tell powerd to attach all devices */
434*2df1fe9cSrandyf 			new_cc.is_autopm_default = 1;
435*2df1fe9cSrandyf 			(void) strcpy(new_cc.apm_behavior, behavior);
436*2df1fe9cSrandyf 		}
437*2df1fe9cSrandyf 		return (ret);
438*2df1fe9cSrandyf 	}
439*2df1fe9cSrandyf #endif
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate static int
4447c478bd9Sstevel@tonic-gate gethm(char *src, int *hour, int *min)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	if (sscanf(src, "%d:%d", hour, min) != 2) {
4477c478bd9Sstevel@tonic-gate 		mesg(MERR, "bad time format (%s)\n", src);
4487c478bd9Sstevel@tonic-gate 		return (-1);
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 	return (0);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate static void
4557c478bd9Sstevel@tonic-gate strcpy_limit(char *dst, char *src, size_t limit, char *info)
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate 	if (strlcpy(dst, src, limit) >= limit)
4587c478bd9Sstevel@tonic-gate 		mesg(MEXIT, "%s is too long (%s)\n", info, src);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate  * Convert autoshutdown idle and start/finish times;
4647c478bd9Sstevel@tonic-gate  * check and record autoshutdown behavior.
4657c478bd9Sstevel@tonic-gate  */
4667c478bd9Sstevel@tonic-gate int
4677c478bd9Sstevel@tonic-gate autosd(void)
4687c478bd9Sstevel@tonic-gate {
4691dc065c6Smh27603 	char **bp, *behavior;
4701dc065c6Smh27603 	char *unrec = gettext("unrecognized autoshutdown behavior");
4717c478bd9Sstevel@tonic-gate 	static char *blist[] = {
4727c478bd9Sstevel@tonic-gate 		"autowakeup", "default", "noshutdown",
4737c478bd9Sstevel@tonic-gate 		"shutdown", "unconfigured", NULL
4747c478bd9Sstevel@tonic-gate 	};
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	new_cc.as_idle = atoi(LINEARG(1));
4777c478bd9Sstevel@tonic-gate 	if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) ||
4787c478bd9Sstevel@tonic-gate 	    gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm))
4797c478bd9Sstevel@tonic-gate 		return (NOUP);
4801dc065c6Smh27603 	mesg(MDEBUG, "idle %d, start %d:%02d, finish %d:%02d\n",
4817c478bd9Sstevel@tonic-gate 	    new_cc.as_idle, new_cc.as_sh, new_cc.as_sm,
4827c478bd9Sstevel@tonic-gate 	    new_cc.as_fh, new_cc.as_fm);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	for (behavior = LINEARG(4), bp = blist; *bp; bp++) {
4857c478bd9Sstevel@tonic-gate 		if (strcmp(behavior, *bp) == 0)
4867c478bd9Sstevel@tonic-gate 			break;
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	if (*bp == NULL) {
4897c478bd9Sstevel@tonic-gate 		mesg(MERR, "%s: \"%s\"\n", unrec, behavior);
4907c478bd9Sstevel@tonic-gate 		return (NOUP);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	STRCPYLIM(new_cc.as_behavior, *bp, unrec);
4937c478bd9Sstevel@tonic-gate 	return (OKUP);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate  * Check for a real device and try to resolve to a full path.
4997c478bd9Sstevel@tonic-gate  * The orig/resolved path may be modified into a prom pathname,
5007c478bd9Sstevel@tonic-gate  * and an allocated copy of the result is stored at *destp;
5017c478bd9Sstevel@tonic-gate  * the caller will need to free that space.  Returns 1 for any
5027c478bd9Sstevel@tonic-gate  * error, otherwise 0; also sets *errp after an alloc error.
5037c478bd9Sstevel@tonic-gate  */
5047c478bd9Sstevel@tonic-gate static int
5057c478bd9Sstevel@tonic-gate devpath(char **destp, char *src, int *errp)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	struct stat stbuf;
5087c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX];
5097c478bd9Sstevel@tonic-gate 	char *cp, *dstr;
5107c478bd9Sstevel@tonic-gate 	int devok, dcs = 0;
5117c478bd9Sstevel@tonic-gate 	size_t len;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	/*
5147c478bd9Sstevel@tonic-gate 	 * When there's a real device, try to resolve the path
5157c478bd9Sstevel@tonic-gate 	 * and trim the leading "/devices" component.
5167c478bd9Sstevel@tonic-gate 	 */
5177c478bd9Sstevel@tonic-gate 	if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) {
5187c478bd9Sstevel@tonic-gate 		if (realpath(src, buf) == NULL) {
5197c478bd9Sstevel@tonic-gate 			mesg(MERR, "realpath cannot resolve \"%s\"\n",
5207c478bd9Sstevel@tonic-gate 			    src, strerror(errno));
5217c478bd9Sstevel@tonic-gate 			return (1);
5227c478bd9Sstevel@tonic-gate 		}
5237c478bd9Sstevel@tonic-gate 		src = buf;
5247c478bd9Sstevel@tonic-gate 		dstr = "/devices";
5257c478bd9Sstevel@tonic-gate 		len = strlen(dstr);
5267c478bd9Sstevel@tonic-gate 		dcs = (strncmp(src, dstr, len) == 0);
5277c478bd9Sstevel@tonic-gate 		if (dcs)
5287c478bd9Sstevel@tonic-gate 			src += len;
5297c478bd9Sstevel@tonic-gate 	} else
5307c478bd9Sstevel@tonic-gate 		mesg(MDEBUG, stat_fmt, src, strerror(errno));
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * When the path has ":anything", display an error for
5347c478bd9Sstevel@tonic-gate 	 * a non-device or truncate a resolved+modifed path.
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	if (cp = strchr(src, ':')) {
5377c478bd9Sstevel@tonic-gate 		if (devok == 0) {
5387c478bd9Sstevel@tonic-gate 			mesg(MERR, "physical path may not contain "
5397c478bd9Sstevel@tonic-gate 			    "a minor string (%s)\n", src);
5407c478bd9Sstevel@tonic-gate 			return (1);
5417c478bd9Sstevel@tonic-gate 		} else if (dcs)
5427c478bd9Sstevel@tonic-gate 			*cp = '\0';
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	if ((*destp = strdup(src)) == NULL) {
5467c478bd9Sstevel@tonic-gate 		*errp = NOUP;
5477c478bd9Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, src, strerror(errno));
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 	return (*destp == NULL);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate  * Call pm ioctl request(s) to set property/device dependencies.
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate static int
5577c478bd9Sstevel@tonic-gate dev_dep_common(int isprop)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	int cmd, argn, upval = OKUP;
5607c478bd9Sstevel@tonic-gate 	char *src, *first, **destp;
5617c478bd9Sstevel@tonic-gate 	pm_req_t pmreq;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
5647c478bd9Sstevel@tonic-gate 	src = LINEARG(1);
5657c478bd9Sstevel@tonic-gate 	if (isprop) {
5667c478bd9Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT_PROPERTY;
5677c478bd9Sstevel@tonic-gate 		first = NULL;
5687c478bd9Sstevel@tonic-gate 		pmreq.pmreq_kept = src;
5697c478bd9Sstevel@tonic-gate 	} else {
5707c478bd9Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT;
5717c478bd9Sstevel@tonic-gate 		if (devpath(&first, src, &upval))
5727c478bd9Sstevel@tonic-gate 			return (upval);
5737c478bd9Sstevel@tonic-gate 		pmreq.pmreq_kept = first;
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 	destp = &pmreq.pmreq_keeper;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/*
5787c478bd9Sstevel@tonic-gate 	 * Now loop through any dependents.
5797c478bd9Sstevel@tonic-gate 	 */
5807c478bd9Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
5817c478bd9Sstevel@tonic-gate 		if (devpath(destp, src, &upval)) {
5827c478bd9Sstevel@tonic-gate 			if (upval != OKUP)
5837c478bd9Sstevel@tonic-gate 				return (upval);
5847c478bd9Sstevel@tonic-gate 			break;
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 		if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) {
5877c478bd9Sstevel@tonic-gate 			mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n"
5887c478bd9Sstevel@tonic-gate 			    "kept \"%s\", keeper \"%s\"\n",
5897c478bd9Sstevel@tonic-gate 			    cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper);
5907c478bd9Sstevel@tonic-gate 			mesg(MERR, "cannot set \"%s\" dependency "
5917c478bd9Sstevel@tonic-gate 			    "for \"%s\", %s\n", pmreq.pmreq_keeper,
5927c478bd9Sstevel@tonic-gate 			    pmreq.pmreq_kept, strerror(errno));
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 		free(*destp);
5957c478bd9Sstevel@tonic-gate 		*destp = NULL;
5967c478bd9Sstevel@tonic-gate 		if (upval != OKUP)
5977c478bd9Sstevel@tonic-gate 			break;
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	free(first);
6017c478bd9Sstevel@tonic-gate 	return (upval);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate int
6067c478bd9Sstevel@tonic-gate ddprop(void)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	return (dev_dep_common(1));
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate int
6137c478bd9Sstevel@tonic-gate devdep(void)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	return (dev_dep_common(0));
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate /*
6207c478bd9Sstevel@tonic-gate  * Convert a numeric string (with a possible trailing scaling byte)
6217c478bd9Sstevel@tonic-gate  * into an integer.  Returns a converted value and *nerrp unchanged,
6227c478bd9Sstevel@tonic-gate  * or 0 with *nerrp set to 1 for a conversion error.
6237c478bd9Sstevel@tonic-gate  */
6247c478bd9Sstevel@tonic-gate static int
6257c478bd9Sstevel@tonic-gate get_scaled_value(char *str, int *nerrp)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	longlong_t svalue = 0, factor = 1;
6287c478bd9Sstevel@tonic-gate 	char *sp;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	errno = 0;
6317c478bd9Sstevel@tonic-gate 	svalue = strtol(str, &sp, 0);
6327c478bd9Sstevel@tonic-gate 	if (errno || (*str != '-' && (*str < '0' || *str > '9')))
6337c478bd9Sstevel@tonic-gate 		*nerrp = 1;
6347c478bd9Sstevel@tonic-gate 	else if (sp && *sp != '\0') {
6357c478bd9Sstevel@tonic-gate 		if (*sp == 'h')
6367c478bd9Sstevel@tonic-gate 			factor = 3600;
6377c478bd9Sstevel@tonic-gate 		else if (*sp == 'm')
6387c478bd9Sstevel@tonic-gate 			factor = 60;
6397c478bd9Sstevel@tonic-gate 		else if (*sp != 's')
6407c478bd9Sstevel@tonic-gate 			*nerrp = 1;
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 	/* any bytes following sp are ignored */
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	if (*nerrp == 0) {
6457c478bd9Sstevel@tonic-gate 		svalue *= factor;
6467c478bd9Sstevel@tonic-gate 		if (svalue < INT_MIN || svalue > INT_MAX)
6477c478bd9Sstevel@tonic-gate 			*nerrp = 1;
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 	if (*nerrp)
6507c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, str);
6517c478bd9Sstevel@tonic-gate 	mesg(MDEBUG, "got scaled value %d\n", (int)svalue);
6527c478bd9Sstevel@tonic-gate 	return ((int)svalue);
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate /*
6577c478bd9Sstevel@tonic-gate  * Increment the count of threshold values,
6587c478bd9Sstevel@tonic-gate  * reallocate *vlistp and append another element.
6597c478bd9Sstevel@tonic-gate  * Returns 1 on error, otherwise 0.
6607c478bd9Sstevel@tonic-gate  */
6617c478bd9Sstevel@tonic-gate static int
6627c478bd9Sstevel@tonic-gate vlist_append(int **vlistp, int *vcntp, int value)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	(*vcntp)++;
6657c478bd9Sstevel@tonic-gate 	if (*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp)))
6667c478bd9Sstevel@tonic-gate 		*(*vlistp + *vcntp - 1) = value;
6677c478bd9Sstevel@tonic-gate 	else
6687c478bd9Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, "threshold list", strerror(errno));
6697c478bd9Sstevel@tonic-gate 	return (*vlistp == NULL);
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * Convert a single threshold string or paren groups of thresh's as
6757c478bd9Sstevel@tonic-gate  * described below.  All thresh's are saved to an allocated list at
6767c478bd9Sstevel@tonic-gate  * *vlistp; the caller will need to free that space.  On return:
6777c478bd9Sstevel@tonic-gate  * *vcntp is the count of the vlist array, and vlist is either
6787c478bd9Sstevel@tonic-gate  * a single thresh or N groups of thresh's with a trailing zero:
6797c478bd9Sstevel@tonic-gate  * (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0.
6807c478bd9Sstevel@tonic-gate  * Returns 0 when all conversions were OK, and 1 for any syntax,
6817c478bd9Sstevel@tonic-gate  * conversion, or alloc error.
6827c478bd9Sstevel@tonic-gate  */
6837c478bd9Sstevel@tonic-gate static int
6847c478bd9Sstevel@tonic-gate get_thresh(int **vlistp, int *vcntp)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	int argn, value, gci, grp_cnt = 0, paren = 0, nerr = 0;
6877c478bd9Sstevel@tonic-gate 	char *rp, *src;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
6907c478bd9Sstevel@tonic-gate 		if (*src == LPAREN) {
6917c478bd9Sstevel@tonic-gate 			gci = *vcntp;
6927c478bd9Sstevel@tonic-gate 			if (nerr = vlist_append(vlistp, vcntp, 0))
6937c478bd9Sstevel@tonic-gate 				break;
6947c478bd9Sstevel@tonic-gate 			paren = 1;
6957c478bd9Sstevel@tonic-gate 			src++;
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 		if (*(rp = LASTBYTE(src)) == RPAREN) {
6987c478bd9Sstevel@tonic-gate 			if (paren) {
6997c478bd9Sstevel@tonic-gate 				grp_cnt = *vcntp - gci;
7007c478bd9Sstevel@tonic-gate 				*(*vlistp + gci) = grp_cnt;
7017c478bd9Sstevel@tonic-gate 				paren = 0;
7027c478bd9Sstevel@tonic-gate 				*rp = '\0';
7037c478bd9Sstevel@tonic-gate 			} else {
7047c478bd9Sstevel@tonic-gate 				nerr = 1;
7057c478bd9Sstevel@tonic-gate 				break;
7067c478bd9Sstevel@tonic-gate 			}
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		value = get_scaled_value(src, &nerr);
7107c478bd9Sstevel@tonic-gate 		if (nerr || (nerr = vlist_append(vlistp, vcntp, value)))
7117c478bd9Sstevel@tonic-gate 			break;
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if (nerr == 0 && grp_cnt)
7157c478bd9Sstevel@tonic-gate 		nerr = vlist_append(vlistp, vcntp, 0);
7167c478bd9Sstevel@tonic-gate 	return (nerr);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate /*
7217c478bd9Sstevel@tonic-gate  * Set device thresholds from (3) formats:
7227c478bd9Sstevel@tonic-gate  * 	path	"always-on"
7237c478bd9Sstevel@tonic-gate  * 	path	time-spec: [0-9]+[{h,m,s}]
7247c478bd9Sstevel@tonic-gate  *	path	(ts1 ts2 ...)+
7257c478bd9Sstevel@tonic-gate  */
7267c478bd9Sstevel@tonic-gate int
7277c478bd9Sstevel@tonic-gate devthr(void)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 	int cmd, upval = OKUP, nthresh = 0, *vlist = NULL;
7307c478bd9Sstevel@tonic-gate 	pm_req_t pmreq;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
7337c478bd9Sstevel@tonic-gate 	if (devpath(&pmreq.physpath, LINEARG(1), &upval))
7347c478bd9Sstevel@tonic-gate 		return (upval);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	if (strcmp(LINEARG(2), always_on) == 0) {
7377c478bd9Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
7387c478bd9Sstevel@tonic-gate 		pmreq.value = INT_MAX;
7397c478bd9Sstevel@tonic-gate 	} else if (get_thresh(&vlist, &nthresh)) {
7407c478bd9Sstevel@tonic-gate 		mesg(MERR, bad_thresh_fmt);
7417c478bd9Sstevel@tonic-gate 		upval = NOUP;
7427c478bd9Sstevel@tonic-gate 	} else if (nthresh == 1) {
7437c478bd9Sstevel@tonic-gate 		pmreq.value = *vlist;
7447c478bd9Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
7457c478bd9Sstevel@tonic-gate 	} else {
7467c478bd9Sstevel@tonic-gate 		pmreq.data = vlist;
7477c478bd9Sstevel@tonic-gate 		pmreq.datasize = (nthresh * sizeof (*vlist));
7487c478bd9Sstevel@tonic-gate 		cmd = PM_SET_COMPONENT_THRESHOLDS;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1)
7527c478bd9Sstevel@tonic-gate 		mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno));
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	free(vlist);
7557c478bd9Sstevel@tonic-gate 	free(pmreq.physpath);
7567c478bd9Sstevel@tonic-gate 	return (upval);
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate static int
7617c478bd9Sstevel@tonic-gate scan_int(char *src, int *dst)
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	long lval;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	errno = 0;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	lval = strtol(LINEARG(1), NULL, 0);
7687c478bd9Sstevel@tonic-gate 	if (errno || lval > INT_MAX || lval < 0) {
7697c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
7707c478bd9Sstevel@tonic-gate 		return (NOUP);
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	*dst = (int)lval;
7747c478bd9Sstevel@tonic-gate 	return (OKUP);
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate static int
7787c478bd9Sstevel@tonic-gate scan_float(char *src, float *dst)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	float fval;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	errno = 0;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	fval = strtof(src, NULL);
7857c478bd9Sstevel@tonic-gate 	if (errno || fval < 0.0) {
7867c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
7877c478bd9Sstevel@tonic-gate 		return (NOUP);
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	*dst = fval;
7917c478bd9Sstevel@tonic-gate 	return (OKUP);
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate int
7967c478bd9Sstevel@tonic-gate dreads(void)
7977c478bd9Sstevel@tonic-gate {
7987c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.diskreads_thold));
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate /*
8037c478bd9Sstevel@tonic-gate  * Set pathname for idlecheck;
8047c478bd9Sstevel@tonic-gate  * an overflowed pathname is treated as a fatal error.
8057c478bd9Sstevel@tonic-gate  */
8067c478bd9Sstevel@tonic-gate int
8077c478bd9Sstevel@tonic-gate idlechk(void)
8087c478bd9Sstevel@tonic-gate {
8097c478bd9Sstevel@tonic-gate 	STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path");
8107c478bd9Sstevel@tonic-gate 	return (OKUP);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate int
8157c478bd9Sstevel@tonic-gate loadavg(void)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	return (scan_float(LINEARG(1), &new_cc.loadaverage_thold));
8187c478bd9Sstevel@tonic-gate }
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate int
8227c478bd9Sstevel@tonic-gate nfsreq(void)
8237c478bd9Sstevel@tonic-gate {
8247c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold));
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate #ifdef sparc
8297c478bd9Sstevel@tonic-gate static char open_fmt[] = "cannot open \"%s\", %s\n";
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate /*
8327c478bd9Sstevel@tonic-gate  * Verify the filesystem type for a regular statefile is "ufs"
8337c478bd9Sstevel@tonic-gate  * or verify a block device is not in use as a mounted filesytem.
8347c478bd9Sstevel@tonic-gate  * Returns 1 if any error, otherwise 0.
8357c478bd9Sstevel@tonic-gate  */
8367c478bd9Sstevel@tonic-gate static int
8377c478bd9Sstevel@tonic-gate check_mount(char *sfile, dev_t sfdev, int ufs)
8387c478bd9Sstevel@tonic-gate {
8397c478bd9Sstevel@tonic-gate 	char *src, *err_fmt = NULL, *mnttab = MNTTAB;
8407c478bd9Sstevel@tonic-gate 	int rgent, match = 0;
8417c478bd9Sstevel@tonic-gate 	struct extmnttab ent;
8427c478bd9Sstevel@tonic-gate 	FILE *fp;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
8457c478bd9Sstevel@tonic-gate 		mesg(MERR, open_fmt, mnttab, strerror(errno));
8467c478bd9Sstevel@tonic-gate 		return (1);
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	/*
8507c478bd9Sstevel@tonic-gate 	 * Search for a matching dev_t;
8517c478bd9Sstevel@tonic-gate 	 * ignore non-ufs filesystems for a regular statefile.
8527c478bd9Sstevel@tonic-gate 	 */
8537c478bd9Sstevel@tonic-gate 	while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) {
8547c478bd9Sstevel@tonic-gate 		if (rgent > 0) {
8557c478bd9Sstevel@tonic-gate 			mesg(MERR, "error reading \"%s\"\n", mnttab);
8567c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
8577c478bd9Sstevel@tonic-gate 			return (1);
8587c478bd9Sstevel@tonic-gate 		} else if (ufs && strcmp(ent.mnt_fstype, "ufs"))
8597c478bd9Sstevel@tonic-gate 			continue;
8607c478bd9Sstevel@tonic-gate 		else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) {
8617c478bd9Sstevel@tonic-gate 			match = 1;
8627c478bd9Sstevel@tonic-gate 			break;
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	/*
8687c478bd9Sstevel@tonic-gate 	 * No match is needed for a block device statefile,
8697c478bd9Sstevel@tonic-gate 	 * a match is needed for a regular statefile.
8707c478bd9Sstevel@tonic-gate 	 */
8717c478bd9Sstevel@tonic-gate 	if (match == 0) {
8727c478bd9Sstevel@tonic-gate 		if (new_cc.cf_type == CFT_SPEC)
8737c478bd9Sstevel@tonic-gate 			STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile");
8747c478bd9Sstevel@tonic-gate 		else
8757c478bd9Sstevel@tonic-gate 			err_fmt = "cannot find ufs mount point for \"%s\"\n";
8767c478bd9Sstevel@tonic-gate 	} else if (new_cc.cf_type == CFT_UFS) {
8777c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry");
8787c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special");
8797c478bd9Sstevel@tonic-gate 		while (*(sfile + 1) == '/') sfile++;
8807c478bd9Sstevel@tonic-gate 		src = sfile + strlen(ent.mnt_mountp);
8817c478bd9Sstevel@tonic-gate 		while (*src == '/') src++;
8827c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_path, src, "statefile path");
8837c478bd9Sstevel@tonic-gate 	} else
8847c478bd9Sstevel@tonic-gate 		err_fmt = "statefile device \"%s\" is a mounted filesystem\n";
8857c478bd9Sstevel@tonic-gate 	if (err_fmt)
8867c478bd9Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
8877c478bd9Sstevel@tonic-gate 	return (err_fmt != NULL);
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate /*
8927c478bd9Sstevel@tonic-gate  * Convert a Unix device to a prom device and save on success,
8937c478bd9Sstevel@tonic-gate  * log any ioctl/conversion error.
8947c478bd9Sstevel@tonic-gate  */
8957c478bd9Sstevel@tonic-gate static int
8967c478bd9Sstevel@tonic-gate utop(void)
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate 	union obpbuf {
8997c478bd9Sstevel@tonic-gate 		char	buf[OBP_MAXPATHLEN + sizeof (uint_t)];
9007c478bd9Sstevel@tonic-gate 		struct	openpromio oppio;
9017c478bd9Sstevel@tonic-gate 	};
9027c478bd9Sstevel@tonic-gate 	union obpbuf oppbuf;
9037c478bd9Sstevel@tonic-gate 	struct openpromio *opp;
9047c478bd9Sstevel@tonic-gate 	char *promdev = "/dev/openprom";
9057c478bd9Sstevel@tonic-gate 	int fd, upval;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	if ((fd = open(promdev, O_RDONLY)) == -1) {
9087c478bd9Sstevel@tonic-gate 		mesg(MERR, open_fmt, promdev, strerror(errno));
9097c478bd9Sstevel@tonic-gate 		return (NOUP);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	opp = &oppbuf.oppio;
9137c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPATHLEN;
9147c478bd9Sstevel@tonic-gate 	strcpy_limit(opp->oprom_array, new_cc.cf_devfs,
9157c478bd9Sstevel@tonic-gate 	    OBP_MAXPATHLEN, "statefile device");
9167c478bd9Sstevel@tonic-gate 	upval = ioctl(fd, OPROMDEV2PROMNAME, opp);
9177c478bd9Sstevel@tonic-gate 	(void) close(fd);
9187c478bd9Sstevel@tonic-gate 	if (upval == OKUP)
9197c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_dev_prom, opp->oprom_array, "prom device");
9207c478bd9Sstevel@tonic-gate 	else {
9217c478bd9Sstevel@tonic-gate 		openlog("pmconfig", 0, LOG_DAEMON);
9227c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE,
9237c478bd9Sstevel@tonic-gate 		    gettext("cannot convert \"%s\" to prom device"),
9247c478bd9Sstevel@tonic-gate 		    new_cc.cf_devfs);
9257c478bd9Sstevel@tonic-gate 		closelog();
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	return (upval);
9297c478bd9Sstevel@tonic-gate }
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate /*
9337c478bd9Sstevel@tonic-gate  * Check for a valid statefile pathname, inode and mount status.
9347c478bd9Sstevel@tonic-gate  */
9357c478bd9Sstevel@tonic-gate int
9367c478bd9Sstevel@tonic-gate sfpath(void)
9377c478bd9Sstevel@tonic-gate {
9387c478bd9Sstevel@tonic-gate 	static int statefile;
9397c478bd9Sstevel@tonic-gate 	char *err_fmt = NULL;
9407c478bd9Sstevel@tonic-gate 	char *sfile, *sp, ch;
9417c478bd9Sstevel@tonic-gate 	struct stat stbuf;
9427c478bd9Sstevel@tonic-gate 	int dir = 0;
9437c478bd9Sstevel@tonic-gate 	dev_t dev;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	if (statefile) {
9467c478bd9Sstevel@tonic-gate 		mesg(MERR, "ignored redundant statefile entry\n");
9477c478bd9Sstevel@tonic-gate 		return (OKUP);
9487c478bd9Sstevel@tonic-gate 	} else if (ua_err) {
9497c478bd9Sstevel@tonic-gate 		if (ua_err != ENOTSUP)
9507c478bd9Sstevel@tonic-gate 			mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n",
9517c478bd9Sstevel@tonic-gate 			    strerror(ua_err));
9527c478bd9Sstevel@tonic-gate 		return (NOUP);
9537c478bd9Sstevel@tonic-gate 	}
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	/*
9567c478bd9Sstevel@tonic-gate 	 * Check for an absolute path and trim any trailing '/'.
9577c478bd9Sstevel@tonic-gate 	 */
9587c478bd9Sstevel@tonic-gate 	sfile = LINEARG(1);
9597c478bd9Sstevel@tonic-gate 	if (*sfile != '/') {
9607c478bd9Sstevel@tonic-gate 		mesg(MERR, "statefile requires an absolute path\n");
9617c478bd9Sstevel@tonic-gate 		return (NOUP);
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 	for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--)
9647c478bd9Sstevel@tonic-gate 		*sp = '\0';
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	/*
9677c478bd9Sstevel@tonic-gate 	 * If the statefile doesn't exist, the leading path must be a dir.
9687c478bd9Sstevel@tonic-gate 	 */
9697c478bd9Sstevel@tonic-gate 	if (stat(sfile, &stbuf) == -1) {
9707c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
9717c478bd9Sstevel@tonic-gate 			dir = 1;
9727c478bd9Sstevel@tonic-gate 			if ((sp = strrchr(sfile, '/')) == sfile)
9737c478bd9Sstevel@tonic-gate 				sp++;
9747c478bd9Sstevel@tonic-gate 			ch = *sp;
9757c478bd9Sstevel@tonic-gate 			*sp = '\0';
9767c478bd9Sstevel@tonic-gate 			if (stat(sfile, &stbuf) == -1)
9777c478bd9Sstevel@tonic-gate 				err_fmt = stat_fmt;
9787c478bd9Sstevel@tonic-gate 			*sp = ch;
9797c478bd9Sstevel@tonic-gate 		} else
9807c478bd9Sstevel@tonic-gate 			err_fmt = stat_fmt;
9817c478bd9Sstevel@tonic-gate 		if (err_fmt) {
9827c478bd9Sstevel@tonic-gate 			mesg(MERR, err_fmt, sfile, strerror(errno));
9837c478bd9Sstevel@tonic-gate 			return (NOUP);
9847c478bd9Sstevel@tonic-gate 		}
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	/*
9887c478bd9Sstevel@tonic-gate 	 * Check for regular/dir/block types, set cf_type and dev.
9897c478bd9Sstevel@tonic-gate 	 */
9907c478bd9Sstevel@tonic-gate 	if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) {
9917c478bd9Sstevel@tonic-gate 		new_cc.cf_type = CFT_UFS;
9927c478bd9Sstevel@tonic-gate 		dev = stbuf.st_dev;
9937c478bd9Sstevel@tonic-gate 	} else if (S_ISBLK(stbuf.st_mode)) {
9947c478bd9Sstevel@tonic-gate 		if (minor(stbuf.st_rdev) != 2) {
9957c478bd9Sstevel@tonic-gate 			new_cc.cf_type = CFT_SPEC;
9967c478bd9Sstevel@tonic-gate 			dev = stbuf.st_rdev;
9977c478bd9Sstevel@tonic-gate 		} else
9987c478bd9Sstevel@tonic-gate 			err_fmt = "statefile device cannot be slice 2 (%s)\n"
9997c478bd9Sstevel@tonic-gate 			    "would clobber the disk label and boot-block\n";
10007c478bd9Sstevel@tonic-gate 	} else
10017c478bd9Sstevel@tonic-gate 		err_fmt = "bad file type for \"%s\"\n"
10027c478bd9Sstevel@tonic-gate 		    "statefile must be a regular file or block device\n";
10037c478bd9Sstevel@tonic-gate 	if (err_fmt) {
10047c478bd9Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
10057c478bd9Sstevel@tonic-gate 		return (NOUP);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)) || utop())
10097c478bd9Sstevel@tonic-gate 		return (NOUP);
10107c478bd9Sstevel@tonic-gate 	new_cc.cf_magic = CPR_CONFIG_MAGIC;
10117c478bd9Sstevel@tonic-gate 	statefile = 1;
10127c478bd9Sstevel@tonic-gate 	return (OKUP);
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate #endif /* sparc */
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate /*
1018c42872d4Smh27603  * Common function to set a system or cpu threshold.
10197c478bd9Sstevel@tonic-gate  */
1020c42872d4Smh27603 static int
1021c42872d4Smh27603 cmnthr(int req)
10227c478bd9Sstevel@tonic-gate {
10237c478bd9Sstevel@tonic-gate 	int value, nerr = 0, upval = OKUP;
10247c478bd9Sstevel@tonic-gate 	char *thresh = LINEARG(1);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	if (strcmp(thresh, always_on) == 0)
10277c478bd9Sstevel@tonic-gate 		value = INT_MAX;
10287c478bd9Sstevel@tonic-gate 	else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) {
10297c478bd9Sstevel@tonic-gate 		mesg(MERR, "%s must be a positive value\n", LINEARG(0));
10307c478bd9Sstevel@tonic-gate 		upval = NOUP;
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 	if (upval == OKUP)
1033c42872d4Smh27603 		(void) ioctl(pm_fd, req, value);
10347c478bd9Sstevel@tonic-gate 	return (upval);
10357c478bd9Sstevel@tonic-gate }
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 
1038c42872d4Smh27603 /*
1039c42872d4Smh27603  * Try setting system threshold.
1040c42872d4Smh27603  */
1041c42872d4Smh27603 int
1042c42872d4Smh27603 systhr(void)
1043c42872d4Smh27603 {
1044c42872d4Smh27603 	return (cmnthr(PM_SET_SYSTEM_THRESHOLD));
1045c42872d4Smh27603 }
1046c42872d4Smh27603 
1047c42872d4Smh27603 
1048c42872d4Smh27603 /*
1049c42872d4Smh27603  * Try setting cpu threshold.
1050c42872d4Smh27603  */
1051c42872d4Smh27603 int
1052c42872d4Smh27603 cputhr(void)
1053c42872d4Smh27603 {
1054c42872d4Smh27603 	return (cmnthr(PM_SET_CPU_THRESHOLD));
1055c42872d4Smh27603 }
1056c42872d4Smh27603 
1057c42872d4Smh27603 
10587c478bd9Sstevel@tonic-gate int
10597c478bd9Sstevel@tonic-gate tchars(void)
10607c478bd9Sstevel@tonic-gate {
10617c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.ttychars_thold));
10627c478bd9Sstevel@tonic-gate }
1063