10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53028Smh27603 * Common Development and Distribution License (the "License"). 63028Smh27603 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*6171Smh27603 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include "pmconfig.h" 290Sstevel@tonic-gate #include <sys/mkdev.h> 300Sstevel@tonic-gate #include <sys/syslog.h> 310Sstevel@tonic-gate #include <sys/openpromio.h> 320Sstevel@tonic-gate #include <sys/mnttab.h> 330Sstevel@tonic-gate #include <syslog.h> 340Sstevel@tonic-gate #include <stdlib.h> 355295Srandyf #include <sys/pm.h> 365295Srandyf #include <kstat.h> 375295Srandyf #include <sys/smbios.h> 380Sstevel@tonic-gate 390Sstevel@tonic-gate 400Sstevel@tonic-gate #define STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str) 410Sstevel@tonic-gate #define LASTBYTE(str) (str + strlen(str) - 1) 420Sstevel@tonic-gate 430Sstevel@tonic-gate static char nerr_fmt[] = "number is out of range (%s)\n"; 440Sstevel@tonic-gate static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n"; 450Sstevel@tonic-gate static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n"; 460Sstevel@tonic-gate static char bad_thresh_fmt[] = "bad threshold(s)\n"; 470Sstevel@tonic-gate static char stat_fmt[] = "cannot stat \"%s\", %s\n"; 480Sstevel@tonic-gate static char always_on[] = "always-on"; 490Sstevel@tonic-gate 505295Srandyf #define PM_DEFAULT_ALGORITHM -1 510Sstevel@tonic-gate /* 520Sstevel@tonic-gate * When lines in a config file (usually "/etc/power.conf") start with 530Sstevel@tonic-gate * a recognized keyword, a "handler" routine is called for specific 540Sstevel@tonic-gate * CPR or PM -related action(s). Each routine returns a status code 550Sstevel@tonic-gate * indicating whether all tasks were successful; if any errors occured, 560Sstevel@tonic-gate * future CPR or PM updates are skipped. Following are the handler 570Sstevel@tonic-gate * routines for all keywords: 580Sstevel@tonic-gate */ 590Sstevel@tonic-gate 600Sstevel@tonic-gate 615295Srandyf static char pm_cmd_string[32]; 625295Srandyf 635295Srandyf static char * 645295Srandyf pm_map(int cmd) 655295Srandyf { 665295Srandyf pm_req_t req; 675295Srandyf 685295Srandyf req.value = cmd; 695295Srandyf req.data = (void *)pm_cmd_string; 705295Srandyf req.datasize = sizeof (pm_cmd_string); 715295Srandyf 725295Srandyf if (ioctl(pm_fd, PM_GET_CMD_NAME, &req) < 0) { 73*6171Smh27603 perror(gettext("PM_GET_CMD_NAME failed:")); 745295Srandyf return ("??"); 755295Srandyf } 765295Srandyf return (pm_cmd_string); 775295Srandyf } 785295Srandyf 795295Srandyf static int 805295Srandyf isonlist(char *listname, const char *man, const char *prod) 815295Srandyf { 825295Srandyf pm_searchargs_t sl; 835295Srandyf int ret; 845295Srandyf 855295Srandyf sl.pms_listname = listname; 865295Srandyf sl.pms_manufacturer = (char *)man; 875295Srandyf sl.pms_product = (char *)prod; 885295Srandyf ret = ioctl(pm_fd, PM_SEARCH_LIST, &sl); 895295Srandyf mesg(MDEBUG, "PM_SEARCH_LIST %s for %s,%s returns %d\n", 905295Srandyf listname, man, prod, ret); 915295Srandyf return (ret == 0); 925295Srandyf } 935295Srandyf 945295Srandyf static int 955295Srandyf do_ioctl(int ioctl_cmd, char *keyword, char *behavior, int suppress) 965295Srandyf { 975295Srandyf mesg(MDEBUG, "doing ioctl %s for %s ", pm_map(ioctl_cmd), keyword); 985295Srandyf if (ioctl(pm_fd, ioctl_cmd, NULL) == -1) { 995295Srandyf int suppressed = suppress == -1 || suppress == errno; 1005295Srandyf if (!suppressed) { 1015295Srandyf mesg(MERR, "%s %s failed, %s\n", keyword, behavior, 1025295Srandyf strerror(errno)); 1035295Srandyf return (NOUP); 1045295Srandyf } else { 105*6171Smh27603 mesg(MDEBUG, "%s %s failed, %s\n", keyword, behavior, 106*6171Smh27603 strerror(errno)); 1075295Srandyf return (OKUP); 1085295Srandyf } 1095295Srandyf } 1105295Srandyf mesg(MDEBUG, "succeeded\n"); 1115295Srandyf return (OKUP); 1125295Srandyf } 1135295Srandyf 1140Sstevel@tonic-gate /* 1153028Smh27603 * Check for valid cpupm behavior and communicate it to the kernel. 1163028Smh27603 */ 1173028Smh27603 int 1183028Smh27603 cpupm(void) 1193028Smh27603 { 1203028Smh27603 struct btoc { 1213028Smh27603 char *behavior; 1223028Smh27603 int cmd; 1233028Smh27603 int Errno; 1243028Smh27603 }; 1253028Smh27603 static struct btoc blist[] = { 1263028Smh27603 "disable", PM_STOP_CPUPM, EINVAL, 1273028Smh27603 "enable", PM_START_CPUPM, EBUSY, 1283028Smh27603 NULL, 0, 0 1293028Smh27603 }; 1303028Smh27603 struct btoc *bp; 1313028Smh27603 char *behavior; 1323028Smh27603 1333028Smh27603 for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) { 1343028Smh27603 if (strcmp(behavior, bp->behavior) == 0) 1353028Smh27603 break; 1363028Smh27603 } 1373028Smh27603 if (bp->cmd == 0) { 1383028Smh27603 mesg(MERR, "invalid cpupm behavior \"%s\"\n", behavior); 1393028Smh27603 return (NOUP); 1403028Smh27603 } 1413028Smh27603 if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) { 1423028Smh27603 mesg(MERR, "cpupm %s failed, %s\n", 1433028Smh27603 behavior, strerror(errno)); 1443028Smh27603 return (NOUP); 1453028Smh27603 } 1463028Smh27603 return (OKUP); 1473028Smh27603 } 1483028Smh27603 1493028Smh27603 1503028Smh27603 /* 1515295Srandyf * Two decisions are identical except for the list names and ioctl commands 1525295Srandyf * inputs: whitelist, blacklist, yes, no 1535295Srandyf * if (! ("S3" kstat exists)) 1545295Srandyf * return (no) 1555295Srandyf * if (SystemInformation.Manufacturer == "Sun Microsystems" && 1565295Srandyf * (Pref_PM_Profile == Workstation || Pref_PM_Profile == Desktop)) { 1575295Srandyf * if (platform on blacklist) 1585295Srandyf * return (no) 1595295Srandyf * return (yes) 1605295Srandyf * } else { 1615295Srandyf * if (platform on whitelist) 1625295Srandyf * return (yes) 1635295Srandyf * return (no) 1645295Srandyf * } 1655295Srandyf */ 1665295Srandyf 1675295Srandyf int 1685295Srandyf S3_helper(char *whitelist, char *blacklist, int yes, int no, char *keyword, 1695295Srandyf char *behavior, int *didyes, int suppress) 1705295Srandyf { 1715295Srandyf int oflags = SMB_O_NOCKSUM | SMB_O_NOVERS; 1725295Srandyf smbios_hdl_t *shp; 1735295Srandyf smbios_system_t sys; 1745295Srandyf id_t id; 1755295Srandyf int ret; 1765295Srandyf kstat_ctl_t *kc; 1775295Srandyf kstat_t *ksp; 1785295Srandyf kstat_named_t *dp; 1795295Srandyf smbios_info_t info; 1805295Srandyf int preferred_pm_profile = 0; 1815295Srandyf char yesstr[32], nostr[32]; /* DEBUG */ 1825295Srandyf 1835295Srandyf *didyes = 0; 1845295Srandyf 1855295Srandyf strncpy(yesstr, pm_map(yes), sizeof (yesstr)); 1865295Srandyf strncpy(nostr, pm_map(no), sizeof (nostr)); 1875295Srandyf mesg(MDEBUG, "S3_helper(%s, %s, %s, %s, %s, %s)\n", whitelist, 1885295Srandyf blacklist, yesstr, nostr, keyword, behavior); 1895295Srandyf if ((kc = kstat_open()) == NULL) { 1905295Srandyf mesg(MDEBUG, "kstat_open failed\n"); 1915295Srandyf return (OKUP); 1925295Srandyf } 1935295Srandyf ksp = kstat_lookup(kc, "acpi", -1, "acpi"); 1945295Srandyf if (ksp == NULL) { 1955295Srandyf mesg(MDEBUG, "kstat_lookup 'acpi', -1, 'acpi' failed\n"); 1965295Srandyf kstat_close(kc); 1975295Srandyf return (OKUP); 1985295Srandyf } 1995295Srandyf (void) kstat_read(kc, ksp, NULL); 2005295Srandyf dp = kstat_data_lookup(ksp, "S3"); 2015295Srandyf if (dp == NULL || dp->value.l == 0) { 2025295Srandyf mesg(MDEBUG, "kstat_data_lookup 'S3' fails\n"); 2035295Srandyf if (dp != NULL) 2045295Srandyf mesg(MDEBUG, "value.l %lx\n", dp->value.l); 2055295Srandyf kstat_close(kc); 2065295Srandyf return (do_ioctl(no, keyword, behavior, suppress)); 2075295Srandyf } 2085295Srandyf mesg(MDEBUG, "kstat indicates S3 support (%lx)\n", dp->value.l); 2095295Srandyf 2105295Srandyf if (!whitelist_only) { 2115295Srandyf /* 2125295Srandyf * We still have an ACPI ksp, search it again for 2135295Srandyf * 'preferred_pm_profile' (needs to be valid if we don't 2145295Srandyf * aren't only using a whitelist). 2155295Srandyf */ 2165295Srandyf dp = kstat_data_lookup(ksp, "preferred_pm_profile"); 2175295Srandyf if (dp == NULL) { 2185295Srandyf mesg(MDEBUG, "kstat_data_lookup 'ppmp fails\n"); 2195295Srandyf kstat_close(kc); 2205295Srandyf return (do_ioctl(no, keyword, behavior, suppress)); 2215295Srandyf } 222*6171Smh27603 mesg(MDEBUG, "kstat indicates preferred_pm_profile is %lx\n", 2235295Srandyf dp->value.l); 2245295Srandyf preferred_pm_profile = dp->value.l; 2255295Srandyf } 2265295Srandyf kstat_close(kc); 2275295Srandyf 2285295Srandyf if ((shp = smbios_open(NULL, 2295295Srandyf SMB_VERSION, oflags, &ret)) == NULL) { 2305295Srandyf /* we promised not to complain */ 2315295Srandyf /* we bail leaving it to the kernel default */ 2325295Srandyf mesg(MDEBUG, "smbios_open failed %d\n", errno); 2335295Srandyf return (OKUP); 2345295Srandyf } 2355295Srandyf if ((id = smbios_info_system(shp, &sys)) == SMB_ERR) { 2365295Srandyf mesg(MDEBUG, "smbios_info_system failed %d\n", errno); 2375295Srandyf smbios_close(shp); 2385295Srandyf return (OKUP); 2395295Srandyf } 2405295Srandyf if (smbios_info_common(shp, id, &info) == SMB_ERR) { 2415295Srandyf mesg(MDEBUG, "smbios_info_common failed %d\n", errno); 2425295Srandyf smbios_close(shp); 2435295Srandyf return (OKUP); 2445295Srandyf } 2455295Srandyf mesg(MDEBUG, "Manufacturer: %s\n", info.smbi_manufacturer); 2465295Srandyf mesg(MDEBUG, "Product: %s\n", info.smbi_product); 2475295Srandyf smbios_close(shp); 2485295Srandyf 2495295Srandyf if (!whitelist_only) { 2505295Srandyf #define PPP_DESKTOP 1 2515295Srandyf #define PPP_WORKSTATION 3 2525295Srandyf if (strcmp(info.smbi_manufacturer, "Sun Microsystems") == 0 && 2535295Srandyf (preferred_pm_profile == PPP_DESKTOP || 2545295Srandyf preferred_pm_profile == PPP_WORKSTATION)) { 2555295Srandyf if (isonlist(blacklist, 2565295Srandyf info.smbi_manufacturer, info.smbi_product)) { 2575295Srandyf return (do_ioctl(no, keyword, behavior, 2585295Srandyf suppress)); 2595295Srandyf } else { 2605295Srandyf ret = do_ioctl(yes, keyword, behavior, 2615295Srandyf suppress); 2625295Srandyf *didyes = (ret == OKUP); 2635295Srandyf return (ret); 2645295Srandyf } 2655295Srandyf } 2665295Srandyf } 2675295Srandyf if (isonlist(whitelist, 2685295Srandyf info.smbi_manufacturer, info.smbi_product)) { 2695295Srandyf ret = do_ioctl(yes, keyword, behavior, suppress); 2705295Srandyf *didyes = (ret == OKUP); 2715295Srandyf return (ret); 2725295Srandyf } else { 2735295Srandyf return (do_ioctl(no, keyword, behavior, suppress)); 2745295Srandyf } 2755295Srandyf } 2765295Srandyf 2775295Srandyf int 2785295Srandyf S3sup(void) /* S3-support keyword handler */ 2795295Srandyf { 2805295Srandyf struct btoc { 2815295Srandyf char *behavior; 2825295Srandyf int cmd; 2835295Srandyf }; 2845295Srandyf static struct btoc blist[] = { 2855295Srandyf "default", PM_DEFAULT_ALGORITHM, 2865295Srandyf "enable", PM_ENABLE_S3, 2875295Srandyf "disable", PM_DISABLE_S3, 2885295Srandyf NULL, 0 2895295Srandyf }; 2905295Srandyf struct btoc *bp; 2915295Srandyf char *behavior; 2925295Srandyf int dontcare; 2935295Srandyf 2945295Srandyf for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) { 2955295Srandyf if (strcmp(behavior, bp->behavior) == 0) 2965295Srandyf break; 2975295Srandyf } 2985295Srandyf if (bp->cmd == 0) { 2995295Srandyf mesg(MERR, "invalid S3-support behavior \"%s\"\n", behavior); 3005295Srandyf return (NOUP); 3015295Srandyf } 3025295Srandyf 3035295Srandyf 3045295Srandyf switch (bp->cmd) { 3055295Srandyf 3065295Srandyf case PM_ENABLE_S3: 3075295Srandyf case PM_DISABLE_S3: 3085295Srandyf return (do_ioctl(bp->cmd, "S3-support", behavior, EBUSY)); 3095295Srandyf 3105295Srandyf case PM_DEFAULT_ALGORITHM: 3115295Srandyf /* 3125295Srandyf * we suppress errors in the "default" case because we 3135295Srandyf * already did an invisible default call, so we know we'll 3145295Srandyf * get EBUSY 3155295Srandyf */ 3165295Srandyf return (S3_helper("S3-support-enable", "S3-support-disable", 3175295Srandyf PM_ENABLE_S3, PM_DISABLE_S3, "S3-support", behavior, 3185295Srandyf &dontcare, EBUSY)); 3195295Srandyf 3205295Srandyf default: 3215295Srandyf mesg(MERR, "S3-support %s failed, %s\n", behavior, 3225295Srandyf strerror(errno)); 3235295Srandyf return (NOUP); 3245295Srandyf } 3255295Srandyf } 3265295Srandyf 3275295Srandyf /* 3285295Srandyf * Check for valid autoS3 behavior and save after ioctl success. 3295295Srandyf */ 3305295Srandyf int 3315295Srandyf autoS3(void) 3325295Srandyf { 3335295Srandyf struct btoc { 3345295Srandyf char *behavior; 3355295Srandyf int cmd; 3365295Srandyf }; 3375295Srandyf static struct btoc blist[] = { 3385295Srandyf "default", PM_DEFAULT_ALGORITHM, 3395295Srandyf "disable", PM_STOP_AUTOS3, 3405295Srandyf "enable", PM_START_AUTOS3, 3415295Srandyf NULL, 0 3425295Srandyf }; 3435295Srandyf struct btoc *bp; 3445295Srandyf char *behavior; 3455295Srandyf int dontcare; 3465295Srandyf 3475295Srandyf for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) { 3485295Srandyf if (strcmp(behavior, bp->behavior) == 0) 3495295Srandyf break; 3505295Srandyf } 3515295Srandyf if (bp->cmd == 0) { 3525295Srandyf mesg(MERR, "invalid autoS3 behavior \"%s\"\n", behavior); 3535295Srandyf return (NOUP); 3545295Srandyf } 3555295Srandyf 3565295Srandyf switch (bp->cmd) { 3575295Srandyf default: 3585295Srandyf mesg(MERR, "autoS3 %s failed, %s\n", 3595295Srandyf behavior, strerror(errno)); 3605295Srandyf mesg(MDEBUG, "unknown command\n", bp->cmd); 3615295Srandyf return (OKUP); 3625295Srandyf 3635295Srandyf case PM_STOP_AUTOS3: 3645295Srandyf case PM_START_AUTOS3: 3655295Srandyf return (do_ioctl(bp->cmd, "autoS3", behavior, EBUSY)); 3665295Srandyf 3675295Srandyf case PM_DEFAULT_ALGORITHM: 3685295Srandyf return (S3_helper("S3-autoenable", "S3-autodisable", 3695295Srandyf PM_START_AUTOS3, PM_STOP_AUTOS3, "autoS3", behavior, 3705295Srandyf &dontcare, EBUSY)); 3715295Srandyf } 3725295Srandyf } 3735295Srandyf 3745295Srandyf 3755295Srandyf /* 3760Sstevel@tonic-gate * Check for valid autopm behavior and save after ioctl success. 3770Sstevel@tonic-gate */ 3780Sstevel@tonic-gate int 3790Sstevel@tonic-gate autopm(void) 3800Sstevel@tonic-gate { 3810Sstevel@tonic-gate struct btoc { 3820Sstevel@tonic-gate char *behavior; 3830Sstevel@tonic-gate int cmd, Errno, isdef; 3840Sstevel@tonic-gate }; 3850Sstevel@tonic-gate static struct btoc blist[] = { 3865295Srandyf "default", PM_START_PM, -1, 1, 3870Sstevel@tonic-gate "disable", PM_STOP_PM, EINVAL, 0, 3880Sstevel@tonic-gate "enable", PM_START_PM, EBUSY, 0, 3890Sstevel@tonic-gate NULL, 0, 0, 0, 3900Sstevel@tonic-gate }; 3910Sstevel@tonic-gate struct btoc *bp; 3920Sstevel@tonic-gate char *behavior; 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) { 3950Sstevel@tonic-gate if (strcmp(behavior, bp->behavior) == 0) 3960Sstevel@tonic-gate break; 3970Sstevel@tonic-gate } 3980Sstevel@tonic-gate if (bp->cmd == 0) { 3990Sstevel@tonic-gate mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior); 4000Sstevel@tonic-gate return (NOUP); 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate /* 4040Sstevel@tonic-gate * for "default" behavior, do not enable autopm if not ESTAR_V3 4050Sstevel@tonic-gate */ 4065295Srandyf #if defined(__sparc) 4070Sstevel@tonic-gate if (!bp->isdef || (estar_vers == ESTAR_V3)) { 4080Sstevel@tonic-gate if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) { 4090Sstevel@tonic-gate mesg(MERR, "autopm %s failed, %s\n", 4100Sstevel@tonic-gate behavior, strerror(errno)); 4110Sstevel@tonic-gate return (NOUP); 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate (void) strcpy(new_cc.apm_behavior, behavior); 4150Sstevel@tonic-gate return (OKUP); 4165295Srandyf #endif 4175295Srandyf #if defined(__x86) 4185295Srandyf if (!bp->isdef) { 4195295Srandyf if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) { 4205295Srandyf mesg(MERR, "autopm %s failed, %s\n", 4215295Srandyf behavior, strerror(errno)); 4225295Srandyf return (NOUP); 4235295Srandyf } 4245295Srandyf mesg(MDEBUG, "autopm %s succeeded\n", behavior); 4255295Srandyf 4265295Srandyf return (OKUP); 4275295Srandyf } else { 4285295Srandyf int didenable; 4295295Srandyf int ret = S3_helper("autopm-enable", "autopm-disable", 4305295Srandyf PM_START_PM, PM_STOP_PM, "autopm", behavior, &didenable, 4315295Srandyf bp->Errno); 4325295Srandyf if (didenable) { 4335295Srandyf /* tell powerd to attach all devices */ 4345295Srandyf new_cc.is_autopm_default = 1; 4355295Srandyf (void) strcpy(new_cc.apm_behavior, behavior); 4365295Srandyf } 4375295Srandyf return (ret); 4385295Srandyf } 4395295Srandyf #endif 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate static int 4440Sstevel@tonic-gate gethm(char *src, int *hour, int *min) 4450Sstevel@tonic-gate { 4460Sstevel@tonic-gate if (sscanf(src, "%d:%d", hour, min) != 2) { 4470Sstevel@tonic-gate mesg(MERR, "bad time format (%s)\n", src); 4480Sstevel@tonic-gate return (-1); 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate return (0); 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate static void 4550Sstevel@tonic-gate strcpy_limit(char *dst, char *src, size_t limit, char *info) 4560Sstevel@tonic-gate { 4570Sstevel@tonic-gate if (strlcpy(dst, src, limit) >= limit) 4580Sstevel@tonic-gate mesg(MEXIT, "%s is too long (%s)\n", info, src); 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate /* 4630Sstevel@tonic-gate * Convert autoshutdown idle and start/finish times; 4640Sstevel@tonic-gate * check and record autoshutdown behavior. 4650Sstevel@tonic-gate */ 4660Sstevel@tonic-gate int 4670Sstevel@tonic-gate autosd(void) 4680Sstevel@tonic-gate { 469763Smh27603 char **bp, *behavior; 470763Smh27603 char *unrec = gettext("unrecognized autoshutdown behavior"); 4710Sstevel@tonic-gate static char *blist[] = { 4720Sstevel@tonic-gate "autowakeup", "default", "noshutdown", 4730Sstevel@tonic-gate "shutdown", "unconfigured", NULL 4740Sstevel@tonic-gate }; 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate new_cc.as_idle = atoi(LINEARG(1)); 4770Sstevel@tonic-gate if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) || 4780Sstevel@tonic-gate gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm)) 4790Sstevel@tonic-gate return (NOUP); 480763Smh27603 mesg(MDEBUG, "idle %d, start %d:%02d, finish %d:%02d\n", 4810Sstevel@tonic-gate new_cc.as_idle, new_cc.as_sh, new_cc.as_sm, 4820Sstevel@tonic-gate new_cc.as_fh, new_cc.as_fm); 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate for (behavior = LINEARG(4), bp = blist; *bp; bp++) { 4850Sstevel@tonic-gate if (strcmp(behavior, *bp) == 0) 4860Sstevel@tonic-gate break; 4870Sstevel@tonic-gate } 4880Sstevel@tonic-gate if (*bp == NULL) { 4890Sstevel@tonic-gate mesg(MERR, "%s: \"%s\"\n", unrec, behavior); 4900Sstevel@tonic-gate return (NOUP); 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate STRCPYLIM(new_cc.as_behavior, *bp, unrec); 4930Sstevel@tonic-gate return (OKUP); 4940Sstevel@tonic-gate } 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate /* 4980Sstevel@tonic-gate * Check for a real device and try to resolve to a full path. 4990Sstevel@tonic-gate * The orig/resolved path may be modified into a prom pathname, 5000Sstevel@tonic-gate * and an allocated copy of the result is stored at *destp; 5010Sstevel@tonic-gate * the caller will need to free that space. Returns 1 for any 5020Sstevel@tonic-gate * error, otherwise 0; also sets *errp after an alloc error. 5030Sstevel@tonic-gate */ 5040Sstevel@tonic-gate static int 5050Sstevel@tonic-gate devpath(char **destp, char *src, int *errp) 5060Sstevel@tonic-gate { 5070Sstevel@tonic-gate struct stat stbuf; 5080Sstevel@tonic-gate char buf[PATH_MAX]; 5090Sstevel@tonic-gate char *cp, *dstr; 5100Sstevel@tonic-gate int devok, dcs = 0; 5110Sstevel@tonic-gate size_t len; 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate /* 5140Sstevel@tonic-gate * When there's a real device, try to resolve the path 5150Sstevel@tonic-gate * and trim the leading "/devices" component. 5160Sstevel@tonic-gate */ 5170Sstevel@tonic-gate if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) { 5180Sstevel@tonic-gate if (realpath(src, buf) == NULL) { 5190Sstevel@tonic-gate mesg(MERR, "realpath cannot resolve \"%s\"\n", 5200Sstevel@tonic-gate src, strerror(errno)); 5210Sstevel@tonic-gate return (1); 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate src = buf; 5240Sstevel@tonic-gate dstr = "/devices"; 5250Sstevel@tonic-gate len = strlen(dstr); 5260Sstevel@tonic-gate dcs = (strncmp(src, dstr, len) == 0); 5270Sstevel@tonic-gate if (dcs) 5280Sstevel@tonic-gate src += len; 5290Sstevel@tonic-gate } else 5300Sstevel@tonic-gate mesg(MDEBUG, stat_fmt, src, strerror(errno)); 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate /* 5330Sstevel@tonic-gate * When the path has ":anything", display an error for 5340Sstevel@tonic-gate * a non-device or truncate a resolved+modifed path. 5350Sstevel@tonic-gate */ 5360Sstevel@tonic-gate if (cp = strchr(src, ':')) { 5370Sstevel@tonic-gate if (devok == 0) { 5380Sstevel@tonic-gate mesg(MERR, "physical path may not contain " 5390Sstevel@tonic-gate "a minor string (%s)\n", src); 5400Sstevel@tonic-gate return (1); 5410Sstevel@tonic-gate } else if (dcs) 5420Sstevel@tonic-gate *cp = '\0'; 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate if ((*destp = strdup(src)) == NULL) { 5460Sstevel@tonic-gate *errp = NOUP; 5470Sstevel@tonic-gate mesg(MERR, alloc_fmt, src, strerror(errno)); 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate return (*destp == NULL); 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate /* 5540Sstevel@tonic-gate * Call pm ioctl request(s) to set property/device dependencies. 5550Sstevel@tonic-gate */ 5560Sstevel@tonic-gate static int 5570Sstevel@tonic-gate dev_dep_common(int isprop) 5580Sstevel@tonic-gate { 5590Sstevel@tonic-gate int cmd, argn, upval = OKUP; 5600Sstevel@tonic-gate char *src, *first, **destp; 5610Sstevel@tonic-gate pm_req_t pmreq; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate bzero(&pmreq, sizeof (pmreq)); 5640Sstevel@tonic-gate src = LINEARG(1); 5650Sstevel@tonic-gate if (isprop) { 5660Sstevel@tonic-gate cmd = PM_ADD_DEPENDENT_PROPERTY; 5670Sstevel@tonic-gate first = NULL; 5680Sstevel@tonic-gate pmreq.pmreq_kept = src; 5690Sstevel@tonic-gate } else { 5700Sstevel@tonic-gate cmd = PM_ADD_DEPENDENT; 5710Sstevel@tonic-gate if (devpath(&first, src, &upval)) 5720Sstevel@tonic-gate return (upval); 5730Sstevel@tonic-gate pmreq.pmreq_kept = first; 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate destp = &pmreq.pmreq_keeper; 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate /* 5780Sstevel@tonic-gate * Now loop through any dependents. 5790Sstevel@tonic-gate */ 5800Sstevel@tonic-gate for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) { 5810Sstevel@tonic-gate if (devpath(destp, src, &upval)) { 5820Sstevel@tonic-gate if (upval != OKUP) 5830Sstevel@tonic-gate return (upval); 5840Sstevel@tonic-gate break; 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) { 5870Sstevel@tonic-gate mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n" 5880Sstevel@tonic-gate "kept \"%s\", keeper \"%s\"\n", 5890Sstevel@tonic-gate cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper); 5900Sstevel@tonic-gate mesg(MERR, "cannot set \"%s\" dependency " 5910Sstevel@tonic-gate "for \"%s\", %s\n", pmreq.pmreq_keeper, 5920Sstevel@tonic-gate pmreq.pmreq_kept, strerror(errno)); 5930Sstevel@tonic-gate } 5940Sstevel@tonic-gate free(*destp); 5950Sstevel@tonic-gate *destp = NULL; 5960Sstevel@tonic-gate if (upval != OKUP) 5970Sstevel@tonic-gate break; 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate free(first); 6010Sstevel@tonic-gate return (upval); 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate int 6060Sstevel@tonic-gate ddprop(void) 6070Sstevel@tonic-gate { 6080Sstevel@tonic-gate return (dev_dep_common(1)); 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate int 6130Sstevel@tonic-gate devdep(void) 6140Sstevel@tonic-gate { 6150Sstevel@tonic-gate return (dev_dep_common(0)); 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate /* 6200Sstevel@tonic-gate * Convert a numeric string (with a possible trailing scaling byte) 6210Sstevel@tonic-gate * into an integer. Returns a converted value and *nerrp unchanged, 6220Sstevel@tonic-gate * or 0 with *nerrp set to 1 for a conversion error. 6230Sstevel@tonic-gate */ 6240Sstevel@tonic-gate static int 6250Sstevel@tonic-gate get_scaled_value(char *str, int *nerrp) 6260Sstevel@tonic-gate { 6270Sstevel@tonic-gate longlong_t svalue = 0, factor = 1; 6280Sstevel@tonic-gate char *sp; 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate errno = 0; 6310Sstevel@tonic-gate svalue = strtol(str, &sp, 0); 6320Sstevel@tonic-gate if (errno || (*str != '-' && (*str < '0' || *str > '9'))) 6330Sstevel@tonic-gate *nerrp = 1; 6340Sstevel@tonic-gate else if (sp && *sp != '\0') { 6350Sstevel@tonic-gate if (*sp == 'h') 6360Sstevel@tonic-gate factor = 3600; 6370Sstevel@tonic-gate else if (*sp == 'm') 6380Sstevel@tonic-gate factor = 60; 6390Sstevel@tonic-gate else if (*sp != 's') 6400Sstevel@tonic-gate *nerrp = 1; 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate /* any bytes following sp are ignored */ 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate if (*nerrp == 0) { 6450Sstevel@tonic-gate svalue *= factor; 6460Sstevel@tonic-gate if (svalue < INT_MIN || svalue > INT_MAX) 6470Sstevel@tonic-gate *nerrp = 1; 6480Sstevel@tonic-gate } 6490Sstevel@tonic-gate if (*nerrp) 6500Sstevel@tonic-gate mesg(MERR, nerr_fmt, str); 6510Sstevel@tonic-gate mesg(MDEBUG, "got scaled value %d\n", (int)svalue); 6520Sstevel@tonic-gate return ((int)svalue); 6530Sstevel@tonic-gate } 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate /* 6570Sstevel@tonic-gate * Increment the count of threshold values, 6580Sstevel@tonic-gate * reallocate *vlistp and append another element. 6590Sstevel@tonic-gate * Returns 1 on error, otherwise 0. 6600Sstevel@tonic-gate */ 6610Sstevel@tonic-gate static int 6620Sstevel@tonic-gate vlist_append(int **vlistp, int *vcntp, int value) 6630Sstevel@tonic-gate { 6640Sstevel@tonic-gate (*vcntp)++; 6650Sstevel@tonic-gate if (*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp))) 6660Sstevel@tonic-gate *(*vlistp + *vcntp - 1) = value; 6670Sstevel@tonic-gate else 6680Sstevel@tonic-gate mesg(MERR, alloc_fmt, "threshold list", strerror(errno)); 6690Sstevel@tonic-gate return (*vlistp == NULL); 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * Convert a single threshold string or paren groups of thresh's as 6750Sstevel@tonic-gate * described below. All thresh's are saved to an allocated list at 6760Sstevel@tonic-gate * *vlistp; the caller will need to free that space. On return: 6770Sstevel@tonic-gate * *vcntp is the count of the vlist array, and vlist is either 6780Sstevel@tonic-gate * a single thresh or N groups of thresh's with a trailing zero: 6790Sstevel@tonic-gate * (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0. 6800Sstevel@tonic-gate * Returns 0 when all conversions were OK, and 1 for any syntax, 6810Sstevel@tonic-gate * conversion, or alloc error. 6820Sstevel@tonic-gate */ 6830Sstevel@tonic-gate static int 6840Sstevel@tonic-gate get_thresh(int **vlistp, int *vcntp) 6850Sstevel@tonic-gate { 6860Sstevel@tonic-gate int argn, value, gci, grp_cnt = 0, paren = 0, nerr = 0; 6870Sstevel@tonic-gate char *rp, *src; 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) { 6900Sstevel@tonic-gate if (*src == LPAREN) { 6910Sstevel@tonic-gate gci = *vcntp; 6920Sstevel@tonic-gate if (nerr = vlist_append(vlistp, vcntp, 0)) 6930Sstevel@tonic-gate break; 6940Sstevel@tonic-gate paren = 1; 6950Sstevel@tonic-gate src++; 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate if (*(rp = LASTBYTE(src)) == RPAREN) { 6980Sstevel@tonic-gate if (paren) { 6990Sstevel@tonic-gate grp_cnt = *vcntp - gci; 7000Sstevel@tonic-gate *(*vlistp + gci) = grp_cnt; 7010Sstevel@tonic-gate paren = 0; 7020Sstevel@tonic-gate *rp = '\0'; 7030Sstevel@tonic-gate } else { 7040Sstevel@tonic-gate nerr = 1; 7050Sstevel@tonic-gate break; 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate 7090Sstevel@tonic-gate value = get_scaled_value(src, &nerr); 7100Sstevel@tonic-gate if (nerr || (nerr = vlist_append(vlistp, vcntp, value))) 7110Sstevel@tonic-gate break; 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate if (nerr == 0 && grp_cnt) 7150Sstevel@tonic-gate nerr = vlist_append(vlistp, vcntp, 0); 7160Sstevel@tonic-gate return (nerr); 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate /* 7210Sstevel@tonic-gate * Set device thresholds from (3) formats: 7220Sstevel@tonic-gate * path "always-on" 7230Sstevel@tonic-gate * path time-spec: [0-9]+[{h,m,s}] 7240Sstevel@tonic-gate * path (ts1 ts2 ...)+ 7250Sstevel@tonic-gate */ 7260Sstevel@tonic-gate int 7270Sstevel@tonic-gate devthr(void) 7280Sstevel@tonic-gate { 7290Sstevel@tonic-gate int cmd, upval = OKUP, nthresh = 0, *vlist = NULL; 7300Sstevel@tonic-gate pm_req_t pmreq; 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate bzero(&pmreq, sizeof (pmreq)); 7330Sstevel@tonic-gate if (devpath(&pmreq.physpath, LINEARG(1), &upval)) 7340Sstevel@tonic-gate return (upval); 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate if (strcmp(LINEARG(2), always_on) == 0) { 7370Sstevel@tonic-gate cmd = PM_SET_DEVICE_THRESHOLD; 7380Sstevel@tonic-gate pmreq.value = INT_MAX; 7390Sstevel@tonic-gate } else if (get_thresh(&vlist, &nthresh)) { 7400Sstevel@tonic-gate mesg(MERR, bad_thresh_fmt); 7410Sstevel@tonic-gate upval = NOUP; 7420Sstevel@tonic-gate } else if (nthresh == 1) { 7430Sstevel@tonic-gate pmreq.value = *vlist; 7440Sstevel@tonic-gate cmd = PM_SET_DEVICE_THRESHOLD; 7450Sstevel@tonic-gate } else { 7460Sstevel@tonic-gate pmreq.data = vlist; 7470Sstevel@tonic-gate pmreq.datasize = (nthresh * sizeof (*vlist)); 7480Sstevel@tonic-gate cmd = PM_SET_COMPONENT_THRESHOLDS; 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1) 7520Sstevel@tonic-gate mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno)); 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate free(vlist); 7550Sstevel@tonic-gate free(pmreq.physpath); 7560Sstevel@tonic-gate return (upval); 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate static int 7610Sstevel@tonic-gate scan_int(char *src, int *dst) 7620Sstevel@tonic-gate { 7630Sstevel@tonic-gate long lval; 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate errno = 0; 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate lval = strtol(LINEARG(1), NULL, 0); 7680Sstevel@tonic-gate if (errno || lval > INT_MAX || lval < 0) { 7690Sstevel@tonic-gate mesg(MERR, nerr_fmt, src); 7700Sstevel@tonic-gate return (NOUP); 7710Sstevel@tonic-gate } 7720Sstevel@tonic-gate 7730Sstevel@tonic-gate *dst = (int)lval; 7740Sstevel@tonic-gate return (OKUP); 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate static int 7780Sstevel@tonic-gate scan_float(char *src, float *dst) 7790Sstevel@tonic-gate { 7800Sstevel@tonic-gate float fval; 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate errno = 0; 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate fval = strtof(src, NULL); 7850Sstevel@tonic-gate if (errno || fval < 0.0) { 7860Sstevel@tonic-gate mesg(MERR, nerr_fmt, src); 7870Sstevel@tonic-gate return (NOUP); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate *dst = fval; 7910Sstevel@tonic-gate return (OKUP); 7920Sstevel@tonic-gate } 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate int 7960Sstevel@tonic-gate dreads(void) 7970Sstevel@tonic-gate { 7980Sstevel@tonic-gate return (scan_int(LINEARG(1), &new_cc.diskreads_thold)); 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate /* 8030Sstevel@tonic-gate * Set pathname for idlecheck; 8040Sstevel@tonic-gate * an overflowed pathname is treated as a fatal error. 8050Sstevel@tonic-gate */ 8060Sstevel@tonic-gate int 8070Sstevel@tonic-gate idlechk(void) 8080Sstevel@tonic-gate { 8090Sstevel@tonic-gate STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path"); 8100Sstevel@tonic-gate return (OKUP); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate int 8150Sstevel@tonic-gate loadavg(void) 8160Sstevel@tonic-gate { 8170Sstevel@tonic-gate return (scan_float(LINEARG(1), &new_cc.loadaverage_thold)); 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate int 8220Sstevel@tonic-gate nfsreq(void) 8230Sstevel@tonic-gate { 8240Sstevel@tonic-gate return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold)); 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate #ifdef sparc 8290Sstevel@tonic-gate static char open_fmt[] = "cannot open \"%s\", %s\n"; 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate /* 8320Sstevel@tonic-gate * Verify the filesystem type for a regular statefile is "ufs" 8330Sstevel@tonic-gate * or verify a block device is not in use as a mounted filesytem. 8340Sstevel@tonic-gate * Returns 1 if any error, otherwise 0. 8350Sstevel@tonic-gate */ 8360Sstevel@tonic-gate static int 8370Sstevel@tonic-gate check_mount(char *sfile, dev_t sfdev, int ufs) 8380Sstevel@tonic-gate { 8390Sstevel@tonic-gate char *src, *err_fmt = NULL, *mnttab = MNTTAB; 8400Sstevel@tonic-gate int rgent, match = 0; 8410Sstevel@tonic-gate struct extmnttab ent; 8420Sstevel@tonic-gate FILE *fp; 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate if ((fp = fopen(mnttab, "r")) == NULL) { 8450Sstevel@tonic-gate mesg(MERR, open_fmt, mnttab, strerror(errno)); 8460Sstevel@tonic-gate return (1); 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate /* 8500Sstevel@tonic-gate * Search for a matching dev_t; 8510Sstevel@tonic-gate * ignore non-ufs filesystems for a regular statefile. 8520Sstevel@tonic-gate */ 8530Sstevel@tonic-gate while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) { 8540Sstevel@tonic-gate if (rgent > 0) { 8550Sstevel@tonic-gate mesg(MERR, "error reading \"%s\"\n", mnttab); 8560Sstevel@tonic-gate (void) fclose(fp); 8570Sstevel@tonic-gate return (1); 8580Sstevel@tonic-gate } else if (ufs && strcmp(ent.mnt_fstype, "ufs")) 8590Sstevel@tonic-gate continue; 8600Sstevel@tonic-gate else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) { 8610Sstevel@tonic-gate match = 1; 8620Sstevel@tonic-gate break; 8630Sstevel@tonic-gate } 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate (void) fclose(fp); 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate /* 8680Sstevel@tonic-gate * No match is needed for a block device statefile, 8690Sstevel@tonic-gate * a match is needed for a regular statefile. 8700Sstevel@tonic-gate */ 8710Sstevel@tonic-gate if (match == 0) { 8720Sstevel@tonic-gate if (new_cc.cf_type == CFT_SPEC) 8730Sstevel@tonic-gate STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile"); 8740Sstevel@tonic-gate else 8750Sstevel@tonic-gate err_fmt = "cannot find ufs mount point for \"%s\"\n"; 8760Sstevel@tonic-gate } else if (new_cc.cf_type == CFT_UFS) { 8770Sstevel@tonic-gate STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry"); 8780Sstevel@tonic-gate STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special"); 8790Sstevel@tonic-gate while (*(sfile + 1) == '/') sfile++; 8800Sstevel@tonic-gate src = sfile + strlen(ent.mnt_mountp); 8810Sstevel@tonic-gate while (*src == '/') src++; 8820Sstevel@tonic-gate STRCPYLIM(new_cc.cf_path, src, "statefile path"); 8830Sstevel@tonic-gate } else 8840Sstevel@tonic-gate err_fmt = "statefile device \"%s\" is a mounted filesystem\n"; 8850Sstevel@tonic-gate if (err_fmt) 8860Sstevel@tonic-gate mesg(MERR, err_fmt, sfile); 8870Sstevel@tonic-gate return (err_fmt != NULL); 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate /* 8920Sstevel@tonic-gate * Convert a Unix device to a prom device and save on success, 8930Sstevel@tonic-gate * log any ioctl/conversion error. 8940Sstevel@tonic-gate */ 8950Sstevel@tonic-gate static int 8960Sstevel@tonic-gate utop(void) 8970Sstevel@tonic-gate { 8980Sstevel@tonic-gate union obpbuf { 8990Sstevel@tonic-gate char buf[OBP_MAXPATHLEN + sizeof (uint_t)]; 9000Sstevel@tonic-gate struct openpromio oppio; 9010Sstevel@tonic-gate }; 9020Sstevel@tonic-gate union obpbuf oppbuf; 9030Sstevel@tonic-gate struct openpromio *opp; 9040Sstevel@tonic-gate char *promdev = "/dev/openprom"; 9050Sstevel@tonic-gate int fd, upval; 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate if ((fd = open(promdev, O_RDONLY)) == -1) { 9080Sstevel@tonic-gate mesg(MERR, open_fmt, promdev, strerror(errno)); 9090Sstevel@tonic-gate return (NOUP); 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate opp = &oppbuf.oppio; 9130Sstevel@tonic-gate opp->oprom_size = OBP_MAXPATHLEN; 9140Sstevel@tonic-gate strcpy_limit(opp->oprom_array, new_cc.cf_devfs, 9150Sstevel@tonic-gate OBP_MAXPATHLEN, "statefile device"); 9160Sstevel@tonic-gate upval = ioctl(fd, OPROMDEV2PROMNAME, opp); 9170Sstevel@tonic-gate (void) close(fd); 9180Sstevel@tonic-gate if (upval == OKUP) 9190Sstevel@tonic-gate STRCPYLIM(new_cc.cf_dev_prom, opp->oprom_array, "prom device"); 9200Sstevel@tonic-gate else { 9210Sstevel@tonic-gate openlog("pmconfig", 0, LOG_DAEMON); 9220Sstevel@tonic-gate syslog(LOG_NOTICE, 9230Sstevel@tonic-gate gettext("cannot convert \"%s\" to prom device"), 9240Sstevel@tonic-gate new_cc.cf_devfs); 9250Sstevel@tonic-gate closelog(); 9260Sstevel@tonic-gate } 9270Sstevel@tonic-gate 9280Sstevel@tonic-gate return (upval); 9290Sstevel@tonic-gate } 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate /* 9330Sstevel@tonic-gate * Check for a valid statefile pathname, inode and mount status. 9340Sstevel@tonic-gate */ 9350Sstevel@tonic-gate int 9360Sstevel@tonic-gate sfpath(void) 9370Sstevel@tonic-gate { 9380Sstevel@tonic-gate static int statefile; 9390Sstevel@tonic-gate char *err_fmt = NULL; 9400Sstevel@tonic-gate char *sfile, *sp, ch; 9410Sstevel@tonic-gate struct stat stbuf; 9420Sstevel@tonic-gate int dir = 0; 9430Sstevel@tonic-gate dev_t dev; 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate if (statefile) { 9460Sstevel@tonic-gate mesg(MERR, "ignored redundant statefile entry\n"); 9470Sstevel@tonic-gate return (OKUP); 9480Sstevel@tonic-gate } else if (ua_err) { 9490Sstevel@tonic-gate if (ua_err != ENOTSUP) 9500Sstevel@tonic-gate mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n", 9510Sstevel@tonic-gate strerror(ua_err)); 9520Sstevel@tonic-gate return (NOUP); 9530Sstevel@tonic-gate } 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate /* 9560Sstevel@tonic-gate * Check for an absolute path and trim any trailing '/'. 9570Sstevel@tonic-gate */ 9580Sstevel@tonic-gate sfile = LINEARG(1); 9590Sstevel@tonic-gate if (*sfile != '/') { 9600Sstevel@tonic-gate mesg(MERR, "statefile requires an absolute path\n"); 9610Sstevel@tonic-gate return (NOUP); 9620Sstevel@tonic-gate } 9630Sstevel@tonic-gate for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--) 9640Sstevel@tonic-gate *sp = '\0'; 9650Sstevel@tonic-gate 9660Sstevel@tonic-gate /* 9670Sstevel@tonic-gate * If the statefile doesn't exist, the leading path must be a dir. 9680Sstevel@tonic-gate */ 9690Sstevel@tonic-gate if (stat(sfile, &stbuf) == -1) { 9700Sstevel@tonic-gate if (errno == ENOENT) { 9710Sstevel@tonic-gate dir = 1; 9720Sstevel@tonic-gate if ((sp = strrchr(sfile, '/')) == sfile) 9730Sstevel@tonic-gate sp++; 9740Sstevel@tonic-gate ch = *sp; 9750Sstevel@tonic-gate *sp = '\0'; 9760Sstevel@tonic-gate if (stat(sfile, &stbuf) == -1) 9770Sstevel@tonic-gate err_fmt = stat_fmt; 9780Sstevel@tonic-gate *sp = ch; 9790Sstevel@tonic-gate } else 9800Sstevel@tonic-gate err_fmt = stat_fmt; 9810Sstevel@tonic-gate if (err_fmt) { 9820Sstevel@tonic-gate mesg(MERR, err_fmt, sfile, strerror(errno)); 9830Sstevel@tonic-gate return (NOUP); 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate } 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate /* 9880Sstevel@tonic-gate * Check for regular/dir/block types, set cf_type and dev. 9890Sstevel@tonic-gate */ 9900Sstevel@tonic-gate if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) { 9910Sstevel@tonic-gate new_cc.cf_type = CFT_UFS; 9920Sstevel@tonic-gate dev = stbuf.st_dev; 9930Sstevel@tonic-gate } else if (S_ISBLK(stbuf.st_mode)) { 9940Sstevel@tonic-gate if (minor(stbuf.st_rdev) != 2) { 9950Sstevel@tonic-gate new_cc.cf_type = CFT_SPEC; 9960Sstevel@tonic-gate dev = stbuf.st_rdev; 9970Sstevel@tonic-gate } else 9980Sstevel@tonic-gate err_fmt = "statefile device cannot be slice 2 (%s)\n" 9990Sstevel@tonic-gate "would clobber the disk label and boot-block\n"; 10000Sstevel@tonic-gate } else 10010Sstevel@tonic-gate err_fmt = "bad file type for \"%s\"\n" 10020Sstevel@tonic-gate "statefile must be a regular file or block device\n"; 10030Sstevel@tonic-gate if (err_fmt) { 10040Sstevel@tonic-gate mesg(MERR, err_fmt, sfile); 10050Sstevel@tonic-gate return (NOUP); 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)) || utop()) 10090Sstevel@tonic-gate return (NOUP); 10100Sstevel@tonic-gate new_cc.cf_magic = CPR_CONFIG_MAGIC; 10110Sstevel@tonic-gate statefile = 1; 10120Sstevel@tonic-gate return (OKUP); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate #endif /* sparc */ 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate /* 10183028Smh27603 * Common function to set a system or cpu threshold. 10190Sstevel@tonic-gate */ 10203028Smh27603 static int 10213028Smh27603 cmnthr(int req) 10220Sstevel@tonic-gate { 10230Sstevel@tonic-gate int value, nerr = 0, upval = OKUP; 10240Sstevel@tonic-gate char *thresh = LINEARG(1); 10250Sstevel@tonic-gate 10260Sstevel@tonic-gate if (strcmp(thresh, always_on) == 0) 10270Sstevel@tonic-gate value = INT_MAX; 10280Sstevel@tonic-gate else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) { 10290Sstevel@tonic-gate mesg(MERR, "%s must be a positive value\n", LINEARG(0)); 10300Sstevel@tonic-gate upval = NOUP; 10310Sstevel@tonic-gate } 10320Sstevel@tonic-gate if (upval == OKUP) 10333028Smh27603 (void) ioctl(pm_fd, req, value); 10340Sstevel@tonic-gate return (upval); 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate 10383028Smh27603 /* 10393028Smh27603 * Try setting system threshold. 10403028Smh27603 */ 10413028Smh27603 int 10423028Smh27603 systhr(void) 10433028Smh27603 { 10443028Smh27603 return (cmnthr(PM_SET_SYSTEM_THRESHOLD)); 10453028Smh27603 } 10463028Smh27603 10473028Smh27603 10483028Smh27603 /* 10493028Smh27603 * Try setting cpu threshold. 10503028Smh27603 */ 10513028Smh27603 int 10523028Smh27603 cputhr(void) 10533028Smh27603 { 10543028Smh27603 return (cmnthr(PM_SET_CPU_THRESHOLD)); 10553028Smh27603 } 10563028Smh27603 10573028Smh27603 10580Sstevel@tonic-gate int 10590Sstevel@tonic-gate tchars(void) 10600Sstevel@tonic-gate { 10610Sstevel@tonic-gate return (scan_int(LINEARG(1), &new_cc.ttychars_thold)); 10620Sstevel@tonic-gate } 1063