~ubuntu-branches/ubuntu/lucid/acpid/lucid-updates

« back to all changes in this revision

Viewing changes to event.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Meskes
  • Date: 2009-11-17 14:50:01 UTC
  • mfrom: (2.1.12 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091117145001-y5hevyg7gcg6uwjk
Tags: 1.0.10-4
Updated netlink patch to version 6.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <sys/stat.h>
25
25
#include <sys/wait.h>
26
26
#include <sys/poll.h>
27
 
#include <libgen.h>
28
27
#include <fcntl.h>
29
28
#include <unistd.h>
30
29
#include <stdio.h>
93
92
        struct dirent *dirent;
94
93
        char *file = NULL;
95
94
        int nrules = 0;
96
 
        char *basen = NULL;
97
 
        regex_t preg;
98
95
 
99
96
        lock_rules();
100
97
 
123
120
 
124
121
                len += strlen(confdir) + 2;
125
122
 
126
 
                file = (char *)malloc(len);
 
123
                file = malloc(len);
127
124
                if (!file) {
128
125
                        acpid_log(LOG_ERR, "malloc(): %s\n", strerror(errno));
129
126
                        unlock_rules();
131
128
                }
132
129
                snprintf(file, len, "%s/%s", confdir, dirent->d_name);
133
130
 
134
 
                /* allow only regular files and symlinks to these */
 
131
                /* allow only regular files and symlinks to files */
135
132
                if (stat(file, &stat_buf) != 0) {
136
 
                        acpid_log(LOG_ERR, "stat(%s): %s\n", file, strerror(errno));
137
 
                        unlock_rules();
138
 
                        return -1;
 
133
                        acpid_log(LOG_ERR, "stat(%s): %s\n", file,
 
134
                                strerror(errno));
 
135
                        free(file);
 
136
                        continue; /* keep trying the rest of the files */
139
137
                }
140
138
                if (!S_ISREG(stat_buf.st_mode)) {
141
139
                        acpid_log(LOG_DEBUG, "skipping non-file %s\n", file);
142
140
                        free(file);
143
 
                        continue; /* skip subdirs */
 
141
                        continue; /* skip non-regular files */
144
142
                }
145
143
 
146
 
                /* check for run-parts style filename */
147
 
                basen = basename(file);
148
 
                if (regcomp(&preg, "^[a-zA-Z0-9_-]+$", RULE_REGEX_FLAGS) == 0){
149
 
                        if (regexec(&preg, basen, 0, NULL, 0) == 0){
150
 
                                r = parse_file(file);
151
 
                                if (r) {
152
 
                                        enlist_rule(&cmd_list, r);
153
 
                                        nrules++;
154
 
                                }
155
 
                        } else {
156
 
                                acpid_log(LOG_DEBUG, "ignoring conf file %s\n", file);
157
 
                        }
158
 
 
 
144
                r = parse_file(file);
 
145
                if (r) {
 
146
                        enlist_rule(&cmd_list, r);
 
147
                        nrules++;
159
148
                }
160
149
                free(file);
161
150
        }
290
279
                        }
291
280
                        rv = regcomp(r->event, val, RULE_REGEX_FLAGS);
292
281
                        if (rv) {
293
 
                                char buf[128];
294
 
                                regerror(rv, r->event, buf, sizeof(buf));
295
 
                                acpid_log(LOG_ERR, "regcomp(): %s\n", buf);
 
282
                                char rbuf[128];
 
283
                                regerror(rv, r->event, rbuf, sizeof(rbuf));
 
284
                                acpid_log(LOG_ERR, "regcomp(): %s\n", rbuf);
296
285
                                free_rule(r);
297
286
                                fclose(fp);
298
287
                                return NULL;
326
315
                            file);
327
316
                }
328
317
                free_rule(r);
 
318
                fclose(fp);
329
319
                return NULL;
330
320
        }
331
321
        fclose(fp);
533
523
                        struct rule *pnext = p->next;
534
524
                        if (!regexec(p->event, event, 0, NULL, 0)) {
535
525
                                /* a match! */
536
 
                                if (acpid_debug) {
 
526
                                if (acpid_debug && logevents) {
537
527
                                        acpid_log(LOG_DEBUG,
538
528
                                            "rule from %s matched\n",
539
529
                                            p->origin);
549
539
                                            p->type);
550
540
                                }
551
541
                        } else {
552
 
                                if (acpid_debug >= 3) {
 
542
                                if (acpid_debug >= 3 && logevents) {
553
543
                                        acpid_log(LOG_DEBUG,
554
544
                                            "rule from %s did not match\n",
555
545
                                            p->origin);
561
551
 
562
552
        unlock_rules();
563
553
 
564
 
        if (acpid_debug) {
 
554
        if (acpid_debug && logevents) {
565
555
                acpid_log(LOG_DEBUG, "%d total rule%s matched\n",
566
556
                        nrules, (nrules == 1)?"":"s");
567
557
        }
573
563
static sigset_t *
574
564
signals_handled(void)
575
565
{
576
 
        static sigset_t sigset;
577
 
 
578
 
        sigemptyset(&sigset);
579
 
        sigaddset(&sigset, SIGHUP);
580
 
        sigaddset(&sigset, SIGTERM);
581
 
        sigaddset(&sigset, SIGQUIT);
582
 
        sigaddset(&sigset, SIGINT);
583
 
 
584
 
        return &sigset;
 
566
        static sigset_t set;
 
567
 
 
568
        sigemptyset(&set);
 
569
        sigaddset(&set, SIGHUP);
 
570
        sigaddset(&set, SIGTERM);
 
571
        sigaddset(&set, SIGQUIT);
 
572
        sigaddset(&set, SIGINT);
 
573
 
 
574
        return &set;
585
575
}
586
576
 
587
577
static void
621
611
        case 0: /* child */
622
612
                /* parse the commandline, doing any substitutions needed */
623
613
                action = parse_cmd(rule->action.cmd, event);
624
 
                acpid_log(LOG_INFO, "executing action \"%s\"\n", action);
 
614
                if (logevents) {
 
615
                        acpid_log(LOG_INFO,
 
616
                            "executing action \"%s\"\n", action);
 
617
                }
625
618
 
626
619
                /* reset signals */
627
620
                signal(SIGHUP, SIG_DFL);
631
624
                signal(SIGPIPE, SIG_DFL);
632
625
                sigprocmask(SIG_UNBLOCK, signals_handled(), NULL);
633
626
 
634
 
                if (acpid_debug)
 
627
                if (acpid_debug && logevents) {
635
628
                        fprintf(stdout, "BEGIN HANDLER MESSAGES\n");
 
629
                }
636
630
                execl("/bin/sh", "/bin/sh", "-c", action, NULL);
637
631
                /* should not get here */
638
632
                acpid_log(LOG_ERR, "execl(): %s\n", strerror(errno));
641
635
 
642
636
        /* parent */
643
637
        waitpid(pid, &status, 0);
644
 
        if (acpid_debug)
 
638
        if (acpid_debug && logevents) {
645
639
                fprintf(stdout, "END HANDLER MESSAGES\n");
646
 
        if (WIFEXITED(status)) {
647
 
                acpid_log(LOG_INFO, "action exited with status %d\n",
648
 
                    WEXITSTATUS(status));
649
 
        } else if (WIFSIGNALED(status)) {
650
 
                acpid_log(LOG_INFO, "action exited on signal %d\n",
651
 
                    WTERMSIG(status));
652
 
        } else {
653
 
                acpid_log(LOG_INFO, "action exited with status %d\n",
654
 
                    status);
 
640
        }
 
641
 
 
642
        if (logevents) {
 
643
                if (WIFEXITED(status)) {
 
644
                        acpid_log(LOG_INFO, "action exited with status %d\n",
 
645
                            WEXITSTATUS(status));
 
646
                } else if (WIFSIGNALED(status)) {
 
647
                        acpid_log(LOG_INFO, "action exited on signal %d\n",
 
648
                            WTERMSIG(status));
 
649
                } else {
 
650
                        acpid_log(LOG_INFO, "action exited with status %d\n",
 
651
                            status);
 
652
                }
655
653
        }
656
654
 
657
655
        return 0;
663
661
        int r;
664
662
        int client = rule->action.fd;
665
663
 
666
 
        acpid_log(LOG_INFO, "notifying client %s\n", rule->origin);
 
664
        if (logevents) {
 
665
                acpid_log(LOG_INFO, "notifying client %s\n", rule->origin);
 
666
        }
667
667
 
668
668
        r = safe_write(client, event, strlen(event));
669
669
        if (r < 0 && errno == EPIPE) {
670
670
                struct ucred cred;
671
671
                /* closed */
672
 
                acpid_log(LOG_NOTICE, "client has disconnected\n");
 
672
                acpid_log(LOG_NOTICE,
 
673
                    "client %s has disconnected\n", rule->origin);
673
674
                delist_rule(&client_list, rule);
674
675
                ud_get_peercred(rule->action.fd, &cred);
675
676
                if (cred.uid != 0) {
722
723
parse_cmd(const char *cmd, const char *event)
723
724
{
724
725
        static char buf[4096];
725
 
        int i;
 
726
        size_t i;
726
727
        const char *p;
727
728
 
728
729
        p = cmd;
734
735
                        p++;
735
736
                        if (*p == 'e') {
736
737
                                /* handle an event expansion */
737
 
                                int size = sizeof(buf) - i;
 
738
                                size_t size = sizeof(buf) - i;
738
739
                                size = snprintf(buf+i, size, "%s", event);
739
740
                                i += size;
740
741
                                p++;