Ignore:
Timestamp:
Jan 1, 2014, 12:47:38 AM (7 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.21.1
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mindi-busybox/util-linux/acpid.c

    r2725 r3232  
    77 * Licensed under GPLv2, see file LICENSE in this source tree.
    88 */
     9
     10//usage:#define acpid_trivial_usage
     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"
     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"
     16//usage:     "\n    -c DIR  Config directory [/etc/acpi]"
     17//usage:     "\n    -e FILE /proc event file [/proc/acpi/event]"
     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
    932#include "libbb.h"
    1033#include <syslog.h>
    1134#include <linux/input.h>
     35
     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
    1254
    1355enum {
     
    3476    { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRF 00000080" },
    3577    { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRB 00000080" },
     78    { "EV_SW", 0x05, "SW_LID", 0x00, 1, "button/lid LID0 00000080" },
    3679};
    3780
     
    184227int acpid_main(int argc UNUSED_PARAM, char **argv)
    185228{
    186     struct input_event ev;
    187229    int nfd;
    188230    int opts;
     
    194236    const char *opt_map = "/etc/acpi.map";
    195237#if ENABLE_FEATURE_PIDFILE
    196     const char *opt_pidfile = "/var/run/acpid.pid";
     238    const char *opt_pidfile = CONFIG_PID_FILE_PATH "/acpid.pid";
    197239#endif
    198240
     
    207249
    208250    if (!(opts & OPT_f)) {
     251        /* No -f "Foreground", we go to background */
    209252        bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
    210253    }
    211254
    212255    if (!(opts & OPT_d)) {
     256        /* No -d "Debug", we log to log file.
     257         * This includes any output from children.
     258         */
     259        xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
     260        xdup2(STDOUT_FILENO, STDERR_FILENO);
     261        /* Also, acpid's messages (but not children) will go to syslog too */
    213262        openlog(applet_name, LOG_PID, LOG_DAEMON);
    214263        logmode = LOGMODE_SYSLOG | LOGMODE_STDIO;
    215     } else {
    216         xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
    217     }
     264    }
     265    /* else: -d "Debug", log is not redirected */
    218266
    219267    parse_conf_file(opt_action);
     
    222270    xchdir(opt_dir);
    223271
     272    /* We spawn children but don't wait for them. Prevent zombies: */
    224273    bb_signals((1 << SIGCHLD), SIG_IGN);
    225     bb_signals(BB_FATAL_SIGS, record_signo);
     274    // If you enable this, (1) explain why, (2)
     275    // make sure while(poll) loop below is still interruptible
     276    // by SIGTERM et al:
     277    //bb_signals(BB_FATAL_SIGS, record_signo);
    226278
    227279    pfd = NULL;
     
    231283        char *dev_event;
    232284
    233         dev_event = xasprintf((option_mask32 & OPT_e) ? "%s" : "%s%u", opt_input, nfd);
     285        dev_event = xasprintf((opts & OPT_e) ? "%s" : "%s%u", opt_input, nfd);
    234286        fd = open(dev_event, O_RDONLY | O_NONBLOCK);
    235287        if (fd < 0) {
     
    238290            break;
    239291        }
     292        free(dev_event);
    240293        pfd = xrealloc_vector(pfd, 1, nfd);
    241294        pfd[nfd].fd = fd;
     
    246299    write_pidfile(opt_pidfile);
    247300
    248     while (poll(pfd, nfd, -1) > 0) {
     301    while (safe_poll(pfd, nfd, -1) > 0) {
    249302        int i;
    250303        for (i = 0; i < nfd; i++) {
    251             const char *event = NULL;
    252 
    253             memset(&ev, 0, sizeof(ev));
    254 
    255             if (!(pfd[i].revents & POLLIN))
    256                 continue;
    257 
     304            const char *event;
     305
     306            if (!(pfd[i].revents & POLLIN)) {
     307                if (pfd[i].revents == 0)
     308                    continue; /* this fd has nothing */
     309
     310                /* Likely POLLERR, POLLHUP, POLLNVAL.
     311                 * Do not listen on this fd anymore.
     312                 */
     313                close(pfd[i].fd);
     314                nfd--;
     315                for (; i < nfd; i++)
     316                    pfd[i].fd = pfd[i + 1].fd;
     317                break; /* do poll() again */
     318            }
     319
     320            event = NULL;
    258321            if (option_mask32 & OPT_e) {
    259322                char *buf;
    260323                int len;
    261324
    262                 buf = xmalloc_reads(pfd[i].fd, NULL, NULL);
     325                buf = xmalloc_reads(pfd[i].fd, NULL);
    263326                /* buf = "button/power PWRB 00000080 00000000" */
    264327                len = strlen(buf) - 9;
     
    266329                    buf[len] = '\0';
    267330                event = find_action(NULL, buf);
     331                free(buf);
    268332            } else {
     333                struct input_event ev;
     334
    269335                if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev)))
    270336                    continue;
     
    277343            if (!event)
    278344                continue;
    279             // spawn event handler
     345            /* spawn event handler */
    280346            process_event(event);
    281347        }
     
    283349
    284350    if (ENABLE_FEATURE_CLEAN_UP) {
    285         while (nfd--) {
    286             if (pfd[nfd].fd) {
    287                 close(pfd[nfd].fd);
    288             }
    289         }
     351        while (nfd--)
     352            close(pfd[nfd].fd);
    290353        free(pfd);
    291354    }
Note: See TracChangeset for help on using the changeset viewer.