2 * APM (Advanced Power Management) Event Dispatcher
4 * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
5 * Copyright (c) 1999 KOIE Hidetaka <koie@suri.co.jp>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char rcsid[] =
36 #include <bitstring.h>
47 #include <sys/ioctl.h>
48 #include <sys/types.h>
51 #include <machine/apm_bios.h>
55 extern int yyparse(void);
59 const char *apmd_configfile = APMD_CONFIGFILE;
60 const char *apmd_pidfile = APMD_PIDFILE;
61 int apmctl_fd = -1, apmnorm_fd = -1;
64 * table of event handlers
66 #define EVENT_CONFIG_INITIALIZER(EV,R) { #EV, NULL, R },
67 struct event_config events[EVENT_MAX] = {
68 EVENT_CONFIG_INITIALIZER(NOEVENT, 0)
69 EVENT_CONFIG_INITIALIZER(STANDBYREQ, 1)
70 EVENT_CONFIG_INITIALIZER(SUSPENDREQ, 1)
71 EVENT_CONFIG_INITIALIZER(NORMRESUME, 0)
72 EVENT_CONFIG_INITIALIZER(CRITRESUME, 0)
73 EVENT_CONFIG_INITIALIZER(BATTERYLOW, 0)
74 EVENT_CONFIG_INITIALIZER(POWERSTATECHANGE, 0)
75 EVENT_CONFIG_INITIALIZER(UPDATETIME, 0)
76 EVENT_CONFIG_INITIALIZER(CRITSUSPEND, 1)
77 EVENT_CONFIG_INITIALIZER(USERSTANDBYREQ, 1)
78 EVENT_CONFIG_INITIALIZER(USERSUSPENDREQ, 1)
79 EVENT_CONFIG_INITIALIZER(STANDBYRESUME, 0)
80 EVENT_CONFIG_INITIALIZER(CAPABILITIESCHANGE, 0)
84 * List of battery events
86 struct battery_watch_event *battery_watch_list = NULL;
88 #define BATT_CHK_INTV 10 /* how many seconds between battery state checks? */
94 event_cmd_default_clone(void *this)
96 struct event_cmd * oldone = this;
97 struct event_cmd * newone = malloc(oldone->len);
100 newone->len = oldone->len;
101 newone->name = oldone->name;
102 newone->op = oldone->op;
110 event_cmd_exec_act(void *this)
112 struct event_cmd_exec * p = this;
116 switch ((pid = fork())) {
118 (void) warn("cannot fork");
122 execl(_PATH_BSHELL, "sh", "-c", p->line, (char *)NULL);
127 pid = waitpid(pid, &status, 0);
128 } while (pid == -1 && errno == EINTR);
135 event_cmd_exec_dump(void *this, FILE *fp)
137 fprintf(fp, " \"%s\"", ((struct event_cmd_exec *)this)->line);
140 event_cmd_exec_clone(void *this)
142 struct event_cmd_exec * newone = (struct event_cmd_exec *) event_cmd_default_clone(this);
143 struct event_cmd_exec * oldone = this;
145 newone->evcmd.next = NULL;
146 newone->evcmd.len = oldone->evcmd.len;
147 newone->evcmd.name = oldone->evcmd.name;
148 newone->evcmd.op = oldone->evcmd.op;
149 if ((newone->line = strdup(oldone->line)) == NULL)
150 err(1, "out of memory");
151 return (struct event_cmd *) newone;
154 event_cmd_exec_free(void *this)
156 free(((struct event_cmd_exec *)this)->line);
158 struct event_cmd_op event_cmd_exec_ops = {
161 event_cmd_exec_clone,
169 event_cmd_reject_act(void *this)
173 if (ioctl(apmctl_fd, APMIO_REJECTLASTREQ, NULL)) {
174 syslog(LOG_NOTICE, "fail to reject\n");
181 struct event_cmd_op event_cmd_reject_ops = {
182 event_cmd_reject_act,
184 event_cmd_default_clone,
189 * manipulate event_config
192 clone_event_cmd_list(struct event_cmd *p)
194 struct event_cmd dummy;
195 struct event_cmd *q = &dummy;
196 for ( ;p; p = p->next) {
197 assert(p->op->clone);
198 if ((q->next = p->op->clone(p)) == NULL)
199 (void) err(1, "out of memory");
206 free_event_cmd_list(struct event_cmd *p)
208 struct event_cmd * q;
217 register_battery_handlers(
218 int level, int direction,
219 struct event_cmd *cmdlist)
222 * level is negative if it's in "minutes", non-negative if
225 * direction =1 means we care about this level when charging,
226 * direction =-1 means we care about it when discharging.
228 if (level>100) /* percentage > 100 */
230 if (abs(direction) != 1) /* nonsense direction value */
234 struct battery_watch_event *we;
236 if ((we = malloc(sizeof(struct battery_watch_event))) == NULL)
237 (void) err(1, "out of memory");
239 we->next = battery_watch_list; /* starts at NULL */
240 battery_watch_list = we;
241 we->level = abs(level);
242 we->type = (level<0)?BATTERY_MINUTES:BATTERY_PERCENT;
243 we->direction = (direction<0)?BATTERY_DISCHARGING:
246 we->cmdlist = clone_event_cmd_list(cmdlist);
251 register_apm_event_handlers(
252 bitstr_t bit_decl(evlist, EVENT_MAX),
253 struct event_cmd *cmdlist)
256 bitstr_t bit_decl(tmp, EVENT_MAX);
257 memcpy(&tmp, evlist, bitstr_size(EVENT_MAX));
263 bit_ffs(tmp, EVENT_MAX, &n);
266 p = events[n].cmdlist;
267 if ((q = clone_event_cmd_list(cmdlist)) == NULL)
268 (void) err(1, "out of memory");
270 while (p->next != NULL)
274 events[n].cmdlist = q;
286 exec_run_cmd(struct event_cmd *p)
290 for (; p; p = p->next) {
293 syslog(LOG_INFO, "action: %s", p->name);
294 status = p->op->act(p);
296 syslog(LOG_NOTICE, "command finished with %d\n", status);
304 * execute command -- the event version
307 exec_event_cmd(struct event_config *ev)
311 status = exec_run_cmd(ev->cmdlist);
312 if (status && ev->rejectable) {
313 syslog(LOG_ERR, "canceled");
314 (void) event_cmd_reject_act(NULL);
330 if ((yyin = fopen(apmd_configfile, "r")) == NULL) {
331 (void) err(1, "cannot open config file");
335 yydebug = debug_level;
339 (void) err(1, "cannot parse config file");
344 for (i = 0; i < EVENT_MAX; i++) {
345 if (events[i].cmdlist) {
346 u_int event_type = i;
347 if (write(apmctl_fd, &event_type, sizeof(u_int)) == -1) {
348 (void) err(1, "cannot enable event 0x%x", event_type);
358 struct battery_watch_event *q;
360 for (i = 0; i < EVENT_MAX; i++) {
361 struct event_cmd * p;
362 if ((p = events[i].cmdlist)) {
363 fprintf(stderr, "apm_event %s {\n", events[i].name);
364 for ( ; p ; p = p->next) {
365 fprintf(stderr, "\t%s", p->name);
367 p->op->dump(p, stderr);
368 fprintf(stderr, ";\n");
370 fprintf(stderr, "}\n");
373 for (q = battery_watch_list ; q != NULL ; q = q -> next) {
374 struct event_cmd * p;
375 fprintf(stderr, "apm_battery %d%s %s {\n",
377 (q -> type == BATTERY_PERCENT)?"%":"m",
378 (q -> direction == BATTERY_CHARGING)?"charging":
380 for ( p = q -> cmdlist; p ; p = p->next) {
381 fprintf(stderr, "\t%s", p->name);
383 p->op->dump(p, stderr);
384 fprintf(stderr, ";\n");
386 fprintf(stderr, "}\n");
394 struct battery_watch_event *q;
397 for (i = 0; i < EVENT_MAX; i++) {
398 if (events[i].cmdlist) {
399 u_int event_type = i;
400 if (write(apmctl_fd, &event_type, sizeof(u_int)) == -1) {
401 (void) err(1, "cannot disable event 0x%x", event_type);
406 for (i = 0; i < EVENT_MAX; i++) {
407 struct event_cmd * p;
408 if ((p = events[i].cmdlist))
409 free_event_cmd_list(p);
410 events[i].cmdlist = NULL;
413 for( ; battery_watch_list; battery_watch_list = battery_watch_list -> next) {
414 free_event_cmd_list(battery_watch_list->cmdlist);
415 q = battery_watch_list->next;
416 free(battery_watch_list);
417 battery_watch_list = q;
436 FILE *fp = fopen(apmd_pidfile, "w");
439 fprintf(fp, "%d\n", getpid());
447 static int signal_fd[2];
450 enque_signal(int sig)
452 if (write(signal_fd[1], &sig, sizeof sig) != sizeof sig)
453 (void) err(1, "cannot process signal.");
460 while (waitpid(-1, &status, WNOHANG) > 0)
470 while (read(fd, &sig, sizeof sig) == sizeof sig) {
471 syslog(LOG_INFO, "caught signal: %d", sig);
474 syslog(LOG_NOTICE, "restart by SIG");
478 syslog(LOG_NOTICE, "going down on signal %d", sig);
485 (void) warn("unexpected signal(%d) received.", sig);
494 proc_apmevent(int fd)
496 struct apm_event_info apmevent;
498 while (ioctl(fd, APMIO_NEXTEVENT, &apmevent) == 0) {
500 syslog(LOG_NOTICE, "apmevent %04x index %d\n",
501 apmevent.type, apmevent.index);
502 syslog(LOG_INFO, "apm event: %s", events[apmevent.type].name);
504 status = exec_event_cmd(&events[apmevent.type]);
510 #define AC_POWER_STATE ((pw_info.ai_acline == 1) ? BATTERY_CHARGING :\
517 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 (void) 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 (void) 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 last_state = AC_POWER_STATE;
552 for (p = battery_watch_list ; p!=NULL ; p = p -> next)
555 for (p = battery_watch_list ; p != NULL ; p = p -> next)
556 if (p -> direction == AC_POWER_STATE &&
558 ((p -> type == BATTERY_PERCENT &&
559 p -> level == pw_info.ai_batt_life) ||
560 (p -> type == BATTERY_MINUTES &&
561 p -> level == (pw_info.ai_batt_time / 60)))) {
564 syslog(LOG_NOTICE, "Caught battery event: %s, %d%s",
565 (p -> direction == BATTERY_CHARGING)?"charging":"discharging",
567 (p -> type == BATTERY_PERCENT)?"%":" minutes");
570 status = exec_run_cmd(p -> cmdlist);
579 struct sigaction nsa;
581 sigset_t sigmask, osigmask;
583 FD_ZERO(&master_rfds);
584 FD_SET(apmctl_fd, &master_rfds);
585 fdmax = apmctl_fd > fdmax ? apmctl_fd : fdmax;
587 FD_SET(signal_fd[0], &master_rfds);
588 fdmax = signal_fd[0] > fdmax ? signal_fd[0] : fdmax;
590 memset(&nsa, 0, sizeof nsa);
591 nsa.sa_handler = enque_signal;
592 sigfillset(&nsa.sa_mask);
593 nsa.sa_flags = SA_RESTART;
594 sigaction(SIGHUP, &nsa, NULL);
595 sigaction(SIGCHLD, &nsa, NULL);
596 sigaction(SIGTERM, &nsa, NULL);
598 sigemptyset(&sigmask);
599 sigaddset(&sigmask, SIGHUP);
600 sigaddset(&sigmask, SIGCHLD);
601 sigaddset(&sigmask, SIGTERM);
602 sigprocmask(SIG_SETMASK, &sigmask, &osigmask);
609 to.tv_sec = BATT_CHK_INTV;
612 memcpy(&rfds, &master_rfds, sizeof rfds);
613 sigprocmask(SIG_SETMASK, &osigmask, NULL);
614 if ((res=select(fdmax + 1, &rfds, 0, 0, &to)) < 0) {
616 (void) err(1, "select");
618 sigprocmask(SIG_SETMASK, &sigmask, NULL);
620 if (res == 0) { /* time to check the battery */
625 if (FD_ISSET(signal_fd[0], &rfds)) {
626 if (proc_signal(signal_fd[0]) < 0)
630 if (FD_ISSET(apmctl_fd, &rfds))
631 proc_apmevent(apmctl_fd);
638 main(int ac, char* av[])
643 int logopt = LOG_NDELAY | LOG_PID;
645 while ((ch = getopt(ac, av, "df:v")) != EOF) {
652 apmd_configfile = optarg;
658 (void) err(1, "unknown option `%c'", ch);
666 (void) nice(NICE_INCR);
670 logopt |= LOG_PERROR;
672 prog = strrchr(av[0], '/');
673 openlog(prog ? prog+1 : av[0], logopt, LOG_DAEMON);
675 syslog(LOG_NOTICE, "start");
677 if (pipe(signal_fd) < 0)
678 (void) err(1, "pipe");
679 if (fcntl(signal_fd[0], F_SETFL, O_NONBLOCK) < 0)
680 (void) err(1, "fcntl");
682 if ((apmnorm_fd = open(APM_NORM_DEVICEFILE, O_RDWR)) == -1) {
683 (void) err(1, "cannot open device file `%s'", APM_NORM_DEVICEFILE);
686 if ((apmctl_fd = open(APM_CTL_DEVICEFILE, O_RDWR)) == -1) {
687 (void) err(1, "cannot open device file `%s'", APM_CTL_DEVICEFILE);