1 /* vi: set sw=4 ts=4: */
3 * simple ACPI events listener
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
11 #include <linux/input.h>
19 OPT_p = (1 << 5) * ENABLE_FEATURE_PIDFILE,
33 static const struct acpi_event f_evt_tab[] = {
34 { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRF 00000080" },
35 { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRB 00000080" },
43 static const struct acpi_action f_act_tab[] = {
44 { "PWRF", "PWRF/00000080" },
45 { "LID0", "LID/00000080" },
49 struct acpi_action *act_tab;
51 struct acpi_event *evt_tab;
54 #define G (*ptr_to_globals)
55 #define act_tab (G.act_tab)
56 #define n_act (G.n_act )
57 #define evt_tab (G.evt_tab)
58 #define n_evt (G.n_evt )
59 #define INIT_G() do { \
60 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
64 * acpid listens to ACPI events coming either in textual form
65 * from /proc/acpi/event (though it is marked deprecated,
66 * it is still widely used and _is_ a standard) or in binary form
67 * from specified evdevs (just use /dev/input/event*).
68 * It parses the event to retrieve ACTION and a possible PARAMETER.
69 * It then spawns /etc/acpi/<ACTION>[/<PARAMETER>] either via run-parts
70 * (if the resulting path is a directory) or directly.
71 * If the resulting path does not exist it logs it via perror
72 * and continues listening.
75 static void process_event(const char *event)
78 char *handler = xasprintf("./%s", event);
79 const char *args[] = { "run-parts", handler, NULL };
82 if (option_mask32 & OPT_d) {
83 bb_error_msg("%s", event);
87 // N.B. run-parts would require scripts to have #!/bin/sh
88 // handler is directory? -> use run-parts
89 // handler is file? -> run it directly
90 if (0 == stat(event, &st))
91 spawn((char **)args + (0==(st.st_mode & S_IFDIR)));
93 bb_simple_perror_msg(event);
98 static const char *find_action(struct input_event *ev, const char *buf)
100 const char *action = NULL;
104 for (i = 0; i < n_evt; i++) {
106 if (ev->type == evt_tab[i].n_type && ev->code == evt_tab[i].n_code && ev->value == evt_tab[i].value) {
107 action = evt_tab[i].desc;
113 if (strncmp(buf, evt_tab[i].desc, strlen(buf)) == 0) {
114 action = evt_tab[i].desc;
122 for (i = 0; i < n_act; i++) {
123 if (strstr(action, act_tab[i].key)) {
124 action = act_tab[i].action;
133 static void parse_conf_file(const char *filename)
138 parser = config_open2(filename, fopen_for_read);
141 while (config_read(parser, tokens, 2, 2, "# \t", PARSE_NORMAL)) {
142 act_tab = xrealloc_vector(act_tab, 1, n_act);
143 act_tab[n_act].key = xstrdup(tokens[0]);
144 act_tab[n_act].action = xstrdup(tokens[1]);
147 config_close(parser);
149 act_tab = (void*)f_act_tab;
150 n_act = ARRAY_SIZE(f_act_tab);
154 static void parse_map_file(const char *filename)
159 parser = config_open2(filename, fopen_for_read);
162 while (config_read(parser, tokens, 6, 6, "# \t", PARSE_NORMAL)) {
163 evt_tab = xrealloc_vector(evt_tab, 1, n_evt);
164 evt_tab[n_evt].s_type = xstrdup(tokens[0]);
165 evt_tab[n_evt].n_type = xstrtou(tokens[1], 16);
166 evt_tab[n_evt].s_code = xstrdup(tokens[2]);
167 evt_tab[n_evt].n_code = xatou16(tokens[3]);
168 evt_tab[n_evt].value = xatoi_positive(tokens[4]);
169 evt_tab[n_evt].desc = xstrdup(tokens[5]);
172 config_close(parser);
174 evt_tab = (void*)f_evt_tab;
175 n_evt = ARRAY_SIZE(f_evt_tab);
180 * acpid [-c conf_dir] [-r conf_file ] [-a map_file ] [-l log_file] [-e proc_event_file]
183 int acpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
184 int acpid_main(int argc UNUSED_PARAM, char **argv)
186 struct input_event ev;
190 const char *opt_dir = "/etc/acpi";
191 const char *opt_input = "/dev/input/event";
192 const char *opt_logfile = "/var/log/acpid.log";
193 const char *opt_action = "/etc/acpid.conf";
194 const char *opt_map = "/etc/acpi.map";
195 #if ENABLE_FEATURE_PIDFILE
196 const char *opt_pidfile = "/var/run/acpid.pid";
201 opt_complementary = "df:e--e";
202 opts = getopt32(argv, "c:de:fl:p:a:M:" IF_FEATURE_ACPID_COMPAT("g:m:s:S:v"),
203 &opt_dir, &opt_input, &opt_logfile, &opt_pidfile, &opt_action, &opt_map
204 IF_FEATURE_ACPID_COMPAT(, NULL, NULL, NULL, NULL)
207 if (!(opts & OPT_f)) {
208 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
211 if (!(opts & OPT_d)) {
212 openlog(applet_name, LOG_PID, LOG_DAEMON);
213 logmode = LOGMODE_SYSLOG | LOGMODE_STDIO;
215 xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
218 parse_conf_file(opt_action);
219 parse_map_file(opt_map);
223 bb_signals((1 << SIGCHLD), SIG_IGN);
224 bb_signals(BB_FATAL_SIGS, record_signo);
232 dev_event = xasprintf((option_mask32 & OPT_e) ? "%s" : "%s%u", opt_input, nfd);
233 fd = open(dev_event, O_RDONLY | O_NONBLOCK);
236 bb_simple_perror_msg_and_die(dev_event);
239 pfd = xrealloc_vector(pfd, 1, nfd);
241 pfd[nfd].events = POLLIN;
245 write_pidfile(opt_pidfile);
247 while (poll(pfd, nfd, -1) > 0) {
249 for (i = 0; i < nfd; i++) {
250 const char *event = NULL;
252 memset(&ev, 0, sizeof(ev));
254 if (!(pfd[i].revents & POLLIN))
257 if (option_mask32 & OPT_e) {
261 buf = xmalloc_reads(pfd[i].fd, NULL, NULL);
262 /* buf = "button/power PWRB 00000080 00000000" */
263 len = strlen(buf) - 9;
266 event = find_action(NULL, buf);
268 if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev)))
271 if (ev.value != 1 && ev.value != 0)
274 event = find_action(&ev, NULL);
278 // spawn event handler
279 process_event(event);
283 if (ENABLE_FEATURE_CLEAN_UP) {
291 remove_pidfile(opt_pidfile);