1 /* $OpenBSD: apmd.c,v 1.57 2011/04/21 06:45:04 jasper 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 setperf(int new_perf); 83 void sigexit(int signo); 84 void do_etc_file(const char *file); 85 void sockunlink(void); 86 87 /* ARGSUSED */ 88 void 89 sigexit(int signo) 90 { 91 sockunlink(); 92 _exit(1); 93 } 94 95 void 96 usage(void) 97 { 98 fprintf(stderr, 99 "usage: %s [-AaCdHLs] [-f devname] [-S sockname] [-t seconds]\n", 100 __progname); 101 exit(1); 102 } 103 104 void 105 error(const char *fmt, const char *arg) 106 { 107 char buf[128]; 108 109 if (debug) 110 err(1, fmt, arg); 111 else { 112 strlcpy(buf, fmt, sizeof(buf)); 113 strlcat(buf, ": %m", sizeof(buf)); 114 syslog(LOG_ERR, buf, arg); 115 exit(1); 116 } 117 } 118 119 120 /* 121 * tell the driver if it should display messages or not. 122 */ 123 void 124 set_driver_messages(int fd, int mode) 125 { 126 if (ioctl(fd, APM_IOC_PRN_CTL, &mode) == -1) 127 syslog(LOG_DEBUG, "can't disable driver messages, error: %m"); 128 } 129 130 int 131 power_status(int fd, int force, struct apm_power_info *pinfo) 132 { 133 struct apm_power_info bstate; 134 static struct apm_power_info last; 135 int acon = 0; 136 137 if (fd == -1) { 138 if (pinfo) { 139 bstate.battery_state = 255; 140 bstate.ac_state = 255; 141 bstate.battery_life = 0; 142 bstate.minutes_left = -1; 143 *pinfo = bstate; 144 } 145 146 return 0; 147 } 148 149 if (ioctl(fd, APM_IOC_GETPOWER, &bstate) == 0) { 150 /* various conditions under which we report status: something changed 151 * enough since last report, or asked to force a print */ 152 if (bstate.ac_state == APM_AC_ON) 153 acon = 1; 154 if (force || 155 bstate.ac_state != last.ac_state || 156 bstate.battery_state != last.battery_state || 157 (bstate.minutes_left && bstate.minutes_left < 15) || 158 abs(bstate.battery_life - last.battery_life) > 20) { 159 #ifdef __powerpc__ 160 /* 161 * When the battery is charging, the estimated life 162 * time is in fact the estimated remaining charge time 163 * on Apple machines, so lie in the stats. 164 * We still want an useful message if the battery or 165 * ac status changes, however. 166 */ 167 if (bstate.minutes_left != 0 && 168 bstate.battery_state != APM_BATT_CHARGING) 169 #else 170 if ((int)bstate.minutes_left > 0) 171 #endif 172 syslog(LOG_NOTICE, "battery status: %s. " 173 "external power status: %s. " 174 "estimated battery life %d%% (%u minutes)", 175 battstate(bstate.battery_state), 176 ac_state(bstate.ac_state), 177 bstate.battery_life, 178 bstate.minutes_left); 179 else 180 syslog(LOG_NOTICE, "battery status: %s. " 181 "external power status: %s. " 182 "estimated battery life %d%%", 183 battstate(bstate.battery_state), 184 ac_state(bstate.ac_state), 185 bstate.battery_life); 186 last = bstate; 187 } 188 if (pinfo) 189 *pinfo = bstate; 190 } else 191 syslog(LOG_ERR, "cannot fetch power status: %m"); 192 193 return acon; 194 } 195 196 /* multi- and uni-processor case */ 197 int 198 get_avg_idle_mp(int ncpu) 199 { 200 static int64_t **cp_time_old; 201 static int64_t **cp_time; 202 static int *avg_idle; 203 int64_t change, sum, idle; 204 int i, cpu, min_avg_idle; 205 size_t cp_time_sz = CPUSTATES * sizeof(int64_t); 206 207 if (!cp_time_old) 208 if ((cp_time_old = calloc(sizeof(int64_t *), ncpu)) == NULL) 209 return -1; 210 211 if (!cp_time) 212 if ((cp_time = calloc(sizeof(int64_t *), ncpu)) == NULL) 213 return -1; 214 215 if (!avg_idle) 216 if ((avg_idle = calloc(sizeof(int), ncpu)) == NULL) 217 return -1; 218 219 min_avg_idle = 0; 220 for (cpu = 0; cpu < ncpu; cpu++) { 221 int cp_time_mib[] = {CTL_KERN, KERN_CPTIME2, cpu}; 222 223 if (!cp_time_old[cpu]) 224 if ((cp_time_old[cpu] = 225 calloc(sizeof(int64_t), CPUSTATES)) == NULL) 226 return -1; 227 228 if (!cp_time[cpu]) 229 if ((cp_time[cpu] = 230 calloc(sizeof(int64_t), CPUSTATES)) == NULL) 231 return -1; 232 233 if (sysctl(cp_time_mib, 3, cp_time[cpu], &cp_time_sz, NULL, 0) 234 < 0) 235 syslog(LOG_INFO, "cannot read kern.cp_time2"); 236 237 sum = 0; 238 for (i = 0; i < CPUSTATES; i++) { 239 if ((change = cp_time[cpu][i] - cp_time_old[cpu][i]) 240 < 0) { 241 /* counter wrapped */ 242 change = ((uint64_t)cp_time[cpu][i] - 243 (uint64_t)cp_time_old[cpu][i]); 244 } 245 sum += change; 246 if (i == CP_IDLE) 247 idle = change; 248 } 249 if (sum == 0) 250 sum = 1; 251 252 /* smooth data */ 253 avg_idle[cpu] = (avg_idle[cpu] + (100 * idle) / sum) / 2; 254 255 if (cpu == 0) 256 min_avg_idle = avg_idle[cpu]; 257 258 if (avg_idle[cpu] < min_avg_idle) 259 min_avg_idle = avg_idle[cpu]; 260 261 memcpy(cp_time_old[cpu], cp_time[cpu], cp_time_sz); 262 } 263 264 return min_avg_idle; 265 } 266 267 int 268 get_avg_idle_up(void) 269 { 270 static long cp_time_old[CPUSTATES]; 271 static int avg_idle; 272 long change, cp_time[CPUSTATES]; 273 int cp_time_mib[] = {CTL_KERN, KERN_CPTIME}; 274 size_t cp_time_sz = sizeof(cp_time); 275 int i, idle, sum = 0; 276 277 if (sysctl(cp_time_mib, 2, &cp_time, &cp_time_sz, NULL, 0) < 0) 278 syslog(LOG_INFO, "cannot read kern.cp_time"); 279 280 for (i = 0; i < CPUSTATES; i++) { 281 if ((change = cp_time[i] - cp_time_old[i]) < 0) { 282 /* counter wrapped */ 283 change = ((unsigned long)cp_time[i] - 284 (unsigned long)cp_time_old[i]); 285 } 286 sum += change; 287 if (i == CP_IDLE) 288 idle = change; 289 } 290 if (sum == 0) 291 sum = 1; 292 293 /* smooth data */ 294 avg_idle = (avg_idle + (100 * idle) / sum) / 2; 295 296 memcpy(cp_time_old, cp_time, sizeof(cp_time_old)); 297 298 return avg_idle; 299 } 300 301 void 302 perf_status(struct apm_power_info *pinfo, int ncpu) 303 { 304 int avg_idle; 305 int hw_perf_mib[] = {CTL_HW, HW_SETPERF}; 306 int perf; 307 int forcehi = 0; 308 size_t perf_sz = sizeof(perf); 309 310 if (ncpu > 1) { 311 avg_idle = get_avg_idle_mp(ncpu); 312 } else { 313 avg_idle = get_avg_idle_up(); 314 } 315 316 if (avg_idle == -1) 317 return; 318 319 switch (doperf) { 320 case PERF_AUTO: 321 /* 322 * force setperf towards the max if we are connected to AC 323 * power and have a battery life greater than 15%, or if 324 * the battery is absent 325 */ 326 if (pinfo->ac_state == APM_AC_ON && pinfo->battery_life > 15 || 327 pinfo->battery_state == APM_BATTERY_ABSENT) 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 #define TIMO (10*60) /* 10 minutes */ 491 492 int 493 main(int argc, char *argv[]) 494 { 495 const char *fname = apmdev; 496 int ctl_fd, sock_fd, ch, suspends, standbys, resumes; 497 int statonly = 0; 498 int powerstatus = 0, powerbak = 0, powerchange = 0; 499 int noacsleep = 0; 500 struct timespec ts = {TIMO, 0}, sts = {0, 0}; 501 struct apm_power_info pinfo; 502 time_t apmtimeout = 0; 503 const char *sockname = sockfile; 504 int kq, nchanges; 505 struct kevent ev[2]; 506 int ncpu_mib[2] = { CTL_HW, HW_NCPU }; 507 int ncpu; 508 size_t ncpu_sz = sizeof(ncpu); 509 510 while ((ch = getopt(argc, argv, "aACdHLsf:t:S:")) != -1) 511 switch(ch) { 512 case 'a': 513 noacsleep = 1; 514 break; 515 case 'd': 516 debug = 1; 517 break; 518 case 'f': 519 fname = optarg; 520 break; 521 case 'S': 522 sockname = optarg; 523 break; 524 case 't': 525 ts.tv_sec = strtoul(optarg, NULL, 0); 526 if (ts.tv_sec == 0) 527 usage(); 528 break; 529 case 's': /* status only */ 530 statonly = 1; 531 break; 532 case 'A': 533 if (doperf != PERF_NONE) 534 usage(); 535 doperf = PERF_AUTO; 536 break; 537 case 'C': 538 if (doperf != PERF_NONE) 539 usage(); 540 doperf = PERF_COOL; 541 break; 542 case 'L': 543 if (doperf != PERF_NONE) 544 usage(); 545 doperf = PERF_MANUAL; 546 setperf(PERFMIN); 547 break; 548 case 'H': 549 if (doperf != PERF_NONE) 550 usage(); 551 doperf = PERF_MANUAL; 552 setperf(PERFMAX); 553 break; 554 case '?': 555 default: 556 usage(); 557 } 558 559 argc -= optind; 560 argv += optind; 561 562 if (argc != 0) 563 usage(); 564 565 if (doperf == PERF_NONE) 566 doperf = PERF_MANUAL; 567 568 if (debug) 569 openlog(__progname, LOG_CONS, LOG_LOCAL1); 570 else { 571 if (daemon(0, 0) < 0) 572 error("failed to daemonize", NULL); 573 openlog(__progname, LOG_CONS, LOG_DAEMON); 574 setlogmask(LOG_UPTO(LOG_NOTICE)); 575 } 576 577 (void) signal(SIGTERM, sigexit); 578 (void) signal(SIGHUP, sigexit); 579 (void) signal(SIGINT, sigexit); 580 581 if ((ctl_fd = open(fname, O_RDWR)) == -1) { 582 if (errno != ENXIO && errno != ENOENT) 583 error("cannot open device file `%s'", fname); 584 } else if (fcntl(ctl_fd, F_SETFD, 1) == -1) 585 error("cannot set close-on-exec for `%s'", fname); 586 587 sock_fd = bind_socket(sockname); 588 589 if (fcntl(sock_fd, F_SETFD, 1) == -1) 590 error("cannot set close-on-exec for the socket", NULL); 591 592 power_status(ctl_fd, 1, &pinfo); 593 594 if (statonly) 595 exit(0); 596 597 set_driver_messages(ctl_fd, APM_PRINT_OFF); 598 599 kq = kqueue(); 600 if (kq <= 0) 601 error("kqueue", NULL); 602 603 EV_SET(&ev[0], sock_fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 604 0, 0, NULL); 605 if (ctl_fd == -1) 606 nchanges = 1; 607 else { 608 EV_SET(&ev[1], ctl_fd, EVFILT_READ, EV_ADD | EV_ENABLE | 609 EV_CLEAR, 0, 0, NULL); 610 nchanges = 2; 611 } 612 if (kevent(kq, ev, nchanges, NULL, 0, &sts) < 0) 613 error("kevent", NULL); 614 615 if (sysctl(ncpu_mib, 2, &ncpu, &ncpu_sz, NULL, 0) < 0) 616 error("cannot read hw.ncpu", NULL); 617 618 if (doperf == PERF_AUTO || doperf == PERF_COOL) { 619 setperf(0); 620 setperf(100); 621 } 622 for (;;) { 623 int rv; 624 625 sts = ts; 626 627 if (doperf == PERF_AUTO || doperf == PERF_COOL) { 628 sts.tv_sec = 1; 629 perf_status(&pinfo, ncpu); 630 } 631 632 apmtimeout += sts.tv_sec; 633 if ((rv = kevent(kq, NULL, 0, ev, 1, &sts)) < 0) 634 break; 635 636 if (apmtimeout >= ts.tv_sec) { 637 apmtimeout = 0; 638 639 /* wakeup for timeout: take status */ 640 powerbak = power_status(ctl_fd, 0, &pinfo); 641 if (powerstatus != powerbak) { 642 powerstatus = powerbak; 643 powerchange = 1; 644 } 645 } 646 647 if (!rv) 648 continue; 649 650 if (ev->ident == ctl_fd) { 651 suspends = standbys = resumes = 0; 652 syslog(LOG_DEBUG, "apmevent %04x index %d", 653 APM_EVENT_TYPE(ev->data), 654 APM_EVENT_INDEX(ev->data)); 655 656 switch (APM_EVENT_TYPE(ev->data)) { 657 case APM_SUSPEND_REQ: 658 case APM_USER_SUSPEND_REQ: 659 case APM_CRIT_SUSPEND_REQ: 660 case APM_BATTERY_LOW: 661 suspends++; 662 break; 663 case APM_USER_STANDBY_REQ: 664 case APM_STANDBY_REQ: 665 standbys++; 666 break; 667 #if 0 668 case APM_CANCEL: 669 suspends = standbys = 0; 670 break; 671 #endif 672 case APM_NORMAL_RESUME: 673 case APM_CRIT_RESUME: 674 case APM_SYS_STANDBY_RESUME: 675 powerbak = power_status(ctl_fd, 0, &pinfo); 676 if (powerstatus != powerbak) { 677 powerstatus = powerbak; 678 powerchange = 1; 679 } 680 resumes++; 681 break; 682 case APM_POWER_CHANGE: 683 powerbak = power_status(ctl_fd, 0, &pinfo); 684 if (powerstatus != powerbak) { 685 powerstatus = powerbak; 686 powerchange = 1; 687 } 688 break; 689 default: 690 ; 691 } 692 693 if ((standbys || suspends) && noacsleep && 694 power_status(ctl_fd, 0, &pinfo)) 695 syslog(LOG_DEBUG, "no! sleep! till brooklyn!"); 696 else if (suspends) 697 suspend(ctl_fd); 698 else if (standbys) 699 stand_by(ctl_fd); 700 else if (resumes) { 701 do_etc_file(_PATH_APM_ETC_RESUME); 702 syslog(LOG_NOTICE, 703 "system resumed from APM sleep"); 704 } 705 706 if (powerchange) { 707 if (powerstatus) 708 do_etc_file(_PATH_APM_ETC_POWERUP); 709 else 710 do_etc_file(_PATH_APM_ETC_POWERDOWN); 711 powerchange = 0; 712 } 713 714 } else if (ev->ident == sock_fd) 715 switch (handle_client(sock_fd, ctl_fd)) { 716 case NORMAL: 717 break; 718 case SUSPENDING: 719 suspend(ctl_fd); 720 break; 721 case STANDING_BY: 722 stand_by(ctl_fd); 723 break; 724 } 725 } 726 error("kevent loop", NULL); 727 728 return 1; 729 } 730 731 void 732 setperf(int new_perf) 733 { 734 int hw_perf_mib[] = {CTL_HW, HW_SETPERF}; 735 int perf; 736 size_t perf_sz = sizeof(perf); 737 738 if (sysctl(hw_perf_mib, 2, &perf, &perf_sz, &new_perf, perf_sz) < 0) 739 syslog(LOG_INFO, "cannot set hw.setperf"); 740 } 741 742 void 743 do_etc_file(const char *file) 744 { 745 pid_t pid; 746 int status; 747 const char *prog; 748 749 /* If file doesn't exist, do nothing. */ 750 if (access(file, X_OK|R_OK)) { 751 syslog(LOG_DEBUG, "do_etc_file(): cannot access file %s", file); 752 return; 753 } 754 755 prog = strrchr(file, '/'); 756 if (prog) 757 prog++; 758 else 759 prog = file; 760 761 pid = fork(); 762 switch (pid) { 763 case -1: 764 syslog(LOG_ERR, "failed to fork(): %m"); 765 return; 766 case 0: 767 /* We are the child. */ 768 execl(file, prog, (char *)NULL); 769 _exit(1); 770 /* NOTREACHED */ 771 default: 772 /* We are the parent. */ 773 wait4(pid, &status, 0, 0); 774 if (WIFEXITED(status)) 775 syslog(LOG_DEBUG, "%s exited with status %d", file, 776 WEXITSTATUS(status)); 777 else 778 syslog(LOG_ERR, "%s exited abnormally.", file); 779 } 780 } 781