xref: /onnv-gate/usr/src/cmd/power/handlers.c (revision 763)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*763Smh27603  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "pmconfig.h"
300Sstevel@tonic-gate #include <sys/mkdev.h>
310Sstevel@tonic-gate #include <sys/syslog.h>
320Sstevel@tonic-gate #include <sys/openpromio.h>
330Sstevel@tonic-gate #include <sys/mnttab.h>
340Sstevel@tonic-gate #include <syslog.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define	STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str)
390Sstevel@tonic-gate #define	LASTBYTE(str) (str + strlen(str) - 1)
400Sstevel@tonic-gate 
410Sstevel@tonic-gate static char nerr_fmt[] = "number is out of range (%s)\n";
420Sstevel@tonic-gate static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n";
430Sstevel@tonic-gate static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n";
440Sstevel@tonic-gate static char bad_thresh_fmt[] = "bad threshold(s)\n";
450Sstevel@tonic-gate static char stat_fmt[] = "cannot stat \"%s\", %s\n";
460Sstevel@tonic-gate static char always_on[] = "always-on";
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * When lines in a config file (usually "/etc/power.conf") start with
500Sstevel@tonic-gate  * a recognized keyword, a "handler" routine is called for specific
510Sstevel@tonic-gate  * CPR or PM -related action(s).  Each routine returns a status code
520Sstevel@tonic-gate  * indicating whether all tasks were successful; if any errors occured,
530Sstevel@tonic-gate  * future CPR or PM updates are skipped.  Following are the handler
540Sstevel@tonic-gate  * routines for all keywords:
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * Check for valid autopm behavior and save after ioctl success.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate int
620Sstevel@tonic-gate autopm(void)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	struct btoc {
650Sstevel@tonic-gate 		char *behavior;
660Sstevel@tonic-gate 		int cmd, Errno, isdef;
670Sstevel@tonic-gate 	};
680Sstevel@tonic-gate 	static struct btoc blist[] = {
690Sstevel@tonic-gate 		"default",	PM_START_PM,	EBUSY,	1,
700Sstevel@tonic-gate 		"disable",	PM_STOP_PM,	EINVAL,	0,
710Sstevel@tonic-gate 		"enable",	PM_START_PM,	EBUSY,	0,
720Sstevel@tonic-gate 		NULL,		0,		0,	0,
730Sstevel@tonic-gate 	};
740Sstevel@tonic-gate 	struct btoc *bp;
750Sstevel@tonic-gate 	char *behavior;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
780Sstevel@tonic-gate 		if (strcmp(behavior, bp->behavior) == 0)
790Sstevel@tonic-gate 			break;
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	if (bp->cmd == 0) {
820Sstevel@tonic-gate 		mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior);
830Sstevel@tonic-gate 		return (NOUP);
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * for "default" behavior, do not enable autopm if not ESTAR_V3
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 	if (!bp->isdef || (estar_vers == ESTAR_V3)) {
900Sstevel@tonic-gate 		if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
910Sstevel@tonic-gate 			mesg(MERR, "autopm %s failed, %s\n",
920Sstevel@tonic-gate 			    behavior, strerror(errno));
930Sstevel@tonic-gate 			return (NOUP);
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 	(void) strcpy(new_cc.apm_behavior, behavior);
970Sstevel@tonic-gate 	return (OKUP);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static int
1020Sstevel@tonic-gate gethm(char *src, int *hour, int *min)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	if (sscanf(src, "%d:%d", hour, min) != 2) {
1050Sstevel@tonic-gate 		mesg(MERR, "bad time format (%s)\n", src);
1060Sstevel@tonic-gate 		return (-1);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 	return (0);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static void
1130Sstevel@tonic-gate strcpy_limit(char *dst, char *src, size_t limit, char *info)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	if (strlcpy(dst, src, limit) >= limit)
1160Sstevel@tonic-gate 		mesg(MEXIT, "%s is too long (%s)\n", info, src);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * Convert autoshutdown idle and start/finish times;
1220Sstevel@tonic-gate  * check and record autoshutdown behavior.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate int
1250Sstevel@tonic-gate autosd(void)
1260Sstevel@tonic-gate {
127*763Smh27603 	char **bp, *behavior;
128*763Smh27603 	char *unrec = gettext("unrecognized autoshutdown behavior");
1290Sstevel@tonic-gate 	static char *blist[] = {
1300Sstevel@tonic-gate 		"autowakeup", "default", "noshutdown",
1310Sstevel@tonic-gate 		"shutdown", "unconfigured", NULL
1320Sstevel@tonic-gate 	};
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	new_cc.as_idle = atoi(LINEARG(1));
1350Sstevel@tonic-gate 	if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) ||
1360Sstevel@tonic-gate 	    gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm))
1370Sstevel@tonic-gate 		return (NOUP);
138*763Smh27603 	mesg(MDEBUG, "idle %d, start %d:%02d, finish %d:%02d\n",
1390Sstevel@tonic-gate 	    new_cc.as_idle, new_cc.as_sh, new_cc.as_sm,
1400Sstevel@tonic-gate 	    new_cc.as_fh, new_cc.as_fm);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	for (behavior = LINEARG(4), bp = blist; *bp; bp++) {
1430Sstevel@tonic-gate 		if (strcmp(behavior, *bp) == 0)
1440Sstevel@tonic-gate 			break;
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 	if (*bp == NULL) {
1470Sstevel@tonic-gate 		mesg(MERR, "%s: \"%s\"\n", unrec, behavior);
1480Sstevel@tonic-gate 		return (NOUP);
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 	STRCPYLIM(new_cc.as_behavior, *bp, unrec);
1510Sstevel@tonic-gate 	return (OKUP);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * Check for a real device and try to resolve to a full path.
1570Sstevel@tonic-gate  * The orig/resolved path may be modified into a prom pathname,
1580Sstevel@tonic-gate  * and an allocated copy of the result is stored at *destp;
1590Sstevel@tonic-gate  * the caller will need to free that space.  Returns 1 for any
1600Sstevel@tonic-gate  * error, otherwise 0; also sets *errp after an alloc error.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate static int
1630Sstevel@tonic-gate devpath(char **destp, char *src, int *errp)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	struct stat stbuf;
1660Sstevel@tonic-gate 	char buf[PATH_MAX];
1670Sstevel@tonic-gate 	char *cp, *dstr;
1680Sstevel@tonic-gate 	int devok, dcs = 0;
1690Sstevel@tonic-gate 	size_t len;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	/*
1720Sstevel@tonic-gate 	 * When there's a real device, try to resolve the path
1730Sstevel@tonic-gate 	 * and trim the leading "/devices" component.
1740Sstevel@tonic-gate 	 */
1750Sstevel@tonic-gate 	if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) {
1760Sstevel@tonic-gate 		if (realpath(src, buf) == NULL) {
1770Sstevel@tonic-gate 			mesg(MERR, "realpath cannot resolve \"%s\"\n",
1780Sstevel@tonic-gate 			    src, strerror(errno));
1790Sstevel@tonic-gate 			return (1);
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 		src = buf;
1820Sstevel@tonic-gate 		dstr = "/devices";
1830Sstevel@tonic-gate 		len = strlen(dstr);
1840Sstevel@tonic-gate 		dcs = (strncmp(src, dstr, len) == 0);
1850Sstevel@tonic-gate 		if (dcs)
1860Sstevel@tonic-gate 			src += len;
1870Sstevel@tonic-gate 	} else
1880Sstevel@tonic-gate 		mesg(MDEBUG, stat_fmt, src, strerror(errno));
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	/*
1910Sstevel@tonic-gate 	 * When the path has ":anything", display an error for
1920Sstevel@tonic-gate 	 * a non-device or truncate a resolved+modifed path.
1930Sstevel@tonic-gate 	 */
1940Sstevel@tonic-gate 	if (cp = strchr(src, ':')) {
1950Sstevel@tonic-gate 		if (devok == 0) {
1960Sstevel@tonic-gate 			mesg(MERR, "physical path may not contain "
1970Sstevel@tonic-gate 			    "a minor string (%s)\n", src);
1980Sstevel@tonic-gate 			return (1);
1990Sstevel@tonic-gate 		} else if (dcs)
2000Sstevel@tonic-gate 			*cp = '\0';
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if ((*destp = strdup(src)) == NULL) {
2040Sstevel@tonic-gate 		*errp = NOUP;
2050Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, src, strerror(errno));
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 	return (*destp == NULL);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * Call pm ioctl request(s) to set property/device dependencies.
2130Sstevel@tonic-gate  */
2140Sstevel@tonic-gate static int
2150Sstevel@tonic-gate dev_dep_common(int isprop)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	int cmd, argn, upval = OKUP;
2180Sstevel@tonic-gate 	char *src, *first, **destp;
2190Sstevel@tonic-gate 	pm_req_t pmreq;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
2220Sstevel@tonic-gate 	src = LINEARG(1);
2230Sstevel@tonic-gate 	if (isprop) {
2240Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT_PROPERTY;
2250Sstevel@tonic-gate 		first = NULL;
2260Sstevel@tonic-gate 		pmreq.pmreq_kept = src;
2270Sstevel@tonic-gate 	} else {
2280Sstevel@tonic-gate 		cmd = PM_ADD_DEPENDENT;
2290Sstevel@tonic-gate 		if (devpath(&first, src, &upval))
2300Sstevel@tonic-gate 			return (upval);
2310Sstevel@tonic-gate 		pmreq.pmreq_kept = first;
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 	destp = &pmreq.pmreq_keeper;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/*
2360Sstevel@tonic-gate 	 * Now loop through any dependents.
2370Sstevel@tonic-gate 	 */
2380Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
2390Sstevel@tonic-gate 		if (devpath(destp, src, &upval)) {
2400Sstevel@tonic-gate 			if (upval != OKUP)
2410Sstevel@tonic-gate 				return (upval);
2420Sstevel@tonic-gate 			break;
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 		if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) {
2450Sstevel@tonic-gate 			mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n"
2460Sstevel@tonic-gate 			    "kept \"%s\", keeper \"%s\"\n",
2470Sstevel@tonic-gate 			    cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper);
2480Sstevel@tonic-gate 			mesg(MERR, "cannot set \"%s\" dependency "
2490Sstevel@tonic-gate 			    "for \"%s\", %s\n", pmreq.pmreq_keeper,
2500Sstevel@tonic-gate 			    pmreq.pmreq_kept, strerror(errno));
2510Sstevel@tonic-gate 		}
2520Sstevel@tonic-gate 		free(*destp);
2530Sstevel@tonic-gate 		*destp = NULL;
2540Sstevel@tonic-gate 		if (upval != OKUP)
2550Sstevel@tonic-gate 			break;
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	free(first);
2590Sstevel@tonic-gate 	return (upval);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate int
2640Sstevel@tonic-gate ddprop(void)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate 	return (dev_dep_common(1));
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate int
2710Sstevel@tonic-gate devdep(void)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	return (dev_dep_common(0));
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate  * Convert a numeric string (with a possible trailing scaling byte)
2790Sstevel@tonic-gate  * into an integer.  Returns a converted value and *nerrp unchanged,
2800Sstevel@tonic-gate  * or 0 with *nerrp set to 1 for a conversion error.
2810Sstevel@tonic-gate  */
2820Sstevel@tonic-gate static int
2830Sstevel@tonic-gate get_scaled_value(char *str, int *nerrp)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	longlong_t svalue = 0, factor = 1;
2860Sstevel@tonic-gate 	char *sp;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	errno = 0;
2890Sstevel@tonic-gate 	svalue = strtol(str, &sp, 0);
2900Sstevel@tonic-gate 	if (errno || (*str != '-' && (*str < '0' || *str > '9')))
2910Sstevel@tonic-gate 		*nerrp = 1;
2920Sstevel@tonic-gate 	else if (sp && *sp != '\0') {
2930Sstevel@tonic-gate 		if (*sp == 'h')
2940Sstevel@tonic-gate 			factor = 3600;
2950Sstevel@tonic-gate 		else if (*sp == 'm')
2960Sstevel@tonic-gate 			factor = 60;
2970Sstevel@tonic-gate 		else if (*sp != 's')
2980Sstevel@tonic-gate 			*nerrp = 1;
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 	/* any bytes following sp are ignored */
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	if (*nerrp == 0) {
3030Sstevel@tonic-gate 		svalue *= factor;
3040Sstevel@tonic-gate 		if (svalue < INT_MIN || svalue > INT_MAX)
3050Sstevel@tonic-gate 			*nerrp = 1;
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 	if (*nerrp)
3080Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, str);
3090Sstevel@tonic-gate 	mesg(MDEBUG, "got scaled value %d\n", (int)svalue);
3100Sstevel@tonic-gate 	return ((int)svalue);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate  * Increment the count of threshold values,
3160Sstevel@tonic-gate  * reallocate *vlistp and append another element.
3170Sstevel@tonic-gate  * Returns 1 on error, otherwise 0.
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate static int
3200Sstevel@tonic-gate vlist_append(int **vlistp, int *vcntp, int value)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	(*vcntp)++;
3230Sstevel@tonic-gate 	if (*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp)))
3240Sstevel@tonic-gate 		*(*vlistp + *vcntp - 1) = value;
3250Sstevel@tonic-gate 	else
3260Sstevel@tonic-gate 		mesg(MERR, alloc_fmt, "threshold list", strerror(errno));
3270Sstevel@tonic-gate 	return (*vlistp == NULL);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate  * Convert a single threshold string or paren groups of thresh's as
3330Sstevel@tonic-gate  * described below.  All thresh's are saved to an allocated list at
3340Sstevel@tonic-gate  * *vlistp; the caller will need to free that space.  On return:
3350Sstevel@tonic-gate  * *vcntp is the count of the vlist array, and vlist is either
3360Sstevel@tonic-gate  * a single thresh or N groups of thresh's with a trailing zero:
3370Sstevel@tonic-gate  * (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0.
3380Sstevel@tonic-gate  * Returns 0 when all conversions were OK, and 1 for any syntax,
3390Sstevel@tonic-gate  * conversion, or alloc error.
3400Sstevel@tonic-gate  */
3410Sstevel@tonic-gate static int
3420Sstevel@tonic-gate get_thresh(int **vlistp, int *vcntp)
3430Sstevel@tonic-gate {
3440Sstevel@tonic-gate 	int argn, value, gci, grp_cnt = 0, paren = 0, nerr = 0;
3450Sstevel@tonic-gate 	char *rp, *src;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
3480Sstevel@tonic-gate 		if (*src == LPAREN) {
3490Sstevel@tonic-gate 			gci = *vcntp;
3500Sstevel@tonic-gate 			if (nerr = vlist_append(vlistp, vcntp, 0))
3510Sstevel@tonic-gate 				break;
3520Sstevel@tonic-gate 			paren = 1;
3530Sstevel@tonic-gate 			src++;
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 		if (*(rp = LASTBYTE(src)) == RPAREN) {
3560Sstevel@tonic-gate 			if (paren) {
3570Sstevel@tonic-gate 				grp_cnt = *vcntp - gci;
3580Sstevel@tonic-gate 				*(*vlistp + gci) = grp_cnt;
3590Sstevel@tonic-gate 				paren = 0;
3600Sstevel@tonic-gate 				*rp = '\0';
3610Sstevel@tonic-gate 			} else {
3620Sstevel@tonic-gate 				nerr = 1;
3630Sstevel@tonic-gate 				break;
3640Sstevel@tonic-gate 			}
3650Sstevel@tonic-gate 		}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		value = get_scaled_value(src, &nerr);
3680Sstevel@tonic-gate 		if (nerr || (nerr = vlist_append(vlistp, vcntp, value)))
3690Sstevel@tonic-gate 			break;
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	if (nerr == 0 && grp_cnt)
3730Sstevel@tonic-gate 		nerr = vlist_append(vlistp, vcntp, 0);
3740Sstevel@tonic-gate 	return (nerr);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate  * Set device thresholds from (3) formats:
3800Sstevel@tonic-gate  * 	path	"always-on"
3810Sstevel@tonic-gate  * 	path	time-spec: [0-9]+[{h,m,s}]
3820Sstevel@tonic-gate  *	path	(ts1 ts2 ...)+
3830Sstevel@tonic-gate  */
3840Sstevel@tonic-gate int
3850Sstevel@tonic-gate devthr(void)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	int cmd, upval = OKUP, nthresh = 0, *vlist = NULL;
3880Sstevel@tonic-gate 	pm_req_t pmreq;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	bzero(&pmreq, sizeof (pmreq));
3910Sstevel@tonic-gate 	if (devpath(&pmreq.physpath, LINEARG(1), &upval))
3920Sstevel@tonic-gate 		return (upval);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	if (strcmp(LINEARG(2), always_on) == 0) {
3950Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
3960Sstevel@tonic-gate 		pmreq.value = INT_MAX;
3970Sstevel@tonic-gate 	} else if (get_thresh(&vlist, &nthresh)) {
3980Sstevel@tonic-gate 		mesg(MERR, bad_thresh_fmt);
3990Sstevel@tonic-gate 		upval = NOUP;
4000Sstevel@tonic-gate 	} else if (nthresh == 1) {
4010Sstevel@tonic-gate 		pmreq.value = *vlist;
4020Sstevel@tonic-gate 		cmd = PM_SET_DEVICE_THRESHOLD;
4030Sstevel@tonic-gate 	} else {
4040Sstevel@tonic-gate 		pmreq.data = vlist;
4050Sstevel@tonic-gate 		pmreq.datasize = (nthresh * sizeof (*vlist));
4060Sstevel@tonic-gate 		cmd = PM_SET_COMPONENT_THRESHOLDS;
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1)
4100Sstevel@tonic-gate 		mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno));
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	free(vlist);
4130Sstevel@tonic-gate 	free(pmreq.physpath);
4140Sstevel@tonic-gate 	return (upval);
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate static int
4190Sstevel@tonic-gate scan_int(char *src, int *dst)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	long lval;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	errno = 0;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	lval = strtol(LINEARG(1), NULL, 0);
4260Sstevel@tonic-gate 	if (errno || lval > INT_MAX || lval < 0) {
4270Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
4280Sstevel@tonic-gate 		return (NOUP);
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	*dst = (int)lval;
4320Sstevel@tonic-gate 	return (OKUP);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate static int
4360Sstevel@tonic-gate scan_float(char *src, float *dst)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 	float fval;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	errno = 0;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	fval = strtof(src, NULL);
4430Sstevel@tonic-gate 	if (errno || fval < 0.0) {
4440Sstevel@tonic-gate 		mesg(MERR, nerr_fmt, src);
4450Sstevel@tonic-gate 		return (NOUP);
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	*dst = fval;
4490Sstevel@tonic-gate 	return (OKUP);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate int
4540Sstevel@tonic-gate dreads(void)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.diskreads_thold));
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * Set pathname for idlecheck;
4620Sstevel@tonic-gate  * an overflowed pathname is treated as a fatal error.
4630Sstevel@tonic-gate  */
4640Sstevel@tonic-gate int
4650Sstevel@tonic-gate idlechk(void)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path");
4680Sstevel@tonic-gate 	return (OKUP);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate int
4730Sstevel@tonic-gate loadavg(void)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	return (scan_float(LINEARG(1), &new_cc.loadaverage_thold));
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate int
4800Sstevel@tonic-gate nfsreq(void)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold));
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate #ifdef sparc
4870Sstevel@tonic-gate static char open_fmt[] = "cannot open \"%s\", %s\n";
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate /*
4900Sstevel@tonic-gate  * Verify the filesystem type for a regular statefile is "ufs"
4910Sstevel@tonic-gate  * or verify a block device is not in use as a mounted filesytem.
4920Sstevel@tonic-gate  * Returns 1 if any error, otherwise 0.
4930Sstevel@tonic-gate  */
4940Sstevel@tonic-gate static int
4950Sstevel@tonic-gate check_mount(char *sfile, dev_t sfdev, int ufs)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate 	char *src, *err_fmt = NULL, *mnttab = MNTTAB;
4980Sstevel@tonic-gate 	int rgent, match = 0;
4990Sstevel@tonic-gate 	struct extmnttab ent;
5000Sstevel@tonic-gate 	FILE *fp;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	if ((fp = fopen(mnttab, "r")) == NULL) {
5030Sstevel@tonic-gate 		mesg(MERR, open_fmt, mnttab, strerror(errno));
5040Sstevel@tonic-gate 		return (1);
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * Search for a matching dev_t;
5090Sstevel@tonic-gate 	 * ignore non-ufs filesystems for a regular statefile.
5100Sstevel@tonic-gate 	 */
5110Sstevel@tonic-gate 	while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) {
5120Sstevel@tonic-gate 		if (rgent > 0) {
5130Sstevel@tonic-gate 			mesg(MERR, "error reading \"%s\"\n", mnttab);
5140Sstevel@tonic-gate 			(void) fclose(fp);
5150Sstevel@tonic-gate 			return (1);
5160Sstevel@tonic-gate 		} else if (ufs && strcmp(ent.mnt_fstype, "ufs"))
5170Sstevel@tonic-gate 			continue;
5180Sstevel@tonic-gate 		else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) {
5190Sstevel@tonic-gate 			match = 1;
5200Sstevel@tonic-gate 			break;
5210Sstevel@tonic-gate 		}
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 	(void) fclose(fp);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/*
5260Sstevel@tonic-gate 	 * No match is needed for a block device statefile,
5270Sstevel@tonic-gate 	 * a match is needed for a regular statefile.
5280Sstevel@tonic-gate 	 */
5290Sstevel@tonic-gate 	if (match == 0) {
5300Sstevel@tonic-gate 		if (new_cc.cf_type == CFT_SPEC)
5310Sstevel@tonic-gate 			STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile");
5320Sstevel@tonic-gate 		else
5330Sstevel@tonic-gate 			err_fmt = "cannot find ufs mount point for \"%s\"\n";
5340Sstevel@tonic-gate 	} else if (new_cc.cf_type == CFT_UFS) {
5350Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry");
5360Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special");
5370Sstevel@tonic-gate 		while (*(sfile + 1) == '/') sfile++;
5380Sstevel@tonic-gate 		src = sfile + strlen(ent.mnt_mountp);
5390Sstevel@tonic-gate 		while (*src == '/') src++;
5400Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_path, src, "statefile path");
5410Sstevel@tonic-gate 	} else
5420Sstevel@tonic-gate 		err_fmt = "statefile device \"%s\" is a mounted filesystem\n";
5430Sstevel@tonic-gate 	if (err_fmt)
5440Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
5450Sstevel@tonic-gate 	return (err_fmt != NULL);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate /*
5500Sstevel@tonic-gate  * Convert a Unix device to a prom device and save on success,
5510Sstevel@tonic-gate  * log any ioctl/conversion error.
5520Sstevel@tonic-gate  */
5530Sstevel@tonic-gate static int
5540Sstevel@tonic-gate utop(void)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	union obpbuf {
5570Sstevel@tonic-gate 		char	buf[OBP_MAXPATHLEN + sizeof (uint_t)];
5580Sstevel@tonic-gate 		struct	openpromio oppio;
5590Sstevel@tonic-gate 	};
5600Sstevel@tonic-gate 	union obpbuf oppbuf;
5610Sstevel@tonic-gate 	struct openpromio *opp;
5620Sstevel@tonic-gate 	char *promdev = "/dev/openprom";
5630Sstevel@tonic-gate 	int fd, upval;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	if ((fd = open(promdev, O_RDONLY)) == -1) {
5660Sstevel@tonic-gate 		mesg(MERR, open_fmt, promdev, strerror(errno));
5670Sstevel@tonic-gate 		return (NOUP);
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	opp = &oppbuf.oppio;
5710Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPATHLEN;
5720Sstevel@tonic-gate 	strcpy_limit(opp->oprom_array, new_cc.cf_devfs,
5730Sstevel@tonic-gate 	    OBP_MAXPATHLEN, "statefile device");
5740Sstevel@tonic-gate 	upval = ioctl(fd, OPROMDEV2PROMNAME, opp);
5750Sstevel@tonic-gate 	(void) close(fd);
5760Sstevel@tonic-gate 	if (upval == OKUP)
5770Sstevel@tonic-gate 		STRCPYLIM(new_cc.cf_dev_prom, opp->oprom_array, "prom device");
5780Sstevel@tonic-gate 	else {
5790Sstevel@tonic-gate 		openlog("pmconfig", 0, LOG_DAEMON);
5800Sstevel@tonic-gate 		syslog(LOG_NOTICE,
5810Sstevel@tonic-gate 		    gettext("cannot convert \"%s\" to prom device"),
5820Sstevel@tonic-gate 		    new_cc.cf_devfs);
5830Sstevel@tonic-gate 		closelog();
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	return (upval);
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate /*
5910Sstevel@tonic-gate  * Check for a valid statefile pathname, inode and mount status.
5920Sstevel@tonic-gate  */
5930Sstevel@tonic-gate int
5940Sstevel@tonic-gate sfpath(void)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate 	static int statefile;
5970Sstevel@tonic-gate 	char *err_fmt = NULL;
5980Sstevel@tonic-gate 	char *sfile, *sp, ch;
5990Sstevel@tonic-gate 	struct stat stbuf;
6000Sstevel@tonic-gate 	int dir = 0;
6010Sstevel@tonic-gate 	dev_t dev;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if (statefile) {
6040Sstevel@tonic-gate 		mesg(MERR, "ignored redundant statefile entry\n");
6050Sstevel@tonic-gate 		return (OKUP);
6060Sstevel@tonic-gate 	} else if (ua_err) {
6070Sstevel@tonic-gate 		if (ua_err != ENOTSUP)
6080Sstevel@tonic-gate 			mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n",
6090Sstevel@tonic-gate 			    strerror(ua_err));
6100Sstevel@tonic-gate 		return (NOUP);
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	/*
6140Sstevel@tonic-gate 	 * Check for an absolute path and trim any trailing '/'.
6150Sstevel@tonic-gate 	 */
6160Sstevel@tonic-gate 	sfile = LINEARG(1);
6170Sstevel@tonic-gate 	if (*sfile != '/') {
6180Sstevel@tonic-gate 		mesg(MERR, "statefile requires an absolute path\n");
6190Sstevel@tonic-gate 		return (NOUP);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 	for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--)
6220Sstevel@tonic-gate 		*sp = '\0';
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/*
6250Sstevel@tonic-gate 	 * If the statefile doesn't exist, the leading path must be a dir.
6260Sstevel@tonic-gate 	 */
6270Sstevel@tonic-gate 	if (stat(sfile, &stbuf) == -1) {
6280Sstevel@tonic-gate 		if (errno == ENOENT) {
6290Sstevel@tonic-gate 			dir = 1;
6300Sstevel@tonic-gate 			if ((sp = strrchr(sfile, '/')) == sfile)
6310Sstevel@tonic-gate 				sp++;
6320Sstevel@tonic-gate 			ch = *sp;
6330Sstevel@tonic-gate 			*sp = '\0';
6340Sstevel@tonic-gate 			if (stat(sfile, &stbuf) == -1)
6350Sstevel@tonic-gate 				err_fmt = stat_fmt;
6360Sstevel@tonic-gate 			*sp = ch;
6370Sstevel@tonic-gate 		} else
6380Sstevel@tonic-gate 			err_fmt = stat_fmt;
6390Sstevel@tonic-gate 		if (err_fmt) {
6400Sstevel@tonic-gate 			mesg(MERR, err_fmt, sfile, strerror(errno));
6410Sstevel@tonic-gate 			return (NOUP);
6420Sstevel@tonic-gate 		}
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	/*
6460Sstevel@tonic-gate 	 * Check for regular/dir/block types, set cf_type and dev.
6470Sstevel@tonic-gate 	 */
6480Sstevel@tonic-gate 	if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) {
6490Sstevel@tonic-gate 		new_cc.cf_type = CFT_UFS;
6500Sstevel@tonic-gate 		dev = stbuf.st_dev;
6510Sstevel@tonic-gate 	} else if (S_ISBLK(stbuf.st_mode)) {
6520Sstevel@tonic-gate 		if (minor(stbuf.st_rdev) != 2) {
6530Sstevel@tonic-gate 			new_cc.cf_type = CFT_SPEC;
6540Sstevel@tonic-gate 			dev = stbuf.st_rdev;
6550Sstevel@tonic-gate 		} else
6560Sstevel@tonic-gate 			err_fmt = "statefile device cannot be slice 2 (%s)\n"
6570Sstevel@tonic-gate 			    "would clobber the disk label and boot-block\n";
6580Sstevel@tonic-gate 	} else
6590Sstevel@tonic-gate 		err_fmt = "bad file type for \"%s\"\n"
6600Sstevel@tonic-gate 		    "statefile must be a regular file or block device\n";
6610Sstevel@tonic-gate 	if (err_fmt) {
6620Sstevel@tonic-gate 		mesg(MERR, err_fmt, sfile);
6630Sstevel@tonic-gate 		return (NOUP);
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)) || utop())
6670Sstevel@tonic-gate 		return (NOUP);
6680Sstevel@tonic-gate 	new_cc.cf_magic = CPR_CONFIG_MAGIC;
6690Sstevel@tonic-gate 	statefile = 1;
6700Sstevel@tonic-gate 	return (OKUP);
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate #endif /* sparc */
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate /*
6760Sstevel@tonic-gate  * Try setting system threshold.
6770Sstevel@tonic-gate  */
6780Sstevel@tonic-gate int
6790Sstevel@tonic-gate systhr(void)
6800Sstevel@tonic-gate {
6810Sstevel@tonic-gate 	int value, nerr = 0, upval = OKUP;
6820Sstevel@tonic-gate 	char *thresh = LINEARG(1);
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	if (strcmp(thresh, always_on) == 0)
6850Sstevel@tonic-gate 		value = INT_MAX;
6860Sstevel@tonic-gate 	else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) {
6870Sstevel@tonic-gate 		mesg(MERR, "%s must be a positive value\n", LINEARG(0));
6880Sstevel@tonic-gate 		upval = NOUP;
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 	if (upval == OKUP)
6910Sstevel@tonic-gate 		(void) ioctl(pm_fd, PM_SET_SYSTEM_THRESHOLD, value);
6920Sstevel@tonic-gate 	return (upval);
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate int
6970Sstevel@tonic-gate tchars(void)
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate 	return (scan_int(LINEARG(1), &new_cc.ttychars_thold));
7000Sstevel@tonic-gate }
701