diff options
-rw-r--r-- | util-linux/acpid.c | 54 |
1 files changed, 35 insertions, 19 deletions
diff --git a/util-linux/acpid.c b/util-linux/acpid.c index 6e7321b02..361a2b206 100644 --- a/util-linux/acpid.c +++ b/util-linux/acpid.c | |||
@@ -8,13 +8,13 @@ | |||
8 | */ | 8 | */ |
9 | 9 | ||
10 | //usage:#define acpid_trivial_usage | 10 | //usage:#define acpid_trivial_usage |
11 | //usage: "[-d] [-c CONFDIR] [-l LOGFILE] [-a ACTIONFILE] [-M MAPFILE] [-e PROC_EVENT_FILE] [-p PIDFILE]" | 11 | //usage: "[-df] [-c CONFDIR] [-l LOGFILE] [-a ACTIONFILE] [-M MAPFILE] [-e PROC_EVENT_FILE] [-p PIDFILE]" |
12 | //usage:#define acpid_full_usage "\n\n" | 12 | //usage:#define acpid_full_usage "\n\n" |
13 | //usage: "Listen to ACPI events and spawn specific helpers on event arrival\n" | 13 | //usage: "Listen to ACPI events and spawn specific helpers on event arrival\n" |
14 | //usage: "\n -d Log to stderr, not log file (implies -f)" | ||
15 | //usage: "\n -f Run in foreground" | ||
14 | //usage: "\n -c DIR Config directory [/etc/acpi]" | 16 | //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 -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]" | 18 | //usage: "\n -l FILE Log file [/var/log/acpid.log]" |
19 | //usage: "\n -p FILE Pid file [/var/run/acpid.pid]" | 19 | //usage: "\n -p FILE Pid file [/var/run/acpid.pid]" |
20 | //usage: "\n -a FILE Action file [/etc/acpid.conf]" | 20 | //usage: "\n -a FILE Action file [/etc/acpid.conf]" |
@@ -225,7 +225,6 @@ static void parse_map_file(const char *filename) | |||
225 | int acpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 225 | int acpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
226 | int acpid_main(int argc UNUSED_PARAM, char **argv) | 226 | int acpid_main(int argc UNUSED_PARAM, char **argv) |
227 | { | 227 | { |
228 | struct input_event ev; | ||
229 | int nfd; | 228 | int nfd; |
230 | int opts; | 229 | int opts; |
231 | struct pollfd *pfd; | 230 | struct pollfd *pfd; |
@@ -248,15 +247,21 @@ int acpid_main(int argc UNUSED_PARAM, char **argv) | |||
248 | ); | 247 | ); |
249 | 248 | ||
250 | if (!(opts & OPT_f)) { | 249 | if (!(opts & OPT_f)) { |
250 | /* No -f "Foreground", we go to background */ | ||
251 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); | 251 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); |
252 | } | 252 | } |
253 | 253 | ||
254 | if (!(opts & OPT_d)) { | 254 | if (!(opts & OPT_d)) { |
255 | /* No -d "Debug", we log to log file. | ||
256 | * This includes any output from children. | ||
257 | */ | ||
258 | xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO); | ||
259 | xdup2(STDOUT_FILENO, STDERR_FILENO); | ||
260 | /* Also, acpid's messages (but not children) will go to syslog too */ | ||
255 | openlog(applet_name, LOG_PID, LOG_DAEMON); | 261 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
256 | logmode = LOGMODE_SYSLOG | LOGMODE_STDIO; | 262 | logmode = LOGMODE_SYSLOG | LOGMODE_STDIO; |
257 | } else { | ||
258 | xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO); | ||
259 | } | 263 | } |
264 | /* else: -d "Debug", log is not redirected */ | ||
260 | 265 | ||
261 | parse_conf_file(opt_action); | 266 | parse_conf_file(opt_action); |
262 | parse_map_file(opt_map); | 267 | parse_map_file(opt_map); |
@@ -272,13 +277,14 @@ int acpid_main(int argc UNUSED_PARAM, char **argv) | |||
272 | int fd; | 277 | int fd; |
273 | char *dev_event; | 278 | char *dev_event; |
274 | 279 | ||
275 | dev_event = xasprintf((option_mask32 & OPT_e) ? "%s" : "%s%u", opt_input, nfd); | 280 | dev_event = xasprintf((opts & OPT_e) ? "%s" : "%s%u", opt_input, nfd); |
276 | fd = open(dev_event, O_RDONLY | O_NONBLOCK); | 281 | fd = open(dev_event, O_RDONLY | O_NONBLOCK); |
277 | if (fd < 0) { | 282 | if (fd < 0) { |
278 | if (nfd == 0) | 283 | if (nfd == 0) |
279 | bb_simple_perror_msg_and_die(dev_event); | 284 | bb_simple_perror_msg_and_die(dev_event); |
280 | break; | 285 | break; |
281 | } | 286 | } |
287 | free(dev_event); | ||
282 | pfd = xrealloc_vector(pfd, 1, nfd); | 288 | pfd = xrealloc_vector(pfd, 1, nfd); |
283 | pfd[nfd].fd = fd; | 289 | pfd[nfd].fd = fd; |
284 | pfd[nfd].events = POLLIN; | 290 | pfd[nfd].events = POLLIN; |
@@ -287,16 +293,26 @@ int acpid_main(int argc UNUSED_PARAM, char **argv) | |||
287 | 293 | ||
288 | write_pidfile(opt_pidfile); | 294 | write_pidfile(opt_pidfile); |
289 | 295 | ||
290 | while (poll(pfd, nfd, -1) > 0) { | 296 | while (safe_poll(pfd, nfd, -1) > 0) { |
291 | int i; | 297 | int i; |
292 | for (i = 0; i < nfd; i++) { | 298 | for (i = 0; i < nfd; i++) { |
293 | const char *event = NULL; | 299 | const char *event; |
294 | 300 | ||
295 | memset(&ev, 0, sizeof(ev)); | 301 | if (!(pfd[i].revents & POLLIN)) { |
296 | 302 | if (pfd[i].revents == 0) | |
297 | if (!(pfd[i].revents & POLLIN)) | 303 | continue; /* this fd has nothing */ |
298 | continue; | 304 | |
305 | /* Likely POLLERR, POLLHUP, POLLNVAL. | ||
306 | * Do not listen on this fd anymore. | ||
307 | */ | ||
308 | close(pfd[i].fd); | ||
309 | nfd--; | ||
310 | for (; i < nfd; i++) | ||
311 | pfd[i].fd = pfd[i + 1].fd; | ||
312 | break; /* do poll() again */ | ||
313 | } | ||
299 | 314 | ||
315 | event = NULL; | ||
300 | if (option_mask32 & OPT_e) { | 316 | if (option_mask32 & OPT_e) { |
301 | char *buf; | 317 | char *buf; |
302 | int len; | 318 | int len; |
@@ -307,7 +323,10 @@ int acpid_main(int argc UNUSED_PARAM, char **argv) | |||
307 | if (len >= 0) | 323 | if (len >= 0) |
308 | buf[len] = '\0'; | 324 | buf[len] = '\0'; |
309 | event = find_action(NULL, buf); | 325 | event = find_action(NULL, buf); |
326 | free(buf); | ||
310 | } else { | 327 | } else { |
328 | struct input_event ev; | ||
329 | |||
311 | if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev))) | 330 | if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev))) |
312 | continue; | 331 | continue; |
313 | 332 | ||
@@ -324,11 +343,8 @@ int acpid_main(int argc UNUSED_PARAM, char **argv) | |||
324 | } | 343 | } |
325 | 344 | ||
326 | if (ENABLE_FEATURE_CLEAN_UP) { | 345 | if (ENABLE_FEATURE_CLEAN_UP) { |
327 | while (nfd--) { | 346 | while (nfd--) |
328 | if (pfd[nfd].fd) { | 347 | close(pfd[nfd].fd); |
329 | close(pfd[nfd].fd); | ||
330 | } | ||
331 | } | ||
332 | free(pfd); | 348 | free(pfd); |
333 | } | 349 | } |
334 | remove_pidfile(opt_pidfile); | 350 | remove_pidfile(opt_pidfile); |