blob: 5d27929483277ede682e9ad2ea23f0dddec65db2 [file] [log] [blame]
Denis Vlasenkod16950d2008-11-29 09:05:50 +00001/* vi: set sw=4 ts=4: */
2/*
3 * simple ACPI events listener
4 *
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkod16950d2008-11-29 09:05:50 +00008 */
Pere Orga5bc8c002011-04-11 03:29:49 +02009
10//usage:#define acpid_trivial_usage
Denys Vlasenko982fdaf2012-01-09 05:01:25 +010011//usage: "[-df] [-c CONFDIR] [-l LOGFILE] [-a ACTIONFILE] [-M MAPFILE] [-e PROC_EVENT_FILE] [-p PIDFILE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020012//usage:#define acpid_full_usage "\n\n"
13//usage: "Listen to ACPI events and spawn specific helpers on event arrival\n"
Denys Vlasenko982fdaf2012-01-09 05:01:25 +010014//usage: "\n -d Log to stderr, not log file (implies -f)"
Pere Orga5bc8c002011-04-11 03:29:49 +020015//usage: "\n -f Run in foreground"
Denys Vlasenko982fdaf2012-01-09 05:01:25 +010016//usage: "\n -c DIR Config directory [/etc/acpi]"
17//usage: "\n -e FILE /proc event file [/proc/acpi/event]"
Pere Orga5bc8c002011-04-11 03:29:49 +020018//usage: "\n -l FILE Log file [/var/log/acpid.log]"
19//usage: "\n -p FILE Pid file [/var/run/acpid.pid]"
20//usage: "\n -a FILE Action file [/etc/acpid.conf]"
21//usage: "\n -M FILE Map file [/etc/acpi.map]"
22//usage: IF_FEATURE_ACPID_COMPAT(
23//usage: "\n\nAccept and ignore compatibility options -g -m -s -S -v"
24//usage: )
25//usage:
26//usage:#define acpid_example_usage
27//usage: "Without -e option, acpid uses all /dev/input/event* files\n"
28//usage: "# acpid\n"
29//usage: "# acpid -l /var/log/my-acpi-log\n"
30//usage: "# acpid -e /proc/acpi/event\n"
31
Denis Vlasenkod16950d2008-11-29 09:05:50 +000032#include "libbb.h"
Souf Ouedccb7a432010-09-26 12:40:05 +020033#include <syslog.h>
Denis Vlasenkod16950d2008-11-29 09:05:50 +000034#include <linux/input.h>
Denys Vlasenko6af732b2010-07-12 06:20:11 +020035
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020036#ifndef EV_SW
37# define EV_SW 0x05
38#endif
39#ifndef EV_KEY
40# define EV_KEY 0x01
41#endif
42#ifndef SW_LID
43# define SW_LID 0x00
44#endif
45#ifndef SW_RFKILL_ALL
46# define SW_RFKILL_ALL 0x03
47#endif
48#ifndef KEY_POWER
49# define KEY_POWER 116 /* SC System Power Down */
50#endif
51#ifndef KEY_SLEEP
52# define KEY_SLEEP 142 /* SC System Sleep */
53#endif
54
Souf Ouedccb7a432010-09-26 12:40:05 +020055enum {
56 OPT_c = (1 << 0),
57 OPT_d = (1 << 1),
58 OPT_e = (1 << 2),
59 OPT_f = (1 << 3),
60 OPT_l = (1 << 4),
Denys Vlasenko26777aa2010-11-22 23:49:10 +010061 OPT_a = (1 << 5),
62 OPT_M = (1 << 6),
63 OPT_p = (1 << 7) * ENABLE_FEATURE_PIDFILE,
Souf Ouedccb7a432010-09-26 12:40:05 +020064};
65
66struct acpi_event {
67 const char *s_type;
68 uint16_t n_type;
69 const char *s_code;
70 uint16_t n_code;
71 uint32_t value;
72 const char *desc;
73};
74
75static const struct acpi_event f_evt_tab[] = {
76 { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRF 00000080" },
77 { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRB 00000080" },
Eric Martin5345b8d2012-07-07 19:06:50 +020078 { "EV_SW", 0x05, "SW_LID", 0x00, 1, "button/lid LID0 00000080" },
Souf Ouedccb7a432010-09-26 12:40:05 +020079};
80
81struct acpi_action {
82 const char *key;
83 const char *action;
84};
85
86static const struct acpi_action f_act_tab[] = {
87 { "PWRF", "PWRF/00000080" },
88 { "LID0", "LID/00000080" },
89};
90
91struct globals {
92 struct acpi_action *act_tab;
93 int n_act;
94 struct acpi_event *evt_tab;
95 int n_evt;
96} FIX_ALIASING;
97#define G (*ptr_to_globals)
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020098#define act_tab (G.act_tab)
Souf Ouedccb7a432010-09-26 12:40:05 +020099#define n_act (G.n_act )
100#define evt_tab (G.evt_tab)
101#define n_evt (G.n_evt )
102#define INIT_G() do { \
103 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
104} while (0)
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000105
106/*
107 * acpid listens to ACPI events coming either in textual form
108 * from /proc/acpi/event (though it is marked deprecated,
109 * it is still widely used and _is_ a standard) or in binary form
110 * from specified evdevs (just use /dev/input/event*).
111 * It parses the event to retrieve ACTION and a possible PARAMETER.
112 * It then spawns /etc/acpi/<ACTION>[/<PARAMETER>] either via run-parts
113 * (if the resulting path is a directory) or directly.
114 * If the resulting path does not exist it logs it via perror
115 * and continues listening.
116 */
117
118static void process_event(const char *event)
119{
120 struct stat st;
121 char *handler = xasprintf("./%s", event);
122 const char *args[] = { "run-parts", handler, NULL };
123
124 // debug info
Souf Ouedccb7a432010-09-26 12:40:05 +0200125 if (option_mask32 & OPT_d) {
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000126 bb_error_msg("%s", event);
127 }
128
129 // spawn handler
130 // N.B. run-parts would require scripts to have #!/bin/sh
131 // handler is directory? -> use run-parts
132 // handler is file? -> run it directly
133 if (0 == stat(event, &st))
134 spawn((char **)args + (0==(st.st_mode & S_IFDIR)));
135 else
136 bb_simple_perror_msg(event);
Souf Ouedccb7a432010-09-26 12:40:05 +0200137
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000138 free(handler);
139}
140
Souf Ouedccb7a432010-09-26 12:40:05 +0200141static const char *find_action(struct input_event *ev, const char *buf)
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000142{
Souf Ouedccb7a432010-09-26 12:40:05 +0200143 const char *action = NULL;
144 int i;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000145
Souf Ouedccb7a432010-09-26 12:40:05 +0200146 // map event
147 for (i = 0; i < n_evt; i++) {
148 if (ev) {
149 if (ev->type == evt_tab[i].n_type && ev->code == evt_tab[i].n_code && ev->value == evt_tab[i].value) {
150 action = evt_tab[i].desc;
151 break;
152 }
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000153 }
154
Souf Ouedccb7a432010-09-26 12:40:05 +0200155 if (buf) {
156 if (strncmp(buf, evt_tab[i].desc, strlen(buf)) == 0) {
157 action = evt_tab[i].desc;
158 break;
159 }
160 }
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000161 }
162
Souf Ouedccb7a432010-09-26 12:40:05 +0200163 // get action
164 if (action) {
165 for (i = 0; i < n_act; i++) {
166 if (strstr(action, act_tab[i].key)) {
167 action = act_tab[i].action;
168 break;
169 }
170 }
171 }
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000172
Souf Ouedccb7a432010-09-26 12:40:05 +0200173 return action;
174}
175
176static void parse_conf_file(const char *filename)
177{
178 parser_t *parser;
179 char *tokens[2];
180
181 parser = config_open2(filename, fopen_for_read);
182
183 if (parser) {
184 while (config_read(parser, tokens, 2, 2, "# \t", PARSE_NORMAL)) {
185 act_tab = xrealloc_vector(act_tab, 1, n_act);
186 act_tab[n_act].key = xstrdup(tokens[0]);
187 act_tab[n_act].action = xstrdup(tokens[1]);
188 n_act++;
189 }
190 config_close(parser);
191 } else {
192 act_tab = (void*)f_act_tab;
193 n_act = ARRAY_SIZE(f_act_tab);
194 }
195}
196
197static void parse_map_file(const char *filename)
198{
199 parser_t *parser;
200 char *tokens[6];
201
202 parser = config_open2(filename, fopen_for_read);
203
204 if (parser) {
205 while (config_read(parser, tokens, 6, 6, "# \t", PARSE_NORMAL)) {
206 evt_tab = xrealloc_vector(evt_tab, 1, n_evt);
207 evt_tab[n_evt].s_type = xstrdup(tokens[0]);
208 evt_tab[n_evt].n_type = xstrtou(tokens[1], 16);
209 evt_tab[n_evt].s_code = xstrdup(tokens[2]);
210 evt_tab[n_evt].n_code = xatou16(tokens[3]);
211 evt_tab[n_evt].value = xatoi_positive(tokens[4]);
212 evt_tab[n_evt].desc = xstrdup(tokens[5]);
213 n_evt++;
214 }
215 config_close(parser);
216 } else {
217 evt_tab = (void*)f_evt_tab;
218 n_evt = ARRAY_SIZE(f_evt_tab);
219 }
220}
221
222/*
223 * acpid [-c conf_dir] [-r conf_file ] [-a map_file ] [-l log_file] [-e proc_event_file]
224 */
225
226int acpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
227int acpid_main(int argc UNUSED_PARAM, char **argv)
228{
Souf Ouedccb7a432010-09-26 12:40:05 +0200229 int nfd;
230 int opts;
231 struct pollfd *pfd;
232 const char *opt_dir = "/etc/acpi";
233 const char *opt_input = "/dev/input/event";
234 const char *opt_logfile = "/var/log/acpid.log";
235 const char *opt_action = "/etc/acpid.conf";
236 const char *opt_map = "/etc/acpi.map";
237#if ENABLE_FEATURE_PIDFILE
238 const char *opt_pidfile = "/var/run/acpid.pid";
239#endif
240
241 INIT_G();
242
243 opt_complementary = "df:e--e";
Denys Vlasenko26777aa2010-11-22 23:49:10 +0100244 opts = getopt32(argv, "c:de:fl:a:M:" IF_FEATURE_PIDFILE("p:") IF_FEATURE_ACPID_COMPAT("g:m:s:S:v"),
245 &opt_dir, &opt_input, &opt_logfile, &opt_action, &opt_map
246 IF_FEATURE_PIDFILE(, &opt_pidfile)
Souf Ouedccb7a432010-09-26 12:40:05 +0200247 IF_FEATURE_ACPID_COMPAT(, NULL, NULL, NULL, NULL)
248 );
249
250 if (!(opts & OPT_f)) {
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100251 /* No -f "Foreground", we go to background */
Souf Ouedccb7a432010-09-26 12:40:05 +0200252 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
253 }
254
255 if (!(opts & OPT_d)) {
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100256 /* No -d "Debug", we log to log file.
257 * This includes any output from children.
258 */
259 xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
260 xdup2(STDOUT_FILENO, STDERR_FILENO);
261 /* Also, acpid's messages (but not children) will go to syslog too */
Souf Ouedccb7a432010-09-26 12:40:05 +0200262 openlog(applet_name, LOG_PID, LOG_DAEMON);
263 logmode = LOGMODE_SYSLOG | LOGMODE_STDIO;
Souf Ouedccb7a432010-09-26 12:40:05 +0200264 }
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100265 /* else: -d "Debug", log is not redirected */
Souf Ouedccb7a432010-09-26 12:40:05 +0200266
267 parse_conf_file(opt_action);
268 parse_map_file(opt_map);
269
270 xchdir(opt_dir);
271
Denys Vlasenkobbf1e3c2012-02-05 15:08:08 +0100272 /* We spawn children but don't wait for them. Prevent zombies: */
Souf Ouedccb7a432010-09-26 12:40:05 +0200273 bb_signals((1 << SIGCHLD), SIG_IGN);
Denys Vlasenkobbf1e3c2012-02-05 15:08:08 +0100274 // If you enable this, (1) explain why, (2)
275 // make sure while(poll) loop below is still interruptible
276 // by SIGTERM et al:
277 //bb_signals(BB_FATAL_SIGS, record_signo);
Souf Ouedccb7a432010-09-26 12:40:05 +0200278
279 pfd = NULL;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000280 nfd = 0;
Souf Ouedccb7a432010-09-26 12:40:05 +0200281 while (1) {
282 int fd;
283 char *dev_event;
284
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100285 dev_event = xasprintf((opts & OPT_e) ? "%s" : "%s%u", opt_input, nfd);
Souf Ouedccb7a432010-09-26 12:40:05 +0200286 fd = open(dev_event, O_RDONLY | O_NONBLOCK);
287 if (fd < 0) {
288 if (nfd == 0)
289 bb_simple_perror_msg_and_die(dev_event);
290 break;
291 }
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100292 free(dev_event);
Souf Ouedccb7a432010-09-26 12:40:05 +0200293 pfd = xrealloc_vector(pfd, 1, nfd);
294 pfd[nfd].fd = fd;
295 pfd[nfd].events = POLLIN;
296 nfd++;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000297 }
298
Souf Ouedccb7a432010-09-26 12:40:05 +0200299 write_pidfile(opt_pidfile);
300
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100301 while (safe_poll(pfd, nfd, -1) > 0) {
Souf Ouedccb7a432010-09-26 12:40:05 +0200302 int i;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000303 for (i = 0; i < nfd; i++) {
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100304 const char *event;
Souf Ouedccb7a432010-09-26 12:40:05 +0200305
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100306 if (!(pfd[i].revents & POLLIN)) {
307 if (pfd[i].revents == 0)
308 continue; /* this fd has nothing */
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000309
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100310 /* Likely POLLERR, POLLHUP, POLLNVAL.
311 * Do not listen on this fd anymore.
312 */
313 close(pfd[i].fd);
314 nfd--;
315 for (; i < nfd; i++)
316 pfd[i].fd = pfd[i + 1].fd;
317 break; /* do poll() again */
318 }
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000319
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100320 event = NULL;
Souf Ouedccb7a432010-09-26 12:40:05 +0200321 if (option_mask32 & OPT_e) {
322 char *buf;
323 int len;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000324
Denys Vlasenko80c5b682011-05-08 21:21:10 +0200325 buf = xmalloc_reads(pfd[i].fd, NULL);
Souf Ouedccb7a432010-09-26 12:40:05 +0200326 /* buf = "button/power PWRB 00000080 00000000" */
327 len = strlen(buf) - 9;
328 if (len >= 0)
329 buf[len] = '\0';
330 event = find_action(NULL, buf);
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100331 free(buf);
Souf Ouedccb7a432010-09-26 12:40:05 +0200332 } else {
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100333 struct input_event ev;
334
Souf Ouedccb7a432010-09-26 12:40:05 +0200335 if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev)))
336 continue;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000337
Souf Ouedccb7a432010-09-26 12:40:05 +0200338 if (ev.value != 1 && ev.value != 0)
339 continue;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000340
Souf Ouedccb7a432010-09-26 12:40:05 +0200341 event = find_action(&ev, NULL);
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000342 }
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000343 if (!event)
344 continue;
Denys Vlasenkobbf1e3c2012-02-05 15:08:08 +0100345 /* spawn event handler */
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000346 process_event(event);
347 }
348 }
349
350 if (ENABLE_FEATURE_CLEAN_UP) {
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100351 while (nfd--)
352 close(pfd[nfd].fd);
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000353 free(pfd);
354 }
Souf Ouedccb7a432010-09-26 12:40:05 +0200355 remove_pidfile(opt_pidfile);
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000356
357 return EXIT_SUCCESS;
358}