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