acpid.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * simple ACPI events listener
  4. *
  5. * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. //config:config ACPID
  10. //config: bool "acpid (9.3 kb)"
  11. //config: default y
  12. //config: help
  13. //config: acpid listens to ACPI events coming either in textual form from
  14. //config: /proc/acpi/event (though it is marked deprecated it is still widely
  15. //config: used and _is_ a standard) or in binary form from specified evdevs
  16. //config: (just use /dev/input/event*).
  17. //config:
  18. //config: It parses the event to retrieve ACTION and a possible PARAMETER.
  19. //config: It then spawns /etc/acpi/<ACTION>[/<PARAMETER>] either via run-parts
  20. //config: (if the resulting path is a directory) or directly as an executable.
  21. //config:
  22. //config: N.B. acpid relies on run-parts so have the latter installed.
  23. //config:
  24. //config:config FEATURE_ACPID_COMPAT
  25. //config: bool "Accept and ignore redundant options"
  26. //config: default y
  27. //config: depends on ACPID
  28. //config: help
  29. //config: Accept and ignore compatibility options -g -m -s -S -v.
  30. //applet:IF_ACPID(APPLET(acpid, BB_DIR_SBIN, BB_SUID_DROP))
  31. //kbuild:lib-$(CONFIG_ACPID) += acpid.o
  32. //usage:#define acpid_trivial_usage
  33. //usage: "[-df] [-c CONFDIR] [-l LOGFILE] [-a ACTIONFILE] [-M MAPFILE] [-e PROC_EVENT_FILE] [-p PIDFILE]"
  34. //usage:#define acpid_full_usage "\n\n"
  35. //usage: "Listen to ACPI events and spawn specific helpers on event arrival\n"
  36. //usage: "\n -d Log to stderr, not log file (implies -f)"
  37. //usage: "\n -f Run in foreground"
  38. //usage: "\n -c DIR Config directory [/etc/acpi]"
  39. //usage: "\n -e FILE /proc event file [/proc/acpi/event]"
  40. //usage: "\n -l FILE Log file [/var/log/acpid.log]"
  41. //usage: IF_FEATURE_PIDFILE(
  42. //usage: "\n -p FILE Pid file [" CONFIG_PID_FILE_PATH "/acpid.pid]"
  43. //usage: )
  44. //usage: "\n -a FILE Action file [/etc/acpid.conf]"
  45. //usage: "\n -M FILE Map file [/etc/acpi.map]"
  46. //usage: IF_FEATURE_ACPID_COMPAT(
  47. //usage: "\n\nAccept and ignore compatibility options -g -m -s -S -v"
  48. //usage: )
  49. //usage:
  50. //usage:#define acpid_example_usage
  51. //usage: "Without -e option, acpid uses all /dev/input/event* files\n"
  52. //usage: "# acpid\n"
  53. //usage: "# acpid -l /var/log/my-acpi-log\n"
  54. //usage: "# acpid -e /proc/acpi/event\n"
  55. #include "libbb.h"
  56. #include <syslog.h>
  57. #include <linux/input.h>
  58. #ifndef EV_SW
  59. # define EV_SW 0x05
  60. #endif
  61. #ifndef EV_KEY
  62. # define EV_KEY 0x01
  63. #endif
  64. #ifndef SW_LID
  65. # define SW_LID 0x00
  66. #endif
  67. #ifndef SW_RFKILL_ALL
  68. # define SW_RFKILL_ALL 0x03
  69. #endif
  70. #ifndef KEY_POWER
  71. # define KEY_POWER 116 /* SC System Power Down */
  72. #endif
  73. #ifndef KEY_SLEEP
  74. # define KEY_SLEEP 142 /* SC System Sleep */
  75. #endif
  76. enum {
  77. OPT_c = (1 << 0),
  78. OPT_d = (1 << 1),
  79. OPT_e = (1 << 2),
  80. OPT_f = (1 << 3),
  81. OPT_l = (1 << 4),
  82. OPT_a = (1 << 5),
  83. OPT_M = (1 << 6),
  84. OPT_p = (1 << 7) * ENABLE_FEATURE_PIDFILE,
  85. };
  86. struct acpi_event {
  87. const char *s_type;
  88. uint16_t n_type;
  89. const char *s_code;
  90. uint16_t n_code;
  91. uint32_t value;
  92. const char *desc;
  93. };
  94. static const struct acpi_event f_evt_tab[] ALIGN_PTR = {
  95. { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRF 00000080" },
  96. { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRB 00000080" },
  97. { "EV_SW", 0x05, "SW_LID", 0x00, 1, "button/lid LID0 00000080" },
  98. };
  99. struct acpi_action {
  100. const char *key;
  101. const char *action;
  102. };
  103. static const struct acpi_action f_act_tab[] ALIGN_PTR = {
  104. { "PWRF", "PWRF/00000080" },
  105. { "LID0", "LID/00000080" },
  106. };
  107. struct globals {
  108. struct acpi_action *act_tab;
  109. int n_act;
  110. struct acpi_event *evt_tab;
  111. int n_evt;
  112. } FIX_ALIASING;
  113. #define G (*ptr_to_globals)
  114. #define act_tab (G.act_tab)
  115. #define n_act (G.n_act )
  116. #define evt_tab (G.evt_tab)
  117. #define n_evt (G.n_evt )
  118. #define INIT_G() do { \
  119. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  120. } while (0)
  121. /*
  122. * acpid listens to ACPI events coming either in textual form
  123. * from /proc/acpi/event (though it is marked deprecated,
  124. * it is still widely used and _is_ a standard) or in binary form
  125. * from specified evdevs (just use /dev/input/event*).
  126. * It parses the event to retrieve ACTION and a possible PARAMETER.
  127. * It then spawns /etc/acpi/<ACTION>[/<PARAMETER>] either via run-parts
  128. * (if the resulting path is a directory) or directly.
  129. * If the resulting path does not exist it logs it via perror
  130. * and continues listening.
  131. */
  132. static void process_event(const char *event)
  133. {
  134. struct stat st;
  135. char *handler = xasprintf("./%s", event);
  136. const char *args[] = { "run-parts", handler, NULL };
  137. // log the event
  138. bb_simple_error_msg(event);
  139. // spawn handler
  140. // N.B. run-parts would require scripts to have #!/bin/sh
  141. // handler is directory? -> use run-parts
  142. // handler is file? -> run it directly
  143. if (0 == stat(event, &st))
  144. spawn((char **)args + (0==(st.st_mode & S_IFDIR)));
  145. else
  146. bb_simple_perror_msg(event);
  147. free(handler);
  148. }
  149. static const char *find_action(struct input_event *ev, const char *buf)
  150. {
  151. const char *action = NULL;
  152. int i;
  153. // map event
  154. for (i = 0; i < n_evt; i++) {
  155. if (ev) {
  156. if (ev->type == evt_tab[i].n_type && ev->code == evt_tab[i].n_code && ev->value == evt_tab[i].value) {
  157. action = evt_tab[i].desc;
  158. break;
  159. }
  160. }
  161. if (buf) {
  162. if (is_prefixed_with(evt_tab[i].desc, buf)) {
  163. action = evt_tab[i].desc;
  164. break;
  165. }
  166. }
  167. }
  168. // get action
  169. if (action) {
  170. for (i = 0; i < n_act; i++) {
  171. if (strstr(action, act_tab[i].key)) {
  172. action = act_tab[i].action;
  173. break;
  174. }
  175. }
  176. }
  177. return action;
  178. }
  179. static void parse_conf_file(const char *filename)
  180. {
  181. parser_t *parser;
  182. char *tokens[2];
  183. parser = config_open2(filename, fopen_for_read);
  184. if (parser) {
  185. while (config_read(parser, tokens, 2, 2, "# \t", PARSE_NORMAL)) {
  186. act_tab = xrealloc_vector(act_tab, 1, n_act);
  187. act_tab[n_act].key = xstrdup(tokens[0]);
  188. act_tab[n_act].action = xstrdup(tokens[1]);
  189. n_act++;
  190. }
  191. config_close(parser);
  192. } else {
  193. act_tab = (void*)f_act_tab;
  194. n_act = ARRAY_SIZE(f_act_tab);
  195. }
  196. }
  197. static void parse_map_file(const char *filename)
  198. {
  199. parser_t *parser;
  200. char *tokens[6];
  201. parser = config_open2(filename, fopen_for_read);
  202. if (parser) {
  203. while (config_read(parser, tokens, 6, 6, "# \t", PARSE_NORMAL)) {
  204. evt_tab = xrealloc_vector(evt_tab, 1, n_evt);
  205. evt_tab[n_evt].s_type = xstrdup(tokens[0]);
  206. evt_tab[n_evt].n_type = xstrtou(tokens[1], 16);
  207. evt_tab[n_evt].s_code = xstrdup(tokens[2]);
  208. evt_tab[n_evt].n_code = xatou16(tokens[3]);
  209. evt_tab[n_evt].value = xatoi_positive(tokens[4]);
  210. evt_tab[n_evt].desc = xstrdup(tokens[5]);
  211. n_evt++;
  212. }
  213. config_close(parser);
  214. } else {
  215. evt_tab = (void*)f_evt_tab;
  216. n_evt = ARRAY_SIZE(f_evt_tab);
  217. }
  218. }
  219. /*
  220. * acpid [-c conf_dir] [-r conf_file ] [-a map_file ] [-l log_file] [-e proc_event_file]
  221. */
  222. int acpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  223. int acpid_main(int argc UNUSED_PARAM, char **argv)
  224. {
  225. int nfd;
  226. int opts;
  227. struct pollfd *pfd;
  228. const char *opt_dir = "/etc/acpi";
  229. const char *opt_input = "/dev/input/event";
  230. const char *opt_logfile = "/var/log/acpid.log";
  231. const char *opt_action = "/etc/acpid.conf";
  232. const char *opt_map = "/etc/acpi.map";
  233. #if ENABLE_FEATURE_PIDFILE
  234. const char *opt_pidfile = CONFIG_PID_FILE_PATH "/acpid.pid";
  235. #endif
  236. INIT_G();
  237. opts = getopt32(argv, "^"
  238. "c:de:fl:a:M:"
  239. IF_FEATURE_PIDFILE("p:")
  240. IF_FEATURE_ACPID_COMPAT("g:m:s:S:v")
  241. "\0"
  242. "df:e--e",
  243. &opt_dir, &opt_input, &opt_logfile, &opt_action, &opt_map
  244. IF_FEATURE_PIDFILE(, &opt_pidfile)
  245. IF_FEATURE_ACPID_COMPAT(, NULL, NULL, NULL, NULL)
  246. );
  247. if (!(opts & OPT_f)) {
  248. /* No -f "Foreground", we go to background */
  249. bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
  250. }
  251. if (!(opts & OPT_d)) {
  252. /* No -d "Debug", we log to log file.
  253. * This includes any output from children.
  254. */
  255. xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_APPEND), STDOUT_FILENO);
  256. xdup2(STDOUT_FILENO, STDERR_FILENO);
  257. /* Also, acpid's messages (but not children) will go to syslog too */
  258. openlog(applet_name, LOG_PID, LOG_DAEMON);
  259. logmode = LOGMODE_SYSLOG | LOGMODE_STDIO;
  260. }
  261. /* else: -d "Debug", log is not redirected */
  262. parse_conf_file(opt_action);
  263. parse_map_file(opt_map);
  264. xchdir(opt_dir);
  265. /* We spawn children but don't wait for them. Prevent zombies: */
  266. bb_signals((1 << SIGCHLD), SIG_IGN);
  267. // If you enable this, (1) explain why, (2)
  268. // make sure while(poll) loop below is still interruptible
  269. // by SIGTERM et al:
  270. //bb_signals(BB_FATAL_SIGS, record_signo);
  271. pfd = NULL;
  272. nfd = 0;
  273. while (1) {
  274. int fd;
  275. char *dev_event;
  276. dev_event = xasprintf((opts & OPT_e) ? "%s" : "%s%u", opt_input, nfd);
  277. fd = open(dev_event, O_RDONLY | O_NONBLOCK);
  278. if (fd < 0) {
  279. if (nfd == 0)
  280. bb_simple_perror_msg_and_die(dev_event);
  281. break;
  282. }
  283. free(dev_event);
  284. pfd = xrealloc_vector(pfd, 1, nfd);
  285. pfd[nfd].fd = fd;
  286. pfd[nfd].events = POLLIN;
  287. nfd++;
  288. }
  289. write_pidfile(opt_pidfile);
  290. while (safe_poll(pfd, nfd, -1) > 0) {
  291. int i;
  292. for (i = 0; i < nfd; i++) {
  293. const char *event;
  294. if (!(pfd[i].revents & POLLIN)) {
  295. if (pfd[i].revents == 0)
  296. continue; /* this fd has nothing */
  297. /* Likely POLLERR, POLLHUP, POLLNVAL.
  298. * Do not listen on this fd anymore.
  299. */
  300. close(pfd[i].fd);
  301. nfd--;
  302. for (; i < nfd; i++)
  303. pfd[i].fd = pfd[i + 1].fd;
  304. break; /* do poll() again */
  305. }
  306. event = NULL;
  307. if (option_mask32 & OPT_e) {
  308. char *buf;
  309. int len;
  310. buf = xmalloc_reads(pfd[i].fd, NULL);
  311. /* buf = "button/power PWRB 00000080 00000000" */
  312. len = strlen(buf) - 9;
  313. if (len >= 0)
  314. buf[len] = '\0';
  315. event = find_action(NULL, buf);
  316. free(buf);
  317. } else {
  318. struct input_event ev;
  319. if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev)))
  320. continue;
  321. if (ev.value != 1 && ev.value != 0)
  322. continue;
  323. event = find_action(&ev, NULL);
  324. }
  325. if (!event)
  326. continue;
  327. /* spawn event handler */
  328. process_event(event);
  329. }
  330. }
  331. if (ENABLE_FEATURE_CLEAN_UP) {
  332. while (nfd--)
  333. close(pfd[nfd].fd);
  334. free(pfd);
  335. }
  336. remove_pidfile(opt_pidfile);
  337. return EXIT_SUCCESS;
  338. }