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>
57 int soft_power_state_change = 0;
58 const char *apmd_configfile = APMD_CONFIGFILE;
59 const char *apmd_pidfile = APMD_PIDFILE;
60 int apmctl_fd = -1, apmnorm_fd = -1;
63 * table of event handlers
65 #define EVENT_CONFIG_INITIALIZER(EV,R) { #EV, NULL, R },
66 struct event_config events[EVENT_MAX] = {
67 EVENT_CONFIG_INITIALIZER(NOEVENT, 0)
68 EVENT_CONFIG_INITIALIZER(STANDBYREQ, 1)
69 EVENT_CONFIG_INITIALIZER(SUSPENDREQ, 1)
70 EVENT_CONFIG_INITIALIZER(NORMRESUME, 0)
71 EVENT_CONFIG_INITIALIZER(CRITRESUME, 0)
72 EVENT_CONFIG_INITIALIZER(BATTERYLOW, 0)
73 EVENT_CONFIG_INITIALIZER(POWERSTATECHANGE, 0)
74 EVENT_CONFIG_INITIALIZER(UPDATETIME, 0)
75 EVENT_CONFIG_INITIALIZER(CRITSUSPEND, 1)
76 EVENT_CONFIG_INITIALIZER(USERSTANDBYREQ, 1)
77 EVENT_CONFIG_INITIALIZER(USERSUSPENDREQ, 1)
78 EVENT_CONFIG_INITIALIZER(STANDBYRESUME, 0)
79 EVENT_CONFIG_INITIALIZER(CAPABILITIESCHANGE, 0)
83 * List of battery events
85 struct battery_watch_event *battery_watch_list = NULL;
87 #define BATT_CHK_INTV 10 /* how many seconds between battery state checks? */
93 event_cmd_default_clone(void *this)
95 struct event_cmd * oldone = this;
96 struct event_cmd * newone = malloc(oldone->len);
99 newone->len = oldone->len;
100 newone->name = oldone->name;
101 newone->op = oldone->op;
109 event_cmd_exec_act(void *this)
111 struct event_cmd_exec * p = this;
115 switch ((pid = fork())) {
121 signal(SIGHUP, SIG_DFL);
122 signal(SIGCHLD, SIG_DFL);
123 signal(SIGTERM, SIG_DFL);
124 execl(_PATH_BSHELL, "sh", "-c", p->line, (char *)NULL);
129 pid = waitpid(pid, &status, 0);
130 } while (pid == -1 && errno == EINTR);
136 event_cmd_exec_dump(void *this, FILE *fp)
138 fprintf(fp, " \"%s\"", ((struct event_cmd_exec *)this)->line);
141 event_cmd_exec_clone(void *this)
143 struct event_cmd_exec * newone = (struct event_cmd_exec *) event_cmd_default_clone(this);
144 struct event_cmd_exec * oldone = this;
146 newone->evcmd.next = NULL;
147 newone->evcmd.len = oldone->evcmd.len;
148 newone->evcmd.name = oldone->evcmd.name;
149 newone->evcmd.op = oldone->evcmd.op;
150 if ((newone->line = strdup(oldone->line)) == NULL)
151 err(1, "out of memory");
152 return (struct event_cmd *) newone;
155 event_cmd_exec_free(void *this)
157 free(((struct event_cmd_exec *)this)->line);
159 struct event_cmd_op event_cmd_exec_ops = {
162 event_cmd_exec_clone,
170 event_cmd_reject_act(void *this __unused)
174 if (ioctl(apmctl_fd, APMIO_REJECTLASTREQ, NULL)) {
175 syslog(LOG_NOTICE, "fail to reject\n");
180 struct event_cmd_op event_cmd_reject_ops = {
181 event_cmd_reject_act,
183 event_cmd_default_clone,
188 * manipulate event_config
191 clone_event_cmd_list(struct event_cmd *p)
193 struct event_cmd dummy;
194 struct event_cmd *q = &dummy;
195 for ( ;p; p = p->next) {
196 assert(p->op->clone);
197 if ((q->next = p->op->clone(p)) == NULL)
198 err(1, "out of memory");
205 free_event_cmd_list(struct event_cmd *p)
207 struct event_cmd * q;
216 register_battery_handlers(
217 int level, int direction,
218 struct event_cmd *cmdlist)
221 * level is negative if it's in "minutes", non-negative if
224 * direction =1 means we care about this level when charging,
225 * direction =-1 means we care about it when discharging.
227 if (level>100) /* percentage > 100 */
229 if (abs(direction) != 1) /* nonsense direction value */
233 struct battery_watch_event *we;
235 if ((we = malloc(sizeof(struct battery_watch_event))) == NULL)
236 err(1, "out of memory");
238 we->next = battery_watch_list; /* starts at NULL */
239 battery_watch_list = we;
240 we->level = abs(level);
241 we->type = (level<0)?BATTERY_MINUTES:BATTERY_PERCENT;
242 we->direction = (direction<0)?BATTERY_DISCHARGING:
245 we->cmdlist = clone_event_cmd_list(cmdlist);
250 register_apm_event_handlers(
251 bitstr_t bit_decl(evlist, EVENT_MAX),
252 struct event_cmd *cmdlist)
255 bitstr_t bit_decl(tmp, EVENT_MAX);
256 memcpy(&tmp, evlist, bitstr_size(EVENT_MAX));
262 bit_ffs(tmp, EVENT_MAX, &n);
265 p = events[n].cmdlist;
266 if ((q = clone_event_cmd_list(cmdlist)) == NULL)
267 err(1, "out of memory");
269 while (p->next != NULL)
273 events[n].cmdlist = q;
285 exec_run_cmd(struct event_cmd *p)
289 for (; p; p = p->next) {
292 syslog(LOG_INFO, "action: %s", p->name);
293 status = p->op->act(p);
295 syslog(LOG_NOTICE, "command finished with %d\n", status);
303 * execute command -- the event version
306 exec_event_cmd(struct event_config *ev)
310 status = exec_run_cmd(ev->cmdlist);
311 if (status && ev->rejectable) {
312 syslog(LOG_ERR, "canceled");
313 event_cmd_reject_act(NULL);
329 if ((yyin = fopen(apmd_configfile, "r")) == NULL) {
330 err(1, "cannot open config file");
334 yydebug = debug_level;
338 err(1, "cannot parse config file");
343 for (i = 0; i < EVENT_MAX; i++) {
344 if (events[i].cmdlist) {
345 u_int event_type = i;
346 if (write(apmctl_fd, &event_type, sizeof(u_int)) == -1) {
347 err(1, "cannot enable event 0x%x", event_type);
357 struct battery_watch_event *q;
359 for (i = 0; i < EVENT_MAX; i++) {
360 struct event_cmd * p;
361 if ((p = events[i].cmdlist)) {
362 fprintf(stderr, "apm_event %s {\n", events[i].name);
363 for ( ; p ; p = p->next) {
364 fprintf(stderr, "\t%s", p->name);
366 p->op->dump(p, stderr);
367 fprintf(stderr, ";\n");
369 fprintf(stderr, "}\n");
372 for (q = battery_watch_list ; q != NULL ; q = q -> next) {
373 struct event_cmd * p;
374 fprintf(stderr, "apm_battery %d%s %s {\n",
376 (q -> type == BATTERY_PERCENT)?"%":"m",
377 (q -> direction == BATTERY_CHARGING)?"charging":
379 for ( p = q -> cmdlist; p ; p = p->next) {
380 fprintf(stderr, "\t%s", p->name);
382 p->op->dump(p, stderr);
383 fprintf(stderr, ";\n");
385 fprintf(stderr, "}\n");
393 struct battery_watch_event *q;
396 for (i = 0; i < EVENT_MAX; i++) {
397 if (events[i].cmdlist) {
398 u_int event_type = i;
399 if (write(apmctl_fd, &event_type, sizeof(u_int)) == -1) {
400 err(1, "cannot disable event 0x%x", event_type);
405 for (i = 0; i < EVENT_MAX; i++) {
406 struct event_cmd * p;
407 if ((p = events[i].cmdlist))
408 free_event_cmd_list(p);
409 events[i].cmdlist = NULL;
412 for( ; battery_watch_list; battery_watch_list = battery_watch_list -> next) {
413 free_event_cmd_list(battery_watch_list->cmdlist);
414 q = battery_watch_list->next;
415 free(battery_watch_list);
416 battery_watch_list = q;
435 FILE *fp = fopen(apmd_pidfile, "w");
438 fprintf(fp, "%ld\n", (long)getpid());
446 static int signal_fd[2];
449 enque_signal(int sig)
451 if (write(signal_fd[1], &sig, sizeof sig) != sizeof sig)
452 err(1, "cannot process signal.");
459 while (waitpid(-1, &status, WNOHANG) > 0)
469 while (read(fd, &sig, sizeof sig) == sizeof sig) {
470 syslog(LOG_INFO, "caught signal: %d", sig);
473 syslog(LOG_NOTICE, "restart by SIG");
477 syslog(LOG_NOTICE, "going down on signal %d", sig);
484 warn("unexpected signal(%d) received.", sig);
491 proc_apmevent(int fd)
493 struct apm_event_info apmevent;
495 while (ioctl(fd, APMIO_NEXTEVENT, &apmevent) == 0) {
497 syslog(LOG_NOTICE, "apmevent %04x index %d\n",
498 apmevent.type, apmevent.index);
499 syslog(LOG_INFO, "apm event: %s", events[apmevent.type].name);
501 status = exec_event_cmd(&events[apmevent.type]);
507 #define AC_POWER_STATE ((pw_info.ai_acline == 1) ? BATTERY_CHARGING :\
514 static int first_time=1, last_state;
517 struct apm_info pw_info;
518 struct battery_watch_event *p;
520 /* If we don't care, don't bother */
521 if (battery_watch_list == NULL)
525 if ( ioctl(apmnorm_fd, APMIO_GETINFO, &pw_info) < 0)
526 err(1, "cannot check battery state.");
528 * This next statement isn't entirely true. The spec does not tie AC
529 * line state to battery charging or not, but this is a bit lazier to do.
531 last_state = AC_POWER_STATE;
533 return; /* We can't process events, we have no baseline */
537 * XXX - should we do this a bunch of times and perform some sort
538 * of smoothing or correction?
540 if ( ioctl(apmnorm_fd, APMIO_GETINFO, &pw_info) < 0)
541 err(1, "cannot check battery state.");
544 * If we're not in the state now that we were in last time,
545 * then it's a transition, which means we must clean out
546 * the event-caught state.
548 if (last_state != AC_POWER_STATE) {
549 if (soft_power_state_change && fork() == 0) {
550 status = exec_event_cmd(&events[PMEV_POWERSTATECHANGE]);
553 last_state = AC_POWER_STATE;
554 for (p = battery_watch_list ; p!=NULL ; p = p -> next)
557 for (p = battery_watch_list ; p != NULL ; p = p -> next)
558 if (p -> direction == AC_POWER_STATE &&
560 ((p -> type == BATTERY_PERCENT &&
561 p -> level == (int)pw_info.ai_batt_life) ||
562 (p -> type == BATTERY_MINUTES &&
563 p -> level == (pw_info.ai_batt_time / 60)))) {
566 syslog(LOG_NOTICE, "Caught battery event: %s, %d%s",
567 (p -> direction == BATTERY_CHARGING)?"charging":"discharging",
569 (p -> type == BATTERY_PERCENT)?"%":" minutes");
571 status = exec_run_cmd(p -> cmdlist);
580 struct sigaction nsa;
582 sigset_t sigmask, osigmask;
584 FD_ZERO(&master_rfds);
585 FD_SET(apmctl_fd, &master_rfds);
586 fdmax = apmctl_fd > fdmax ? apmctl_fd : fdmax;
588 FD_SET(signal_fd[0], &master_rfds);
589 fdmax = signal_fd[0] > fdmax ? signal_fd[0] : fdmax;
591 memset(&nsa, 0, sizeof nsa);
592 nsa.sa_handler = enque_signal;
593 sigfillset(&nsa.sa_mask);
594 nsa.sa_flags = SA_RESTART;
595 sigaction(SIGHUP, &nsa, NULL);
596 sigaction(SIGCHLD, &nsa, NULL);
597 sigaction(SIGTERM, &nsa, NULL);
599 sigemptyset(&sigmask);
600 sigaddset(&sigmask, SIGHUP);
601 sigaddset(&sigmask, SIGCHLD);
602 sigaddset(&sigmask, SIGTERM);
603 sigprocmask(SIG_SETMASK, &sigmask, &osigmask);
610 to.tv_sec = BATT_CHK_INTV;
613 memcpy(&rfds, &master_rfds, sizeof rfds);
614 sigprocmask(SIG_SETMASK, &osigmask, NULL);
615 if ((res=select(fdmax + 1, &rfds, 0, 0, &to)) < 0) {
619 sigprocmask(SIG_SETMASK, &sigmask, NULL);
621 if (res == 0) { /* time to check the battery */
626 if (FD_ISSET(signal_fd[0], &rfds)) {
627 if (proc_signal(signal_fd[0]) < 0)
631 if (FD_ISSET(apmctl_fd, &rfds))
632 proc_apmevent(apmctl_fd);
637 main(int ac, char* av[])
642 int logopt = LOG_NDELAY | LOG_PID;
644 while ((ch = getopt(ac, av, "df:sv")) != -1) {
651 apmd_configfile = optarg;
654 soft_power_state_change = 1;
660 err(1, "unknown option `%c'", ch);
672 logopt |= LOG_PERROR;
674 prog = strrchr(av[0], '/');
675 openlog(prog ? prog+1 : av[0], logopt, LOG_DAEMON);
677 syslog(LOG_NOTICE, "start");
679 if (pipe(signal_fd) < 0)
681 if (fcntl(signal_fd[0], F_SETFL, O_NONBLOCK) < 0)
684 if ((apmnorm_fd = open(APM_NORM_DEVICEFILE, O_RDWR)) == -1) {
685 err(1, "cannot open device file `%s'", APM_NORM_DEVICEFILE);
688 if (fcntl(apmnorm_fd, F_SETFD, 1) == -1) {
689 err(1, "cannot set close-on-exec flag for device file '%s'", APM_NORM_DEVICEFILE);
692 if ((apmctl_fd = open(APM_CTL_DEVICEFILE, O_RDWR)) == -1) {
693 err(1, "cannot open device file `%s'", APM_CTL_DEVICEFILE);
696 if (fcntl(apmctl_fd, F_SETFD, 1) == -1) {
697 err(1, "cannot set close-on-exec flag for device file '%s'", APM_CTL_DEVICEFILE);