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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*1dc065c6Smh27603 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * "pmconfig" performs a mixture of Energy-Star configuration tasks 317c478bd9Sstevel@tonic-gate * for both CheckPoint-Resume and Power-Management services. 327c478bd9Sstevel@tonic-gate * Tasks include parsing a config file (usually "/etc/power.conf"), 337c478bd9Sstevel@tonic-gate * updating CPR and PM config files, and setting various PM options 347c478bd9Sstevel@tonic-gate * via ioctl requests. From the mix, pmconfig should have a more 357c478bd9Sstevel@tonic-gate * generalized name similar to "estarconfig". 367c478bd9Sstevel@tonic-gate * 377c478bd9Sstevel@tonic-gate * OPTIONS: 387c478bd9Sstevel@tonic-gate * "-r" reset CPR and PM options to default and exit. 397c478bd9Sstevel@tonic-gate * "-f file" specify an alternate config file; this is a 407c478bd9Sstevel@tonic-gate * private/non-advertised option used by "dtpower". 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include "pmconfig.h" 447c478bd9Sstevel@tonic-gate #include <sys/wait.h> 457c478bd9Sstevel@tonic-gate #include <signal.h> 467c478bd9Sstevel@tonic-gate #include <stdarg.h> 477c478bd9Sstevel@tonic-gate #include <locale.h> 487c478bd9Sstevel@tonic-gate #include "powerd.h" 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #define MCCPY_FIELD(dst, src, field) \ 527c478bd9Sstevel@tonic-gate (void) memccpy(&dst.field, &src.field, 0, sizeof (dst.field) - 1) 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate static char conf_header[] = 567c478bd9Sstevel@tonic-gate "#\n" 577c478bd9Sstevel@tonic-gate "# Copyright 1996-2002 Sun Microsystems, Inc. All rights reserved.\n" 587c478bd9Sstevel@tonic-gate "# Use is subject to license terms.\n" 597c478bd9Sstevel@tonic-gate "#\n" 607c478bd9Sstevel@tonic-gate "#pragma ident \"@(#)power.conf 2.1 02/03/04 SMI\"\n" 617c478bd9Sstevel@tonic-gate "#\n" 627c478bd9Sstevel@tonic-gate "# Power Management Configuration File\n" 637c478bd9Sstevel@tonic-gate "#\n\n"; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate static char *prog; 667c478bd9Sstevel@tonic-gate static char *cpr_conf = CPR_CONFIG; 677c478bd9Sstevel@tonic-gate static char tmp_conf[] = "/etc/.tmp.conf.XXXXXX"; 687c478bd9Sstevel@tonic-gate static char orig_conf[] = "/etc/power.conf-Orig"; 697c478bd9Sstevel@tonic-gate static char default_conf[] = "/etc/power.conf"; 707c478bd9Sstevel@tonic-gate static char *power_conf = default_conf; 717c478bd9Sstevel@tonic-gate static pid_t powerd_pid; 727c478bd9Sstevel@tonic-gate static prmup_t *checkup; 737c478bd9Sstevel@tonic-gate static int tmp_fd; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate char estar_vers = ESTAR_VNONE; 767c478bd9Sstevel@tonic-gate int ua_err = 0; 777c478bd9Sstevel@tonic-gate int debug = 0; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate static struct cprconfig disk_cc; 807c478bd9Sstevel@tonic-gate struct cprconfig new_cc; 817c478bd9Sstevel@tonic-gate struct stat def_info; 827c478bd9Sstevel@tonic-gate static int fflag, rflag; 837c478bd9Sstevel@tonic-gate int pm_fd; 847c478bd9Sstevel@tonic-gate uid_t ruid; 857c478bd9Sstevel@tonic-gate int def_src; 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate static void 897c478bd9Sstevel@tonic-gate cleanup(void) 907c478bd9Sstevel@tonic-gate { 917c478bd9Sstevel@tonic-gate free(line_args); 927c478bd9Sstevel@tonic-gate if (access(tmp_conf, F_OK) == 0) 937c478bd9Sstevel@tonic-gate (void) unlink(tmp_conf); 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * Multi-purpose message output routine; also exits when 997c478bd9Sstevel@tonic-gate * (status == MEXIT), other status is non-fatal. 1007c478bd9Sstevel@tonic-gate * VARARGS2 1017c478bd9Sstevel@tonic-gate */ 1027c478bd9Sstevel@tonic-gate void 1037c478bd9Sstevel@tonic-gate mesg(int code, char *fmt, ...) 1047c478bd9Sstevel@tonic-gate { 1057c478bd9Sstevel@tonic-gate va_list vargs; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * debug is checked once here, avoiding N duplicate checks 1097c478bd9Sstevel@tonic-gate * before each MDEBUG caller and unnecessary text dupduplication. 1107c478bd9Sstevel@tonic-gate */ 1117c478bd9Sstevel@tonic-gate if (debug == 0) { 1127c478bd9Sstevel@tonic-gate /* 1137c478bd9Sstevel@tonic-gate * If debug is not enabled, skip a debug message; 1147c478bd9Sstevel@tonic-gate * lead with the program name for an error message, 1157c478bd9Sstevel@tonic-gate * and follow with a filename and line number if an 1167c478bd9Sstevel@tonic-gate * error occurs while parsing a conf file. 1177c478bd9Sstevel@tonic-gate */ 1187c478bd9Sstevel@tonic-gate if (code == MDEBUG) 1197c478bd9Sstevel@tonic-gate return; 1207c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: ", prog); 1217c478bd9Sstevel@tonic-gate if (lineno) 1227c478bd9Sstevel@tonic-gate fprintf(stderr, "\"%s\" line %d, ", power_conf, lineno); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate va_start(vargs, fmt); 1267c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, gettext(fmt), vargs); 1277c478bd9Sstevel@tonic-gate va_end(vargs); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate if (code == MEXIT) { 1307c478bd9Sstevel@tonic-gate cleanup(); 1317c478bd9Sstevel@tonic-gate exit(MEXIT); 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate static void 1377c478bd9Sstevel@tonic-gate usage(void) 1387c478bd9Sstevel@tonic-gate { 139*1dc065c6Smh27603 (void) fprintf(stderr, gettext("Usage: %s [-r]\n"), prog); 1407c478bd9Sstevel@tonic-gate exit(1); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * Lookup estar version, check if uadmin() service is supported, 1467c478bd9Sstevel@tonic-gate * and read cpr_config info from disk. 1477c478bd9Sstevel@tonic-gate */ 1487c478bd9Sstevel@tonic-gate static void 1497c478bd9Sstevel@tonic-gate get_cpr_info(void) 1507c478bd9Sstevel@tonic-gate { 1517c478bd9Sstevel@tonic-gate ssize_t nread; 1527c478bd9Sstevel@tonic-gate char *err_fmt; 1537c478bd9Sstevel@tonic-gate int fd; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate #ifdef sparc 1567c478bd9Sstevel@tonic-gate lookup_estar_vers(); 1577c478bd9Sstevel@tonic-gate if (estar_vers == ESTAR_V2) 1587c478bd9Sstevel@tonic-gate new_cc.is_cpr_default = 1; 1597c478bd9Sstevel@tonic-gate else if (estar_vers == ESTAR_V3) 1607c478bd9Sstevel@tonic-gate new_cc.is_autopm_default = 1; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate if (uadmin(A_FREEZE, AD_CHECK, 0) == 0) 1637c478bd9Sstevel@tonic-gate new_cc.is_cpr_capable = 1; 1647c478bd9Sstevel@tonic-gate else 1657c478bd9Sstevel@tonic-gate ua_err = errno; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate if ((fd = open("/dev/tod", O_RDONLY)) != -1) { 1687c478bd9Sstevel@tonic-gate new_cc.is_autowakeup_capable = 1; 1697c478bd9Sstevel@tonic-gate (void) close(fd); 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate #endif /* sparc */ 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate /* 1747c478bd9Sstevel@tonic-gate * Read in the cpr conf file. If any open or read error occurs, 1757c478bd9Sstevel@tonic-gate * display an error message only for a non-root user. The file 1767c478bd9Sstevel@tonic-gate * may not exist on a newly installed system. 1777c478bd9Sstevel@tonic-gate */ 1787c478bd9Sstevel@tonic-gate err_fmt = "%s %s; please rerun %s as root\n"; 1797c478bd9Sstevel@tonic-gate if ((fd = open(cpr_conf, O_RDONLY)) == -1) { 1807c478bd9Sstevel@tonic-gate if (ruid) 1817c478bd9Sstevel@tonic-gate mesg(MEXIT, err_fmt, gettext("cannot open"), 1827c478bd9Sstevel@tonic-gate cpr_conf, prog); 1837c478bd9Sstevel@tonic-gate } else { 1847c478bd9Sstevel@tonic-gate nread = read(fd, &disk_cc, sizeof (disk_cc)); 1857c478bd9Sstevel@tonic-gate (void) close(fd); 1867c478bd9Sstevel@tonic-gate if (nread != (ssize_t)sizeof (disk_cc)) { 1877c478bd9Sstevel@tonic-gate if (ruid) 1887c478bd9Sstevel@tonic-gate mesg(MEXIT, err_fmt, cpr_conf, 1897c478bd9Sstevel@tonic-gate gettext("file corrupted"), prog); 1907c478bd9Sstevel@tonic-gate else { 1917c478bd9Sstevel@tonic-gate (void) unlink(cpr_conf); 1927c478bd9Sstevel@tonic-gate bzero(&disk_cc, sizeof (disk_cc)); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate /* 2007c478bd9Sstevel@tonic-gate * Unconfigure and reset PM, device is left open for later use. 2017c478bd9Sstevel@tonic-gate */ 2027c478bd9Sstevel@tonic-gate static void 2037c478bd9Sstevel@tonic-gate pm_rem_reset(void) 2047c478bd9Sstevel@tonic-gate { 2057c478bd9Sstevel@tonic-gate char *err_fmt = NULL; 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate if ((pm_fd = open("/dev/pm", O_RDWR)) == -1) 2087c478bd9Sstevel@tonic-gate err_fmt = "cannot open \"/dev/pm\": %s\n"; 2097c478bd9Sstevel@tonic-gate else if (ioctl(pm_fd, PM_RESET_PM, 0) == -1) 2107c478bd9Sstevel@tonic-gate err_fmt = "cannot reset pm state: %s\n"; 2117c478bd9Sstevel@tonic-gate if (err_fmt) 2127c478bd9Sstevel@tonic-gate mesg(MEXIT, err_fmt, strerror(errno)); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate static void 2177c478bd9Sstevel@tonic-gate get_powerd_pid(void) 2187c478bd9Sstevel@tonic-gate { 2197c478bd9Sstevel@tonic-gate char pidstr[16]; 2207c478bd9Sstevel@tonic-gate int fd; 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate if ((fd = open(PIDPATH, O_RDONLY)) == -1) 2237c478bd9Sstevel@tonic-gate return; 2247c478bd9Sstevel@tonic-gate bzero(pidstr, sizeof (pidstr)); 2257c478bd9Sstevel@tonic-gate if (read(fd, pidstr, sizeof (pidstr)) > 0) { 2267c478bd9Sstevel@tonic-gate powerd_pid = atoi(pidstr); 2277c478bd9Sstevel@tonic-gate mesg(MDEBUG, "got powerd pid %ld\n", powerd_pid); 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate (void) close(fd); 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate /* 2347c478bd9Sstevel@tonic-gate * Write revised cprconfig struct to disk based on perms; 2357c478bd9Sstevel@tonic-gate * returns 1 if any error, otherwise 0. 2367c478bd9Sstevel@tonic-gate */ 2377c478bd9Sstevel@tonic-gate static int 2387c478bd9Sstevel@tonic-gate update_cprconfig(void) 2397c478bd9Sstevel@tonic-gate { 2407c478bd9Sstevel@tonic-gate struct cprconfig *wrt_cc = &new_cc; 2417c478bd9Sstevel@tonic-gate char *err_fmt = NULL; 2427c478bd9Sstevel@tonic-gate int fd; 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (rflag) { 2457c478bd9Sstevel@tonic-gate /* For "pmconfig -r" case, copy select cpr-related fields. */ 2467c478bd9Sstevel@tonic-gate new_cc.cf_magic = disk_cc.cf_magic; 2477c478bd9Sstevel@tonic-gate new_cc.cf_type = disk_cc.cf_type; 2487c478bd9Sstevel@tonic-gate MCCPY_FIELD(new_cc, disk_cc, cf_path); 2497c478bd9Sstevel@tonic-gate MCCPY_FIELD(new_cc, disk_cc, cf_fs); 2507c478bd9Sstevel@tonic-gate MCCPY_FIELD(new_cc, disk_cc, cf_devfs); 2517c478bd9Sstevel@tonic-gate MCCPY_FIELD(new_cc, disk_cc, cf_dev_prom); 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate if (!pm_status.perm) { 2557c478bd9Sstevel@tonic-gate if (cpr_status.update == NOUP) 2567c478bd9Sstevel@tonic-gate return (1); 2577c478bd9Sstevel@tonic-gate /* save new struct data with old autopm setting */ 2587c478bd9Sstevel@tonic-gate MCCPY_FIELD(new_cc, disk_cc, apm_behavior); 2597c478bd9Sstevel@tonic-gate } else if (!cpr_status.perm) { 2607c478bd9Sstevel@tonic-gate if (pm_status.update == NOUP) 2617c478bd9Sstevel@tonic-gate return (1); 2627c478bd9Sstevel@tonic-gate /* save original struct with new autopm setting */ 2637c478bd9Sstevel@tonic-gate MCCPY_FIELD(disk_cc, new_cc, apm_behavior); 2647c478bd9Sstevel@tonic-gate wrt_cc = &disk_cc; 2657c478bd9Sstevel@tonic-gate } else if (cpr_status.update == NOUP || pm_status.update == NOUP) 2667c478bd9Sstevel@tonic-gate return (1); 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate if ((fd = open(cpr_conf, O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) 2697c478bd9Sstevel@tonic-gate err_fmt = "cannot open/create \"%s\", %s\n"; 2707c478bd9Sstevel@tonic-gate else if (write(fd, wrt_cc, sizeof (*wrt_cc)) != sizeof (*wrt_cc)) 2717c478bd9Sstevel@tonic-gate err_fmt = "error writing \"%s\", %s\n"; 2727c478bd9Sstevel@tonic-gate (void) close(fd); 2737c478bd9Sstevel@tonic-gate if (err_fmt) 2747c478bd9Sstevel@tonic-gate mesg(MERR, err_fmt, cpr_conf, strerror(errno)); 2757c478bd9Sstevel@tonic-gate return (err_fmt != NULL); 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate /* 2807c478bd9Sstevel@tonic-gate * Signal old powerd when there's a valid pid, or start a new one; 2817c478bd9Sstevel@tonic-gate * returns 1 if any error, otherwise 0. 2827c478bd9Sstevel@tonic-gate */ 2837c478bd9Sstevel@tonic-gate static int 2847c478bd9Sstevel@tonic-gate restart_powerd(void) 2857c478bd9Sstevel@tonic-gate { 2867c478bd9Sstevel@tonic-gate char *powerd = "/usr/lib/power/powerd"; 2877c478bd9Sstevel@tonic-gate int status = 0; 2887c478bd9Sstevel@tonic-gate pid_t pid, wp; 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate if (powerd_pid > 0) { 2917c478bd9Sstevel@tonic-gate if (sigsend(P_PID, powerd_pid, SIGHUP) == 0) 2927c478bd9Sstevel@tonic-gate return (0); 2937c478bd9Sstevel@tonic-gate else if (errno != ESRCH) { 2947c478bd9Sstevel@tonic-gate mesg(MERR, "cannot deliver hangup to powerd\n"); 2957c478bd9Sstevel@tonic-gate return (1); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate if ((pid = fork()) == NOPID) 3007c478bd9Sstevel@tonic-gate wp = -1; 3017c478bd9Sstevel@tonic-gate else if (pid == P_MYPID) { 3027c478bd9Sstevel@tonic-gate (void) setreuid(0, 0); 3037c478bd9Sstevel@tonic-gate (void) setregid(0, 0); 3047c478bd9Sstevel@tonic-gate (void) setgroups(0, NULL); 3057c478bd9Sstevel@tonic-gate (void) execle(powerd, powerd, NULL, NULL); 3067c478bd9Sstevel@tonic-gate exit(1); 3077c478bd9Sstevel@tonic-gate } else { 3087c478bd9Sstevel@tonic-gate do { 3097c478bd9Sstevel@tonic-gate wp = waitpid(pid, &status, 0); 3107c478bd9Sstevel@tonic-gate } while (wp == -1 && errno == EINTR); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate if (wp == -1) 3147c478bd9Sstevel@tonic-gate mesg(MERR, "could not start %s\n", powerd); 3157c478bd9Sstevel@tonic-gate return (wp == -1 || status != 0); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate static void 3207c478bd9Sstevel@tonic-gate save_orig(void) 3217c478bd9Sstevel@tonic-gate { 3227c478bd9Sstevel@tonic-gate static char *args[] = { "/usr/bin/cp", default_conf, orig_conf, NULL }; 3237c478bd9Sstevel@tonic-gate struct stat stbuf; 3247c478bd9Sstevel@tonic-gate int pid; 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate if (stat(orig_conf, &stbuf) == 0 && stbuf.st_size) 3277c478bd9Sstevel@tonic-gate return; 3287c478bd9Sstevel@tonic-gate pid = fork(); 3297c478bd9Sstevel@tonic-gate if (pid == NOPID) 3307c478bd9Sstevel@tonic-gate return; 3317c478bd9Sstevel@tonic-gate else if (pid == P_MYPID) { 3327c478bd9Sstevel@tonic-gate (void) execve(args[0], args, NULL); 3337c478bd9Sstevel@tonic-gate exit(1); 3347c478bd9Sstevel@tonic-gate } else 3357c478bd9Sstevel@tonic-gate (void) waitpid(pid, NULL, 0); 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate static void 3407c478bd9Sstevel@tonic-gate tmp_write(void *buf, size_t len) 3417c478bd9Sstevel@tonic-gate { 3427c478bd9Sstevel@tonic-gate if (write(tmp_fd, buf, len) != (ssize_t)len) 3437c478bd9Sstevel@tonic-gate mesg(MEXIT, "error writing tmp file, %s\n", strerror(errno)); 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate static void 3487c478bd9Sstevel@tonic-gate tmp_save_line(char *line, size_t len, cinfo_t *cip) 3497c478bd9Sstevel@tonic-gate { 3507c478bd9Sstevel@tonic-gate if (cip && cip->cmt) 3517c478bd9Sstevel@tonic-gate tmp_write(cip->cmt, strlen(cip->cmt)); 3527c478bd9Sstevel@tonic-gate tmp_write(line, len); 3537c478bd9Sstevel@tonic-gate } 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate /* 3577c478bd9Sstevel@tonic-gate * Filter conf lines and write them to the tmp file. 3587c478bd9Sstevel@tonic-gate */ 3597c478bd9Sstevel@tonic-gate static void 3607c478bd9Sstevel@tonic-gate filter(char *line, size_t len, cinfo_t *cip) 3617c478bd9Sstevel@tonic-gate { 3627c478bd9Sstevel@tonic-gate int selected; 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate /* 3657c478bd9Sstevel@tonic-gate * Lines from an alt conf file are selected when either: 3667c478bd9Sstevel@tonic-gate * cip is NULL (keyword not matched, probably an old-style device), 3677c478bd9Sstevel@tonic-gate * OR: it's both OK to accept the conf line (alt) AND either: 3687c478bd9Sstevel@tonic-gate * preference is not set (NULL checkup) OR the cpr/pm preference 3697c478bd9Sstevel@tonic-gate * (checkup) matches conftab status. 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate selected = (cip == NULL || (cip->alt && 3727c478bd9Sstevel@tonic-gate (checkup == NULL || checkup == cip->status))); 3737c478bd9Sstevel@tonic-gate mesg(MDEBUG, "filter: set \"%s\", selected %d\n", 3747c478bd9Sstevel@tonic-gate cip ? cip->status->set : "none", selected); 3757c478bd9Sstevel@tonic-gate if (selected) 3767c478bd9Sstevel@tonic-gate tmp_save_line(line, len, cip); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate /* 3817c478bd9Sstevel@tonic-gate * Set checkup for conf line selection and parse a conf file with filtering. 3827c478bd9Sstevel@tonic-gate * When pref is NULL, filter selects all conf lines from the new conf file; 3837c478bd9Sstevel@tonic-gate * otherwise filter selects only cpr or pm related lines from the new or 3847c478bd9Sstevel@tonic-gate * default conf file based on cpr or pm perm. 3857c478bd9Sstevel@tonic-gate */ 3867c478bd9Sstevel@tonic-gate static void 3877c478bd9Sstevel@tonic-gate conf_scanner(prmup_t *pref) 3887c478bd9Sstevel@tonic-gate { 3897c478bd9Sstevel@tonic-gate mesg(MDEBUG, "\nscanning set is %s\n", pref ? pref->set : "both"); 3907c478bd9Sstevel@tonic-gate checkup = pref; 3917c478bd9Sstevel@tonic-gate parse_conf_file((pref == NULL || pref->perm) 3927c478bd9Sstevel@tonic-gate ? power_conf : default_conf, filter); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate /* 3977c478bd9Sstevel@tonic-gate * Search for any non-alt entries, call the handler routine, 3987c478bd9Sstevel@tonic-gate * and write entries to the tmp file. 3997c478bd9Sstevel@tonic-gate */ 4007c478bd9Sstevel@tonic-gate static void 4017c478bd9Sstevel@tonic-gate search(char *line, size_t len, cinfo_t *cip) 4027c478bd9Sstevel@tonic-gate { 4037c478bd9Sstevel@tonic-gate int skip; 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate skip = (cip == NULL || cip->alt); 4067c478bd9Sstevel@tonic-gate mesg(MDEBUG, "search: %s\n", skip ? "skipped" : "retained"); 4077c478bd9Sstevel@tonic-gate if (skip) 4087c478bd9Sstevel@tonic-gate return; 4097c478bd9Sstevel@tonic-gate if (cip->status->perm) 4107c478bd9Sstevel@tonic-gate (void) (*cip->handler)(); 4117c478bd9Sstevel@tonic-gate tmp_save_line(line, len, cip); 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate /* 4167c478bd9Sstevel@tonic-gate * When perm and update status are OK, write a new conf file 4177c478bd9Sstevel@tonic-gate * and rename to default_conf with the original attributes; 4187c478bd9Sstevel@tonic-gate * returns 1 if any error, otherwise 0. 4197c478bd9Sstevel@tonic-gate */ 4207c478bd9Sstevel@tonic-gate static int 4217c478bd9Sstevel@tonic-gate write_conf(void) 4227c478bd9Sstevel@tonic-gate { 4237c478bd9Sstevel@tonic-gate char *name, *err_str = NULL; 4247c478bd9Sstevel@tonic-gate struct stat stbuf; 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate if ((cpr_status.perm && cpr_status.update != OKUP) || 4277c478bd9Sstevel@tonic-gate (pm_status.perm && pm_status.update != OKUP)) { 4287c478bd9Sstevel@tonic-gate mesg(MDEBUG, "\nconf not written, " 4297c478bd9Sstevel@tonic-gate "(cpr perm %d update %d), (pm perm %d update %d)\n", 4307c478bd9Sstevel@tonic-gate cpr_status.perm, cpr_status.update, 4317c478bd9Sstevel@tonic-gate pm_status.perm, pm_status.update); 4327c478bd9Sstevel@tonic-gate return (1); 4337c478bd9Sstevel@tonic-gate } 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate save_orig(); 4367c478bd9Sstevel@tonic-gate if ((tmp_fd = mkstemp(tmp_conf)) == -1) { 4377c478bd9Sstevel@tonic-gate mesg(MERR, "cannot open/create tmp file \"%s\"\n", tmp_conf); 4387c478bd9Sstevel@tonic-gate return (1); 4397c478bd9Sstevel@tonic-gate } 4407c478bd9Sstevel@tonic-gate tmp_write(conf_header, sizeof (conf_header) - 1); 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate /* 4437c478bd9Sstevel@tonic-gate * When both perms are set, save selected lines from the new file; 4447c478bd9Sstevel@tonic-gate * otherwise save selected subsets from the new and default files. 4457c478bd9Sstevel@tonic-gate */ 4467c478bd9Sstevel@tonic-gate if (cpr_status.perm && pm_status.perm) 4477c478bd9Sstevel@tonic-gate conf_scanner(NULL); 4487c478bd9Sstevel@tonic-gate else { 4497c478bd9Sstevel@tonic-gate conf_scanner(&cpr_status); 4507c478bd9Sstevel@tonic-gate conf_scanner(&pm_status); 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate /* 4547c478bd9Sstevel@tonic-gate * "dtpower" will craft an alt conf file with modified content from 4557c478bd9Sstevel@tonic-gate * /etc/power.conf, but any alt conf file is not a trusted source; 4567c478bd9Sstevel@tonic-gate * since some alt conf lines may be skipped, the trusted source is 4577c478bd9Sstevel@tonic-gate * searched for those lines to retain their functionality. 4587c478bd9Sstevel@tonic-gate */ 4597c478bd9Sstevel@tonic-gate parse_conf_file(default_conf, search); 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate (void) close(tmp_fd); 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate if (stat(name = default_conf, &stbuf) == -1) 4647c478bd9Sstevel@tonic-gate err_str = "stat"; 4657c478bd9Sstevel@tonic-gate else if (chmod(name = tmp_conf, stbuf.st_mode) == -1) 4667c478bd9Sstevel@tonic-gate err_str = "chmod"; 4677c478bd9Sstevel@tonic-gate else if (chown(tmp_conf, stbuf.st_uid, stbuf.st_gid) == -1) 4687c478bd9Sstevel@tonic-gate err_str = "chown"; 4697c478bd9Sstevel@tonic-gate else if (rename(tmp_conf, default_conf) == -1) 4707c478bd9Sstevel@tonic-gate err_str = "rename"; 4717c478bd9Sstevel@tonic-gate else 4727c478bd9Sstevel@tonic-gate mesg(MDEBUG, "\n\"%s\" renamed to \"%s\"\n", 4737c478bd9Sstevel@tonic-gate tmp_conf, default_conf); 4747c478bd9Sstevel@tonic-gate if (err_str) 4757c478bd9Sstevel@tonic-gate mesg(MERR, "cannot %s \"%s\", %s\n", 4767c478bd9Sstevel@tonic-gate err_str, name, strerror(errno)); 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate return (err_str != NULL); 4797c478bd9Sstevel@tonic-gate } 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate /* ARGSUSED */ 4837c478bd9Sstevel@tonic-gate int 4847c478bd9Sstevel@tonic-gate main(int cnt, char **vec) 4857c478bd9Sstevel@tonic-gate { 4867c478bd9Sstevel@tonic-gate int rval = 0; 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 4897c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate for (prog = *vec++; *vec && **vec == '-'; vec++) { 4927c478bd9Sstevel@tonic-gate if (strlen(*vec) > 2) 4937c478bd9Sstevel@tonic-gate usage(); 4947c478bd9Sstevel@tonic-gate switch (*(*vec + 1)) { 4957c478bd9Sstevel@tonic-gate case 'd': 4967c478bd9Sstevel@tonic-gate debug = 1; 4977c478bd9Sstevel@tonic-gate break; 4987c478bd9Sstevel@tonic-gate case 'f': 4997c478bd9Sstevel@tonic-gate fflag = 1; 5007c478bd9Sstevel@tonic-gate if ((power_conf = *++vec) == NULL) 5017c478bd9Sstevel@tonic-gate usage(); 5027c478bd9Sstevel@tonic-gate break; 5037c478bd9Sstevel@tonic-gate case 'r': 5047c478bd9Sstevel@tonic-gate rflag = 1; 5057c478bd9Sstevel@tonic-gate break; 5067c478bd9Sstevel@tonic-gate default: 5077c478bd9Sstevel@tonic-gate usage(); 5087c478bd9Sstevel@tonic-gate break; 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate if (rflag && fflag) 5127c478bd9Sstevel@tonic-gate usage(); 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate lookup_perms(); 5157c478bd9Sstevel@tonic-gate mesg(MDEBUG, "ruid %d, perms: cpr %d, pm %d\n", 5167c478bd9Sstevel@tonic-gate ruid, cpr_status.perm, pm_status.perm); 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate if ((!cpr_status.perm && !pm_status.perm) || 5197c478bd9Sstevel@tonic-gate (rflag && !(cpr_status.perm && pm_status.perm))) 5207c478bd9Sstevel@tonic-gate mesg(MEXIT, "%s\n", strerror(EACCES)); 5217c478bd9Sstevel@tonic-gate if (rflag == 0 && access(power_conf, R_OK)) 5227c478bd9Sstevel@tonic-gate mesg(MEXIT, "\"%s\" is not readable\n", power_conf); 5237c478bd9Sstevel@tonic-gate 5247c478bd9Sstevel@tonic-gate get_cpr_info(); 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate if (pm_status.perm) 5277c478bd9Sstevel@tonic-gate pm_rem_reset(); 5287c478bd9Sstevel@tonic-gate get_powerd_pid(); 5297c478bd9Sstevel@tonic-gate (void) umask(022); 5307c478bd9Sstevel@tonic-gate if (rflag) 5317c478bd9Sstevel@tonic-gate return (update_cprconfig() || restart_powerd()); 5327c478bd9Sstevel@tonic-gate if (stat(default_conf, &def_info) == -1) 5337c478bd9Sstevel@tonic-gate mesg(MEXIT, "cannot stat %s, %s\n", default_conf, 5347c478bd9Sstevel@tonic-gate strerror(errno)); 5357c478bd9Sstevel@tonic-gate new_cc.loadaverage_thold = DFLT_THOLD; 5367c478bd9Sstevel@tonic-gate parse_conf_file(power_conf, NULL); 5377c478bd9Sstevel@tonic-gate if (pm_status.perm) 5387c478bd9Sstevel@tonic-gate (void) close(pm_fd); 5397c478bd9Sstevel@tonic-gate if (fflag) 5407c478bd9Sstevel@tonic-gate rval = write_conf(); 5417c478bd9Sstevel@tonic-gate cleanup(); 5427c478bd9Sstevel@tonic-gate if (rval == 0) 5437c478bd9Sstevel@tonic-gate rval = (update_cprconfig() || restart_powerd()); 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate return (rval); 5467c478bd9Sstevel@tonic-gate } 547