From 95c85f17d11a33f297a4761c73095deef48f928d Mon Sep 17 00:00:00 2001 From: tedu Date: Mon, 28 Aug 2017 16:16:58 +0000 Subject: add -z and -Z options to auto suspend or hibernate when low on battery. from Jesper Wallin --- usr.sbin/apmd/apmd.8 | 20 ++++++++++++++++++-- usr.sbin/apmd/apmd.c | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/usr.sbin/apmd/apmd.8 b/usr.sbin/apmd/apmd.8 index 6b047d2fc25..d171bcf93f6 100644 --- a/usr.sbin/apmd/apmd.8 +++ b/usr.sbin/apmd/apmd.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: apmd.8,v 1.47 2015/02/12 14:03:49 schwarze Exp $ +.\" $OpenBSD: apmd.8,v 1.48 2017/08/28 16:16:58 tedu Exp $ .\" .\" Copyright (c) 1995 John T. Kohl .\" All rights reserved. @@ -26,7 +26,7 @@ .\" ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd $Mdocdate: February 12 2015 $ +.Dd $Mdocdate: August 28 2017 $ .Dt APMD 8 .Os .Sh NAME @@ -38,6 +38,8 @@ .Op Fl f Ar devname .Op Fl S Ar sockname .Op Fl t Ar seconds +.Op Fl Z Ar percent +.Op Fl z Ar percent .Sh DESCRIPTION .Nm monitors the advanced power management device, @@ -113,6 +115,20 @@ The polling rate defaults to once per 10 minutes, but may be specified using the .Fl t command-line flag. +.It Fl Z Ar percent +Automatically hibernate the system if no AC is connected and the +estimated battery life is equal or below +.Ar percent . +.It Fl z Ar percent +Automatically suspend the system if no AC is connected and the +estimated battery life is equal or below +.Ar percent . +.Pp +If both +.Fl Z +and +.Fl z +are specified, the last one will supersede the other. .El .Pp When a client requests a suspend or stand-by state, diff --git a/usr.sbin/apmd/apmd.c b/usr.sbin/apmd/apmd.c index 666ae0d0d29..549f641cad6 100644 --- a/usr.sbin/apmd/apmd.c +++ b/usr.sbin/apmd/apmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: apmd.c,v 1.79 2015/11/16 17:35:05 tedu Exp $ */ +/* $OpenBSD: apmd.c,v 1.80 2017/08/28 16:16:58 tedu Exp $ */ /* * Copyright (c) 1995, 1996 John T. Kohl @@ -56,6 +56,9 @@ #define TRUE 1 #define FALSE 0 +#define AUTO_SUSPEND 1 +#define AUTO_HIBERNATE 2 + const char apmdev[] = _PATH_APM_CTLDEV; const char sockfile[] = _PATH_APM_SOCKET; @@ -94,8 +97,8 @@ void usage(void) { fprintf(stderr, - "usage: %s [-AadHLs] [-f devname] [-S sockname] [-t seconds]\n", - __progname); + "usage: %s [-AadHLs] [-f devname] [-S sockname] [-t seconds] " + "[-Z percent] [-z percent]\n", __progname); exit(1); } @@ -348,6 +351,8 @@ main(int argc, char *argv[]) { const char *fname = apmdev; int ctl_fd, sock_fd, ch, suspends, standbys, hibernates, resumes; + int autoaction = 0; + int autolimit = 0; int statonly = 0; int powerstatus = 0, powerbak = 0, powerchange = 0; int noacsleep = 0; @@ -355,13 +360,14 @@ main(int argc, char *argv[]) struct apm_power_info pinfo; time_t apmtimeout = 0; const char *sockname = sockfile; + const char *errstr; int kq, nchanges; struct kevent ev[2]; int ncpu_mib[2] = { CTL_HW, HW_NCPU }; int ncpu; size_t ncpu_sz = sizeof(ncpu); - while ((ch = getopt(argc, argv, "aACdHLsf:t:S:")) != -1) + while ((ch = getopt(argc, argv, "aACdHLsf:t:S:z:Z:")) != -1) switch(ch) { case 'a': noacsleep = 1; @@ -402,6 +408,20 @@ main(int argc, char *argv[]) doperf = PERF_MANUAL; setperfpolicy("high"); break; + case 'Z': + autoaction = AUTO_HIBERNATE; + autolimit = strtonum(optarg, 1, 100, &errstr); + if (errstr != NULL) + errc(1, EINVAL, "%s percentage: %s", errstr, + optarg); + break; + case 'z': + autoaction = AUTO_SUSPEND; + autolimit = strtonum(optarg, 1, 100, &errstr); + if (errstr != NULL) + errc(1, EINVAL, "%s percentage: %s", errstr, + optarg); + break; case '?': default: usage(); @@ -480,6 +500,21 @@ main(int argc, char *argv[]) powerstatus = powerbak; powerchange = 1; } + + if (!powerstatus && autoaction && + autolimit > (int)pinfo.battery_life) { + syslog(LOG_NOTICE, + "estimated battery life %d%%, " + "autoaction limit set to %d%% .", + pinfo.battery_life, + autolimit + ); + + if (autoaction == AUTO_SUSPEND) + suspend(ctl_fd); + else + hibernate(ctl_fd); + } } if (!rv) -- cgit v1.2.3-59-g8ed1b