blob: 95f8150e2809505b19d4408c5322d0454b6cc461 [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 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +01009//config:config ACPID
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "acpid (9 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011//config: default y
12//config: select PLATFORM_LINUX
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: acpid listens to ACPI events coming either in textual form from
15//config: /proc/acpi/event (though it is marked deprecated it is still widely
16//config: used and _is_ a standard) or in binary form from specified evdevs
17//config: (just use /dev/input/event*).
Denys Vlasenkodd898c92016-11-23 11:46:32 +010018//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020019//config: It parses the event to retrieve ACTION and a possible PARAMETER.
20//config: It then spawns /etc/acpi/<ACTION>[/<PARAMETER>] either via run-parts
21//config: (if the resulting path is a directory) or directly as an executable.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010022//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020023//config: N.B. acpid relies on run-parts so have the latter installed.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010024//config:
25//config:config FEATURE_ACPID_COMPAT
26//config: bool "Accept and ignore redundant options"
27//config: default y
28//config: depends on ACPID
29//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020030//config: Accept and ignore compatibility options -g -m -s -S -v.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010031
32//applet:IF_ACPID(APPLET(acpid, BB_DIR_SBIN, BB_SUID_DROP))
33
34//kbuild:lib-$(CONFIG_ACPID) += acpid.o
Pere Orga5bc8c002011-04-11 03:29:49 +020035
36//usage:#define acpid_trivial_usage
Denys Vlasenko982fdaf2012-01-09 05:01:25 +010037//usage: "[-df] [-c CONFDIR] [-l LOGFILE] [-a ACTIONFILE] [-M MAPFILE] [-e PROC_EVENT_FILE] [-p PIDFILE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020038//usage:#define acpid_full_usage "\n\n"
39//usage: "Listen to ACPI events and spawn specific helpers on event arrival\n"
Denys Vlasenko982fdaf2012-01-09 05:01:25 +010040//usage: "\n -d Log to stderr, not log file (implies -f)"
Pere Orga5bc8c002011-04-11 03:29:49 +020041//usage: "\n -f Run in foreground"
Denys Vlasenko982fdaf2012-01-09 05:01:25 +010042//usage: "\n -c DIR Config directory [/etc/acpi]"
43//usage: "\n -e FILE /proc event file [/proc/acpi/event]"
Pere Orga5bc8c002011-04-11 03:29:49 +020044//usage: "\n -l FILE Log file [/var/log/acpid.log]"
45//usage: "\n -p FILE Pid file [/var/run/acpid.pid]"
46//usage: "\n -a FILE Action file [/etc/acpid.conf]"
47//usage: "\n -M FILE Map file [/etc/acpi.map]"
48//usage: IF_FEATURE_ACPID_COMPAT(
49//usage: "\n\nAccept and ignore compatibility options -g -m -s -S -v"
50//usage: )
51//usage:
52//usage:#define acpid_example_usage
53//usage: "Without -e option, acpid uses all /dev/input/event* files\n"
54//usage: "# acpid\n"
55//usage: "# acpid -l /var/log/my-acpi-log\n"
56//usage: "# acpid -e /proc/acpi/event\n"
57
Denis Vlasenkod16950d2008-11-29 09:05:50 +000058#include "libbb.h"
Souf Ouedccb7a432010-09-26 12:40:05 +020059#include <syslog.h>
Denis Vlasenkod16950d2008-11-29 09:05:50 +000060#include <linux/input.h>
Denys Vlasenko6af732b2010-07-12 06:20:11 +020061
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020062#ifndef EV_SW
63# define EV_SW 0x05
64#endif
65#ifndef EV_KEY
66# define EV_KEY 0x01
67#endif
68#ifndef SW_LID
69# define SW_LID 0x00
70#endif
71#ifndef SW_RFKILL_ALL
72# define SW_RFKILL_ALL 0x03
73#endif
74#ifndef KEY_POWER
75# define KEY_POWER 116 /* SC System Power Down */
76#endif
77#ifndef KEY_SLEEP
78# define KEY_SLEEP 142 /* SC System Sleep */
79#endif
80
Souf Ouedccb7a432010-09-26 12:40:05 +020081enum {
82 OPT_c = (1 << 0),
83 OPT_d = (1 << 1),
84 OPT_e = (1 << 2),
85 OPT_f = (1 << 3),
86 OPT_l = (1 << 4),
Denys Vlasenko26777aa2010-11-22 23:49:10 +010087 OPT_a = (1 << 5),
88 OPT_M = (1 << 6),
89 OPT_p = (1 << 7) * ENABLE_FEATURE_PIDFILE,
Souf Ouedccb7a432010-09-26 12:40:05 +020090};
91
92struct acpi_event {
93 const char *s_type;
94 uint16_t n_type;
95 const char *s_code;
96 uint16_t n_code;
97 uint32_t value;
98 const char *desc;
99};
100
101static const struct acpi_event f_evt_tab[] = {
102 { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRF 00000080" },
103 { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRB 00000080" },
Eric Martin5345b8d2012-07-07 19:06:50 +0200104 { "EV_SW", 0x05, "SW_LID", 0x00, 1, "button/lid LID0 00000080" },
Souf Ouedccb7a432010-09-26 12:40:05 +0200105};
106
107struct acpi_action {
108 const char *key;
109 const char *action;
110};
111
112static const struct acpi_action f_act_tab[] = {
113 { "PWRF", "PWRF/00000080" },
114 { "LID0", "LID/00000080" },
115};
116
117struct globals {
118 struct acpi_action *act_tab;
119 int n_act;
120 struct acpi_event *evt_tab;
121 int n_evt;
122} FIX_ALIASING;
123#define G (*ptr_to_globals)
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200124#define act_tab (G.act_tab)
Souf Ouedccb7a432010-09-26 12:40:05 +0200125#define n_act (G.n_act )
126#define evt_tab (G.evt_tab)
127#define n_evt (G.n_evt )
128#define INIT_G() do { \
129 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
130} while (0)
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000131
132/*
133 * acpid listens to ACPI events coming either in textual form
134 * from /proc/acpi/event (though it is marked deprecated,
135 * it is still widely used and _is_ a standard) or in binary form
136 * from specified evdevs (just use /dev/input/event*).
137 * It parses the event to retrieve ACTION and a possible PARAMETER.
138 * It then spawns /etc/acpi/<ACTION>[/<PARAMETER>] either via run-parts
139 * (if the resulting path is a directory) or directly.
140 * If the resulting path does not exist it logs it via perror
141 * and continues listening.
142 */
143
144static void process_event(const char *event)
145{
146 struct stat st;
147 char *handler = xasprintf("./%s", event);
148 const char *args[] = { "run-parts", handler, NULL };
149
Serj Kalichev1eafd442015-02-23 15:26:47 +0100150 // log the event
James Byrne69374872019-07-02 11:35:03 +0200151 bb_simple_error_msg(event);
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000152
153 // spawn handler
154 // N.B. run-parts would require scripts to have #!/bin/sh
155 // handler is directory? -> use run-parts
156 // handler is file? -> run it directly
157 if (0 == stat(event, &st))
158 spawn((char **)args + (0==(st.st_mode & S_IFDIR)));
159 else
160 bb_simple_perror_msg(event);
Souf Ouedccb7a432010-09-26 12:40:05 +0200161
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000162 free(handler);
163}
164
Souf Ouedccb7a432010-09-26 12:40:05 +0200165static const char *find_action(struct input_event *ev, const char *buf)
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000166{
Souf Ouedccb7a432010-09-26 12:40:05 +0200167 const char *action = NULL;
168 int i;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000169
Souf Ouedccb7a432010-09-26 12:40:05 +0200170 // map event
171 for (i = 0; i < n_evt; i++) {
172 if (ev) {
173 if (ev->type == evt_tab[i].n_type && ev->code == evt_tab[i].n_code && ev->value == evt_tab[i].value) {
174 action = evt_tab[i].desc;
175 break;
176 }
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000177 }
178
Souf Ouedccb7a432010-09-26 12:40:05 +0200179 if (buf) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100180 if (is_prefixed_with(evt_tab[i].desc, buf)) {
Souf Ouedccb7a432010-09-26 12:40:05 +0200181 action = evt_tab[i].desc;
182 break;
183 }
184 }
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000185 }
186
Souf Ouedccb7a432010-09-26 12:40:05 +0200187 // get action
188 if (action) {
189 for (i = 0; i < n_act; i++) {
190 if (strstr(action, act_tab[i].key)) {
191 action = act_tab[i].action;
192 break;
193 }
194 }
195 }
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000196
Souf Ouedccb7a432010-09-26 12:40:05 +0200197 return action;
198}
199
200static void parse_conf_file(const char *filename)
201{
202 parser_t *parser;
203 char *tokens[2];
204
205 parser = config_open2(filename, fopen_for_read);
206
207 if (parser) {
208 while (config_read(parser, tokens, 2, 2, "# \t", PARSE_NORMAL)) {
209 act_tab = xrealloc_vector(act_tab, 1, n_act);
210 act_tab[n_act].key = xstrdup(tokens[0]);
211 act_tab[n_act].action = xstrdup(tokens[1]);
212 n_act++;
213 }
214 config_close(parser);
215 } else {
216 act_tab = (void*)f_act_tab;
217 n_act = ARRAY_SIZE(f_act_tab);
218 }
219}
220
221static void parse_map_file(const char *filename)
222{
223 parser_t *parser;
224 char *tokens[6];
225
226 parser = config_open2(filename, fopen_for_read);
227
228 if (parser) {
229 while (config_read(parser, tokens, 6, 6, "# \t", PARSE_NORMAL)) {
230 evt_tab = xrealloc_vector(evt_tab, 1, n_evt);
231 evt_tab[n_evt].s_type = xstrdup(tokens[0]);
232 evt_tab[n_evt].n_type = xstrtou(tokens[1], 16);
233 evt_tab[n_evt].s_code = xstrdup(tokens[2]);
234 evt_tab[n_evt].n_code = xatou16(tokens[3]);
235 evt_tab[n_evt].value = xatoi_positive(tokens[4]);
236 evt_tab[n_evt].desc = xstrdup(tokens[5]);
237 n_evt++;
238 }
239 config_close(parser);
240 } else {
241 evt_tab = (void*)f_evt_tab;
242 n_evt = ARRAY_SIZE(f_evt_tab);
243 }
244}
245
246/*
247 * acpid [-c conf_dir] [-r conf_file ] [-a map_file ] [-l log_file] [-e proc_event_file]
248 */
249
250int acpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
251int acpid_main(int argc UNUSED_PARAM, char **argv)
252{
Souf Ouedccb7a432010-09-26 12:40:05 +0200253 int nfd;
254 int opts;
255 struct pollfd *pfd;
256 const char *opt_dir = "/etc/acpi";
257 const char *opt_input = "/dev/input/event";
258 const char *opt_logfile = "/var/log/acpid.log";
259 const char *opt_action = "/etc/acpid.conf";
260 const char *opt_map = "/etc/acpi.map";
261#if ENABLE_FEATURE_PIDFILE
Anthony G. Basile12677ac2012-12-10 14:49:39 -0500262 const char *opt_pidfile = CONFIG_PID_FILE_PATH "/acpid.pid";
Souf Ouedccb7a432010-09-26 12:40:05 +0200263#endif
264
265 INIT_G();
266
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200267 opts = getopt32(argv, "^"
268 "c:de:fl:a:M:"
269 IF_FEATURE_PIDFILE("p:")
270 IF_FEATURE_ACPID_COMPAT("g:m:s:S:v")
271 "\0"
272 "df:e--e",
Denys Vlasenko26777aa2010-11-22 23:49:10 +0100273 &opt_dir, &opt_input, &opt_logfile, &opt_action, &opt_map
274 IF_FEATURE_PIDFILE(, &opt_pidfile)
Souf Ouedccb7a432010-09-26 12:40:05 +0200275 IF_FEATURE_ACPID_COMPAT(, NULL, NULL, NULL, NULL)
276 );
277
278 if (!(opts & OPT_f)) {
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100279 /* No -f "Foreground", we go to background */
Souf Ouedccb7a432010-09-26 12:40:05 +0200280 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
281 }
282
283 if (!(opts & OPT_d)) {
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100284 /* No -d "Debug", we log to log file.
285 * This includes any output from children.
286 */
Serj Kalichev1eafd442015-02-23 15:26:47 +0100287 xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_APPEND), STDOUT_FILENO);
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100288 xdup2(STDOUT_FILENO, STDERR_FILENO);
289 /* Also, acpid's messages (but not children) will go to syslog too */
Souf Ouedccb7a432010-09-26 12:40:05 +0200290 openlog(applet_name, LOG_PID, LOG_DAEMON);
291 logmode = LOGMODE_SYSLOG | LOGMODE_STDIO;
Souf Ouedccb7a432010-09-26 12:40:05 +0200292 }
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100293 /* else: -d "Debug", log is not redirected */
Souf Ouedccb7a432010-09-26 12:40:05 +0200294
295 parse_conf_file(opt_action);
296 parse_map_file(opt_map);
297
298 xchdir(opt_dir);
299
Denys Vlasenkobbf1e3c2012-02-05 15:08:08 +0100300 /* We spawn children but don't wait for them. Prevent zombies: */
Souf Ouedccb7a432010-09-26 12:40:05 +0200301 bb_signals((1 << SIGCHLD), SIG_IGN);
Denys Vlasenkobbf1e3c2012-02-05 15:08:08 +0100302 // If you enable this, (1) explain why, (2)
303 // make sure while(poll) loop below is still interruptible
304 // by SIGTERM et al:
305 //bb_signals(BB_FATAL_SIGS, record_signo);
Souf Ouedccb7a432010-09-26 12:40:05 +0200306
307 pfd = NULL;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000308 nfd = 0;
Souf Ouedccb7a432010-09-26 12:40:05 +0200309 while (1) {
310 int fd;
311 char *dev_event;
312
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100313 dev_event = xasprintf((opts & OPT_e) ? "%s" : "%s%u", opt_input, nfd);
Souf Ouedccb7a432010-09-26 12:40:05 +0200314 fd = open(dev_event, O_RDONLY | O_NONBLOCK);
315 if (fd < 0) {
316 if (nfd == 0)
317 bb_simple_perror_msg_and_die(dev_event);
318 break;
319 }
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100320 free(dev_event);
Souf Ouedccb7a432010-09-26 12:40:05 +0200321 pfd = xrealloc_vector(pfd, 1, nfd);
322 pfd[nfd].fd = fd;
323 pfd[nfd].events = POLLIN;
324 nfd++;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000325 }
326
Souf Ouedccb7a432010-09-26 12:40:05 +0200327 write_pidfile(opt_pidfile);
328
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100329 while (safe_poll(pfd, nfd, -1) > 0) {
Souf Ouedccb7a432010-09-26 12:40:05 +0200330 int i;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000331 for (i = 0; i < nfd; i++) {
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100332 const char *event;
Souf Ouedccb7a432010-09-26 12:40:05 +0200333
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100334 if (!(pfd[i].revents & POLLIN)) {
335 if (pfd[i].revents == 0)
336 continue; /* this fd has nothing */
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000337
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100338 /* Likely POLLERR, POLLHUP, POLLNVAL.
339 * Do not listen on this fd anymore.
340 */
341 close(pfd[i].fd);
342 nfd--;
343 for (; i < nfd; i++)
344 pfd[i].fd = pfd[i + 1].fd;
345 break; /* do poll() again */
346 }
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000347
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100348 event = NULL;
Souf Ouedccb7a432010-09-26 12:40:05 +0200349 if (option_mask32 & OPT_e) {
350 char *buf;
351 int len;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000352
Denys Vlasenko80c5b682011-05-08 21:21:10 +0200353 buf = xmalloc_reads(pfd[i].fd, NULL);
Souf Ouedccb7a432010-09-26 12:40:05 +0200354 /* buf = "button/power PWRB 00000080 00000000" */
355 len = strlen(buf) - 9;
356 if (len >= 0)
357 buf[len] = '\0';
358 event = find_action(NULL, buf);
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100359 free(buf);
Souf Ouedccb7a432010-09-26 12:40:05 +0200360 } else {
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100361 struct input_event ev;
362
Souf Ouedccb7a432010-09-26 12:40:05 +0200363 if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev)))
364 continue;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000365
Souf Ouedccb7a432010-09-26 12:40:05 +0200366 if (ev.value != 1 && ev.value != 0)
367 continue;
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000368
Souf Ouedccb7a432010-09-26 12:40:05 +0200369 event = find_action(&ev, NULL);
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000370 }
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000371 if (!event)
372 continue;
Denys Vlasenkobbf1e3c2012-02-05 15:08:08 +0100373 /* spawn event handler */
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000374 process_event(event);
375 }
376 }
377
378 if (ENABLE_FEATURE_CLEAN_UP) {
Denys Vlasenko982fdaf2012-01-09 05:01:25 +0100379 while (nfd--)
380 close(pfd[nfd].fd);
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000381 free(pfd);
382 }
Souf Ouedccb7a432010-09-26 12:40:05 +0200383 remove_pidfile(opt_pidfile);
Denis Vlasenkod16950d2008-11-29 09:05:50 +0000384
385 return EXIT_SUCCESS;
386}