1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <stdio.h>			/* Standard */
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <fcntl.h>
31*0Sstevel@tonic-gate #include <sys/types.h>
32*0Sstevel@tonic-gate #include <time.h>
33*0Sstevel@tonic-gate #include <string.h>
34*0Sstevel@tonic-gate #include <errno.h>
35*0Sstevel@tonic-gate #include <pwd.h>
36*0Sstevel@tonic-gate #include <procfs.h>
37*0Sstevel@tonic-gate #include <dirent.h>
38*0Sstevel@tonic-gate #include <thread.h>
39*0Sstevel@tonic-gate #include <limits.h>
40*0Sstevel@tonic-gate #include <sys/todio.h>			/* Time-Of-Day chip */
41*0Sstevel@tonic-gate #include <sys/stat.h>
42*0Sstevel@tonic-gate #include <sys/wait.h>
43*0Sstevel@tonic-gate #include <sys/ipc.h>			/* IPC functions */
44*0Sstevel@tonic-gate #include <signal.h>			/* signal handling */
45*0Sstevel@tonic-gate #include <syslog.h>
46*0Sstevel@tonic-gate #include <unistd.h>
47*0Sstevel@tonic-gate #include <libdevinfo.h>
48*0Sstevel@tonic-gate #include <poll.h>
49*0Sstevel@tonic-gate #include <sys/pm.h>			/* power management driver */
50*0Sstevel@tonic-gate #include <sys/uadmin.h>
51*0Sstevel@tonic-gate #include <sys/openpromio.h>		/* for prom access */
52*0Sstevel@tonic-gate #include <sys/sysmacros.h>		/* for MIN & MAX macros */
53*0Sstevel@tonic-gate #include <sys/modctl.h>
54*0Sstevel@tonic-gate #include <sys/stropts.h>		/* for INFTIM */
55*0Sstevel@tonic-gate #include <sys/pbio.h>
56*0Sstevel@tonic-gate #include <sys/cpr.h>
57*0Sstevel@tonic-gate #include <stdarg.h>
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include "powerd.h"
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate /* External Functions */
62*0Sstevel@tonic-gate extern struct tm *localtime_r(const time_t *, struct tm *);
63*0Sstevel@tonic-gate extern void sysstat_init(void);
64*0Sstevel@tonic-gate extern int check_tty(hrtime_t *, int);
65*0Sstevel@tonic-gate extern int check_disks(hrtime_t *, int);
66*0Sstevel@tonic-gate extern int check_load_ave(hrtime_t *, float);
67*0Sstevel@tonic-gate extern int check_nfs(hrtime_t *, int);
68*0Sstevel@tonic-gate extern int last_disk_activity(hrtime_t *, int);
69*0Sstevel@tonic-gate extern int last_tty_activity(hrtime_t *, int);
70*0Sstevel@tonic-gate extern int last_load_ave_activity(hrtime_t *);
71*0Sstevel@tonic-gate extern int last_nfs_activity(hrtime_t *, int);
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate #define	PM		"/dev/pm"
74*0Sstevel@tonic-gate #define	TOD		"/dev/tod"
75*0Sstevel@tonic-gate #define	PROM		"/dev/openprom"
76*0Sstevel@tonic-gate #define	PB		"/dev/power_button"
77*0Sstevel@tonic-gate #define	LOGFILE		"./powerd.log"
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate #define	PBM_THREAD	0
80*0Sstevel@tonic-gate #define	ATTACH_THREAD	1
81*0Sstevel@tonic-gate #define	NUM_THREADS	2
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate #define	CHECK_INTERVAL	5
84*0Sstevel@tonic-gate #define	IDLECHK_INTERVAL	15
85*0Sstevel@tonic-gate #define	MINS_TO_SECS	60
86*0Sstevel@tonic-gate #define	HOURS_TO_SECS	(60 * 60)
87*0Sstevel@tonic-gate #define	DAYS_TO_SECS	(24 * 60 * 60)
88*0Sstevel@tonic-gate #define	HOURS_TO_MINS	60
89*0Sstevel@tonic-gate #define	DAYS_TO_MINS	(24 * 60)
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate #define	LIFETIME_SECS			(7 * 365 * DAYS_TO_SECS)
92*0Sstevel@tonic-gate #define	DEFAULT_POWER_CYCLE_LIMIT	10000
93*0Sstevel@tonic-gate #define	DEFAULT_SYSTEM_BOARD_DATE	804582000	/* July 1, 1995 */
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate #define	LLEN 80
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate typedef	enum {root, options} prom_node_t;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /* State Variables */
100*0Sstevel@tonic-gate static struct cprconfig	asinfo;
101*0Sstevel@tonic-gate static time_t		shutdown_time;	/* Time for next shutdown check */
102*0Sstevel@tonic-gate static time_t		checkidle_time;	/* Time for next idleness check */
103*0Sstevel@tonic-gate static time_t		last_resume;
104*0Sstevel@tonic-gate pwr_info_t		*info;		/* private as config data buffer */
105*0Sstevel@tonic-gate static int		pb_fd;		/* power button driver */
106*0Sstevel@tonic-gate static int		broadcast;	/* Enables syslog messages */
107*0Sstevel@tonic-gate static int		start_calc;
108*0Sstevel@tonic-gate static int		autoshutdown_en;
109*0Sstevel@tonic-gate static int		do_idlecheck;
110*0Sstevel@tonic-gate static int		got_sighup;
111*0Sstevel@tonic-gate static int		estar_v2_prop;
112*0Sstevel@tonic-gate static int		estar_v3_prop;
113*0Sstevel@tonic-gate static int		log_power_cycles_error = 0;
114*0Sstevel@tonic-gate static int		log_system_board_date_error = 0;
115*0Sstevel@tonic-gate static int		log_no_autoshutdown_warning = 0;
116*0Sstevel@tonic-gate static mutex_t		poweroff_mutex;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate static char *autoshutdown_cmd[] = {
119*0Sstevel@tonic-gate 	"/usr/openwin/bin/sys-suspend",
120*0Sstevel@tonic-gate 	"-n", "-d", ":0", NULL
121*0Sstevel@tonic-gate };
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate static char *power_button_cmd[] = {
124*0Sstevel@tonic-gate 	"/usr/openwin/bin/sys-suspend",
125*0Sstevel@tonic-gate 	"-h", "-d", ":0", NULL
126*0Sstevel@tonic-gate };
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate static char pidpath[] = PIDPATH;
129*0Sstevel@tonic-gate static char scratch[PATH_MAX];
130*0Sstevel@tonic-gate static char *prog;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate /* Local Functions */
133*0Sstevel@tonic-gate static void alarm_handler(int);
134*0Sstevel@tonic-gate static void thaw_handler(int);
135*0Sstevel@tonic-gate static void kill_handler(int);
136*0Sstevel@tonic-gate static void work_handler(int);
137*0Sstevel@tonic-gate static void check_shutdown(time_t *, hrtime_t *);
138*0Sstevel@tonic-gate static void check_idleness(time_t *, hrtime_t *);
139*0Sstevel@tonic-gate static int last_system_activity(hrtime_t *);
140*0Sstevel@tonic-gate static int run_idlecheck(void);
141*0Sstevel@tonic-gate static void set_alarm(time_t);
142*0Sstevel@tonic-gate static int poweroff(char *, char **);
143*0Sstevel@tonic-gate static int is_ok2shutdown(time_t *);
144*0Sstevel@tonic-gate static int get_prom(int, prom_node_t, char *, char *, size_t);
145*0Sstevel@tonic-gate static void power_button_monitor(void *);
146*0Sstevel@tonic-gate static int open_pidfile(char *);
147*0Sstevel@tonic-gate static int write_pidfile(int, pid_t);
148*0Sstevel@tonic-gate static int read_cpr_config(void);
149*0Sstevel@tonic-gate static void system_activity_monitor(void);
150*0Sstevel@tonic-gate #ifdef sparc
151*0Sstevel@tonic-gate static void do_attach(void);
152*0Sstevel@tonic-gate static void *attach_devices(void *);
153*0Sstevel@tonic-gate #endif
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate /* VARARGS0 */
157*0Sstevel@tonic-gate static void
158*0Sstevel@tonic-gate logerror(char *fmt, ...)
159*0Sstevel@tonic-gate {
160*0Sstevel@tonic-gate 	va_list args;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	va_start(args, fmt);
163*0Sstevel@tonic-gate 	if (broadcast)
164*0Sstevel@tonic-gate 		vsyslog(LOG_ERR, fmt, args);
165*0Sstevel@tonic-gate 	va_end(args);
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate static void
170*0Sstevel@tonic-gate estrcpy(char *dst, char *src, size_t dlen)
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate 	size_t slen;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	slen = strlcpy(dst, src, dlen);
175*0Sstevel@tonic-gate 	if (slen >= dlen) {
176*0Sstevel@tonic-gate 		logerror("%s: string too long \"%s ...\"\n"
177*0Sstevel@tonic-gate 		    "(len %d, max %d)\n", prog, dst, slen, dlen - 1);
178*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate int
184*0Sstevel@tonic-gate main(int argc, char *argv[])
185*0Sstevel@tonic-gate {
186*0Sstevel@tonic-gate 	pid_t		pid;
187*0Sstevel@tonic-gate 	int		pm_fd;
188*0Sstevel@tonic-gate 	struct sigaction act;
189*0Sstevel@tonic-gate 	sigset_t	sigmask;
190*0Sstevel@tonic-gate 	int		c;
191*0Sstevel@tonic-gate 	char		errmsg[PATH_MAX + 64];
192*0Sstevel@tonic-gate 	int		pid_fd;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	prog = argv[0];
195*0Sstevel@tonic-gate 	if (geteuid() != 0) {
196*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: Must be root\n", prog);
197*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
198*0Sstevel@tonic-gate 	}
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	if ((pid_fd = open_pidfile(prog)) ==  -1)
201*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	/*
204*0Sstevel@tonic-gate 	 * Process options
205*0Sstevel@tonic-gate 	 */
206*0Sstevel@tonic-gate 	broadcast = 1;
207*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "n")) != EOF) {
208*0Sstevel@tonic-gate 		switch (c) {
209*0Sstevel@tonic-gate 		case 'n':
210*0Sstevel@tonic-gate 			broadcast = 0;
211*0Sstevel@tonic-gate 			break;
212*0Sstevel@tonic-gate 		case '?':
213*0Sstevel@tonic-gate 			(void) fprintf(stderr, "Usage: %s [-n]\n", prog);
214*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
215*0Sstevel@tonic-gate 		}
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	pm_fd = open(PM, O_RDWR);
219*0Sstevel@tonic-gate 	if (pm_fd == -1) {
220*0Sstevel@tonic-gate 		(void) sprintf(errmsg, "%s: %s", prog, PM);
221*0Sstevel@tonic-gate 		perror(errmsg);
222*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
223*0Sstevel@tonic-gate 	}
224*0Sstevel@tonic-gate 	(void) close(pm_fd);
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	/*
227*0Sstevel@tonic-gate 	 * Initialize mutex lock used to insure only one command to
228*0Sstevel@tonic-gate 	 * run at a time.
229*0Sstevel@tonic-gate 	 */
230*0Sstevel@tonic-gate 	if (mutex_init(&poweroff_mutex, USYNC_THREAD, NULL) != 0) {
231*0Sstevel@tonic-gate 		(void) fprintf(stderr,
232*0Sstevel@tonic-gate 			"%s: Unable to initialize mutex lock\n", prog);
233*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	if ((info = (pwr_info_t *)malloc(sizeof (pwr_info_t))) == NULL) {
237*0Sstevel@tonic-gate 		(void) sprintf(errmsg, "%s: malloc", prog);
238*0Sstevel@tonic-gate 		perror(errmsg);
239*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
240*0Sstevel@tonic-gate 	}
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	/*
243*0Sstevel@tonic-gate 	 * Daemon is set to go...
244*0Sstevel@tonic-gate 	 */
245*0Sstevel@tonic-gate 	if ((pid = fork()) < 0)
246*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
247*0Sstevel@tonic-gate 	else if (pid != 0)
248*0Sstevel@tonic-gate 		exit(EXIT_SUCCESS);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	pid = getpid();
251*0Sstevel@tonic-gate 	openlog(prog, 0, LOG_DAEMON);
252*0Sstevel@tonic-gate 	if (write_pidfile(pid_fd, pid) == -1)	/* logs errors on failure */
253*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
254*0Sstevel@tonic-gate 	(void) close(pid_fd);
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	/*
257*0Sstevel@tonic-gate 	 * Close all the parent's file descriptors (Bug 1225843).
258*0Sstevel@tonic-gate 	 */
259*0Sstevel@tonic-gate 	closefrom(0);
260*0Sstevel@tonic-gate 	(void) setsid();
261*0Sstevel@tonic-gate 	(void) chdir("/");
262*0Sstevel@tonic-gate 	(void) umask(0);
263*0Sstevel@tonic-gate #ifdef DEBUG
264*0Sstevel@tonic-gate 	/*
265*0Sstevel@tonic-gate 	 * Connect stdout to the console.
266*0Sstevel@tonic-gate 	 */
267*0Sstevel@tonic-gate 	if (dup2(open("/dev/console", O_WRONLY|O_NOCTTY), 1) == -1) {
268*0Sstevel@tonic-gate 		logerror("Unable to connect to the console.");
269*0Sstevel@tonic-gate 	}
270*0Sstevel@tonic-gate #endif
271*0Sstevel@tonic-gate 	info->pd_flags = PD_AC;
272*0Sstevel@tonic-gate 	info->pd_idle_time = -1;
273*0Sstevel@tonic-gate 	info->pd_start_time = 0;
274*0Sstevel@tonic-gate 	info->pd_finish_time = 0;
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	/*
277*0Sstevel@tonic-gate 	 * Allow SIGQUIT, SIGINT and SIGTERM signals to terminate us
278*0Sstevel@tonic-gate 	 * any time
279*0Sstevel@tonic-gate 	 */
280*0Sstevel@tonic-gate 	act.sa_handler = kill_handler;
281*0Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
282*0Sstevel@tonic-gate 	act.sa_flags = 0;
283*0Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &act, NULL);
284*0Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
285*0Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	(void) sigfillset(&sigmask);
288*0Sstevel@tonic-gate 	(void) sigdelset(&sigmask, SIGQUIT);
289*0Sstevel@tonic-gate 	(void) sigdelset(&sigmask, SIGINT);
290*0Sstevel@tonic-gate 	(void) sigdelset(&sigmask, SIGTERM);
291*0Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_SETMASK, &sigmask, NULL);
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	/*
294*0Sstevel@tonic-gate 	 * If "power_button" device node can be opened, create a new
295*0Sstevel@tonic-gate 	 * thread to monitor the power button.
296*0Sstevel@tonic-gate 	 */
297*0Sstevel@tonic-gate 	if ((pb_fd = open(PB, O_RDONLY)) != -1) {
298*0Sstevel@tonic-gate 		if (thr_create(NULL, NULL,
299*0Sstevel@tonic-gate 		    (void *(*)(void *))power_button_monitor, NULL,
300*0Sstevel@tonic-gate 		    THR_DAEMON, NULL) != 0) {
301*0Sstevel@tonic-gate 			logerror("Unable to monitor system's power button.");
302*0Sstevel@tonic-gate 		}
303*0Sstevel@tonic-gate 	}
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate #ifdef sparc
306*0Sstevel@tonic-gate 	do_attach();
307*0Sstevel@tonic-gate #endif
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	/*
310*0Sstevel@tonic-gate 	 * Create a new thread to monitor system activity and suspend
311*0Sstevel@tonic-gate 	 * system if idle.
312*0Sstevel@tonic-gate 	 */
313*0Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
314*0Sstevel@tonic-gate 	    (void *(*)(void *))system_activity_monitor, NULL,
315*0Sstevel@tonic-gate 	    THR_DAEMON, NULL) != 0) {
316*0Sstevel@tonic-gate 		logerror("Unable to create thread to monitor system activity.");
317*0Sstevel@tonic-gate 	}
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	/*
320*0Sstevel@tonic-gate 	 * Block until we receive an explicit terminate signal
321*0Sstevel@tonic-gate 	 */
322*0Sstevel@tonic-gate 	(void) sigsuspend(&sigmask);
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	return (1);
325*0Sstevel@tonic-gate }
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate static void
328*0Sstevel@tonic-gate system_activity_monitor(void)
329*0Sstevel@tonic-gate {
330*0Sstevel@tonic-gate 	struct sigaction act;
331*0Sstevel@tonic-gate 	sigset_t sigmask;
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	/*
334*0Sstevel@tonic-gate 	 * Setup for gathering system's statistic.
335*0Sstevel@tonic-gate 	 */
336*0Sstevel@tonic-gate 	sysstat_init();
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	/*
339*0Sstevel@tonic-gate 	 * In addition to the SIGQUIT, SIGINT and SIGTERM signals already
340*0Sstevel@tonic-gate 	 * being handled, this thread also needs to handle SIGHUP, SIGALRM
341*0Sstevel@tonic-gate 	 * and SIGTHAW signals.
342*0Sstevel@tonic-gate 	 */
343*0Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
344*0Sstevel@tonic-gate 	act.sa_flags = 0;
345*0Sstevel@tonic-gate 	act.sa_handler = alarm_handler;
346*0Sstevel@tonic-gate 	(void) sigaction(SIGALRM, &act, NULL);
347*0Sstevel@tonic-gate 	act.sa_handler = work_handler;
348*0Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &act, NULL);
349*0Sstevel@tonic-gate 	act.sa_handler = thaw_handler;
350*0Sstevel@tonic-gate 	(void) sigaction(SIGTHAW, &act, NULL);
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	/*
353*0Sstevel@tonic-gate 	 * Invoke work_handler with a dummy SIGHUP signal to read
354*0Sstevel@tonic-gate 	 * cpr config file, get autoshutdown properties and schedule
355*0Sstevel@tonic-gate 	 * an alarm if needed.
356*0Sstevel@tonic-gate 	 */
357*0Sstevel@tonic-gate 	work_handler(SIGHUP);
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	/*
360*0Sstevel@tonic-gate 	 * Wait for signal to read file
361*0Sstevel@tonic-gate 	 */
362*0Sstevel@tonic-gate 	(void) thr_sigsetmask(0, 0, &sigmask);
363*0Sstevel@tonic-gate 	(void) sigdelset(&sigmask, SIGHUP);
364*0Sstevel@tonic-gate 	(void) sigdelset(&sigmask, SIGALRM);
365*0Sstevel@tonic-gate 	(void) sigdelset(&sigmask, SIGTHAW);
366*0Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_SETMASK, &sigmask, NULL);
367*0Sstevel@tonic-gate 	do {
368*0Sstevel@tonic-gate 		(void) sigsuspend(&sigmask);
369*0Sstevel@tonic-gate 	} while (errno == EINTR);
370*0Sstevel@tonic-gate }
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate static int
373*0Sstevel@tonic-gate read_cpr_config(void)
374*0Sstevel@tonic-gate {
375*0Sstevel@tonic-gate 	int	asfd;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	if ((asfd = open(CPR_CONFIG, O_RDONLY)) < 0) {
378*0Sstevel@tonic-gate 		logerror("Unable to open CPR config file '%s'", CPR_CONFIG);
379*0Sstevel@tonic-gate 		return (-1);
380*0Sstevel@tonic-gate 	}
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	if (read(asfd, (void *)&asinfo, sizeof (asinfo)) != sizeof (asinfo)) {
383*0Sstevel@tonic-gate 		logerror("Unable to read CPR config file '%s'", CPR_CONFIG);
384*0Sstevel@tonic-gate 		close(asfd);
385*0Sstevel@tonic-gate 		return (-1);
386*0Sstevel@tonic-gate 	}
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	(void) close(asfd);
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	return (0);
391*0Sstevel@tonic-gate }
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate /*ARGSUSED*/
394*0Sstevel@tonic-gate static void
395*0Sstevel@tonic-gate thaw_handler(int sig)
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate 	start_calc  = 0;
398*0Sstevel@tonic-gate 	last_resume = time(NULL);
399*0Sstevel@tonic-gate }
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate /*ARGSUSED*/
402*0Sstevel@tonic-gate static void
403*0Sstevel@tonic-gate kill_handler(int sig)
404*0Sstevel@tonic-gate {
405*0Sstevel@tonic-gate 	int ret_code = EXIT_SUCCESS;
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	/*
408*0Sstevel@tonic-gate 	 * Free resources
409*0Sstevel@tonic-gate 	 */
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	free(info);
412*0Sstevel@tonic-gate 	if (pb_fd != -1) {
413*0Sstevel@tonic-gate 		(void) close(pb_fd);
414*0Sstevel@tonic-gate 	}
415*0Sstevel@tonic-gate 	(void) mutex_destroy(&poweroff_mutex);
416*0Sstevel@tonic-gate 	(void) unlink(pidpath);
417*0Sstevel@tonic-gate 	closelog();
418*0Sstevel@tonic-gate 	exit(ret_code);
419*0Sstevel@tonic-gate }
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate /*ARGSUSED*/
422*0Sstevel@tonic-gate static void
423*0Sstevel@tonic-gate alarm_handler(int sig)
424*0Sstevel@tonic-gate {
425*0Sstevel@tonic-gate 	time_t		now;
426*0Sstevel@tonic-gate 	hrtime_t	hr_now;
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 	now = time(NULL);
429*0Sstevel@tonic-gate 	hr_now = gethrtime();
430*0Sstevel@tonic-gate 	if (checkidle_time <= now && checkidle_time != 0)
431*0Sstevel@tonic-gate 		check_idleness(&now, &hr_now);
432*0Sstevel@tonic-gate 	if (shutdown_time <= now && shutdown_time != 0)
433*0Sstevel@tonic-gate 		check_shutdown(&now, &hr_now);
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	set_alarm(now);
436*0Sstevel@tonic-gate }
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate /*ARGSUSED*/
439*0Sstevel@tonic-gate static void
440*0Sstevel@tonic-gate work_handler(int sig)
441*0Sstevel@tonic-gate {
442*0Sstevel@tonic-gate 	time_t		now;
443*0Sstevel@tonic-gate 	hrtime_t	hr_now;
444*0Sstevel@tonic-gate 	struct stat	stat_buf;
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 	do_idlecheck = 0;
447*0Sstevel@tonic-gate 	info->pd_flags = PD_AC;
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	/*
450*0Sstevel@tonic-gate 	 * Parse the config file for autoshutdown and idleness entries.
451*0Sstevel@tonic-gate 	 */
452*0Sstevel@tonic-gate 	if (read_cpr_config() < 0)
453*0Sstevel@tonic-gate 		return;
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	/*
456*0Sstevel@tonic-gate 	 * Since Oct. 1, 1995, any new system shipped had root
457*0Sstevel@tonic-gate 	 * property "energystar-v2" defined in its prom.  Systems
458*0Sstevel@tonic-gate 	 * shipped after July 1, 1999, will have "energystar-v3"
459*0Sstevel@tonic-gate 	 * property.
460*0Sstevel@tonic-gate 	 */
461*0Sstevel@tonic-gate 	estar_v2_prop = asinfo.is_cpr_default;
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	info->pd_flags |= asinfo.is_autowakeup_capable;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	if (strlen(asinfo.idlecheck_path) > 0) {
466*0Sstevel@tonic-gate 		if (stat(asinfo.idlecheck_path, &stat_buf) != 0) {
467*0Sstevel@tonic-gate 			logerror("unable to access idlecheck program \"%s\".",
468*0Sstevel@tonic-gate 			    asinfo.idlecheck_path);
469*0Sstevel@tonic-gate 		} else if (!(stat_buf.st_mode & S_IXUSR)) {
470*0Sstevel@tonic-gate 			logerror("idlecheck program \"%s\" is not executable.",
471*0Sstevel@tonic-gate 			    asinfo.idlecheck_path);
472*0Sstevel@tonic-gate 		} else {
473*0Sstevel@tonic-gate 			do_idlecheck = 1;
474*0Sstevel@tonic-gate 		}
475*0Sstevel@tonic-gate 	}
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	if (strlen(asinfo.as_behavior) == 0 ||
478*0Sstevel@tonic-gate 	    strcmp(asinfo.as_behavior, "noshutdown") == 0 ||
479*0Sstevel@tonic-gate 	    strcmp(asinfo.as_behavior, "unconfigured") == 0) {
480*0Sstevel@tonic-gate 		info->pd_autoshutdown = 0;
481*0Sstevel@tonic-gate 	} else if (strcmp(asinfo.as_behavior, "default") == 0) {
482*0Sstevel@tonic-gate 		info->pd_autoshutdown = estar_v2_prop;
483*0Sstevel@tonic-gate 	} else if (strcmp(asinfo.as_behavior, "shutdown") == 0 ||
484*0Sstevel@tonic-gate 		strcmp(asinfo.as_behavior, "autowakeup") == 0) {
485*0Sstevel@tonic-gate 		info->pd_autoshutdown = asinfo.is_cpr_capable;
486*0Sstevel@tonic-gate 	} else {
487*0Sstevel@tonic-gate 		logerror("autoshutdown behavior \"%s\" unrecognized.",
488*0Sstevel@tonic-gate 		    asinfo.as_behavior);
489*0Sstevel@tonic-gate 		info->pd_autoshutdown = 0;
490*0Sstevel@tonic-gate 	}
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 	if (info->pd_autoshutdown) {
493*0Sstevel@tonic-gate 		info->pd_idle_time = asinfo.as_idle;
494*0Sstevel@tonic-gate 		info->pd_start_time =
495*0Sstevel@tonic-gate 		    (asinfo.as_sh * 60 + asinfo.as_sm) % DAYS_TO_MINS;
496*0Sstevel@tonic-gate 		info->pd_finish_time =
497*0Sstevel@tonic-gate 		    (asinfo.as_fh * 60 + asinfo.as_fm) % DAYS_TO_MINS;
498*0Sstevel@tonic-gate 		info->pd_autoresume =
499*0Sstevel@tonic-gate 		    (strcmp(asinfo.as_behavior, "autowakeup") == 0) ? 1 : 0;
500*0Sstevel@tonic-gate 	}
501*0Sstevel@tonic-gate 	autoshutdown_en = (asinfo.as_idle >= 0 && info->pd_autoshutdown)
502*0Sstevel@tonic-gate 		? 1 : 0;
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate #ifdef DEBUG
505*0Sstevel@tonic-gate 	(void) fprintf(stderr, "autoshutdown_en = %d, as_idle = %d, "
506*0Sstevel@tonic-gate 			"pd_autoresume = %d\n",
507*0Sstevel@tonic-gate 		autoshutdown_en, asinfo.as_idle, info->pd_autoresume);
508*0Sstevel@tonic-gate 	(void) fprintf(stderr, " pd_start_time=%d, pd_finish_time=%d\n",
509*0Sstevel@tonic-gate 		info->pd_start_time, info->pd_finish_time);
510*0Sstevel@tonic-gate #endif
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 	got_sighup = 1;
513*0Sstevel@tonic-gate 	now = last_resume = time(NULL);
514*0Sstevel@tonic-gate 	hr_now = gethrtime();
515*0Sstevel@tonic-gate 	check_idleness(&now, &hr_now);
516*0Sstevel@tonic-gate 	check_shutdown(&now, &hr_now);
517*0Sstevel@tonic-gate 	set_alarm(now);
518*0Sstevel@tonic-gate }
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate static void
521*0Sstevel@tonic-gate check_shutdown(time_t *now, hrtime_t *hr_now)
522*0Sstevel@tonic-gate {
523*0Sstevel@tonic-gate 	int		tod_fd = -1;
524*0Sstevel@tonic-gate 	int		kbd, mouse, system, least_idle, idlecheck_time;
525*0Sstevel@tonic-gate 	int		next_time;
526*0Sstevel@tonic-gate 	int		s, f;
527*0Sstevel@tonic-gate 	struct tm	tmp_time;
528*0Sstevel@tonic-gate 	time_t		start_of_day, time_since_last_resume;
529*0Sstevel@tonic-gate 	time_t		wakeup_time;
530*0Sstevel@tonic-gate 	extern long	conskbd_idle_time(void);
531*0Sstevel@tonic-gate 	extern long	consms_idle_time(void);
532*0Sstevel@tonic-gate 	static int	warned_kbd, warned_ms; /* print error msg one time */
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	if (!autoshutdown_en) {
535*0Sstevel@tonic-gate 		shutdown_time = 0;
536*0Sstevel@tonic-gate 		return;
537*0Sstevel@tonic-gate 	}
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate 	(void) localtime_r(now, &tmp_time);
540*0Sstevel@tonic-gate 	tmp_time.tm_sec = 0;
541*0Sstevel@tonic-gate 	tmp_time.tm_min = 0;
542*0Sstevel@tonic-gate 	tmp_time.tm_hour = 0;
543*0Sstevel@tonic-gate 	start_of_day = mktime(&tmp_time);
544*0Sstevel@tonic-gate 	s = start_of_day + info->pd_start_time * 60;
545*0Sstevel@tonic-gate 	f = start_of_day + info->pd_finish_time * 60;
546*0Sstevel@tonic-gate 	if ((s < f && *now >= s && *now < f) ||
547*0Sstevel@tonic-gate 	    (s >= f && (*now < f || *now >= s))) {
548*0Sstevel@tonic-gate 		if ((mouse = (int)consms_idle_time()) < 0) {
549*0Sstevel@tonic-gate 			if (! warned_ms) {
550*0Sstevel@tonic-gate 				warned_ms = 1;
551*0Sstevel@tonic-gate 				logerror("powerd: failed to get "
552*0Sstevel@tonic-gate 				    "idle time for console mouse");
553*0Sstevel@tonic-gate 			}
554*0Sstevel@tonic-gate 			return;
555*0Sstevel@tonic-gate 		}
556*0Sstevel@tonic-gate 		if ((kbd = (int)conskbd_idle_time()) < 0) {
557*0Sstevel@tonic-gate 			if (! warned_kbd) {
558*0Sstevel@tonic-gate 				warned_kbd = 1;
559*0Sstevel@tonic-gate 				logerror("powerd: failed to get "
560*0Sstevel@tonic-gate 				    "idle time for console keyboard");
561*0Sstevel@tonic-gate 			}
562*0Sstevel@tonic-gate 			return;
563*0Sstevel@tonic-gate 		}
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 		system = last_system_activity(hr_now);
566*0Sstevel@tonic-gate 		/* who is the last to go idle */
567*0Sstevel@tonic-gate 		least_idle = MIN(system, MIN(kbd, mouse));
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 		/*
570*0Sstevel@tonic-gate 		 * Calculate time_since_last_resume and the next_time
571*0Sstevel@tonic-gate 		 * to auto suspend.
572*0Sstevel@tonic-gate 		 */
573*0Sstevel@tonic-gate 		start_calc = 1;
574*0Sstevel@tonic-gate 		time_since_last_resume = time(NULL) - last_resume;
575*0Sstevel@tonic-gate 		next_time = info->pd_idle_time * 60 -
576*0Sstevel@tonic-gate 				MIN(least_idle, time_since_last_resume);
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate #ifdef DEBUG
579*0Sstevel@tonic-gate 		fprintf(stderr, " check_shutdown: next_time=%d\n",
580*0Sstevel@tonic-gate 			next_time);
581*0Sstevel@tonic-gate #endif
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 		/*
584*0Sstevel@tonic-gate 		 * If we have get the SIGTHAW signal at this point - our
585*0Sstevel@tonic-gate 		 * calculation of time_since_last_resume is wrong  so
586*0Sstevel@tonic-gate 		 * - we need to recalculate.
587*0Sstevel@tonic-gate 		 */
588*0Sstevel@tonic-gate 		while (start_calc == 0) {
589*0Sstevel@tonic-gate 			/* need to redo calculation */
590*0Sstevel@tonic-gate 			start_calc = 1;
591*0Sstevel@tonic-gate 			time_since_last_resume = time(NULL) - last_resume;
592*0Sstevel@tonic-gate 			next_time = info->pd_idle_time * 60 -
593*0Sstevel@tonic-gate 				MIN(least_idle, time_since_last_resume);
594*0Sstevel@tonic-gate 		}
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 		/*
597*0Sstevel@tonic-gate 		 * Only when everything else is idle, run the user's idlecheck
598*0Sstevel@tonic-gate 		 * script.
599*0Sstevel@tonic-gate 		 */
600*0Sstevel@tonic-gate 		if (next_time <= 0 && do_idlecheck) {
601*0Sstevel@tonic-gate 			got_sighup = 0;
602*0Sstevel@tonic-gate 			idlecheck_time = run_idlecheck();
603*0Sstevel@tonic-gate 			next_time = info->pd_idle_time * 60 -
604*0Sstevel@tonic-gate 				MIN(idlecheck_time, MIN(least_idle,
605*0Sstevel@tonic-gate 				time_since_last_resume));
606*0Sstevel@tonic-gate 			/*
607*0Sstevel@tonic-gate 			 * If we have caught SIGTHAW or SIGHUP, need to
608*0Sstevel@tonic-gate 			 * recalculate.
609*0Sstevel@tonic-gate 			 */
610*0Sstevel@tonic-gate 			while (start_calc == 0 || got_sighup == 1) {
611*0Sstevel@tonic-gate 				start_calc = 1;
612*0Sstevel@tonic-gate 				got_sighup = 0;
613*0Sstevel@tonic-gate 				idlecheck_time = run_idlecheck();
614*0Sstevel@tonic-gate 				time_since_last_resume = time(NULL) -
615*0Sstevel@tonic-gate 					last_resume;
616*0Sstevel@tonic-gate 				next_time = info->pd_idle_time * 60 -
617*0Sstevel@tonic-gate 					MIN(idlecheck_time, MIN(least_idle,
618*0Sstevel@tonic-gate 					time_since_last_resume));
619*0Sstevel@tonic-gate 			}
620*0Sstevel@tonic-gate 		}
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 		if (next_time <= 0) {
623*0Sstevel@tonic-gate 			if (is_ok2shutdown(now)) {
624*0Sstevel@tonic-gate 				/*
625*0Sstevel@tonic-gate 				 * Setup the autowakeup alarm.  Clear it
626*0Sstevel@tonic-gate 				 * right after poweroff, just in case if
627*0Sstevel@tonic-gate 				 * shutdown doesn't go through.
628*0Sstevel@tonic-gate 				 */
629*0Sstevel@tonic-gate 				if (info->pd_autoresume)
630*0Sstevel@tonic-gate 					tod_fd = open(TOD, O_RDWR);
631*0Sstevel@tonic-gate 				if (info->pd_autoresume && tod_fd != -1) {
632*0Sstevel@tonic-gate 					wakeup_time = (*now < f) ? f :
633*0Sstevel@tonic-gate 							(f + DAYS_TO_SECS);
634*0Sstevel@tonic-gate 					/*
635*0Sstevel@tonic-gate 					 * A software fix for hardware
636*0Sstevel@tonic-gate 					 * bug 1217415.
637*0Sstevel@tonic-gate 					 */
638*0Sstevel@tonic-gate 					if ((wakeup_time - *now) < 180) {
639*0Sstevel@tonic-gate 						logerror(
640*0Sstevel@tonic-gate 		"Since autowakeup time is less than 3 minutes away, "
641*0Sstevel@tonic-gate 		"autoshutdown will not occur.");
642*0Sstevel@tonic-gate 						shutdown_time = *now + 180;
643*0Sstevel@tonic-gate 						close(tod_fd);
644*0Sstevel@tonic-gate 						return;
645*0Sstevel@tonic-gate 					}
646*0Sstevel@tonic-gate 					if (ioctl(tod_fd, TOD_SET_ALARM,
647*0Sstevel@tonic-gate 							&wakeup_time) == -1) {
648*0Sstevel@tonic-gate 						logerror("Unable to program "
649*0Sstevel@tonic-gate 							"TOD alarm for "
650*0Sstevel@tonic-gate 							"autowakeup.");
651*0Sstevel@tonic-gate 						close(tod_fd);
652*0Sstevel@tonic-gate 						return;
653*0Sstevel@tonic-gate 					}
654*0Sstevel@tonic-gate 				}
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 				(void) poweroff("Autoshutdown",
657*0Sstevel@tonic-gate 				    autoshutdown_cmd);
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 				if (info->pd_autoresume && tod_fd != -1) {
660*0Sstevel@tonic-gate 					if (ioctl(tod_fd, TOD_CLEAR_ALARM,
661*0Sstevel@tonic-gate 							NULL) == -1)
662*0Sstevel@tonic-gate 						logerror("Unable to clear "
663*0Sstevel@tonic-gate 						    "alarm in TOD device.");
664*0Sstevel@tonic-gate 					close(tod_fd);
665*0Sstevel@tonic-gate 				}
666*0Sstevel@tonic-gate 
667*0Sstevel@tonic-gate 				(void) time(now);
668*0Sstevel@tonic-gate 				/* wait at least 5 mins */
669*0Sstevel@tonic-gate 				shutdown_time = *now +
670*0Sstevel@tonic-gate 					((info->pd_idle_time * 60) > 300 ?
671*0Sstevel@tonic-gate 					(info->pd_idle_time * 60) : 300);
672*0Sstevel@tonic-gate 			} else {
673*0Sstevel@tonic-gate 				/* wait 5 mins */
674*0Sstevel@tonic-gate 				shutdown_time = *now + 300;
675*0Sstevel@tonic-gate 			}
676*0Sstevel@tonic-gate 		} else
677*0Sstevel@tonic-gate 			shutdown_time = *now + next_time;
678*0Sstevel@tonic-gate 	} else if (s < f && *now >= f) {
679*0Sstevel@tonic-gate 		shutdown_time = s + DAYS_TO_SECS;
680*0Sstevel@tonic-gate 	} else
681*0Sstevel@tonic-gate 		shutdown_time = s;
682*0Sstevel@tonic-gate }
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate static int
685*0Sstevel@tonic-gate is_ok2shutdown(time_t *now)
686*0Sstevel@tonic-gate {
687*0Sstevel@tonic-gate 	int	prom_fd = -1;
688*0Sstevel@tonic-gate 	char	power_cycles_st[LLEN];
689*0Sstevel@tonic-gate 	char	power_cycle_limit_st[LLEN];
690*0Sstevel@tonic-gate 	char	system_board_date_st[LLEN];
691*0Sstevel@tonic-gate 	int	power_cycles, power_cycle_limit, free_cycles, scaled_cycles;
692*0Sstevel@tonic-gate 	time_t	life_began, life_passed;
693*0Sstevel@tonic-gate 	int	no_power_cycles = 0;
694*0Sstevel@tonic-gate 	int	no_system_board_date = 0;
695*0Sstevel@tonic-gate 	int	ret = 1;
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	/* CONSTCOND */
698*0Sstevel@tonic-gate 	while (1) {
699*0Sstevel@tonic-gate 		if ((prom_fd = open(PROM, O_RDWR)) == -1 &&
700*0Sstevel@tonic-gate 			(errno == EAGAIN))
701*0Sstevel@tonic-gate 				continue;
702*0Sstevel@tonic-gate 		break;
703*0Sstevel@tonic-gate 	}
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 	/*
706*0Sstevel@tonic-gate 	 * when #power-cycles property does not exist
707*0Sstevel@tonic-gate 	 * power cycles are unlimited.
708*0Sstevel@tonic-gate 	 */
709*0Sstevel@tonic-gate 	if (get_prom(prom_fd, options, "#power-cycles",
710*0Sstevel@tonic-gate 	    power_cycles_st, sizeof (power_cycles_st)) == 0)
711*0Sstevel@tonic-gate 		goto ckdone;
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	if (get_prom(prom_fd, root, "power-cycle-limit",
714*0Sstevel@tonic-gate 	    power_cycle_limit_st, sizeof (power_cycle_limit_st)) == 0) {
715*0Sstevel@tonic-gate 		power_cycle_limit = DEFAULT_POWER_CYCLE_LIMIT;
716*0Sstevel@tonic-gate 	} else {
717*0Sstevel@tonic-gate 		power_cycle_limit = atoi(power_cycle_limit_st);
718*0Sstevel@tonic-gate 	}
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 	/*
721*0Sstevel@tonic-gate 	 * Allow 10% of power_cycle_limit as free cycles.
722*0Sstevel@tonic-gate 	 */
723*0Sstevel@tonic-gate 	free_cycles = power_cycle_limit * 0.1;
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate 	power_cycles = atoi(power_cycles_st);
726*0Sstevel@tonic-gate 	if (power_cycles < 0)
727*0Sstevel@tonic-gate 		no_power_cycles++;
728*0Sstevel@tonic-gate 	else if (power_cycles <= free_cycles)
729*0Sstevel@tonic-gate 		goto ckdone;
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 	if (no_power_cycles && log_power_cycles_error == 0) {
732*0Sstevel@tonic-gate 		logerror("Invalid PROM property \"#power-cycles\" was found.");
733*0Sstevel@tonic-gate 		log_power_cycles_error++;
734*0Sstevel@tonic-gate 	}
735*0Sstevel@tonic-gate 
736*0Sstevel@tonic-gate 	if (get_prom(prom_fd, options, "system-board-date",
737*0Sstevel@tonic-gate 	    system_board_date_st, sizeof (system_board_date_st)) == 0) {
738*0Sstevel@tonic-gate 		no_system_board_date++;
739*0Sstevel@tonic-gate 	} else {
740*0Sstevel@tonic-gate 		life_began = strtol(system_board_date_st, (char **)NULL, 16);
741*0Sstevel@tonic-gate 		if (life_began > *now) {
742*0Sstevel@tonic-gate 			no_system_board_date++;
743*0Sstevel@tonic-gate 		}
744*0Sstevel@tonic-gate 	}
745*0Sstevel@tonic-gate 	if (no_system_board_date) {
746*0Sstevel@tonic-gate 		if (log_system_board_date_error == 0) {
747*0Sstevel@tonic-gate 			logerror("No or invalid PROM property "
748*0Sstevel@tonic-gate 			    "\"system-board-date\" was found.");
749*0Sstevel@tonic-gate 			log_system_board_date_error++;
750*0Sstevel@tonic-gate 		}
751*0Sstevel@tonic-gate 		life_began = DEFAULT_SYSTEM_BOARD_DATE;
752*0Sstevel@tonic-gate 	}
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 	life_passed = *now - life_began;
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate 	/*
757*0Sstevel@tonic-gate 	 * Since we don't keep the date that last free_cycle is ended, we
758*0Sstevel@tonic-gate 	 * need to spread (power_cycle_limit - free_cycles) over the entire
759*0Sstevel@tonic-gate 	 * 7-year life span instead of (lifetime - date free_cycles ended).
760*0Sstevel@tonic-gate 	 */
761*0Sstevel@tonic-gate 	scaled_cycles = ((float)life_passed / (float)LIFETIME_SECS) *
762*0Sstevel@tonic-gate 				(power_cycle_limit - free_cycles);
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate 	if (no_power_cycles)
765*0Sstevel@tonic-gate 		goto ckdone;
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate #ifdef DEBUG
768*0Sstevel@tonic-gate 	(void) fprintf(stderr, "Actual power_cycles = %d\t"
769*0Sstevel@tonic-gate 				"Scaled power_cycles = %d\n",
770*0Sstevel@tonic-gate 				power_cycles, scaled_cycles);
771*0Sstevel@tonic-gate #endif
772*0Sstevel@tonic-gate 	if (power_cycles > scaled_cycles) {
773*0Sstevel@tonic-gate 		if (log_no_autoshutdown_warning == 0) {
774*0Sstevel@tonic-gate 			logerror("Automatic shutdown has been temporarily "
775*0Sstevel@tonic-gate 			    "suspended in order to preserve the reliability "
776*0Sstevel@tonic-gate 			    "of this system.");
777*0Sstevel@tonic-gate 			log_no_autoshutdown_warning++;
778*0Sstevel@tonic-gate 		}
779*0Sstevel@tonic-gate 		ret = 0;
780*0Sstevel@tonic-gate 		goto ckdone;
781*0Sstevel@tonic-gate 	}
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate ckdone:
784*0Sstevel@tonic-gate 	if (prom_fd != -1)
785*0Sstevel@tonic-gate 		close(prom_fd);
786*0Sstevel@tonic-gate 	return (ret);
787*0Sstevel@tonic-gate }
788*0Sstevel@tonic-gate 
789*0Sstevel@tonic-gate static void
790*0Sstevel@tonic-gate check_idleness(time_t *now, hrtime_t *hr_now)
791*0Sstevel@tonic-gate {
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 	/*
794*0Sstevel@tonic-gate 	 * Check idleness only when autoshutdown is enabled.
795*0Sstevel@tonic-gate 	 */
796*0Sstevel@tonic-gate 	if (!autoshutdown_en) {
797*0Sstevel@tonic-gate 		checkidle_time = 0;
798*0Sstevel@tonic-gate 		return;
799*0Sstevel@tonic-gate 	}
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 	info->pd_ttychars_idle = check_tty(hr_now, asinfo.ttychars_thold);
802*0Sstevel@tonic-gate 	info->pd_loadaverage_idle =
803*0Sstevel@tonic-gate 	    check_load_ave(hr_now, asinfo.loadaverage_thold);
804*0Sstevel@tonic-gate 	info->pd_diskreads_idle = check_disks(hr_now, asinfo.diskreads_thold);
805*0Sstevel@tonic-gate 	info->pd_nfsreqs_idle = check_nfs(hr_now, asinfo.nfsreqs_thold);
806*0Sstevel@tonic-gate 
807*0Sstevel@tonic-gate #ifdef DEBUG
808*0Sstevel@tonic-gate 	(void) fprintf(stderr, "Idle ttychars for %d secs.\n",
809*0Sstevel@tonic-gate 			info->pd_ttychars_idle);
810*0Sstevel@tonic-gate 	(void) fprintf(stderr, "Idle loadaverage for %d secs.\n",
811*0Sstevel@tonic-gate 			info->pd_loadaverage_idle);
812*0Sstevel@tonic-gate 	(void) fprintf(stderr, "Idle diskreads for %d secs.\n",
813*0Sstevel@tonic-gate 			info->pd_diskreads_idle);
814*0Sstevel@tonic-gate 	(void) fprintf(stderr, "Idle nfsreqs for %d secs.\n",
815*0Sstevel@tonic-gate 			info->pd_nfsreqs_idle);
816*0Sstevel@tonic-gate #endif
817*0Sstevel@tonic-gate 
818*0Sstevel@tonic-gate 	checkidle_time = *now + IDLECHK_INTERVAL;
819*0Sstevel@tonic-gate }
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate static int
822*0Sstevel@tonic-gate last_system_activity(hrtime_t *hr_now)
823*0Sstevel@tonic-gate {
824*0Sstevel@tonic-gate 	int	act_idle, latest;
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 	latest = info->pd_idle_time * 60;
827*0Sstevel@tonic-gate 	act_idle = last_tty_activity(hr_now, asinfo.ttychars_thold);
828*0Sstevel@tonic-gate 	latest = MIN(latest, act_idle);
829*0Sstevel@tonic-gate 	act_idle = last_load_ave_activity(hr_now);
830*0Sstevel@tonic-gate 	latest = MIN(latest, act_idle);
831*0Sstevel@tonic-gate 	act_idle = last_disk_activity(hr_now, asinfo.diskreads_thold);
832*0Sstevel@tonic-gate 	latest = MIN(latest, act_idle);
833*0Sstevel@tonic-gate 	act_idle = last_nfs_activity(hr_now, asinfo.nfsreqs_thold);
834*0Sstevel@tonic-gate 	latest = MIN(latest, act_idle);
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate 	return (latest);
837*0Sstevel@tonic-gate }
838*0Sstevel@tonic-gate 
839*0Sstevel@tonic-gate static int
840*0Sstevel@tonic-gate run_idlecheck()
841*0Sstevel@tonic-gate {
842*0Sstevel@tonic-gate 	char		pm_variable[LLEN];
843*0Sstevel@tonic-gate 	char		*cp;
844*0Sstevel@tonic-gate 	int		status;
845*0Sstevel@tonic-gate 	pid_t		child;
846*0Sstevel@tonic-gate 
847*0Sstevel@tonic-gate 	/*
848*0Sstevel@tonic-gate 	 * Reap any child process which has been left over.
849*0Sstevel@tonic-gate 	 */
850*0Sstevel@tonic-gate 	while (waitpid((pid_t)-1, &status, WNOHANG) > 0);
851*0Sstevel@tonic-gate 
852*0Sstevel@tonic-gate 	/*
853*0Sstevel@tonic-gate 	 * Execute the user's idlecheck script and set variable PM_IDLETIME.
854*0Sstevel@tonic-gate 	 * Returned exit value is the idle time in minutes.
855*0Sstevel@tonic-gate 	 */
856*0Sstevel@tonic-gate 	if ((child = fork1()) == 0) {
857*0Sstevel@tonic-gate 		(void) sprintf(pm_variable, "PM_IDLETIME=%d",
858*0Sstevel@tonic-gate 			info->pd_idle_time);
859*0Sstevel@tonic-gate 		(void) putenv(pm_variable);
860*0Sstevel@tonic-gate 		cp = strrchr(asinfo.idlecheck_path, '/');
861*0Sstevel@tonic-gate 		if (cp == NULL)
862*0Sstevel@tonic-gate 			cp = asinfo.idlecheck_path;
863*0Sstevel@tonic-gate 		else
864*0Sstevel@tonic-gate 			cp++;
865*0Sstevel@tonic-gate 		(void) execl(asinfo.idlecheck_path, cp, NULL);
866*0Sstevel@tonic-gate 		exit(-1);
867*0Sstevel@tonic-gate 	} else if (child == -1) {
868*0Sstevel@tonic-gate 		return (info->pd_idle_time * 60);
869*0Sstevel@tonic-gate 	}
870*0Sstevel@tonic-gate 
871*0Sstevel@tonic-gate 	/*
872*0Sstevel@tonic-gate 	 * Wait until the idlecheck program completes.
873*0Sstevel@tonic-gate 	 */
874*0Sstevel@tonic-gate 	if (waitpid(child, &status, 0) != child) {
875*0Sstevel@tonic-gate 		/*
876*0Sstevel@tonic-gate 		 * We get here if the calling process gets a signal.
877*0Sstevel@tonic-gate 		 */
878*0Sstevel@tonic-gate 		return (info->pd_idle_time * 60);
879*0Sstevel@tonic-gate 	}
880*0Sstevel@tonic-gate 
881*0Sstevel@tonic-gate 	if (WEXITSTATUS(status) < 0) {
882*0Sstevel@tonic-gate 		return (info->pd_idle_time * 60);
883*0Sstevel@tonic-gate 	} else {
884*0Sstevel@tonic-gate 		return (WEXITSTATUS(status) * 60);
885*0Sstevel@tonic-gate 	}
886*0Sstevel@tonic-gate }
887*0Sstevel@tonic-gate 
888*0Sstevel@tonic-gate static void
889*0Sstevel@tonic-gate set_alarm(time_t now)
890*0Sstevel@tonic-gate {
891*0Sstevel@tonic-gate 	time_t	itime, stime, next_time, max_time;
892*0Sstevel@tonic-gate 	int	next_alarm;
893*0Sstevel@tonic-gate 
894*0Sstevel@tonic-gate 	max_time = MAX(checkidle_time, shutdown_time);
895*0Sstevel@tonic-gate 	if (max_time == 0) {
896*0Sstevel@tonic-gate 		(void) alarm(0);
897*0Sstevel@tonic-gate 		return;
898*0Sstevel@tonic-gate 	}
899*0Sstevel@tonic-gate 	itime = (checkidle_time == 0) ? max_time : checkidle_time;
900*0Sstevel@tonic-gate 	stime = (shutdown_time == 0) ? max_time : shutdown_time;
901*0Sstevel@tonic-gate 	next_time = MIN(itime, stime);
902*0Sstevel@tonic-gate 	next_alarm = (next_time <= now) ? 1 : (next_time - now);
903*0Sstevel@tonic-gate 	(void) alarm(next_alarm);
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate #ifdef DEBUG
906*0Sstevel@tonic-gate 	(void) fprintf(stderr, "Currently @ %s", ctime(&now));
907*0Sstevel@tonic-gate 	(void) fprintf(stderr, "Checkidle in %d secs\n", checkidle_time - now);
908*0Sstevel@tonic-gate 	(void) fprintf(stderr, "Shutdown  in %d secs\n", shutdown_time - now);
909*0Sstevel@tonic-gate 	(void) fprintf(stderr, "Next alarm goes off in %d secs\n", next_alarm);
910*0Sstevel@tonic-gate 	(void) fprintf(stderr, "************************************\n");
911*0Sstevel@tonic-gate #endif
912*0Sstevel@tonic-gate }
913*0Sstevel@tonic-gate 
914*0Sstevel@tonic-gate static int
915*0Sstevel@tonic-gate poweroff(char *msg, char **cmd_argv)
916*0Sstevel@tonic-gate {
917*0Sstevel@tonic-gate 	struct stat	statbuf;
918*0Sstevel@tonic-gate 	pid_t		pid, child;
919*0Sstevel@tonic-gate 	struct passwd	*pwd;
920*0Sstevel@tonic-gate 	char		*home, *user;
921*0Sstevel@tonic-gate 	char		ehome[] = "HOME=";
922*0Sstevel@tonic-gate 	char		euser[] = "LOGNAME=";
923*0Sstevel@tonic-gate 	int		status;
924*0Sstevel@tonic-gate 	char		**ca;
925*0Sstevel@tonic-gate 
926*0Sstevel@tonic-gate 	if (mutex_trylock(&poweroff_mutex) != 0)
927*0Sstevel@tonic-gate 		return (0);
928*0Sstevel@tonic-gate 
929*0Sstevel@tonic-gate 	if (stat("/dev/console", &statbuf) == -1 ||
930*0Sstevel@tonic-gate 	    (pwd = getpwuid(statbuf.st_uid)) == NULL) {
931*0Sstevel@tonic-gate 		mutex_unlock(&poweroff_mutex);
932*0Sstevel@tonic-gate 		return (1);
933*0Sstevel@tonic-gate 	}
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate 	if (msg)
936*0Sstevel@tonic-gate 		syslog(LOG_NOTICE, msg);
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 	if (*cmd_argv == NULL) {
939*0Sstevel@tonic-gate 		logerror("No command to run.");
940*0Sstevel@tonic-gate 		mutex_unlock(&poweroff_mutex);
941*0Sstevel@tonic-gate 		return (1);
942*0Sstevel@tonic-gate 	}
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 	home = malloc(strlen(pwd->pw_dir) + sizeof (ehome));
945*0Sstevel@tonic-gate 	user = malloc(strlen(pwd->pw_name) + sizeof (euser));
946*0Sstevel@tonic-gate 	if (home == NULL || user == NULL) {
947*0Sstevel@tonic-gate 		free(home);
948*0Sstevel@tonic-gate 		free(user);
949*0Sstevel@tonic-gate 		logerror("No memory.");
950*0Sstevel@tonic-gate 		mutex_unlock(&poweroff_mutex);
951*0Sstevel@tonic-gate 		return (1);
952*0Sstevel@tonic-gate 	}
953*0Sstevel@tonic-gate 	(void) strcpy(home, ehome);
954*0Sstevel@tonic-gate 	(void) strcat(home, pwd->pw_dir);
955*0Sstevel@tonic-gate 	(void) strcpy(user, euser);
956*0Sstevel@tonic-gate 	(void) strcat(user, pwd->pw_name);
957*0Sstevel@tonic-gate 
958*0Sstevel@tonic-gate 	/*
959*0Sstevel@tonic-gate 	 * Need to simulate the user enviroment, minimaly set HOME, and USER.
960*0Sstevel@tonic-gate 	 */
961*0Sstevel@tonic-gate 	if ((child = fork1()) == 0) {
962*0Sstevel@tonic-gate 		(void) putenv(home);
963*0Sstevel@tonic-gate 		(void) putenv(user);
964*0Sstevel@tonic-gate 		(void) setgid(pwd->pw_gid);
965*0Sstevel@tonic-gate 		(void) setuid(pwd->pw_uid);
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate 		/*
968*0Sstevel@tonic-gate 		 * check for shutdown flag and set environment
969*0Sstevel@tonic-gate 		 */
970*0Sstevel@tonic-gate 		for (ca = cmd_argv; *ca; ca++) {
971*0Sstevel@tonic-gate 			if (strcmp("-h", *ca) == 0) {
972*0Sstevel@tonic-gate 				(void) putenv("SYSSUSPENDDODEFAULT=");
973*0Sstevel@tonic-gate 				break;
974*0Sstevel@tonic-gate 			}
975*0Sstevel@tonic-gate 		}
976*0Sstevel@tonic-gate 
977*0Sstevel@tonic-gate 		(void) execv(cmd_argv[0], cmd_argv);
978*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
979*0Sstevel@tonic-gate 	} else {
980*0Sstevel@tonic-gate 		free(home);
981*0Sstevel@tonic-gate 		free(user);
982*0Sstevel@tonic-gate 		if (child == -1) {
983*0Sstevel@tonic-gate 			mutex_unlock(&poweroff_mutex);
984*0Sstevel@tonic-gate 			return (1);
985*0Sstevel@tonic-gate 		}
986*0Sstevel@tonic-gate 	}
987*0Sstevel@tonic-gate 	pid = 0;
988*0Sstevel@tonic-gate 	while (pid != child)
989*0Sstevel@tonic-gate 		pid = wait(&status);
990*0Sstevel@tonic-gate 	if (WEXITSTATUS(status)) {
991*0Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "Failed to exec \"%s\".", cmd_argv[0]);
992*0Sstevel@tonic-gate 		mutex_unlock(&poweroff_mutex);
993*0Sstevel@tonic-gate 		return (1);
994*0Sstevel@tonic-gate 	}
995*0Sstevel@tonic-gate 
996*0Sstevel@tonic-gate 	mutex_unlock(&poweroff_mutex);
997*0Sstevel@tonic-gate 	return (0);
998*0Sstevel@tonic-gate }
999*0Sstevel@tonic-gate 
1000*0Sstevel@tonic-gate #define	PBUFSIZE	256
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate /*
1003*0Sstevel@tonic-gate  * Gets the value of a prom property at either root or options node.  It
1004*0Sstevel@tonic-gate  * returns 1 if it is successful, otherwise it returns 0 .
1005*0Sstevel@tonic-gate  */
1006*0Sstevel@tonic-gate static int
1007*0Sstevel@tonic-gate get_prom(int prom_fd, prom_node_t node_name,
1008*0Sstevel@tonic-gate 	char *property_name, char *property_value, size_t len)
1009*0Sstevel@tonic-gate {
1010*0Sstevel@tonic-gate 	union {
1011*0Sstevel@tonic-gate 		char buf[PBUFSIZE + sizeof (uint_t)];
1012*0Sstevel@tonic-gate 		struct openpromio opp;
1013*0Sstevel@tonic-gate 	} oppbuf;
1014*0Sstevel@tonic-gate 	register struct openpromio *opp = &(oppbuf.opp);
1015*0Sstevel@tonic-gate 	int	got_it = 0;
1016*0Sstevel@tonic-gate 
1017*0Sstevel@tonic-gate 	if (prom_fd == -1) {
1018*0Sstevel@tonic-gate 		return (0);
1019*0Sstevel@tonic-gate 	}
1020*0Sstevel@tonic-gate 
1021*0Sstevel@tonic-gate 	switch (node_name) {
1022*0Sstevel@tonic-gate 	case root:
1023*0Sstevel@tonic-gate 		(void *) memset(oppbuf.buf, 0, PBUFSIZE);
1024*0Sstevel@tonic-gate 		opp->oprom_size = PBUFSIZE;
1025*0Sstevel@tonic-gate 		if (ioctl(prom_fd, OPROMNEXT, opp) < 0) {
1026*0Sstevel@tonic-gate 			return (0);
1027*0Sstevel@tonic-gate 		}
1028*0Sstevel@tonic-gate 
1029*0Sstevel@tonic-gate 		/*
1030*0Sstevel@tonic-gate 		 * Passing null string will give us the first property.
1031*0Sstevel@tonic-gate 		 */
1032*0Sstevel@tonic-gate 		(void *) memset(oppbuf.buf, 0, PBUFSIZE);
1033*0Sstevel@tonic-gate 		do {
1034*0Sstevel@tonic-gate 			opp->oprom_size = PBUFSIZE;
1035*0Sstevel@tonic-gate 			if (ioctl(prom_fd, OPROMNXTPROP, opp) < 0) {
1036*0Sstevel@tonic-gate 				return (0);
1037*0Sstevel@tonic-gate 			}
1038*0Sstevel@tonic-gate 			if (strcmp(opp->oprom_array, property_name) == 0) {
1039*0Sstevel@tonic-gate 				got_it++;
1040*0Sstevel@tonic-gate 				break;
1041*0Sstevel@tonic-gate 			}
1042*0Sstevel@tonic-gate 		} while (opp->oprom_size > 0);
1043*0Sstevel@tonic-gate 
1044*0Sstevel@tonic-gate 		if (!got_it) {
1045*0Sstevel@tonic-gate 			return (0);
1046*0Sstevel@tonic-gate 		}
1047*0Sstevel@tonic-gate 		if (got_it && property_value == NULL) {
1048*0Sstevel@tonic-gate 			return (1);
1049*0Sstevel@tonic-gate 		}
1050*0Sstevel@tonic-gate 		opp->oprom_size = PBUFSIZE;
1051*0Sstevel@tonic-gate 		if (ioctl(prom_fd, OPROMGETPROP, opp) < 0) {
1052*0Sstevel@tonic-gate 			return (0);
1053*0Sstevel@tonic-gate 		}
1054*0Sstevel@tonic-gate 		if (opp->oprom_size == 0) {
1055*0Sstevel@tonic-gate 			*property_value = '\0';
1056*0Sstevel@tonic-gate 		} else {
1057*0Sstevel@tonic-gate 			estrcpy(property_value, opp->oprom_array, len);
1058*0Sstevel@tonic-gate 		}
1059*0Sstevel@tonic-gate 		break;
1060*0Sstevel@tonic-gate 	case options:
1061*0Sstevel@tonic-gate 		estrcpy(opp->oprom_array, property_name, PBUFSIZE);
1062*0Sstevel@tonic-gate 		opp->oprom_size = PBUFSIZE;
1063*0Sstevel@tonic-gate 		if (ioctl(prom_fd, OPROMGETOPT, opp) < 0) {
1064*0Sstevel@tonic-gate 			return (0);
1065*0Sstevel@tonic-gate 		}
1066*0Sstevel@tonic-gate 		if (opp->oprom_size == 0) {
1067*0Sstevel@tonic-gate 			return (0);
1068*0Sstevel@tonic-gate 		}
1069*0Sstevel@tonic-gate 		if (property_value != NULL) {
1070*0Sstevel@tonic-gate 			estrcpy(property_value, opp->oprom_array, len);
1071*0Sstevel@tonic-gate 		}
1072*0Sstevel@tonic-gate 		break;
1073*0Sstevel@tonic-gate 	default:
1074*0Sstevel@tonic-gate 		logerror("Only root node and options node are supported.\n");
1075*0Sstevel@tonic-gate 		return (0);
1076*0Sstevel@tonic-gate 	}
1077*0Sstevel@tonic-gate 
1078*0Sstevel@tonic-gate 	return (1);
1079*0Sstevel@tonic-gate }
1080*0Sstevel@tonic-gate 
1081*0Sstevel@tonic-gate #define	isspace(ch)	((ch) == ' ' || (ch) == '\t')
1082*0Sstevel@tonic-gate #define	iseol(ch)	((ch) == '\n' || (ch) == '\r' || (ch) == '\f')
1083*0Sstevel@tonic-gate 
1084*0Sstevel@tonic-gate /*ARGSUSED*/
1085*0Sstevel@tonic-gate static void
1086*0Sstevel@tonic-gate power_button_monitor(void *arg)
1087*0Sstevel@tonic-gate {
1088*0Sstevel@tonic-gate 	struct pollfd pfd;
1089*0Sstevel@tonic-gate 	int events;
1090*0Sstevel@tonic-gate 
1091*0Sstevel@tonic-gate 	if (ioctl(pb_fd, PB_BEGIN_MONITOR, NULL) == -1) {
1092*0Sstevel@tonic-gate 		logerror("Failed to monitor the power button.");
1093*0Sstevel@tonic-gate 		thr_exit((void *) 0);
1094*0Sstevel@tonic-gate 	}
1095*0Sstevel@tonic-gate 
1096*0Sstevel@tonic-gate 	pfd.fd = pb_fd;
1097*0Sstevel@tonic-gate 	pfd.events = POLLIN;
1098*0Sstevel@tonic-gate 
1099*0Sstevel@tonic-gate 	/*CONSTCOND*/
1100*0Sstevel@tonic-gate 	while (1) {
1101*0Sstevel@tonic-gate 		if (poll(&pfd, 1, INFTIM) == -1) {
1102*0Sstevel@tonic-gate 			logerror("Failed to poll for power button events.");
1103*0Sstevel@tonic-gate 			thr_exit((void *) 0);
1104*0Sstevel@tonic-gate 		}
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate 		if (!(pfd.revents & POLLIN))
1107*0Sstevel@tonic-gate 			continue;
1108*0Sstevel@tonic-gate 
1109*0Sstevel@tonic-gate 		if (ioctl(pfd.fd, PB_GET_EVENTS, &events) == -1) {
1110*0Sstevel@tonic-gate 			logerror("Failed to get power button events.");
1111*0Sstevel@tonic-gate 			thr_exit((void *) 0);
1112*0Sstevel@tonic-gate 		}
1113*0Sstevel@tonic-gate 
1114*0Sstevel@tonic-gate 		if ((events & PB_BUTTON_PRESS) &&
1115*0Sstevel@tonic-gate 		    (poweroff(NULL, power_button_cmd) != 0)) {
1116*0Sstevel@tonic-gate 			logerror("Power button is pressed, powering "
1117*0Sstevel@tonic-gate 			    "down the system!");
1118*0Sstevel@tonic-gate 
1119*0Sstevel@tonic-gate 			/*
1120*0Sstevel@tonic-gate 			 * Send SIGPWR signal to the init process to
1121*0Sstevel@tonic-gate 			 * shut down the system.
1122*0Sstevel@tonic-gate 			 */
1123*0Sstevel@tonic-gate 			if (kill(1, SIGPWR) == -1)
1124*0Sstevel@tonic-gate 				(void) uadmin(A_SHUTDOWN, AD_POWEROFF, 0);
1125*0Sstevel@tonic-gate 		}
1126*0Sstevel@tonic-gate 
1127*0Sstevel@tonic-gate 		/*
1128*0Sstevel@tonic-gate 		 * Clear any power button event that has happened
1129*0Sstevel@tonic-gate 		 * meanwhile we were busy processing the last one.
1130*0Sstevel@tonic-gate 		 */
1131*0Sstevel@tonic-gate 		if (ioctl(pfd.fd, PB_GET_EVENTS, &events) == -1) {
1132*0Sstevel@tonic-gate 			logerror("Failed to get power button events.");
1133*0Sstevel@tonic-gate 			thr_exit((void *) 0);
1134*0Sstevel@tonic-gate 		}
1135*0Sstevel@tonic-gate 	}
1136*0Sstevel@tonic-gate }
1137*0Sstevel@tonic-gate 
1138*0Sstevel@tonic-gate #ifdef sparc
1139*0Sstevel@tonic-gate static void
1140*0Sstevel@tonic-gate do_attach(void)
1141*0Sstevel@tonic-gate {
1142*0Sstevel@tonic-gate 	if (read_cpr_config() < 0)
1143*0Sstevel@tonic-gate 		return;
1144*0Sstevel@tonic-gate 
1145*0Sstevel@tonic-gate 	/*
1146*0Sstevel@tonic-gate 	 * If autopm behavior is explicitly enabled for energystar-v2, or
1147*0Sstevel@tonic-gate 	 * set to default for energystar-v3, create a new thread to attach
1148*0Sstevel@tonic-gate 	 * all devices.
1149*0Sstevel@tonic-gate 	 */
1150*0Sstevel@tonic-gate 	estar_v3_prop = asinfo.is_autopm_default;
1151*0Sstevel@tonic-gate 	if ((strcmp(asinfo.apm_behavior, "enable") == 0) ||
1152*0Sstevel@tonic-gate 	    (estar_v3_prop && strcmp(asinfo.apm_behavior, "default") == 0)) {
1153*0Sstevel@tonic-gate 		if (thr_create(NULL, NULL, attach_devices, NULL,
1154*0Sstevel@tonic-gate 		    THR_DAEMON, NULL) != 0) {
1155*0Sstevel@tonic-gate 			logerror("Unable to create thread to attach devices.");
1156*0Sstevel@tonic-gate 		}
1157*0Sstevel@tonic-gate 	}
1158*0Sstevel@tonic-gate }
1159*0Sstevel@tonic-gate 
1160*0Sstevel@tonic-gate /*ARGSUSED*/
1161*0Sstevel@tonic-gate static void *
1162*0Sstevel@tonic-gate attach_devices(void *arg)
1163*0Sstevel@tonic-gate {
1164*0Sstevel@tonic-gate 	di_node_t root_node;
1165*0Sstevel@tonic-gate 
1166*0Sstevel@tonic-gate 	sleep(60);	/* let booting finish first */
1167*0Sstevel@tonic-gate 
1168*0Sstevel@tonic-gate 	if ((root_node = di_init("/", DINFOFORCE)) == DI_NODE_NIL) {
1169*0Sstevel@tonic-gate 		logerror("Failed to attach devices.");
1170*0Sstevel@tonic-gate 		return (NULL);
1171*0Sstevel@tonic-gate 	}
1172*0Sstevel@tonic-gate 	di_fini(root_node);
1173*0Sstevel@tonic-gate 
1174*0Sstevel@tonic-gate 	/*
1175*0Sstevel@tonic-gate 	 * Unload all the modules.
1176*0Sstevel@tonic-gate 	 */
1177*0Sstevel@tonic-gate 	(void) modctl(MODUNLOAD, 0);
1178*0Sstevel@tonic-gate 
1179*0Sstevel@tonic-gate 	return (NULL);
1180*0Sstevel@tonic-gate }
1181*0Sstevel@tonic-gate #endif
1182*0Sstevel@tonic-gate 
1183*0Sstevel@tonic-gate 
1184*0Sstevel@tonic-gate /*
1185*0Sstevel@tonic-gate  * Create a file which will contain our pid.  Pmconfig will check this file
1186*0Sstevel@tonic-gate  * to see if we are running and can use the pid to signal us.  Returns the
1187*0Sstevel@tonic-gate  * file descriptor if successful, -1 otherwise.
1188*0Sstevel@tonic-gate  *
1189*0Sstevel@tonic-gate  * Note: Deal with attempt to launch multiple instances and also with existence
1190*0Sstevel@tonic-gate  * of an obsolete pid file caused by an earlier abort.
1191*0Sstevel@tonic-gate  */
1192*0Sstevel@tonic-gate static int
1193*0Sstevel@tonic-gate open_pidfile(char *me)
1194*0Sstevel@tonic-gate {
1195*0Sstevel@tonic-gate 	int fd;
1196*0Sstevel@tonic-gate 	char *e1 = "%s: Cannot open pid file for read: ";
1197*0Sstevel@tonic-gate 	char *e2 = "%s: Cannot unlink obsolete pid file: ";
1198*0Sstevel@tonic-gate 	char *e3 = "%s: Cannot open /proc for pid %ld: ";
1199*0Sstevel@tonic-gate 	char *e4 = "%s: Cannot read /proc for pid %ld: ";
1200*0Sstevel@tonic-gate 	char *e5 = "%s: Another instance (pid %ld) is trying to exit"
1201*0Sstevel@tonic-gate 			"and may be hung.  Please contact sysadmin.\n";
1202*0Sstevel@tonic-gate 	char *e6 = "%s: Another daemon is running\n";
1203*0Sstevel@tonic-gate 	char *e7 = "%s: Cannot create pid file: ";
1204*0Sstevel@tonic-gate 
1205*0Sstevel@tonic-gate again:
1206*0Sstevel@tonic-gate 	if ((fd = open(pidpath, O_CREAT | O_EXCL | O_WRONLY, 0444)) == -1) {
1207*0Sstevel@tonic-gate 		if (errno  == EEXIST) {
1208*0Sstevel@tonic-gate 			FILE *fp;
1209*0Sstevel@tonic-gate 			int ps_fd;
1210*0Sstevel@tonic-gate 			pid_t pid;
1211*0Sstevel@tonic-gate 			psinfo_t ps_info;
1212*0Sstevel@tonic-gate 
1213*0Sstevel@tonic-gate 			if ((fp = fopen(pidpath, "r")) == NULL) {
1214*0Sstevel@tonic-gate 				(void) fprintf(stderr, e1, me);
1215*0Sstevel@tonic-gate 				perror(NULL);
1216*0Sstevel@tonic-gate 				return (-1);
1217*0Sstevel@tonic-gate 			}
1218*0Sstevel@tonic-gate 
1219*0Sstevel@tonic-gate 			/* Read the pid */
1220*0Sstevel@tonic-gate 			pid = (pid_t)-1;
1221*0Sstevel@tonic-gate 			(void) fscanf(fp, "%ld", &pid);
1222*0Sstevel@tonic-gate 			(void) fclose(fp);
1223*0Sstevel@tonic-gate 			if (pid == -1) {
1224*0Sstevel@tonic-gate 				if (unlink(pidpath) == -1) {
1225*0Sstevel@tonic-gate 					(void) fprintf(stderr, e2, me);
1226*0Sstevel@tonic-gate 					perror(NULL);
1227*0Sstevel@tonic-gate 					return (-1);
1228*0Sstevel@tonic-gate 				} else /* try without corrupted file */
1229*0Sstevel@tonic-gate 					goto again;
1230*0Sstevel@tonic-gate 			}
1231*0Sstevel@tonic-gate 
1232*0Sstevel@tonic-gate 			/* Is pid for a running process? */
1233*0Sstevel@tonic-gate 			(void) sprintf(scratch, "/proc/%ld/psinfo", pid);
1234*0Sstevel@tonic-gate 			ps_fd = open(scratch, O_RDONLY | O_NDELAY);
1235*0Sstevel@tonic-gate 			if (ps_fd == -1) {
1236*0Sstevel@tonic-gate 				if (errno == ENOENT) {
1237*0Sstevel@tonic-gate 					if (unlink(pidpath) == -1) {
1238*0Sstevel@tonic-gate 						(void) fprintf(stderr, e2, me);
1239*0Sstevel@tonic-gate 						perror(NULL);
1240*0Sstevel@tonic-gate 						return (-1);
1241*0Sstevel@tonic-gate 					} else	/* try without obsolete file */
1242*0Sstevel@tonic-gate 						goto again;
1243*0Sstevel@tonic-gate 				}
1244*0Sstevel@tonic-gate 				(void) fprintf(stderr, e3, me, pid);
1245*0Sstevel@tonic-gate 				return (-1);
1246*0Sstevel@tonic-gate 			}
1247*0Sstevel@tonic-gate 			if (read(ps_fd, &ps_info,
1248*0Sstevel@tonic-gate 			    sizeof (ps_info)) != sizeof (ps_info)) {
1249*0Sstevel@tonic-gate 				(void) fprintf(stderr, e4, me, pid);
1250*0Sstevel@tonic-gate 				perror(NULL);
1251*0Sstevel@tonic-gate 				(void) close(ps_fd);
1252*0Sstevel@tonic-gate 				return (-1);
1253*0Sstevel@tonic-gate 			}
1254*0Sstevel@tonic-gate 			(void) close(ps_fd);
1255*0Sstevel@tonic-gate 			if (ps_info.pr_nlwp == 0) {	/* defunct process */
1256*0Sstevel@tonic-gate 				(void) fprintf(stderr, e5, me, pid);
1257*0Sstevel@tonic-gate 				return (-1);
1258*0Sstevel@tonic-gate 			} else {	/* instance of daemon already running */
1259*0Sstevel@tonic-gate 				(void) fprintf(stderr, e6, me);
1260*0Sstevel@tonic-gate 				return (-1);
1261*0Sstevel@tonic-gate 			}
1262*0Sstevel@tonic-gate 		} else {	/* create failure not due to existing file */
1263*0Sstevel@tonic-gate 			(void) fprintf(stderr, e7, me);
1264*0Sstevel@tonic-gate 			perror(NULL);
1265*0Sstevel@tonic-gate 			return (-1);
1266*0Sstevel@tonic-gate 		}
1267*0Sstevel@tonic-gate 	}
1268*0Sstevel@tonic-gate 
1269*0Sstevel@tonic-gate 	(void) fchown(fd, (uid_t)-1, (gid_t)0);
1270*0Sstevel@tonic-gate 	return (fd);
1271*0Sstevel@tonic-gate }
1272*0Sstevel@tonic-gate 
1273*0Sstevel@tonic-gate /*
1274*0Sstevel@tonic-gate  * Write a pid to the pid file.  Report errors to syslog.
1275*0Sstevel@tonic-gate  *
1276*0Sstevel@tonic-gate  */
1277*0Sstevel@tonic-gate static int
1278*0Sstevel@tonic-gate write_pidfile(int fd, pid_t pid)
1279*0Sstevel@tonic-gate {
1280*0Sstevel@tonic-gate 	int	len;
1281*0Sstevel@tonic-gate 	int	rc = 0;			/* assume success */
1282*0Sstevel@tonic-gate 
1283*0Sstevel@tonic-gate 	len = sprintf(scratch, "%ld\n", pid);
1284*0Sstevel@tonic-gate 	if (write(fd, scratch, len) != len) {
1285*0Sstevel@tonic-gate 		logerror("Cannot write pid file: %s", strerror(errno));
1286*0Sstevel@tonic-gate 		rc = -1;
1287*0Sstevel@tonic-gate 	}
1288*0Sstevel@tonic-gate 
1289*0Sstevel@tonic-gate 	return (rc);
1290*0Sstevel@tonic-gate }
1291