3
0

acpid.c 9.9 KB

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