Hello tech@

I've been stuck with fsck for way to many times now because of me
forgetting to connect the AC power. This patch will add -z and -Z
followed by the percentage. apmd will try to suspend or hibernate the
system if the AC is not connected and the estimated battery life goes
below the specified percentage.


Jesper Wallin


Index: usr.sbin/apmd/apmd.8
===================================================================
RCS file: /cvs/src/usr.sbin/apmd/apmd.8,v
retrieving revision 1.47
diff -u -p -r1.47 apmd.8
--- usr.sbin/apmd/apmd.8        12 Feb 2015 14:03:49 -0000      1.47
+++ usr.sbin/apmd/apmd.8        12 Aug 2017 21:18:28 -0000
@@ -113,6 +113,16 @@ 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 suspend the system if no AC is connected and the
+estimated battery life is equal or below
+.Ar percent .
+.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 .
+This will supersedes
+.Fl z .
 .El
 .Pp
 When a client requests a suspend or stand-by state,
Index: usr.sbin/apmd/apmd.c
===================================================================
RCS file: /cvs/src/usr.sbin/apmd/apmd.c,v
retrieving revision 1.79
diff -u -p -r1.79 apmd.c
--- usr.sbin/apmd/apmd.c        16 Nov 2015 17:35:05 -0000      1.79
+++ usr.sbin/apmd/apmd.c        12 Aug 2017 21:18:28 -0000
@@ -94,8 +94,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 +348,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;
@@ -361,7 +363,7 @@ main(int argc, char *argv[])
        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 +404,14 @@ main(int argc, char *argv[])
                        doperf = PERF_MANUAL;
                        setperfpolicy("high");
                        break;
+               case 'z':
+                       autoaction = 1;
+                       autolimit = strtoul(optarg, NULL, 0);
+                       break;
+               case 'Z':
+                       autoaction = 2;
+                       autolimit = strtoul(optarg, NULL, 0);
+                       break;
                case '?':
                default:
                        usage();
@@ -479,6 +489,20 @@ main(int argc, char *argv[])
                        if (powerstatus != powerbak) {
                                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 == 1)
+                                       suspend(ctl_fd);
+                               else
+                                       hibernate(ctl_fd);
                        }
                }
 

Reply via email to