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