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*5295Srandyf * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include <stdio.h> /* Standard */ 280Sstevel@tonic-gate #include <stdlib.h> 290Sstevel@tonic-gate #include <fcntl.h> 300Sstevel@tonic-gate #include <sys/types.h> 310Sstevel@tonic-gate #include <time.h> 320Sstevel@tonic-gate #include <string.h> 330Sstevel@tonic-gate #include <errno.h> 340Sstevel@tonic-gate #include <pwd.h> 350Sstevel@tonic-gate #include <procfs.h> 360Sstevel@tonic-gate #include <dirent.h> 370Sstevel@tonic-gate #include <thread.h> 380Sstevel@tonic-gate #include <limits.h> 390Sstevel@tonic-gate #include <sys/todio.h> /* Time-Of-Day chip */ 400Sstevel@tonic-gate #include <sys/stat.h> 410Sstevel@tonic-gate #include <sys/wait.h> 420Sstevel@tonic-gate #include <sys/ipc.h> /* IPC functions */ 430Sstevel@tonic-gate #include <signal.h> /* signal handling */ 440Sstevel@tonic-gate #include <syslog.h> 450Sstevel@tonic-gate #include <unistd.h> 460Sstevel@tonic-gate #include <libdevinfo.h> 470Sstevel@tonic-gate #include <poll.h> 480Sstevel@tonic-gate #include <sys/pm.h> /* power management driver */ 490Sstevel@tonic-gate #include <sys/uadmin.h> 500Sstevel@tonic-gate #include <sys/openpromio.h> /* for prom access */ 510Sstevel@tonic-gate #include <sys/sysmacros.h> /* for MIN & MAX macros */ 520Sstevel@tonic-gate #include <sys/modctl.h> 530Sstevel@tonic-gate #include <sys/stropts.h> /* for INFTIM */ 540Sstevel@tonic-gate #include <sys/pbio.h> 550Sstevel@tonic-gate #include <sys/cpr.h> 56*5295Srandyf #include <sys/srn.h> 570Sstevel@tonic-gate #include <stdarg.h> 580Sstevel@tonic-gate 590Sstevel@tonic-gate #include "powerd.h" 600Sstevel@tonic-gate 610Sstevel@tonic-gate /* External Functions */ 620Sstevel@tonic-gate extern struct tm *localtime_r(const time_t *, struct tm *); 630Sstevel@tonic-gate extern void sysstat_init(void); 640Sstevel@tonic-gate extern int check_tty(hrtime_t *, int); 650Sstevel@tonic-gate extern int check_disks(hrtime_t *, int); 660Sstevel@tonic-gate extern int check_load_ave(hrtime_t *, float); 670Sstevel@tonic-gate extern int check_nfs(hrtime_t *, int); 680Sstevel@tonic-gate extern int last_disk_activity(hrtime_t *, int); 690Sstevel@tonic-gate extern int last_tty_activity(hrtime_t *, int); 700Sstevel@tonic-gate extern int last_load_ave_activity(hrtime_t *); 710Sstevel@tonic-gate extern int last_nfs_activity(hrtime_t *, int); 720Sstevel@tonic-gate 730Sstevel@tonic-gate #define PM "/dev/pm" 740Sstevel@tonic-gate #define TOD "/dev/tod" 750Sstevel@tonic-gate #define PROM "/dev/openprom" 760Sstevel@tonic-gate #define PB "/dev/power_button" 77*5295Srandyf #define SRN "/dev/srn" 780Sstevel@tonic-gate #define LOGFILE "./powerd.log" 790Sstevel@tonic-gate 800Sstevel@tonic-gate #define PBM_THREAD 0 810Sstevel@tonic-gate #define ATTACH_THREAD 1 820Sstevel@tonic-gate #define NUM_THREADS 2 830Sstevel@tonic-gate 840Sstevel@tonic-gate #define CHECK_INTERVAL 5 850Sstevel@tonic-gate #define IDLECHK_INTERVAL 15 860Sstevel@tonic-gate #define MINS_TO_SECS 60 870Sstevel@tonic-gate #define HOURS_TO_SECS (60 * 60) 880Sstevel@tonic-gate #define DAYS_TO_SECS (24 * 60 * 60) 890Sstevel@tonic-gate #define HOURS_TO_MINS 60 900Sstevel@tonic-gate #define DAYS_TO_MINS (24 * 60) 910Sstevel@tonic-gate 920Sstevel@tonic-gate #define LIFETIME_SECS (7 * 365 * DAYS_TO_SECS) 930Sstevel@tonic-gate #define DEFAULT_POWER_CYCLE_LIMIT 10000 940Sstevel@tonic-gate #define DEFAULT_SYSTEM_BOARD_DATE 804582000 /* July 1, 1995 */ 950Sstevel@tonic-gate 960Sstevel@tonic-gate #define LLEN 80 970Sstevel@tonic-gate 980Sstevel@tonic-gate typedef enum {root, options} prom_node_t; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* State Variables */ 1010Sstevel@tonic-gate static struct cprconfig asinfo; 1020Sstevel@tonic-gate static time_t shutdown_time; /* Time for next shutdown check */ 1030Sstevel@tonic-gate static time_t checkidle_time; /* Time for next idleness check */ 1040Sstevel@tonic-gate static time_t last_resume; 1050Sstevel@tonic-gate pwr_info_t *info; /* private as config data buffer */ 1060Sstevel@tonic-gate static int pb_fd; /* power button driver */ 1070Sstevel@tonic-gate static int broadcast; /* Enables syslog messages */ 1080Sstevel@tonic-gate static int start_calc; 1090Sstevel@tonic-gate static int autoshutdown_en; 1100Sstevel@tonic-gate static int do_idlecheck; 1110Sstevel@tonic-gate static int got_sighup; 1120Sstevel@tonic-gate static int estar_v2_prop; 1130Sstevel@tonic-gate static int estar_v3_prop; 1140Sstevel@tonic-gate static int log_power_cycles_error = 0; 1150Sstevel@tonic-gate static int log_system_board_date_error = 0; 1160Sstevel@tonic-gate static int log_no_autoshutdown_warning = 0; 1170Sstevel@tonic-gate static mutex_t poweroff_mutex; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate static char *autoshutdown_cmd[] = { 1200Sstevel@tonic-gate "/usr/openwin/bin/sys-suspend", 1210Sstevel@tonic-gate "-n", "-d", ":0", NULL 1220Sstevel@tonic-gate }; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate static char *power_button_cmd[] = { 1250Sstevel@tonic-gate "/usr/openwin/bin/sys-suspend", 1260Sstevel@tonic-gate "-h", "-d", ":0", NULL 1270Sstevel@tonic-gate }; 1280Sstevel@tonic-gate 129*5295Srandyf static char *autoS3_cmd[] = { 130*5295Srandyf "/usr/openwin/bin/sys-suspend", 131*5295Srandyf "-n", "-d", ":0", NULL 132*5295Srandyf }; 133*5295Srandyf 1340Sstevel@tonic-gate static char pidpath[] = PIDPATH; 1350Sstevel@tonic-gate static char scratch[PATH_MAX]; 1360Sstevel@tonic-gate static char *prog; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate /* Local Functions */ 1390Sstevel@tonic-gate static void alarm_handler(int); 1400Sstevel@tonic-gate static void thaw_handler(int); 1410Sstevel@tonic-gate static void kill_handler(int); 1420Sstevel@tonic-gate static void work_handler(int); 1430Sstevel@tonic-gate static void check_shutdown(time_t *, hrtime_t *); 1440Sstevel@tonic-gate static void check_idleness(time_t *, hrtime_t *); 1450Sstevel@tonic-gate static int last_system_activity(hrtime_t *); 1460Sstevel@tonic-gate static int run_idlecheck(void); 1470Sstevel@tonic-gate static void set_alarm(time_t); 1483028Smh27603 static int poweroff(const char *, char **); 1490Sstevel@tonic-gate static int is_ok2shutdown(time_t *); 1500Sstevel@tonic-gate static int get_prom(int, prom_node_t, char *, char *, size_t); 1510Sstevel@tonic-gate static void power_button_monitor(void *); 1520Sstevel@tonic-gate static int open_pidfile(char *); 1530Sstevel@tonic-gate static int write_pidfile(int, pid_t); 1540Sstevel@tonic-gate static int read_cpr_config(void); 1550Sstevel@tonic-gate static void system_activity_monitor(void); 156*5295Srandyf static void autos3_monitor(void); 1570Sstevel@tonic-gate static void do_attach(void); 1580Sstevel@tonic-gate static void *attach_devices(void *); 159*5295Srandyf static int powerd_debug; 1600Sstevel@tonic-gate 1613028Smh27603 /* PRINTFLIKE1 */ 1620Sstevel@tonic-gate static void 1633028Smh27603 logerror(const char *fmt, ...) 1640Sstevel@tonic-gate { 1650Sstevel@tonic-gate va_list args; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate va_start(args, fmt); 1680Sstevel@tonic-gate if (broadcast) 1690Sstevel@tonic-gate vsyslog(LOG_ERR, fmt, args); 1700Sstevel@tonic-gate va_end(args); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate static void 1750Sstevel@tonic-gate estrcpy(char *dst, char *src, size_t dlen) 1760Sstevel@tonic-gate { 1770Sstevel@tonic-gate size_t slen; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate slen = strlcpy(dst, src, dlen); 1800Sstevel@tonic-gate if (slen >= dlen) { 1810Sstevel@tonic-gate logerror("%s: string too long \"%s ...\"\n" 1820Sstevel@tonic-gate "(len %d, max %d)\n", prog, dst, slen, dlen - 1); 1830Sstevel@tonic-gate exit(EXIT_FAILURE); 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate int 1890Sstevel@tonic-gate main(int argc, char *argv[]) 1900Sstevel@tonic-gate { 1910Sstevel@tonic-gate pid_t pid; 1920Sstevel@tonic-gate int pm_fd; 1930Sstevel@tonic-gate struct sigaction act; 1940Sstevel@tonic-gate sigset_t sigmask; 1950Sstevel@tonic-gate int c; 1960Sstevel@tonic-gate char errmsg[PATH_MAX + 64]; 1970Sstevel@tonic-gate int pid_fd; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate prog = argv[0]; 2000Sstevel@tonic-gate if (geteuid() != 0) { 2010Sstevel@tonic-gate (void) fprintf(stderr, "%s: Must be root\n", prog); 2020Sstevel@tonic-gate exit(EXIT_FAILURE); 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate if ((pid_fd = open_pidfile(prog)) == -1) 2060Sstevel@tonic-gate exit(EXIT_FAILURE); 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* 2090Sstevel@tonic-gate * Process options 2100Sstevel@tonic-gate */ 2110Sstevel@tonic-gate broadcast = 1; 212*5295Srandyf while ((c = getopt(argc, argv, "nd")) != EOF) { 2130Sstevel@tonic-gate switch (c) { 214*5295Srandyf case 'd': 215*5295Srandyf powerd_debug = 1; 216*5295Srandyf break; 2170Sstevel@tonic-gate case 'n': 2180Sstevel@tonic-gate broadcast = 0; 2190Sstevel@tonic-gate break; 2200Sstevel@tonic-gate case '?': 2210Sstevel@tonic-gate (void) fprintf(stderr, "Usage: %s [-n]\n", prog); 2220Sstevel@tonic-gate exit(EXIT_FAILURE); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate pm_fd = open(PM, O_RDWR); 2270Sstevel@tonic-gate if (pm_fd == -1) { 2283028Smh27603 (void) snprintf(errmsg, sizeof (errmsg), "%s: %s", prog, PM); 2290Sstevel@tonic-gate perror(errmsg); 2300Sstevel@tonic-gate exit(EXIT_FAILURE); 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate (void) close(pm_fd); 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate /* 2350Sstevel@tonic-gate * Initialize mutex lock used to insure only one command to 2360Sstevel@tonic-gate * run at a time. 2370Sstevel@tonic-gate */ 2380Sstevel@tonic-gate if (mutex_init(&poweroff_mutex, USYNC_THREAD, NULL) != 0) { 2390Sstevel@tonic-gate (void) fprintf(stderr, 240*5295Srandyf "%s: Unable to initialize mutex lock\n", prog); 2410Sstevel@tonic-gate exit(EXIT_FAILURE); 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate if ((info = (pwr_info_t *)malloc(sizeof (pwr_info_t))) == NULL) { 2453028Smh27603 (void) snprintf(errmsg, sizeof (errmsg), "%s: malloc", prog); 2460Sstevel@tonic-gate perror(errmsg); 2470Sstevel@tonic-gate exit(EXIT_FAILURE); 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate /* 2510Sstevel@tonic-gate * Daemon is set to go... 2520Sstevel@tonic-gate */ 2530Sstevel@tonic-gate if ((pid = fork()) < 0) 2540Sstevel@tonic-gate exit(EXIT_FAILURE); 2550Sstevel@tonic-gate else if (pid != 0) 2560Sstevel@tonic-gate exit(EXIT_SUCCESS); 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate pid = getpid(); 2590Sstevel@tonic-gate openlog(prog, 0, LOG_DAEMON); 2600Sstevel@tonic-gate if (write_pidfile(pid_fd, pid) == -1) /* logs errors on failure */ 2610Sstevel@tonic-gate exit(EXIT_FAILURE); 2620Sstevel@tonic-gate (void) close(pid_fd); 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate /* 2650Sstevel@tonic-gate * Close all the parent's file descriptors (Bug 1225843). 2660Sstevel@tonic-gate */ 2670Sstevel@tonic-gate closefrom(0); 2680Sstevel@tonic-gate (void) setsid(); 2690Sstevel@tonic-gate (void) chdir("/"); 2700Sstevel@tonic-gate (void) umask(0); 2710Sstevel@tonic-gate #ifdef DEBUG 2720Sstevel@tonic-gate /* 2730Sstevel@tonic-gate * Connect stdout to the console. 2740Sstevel@tonic-gate */ 2750Sstevel@tonic-gate if (dup2(open("/dev/console", O_WRONLY|O_NOCTTY), 1) == -1) { 2760Sstevel@tonic-gate logerror("Unable to connect to the console."); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate #endif 2790Sstevel@tonic-gate info->pd_flags = PD_AC; 2800Sstevel@tonic-gate info->pd_idle_time = -1; 2810Sstevel@tonic-gate info->pd_start_time = 0; 2820Sstevel@tonic-gate info->pd_finish_time = 0; 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate /* 2850Sstevel@tonic-gate * Allow SIGQUIT, SIGINT and SIGTERM signals to terminate us 2860Sstevel@tonic-gate * any time 2870Sstevel@tonic-gate */ 2880Sstevel@tonic-gate act.sa_handler = kill_handler; 2890Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 2900Sstevel@tonic-gate act.sa_flags = 0; 2910Sstevel@tonic-gate (void) sigaction(SIGQUIT, &act, NULL); 2920Sstevel@tonic-gate (void) sigaction(SIGINT, &act, NULL); 2930Sstevel@tonic-gate (void) sigaction(SIGTERM, &act, NULL); 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate (void) sigfillset(&sigmask); 2960Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGQUIT); 2970Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGINT); 2980Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGTERM); 2990Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &sigmask, NULL); 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate /* 3020Sstevel@tonic-gate * If "power_button" device node can be opened, create a new 3030Sstevel@tonic-gate * thread to monitor the power button. 3040Sstevel@tonic-gate */ 3050Sstevel@tonic-gate if ((pb_fd = open(PB, O_RDONLY)) != -1) { 306*5295Srandyf if (powerd_debug) 307*5295Srandyf logerror("powerd starting power button monitor."); 3080Sstevel@tonic-gate if (thr_create(NULL, NULL, 3090Sstevel@tonic-gate (void *(*)(void *))power_button_monitor, NULL, 3100Sstevel@tonic-gate THR_DAEMON, NULL) != 0) { 3110Sstevel@tonic-gate logerror("Unable to monitor system's power button."); 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate do_attach(); 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate /* 3180Sstevel@tonic-gate * Create a new thread to monitor system activity and suspend 3190Sstevel@tonic-gate * system if idle. 3200Sstevel@tonic-gate */ 321*5295Srandyf if (powerd_debug) 322*5295Srandyf logerror("powerd starting system activity monitor."); 3230Sstevel@tonic-gate if (thr_create(NULL, NULL, 3240Sstevel@tonic-gate (void *(*)(void *))system_activity_monitor, NULL, 3250Sstevel@tonic-gate THR_DAEMON, NULL) != 0) { 3260Sstevel@tonic-gate logerror("Unable to create thread to monitor system activity."); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate /* 330*5295Srandyf * Create a new thread to handle autos3 trigger 331*5295Srandyf */ 332*5295Srandyf if (powerd_debug) 333*5295Srandyf logerror("powerd starting autos3 monitor."); 334*5295Srandyf if (thr_create(NULL, NULL, 335*5295Srandyf (void *(*)(void *))autos3_monitor, NULL, THR_DAEMON, NULL) != 0) { 336*5295Srandyf logerror("Unable to create thread to monitor autos3 activity."); 337*5295Srandyf } 338*5295Srandyf 339*5295Srandyf /* 3400Sstevel@tonic-gate * Block until we receive an explicit terminate signal 3410Sstevel@tonic-gate */ 3420Sstevel@tonic-gate (void) sigsuspend(&sigmask); 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate return (1); 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate static void 3480Sstevel@tonic-gate system_activity_monitor(void) 3490Sstevel@tonic-gate { 3500Sstevel@tonic-gate struct sigaction act; 3510Sstevel@tonic-gate sigset_t sigmask; 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate /* 3540Sstevel@tonic-gate * Setup for gathering system's statistic. 3550Sstevel@tonic-gate */ 3560Sstevel@tonic-gate sysstat_init(); 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* 3590Sstevel@tonic-gate * In addition to the SIGQUIT, SIGINT and SIGTERM signals already 3600Sstevel@tonic-gate * being handled, this thread also needs to handle SIGHUP, SIGALRM 3610Sstevel@tonic-gate * and SIGTHAW signals. 3620Sstevel@tonic-gate */ 3630Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 3640Sstevel@tonic-gate act.sa_flags = 0; 3650Sstevel@tonic-gate act.sa_handler = alarm_handler; 3660Sstevel@tonic-gate (void) sigaction(SIGALRM, &act, NULL); 3670Sstevel@tonic-gate act.sa_handler = work_handler; 3680Sstevel@tonic-gate (void) sigaction(SIGHUP, &act, NULL); 3690Sstevel@tonic-gate act.sa_handler = thaw_handler; 3700Sstevel@tonic-gate (void) sigaction(SIGTHAW, &act, NULL); 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate /* 3730Sstevel@tonic-gate * Invoke work_handler with a dummy SIGHUP signal to read 3740Sstevel@tonic-gate * cpr config file, get autoshutdown properties and schedule 3750Sstevel@tonic-gate * an alarm if needed. 3760Sstevel@tonic-gate */ 3770Sstevel@tonic-gate work_handler(SIGHUP); 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate /* 3800Sstevel@tonic-gate * Wait for signal to read file 3810Sstevel@tonic-gate */ 3820Sstevel@tonic-gate (void) thr_sigsetmask(0, 0, &sigmask); 3830Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGHUP); 3840Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGALRM); 3850Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGTHAW); 3860Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &sigmask, NULL); 3870Sstevel@tonic-gate do { 3880Sstevel@tonic-gate (void) sigsuspend(&sigmask); 3890Sstevel@tonic-gate } while (errno == EINTR); 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate 392*5295Srandyf static void 393*5295Srandyf autos3_monitor(void) 394*5295Srandyf { 395*5295Srandyf struct pollfd poll_fd; 396*5295Srandyf srn_event_info_t srn_event; /* contains suspend type */ 397*5295Srandyf int fd, ret; 398*5295Srandyf 399*5295Srandyf fd = open(SRN, O_RDWR|O_EXCL|O_NDELAY); 400*5295Srandyf if (fd == -1) { 401*5295Srandyf logerror("Unable to open %s: %s", SRN, strerror(errno)); 402*5295Srandyf thr_exit((void *) errno); 403*5295Srandyf } 404*5295Srandyf logerror("Able to open %s", SRN); 405*5295Srandyf 406*5295Srandyf /* 407*5295Srandyf * Tell device we want the special sauce 408*5295Srandyf */ 409*5295Srandyf ret = ioctl(fd, SRN_IOC_AUTOSX, NULL); 410*5295Srandyf if (ret == -1) { 411*5295Srandyf logerror("Ioctl SRN_IOC_AUTOSX failed: %s", strerror(errno)); 412*5295Srandyf close(fd); 413*5295Srandyf thr_exit((void *) errno); 414*5295Srandyf } 415*5295Srandyf poll_fd.fd = fd; 416*5295Srandyf /*CONSTCOND*/ 417*5295Srandyf while (1) { 418*5295Srandyf poll_fd.revents = 0; 419*5295Srandyf poll_fd.events = POLLIN; 420*5295Srandyf if (poll(&poll_fd, 1, -1) < 0) { 421*5295Srandyf switch (errno) { 422*5295Srandyf case EINTR: 423*5295Srandyf case EAGAIN: 424*5295Srandyf continue; 425*5295Srandyf default: 426*5295Srandyf logerror("Poll error: %s", strerror(errno)); 427*5295Srandyf close(fd); 428*5295Srandyf thr_exit((void *) errno); 429*5295Srandyf } 430*5295Srandyf } 431*5295Srandyf 432*5295Srandyf ret = ioctl(fd, SRN_IOC_NEXTEVENT, &srn_event); 433*5295Srandyf if (ret == -1) { 434*5295Srandyf logerror("ioctl error: %s", strerror(errno)); 435*5295Srandyf close(fd); 436*5295Srandyf thr_exit((void *) errno); 437*5295Srandyf } 438*5295Srandyf switch (srn_event.ae_type) { 439*5295Srandyf case 3: /* S3 */ 440*5295Srandyf if (powerd_debug) 441*5295Srandyf logerror("ioctl returns type: %d", 442*5295Srandyf srn_event.ae_type); 443*5295Srandyf break; 444*5295Srandyf default: 445*5295Srandyf logerror("Unsupported target state %d", 446*5295Srandyf srn_event.ae_type); 447*5295Srandyf continue; 448*5295Srandyf } 449*5295Srandyf (void) poweroff("AutoS3", autoS3_cmd); 450*5295Srandyf continue; 451*5295Srandyf } 452*5295Srandyf } 453*5295Srandyf 4540Sstevel@tonic-gate static int 4550Sstevel@tonic-gate read_cpr_config(void) 4560Sstevel@tonic-gate { 4570Sstevel@tonic-gate int asfd; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate if ((asfd = open(CPR_CONFIG, O_RDONLY)) < 0) { 4600Sstevel@tonic-gate logerror("Unable to open CPR config file '%s'", CPR_CONFIG); 4610Sstevel@tonic-gate return (-1); 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate if (read(asfd, (void *)&asinfo, sizeof (asinfo)) != sizeof (asinfo)) { 4650Sstevel@tonic-gate logerror("Unable to read CPR config file '%s'", CPR_CONFIG); 4660Sstevel@tonic-gate close(asfd); 4670Sstevel@tonic-gate return (-1); 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate (void) close(asfd); 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate return (0); 4730Sstevel@tonic-gate } 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate /*ARGSUSED*/ 4760Sstevel@tonic-gate static void 4770Sstevel@tonic-gate thaw_handler(int sig) 4780Sstevel@tonic-gate { 4790Sstevel@tonic-gate start_calc = 0; 4800Sstevel@tonic-gate last_resume = time(NULL); 4810Sstevel@tonic-gate } 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate /*ARGSUSED*/ 4840Sstevel@tonic-gate static void 4850Sstevel@tonic-gate kill_handler(int sig) 4860Sstevel@tonic-gate { 4870Sstevel@tonic-gate int ret_code = EXIT_SUCCESS; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate /* 4900Sstevel@tonic-gate * Free resources 4910Sstevel@tonic-gate */ 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate free(info); 4940Sstevel@tonic-gate if (pb_fd != -1) { 4950Sstevel@tonic-gate (void) close(pb_fd); 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate (void) mutex_destroy(&poweroff_mutex); 4980Sstevel@tonic-gate (void) unlink(pidpath); 4990Sstevel@tonic-gate closelog(); 5000Sstevel@tonic-gate exit(ret_code); 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate /*ARGSUSED*/ 5040Sstevel@tonic-gate static void 5050Sstevel@tonic-gate alarm_handler(int sig) 5060Sstevel@tonic-gate { 5070Sstevel@tonic-gate time_t now; 5080Sstevel@tonic-gate hrtime_t hr_now; 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate now = time(NULL); 5110Sstevel@tonic-gate hr_now = gethrtime(); 5120Sstevel@tonic-gate if (checkidle_time <= now && checkidle_time != 0) 5130Sstevel@tonic-gate check_idleness(&now, &hr_now); 5140Sstevel@tonic-gate if (shutdown_time <= now && shutdown_time != 0) 5150Sstevel@tonic-gate check_shutdown(&now, &hr_now); 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate set_alarm(now); 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate /*ARGSUSED*/ 5210Sstevel@tonic-gate static void 5220Sstevel@tonic-gate work_handler(int sig) 5230Sstevel@tonic-gate { 5240Sstevel@tonic-gate time_t now; 5250Sstevel@tonic-gate hrtime_t hr_now; 5260Sstevel@tonic-gate struct stat stat_buf; 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate do_idlecheck = 0; 5290Sstevel@tonic-gate info->pd_flags = PD_AC; 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate /* 5320Sstevel@tonic-gate * Parse the config file for autoshutdown and idleness entries. 5330Sstevel@tonic-gate */ 5340Sstevel@tonic-gate if (read_cpr_config() < 0) 5350Sstevel@tonic-gate return; 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate /* 5380Sstevel@tonic-gate * Since Oct. 1, 1995, any new system shipped had root 5390Sstevel@tonic-gate * property "energystar-v2" defined in its prom. Systems 5400Sstevel@tonic-gate * shipped after July 1, 1999, will have "energystar-v3" 5410Sstevel@tonic-gate * property. 5420Sstevel@tonic-gate */ 5430Sstevel@tonic-gate estar_v2_prop = asinfo.is_cpr_default; 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate info->pd_flags |= asinfo.is_autowakeup_capable; 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate if (strlen(asinfo.idlecheck_path) > 0) { 5480Sstevel@tonic-gate if (stat(asinfo.idlecheck_path, &stat_buf) != 0) { 5490Sstevel@tonic-gate logerror("unable to access idlecheck program \"%s\".", 5500Sstevel@tonic-gate asinfo.idlecheck_path); 5510Sstevel@tonic-gate } else if (!(stat_buf.st_mode & S_IXUSR)) { 5520Sstevel@tonic-gate logerror("idlecheck program \"%s\" is not executable.", 5530Sstevel@tonic-gate asinfo.idlecheck_path); 5540Sstevel@tonic-gate } else { 5550Sstevel@tonic-gate do_idlecheck = 1; 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate if (strlen(asinfo.as_behavior) == 0 || 5600Sstevel@tonic-gate strcmp(asinfo.as_behavior, "noshutdown") == 0 || 5610Sstevel@tonic-gate strcmp(asinfo.as_behavior, "unconfigured") == 0) { 5620Sstevel@tonic-gate info->pd_autoshutdown = 0; 5630Sstevel@tonic-gate } else if (strcmp(asinfo.as_behavior, "default") == 0) { 5640Sstevel@tonic-gate info->pd_autoshutdown = estar_v2_prop; 5650Sstevel@tonic-gate } else if (strcmp(asinfo.as_behavior, "shutdown") == 0 || 566*5295Srandyf strcmp(asinfo.as_behavior, "autowakeup") == 0) { 5670Sstevel@tonic-gate info->pd_autoshutdown = asinfo.is_cpr_capable; 5680Sstevel@tonic-gate } else { 5690Sstevel@tonic-gate logerror("autoshutdown behavior \"%s\" unrecognized.", 5700Sstevel@tonic-gate asinfo.as_behavior); 5710Sstevel@tonic-gate info->pd_autoshutdown = 0; 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate if (info->pd_autoshutdown) { 5750Sstevel@tonic-gate info->pd_idle_time = asinfo.as_idle; 5760Sstevel@tonic-gate info->pd_start_time = 5770Sstevel@tonic-gate (asinfo.as_sh * 60 + asinfo.as_sm) % DAYS_TO_MINS; 5780Sstevel@tonic-gate info->pd_finish_time = 5790Sstevel@tonic-gate (asinfo.as_fh * 60 + asinfo.as_fm) % DAYS_TO_MINS; 5800Sstevel@tonic-gate info->pd_autoresume = 5810Sstevel@tonic-gate (strcmp(asinfo.as_behavior, "autowakeup") == 0) ? 1 : 0; 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate autoshutdown_en = (asinfo.as_idle >= 0 && info->pd_autoshutdown) 584*5295Srandyf ? 1 : 0; 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate #ifdef DEBUG 5870Sstevel@tonic-gate (void) fprintf(stderr, "autoshutdown_en = %d, as_idle = %d, " 588*5295Srandyf "pd_autoresume = %d\n", 589*5295Srandyf autoshutdown_en, asinfo.as_idle, info->pd_autoresume); 590*5295Srandyf 5910Sstevel@tonic-gate (void) fprintf(stderr, " pd_start_time=%d, pd_finish_time=%d\n", 592*5295Srandyf info->pd_start_time, info->pd_finish_time); 5930Sstevel@tonic-gate #endif 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate got_sighup = 1; 5960Sstevel@tonic-gate now = last_resume = time(NULL); 5970Sstevel@tonic-gate hr_now = gethrtime(); 5980Sstevel@tonic-gate check_idleness(&now, &hr_now); 5990Sstevel@tonic-gate check_shutdown(&now, &hr_now); 6000Sstevel@tonic-gate set_alarm(now); 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate static void 6040Sstevel@tonic-gate check_shutdown(time_t *now, hrtime_t *hr_now) 6050Sstevel@tonic-gate { 6060Sstevel@tonic-gate int tod_fd = -1; 6070Sstevel@tonic-gate int kbd, mouse, system, least_idle, idlecheck_time; 6080Sstevel@tonic-gate int next_time; 6090Sstevel@tonic-gate int s, f; 6100Sstevel@tonic-gate struct tm tmp_time; 6110Sstevel@tonic-gate time_t start_of_day, time_since_last_resume; 6120Sstevel@tonic-gate time_t wakeup_time; 6130Sstevel@tonic-gate extern long conskbd_idle_time(void); 6140Sstevel@tonic-gate extern long consms_idle_time(void); 6150Sstevel@tonic-gate static int warned_kbd, warned_ms; /* print error msg one time */ 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate if (!autoshutdown_en) { 6180Sstevel@tonic-gate shutdown_time = 0; 6190Sstevel@tonic-gate return; 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate (void) localtime_r(now, &tmp_time); 6230Sstevel@tonic-gate tmp_time.tm_sec = 0; 6240Sstevel@tonic-gate tmp_time.tm_min = 0; 6250Sstevel@tonic-gate tmp_time.tm_hour = 0; 6260Sstevel@tonic-gate start_of_day = mktime(&tmp_time); 6270Sstevel@tonic-gate s = start_of_day + info->pd_start_time * 60; 6280Sstevel@tonic-gate f = start_of_day + info->pd_finish_time * 60; 6290Sstevel@tonic-gate if ((s < f && *now >= s && *now < f) || 6300Sstevel@tonic-gate (s >= f && (*now < f || *now >= s))) { 6310Sstevel@tonic-gate if ((mouse = (int)consms_idle_time()) < 0) { 6320Sstevel@tonic-gate if (! warned_ms) { 6330Sstevel@tonic-gate warned_ms = 1; 6340Sstevel@tonic-gate logerror("powerd: failed to get " 6350Sstevel@tonic-gate "idle time for console mouse"); 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate return; 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate if ((kbd = (int)conskbd_idle_time()) < 0) { 6400Sstevel@tonic-gate if (! warned_kbd) { 6410Sstevel@tonic-gate warned_kbd = 1; 6420Sstevel@tonic-gate logerror("powerd: failed to get " 6430Sstevel@tonic-gate "idle time for console keyboard"); 6440Sstevel@tonic-gate } 6450Sstevel@tonic-gate return; 6460Sstevel@tonic-gate } 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate system = last_system_activity(hr_now); 6490Sstevel@tonic-gate /* who is the last to go idle */ 6500Sstevel@tonic-gate least_idle = MIN(system, MIN(kbd, mouse)); 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate /* 6530Sstevel@tonic-gate * Calculate time_since_last_resume and the next_time 6540Sstevel@tonic-gate * to auto suspend. 6550Sstevel@tonic-gate */ 6560Sstevel@tonic-gate start_calc = 1; 6570Sstevel@tonic-gate time_since_last_resume = time(NULL) - last_resume; 6580Sstevel@tonic-gate next_time = info->pd_idle_time * 60 - 659*5295Srandyf MIN(least_idle, time_since_last_resume); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate #ifdef DEBUG 662*5295Srandyf fprintf(stderr, " check_shutdown: next_time=%d\n", next_time); 6630Sstevel@tonic-gate #endif 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate /* 6660Sstevel@tonic-gate * If we have get the SIGTHAW signal at this point - our 6670Sstevel@tonic-gate * calculation of time_since_last_resume is wrong so 6680Sstevel@tonic-gate * - we need to recalculate. 6690Sstevel@tonic-gate */ 6700Sstevel@tonic-gate while (start_calc == 0) { 6710Sstevel@tonic-gate /* need to redo calculation */ 6720Sstevel@tonic-gate start_calc = 1; 6730Sstevel@tonic-gate time_since_last_resume = time(NULL) - last_resume; 6740Sstevel@tonic-gate next_time = info->pd_idle_time * 60 - 675*5295Srandyf MIN(least_idle, time_since_last_resume); 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate /* 6790Sstevel@tonic-gate * Only when everything else is idle, run the user's idlecheck 6800Sstevel@tonic-gate * script. 6810Sstevel@tonic-gate */ 6820Sstevel@tonic-gate if (next_time <= 0 && do_idlecheck) { 6830Sstevel@tonic-gate got_sighup = 0; 6840Sstevel@tonic-gate idlecheck_time = run_idlecheck(); 6850Sstevel@tonic-gate next_time = info->pd_idle_time * 60 - 686*5295Srandyf MIN(idlecheck_time, MIN(least_idle, 687*5295Srandyf time_since_last_resume)); 6880Sstevel@tonic-gate /* 6890Sstevel@tonic-gate * If we have caught SIGTHAW or SIGHUP, need to 6900Sstevel@tonic-gate * recalculate. 6910Sstevel@tonic-gate */ 6920Sstevel@tonic-gate while (start_calc == 0 || got_sighup == 1) { 6930Sstevel@tonic-gate start_calc = 1; 6940Sstevel@tonic-gate got_sighup = 0; 6950Sstevel@tonic-gate idlecheck_time = run_idlecheck(); 6960Sstevel@tonic-gate time_since_last_resume = time(NULL) - 697*5295Srandyf last_resume; 6980Sstevel@tonic-gate next_time = info->pd_idle_time * 60 - 699*5295Srandyf MIN(idlecheck_time, MIN(least_idle, 700*5295Srandyf time_since_last_resume)); 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate if (next_time <= 0) { 7050Sstevel@tonic-gate if (is_ok2shutdown(now)) { 7060Sstevel@tonic-gate /* 7070Sstevel@tonic-gate * Setup the autowakeup alarm. Clear it 7080Sstevel@tonic-gate * right after poweroff, just in case if 7090Sstevel@tonic-gate * shutdown doesn't go through. 7100Sstevel@tonic-gate */ 7110Sstevel@tonic-gate if (info->pd_autoresume) 7120Sstevel@tonic-gate tod_fd = open(TOD, O_RDWR); 7130Sstevel@tonic-gate if (info->pd_autoresume && tod_fd != -1) { 7140Sstevel@tonic-gate wakeup_time = (*now < f) ? f : 715*5295Srandyf (f + DAYS_TO_SECS); 7160Sstevel@tonic-gate /* 7170Sstevel@tonic-gate * A software fix for hardware 7180Sstevel@tonic-gate * bug 1217415. 7190Sstevel@tonic-gate */ 7200Sstevel@tonic-gate if ((wakeup_time - *now) < 180) { 7210Sstevel@tonic-gate logerror( 7220Sstevel@tonic-gate "Since autowakeup time is less than 3 minutes away, " 7230Sstevel@tonic-gate "autoshutdown will not occur."); 7240Sstevel@tonic-gate shutdown_time = *now + 180; 7250Sstevel@tonic-gate close(tod_fd); 7260Sstevel@tonic-gate return; 7270Sstevel@tonic-gate } 7280Sstevel@tonic-gate if (ioctl(tod_fd, TOD_SET_ALARM, 729*5295Srandyf &wakeup_time) == -1) { 730*5295Srandyf logerror("Unable to program TOD" 731*5295Srandyf " alarm for autowakeup."); 7320Sstevel@tonic-gate close(tod_fd); 7330Sstevel@tonic-gate return; 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate 7370Sstevel@tonic-gate (void) poweroff("Autoshutdown", 7380Sstevel@tonic-gate autoshutdown_cmd); 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate if (info->pd_autoresume && tod_fd != -1) { 7410Sstevel@tonic-gate if (ioctl(tod_fd, TOD_CLEAR_ALARM, 742*5295Srandyf NULL) == -1) 7430Sstevel@tonic-gate logerror("Unable to clear " 7440Sstevel@tonic-gate "alarm in TOD device."); 7450Sstevel@tonic-gate close(tod_fd); 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate (void) time(now); 7490Sstevel@tonic-gate /* wait at least 5 mins */ 7500Sstevel@tonic-gate shutdown_time = *now + 751*5295Srandyf ((info->pd_idle_time * 60) > 300 ? 752*5295Srandyf (info->pd_idle_time * 60) : 300); 7530Sstevel@tonic-gate } else { 7540Sstevel@tonic-gate /* wait 5 mins */ 7550Sstevel@tonic-gate shutdown_time = *now + 300; 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate } else 7580Sstevel@tonic-gate shutdown_time = *now + next_time; 7590Sstevel@tonic-gate } else if (s < f && *now >= f) { 7600Sstevel@tonic-gate shutdown_time = s + DAYS_TO_SECS; 7610Sstevel@tonic-gate } else 7620Sstevel@tonic-gate shutdown_time = s; 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate static int 7660Sstevel@tonic-gate is_ok2shutdown(time_t *now) 7670Sstevel@tonic-gate { 7680Sstevel@tonic-gate int prom_fd = -1; 7690Sstevel@tonic-gate char power_cycles_st[LLEN]; 7700Sstevel@tonic-gate char power_cycle_limit_st[LLEN]; 7710Sstevel@tonic-gate char system_board_date_st[LLEN]; 7720Sstevel@tonic-gate int power_cycles, power_cycle_limit, free_cycles, scaled_cycles; 7730Sstevel@tonic-gate time_t life_began, life_passed; 7740Sstevel@tonic-gate int no_power_cycles = 0; 7750Sstevel@tonic-gate int no_system_board_date = 0; 7760Sstevel@tonic-gate int ret = 1; 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate /* CONSTCOND */ 7790Sstevel@tonic-gate while (1) { 7800Sstevel@tonic-gate if ((prom_fd = open(PROM, O_RDWR)) == -1 && 781*5295Srandyf (errno == EAGAIN)) 782*5295Srandyf continue; 7830Sstevel@tonic-gate break; 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate 7860Sstevel@tonic-gate /* 7870Sstevel@tonic-gate * when #power-cycles property does not exist 7880Sstevel@tonic-gate * power cycles are unlimited. 7890Sstevel@tonic-gate */ 7900Sstevel@tonic-gate if (get_prom(prom_fd, options, "#power-cycles", 7910Sstevel@tonic-gate power_cycles_st, sizeof (power_cycles_st)) == 0) 7920Sstevel@tonic-gate goto ckdone; 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate if (get_prom(prom_fd, root, "power-cycle-limit", 7950Sstevel@tonic-gate power_cycle_limit_st, sizeof (power_cycle_limit_st)) == 0) { 7960Sstevel@tonic-gate power_cycle_limit = DEFAULT_POWER_CYCLE_LIMIT; 7970Sstevel@tonic-gate } else { 7980Sstevel@tonic-gate power_cycle_limit = atoi(power_cycle_limit_st); 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate /* 8020Sstevel@tonic-gate * Allow 10% of power_cycle_limit as free cycles. 8030Sstevel@tonic-gate */ 8043028Smh27603 free_cycles = power_cycle_limit / 10; 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate power_cycles = atoi(power_cycles_st); 8070Sstevel@tonic-gate if (power_cycles < 0) 8080Sstevel@tonic-gate no_power_cycles++; 8090Sstevel@tonic-gate else if (power_cycles <= free_cycles) 8100Sstevel@tonic-gate goto ckdone; 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate if (no_power_cycles && log_power_cycles_error == 0) { 8130Sstevel@tonic-gate logerror("Invalid PROM property \"#power-cycles\" was found."); 8140Sstevel@tonic-gate log_power_cycles_error++; 8150Sstevel@tonic-gate } 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate if (get_prom(prom_fd, options, "system-board-date", 8180Sstevel@tonic-gate system_board_date_st, sizeof (system_board_date_st)) == 0) { 8190Sstevel@tonic-gate no_system_board_date++; 8200Sstevel@tonic-gate } else { 8210Sstevel@tonic-gate life_began = strtol(system_board_date_st, (char **)NULL, 16); 8220Sstevel@tonic-gate if (life_began > *now) { 8230Sstevel@tonic-gate no_system_board_date++; 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate if (no_system_board_date) { 8270Sstevel@tonic-gate if (log_system_board_date_error == 0) { 8280Sstevel@tonic-gate logerror("No or invalid PROM property " 8290Sstevel@tonic-gate "\"system-board-date\" was found."); 8300Sstevel@tonic-gate log_system_board_date_error++; 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate life_began = DEFAULT_SYSTEM_BOARD_DATE; 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate life_passed = *now - life_began; 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate /* 8380Sstevel@tonic-gate * Since we don't keep the date that last free_cycle is ended, we 8390Sstevel@tonic-gate * need to spread (power_cycle_limit - free_cycles) over the entire 8400Sstevel@tonic-gate * 7-year life span instead of (lifetime - date free_cycles ended). 8410Sstevel@tonic-gate */ 8423028Smh27603 scaled_cycles = (int)(((float)life_passed / (float)LIFETIME_SECS) * 843*5295Srandyf (power_cycle_limit - free_cycles)); 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate if (no_power_cycles) 8460Sstevel@tonic-gate goto ckdone; 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate #ifdef DEBUG 8490Sstevel@tonic-gate (void) fprintf(stderr, "Actual power_cycles = %d\t" 850*5295Srandyf "Scaled power_cycles = %d\n", power_cycles, scaled_cycles); 8510Sstevel@tonic-gate #endif 8520Sstevel@tonic-gate if (power_cycles > scaled_cycles) { 8530Sstevel@tonic-gate if (log_no_autoshutdown_warning == 0) { 8540Sstevel@tonic-gate logerror("Automatic shutdown has been temporarily " 8550Sstevel@tonic-gate "suspended in order to preserve the reliability " 8560Sstevel@tonic-gate "of this system."); 8570Sstevel@tonic-gate log_no_autoshutdown_warning++; 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate ret = 0; 8600Sstevel@tonic-gate goto ckdone; 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate ckdone: 8640Sstevel@tonic-gate if (prom_fd != -1) 8650Sstevel@tonic-gate close(prom_fd); 8660Sstevel@tonic-gate return (ret); 8670Sstevel@tonic-gate } 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate static void 8700Sstevel@tonic-gate check_idleness(time_t *now, hrtime_t *hr_now) 8710Sstevel@tonic-gate { 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate /* 8740Sstevel@tonic-gate * Check idleness only when autoshutdown is enabled. 8750Sstevel@tonic-gate */ 8760Sstevel@tonic-gate if (!autoshutdown_en) { 8770Sstevel@tonic-gate checkidle_time = 0; 8780Sstevel@tonic-gate return; 8790Sstevel@tonic-gate } 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate info->pd_ttychars_idle = check_tty(hr_now, asinfo.ttychars_thold); 8820Sstevel@tonic-gate info->pd_loadaverage_idle = 8830Sstevel@tonic-gate check_load_ave(hr_now, asinfo.loadaverage_thold); 8840Sstevel@tonic-gate info->pd_diskreads_idle = check_disks(hr_now, asinfo.diskreads_thold); 8850Sstevel@tonic-gate info->pd_nfsreqs_idle = check_nfs(hr_now, asinfo.nfsreqs_thold); 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate #ifdef DEBUG 8880Sstevel@tonic-gate (void) fprintf(stderr, "Idle ttychars for %d secs.\n", 889*5295Srandyf info->pd_ttychars_idle); 8900Sstevel@tonic-gate (void) fprintf(stderr, "Idle loadaverage for %d secs.\n", 891*5295Srandyf info->pd_loadaverage_idle); 8920Sstevel@tonic-gate (void) fprintf(stderr, "Idle diskreads for %d secs.\n", 893*5295Srandyf info->pd_diskreads_idle); 8940Sstevel@tonic-gate (void) fprintf(stderr, "Idle nfsreqs for %d secs.\n", 895*5295Srandyf info->pd_nfsreqs_idle); 8960Sstevel@tonic-gate #endif 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate checkidle_time = *now + IDLECHK_INTERVAL; 8990Sstevel@tonic-gate } 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate static int 9020Sstevel@tonic-gate last_system_activity(hrtime_t *hr_now) 9030Sstevel@tonic-gate { 9040Sstevel@tonic-gate int act_idle, latest; 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate latest = info->pd_idle_time * 60; 9070Sstevel@tonic-gate act_idle = last_tty_activity(hr_now, asinfo.ttychars_thold); 9080Sstevel@tonic-gate latest = MIN(latest, act_idle); 9090Sstevel@tonic-gate act_idle = last_load_ave_activity(hr_now); 9100Sstevel@tonic-gate latest = MIN(latest, act_idle); 9110Sstevel@tonic-gate act_idle = last_disk_activity(hr_now, asinfo.diskreads_thold); 9120Sstevel@tonic-gate latest = MIN(latest, act_idle); 9130Sstevel@tonic-gate act_idle = last_nfs_activity(hr_now, asinfo.nfsreqs_thold); 9140Sstevel@tonic-gate latest = MIN(latest, act_idle); 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate return (latest); 9170Sstevel@tonic-gate } 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate static int 9200Sstevel@tonic-gate run_idlecheck() 9210Sstevel@tonic-gate { 9220Sstevel@tonic-gate char pm_variable[LLEN]; 9230Sstevel@tonic-gate char *cp; 9240Sstevel@tonic-gate int status; 9250Sstevel@tonic-gate pid_t child; 9260Sstevel@tonic-gate 9270Sstevel@tonic-gate /* 9280Sstevel@tonic-gate * Reap any child process which has been left over. 9290Sstevel@tonic-gate */ 930*5295Srandyf while (waitpid((pid_t)-1, &status, WNOHANG) > 0) 931*5295Srandyf ; 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate /* 9340Sstevel@tonic-gate * Execute the user's idlecheck script and set variable PM_IDLETIME. 9350Sstevel@tonic-gate * Returned exit value is the idle time in minutes. 9360Sstevel@tonic-gate */ 9370Sstevel@tonic-gate if ((child = fork1()) == 0) { 9380Sstevel@tonic-gate (void) sprintf(pm_variable, "PM_IDLETIME=%d", 939*5295Srandyf info->pd_idle_time); 9400Sstevel@tonic-gate (void) putenv(pm_variable); 9410Sstevel@tonic-gate cp = strrchr(asinfo.idlecheck_path, '/'); 9420Sstevel@tonic-gate if (cp == NULL) 9430Sstevel@tonic-gate cp = asinfo.idlecheck_path; 9440Sstevel@tonic-gate else 9450Sstevel@tonic-gate cp++; 9460Sstevel@tonic-gate (void) execl(asinfo.idlecheck_path, cp, NULL); 9470Sstevel@tonic-gate exit(-1); 9480Sstevel@tonic-gate } else if (child == -1) { 9490Sstevel@tonic-gate return (info->pd_idle_time * 60); 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate /* 9530Sstevel@tonic-gate * Wait until the idlecheck program completes. 9540Sstevel@tonic-gate */ 9550Sstevel@tonic-gate if (waitpid(child, &status, 0) != child) { 9560Sstevel@tonic-gate /* 9570Sstevel@tonic-gate * We get here if the calling process gets a signal. 9580Sstevel@tonic-gate */ 9590Sstevel@tonic-gate return (info->pd_idle_time * 60); 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate if (WEXITSTATUS(status) < 0) { 9630Sstevel@tonic-gate return (info->pd_idle_time * 60); 9640Sstevel@tonic-gate } else { 9650Sstevel@tonic-gate return (WEXITSTATUS(status) * 60); 9660Sstevel@tonic-gate } 9670Sstevel@tonic-gate } 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate static void 9700Sstevel@tonic-gate set_alarm(time_t now) 9710Sstevel@tonic-gate { 9720Sstevel@tonic-gate time_t itime, stime, next_time, max_time; 9730Sstevel@tonic-gate int next_alarm; 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate max_time = MAX(checkidle_time, shutdown_time); 9760Sstevel@tonic-gate if (max_time == 0) { 9770Sstevel@tonic-gate (void) alarm(0); 9780Sstevel@tonic-gate return; 9790Sstevel@tonic-gate } 9800Sstevel@tonic-gate itime = (checkidle_time == 0) ? max_time : checkidle_time; 9810Sstevel@tonic-gate stime = (shutdown_time == 0) ? max_time : shutdown_time; 9820Sstevel@tonic-gate next_time = MIN(itime, stime); 9830Sstevel@tonic-gate next_alarm = (next_time <= now) ? 1 : (next_time - now); 9840Sstevel@tonic-gate (void) alarm(next_alarm); 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate #ifdef DEBUG 9870Sstevel@tonic-gate (void) fprintf(stderr, "Currently @ %s", ctime(&now)); 9880Sstevel@tonic-gate (void) fprintf(stderr, "Checkidle in %d secs\n", checkidle_time - now); 9890Sstevel@tonic-gate (void) fprintf(stderr, "Shutdown in %d secs\n", shutdown_time - now); 9900Sstevel@tonic-gate (void) fprintf(stderr, "Next alarm goes off in %d secs\n", next_alarm); 9910Sstevel@tonic-gate (void) fprintf(stderr, "************************************\n"); 9920Sstevel@tonic-gate #endif 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate static int 9963028Smh27603 poweroff(const char *msg, char **cmd_argv) 9970Sstevel@tonic-gate { 9980Sstevel@tonic-gate struct stat statbuf; 9990Sstevel@tonic-gate pid_t pid, child; 10000Sstevel@tonic-gate struct passwd *pwd; 10010Sstevel@tonic-gate char *home, *user; 10020Sstevel@tonic-gate char ehome[] = "HOME="; 10030Sstevel@tonic-gate char euser[] = "LOGNAME="; 10040Sstevel@tonic-gate int status; 10050Sstevel@tonic-gate char **ca; 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate if (mutex_trylock(&poweroff_mutex) != 0) 10080Sstevel@tonic-gate return (0); 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate if (stat("/dev/console", &statbuf) == -1 || 10110Sstevel@tonic-gate (pwd = getpwuid(statbuf.st_uid)) == NULL) { 10120Sstevel@tonic-gate mutex_unlock(&poweroff_mutex); 10130Sstevel@tonic-gate return (1); 10140Sstevel@tonic-gate } 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate if (msg) 10170Sstevel@tonic-gate syslog(LOG_NOTICE, msg); 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate if (*cmd_argv == NULL) { 10200Sstevel@tonic-gate logerror("No command to run."); 10210Sstevel@tonic-gate mutex_unlock(&poweroff_mutex); 10220Sstevel@tonic-gate return (1); 10230Sstevel@tonic-gate } 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate home = malloc(strlen(pwd->pw_dir) + sizeof (ehome)); 10260Sstevel@tonic-gate user = malloc(strlen(pwd->pw_name) + sizeof (euser)); 10270Sstevel@tonic-gate if (home == NULL || user == NULL) { 10280Sstevel@tonic-gate free(home); 10290Sstevel@tonic-gate free(user); 10300Sstevel@tonic-gate logerror("No memory."); 10310Sstevel@tonic-gate mutex_unlock(&poweroff_mutex); 10320Sstevel@tonic-gate return (1); 10330Sstevel@tonic-gate } 10340Sstevel@tonic-gate (void) strcpy(home, ehome); 10350Sstevel@tonic-gate (void) strcat(home, pwd->pw_dir); 10360Sstevel@tonic-gate (void) strcpy(user, euser); 10370Sstevel@tonic-gate (void) strcat(user, pwd->pw_name); 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate /* 10400Sstevel@tonic-gate * Need to simulate the user enviroment, minimaly set HOME, and USER. 10410Sstevel@tonic-gate */ 10420Sstevel@tonic-gate if ((child = fork1()) == 0) { 10430Sstevel@tonic-gate (void) putenv(home); 10440Sstevel@tonic-gate (void) putenv(user); 10450Sstevel@tonic-gate (void) setgid(pwd->pw_gid); 10460Sstevel@tonic-gate (void) setuid(pwd->pw_uid); 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate /* 10490Sstevel@tonic-gate * check for shutdown flag and set environment 10500Sstevel@tonic-gate */ 10510Sstevel@tonic-gate for (ca = cmd_argv; *ca; ca++) { 10520Sstevel@tonic-gate if (strcmp("-h", *ca) == 0) { 10530Sstevel@tonic-gate (void) putenv("SYSSUSPENDDODEFAULT="); 10540Sstevel@tonic-gate break; 10550Sstevel@tonic-gate } 10560Sstevel@tonic-gate } 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate (void) execv(cmd_argv[0], cmd_argv); 10590Sstevel@tonic-gate exit(EXIT_FAILURE); 10600Sstevel@tonic-gate } else { 10610Sstevel@tonic-gate free(home); 10620Sstevel@tonic-gate free(user); 10630Sstevel@tonic-gate if (child == -1) { 10640Sstevel@tonic-gate mutex_unlock(&poweroff_mutex); 10650Sstevel@tonic-gate return (1); 10660Sstevel@tonic-gate } 10670Sstevel@tonic-gate } 10680Sstevel@tonic-gate pid = 0; 10690Sstevel@tonic-gate while (pid != child) 10700Sstevel@tonic-gate pid = wait(&status); 10710Sstevel@tonic-gate if (WEXITSTATUS(status)) { 10720Sstevel@tonic-gate (void) syslog(LOG_ERR, "Failed to exec \"%s\".", cmd_argv[0]); 10730Sstevel@tonic-gate mutex_unlock(&poweroff_mutex); 10740Sstevel@tonic-gate return (1); 10750Sstevel@tonic-gate } 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate mutex_unlock(&poweroff_mutex); 10780Sstevel@tonic-gate return (0); 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate #define PBUFSIZE 256 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate /* 10840Sstevel@tonic-gate * Gets the value of a prom property at either root or options node. It 10850Sstevel@tonic-gate * returns 1 if it is successful, otherwise it returns 0 . 10860Sstevel@tonic-gate */ 10870Sstevel@tonic-gate static int 10880Sstevel@tonic-gate get_prom(int prom_fd, prom_node_t node_name, 10890Sstevel@tonic-gate char *property_name, char *property_value, size_t len) 10900Sstevel@tonic-gate { 10910Sstevel@tonic-gate union { 10920Sstevel@tonic-gate char buf[PBUFSIZE + sizeof (uint_t)]; 10930Sstevel@tonic-gate struct openpromio opp; 10940Sstevel@tonic-gate } oppbuf; 10950Sstevel@tonic-gate register struct openpromio *opp = &(oppbuf.opp); 10960Sstevel@tonic-gate int got_it = 0; 10970Sstevel@tonic-gate 10980Sstevel@tonic-gate if (prom_fd == -1) { 10990Sstevel@tonic-gate return (0); 11000Sstevel@tonic-gate } 11010Sstevel@tonic-gate 11020Sstevel@tonic-gate switch (node_name) { 11030Sstevel@tonic-gate case root: 11040Sstevel@tonic-gate (void *) memset(oppbuf.buf, 0, PBUFSIZE); 11050Sstevel@tonic-gate opp->oprom_size = PBUFSIZE; 11060Sstevel@tonic-gate if (ioctl(prom_fd, OPROMNEXT, opp) < 0) { 11070Sstevel@tonic-gate return (0); 11080Sstevel@tonic-gate } 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate /* 11110Sstevel@tonic-gate * Passing null string will give us the first property. 11120Sstevel@tonic-gate */ 11130Sstevel@tonic-gate (void *) memset(oppbuf.buf, 0, PBUFSIZE); 11140Sstevel@tonic-gate do { 11150Sstevel@tonic-gate opp->oprom_size = PBUFSIZE; 11160Sstevel@tonic-gate if (ioctl(prom_fd, OPROMNXTPROP, opp) < 0) { 11170Sstevel@tonic-gate return (0); 11180Sstevel@tonic-gate } 11190Sstevel@tonic-gate if (strcmp(opp->oprom_array, property_name) == 0) { 11200Sstevel@tonic-gate got_it++; 11210Sstevel@tonic-gate break; 11220Sstevel@tonic-gate } 11230Sstevel@tonic-gate } while (opp->oprom_size > 0); 11240Sstevel@tonic-gate 11250Sstevel@tonic-gate if (!got_it) { 11260Sstevel@tonic-gate return (0); 11270Sstevel@tonic-gate } 11280Sstevel@tonic-gate if (got_it && property_value == NULL) { 11290Sstevel@tonic-gate return (1); 11300Sstevel@tonic-gate } 11310Sstevel@tonic-gate opp->oprom_size = PBUFSIZE; 11320Sstevel@tonic-gate if (ioctl(prom_fd, OPROMGETPROP, opp) < 0) { 11330Sstevel@tonic-gate return (0); 11340Sstevel@tonic-gate } 11350Sstevel@tonic-gate if (opp->oprom_size == 0) { 11360Sstevel@tonic-gate *property_value = '\0'; 11370Sstevel@tonic-gate } else { 11380Sstevel@tonic-gate estrcpy(property_value, opp->oprom_array, len); 11390Sstevel@tonic-gate } 11400Sstevel@tonic-gate break; 11410Sstevel@tonic-gate case options: 11420Sstevel@tonic-gate estrcpy(opp->oprom_array, property_name, PBUFSIZE); 11430Sstevel@tonic-gate opp->oprom_size = PBUFSIZE; 11440Sstevel@tonic-gate if (ioctl(prom_fd, OPROMGETOPT, opp) < 0) { 11450Sstevel@tonic-gate return (0); 11460Sstevel@tonic-gate } 11470Sstevel@tonic-gate if (opp->oprom_size == 0) { 11480Sstevel@tonic-gate return (0); 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate if (property_value != NULL) { 11510Sstevel@tonic-gate estrcpy(property_value, opp->oprom_array, len); 11520Sstevel@tonic-gate } 11530Sstevel@tonic-gate break; 11540Sstevel@tonic-gate default: 11550Sstevel@tonic-gate logerror("Only root node and options node are supported.\n"); 11560Sstevel@tonic-gate return (0); 11570Sstevel@tonic-gate } 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate return (1); 11600Sstevel@tonic-gate } 11610Sstevel@tonic-gate 11620Sstevel@tonic-gate #define isspace(ch) ((ch) == ' ' || (ch) == '\t') 11630Sstevel@tonic-gate #define iseol(ch) ((ch) == '\n' || (ch) == '\r' || (ch) == '\f') 11640Sstevel@tonic-gate 11650Sstevel@tonic-gate /*ARGSUSED*/ 11660Sstevel@tonic-gate static void 11670Sstevel@tonic-gate power_button_monitor(void *arg) 11680Sstevel@tonic-gate { 11690Sstevel@tonic-gate struct pollfd pfd; 11700Sstevel@tonic-gate int events; 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate if (ioctl(pb_fd, PB_BEGIN_MONITOR, NULL) == -1) { 11730Sstevel@tonic-gate logerror("Failed to monitor the power button."); 11740Sstevel@tonic-gate thr_exit((void *) 0); 11750Sstevel@tonic-gate } 11760Sstevel@tonic-gate 11770Sstevel@tonic-gate pfd.fd = pb_fd; 11780Sstevel@tonic-gate pfd.events = POLLIN; 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate /*CONSTCOND*/ 11810Sstevel@tonic-gate while (1) { 11820Sstevel@tonic-gate if (poll(&pfd, 1, INFTIM) == -1) { 11830Sstevel@tonic-gate logerror("Failed to poll for power button events."); 11840Sstevel@tonic-gate thr_exit((void *) 0); 11850Sstevel@tonic-gate } 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate if (!(pfd.revents & POLLIN)) 11880Sstevel@tonic-gate continue; 11890Sstevel@tonic-gate 11900Sstevel@tonic-gate if (ioctl(pfd.fd, PB_GET_EVENTS, &events) == -1) { 11910Sstevel@tonic-gate logerror("Failed to get power button events."); 11920Sstevel@tonic-gate thr_exit((void *) 0); 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate 11950Sstevel@tonic-gate if ((events & PB_BUTTON_PRESS) && 11960Sstevel@tonic-gate (poweroff(NULL, power_button_cmd) != 0)) { 11970Sstevel@tonic-gate logerror("Power button is pressed, powering " 11980Sstevel@tonic-gate "down the system!"); 11990Sstevel@tonic-gate 12000Sstevel@tonic-gate /* 12010Sstevel@tonic-gate * Send SIGPWR signal to the init process to 12020Sstevel@tonic-gate * shut down the system. 12030Sstevel@tonic-gate */ 12040Sstevel@tonic-gate if (kill(1, SIGPWR) == -1) 12050Sstevel@tonic-gate (void) uadmin(A_SHUTDOWN, AD_POWEROFF, 0); 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate /* 12090Sstevel@tonic-gate * Clear any power button event that has happened 12100Sstevel@tonic-gate * meanwhile we were busy processing the last one. 12110Sstevel@tonic-gate */ 12120Sstevel@tonic-gate if (ioctl(pfd.fd, PB_GET_EVENTS, &events) == -1) { 12130Sstevel@tonic-gate logerror("Failed to get power button events."); 12140Sstevel@tonic-gate thr_exit((void *) 0); 12150Sstevel@tonic-gate } 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate static void 12200Sstevel@tonic-gate do_attach(void) 12210Sstevel@tonic-gate { 12220Sstevel@tonic-gate if (read_cpr_config() < 0) 12230Sstevel@tonic-gate return; 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate /* 12260Sstevel@tonic-gate * If autopm behavior is explicitly enabled for energystar-v2, or 12270Sstevel@tonic-gate * set to default for energystar-v3, create a new thread to attach 12280Sstevel@tonic-gate * all devices. 12290Sstevel@tonic-gate */ 12300Sstevel@tonic-gate estar_v3_prop = asinfo.is_autopm_default; 12310Sstevel@tonic-gate if ((strcmp(asinfo.apm_behavior, "enable") == 0) || 12320Sstevel@tonic-gate (estar_v3_prop && strcmp(asinfo.apm_behavior, "default") == 0)) { 1233*5295Srandyf if (powerd_debug) 1234*5295Srandyf logerror("powerd starting device attach thread."); 12350Sstevel@tonic-gate if (thr_create(NULL, NULL, attach_devices, NULL, 12360Sstevel@tonic-gate THR_DAEMON, NULL) != 0) { 12370Sstevel@tonic-gate logerror("Unable to create thread to attach devices."); 12380Sstevel@tonic-gate } 12390Sstevel@tonic-gate } 12400Sstevel@tonic-gate } 12410Sstevel@tonic-gate 12420Sstevel@tonic-gate /*ARGSUSED*/ 12430Sstevel@tonic-gate static void * 12440Sstevel@tonic-gate attach_devices(void *arg) 12450Sstevel@tonic-gate { 12460Sstevel@tonic-gate di_node_t root_node; 12470Sstevel@tonic-gate 12480Sstevel@tonic-gate sleep(60); /* let booting finish first */ 12490Sstevel@tonic-gate 12500Sstevel@tonic-gate if ((root_node = di_init("/", DINFOFORCE)) == DI_NODE_NIL) { 12510Sstevel@tonic-gate logerror("Failed to attach devices."); 12520Sstevel@tonic-gate return (NULL); 12530Sstevel@tonic-gate } 12540Sstevel@tonic-gate di_fini(root_node); 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate /* 12570Sstevel@tonic-gate * Unload all the modules. 12580Sstevel@tonic-gate */ 12590Sstevel@tonic-gate (void) modctl(MODUNLOAD, 0); 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate return (NULL); 12620Sstevel@tonic-gate } 12630Sstevel@tonic-gate 12640Sstevel@tonic-gate 12650Sstevel@tonic-gate /* 12660Sstevel@tonic-gate * Create a file which will contain our pid. Pmconfig will check this file 12670Sstevel@tonic-gate * to see if we are running and can use the pid to signal us. Returns the 12680Sstevel@tonic-gate * file descriptor if successful, -1 otherwise. 12690Sstevel@tonic-gate * 12700Sstevel@tonic-gate * Note: Deal with attempt to launch multiple instances and also with existence 12710Sstevel@tonic-gate * of an obsolete pid file caused by an earlier abort. 12720Sstevel@tonic-gate */ 12730Sstevel@tonic-gate static int 12740Sstevel@tonic-gate open_pidfile(char *me) 12750Sstevel@tonic-gate { 12760Sstevel@tonic-gate int fd; 12773028Smh27603 const char *e1 = "%s: Cannot open pid file for read: "; 12783028Smh27603 const char *e2 = "%s: Cannot unlink obsolete pid file: "; 12793028Smh27603 const char *e3 = "%s: Cannot open /proc for pid %ld: "; 12803028Smh27603 const char *e4 = "%s: Cannot read /proc for pid %ld: "; 12813028Smh27603 const char *e5 = "%s: Another instance (pid %ld) is trying to exit" 1282*5295Srandyf "and may be hung. Please contact sysadmin.\n"; 12833028Smh27603 const char *e6 = "%s: Another daemon is running\n"; 12843028Smh27603 const char *e7 = "%s: Cannot create pid file: "; 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate again: 12870Sstevel@tonic-gate if ((fd = open(pidpath, O_CREAT | O_EXCL | O_WRONLY, 0444)) == -1) { 12880Sstevel@tonic-gate if (errno == EEXIST) { 12890Sstevel@tonic-gate FILE *fp; 12900Sstevel@tonic-gate int ps_fd; 12910Sstevel@tonic-gate pid_t pid; 12920Sstevel@tonic-gate psinfo_t ps_info; 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate if ((fp = fopen(pidpath, "r")) == NULL) { 12950Sstevel@tonic-gate (void) fprintf(stderr, e1, me); 12960Sstevel@tonic-gate perror(NULL); 12970Sstevel@tonic-gate return (-1); 12980Sstevel@tonic-gate } 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate /* Read the pid */ 13010Sstevel@tonic-gate pid = (pid_t)-1; 13020Sstevel@tonic-gate (void) fscanf(fp, "%ld", &pid); 13030Sstevel@tonic-gate (void) fclose(fp); 13040Sstevel@tonic-gate if (pid == -1) { 13050Sstevel@tonic-gate if (unlink(pidpath) == -1) { 13060Sstevel@tonic-gate (void) fprintf(stderr, e2, me); 13070Sstevel@tonic-gate perror(NULL); 13080Sstevel@tonic-gate return (-1); 13090Sstevel@tonic-gate } else /* try without corrupted file */ 13100Sstevel@tonic-gate goto again; 13110Sstevel@tonic-gate } 13120Sstevel@tonic-gate 13130Sstevel@tonic-gate /* Is pid for a running process? */ 13140Sstevel@tonic-gate (void) sprintf(scratch, "/proc/%ld/psinfo", pid); 13150Sstevel@tonic-gate ps_fd = open(scratch, O_RDONLY | O_NDELAY); 13160Sstevel@tonic-gate if (ps_fd == -1) { 13170Sstevel@tonic-gate if (errno == ENOENT) { 13180Sstevel@tonic-gate if (unlink(pidpath) == -1) { 13190Sstevel@tonic-gate (void) fprintf(stderr, e2, me); 13200Sstevel@tonic-gate perror(NULL); 13210Sstevel@tonic-gate return (-1); 13220Sstevel@tonic-gate } else /* try without obsolete file */ 13230Sstevel@tonic-gate goto again; 13240Sstevel@tonic-gate } 13250Sstevel@tonic-gate (void) fprintf(stderr, e3, me, pid); 13260Sstevel@tonic-gate return (-1); 13270Sstevel@tonic-gate } 13280Sstevel@tonic-gate if (read(ps_fd, &ps_info, 13290Sstevel@tonic-gate sizeof (ps_info)) != sizeof (ps_info)) { 13300Sstevel@tonic-gate (void) fprintf(stderr, e4, me, pid); 13310Sstevel@tonic-gate perror(NULL); 13320Sstevel@tonic-gate (void) close(ps_fd); 13330Sstevel@tonic-gate return (-1); 13340Sstevel@tonic-gate } 13350Sstevel@tonic-gate (void) close(ps_fd); 13360Sstevel@tonic-gate if (ps_info.pr_nlwp == 0) { /* defunct process */ 13370Sstevel@tonic-gate (void) fprintf(stderr, e5, me, pid); 13380Sstevel@tonic-gate return (-1); 13390Sstevel@tonic-gate } else { /* instance of daemon already running */ 13400Sstevel@tonic-gate (void) fprintf(stderr, e6, me); 13410Sstevel@tonic-gate return (-1); 13420Sstevel@tonic-gate } 13430Sstevel@tonic-gate } else { /* create failure not due to existing file */ 13440Sstevel@tonic-gate (void) fprintf(stderr, e7, me); 13450Sstevel@tonic-gate perror(NULL); 13460Sstevel@tonic-gate return (-1); 13470Sstevel@tonic-gate } 13480Sstevel@tonic-gate } 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate (void) fchown(fd, (uid_t)-1, (gid_t)0); 13510Sstevel@tonic-gate return (fd); 13520Sstevel@tonic-gate } 13530Sstevel@tonic-gate 13540Sstevel@tonic-gate /* 13550Sstevel@tonic-gate * Write a pid to the pid file. Report errors to syslog. 13560Sstevel@tonic-gate * 13570Sstevel@tonic-gate */ 13580Sstevel@tonic-gate static int 13590Sstevel@tonic-gate write_pidfile(int fd, pid_t pid) 13600Sstevel@tonic-gate { 13610Sstevel@tonic-gate int len; 13620Sstevel@tonic-gate int rc = 0; /* assume success */ 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate len = sprintf(scratch, "%ld\n", pid); 13650Sstevel@tonic-gate if (write(fd, scratch, len) != len) { 13660Sstevel@tonic-gate logerror("Cannot write pid file: %s", strerror(errno)); 13670Sstevel@tonic-gate rc = -1; 13680Sstevel@tonic-gate } 13690Sstevel@tonic-gate 13700Sstevel@tonic-gate return (rc); 13710Sstevel@tonic-gate } 1372