acpid.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. // log the event
  113. bb_error_msg("%s", event);
  114. // spawn handler
  115. // N.B. run-parts would require scripts to have #!/bin/sh
  116. // handler is directory? -> use run-parts
  117. // handler is file? -> run it directly
  118. if (0 == stat(event, &st))
  119. spawn((char **)args + (0==(st.st_mode & S_IFDIR)));
  120. else
  121. bb_simple_perror_msg(event);
  122. free(handler);
  123. }
  124. static const char *find_action(struct input_event *ev, const char *buf)
  125. {
  126. const char *action = NULL;
  127. int i;
  128. // map event
  129. for (i = 0; i < n_evt; i++) {
  130. if (ev) {
  131. if (ev->type == evt_tab[i].n_type && ev->code == evt_tab[i].n_code && ev->value == evt_tab[i].value) {
  132. action = evt_tab[i].desc;
  133. break;
  134. }
  135. }
  136. if (buf) {
  137. if (is_prefixed_with(evt_tab[i].desc, buf)) {
  138. action = evt_tab[i].desc;
  139. break;
  140. }
  141. }
  142. }
  143. // get action
  144. if (action) {
  145. for (i = 0; i < n_act; i++) {
  146. if (strstr(action, act_tab[i].key)) {
  147. action = act_tab[i].action;
  148. break;
  149. }
  150. }
  151. }
  152. return action;
  153. }
  154. static void parse_conf_file(const char *filename)
  155. {
  156. parser_t *parser;
  157. char *tokens[2];
  158. parser = config_open2(filename, fopen_for_read);
  159. if (parser) {
  160. while (config_read(parser, tokens, 2, 2, "# \t", PARSE_NORMAL)) {
  161. act_tab = xrealloc_vector(act_tab, 1, n_act);
  162. act_tab[n_act].key = xstrdup(tokens[0]);
  163. act_tab[n_act].action = xstrdup(tokens[1]);
  164. n_act++;
  165. }
  166. config_close(parser);
  167. } else {
  168. act_tab = (void*)f_act_tab;
  169. n_act = ARRAY_SIZE(f_act_tab);
  170. }
  171. }
  172. static void parse_map_file(const char *filename)
  173. {
  174. parser_t *parser;
  175. char *tokens[6];
  176. parser = config_open2(filename, fopen_for_read);
  177. if (parser) {
  178. while (config_read(parser, tokens, 6, 6, "# \t", PARSE_NORMAL)) {
  179. evt_tab = xrealloc_vector(evt_tab, 1, n_evt);
  180. evt_tab[n_evt].s_type = xstrdup(tokens[0]);
  181. evt_tab[n_evt].n_type = xstrtou(tokens[1], 16);
  182. evt_tab[n_evt].s_code = xstrdup(tokens[2]);
  183. evt_tab[n_evt].n_code = xatou16(tokens[3]);
  184. evt_tab[n_evt].value = xatoi_positive(tokens[4]);
  185. evt_tab[n_evt].desc = xstrdup(tokens[5]);
  186. n_evt++;
  187. }
  188. config_close(parser);
  189. } else {
  190. evt_tab = (void*)f_evt_tab;
  191. n_evt = ARRAY_SIZE(f_evt_tab);
  192. }
  193. }
  194. /*
  195. * acpid [-c conf_dir] [-r conf_file ] [-a map_file ] [-l log_file] [-e proc_event_file]
  196. */
  197. int acpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  198. int acpid_main(int argc UNUSED_PARAM, char **argv)
  199. {
  200. int nfd;
  201. int opts;
  202. struct pollfd *pfd;
  203. const char *opt_dir = "/etc/acpi";
  204. const char *opt_input = "/dev/input/event";
  205. const char *opt_logfile = "/var/log/acpid.log";
  206. const char *opt_action = "/etc/acpid.conf";
  207. const char *opt_map = "/etc/acpi.map";
  208. #if ENABLE_FEATURE_PIDFILE
  209. const char *opt_pidfile = CONFIG_PID_FILE_PATH "/acpid.pid";
  210. #endif
  211. INIT_G();
  212. opt_complementary = "df:e--e";
  213. opts = getopt32(argv, "c:de:fl:a:M:" IF_FEATURE_PIDFILE("p:") IF_FEATURE_ACPID_COMPAT("g:m:s:S:v"),
  214. &opt_dir, &opt_input, &opt_logfile, &opt_action, &opt_map
  215. IF_FEATURE_PIDFILE(, &opt_pidfile)
  216. IF_FEATURE_ACPID_COMPAT(, NULL, NULL, NULL, NULL)
  217. );
  218. if (!(opts & OPT_f)) {
  219. /* No -f "Foreground", we go to background */
  220. bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
  221. }
  222. if (!(opts & OPT_d)) {
  223. /* No -d "Debug", we log to log file.
  224. * This includes any output from children.
  225. */
  226. xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_APPEND), STDOUT_FILENO);
  227. xdup2(STDOUT_FILENO, STDERR_FILENO);
  228. /* Also, acpid's messages (but not children) will go to syslog too */
  229. openlog(applet_name, LOG_PID, LOG_DAEMON);
  230. logmode = LOGMODE_SYSLOG | LOGMODE_STDIO;
  231. }
  232. /* else: -d "Debug", log is not redirected */
  233. parse_conf_file(opt_action);
  234. parse_map_file(opt_map);
  235. xchdir(opt_dir);
  236. /* We spawn children but don't wait for them. Prevent zombies: */
  237. bb_signals((1 << SIGCHLD), SIG_IGN);
  238. // If you enable this, (1) explain why, (2)
  239. // make sure while(poll) loop below is still interruptible
  240. // by SIGTERM et al:
  241. //bb_signals(BB_FATAL_SIGS, record_signo);
  242. pfd = NULL;
  243. nfd = 0;
  244. while (1) {
  245. int fd;
  246. char *dev_event;
  247. dev_event = xasprintf((opts & OPT_e) ? "%s" : "%s%u", opt_input, nfd);
  248. fd = open(dev_event, O_RDONLY | O_NONBLOCK);
  249. if (fd < 0) {
  250. if (nfd == 0)
  251. bb_simple_perror_msg_and_die(dev_event);
  252. break;
  253. }
  254. free(dev_event);
  255. pfd = xrealloc_vector(pfd, 1, nfd);
  256. pfd[nfd].fd = fd;
  257. pfd[nfd].events = POLLIN;
  258. nfd++;
  259. }
  260. write_pidfile(opt_pidfile);
  261. while (safe_poll(pfd, nfd, -1) > 0) {
  262. int i;
  263. for (i = 0; i < nfd; i++) {
  264. const char *event;
  265. if (!(pfd[i].revents & POLLIN)) {
  266. if (pfd[i].revents == 0)
  267. continue; /* this fd has nothing */
  268. /* Likely POLLERR, POLLHUP, POLLNVAL.
  269. * Do not listen on this fd anymore.
  270. */
  271. close(pfd[i].fd);
  272. nfd--;
  273. for (; i < nfd; i++)
  274. pfd[i].fd = pfd[i + 1].fd;
  275. break; /* do poll() again */
  276. }
  277. event = NULL;
  278. if (option_mask32 & OPT_e) {
  279. char *buf;
  280. int len;
  281. buf = xmalloc_reads(pfd[i].fd, NULL);
  282. /* buf = "button/power PWRB 00000080 00000000" */
  283. len = strlen(buf) - 9;
  284. if (len >= 0)
  285. buf[len] = '\0';
  286. event = find_action(NULL, buf);
  287. free(buf);
  288. } else {
  289. struct input_event ev;
  290. if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev)))
  291. continue;
  292. if (ev.value != 1 && ev.value != 0)
  293. continue;
  294. event = find_action(&ev, NULL);
  295. }
  296. if (!event)
  297. continue;
  298. /* spawn event handler */
  299. process_event(event);
  300. }
  301. }
  302. if (ENABLE_FEATURE_CLEAN_UP) {
  303. while (nfd--)
  304. close(pfd[nfd].fd);
  305. free(pfd);
  306. }
  307. remove_pidfile(opt_pidfile);
  308. return EXIT_SUCCESS;
  309. }