acpid.c 8.9 KB

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