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