17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5c42872d4Smh27603 * Common Development and Distribution License (the "License").
6c42872d4Smh27603 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22623ec8b0SRandy Fishel * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <stdio.h> /* Standard */
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <fcntl.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <time.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <pwd.h>
347c478bd9Sstevel@tonic-gate #include <dirent.h>
357c478bd9Sstevel@tonic-gate #include <thread.h>
367c478bd9Sstevel@tonic-gate #include <limits.h>
377c478bd9Sstevel@tonic-gate #include <sys/todio.h> /* Time-Of-Day chip */
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/wait.h>
407c478bd9Sstevel@tonic-gate #include <sys/ipc.h> /* IPC functions */
417c478bd9Sstevel@tonic-gate #include <signal.h> /* signal handling */
427c478bd9Sstevel@tonic-gate #include <syslog.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
457c478bd9Sstevel@tonic-gate #include <poll.h>
467c478bd9Sstevel@tonic-gate #include <sys/pm.h> /* power management driver */
477c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
487c478bd9Sstevel@tonic-gate #include <sys/openpromio.h> /* for prom access */
497c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> /* for MIN & MAX macros */
507c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
517c478bd9Sstevel@tonic-gate #include <sys/stropts.h> /* for INFTIM */
527c478bd9Sstevel@tonic-gate #include <sys/pbio.h>
537c478bd9Sstevel@tonic-gate #include <sys/cpr.h>
542df1fe9cSrandyf #include <sys/srn.h>
557c478bd9Sstevel@tonic-gate #include <stdarg.h>
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate #include "powerd.h"
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /* External Functions */
607c478bd9Sstevel@tonic-gate extern struct tm *localtime_r(const time_t *, struct tm *);
617c478bd9Sstevel@tonic-gate extern void sysstat_init(void);
627c478bd9Sstevel@tonic-gate extern int check_tty(hrtime_t *, int);
637c478bd9Sstevel@tonic-gate extern int check_disks(hrtime_t *, int);
647c478bd9Sstevel@tonic-gate extern int check_load_ave(hrtime_t *, float);
657c478bd9Sstevel@tonic-gate extern int check_nfs(hrtime_t *, int);
667c478bd9Sstevel@tonic-gate extern int last_disk_activity(hrtime_t *, int);
677c478bd9Sstevel@tonic-gate extern int last_tty_activity(hrtime_t *, int);
687c478bd9Sstevel@tonic-gate extern int last_load_ave_activity(hrtime_t *);
697c478bd9Sstevel@tonic-gate extern int last_nfs_activity(hrtime_t *, int);
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate #define PM "/dev/pm"
727c478bd9Sstevel@tonic-gate #define TOD "/dev/tod"
737c478bd9Sstevel@tonic-gate #define PROM "/dev/openprom"
747c478bd9Sstevel@tonic-gate #define PB "/dev/power_button"
752df1fe9cSrandyf #define SRN "/dev/srn"
767c478bd9Sstevel@tonic-gate #define LOGFILE "./powerd.log"
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate #define PBM_THREAD 0
797c478bd9Sstevel@tonic-gate #define ATTACH_THREAD 1
807c478bd9Sstevel@tonic-gate #define NUM_THREADS 2
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate #define CHECK_INTERVAL 5
837c478bd9Sstevel@tonic-gate #define IDLECHK_INTERVAL 15
847c478bd9Sstevel@tonic-gate #define MINS_TO_SECS 60
857c478bd9Sstevel@tonic-gate #define HOURS_TO_SECS (60 * 60)
867c478bd9Sstevel@tonic-gate #define DAYS_TO_SECS (24 * 60 * 60)
877c478bd9Sstevel@tonic-gate #define HOURS_TO_MINS 60
887c478bd9Sstevel@tonic-gate #define DAYS_TO_MINS (24 * 60)
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate #define LIFETIME_SECS (7 * 365 * DAYS_TO_SECS)
917c478bd9Sstevel@tonic-gate #define DEFAULT_POWER_CYCLE_LIMIT 10000
927c478bd9Sstevel@tonic-gate #define DEFAULT_SYSTEM_BOARD_DATE 804582000 /* July 1, 1995 */
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate #define LLEN 80
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate typedef enum {root, options} prom_node_t;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /* State Variables */
997c478bd9Sstevel@tonic-gate static struct cprconfig asinfo;
1007c478bd9Sstevel@tonic-gate static time_t shutdown_time; /* Time for next shutdown check */
1017c478bd9Sstevel@tonic-gate static time_t checkidle_time; /* Time for next idleness check */
1027c478bd9Sstevel@tonic-gate static time_t last_resume;
1037c478bd9Sstevel@tonic-gate pwr_info_t *info; /* private as config data buffer */
1047c478bd9Sstevel@tonic-gate static int pb_fd; /* power button driver */
1057c478bd9Sstevel@tonic-gate static int broadcast; /* Enables syslog messages */
1067c478bd9Sstevel@tonic-gate static int start_calc;
1077c478bd9Sstevel@tonic-gate static int autoshutdown_en;
1087c478bd9Sstevel@tonic-gate static int do_idlecheck;
1097c478bd9Sstevel@tonic-gate static int got_sighup;
1107c478bd9Sstevel@tonic-gate static int estar_v2_prop;
1117c478bd9Sstevel@tonic-gate static int estar_v3_prop;
1127c478bd9Sstevel@tonic-gate static int log_power_cycles_error = 0;
1137c478bd9Sstevel@tonic-gate static int log_system_board_date_error = 0;
1147c478bd9Sstevel@tonic-gate static int log_no_autoshutdown_warning = 0;
1157c478bd9Sstevel@tonic-gate static mutex_t poweroff_mutex;
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate static char *autoshutdown_cmd[] = {
118623ec8b0SRandy Fishel "/usr/bin/sys-suspend",
1197c478bd9Sstevel@tonic-gate "-n", "-d", ":0", NULL
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate static char *power_button_cmd[] = {
123623ec8b0SRandy Fishel "/usr/bin/sys-suspend",
1247c478bd9Sstevel@tonic-gate "-h", "-d", ":0", NULL
1257c478bd9Sstevel@tonic-gate };
1267c478bd9Sstevel@tonic-gate
1272df1fe9cSrandyf static char *autoS3_cmd[] = {
128623ec8b0SRandy Fishel "/usr/bin/sys-suspend",
1292df1fe9cSrandyf "-n", "-d", ":0", NULL
1302df1fe9cSrandyf };
1312df1fe9cSrandyf
1327c478bd9Sstevel@tonic-gate static char pidpath[] = PIDPATH;
1337c478bd9Sstevel@tonic-gate static char scratch[PATH_MAX];
1347c478bd9Sstevel@tonic-gate static char *prog;
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate /* Local Functions */
1377c478bd9Sstevel@tonic-gate static void alarm_handler(int);
1387c478bd9Sstevel@tonic-gate static void thaw_handler(int);
1397c478bd9Sstevel@tonic-gate static void kill_handler(int);
1407c478bd9Sstevel@tonic-gate static void work_handler(int);
1417c478bd9Sstevel@tonic-gate static void check_shutdown(time_t *, hrtime_t *);
1427c478bd9Sstevel@tonic-gate static void check_idleness(time_t *, hrtime_t *);
1437c478bd9Sstevel@tonic-gate static int last_system_activity(hrtime_t *);
1447c478bd9Sstevel@tonic-gate static int run_idlecheck(void);
1457c478bd9Sstevel@tonic-gate static void set_alarm(time_t);
146c42872d4Smh27603 static int poweroff(const char *, char **);
1477c478bd9Sstevel@tonic-gate static int is_ok2shutdown(time_t *);
1487c478bd9Sstevel@tonic-gate static int get_prom(int, prom_node_t, char *, char *, size_t);
1497c478bd9Sstevel@tonic-gate static void power_button_monitor(void *);
1507c478bd9Sstevel@tonic-gate static int open_pidfile(char *);
1517c478bd9Sstevel@tonic-gate static int write_pidfile(int, pid_t);
1527c478bd9Sstevel@tonic-gate static int read_cpr_config(void);
1537c478bd9Sstevel@tonic-gate static void system_activity_monitor(void);
1542df1fe9cSrandyf static void autos3_monitor(void);
1557c478bd9Sstevel@tonic-gate static void do_attach(void);
1567c478bd9Sstevel@tonic-gate static void *attach_devices(void *);
1572df1fe9cSrandyf static int powerd_debug;
1587c478bd9Sstevel@tonic-gate
159c42872d4Smh27603 /* PRINTFLIKE1 */
1607c478bd9Sstevel@tonic-gate static void
logerror(const char * fmt,...)161c42872d4Smh27603 logerror(const char *fmt, ...)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate va_list args;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate va_start(args, fmt);
1667c478bd9Sstevel@tonic-gate if (broadcast)
1677c478bd9Sstevel@tonic-gate vsyslog(LOG_ERR, fmt, args);
1687c478bd9Sstevel@tonic-gate va_end(args);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate static void
estrcpy(char * dst,char * src,size_t dlen)1737c478bd9Sstevel@tonic-gate estrcpy(char *dst, char *src, size_t dlen)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate size_t slen;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate slen = strlcpy(dst, src, dlen);
1787c478bd9Sstevel@tonic-gate if (slen >= dlen) {
1797c478bd9Sstevel@tonic-gate logerror("%s: string too long \"%s ...\"\n"
1807c478bd9Sstevel@tonic-gate "(len %d, max %d)\n", prog, dst, slen, dlen - 1);
1817c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1877c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate pid_t pid;
1907c478bd9Sstevel@tonic-gate int pm_fd;
1917c478bd9Sstevel@tonic-gate struct sigaction act;
1927c478bd9Sstevel@tonic-gate sigset_t sigmask;
1937c478bd9Sstevel@tonic-gate int c;
1947c478bd9Sstevel@tonic-gate char errmsg[PATH_MAX + 64];
1957c478bd9Sstevel@tonic-gate int pid_fd;
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate prog = argv[0];
1987c478bd9Sstevel@tonic-gate if (geteuid() != 0) {
1997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Must be root\n", prog);
2007c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate if ((pid_fd = open_pidfile(prog)) == -1)
2047c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate * Process options
2087c478bd9Sstevel@tonic-gate */
2097c478bd9Sstevel@tonic-gate broadcast = 1;
2102df1fe9cSrandyf while ((c = getopt(argc, argv, "nd")) != EOF) {
2117c478bd9Sstevel@tonic-gate switch (c) {
2122df1fe9cSrandyf case 'd':
2132df1fe9cSrandyf powerd_debug = 1;
2142df1fe9cSrandyf break;
2157c478bd9Sstevel@tonic-gate case 'n':
2167c478bd9Sstevel@tonic-gate broadcast = 0;
2177c478bd9Sstevel@tonic-gate break;
2187c478bd9Sstevel@tonic-gate case '?':
2197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Usage: %s [-n]\n", prog);
2207c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate pm_fd = open(PM, O_RDWR);
2257c478bd9Sstevel@tonic-gate if (pm_fd == -1) {
226c42872d4Smh27603 (void) snprintf(errmsg, sizeof (errmsg), "%s: %s", prog, PM);
2277c478bd9Sstevel@tonic-gate perror(errmsg);
2287c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate (void) close(pm_fd);
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate * Initialize mutex lock used to insure only one command to
2347c478bd9Sstevel@tonic-gate * run at a time.
2357c478bd9Sstevel@tonic-gate */
2367c478bd9Sstevel@tonic-gate if (mutex_init(&poweroff_mutex, USYNC_THREAD, NULL) != 0) {
2377c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2387c478bd9Sstevel@tonic-gate "%s: Unable to initialize mutex lock\n", prog);
2397c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate if ((info = (pwr_info_t *)malloc(sizeof (pwr_info_t))) == NULL) {
243c42872d4Smh27603 (void) snprintf(errmsg, sizeof (errmsg), "%s: malloc", prog);
2447c478bd9Sstevel@tonic-gate perror(errmsg);
2457c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate * Daemon is set to go...
2507c478bd9Sstevel@tonic-gate */
2517c478bd9Sstevel@tonic-gate if ((pid = fork()) < 0)
2527c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
2537c478bd9Sstevel@tonic-gate else if (pid != 0)
2547c478bd9Sstevel@tonic-gate exit(EXIT_SUCCESS);
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate pid = getpid();
2577c478bd9Sstevel@tonic-gate openlog(prog, 0, LOG_DAEMON);
2587c478bd9Sstevel@tonic-gate if (write_pidfile(pid_fd, pid) == -1) /* logs errors on failure */
2597c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
2607c478bd9Sstevel@tonic-gate (void) close(pid_fd);
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate * Close all the parent's file descriptors (Bug 1225843).
2647c478bd9Sstevel@tonic-gate */
2657c478bd9Sstevel@tonic-gate closefrom(0);
2667c478bd9Sstevel@tonic-gate (void) setsid();
2677c478bd9Sstevel@tonic-gate (void) chdir("/");
2687c478bd9Sstevel@tonic-gate (void) umask(0);
2697c478bd9Sstevel@tonic-gate #ifdef DEBUG
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate * Connect stdout to the console.
2727c478bd9Sstevel@tonic-gate */
2737c478bd9Sstevel@tonic-gate if (dup2(open("/dev/console", O_WRONLY|O_NOCTTY), 1) == -1) {
2747c478bd9Sstevel@tonic-gate logerror("Unable to connect to the console.");
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate #endif
2777c478bd9Sstevel@tonic-gate info->pd_flags = PD_AC;
2787c478bd9Sstevel@tonic-gate info->pd_idle_time = -1;
2797c478bd9Sstevel@tonic-gate info->pd_start_time = 0;
2807c478bd9Sstevel@tonic-gate info->pd_finish_time = 0;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate * Allow SIGQUIT, SIGINT and SIGTERM signals to terminate us
2847c478bd9Sstevel@tonic-gate * any time
2857c478bd9Sstevel@tonic-gate */
2867c478bd9Sstevel@tonic-gate act.sa_handler = kill_handler;
2877c478bd9Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask);
2887c478bd9Sstevel@tonic-gate act.sa_flags = 0;
2897c478bd9Sstevel@tonic-gate (void) sigaction(SIGQUIT, &act, NULL);
2907c478bd9Sstevel@tonic-gate (void) sigaction(SIGINT, &act, NULL);
2917c478bd9Sstevel@tonic-gate (void) sigaction(SIGTERM, &act, NULL);
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate (void) sigfillset(&sigmask);
2947c478bd9Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGQUIT);
2957c478bd9Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGINT);
2967c478bd9Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGTERM);
2977c478bd9Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &sigmask, NULL);
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate * If "power_button" device node can be opened, create a new
3017c478bd9Sstevel@tonic-gate * thread to monitor the power button.
3027c478bd9Sstevel@tonic-gate */
3037c478bd9Sstevel@tonic-gate if ((pb_fd = open(PB, O_RDONLY)) != -1) {
3042df1fe9cSrandyf if (powerd_debug)
3052df1fe9cSrandyf logerror("powerd starting power button monitor.");
3067c478bd9Sstevel@tonic-gate if (thr_create(NULL, NULL,
3077c478bd9Sstevel@tonic-gate (void *(*)(void *))power_button_monitor, NULL,
3087c478bd9Sstevel@tonic-gate THR_DAEMON, NULL) != 0) {
3097c478bd9Sstevel@tonic-gate logerror("Unable to monitor system's power button.");
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate do_attach();
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate /*
3167c478bd9Sstevel@tonic-gate * Create a new thread to monitor system activity and suspend
3177c478bd9Sstevel@tonic-gate * system if idle.
3187c478bd9Sstevel@tonic-gate */
3192df1fe9cSrandyf if (powerd_debug)
3202df1fe9cSrandyf logerror("powerd starting system activity monitor.");
3217c478bd9Sstevel@tonic-gate if (thr_create(NULL, NULL,
3227c478bd9Sstevel@tonic-gate (void *(*)(void *))system_activity_monitor, NULL,
3237c478bd9Sstevel@tonic-gate THR_DAEMON, NULL) != 0) {
3247c478bd9Sstevel@tonic-gate logerror("Unable to create thread to monitor system activity.");
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate /*
3282df1fe9cSrandyf * Create a new thread to handle autos3 trigger
3292df1fe9cSrandyf */
3302df1fe9cSrandyf if (powerd_debug)
3312df1fe9cSrandyf logerror("powerd starting autos3 monitor.");
3322df1fe9cSrandyf if (thr_create(NULL, NULL,
3332df1fe9cSrandyf (void *(*)(void *))autos3_monitor, NULL, THR_DAEMON, NULL) != 0) {
3342df1fe9cSrandyf logerror("Unable to create thread to monitor autos3 activity.");
3352df1fe9cSrandyf }
3362df1fe9cSrandyf
3372df1fe9cSrandyf /*
3387c478bd9Sstevel@tonic-gate * Block until we receive an explicit terminate signal
3397c478bd9Sstevel@tonic-gate */
3407c478bd9Sstevel@tonic-gate (void) sigsuspend(&sigmask);
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate return (1);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate static void
system_activity_monitor(void)3467c478bd9Sstevel@tonic-gate system_activity_monitor(void)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate struct sigaction act;
3497c478bd9Sstevel@tonic-gate sigset_t sigmask;
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate * Setup for gathering system's statistic.
3537c478bd9Sstevel@tonic-gate */
3547c478bd9Sstevel@tonic-gate sysstat_init();
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate * In addition to the SIGQUIT, SIGINT and SIGTERM signals already
3587c478bd9Sstevel@tonic-gate * being handled, this thread also needs to handle SIGHUP, SIGALRM
3597c478bd9Sstevel@tonic-gate * and SIGTHAW signals.
3607c478bd9Sstevel@tonic-gate */
3617c478bd9Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask);
3627c478bd9Sstevel@tonic-gate act.sa_flags = 0;
3637c478bd9Sstevel@tonic-gate act.sa_handler = alarm_handler;
3647c478bd9Sstevel@tonic-gate (void) sigaction(SIGALRM, &act, NULL);
3657c478bd9Sstevel@tonic-gate act.sa_handler = work_handler;
3667c478bd9Sstevel@tonic-gate (void) sigaction(SIGHUP, &act, NULL);
3677c478bd9Sstevel@tonic-gate act.sa_handler = thaw_handler;
3687c478bd9Sstevel@tonic-gate (void) sigaction(SIGTHAW, &act, NULL);
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate * Invoke work_handler with a dummy SIGHUP signal to read
3727c478bd9Sstevel@tonic-gate * cpr config file, get autoshutdown properties and schedule
3737c478bd9Sstevel@tonic-gate * an alarm if needed.
3747c478bd9Sstevel@tonic-gate */
3757c478bd9Sstevel@tonic-gate work_handler(SIGHUP);
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate * Wait for signal to read file
3797c478bd9Sstevel@tonic-gate */
3807c478bd9Sstevel@tonic-gate (void) thr_sigsetmask(0, 0, &sigmask);
3817c478bd9Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGHUP);
3827c478bd9Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGALRM);
3837c478bd9Sstevel@tonic-gate (void) sigdelset(&sigmask, SIGTHAW);
3847c478bd9Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &sigmask, NULL);
3857c478bd9Sstevel@tonic-gate do {
3867c478bd9Sstevel@tonic-gate (void) sigsuspend(&sigmask);
3877c478bd9Sstevel@tonic-gate } while (errno == EINTR);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate
3902df1fe9cSrandyf static void
autos3_monitor(void)3912df1fe9cSrandyf autos3_monitor(void)
3922df1fe9cSrandyf {
3932df1fe9cSrandyf struct pollfd poll_fd;
3942df1fe9cSrandyf srn_event_info_t srn_event; /* contains suspend type */
3952df1fe9cSrandyf int fd, ret;
3962df1fe9cSrandyf
3972df1fe9cSrandyf fd = open(SRN, O_RDWR|O_EXCL|O_NDELAY);
3982df1fe9cSrandyf if (fd == -1) {
3992df1fe9cSrandyf logerror("Unable to open %s: %s", SRN, strerror(errno));
400*5009f788SIgor Kozhukhov thr_exit((void *)(intptr_t)errno);
4012df1fe9cSrandyf }
4022df1fe9cSrandyf
4032df1fe9cSrandyf /*
4042df1fe9cSrandyf * Tell device we want the special sauce
4052df1fe9cSrandyf */
4062df1fe9cSrandyf ret = ioctl(fd, SRN_IOC_AUTOSX, NULL);
4072df1fe9cSrandyf if (ret == -1) {
4082df1fe9cSrandyf logerror("Ioctl SRN_IOC_AUTOSX failed: %s", strerror(errno));
40980148899SSurya Prakki (void) close(fd);
410*5009f788SIgor Kozhukhov thr_exit((void *)(intptr_t)errno);
4112df1fe9cSrandyf }
4122df1fe9cSrandyf poll_fd.fd = fd;
4132df1fe9cSrandyf /*CONSTCOND*/
4142df1fe9cSrandyf while (1) {
4152df1fe9cSrandyf poll_fd.revents = 0;
4162df1fe9cSrandyf poll_fd.events = POLLIN;
4172df1fe9cSrandyf if (poll(&poll_fd, 1, -1) < 0) {
4182df1fe9cSrandyf switch (errno) {
4192df1fe9cSrandyf case EINTR:
4202df1fe9cSrandyf case EAGAIN:
4212df1fe9cSrandyf continue;
4222df1fe9cSrandyf default:
4232df1fe9cSrandyf logerror("Poll error: %s", strerror(errno));
42480148899SSurya Prakki (void) close(fd);
425*5009f788SIgor Kozhukhov thr_exit((void *)(intptr_t)errno);
4262df1fe9cSrandyf }
4272df1fe9cSrandyf }
4282df1fe9cSrandyf
4292df1fe9cSrandyf ret = ioctl(fd, SRN_IOC_NEXTEVENT, &srn_event);
4302df1fe9cSrandyf if (ret == -1) {
4312df1fe9cSrandyf logerror("ioctl error: %s", strerror(errno));
43280148899SSurya Prakki (void) close(fd);
433*5009f788SIgor Kozhukhov thr_exit((void *)(intptr_t)errno);
4342df1fe9cSrandyf }
4352df1fe9cSrandyf switch (srn_event.ae_type) {
4362df1fe9cSrandyf case 3: /* S3 */
4372df1fe9cSrandyf if (powerd_debug)
4382df1fe9cSrandyf logerror("ioctl returns type: %d",
4392df1fe9cSrandyf srn_event.ae_type);
4402df1fe9cSrandyf break;
4412df1fe9cSrandyf default:
4422df1fe9cSrandyf logerror("Unsupported target state %d",
4432df1fe9cSrandyf srn_event.ae_type);
4442df1fe9cSrandyf continue;
4452df1fe9cSrandyf }
4462df1fe9cSrandyf (void) poweroff("AutoS3", autoS3_cmd);
4472df1fe9cSrandyf continue;
4482df1fe9cSrandyf }
4492df1fe9cSrandyf }
4502df1fe9cSrandyf
4517c478bd9Sstevel@tonic-gate static int
read_cpr_config(void)4527c478bd9Sstevel@tonic-gate read_cpr_config(void)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate int asfd;
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate if ((asfd = open(CPR_CONFIG, O_RDONLY)) < 0) {
4577c478bd9Sstevel@tonic-gate logerror("Unable to open CPR config file '%s'", CPR_CONFIG);
4587c478bd9Sstevel@tonic-gate return (-1);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate if (read(asfd, (void *)&asinfo, sizeof (asinfo)) != sizeof (asinfo)) {
4627c478bd9Sstevel@tonic-gate logerror("Unable to read CPR config file '%s'", CPR_CONFIG);
46380148899SSurya Prakki (void) close(asfd);
4647c478bd9Sstevel@tonic-gate return (-1);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate
4677c478bd9Sstevel@tonic-gate (void) close(asfd);
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate return (0);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4737c478bd9Sstevel@tonic-gate static void
thaw_handler(int sig)4747c478bd9Sstevel@tonic-gate thaw_handler(int sig)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate start_calc = 0;
4777c478bd9Sstevel@tonic-gate last_resume = time(NULL);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4817c478bd9Sstevel@tonic-gate static void
kill_handler(int sig)4827c478bd9Sstevel@tonic-gate kill_handler(int sig)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate int ret_code = EXIT_SUCCESS;
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate * Free resources
4887c478bd9Sstevel@tonic-gate */
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate free(info);
4917c478bd9Sstevel@tonic-gate if (pb_fd != -1) {
4927c478bd9Sstevel@tonic-gate (void) close(pb_fd);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate (void) mutex_destroy(&poweroff_mutex);
4957c478bd9Sstevel@tonic-gate (void) unlink(pidpath);
4967c478bd9Sstevel@tonic-gate closelog();
4977c478bd9Sstevel@tonic-gate exit(ret_code);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5017c478bd9Sstevel@tonic-gate static void
alarm_handler(int sig)5027c478bd9Sstevel@tonic-gate alarm_handler(int sig)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate time_t now;
5057c478bd9Sstevel@tonic-gate hrtime_t hr_now;
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate now = time(NULL);
5087c478bd9Sstevel@tonic-gate hr_now = gethrtime();
5097c478bd9Sstevel@tonic-gate if (checkidle_time <= now && checkidle_time != 0)
5107c478bd9Sstevel@tonic-gate check_idleness(&now, &hr_now);
5117c478bd9Sstevel@tonic-gate if (shutdown_time <= now && shutdown_time != 0)
5127c478bd9Sstevel@tonic-gate check_shutdown(&now, &hr_now);
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate set_alarm(now);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5187c478bd9Sstevel@tonic-gate static void
work_handler(int sig)5197c478bd9Sstevel@tonic-gate work_handler(int sig)
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate time_t now;
5227c478bd9Sstevel@tonic-gate hrtime_t hr_now;
5237c478bd9Sstevel@tonic-gate struct stat stat_buf;
5247c478bd9Sstevel@tonic-gate
5257c478bd9Sstevel@tonic-gate do_idlecheck = 0;
5267c478bd9Sstevel@tonic-gate info->pd_flags = PD_AC;
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate * Parse the config file for autoshutdown and idleness entries.
5307c478bd9Sstevel@tonic-gate */
5317c478bd9Sstevel@tonic-gate if (read_cpr_config() < 0)
5327c478bd9Sstevel@tonic-gate return;
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate /*
5357c478bd9Sstevel@tonic-gate * Since Oct. 1, 1995, any new system shipped had root
5367c478bd9Sstevel@tonic-gate * property "energystar-v2" defined in its prom. Systems
5377c478bd9Sstevel@tonic-gate * shipped after July 1, 1999, will have "energystar-v3"
5387c478bd9Sstevel@tonic-gate * property.
5397c478bd9Sstevel@tonic-gate */
5407c478bd9Sstevel@tonic-gate estar_v2_prop = asinfo.is_cpr_default;
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate info->pd_flags |= asinfo.is_autowakeup_capable;
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate if (strlen(asinfo.idlecheck_path) > 0) {
5457c478bd9Sstevel@tonic-gate if (stat(asinfo.idlecheck_path, &stat_buf) != 0) {
5467c478bd9Sstevel@tonic-gate logerror("unable to access idlecheck program \"%s\".",
5477c478bd9Sstevel@tonic-gate asinfo.idlecheck_path);
5487c478bd9Sstevel@tonic-gate } else if (!(stat_buf.st_mode & S_IXUSR)) {
5497c478bd9Sstevel@tonic-gate logerror("idlecheck program \"%s\" is not executable.",
5507c478bd9Sstevel@tonic-gate asinfo.idlecheck_path);
5517c478bd9Sstevel@tonic-gate } else {
5527c478bd9Sstevel@tonic-gate do_idlecheck = 1;
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate if (strlen(asinfo.as_behavior) == 0 ||
5577c478bd9Sstevel@tonic-gate strcmp(asinfo.as_behavior, "noshutdown") == 0 ||
5587c478bd9Sstevel@tonic-gate strcmp(asinfo.as_behavior, "unconfigured") == 0) {
5597c478bd9Sstevel@tonic-gate info->pd_autoshutdown = 0;
5607c478bd9Sstevel@tonic-gate } else if (strcmp(asinfo.as_behavior, "default") == 0) {
5617c478bd9Sstevel@tonic-gate info->pd_autoshutdown = estar_v2_prop;
5627c478bd9Sstevel@tonic-gate } else if (strcmp(asinfo.as_behavior, "shutdown") == 0 ||
5637c478bd9Sstevel@tonic-gate strcmp(asinfo.as_behavior, "autowakeup") == 0) {
5647c478bd9Sstevel@tonic-gate info->pd_autoshutdown = asinfo.is_cpr_capable;
5657c478bd9Sstevel@tonic-gate } else {
5667c478bd9Sstevel@tonic-gate logerror("autoshutdown behavior \"%s\" unrecognized.",
5677c478bd9Sstevel@tonic-gate asinfo.as_behavior);
5687c478bd9Sstevel@tonic-gate info->pd_autoshutdown = 0;
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate if (info->pd_autoshutdown) {
5727c478bd9Sstevel@tonic-gate info->pd_idle_time = asinfo.as_idle;
5737c478bd9Sstevel@tonic-gate info->pd_start_time =
5747c478bd9Sstevel@tonic-gate (asinfo.as_sh * 60 + asinfo.as_sm) % DAYS_TO_MINS;
5757c478bd9Sstevel@tonic-gate info->pd_finish_time =
5767c478bd9Sstevel@tonic-gate (asinfo.as_fh * 60 + asinfo.as_fm) % DAYS_TO_MINS;
5777c478bd9Sstevel@tonic-gate info->pd_autoresume =
5787c478bd9Sstevel@tonic-gate (strcmp(asinfo.as_behavior, "autowakeup") == 0) ? 1 : 0;
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate autoshutdown_en = (asinfo.as_idle >= 0 && info->pd_autoshutdown)
5817c478bd9Sstevel@tonic-gate ? 1 : 0;
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate #ifdef DEBUG
5847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "autoshutdown_en = %d, as_idle = %d, "
5857c478bd9Sstevel@tonic-gate "pd_autoresume = %d\n",
5867c478bd9Sstevel@tonic-gate autoshutdown_en, asinfo.as_idle, info->pd_autoresume);
5872df1fe9cSrandyf
5887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, " pd_start_time=%d, pd_finish_time=%d\n",
5897c478bd9Sstevel@tonic-gate info->pd_start_time, info->pd_finish_time);
5907c478bd9Sstevel@tonic-gate #endif
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate got_sighup = 1;
5937c478bd9Sstevel@tonic-gate now = last_resume = time(NULL);
5947c478bd9Sstevel@tonic-gate hr_now = gethrtime();
5957c478bd9Sstevel@tonic-gate check_idleness(&now, &hr_now);
5967c478bd9Sstevel@tonic-gate check_shutdown(&now, &hr_now);
5977c478bd9Sstevel@tonic-gate set_alarm(now);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate static void
check_shutdown(time_t * now,hrtime_t * hr_now)6017c478bd9Sstevel@tonic-gate check_shutdown(time_t *now, hrtime_t *hr_now)
6027c478bd9Sstevel@tonic-gate {
6037c478bd9Sstevel@tonic-gate int tod_fd = -1;
6047c478bd9Sstevel@tonic-gate int kbd, mouse, system, least_idle, idlecheck_time;
6057c478bd9Sstevel@tonic-gate int next_time;
6067c478bd9Sstevel@tonic-gate int s, f;
6077c478bd9Sstevel@tonic-gate struct tm tmp_time;
6087c478bd9Sstevel@tonic-gate time_t start_of_day, time_since_last_resume;
6097c478bd9Sstevel@tonic-gate time_t wakeup_time;
6107c478bd9Sstevel@tonic-gate extern long conskbd_idle_time(void);
6117c478bd9Sstevel@tonic-gate extern long consms_idle_time(void);
6127c478bd9Sstevel@tonic-gate static int warned_kbd, warned_ms; /* print error msg one time */
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate if (!autoshutdown_en) {
6157c478bd9Sstevel@tonic-gate shutdown_time = 0;
6167c478bd9Sstevel@tonic-gate return;
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate
6197c478bd9Sstevel@tonic-gate (void) localtime_r(now, &tmp_time);
6207c478bd9Sstevel@tonic-gate tmp_time.tm_sec = 0;
6217c478bd9Sstevel@tonic-gate tmp_time.tm_min = 0;
6227c478bd9Sstevel@tonic-gate tmp_time.tm_hour = 0;
6237c478bd9Sstevel@tonic-gate start_of_day = mktime(&tmp_time);
6247c478bd9Sstevel@tonic-gate s = start_of_day + info->pd_start_time * 60;
6257c478bd9Sstevel@tonic-gate f = start_of_day + info->pd_finish_time * 60;
6267c478bd9Sstevel@tonic-gate if ((s < f && *now >= s && *now < f) ||
6277c478bd9Sstevel@tonic-gate (s >= f && (*now < f || *now >= s))) {
6287c478bd9Sstevel@tonic-gate if ((mouse = (int)consms_idle_time()) < 0) {
6297c478bd9Sstevel@tonic-gate if (! warned_ms) {
6307c478bd9Sstevel@tonic-gate warned_ms = 1;
6317c478bd9Sstevel@tonic-gate logerror("powerd: failed to get "
6327c478bd9Sstevel@tonic-gate "idle time for console mouse");
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate return;
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate if ((kbd = (int)conskbd_idle_time()) < 0) {
6377c478bd9Sstevel@tonic-gate if (! warned_kbd) {
6387c478bd9Sstevel@tonic-gate warned_kbd = 1;
6397c478bd9Sstevel@tonic-gate logerror("powerd: failed to get "
6407c478bd9Sstevel@tonic-gate "idle time for console keyboard");
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate return;
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate
6457c478bd9Sstevel@tonic-gate system = last_system_activity(hr_now);
6467c478bd9Sstevel@tonic-gate /* who is the last to go idle */
6477c478bd9Sstevel@tonic-gate least_idle = MIN(system, MIN(kbd, mouse));
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate /*
6507c478bd9Sstevel@tonic-gate * Calculate time_since_last_resume and the next_time
6517c478bd9Sstevel@tonic-gate * to auto suspend.
6527c478bd9Sstevel@tonic-gate */
6537c478bd9Sstevel@tonic-gate start_calc = 1;
6547c478bd9Sstevel@tonic-gate time_since_last_resume = time(NULL) - last_resume;
6557c478bd9Sstevel@tonic-gate next_time = info->pd_idle_time * 60 -
6567c478bd9Sstevel@tonic-gate MIN(least_idle, time_since_last_resume);
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate #ifdef DEBUG
6592df1fe9cSrandyf fprintf(stderr, " check_shutdown: next_time=%d\n", next_time);
6607c478bd9Sstevel@tonic-gate #endif
6617c478bd9Sstevel@tonic-gate
6627c478bd9Sstevel@tonic-gate /*
6637c478bd9Sstevel@tonic-gate * If we have get the SIGTHAW signal at this point - our
6647c478bd9Sstevel@tonic-gate * calculation of time_since_last_resume is wrong so
6657c478bd9Sstevel@tonic-gate * - we need to recalculate.
6667c478bd9Sstevel@tonic-gate */
6677c478bd9Sstevel@tonic-gate while (start_calc == 0) {
6687c478bd9Sstevel@tonic-gate /* need to redo calculation */
6697c478bd9Sstevel@tonic-gate start_calc = 1;
6707c478bd9Sstevel@tonic-gate time_since_last_resume = time(NULL) - last_resume;
6717c478bd9Sstevel@tonic-gate next_time = info->pd_idle_time * 60 -
6727c478bd9Sstevel@tonic-gate MIN(least_idle, time_since_last_resume);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate /*
6767c478bd9Sstevel@tonic-gate * Only when everything else is idle, run the user's idlecheck
6777c478bd9Sstevel@tonic-gate * script.
6787c478bd9Sstevel@tonic-gate */
6797c478bd9Sstevel@tonic-gate if (next_time <= 0 && do_idlecheck) {
6807c478bd9Sstevel@tonic-gate got_sighup = 0;
6817c478bd9Sstevel@tonic-gate idlecheck_time = run_idlecheck();
6827c478bd9Sstevel@tonic-gate next_time = info->pd_idle_time * 60 -
6837c478bd9Sstevel@tonic-gate MIN(idlecheck_time, MIN(least_idle,
6847c478bd9Sstevel@tonic-gate time_since_last_resume));
6857c478bd9Sstevel@tonic-gate /*
6867c478bd9Sstevel@tonic-gate * If we have caught SIGTHAW or SIGHUP, need to
6877c478bd9Sstevel@tonic-gate * recalculate.
6887c478bd9Sstevel@tonic-gate */
6897c478bd9Sstevel@tonic-gate while (start_calc == 0 || got_sighup == 1) {
6907c478bd9Sstevel@tonic-gate start_calc = 1;
6917c478bd9Sstevel@tonic-gate got_sighup = 0;
6927c478bd9Sstevel@tonic-gate idlecheck_time = run_idlecheck();
6937c478bd9Sstevel@tonic-gate time_since_last_resume = time(NULL) -
6947c478bd9Sstevel@tonic-gate last_resume;
6957c478bd9Sstevel@tonic-gate next_time = info->pd_idle_time * 60 -
6967c478bd9Sstevel@tonic-gate MIN(idlecheck_time, MIN(least_idle,
6977c478bd9Sstevel@tonic-gate time_since_last_resume));
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate
7017c478bd9Sstevel@tonic-gate if (next_time <= 0) {
7027c478bd9Sstevel@tonic-gate if (is_ok2shutdown(now)) {
7037c478bd9Sstevel@tonic-gate /*
7047c478bd9Sstevel@tonic-gate * Setup the autowakeup alarm. Clear it
7057c478bd9Sstevel@tonic-gate * right after poweroff, just in case if
7067c478bd9Sstevel@tonic-gate * shutdown doesn't go through.
7077c478bd9Sstevel@tonic-gate */
7087c478bd9Sstevel@tonic-gate if (info->pd_autoresume)
7097c478bd9Sstevel@tonic-gate tod_fd = open(TOD, O_RDWR);
7107c478bd9Sstevel@tonic-gate if (info->pd_autoresume && tod_fd != -1) {
7117c478bd9Sstevel@tonic-gate wakeup_time = (*now < f) ? f :
7127c478bd9Sstevel@tonic-gate (f + DAYS_TO_SECS);
7137c478bd9Sstevel@tonic-gate /*
7147c478bd9Sstevel@tonic-gate * A software fix for hardware
7157c478bd9Sstevel@tonic-gate * bug 1217415.
7167c478bd9Sstevel@tonic-gate */
7177c478bd9Sstevel@tonic-gate if ((wakeup_time - *now) < 180) {
7187c478bd9Sstevel@tonic-gate logerror(
7197c478bd9Sstevel@tonic-gate "Since autowakeup time is less than 3 minutes away, "
7207c478bd9Sstevel@tonic-gate "autoshutdown will not occur.");
7217c478bd9Sstevel@tonic-gate shutdown_time = *now + 180;
72280148899SSurya Prakki (void) close(tod_fd);
7237c478bd9Sstevel@tonic-gate return;
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate if (ioctl(tod_fd, TOD_SET_ALARM,
7267c478bd9Sstevel@tonic-gate &wakeup_time) == -1) {
7272df1fe9cSrandyf logerror("Unable to program TOD"
7282df1fe9cSrandyf " alarm for autowakeup.");
72980148899SSurya Prakki (void) close(tod_fd);
7307c478bd9Sstevel@tonic-gate return;
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate (void) poweroff("Autoshutdown",
7357c478bd9Sstevel@tonic-gate autoshutdown_cmd);
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate if (info->pd_autoresume && tod_fd != -1) {
7387c478bd9Sstevel@tonic-gate if (ioctl(tod_fd, TOD_CLEAR_ALARM,
7397c478bd9Sstevel@tonic-gate NULL) == -1)
7407c478bd9Sstevel@tonic-gate logerror("Unable to clear "
7417c478bd9Sstevel@tonic-gate "alarm in TOD device.");
74280148899SSurya Prakki (void) close(tod_fd);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate
7457c478bd9Sstevel@tonic-gate (void) time(now);
7467c478bd9Sstevel@tonic-gate /* wait at least 5 mins */
7477c478bd9Sstevel@tonic-gate shutdown_time = *now +
7487c478bd9Sstevel@tonic-gate ((info->pd_idle_time * 60) > 300 ?
7497c478bd9Sstevel@tonic-gate (info->pd_idle_time * 60) : 300);
7507c478bd9Sstevel@tonic-gate } else {
7517c478bd9Sstevel@tonic-gate /* wait 5 mins */
7527c478bd9Sstevel@tonic-gate shutdown_time = *now + 300;
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate } else
7557c478bd9Sstevel@tonic-gate shutdown_time = *now + next_time;
7567c478bd9Sstevel@tonic-gate } else if (s < f && *now >= f) {
7577c478bd9Sstevel@tonic-gate shutdown_time = s + DAYS_TO_SECS;
7587c478bd9Sstevel@tonic-gate } else
7597c478bd9Sstevel@tonic-gate shutdown_time = s;
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate
7627c478bd9Sstevel@tonic-gate static int
is_ok2shutdown(time_t * now)7637c478bd9Sstevel@tonic-gate is_ok2shutdown(time_t *now)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate int prom_fd = -1;
7667c478bd9Sstevel@tonic-gate char power_cycles_st[LLEN];
7677c478bd9Sstevel@tonic-gate char power_cycle_limit_st[LLEN];
7687c478bd9Sstevel@tonic-gate char system_board_date_st[LLEN];
7697c478bd9Sstevel@tonic-gate int power_cycles, power_cycle_limit, free_cycles, scaled_cycles;
7707c478bd9Sstevel@tonic-gate time_t life_began, life_passed;
7717c478bd9Sstevel@tonic-gate int no_power_cycles = 0;
7727c478bd9Sstevel@tonic-gate int no_system_board_date = 0;
7737c478bd9Sstevel@tonic-gate int ret = 1;
7747c478bd9Sstevel@tonic-gate
7757c478bd9Sstevel@tonic-gate /* CONSTCOND */
7767c478bd9Sstevel@tonic-gate while (1) {
7777c478bd9Sstevel@tonic-gate if ((prom_fd = open(PROM, O_RDWR)) == -1 &&
7787c478bd9Sstevel@tonic-gate (errno == EAGAIN))
7797c478bd9Sstevel@tonic-gate continue;
7807c478bd9Sstevel@tonic-gate break;
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate /*
7847c478bd9Sstevel@tonic-gate * when #power-cycles property does not exist
7857c478bd9Sstevel@tonic-gate * power cycles are unlimited.
7867c478bd9Sstevel@tonic-gate */
7877c478bd9Sstevel@tonic-gate if (get_prom(prom_fd, options, "#power-cycles",
7887c478bd9Sstevel@tonic-gate power_cycles_st, sizeof (power_cycles_st)) == 0)
7897c478bd9Sstevel@tonic-gate goto ckdone;
7907c478bd9Sstevel@tonic-gate
7917c478bd9Sstevel@tonic-gate if (get_prom(prom_fd, root, "power-cycle-limit",
7927c478bd9Sstevel@tonic-gate power_cycle_limit_st, sizeof (power_cycle_limit_st)) == 0) {
7937c478bd9Sstevel@tonic-gate power_cycle_limit = DEFAULT_POWER_CYCLE_LIMIT;
7947c478bd9Sstevel@tonic-gate } else {
7957c478bd9Sstevel@tonic-gate power_cycle_limit = atoi(power_cycle_limit_st);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate
7987c478bd9Sstevel@tonic-gate /*
7997c478bd9Sstevel@tonic-gate * Allow 10% of power_cycle_limit as free cycles.
8007c478bd9Sstevel@tonic-gate */
801c42872d4Smh27603 free_cycles = power_cycle_limit / 10;
8027c478bd9Sstevel@tonic-gate
8037c478bd9Sstevel@tonic-gate power_cycles = atoi(power_cycles_st);
8047c478bd9Sstevel@tonic-gate if (power_cycles < 0)
8057c478bd9Sstevel@tonic-gate no_power_cycles++;
8067c478bd9Sstevel@tonic-gate else if (power_cycles <= free_cycles)
8077c478bd9Sstevel@tonic-gate goto ckdone;
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate if (no_power_cycles && log_power_cycles_error == 0) {
8107c478bd9Sstevel@tonic-gate logerror("Invalid PROM property \"#power-cycles\" was found.");
8117c478bd9Sstevel@tonic-gate log_power_cycles_error++;
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate
8147c478bd9Sstevel@tonic-gate if (get_prom(prom_fd, options, "system-board-date",
8157c478bd9Sstevel@tonic-gate system_board_date_st, sizeof (system_board_date_st)) == 0) {
8167c478bd9Sstevel@tonic-gate no_system_board_date++;
8177c478bd9Sstevel@tonic-gate } else {
8187c478bd9Sstevel@tonic-gate life_began = strtol(system_board_date_st, (char **)NULL, 16);
8197c478bd9Sstevel@tonic-gate if (life_began > *now) {
8207c478bd9Sstevel@tonic-gate no_system_board_date++;
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate if (no_system_board_date) {
8247c478bd9Sstevel@tonic-gate if (log_system_board_date_error == 0) {
8257c478bd9Sstevel@tonic-gate logerror("No or invalid PROM property "
8267c478bd9Sstevel@tonic-gate "\"system-board-date\" was found.");
8277c478bd9Sstevel@tonic-gate log_system_board_date_error++;
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate life_began = DEFAULT_SYSTEM_BOARD_DATE;
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate
8327c478bd9Sstevel@tonic-gate life_passed = *now - life_began;
8337c478bd9Sstevel@tonic-gate
8347c478bd9Sstevel@tonic-gate /*
8357c478bd9Sstevel@tonic-gate * Since we don't keep the date that last free_cycle is ended, we
8367c478bd9Sstevel@tonic-gate * need to spread (power_cycle_limit - free_cycles) over the entire
8377c478bd9Sstevel@tonic-gate * 7-year life span instead of (lifetime - date free_cycles ended).
8387c478bd9Sstevel@tonic-gate */
839c42872d4Smh27603 scaled_cycles = (int)(((float)life_passed / (float)LIFETIME_SECS) *
840c42872d4Smh27603 (power_cycle_limit - free_cycles));
8417c478bd9Sstevel@tonic-gate
8427c478bd9Sstevel@tonic-gate if (no_power_cycles)
8437c478bd9Sstevel@tonic-gate goto ckdone;
8447c478bd9Sstevel@tonic-gate
8457c478bd9Sstevel@tonic-gate #ifdef DEBUG
8467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Actual power_cycles = %d\t"
8472df1fe9cSrandyf "Scaled power_cycles = %d\n", power_cycles, scaled_cycles);
8487c478bd9Sstevel@tonic-gate #endif
8497c478bd9Sstevel@tonic-gate if (power_cycles > scaled_cycles) {
8507c478bd9Sstevel@tonic-gate if (log_no_autoshutdown_warning == 0) {
8517c478bd9Sstevel@tonic-gate logerror("Automatic shutdown has been temporarily "
8527c478bd9Sstevel@tonic-gate "suspended in order to preserve the reliability "
8537c478bd9Sstevel@tonic-gate "of this system.");
8547c478bd9Sstevel@tonic-gate log_no_autoshutdown_warning++;
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate ret = 0;
8577c478bd9Sstevel@tonic-gate goto ckdone;
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate
8607c478bd9Sstevel@tonic-gate ckdone:
8617c478bd9Sstevel@tonic-gate if (prom_fd != -1)
86280148899SSurya Prakki (void) close(prom_fd);
8637c478bd9Sstevel@tonic-gate return (ret);
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate
8667c478bd9Sstevel@tonic-gate static void
check_idleness(time_t * now,hrtime_t * hr_now)8677c478bd9Sstevel@tonic-gate check_idleness(time_t *now, hrtime_t *hr_now)
8687c478bd9Sstevel@tonic-gate {
8697c478bd9Sstevel@tonic-gate
8707c478bd9Sstevel@tonic-gate /*
8717c478bd9Sstevel@tonic-gate * Check idleness only when autoshutdown is enabled.
8727c478bd9Sstevel@tonic-gate */
8737c478bd9Sstevel@tonic-gate if (!autoshutdown_en) {
8747c478bd9Sstevel@tonic-gate checkidle_time = 0;
8757c478bd9Sstevel@tonic-gate return;
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate
8787c478bd9Sstevel@tonic-gate info->pd_ttychars_idle = check_tty(hr_now, asinfo.ttychars_thold);
8797c478bd9Sstevel@tonic-gate info->pd_loadaverage_idle =
8807c478bd9Sstevel@tonic-gate check_load_ave(hr_now, asinfo.loadaverage_thold);
8817c478bd9Sstevel@tonic-gate info->pd_diskreads_idle = check_disks(hr_now, asinfo.diskreads_thold);
8827c478bd9Sstevel@tonic-gate info->pd_nfsreqs_idle = check_nfs(hr_now, asinfo.nfsreqs_thold);
8837c478bd9Sstevel@tonic-gate
8847c478bd9Sstevel@tonic-gate #ifdef DEBUG
8857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Idle ttychars for %d secs.\n",
8867c478bd9Sstevel@tonic-gate info->pd_ttychars_idle);
8877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Idle loadaverage for %d secs.\n",
8887c478bd9Sstevel@tonic-gate info->pd_loadaverage_idle);
8897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Idle diskreads for %d secs.\n",
8907c478bd9Sstevel@tonic-gate info->pd_diskreads_idle);
8917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Idle nfsreqs for %d secs.\n",
8927c478bd9Sstevel@tonic-gate info->pd_nfsreqs_idle);
8937c478bd9Sstevel@tonic-gate #endif
8947c478bd9Sstevel@tonic-gate
8957c478bd9Sstevel@tonic-gate checkidle_time = *now + IDLECHK_INTERVAL;
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate
8987c478bd9Sstevel@tonic-gate static int
last_system_activity(hrtime_t * hr_now)8997c478bd9Sstevel@tonic-gate last_system_activity(hrtime_t *hr_now)
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate int act_idle, latest;
9027c478bd9Sstevel@tonic-gate
9037c478bd9Sstevel@tonic-gate latest = info->pd_idle_time * 60;
9047c478bd9Sstevel@tonic-gate act_idle = last_tty_activity(hr_now, asinfo.ttychars_thold);
9057c478bd9Sstevel@tonic-gate latest = MIN(latest, act_idle);
9067c478bd9Sstevel@tonic-gate act_idle = last_load_ave_activity(hr_now);
9077c478bd9Sstevel@tonic-gate latest = MIN(latest, act_idle);
9087c478bd9Sstevel@tonic-gate act_idle = last_disk_activity(hr_now, asinfo.diskreads_thold);
9097c478bd9Sstevel@tonic-gate latest = MIN(latest, act_idle);
9107c478bd9Sstevel@tonic-gate act_idle = last_nfs_activity(hr_now, asinfo.nfsreqs_thold);
9117c478bd9Sstevel@tonic-gate latest = MIN(latest, act_idle);
9127c478bd9Sstevel@tonic-gate
9137c478bd9Sstevel@tonic-gate return (latest);
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate
9167c478bd9Sstevel@tonic-gate static int
run_idlecheck()9177c478bd9Sstevel@tonic-gate run_idlecheck()
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate char pm_variable[LLEN];
9207c478bd9Sstevel@tonic-gate char *cp;
9217c478bd9Sstevel@tonic-gate int status;
9227c478bd9Sstevel@tonic-gate pid_t child;
9237c478bd9Sstevel@tonic-gate
9247c478bd9Sstevel@tonic-gate /*
9257c478bd9Sstevel@tonic-gate * Reap any child process which has been left over.
9267c478bd9Sstevel@tonic-gate */
9272df1fe9cSrandyf while (waitpid((pid_t)-1, &status, WNOHANG) > 0)
9282df1fe9cSrandyf ;
9297c478bd9Sstevel@tonic-gate
9307c478bd9Sstevel@tonic-gate /*
9317c478bd9Sstevel@tonic-gate * Execute the user's idlecheck script and set variable PM_IDLETIME.
9327c478bd9Sstevel@tonic-gate * Returned exit value is the idle time in minutes.
9337c478bd9Sstevel@tonic-gate */
9347c478bd9Sstevel@tonic-gate if ((child = fork1()) == 0) {
9357c478bd9Sstevel@tonic-gate (void) sprintf(pm_variable, "PM_IDLETIME=%d",
9367c478bd9Sstevel@tonic-gate info->pd_idle_time);
9377c478bd9Sstevel@tonic-gate (void) putenv(pm_variable);
9387c478bd9Sstevel@tonic-gate cp = strrchr(asinfo.idlecheck_path, '/');
9397c478bd9Sstevel@tonic-gate if (cp == NULL)
9407c478bd9Sstevel@tonic-gate cp = asinfo.idlecheck_path;
9417c478bd9Sstevel@tonic-gate else
9427c478bd9Sstevel@tonic-gate cp++;
9437c478bd9Sstevel@tonic-gate (void) execl(asinfo.idlecheck_path, cp, NULL);
9447c478bd9Sstevel@tonic-gate exit(-1);
9457c478bd9Sstevel@tonic-gate } else if (child == -1) {
9467c478bd9Sstevel@tonic-gate return (info->pd_idle_time * 60);
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate
9497c478bd9Sstevel@tonic-gate /*
9507c478bd9Sstevel@tonic-gate * Wait until the idlecheck program completes.
9517c478bd9Sstevel@tonic-gate */
9527c478bd9Sstevel@tonic-gate if (waitpid(child, &status, 0) != child) {
9537c478bd9Sstevel@tonic-gate /*
9547c478bd9Sstevel@tonic-gate * We get here if the calling process gets a signal.
9557c478bd9Sstevel@tonic-gate */
9567c478bd9Sstevel@tonic-gate return (info->pd_idle_time * 60);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate
9597c478bd9Sstevel@tonic-gate if (WEXITSTATUS(status) < 0) {
9607c478bd9Sstevel@tonic-gate return (info->pd_idle_time * 60);
9617c478bd9Sstevel@tonic-gate } else {
9627c478bd9Sstevel@tonic-gate return (WEXITSTATUS(status) * 60);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate
9667c478bd9Sstevel@tonic-gate static void
set_alarm(time_t now)9677c478bd9Sstevel@tonic-gate set_alarm(time_t now)
9687c478bd9Sstevel@tonic-gate {
9697c478bd9Sstevel@tonic-gate time_t itime, stime, next_time, max_time;
9707c478bd9Sstevel@tonic-gate int next_alarm;
9717c478bd9Sstevel@tonic-gate
9727c478bd9Sstevel@tonic-gate max_time = MAX(checkidle_time, shutdown_time);
9737c478bd9Sstevel@tonic-gate if (max_time == 0) {
9747c478bd9Sstevel@tonic-gate (void) alarm(0);
9757c478bd9Sstevel@tonic-gate return;
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate itime = (checkidle_time == 0) ? max_time : checkidle_time;
9787c478bd9Sstevel@tonic-gate stime = (shutdown_time == 0) ? max_time : shutdown_time;
9797c478bd9Sstevel@tonic-gate next_time = MIN(itime, stime);
9807c478bd9Sstevel@tonic-gate next_alarm = (next_time <= now) ? 1 : (next_time - now);
9817c478bd9Sstevel@tonic-gate (void) alarm(next_alarm);
9827c478bd9Sstevel@tonic-gate
9837c478bd9Sstevel@tonic-gate #ifdef DEBUG
9847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Currently @ %s", ctime(&now));
9857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Checkidle in %d secs\n", checkidle_time - now);
9867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Shutdown in %d secs\n", shutdown_time - now);
9877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Next alarm goes off in %d secs\n", next_alarm);
9887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "************************************\n");
9897c478bd9Sstevel@tonic-gate #endif
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate
9927c478bd9Sstevel@tonic-gate static int
poweroff(const char * msg,char ** cmd_argv)993c42872d4Smh27603 poweroff(const char *msg, char **cmd_argv)
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate struct stat statbuf;
9967c478bd9Sstevel@tonic-gate pid_t pid, child;
9977c478bd9Sstevel@tonic-gate struct passwd *pwd;
9987c478bd9Sstevel@tonic-gate char *home, *user;
9997c478bd9Sstevel@tonic-gate char ehome[] = "HOME=";
10007c478bd9Sstevel@tonic-gate char euser[] = "LOGNAME=";
10017c478bd9Sstevel@tonic-gate int status;
10027c478bd9Sstevel@tonic-gate char **ca;
10037c478bd9Sstevel@tonic-gate
10047c478bd9Sstevel@tonic-gate if (mutex_trylock(&poweroff_mutex) != 0)
10057c478bd9Sstevel@tonic-gate return (0);
10067c478bd9Sstevel@tonic-gate
10077c478bd9Sstevel@tonic-gate if (stat("/dev/console", &statbuf) == -1 ||
10087c478bd9Sstevel@tonic-gate (pwd = getpwuid(statbuf.st_uid)) == NULL) {
100980148899SSurya Prakki (void) mutex_unlock(&poweroff_mutex);
10107c478bd9Sstevel@tonic-gate return (1);
10117c478bd9Sstevel@tonic-gate }
10127c478bd9Sstevel@tonic-gate
10137c478bd9Sstevel@tonic-gate if (msg)
10147c478bd9Sstevel@tonic-gate syslog(LOG_NOTICE, msg);
10157c478bd9Sstevel@tonic-gate
10167c478bd9Sstevel@tonic-gate if (*cmd_argv == NULL) {
10177c478bd9Sstevel@tonic-gate logerror("No command to run.");
101880148899SSurya Prakki (void) mutex_unlock(&poweroff_mutex);
10197c478bd9Sstevel@tonic-gate return (1);
10207c478bd9Sstevel@tonic-gate }
10217c478bd9Sstevel@tonic-gate
10227c478bd9Sstevel@tonic-gate home = malloc(strlen(pwd->pw_dir) + sizeof (ehome));
10237c478bd9Sstevel@tonic-gate user = malloc(strlen(pwd->pw_name) + sizeof (euser));
10247c478bd9Sstevel@tonic-gate if (home == NULL || user == NULL) {
10257c478bd9Sstevel@tonic-gate free(home);
10267c478bd9Sstevel@tonic-gate free(user);
10277c478bd9Sstevel@tonic-gate logerror("No memory.");
102880148899SSurya Prakki (void) mutex_unlock(&poweroff_mutex);
10297c478bd9Sstevel@tonic-gate return (1);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate (void) strcpy(home, ehome);
10327c478bd9Sstevel@tonic-gate (void) strcat(home, pwd->pw_dir);
10337c478bd9Sstevel@tonic-gate (void) strcpy(user, euser);
10347c478bd9Sstevel@tonic-gate (void) strcat(user, pwd->pw_name);
10357c478bd9Sstevel@tonic-gate
10367c478bd9Sstevel@tonic-gate /*
10377c478bd9Sstevel@tonic-gate * Need to simulate the user enviroment, minimaly set HOME, and USER.
10387c478bd9Sstevel@tonic-gate */
10397c478bd9Sstevel@tonic-gate if ((child = fork1()) == 0) {
10407c478bd9Sstevel@tonic-gate (void) putenv(home);
10417c478bd9Sstevel@tonic-gate (void) putenv(user);
10427c478bd9Sstevel@tonic-gate (void) setgid(pwd->pw_gid);
10437c478bd9Sstevel@tonic-gate (void) setuid(pwd->pw_uid);
10447c478bd9Sstevel@tonic-gate
10457c478bd9Sstevel@tonic-gate /*
10467c478bd9Sstevel@tonic-gate * check for shutdown flag and set environment
10477c478bd9Sstevel@tonic-gate */
10487c478bd9Sstevel@tonic-gate for (ca = cmd_argv; *ca; ca++) {
10497c478bd9Sstevel@tonic-gate if (strcmp("-h", *ca) == 0) {
10507c478bd9Sstevel@tonic-gate (void) putenv("SYSSUSPENDDODEFAULT=");
10517c478bd9Sstevel@tonic-gate break;
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate
10557c478bd9Sstevel@tonic-gate (void) execv(cmd_argv[0], cmd_argv);
10567c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
10577c478bd9Sstevel@tonic-gate } else {
10587c478bd9Sstevel@tonic-gate free(home);
10597c478bd9Sstevel@tonic-gate free(user);
10607c478bd9Sstevel@tonic-gate if (child == -1) {
106180148899SSurya Prakki (void) mutex_unlock(&poweroff_mutex);
10627c478bd9Sstevel@tonic-gate return (1);
10637c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate pid = 0;
10667c478bd9Sstevel@tonic-gate while (pid != child)
10677c478bd9Sstevel@tonic-gate pid = wait(&status);
10687c478bd9Sstevel@tonic-gate if (WEXITSTATUS(status)) {
10697c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR, "Failed to exec \"%s\".", cmd_argv[0]);
107080148899SSurya Prakki (void) mutex_unlock(&poweroff_mutex);
10717c478bd9Sstevel@tonic-gate return (1);
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate
107480148899SSurya Prakki (void) mutex_unlock(&poweroff_mutex);
10757c478bd9Sstevel@tonic-gate return (0);
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate
10787c478bd9Sstevel@tonic-gate #define PBUFSIZE 256
10797c478bd9Sstevel@tonic-gate
10807c478bd9Sstevel@tonic-gate /*
10817c478bd9Sstevel@tonic-gate * Gets the value of a prom property at either root or options node. It
10827c478bd9Sstevel@tonic-gate * returns 1 if it is successful, otherwise it returns 0 .
10837c478bd9Sstevel@tonic-gate */
10847c478bd9Sstevel@tonic-gate static int
get_prom(int prom_fd,prom_node_t node_name,char * property_name,char * property_value,size_t len)10857c478bd9Sstevel@tonic-gate get_prom(int prom_fd, prom_node_t node_name,
10867c478bd9Sstevel@tonic-gate char *property_name, char *property_value, size_t len)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate union {
10897c478bd9Sstevel@tonic-gate char buf[PBUFSIZE + sizeof (uint_t)];
10907c478bd9Sstevel@tonic-gate struct openpromio opp;
10917c478bd9Sstevel@tonic-gate } oppbuf;
10927c478bd9Sstevel@tonic-gate register struct openpromio *opp = &(oppbuf.opp);
10937c478bd9Sstevel@tonic-gate int got_it = 0;
10947c478bd9Sstevel@tonic-gate
10957c478bd9Sstevel@tonic-gate if (prom_fd == -1) {
10967c478bd9Sstevel@tonic-gate return (0);
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate
10997c478bd9Sstevel@tonic-gate switch (node_name) {
11007c478bd9Sstevel@tonic-gate case root:
11017c478bd9Sstevel@tonic-gate (void *) memset(oppbuf.buf, 0, PBUFSIZE);
11027c478bd9Sstevel@tonic-gate opp->oprom_size = PBUFSIZE;
11037c478bd9Sstevel@tonic-gate if (ioctl(prom_fd, OPROMNEXT, opp) < 0) {
11047c478bd9Sstevel@tonic-gate return (0);
11057c478bd9Sstevel@tonic-gate }
11067c478bd9Sstevel@tonic-gate
11077c478bd9Sstevel@tonic-gate /*
11087c478bd9Sstevel@tonic-gate * Passing null string will give us the first property.
11097c478bd9Sstevel@tonic-gate */
11107c478bd9Sstevel@tonic-gate (void *) memset(oppbuf.buf, 0, PBUFSIZE);
11117c478bd9Sstevel@tonic-gate do {
11127c478bd9Sstevel@tonic-gate opp->oprom_size = PBUFSIZE;
11137c478bd9Sstevel@tonic-gate if (ioctl(prom_fd, OPROMNXTPROP, opp) < 0) {
11147c478bd9Sstevel@tonic-gate return (0);
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate if (strcmp(opp->oprom_array, property_name) == 0) {
11177c478bd9Sstevel@tonic-gate got_it++;
11187c478bd9Sstevel@tonic-gate break;
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate } while (opp->oprom_size > 0);
11217c478bd9Sstevel@tonic-gate
11227c478bd9Sstevel@tonic-gate if (!got_it) {
11237c478bd9Sstevel@tonic-gate return (0);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate if (got_it && property_value == NULL) {
11267c478bd9Sstevel@tonic-gate return (1);
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate opp->oprom_size = PBUFSIZE;
11297c478bd9Sstevel@tonic-gate if (ioctl(prom_fd, OPROMGETPROP, opp) < 0) {
11307c478bd9Sstevel@tonic-gate return (0);
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate if (opp->oprom_size == 0) {
11337c478bd9Sstevel@tonic-gate *property_value = '\0';
11347c478bd9Sstevel@tonic-gate } else {
11357c478bd9Sstevel@tonic-gate estrcpy(property_value, opp->oprom_array, len);
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate break;
11387c478bd9Sstevel@tonic-gate case options:
11397c478bd9Sstevel@tonic-gate estrcpy(opp->oprom_array, property_name, PBUFSIZE);
11407c478bd9Sstevel@tonic-gate opp->oprom_size = PBUFSIZE;
11417c478bd9Sstevel@tonic-gate if (ioctl(prom_fd, OPROMGETOPT, opp) < 0) {
11427c478bd9Sstevel@tonic-gate return (0);
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate if (opp->oprom_size == 0) {
11457c478bd9Sstevel@tonic-gate return (0);
11467c478bd9Sstevel@tonic-gate }
11477c478bd9Sstevel@tonic-gate if (property_value != NULL) {
11487c478bd9Sstevel@tonic-gate estrcpy(property_value, opp->oprom_array, len);
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate break;
11517c478bd9Sstevel@tonic-gate default:
11527c478bd9Sstevel@tonic-gate logerror("Only root node and options node are supported.\n");
11537c478bd9Sstevel@tonic-gate return (0);
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate
11567c478bd9Sstevel@tonic-gate return (1);
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate
11597c478bd9Sstevel@tonic-gate #define isspace(ch) ((ch) == ' ' || (ch) == '\t')
11607c478bd9Sstevel@tonic-gate #define iseol(ch) ((ch) == '\n' || (ch) == '\r' || (ch) == '\f')
11617c478bd9Sstevel@tonic-gate
11627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11637c478bd9Sstevel@tonic-gate static void
power_button_monitor(void * arg)11647c478bd9Sstevel@tonic-gate power_button_monitor(void *arg)
11657c478bd9Sstevel@tonic-gate {
11667c478bd9Sstevel@tonic-gate struct pollfd pfd;
1167d2ec54f7Sphitran int events, ret;
11687c478bd9Sstevel@tonic-gate
11697c478bd9Sstevel@tonic-gate if (ioctl(pb_fd, PB_BEGIN_MONITOR, NULL) == -1) {
11707c478bd9Sstevel@tonic-gate logerror("Failed to monitor the power button.");
11717c478bd9Sstevel@tonic-gate thr_exit((void *) 0);
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate
11747c478bd9Sstevel@tonic-gate pfd.fd = pb_fd;
11757c478bd9Sstevel@tonic-gate pfd.events = POLLIN;
11767c478bd9Sstevel@tonic-gate
11777c478bd9Sstevel@tonic-gate /*CONSTCOND*/
11787c478bd9Sstevel@tonic-gate while (1) {
11797c478bd9Sstevel@tonic-gate if (poll(&pfd, 1, INFTIM) == -1) {
11807c478bd9Sstevel@tonic-gate logerror("Failed to poll for power button events.");
11817c478bd9Sstevel@tonic-gate thr_exit((void *) 0);
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate
11847c478bd9Sstevel@tonic-gate if (!(pfd.revents & POLLIN))
11857c478bd9Sstevel@tonic-gate continue;
11867c478bd9Sstevel@tonic-gate
1187d2ec54f7Sphitran /*
1188d2ec54f7Sphitran * Monitor the power button, but only take action if
1189d2ec54f7Sphitran * gnome-power-manager is not running.
1190d2ec54f7Sphitran *
1191d2ec54f7Sphitran * ret greater than 0 means could not find process.
1192d2ec54f7Sphitran */
11933f14f381SPhi Tran ret = system("/usr/bin/pgrep -fx gnome-power-manager");
1194d2ec54f7Sphitran
11957c478bd9Sstevel@tonic-gate if (ioctl(pfd.fd, PB_GET_EVENTS, &events) == -1) {
11967c478bd9Sstevel@tonic-gate logerror("Failed to get power button events.");
11977c478bd9Sstevel@tonic-gate thr_exit((void *) 0);
11987c478bd9Sstevel@tonic-gate }
11997c478bd9Sstevel@tonic-gate
1200d2ec54f7Sphitran if ((ret > 0) && (events & PB_BUTTON_PRESS) &&
12017c478bd9Sstevel@tonic-gate (poweroff(NULL, power_button_cmd) != 0)) {
12027c478bd9Sstevel@tonic-gate logerror("Power button is pressed, powering "
12037c478bd9Sstevel@tonic-gate "down the system!");
12047c478bd9Sstevel@tonic-gate
12057c478bd9Sstevel@tonic-gate /*
12067c478bd9Sstevel@tonic-gate * Send SIGPWR signal to the init process to
12077c478bd9Sstevel@tonic-gate * shut down the system.
12087c478bd9Sstevel@tonic-gate */
12097c478bd9Sstevel@tonic-gate if (kill(1, SIGPWR) == -1)
12107c478bd9Sstevel@tonic-gate (void) uadmin(A_SHUTDOWN, AD_POWEROFF, 0);
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate
12137c478bd9Sstevel@tonic-gate /*
12147c478bd9Sstevel@tonic-gate * Clear any power button event that has happened
12157c478bd9Sstevel@tonic-gate * meanwhile we were busy processing the last one.
12167c478bd9Sstevel@tonic-gate */
12177c478bd9Sstevel@tonic-gate if (ioctl(pfd.fd, PB_GET_EVENTS, &events) == -1) {
12187c478bd9Sstevel@tonic-gate logerror("Failed to get power button events.");
12197c478bd9Sstevel@tonic-gate thr_exit((void *) 0);
12207c478bd9Sstevel@tonic-gate }
12217c478bd9Sstevel@tonic-gate }
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate
12247c478bd9Sstevel@tonic-gate static void
do_attach(void)12257c478bd9Sstevel@tonic-gate do_attach(void)
12267c478bd9Sstevel@tonic-gate {
12277c478bd9Sstevel@tonic-gate if (read_cpr_config() < 0)
12287c478bd9Sstevel@tonic-gate return;
12297c478bd9Sstevel@tonic-gate
12307c478bd9Sstevel@tonic-gate /*
12317c478bd9Sstevel@tonic-gate * If autopm behavior is explicitly enabled for energystar-v2, or
12327c478bd9Sstevel@tonic-gate * set to default for energystar-v3, create a new thread to attach
12337c478bd9Sstevel@tonic-gate * all devices.
12347c478bd9Sstevel@tonic-gate */
12357c478bd9Sstevel@tonic-gate estar_v3_prop = asinfo.is_autopm_default;
12367c478bd9Sstevel@tonic-gate if ((strcmp(asinfo.apm_behavior, "enable") == 0) ||
12377c478bd9Sstevel@tonic-gate (estar_v3_prop && strcmp(asinfo.apm_behavior, "default") == 0)) {
12382df1fe9cSrandyf if (powerd_debug)
12392df1fe9cSrandyf logerror("powerd starting device attach thread.");
12407c478bd9Sstevel@tonic-gate if (thr_create(NULL, NULL, attach_devices, NULL,
12417c478bd9Sstevel@tonic-gate THR_DAEMON, NULL) != 0) {
12427c478bd9Sstevel@tonic-gate logerror("Unable to create thread to attach devices.");
12437c478bd9Sstevel@tonic-gate }
12447c478bd9Sstevel@tonic-gate }
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate
12477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12487c478bd9Sstevel@tonic-gate static void *
attach_devices(void * arg)12497c478bd9Sstevel@tonic-gate attach_devices(void *arg)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate di_node_t root_node;
12527c478bd9Sstevel@tonic-gate
125380148899SSurya Prakki (void) sleep(60); /* let booting finish first */
12547c478bd9Sstevel@tonic-gate
12557c478bd9Sstevel@tonic-gate if ((root_node = di_init("/", DINFOFORCE)) == DI_NODE_NIL) {
12567c478bd9Sstevel@tonic-gate logerror("Failed to attach devices.");
12577c478bd9Sstevel@tonic-gate return (NULL);
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate di_fini(root_node);
12607c478bd9Sstevel@tonic-gate
12617c478bd9Sstevel@tonic-gate /*
12627c478bd9Sstevel@tonic-gate * Unload all the modules.
12637c478bd9Sstevel@tonic-gate */
12647c478bd9Sstevel@tonic-gate (void) modctl(MODUNLOAD, 0);
12657c478bd9Sstevel@tonic-gate
12667c478bd9Sstevel@tonic-gate return (NULL);
12677c478bd9Sstevel@tonic-gate }
12687c478bd9Sstevel@tonic-gate
12697c478bd9Sstevel@tonic-gate
12707c478bd9Sstevel@tonic-gate /*
12717c478bd9Sstevel@tonic-gate * Create a file which will contain our pid. Pmconfig will check this file
12727c478bd9Sstevel@tonic-gate * to see if we are running and can use the pid to signal us. Returns the
12737c478bd9Sstevel@tonic-gate * file descriptor if successful, -1 otherwise.
12747c478bd9Sstevel@tonic-gate *
12757c478bd9Sstevel@tonic-gate * Note: Deal with attempt to launch multiple instances and also with existence
12767c478bd9Sstevel@tonic-gate * of an obsolete pid file caused by an earlier abort.
12777c478bd9Sstevel@tonic-gate */
12787c478bd9Sstevel@tonic-gate static int
open_pidfile(char * me)12797c478bd9Sstevel@tonic-gate open_pidfile(char *me)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate int fd;
1282c42872d4Smh27603 const char *e1 = "%s: Cannot open pid file for read: ";
1283c42872d4Smh27603 const char *e2 = "%s: Cannot unlink obsolete pid file: ";
1284c3a64150SMargot Miller const char *e3 = "%s: Either another daemon is running or the"
1285c3a64150SMargot Miller " process is defunct (pid %d). \n";
1286c3a64150SMargot Miller const char *e4 = "%s: Cannot create pid file: ";
12877c478bd9Sstevel@tonic-gate
12887c478bd9Sstevel@tonic-gate again:
12897c478bd9Sstevel@tonic-gate if ((fd = open(pidpath, O_CREAT | O_EXCL | O_WRONLY, 0444)) == -1) {
12907c478bd9Sstevel@tonic-gate if (errno == EEXIST) {
12917c478bd9Sstevel@tonic-gate FILE *fp;
12927c478bd9Sstevel@tonic-gate pid_t pid;
12937c478bd9Sstevel@tonic-gate
12947c478bd9Sstevel@tonic-gate if ((fp = fopen(pidpath, "r")) == NULL) {
12957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, e1, me);
12967c478bd9Sstevel@tonic-gate perror(NULL);
12977c478bd9Sstevel@tonic-gate return (-1);
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate
13007c478bd9Sstevel@tonic-gate /* Read the pid */
13017c478bd9Sstevel@tonic-gate pid = (pid_t)-1;
13027c478bd9Sstevel@tonic-gate (void) fscanf(fp, "%ld", &pid);
13037c478bd9Sstevel@tonic-gate (void) fclose(fp);
13047c478bd9Sstevel@tonic-gate if (pid == -1) {
13057c478bd9Sstevel@tonic-gate if (unlink(pidpath) == -1) {
13067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, e2, me);
13077c478bd9Sstevel@tonic-gate perror(NULL);
13087c478bd9Sstevel@tonic-gate return (-1);
13097c478bd9Sstevel@tonic-gate } else /* try without corrupted file */
13107c478bd9Sstevel@tonic-gate goto again;
13117c478bd9Sstevel@tonic-gate }
13127c478bd9Sstevel@tonic-gate
1313c3a64150SMargot Miller /* Is pid for a running process */
1314c3a64150SMargot Miller if (kill(pid, 0) == -1) {
1315c3a64150SMargot Miller if (errno == ESRCH) {
13167c478bd9Sstevel@tonic-gate if (unlink(pidpath) == -1) {
13177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, e2, me);
13187c478bd9Sstevel@tonic-gate perror(NULL);
13197c478bd9Sstevel@tonic-gate return (-1);
13207c478bd9Sstevel@tonic-gate } else /* try without obsolete file */
13217c478bd9Sstevel@tonic-gate goto again;
13227c478bd9Sstevel@tonic-gate }
1323c3a64150SMargot Miller } else { /* powerd deamon still running or defunct */
13247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, e3, me, pid);
13257c478bd9Sstevel@tonic-gate return (-1);
13267c478bd9Sstevel@tonic-gate }
1327c3a64150SMargot Miller
13287c478bd9Sstevel@tonic-gate } else { /* create failure not due to existing file */
1329c3a64150SMargot Miller (void) fprintf(stderr, e4, me);
13307c478bd9Sstevel@tonic-gate perror(NULL);
13317c478bd9Sstevel@tonic-gate return (-1);
13327c478bd9Sstevel@tonic-gate }
13337c478bd9Sstevel@tonic-gate }
13347c478bd9Sstevel@tonic-gate
13357c478bd9Sstevel@tonic-gate (void) fchown(fd, (uid_t)-1, (gid_t)0);
13367c478bd9Sstevel@tonic-gate return (fd);
13377c478bd9Sstevel@tonic-gate }
13387c478bd9Sstevel@tonic-gate
13397c478bd9Sstevel@tonic-gate /*
13407c478bd9Sstevel@tonic-gate * Write a pid to the pid file. Report errors to syslog.
13417c478bd9Sstevel@tonic-gate *
13427c478bd9Sstevel@tonic-gate */
13437c478bd9Sstevel@tonic-gate static int
write_pidfile(int fd,pid_t pid)13447c478bd9Sstevel@tonic-gate write_pidfile(int fd, pid_t pid)
13457c478bd9Sstevel@tonic-gate {
13467c478bd9Sstevel@tonic-gate int len;
13477c478bd9Sstevel@tonic-gate int rc = 0; /* assume success */
13487c478bd9Sstevel@tonic-gate
13497c478bd9Sstevel@tonic-gate len = sprintf(scratch, "%ld\n", pid);
13507c478bd9Sstevel@tonic-gate if (write(fd, scratch, len) != len) {
13517c478bd9Sstevel@tonic-gate logerror("Cannot write pid file: %s", strerror(errno));
13527c478bd9Sstevel@tonic-gate rc = -1;
13537c478bd9Sstevel@tonic-gate }
13547c478bd9Sstevel@tonic-gate
13557c478bd9Sstevel@tonic-gate return (rc);
13567c478bd9Sstevel@tonic-gate }
1357