diff options
author | Michael Shalayeff <mickey@cvs.openbsd.org> | 2001-07-07 01:10:43 +0000 |
---|---|---|
committer | Michael Shalayeff <mickey@cvs.openbsd.org> | 2001-07-07 01:10:43 +0000 |
commit | a1da01e7c86e35444694aa870e4de27d2c3b4df2 (patch) | |
tree | d76b5cd80a6fc40c397355bbeff43e638c449815 /usr.sbin/apmd | |
parent | eb71b9bc1018ad1c110369e685e32229f6712e7a (diff) |
cleanup and knf, some are from Kevin Steves <stevesk@pobox.com>
Diffstat (limited to 'usr.sbin/apmd')
-rw-r--r-- | usr.sbin/apmd/apmd.8 | 10 | ||||
-rw-r--r-- | usr.sbin/apmd/apmd.c | 793 | ||||
-rw-r--r-- | usr.sbin/apmd/apmsubr.c | 67 |
3 files changed, 424 insertions, 446 deletions
diff --git a/usr.sbin/apmd/apmd.8 b/usr.sbin/apmd/apmd.8 index b395a670ccf..2d310fb2ec2 100644 --- a/usr.sbin/apmd/apmd.8 +++ b/usr.sbin/apmd/apmd.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: apmd.8,v 1.19 2001/07/05 22:06:00 mickey Exp $ +.\" $OpenBSD: apmd.8,v 1.20 2001/07/07 01:10:41 mickey Exp $ .\" .\" Copyright (c) 1995 John T. Kohl .\" All rights reserved. @@ -125,12 +125,12 @@ In both cases exits immediately after setting the desired option. The .Fl e -unconditionally enables power status messages. +flag unconditionally enables power status messages. The .Fl p -causes power status messages to be displayed only when the +flag causes power status messages to be displayed only when the battery life expectancy changes. -This minimized message output +This minimizes message output for those devices that are constantly updating the estimated time remaining based upon current processor load. However, in no case @@ -154,7 +154,7 @@ replies with the intended state to the client and then places the system in the requested mode after running the configuration script and flushing the buffer cache. .Pp -Actions can be configured for the three transitions: +Actions can be configured for the following five transitions: .Cm suspend , .Cm standby , .Cm resume , diff --git a/usr.sbin/apmd/apmd.c b/usr.sbin/apmd/apmd.c index 5385e83d487..44e98dd103d 100644 --- a/usr.sbin/apmd/apmd.c +++ b/usr.sbin/apmd/apmd.c @@ -1,9 +1,9 @@ -/* $OpenBSD: apmd.c,v 1.14 2001/03/23 01:00:41 mickey Exp $ */ +/* $OpenBSD: apmd.c,v 1.15 2001/07/07 01:10:42 mickey Exp $ */ /* * Copyright (c) 1995, 1996 John T. Kohl * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -14,7 +14,7 @@ * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -26,11 +26,10 @@ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. - * + * */ #include <stdio.h> -#include <errno.h> #include <syslog.h> #include <fcntl.h> #include <unistd.h> @@ -40,12 +39,12 @@ #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> -#include <sys/time.h> #include <sys/socket.h> #include <sys/un.h> #include <sys/wait.h> -#include <machine/apmvar.h> +#include <errno.h> #include <err.h> +#include <machine/apmvar.h> #include "pathnames.h" #include "apm-proto.h" @@ -56,14 +55,9 @@ const char apmdev[] = _PATH_APM_CTLDEV; const char sockfile[] = _PATH_APM_SOCKET; -static int debug = 0; +int debug = 0; extern char *__progname; -extern char *optarg; -extern int optind; -extern int optopt; -extern int opterr; -extern int optreset; void usage (void); int power_status (int fd, int force, struct apm_power_info *pinfo); @@ -71,9 +65,6 @@ int bind_socket (const char *sn); enum apm_state handle_client(int sock_fd, int ctl_fd); void suspend(int ctl_fd); void stand_by(int ctl_fd); -void resume(int ctl_fd); -void powerup(int ctl_fd); -void powerdown(int ctl_fd); void sigexit(int signo); void make_noise(int howmany); void do_etc_file(const char *file); @@ -87,464 +78,460 @@ sigexit(int signo) void usage(void) { - fprintf(stderr, - "usage: %s [-dsaqepm] [-t timo] [-a] [-f devfile] [-S sockfile]\n", + fprintf(stderr, + "usage: %s [-adempqs] [-f devfile] [-S sockfile] [-t timo]\n", __progname); - exit(1); + exit(1); } /* * tell the driver if it should display messages or not. */ -static void +void set_driver_messages(int fd, int mode) { - if ( ioctl(fd, APM_IOC_PRN_CTL, &mode) == -1 ) - syslog( LOG_DEBUG, "can't disable driver messages, error: %m" ); + if (ioctl(fd, APM_IOC_PRN_CTL, &mode) == -1) + syslog(LOG_DEBUG, "can't disable driver messages, error: %m"); } int power_status(int fd, int force, struct apm_power_info *pinfo) { - struct apm_power_info bstate; - static struct apm_power_info last; - int acon = 0; + struct apm_power_info bstate; + static struct apm_power_info last; + int acon = 0; - if (ioctl(fd, APM_IOC_GETPOWER, &bstate) == 0) { + if (ioctl(fd, APM_IOC_GETPOWER, &bstate) == 0) { /* various conditions under which we report status: something changed - enough since last report, or asked to force a print */ - if (bstate.ac_state == APM_AC_ON) - acon = 1; - if (force || - bstate.ac_state != last.ac_state || - bstate.battery_state != last.battery_state || - (bstate.minutes_left && bstate.minutes_left < 15) || - abs(bstate.battery_life - last.battery_life) > 20) { - if (bstate.minutes_left) - syslog(LOG_NOTICE, - "battery status: %s. external power status: %s. " - "estimated battery life %d%% (%d minutes)", - battstate(bstate.battery_state), - ac_state(bstate.ac_state), bstate.battery_life, - bstate.minutes_left ); - else - syslog(LOG_NOTICE, - "battery status: %s. external power status: %s. " - "estimated battery life %d%%", - battstate(bstate.battery_state), - ac_state(bstate.ac_state), bstate.battery_life); - last = bstate; - } - if (pinfo) - *pinfo = bstate; - } else - syslog(LOG_ERR, "cannot fetch power status: %m"); - return acon; -} + * enough since last report, or asked to force a print */ + if (bstate.ac_state == APM_AC_ON) + acon = 1; + if (force || + bstate.ac_state != last.ac_state || + bstate.battery_state != last.battery_state || + (bstate.minutes_left && bstate.minutes_left < 15) || + abs(bstate.battery_life - last.battery_life) > 20) { + if (bstate.minutes_left) + syslog(LOG_NOTICE, "battery status: %s. " + "external power status: %s. " + "estimated battery life %d%% (%u minutes)", + battstate(bstate.battery_state), + ac_state(bstate.ac_state), + bstate.battery_life, + bstate.minutes_left); + else + syslog(LOG_NOTICE, "battery status: %s. " + "external power status: %s. " + "estimated battery life %d%%", + battstate(bstate.battery_state), + ac_state(bstate.ac_state), + bstate.battery_life); + last = bstate; + } + if (pinfo) + *pinfo = bstate; + } else + syslog(LOG_ERR, "cannot fetch power status: %m"); -static char *socketname; + return acon; +} -static void sockunlink(void); +char *socketname; -static void +void sockunlink(void) { - if (socketname) - (void) remove(socketname); + if (socketname) + remove(socketname); } int bind_socket(const char *sockname) { - int sock; - struct sockaddr_un s_un; - - sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock == -1) - err(1, "cannot create local socket"); - - s_un.sun_family = AF_UNIX; - strncpy(s_un.sun_path, sockname, sizeof(s_un.sun_path)); - s_un.sun_len = SUN_LEN(&s_un); - /* remove it if present, we're moving in */ - (void) remove(sockname); - umask (077); - if (bind(sock, (struct sockaddr *)&s_un, s_un.sun_len) == -1) - err(1, "cannot connect to APM socket"); - if (chmod(sockname, 0660) == -1 || chown(sockname, 0, 0) == -1) - err(1, "cannot set socket mode/owner/group to 660/0/0"); - listen(sock, 1); - socketname = strdup(sockname); - atexit(sockunlink); - return sock; + struct sockaddr_un s_un; + int sock; + + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock == -1) + err(1, "cannot create local socket"); + + s_un.sun_family = AF_UNIX; + strncpy(s_un.sun_path, sockname, sizeof(s_un.sun_path)); + s_un.sun_len = SUN_LEN(&s_un); + + /* remove it if present, we're moving in */ + (void) remove(sockname); + umask (077); + + if (bind(sock, (struct sockaddr *)&s_un, s_un.sun_len) == -1) + err(1, "cannot connect to APM socket"); + if (chmod(sockname, 0660) == -1 || chown(sockname, 0, 0) == -1) + err(1, "cannot set socket mode/owner/group to 660/0/0"); + + listen(sock, 1); + socketname = strdup(sockname); + atexit(sockunlink); + + return sock; } enum apm_state handle_client(int sock_fd, int ctl_fd) { - /* accept a handle from the client, process it, then clean up */ - int cli_fd; - struct sockaddr_un from; - int fromlen; - struct apm_command cmd; - struct apm_reply reply; - - cli_fd = accept(sock_fd, (struct sockaddr *)&from, &fromlen); - if (cli_fd == -1) { - syslog(LOG_INFO, "client accept failure: %m"); - return NORMAL; - } - if (recv(cli_fd, &cmd, sizeof(cmd), 0) != sizeof(cmd)) { - (void) close(cli_fd); - syslog(LOG_INFO, "client size botch"); - return NORMAL; - } - if (cmd.vno != APMD_VNO) { - close(cli_fd); /* terminate client */ - /* no error message, just drop it. */ - return NORMAL; - } - power_status(ctl_fd, 0, &reply.batterystate); - switch (cmd.action) { - default: - reply.newstate = NORMAL; - break; - case SUSPEND: - reply.newstate = SUSPENDING; - break; - case STANDBY: - reply.newstate = STANDING_BY; - break; - } - reply.vno = APMD_VNO; - if (send(cli_fd, &reply, sizeof(reply), 0) != sizeof(reply)) { - syslog(LOG_INFO, "client reply botch"); - } - close(cli_fd); - return reply.newstate; + /* accept a handle from the client, process it, then clean up */ + int cli_fd; + struct sockaddr_un from; + int fromlen; + struct apm_command cmd; + struct apm_reply reply; + + cli_fd = accept(sock_fd, (struct sockaddr *)&from, &fromlen); + if (cli_fd == -1) { + syslog(LOG_INFO, "client accept failure: %m"); + return NORMAL; + } + + if (recv(cli_fd, &cmd, sizeof(cmd), 0) != sizeof(cmd)) { + (void) close(cli_fd); + syslog(LOG_INFO, "client size botch"); + return NORMAL; + } + + if (cmd.vno != APMD_VNO) { + close(cli_fd); /* terminate client */ + /* no error message, just drop it. */ + return NORMAL; + } + + power_status(ctl_fd, 0, &reply.batterystate); + switch (cmd.action) { + case SUSPEND: reply.newstate = SUSPENDING; break; + case STANDBY: reply.newstate = STANDING_BY; break; + default: reply.newstate = NORMAL; break; + } + + reply.vno = APMD_VNO; + if (send(cli_fd, &reply, sizeof(reply), 0) != sizeof(reply)) + syslog(LOG_INFO, "client reply botch"); + close(cli_fd); + + return reply.newstate; } -static int speaker_ok = TRUE; +int speaker_ok = TRUE; void make_noise(howmany) int howmany; { - int spkrfd = -1; - int trycnt; + int spkrfd = -1; + int trycnt; - if (!speaker_ok) /* don't bother after sticky errors */ - return; + if (!speaker_ok) /* don't bother after sticky errors */ + return; + + for (trycnt = 0; trycnt < 3; trycnt++) { + spkrfd = open(_PATH_DEV_SPEAKER, O_WRONLY); + if (spkrfd == -1) { + switch (errno) { + case EBUSY: + usleep(500000); + errno = EBUSY; + continue; + case ENOENT: + case ENODEV: + case ENXIO: + case EPERM: + case EACCES: + syslog(LOG_WARNING, "speaker device " + _PATH_DEV_SPEAKER " unavailable: %m"); + speaker_ok = FALSE; + return; + } + } else + break; + } - for (trycnt = 0; trycnt < 3; trycnt++) { - spkrfd = open(_PATH_DEV_SPEAKER, O_WRONLY); if (spkrfd == -1) { - switch (errno) { - case EBUSY: - usleep(500000); - errno = EBUSY; - continue; - case ENOENT: - case ENODEV: - case ENXIO: - case EPERM: - case EACCES: - syslog(LOG_INFO, - "speaker device " _PATH_DEV_SPEAKER " unavailable: %m"); - speaker_ok = FALSE; + syslog(LOG_WARNING, "cannot open " _PATH_DEV_SPEAKER ": %m"); return; - } - } else - break; - } - if (spkrfd == -1) { - syslog(LOG_WARNING, "cannot open " _PATH_DEV_SPEAKER ": %m"); - return; - } - syslog(LOG_DEBUG, "sending %d tones to speaker", howmany); - write(spkrfd, "o4cc", 2 + howmany); - close(spkrfd); - return; -} - -void -powerup(int ctl_fd) -{ - do_etc_file(_PATH_APM_ETC_POWERUP); -} + } -void -powerdown(int ctl_fd) -{ - do_etc_file(_PATH_APM_ETC_POWERDOWN); + syslog(LOG_DEBUG, "sending %d tones to speaker", howmany); + write(spkrfd, "o4cc", 2 + howmany); + close(spkrfd); } void suspend(int ctl_fd) { - do_etc_file(_PATH_APM_ETC_SUSPEND); - sync(); - make_noise(2); - sync(); - sync(); - sleep(1); - ioctl(ctl_fd, APM_IOC_SUSPEND, 0); + do_etc_file(_PATH_APM_ETC_SUSPEND); + sync(); + make_noise(2); + sync(); + sync(); + sleep(1); + ioctl(ctl_fd, APM_IOC_SUSPEND, 0); } void stand_by(int ctl_fd) { - do_etc_file(_PATH_APM_ETC_STANDBY); - sync(); - make_noise(1); - sync(); - sync(); - sleep(1); - ioctl(ctl_fd, APM_IOC_STANDBY, 0); + do_etc_file(_PATH_APM_ETC_STANDBY); + sync(); + make_noise(1); + sync(); + sync(); + sleep(1); + ioctl(ctl_fd, APM_IOC_STANDBY, 0); } #define TIMO (10*60) /* 10 minutes */ -void -resume(int ctl_fd) -{ - do_etc_file(_PATH_APM_ETC_RESUME); -} - int main(int argc, char *argv[]) { - const char *fname = apmdev; - int ctl_fd, sock_fd, ch, ready; - int statonly = 0; - int enableonly = 0; - int pctonly = 0; - int powerstatus = 0, powerbak = 0, powerchange = 0; - int messages = 0; - fd_set *devfdsp, *selfdsp; - struct apm_event_info apmevent; - int suspends, standbys, resumes; - int noacsleep = 0; - struct timeval tv = {TIMO, 0}, stv; - const char *sockname = sockfile; - int fdsn; - - while ((ch = getopt(argc, argv, "qadsepmf:t:S:")) != -1) - switch(ch) { - case 'q': - speaker_ok = FALSE; - break; - case 'a': - noacsleep = 1; - break; - case 'd': - debug = 1; - break; - case 'f': - fname = optarg; - break; - case 'S': - sockname = optarg; - break; - case 't': - tv.tv_sec = strtoul(optarg, 0, 0); - if (tv.tv_sec == 0) - usage(); - break; - case 's': /* status only */ - statonly = 1; - break; - case 'e': - enableonly = 1; - break; - case 'p': - pctonly = 1; - break; - case 'm': - messages = 1; - break; - case '?': + const char *fname = apmdev; + int ctl_fd, sock_fd, ch, ready, fdsn, suspends, standbys, resumes; + int statonly = 0; + int enableonly = 0; + int pctonly = 0; + int powerstatus = 0, powerbak = 0, powerchange = 0; + int messages = 0; + int noacsleep = 0; + fd_set *devfdsp, *selfdsp; + struct apm_event_info apmevent; + struct timeval tv = {TIMO, 0}, stv; + const char *sockname = sockfile; + + while ((ch = getopt(argc, argv, "qadsepmf:t:S:")) != -1) + switch(ch) { + case 'q': + speaker_ok = FALSE; + break; + case 'a': + noacsleep = 1; + break; + case 'd': + debug = 1; + break; + case 'f': + fname = optarg; + break; + case 'S': + sockname = optarg; + break; + case 't': + tv.tv_sec = strtoul(optarg, NULL, 0); + if (tv.tv_sec == 0) + usage(); + break; + case 's': /* status only */ + statonly = 1; + break; + case 'e': + enableonly = 1; + break; + case 'p': + pctonly = 1; + break; + case 'm': + messages = 1; + break; + case '?': + default: + usage(); + } - default: - usage(); + argc -= optind; + argv += optind; + + if ((ctl_fd = open(fname, O_RDWR)) == -1) + err(1, "cannot open device file `%s'", fname); + + if (debug) + openlog(__progname, LOG_CONS, LOG_LOCAL1); + else { + openlog(__progname, LOG_CONS, LOG_DAEMON); + setlogmask(LOG_UPTO(LOG_NOTICE)); + daemon(0, 0); } - argc -= optind; - argv += optind; - if ((ctl_fd = open(fname, O_RDWR)) == -1) { - (void)err(1, "cannot open device file `%s'", fname); - } - if (debug) { - openlog(__progname, LOG_CONS, LOG_LOCAL1); - } else { - openlog(__progname, LOG_CONS, LOG_DAEMON); - setlogmask(LOG_UPTO(LOG_NOTICE)); - daemon(0, 0); - } - power_status(ctl_fd, 1, 0); - if (statonly) - exit(0); - if (enableonly) { - set_driver_messages(ctl_fd, APM_PRINT_ON); - exit(0); - } - if (pctonly) { - set_driver_messages(ctl_fd, APM_PRINT_PCT); - exit(0); - } - if ( ! messages ) { - set_driver_messages(ctl_fd, APM_PRINT_OFF); - } - (void) signal(SIGTERM, sigexit); - (void) signal(SIGHUP, sigexit); - (void) signal(SIGINT, sigexit); - - sock_fd = bind_socket(sockname); - - fdsn = howmany(MAX(ctl_fd, sock_fd)+1, NFDBITS) * sizeof(fd_mask); - if ((devfdsp = (fd_set *)malloc(fdsn)) == NULL) - err(1, "malloc"); - if ((selfdsp = (fd_set *)malloc(fdsn)) == NULL) - err(1, "malloc"); - memset(devfdsp, 0, fdsn); - FD_SET(ctl_fd, devfdsp); - FD_SET(sock_fd, devfdsp); - - for (;;) { - stv = tv; - memmove(selfdsp, devfdsp, fdsn); - - ready = select(MAX(ctl_fd,sock_fd)+1, selfdsp, 0, 0, &stv); - if (ready == -1 && errno != EINTR) - continue; - - if (ready == 0) { - /* wakeup for timeout: take status */ - powerbak = power_status(ctl_fd, 0, 0); - if (powerstatus != powerbak) { - powerstatus = powerbak; - powerchange = 1; - } + + power_status(ctl_fd, 1, 0); + + if (statonly) + exit(0); + + if (enableonly) { + set_driver_messages(ctl_fd, APM_PRINT_ON); + exit(0); } - if (FD_ISSET(ctl_fd, selfdsp)) { - suspends = standbys = resumes = 0; - while (ioctl(ctl_fd, APM_IOC_NEXTEVENT, &apmevent) == 0) { - syslog(LOG_DEBUG, "apmevent %04x index %d", apmevent.type, - apmevent.index); - switch (apmevent.type) { - case APM_SUSPEND_REQ: - case APM_USER_SUSPEND_REQ: - case APM_CRIT_SUSPEND_REQ: - case APM_BATTERY_LOW: - suspends++; - break; - case APM_USER_STANDBY_REQ: - case APM_STANDBY_REQ: - standbys++; - break; + + if (pctonly) { + set_driver_messages(ctl_fd, APM_PRINT_PCT); + exit(0); + } + + if (!messages) + set_driver_messages(ctl_fd, APM_PRINT_OFF); + + (void) signal(SIGTERM, sigexit); + (void) signal(SIGHUP, sigexit); + (void) signal(SIGINT, sigexit); + + sock_fd = bind_socket(sockname); + fdsn = howmany(MAX(ctl_fd, sock_fd)+1, NFDBITS) * sizeof(fd_mask); + if ((devfdsp = (fd_set *)malloc(fdsn)) == NULL) + err(1, "malloc"); + + if ((selfdsp = (fd_set *)malloc(fdsn)) == NULL) + err(1, "malloc"); + + memset(devfdsp, 0, fdsn); + FD_SET(ctl_fd, devfdsp); + FD_SET(sock_fd, devfdsp); + + for (;;) { + stv = tv; + memmove(selfdsp, devfdsp, fdsn); + + ready = select(MAX(ctl_fd,sock_fd)+1, selfdsp, 0, 0, &stv); + if (ready == -1 && errno != EINTR) + continue; + + if (ready == 0) { + /* wakeup for timeout: take status */ + powerbak = power_status(ctl_fd, 0, 0); + if (powerstatus != powerbak) { + powerstatus = powerbak; + powerchange = 1; + } + } + + if (FD_ISSET(ctl_fd, selfdsp)) { + suspends = standbys = resumes = 0; + while (!ioctl(ctl_fd, APM_IOC_NEXTEVENT, &apmevent)) { + + syslog(LOG_DEBUG, "apmevent %04x index %d", + apmevent.type, apmevent.index); + + switch (apmevent.type) { + case APM_SUSPEND_REQ: + case APM_USER_SUSPEND_REQ: + case APM_CRIT_SUSPEND_REQ: + case APM_BATTERY_LOW: + suspends++; + break; + case APM_USER_STANDBY_REQ: + case APM_STANDBY_REQ: + standbys++; + break; #if 0 - case APM_CANCEL: - suspends = standbys = 0; - break; + case APM_CANCEL: + suspends = standbys = 0; + break; #endif - case APM_NORMAL_RESUME: - case APM_CRIT_RESUME: - case APM_SYS_STANDBY_RESUME: - powerbak = power_status(ctl_fd, 0, 0); - if (powerstatus != powerbak) { - powerstatus = powerbak; - powerchange = 1; - } - resumes++; - break; - case APM_POWER_CHANGE: - powerbak = power_status(ctl_fd, 0, 0); - if (powerstatus != powerbak) { - powerstatus = powerbak; - powerchange = 1; - } - break; - default: - break; + case APM_NORMAL_RESUME: + case APM_CRIT_RESUME: + case APM_SYS_STANDBY_RESUME: + powerbak = power_status(ctl_fd, 0, 0); + if (powerstatus != powerbak) { + powerstatus = powerbak; + powerchange = 1; + } + resumes++; + break; + case APM_POWER_CHANGE: + powerbak = power_status(ctl_fd, 0, 0); + if (powerstatus != powerbak) { + powerstatus = powerbak; + powerchange = 1; + } + break; + default: + } + } + + if ((standbys || suspends) && noacsleep && + power_status(ctl_fd, 0, 0)) + syslog(LOG_DEBUG, "no sleep till brooklyn"); + else if (suspends) + suspend(ctl_fd); + else if (standbys) + stand_by(ctl_fd); + else if (resumes) { + do_etc_file(_PATH_APM_ETC_RESUME); + syslog(LOG_NOTICE, + "system resumed from APM sleep"); + } + + if (powerchange) { + if (powerstatus) + do_etc_file(_PATH_APM_ETC_POWERUP); + else + do_etc_file(_PATH_APM_ETC_POWERDOWN); + powerchange = 0; + } + ready--; } - } - if ((standbys || suspends) && noacsleep && - power_status(ctl_fd, 0, 0)) { - syslog(LOG_DEBUG, "not sleeping cuz AC is connected"); - } else if (suspends) { - suspend(ctl_fd); - } else if (standbys) { - stand_by(ctl_fd); - } else if (resumes) { - resume(ctl_fd); - syslog(LOG_NOTICE, "system resumed from APM sleep"); - } - if (powerchange) { - if (powerstatus) - powerup(ctl_fd); - else - powerdown(ctl_fd); - powerchange = 0; - } - ready--; - } - if (ready == 0) - continue; - if (FD_ISSET(sock_fd, selfdsp)) { - switch (handle_client(sock_fd, ctl_fd)) { - case NORMAL: - break; - case SUSPENDING: - suspend(ctl_fd); - break; - case STANDING_BY: - stand_by(ctl_fd); - break; - } + + if (ready == 0) + continue; + + if (FD_ISSET(sock_fd, selfdsp)) + switch (handle_client(sock_fd, ctl_fd)) { + case NORMAL: + break; + case SUSPENDING: + suspend(ctl_fd); + break; + case STANDING_BY: + stand_by(ctl_fd); + break; + } } - } - syslog(LOG_ERR, "select failed: %m"); - return 1; + syslog(LOG_ERR, "select failed: %m"); + + return 1; } void do_etc_file(const char *file) { - pid_t pid; - int status; - const char *prog; - - /* If file doesn't exist, do nothing. */ - if (access(file, X_OK|R_OK)) { - syslog(LOG_DEBUG, "do_etc_file(): cannot access file %s", file); - return; - } - - prog = strrchr(file, '/'); - if (prog) - prog++; - else - prog = file; - - pid = fork(); - switch (pid) { - case -1: - syslog(LOG_ERR, "failed to fork(): %m"); - return; - case 0: - /* We are the child. */ - execl(file, prog, NULL); - _exit(1); - /* NOTREACHED */ - default: - /* We are the parent. */ - wait4(pid, &status, 0, 0); - if (WIFEXITED(status)) - syslog(LOG_DEBUG, "%s exited with status %d", file, - WEXITSTATUS(status)); - else { - syslog(LOG_ERR, "%s exited abnormally.", file); + pid_t pid; + int status; + const char *prog; + + /* If file doesn't exist, do nothing. */ + if (access(file, X_OK|R_OK)) { + syslog(LOG_DEBUG, "do_etc_file(): cannot access file %s", file); + return; + } + + prog = strrchr(file, '/'); + if (prog) + prog++; + else + prog = file; + + pid = fork(); + switch (pid) { + case -1: + syslog(LOG_ERR, "failed to fork(): %m"); + return; + case 0: + /* We are the child. */ + execl(file, prog, NULL); + _exit(1); + /* NOTREACHED */ + default: + /* We are the parent. */ + wait4(pid, &status, 0, 0); + if (WIFEXITED(status)) + syslog(LOG_DEBUG, "%s exited with status %d", file, + WEXITSTATUS(status)); + else + syslog(LOG_ERR, "%s exited abnormally.", file); } - break; - } } diff --git a/usr.sbin/apmd/apmsubr.c b/usr.sbin/apmd/apmsubr.c index 02cc864dd47..95943e5c1db 100644 --- a/usr.sbin/apmd/apmsubr.c +++ b/usr.sbin/apmd/apmsubr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: apmsubr.c,v 1.2 2001/03/23 01:00:41 mickey Exp $ */ +/* $OpenBSD: apmsubr.c,v 1.3 2001/07/07 01:10:42 mickey Exp $ */ /* * Copyright (c) 1995,1996 John T. Kohl @@ -29,53 +29,44 @@ * */ -#include <stdio.h> -#include <errno.h> -#include <syslog.h> -#include <fcntl.h> -#include <unistd.h> -#include <stdlib.h> #include <sys/types.h> -#include <sys/ioctl.h> -#include <sys/time.h> #include <machine/apmvar.h> -#include <err.h> #include "apm-proto.h" const char * battstate(int state) { - switch (state) { - case APM_BATT_HIGH: - return "high"; - case APM_BATT_LOW: - return "low"; - case APM_BATT_CRITICAL: - return "CRITICAL"; - case APM_BATT_CHARGING: - return "charging"; - case APM_BATTERY_ABSENT: - return "absent"; - case APM_BATT_UNKNOWN: - return "unknown (absent?)"; - default: - return "invalid battery state"; - } + switch (state) { + case APM_BATT_HIGH: + return "high"; + case APM_BATT_LOW: + return "low"; + case APM_BATT_CRITICAL: + return "CRITICAL"; + case APM_BATT_CHARGING: + return "charging"; + case APM_BATTERY_ABSENT: + return "absent"; + case APM_BATT_UNKNOWN: + return "unknown (absent?)"; + default: + return "invalid battery state"; + } } const char * ac_state(int state) { - switch (state) { - case APM_AC_OFF: - return "not connected"; - case APM_AC_ON: - return "connected"; - case APM_AC_BACKUP: - return "backup power source"; - case APM_AC_UNKNOWN: - return "not known"; - default: - return "invalid AC status"; - } + switch (state) { + case APM_AC_OFF: + return "not connected"; + case APM_AC_ON: + return "connected"; + case APM_AC_BACKUP: + return "backup power source"; + case APM_AC_UNKNOWN: + return "not known"; + default: + return "invalid AC status"; + } } |