1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
/*
* event.c - ACPI daemon
*
* Copyright (C) 2000 Andrew Henroid
* Copyright (C) 2001 Sun Microsystems (thockin@sun.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <libgen.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <ctype.h>
#include <regex.h>
#include <signal.h>
#include "acpid.h"
/*
* What is a rule? It's polymorphic, pretty much.
*/
#define RULE_REGEX_FLAGS (REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE)
struct rule {
enum {
RULE_NONE = 0,
RULE_CMD,
RULE_CLIENT,
} type;
char *origin;
regex_t *event;
union {
char *cmd;
int fd;
} action;
struct rule *next;
struct rule *prev;
};
struct rule_list {
struct rule *head;
struct rule *tail;
};
static struct rule_list cmd_list;
static struct rule_list client_list;
/* rule routines */
static void enlist_rule(struct rule_list *list, struct rule *r);
static void delist_rule(struct rule_list *list, struct rule *r);
static struct rule *new_rule(void);
static void free_rule(struct rule *r);
/* other helper routines */
static void lock_rules(void);
static void unlock_rules(void);
static sigset_t *signals_handled(void);
static struct rule *parse_file(const char *file);
static struct rule *parse_client(int client);
static int do_cmd_rule(struct rule *r, const char *event);
static int do_client_rule(struct rule *r, const char *event);
static int safe_write(int fd, const char *buf, int len);
static char *parse_cmd(const char *cmd, const char *event);
static int check_escapes(const char *str);
/*
* read in all the configuration files
*/
int
acpid_read_conf(const char *confdir)
{
DIR *dir;
struct dirent *dirent;
char *file = NULL;
char *basen = NULL;
regex_t preg;
int nrules = 0;
lock_rules();
dir = opendir(confdir);
if (!dir) {
acpid_log("ERR: opendir(%s): %s\n",
confdir, strerror(errno));
unlock_rules();
return -1;
}
/* scan all the files */
while ((dirent = readdir(dir))) {
int len;
struct rule *r;
if (dirent->d_name[0] == '.')
continue; /* skip dotfiles */
len = strlen(dirent->d_name) + strlen(confdir) + 2;
file = (char *)malloc(len);
if (!file) {
acpid_log("ERR: malloc(): %s\n", strerror(errno));
unlock_rules();
return -1;
}
snprintf(file, len, "%s/%s", confdir, dirent->d_name);
/* check for run-parts style filename */
basen= basename(file);
if (regcomp(&preg, "^[a-zA-Z0-9_-]+$", RULE_REGEX_FLAGS) == 0){
if (regexec(&preg, basen, 0, NULL, 0) == 0){
r = parse_file(file);
if (r) {
enlist_rule(&cmd_list, r);
nrules++;
}
}
else {
acpid_log("DBG: ignoring conf file %s\n", file);
}
}
free(basen);
free(file);
}
closedir(dir);
unlock_rules();
acpid_log("%d rule%s loaded\n", nrules, (nrules == 1)?"":"s");
return 0;
}
/*
* cleanup all rules
*/
int
acpid_cleanup_rules(void)
{
struct rule *p;
struct rule *next;
lock_rules();
if (acpid_debug >= 3) {
acpid_log("DBG: cleaning up rules\n");
}
/* tell our clients to buzz off */
p = client_list.head;
while (p) {
next = p->next;
delist_rule(&client_list, p);
close(p->action.fd);
free_rule(p);
p = next;
}
/* clear out our conf rules */
p = cmd_list.head;
while (p) {
next = p->next;
delist_rule(&cmd_list, p);
free_rule(p);
p = next;
}
unlock_rules();
return 0;
}
static struct rule *
parse_file(const char *file)
{
FILE *fp;
char buf[512];
int line = 0;
struct rule *r;
if (acpid_debug) {
acpid_log("DBG: parsing conf file %s\n", file);
}
fp = fopen(file, "r");
if (!fp) {
acpid_log("ERR: fopen(%s): %s\n", file, strerror(errno));
return NULL;
}
/* make a new rule */
r = new_rule();
if (!r) {
fclose(fp);
return NULL;
}
r->type = RULE_CMD;
r->origin = strdup(file);
if (!r->origin) {
acpid_log("ERR: strdup(): %s\n", strerror(errno));
free_rule(r);
fclose(fp);
return NULL;
}
/* read each line */
while (!feof(fp)) {
char *p = buf;
char key[64];
char val[512];
int n;
line++;
memset(key, 0, sizeof(key));
memset(val, 0, sizeof(val));
if (fgets(buf, sizeof(buf)-1, fp) == NULL) {
continue;
}
/* skip leading whitespace */
while (*p && isspace((int)*p)) {
p++;
}
/* blank lines and comments get ignored */
if (!*p || *p == '#') {
continue;
}
/* quick parse */
n = sscanf(p, "%63[^=\n]=%255[^\n]", key, val);
if (n != 2) {
acpid_log("can't parse %s at line %d\n",
file, line);
continue;
}
if (acpid_debug >= 3) {
acpid_log("DBG: key=\"%s\" val=\"%s\"\n", key, val);
}
/* handle the parsed line */
if (!strcasecmp(key, "event")) {
int rv;
r->event = malloc(sizeof(regex_t));
if (!r) {
acpid_log("ERR: malloc(): %s\n",
strerror(errno));
free_rule(r);
fclose(fp);
return NULL;
}
rv = regcomp(r->event, val, RULE_REGEX_FLAGS);
if (rv) {
char buf[128];
regerror(rv, r->event, buf, sizeof(buf));
acpid_log("ERR: regcomp(): %s\n", buf);
free_rule(r);
fclose(fp);
return NULL;
}
} else if (!strcasecmp(key, "action")) {
if (check_escapes(val) < 0) {
acpid_log("ERR: can't load file %s\n", file);
free_rule(r);
fclose(fp);
return NULL;
}
r->action.cmd = strdup(val);
if (!r->action.cmd) {
acpid_log("ERR: strdup(): %s\n",
strerror(errno));
free_rule(r);
fclose(fp);
return NULL;
}
} else {
acpid_log("unknown option '%s' in %s at line %d\n",
key, file, line);
continue;
}
}
if (!r->event || !r->action.cmd) {
if (acpid_debug) {
acpid_log("DBG: skipping incomplete file %s\n", file);
}
free_rule(r);
return NULL;
}
fclose(fp);
return r;
}
int
acpid_add_client(int clifd, const char *origin)
{
struct rule *r;
int nrules = 0;
acpid_log("client connected from %s\n", origin);
r = parse_client(clifd);
if (r) {
r->origin = strdup(origin);
enlist_rule(&client_list, r);
nrules++;
}
acpid_log("%d client rule%s loaded\n", nrules, (nrules == 1)?"":"s");
return 0;
}
static struct rule *
parse_client(int client)
{
struct rule *r;
int rv;
/* make a new rule */
r = new_rule();
if (!r) {
return NULL;
}
r->type = RULE_CLIENT;
r->action.fd = client;
r->event = malloc(sizeof(regex_t));
if (!r) {
acpid_log("ERR: malloc(): %s\n", strerror(errno));
free_rule(r);
return NULL;
}
rv = regcomp(r->event, ".*", RULE_REGEX_FLAGS);
if (rv) {
char buf[128];
regerror(rv, r->event, buf, sizeof(buf));
acpid_log("ERR: regcomp(): %s\n", buf);
free_rule(r);
return NULL;
}
return r;
}
/*
* a few rule methods
*/
static void
enlist_rule(struct rule_list *list, struct rule *r)
{
r->next = r->prev = NULL;
if (!list->head) {
list->head = list->tail = r;
} else {
list->tail->next = r;
r->prev = list->tail;
list->tail = r;
}
}
static void
delist_rule(struct rule_list *list, struct rule *r)
{
if (r->next) {
r->next->prev = r->prev;
} else {
list->tail = r->prev;
}
if (r->prev) {
r->prev->next = r->next;
} else {
list->head = r->next;;
}
r->next = r->prev = NULL;
}
static struct rule *
new_rule(void)
{
struct rule *r;
r = malloc(sizeof(*r));
if (!r) {
acpid_log("ERR: malloc(): %s\n", strerror(errno));
return NULL;
}
r->type = RULE_NONE;
r->origin = NULL;
r->event = NULL;
r->action.cmd = NULL;
r->prev = r->next = NULL;
return r;
}
/* I hope you delisted the rule before you free() it */
static void
free_rule(struct rule *r)
{
if (r->type == RULE_CMD) {
if (r->action.cmd) {
free(r->action.cmd);
}
}
if (r->origin) {
free(r->origin);
}
if (r->event) {
regfree(r->event);
free(r->event);
}
free(r);
}
/*
* the main hook for propogating events
*/
int
acpid_handle_event(const char *event)
{
struct rule *p;
int nrules = 0;
struct rule_list *ar[] = { &client_list, &cmd_list, NULL };
struct rule_list **lp;
/* make an event be atomic wrt known signals */
lock_rules();
/* scan each rule list for any rules that care about this event */
for (lp = ar; *lp; lp++) {
struct rule_list *l = *lp;
p = l->head;
while (p) {
/* the list can change underneath us */
struct rule *pnext = p->next;
if (!regexec(p->event, event, 0, NULL, 0)) {
/* a match! */
if (acpid_debug) {
acpid_log("DBG: rule from %s matched\n",
p->origin);
}
nrules++;
if (p->type == RULE_CMD) {
do_cmd_rule(p, event);
} else if (p->type == RULE_CLIENT) {
do_client_rule(p, event);
} else {
acpid_log("unknown rule type: %d\n",
p->type);
}
} else {
if (acpid_debug >= 3) {
acpid_log("DBG: rule from %s did not "
"match\n", p->origin);
}
}
p = pnext;
}
}
unlock_rules();
if (acpid_debug) {
acpid_log("DBG: %d total rule%s matched\n",
nrules, (nrules == 1)?"":"s");
}
return 0;
}
/* helper functions to block signals while iterating */
static sigset_t *
signals_handled(void)
{
static int been_here;
static sigset_t sigset;
been_here = 1;
sigemptyset(&sigset);
sigaddset(&sigset, SIGHUP);
sigaddset(&sigset, SIGTERM);
sigaddset(&sigset, SIGQUIT);
sigaddset(&sigset, SIGINT);
return &sigset;
}
static void
lock_rules(void)
{
if (acpid_debug >= 4) {
acpid_log("DBG: blocking signals for rule lock\n");
}
sigprocmask(SIG_BLOCK, signals_handled(), NULL);
}
static void
unlock_rules(void)
{
if (acpid_debug >= 4) {
acpid_log("DBG: unblocking signals for rule lock\n");
}
sigprocmask(SIG_UNBLOCK, signals_handled(), NULL);
}
/*
* the meat of the rules
*/
static int
do_cmd_rule(struct rule *rule, const char *event)
{
pid_t pid;
int status;
const char *action;
pid = fork();
switch (pid) {
case -1:
acpid_log("ERR: fork(): %s\n", strerror(errno));
return -1;
case 0: /* child */
/* parse the commandline, doing any substitutions needed */
action = parse_cmd(rule->action.cmd, event);
acpid_log("executing action \"%s\"\n", action);
signal(SIGHUP, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
acpid_log("BEGIN HANDLER MESSAGES\n");
execl("/bin/sh", "/bin/sh", "-c", action, NULL);
/* should not get here */
acpid_log("ERR: execl(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
/* parent */
waitpid(pid, &status, 0);
acpid_log("END HANDLER MESSAGES\n");
if (WIFEXITED(status)) {
acpid_log("action exited with status %d\n",
WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
acpid_log("action exited on signal %d\n", WTERMSIG(status));
} else {
acpid_log("action exited with status %d\n", status);
}
return 0;
}
static int
do_client_rule(struct rule *rule, const char *event)
{
int r;
int client = rule->action.fd;
acpid_log("notifying client %s\n", rule->origin);
r = safe_write(client, event, strlen(event));
if (r < 0 && errno == EPIPE) {
/* closed */
acpid_log("client has disconnected\n");
delist_rule(&client_list, rule);
return -1;
}
safe_write(client, "\n", 1);
return 0;
}
#define NTRIES 100
static int
safe_write(int fd, const char *buf, int len)
{
int r;
int ttl = 0;
int ntries = NTRIES;
do {
r = write(fd, buf+ttl, len-ttl);
if (r < 0) {
if (errno != EAGAIN && errno != EINTR) {
/* a legit error */
return r;
}
ntries--;
} else if (r > 0) {
/* as long as we make forward progress, reset ntries */
ntries = NTRIES;
ttl += r;
}
} while (ttl < len && ntries);
if (!ntries) {
/* crap */
if (acpid_debug >= 2) {
acpid_log("uh-oh! safe_write() timed out\n");
}
return r;
}
return ttl;
}
static char *
parse_cmd(const char *cmd, const char *event)
{
static char buf[4096];
int i;
const char *p;
p = cmd;
i = 0;
memset(buf, 0, sizeof(buf));
while (i < (sizeof(buf)-1)) {
if (*p == '%') {
p++;
if (*p == 'e') {
/* handle an event expansion */
int size;
size = snprintf(&buf[i], size, "%s", event);
i += size;
p++;
continue;
}
}
if (!*p) {
break;
}
buf[i++] = *p++;
}
if (acpid_debug >= 2) {
acpid_log("DBG: expanded \"%s\" -> \"%s\"\n", cmd, buf);
}
return buf;
}
static int
check_escapes(const char *str)
{
const char *p;
int r = 0;
p = str;
while (*p) {
/* found an escape */
if (*p == '%') {
p++;
if (!*p) {
acpid_log("invalid escape at EOL\n");
return -1;
} else if (*p != '%' && *p != 'e') {
acpid_log("invalid escape \"%%%c\"\n", *p);
r = -1;
}
}
p++;
}
return r;
}
|