acpid.c 8.8 KB

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