xref: /onnv-gate/usr/src/cmd/power/handlers.c (revision 5295)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53028Smh27603  * Common Development and Distribution License (the "License").
63028Smh27603  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*5295Srandyf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include "pmconfig.h"
290Sstevel@tonic-gate #include <sys/mkdev.h>
300Sstevel@tonic-gate #include <sys/syslog.h>
310Sstevel@tonic-gate #include <sys/openpromio.h>
320Sstevel@tonic-gate #include <sys/mnttab.h>
330Sstevel@tonic-gate #include <syslog.h>
340Sstevel@tonic-gate #include <stdlib.h>
35*5295Srandyf #include <sys/pm.h>
36*5295Srandyf #include <kstat.h>
37*5295Srandyf #include <sys/smbios.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #define	STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str)
410Sstevel@tonic-gate #define	LASTBYTE(str) (str + strlen(str) - 1)
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static char nerr_fmt[] = "number is out of range (%s)\n";
440Sstevel@tonic-gate static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n";
450Sstevel@tonic-gate static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n";
460Sstevel@tonic-gate static char bad_thresh_fmt[] = "bad threshold(s)\n";
470Sstevel@tonic-gate static char stat_fmt[] = "cannot stat \"%s\", %s\n";
480Sstevel@tonic-gate static char always_on[] = "always-on";
490Sstevel@tonic-gate 
50*5295Srandyf #define	PM_DEFAULT_ALGORITHM -1
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * When lines in a config file (usually "/etc/power.conf") start with
530Sstevel@tonic-gate  * a recognized keyword, a "handler" routine is called for specific
540Sstevel@tonic-gate  * CPR or PM -related action(s).  Each routine returns a status code
550Sstevel@tonic-gate  * indicating whether all tasks were successful; if any errors occured,
560Sstevel@tonic-gate  * future CPR or PM updates are skipped.  Following are the handler
570Sstevel@tonic-gate  * routines for all keywords:
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 
61*5295Srandyf static char pm_cmd_string[32];
62*5295Srandyf 
63*5295Srandyf static char *
64*5295Srandyf pm_map(int cmd)
65*5295Srandyf {
66*5295Srandyf 	pm_req_t req;
67*5295Srandyf 
68*5295Srandyf 	req.value = cmd;
69*5295Srandyf 	req.data = (void *)pm_cmd_string;
70*5295Srandyf 	req.datasize = sizeof (pm_cmd_string);
71*5295Srandyf 
72*5295Srandyf 	if (ioctl(pm_fd, PM_GET_CMD_NAME, &req) < 0) {
73*5295Srandyf 		perror("PM cmd name lookup:");
74*5295Srandyf 		return ("??");
75*5295Srandyf 	}
76*5295Srandyf 	return (pm_cmd_string);
77*5295Srandyf }
78*5295Srandyf 
79*5295Srandyf static int
80*5295Srandyf isonlist(char *listname, const char *man, const char *prod)
81*5295Srandyf {
82*5295Srandyf 	pm_searchargs_t sl;
83*5295Srandyf 	int ret;
84*5295Srandyf 
85*5295Srandyf 	sl.pms_listname = listname;
86*5295Srandyf 	sl.pms_manufacturer = (char *)man;
87*5295Srandyf 	sl.pms_product = (char *)prod;
88*5295Srandyf 	ret = ioctl(pm_fd, PM_SEARCH_LIST, &sl);
89*5295Srandyf 	mesg(MDEBUG, "PM_SEARCH_LIST %s for %s,%s returns %d\n",
90*5295Srandyf 	    listname, man, prod, ret);
91*5295Srandyf 	return (ret == 0);
92*5295Srandyf }
93*5295Srandyf 
94*5295Srandyf static int
95*5295Srandyf do_ioctl(int ioctl_cmd, char *keyword, char *behavior, int suppress)
96*5295Srandyf {
97*5295Srandyf 	mesg(MDEBUG, "doing ioctl %s for %s ", pm_map(ioctl_cmd), keyword);
98*5295Srandyf 	if (ioctl(pm_fd, ioctl_cmd, NULL) == -1) {
99*5295Srandyf 		int suppressed = suppress == -1 || suppress == errno;
100*5295Srandyf 		mesg(MDEBUG, "%s failed, %s (%ssuppressed)\n", behavior,
101*5295Srandyf 		    strerror(errno), (suppressed ? "" : "not "));
102*5295Srandyf 		if (!suppressed) {
103*5295Srandyf 			mesg(MERR, "%s %s failed, %s\n", keyword, behavior,
104*5295Srandyf 			    strerror(errno));
105*5295Srandyf 			return (NOUP);
106*5295Srandyf 		} else {
107*5295Srandyf 			return (OKUP);
108*5295Srandyf 		}
109*5295Srandyf 	}
110*5295Srandyf 	mesg(MDEBUG, "succeeded\n");
111*5295Srandyf 	return (OKUP);
112*5295Srandyf }
113*5295Srandyf 
1140Sstevel@tonic-gate /*
1153028Smh27603  * Check for valid cpupm behavior and communicate it to the kernel.
1163028Smh27603  */
1173028Smh27603 int
1183028Smh27603 cpupm(void)
1193028Smh27603 {
1203028Smh27603 	struct btoc {
1213028Smh27603 		char *behavior;
1223028Smh27603 		int cmd;
1233028Smh27603 		int Errno;
1243028Smh27603 	};
1253028Smh27603 	static struct btoc blist[] = {
1263028Smh27603 		"disable",	PM_STOP_CPUPM, EINVAL,
1273028Smh27603 		"enable",	PM_START_CPUPM, EBUSY,
1283028Smh27603 		NULL,		0, 0
1293028Smh27603 	};
1303028Smh27603 	struct btoc *bp;
1313028Smh27603 	char *behavior;
1323028Smh27603 
1333028Smh27603 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
1343028Smh27603 		if (strcmp(behavior, bp->behavior) == 0)
1353028Smh27603 			break;
1363028Smh27603 	}
1373028Smh27603 	if (bp->cmd == 0) {
1383028Smh27603 		mesg(MERR, "invalid cpupm behavior \"%s\"\n", behavior);
1393028Smh27603 		return (NOUP);
1403028Smh27603 	}
1413028Smh27603 	if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
1423028Smh27603 		mesg(MERR, "cpupm %s failed, %s\n",
1433028Smh27603 		    behavior, strerror(errno));
1443028Smh27603 		return (NOUP);
1453028Smh27603 	}
1463028Smh27603 	return (OKUP);
1473028Smh27603 }
1483028Smh27603 
1493028Smh27603 
1503028Smh27603 /*
151*5295Srandyf  * Two decisions are identical except for the list names and ioctl commands
152*5295Srandyf  * inputs: whitelist, blacklist, yes, no
153*5295Srandyf  * if (! ("S3" kstat exists))
154*5295Srandyf  *	return (no)
155*5295Srandyf  * if (SystemInformation.Manufacturer == "Sun Microsystems" &&
156*5295Srandyf  *    (Pref_PM_Profile == Workstation || Pref_PM_Profile == Desktop)) {
157*5295Srandyf  *	if (platform on blacklist)
158*5295Srandyf  *		return (no)
159*5295Srandyf  *	return (yes)
160*5295Srandyf  * } else {
161*5295Srandyf  *	if (platform on whitelist)
162*5295Srandyf  *		return (yes)
163*5295Srandyf  *	return (no)
164*5295Srandyf  * }
165*5295Srandyf  */
166*5295Srandyf 
167*5295Srandyf int
168*5295Srandyf S3_helper(char *whitelist, char *blacklist, int yes, int no, char *keyword,
169*5295Srandyf 	char *behavior, int *didyes, int suppress)
170*5295Srandyf {
171*5295Srandyf 	int oflags = SMB_O_NOCKSUM | SMB_O_NOVERS;
172*5295Srandyf 	smbios_hdl_t *shp;
173*5295Srandyf 	smbios_system_t sys;
174*5295Srandyf 	id_t id;
175*5295Srandyf 	int ret;
176*5295Srandyf 	kstat_ctl_t *kc;
177*5295Srandyf 	kstat_t *ksp;
178*5295Srandyf 	kstat_named_t *dp;
179*5295Srandyf 	smbios_info_t info;
180*5295Srandyf 	int preferred_pm_profile = 0;
181*5295Srandyf 	char yesstr[32], nostr[32];	/* DEBUG */
182*5295Srandyf 
183*5295Srandyf 	*didyes = 0;
184*5295Srandyf 
185*5295Srandyf 	strncpy(yesstr, pm_map(yes), sizeof (yesstr));
186*5295Srandyf 	strncpy(nostr, pm_map(no), sizeof (nostr));
187*5295Srandyf 	mesg(MDEBUG, "S3_helper(%s, %s, %s, %s, %s, %s)\n", whitelist,
188*5295Srandyf 	    blacklist, yesstr, nostr, keyword, behavior);
189*5295Srandyf 	if ((kc = kstat_open()) == NULL) {
190*5295Srandyf 		mesg(MDEBUG, "kstat_open failed\n");
191*5295Srandyf 		return (OKUP);
192*5295Srandyf 	}
193*5295Srandyf 	ksp = kstat_lookup(kc, "acpi", -1, "acpi");
194*5295Srandyf 	if (ksp == NULL) {
195*5295Srandyf 		mesg(MDEBUG, "kstat_lookup 'acpi', -1, 'acpi' failed\n");
196*5295Srandyf 		kstat_close(kc);
197*5295Srandyf 		return (OKUP);
198*5295Srandyf 	}
199*5295Srandyf 	(void) kstat_read(kc, ksp,  NULL);
200*5295Srandyf 	dp = kstat_data_lookup(ksp, "S3");
201*5295Srandyf 	if (dp == NULL || dp->value.l == 0) {
202*5295Srandyf 		mesg(MDEBUG, "kstat_data_lookup 'S3' fails\n");
203*5295Srandyf 		if (dp != NULL)
204*5295Srandyf 			mesg(MDEBUG, "value.l %lx\n", dp->value.l);
205*5295Srandyf 		kstat_close(kc);
206*5295Srandyf 		return (do_ioctl(no, keyword, behavior, suppress));
207*5295Srandyf 	}
208*5295Srandyf 	mesg(MDEBUG, "kstat indicates S3 support (%lx)\n", dp->value.l);
209*5295Srandyf 
210*5295Srandyf 	if (!whitelist_only) {
211*5295Srandyf 		/*
212*5295Srandyf 		 * We still have an ACPI ksp, search it again for
213*5295Srandyf 		 * 'preferred_pm_profile' (needs to be valid if we don't
214*5295Srandyf 		 * aren't only using a whitelist).
215*5295Srandyf 		 */
216*5295Srandyf 		dp = kstat_data_lookup(ksp, "preferred_pm_profile");
217*5295Srandyf 		if (dp == NULL) {
218*5295Srandyf 			mesg(MDEBUG, "kstat_data_lookup 'ppmp fails\n");
219*5295Srandyf 			kstat_close(kc);
220*5295Srandyf 			return (do_ioctl(no, keyword, behavior, suppress));
221*5295Srandyf 		}
222*5295Srandyf 		mesg(MDEBUG, "kstat indicates preffered_pm_profile is %lx\n",
223*5295Srandyf 		    dp->value.l);
224*5295Srandyf 		preferred_pm_profile = dp->value.l;
225*5295Srandyf 	}
226*5295Srandyf 	kstat_close(kc);
227*5295Srandyf 
228*5295Srandyf 	if ((shp = smbios_open(NULL,
229*5295Srandyf 	    SMB_VERSION, oflags, &ret)) == NULL) {
230*5295Srandyf 		/* we promised not to complain */
231*5295Srandyf 		/* we bail leaving it to the kernel default */
232*5295Srandyf 		mesg(MDEBUG, "smbios_open failed %d\n", errno);
233*5295Srandyf 		return (OKUP);
234*5295Srandyf 	}
235*5295Srandyf 	if ((id = smbios_info_system(shp, &sys)) == SMB_ERR) {
236*5295Srandyf 		mesg(MDEBUG, "smbios_info_system failed %d\n", errno);
237*5295Srandyf 		smbios_close(shp);
238*5295Srandyf 		return (OKUP);
239*5295Srandyf 	}
240*5295Srandyf 	if (smbios_info_common(shp, id, &info) == SMB_ERR) {
241*5295Srandyf 		mesg(MDEBUG, "smbios_info_common failed %d\n", errno);
242*5295Srandyf 		smbios_close(shp);
243*5295Srandyf 		return (OKUP);
244*5295Srandyf 	}
245*5295Srandyf 	mesg(MDEBUG, "Manufacturer: %s\n", info.smbi_manufacturer);
246*5295Srandyf 	mesg(MDEBUG, "Product: %s\n", info.smbi_product);
247*5295Srandyf 	smbios_close(shp);
248*5295Srandyf 
249*5295Srandyf 	if (!whitelist_only) {
250*5295Srandyf #define	PPP_DESKTOP 1
251*5295Srandyf #define	PPP_WORKSTATION 3
252*5295Srandyf 		if (strcmp(info.smbi_manufacturer, "Sun Microsystems") == 0 &&
253*5295Srandyf 		    (preferred_pm_profile == PPP_DESKTOP ||
254*5295Srandyf 		    preferred_pm_profile == PPP_WORKSTATION)) {
255*5295Srandyf 			if (isonlist(blacklist,
256*5295Srandyf 			    info.smbi_manufacturer, info.smbi_product)) {
257*5295Srandyf 				return (do_ioctl(no, keyword, behavior,
258*5295Srandyf 				    suppress));
259*5295Srandyf 			} else {
260*5295Srandyf 				ret = do_ioctl(yes, keyword, behavior,
261*5295Srandyf 				    suppress);
262*5295Srandyf 				*didyes = (ret == OKUP);
263*5295Srandyf 				return (ret);
264*5295Srandyf 			}
265*5295Srandyf 		}
266*5295Srandyf 	}
267*5295Srandyf 	if (isonlist(whitelist,
268*5295Srandyf 	    info.smbi_manufacturer, info.smbi_product)) {
269*5295Srandyf 		ret = do_ioctl(yes, keyword, behavior, suppress);
270*5295Srandyf 		*didyes = (ret == OKUP);
271*5295Srandyf 		return (ret);
272*5295Srandyf 	} else {
273*5295Srandyf 		return (do_ioctl(no, keyword, behavior, suppress));
274*5295Srandyf 	}
275*5295Srandyf }
276*5295Srandyf 
277*5295Srandyf int
278*5295Srandyf S3sup(void)	/* S3-support keyword handler */
279*5295Srandyf {
280*5295Srandyf 	struct btoc {
281*5295Srandyf 		char *behavior;
282*5295Srandyf 		int cmd;
283*5295Srandyf 	};
284*5295Srandyf 	static struct btoc blist[] = {
285*5295Srandyf 		"default",	PM_DEFAULT_ALGORITHM,
286*5295Srandyf 		"enable",	PM_ENABLE_S3,
287*5295Srandyf 		"disable",	PM_DISABLE_S3,
288*5295Srandyf 		NULL,		0
289*5295Srandyf 	};
290*5295Srandyf 	struct btoc *bp;
291*5295Srandyf 	char *behavior;
292*5295Srandyf 	int dontcare;
293*5295Srandyf 
294*5295Srandyf 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
295*5295Srandyf 		if (strcmp(behavior, bp->behavior) == 0)
296*5295Srandyf 			break;
297*5295Srandyf 	}
298*5295Srandyf 	if (bp->cmd == 0) {
299*5295Srandyf 		mesg(MERR, "invalid S3-support behavior \"%s\"\n", behavior);
300*5295Srandyf 		return (NOUP);
301*5295Srandyf 	}
302*5295Srandyf 
303*5295Srandyf 
304*5295Srandyf 	switch (bp->cmd) {
305*5295Srandyf 
306*5295Srandyf 	case PM_ENABLE_S3:
307*5295Srandyf 	case PM_DISABLE_S3:
308*5295Srandyf 		return (do_ioctl(bp->cmd, "S3-support", behavior, EBUSY));
309*5295Srandyf 
310*5295Srandyf 	case PM_DEFAULT_ALGORITHM:
311*5295Srandyf 		/*
312*5295Srandyf 		 * we suppress errors in the "default" case because we
313*5295Srandyf 		 * already did an invisible default call, so we know we'll
314*5295Srandyf 		 * get EBUSY
315*5295Srandyf 		 */
316*5295Srandyf 		return (S3_helper("S3-support-enable", "S3-support-disable",
317*5295Srandyf 		    PM_ENABLE_S3, PM_DISABLE_S3, "S3-support", behavior,
318*5295Srandyf 		    &dontcare, EBUSY));
319*5295Srandyf 
320*5295Srandyf 	default:
321*5295Srandyf 		mesg(MERR, "S3-support %s failed, %s\n", behavior,
322*5295Srandyf 		    strerror(errno));
323*5295Srandyf 		return (NOUP);
324*5295Srandyf 	}
325*5295Srandyf }
326*5295Srandyf 
327*5295Srandyf /*
328*5295Srandyf  * Check for valid autoS3 behavior and save after ioctl success.
329*5295Srandyf  */
330*5295Srandyf int
331*5295Srandyf autoS3(void)
332*5295Srandyf {
333*5295Srandyf 	struct btoc {
334*5295Srandyf 		char *behavior;
335*5295Srandyf 		int cmd;
336*5295Srandyf 	};
337*5295Srandyf 	static struct btoc blist[] = {
338*5295Srandyf 		"default",	PM_DEFAULT_ALGORITHM,
339*5295Srandyf 		"disable",	PM_STOP_AUTOS3,
340*5295Srandyf 		"enable",	PM_START_AUTOS3,
341*5295Srandyf 		NULL,		0
342*5295Srandyf 	};
343*5295Srandyf 	struct btoc *bp;
344*5295Srandyf 	char *behavior;
345*5295Srandyf 	int dontcare;
346*5295Srandyf 
347*5295Srandyf 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
348*5295Srandyf 		if (strcmp(behavior, bp->behavior) == 0)
349*5295Srandyf 			break;
350*5295Srandyf 	}
351*5295Srandyf 	if (bp->cmd == 0) {
352*5295Srandyf 		mesg(MERR, "invalid autoS3 behavior \"%s\"\n", behavior);
353*5295Srandyf 		return (NOUP);
354*5295Srandyf 	}
355*5295Srandyf 
356*5295Srandyf 	switch (bp->cmd) {
357*5295Srandyf 	default:
358*5295Srandyf 		mesg(MERR, "autoS3 %s failed, %s\n",
359*5295Srandyf 		    behavior, strerror(errno));
360*5295Srandyf 		mesg(MDEBUG, "unknown command\n", bp->cmd);
361*5295Srandyf 		return (OKUP);
362*5295Srandyf 
363*5295Srandyf 	case PM_STOP_AUTOS3:
364*5295Srandyf 	case PM_START_AUTOS3:
365*5295Srandyf 		return (do_ioctl(bp->cmd, "autoS3", behavior, EBUSY));
366*5295Srandyf 
367*5295Srandyf 	case PM_DEFAULT_ALGORITHM:
368*5295Srandyf 		return (S3_helper("S3-autoenable", "S3-autodisable",
369*5295Srandyf 		    PM_START_AUTOS3, PM_STOP_AUTOS3, "autoS3", behavior,
370*5295Srandyf 		    &dontcare, EBUSY));
371*5295Srandyf 	}
372*5295Srandyf }
373*5295Srandyf 
374*5295Srandyf 
375*5295Srandyf /*
3760Sstevel@tonic-gate  * Check for valid autopm behavior and save after ioctl success.
3770Sstevel@tonic-gate  */
3780Sstevel@tonic-gate int
3790Sstevel@tonic-gate autopm(void)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	struct btoc {
3820Sstevel@tonic-gate 		char *behavior;
3830Sstevel@tonic-gate 		int cmd, Errno, isdef;
3840Sstevel@tonic-gate 	};
3850Sstevel@tonic-gate 	static struct btoc blist[] = {
386*5295Srandyf 		"default",	PM_START_PM,	-1,	1,
3870Sstevel@tonic-gate 		"disable",	PM_STOP_PM,	EINVAL,	0,
3880Sstevel@tonic-gate 		"enable",	PM_START_PM,	EBUSY,	0,
3890Sstevel@tonic-gate 		NULL,		0,		0,	0,
3900Sstevel@tonic-gate 	};
3910Sstevel@tonic-gate 	struct btoc *bp;
3920Sstevel@tonic-gate 	char *behavior;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
3950Sstevel@tonic-gate 		if (strcmp(behavior, bp->behavior) == 0)
3960Sstevel@tonic-gate 			break;
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 	if (bp->cmd == 0) {
3990Sstevel@tonic-gate 		mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior);
4000Sstevel@tonic-gate 		return (NOUP);
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	/*
4040Sstevel@tonic-gate 	 * for "default" behavior, do not enable autopm if not ESTAR_V3
4050Sstevel@tonic-gate 	 */
406*5295Srandyf #if defined(__sparc)
4070Sstevel@tonic-gate 	if (!bp->isdef || (estar_vers == ESTAR_V3)) {
4080Sstevel@tonic-gate 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
4090Sstevel@tonic-gate 			mesg(MERR, "autopm %s failed, %s\n",
4100Sstevel@tonic-gate 			    behavior, strerror(errno));
4110Sstevel@tonic-gate 			return (NOUP);
4120Sstevel@tonic-gate 		}
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 	(void) strcpy(new_cc.apm_behavior, behavior);
4150Sstevel@tonic-gate 	return (OKUP);
416*5295Srandyf #endif
417*5295Srandyf #if defined(__x86)
418*5295Srandyf 	if (!bp->isdef) {
419*5295Srandyf 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
420*5295Srandyf 			mesg(MERR, "autopm %s failed, %s\n",
421*5295Srandyf 			    behavior, strerror(errno));
422*5295Srandyf 			return (NOUP);
423*5295Srandyf 		}
424*5295Srandyf 		mesg(MDEBUG, "autopm %s succeeded\n", behavior);
425*5295Srandyf 
426*5295Srandyf 		return (OKUP);
427*5295Srandyf 	} else {
428*5295Srandyf 		int didenable;
429*5295Srandyf 		int ret = S3_helper("autopm-enable", "autopm-disable",
430*5295Srandyf 		    PM_START_PM, PM_STOP_PM, "autopm", behavior, &didenable,
431*5295Srandyf 		    bp->Errno);
432*5295Srandyf 		if (didenable) {
433*5295Srandyf 			/* tell powerd to attach all devices */
434*5295Srandyf 			new_cc.is_autopm_default = 1;
435*5295Srandyf 			(void) strcpy(new_cc.apm_behavior, behavior);
436*5295Srandyf 		}
437*5295Srandyf 		return (ret);
438*5295Srandyf 	}
439*5295Srandyf #endif
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate static int
4440Sstevel@tonic-gate gethm(char *src, int *hour, int *min)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	if (sscanf(src, "%d:%d", hour, min) != 2) {
4470Sstevel@tonic-gate 		mesg(MERR, "bad time format (%s)\n", src);
4480Sstevel@tonic-gate 		return (-1);
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 	return (0);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate static void
4550Sstevel@tonic-gate strcpy_limit(char *dst, char *src, size_t limit, char *info)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 	if (strlcpy(dst, src, limit) >= limit)
4580Sstevel@tonic-gate 		mesg(MEXIT, "%s is too long (%s)\n", info, src);
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate  * Convert autoshutdown idle and start/finish times;
4640Sstevel@tonic-gate  * check and record autoshutdown behavior.
4650Sstevel@tonic-gate  */
4660Sstevel@tonic-gate int
4670Sstevel@tonic-gate autosd(void)
4680Sstevel@tonic-gate {
469763Smh27603 	char **bp, *behavior;
470763Smh27603 	char *unrec = gettext("unrecognized autoshutdown behavior");
4710Sstevel@tonic-gate 	static char *blist[] = {
4720Sstevel@tonic-gate 		"autowakeup", "default", "noshutdown",
4730Sstevel@tonic-gate 		"shutdown", "unconfigured", NULL
4740Sstevel@tonic-gate 	};
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	new_cc.as_idle = atoi(LINEARG(1));
4770Sstevel@tonic-gate 	if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) ||
4780Sstevel@tonic-gate 	    gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm))
4790Sstevel@tonic-gate 		return (NOUP);
480763Smh27603 	mesg(MDEBUG, "idle %d, start %d:%02d, finish %d:%02d\n",
4810Sstevel@tonic-gate 	    new_cc.as_idle, new_cc.as_sh, new_cc.as_sm,
4820Sstevel@tonic-gate 	    new_cc.as_fh, new_cc.as_fm);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	for (behavior = LINEARG(4), bp = blist; *bp; bp++) {
4850Sstevel@tonic-gate 		if (strcmp(behavior, *bp) == 0)
4860Sstevel@tonic-gate 			break;
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 	if (*bp == NULL) {
4890Sstevel@tonic-gate 		mesg(MERR, "%s: \"%s\"\n", unrec, behavior);
4900Sstevel@tonic-gate 		return (NOUP);
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 	STRCPYLIM(new_cc.as_behavior, *bp, unrec);
4930Sstevel@tonic-gate 	return (OKUP);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate  * Check for a real device and try to resolve to a full path.
4990Sstevel@tonic-gate  * The orig/resolved path may be modified into a prom pathname,
5000Sstevel@tonic-gate  * and an allocated copy of the result is stored at *destp;
5010Sstevel@tonic-gate  * the caller will need to free that space.  Returns 1 for any
5020Sstevel@tonic-gate  * error, otherwise 0; also sets *errp after an alloc error.
5030Sstevel@tonic-gate  */
5040Sstevel@tonic-gate static int
5050Sstevel@tonic-gate devpath(char **destp, char *src, int *errp)
5060Sstevel@tonic-gate {
5070Sstevel@tonic-gate 	struct stat stbuf;
5080Sstevel@tonic-gate 	char buf[PATH_MAX];
5090Sstevel@tonic-gate 	char *cp, *dstr;
5100Sstevel@tonic-gate 	int devok, dcs = 0;
5110Sstevel@tonic-gate 	size_t len;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	/*
5140Sstevel@tonic-gate 	 * When there's a real device, try to resolve the path
5150Sstevel@tonic-gate 	 * and trim the leading "/devices" component.
5160Sstevel@tonic-gate 	 */
5170Sstevel@tonic-gate 	if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) {
5180Sstevel@tonic-gate 		if (realpath(src, buf) == NULL) {
5190Sstevel@tonic-gate 			mesg(MERR, "realpath cannot resolve \"%s\"\n",
5200Sstevel@tonic-gate 			    src, strerror(errno));
5210Sstevel@tonic-gate 			return (1);
5220Sstevel@tonic-gate 		}
5230Sstevel@tonic-gate 		src = buf;
5240Sstevel@tonic-gate 		dstr = "/devices";
5250Sstevel@tonic-gate 		len = strlen(dstr);
5260Sstevel@tonic-gate 		dcs = (strncmp(src, dstr, len) == 0);
5270Sstevel@tonic-gate 		if (dcs)
5280Sstevel@tonic-gate 			src += len;
5290Sstevel@tonic-gate 	} else
5300Sstevel@tonic-gate 		mesg(MDEBUG, stat_fmt, src, strerror(errno));
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/*
5330Sstevel@tonic-gate 	 * When the path has ":anything", display an error for
5340Sstevel@tonic-gate 	 * a non-device or truncate a resolved+modifed path.
5350Sstevel@tonic-gate 	 */
5360Sstevel@tonic-gate 	if (cp = strchr(src, ':')) {
5370Sstevel@tonic-gate 		if (devok == 0) {
5380Sstevel@tonic-gate 			mesg(MERR, "physical path may not contain "
5390Sstevel@tonic-gate 			    "a minor string (%s)\n", src);
5400Sstevel@tonic-gate 			return (1);
5410Sstevel@tonic-gate 		} else if (dcs)
5420Sstevel@tonic-gate 			*cp = '\0';
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	if ((*destp = strdup(src)) == NULL) {
5460Sstevel@tonic-gate 		*errp = NOUP;
5470Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, src, strerror(errno));
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate 	return (*destp == NULL);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate /*
5540Sstevel@tonic-gate  * Call pm ioctl request(s) to set property/device dependencies.
5550Sstevel@tonic-gate  */
5560Sstevel@tonic-gate static int
5570Sstevel@tonic-gate dev_dep_common(int isprop)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate 	int cmd, argn, upval = OKUP;
5600Sstevel@tonic-gate 	char *src, *first, **destp;
5610Sstevel@tonic-gate 	pm_req_t pmreq;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
5640Sstevel@tonic-gate 	src = LINEARG(1);
5650Sstevel@tonic-gate 	if (isprop) {
5660Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT_PROPERTY;
5670Sstevel@tonic-gate 		first = NULL;
5680Sstevel@tonic-gate 		pmreq.pmreq_kept = src;
5690Sstevel@tonic-gate 	} else {
5700Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT;
5710Sstevel@tonic-gate 		if (devpath(&first, src, &upval))
5720Sstevel@tonic-gate 			return (upval);
5730Sstevel@tonic-gate 		pmreq.pmreq_kept = first;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 	destp = &pmreq.pmreq_keeper;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	/*
5780Sstevel@tonic-gate 	 * Now loop through any dependents.
5790Sstevel@tonic-gate 	 */
5800Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
5810Sstevel@tonic-gate 		if (devpath(destp, src, &upval)) {
5820Sstevel@tonic-gate 			if (upval != OKUP)
5830Sstevel@tonic-gate 				return (upval);
5840Sstevel@tonic-gate 			break;
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 		if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) {
5870Sstevel@tonic-gate 			mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n"
5880Sstevel@tonic-gate 			    "kept \"%s\", keeper \"%s\"\n",
5890Sstevel@tonic-gate 			    cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper);
5900Sstevel@tonic-gate 			mesg(MERR, "cannot set \"%s\" dependency "
5910Sstevel@tonic-gate 			    "for \"%s\", %s\n", pmreq.pmreq_keeper,
5920Sstevel@tonic-gate 			    pmreq.pmreq_kept, strerror(errno));
5930Sstevel@tonic-gate 		}
5940Sstevel@tonic-gate 		free(*destp);
5950Sstevel@tonic-gate 		*destp = NULL;
5960Sstevel@tonic-gate 		if (upval != OKUP)
5970Sstevel@tonic-gate 			break;
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	free(first);
6010Sstevel@tonic-gate 	return (upval);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate int
6060Sstevel@tonic-gate ddprop(void)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	return (dev_dep_common(1));
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate int
6130Sstevel@tonic-gate devdep(void)
6140Sstevel@tonic-gate {
6150Sstevel@tonic-gate 	return (dev_dep_common(0));
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate /*
6200Sstevel@tonic-gate  * Convert a numeric string (with a possible trailing scaling byte)
6210Sstevel@tonic-gate  * into an integer.  Returns a converted value and *nerrp unchanged,
6220Sstevel@tonic-gate  * or 0 with *nerrp set to 1 for a conversion error.
6230Sstevel@tonic-gate  */
6240Sstevel@tonic-gate static int
6250Sstevel@tonic-gate get_scaled_value(char *str, int *nerrp)
6260Sstevel@tonic-gate {
6270Sstevel@tonic-gate 	longlong_t svalue = 0, factor = 1;
6280Sstevel@tonic-gate 	char *sp;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	errno = 0;
6310Sstevel@tonic-gate 	svalue = strtol(str, &sp, 0);
6320Sstevel@tonic-gate 	if (errno || (*str != '-' && (*str < '0' || *str > '9')))
6330Sstevel@tonic-gate 		*nerrp = 1;
6340Sstevel@tonic-gate 	else if (sp && *sp != '\0') {
6350Sstevel@tonic-gate 		if (*sp == 'h')
6360Sstevel@tonic-gate 			factor = 3600;
6370Sstevel@tonic-gate 		else if (*sp == 'm')
6380Sstevel@tonic-gate 			factor = 60;
6390Sstevel@tonic-gate 		else if (*sp != 's')
6400Sstevel@tonic-gate 			*nerrp = 1;
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 	/* any bytes following sp are ignored */
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	if (*nerrp == 0) {
6450Sstevel@tonic-gate 		svalue *= factor;
6460Sstevel@tonic-gate 		if (svalue < INT_MIN || svalue > INT_MAX)
6470Sstevel@tonic-gate 			*nerrp = 1;
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 	if (*nerrp)
6500Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, str);
6510Sstevel@tonic-gate 	mesg(MDEBUG, "got scaled value %d\n", (int)svalue);
6520Sstevel@tonic-gate 	return ((int)svalue);
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate /*
6570Sstevel@tonic-gate  * Increment the count of threshold values,
6580Sstevel@tonic-gate  * reallocate *vlistp and append another element.
6590Sstevel@tonic-gate  * Returns 1 on error, otherwise 0.
6600Sstevel@tonic-gate  */
6610Sstevel@tonic-gate static int
6620Sstevel@tonic-gate vlist_append(int **vlistp, int *vcntp, int value)
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate 	(*vcntp)++;
6650Sstevel@tonic-gate 	if (*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp)))
6660Sstevel@tonic-gate 		*(*vlistp + *vcntp - 1) = value;
6670Sstevel@tonic-gate 	else
6680Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, "threshold list", strerror(errno));
6690Sstevel@tonic-gate 	return (*vlistp == NULL);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate /*
6740Sstevel@tonic-gate  * Convert a single threshold string or paren groups of thresh's as
6750Sstevel@tonic-gate  * described below.  All thresh's are saved to an allocated list at
6760Sstevel@tonic-gate  * *vlistp; the caller will need to free that space.  On return:
6770Sstevel@tonic-gate  * *vcntp is the count of the vlist array, and vlist is either
6780Sstevel@tonic-gate  * a single thresh or N groups of thresh's with a trailing zero:
6790Sstevel@tonic-gate  * (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0.
6800Sstevel@tonic-gate  * Returns 0 when all conversions were OK, and 1 for any syntax,
6810Sstevel@tonic-gate  * conversion, or alloc error.
6820Sstevel@tonic-gate  */
6830Sstevel@tonic-gate static int
6840Sstevel@tonic-gate get_thresh(int **vlistp, int *vcntp)
6850Sstevel@tonic-gate {
6860Sstevel@tonic-gate 	int argn, value, gci, grp_cnt = 0, paren = 0, nerr = 0;
6870Sstevel@tonic-gate 	char *rp, *src;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
6900Sstevel@tonic-gate 		if (*src == LPAREN) {
6910Sstevel@tonic-gate 			gci = *vcntp;
6920Sstevel@tonic-gate 			if (nerr = vlist_append(vlistp, vcntp, 0))
6930Sstevel@tonic-gate 				break;
6940Sstevel@tonic-gate 			paren = 1;
6950Sstevel@tonic-gate 			src++;
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 		if (*(rp = LASTBYTE(src)) == RPAREN) {
6980Sstevel@tonic-gate 			if (paren) {
6990Sstevel@tonic-gate 				grp_cnt = *vcntp - gci;
7000Sstevel@tonic-gate 				*(*vlistp + gci) = grp_cnt;
7010Sstevel@tonic-gate 				paren = 0;
7020Sstevel@tonic-gate 				*rp = '\0';
7030Sstevel@tonic-gate 			} else {
7040Sstevel@tonic-gate 				nerr = 1;
7050Sstevel@tonic-gate 				break;
7060Sstevel@tonic-gate 			}
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 		value = get_scaled_value(src, &nerr);
7100Sstevel@tonic-gate 		if (nerr || (nerr = vlist_append(vlistp, vcntp, value)))
7110Sstevel@tonic-gate 			break;
7120Sstevel@tonic-gate 	}
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	if (nerr == 0 && grp_cnt)
7150Sstevel@tonic-gate 		nerr = vlist_append(vlistp, vcntp, 0);
7160Sstevel@tonic-gate 	return (nerr);
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate /*
7210Sstevel@tonic-gate  * Set device thresholds from (3) formats:
7220Sstevel@tonic-gate  * 	path	"always-on"
7230Sstevel@tonic-gate  * 	path	time-spec: [0-9]+[{h,m,s}]
7240Sstevel@tonic-gate  *	path	(ts1 ts2 ...)+
7250Sstevel@tonic-gate  */
7260Sstevel@tonic-gate int
7270Sstevel@tonic-gate devthr(void)
7280Sstevel@tonic-gate {
7290Sstevel@tonic-gate 	int cmd, upval = OKUP, nthresh = 0, *vlist = NULL;
7300Sstevel@tonic-gate 	pm_req_t pmreq;
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
7330Sstevel@tonic-gate 	if (devpath(&pmreq.physpath, LINEARG(1), &upval))
7340Sstevel@tonic-gate 		return (upval);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (strcmp(LINEARG(2), always_on) == 0) {
7370Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
7380Sstevel@tonic-gate 		pmreq.value = INT_MAX;
7390Sstevel@tonic-gate 	} else if (get_thresh(&vlist, &nthresh)) {
7400Sstevel@tonic-gate 		mesg(MERR, bad_thresh_fmt);
7410Sstevel@tonic-gate 		upval = NOUP;
7420Sstevel@tonic-gate 	} else if (nthresh == 1) {
7430Sstevel@tonic-gate 		pmreq.value = *vlist;
7440Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
7450Sstevel@tonic-gate 	} else {
7460Sstevel@tonic-gate 		pmreq.data = vlist;
7470Sstevel@tonic-gate 		pmreq.datasize = (nthresh * sizeof (*vlist));
7480Sstevel@tonic-gate 		cmd = PM_SET_COMPONENT_THRESHOLDS;
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1)
7520Sstevel@tonic-gate 		mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno));
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	free(vlist);
7550Sstevel@tonic-gate 	free(pmreq.physpath);
7560Sstevel@tonic-gate 	return (upval);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate static int
7610Sstevel@tonic-gate scan_int(char *src, int *dst)
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate 	long lval;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	errno = 0;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	lval = strtol(LINEARG(1), NULL, 0);
7680Sstevel@tonic-gate 	if (errno || lval > INT_MAX || lval < 0) {
7690Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
7700Sstevel@tonic-gate 		return (NOUP);
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	*dst = (int)lval;
7740Sstevel@tonic-gate 	return (OKUP);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate static int
7780Sstevel@tonic-gate scan_float(char *src, float *dst)
7790Sstevel@tonic-gate {
7800Sstevel@tonic-gate 	float fval;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	errno = 0;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	fval = strtof(src, NULL);
7850Sstevel@tonic-gate 	if (errno || fval < 0.0) {
7860Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
7870Sstevel@tonic-gate 		return (NOUP);
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	*dst = fval;
7910Sstevel@tonic-gate 	return (OKUP);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate int
7960Sstevel@tonic-gate dreads(void)
7970Sstevel@tonic-gate {
7980Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.diskreads_thold));
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate /*
8030Sstevel@tonic-gate  * Set pathname for idlecheck;
8040Sstevel@tonic-gate  * an overflowed pathname is treated as a fatal error.
8050Sstevel@tonic-gate  */
8060Sstevel@tonic-gate int
8070Sstevel@tonic-gate idlechk(void)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate 	STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path");
8100Sstevel@tonic-gate 	return (OKUP);
8110Sstevel@tonic-gate }
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate int
8150Sstevel@tonic-gate loadavg(void)
8160Sstevel@tonic-gate {
8170Sstevel@tonic-gate 	return (scan_float(LINEARG(1), &new_cc.loadaverage_thold));
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate int
8220Sstevel@tonic-gate nfsreq(void)
8230Sstevel@tonic-gate {
8240Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold));
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate #ifdef sparc
8290Sstevel@tonic-gate static char open_fmt[] = "cannot open \"%s\", %s\n";
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate /*
8320Sstevel@tonic-gate  * Verify the filesystem type for a regular statefile is "ufs"
8330Sstevel@tonic-gate  * or verify a block device is not in use as a mounted filesytem.
8340Sstevel@tonic-gate  * Returns 1 if any error, otherwise 0.
8350Sstevel@tonic-gate  */
8360Sstevel@tonic-gate static int
8370Sstevel@tonic-gate check_mount(char *sfile, dev_t sfdev, int ufs)
8380Sstevel@tonic-gate {
8390Sstevel@tonic-gate 	char *src, *err_fmt = NULL, *mnttab = MNTTAB;
8400Sstevel@tonic-gate 	int rgent, match = 0;
8410Sstevel@tonic-gate 	struct extmnttab ent;
8420Sstevel@tonic-gate 	FILE *fp;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
8450Sstevel@tonic-gate 		mesg(MERR, open_fmt, mnttab, strerror(errno));
8460Sstevel@tonic-gate 		return (1);
8470Sstevel@tonic-gate 	}
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	/*
8500Sstevel@tonic-gate 	 * Search for a matching dev_t;
8510Sstevel@tonic-gate 	 * ignore non-ufs filesystems for a regular statefile.
8520Sstevel@tonic-gate 	 */
8530Sstevel@tonic-gate 	while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) {
8540Sstevel@tonic-gate 		if (rgent > 0) {
8550Sstevel@tonic-gate 			mesg(MERR, "error reading \"%s\"\n", mnttab);
8560Sstevel@tonic-gate 			(void) fclose(fp);
8570Sstevel@tonic-gate 			return (1);
8580Sstevel@tonic-gate 		} else if (ufs && strcmp(ent.mnt_fstype, "ufs"))
8590Sstevel@tonic-gate 			continue;
8600Sstevel@tonic-gate 		else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) {
8610Sstevel@tonic-gate 			match = 1;
8620Sstevel@tonic-gate 			break;
8630Sstevel@tonic-gate 		}
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate 	(void) fclose(fp);
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	/*
8680Sstevel@tonic-gate 	 * No match is needed for a block device statefile,
8690Sstevel@tonic-gate 	 * a match is needed for a regular statefile.
8700Sstevel@tonic-gate 	 */
8710Sstevel@tonic-gate 	if (match == 0) {
8720Sstevel@tonic-gate 		if (new_cc.cf_type == CFT_SPEC)
8730Sstevel@tonic-gate 			STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile");
8740Sstevel@tonic-gate 		else
8750Sstevel@tonic-gate 			err_fmt = "cannot find ufs mount point for \"%s\"\n";
8760Sstevel@tonic-gate 	} else if (new_cc.cf_type == CFT_UFS) {
8770Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry");
8780Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special");
8790Sstevel@tonic-gate 		while (*(sfile + 1) == '/') sfile++;
8800Sstevel@tonic-gate 		src = sfile + strlen(ent.mnt_mountp);
8810Sstevel@tonic-gate 		while (*src == '/') src++;
8820Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_path, src, "statefile path");
8830Sstevel@tonic-gate 	} else
8840Sstevel@tonic-gate 		err_fmt = "statefile device \"%s\" is a mounted filesystem\n";
8850Sstevel@tonic-gate 	if (err_fmt)
8860Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
8870Sstevel@tonic-gate 	return (err_fmt != NULL);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate /*
8920Sstevel@tonic-gate  * Convert a Unix device to a prom device and save on success,
8930Sstevel@tonic-gate  * log any ioctl/conversion error.
8940Sstevel@tonic-gate  */
8950Sstevel@tonic-gate static int
8960Sstevel@tonic-gate utop(void)
8970Sstevel@tonic-gate {
8980Sstevel@tonic-gate 	union obpbuf {
8990Sstevel@tonic-gate 		char	buf[OBP_MAXPATHLEN + sizeof (uint_t)];
9000Sstevel@tonic-gate 		struct	openpromio oppio;
9010Sstevel@tonic-gate 	};
9020Sstevel@tonic-gate 	union obpbuf oppbuf;
9030Sstevel@tonic-gate 	struct openpromio *opp;
9040Sstevel@tonic-gate 	char *promdev = "/dev/openprom";
9050Sstevel@tonic-gate 	int fd, upval;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	if ((fd = open(promdev, O_RDONLY)) == -1) {
9080Sstevel@tonic-gate 		mesg(MERR, open_fmt, promdev, strerror(errno));
9090Sstevel@tonic-gate 		return (NOUP);
9100Sstevel@tonic-gate 	}
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	opp = &oppbuf.oppio;
9130Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPATHLEN;
9140Sstevel@tonic-gate 	strcpy_limit(opp->oprom_array, new_cc.cf_devfs,
9150Sstevel@tonic-gate 	    OBP_MAXPATHLEN, "statefile device");
9160Sstevel@tonic-gate 	upval = ioctl(fd, OPROMDEV2PROMNAME, opp);
9170Sstevel@tonic-gate 	(void) close(fd);
9180Sstevel@tonic-gate 	if (upval == OKUP)
9190Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_dev_prom, opp->oprom_array, "prom device");
9200Sstevel@tonic-gate 	else {
9210Sstevel@tonic-gate 		openlog("pmconfig", 0, LOG_DAEMON);
9220Sstevel@tonic-gate 		syslog(LOG_NOTICE,
9230Sstevel@tonic-gate 		    gettext("cannot convert \"%s\" to prom device"),
9240Sstevel@tonic-gate 		    new_cc.cf_devfs);
9250Sstevel@tonic-gate 		closelog();
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	return (upval);
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate /*
9330Sstevel@tonic-gate  * Check for a valid statefile pathname, inode and mount status.
9340Sstevel@tonic-gate  */
9350Sstevel@tonic-gate int
9360Sstevel@tonic-gate sfpath(void)
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate 	static int statefile;
9390Sstevel@tonic-gate 	char *err_fmt = NULL;
9400Sstevel@tonic-gate 	char *sfile, *sp, ch;
9410Sstevel@tonic-gate 	struct stat stbuf;
9420Sstevel@tonic-gate 	int dir = 0;
9430Sstevel@tonic-gate 	dev_t dev;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	if (statefile) {
9460Sstevel@tonic-gate 		mesg(MERR, "ignored redundant statefile entry\n");
9470Sstevel@tonic-gate 		return (OKUP);
9480Sstevel@tonic-gate 	} else if (ua_err) {
9490Sstevel@tonic-gate 		if (ua_err != ENOTSUP)
9500Sstevel@tonic-gate 			mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n",
9510Sstevel@tonic-gate 			    strerror(ua_err));
9520Sstevel@tonic-gate 		return (NOUP);
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/*
9560Sstevel@tonic-gate 	 * Check for an absolute path and trim any trailing '/'.
9570Sstevel@tonic-gate 	 */
9580Sstevel@tonic-gate 	sfile = LINEARG(1);
9590Sstevel@tonic-gate 	if (*sfile != '/') {
9600Sstevel@tonic-gate 		mesg(MERR, "statefile requires an absolute path\n");
9610Sstevel@tonic-gate 		return (NOUP);
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 	for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--)
9640Sstevel@tonic-gate 		*sp = '\0';
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	/*
9670Sstevel@tonic-gate 	 * If the statefile doesn't exist, the leading path must be a dir.
9680Sstevel@tonic-gate 	 */
9690Sstevel@tonic-gate 	if (stat(sfile, &stbuf) == -1) {
9700Sstevel@tonic-gate 		if (errno == ENOENT) {
9710Sstevel@tonic-gate 			dir = 1;
9720Sstevel@tonic-gate 			if ((sp = strrchr(sfile, '/')) == sfile)
9730Sstevel@tonic-gate 				sp++;
9740Sstevel@tonic-gate 			ch = *sp;
9750Sstevel@tonic-gate 			*sp = '\0';
9760Sstevel@tonic-gate 			if (stat(sfile, &stbuf) == -1)
9770Sstevel@tonic-gate 				err_fmt = stat_fmt;
9780Sstevel@tonic-gate 			*sp = ch;
9790Sstevel@tonic-gate 		} else
9800Sstevel@tonic-gate 			err_fmt = stat_fmt;
9810Sstevel@tonic-gate 		if (err_fmt) {
9820Sstevel@tonic-gate 			mesg(MERR, err_fmt, sfile, strerror(errno));
9830Sstevel@tonic-gate 			return (NOUP);
9840Sstevel@tonic-gate 		}
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	/*
9880Sstevel@tonic-gate 	 * Check for regular/dir/block types, set cf_type and dev.
9890Sstevel@tonic-gate 	 */
9900Sstevel@tonic-gate 	if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) {
9910Sstevel@tonic-gate 		new_cc.cf_type = CFT_UFS;
9920Sstevel@tonic-gate 		dev = stbuf.st_dev;
9930Sstevel@tonic-gate 	} else if (S_ISBLK(stbuf.st_mode)) {
9940Sstevel@tonic-gate 		if (minor(stbuf.st_rdev) != 2) {
9950Sstevel@tonic-gate 			new_cc.cf_type = CFT_SPEC;
9960Sstevel@tonic-gate 			dev = stbuf.st_rdev;
9970Sstevel@tonic-gate 		} else
9980Sstevel@tonic-gate 			err_fmt = "statefile device cannot be slice 2 (%s)\n"
9990Sstevel@tonic-gate 			    "would clobber the disk label and boot-block\n";
10000Sstevel@tonic-gate 	} else
10010Sstevel@tonic-gate 		err_fmt = "bad file type for \"%s\"\n"
10020Sstevel@tonic-gate 		    "statefile must be a regular file or block device\n";
10030Sstevel@tonic-gate 	if (err_fmt) {
10040Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
10050Sstevel@tonic-gate 		return (NOUP);
10060Sstevel@tonic-gate 	}
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)) || utop())
10090Sstevel@tonic-gate 		return (NOUP);
10100Sstevel@tonic-gate 	new_cc.cf_magic = CPR_CONFIG_MAGIC;
10110Sstevel@tonic-gate 	statefile = 1;
10120Sstevel@tonic-gate 	return (OKUP);
10130Sstevel@tonic-gate }
10140Sstevel@tonic-gate #endif /* sparc */
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate /*
10183028Smh27603  * Common function to set a system or cpu threshold.
10190Sstevel@tonic-gate  */
10203028Smh27603 static int
10213028Smh27603 cmnthr(int req)
10220Sstevel@tonic-gate {
10230Sstevel@tonic-gate 	int value, nerr = 0, upval = OKUP;
10240Sstevel@tonic-gate 	char *thresh = LINEARG(1);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	if (strcmp(thresh, always_on) == 0)
10270Sstevel@tonic-gate 		value = INT_MAX;
10280Sstevel@tonic-gate 	else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) {
10290Sstevel@tonic-gate 		mesg(MERR, "%s must be a positive value\n", LINEARG(0));
10300Sstevel@tonic-gate 		upval = NOUP;
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 	if (upval == OKUP)
10333028Smh27603 		(void) ioctl(pm_fd, req, value);
10340Sstevel@tonic-gate 	return (upval);
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 
10383028Smh27603 /*
10393028Smh27603  * Try setting system threshold.
10403028Smh27603  */
10413028Smh27603 int
10423028Smh27603 systhr(void)
10433028Smh27603 {
10443028Smh27603 	return (cmnthr(PM_SET_SYSTEM_THRESHOLD));
10453028Smh27603 }
10463028Smh27603 
10473028Smh27603 
10483028Smh27603 /*
10493028Smh27603  * Try setting cpu threshold.
10503028Smh27603  */
10513028Smh27603 int
10523028Smh27603 cputhr(void)
10533028Smh27603 {
10543028Smh27603 	return (cmnthr(PM_SET_CPU_THRESHOLD));
10553028Smh27603 }
10563028Smh27603 
10573028Smh27603 
10580Sstevel@tonic-gate int
10590Sstevel@tonic-gate tchars(void)
10600Sstevel@tonic-gate {
10610Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.ttychars_thold));
10620Sstevel@tonic-gate }
1063