2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * APM (Advanced Power Management) Event Dispatcher
6 * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
7 * Copyright (c) 1999 KOIE Hidetaka <koie@suri.co.jp>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char rcsid[] =
37 #include <sys/types.h>
39 #include <bitstring.h>
50 #include <sys/ioctl.h>
53 #include <machine/apm_bios.h>
59 int soft_power_state_change = 0;
60 const char *apmd_configfile = APMD_CONFIGFILE;
61 const char *apmd_pidfile = APMD_PIDFILE;
62 int apmctl_fd = -1, apmnorm_fd = -1;
65 * table of event handlers
67 #define EVENT_CONFIG_INITIALIZER(EV,R) { #EV, NULL, R },
68 struct event_config events[EVENT_MAX] = {
69 EVENT_CONFIG_INITIALIZER(NOEVENT, 0)
70 EVENT_CONFIG_INITIALIZER(STANDBYREQ, 1)
71 EVENT_CONFIG_INITIALIZER(SUSPENDREQ, 1)
72 EVENT_CONFIG_INITIALIZER(NORMRESUME, 0)
73 EVENT_CONFIG_INITIALIZER(CRITRESUME, 0)
74 EVENT_CONFIG_INITIALIZER(BATTERYLOW, 0)
75 EVENT_CONFIG_INITIALIZER(POWERSTATECHANGE, 0)
76 EVENT_CONFIG_INITIALIZER(UPDATETIME, 0)
77 EVENT_CONFIG_INITIALIZER(CRITSUSPEND, 1)
78 EVENT_CONFIG_INITIALIZER(USERSTANDBYREQ, 1)
79 EVENT_CONFIG_INITIALIZER(USERSUSPENDREQ, 1)
80 EVENT_CONFIG_INITIALIZER(STANDBYRESUME, 0)
81 EVENT_CONFIG_INITIALIZER(CAPABILITIESCHANGE, 0)
85 * List of battery events
87 struct battery_watch_event *battery_watch_list = NULL;
89 #define BATT_CHK_INTV 10 /* how many seconds between battery state checks? */
95 event_cmd_default_clone(void *this)
97 struct event_cmd * oldone = this;
98 struct event_cmd * newone = malloc(oldone->len);
101 newone->len = oldone->len;
102 newone->name = oldone->name;
103 newone->op = oldone->op;
111 event_cmd_exec_act(void *this)
113 struct event_cmd_exec * p = this;
117 switch ((pid = fork())) {
123 signal(SIGHUP, SIG_DFL);
124 signal(SIGCHLD, SIG_DFL);
125 signal(SIGTERM, SIG_DFL);
126 execl(_PATH_BSHELL, "sh", "-c", p->line, (char *)NULL);
131 pid = waitpid(pid, &status, 0);
132 } while (pid == -1 && errno == EINTR);
138 event_cmd_exec_dump(void *this, FILE *fp)
140 fprintf(fp, " \"%s\"", ((struct event_cmd_exec *)this)->line);
143 event_cmd_exec_clone(void *this)
145 struct event_cmd_exec * newone = (struct event_cmd_exec *) event_cmd_default_clone(this);
146 struct event_cmd_exec * oldone = this;
148 newone->evcmd.next = NULL;
149 newone->evcmd.len = oldone->evcmd.len;
150 newone->evcmd.name = oldone->evcmd.name;
151 newone->evcmd.op = oldone->evcmd.op;
152 if ((newone->line = strdup(oldone->line)) == NULL)
153 err(1, "out of memory");
154 return (struct event_cmd *) newone;
157 event_cmd_exec_free(void *this)
159 free(((struct event_cmd_exec *)this)->line);
161 struct event_cmd_op event_cmd_exec_ops = {
164 event_cmd_exec_clone,
172 event_cmd_reject_act(void *this __unused)
176 if (ioctl(apmctl_fd, APMIO_REJECTLASTREQ, NULL)) {
177 syslog(LOG_NOTICE, "fail to reject\n");
182 struct event_cmd_op event_cmd_reject_ops = {
183 event_cmd_reject_act,
185 event_cmd_default_clone,
190 * manipulate event_config
193 clone_event_cmd_list(struct event_cmd *p)
195 struct event_cmd dummy;
196 struct event_cmd *q = &dummy;
197 for ( ;p; p = p->next) {
198 assert(p->op->clone);
199 if ((q->next = p->op->clone(p)) == NULL)
200 err(1, "out of memory");
207 free_event_cmd_list(struct event_cmd *p)
209 struct event_cmd * q;
218 register_battery_handlers(
219 int level, int direction,
220 struct event_cmd *cmdlist)
223 * level is negative if it's in "minutes", non-negative if
226 * direction =1 means we care about this level when charging,
227 * direction =-1 means we care about it when discharging.
229 if (level>100) /* percentage > 100 */
231 if (abs(direction) != 1) /* nonsense direction value */
235 struct battery_watch_event *we;
237 if ((we = malloc(sizeof(struct battery_watch_event))) == NULL)
238 err(1, "out of memory");
240 we->next = battery_watch_list; /* starts at NULL */
241 battery_watch_list = we;
242 we->level = abs(level);
243 we->type = (level<0)?BATTERY_MINUTES:BATTERY_PERCENT;
244 we->direction = (direction<0)?BATTERY_DISCHARGING:
247 we->cmdlist = clone_event_cmd_list(cmdlist);
252 register_apm_event_handlers(
253 bitstr_t bit_decl(evlist, EVENT_MAX),
254 struct event_cmd *cmdlist)
257 bitstr_t bit_decl(tmp, EVENT_MAX);
258 memcpy(&tmp, evlist, bitstr_size(EVENT_MAX));
264 bit_ffs(tmp, EVENT_MAX, &n);
267 p = events[n].cmdlist;
268 if ((q = clone_event_cmd_list(cmdlist)) == NULL)
269 err(1, "out of memory");
271 while (p->next != NULL)
275 events[n].cmdlist = q;
287 exec_run_cmd(struct event_cmd *p)
291 for (; p; p = p->next) {
294 syslog(LOG_INFO, "action: %s", p->name);
295 status = p->op->act(p);
297 syslog(LOG_NOTICE, "command finished with %d\n", status);
305 * execute command -- the event version
308 exec_event_cmd(struct event_config *ev)
312 status = exec_run_cmd(ev->cmdlist);
313 if (status && ev->rejectable) {
314 syslog(LOG_ERR, "canceled");
315 event_cmd_reject_act(NULL);
331 if ((yyin = fopen(apmd_configfile, "r")) == NULL) {
332 err(1, "cannot open config file");
336 yydebug = debug_level;
340 err(1, "cannot parse config file");
345 for (i = 0; i < EVENT_MAX; i++) {
346 if (events[i].cmdlist) {
347 u_int event_type = i;
348 if (write(apmctl_fd, &event_type, sizeof(u_int)) == -1) {
349 err(1, "cannot enable event 0x%x", event_type);
359 struct battery_watch_event *q;
361 for (i = 0; i < EVENT_MAX; i++) {
362 struct event_cmd * p;
363 if ((p = events[i].cmdlist)) {
364 fprintf(stderr, "apm_event %s {\n", events[i].name);
365 for ( ; p ; p = p->next) {
366 fprintf(stderr, "\t%s", p->name);
368 p->op->dump(p, stderr);
369 fprintf(stderr, ";\n");
371 fprintf(stderr, "}\n");
374 for (q = battery_watch_list ; q != NULL ; q = q -> next) {
375 struct event_cmd * p;
376 fprintf(stderr, "apm_battery %d%s %s {\n",
378 (q -> type == BATTERY_PERCENT)?"%":"m",
379 (q -> direction == BATTERY_CHARGING)?"charging":
381 for ( p = q -> cmdlist; p ; p = p->next) {
382 fprintf(stderr, "\t%s", p->name);
384 p->op->dump(p, stderr);
385 fprintf(stderr, ";\n");
387 fprintf(stderr, "}\n");
395 struct battery_watch_event *q;
398 for (i = 0; i < EVENT_MAX; i++) {
399 if (events[i].cmdlist) {
400 u_int event_type = i;
401 if (write(apmctl_fd, &event_type, sizeof(u_int)) == -1) {
402 err(1, "cannot disable event 0x%x", event_type);
407 for (i = 0; i < EVENT_MAX; i++) {
408 struct event_cmd * p;
409 if ((p = events[i].cmdlist))
410 free_event_cmd_list(p);
411 events[i].cmdlist = NULL;
414 for( ; battery_watch_list; battery_watch_list = battery_watch_list -> next) {
415 free_event_cmd_list(battery_watch_list->cmdlist);
416 q = battery_watch_list->next;
417 free(battery_watch_list);
418 battery_watch_list = q;
437 FILE *fp = fopen(apmd_pidfile, "w");
440 fprintf(fp, "%ld\n", (long)getpid());
448 static int signal_fd[2];
451 enque_signal(int sig)
453 if (write(signal_fd[1], &sig, sizeof sig) != sizeof sig)
454 err(1, "cannot process signal.");
461 while (waitpid(-1, &status, WNOHANG) > 0)
471 while (read(fd, &sig, sizeof sig) == sizeof sig) {
472 syslog(LOG_INFO, "caught signal: %d", sig);
475 syslog(LOG_NOTICE, "restart by SIG");
479 syslog(LOG_NOTICE, "going down on signal %d", sig);
486 warn("unexpected signal(%d) received.", sig);
493 proc_apmevent(int fd)
495 struct apm_event_info apmevent;
497 while (ioctl(fd, APMIO_NEXTEVENT, &apmevent) == 0) {
499 syslog(LOG_NOTICE, "apmevent %04x index %d\n",
500 apmevent.type, apmevent.index);
501 syslog(LOG_INFO, "apm event: %s", events[apmevent.type].name);
503 status = exec_event_cmd(&events[apmevent.type]);
509 #define AC_POWER_STATE ((pw_info.ai_acline == 1) ? BATTERY_CHARGING :\
516 static int first_time=1, last_state;
519 struct apm_info pw_info;
520 struct battery_watch_event *p;
522 /* If we don't care, don't bother */
523 if (battery_watch_list == NULL)
527 if ( ioctl(apmnorm_fd, APMIO_GETINFO, &pw_info) < 0)
528 err(1, "cannot check battery state.");
530 * This next statement isn't entirely true. The spec does not tie AC
531 * line state to battery charging or not, but this is a bit lazier to do.
533 last_state = AC_POWER_STATE;
535 return; /* We can't process events, we have no baseline */
539 * XXX - should we do this a bunch of times and perform some sort
540 * of smoothing or correction?
542 if ( ioctl(apmnorm_fd, APMIO_GETINFO, &pw_info) < 0)
543 err(1, "cannot check battery state.");
546 * If we're not in the state now that we were in last time,
547 * then it's a transition, which means we must clean out
548 * the event-caught state.
550 if (last_state != AC_POWER_STATE) {
551 if (soft_power_state_change && fork() == 0) {
552 status = exec_event_cmd(&events[PMEV_POWERSTATECHANGE]);
555 last_state = AC_POWER_STATE;
556 for (p = battery_watch_list ; p!=NULL ; p = p -> next)
559 for (p = battery_watch_list ; p != NULL ; p = p -> next)
560 if (p -> direction == AC_POWER_STATE &&
562 ((p -> type == BATTERY_PERCENT &&
563 p -> level == (int)pw_info.ai_batt_life) ||
564 (p -> type == BATTERY_MINUTES &&
565 p -> level == (pw_info.ai_batt_time / 60)))) {
568 syslog(LOG_NOTICE, "Caught battery event: %s, %d%s",
569 (p -> direction == BATTERY_CHARGING)?"charging":"discharging",
571 (p -> type == BATTERY_PERCENT)?"%":" minutes");
573 status = exec_run_cmd(p -> cmdlist);
582 struct sigaction nsa;
584 sigset_t sigmask, osigmask;
586 FD_ZERO(&master_rfds);
587 FD_SET(apmctl_fd, &master_rfds);
588 fdmax = apmctl_fd > fdmax ? apmctl_fd : fdmax;
590 FD_SET(signal_fd[0], &master_rfds);
591 fdmax = signal_fd[0] > fdmax ? signal_fd[0] : fdmax;
593 memset(&nsa, 0, sizeof nsa);
594 nsa.sa_handler = enque_signal;
595 sigfillset(&nsa.sa_mask);
596 nsa.sa_flags = SA_RESTART;
597 sigaction(SIGHUP, &nsa, NULL);
598 sigaction(SIGCHLD, &nsa, NULL);
599 sigaction(SIGTERM, &nsa, NULL);
601 sigemptyset(&sigmask);
602 sigaddset(&sigmask, SIGHUP);
603 sigaddset(&sigmask, SIGCHLD);
604 sigaddset(&sigmask, SIGTERM);
605 sigprocmask(SIG_SETMASK, &sigmask, &osigmask);
612 to.tv_sec = BATT_CHK_INTV;
615 memcpy(&rfds, &master_rfds, sizeof rfds);
616 sigprocmask(SIG_SETMASK, &osigmask, NULL);
617 if ((res=select(fdmax + 1, &rfds, 0, 0, &to)) < 0) {
621 sigprocmask(SIG_SETMASK, &sigmask, NULL);
623 if (res == 0) { /* time to check the battery */
628 if (FD_ISSET(signal_fd[0], &rfds)) {
629 if (proc_signal(signal_fd[0]) < 0)
633 if (FD_ISSET(apmctl_fd, &rfds))
634 proc_apmevent(apmctl_fd);
639 main(int ac, char* av[])
644 int logopt = LOG_NDELAY | LOG_PID;
646 while ((ch = getopt(ac, av, "df:sv")) != -1) {
653 apmd_configfile = optarg;
656 soft_power_state_change = 1;
662 err(1, "unknown option `%c'", ch);
674 logopt |= LOG_PERROR;
676 prog = strrchr(av[0], '/');
677 openlog(prog ? prog+1 : av[0], logopt, LOG_DAEMON);
679 syslog(LOG_NOTICE, "start");
681 if (pipe(signal_fd) < 0)
683 if (fcntl(signal_fd[0], F_SETFL, O_NONBLOCK) < 0)
686 if ((apmnorm_fd = open(APM_NORM_DEVICEFILE, O_RDWR)) == -1) {
687 err(1, "cannot open device file `%s'", APM_NORM_DEVICEFILE);
690 if (fcntl(apmnorm_fd, F_SETFD, 1) == -1) {
691 err(1, "cannot set close-on-exec flag for device file '%s'", APM_NORM_DEVICEFILE);
694 if ((apmctl_fd = open(APM_CTL_DEVICEFILE, O_RDWR)) == -1) {
695 err(1, "cannot open device file `%s'", APM_CTL_DEVICEFILE);
698 if (fcntl(apmctl_fd, F_SETFD, 1) == -1) {
699 err(1, "cannot set close-on-exec flag for device file '%s'", APM_CTL_DEVICEFILE);