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