1 /* $OpenBSD: apmd.c,v 1.53 2009/02/26 17:21:56 oga 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/param.h> 33 #include <sys/stat.h> 34 #include <sys/ioctl.h> 35 #include <sys/socket.h> 36 #include <sys/un.h> 37 #include <sys/wait.h> 38 #include <sys/event.h> 39 #include <sys/time.h> 40 #include <sys/dkstat.h> 41 #include <sys/sysctl.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 <machine/apmvar.h> 52 #include "pathnames.h" 53 #include "apm-proto.h" 54 55 #define TRUE 1 56 #define FALSE 0 57 58 const char apmdev[] = _PATH_APM_CTLDEV; 59 const char sockfile[] = _PATH_APM_SOCKET; 60 61 int debug = 0; 62 63 int doperf = PERF_NONE; 64 #define PERFINC 50 65 #define PERFDEC 20 66 #define PERFMIN 0 67 #define PERFMAX 100 68 #define PERFINCTHRES 10 69 #define PERFDECTHRES 30 70 71 extern char *__progname; 72 73 void usage(void); 74 int power_status(int fd, int force, struct apm_power_info *pinfo); 75 int bind_socket(const char *sn); 76 enum apm_state handle_client(int sock_fd, int ctl_fd); 77 int get_avg_idle_mp(int ncpu); 78 int get_avg_idle_up(void); 79 void perf_status(struct apm_power_info *pinfo, int ncpu); 80 void suspend(int ctl_fd); 81 void stand_by(int ctl_fd); 82 void suspend_req(int ctl_fd); 83 void stand_by_req(int ctl_fd); 84 void setperf(int new_perf); 85 void sigexit(int signo); 86 void do_etc_file(const char *file); 87 void sockunlink(void); 88 89 /* ARGSUSED */ 90 void 91 sigexit(int signo) 92 { 93 sockunlink(); 94 _exit(1); 95 } 96 97 void 98 usage(void) 99 { 100 fprintf(stderr, 101 "usage: %s [-AaCdeHLmps] [-f devname] [-S sockname] [-t seconds]\n", 102 __progname); 103 exit(1); 104 } 105 106 void 107 error(const char *fmt, const char *arg) 108 { 109 char buf[128]; 110 111 if (debug) 112 err(1, fmt, arg); 113 else { 114 strlcpy(buf, fmt, sizeof(buf)); 115 strlcat(buf, ": %m", sizeof(buf)); 116 syslog(LOG_ERR, buf, arg); 117 exit(1); 118 } 119 } 120 121 122 /* 123 * tell the driver if it should display messages or not. 124 */ 125 void 126 set_driver_messages(int fd, int mode) 127 { 128 if (ioctl(fd, APM_IOC_PRN_CTL, &mode) == -1) 129 syslog(LOG_DEBUG, "can't disable driver messages, error: %m"); 130 } 131 132 int 133 power_status(int fd, int force, struct apm_power_info *pinfo) 134 { 135 struct apm_power_info bstate; 136 static struct apm_power_info last; 137 int acon = 0; 138 139 if (fd == -1) { 140 if (pinfo) { 141 bstate.battery_state = 255; 142 bstate.ac_state = 255; 143 bstate.battery_life = 0; 144 bstate.minutes_left = -1; 145 *pinfo = bstate; 146 } 147 148 return 0; 149 } 150 151 if (ioctl(fd, APM_IOC_GETPOWER, &bstate) == 0) { 152 /* various conditions under which we report status: something changed 153 * enough since last report, or asked to force a print */ 154 if (bstate.ac_state == APM_AC_ON) 155 acon = 1; 156 if (force || 157 bstate.ac_state != last.ac_state || 158 bstate.battery_state != last.battery_state || 159 (bstate.minutes_left && bstate.minutes_left < 15) || 160 abs(bstate.battery_life - last.battery_life) > 20) { 161 #ifdef __powerpc__ 162 /* 163 * When the battery is charging, the estimated life 164 * time is in fact the estimated remaining charge time 165 * on Apple machines, so lie in the stats. 166 * We still want an useful message if the battery or 167 * ac status changes, however. 168 */ 169 if (bstate.minutes_left != 0 && 170 bstate.battery_state != APM_BATT_CHARGING) 171 #else 172 if ((int)bstate.minutes_left > 0) 173 #endif 174 syslog(LOG_NOTICE, "battery status: %s. " 175 "external power status: %s. " 176 "estimated battery life %d%% (%u minutes)", 177 battstate(bstate.battery_state), 178 ac_state(bstate.ac_state), 179 bstate.battery_life, 180 bstate.minutes_left); 181 else 182 syslog(LOG_NOTICE, "battery status: %s. " 183 "external power status: %s. " 184 "estimated battery life %d%%", 185 battstate(bstate.battery_state), 186 ac_state(bstate.ac_state), 187 bstate.battery_life); 188 last = bstate; 189 } 190 if (pinfo) 191 *pinfo = bstate; 192 } else 193 syslog(LOG_ERR, "cannot fetch power status: %m"); 194 195 return acon; 196 } 197 198 /* multi- and uni-processor case */ 199 int 200 get_avg_idle_mp(int ncpu) 201 { 202 static int64_t **cp_time_old; 203 static int64_t **cp_time; 204 static int *avg_idle; 205 int64_t change, sum, idle; 206 int i, cpu, min_avg_idle; 207 size_t cp_time_sz = CPUSTATES * sizeof(int64_t); 208 209 if (!cp_time_old) 210 if ((cp_time_old = calloc(sizeof(int64_t *), ncpu)) == NULL) 211 return -1; 212 213 if (!cp_time) 214 if ((cp_time = calloc(sizeof(int64_t *), ncpu)) == NULL) 215 return -1; 216 217 if (!avg_idle) 218 if ((avg_idle = calloc(sizeof(int), ncpu)) == NULL) 219 return -1; 220 221 min_avg_idle = 0; 222 for (cpu = 0; cpu < ncpu; cpu++) { 223 int cp_time_mib[] = {CTL_KERN, KERN_CPTIME2, cpu}; 224 225 if (!cp_time_old[cpu]) 226 if ((cp_time_old[cpu] = 227 calloc(sizeof(int64_t), CPUSTATES)) == NULL) 228 return -1; 229 230 if (!cp_time[cpu]) 231 if ((cp_time[cpu] = 232 calloc(sizeof(int64_t), CPUSTATES)) == NULL) 233 return -1; 234 235 if (sysctl(cp_time_mib, 3, cp_time[cpu], &cp_time_sz, NULL, 0) 236 < 0) 237 syslog(LOG_INFO, "cannot read kern.cp_time2"); 238 239 sum = 0; 240 for (i = 0; i < CPUSTATES; i++) { 241 if ((change = cp_time[cpu][i] - cp_time_old[cpu][i]) 242 < 0) { 243 /* counter wrapped */ 244 change = ((uint64_t)cp_time[cpu][i] - 245 (uint64_t)cp_time_old[cpu][i]); 246 } 247 sum += change; 248 if (i == CP_IDLE) 249 idle = change; 250 } 251 if (sum == 0) 252 sum = 1; 253 254 /* smooth data */ 255 avg_idle[cpu] = (avg_idle[cpu] + (100 * idle) / sum) / 2; 256 257 if (cpu == 0) 258 min_avg_idle = avg_idle[cpu]; 259 260 if (avg_idle[cpu] < min_avg_idle) 261 min_avg_idle = avg_idle[cpu]; 262 263 memcpy(cp_time_old[cpu], cp_time[cpu], cp_time_sz); 264 } 265 266 return min_avg_idle; 267 } 268 269 int 270 get_avg_idle_up(void) 271 { 272 static long cp_time_old[CPUSTATES]; 273 static int avg_idle; 274 long change, cp_time[CPUSTATES]; 275 int cp_time_mib[] = {CTL_KERN, KERN_CPTIME}; 276 size_t cp_time_sz = sizeof(cp_time); 277 int i, idle, sum = 0; 278 279 if (sysctl(cp_time_mib, 2, &cp_time, &cp_time_sz, NULL, 0) < 0) 280 syslog(LOG_INFO, "cannot read kern.cp_time"); 281 282 for (i = 0; i < CPUSTATES; i++) { 283 if ((change = cp_time[i] - cp_time_old[i]) < 0) { 284 /* counter wrapped */ 285 change = ((unsigned long)cp_time[i] - 286 (unsigned long)cp_time_old[i]); 287 } 288 sum += change; 289 if (i == CP_IDLE) 290 idle = change; 291 } 292 if (sum == 0) 293 sum = 1; 294 295 /* smooth data */ 296 avg_idle = (avg_idle + (100 * idle) / sum) / 2; 297 298 memcpy(cp_time_old, cp_time, sizeof(cp_time_old)); 299 300 return avg_idle; 301 } 302 303 void 304 perf_status(struct apm_power_info *pinfo, int ncpu) 305 { 306 int avg_idle; 307 int hw_perf_mib[] = {CTL_HW, HW_SETPERF}; 308 int perf; 309 int forcehi = 0; 310 size_t perf_sz = sizeof(perf); 311 312 if (ncpu > 1) { 313 avg_idle = get_avg_idle_mp(ncpu); 314 } else { 315 avg_idle = get_avg_idle_up(); 316 } 317 318 if (avg_idle == -1) 319 return; 320 321 switch (doperf) { 322 case PERF_AUTO: 323 /* 324 * force setperf towards the max if we are connected to AC 325 * power and have a battery life greater than 15% 326 */ 327 if (pinfo->ac_state == APM_AC_ON && pinfo->battery_life > 15) 328 forcehi = 1; 329 break; 330 case PERF_COOL: 331 forcehi = 0; 332 break; 333 } 334 335 if (sysctl(hw_perf_mib, 2, &perf, &perf_sz, NULL, 0) < 0) 336 syslog(LOG_INFO, "cannot read hw.setperf"); 337 338 if (forcehi || (avg_idle < PERFINCTHRES && perf < PERFMAX)) { 339 perf += PERFINC; 340 if (perf > PERFMAX) 341 perf = PERFMAX; 342 setperf(perf); 343 } else if (avg_idle > PERFDECTHRES && perf > PERFMIN) { 344 perf -= PERFDEC; 345 if (perf < PERFMIN) 346 perf = PERFMIN; 347 setperf(perf); 348 } 349 } 350 351 char socketname[MAXPATHLEN]; 352 353 void 354 sockunlink(void) 355 { 356 if (socketname[0]) 357 remove(socketname); 358 } 359 360 int 361 bind_socket(const char *sockname) 362 { 363 struct sockaddr_un s_un; 364 mode_t old_umask; 365 int sock; 366 367 sock = socket(AF_UNIX, SOCK_STREAM, 0); 368 if (sock == -1) 369 error("cannot create local socket", NULL); 370 371 s_un.sun_family = AF_UNIX; 372 strncpy(s_un.sun_path, sockname, sizeof(s_un.sun_path)); 373 s_un.sun_len = SUN_LEN(&s_un); 374 375 /* remove it if present, we're moving in */ 376 (void) remove(sockname); 377 378 old_umask = umask(077); 379 if (bind(sock, (struct sockaddr *)&s_un, s_un.sun_len) == -1) 380 error("cannot connect to APM socket", NULL); 381 umask(old_umask); 382 if (chmod(sockname, 0660) == -1 || chown(sockname, 0, 0) == -1) 383 error("cannot set socket mode/owner/group to 660/0/0", NULL); 384 385 listen(sock, 1); 386 strlcpy(socketname, sockname, sizeof socketname); 387 atexit(sockunlink); 388 389 return sock; 390 } 391 392 enum apm_state 393 handle_client(int sock_fd, int ctl_fd) 394 { 395 /* accept a handle from the client, process it, then clean up */ 396 int cli_fd; 397 struct sockaddr_un from; 398 socklen_t fromlen; 399 struct apm_command cmd; 400 struct apm_reply reply; 401 int cpuspeed_mib[] = {CTL_HW, HW_CPUSPEED}; 402 int cpuspeed = 0; 403 size_t cpuspeed_sz = sizeof(cpuspeed); 404 405 fromlen = sizeof(from); 406 cli_fd = accept(sock_fd, (struct sockaddr *)&from, &fromlen); 407 if (cli_fd == -1) { 408 syslog(LOG_INFO, "client accept failure: %m"); 409 return NORMAL; 410 } 411 412 if (recv(cli_fd, &cmd, sizeof(cmd), 0) != sizeof(cmd)) { 413 (void) close(cli_fd); 414 syslog(LOG_INFO, "client size botch"); 415 return NORMAL; 416 } 417 418 if (cmd.vno != APMD_VNO) { 419 close(cli_fd); /* terminate client */ 420 /* no error message, just drop it. */ 421 return NORMAL; 422 } 423 424 power_status(ctl_fd, 0, &reply.batterystate); 425 switch (cmd.action) { 426 case SUSPEND: 427 reply.newstate = SUSPENDING; 428 break; 429 case STANDBY: 430 reply.newstate = STANDING_BY; 431 break; 432 case SETPERF_LOW: 433 doperf = PERF_MANUAL; 434 reply.newstate = NORMAL; 435 syslog(LOG_NOTICE, "setting hw.setperf to %d", PERFMIN); 436 setperf(PERFMIN); 437 break; 438 case SETPERF_HIGH: 439 doperf = PERF_MANUAL; 440 reply.newstate = NORMAL; 441 syslog(LOG_NOTICE, "setting hw.setperf to %d", PERFMAX); 442 setperf(PERFMAX); 443 break; 444 case SETPERF_AUTO: 445 doperf = PERF_AUTO; 446 reply.newstate = NORMAL; 447 syslog(LOG_NOTICE, "setting hw.setperf automatically"); 448 break; 449 case SETPERF_COOL: 450 doperf = PERF_COOL; 451 reply.newstate = NORMAL; 452 syslog(LOG_NOTICE, "setting hw.setperf for cool running"); 453 break; 454 default: 455 reply.newstate = NORMAL; 456 break; 457 } 458 459 if (sysctl(cpuspeed_mib, 2, &cpuspeed, &cpuspeed_sz, NULL, 0) < 0) 460 syslog(LOG_INFO, "cannot read hw.cpuspeed"); 461 462 reply.cpuspeed = cpuspeed; 463 reply.perfmode = doperf; 464 reply.vno = APMD_VNO; 465 if (send(cli_fd, &reply, sizeof(reply), 0) != sizeof(reply)) 466 syslog(LOG_INFO, "client reply botch"); 467 close(cli_fd); 468 469 return reply.newstate; 470 } 471 472 void 473 suspend(int ctl_fd) 474 { 475 do_etc_file(_PATH_APM_ETC_SUSPEND); 476 sync(); 477 sleep(1); 478 ioctl(ctl_fd, APM_IOC_SUSPEND, 0); 479 } 480 481 void 482 stand_by(int ctl_fd) 483 { 484 do_etc_file(_PATH_APM_ETC_STANDBY); 485 sync(); 486 sleep(1); 487 ioctl(ctl_fd, APM_IOC_STANDBY, 0); 488 } 489 490 void 491 suspend_req(int ctl_fd) 492 { 493 /* let the user get their finger off the enter key before we suspend */ 494 sleep(1); 495 ioctl(ctl_fd, APM_IOC_SUSPEND_REQ, 0); 496 /* give X a chance to do things */ 497 sleep(2); 498 } 499 500 void 501 stand_by_req(int ctl_fd) 502 { 503 /* let the user get their finger off the enter key before we suspend */ 504 sleep(1); 505 ioctl(ctl_fd, APM_IOC_STANDBY_REQ, 0); 506 /* give X a chance to do things */ 507 sleep(2); 508 } 509 510 #define TIMO (10*60) /* 10 minutes */ 511 512 int 513 main(int argc, char *argv[]) 514 { 515 const char *fname = apmdev; 516 int ctl_fd, sock_fd, ch, suspends, standbys, resumes; 517 int statonly = 0; 518 int enableonly = 0; 519 int pctonly = 0; 520 int powerstatus = 0, powerbak = 0, powerchange = 0; 521 int messages = 0; 522 int noacsleep = 0; 523 struct timespec ts = {TIMO, 0}, sts = {0, 0}; 524 struct apm_power_info pinfo; 525 time_t apmtimeout = 0; 526 const char *sockname = sockfile; 527 int kq, nchanges; 528 struct kevent ev[2]; 529 int ncpu_mib[2] = { CTL_HW, HW_NCPU }; 530 int ncpu; 531 size_t ncpu_sz = sizeof(ncpu); 532 533 while ((ch = getopt(argc, argv, "aACdHLsepmf:t:S:")) != -1) 534 switch(ch) { 535 case 'a': 536 noacsleep = 1; 537 break; 538 case 'd': 539 debug = 1; 540 break; 541 case 'f': 542 fname = optarg; 543 break; 544 case 'S': 545 sockname = optarg; 546 break; 547 case 't': 548 ts.tv_sec = strtoul(optarg, NULL, 0); 549 if (ts.tv_sec == 0) 550 usage(); 551 break; 552 case 's': /* status only */ 553 statonly = 1; 554 break; 555 case 'e': 556 enableonly = 1; 557 break; 558 case 'p': 559 pctonly = 1; 560 break; 561 case 'A': 562 if (doperf != PERF_NONE) 563 usage(); 564 doperf = PERF_AUTO; 565 break; 566 case 'C': 567 if (doperf != PERF_NONE) 568 usage(); 569 doperf = PERF_COOL; 570 break; 571 case 'L': 572 if (doperf != PERF_NONE) 573 usage(); 574 doperf = PERF_MANUAL; 575 setperf(PERFMIN); 576 break; 577 case 'H': 578 if (doperf != PERF_NONE) 579 usage(); 580 doperf = PERF_MANUAL; 581 setperf(PERFMAX); 582 break; 583 case 'm': 584 messages = 1; 585 break; 586 case '?': 587 default: 588 usage(); 589 } 590 591 argc -= optind; 592 argv += optind; 593 594 if (argc != 0) 595 usage(); 596 597 if (doperf == PERF_NONE) 598 doperf = PERF_MANUAL; 599 600 if (debug) 601 openlog(__progname, LOG_CONS, LOG_LOCAL1); 602 else { 603 daemon(0, 0); 604 openlog(__progname, LOG_CONS, LOG_DAEMON); 605 setlogmask(LOG_UPTO(LOG_NOTICE)); 606 } 607 608 (void) signal(SIGTERM, sigexit); 609 (void) signal(SIGHUP, sigexit); 610 (void) signal(SIGINT, sigexit); 611 612 if ((ctl_fd = open(fname, O_RDWR)) == -1) { 613 if (errno != ENXIO && errno != ENOENT) 614 error("cannot open device file `%s'", fname); 615 } else if (fcntl(ctl_fd, F_SETFD, 1) == -1) 616 error("cannot set close-on-exec for `%s'", fname); 617 618 sock_fd = bind_socket(sockname); 619 620 if (fcntl(sock_fd, F_SETFD, 1) == -1) 621 error("cannot set close-on-exec for the socket", NULL); 622 623 power_status(ctl_fd, 1, &pinfo); 624 625 if (statonly) 626 exit(0); 627 628 if (enableonly) { 629 set_driver_messages(ctl_fd, APM_PRINT_ON); 630 exit(0); 631 } 632 633 if (pctonly) { 634 set_driver_messages(ctl_fd, APM_PRINT_PCT); 635 exit(0); 636 } 637 638 if (!messages) 639 set_driver_messages(ctl_fd, APM_PRINT_OFF); 640 641 kq = kqueue(); 642 if (kq <= 0) 643 error("kqueue", NULL); 644 645 EV_SET(&ev[0], sock_fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 646 0, 0, NULL); 647 if (ctl_fd == -1) 648 nchanges = 1; 649 else { 650 EV_SET(&ev[1], ctl_fd, EVFILT_READ, EV_ADD | EV_ENABLE | 651 EV_CLEAR, 0, 0, NULL); 652 nchanges = 2; 653 } 654 if (kevent(kq, ev, nchanges, NULL, 0, &sts) < 0) 655 error("kevent", NULL); 656 657 if (sysctl(ncpu_mib, 2, &ncpu, &ncpu_sz, NULL, 0) < 0) 658 error("cannot read hw.ncpu", NULL); 659 660 if (doperf == PERF_AUTO || doperf == PERF_COOL) { 661 setperf(0); 662 setperf(100); 663 } 664 for (;;) { 665 int rv; 666 667 sts = ts; 668 669 if (doperf == PERF_AUTO || doperf == PERF_COOL) { 670 sts.tv_sec = 1; 671 perf_status(&pinfo, ncpu); 672 } 673 674 apmtimeout += sts.tv_sec; 675 if ((rv = kevent(kq, NULL, 0, ev, 1, &sts)) < 0) 676 break; 677 678 if (apmtimeout >= ts.tv_sec) { 679 apmtimeout = 0; 680 681 /* wakeup for timeout: take status */ 682 powerbak = power_status(ctl_fd, 0, &pinfo); 683 if (powerstatus != powerbak) { 684 powerstatus = powerbak; 685 powerchange = 1; 686 } 687 } 688 689 if (!rv) 690 continue; 691 692 if (ev->ident == ctl_fd) { 693 suspends = standbys = resumes = 0; 694 syslog(LOG_DEBUG, "apmevent %04x index %d", 695 APM_EVENT_TYPE(ev->data), 696 APM_EVENT_INDEX(ev->data)); 697 698 switch (APM_EVENT_TYPE(ev->data)) { 699 case APM_SUSPEND_REQ: 700 case APM_USER_SUSPEND_REQ: 701 case APM_CRIT_SUSPEND_REQ: 702 case APM_BATTERY_LOW: 703 suspends++; 704 break; 705 case APM_USER_STANDBY_REQ: 706 case APM_STANDBY_REQ: 707 standbys++; 708 break; 709 #if 0 710 case APM_CANCEL: 711 suspends = standbys = 0; 712 break; 713 #endif 714 case APM_NORMAL_RESUME: 715 case APM_CRIT_RESUME: 716 case APM_SYS_STANDBY_RESUME: 717 powerbak = power_status(ctl_fd, 0, &pinfo); 718 if (powerstatus != powerbak) { 719 powerstatus = powerbak; 720 powerchange = 1; 721 } 722 resumes++; 723 break; 724 case APM_POWER_CHANGE: 725 powerbak = power_status(ctl_fd, 0, &pinfo); 726 if (powerstatus != powerbak) { 727 powerstatus = powerbak; 728 powerchange = 1; 729 } 730 break; 731 default: 732 ; 733 } 734 735 if ((standbys || suspends) && noacsleep && 736 power_status(ctl_fd, 0, &pinfo)) 737 syslog(LOG_DEBUG, "no! sleep! till brooklyn!"); 738 else if (suspends) 739 suspend(ctl_fd); 740 else if (standbys) 741 stand_by(ctl_fd); 742 else if (resumes) { 743 do_etc_file(_PATH_APM_ETC_RESUME); 744 syslog(LOG_NOTICE, 745 "system resumed from APM sleep"); 746 } 747 748 if (powerchange) { 749 if (powerstatus) 750 do_etc_file(_PATH_APM_ETC_POWERUP); 751 else 752 do_etc_file(_PATH_APM_ETC_POWERDOWN); 753 powerchange = 0; 754 } 755 756 } else if (ev->ident == sock_fd) 757 switch (handle_client(sock_fd, ctl_fd)) { 758 case NORMAL: 759 break; 760 case SUSPENDING: 761 suspend_req(ctl_fd); 762 break; 763 case STANDING_BY: 764 stand_by_req(ctl_fd); 765 break; 766 } 767 } 768 error("kevent loop", NULL); 769 770 return 1; 771 } 772 773 void 774 setperf(int new_perf) 775 { 776 int hw_perf_mib[] = {CTL_HW, HW_SETPERF}; 777 int perf; 778 size_t perf_sz = sizeof(perf); 779 780 if (sysctl(hw_perf_mib, 2, &perf, &perf_sz, &new_perf, perf_sz) < 0) 781 syslog(LOG_INFO, "cannot set hw.setperf"); 782 } 783 784 void 785 do_etc_file(const char *file) 786 { 787 pid_t pid; 788 int status; 789 const char *prog; 790 791 /* If file doesn't exist, do nothing. */ 792 if (access(file, X_OK|R_OK)) { 793 syslog(LOG_DEBUG, "do_etc_file(): cannot access file %s", file); 794 return; 795 } 796 797 prog = strrchr(file, '/'); 798 if (prog) 799 prog++; 800 else 801 prog = file; 802 803 pid = fork(); 804 switch (pid) { 805 case -1: 806 syslog(LOG_ERR, "failed to fork(): %m"); 807 return; 808 case 0: 809 /* We are the child. */ 810 execl(file, prog, (char *)NULL); 811 _exit(1); 812 /* NOTREACHED */ 813 default: 814 /* We are the parent. */ 815 wait4(pid, &status, 0, 0); 816 if (WIFEXITED(status)) 817 syslog(LOG_DEBUG, "%s exited with status %d", file, 818 WEXITSTATUS(status)); 819 else 820 syslog(LOG_ERR, "%s exited abnormally.", file); 821 } 822 } 823