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