1 /* $OpenBSD: apmd.c,v 1.95 2020/02/18 01:18:53 jca Exp $ */ 2 3 /* 4 * Copyright (c) 1995, 1996 John T. Kohl 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 * 30 */ 31 32 #include <sys/stat.h> 33 #include <sys/ioctl.h> 34 #include <sys/socket.h> 35 #include <sys/un.h> 36 #include <sys/wait.h> 37 #include <sys/event.h> 38 #include <sys/time.h> 39 #include <sys/sysctl.h> 40 #include <assert.h> 41 #include <stdarg.h> 42 #include <stdio.h> 43 #include <syslog.h> 44 #include <fcntl.h> 45 #include <unistd.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <signal.h> 49 #include <errno.h> 50 #include <err.h> 51 #include <limits.h> 52 #include <machine/apmvar.h> 53 54 #include "pathnames.h" 55 #include "apm-proto.h" 56 57 #define AUTO_SUSPEND 1 58 #define AUTO_HIBERNATE 2 59 60 int debug = 0; 61 62 int doperf = PERF_NONE; 63 64 extern char *__progname; 65 66 void usage(void); 67 int power_status(int fd, int force, struct apm_power_info *pinfo); 68 int bind_socket(const char *sn); 69 enum apm_state handle_client(int sock_fd, int ctl_fd); 70 void suspend(int ctl_fd); 71 void stand_by(int ctl_fd); 72 void hibernate(int ctl_fd); 73 void resumed(int ctl_fd); 74 void setperfpolicy(char *policy); 75 void sigexit(int signo); 76 void do_etc_file(const char *file); 77 void sockunlink(void); 78 void error(const char *fmt, const char *arg); 79 void set_driver_messages(int fd, int mode); 80 81 /* ARGSUSED */ 82 void 83 sigexit(int signo) 84 { 85 sockunlink(); 86 _exit(1); 87 } 88 89 void 90 logmsg(int prio, const char *msg, ...) 91 { 92 va_list ap; 93 94 va_start(ap, msg); 95 if (debug) { 96 vfprintf(stderr, msg, ap); 97 fprintf(stderr, "\n"); 98 } else { 99 vsyslog(prio, msg, ap); 100 } 101 va_end(ap); 102 } 103 104 void 105 usage(void) 106 { 107 fprintf(stderr, 108 "usage: %s [-AadHLs] [-f devname] [-S sockname] [-t seconds] " 109 "[-Z percent] [-z percent]\n", __progname); 110 exit(1); 111 } 112 113 void 114 error(const char *fmt, const char *arg) 115 { 116 char buf[128]; 117 118 if (debug) 119 err(1, fmt, arg); 120 else { 121 strlcpy(buf, fmt, sizeof(buf)); 122 strlcat(buf, ": %m", sizeof(buf)); 123 syslog(LOG_ERR, buf, arg); 124 exit(1); 125 } 126 } 127 128 129 /* 130 * tell the driver if it should display messages or not. 131 */ 132 void 133 set_driver_messages(int fd, int mode) 134 { 135 if (ioctl(fd, APM_IOC_PRN_CTL, &mode) == -1) 136 logmsg(LOG_DEBUG, "can't disable driver messages, error: %s", 137 strerror(errno)); 138 } 139 140 int 141 power_status(int fd, int force, struct apm_power_info *pinfo) 142 { 143 struct apm_power_info bstate; 144 static struct apm_power_info last; 145 int acon = 0, priority = LOG_NOTICE; 146 147 if (fd == -1) { 148 if (pinfo) { 149 bstate.battery_state = 255; 150 bstate.ac_state = 255; 151 bstate.battery_life = 0; 152 bstate.minutes_left = -1; 153 *pinfo = bstate; 154 } 155 156 return 0; 157 } 158 159 if (ioctl(fd, APM_IOC_GETPOWER, &bstate) == 0) { 160 /* various conditions under which we report status: something changed 161 * enough since last report, or asked to force a print */ 162 if (bstate.ac_state == APM_AC_ON) 163 acon = 1; 164 if (bstate.battery_state == APM_BATT_CRITICAL && 165 bstate.battery_state != last.battery_state) 166 priority = LOG_EMERG; 167 if (force || 168 bstate.ac_state != last.ac_state || 169 bstate.battery_state != last.battery_state || 170 (bstate.minutes_left && bstate.minutes_left < 15) || 171 abs(bstate.battery_life - last.battery_life) >= 10) { 172 #ifdef __powerpc__ 173 /* 174 * When the battery is charging, the estimated life 175 * time is in fact the estimated remaining charge time 176 * on Apple machines, so lie in the stats. 177 * We still want an useful message if the battery or 178 * ac status changes, however. 179 */ 180 if (bstate.minutes_left != 0 && 181 bstate.battery_state != APM_BATT_CHARGING) 182 #else 183 if ((int)bstate.minutes_left > 0) 184 #endif 185 logmsg(priority, "battery status: %s. " 186 "external power status: %s. " 187 "estimated battery life %d%% (%u minutes)", 188 battstate(bstate.battery_state), 189 ac_state(bstate.ac_state), 190 bstate.battery_life, 191 bstate.minutes_left); 192 else 193 logmsg(priority, "battery status: %s. " 194 "external power status: %s. " 195 "estimated battery life %d%%", 196 battstate(bstate.battery_state), 197 ac_state(bstate.ac_state), 198 bstate.battery_life); 199 last = bstate; 200 } 201 if (pinfo) 202 *pinfo = bstate; 203 } else 204 logmsg(LOG_ERR, "cannot fetch power status: %s", strerror(errno)); 205 206 return acon; 207 } 208 209 char socketname[PATH_MAX]; 210 211 void 212 sockunlink(void) 213 { 214 if (socketname[0]) 215 remove(socketname); 216 } 217 218 int 219 bind_socket(const char *sockname) 220 { 221 struct sockaddr_un s_un; 222 mode_t old_umask; 223 int sock; 224 225 sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); 226 if (sock == -1) 227 error("cannot create local socket", NULL); 228 229 s_un.sun_family = AF_UNIX; 230 strlcpy(s_un.sun_path, sockname, sizeof(s_un.sun_path)); 231 232 /* remove it if present, we're moving in */ 233 (void) remove(sockname); 234 235 old_umask = umask(077); 236 if (bind(sock, (struct sockaddr *)&s_un, sizeof(s_un)) == -1) 237 error("cannot bind on APM socket", NULL); 238 umask(old_umask); 239 if (chmod(sockname, 0660) == -1 || chown(sockname, 0, 0) == -1) 240 error("cannot set socket mode/owner/group to 660/0/0", NULL); 241 242 listen(sock, 1); 243 strlcpy(socketname, sockname, sizeof socketname); 244 atexit(sockunlink); 245 246 return sock; 247 } 248 249 enum apm_state 250 handle_client(int sock_fd, int ctl_fd) 251 { 252 /* accept a handle from the client, process it, then clean up */ 253 int cli_fd; 254 struct sockaddr_un from; 255 socklen_t fromlen; 256 struct apm_command cmd; 257 struct apm_reply reply; 258 int cpuspeed_mib[] = {CTL_HW, HW_CPUSPEED}; 259 int cpuspeed = 0; 260 size_t cpuspeed_sz = sizeof(cpuspeed); 261 262 fromlen = sizeof(from); 263 cli_fd = accept(sock_fd, (struct sockaddr *)&from, &fromlen); 264 if (cli_fd == -1) { 265 logmsg(LOG_INFO, "client accept failure: %s", strerror(errno)); 266 return NORMAL; 267 } 268 269 if (recv(cli_fd, &cmd, sizeof(cmd), 0) != sizeof(cmd)) { 270 (void) close(cli_fd); 271 logmsg(LOG_INFO, "client size botch"); 272 return NORMAL; 273 } 274 275 if (cmd.vno != APMD_VNO) { 276 close(cli_fd); /* terminate client */ 277 /* no error message, just drop it. */ 278 return NORMAL; 279 } 280 281 power_status(ctl_fd, 0, &reply.batterystate); 282 switch (cmd.action) { 283 case SUSPEND: 284 reply.newstate = SUSPENDING; 285 break; 286 case STANDBY: 287 reply.newstate = STANDING_BY; 288 break; 289 case HIBERNATE: 290 reply.newstate = HIBERNATING; 291 break; 292 case SETPERF_LOW: 293 doperf = PERF_MANUAL; 294 reply.newstate = NORMAL; 295 logmsg(LOG_NOTICE, "setting hw.perfpolicy to low"); 296 setperfpolicy("low"); 297 break; 298 case SETPERF_HIGH: 299 doperf = PERF_MANUAL; 300 reply.newstate = NORMAL; 301 logmsg(LOG_NOTICE, "setting hw.perfpolicy to high"); 302 setperfpolicy("high"); 303 break; 304 case SETPERF_AUTO: 305 case SETPERF_COOL: 306 doperf = PERF_AUTO; 307 reply.newstate = NORMAL; 308 logmsg(LOG_NOTICE, "setting hw.perfpolicy to auto"); 309 setperfpolicy("auto"); 310 break; 311 default: 312 reply.newstate = NORMAL; 313 break; 314 } 315 316 if (sysctl(cpuspeed_mib, 2, &cpuspeed, &cpuspeed_sz, NULL, 0) == -1) 317 logmsg(LOG_INFO, "cannot read hw.cpuspeed"); 318 319 reply.cpuspeed = cpuspeed; 320 reply.perfmode = doperf; 321 reply.vno = APMD_VNO; 322 if (send(cli_fd, &reply, sizeof(reply), 0) != sizeof(reply)) 323 logmsg(LOG_INFO, "reply to client botched"); 324 close(cli_fd); 325 326 return reply.newstate; 327 } 328 329 void 330 suspend(int ctl_fd) 331 { 332 logmsg(LOG_NOTICE, "system suspending"); 333 power_status(ctl_fd, 1, NULL); 334 do_etc_file(_PATH_APM_ETC_SUSPEND); 335 sync(); 336 sleep(1); 337 ioctl(ctl_fd, APM_IOC_SUSPEND, 0); 338 } 339 340 void 341 stand_by(int ctl_fd) 342 { 343 logmsg(LOG_NOTICE, "system entering standby"); 344 power_status(ctl_fd, 1, NULL); 345 do_etc_file(_PATH_APM_ETC_STANDBY); 346 sync(); 347 sleep(1); 348 ioctl(ctl_fd, APM_IOC_STANDBY, 0); 349 } 350 351 void 352 hibernate(int ctl_fd) 353 { 354 logmsg(LOG_NOTICE, "system hibernating"); 355 power_status(ctl_fd, 1, NULL); 356 do_etc_file(_PATH_APM_ETC_HIBERNATE); 357 sync(); 358 sleep(1); 359 ioctl(ctl_fd, APM_IOC_HIBERNATE, 0); 360 } 361 362 void 363 resumed(int ctl_fd) 364 { 365 do_etc_file(_PATH_APM_ETC_RESUME); 366 logmsg(LOG_NOTICE, "system resumed from sleep"); 367 power_status(ctl_fd, 1, NULL); 368 } 369 370 #define TIMO (10*60) /* 10 minutes */ 371 372 int 373 main(int argc, char *argv[]) 374 { 375 const char *fname = _PATH_APM_CTLDEV; 376 int ctl_fd, sock_fd, ch, suspends, standbys, hibernates, resumes; 377 int autoaction = 0; 378 int autolimit = 0; 379 int statonly = 0; 380 int powerstatus = 0, powerbak = 0, powerchange = 0; 381 int noacsleep = 0; 382 struct timespec ts = {TIMO, 0}, sts = {0, 0}; 383 struct apm_power_info pinfo; 384 const char *sockname = _PATH_APM_SOCKET; 385 const char *errstr; 386 int kq, nchanges; 387 struct kevent ev[2]; 388 389 while ((ch = getopt(argc, argv, "aACdHLsf:t:S:z:Z:")) != -1) 390 switch(ch) { 391 case 'a': 392 noacsleep = 1; 393 break; 394 case 'd': 395 debug = 1; 396 break; 397 case 'f': 398 fname = optarg; 399 break; 400 case 'S': 401 sockname = optarg; 402 break; 403 case 't': 404 ts.tv_sec = strtonum(optarg, 1, LLONG_MAX, &errstr); 405 if (errstr != NULL) 406 errx(1, "number of seconds is %s: %s", errstr, 407 optarg); 408 break; 409 case 's': /* status only */ 410 statonly = 1; 411 break; 412 case 'A': 413 case 'C': 414 if (doperf != PERF_NONE) 415 usage(); 416 doperf = PERF_AUTO; 417 setperfpolicy("auto"); 418 break; 419 case 'L': 420 if (doperf != PERF_NONE) 421 usage(); 422 doperf = PERF_MANUAL; 423 setperfpolicy("low"); 424 break; 425 case 'H': 426 if (doperf != PERF_NONE) 427 usage(); 428 doperf = PERF_MANUAL; 429 setperfpolicy("high"); 430 break; 431 case 'Z': 432 autoaction = AUTO_HIBERNATE; 433 autolimit = strtonum(optarg, 1, 100, &errstr); 434 if (errstr != NULL) 435 errx(1, "battery percentage is %s: %s", errstr, 436 optarg); 437 break; 438 case 'z': 439 autoaction = AUTO_SUSPEND; 440 autolimit = strtonum(optarg, 1, 100, &errstr); 441 if (errstr != NULL) 442 errx(1, "battery percentage is %s: %s", errstr, 443 optarg); 444 break; 445 case '?': 446 default: 447 usage(); 448 } 449 450 argc -= optind; 451 argv += optind; 452 453 if (argc != 0) 454 usage(); 455 456 if (doperf == PERF_NONE) 457 doperf = PERF_MANUAL; 458 459 if (debug == 0) { 460 if (daemon(0, 0) == -1) 461 error("failed to daemonize", NULL); 462 openlog(__progname, LOG_CONS, LOG_DAEMON); 463 setlogmask(LOG_UPTO(LOG_NOTICE)); 464 } 465 466 (void) signal(SIGTERM, sigexit); 467 (void) signal(SIGHUP, sigexit); 468 (void) signal(SIGINT, sigexit); 469 470 if ((ctl_fd = open(fname, O_RDWR | O_CLOEXEC)) == -1) { 471 if (errno != ENXIO && errno != ENOENT) 472 error("cannot open device file `%s'", fname); 473 } 474 475 sock_fd = bind_socket(sockname); 476 477 power_status(ctl_fd, 1, &pinfo); 478 479 if (statonly) 480 exit(0); 481 482 set_driver_messages(ctl_fd, APM_PRINT_OFF); 483 484 kq = kqueue(); 485 if (kq <= 0) 486 error("kqueue", NULL); 487 488 EV_SET(&ev[0], sock_fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 489 0, 0, NULL); 490 if (ctl_fd == -1) 491 nchanges = 1; 492 else { 493 EV_SET(&ev[1], ctl_fd, EVFILT_READ, EV_ADD | EV_ENABLE | 494 EV_CLEAR, 0, 0, NULL); 495 nchanges = 2; 496 } 497 if (kevent(kq, ev, nchanges, NULL, 0, &sts) == -1) 498 error("kevent", NULL); 499 500 for (;;) { 501 int rv, event, index; 502 503 sts = ts; 504 505 if ((rv = kevent(kq, NULL, 0, ev, 1, &sts)) == -1) 506 break; 507 508 if (rv == 1 && ev->ident == sock_fd) { 509 switch (handle_client(sock_fd, ctl_fd)) { 510 case NORMAL: 511 break; 512 case SUSPENDING: 513 suspend(ctl_fd); 514 break; 515 case STANDING_BY: 516 stand_by(ctl_fd); 517 break; 518 case HIBERNATING: 519 hibernate(ctl_fd); 520 break; 521 } 522 continue; 523 } 524 525 suspends = standbys = hibernates = resumes = 0; 526 527 if (rv == 0 && ctl_fd == -1) { 528 /* timeout and no way to query status */ 529 continue; 530 } else if (rv == 0) { 531 /* wakeup for timeout: take status */ 532 event = APM_POWER_CHANGE; 533 index = -1; 534 } else { 535 assert(rv == 1 && ev->ident == ctl_fd); 536 event = APM_EVENT_TYPE(ev->data); 537 index = APM_EVENT_INDEX(ev->data); 538 } 539 540 logmsg(LOG_DEBUG, "apmevent %04x index %d", event, index); 541 542 switch (event) { 543 case APM_SUSPEND_REQ: 544 case APM_USER_SUSPEND_REQ: 545 case APM_CRIT_SUSPEND_REQ: 546 case APM_BATTERY_LOW: 547 suspends++; 548 break; 549 case APM_USER_STANDBY_REQ: 550 case APM_STANDBY_REQ: 551 standbys++; 552 break; 553 case APM_USER_HIBERNATE_REQ: 554 hibernates++; 555 break; 556 #if 0 557 case APM_CANCEL: 558 suspends = standbys = 0; 559 break; 560 #endif 561 case APM_NORMAL_RESUME: 562 case APM_CRIT_RESUME: 563 case APM_SYS_STANDBY_RESUME: 564 powerbak = power_status(ctl_fd, 0, &pinfo); 565 if (powerstatus != powerbak) { 566 powerstatus = powerbak; 567 powerchange = 1; 568 } 569 resumes++; 570 break; 571 case APM_POWER_CHANGE: 572 powerbak = power_status(ctl_fd, 0, &pinfo); 573 if (powerstatus != powerbak) { 574 powerstatus = powerbak; 575 powerchange = 1; 576 } 577 578 if (!powerstatus && autoaction && 579 autolimit > (int)pinfo.battery_life) { 580 logmsg(LOG_NOTICE, 581 "estimated battery life %d%%" 582 " below configured limit %d%%", 583 pinfo.battery_life, 584 autolimit 585 ); 586 587 if (autoaction == AUTO_SUSPEND) 588 suspends++; 589 else 590 hibernates++; 591 } 592 break; 593 default: 594 ; 595 } 596 597 if ((standbys || suspends) && noacsleep && 598 power_status(ctl_fd, 0, &pinfo)) 599 logmsg(LOG_DEBUG, "no! sleep! till brooklyn!"); 600 else if (suspends) 601 suspend(ctl_fd); 602 else if (standbys) 603 stand_by(ctl_fd); 604 else if (hibernates) 605 hibernate(ctl_fd); 606 else if (resumes) { 607 resumed(ctl_fd); 608 } 609 610 if (powerchange) { 611 if (powerstatus) 612 do_etc_file(_PATH_APM_ETC_POWERUP); 613 else 614 do_etc_file(_PATH_APM_ETC_POWERDOWN); 615 powerchange = 0; 616 } 617 } 618 error("kevent loop", NULL); 619 620 return 1; 621 } 622 623 void 624 setperfpolicy(char *policy) 625 { 626 int hw_perfpol_mib[] = { CTL_HW, HW_PERFPOLICY }; 627 char oldpolicy[32]; 628 size_t oldsz = sizeof(oldpolicy); 629 int setlo = 0; 630 631 if (strcmp(policy, "low") == 0) { 632 policy = "manual"; 633 setlo = 1; 634 } 635 636 if (sysctl(hw_perfpol_mib, 2, oldpolicy, &oldsz, 637 policy, strlen(policy) + 1) == -1) 638 logmsg(LOG_INFO, "cannot set hw.perfpolicy"); 639 640 if (setlo == 1) { 641 int hw_perf_mib[] = {CTL_HW, HW_SETPERF}; 642 int perf; 643 int new_perf = 0; 644 size_t perf_sz = sizeof(perf); 645 if (sysctl(hw_perf_mib, 2, &perf, &perf_sz, &new_perf, perf_sz) == -1) 646 logmsg(LOG_INFO, "cannot set hw.setperf"); 647 } 648 } 649 650 void 651 do_etc_file(const char *file) 652 { 653 pid_t pid; 654 int status; 655 const char *prog; 656 657 /* If file doesn't exist, do nothing. */ 658 if (access(file, X_OK|R_OK)) { 659 logmsg(LOG_DEBUG, "do_etc_file(): cannot access file %s", file); 660 return; 661 } 662 663 prog = strrchr(file, '/'); 664 if (prog) 665 prog++; 666 else 667 prog = file; 668 669 pid = fork(); 670 switch (pid) { 671 case -1: 672 logmsg(LOG_ERR, "failed to fork(): %s", strerror(errno)); 673 return; 674 case 0: 675 /* We are the child. */ 676 execl(file, prog, (char *)NULL); 677 logmsg(LOG_ERR, "failed to exec %s: %s", file, strerror(errno)); 678 _exit(1); 679 /* NOTREACHED */ 680 default: 681 /* We are the parent. */ 682 wait4(pid, &status, 0, 0); 683 if (WIFEXITED(status)) 684 logmsg(LOG_DEBUG, "%s exited with status %d", file, 685 WEXITSTATUS(status)); 686 else 687 logmsg(LOG_ERR, "%s exited abnormally.", file); 688 } 689 } 690