17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5c42872d4Smh27603 * Common Development and Distribution License (the "License").
6c42872d4Smh27603 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
224f1c6a56SGary Mills * Copyright 2015 Gary Mills
230e751525SEric Saxe * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include "pmconfig.h"
287c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
297c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
307c478bd9Sstevel@tonic-gate #include <sys/openpromio.h>
317c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
32e7cbe64fSgw25295 #include <sys/vtoc.h>
33e7cbe64fSgw25295 #include <sys/efi_partition.h>
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
362df1fe9cSrandyf #include <sys/pm.h>
372df1fe9cSrandyf #include <kstat.h>
382df1fe9cSrandyf #include <sys/smbios.h>
39e7cbe64fSgw25295 #include <libzfs.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #define STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str)
437c478bd9Sstevel@tonic-gate #define LASTBYTE(str) (str + strlen(str) - 1)
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate static char nerr_fmt[] = "number is out of range (%s)\n";
467c478bd9Sstevel@tonic-gate static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n";
477c478bd9Sstevel@tonic-gate static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n";
487c478bd9Sstevel@tonic-gate static char bad_thresh_fmt[] = "bad threshold(s)\n";
497c478bd9Sstevel@tonic-gate static char stat_fmt[] = "cannot stat \"%s\", %s\n";
507c478bd9Sstevel@tonic-gate static char always_on[] = "always-on";
517c478bd9Sstevel@tonic-gate
522df1fe9cSrandyf #define PM_DEFAULT_ALGORITHM -1
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate * When lines in a config file (usually "/etc/power.conf") start with
557c478bd9Sstevel@tonic-gate * a recognized keyword, a "handler" routine is called for specific
567c478bd9Sstevel@tonic-gate * CPR or PM -related action(s). Each routine returns a status code
577c478bd9Sstevel@tonic-gate * indicating whether all tasks were successful; if any errors occured,
587c478bd9Sstevel@tonic-gate * future CPR or PM updates are skipped. Following are the handler
597c478bd9Sstevel@tonic-gate * routines for all keywords:
607c478bd9Sstevel@tonic-gate */
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate
632df1fe9cSrandyf static char pm_cmd_string[32];
642df1fe9cSrandyf
652df1fe9cSrandyf static char *
pm_map(int cmd)662df1fe9cSrandyf pm_map(int cmd)
672df1fe9cSrandyf {
682df1fe9cSrandyf pm_req_t req;
692df1fe9cSrandyf
702df1fe9cSrandyf req.value = cmd;
712df1fe9cSrandyf req.data = (void *)pm_cmd_string;
722df1fe9cSrandyf req.datasize = sizeof (pm_cmd_string);
732df1fe9cSrandyf
742df1fe9cSrandyf if (ioctl(pm_fd, PM_GET_CMD_NAME, &req) < 0) {
7525aedaedSmh27603 perror(gettext("PM_GET_CMD_NAME failed:"));
762df1fe9cSrandyf return ("??");
772df1fe9cSrandyf }
782df1fe9cSrandyf return (pm_cmd_string);
792df1fe9cSrandyf }
802df1fe9cSrandyf
812df1fe9cSrandyf static int
isonlist(char * listname,const char * man,const char * prod)822df1fe9cSrandyf isonlist(char *listname, const char *man, const char *prod)
832df1fe9cSrandyf {
842df1fe9cSrandyf pm_searchargs_t sl;
852df1fe9cSrandyf int ret;
862df1fe9cSrandyf
872df1fe9cSrandyf sl.pms_listname = listname;
882df1fe9cSrandyf sl.pms_manufacturer = (char *)man;
892df1fe9cSrandyf sl.pms_product = (char *)prod;
902df1fe9cSrandyf ret = ioctl(pm_fd, PM_SEARCH_LIST, &sl);
912df1fe9cSrandyf mesg(MDEBUG, "PM_SEARCH_LIST %s for %s,%s returns %d\n",
922df1fe9cSrandyf listname, man, prod, ret);
932df1fe9cSrandyf return (ret == 0);
942df1fe9cSrandyf }
952df1fe9cSrandyf
962df1fe9cSrandyf static int
do_ioctl(int ioctl_cmd,char * keyword,char * behavior,int suppress)972df1fe9cSrandyf do_ioctl(int ioctl_cmd, char *keyword, char *behavior, int suppress)
982df1fe9cSrandyf {
992df1fe9cSrandyf mesg(MDEBUG, "doing ioctl %s for %s ", pm_map(ioctl_cmd), keyword);
1002df1fe9cSrandyf if (ioctl(pm_fd, ioctl_cmd, NULL) == -1) {
1012df1fe9cSrandyf int suppressed = suppress == -1 || suppress == errno;
1022df1fe9cSrandyf if (!suppressed) {
1032df1fe9cSrandyf mesg(MERR, "%s %s failed, %s\n", keyword, behavior,
1042df1fe9cSrandyf strerror(errno));
1052df1fe9cSrandyf return (NOUP);
1062df1fe9cSrandyf } else {
10725aedaedSmh27603 mesg(MDEBUG, "%s %s failed, %s\n", keyword, behavior,
10825aedaedSmh27603 strerror(errno));
1092df1fe9cSrandyf return (OKUP);
1102df1fe9cSrandyf }
1112df1fe9cSrandyf }
1122df1fe9cSrandyf mesg(MDEBUG, "succeeded\n");
1132df1fe9cSrandyf return (OKUP);
1142df1fe9cSrandyf }
1152df1fe9cSrandyf
1167c478bd9Sstevel@tonic-gate /*
117c42872d4Smh27603 * Check for valid cpupm behavior and communicate it to the kernel.
118c42872d4Smh27603 */
119c42872d4Smh27603 int
cpupm(void)120c42872d4Smh27603 cpupm(void)
121c42872d4Smh27603 {
1220e751525SEric Saxe struct bmtoc {
123c42872d4Smh27603 char *behavior;
1240e751525SEric Saxe char *mode;
125c42872d4Smh27603 int cmd;
126c42872d4Smh27603 int Errno;
127c42872d4Smh27603 };
128c42872d4Smh27603
1290e751525SEric Saxe static struct bmtoc bmlist[] = {
1300e751525SEric Saxe "disable", "\0", PM_STOP_CPUPM, EINVAL,
1310e751525SEric Saxe "enable", "poll-mode", PM_START_CPUPM_POLL, EBUSY,
1320e751525SEric Saxe "enable", "event-mode", PM_START_CPUPM_EV, EBUSY,
1330e751525SEric Saxe "enable", "\0", PM_START_CPUPM, EBUSY,
1340e751525SEric Saxe NULL, 0, 0, 0
1350e751525SEric Saxe };
1360e751525SEric Saxe struct bmtoc *bp;
1370e751525SEric Saxe char *behavior;
1380e751525SEric Saxe char *mode;
1390e751525SEric Saxe
1400e751525SEric Saxe behavior = LINEARG(1);
1410e751525SEric Saxe if ((mode = LINEARG(2)) == NULL)
1420e751525SEric Saxe mode = "\0";
1430e751525SEric Saxe
1440e751525SEric Saxe for (bp = bmlist; bp->cmd; bp++) {
1450e751525SEric Saxe if (strcmp(behavior, bp->behavior) == 0 &&
1460e751525SEric Saxe strcmp(mode, bp->mode) == 0) {
147c42872d4Smh27603 break;
148c42872d4Smh27603 }
1490e751525SEric Saxe }
150c42872d4Smh27603 if (bp->cmd == 0) {
1510e751525SEric Saxe if (LINEARG(2) == NULL) {
152c42872d4Smh27603 mesg(MERR, "invalid cpupm behavior \"%s\"\n", behavior);
1530e751525SEric Saxe } else {
1540e751525SEric Saxe mesg(MERR, "invalid cpupm behavior \"%s %s\"\n",
1550e751525SEric Saxe behavior, mode);
1560e751525SEric Saxe }
157c42872d4Smh27603 return (NOUP);
158c42872d4Smh27603 }
159c42872d4Smh27603 if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
160c42872d4Smh27603 mesg(MERR, "cpupm %s failed, %s\n",
161c42872d4Smh27603 behavior, strerror(errno));
162c42872d4Smh27603 return (NOUP);
163c42872d4Smh27603 }
164c42872d4Smh27603 return (OKUP);
165c42872d4Smh27603 }
166c42872d4Smh27603
1670e751525SEric Saxe /*
1680e751525SEric Saxe * Check for valid cpu_deep_idle option and communicate it to the kernel.
1690e751525SEric Saxe */
1700e751525SEric Saxe int
cpuidle(void)1710e751525SEric Saxe cpuidle(void)
1720e751525SEric Saxe {
1730e751525SEric Saxe struct btoc {
1740e751525SEric Saxe char *behavior;
1750e751525SEric Saxe int cmd;
1760e751525SEric Saxe int Errno;
1770e751525SEric Saxe };
1780e751525SEric Saxe static struct btoc blist[] = {
1790e751525SEric Saxe "disable", PM_DISABLE_CPU_DEEP_IDLE, EINVAL,
1800e751525SEric Saxe "enable", PM_ENABLE_CPU_DEEP_IDLE, EBUSY,
1810e751525SEric Saxe "default", PM_DEFAULT_CPU_DEEP_IDLE, EBUSY,
1820e751525SEric Saxe NULL, 0, 0
1830e751525SEric Saxe };
1840e751525SEric Saxe struct btoc *bp;
1850e751525SEric Saxe char *behavior;
1860e751525SEric Saxe
1870e751525SEric Saxe for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
1880e751525SEric Saxe if (strcmp(behavior, bp->behavior) == 0)
1890e751525SEric Saxe break;
1900e751525SEric Saxe }
1910e751525SEric Saxe if (bp->cmd == 0) {
1920e751525SEric Saxe mesg(MERR, "invalid cpu_deep_idle behavior \"%s\"\n", behavior);
1930e751525SEric Saxe return (NOUP);
1940e751525SEric Saxe }
1950e751525SEric Saxe if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
1960e751525SEric Saxe mesg(MERR, "cpu_deep_idle %s failed, %s\n",
1970e751525SEric Saxe behavior, strerror(errno));
1980e751525SEric Saxe return (NOUP);
1990e751525SEric Saxe }
2000e751525SEric Saxe return (OKUP);
2010e751525SEric Saxe }
202c42872d4Smh27603
203c42872d4Smh27603 /*
2042df1fe9cSrandyf * Two decisions are identical except for the list names and ioctl commands
2052df1fe9cSrandyf * inputs: whitelist, blacklist, yes, no
2062df1fe9cSrandyf * if (! ("S3" kstat exists))
2072df1fe9cSrandyf * return (no)
2082df1fe9cSrandyf * if (SystemInformation.Manufacturer == "Sun Microsystems" &&
2092df1fe9cSrandyf * (Pref_PM_Profile == Workstation || Pref_PM_Profile == Desktop)) {
2102df1fe9cSrandyf * if (platform on blacklist)
2112df1fe9cSrandyf * return (no)
2122df1fe9cSrandyf * return (yes)
2132df1fe9cSrandyf * } else {
2142df1fe9cSrandyf * if (platform on whitelist)
2152df1fe9cSrandyf * return (yes)
2162df1fe9cSrandyf * return (no)
2172df1fe9cSrandyf * }
2182df1fe9cSrandyf */
2192df1fe9cSrandyf
2202df1fe9cSrandyf int
S3_helper(char * whitelist,char * blacklist,int yes,int no,char * keyword,char * behavior,int * didyes,int suppress)2212df1fe9cSrandyf S3_helper(char *whitelist, char *blacklist, int yes, int no, char *keyword,
2222df1fe9cSrandyf char *behavior, int *didyes, int suppress)
2232df1fe9cSrandyf {
2242df1fe9cSrandyf int oflags = SMB_O_NOCKSUM | SMB_O_NOVERS;
2252df1fe9cSrandyf smbios_hdl_t *shp;
2262df1fe9cSrandyf smbios_system_t sys;
2272df1fe9cSrandyf id_t id;
2282df1fe9cSrandyf int ret;
2292df1fe9cSrandyf kstat_ctl_t *kc;
2302df1fe9cSrandyf kstat_t *ksp;
2312df1fe9cSrandyf kstat_named_t *dp;
2322df1fe9cSrandyf smbios_info_t info;
2332df1fe9cSrandyf int preferred_pm_profile = 0;
2342df1fe9cSrandyf char yesstr[32], nostr[32]; /* DEBUG */
2352df1fe9cSrandyf
2362df1fe9cSrandyf *didyes = 0;
2372df1fe9cSrandyf
23880148899SSurya Prakki (void) strncpy(yesstr, pm_map(yes), sizeof (yesstr));
23980148899SSurya Prakki (void) strncpy(nostr, pm_map(no), sizeof (nostr));
2402df1fe9cSrandyf mesg(MDEBUG, "S3_helper(%s, %s, %s, %s, %s, %s)\n", whitelist,
2412df1fe9cSrandyf blacklist, yesstr, nostr, keyword, behavior);
2422df1fe9cSrandyf if ((kc = kstat_open()) == NULL) {
2432df1fe9cSrandyf mesg(MDEBUG, "kstat_open failed\n");
2442df1fe9cSrandyf return (OKUP);
2452df1fe9cSrandyf }
2462df1fe9cSrandyf ksp = kstat_lookup(kc, "acpi", -1, "acpi");
2472df1fe9cSrandyf if (ksp == NULL) {
2482df1fe9cSrandyf mesg(MDEBUG, "kstat_lookup 'acpi', -1, 'acpi' failed\n");
24980148899SSurya Prakki (void) kstat_close(kc);
2502df1fe9cSrandyf return (OKUP);
2512df1fe9cSrandyf }
2522df1fe9cSrandyf (void) kstat_read(kc, ksp, NULL);
2532df1fe9cSrandyf dp = kstat_data_lookup(ksp, "S3");
2542df1fe9cSrandyf if (dp == NULL || dp->value.l == 0) {
2552df1fe9cSrandyf mesg(MDEBUG, "kstat_data_lookup 'S3' fails\n");
2562df1fe9cSrandyf if (dp != NULL)
2572df1fe9cSrandyf mesg(MDEBUG, "value.l %lx\n", dp->value.l);
25880148899SSurya Prakki (void) kstat_close(kc);
2592df1fe9cSrandyf return (do_ioctl(no, keyword, behavior, suppress));
2602df1fe9cSrandyf }
2612df1fe9cSrandyf mesg(MDEBUG, "kstat indicates S3 support (%lx)\n", dp->value.l);
2622df1fe9cSrandyf
2632df1fe9cSrandyf if (!whitelist_only) {
2642df1fe9cSrandyf /*
2652df1fe9cSrandyf * We still have an ACPI ksp, search it again for
2662df1fe9cSrandyf * 'preferred_pm_profile' (needs to be valid if we don't
2672df1fe9cSrandyf * aren't only using a whitelist).
2682df1fe9cSrandyf */
2692df1fe9cSrandyf dp = kstat_data_lookup(ksp, "preferred_pm_profile");
2702df1fe9cSrandyf if (dp == NULL) {
2712df1fe9cSrandyf mesg(MDEBUG, "kstat_data_lookup 'ppmp fails\n");
27280148899SSurya Prakki (void) kstat_close(kc);
2732df1fe9cSrandyf return (do_ioctl(no, keyword, behavior, suppress));
2742df1fe9cSrandyf }
27525aedaedSmh27603 mesg(MDEBUG, "kstat indicates preferred_pm_profile is %lx\n",
2762df1fe9cSrandyf dp->value.l);
2772df1fe9cSrandyf preferred_pm_profile = dp->value.l;
2782df1fe9cSrandyf }
27980148899SSurya Prakki (void) kstat_close(kc);
2802df1fe9cSrandyf
2812df1fe9cSrandyf if ((shp = smbios_open(NULL,
2822df1fe9cSrandyf SMB_VERSION, oflags, &ret)) == NULL) {
2832df1fe9cSrandyf /* we promised not to complain */
2842df1fe9cSrandyf /* we bail leaving it to the kernel default */
2852df1fe9cSrandyf mesg(MDEBUG, "smbios_open failed %d\n", errno);
2862df1fe9cSrandyf return (OKUP);
2872df1fe9cSrandyf }
2882df1fe9cSrandyf if ((id = smbios_info_system(shp, &sys)) == SMB_ERR) {
2892df1fe9cSrandyf mesg(MDEBUG, "smbios_info_system failed %d\n", errno);
2902df1fe9cSrandyf smbios_close(shp);
2912df1fe9cSrandyf return (OKUP);
2922df1fe9cSrandyf }
2932df1fe9cSrandyf if (smbios_info_common(shp, id, &info) == SMB_ERR) {
2942df1fe9cSrandyf mesg(MDEBUG, "smbios_info_common failed %d\n", errno);
2952df1fe9cSrandyf smbios_close(shp);
2962df1fe9cSrandyf return (OKUP);
2972df1fe9cSrandyf }
2982df1fe9cSrandyf mesg(MDEBUG, "Manufacturer: %s\n", info.smbi_manufacturer);
2992df1fe9cSrandyf mesg(MDEBUG, "Product: %s\n", info.smbi_product);
3002df1fe9cSrandyf smbios_close(shp);
3012df1fe9cSrandyf
3022df1fe9cSrandyf if (!whitelist_only) {
3032df1fe9cSrandyf #define PPP_DESKTOP 1
3042df1fe9cSrandyf #define PPP_WORKSTATION 3
3052df1fe9cSrandyf if (strcmp(info.smbi_manufacturer, "Sun Microsystems") == 0 &&
3062df1fe9cSrandyf (preferred_pm_profile == PPP_DESKTOP ||
3072df1fe9cSrandyf preferred_pm_profile == PPP_WORKSTATION)) {
3082df1fe9cSrandyf if (isonlist(blacklist,
3092df1fe9cSrandyf info.smbi_manufacturer, info.smbi_product)) {
3102df1fe9cSrandyf return (do_ioctl(no, keyword, behavior,
3112df1fe9cSrandyf suppress));
3122df1fe9cSrandyf } else {
3132df1fe9cSrandyf ret = do_ioctl(yes, keyword, behavior,
3142df1fe9cSrandyf suppress);
3152df1fe9cSrandyf *didyes = (ret == OKUP);
3162df1fe9cSrandyf return (ret);
3172df1fe9cSrandyf }
3182df1fe9cSrandyf }
3192df1fe9cSrandyf }
3202df1fe9cSrandyf if (isonlist(whitelist,
3212df1fe9cSrandyf info.smbi_manufacturer, info.smbi_product)) {
3222df1fe9cSrandyf ret = do_ioctl(yes, keyword, behavior, suppress);
3232df1fe9cSrandyf *didyes = (ret == OKUP);
3242df1fe9cSrandyf return (ret);
3252df1fe9cSrandyf } else {
3262df1fe9cSrandyf return (do_ioctl(no, keyword, behavior, suppress));
3272df1fe9cSrandyf }
3282df1fe9cSrandyf }
3292df1fe9cSrandyf
3302df1fe9cSrandyf int
S3sup(void)3312df1fe9cSrandyf S3sup(void) /* S3-support keyword handler */
3322df1fe9cSrandyf {
3332df1fe9cSrandyf struct btoc {
3342df1fe9cSrandyf char *behavior;
3352df1fe9cSrandyf int cmd;
3362df1fe9cSrandyf };
3372df1fe9cSrandyf static struct btoc blist[] = {
3382df1fe9cSrandyf "default", PM_DEFAULT_ALGORITHM,
3392df1fe9cSrandyf "enable", PM_ENABLE_S3,
3402df1fe9cSrandyf "disable", PM_DISABLE_S3,
3412df1fe9cSrandyf NULL, 0
3422df1fe9cSrandyf };
3432df1fe9cSrandyf struct btoc *bp;
3442df1fe9cSrandyf char *behavior;
3452df1fe9cSrandyf int dontcare;
3462df1fe9cSrandyf
3472df1fe9cSrandyf for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
3482df1fe9cSrandyf if (strcmp(behavior, bp->behavior) == 0)
3492df1fe9cSrandyf break;
3502df1fe9cSrandyf }
3512df1fe9cSrandyf if (bp->cmd == 0) {
3522df1fe9cSrandyf mesg(MERR, "invalid S3-support behavior \"%s\"\n", behavior);
3532df1fe9cSrandyf return (NOUP);
3542df1fe9cSrandyf }
3552df1fe9cSrandyf
3562df1fe9cSrandyf
3572df1fe9cSrandyf switch (bp->cmd) {
3582df1fe9cSrandyf
3592df1fe9cSrandyf case PM_ENABLE_S3:
3602df1fe9cSrandyf case PM_DISABLE_S3:
3612df1fe9cSrandyf return (do_ioctl(bp->cmd, "S3-support", behavior, EBUSY));
3622df1fe9cSrandyf
3632df1fe9cSrandyf case PM_DEFAULT_ALGORITHM:
3642df1fe9cSrandyf /*
3652df1fe9cSrandyf * we suppress errors in the "default" case because we
3662df1fe9cSrandyf * already did an invisible default call, so we know we'll
3672df1fe9cSrandyf * get EBUSY
3682df1fe9cSrandyf */
3692df1fe9cSrandyf return (S3_helper("S3-support-enable", "S3-support-disable",
3702df1fe9cSrandyf PM_ENABLE_S3, PM_DISABLE_S3, "S3-support", behavior,
3712df1fe9cSrandyf &dontcare, EBUSY));
3722df1fe9cSrandyf
3732df1fe9cSrandyf default:
3742df1fe9cSrandyf mesg(MERR, "S3-support %s failed, %s\n", behavior,
3752df1fe9cSrandyf strerror(errno));
3762df1fe9cSrandyf return (NOUP);
3772df1fe9cSrandyf }
3782df1fe9cSrandyf }
3792df1fe9cSrandyf
3802df1fe9cSrandyf /*
3812df1fe9cSrandyf * Check for valid autoS3 behavior and save after ioctl success.
3822df1fe9cSrandyf */
3832df1fe9cSrandyf int
autoS3(void)3842df1fe9cSrandyf autoS3(void)
3852df1fe9cSrandyf {
3862df1fe9cSrandyf struct btoc {
3872df1fe9cSrandyf char *behavior;
3882df1fe9cSrandyf int cmd;
3892df1fe9cSrandyf };
3902df1fe9cSrandyf static struct btoc blist[] = {
3912df1fe9cSrandyf "default", PM_DEFAULT_ALGORITHM,
3922df1fe9cSrandyf "disable", PM_STOP_AUTOS3,
3932df1fe9cSrandyf "enable", PM_START_AUTOS3,
3942df1fe9cSrandyf NULL, 0
3952df1fe9cSrandyf };
3962df1fe9cSrandyf struct btoc *bp;
3972df1fe9cSrandyf char *behavior;
3982df1fe9cSrandyf int dontcare;
3992df1fe9cSrandyf
4002df1fe9cSrandyf for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
4012df1fe9cSrandyf if (strcmp(behavior, bp->behavior) == 0)
4022df1fe9cSrandyf break;
4032df1fe9cSrandyf }
4042df1fe9cSrandyf if (bp->cmd == 0) {
4052df1fe9cSrandyf mesg(MERR, "invalid autoS3 behavior \"%s\"\n", behavior);
4062df1fe9cSrandyf return (NOUP);
4072df1fe9cSrandyf }
4082df1fe9cSrandyf
4092df1fe9cSrandyf switch (bp->cmd) {
4102df1fe9cSrandyf default:
4112df1fe9cSrandyf mesg(MERR, "autoS3 %s failed, %s\n",
4122df1fe9cSrandyf behavior, strerror(errno));
4132df1fe9cSrandyf mesg(MDEBUG, "unknown command\n", bp->cmd);
4142df1fe9cSrandyf return (OKUP);
4152df1fe9cSrandyf
4162df1fe9cSrandyf case PM_STOP_AUTOS3:
4172df1fe9cSrandyf case PM_START_AUTOS3:
4182df1fe9cSrandyf return (do_ioctl(bp->cmd, "autoS3", behavior, EBUSY));
4192df1fe9cSrandyf
4202df1fe9cSrandyf case PM_DEFAULT_ALGORITHM:
4212df1fe9cSrandyf return (S3_helper("S3-autoenable", "S3-autodisable",
4222df1fe9cSrandyf PM_START_AUTOS3, PM_STOP_AUTOS3, "autoS3", behavior,
4232df1fe9cSrandyf &dontcare, EBUSY));
4242df1fe9cSrandyf }
4252df1fe9cSrandyf }
4262df1fe9cSrandyf
4272df1fe9cSrandyf
4282df1fe9cSrandyf /*
4297c478bd9Sstevel@tonic-gate * Check for valid autopm behavior and save after ioctl success.
4307c478bd9Sstevel@tonic-gate */
4317c478bd9Sstevel@tonic-gate int
autopm(void)4327c478bd9Sstevel@tonic-gate autopm(void)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate struct btoc {
4357c478bd9Sstevel@tonic-gate char *behavior;
4367c478bd9Sstevel@tonic-gate int cmd, Errno, isdef;
4377c478bd9Sstevel@tonic-gate };
4387c478bd9Sstevel@tonic-gate static struct btoc blist[] = {
4392df1fe9cSrandyf "default", PM_START_PM, -1, 1,
4407c478bd9Sstevel@tonic-gate "disable", PM_STOP_PM, EINVAL, 0,
4417c478bd9Sstevel@tonic-gate "enable", PM_START_PM, EBUSY, 0,
4427c478bd9Sstevel@tonic-gate NULL, 0, 0, 0,
4437c478bd9Sstevel@tonic-gate };
4447c478bd9Sstevel@tonic-gate struct btoc *bp;
4457c478bd9Sstevel@tonic-gate char *behavior;
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
4487c478bd9Sstevel@tonic-gate if (strcmp(behavior, bp->behavior) == 0)
4497c478bd9Sstevel@tonic-gate break;
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate if (bp->cmd == 0) {
4527c478bd9Sstevel@tonic-gate mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior);
4537c478bd9Sstevel@tonic-gate return (NOUP);
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate * for "default" behavior, do not enable autopm if not ESTAR_V3
4587c478bd9Sstevel@tonic-gate */
4592df1fe9cSrandyf #if defined(__sparc)
4607c478bd9Sstevel@tonic-gate if (!bp->isdef || (estar_vers == ESTAR_V3)) {
4617c478bd9Sstevel@tonic-gate if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
4627c478bd9Sstevel@tonic-gate mesg(MERR, "autopm %s failed, %s\n",
4637c478bd9Sstevel@tonic-gate behavior, strerror(errno));
4647c478bd9Sstevel@tonic-gate return (NOUP);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate (void) strcpy(new_cc.apm_behavior, behavior);
4687c478bd9Sstevel@tonic-gate return (OKUP);
4692df1fe9cSrandyf #endif
4702df1fe9cSrandyf #if defined(__x86)
4712df1fe9cSrandyf if (!bp->isdef) {
4722df1fe9cSrandyf if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
4732df1fe9cSrandyf mesg(MERR, "autopm %s failed, %s\n",
4742df1fe9cSrandyf behavior, strerror(errno));
4752df1fe9cSrandyf return (NOUP);
4762df1fe9cSrandyf }
4772df1fe9cSrandyf mesg(MDEBUG, "autopm %s succeeded\n", behavior);
4782df1fe9cSrandyf
4792df1fe9cSrandyf return (OKUP);
4802df1fe9cSrandyf } else {
4812df1fe9cSrandyf int didenable;
4822df1fe9cSrandyf int ret = S3_helper("autopm-enable", "autopm-disable",
4832df1fe9cSrandyf PM_START_PM, PM_STOP_PM, "autopm", behavior, &didenable,
4842df1fe9cSrandyf bp->Errno);
4852df1fe9cSrandyf if (didenable) {
4862df1fe9cSrandyf /* tell powerd to attach all devices */
4872df1fe9cSrandyf new_cc.is_autopm_default = 1;
4882df1fe9cSrandyf (void) strcpy(new_cc.apm_behavior, behavior);
4892df1fe9cSrandyf }
4902df1fe9cSrandyf return (ret);
4912df1fe9cSrandyf }
4922df1fe9cSrandyf #endif
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate static int
gethm(char * src,int * hour,int * min)4977c478bd9Sstevel@tonic-gate gethm(char *src, int *hour, int *min)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate if (sscanf(src, "%d:%d", hour, min) != 2) {
5007c478bd9Sstevel@tonic-gate mesg(MERR, "bad time format (%s)\n", src);
5017c478bd9Sstevel@tonic-gate return (-1);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate return (0);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate static void
strcpy_limit(char * dst,char * src,size_t limit,char * info)5087c478bd9Sstevel@tonic-gate strcpy_limit(char *dst, char *src, size_t limit, char *info)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate if (strlcpy(dst, src, limit) >= limit)
5117c478bd9Sstevel@tonic-gate mesg(MEXIT, "%s is too long (%s)\n", info, src);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate * Convert autoshutdown idle and start/finish times;
5177c478bd9Sstevel@tonic-gate * check and record autoshutdown behavior.
5187c478bd9Sstevel@tonic-gate */
5197c478bd9Sstevel@tonic-gate int
autosd(void)5207c478bd9Sstevel@tonic-gate autosd(void)
5217c478bd9Sstevel@tonic-gate {
5221dc065c6Smh27603 char **bp, *behavior;
5231dc065c6Smh27603 char *unrec = gettext("unrecognized autoshutdown behavior");
5247c478bd9Sstevel@tonic-gate static char *blist[] = {
5257c478bd9Sstevel@tonic-gate "autowakeup", "default", "noshutdown",
5267c478bd9Sstevel@tonic-gate "shutdown", "unconfigured", NULL
5277c478bd9Sstevel@tonic-gate };
5287c478bd9Sstevel@tonic-gate
5297c478bd9Sstevel@tonic-gate new_cc.as_idle = atoi(LINEARG(1));
5307c478bd9Sstevel@tonic-gate if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) ||
5317c478bd9Sstevel@tonic-gate gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm))
5327c478bd9Sstevel@tonic-gate return (NOUP);
5331dc065c6Smh27603 mesg(MDEBUG, "idle %d, start %d:%02d, finish %d:%02d\n",
5347c478bd9Sstevel@tonic-gate new_cc.as_idle, new_cc.as_sh, new_cc.as_sm,
5357c478bd9Sstevel@tonic-gate new_cc.as_fh, new_cc.as_fm);
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate for (behavior = LINEARG(4), bp = blist; *bp; bp++) {
5387c478bd9Sstevel@tonic-gate if (strcmp(behavior, *bp) == 0)
5397c478bd9Sstevel@tonic-gate break;
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate if (*bp == NULL) {
5427c478bd9Sstevel@tonic-gate mesg(MERR, "%s: \"%s\"\n", unrec, behavior);
5437c478bd9Sstevel@tonic-gate return (NOUP);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate STRCPYLIM(new_cc.as_behavior, *bp, unrec);
5467c478bd9Sstevel@tonic-gate return (OKUP);
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate /*
5517c478bd9Sstevel@tonic-gate * Check for a real device and try to resolve to a full path.
5527c478bd9Sstevel@tonic-gate * The orig/resolved path may be modified into a prom pathname,
5537c478bd9Sstevel@tonic-gate * and an allocated copy of the result is stored at *destp;
5547c478bd9Sstevel@tonic-gate * the caller will need to free that space. Returns 1 for any
5557c478bd9Sstevel@tonic-gate * error, otherwise 0; also sets *errp after an alloc error.
5567c478bd9Sstevel@tonic-gate */
5577c478bd9Sstevel@tonic-gate static int
devpath(char ** destp,char * src,int * errp)5587c478bd9Sstevel@tonic-gate devpath(char **destp, char *src, int *errp)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate struct stat stbuf;
5617c478bd9Sstevel@tonic-gate char buf[PATH_MAX];
5627c478bd9Sstevel@tonic-gate char *cp, *dstr;
5637c478bd9Sstevel@tonic-gate int devok, dcs = 0;
5647c478bd9Sstevel@tonic-gate size_t len;
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate * When there's a real device, try to resolve the path
5687c478bd9Sstevel@tonic-gate * and trim the leading "/devices" component.
5697c478bd9Sstevel@tonic-gate */
5707c478bd9Sstevel@tonic-gate if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) {
5717c478bd9Sstevel@tonic-gate if (realpath(src, buf) == NULL) {
5727c478bd9Sstevel@tonic-gate mesg(MERR, "realpath cannot resolve \"%s\"\n",
5737c478bd9Sstevel@tonic-gate src, strerror(errno));
5747c478bd9Sstevel@tonic-gate return (1);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate src = buf;
5777c478bd9Sstevel@tonic-gate dstr = "/devices";
5787c478bd9Sstevel@tonic-gate len = strlen(dstr);
5797c478bd9Sstevel@tonic-gate dcs = (strncmp(src, dstr, len) == 0);
5807c478bd9Sstevel@tonic-gate if (dcs)
5817c478bd9Sstevel@tonic-gate src += len;
5827c478bd9Sstevel@tonic-gate } else
5837c478bd9Sstevel@tonic-gate mesg(MDEBUG, stat_fmt, src, strerror(errno));
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate * When the path has ":anything", display an error for
5877c478bd9Sstevel@tonic-gate * a non-device or truncate a resolved+modifed path.
5887c478bd9Sstevel@tonic-gate */
5895009f788SIgor Kozhukhov if ((cp = strchr(src, ':')) != NULL) {
5907c478bd9Sstevel@tonic-gate if (devok == 0) {
5917c478bd9Sstevel@tonic-gate mesg(MERR, "physical path may not contain "
5927c478bd9Sstevel@tonic-gate "a minor string (%s)\n", src);
5937c478bd9Sstevel@tonic-gate return (1);
5947c478bd9Sstevel@tonic-gate } else if (dcs)
5957c478bd9Sstevel@tonic-gate *cp = '\0';
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate if ((*destp = strdup(src)) == NULL) {
5997c478bd9Sstevel@tonic-gate *errp = NOUP;
6007c478bd9Sstevel@tonic-gate mesg(MERR, alloc_fmt, src, strerror(errno));
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate return (*destp == NULL);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate
6057c478bd9Sstevel@tonic-gate
6067c478bd9Sstevel@tonic-gate /*
6077c478bd9Sstevel@tonic-gate * Call pm ioctl request(s) to set property/device dependencies.
6087c478bd9Sstevel@tonic-gate */
6097c478bd9Sstevel@tonic-gate static int
dev_dep_common(int isprop)6107c478bd9Sstevel@tonic-gate dev_dep_common(int isprop)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate int cmd, argn, upval = OKUP;
6137c478bd9Sstevel@tonic-gate char *src, *first, **destp;
6147c478bd9Sstevel@tonic-gate pm_req_t pmreq;
6157c478bd9Sstevel@tonic-gate
6167c478bd9Sstevel@tonic-gate bzero(&pmreq, sizeof (pmreq));
6177c478bd9Sstevel@tonic-gate src = LINEARG(1);
6187c478bd9Sstevel@tonic-gate if (isprop) {
6197c478bd9Sstevel@tonic-gate cmd = PM_ADD_DEPENDENT_PROPERTY;
6207c478bd9Sstevel@tonic-gate first = NULL;
6217c478bd9Sstevel@tonic-gate pmreq.pmreq_kept = src;
6227c478bd9Sstevel@tonic-gate } else {
6237c478bd9Sstevel@tonic-gate cmd = PM_ADD_DEPENDENT;
6247c478bd9Sstevel@tonic-gate if (devpath(&first, src, &upval))
6257c478bd9Sstevel@tonic-gate return (upval);
6267c478bd9Sstevel@tonic-gate pmreq.pmreq_kept = first;
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate destp = &pmreq.pmreq_keeper;
6297c478bd9Sstevel@tonic-gate
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate * Now loop through any dependents.
6327c478bd9Sstevel@tonic-gate */
6337c478bd9Sstevel@tonic-gate for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
6347c478bd9Sstevel@tonic-gate if (devpath(destp, src, &upval)) {
6357c478bd9Sstevel@tonic-gate if (upval != OKUP)
6367c478bd9Sstevel@tonic-gate return (upval);
6377c478bd9Sstevel@tonic-gate break;
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) {
6407c478bd9Sstevel@tonic-gate mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n"
6417c478bd9Sstevel@tonic-gate "kept \"%s\", keeper \"%s\"\n",
6427c478bd9Sstevel@tonic-gate cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper);
6437c478bd9Sstevel@tonic-gate mesg(MERR, "cannot set \"%s\" dependency "
6447c478bd9Sstevel@tonic-gate "for \"%s\", %s\n", pmreq.pmreq_keeper,
6457c478bd9Sstevel@tonic-gate pmreq.pmreq_kept, strerror(errno));
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate free(*destp);
6487c478bd9Sstevel@tonic-gate *destp = NULL;
6497c478bd9Sstevel@tonic-gate if (upval != OKUP)
6507c478bd9Sstevel@tonic-gate break;
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate free(first);
6547c478bd9Sstevel@tonic-gate return (upval);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate int
ddprop(void)6597c478bd9Sstevel@tonic-gate ddprop(void)
6607c478bd9Sstevel@tonic-gate {
6617c478bd9Sstevel@tonic-gate return (dev_dep_common(1));
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate
6647c478bd9Sstevel@tonic-gate
6657c478bd9Sstevel@tonic-gate int
devdep(void)6667c478bd9Sstevel@tonic-gate devdep(void)
6677c478bd9Sstevel@tonic-gate {
6687c478bd9Sstevel@tonic-gate return (dev_dep_common(0));
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate
6727c478bd9Sstevel@tonic-gate /*
6737c478bd9Sstevel@tonic-gate * Convert a numeric string (with a possible trailing scaling byte)
6747c478bd9Sstevel@tonic-gate * into an integer. Returns a converted value and *nerrp unchanged,
6757c478bd9Sstevel@tonic-gate * or 0 with *nerrp set to 1 for a conversion error.
6767c478bd9Sstevel@tonic-gate */
6777c478bd9Sstevel@tonic-gate static int
get_scaled_value(char * str,int * nerrp)6787c478bd9Sstevel@tonic-gate get_scaled_value(char *str, int *nerrp)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate longlong_t svalue = 0, factor = 1;
6817c478bd9Sstevel@tonic-gate char *sp;
6827c478bd9Sstevel@tonic-gate
6837c478bd9Sstevel@tonic-gate errno = 0;
6847c478bd9Sstevel@tonic-gate svalue = strtol(str, &sp, 0);
6857c478bd9Sstevel@tonic-gate if (errno || (*str != '-' && (*str < '0' || *str > '9')))
6867c478bd9Sstevel@tonic-gate *nerrp = 1;
6877c478bd9Sstevel@tonic-gate else if (sp && *sp != '\0') {
6887c478bd9Sstevel@tonic-gate if (*sp == 'h')
6897c478bd9Sstevel@tonic-gate factor = 3600;
6907c478bd9Sstevel@tonic-gate else if (*sp == 'm')
6917c478bd9Sstevel@tonic-gate factor = 60;
6927c478bd9Sstevel@tonic-gate else if (*sp != 's')
6937c478bd9Sstevel@tonic-gate *nerrp = 1;
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate /* any bytes following sp are ignored */
6967c478bd9Sstevel@tonic-gate
6977c478bd9Sstevel@tonic-gate if (*nerrp == 0) {
6987c478bd9Sstevel@tonic-gate svalue *= factor;
6997c478bd9Sstevel@tonic-gate if (svalue < INT_MIN || svalue > INT_MAX)
7007c478bd9Sstevel@tonic-gate *nerrp = 1;
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate if (*nerrp)
7037c478bd9Sstevel@tonic-gate mesg(MERR, nerr_fmt, str);
7047c478bd9Sstevel@tonic-gate mesg(MDEBUG, "got scaled value %d\n", (int)svalue);
7057c478bd9Sstevel@tonic-gate return ((int)svalue);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate
7087c478bd9Sstevel@tonic-gate
7097c478bd9Sstevel@tonic-gate /*
7107c478bd9Sstevel@tonic-gate * Increment the count of threshold values,
7117c478bd9Sstevel@tonic-gate * reallocate *vlistp and append another element.
7127c478bd9Sstevel@tonic-gate * Returns 1 on error, otherwise 0.
7137c478bd9Sstevel@tonic-gate */
7147c478bd9Sstevel@tonic-gate static int
vlist_append(int ** vlistp,int * vcntp,int value)7157c478bd9Sstevel@tonic-gate vlist_append(int **vlistp, int *vcntp, int value)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate (*vcntp)++;
7185009f788SIgor Kozhukhov if ((*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp))) != NULL)
7197c478bd9Sstevel@tonic-gate *(*vlistp + *vcntp - 1) = value;
7207c478bd9Sstevel@tonic-gate else
7217c478bd9Sstevel@tonic-gate mesg(MERR, alloc_fmt, "threshold list", strerror(errno));
7227c478bd9Sstevel@tonic-gate return (*vlistp == NULL);
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate /*
7277c478bd9Sstevel@tonic-gate * Convert a single threshold string or paren groups of thresh's as
7287c478bd9Sstevel@tonic-gate * described below. All thresh's are saved to an allocated list at
7297c478bd9Sstevel@tonic-gate * *vlistp; the caller will need to free that space. On return:
7307c478bd9Sstevel@tonic-gate * *vcntp is the count of the vlist array, and vlist is either
7317c478bd9Sstevel@tonic-gate * a single thresh or N groups of thresh's with a trailing zero:
7327c478bd9Sstevel@tonic-gate * (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0.
7337c478bd9Sstevel@tonic-gate * Returns 0 when all conversions were OK, and 1 for any syntax,
7347c478bd9Sstevel@tonic-gate * conversion, or alloc error.
7357c478bd9Sstevel@tonic-gate */
7367c478bd9Sstevel@tonic-gate static int
get_thresh(int ** vlistp,int * vcntp)7377c478bd9Sstevel@tonic-gate get_thresh(int **vlistp, int *vcntp)
7387c478bd9Sstevel@tonic-gate {
7395009f788SIgor Kozhukhov int argn, value, gci = 0, grp_cnt = 0, paren = 0, nerr = 0;
7407c478bd9Sstevel@tonic-gate char *rp, *src;
7417c478bd9Sstevel@tonic-gate
7427c478bd9Sstevel@tonic-gate for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
7437c478bd9Sstevel@tonic-gate if (*src == LPAREN) {
7447c478bd9Sstevel@tonic-gate gci = *vcntp;
7455009f788SIgor Kozhukhov if ((nerr = vlist_append(vlistp, vcntp, 0)) != 0)
7467c478bd9Sstevel@tonic-gate break;
7477c478bd9Sstevel@tonic-gate paren = 1;
7487c478bd9Sstevel@tonic-gate src++;
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate if (*(rp = LASTBYTE(src)) == RPAREN) {
7517c478bd9Sstevel@tonic-gate if (paren) {
7527c478bd9Sstevel@tonic-gate grp_cnt = *vcntp - gci;
7537c478bd9Sstevel@tonic-gate *(*vlistp + gci) = grp_cnt;
7547c478bd9Sstevel@tonic-gate paren = 0;
7557c478bd9Sstevel@tonic-gate *rp = '\0';
7567c478bd9Sstevel@tonic-gate } else {
7577c478bd9Sstevel@tonic-gate nerr = 1;
7587c478bd9Sstevel@tonic-gate break;
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate
7627c478bd9Sstevel@tonic-gate value = get_scaled_value(src, &nerr);
7637c478bd9Sstevel@tonic-gate if (nerr || (nerr = vlist_append(vlistp, vcntp, value)))
7647c478bd9Sstevel@tonic-gate break;
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate
7677c478bd9Sstevel@tonic-gate if (nerr == 0 && grp_cnt)
7687c478bd9Sstevel@tonic-gate nerr = vlist_append(vlistp, vcntp, 0);
7697c478bd9Sstevel@tonic-gate return (nerr);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate
7727c478bd9Sstevel@tonic-gate
7737c478bd9Sstevel@tonic-gate /*
7747c478bd9Sstevel@tonic-gate * Set device thresholds from (3) formats:
7757c478bd9Sstevel@tonic-gate * path "always-on"
7767c478bd9Sstevel@tonic-gate * path time-spec: [0-9]+[{h,m,s}]
7777c478bd9Sstevel@tonic-gate * path (ts1 ts2 ...)+
7787c478bd9Sstevel@tonic-gate */
7797c478bd9Sstevel@tonic-gate int
devthr(void)7807c478bd9Sstevel@tonic-gate devthr(void)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate int cmd, upval = OKUP, nthresh = 0, *vlist = NULL;
7837c478bd9Sstevel@tonic-gate pm_req_t pmreq;
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate bzero(&pmreq, sizeof (pmreq));
7867c478bd9Sstevel@tonic-gate if (devpath(&pmreq.physpath, LINEARG(1), &upval))
7877c478bd9Sstevel@tonic-gate return (upval);
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate if (strcmp(LINEARG(2), always_on) == 0) {
7907c478bd9Sstevel@tonic-gate cmd = PM_SET_DEVICE_THRESHOLD;
7917c478bd9Sstevel@tonic-gate pmreq.value = INT_MAX;
7927c478bd9Sstevel@tonic-gate } else if (get_thresh(&vlist, &nthresh)) {
7937c478bd9Sstevel@tonic-gate mesg(MERR, bad_thresh_fmt);
7947c478bd9Sstevel@tonic-gate upval = NOUP;
7957c478bd9Sstevel@tonic-gate } else if (nthresh == 1) {
7967c478bd9Sstevel@tonic-gate pmreq.value = *vlist;
7977c478bd9Sstevel@tonic-gate cmd = PM_SET_DEVICE_THRESHOLD;
7987c478bd9Sstevel@tonic-gate } else {
7997c478bd9Sstevel@tonic-gate pmreq.data = vlist;
8007c478bd9Sstevel@tonic-gate pmreq.datasize = (nthresh * sizeof (*vlist));
8017c478bd9Sstevel@tonic-gate cmd = PM_SET_COMPONENT_THRESHOLDS;
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate
8047c478bd9Sstevel@tonic-gate if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1)
8057c478bd9Sstevel@tonic-gate mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno));
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate free(vlist);
8087c478bd9Sstevel@tonic-gate free(pmreq.physpath);
8097c478bd9Sstevel@tonic-gate return (upval);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate
8127c478bd9Sstevel@tonic-gate
8137c478bd9Sstevel@tonic-gate static int
scan_int(char * src,int * dst)8147c478bd9Sstevel@tonic-gate scan_int(char *src, int *dst)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate long lval;
8177c478bd9Sstevel@tonic-gate
8187c478bd9Sstevel@tonic-gate errno = 0;
8197c478bd9Sstevel@tonic-gate
8207c478bd9Sstevel@tonic-gate lval = strtol(LINEARG(1), NULL, 0);
8217c478bd9Sstevel@tonic-gate if (errno || lval > INT_MAX || lval < 0) {
8227c478bd9Sstevel@tonic-gate mesg(MERR, nerr_fmt, src);
8237c478bd9Sstevel@tonic-gate return (NOUP);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate
8267c478bd9Sstevel@tonic-gate *dst = (int)lval;
8277c478bd9Sstevel@tonic-gate return (OKUP);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate
8307c478bd9Sstevel@tonic-gate static int
scan_float(char * src,float * dst)8317c478bd9Sstevel@tonic-gate scan_float(char *src, float *dst)
8327c478bd9Sstevel@tonic-gate {
8337c478bd9Sstevel@tonic-gate float fval;
8347c478bd9Sstevel@tonic-gate
8357c478bd9Sstevel@tonic-gate errno = 0;
8367c478bd9Sstevel@tonic-gate
8377c478bd9Sstevel@tonic-gate fval = strtof(src, NULL);
8387c478bd9Sstevel@tonic-gate if (errno || fval < 0.0) {
8397c478bd9Sstevel@tonic-gate mesg(MERR, nerr_fmt, src);
8407c478bd9Sstevel@tonic-gate return (NOUP);
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate
8437c478bd9Sstevel@tonic-gate *dst = fval;
8447c478bd9Sstevel@tonic-gate return (OKUP);
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate
8477c478bd9Sstevel@tonic-gate
8487c478bd9Sstevel@tonic-gate int
dreads(void)8497c478bd9Sstevel@tonic-gate dreads(void)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate return (scan_int(LINEARG(1), &new_cc.diskreads_thold));
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate
8547c478bd9Sstevel@tonic-gate
8557c478bd9Sstevel@tonic-gate /*
8567c478bd9Sstevel@tonic-gate * Set pathname for idlecheck;
8577c478bd9Sstevel@tonic-gate * an overflowed pathname is treated as a fatal error.
8587c478bd9Sstevel@tonic-gate */
8597c478bd9Sstevel@tonic-gate int
idlechk(void)8607c478bd9Sstevel@tonic-gate idlechk(void)
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path");
8637c478bd9Sstevel@tonic-gate return (OKUP);
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate
8667c478bd9Sstevel@tonic-gate
8677c478bd9Sstevel@tonic-gate int
loadavg(void)8687c478bd9Sstevel@tonic-gate loadavg(void)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate return (scan_float(LINEARG(1), &new_cc.loadaverage_thold));
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate
8737c478bd9Sstevel@tonic-gate
8747c478bd9Sstevel@tonic-gate int
nfsreq(void)8757c478bd9Sstevel@tonic-gate nfsreq(void)
8767c478bd9Sstevel@tonic-gate {
8777c478bd9Sstevel@tonic-gate return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold));
8787c478bd9Sstevel@tonic-gate }
8797c478bd9Sstevel@tonic-gate
8807c478bd9Sstevel@tonic-gate #ifdef sparc
8817c478bd9Sstevel@tonic-gate static char open_fmt[] = "cannot open \"%s\", %s\n";
8827c478bd9Sstevel@tonic-gate
8837c478bd9Sstevel@tonic-gate /*
8847c478bd9Sstevel@tonic-gate * Verify the filesystem type for a regular statefile is "ufs"
8857c478bd9Sstevel@tonic-gate * or verify a block device is not in use as a mounted filesytem.
8867c478bd9Sstevel@tonic-gate * Returns 1 if any error, otherwise 0.
8877c478bd9Sstevel@tonic-gate */
8887c478bd9Sstevel@tonic-gate static int
check_mount(char * sfile,dev_t sfdev,int ufs)8897c478bd9Sstevel@tonic-gate check_mount(char *sfile, dev_t sfdev, int ufs)
8907c478bd9Sstevel@tonic-gate {
8917c478bd9Sstevel@tonic-gate char *src, *err_fmt = NULL, *mnttab = MNTTAB;
8927c478bd9Sstevel@tonic-gate int rgent, match = 0;
893e7cbe64fSgw25295 struct mnttab zroot = { 0 };
894e7cbe64fSgw25295 struct mnttab entry;
8957c478bd9Sstevel@tonic-gate struct extmnttab ent;
8967c478bd9Sstevel@tonic-gate FILE *fp;
8977c478bd9Sstevel@tonic-gate
8987c478bd9Sstevel@tonic-gate if ((fp = fopen(mnttab, "r")) == NULL) {
8997c478bd9Sstevel@tonic-gate mesg(MERR, open_fmt, mnttab, strerror(errno));
9007c478bd9Sstevel@tonic-gate return (1);
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate
903e7cbe64fSgw25295 if (ufs) {
904e7cbe64fSgw25295 zroot.mnt_mountp = "/";
905e7cbe64fSgw25295 zroot.mnt_fstype = "zfs";
906e7cbe64fSgw25295 if (getmntany(fp, &entry, &zroot) == 0) {
907e7cbe64fSgw25295 err_fmt = "ufs statefile with zfs root is not"
908e7cbe64fSgw25295 " supported\n";
909e7cbe64fSgw25295 mesg(MERR, err_fmt, sfile);
91080148899SSurya Prakki (void) fclose(fp);
911e7cbe64fSgw25295 return (1);
912e7cbe64fSgw25295 }
913e7cbe64fSgw25295 resetmnttab(fp);
914e7cbe64fSgw25295 }
9157c478bd9Sstevel@tonic-gate /*
9167c478bd9Sstevel@tonic-gate * Search for a matching dev_t;
9177c478bd9Sstevel@tonic-gate * ignore non-ufs filesystems for a regular statefile.
9187c478bd9Sstevel@tonic-gate */
9197c478bd9Sstevel@tonic-gate while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) {
9207c478bd9Sstevel@tonic-gate if (rgent > 0) {
9217c478bd9Sstevel@tonic-gate mesg(MERR, "error reading \"%s\"\n", mnttab);
9227c478bd9Sstevel@tonic-gate (void) fclose(fp);
9237c478bd9Sstevel@tonic-gate return (1);
9247c478bd9Sstevel@tonic-gate } else if (ufs && strcmp(ent.mnt_fstype, "ufs"))
9257c478bd9Sstevel@tonic-gate continue;
9267c478bd9Sstevel@tonic-gate else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) {
9277c478bd9Sstevel@tonic-gate match = 1;
9287c478bd9Sstevel@tonic-gate break;
9297c478bd9Sstevel@tonic-gate }
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate
9327c478bd9Sstevel@tonic-gate /*
9337c478bd9Sstevel@tonic-gate * No match is needed for a block device statefile,
9347c478bd9Sstevel@tonic-gate * a match is needed for a regular statefile.
9357c478bd9Sstevel@tonic-gate */
9367c478bd9Sstevel@tonic-gate if (match == 0) {
937e7cbe64fSgw25295 if (new_cc.cf_type != CFT_UFS)
9387c478bd9Sstevel@tonic-gate STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile");
9397c478bd9Sstevel@tonic-gate else
9407c478bd9Sstevel@tonic-gate err_fmt = "cannot find ufs mount point for \"%s\"\n";
9417c478bd9Sstevel@tonic-gate } else if (new_cc.cf_type == CFT_UFS) {
9427c478bd9Sstevel@tonic-gate STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry");
9437c478bd9Sstevel@tonic-gate STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special");
9447c478bd9Sstevel@tonic-gate while (*(sfile + 1) == '/') sfile++;
9457c478bd9Sstevel@tonic-gate src = sfile + strlen(ent.mnt_mountp);
9467c478bd9Sstevel@tonic-gate while (*src == '/') src++;
9477c478bd9Sstevel@tonic-gate STRCPYLIM(new_cc.cf_path, src, "statefile path");
9487c478bd9Sstevel@tonic-gate } else
9497c478bd9Sstevel@tonic-gate err_fmt = "statefile device \"%s\" is a mounted filesystem\n";
950e7cbe64fSgw25295 (void) fclose(fp);
9517c478bd9Sstevel@tonic-gate if (err_fmt)
9527c478bd9Sstevel@tonic-gate mesg(MERR, err_fmt, sfile);
9537c478bd9Sstevel@tonic-gate return (err_fmt != NULL);
9547c478bd9Sstevel@tonic-gate }
9557c478bd9Sstevel@tonic-gate
9567c478bd9Sstevel@tonic-gate
9577c478bd9Sstevel@tonic-gate /*
9587c478bd9Sstevel@tonic-gate * Convert a Unix device to a prom device and save on success,
9597c478bd9Sstevel@tonic-gate * log any ioctl/conversion error.
9607c478bd9Sstevel@tonic-gate */
9617c478bd9Sstevel@tonic-gate static int
utop(char * fs_name,char * prom_name)962e7cbe64fSgw25295 utop(char *fs_name, char *prom_name)
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate union obpbuf {
9657c478bd9Sstevel@tonic-gate char buf[OBP_MAXPATHLEN + sizeof (uint_t)];
9667c478bd9Sstevel@tonic-gate struct openpromio oppio;
9677c478bd9Sstevel@tonic-gate };
9687c478bd9Sstevel@tonic-gate union obpbuf oppbuf;
9697c478bd9Sstevel@tonic-gate struct openpromio *opp;
9707c478bd9Sstevel@tonic-gate char *promdev = "/dev/openprom";
9717c478bd9Sstevel@tonic-gate int fd, upval;
9727c478bd9Sstevel@tonic-gate
9737c478bd9Sstevel@tonic-gate if ((fd = open(promdev, O_RDONLY)) == -1) {
9747c478bd9Sstevel@tonic-gate mesg(MERR, open_fmt, promdev, strerror(errno));
9757c478bd9Sstevel@tonic-gate return (NOUP);
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate
9787c478bd9Sstevel@tonic-gate opp = &oppbuf.oppio;
9797c478bd9Sstevel@tonic-gate opp->oprom_size = OBP_MAXPATHLEN;
980e7cbe64fSgw25295 strcpy_limit(opp->oprom_array, fs_name,
9817c478bd9Sstevel@tonic-gate OBP_MAXPATHLEN, "statefile device");
9827c478bd9Sstevel@tonic-gate upval = ioctl(fd, OPROMDEV2PROMNAME, opp);
9837c478bd9Sstevel@tonic-gate (void) close(fd);
984e7cbe64fSgw25295 if (upval == OKUP) {
985e7cbe64fSgw25295 strcpy_limit(prom_name, opp->oprom_array, OBP_MAXPATHLEN,
986e7cbe64fSgw25295 "prom device");
987e7cbe64fSgw25295 } else {
9887c478bd9Sstevel@tonic-gate openlog("pmconfig", 0, LOG_DAEMON);
9897c478bd9Sstevel@tonic-gate syslog(LOG_NOTICE,
9907c478bd9Sstevel@tonic-gate gettext("cannot convert \"%s\" to prom device"),
991e7cbe64fSgw25295 fs_name);
9927c478bd9Sstevel@tonic-gate closelog();
9937c478bd9Sstevel@tonic-gate }
9947c478bd9Sstevel@tonic-gate
9957c478bd9Sstevel@tonic-gate return (upval);
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate
998e7cbe64fSgw25295 /*
999e7cbe64fSgw25295 * given the path to a zvol, return the cXtYdZ name
1000e7cbe64fSgw25295 * returns < 0 on error, 0 if it isn't a zvol, > 1 on success
1001e7cbe64fSgw25295 */
1002e7cbe64fSgw25295 static int
ztop(char * arg,char * diskname)1003e7cbe64fSgw25295 ztop(char *arg, char *diskname)
1004e7cbe64fSgw25295 {
1005e7cbe64fSgw25295 zpool_handle_t *zpool_handle;
1006e7cbe64fSgw25295 nvlist_t *config, *nvroot;
1007e7cbe64fSgw25295 nvlist_t **child;
1008e7cbe64fSgw25295 uint_t children;
1009e7cbe64fSgw25295 libzfs_handle_t *lzfs;
1010e7cbe64fSgw25295 char *vname;
1011e7cbe64fSgw25295 char *p;
1012e7cbe64fSgw25295 char pool_name[MAXPATHLEN];
1013e7cbe64fSgw25295
1014e7cbe64fSgw25295 if (strncmp(arg, "/dev/zvol/dsk/", 14)) {
1015e7cbe64fSgw25295 return (0);
1016e7cbe64fSgw25295 }
1017e7cbe64fSgw25295 arg += 14;
101880148899SSurya Prakki (void) strncpy(pool_name, arg, MAXPATHLEN);
10194f1c6a56SGary Mills if ((p = strchr(pool_name, '/')) != NULL)
1020e7cbe64fSgw25295 *p = '\0';
1021e7cbe64fSgw25295 STRCPYLIM(new_cc.cf_fs, p + 1, "statefile path");
1022e7cbe64fSgw25295
1023e7cbe64fSgw25295 if ((lzfs = libzfs_init()) == NULL) {
1024e7cbe64fSgw25295 mesg(MERR, "failed to initialize ZFS library\n");
1025e7cbe64fSgw25295 return (-1);
1026e7cbe64fSgw25295 }
1027e7cbe64fSgw25295 if ((zpool_handle = zpool_open(lzfs, pool_name)) == NULL) {
1028e7cbe64fSgw25295 mesg(MERR, "couldn't open pool '%s'\n", pool_name);
1029e7cbe64fSgw25295 libzfs_fini(lzfs);
1030e7cbe64fSgw25295 return (-1);
1031e7cbe64fSgw25295 }
1032e7cbe64fSgw25295 config = zpool_get_config(zpool_handle, NULL);
1033e7cbe64fSgw25295 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1034e7cbe64fSgw25295 &nvroot) != 0) {
1035e7cbe64fSgw25295 zpool_close(zpool_handle);
1036e7cbe64fSgw25295 libzfs_fini(lzfs);
1037e7cbe64fSgw25295 return (-1);
1038e7cbe64fSgw25295 }
1039e7cbe64fSgw25295 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1040e7cbe64fSgw25295 &child, &children) == 0);
1041e7cbe64fSgw25295 if (children != 1) {
1042e7cbe64fSgw25295 mesg(MERR, "expected one vdev, got %d\n", children);
1043e7cbe64fSgw25295 zpool_close(zpool_handle);
1044e7cbe64fSgw25295 libzfs_fini(lzfs);
1045e7cbe64fSgw25295 return (-1);
1046e7cbe64fSgw25295 }
104788ecc943SGeorge Wilson vname = zpool_vdev_name(lzfs, zpool_handle, child[0], B_FALSE);
1048e7cbe64fSgw25295 if (vname == NULL) {
1049e7cbe64fSgw25295 mesg(MERR, "couldn't determine vdev name\n");
1050e7cbe64fSgw25295 zpool_close(zpool_handle);
1051e7cbe64fSgw25295 libzfs_fini(lzfs);
1052e7cbe64fSgw25295 return (-1);
1053e7cbe64fSgw25295 }
105480148899SSurya Prakki (void) strcpy(diskname, "/dev/dsk/");
105580148899SSurya Prakki (void) strcat(diskname, vname);
1056e7cbe64fSgw25295 free(vname);
1057e7cbe64fSgw25295 zpool_close(zpool_handle);
1058e7cbe64fSgw25295 libzfs_fini(lzfs);
1059e7cbe64fSgw25295 return (1);
1060e7cbe64fSgw25295 }
1061e7cbe64fSgw25295
1062e7cbe64fSgw25295 /*
1063e7cbe64fSgw25295 * returns NULL if the slice is good (e.g. does not start at block
1064e7cbe64fSgw25295 * zero, or a string describing the error if it doesn't
1065e7cbe64fSgw25295 */
1066e7cbe64fSgw25295 static boolean_t
is_good_slice(char * sfile,char ** err)1067e7cbe64fSgw25295 is_good_slice(char *sfile, char **err)
1068e7cbe64fSgw25295 {
1069e7cbe64fSgw25295 int fd, rc;
1070e7cbe64fSgw25295 struct vtoc vtoc;
1071e7cbe64fSgw25295 dk_gpt_t *gpt;
1072e7cbe64fSgw25295 char rdskname[MAXPATHLEN];
1073e7cbe64fSgw25295 char *x, *y;
1074e7cbe64fSgw25295
1075e7cbe64fSgw25295 *err = NULL;
1076e7cbe64fSgw25295 /* convert from dsk to rdsk */
1077e7cbe64fSgw25295 STRCPYLIM(rdskname, sfile, "disk name");
1078e7cbe64fSgw25295 x = strstr(rdskname, "dsk/");
1079e7cbe64fSgw25295 y = strstr(sfile, "dsk/");
1080e7cbe64fSgw25295 if (x != NULL) {
1081e7cbe64fSgw25295 *x++ = 'r';
108280148899SSurya Prakki (void) strcpy(x, y);
1083e7cbe64fSgw25295 }
1084e7cbe64fSgw25295
1085e7cbe64fSgw25295 if ((fd = open(rdskname, O_RDONLY)) == -1) {
1086e7cbe64fSgw25295 *err = "could not open '%s'\n";
1087e7cbe64fSgw25295 } else if ((rc = read_vtoc(fd, &vtoc)) >= 0) {
1088e7cbe64fSgw25295 /*
1089e7cbe64fSgw25295 * we got a slice number; now check the block
1090e7cbe64fSgw25295 * number where the slice starts
1091e7cbe64fSgw25295 */
1092e7cbe64fSgw25295 if (vtoc.v_part[rc].p_start < 2)
1093e7cbe64fSgw25295 *err = "using '%s' would clobber the disk label\n";
109480148899SSurya Prakki (void) close(fd);
1095e7cbe64fSgw25295 return (*err ? B_FALSE : B_TRUE);
1096e7cbe64fSgw25295 } else if ((rc == VT_ENOTSUP) &&
1097e7cbe64fSgw25295 (efi_alloc_and_read(fd, &gpt)) >= 0) {
1098e7cbe64fSgw25295 /* EFI slices don't clobber the disk label */
1099e7cbe64fSgw25295 free(gpt);
110080148899SSurya Prakki (void) close(fd);
1101e7cbe64fSgw25295 return (B_TRUE);
1102e7cbe64fSgw25295 } else
1103e7cbe64fSgw25295 *err = "could not read partition table from '%s'\n";
1104e7cbe64fSgw25295 return (B_FALSE);
1105e7cbe64fSgw25295 }
11067c478bd9Sstevel@tonic-gate
11077c478bd9Sstevel@tonic-gate /*
11087c478bd9Sstevel@tonic-gate * Check for a valid statefile pathname, inode and mount status.
11097c478bd9Sstevel@tonic-gate */
11107c478bd9Sstevel@tonic-gate int
sfpath(void)11117c478bd9Sstevel@tonic-gate sfpath(void)
11127c478bd9Sstevel@tonic-gate {
11137c478bd9Sstevel@tonic-gate static int statefile;
11147c478bd9Sstevel@tonic-gate char *err_fmt = NULL;
11157c478bd9Sstevel@tonic-gate char *sfile, *sp, ch;
1116e7cbe64fSgw25295 char diskname[256];
11177c478bd9Sstevel@tonic-gate struct stat stbuf;
11187c478bd9Sstevel@tonic-gate int dir = 0;
1119*ff904c11SGordon Ross dev_t dev = NODEV;
11207c478bd9Sstevel@tonic-gate
11217c478bd9Sstevel@tonic-gate if (statefile) {
11227c478bd9Sstevel@tonic-gate mesg(MERR, "ignored redundant statefile entry\n");
11237c478bd9Sstevel@tonic-gate return (OKUP);
11247c478bd9Sstevel@tonic-gate } else if (ua_err) {
11257c478bd9Sstevel@tonic-gate if (ua_err != ENOTSUP)
11267c478bd9Sstevel@tonic-gate mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n",
11277c478bd9Sstevel@tonic-gate strerror(ua_err));
11287c478bd9Sstevel@tonic-gate return (NOUP);
11297c478bd9Sstevel@tonic-gate }
11307c478bd9Sstevel@tonic-gate
11317c478bd9Sstevel@tonic-gate /*
11327c478bd9Sstevel@tonic-gate * Check for an absolute path and trim any trailing '/'.
11337c478bd9Sstevel@tonic-gate */
11347c478bd9Sstevel@tonic-gate sfile = LINEARG(1);
11357c478bd9Sstevel@tonic-gate if (*sfile != '/') {
11367c478bd9Sstevel@tonic-gate mesg(MERR, "statefile requires an absolute path\n");
11377c478bd9Sstevel@tonic-gate return (NOUP);
11387c478bd9Sstevel@tonic-gate }
11397c478bd9Sstevel@tonic-gate for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--)
11407c478bd9Sstevel@tonic-gate *sp = '\0';
11417c478bd9Sstevel@tonic-gate
11427c478bd9Sstevel@tonic-gate /*
11437c478bd9Sstevel@tonic-gate * If the statefile doesn't exist, the leading path must be a dir.
11447c478bd9Sstevel@tonic-gate */
11457c478bd9Sstevel@tonic-gate if (stat(sfile, &stbuf) == -1) {
11467c478bd9Sstevel@tonic-gate if (errno == ENOENT) {
11477c478bd9Sstevel@tonic-gate dir = 1;
11487c478bd9Sstevel@tonic-gate if ((sp = strrchr(sfile, '/')) == sfile)
11497c478bd9Sstevel@tonic-gate sp++;
11507c478bd9Sstevel@tonic-gate ch = *sp;
11517c478bd9Sstevel@tonic-gate *sp = '\0';
11527c478bd9Sstevel@tonic-gate if (stat(sfile, &stbuf) == -1)
11537c478bd9Sstevel@tonic-gate err_fmt = stat_fmt;
11547c478bd9Sstevel@tonic-gate *sp = ch;
11557c478bd9Sstevel@tonic-gate } else
11567c478bd9Sstevel@tonic-gate err_fmt = stat_fmt;
11577c478bd9Sstevel@tonic-gate if (err_fmt) {
11587c478bd9Sstevel@tonic-gate mesg(MERR, err_fmt, sfile, strerror(errno));
11597c478bd9Sstevel@tonic-gate return (NOUP);
11607c478bd9Sstevel@tonic-gate }
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate
11637c478bd9Sstevel@tonic-gate /*
11647c478bd9Sstevel@tonic-gate * Check for regular/dir/block types, set cf_type and dev.
11657c478bd9Sstevel@tonic-gate */
11667c478bd9Sstevel@tonic-gate if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) {
11677c478bd9Sstevel@tonic-gate new_cc.cf_type = CFT_UFS;
11687c478bd9Sstevel@tonic-gate dev = stbuf.st_dev;
11697c478bd9Sstevel@tonic-gate } else if (S_ISBLK(stbuf.st_mode)) {
1170e7cbe64fSgw25295 if (is_good_slice(sfile, &err_fmt)) {
1171e7cbe64fSgw25295 switch (ztop(sfile, diskname)) {
1172e7cbe64fSgw25295 case 1:
1173e7cbe64fSgw25295 new_cc.cf_type = CFT_ZVOL;
1174e7cbe64fSgw25295 break;
1175e7cbe64fSgw25295 case 0:
11767c478bd9Sstevel@tonic-gate new_cc.cf_type = CFT_SPEC;
1177e7cbe64fSgw25295 break;
1178e7cbe64fSgw25295 case -1:
1179e7cbe64fSgw25295 default:
1180e7cbe64fSgw25295 return (NOUP);
1181e7cbe64fSgw25295 }
11827c478bd9Sstevel@tonic-gate dev = stbuf.st_rdev;
1183e7cbe64fSgw25295 }
11847c478bd9Sstevel@tonic-gate } else
11857c478bd9Sstevel@tonic-gate err_fmt = "bad file type for \"%s\"\n"
11867c478bd9Sstevel@tonic-gate "statefile must be a regular file or block device\n";
11877c478bd9Sstevel@tonic-gate if (err_fmt) {
11887c478bd9Sstevel@tonic-gate mesg(MERR, err_fmt, sfile);
11897c478bd9Sstevel@tonic-gate return (NOUP);
11907c478bd9Sstevel@tonic-gate }
1191e7cbe64fSgw25295 if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)))
11927c478bd9Sstevel@tonic-gate return (NOUP);
1193e7cbe64fSgw25295 if (new_cc.cf_type == CFT_ZVOL) {
1194e7cbe64fSgw25295 if (utop(diskname, new_cc.cf_dev_prom))
1195e7cbe64fSgw25295 return (NOUP);
1196e7cbe64fSgw25295 } else if (utop(new_cc.cf_devfs, new_cc.cf_dev_prom)) {
1197e7cbe64fSgw25295 return (NOUP);
1198e7cbe64fSgw25295 }
11997c478bd9Sstevel@tonic-gate new_cc.cf_magic = CPR_CONFIG_MAGIC;
12007c478bd9Sstevel@tonic-gate statefile = 1;
12017c478bd9Sstevel@tonic-gate return (OKUP);
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate #endif /* sparc */
12047c478bd9Sstevel@tonic-gate
12057c478bd9Sstevel@tonic-gate
12067c478bd9Sstevel@tonic-gate /*
1207c42872d4Smh27603 * Common function to set a system or cpu threshold.
12087c478bd9Sstevel@tonic-gate */
1209c42872d4Smh27603 static int
cmnthr(int req)1210c42872d4Smh27603 cmnthr(int req)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate int value, nerr = 0, upval = OKUP;
12137c478bd9Sstevel@tonic-gate char *thresh = LINEARG(1);
12147c478bd9Sstevel@tonic-gate
12157c478bd9Sstevel@tonic-gate if (strcmp(thresh, always_on) == 0)
12167c478bd9Sstevel@tonic-gate value = INT_MAX;
12177c478bd9Sstevel@tonic-gate else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) {
12187c478bd9Sstevel@tonic-gate mesg(MERR, "%s must be a positive value\n", LINEARG(0));
12197c478bd9Sstevel@tonic-gate upval = NOUP;
12207c478bd9Sstevel@tonic-gate }
12217c478bd9Sstevel@tonic-gate if (upval == OKUP)
1222c42872d4Smh27603 (void) ioctl(pm_fd, req, value);
12237c478bd9Sstevel@tonic-gate return (upval);
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate
12267c478bd9Sstevel@tonic-gate
1227c42872d4Smh27603 /*
1228c42872d4Smh27603 * Try setting system threshold.
1229c42872d4Smh27603 */
1230c42872d4Smh27603 int
systhr(void)1231c42872d4Smh27603 systhr(void)
1232c42872d4Smh27603 {
1233c42872d4Smh27603 return (cmnthr(PM_SET_SYSTEM_THRESHOLD));
1234c42872d4Smh27603 }
1235c42872d4Smh27603
1236c42872d4Smh27603
1237c42872d4Smh27603 /*
1238c42872d4Smh27603 * Try setting cpu threshold.
1239c42872d4Smh27603 */
1240c42872d4Smh27603 int
cputhr(void)1241c42872d4Smh27603 cputhr(void)
1242c42872d4Smh27603 {
1243c42872d4Smh27603 return (cmnthr(PM_SET_CPU_THRESHOLD));
1244c42872d4Smh27603 }
1245c42872d4Smh27603
1246c42872d4Smh27603
12477c478bd9Sstevel@tonic-gate int
tchars(void)12487c478bd9Sstevel@tonic-gate tchars(void)
12497c478bd9Sstevel@tonic-gate {
12507c478bd9Sstevel@tonic-gate return (scan_int(LINEARG(1), &new_cc.ttychars_thold));
12517c478bd9Sstevel@tonic-gate }
1252