powerd.c revision 13c646d3d089ec01b47ac30d9629a24d22e14053
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2016 Nexenta Systems, Inc. All rights reserved.
*/
#include <stdio.h> /* Standard */
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <pwd.h>
#include <dirent.h>
#include <thread.h>
#include <limits.h>
#include <signal.h> /* signal handling */
#include <syslog.h>
#include <unistd.h>
#include <libdevinfo.h>
#include <poll.h>
#include <stdarg.h>
#include "powerd.h"
/* External Functions */
extern void sysstat_init(void);
extern int check_disks(hrtime_t *, int);
extern int check_load_ave(hrtime_t *, float);
extern int last_disk_activity(hrtime_t *, int);
extern int last_tty_activity(hrtime_t *, int);
extern int last_load_ave_activity(hrtime_t *);
extern int last_nfs_activity(hrtime_t *, int);
#define PB "/dev/power_button"
#define LOGFILE "./powerd.log"
#define PBM_THREAD 0
#define ATTACH_THREAD 1
#define NUM_THREADS 2
#define CHECK_INTERVAL 5
#define IDLECHK_INTERVAL 15
#define MINS_TO_SECS 60
#define HOURS_TO_MINS 60
#define DEFAULT_POWER_CYCLE_LIMIT 10000
#define LLEN 80
/* State Variables */
static time_t last_resume;
static int pb_fd; /* power button driver */
static int broadcast; /* Enables syslog messages */
static int start_calc;
static int autoshutdown_en;
static int do_idlecheck;
static int got_sighup;
static int estar_v2_prop;
static int estar_v3_prop;
static int log_power_cycles_error = 0;
static int log_system_board_date_error = 0;
static int log_no_autoshutdown_warning = 0;
static mutex_t poweroff_mutex;
static char *autoshutdown_cmd[] = {
"/usr/bin/sys-suspend",
};
static char *power_button_cmd[] = {
"/usr/bin/sys-suspend",
};
#ifdef __x86
static char *autoS3_cmd[] = {
"/usr/bin/sys-suspend",
};
#endif
static char *prog;
/* Local Functions */
static void alarm_handler(int);
static void thaw_handler(int);
static void kill_handler(int);
static void work_handler(int);
static int last_system_activity(hrtime_t *);
static int run_idlecheck(void);
static int poweroff(const char *, char **);
static int is_ok2shutdown(time_t *);
static void power_button_monitor(void *);
static int open_pidfile(char *);
static int write_pidfile(int, pid_t);
static int read_cpr_config(void);
static void system_activity_monitor(void);
#ifdef __x86
static void autos3_monitor(void);
#endif
static void do_attach(void);
static void *attach_devices(void *);
static int powerd_debug;
/* PRINTFLIKE1 */
static void
{
if (broadcast)
}
static void
{
logerror("%s: string too long \"%s ...\"\n"
}
}
int
{
int pm_fd;
int c;
int pid_fd;
if (geteuid() != 0) {
}
/*
* Process options
*/
broadcast = 1;
switch (c) {
case 'd':
powerd_debug = 1;
break;
case 'n':
broadcast = 0;
break;
case '?':
}
}
if (pm_fd == -1) {
}
/*
* Initialize mutex lock used to insure only one command to
* run at a time.
*/
"%s: Unable to initialize mutex lock\n", prog);
}
}
/*
* Daemon is set to go...
*/
else if (pid != 0)
/*
* Close all the parent's file descriptors (Bug 1225843).
*/
closefrom(0);
(void) setsid();
(void) chdir("/");
(void) umask(0);
#ifdef DEBUG
/*
* Connect stdout to the console.
*/
logerror("Unable to connect to the console.");
}
#endif
info->pd_start_time = 0;
info->pd_finish_time = 0;
/*
* Allow SIGQUIT, SIGINT and SIGTERM signals to terminate us
* any time
*/
(void) sigfillset(&sigmask);
/*
* If "power_button" device node can be opened, create a new
* thread to monitor the power button.
*/
if (powerd_debug)
logerror("powerd starting power button monitor.");
(void *(*)(void *))power_button_monitor, NULL,
THR_DAEMON, NULL) != 0) {
logerror("Unable to monitor system's power button.");
}
}
do_attach();
/*
* Create a new thread to monitor system activity and suspend
* system if idle.
*/
if (powerd_debug)
logerror("powerd starting system activity monitor.");
(void *(*)(void *))system_activity_monitor, NULL,
THR_DAEMON, NULL) != 0) {
logerror("Unable to create thread to monitor system activity.");
}
#ifdef __x86
/*
* Create a new thread to handle autos3 trigger
*/
if (powerd_debug)
logerror("powerd starting autos3 monitor.");
logerror("Unable to create thread to monitor autos3 activity.");
}
#endif
/*
* Block until we receive an explicit terminate signal
*/
(void) sigsuspend(&sigmask);
return (1);
}
static void
system_activity_monitor(void)
{
/*
* Setup for gathering system's statistic.
*/
sysstat_init();
/*
* In addition to the SIGQUIT, SIGINT and SIGTERM signals already
* being handled, this thread also needs to handle SIGHUP, SIGALRM
* and SIGTHAW signals.
*/
/*
* Invoke work_handler with a dummy SIGHUP signal to read
* cpr config file, get autoshutdown properties and schedule
* an alarm if needed.
*/
/*
* Wait for signal to read file
*/
(void) thr_sigsetmask(0, 0, &sigmask);
do {
(void) sigsuspend(&sigmask);
}
#ifdef __x86
static void
autos3_monitor(void)
{
if (fd == -1) {
}
/*
* Tell device we want the special sauce
*/
if (ret == -1) {
}
/*CONSTCOND*/
while (1) {
switch (errno) {
case EINTR:
case EAGAIN:
continue;
default:
}
}
if (ret == -1) {
}
case 3: /* S3 */
if (powerd_debug)
logerror("ioctl returns type: %d",
break;
default:
logerror("Unsupported target state %d",
continue;
}
continue;
}
}
#endif
static int
read_cpr_config(void)
{
int asfd;
return (-1);
}
return (-1);
}
return (0);
}
/*ARGSUSED*/
static void
thaw_handler(int sig)
{
start_calc = 0;
}
/*ARGSUSED*/
static void
kill_handler(int sig)
{
int ret_code = EXIT_SUCCESS;
/*
* Free resources
*/
if (pb_fd != -1) {
}
(void) mutex_destroy(&poweroff_mutex);
closelog();
}
/*ARGSUSED*/
static void
alarm_handler(int sig)
{
}
/*ARGSUSED*/
static void
work_handler(int sig)
{
do_idlecheck = 0;
/*
* Parse the config file for autoshutdown and idleness entries.
*/
if (read_cpr_config() < 0)
return;
/*
* Since Oct. 1, 1995, any new system shipped had root
* property "energystar-v2" defined in its prom. Systems
* shipped after July 1, 1999, will have "energystar-v3"
* property.
*/
logerror("unable to access idlecheck program \"%s\".",
logerror("idlecheck program \"%s\" is not executable.",
} else {
do_idlecheck = 1;
}
}
info->pd_autoshutdown = 0;
} else {
logerror("autoshutdown behavior \"%s\" unrecognized.",
info->pd_autoshutdown = 0;
}
if (info->pd_autoshutdown) {
}
? 1 : 0;
#ifdef DEBUG
"pd_autoresume = %d\n",
#endif
got_sighup = 1;
}
static void
{
int tod_fd = -1;
int next_time;
int s, f;
extern long conskbd_idle_time(void);
extern long consms_idle_time(void);
if (!autoshutdown_en) {
shutdown_time = 0;
return;
}
if ((mouse = (int)consms_idle_time()) < 0) {
if (! warned_ms) {
warned_ms = 1;
logerror("powerd: failed to get "
"idle time for console mouse");
}
return;
}
if ((kbd = (int)conskbd_idle_time()) < 0) {
if (! warned_kbd) {
warned_kbd = 1;
logerror("powerd: failed to get "
"idle time for console keyboard");
}
return;
}
/* who is the last to go idle */
/*
* Calculate time_since_last_resume and the next_time
* to auto suspend.
*/
start_calc = 1;
#ifdef DEBUG
#endif
/*
* If we have get the SIGTHAW signal at this point - our
* calculation of time_since_last_resume is wrong so
* - we need to recalculate.
*/
while (start_calc == 0) {
/* need to redo calculation */
start_calc = 1;
}
/*
* Only when everything else is idle, run the user's idlecheck
* script.
*/
if (next_time <= 0 && do_idlecheck) {
got_sighup = 0;
/*
* If we have caught SIGTHAW or SIGHUP, need to
* recalculate.
*/
start_calc = 1;
got_sighup = 0;
}
}
if (next_time <= 0) {
if (is_ok2shutdown(now)) {
/*
* Setup the autowakeup alarm. Clear it
* right after poweroff, just in case if
* shutdown doesn't go through.
*/
if (info->pd_autoresume)
wakeup_time = (*now < f) ? f :
(f + DAYS_TO_SECS);
/*
* A software fix for hardware
* bug 1217415.
*/
"Since autowakeup time is less than 3 minutes away, "
"autoshutdown will not occur.");
return;
}
&wakeup_time) == -1) {
logerror("Unable to program TOD"
" alarm for autowakeup.");
return;
}
}
(void) poweroff("Autoshutdown",
NULL) == -1)
logerror("Unable to clear "
"alarm in TOD device.");
}
/* wait at least 5 mins */
shutdown_time = *now +
} else {
/* wait 5 mins */
}
} else
} else if (s < f && *now >= f) {
shutdown_time = s + DAYS_TO_SECS;
} else
shutdown_time = s;
}
static int
{
int prom_fd = -1;
char power_cycles_st[LLEN];
char power_cycle_limit_st[LLEN];
char system_board_date_st[LLEN];
int no_power_cycles = 0;
int no_system_board_date = 0;
int ret = 1;
/* CONSTCOND */
while (1) {
continue;
break;
}
/*
* when #power-cycles property does not exist
* power cycles are unlimited.
*/
power_cycles_st, sizeof (power_cycles_st)) == 0)
goto ckdone;
power_cycle_limit_st, sizeof (power_cycle_limit_st)) == 0) {
} else {
}
/*
* Allow 10% of power_cycle_limit as free cycles.
*/
if (power_cycles < 0)
else if (power_cycles <= free_cycles)
goto ckdone;
if (no_power_cycles && log_power_cycles_error == 0) {
logerror("Invalid PROM property \"#power-cycles\" was found.");
}
system_board_date_st, sizeof (system_board_date_st)) == 0) {
} else {
if (life_began > *now) {
}
}
if (no_system_board_date) {
if (log_system_board_date_error == 0) {
logerror("No or invalid PROM property "
"\"system-board-date\" was found.");
}
}
/*
* Since we don't keep the date that last free_cycle is ended, we
* need to spread (power_cycle_limit - free_cycles) over the entire
* 7-year life span instead of (lifetime - date free_cycles ended).
*/
(power_cycle_limit - free_cycles));
if (no_power_cycles)
goto ckdone;
#ifdef DEBUG
#endif
if (power_cycles > scaled_cycles) {
if (log_no_autoshutdown_warning == 0) {
logerror("Automatic shutdown has been temporarily "
"suspended in order to preserve the reliability "
"of this system.");
}
ret = 0;
goto ckdone;
}
if (prom_fd != -1)
return (ret);
}
static void
{
/*
* Check idleness only when autoshutdown is enabled.
*/
if (!autoshutdown_en) {
checkidle_time = 0;
return;
}
#ifdef DEBUG
#endif
}
static int
{
return (latest);
}
static int
{
char pm_variable[LLEN];
char *cp;
int status;
/*
* Reap any child process which has been left over.
*/
;
/*
* Execute the user's idlecheck script and set variable PM_IDLETIME.
* Returned exit value is the idle time in minutes.
*/
info->pd_idle_time);
(void) putenv(pm_variable);
else
cp++;
exit(-1);
} else if (child == -1) {
}
/*
* Wait until the idlecheck program completes.
*/
/*
* We get here if the calling process gets a signal.
*/
}
if (WEXITSTATUS(status) < 0) {
} else {
}
}
static void
{
int next_alarm;
if (max_time == 0) {
(void) alarm(0);
return;
}
(void) alarm(next_alarm);
#ifdef DEBUG
#endif
}
static int
{
char ehome[] = "HOME=";
char euser[] = "LOGNAME=";
int status;
char **ca;
if (mutex_trylock(&poweroff_mutex) != 0)
return (0);
(void) mutex_unlock(&poweroff_mutex);
return (1);
}
if (msg)
logerror("No command to run.");
(void) mutex_unlock(&poweroff_mutex);
return (1);
}
logerror("No memory.");
(void) mutex_unlock(&poweroff_mutex);
return (1);
}
/*
* Need to simulate the user enviroment, minimaly set HOME, and USER.
*/
/*
* check for shutdown flag and set environment
*/
(void) putenv("SYSSUSPENDDODEFAULT=");
break;
}
}
} else {
if (child == -1) {
(void) mutex_unlock(&poweroff_mutex);
return (1);
}
}
pid = 0;
if (WEXITSTATUS(status)) {
(void) mutex_unlock(&poweroff_mutex);
return (1);
}
(void) mutex_unlock(&poweroff_mutex);
return (0);
}
#define PBUFSIZE 256
/*
* Gets the value of a prom property at either root or options node. It
* returns 1 if it is successful, otherwise it returns 0 .
*/
static int
{
union {
struct openpromio opp;
} oppbuf;
int got_it = 0;
if (prom_fd == -1) {
return (0);
}
switch (node_name) {
case root:
return (0);
}
/*
* Passing null string will give us the first property.
*/
do {
return (0);
}
got_it++;
break;
}
} while (opp->oprom_size > 0);
if (!got_it) {
return (0);
}
return (1);
}
return (0);
}
if (opp->oprom_size == 0) {
*property_value = '\0';
} else {
}
break;
case options:
return (0);
}
if (opp->oprom_size == 0) {
return (0);
}
if (property_value != NULL) {
}
break;
default:
logerror("Only root node and options node are supported.\n");
return (0);
}
return (1);
}
/*ARGSUSED*/
static void
power_button_monitor(void *arg)
{
logerror("Failed to monitor the power button.");
thr_exit((void *) 0);
}
/*CONSTCOND*/
while (1) {
logerror("Failed to poll for power button events.");
thr_exit((void *) 0);
}
continue;
/*
* Monitor the power button, but only take action if
* gnome-power-manager is not running.
*
* ret greater than 0 means could not find process.
*/
logerror("Failed to get power button events.");
thr_exit((void *) 0);
}
logerror("Power button is pressed, powering "
"down the system!");
/*
* Send SIGPWR signal to the init process to
* shut down the system.
*/
}
/*
* Clear any power button event that has happened
* meanwhile we were busy processing the last one.
*/
logerror("Failed to get power button events.");
thr_exit((void *) 0);
}
}
}
static void
do_attach(void)
{
if (read_cpr_config() < 0)
return;
/*
* If autopm behavior is explicitly enabled for energystar-v2, or
* set to default for energystar-v3, create a new thread to attach
* all devices.
*/
if (powerd_debug)
logerror("powerd starting device attach thread.");
THR_DAEMON, NULL) != 0) {
logerror("Unable to create thread to attach devices.");
}
}
}
/*ARGSUSED*/
static void *
attach_devices(void *arg)
{
logerror("Failed to attach devices.");
return (NULL);
}
/*
* Unload all the modules.
*/
return (NULL);
}
/*
* Create a file which will contain our pid. Pmconfig will check this file
* to see if we are running and can use the pid to signal us. Returns the
* file descriptor if successful, -1 otherwise.
*
* Note: Deal with attempt to launch multiple instances and also with existence
* of an obsolete pid file caused by an earlier abort.
*/
static int
open_pidfile(char *me)
{
int fd;
const char *e1 = "%s: Cannot open pid file for read: ";
const char *e2 = "%s: Cannot unlink obsolete pid file: ";
const char *e3 = "%s: Either another daemon is running or the"
" process is defunct (pid %d). \n";
const char *e4 = "%s: Cannot create pid file: ";
return (-1);
}
/* Read the pid */
if (pid == -1) {
return (-1);
} else /* try without corrupted file */
goto again;
}
/* Is pid for a running process */
return (-1);
} else /* try without obsolete file */
goto again;
}
} else { /* powerd deamon still running or defunct */
return (-1);
}
} else { /* create failure not due to existing file */
return (-1);
}
}
return (fd);
}
/*
* Write a pid to the pid file. Report errors to syslog.
*
*/
static int
{
int len;
int rc = 0; /* assume success */
rc = -1;
}
return (rc);
}