xref: /titanic_54/usr/src/cmd/power/handlers.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "pmconfig.h"
30*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/openpromio.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
34*7c478bd9Sstevel@tonic-gate #include <syslog.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #define	STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str)
39*7c478bd9Sstevel@tonic-gate #define	LASTBYTE(str) (str + strlen(str) - 1)
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static char nerr_fmt[] = "number is out of range (%s)\n";
42*7c478bd9Sstevel@tonic-gate static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n";
43*7c478bd9Sstevel@tonic-gate static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n";
44*7c478bd9Sstevel@tonic-gate static char bad_thresh_fmt[] = "bad threshold(s)\n";
45*7c478bd9Sstevel@tonic-gate static char stat_fmt[] = "cannot stat \"%s\", %s\n";
46*7c478bd9Sstevel@tonic-gate static char always_on[] = "always-on";
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate  * When lines in a config file (usually "/etc/power.conf") start with
50*7c478bd9Sstevel@tonic-gate  * a recognized keyword, a "handler" routine is called for specific
51*7c478bd9Sstevel@tonic-gate  * CPR or PM -related action(s).  Each routine returns a status code
52*7c478bd9Sstevel@tonic-gate  * indicating whether all tasks were successful; if any errors occured,
53*7c478bd9Sstevel@tonic-gate  * future CPR or PM updates are skipped.  Following are the handler
54*7c478bd9Sstevel@tonic-gate  * routines for all keywords:
55*7c478bd9Sstevel@tonic-gate  */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * Check for valid autopm behavior and save after ioctl success.
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate int
62*7c478bd9Sstevel@tonic-gate autopm(void)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	struct btoc {
65*7c478bd9Sstevel@tonic-gate 		char *behavior;
66*7c478bd9Sstevel@tonic-gate 		int cmd, Errno, isdef;
67*7c478bd9Sstevel@tonic-gate 	};
68*7c478bd9Sstevel@tonic-gate 	static struct btoc blist[] = {
69*7c478bd9Sstevel@tonic-gate 		"default",	PM_START_PM,	EBUSY,	1,
70*7c478bd9Sstevel@tonic-gate 		"disable",	PM_STOP_PM,	EINVAL,	0,
71*7c478bd9Sstevel@tonic-gate 		"enable",	PM_START_PM,	EBUSY,	0,
72*7c478bd9Sstevel@tonic-gate 		NULL,		0,		0,	0,
73*7c478bd9Sstevel@tonic-gate 	};
74*7c478bd9Sstevel@tonic-gate 	struct btoc *bp;
75*7c478bd9Sstevel@tonic-gate 	char *behavior;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
78*7c478bd9Sstevel@tonic-gate 		if (strcmp(behavior, bp->behavior) == 0)
79*7c478bd9Sstevel@tonic-gate 			break;
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 	if (bp->cmd == 0) {
82*7c478bd9Sstevel@tonic-gate 		mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior);
83*7c478bd9Sstevel@tonic-gate 		return (NOUP);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/*
87*7c478bd9Sstevel@tonic-gate 	 * for "default" behavior, do not enable autopm if not ESTAR_V3
88*7c478bd9Sstevel@tonic-gate 	 */
89*7c478bd9Sstevel@tonic-gate 	if (!bp->isdef || (estar_vers == ESTAR_V3)) {
90*7c478bd9Sstevel@tonic-gate 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
91*7c478bd9Sstevel@tonic-gate 			mesg(MERR, "autopm %s failed, %s\n",
92*7c478bd9Sstevel@tonic-gate 			    behavior, strerror(errno));
93*7c478bd9Sstevel@tonic-gate 			return (NOUP);
94*7c478bd9Sstevel@tonic-gate 		}
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 	(void) strcpy(new_cc.apm_behavior, behavior);
97*7c478bd9Sstevel@tonic-gate 	return (OKUP);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate static int
102*7c478bd9Sstevel@tonic-gate gethm(char *src, int *hour, int *min)
103*7c478bd9Sstevel@tonic-gate {
104*7c478bd9Sstevel@tonic-gate 	if (sscanf(src, "%d:%d", hour, min) != 2) {
105*7c478bd9Sstevel@tonic-gate 		mesg(MERR, "bad time format (%s)\n", src);
106*7c478bd9Sstevel@tonic-gate 		return (-1);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 	return (0);
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate static void
113*7c478bd9Sstevel@tonic-gate strcpy_limit(char *dst, char *src, size_t limit, char *info)
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate 	if (strlcpy(dst, src, limit) >= limit)
116*7c478bd9Sstevel@tonic-gate 		mesg(MEXIT, "%s is too long (%s)\n", info, src);
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /*
121*7c478bd9Sstevel@tonic-gate  * Convert autoshutdown idle and start/finish times;
122*7c478bd9Sstevel@tonic-gate  * check and record autoshutdown behavior.
123*7c478bd9Sstevel@tonic-gate  */
124*7c478bd9Sstevel@tonic-gate int
125*7c478bd9Sstevel@tonic-gate autosd(void)
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	char **bp, *behavior, *unrec = "unrecognized autoshutdown behavior";
128*7c478bd9Sstevel@tonic-gate 	static char *blist[] = {
129*7c478bd9Sstevel@tonic-gate 		"autowakeup", "default", "noshutdown",
130*7c478bd9Sstevel@tonic-gate 		"shutdown", "unconfigured", NULL
131*7c478bd9Sstevel@tonic-gate 	};
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	new_cc.as_idle = atoi(LINEARG(1));
134*7c478bd9Sstevel@tonic-gate 	if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) ||
135*7c478bd9Sstevel@tonic-gate 	    gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm))
136*7c478bd9Sstevel@tonic-gate 		return (NOUP);
137*7c478bd9Sstevel@tonic-gate 	mesg(MDEBUG, "idle %d, start %d:%02d, finis %d:%02d\n",
138*7c478bd9Sstevel@tonic-gate 	    new_cc.as_idle, new_cc.as_sh, new_cc.as_sm,
139*7c478bd9Sstevel@tonic-gate 	    new_cc.as_fh, new_cc.as_fm);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	for (behavior = LINEARG(4), bp = blist; *bp; bp++) {
142*7c478bd9Sstevel@tonic-gate 		if (strcmp(behavior, *bp) == 0)
143*7c478bd9Sstevel@tonic-gate 			break;
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 	if (*bp == NULL) {
146*7c478bd9Sstevel@tonic-gate 		mesg(MERR, "%s: \"%s\"\n", unrec, behavior);
147*7c478bd9Sstevel@tonic-gate 		return (NOUP);
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 	STRCPYLIM(new_cc.as_behavior, *bp, unrec);
150*7c478bd9Sstevel@tonic-gate 	return (OKUP);
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate /*
155*7c478bd9Sstevel@tonic-gate  * Check for a real device and try to resolve to a full path.
156*7c478bd9Sstevel@tonic-gate  * The orig/resolved path may be modified into a prom pathname,
157*7c478bd9Sstevel@tonic-gate  * and an allocated copy of the result is stored at *destp;
158*7c478bd9Sstevel@tonic-gate  * the caller will need to free that space.  Returns 1 for any
159*7c478bd9Sstevel@tonic-gate  * error, otherwise 0; also sets *errp after an alloc error.
160*7c478bd9Sstevel@tonic-gate  */
161*7c478bd9Sstevel@tonic-gate static int
162*7c478bd9Sstevel@tonic-gate devpath(char **destp, char *src, int *errp)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	struct stat stbuf;
165*7c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX];
166*7c478bd9Sstevel@tonic-gate 	char *cp, *dstr;
167*7c478bd9Sstevel@tonic-gate 	int devok, dcs = 0;
168*7c478bd9Sstevel@tonic-gate 	size_t len;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	/*
171*7c478bd9Sstevel@tonic-gate 	 * When there's a real device, try to resolve the path
172*7c478bd9Sstevel@tonic-gate 	 * and trim the leading "/devices" component.
173*7c478bd9Sstevel@tonic-gate 	 */
174*7c478bd9Sstevel@tonic-gate 	if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) {
175*7c478bd9Sstevel@tonic-gate 		if (realpath(src, buf) == NULL) {
176*7c478bd9Sstevel@tonic-gate 			mesg(MERR, "realpath cannot resolve \"%s\"\n",
177*7c478bd9Sstevel@tonic-gate 			    src, strerror(errno));
178*7c478bd9Sstevel@tonic-gate 			return (1);
179*7c478bd9Sstevel@tonic-gate 		}
180*7c478bd9Sstevel@tonic-gate 		src = buf;
181*7c478bd9Sstevel@tonic-gate 		dstr = "/devices";
182*7c478bd9Sstevel@tonic-gate 		len = strlen(dstr);
183*7c478bd9Sstevel@tonic-gate 		dcs = (strncmp(src, dstr, len) == 0);
184*7c478bd9Sstevel@tonic-gate 		if (dcs)
185*7c478bd9Sstevel@tonic-gate 			src += len;
186*7c478bd9Sstevel@tonic-gate 	} else
187*7c478bd9Sstevel@tonic-gate 		mesg(MDEBUG, stat_fmt, src, strerror(errno));
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	/*
190*7c478bd9Sstevel@tonic-gate 	 * When the path has ":anything", display an error for
191*7c478bd9Sstevel@tonic-gate 	 * a non-device or truncate a resolved+modifed path.
192*7c478bd9Sstevel@tonic-gate 	 */
193*7c478bd9Sstevel@tonic-gate 	if (cp = strchr(src, ':')) {
194*7c478bd9Sstevel@tonic-gate 		if (devok == 0) {
195*7c478bd9Sstevel@tonic-gate 			mesg(MERR, "physical path may not contain "
196*7c478bd9Sstevel@tonic-gate 			    "a minor string (%s)\n", src);
197*7c478bd9Sstevel@tonic-gate 			return (1);
198*7c478bd9Sstevel@tonic-gate 		} else if (dcs)
199*7c478bd9Sstevel@tonic-gate 			*cp = '\0';
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	if ((*destp = strdup(src)) == NULL) {
203*7c478bd9Sstevel@tonic-gate 		*errp = NOUP;
204*7c478bd9Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, src, strerror(errno));
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 	return (*destp == NULL);
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate /*
211*7c478bd9Sstevel@tonic-gate  * Call pm ioctl request(s) to set property/device dependencies.
212*7c478bd9Sstevel@tonic-gate  */
213*7c478bd9Sstevel@tonic-gate static int
214*7c478bd9Sstevel@tonic-gate dev_dep_common(int isprop)
215*7c478bd9Sstevel@tonic-gate {
216*7c478bd9Sstevel@tonic-gate 	int cmd, argn, upval = OKUP;
217*7c478bd9Sstevel@tonic-gate 	char *src, *first, **destp;
218*7c478bd9Sstevel@tonic-gate 	pm_req_t pmreq;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
221*7c478bd9Sstevel@tonic-gate 	src = LINEARG(1);
222*7c478bd9Sstevel@tonic-gate 	if (isprop) {
223*7c478bd9Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT_PROPERTY;
224*7c478bd9Sstevel@tonic-gate 		first = NULL;
225*7c478bd9Sstevel@tonic-gate 		pmreq.pmreq_kept = src;
226*7c478bd9Sstevel@tonic-gate 	} else {
227*7c478bd9Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT;
228*7c478bd9Sstevel@tonic-gate 		if (devpath(&first, src, &upval))
229*7c478bd9Sstevel@tonic-gate 			return (upval);
230*7c478bd9Sstevel@tonic-gate 		pmreq.pmreq_kept = first;
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 	destp = &pmreq.pmreq_keeper;
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	/*
235*7c478bd9Sstevel@tonic-gate 	 * Now loop through any dependents.
236*7c478bd9Sstevel@tonic-gate 	 */
237*7c478bd9Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
238*7c478bd9Sstevel@tonic-gate 		if (devpath(destp, src, &upval)) {
239*7c478bd9Sstevel@tonic-gate 			if (upval != OKUP)
240*7c478bd9Sstevel@tonic-gate 				return (upval);
241*7c478bd9Sstevel@tonic-gate 			break;
242*7c478bd9Sstevel@tonic-gate 		}
243*7c478bd9Sstevel@tonic-gate 		if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) {
244*7c478bd9Sstevel@tonic-gate 			mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n"
245*7c478bd9Sstevel@tonic-gate 			    "kept \"%s\", keeper \"%s\"\n",
246*7c478bd9Sstevel@tonic-gate 			    cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper);
247*7c478bd9Sstevel@tonic-gate 			mesg(MERR, "cannot set \"%s\" dependency "
248*7c478bd9Sstevel@tonic-gate 			    "for \"%s\", %s\n", pmreq.pmreq_keeper,
249*7c478bd9Sstevel@tonic-gate 			    pmreq.pmreq_kept, strerror(errno));
250*7c478bd9Sstevel@tonic-gate 		}
251*7c478bd9Sstevel@tonic-gate 		free(*destp);
252*7c478bd9Sstevel@tonic-gate 		*destp = NULL;
253*7c478bd9Sstevel@tonic-gate 		if (upval != OKUP)
254*7c478bd9Sstevel@tonic-gate 			break;
255*7c478bd9Sstevel@tonic-gate 	}
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	free(first);
258*7c478bd9Sstevel@tonic-gate 	return (upval);
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate int
263*7c478bd9Sstevel@tonic-gate ddprop(void)
264*7c478bd9Sstevel@tonic-gate {
265*7c478bd9Sstevel@tonic-gate 	return (dev_dep_common(1));
266*7c478bd9Sstevel@tonic-gate }
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate int
270*7c478bd9Sstevel@tonic-gate devdep(void)
271*7c478bd9Sstevel@tonic-gate {
272*7c478bd9Sstevel@tonic-gate 	return (dev_dep_common(0));
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate /*
277*7c478bd9Sstevel@tonic-gate  * Convert a numeric string (with a possible trailing scaling byte)
278*7c478bd9Sstevel@tonic-gate  * into an integer.  Returns a converted value and *nerrp unchanged,
279*7c478bd9Sstevel@tonic-gate  * or 0 with *nerrp set to 1 for a conversion error.
280*7c478bd9Sstevel@tonic-gate  */
281*7c478bd9Sstevel@tonic-gate static int
282*7c478bd9Sstevel@tonic-gate get_scaled_value(char *str, int *nerrp)
283*7c478bd9Sstevel@tonic-gate {
284*7c478bd9Sstevel@tonic-gate 	longlong_t svalue = 0, factor = 1;
285*7c478bd9Sstevel@tonic-gate 	char *sp;
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	errno = 0;
288*7c478bd9Sstevel@tonic-gate 	svalue = strtol(str, &sp, 0);
289*7c478bd9Sstevel@tonic-gate 	if (errno || (*str != '-' && (*str < '0' || *str > '9')))
290*7c478bd9Sstevel@tonic-gate 		*nerrp = 1;
291*7c478bd9Sstevel@tonic-gate 	else if (sp && *sp != '\0') {
292*7c478bd9Sstevel@tonic-gate 		if (*sp == 'h')
293*7c478bd9Sstevel@tonic-gate 			factor = 3600;
294*7c478bd9Sstevel@tonic-gate 		else if (*sp == 'm')
295*7c478bd9Sstevel@tonic-gate 			factor = 60;
296*7c478bd9Sstevel@tonic-gate 		else if (*sp != 's')
297*7c478bd9Sstevel@tonic-gate 			*nerrp = 1;
298*7c478bd9Sstevel@tonic-gate 	}
299*7c478bd9Sstevel@tonic-gate 	/* any bytes following sp are ignored */
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	if (*nerrp == 0) {
302*7c478bd9Sstevel@tonic-gate 		svalue *= factor;
303*7c478bd9Sstevel@tonic-gate 		if (svalue < INT_MIN || svalue > INT_MAX)
304*7c478bd9Sstevel@tonic-gate 			*nerrp = 1;
305*7c478bd9Sstevel@tonic-gate 	}
306*7c478bd9Sstevel@tonic-gate 	if (*nerrp)
307*7c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, str);
308*7c478bd9Sstevel@tonic-gate 	mesg(MDEBUG, "got scaled value %d\n", (int)svalue);
309*7c478bd9Sstevel@tonic-gate 	return ((int)svalue);
310*7c478bd9Sstevel@tonic-gate }
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate /*
314*7c478bd9Sstevel@tonic-gate  * Increment the count of threshold values,
315*7c478bd9Sstevel@tonic-gate  * reallocate *vlistp and append another element.
316*7c478bd9Sstevel@tonic-gate  * Returns 1 on error, otherwise 0.
317*7c478bd9Sstevel@tonic-gate  */
318*7c478bd9Sstevel@tonic-gate static int
319*7c478bd9Sstevel@tonic-gate vlist_append(int **vlistp, int *vcntp, int value)
320*7c478bd9Sstevel@tonic-gate {
321*7c478bd9Sstevel@tonic-gate 	(*vcntp)++;
322*7c478bd9Sstevel@tonic-gate 	if (*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp)))
323*7c478bd9Sstevel@tonic-gate 		*(*vlistp + *vcntp - 1) = value;
324*7c478bd9Sstevel@tonic-gate 	else
325*7c478bd9Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, "threshold list", strerror(errno));
326*7c478bd9Sstevel@tonic-gate 	return (*vlistp == NULL);
327*7c478bd9Sstevel@tonic-gate }
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate /*
331*7c478bd9Sstevel@tonic-gate  * Convert a single threshold string or paren groups of thresh's as
332*7c478bd9Sstevel@tonic-gate  * described below.  All thresh's are saved to an allocated list at
333*7c478bd9Sstevel@tonic-gate  * *vlistp; the caller will need to free that space.  On return:
334*7c478bd9Sstevel@tonic-gate  * *vcntp is the count of the vlist array, and vlist is either
335*7c478bd9Sstevel@tonic-gate  * a single thresh or N groups of thresh's with a trailing zero:
336*7c478bd9Sstevel@tonic-gate  * (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0.
337*7c478bd9Sstevel@tonic-gate  * Returns 0 when all conversions were OK, and 1 for any syntax,
338*7c478bd9Sstevel@tonic-gate  * conversion, or alloc error.
339*7c478bd9Sstevel@tonic-gate  */
340*7c478bd9Sstevel@tonic-gate static int
341*7c478bd9Sstevel@tonic-gate get_thresh(int **vlistp, int *vcntp)
342*7c478bd9Sstevel@tonic-gate {
343*7c478bd9Sstevel@tonic-gate 	int argn, value, gci, grp_cnt = 0, paren = 0, nerr = 0;
344*7c478bd9Sstevel@tonic-gate 	char *rp, *src;
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
347*7c478bd9Sstevel@tonic-gate 		if (*src == LPAREN) {
348*7c478bd9Sstevel@tonic-gate 			gci = *vcntp;
349*7c478bd9Sstevel@tonic-gate 			if (nerr = vlist_append(vlistp, vcntp, 0))
350*7c478bd9Sstevel@tonic-gate 				break;
351*7c478bd9Sstevel@tonic-gate 			paren = 1;
352*7c478bd9Sstevel@tonic-gate 			src++;
353*7c478bd9Sstevel@tonic-gate 		}
354*7c478bd9Sstevel@tonic-gate 		if (*(rp = LASTBYTE(src)) == RPAREN) {
355*7c478bd9Sstevel@tonic-gate 			if (paren) {
356*7c478bd9Sstevel@tonic-gate 				grp_cnt = *vcntp - gci;
357*7c478bd9Sstevel@tonic-gate 				*(*vlistp + gci) = grp_cnt;
358*7c478bd9Sstevel@tonic-gate 				paren = 0;
359*7c478bd9Sstevel@tonic-gate 				*rp = '\0';
360*7c478bd9Sstevel@tonic-gate 			} else {
361*7c478bd9Sstevel@tonic-gate 				nerr = 1;
362*7c478bd9Sstevel@tonic-gate 				break;
363*7c478bd9Sstevel@tonic-gate 			}
364*7c478bd9Sstevel@tonic-gate 		}
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 		value = get_scaled_value(src, &nerr);
367*7c478bd9Sstevel@tonic-gate 		if (nerr || (nerr = vlist_append(vlistp, vcntp, value)))
368*7c478bd9Sstevel@tonic-gate 			break;
369*7c478bd9Sstevel@tonic-gate 	}
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 	if (nerr == 0 && grp_cnt)
372*7c478bd9Sstevel@tonic-gate 		nerr = vlist_append(vlistp, vcntp, 0);
373*7c478bd9Sstevel@tonic-gate 	return (nerr);
374*7c478bd9Sstevel@tonic-gate }
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate /*
378*7c478bd9Sstevel@tonic-gate  * Set device thresholds from (3) formats:
379*7c478bd9Sstevel@tonic-gate  * 	path	"always-on"
380*7c478bd9Sstevel@tonic-gate  * 	path	time-spec: [0-9]+[{h,m,s}]
381*7c478bd9Sstevel@tonic-gate  *	path	(ts1 ts2 ...)+
382*7c478bd9Sstevel@tonic-gate  */
383*7c478bd9Sstevel@tonic-gate int
384*7c478bd9Sstevel@tonic-gate devthr(void)
385*7c478bd9Sstevel@tonic-gate {
386*7c478bd9Sstevel@tonic-gate 	int cmd, upval = OKUP, nthresh = 0, *vlist = NULL;
387*7c478bd9Sstevel@tonic-gate 	pm_req_t pmreq;
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
390*7c478bd9Sstevel@tonic-gate 	if (devpath(&pmreq.physpath, LINEARG(1), &upval))
391*7c478bd9Sstevel@tonic-gate 		return (upval);
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 	if (strcmp(LINEARG(2), always_on) == 0) {
394*7c478bd9Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
395*7c478bd9Sstevel@tonic-gate 		pmreq.value = INT_MAX;
396*7c478bd9Sstevel@tonic-gate 	} else if (get_thresh(&vlist, &nthresh)) {
397*7c478bd9Sstevel@tonic-gate 		mesg(MERR, bad_thresh_fmt);
398*7c478bd9Sstevel@tonic-gate 		upval = NOUP;
399*7c478bd9Sstevel@tonic-gate 	} else if (nthresh == 1) {
400*7c478bd9Sstevel@tonic-gate 		pmreq.value = *vlist;
401*7c478bd9Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
402*7c478bd9Sstevel@tonic-gate 	} else {
403*7c478bd9Sstevel@tonic-gate 		pmreq.data = vlist;
404*7c478bd9Sstevel@tonic-gate 		pmreq.datasize = (nthresh * sizeof (*vlist));
405*7c478bd9Sstevel@tonic-gate 		cmd = PM_SET_COMPONENT_THRESHOLDS;
406*7c478bd9Sstevel@tonic-gate 	}
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1)
409*7c478bd9Sstevel@tonic-gate 		mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno));
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	free(vlist);
412*7c478bd9Sstevel@tonic-gate 	free(pmreq.physpath);
413*7c478bd9Sstevel@tonic-gate 	return (upval);
414*7c478bd9Sstevel@tonic-gate }
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate static int
418*7c478bd9Sstevel@tonic-gate scan_int(char *src, int *dst)
419*7c478bd9Sstevel@tonic-gate {
420*7c478bd9Sstevel@tonic-gate 	long lval;
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	errno = 0;
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 	lval = strtol(LINEARG(1), NULL, 0);
425*7c478bd9Sstevel@tonic-gate 	if (errno || lval > INT_MAX || lval < 0) {
426*7c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
427*7c478bd9Sstevel@tonic-gate 		return (NOUP);
428*7c478bd9Sstevel@tonic-gate 	}
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 	*dst = (int)lval;
431*7c478bd9Sstevel@tonic-gate 	return (OKUP);
432*7c478bd9Sstevel@tonic-gate }
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate static int
435*7c478bd9Sstevel@tonic-gate scan_float(char *src, float *dst)
436*7c478bd9Sstevel@tonic-gate {
437*7c478bd9Sstevel@tonic-gate 	float fval;
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	errno = 0;
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	fval = strtof(src, NULL);
442*7c478bd9Sstevel@tonic-gate 	if (errno || fval < 0.0) {
443*7c478bd9Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
444*7c478bd9Sstevel@tonic-gate 		return (NOUP);
445*7c478bd9Sstevel@tonic-gate 	}
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	*dst = fval;
448*7c478bd9Sstevel@tonic-gate 	return (OKUP);
449*7c478bd9Sstevel@tonic-gate }
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate int
453*7c478bd9Sstevel@tonic-gate dreads(void)
454*7c478bd9Sstevel@tonic-gate {
455*7c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.diskreads_thold));
456*7c478bd9Sstevel@tonic-gate }
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate /*
460*7c478bd9Sstevel@tonic-gate  * Set pathname for idlecheck;
461*7c478bd9Sstevel@tonic-gate  * an overflowed pathname is treated as a fatal error.
462*7c478bd9Sstevel@tonic-gate  */
463*7c478bd9Sstevel@tonic-gate int
464*7c478bd9Sstevel@tonic-gate idlechk(void)
465*7c478bd9Sstevel@tonic-gate {
466*7c478bd9Sstevel@tonic-gate 	STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path");
467*7c478bd9Sstevel@tonic-gate 	return (OKUP);
468*7c478bd9Sstevel@tonic-gate }
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate int
472*7c478bd9Sstevel@tonic-gate loadavg(void)
473*7c478bd9Sstevel@tonic-gate {
474*7c478bd9Sstevel@tonic-gate 	return (scan_float(LINEARG(1), &new_cc.loadaverage_thold));
475*7c478bd9Sstevel@tonic-gate }
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate int
479*7c478bd9Sstevel@tonic-gate nfsreq(void)
480*7c478bd9Sstevel@tonic-gate {
481*7c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold));
482*7c478bd9Sstevel@tonic-gate }
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate #ifdef sparc
486*7c478bd9Sstevel@tonic-gate static char open_fmt[] = "cannot open \"%s\", %s\n";
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate /*
489*7c478bd9Sstevel@tonic-gate  * Verify the filesystem type for a regular statefile is "ufs"
490*7c478bd9Sstevel@tonic-gate  * or verify a block device is not in use as a mounted filesytem.
491*7c478bd9Sstevel@tonic-gate  * Returns 1 if any error, otherwise 0.
492*7c478bd9Sstevel@tonic-gate  */
493*7c478bd9Sstevel@tonic-gate static int
494*7c478bd9Sstevel@tonic-gate check_mount(char *sfile, dev_t sfdev, int ufs)
495*7c478bd9Sstevel@tonic-gate {
496*7c478bd9Sstevel@tonic-gate 	char *src, *err_fmt = NULL, *mnttab = MNTTAB;
497*7c478bd9Sstevel@tonic-gate 	int rgent, match = 0;
498*7c478bd9Sstevel@tonic-gate 	struct extmnttab ent;
499*7c478bd9Sstevel@tonic-gate 	FILE *fp;
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
502*7c478bd9Sstevel@tonic-gate 		mesg(MERR, open_fmt, mnttab, strerror(errno));
503*7c478bd9Sstevel@tonic-gate 		return (1);
504*7c478bd9Sstevel@tonic-gate 	}
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 	/*
507*7c478bd9Sstevel@tonic-gate 	 * Search for a matching dev_t;
508*7c478bd9Sstevel@tonic-gate 	 * ignore non-ufs filesystems for a regular statefile.
509*7c478bd9Sstevel@tonic-gate 	 */
510*7c478bd9Sstevel@tonic-gate 	while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) {
511*7c478bd9Sstevel@tonic-gate 		if (rgent > 0) {
512*7c478bd9Sstevel@tonic-gate 			mesg(MERR, "error reading \"%s\"\n", mnttab);
513*7c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
514*7c478bd9Sstevel@tonic-gate 			return (1);
515*7c478bd9Sstevel@tonic-gate 		} else if (ufs && strcmp(ent.mnt_fstype, "ufs"))
516*7c478bd9Sstevel@tonic-gate 			continue;
517*7c478bd9Sstevel@tonic-gate 		else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) {
518*7c478bd9Sstevel@tonic-gate 			match = 1;
519*7c478bd9Sstevel@tonic-gate 			break;
520*7c478bd9Sstevel@tonic-gate 		}
521*7c478bd9Sstevel@tonic-gate 	}
522*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 	/*
525*7c478bd9Sstevel@tonic-gate 	 * No match is needed for a block device statefile,
526*7c478bd9Sstevel@tonic-gate 	 * a match is needed for a regular statefile.
527*7c478bd9Sstevel@tonic-gate 	 */
528*7c478bd9Sstevel@tonic-gate 	if (match == 0) {
529*7c478bd9Sstevel@tonic-gate 		if (new_cc.cf_type == CFT_SPEC)
530*7c478bd9Sstevel@tonic-gate 			STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile");
531*7c478bd9Sstevel@tonic-gate 		else
532*7c478bd9Sstevel@tonic-gate 			err_fmt = "cannot find ufs mount point for \"%s\"\n";
533*7c478bd9Sstevel@tonic-gate 	} else if (new_cc.cf_type == CFT_UFS) {
534*7c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry");
535*7c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special");
536*7c478bd9Sstevel@tonic-gate 		while (*(sfile + 1) == '/') sfile++;
537*7c478bd9Sstevel@tonic-gate 		src = sfile + strlen(ent.mnt_mountp);
538*7c478bd9Sstevel@tonic-gate 		while (*src == '/') src++;
539*7c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_path, src, "statefile path");
540*7c478bd9Sstevel@tonic-gate 	} else
541*7c478bd9Sstevel@tonic-gate 		err_fmt = "statefile device \"%s\" is a mounted filesystem\n";
542*7c478bd9Sstevel@tonic-gate 	if (err_fmt)
543*7c478bd9Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
544*7c478bd9Sstevel@tonic-gate 	return (err_fmt != NULL);
545*7c478bd9Sstevel@tonic-gate }
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate /*
549*7c478bd9Sstevel@tonic-gate  * Convert a Unix device to a prom device and save on success,
550*7c478bd9Sstevel@tonic-gate  * log any ioctl/conversion error.
551*7c478bd9Sstevel@tonic-gate  */
552*7c478bd9Sstevel@tonic-gate static int
553*7c478bd9Sstevel@tonic-gate utop(void)
554*7c478bd9Sstevel@tonic-gate {
555*7c478bd9Sstevel@tonic-gate 	union obpbuf {
556*7c478bd9Sstevel@tonic-gate 		char	buf[OBP_MAXPATHLEN + sizeof (uint_t)];
557*7c478bd9Sstevel@tonic-gate 		struct	openpromio oppio;
558*7c478bd9Sstevel@tonic-gate 	};
559*7c478bd9Sstevel@tonic-gate 	union obpbuf oppbuf;
560*7c478bd9Sstevel@tonic-gate 	struct openpromio *opp;
561*7c478bd9Sstevel@tonic-gate 	char *promdev = "/dev/openprom";
562*7c478bd9Sstevel@tonic-gate 	int fd, upval;
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate 	if ((fd = open(promdev, O_RDONLY)) == -1) {
565*7c478bd9Sstevel@tonic-gate 		mesg(MERR, open_fmt, promdev, strerror(errno));
566*7c478bd9Sstevel@tonic-gate 		return (NOUP);
567*7c478bd9Sstevel@tonic-gate 	}
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate 	opp = &oppbuf.oppio;
570*7c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPATHLEN;
571*7c478bd9Sstevel@tonic-gate 	strcpy_limit(opp->oprom_array, new_cc.cf_devfs,
572*7c478bd9Sstevel@tonic-gate 	    OBP_MAXPATHLEN, "statefile device");
573*7c478bd9Sstevel@tonic-gate 	upval = ioctl(fd, OPROMDEV2PROMNAME, opp);
574*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
575*7c478bd9Sstevel@tonic-gate 	if (upval == OKUP)
576*7c478bd9Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_dev_prom, opp->oprom_array, "prom device");
577*7c478bd9Sstevel@tonic-gate 	else {
578*7c478bd9Sstevel@tonic-gate 		openlog("pmconfig", 0, LOG_DAEMON);
579*7c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE,
580*7c478bd9Sstevel@tonic-gate 		    gettext("cannot convert \"%s\" to prom device"),
581*7c478bd9Sstevel@tonic-gate 		    new_cc.cf_devfs);
582*7c478bd9Sstevel@tonic-gate 		closelog();
583*7c478bd9Sstevel@tonic-gate 	}
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate 	return (upval);
586*7c478bd9Sstevel@tonic-gate }
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate 
589*7c478bd9Sstevel@tonic-gate /*
590*7c478bd9Sstevel@tonic-gate  * Check for a valid statefile pathname, inode and mount status.
591*7c478bd9Sstevel@tonic-gate  */
592*7c478bd9Sstevel@tonic-gate int
593*7c478bd9Sstevel@tonic-gate sfpath(void)
594*7c478bd9Sstevel@tonic-gate {
595*7c478bd9Sstevel@tonic-gate 	static int statefile;
596*7c478bd9Sstevel@tonic-gate 	char *err_fmt = NULL;
597*7c478bd9Sstevel@tonic-gate 	char *sfile, *sp, ch;
598*7c478bd9Sstevel@tonic-gate 	struct stat stbuf;
599*7c478bd9Sstevel@tonic-gate 	int dir = 0;
600*7c478bd9Sstevel@tonic-gate 	dev_t dev;
601*7c478bd9Sstevel@tonic-gate 
602*7c478bd9Sstevel@tonic-gate 	if (statefile) {
603*7c478bd9Sstevel@tonic-gate 		mesg(MERR, "ignored redundant statefile entry\n");
604*7c478bd9Sstevel@tonic-gate 		return (OKUP);
605*7c478bd9Sstevel@tonic-gate 	} else if (ua_err) {
606*7c478bd9Sstevel@tonic-gate 		if (ua_err != ENOTSUP)
607*7c478bd9Sstevel@tonic-gate 			mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n",
608*7c478bd9Sstevel@tonic-gate 			    strerror(ua_err));
609*7c478bd9Sstevel@tonic-gate 		return (NOUP);
610*7c478bd9Sstevel@tonic-gate 	}
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 	/*
613*7c478bd9Sstevel@tonic-gate 	 * Check for an absolute path and trim any trailing '/'.
614*7c478bd9Sstevel@tonic-gate 	 */
615*7c478bd9Sstevel@tonic-gate 	sfile = LINEARG(1);
616*7c478bd9Sstevel@tonic-gate 	if (*sfile != '/') {
617*7c478bd9Sstevel@tonic-gate 		mesg(MERR, "statefile requires an absolute path\n");
618*7c478bd9Sstevel@tonic-gate 		return (NOUP);
619*7c478bd9Sstevel@tonic-gate 	}
620*7c478bd9Sstevel@tonic-gate 	for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--)
621*7c478bd9Sstevel@tonic-gate 		*sp = '\0';
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate 	/*
624*7c478bd9Sstevel@tonic-gate 	 * If the statefile doesn't exist, the leading path must be a dir.
625*7c478bd9Sstevel@tonic-gate 	 */
626*7c478bd9Sstevel@tonic-gate 	if (stat(sfile, &stbuf) == -1) {
627*7c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
628*7c478bd9Sstevel@tonic-gate 			dir = 1;
629*7c478bd9Sstevel@tonic-gate 			if ((sp = strrchr(sfile, '/')) == sfile)
630*7c478bd9Sstevel@tonic-gate 				sp++;
631*7c478bd9Sstevel@tonic-gate 			ch = *sp;
632*7c478bd9Sstevel@tonic-gate 			*sp = '\0';
633*7c478bd9Sstevel@tonic-gate 			if (stat(sfile, &stbuf) == -1)
634*7c478bd9Sstevel@tonic-gate 				err_fmt = stat_fmt;
635*7c478bd9Sstevel@tonic-gate 			*sp = ch;
636*7c478bd9Sstevel@tonic-gate 		} else
637*7c478bd9Sstevel@tonic-gate 			err_fmt = stat_fmt;
638*7c478bd9Sstevel@tonic-gate 		if (err_fmt) {
639*7c478bd9Sstevel@tonic-gate 			mesg(MERR, err_fmt, sfile, strerror(errno));
640*7c478bd9Sstevel@tonic-gate 			return (NOUP);
641*7c478bd9Sstevel@tonic-gate 		}
642*7c478bd9Sstevel@tonic-gate 	}
643*7c478bd9Sstevel@tonic-gate 
644*7c478bd9Sstevel@tonic-gate 	/*
645*7c478bd9Sstevel@tonic-gate 	 * Check for regular/dir/block types, set cf_type and dev.
646*7c478bd9Sstevel@tonic-gate 	 */
647*7c478bd9Sstevel@tonic-gate 	if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) {
648*7c478bd9Sstevel@tonic-gate 		new_cc.cf_type = CFT_UFS;
649*7c478bd9Sstevel@tonic-gate 		dev = stbuf.st_dev;
650*7c478bd9Sstevel@tonic-gate 	} else if (S_ISBLK(stbuf.st_mode)) {
651*7c478bd9Sstevel@tonic-gate 		if (minor(stbuf.st_rdev) != 2) {
652*7c478bd9Sstevel@tonic-gate 			new_cc.cf_type = CFT_SPEC;
653*7c478bd9Sstevel@tonic-gate 			dev = stbuf.st_rdev;
654*7c478bd9Sstevel@tonic-gate 		} else
655*7c478bd9Sstevel@tonic-gate 			err_fmt = "statefile device cannot be slice 2 (%s)\n"
656*7c478bd9Sstevel@tonic-gate 			    "would clobber the disk label and boot-block\n";
657*7c478bd9Sstevel@tonic-gate 	} else
658*7c478bd9Sstevel@tonic-gate 		err_fmt = "bad file type for \"%s\"\n"
659*7c478bd9Sstevel@tonic-gate 		    "statefile must be a regular file or block device\n";
660*7c478bd9Sstevel@tonic-gate 	if (err_fmt) {
661*7c478bd9Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
662*7c478bd9Sstevel@tonic-gate 		return (NOUP);
663*7c478bd9Sstevel@tonic-gate 	}
664*7c478bd9Sstevel@tonic-gate 
665*7c478bd9Sstevel@tonic-gate 	if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)) || utop())
666*7c478bd9Sstevel@tonic-gate 		return (NOUP);
667*7c478bd9Sstevel@tonic-gate 	new_cc.cf_magic = CPR_CONFIG_MAGIC;
668*7c478bd9Sstevel@tonic-gate 	statefile = 1;
669*7c478bd9Sstevel@tonic-gate 	return (OKUP);
670*7c478bd9Sstevel@tonic-gate }
671*7c478bd9Sstevel@tonic-gate #endif /* sparc */
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate /*
675*7c478bd9Sstevel@tonic-gate  * Try setting system threshold.
676*7c478bd9Sstevel@tonic-gate  */
677*7c478bd9Sstevel@tonic-gate int
678*7c478bd9Sstevel@tonic-gate systhr(void)
679*7c478bd9Sstevel@tonic-gate {
680*7c478bd9Sstevel@tonic-gate 	int value, nerr = 0, upval = OKUP;
681*7c478bd9Sstevel@tonic-gate 	char *thresh = LINEARG(1);
682*7c478bd9Sstevel@tonic-gate 
683*7c478bd9Sstevel@tonic-gate 	if (strcmp(thresh, always_on) == 0)
684*7c478bd9Sstevel@tonic-gate 		value = INT_MAX;
685*7c478bd9Sstevel@tonic-gate 	else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) {
686*7c478bd9Sstevel@tonic-gate 		mesg(MERR, "%s must be a positive value\n", LINEARG(0));
687*7c478bd9Sstevel@tonic-gate 		upval = NOUP;
688*7c478bd9Sstevel@tonic-gate 	}
689*7c478bd9Sstevel@tonic-gate 	if (upval == OKUP)
690*7c478bd9Sstevel@tonic-gate 		(void) ioctl(pm_fd, PM_SET_SYSTEM_THRESHOLD, value);
691*7c478bd9Sstevel@tonic-gate 	return (upval);
692*7c478bd9Sstevel@tonic-gate }
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 
695*7c478bd9Sstevel@tonic-gate int
696*7c478bd9Sstevel@tonic-gate tchars(void)
697*7c478bd9Sstevel@tonic-gate {
698*7c478bd9Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.ttychars_thold));
699*7c478bd9Sstevel@tonic-gate }
700