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.
10 //usage:#define acpid_trivial_usage
11 //usage: "[-df] [-c CONFDIR] [-l LOGFILE] [-a ACTIONFILE] [-M MAPFILE] [-e PROC_EVENT_FILE] [-p PIDFILE]"
12 //usage:#define acpid_full_usage "\n\n"
13 //usage: "Listen to ACPI events and spawn specific helpers on event arrival\n"
14 //usage: "\n -d Log to stderr, not log file (implies -f)"
15 //usage: "\n -f Run in foreground"
16 //usage: "\n -c DIR Config directory [/etc/acpi]"
17 //usage: "\n -e FILE /proc event file [/proc/acpi/event]"
18 //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"
26 //usage:#define acpid_example_usage
27 //usage: "Without -e option, acpid uses all /dev/input/event* files\n"
29 //usage: "# acpid -l /var/log/my-acpi-log\n"
30 //usage: "# acpid -e /proc/acpi/event\n"
34 #include <linux/input.h>
46 # define SW_RFKILL_ALL 0x03
49 # define KEY_POWER 116 /* SC System Power Down */
52 # define KEY_SLEEP 142 /* SC System Sleep */
63 OPT_p = (1 << 7) * ENABLE_FEATURE_PIDFILE,
75 static 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" },
85 static const struct acpi_action f_act_tab[] = {
86 { "PWRF", "PWRF/00000080" },
87 { "LID0", "LID/00000080" },
91 struct acpi_action *act_tab;
93 struct acpi_event *evt_tab;
96 #define G (*ptr_to_globals)
97 #define act_tab (G.act_tab)
98 #define n_act (G.n_act )
99 #define evt_tab (G.evt_tab)
100 #define n_evt (G.n_evt )
101 #define INIT_G() do { \
102 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
106 * acpid listens to ACPI events coming either in textual form
107 * from /proc/acpi/event (though it is marked deprecated,
108 * it is still widely used and _is_ a standard) or in binary form
109 * from specified evdevs (just use /dev/input/event*).
110 * It parses the event to retrieve ACTION and a possible PARAMETER.
111 * It then spawns /etc/acpi/<ACTION>[/<PARAMETER>] either via run-parts
112 * (if the resulting path is a directory) or directly.
113 * If the resulting path does not exist it logs it via perror
114 * and continues listening.
117 static void process_event(const char *event)
120 char *handler = xasprintf("./%s", event);
121 const char *args[] = { "run-parts", handler, NULL };
124 if (option_mask32 & OPT_d) {
125 bb_error_msg("%s", event);
129 // N.B. run-parts would require scripts to have #!/bin/sh
130 // handler is directory? -> use run-parts
131 // handler is file? -> run it directly
132 if (0 == stat(event, &st))
133 spawn((char **)args + (0==(st.st_mode & S_IFDIR)));
135 bb_simple_perror_msg(event);
140 static const char *find_action(struct input_event *ev, const char *buf)
142 const char *action = NULL;
146 for (i = 0; i < n_evt; i++) {
148 if (ev->type == evt_tab[i].n_type && ev->code == evt_tab[i].n_code && ev->value == evt_tab[i].value) {
149 action = evt_tab[i].desc;
155 if (strncmp(buf, evt_tab[i].desc, strlen(buf)) == 0) {
156 action = evt_tab[i].desc;
164 for (i = 0; i < n_act; i++) {
165 if (strstr(action, act_tab[i].key)) {
166 action = act_tab[i].action;
175 static void parse_conf_file(const char *filename)
180 parser = config_open2(filename, fopen_for_read);
183 while (config_read(parser, tokens, 2, 2, "# \t", PARSE_NORMAL)) {
184 act_tab = xrealloc_vector(act_tab, 1, n_act);
185 act_tab[n_act].key = xstrdup(tokens[0]);
186 act_tab[n_act].action = xstrdup(tokens[1]);
189 config_close(parser);
191 act_tab = (void*)f_act_tab;
192 n_act = ARRAY_SIZE(f_act_tab);
196 static void parse_map_file(const char *filename)
201 parser = config_open2(filename, fopen_for_read);
204 while (config_read(parser, tokens, 6, 6, "# \t", PARSE_NORMAL)) {
205 evt_tab = xrealloc_vector(evt_tab, 1, n_evt);
206 evt_tab[n_evt].s_type = xstrdup(tokens[0]);
207 evt_tab[n_evt].n_type = xstrtou(tokens[1], 16);
208 evt_tab[n_evt].s_code = xstrdup(tokens[2]);
209 evt_tab[n_evt].n_code = xatou16(tokens[3]);
210 evt_tab[n_evt].value = xatoi_positive(tokens[4]);
211 evt_tab[n_evt].desc = xstrdup(tokens[5]);
214 config_close(parser);
216 evt_tab = (void*)f_evt_tab;
217 n_evt = ARRAY_SIZE(f_evt_tab);
222 * acpid [-c conf_dir] [-r conf_file ] [-a map_file ] [-l log_file] [-e proc_event_file]
225 int acpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
226 int acpid_main(int argc UNUSED_PARAM, char **argv)
231 const char *opt_dir = "/etc/acpi";
232 const char *opt_input = "/dev/input/event";
233 const char *opt_logfile = "/var/log/acpid.log";
234 const char *opt_action = "/etc/acpid.conf";
235 const char *opt_map = "/etc/acpi.map";
236 #if ENABLE_FEATURE_PIDFILE
237 const char *opt_pidfile = "/var/run/acpid.pid";
242 opt_complementary = "df:e--e";
243 opts = getopt32(argv, "c:de:fl:a:M:" IF_FEATURE_PIDFILE("p:") IF_FEATURE_ACPID_COMPAT("g:m:s:S:v"),
244 &opt_dir, &opt_input, &opt_logfile, &opt_action, &opt_map
245 IF_FEATURE_PIDFILE(, &opt_pidfile)
246 IF_FEATURE_ACPID_COMPAT(, NULL, NULL, NULL, NULL)
249 if (!(opts & OPT_f)) {
250 /* No -f "Foreground", we go to background */
251 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
254 if (!(opts & OPT_d)) {
255 /* No -d "Debug", we log to log file.
256 * This includes any output from children.
258 xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
259 xdup2(STDOUT_FILENO, STDERR_FILENO);
260 /* Also, acpid's messages (but not children) will go to syslog too */
261 openlog(applet_name, LOG_PID, LOG_DAEMON);
262 logmode = LOGMODE_SYSLOG | LOGMODE_STDIO;
264 /* else: -d "Debug", log is not redirected */
266 parse_conf_file(opt_action);
267 parse_map_file(opt_map);
271 bb_signals((1 << SIGCHLD), SIG_IGN);
272 bb_signals(BB_FATAL_SIGS, record_signo);
280 dev_event = xasprintf((opts & OPT_e) ? "%s" : "%s%u", opt_input, nfd);
281 fd = open(dev_event, O_RDONLY | O_NONBLOCK);
284 bb_simple_perror_msg_and_die(dev_event);
288 pfd = xrealloc_vector(pfd, 1, nfd);
290 pfd[nfd].events = POLLIN;
294 write_pidfile(opt_pidfile);
296 while (safe_poll(pfd, nfd, -1) > 0) {
298 for (i = 0; i < nfd; i++) {
301 if (!(pfd[i].revents & POLLIN)) {
302 if (pfd[i].revents == 0)
303 continue; /* this fd has nothing */
305 /* Likely POLLERR, POLLHUP, POLLNVAL.
306 * Do not listen on this fd anymore.
311 pfd[i].fd = pfd[i + 1].fd;
312 break; /* do poll() again */
316 if (option_mask32 & OPT_e) {
320 buf = xmalloc_reads(pfd[i].fd, NULL);
321 /* buf = "button/power PWRB 00000080 00000000" */
322 len = strlen(buf) - 9;
325 event = find_action(NULL, buf);
328 struct input_event ev;
330 if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev)))
333 if (ev.value != 1 && ev.value != 0)
336 event = find_action(&ev, NULL);
340 // spawn event handler
341 process_event(event);
345 if (ENABLE_FEATURE_CLEAN_UP) {
350 remove_pidfile(opt_pidfile);