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
|
/* upsdrvctl.c - UPS model driver controller
Copyright (C) 2001 Russell Kroll <rkroll@exploits.org>
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "config.h"
#include "shared.h"
#include "proto.h"
#include "version.h"
#include "common.h"
#include "upsconf.h"
typedef struct {
char *upsname;
char *driver;
char *port;
int sdorder;
int maxstartdelay;
void *next;
} ups_t;
ups_t *upstable = NULL;
int execwait, verbose = 0, maxsdorder = 0, testmode = 0;
/* timer - keeps us from getting stuck if a driver hangs */
int maxstartdelay = 45;
void do_upsconf_args(char *upsname, char *var, char *val)
{
ups_t *tmp, *last;
/* only one global argument right now */
if (!upsname) {
if (!strcmp(var, "maxstartdelay"))
maxstartdelay = atoi(val);
return;
}
last = tmp = upstable;
while (tmp) {
last = tmp;
if (!strcmp(tmp->upsname, upsname)) {
if (!strcmp(var, "driver"))
tmp->driver = xstrdup(val);
if (!strcmp(var, "port"))
tmp->port = xstrdup(val);
if (!strcmp(var, "maxstartdelay"))
tmp->maxstartdelay = atoi(val);
if (!strcmp(var, "sdorder")) {
tmp->sdorder = atoi(val);
if (tmp->sdorder > maxsdorder)
maxsdorder = tmp->sdorder;
}
return;
}
tmp = tmp->next;
}
tmp = xmalloc(sizeof(ups_t));
tmp->upsname = xstrdup(upsname);
tmp->driver = NULL;
tmp->port = NULL;
tmp->next = NULL;
tmp->sdorder = 0;
tmp->maxstartdelay = -1; /* use global value by default */
if (!strcmp(var, "driver"))
tmp->driver = xstrdup(val);
if (!strcmp(var, "port"))
tmp->port = xstrdup(val);
if (last)
last->next = tmp;
else
upstable = tmp;
}
int read_pid_file(char *upsname, char *driver, char *port)
{
char pidfn[SMALLBUF], buf[SMALLBUF];
int ret, pid;
struct stat fs;
FILE *pidf;
snprintf(pidfn, sizeof(pidfn), "%s/%s-%s.pid", ALTPIDPATH,
driver, xbasename(port));
ret = stat(pidfn, &fs);
if (ret != 0) {
upslog(LOG_ERR, "Can't open %s", pidfn);
return -1;
}
pidf = fopen(pidfn, "r");
if (!pidf) {
upslog(LOG_ERR, "Can't open %s", pidfn);
return -1;
}
fgets(buf, sizeof(buf), pidf);
fclose(pidf);
buf[strlen(buf)-1] = '\0';
pid = strtol(buf, (char **)NULL, 10);
return pid;
}
/* handle sending the signal */
void send_term(char *upsname, char *driver, char *port)
{
char pidfn[SMALLBUF], buf[SMALLBUF];
int ret, pid;
struct stat fs;
FILE *pidf;
printf("Stopping UPS: %s\n", upsname);
snprintf(pidfn, sizeof(pidfn), "%s/%s-%s.pid", ALTPIDPATH,
driver, xbasename(port));
ret = stat(pidfn, &fs);
if (ret != 0) {
upslog(LOG_ERR, "Can't open %s", pidfn);
return;
}
pidf = fopen(pidfn, "r");
if (!pidf) {
upslog(LOG_ERR, "Can't open %s", pidfn);
return;
}
fgets(buf, sizeof(buf), pidf);
buf[strlen(buf)-1] = '\0';
pid = strtol(buf, (char **)NULL, 10);
if (pid < 2) {
upslogx(LOG_NOTICE, "Ignoring invalid pid %d in %s",
pid, pidfn);
return;
}
if (verbose)
printf("Sending signal: kill -TERM %d\n", pid);
if (testmode)
return;
ret = kill(pid, SIGTERM);
if (ret < 0) {
upslog(LOG_ERR, "kill pid %d failed", pid);
return;
}
}
/* stop user-selected driver */
void stop_one_driver(char *upsname)
{
ups_t *tmp = upstable;
if (!tmp)
fatalx("Error: no UPS definitions found in ups.conf!\n");
while (tmp) {
if (!strcmp(tmp->upsname, upsname)) {
send_term(tmp->upsname, tmp->driver, tmp->port);
return;
}
tmp = tmp->next;
}
fatalx("UPS %s not found in ups.conf", upsname);
}
/* walk ups table, but stop drivers instead */
void stop_all_drivers(void)
{
ups_t *tmp = upstable;
if (!tmp)
fatalx("Error: no UPS definitions found in ups.conf!\n");
while (tmp) {
send_term(tmp->upsname, tmp->driver, tmp->port);
tmp = tmp->next;
}
exit(0);
}
/* check user-selected driver */
void check_one_driver(char *upsname)
{
ups_t *tmp = upstable;
int pid, stat_error = 0;
if (!tmp)
fatalx("Error: no UPS definitions found in ups.conf!\n");
while (tmp) {
if (!strcmp(tmp->upsname, upsname)) {
pid = read_pid_file(tmp->upsname, tmp->driver,
tmp->port);
if (pid < 2)
stat_error = 1;
if (verbose) {
if (pid > 1)
printf("UPS %s: driver %s (PID %d)\n",
tmp->upsname, tmp->driver, pid);
else
printf("UPS %s: driver %s not running "
"or invalid PID\n",
tmp->upsname, tmp->driver);
}
exit(stat_error);
}
tmp = tmp->next;
}
fatalx("UPS %s not found in ups.conf", upsname);
}
/* walk ups table and make sure they're still running */
void check_all_drivers(void)
{
ups_t *tmp = upstable;
int pid, stat_error = 0;
if (!tmp)
fatalx("Error: no UPS definitions found in ups.conf!\n");
while (tmp) {
pid = read_pid_file(tmp->upsname, tmp->driver, tmp->port);
if (pid < 2)
stat_error = 1;
if (verbose) {
if (pid > 1)
printf("UPS %s: driver %s (PID %d)\n",
tmp->upsname, tmp->driver, pid);
else
printf("UPS %s: driver %s not running or "
"invalid PID\n",
tmp->upsname, tmp->driver);
}
tmp = tmp->next;
}
exit(stat_error);
}
/* reap exiting children */
void sigchld(int sig)
{
execwait = 0;
}
/* break free of drivers that don't start up quickly */
void sigalrm(int sig)
{
upslogx(LOG_NOTICE, "Startup timer elapsed, continuing...");
execwait = 0;
}
void forkexec(char *prog, ups_t *ups)
{
int ret;
char *argv[4];
/* TODO: consider redoing this with wait() depending on portability */
/* parent spins until the child exits */
execwait = 1;
ret = fork();
if (ret < 0)
fatal("fork");
/* parent */
if (ret != 0) {
/* catching this will unset execwait */
signal(SIGCHLD, sigchld);
/* give us a way out if the child gets stuck */
signal(SIGALRM, sigalrm);
if (ups->maxstartdelay != -1)
alarm(ups->maxstartdelay);
else
alarm(maxstartdelay);
/* spin until the child is done */
while (execwait)
usleep(250000);
alarm(0);
signal(SIGALRM, SIG_IGN);
return;
}
if (verbose)
printf("exec: %s -a %s\n", prog, ups->upsname);
argv[0] = xstrdup(prog);
argv[1] = "-a";
argv[2] = ups->upsname;
argv[3] = NULL;
/* child */
if (testmode)
exit(0);
ret = execve(prog, argv, NULL);
/* should not be reached */
fatal("execve");
}
void start_driver(ups_t *ups)
{
char dfn[SMALLBUF];
int ret;
struct stat fs;
snprintf(dfn, sizeof(dfn), "%s/%s", MODELPATH, ups->driver);
ret = stat(dfn, &fs);
if (ret < 0)
fatal("Can't start %s", dfn);
forkexec(dfn, ups);
}
/* start user-selected driver */
void start_one_driver(char *upsname)
{
ups_t *tmp = upstable;
if (!tmp)
fatalx("Error: no UPS definitions found in ups.conf!\n");
while (tmp) {
if (!strcmp(tmp->upsname, upsname)) {
start_driver(tmp);
return;
}
tmp = tmp->next;
}
fatalx("UPS %s not found in ups.conf", upsname);
}
/* walk ups table and invoke drivers */
void start_all_drivers(void)
{
ups_t *tmp = upstable;
if (!tmp)
fatalx("Error: no UPS definitions found in ups.conf!\n");
while (tmp) {
start_driver(tmp);
tmp = tmp->next;
}
}
void help(const char *progname)
{
printf("Starts and stops UPS drivers via ups.conf.\n\n");
printf("usage: %s [-h] [-t] [-v] (start | stop | shutdown [<ups>])\n\n", progname);
printf(" -h display this help\n");
printf(" -v enable verbose messages\n");
printf(" -t testing mode - prints actions without doing them\n");
printf(" start start all UPS drivers in ups.conf\n");
printf(" start <ups> only start driver for UPS <ups>\n");
printf(" stop stop all UPS drivers in ups.conf\n");
printf(" stop <ups> only stop driver for UPS <ups>\n");
printf(" status check all driver PIDs, returns 0 on success\n");
printf(" status <ups> check just UPS <ups>\n");
printf(" shutdown shutdown all UPS drivers in ups.conf\n");
printf(" shutdown <ups> only shutdown UPS <ups>\n");
exit(1);
}
void shutdown_driver(ups_t *ups)
{
int ret;
char *argv[7], dfn[SMALLBUF];
/* parent spins until the child exits */
execwait = 1;
ret = fork();
if (ret < 0)
fatal("fork");
/* parent */
if (ret != 0) {
/* wait for child to return */
signal(SIGCHLD, sigchld);
/* give us a way out if the child gets stuck */
signal(SIGALRM, sigalrm);
if (ups->maxstartdelay != -1)
alarm(ups->maxstartdelay);
else
alarm(maxstartdelay);
while (execwait)
usleep(250000);
alarm(0);
signal(SIGALRM, SIG_IGN);
return;
}
/* child */
snprintf(dfn, sizeof(dfn), "%s/%s", MODELPATH, ups->driver);
if (verbose)
printf("exec: %s -a %s -d 0 -k\n", dfn, ups->upsname);
argv[0] = dfn;
argv[1] = "-a";
argv[2] = xstrdup(ups->upsname);
argv[3] = "-d";
argv[4] = "0";
argv[5] = "-k";
argv[6] = NULL;
if (testmode)
exit(0);
ret = execve(dfn, argv, NULL);
/* should not be reached */
fatal("execve");
}
void shutdown_one_driver(char *upsname)
{
ups_t *tmp = upstable;
if (!tmp)
fatalx("Error: no UPS definitions found in ups.conf!\n");
while (tmp) {
if (!strcmp(tmp->upsname, upsname)) {
shutdown_driver(tmp);
return;
}
tmp = tmp->next;
}
fatalx("UPS %s not found in ups.conf", upsname);
}
/* walk UPS table and shut down all UPSes according to sdorder */
void shutdown_all_drivers(void)
{
ups_t *tmp;
int i;
if (!upstable)
fatalx("Error: no UPS definitions found in ups.conf");
for (i = 0; i <= maxsdorder; i++) {
tmp = upstable;
while (tmp) {
if (tmp->sdorder == i)
shutdown_driver(tmp);
tmp = tmp->next;
}
}
exit(0);
}
int main(int argc, char **argv)
{
int i;
char *prog;
printf("Network UPS Tools - UPS driver controller 0.20 (%s)\n",
UPS_VERSION);
prog = argv[0];
while ((i = getopt(argc, argv, "+htv")) != EOF) {
switch(i) {
case 'v':
verbose++;
break;
case 't':
testmode = 1;
/* force verbose mode while testing */
if (verbose == 0)
verbose = 1;
break;
case 'h':
default:
help(prog);
break;
}
}
argc -= optind;
argv += optind;
if (argc < 1)
help(prog);
if (testmode)
printf("*** Testing mode: not calling exec/kill\n");
if (!strcmp(argv[0], "start")) {
read_upsconf(1);
if (argc == 1)
start_all_drivers();
else
start_one_driver(argv[1]);
exit(0);
}
if (!strcmp(argv[0], "stop")) {
read_upsconf(1);
if (argc == 1)
stop_all_drivers();
else
stop_one_driver(argv[1]);
exit(0);
}
if (!strcmp(argv[0], "status")) {
read_upsconf(1);
if (argc == 1)
check_all_drivers();
else
check_one_driver(argv[1]);
}
if (!strcmp(argv[0], "shutdown")) {
read_upsconf(1);
if (argc == 1)
shutdown_all_drivers();
else
shutdown_one_driver(argv[1]);
exit(0);
}
fatalx("Error: unrecognized command [%s]\n", argv[0]);
/* NOTREACHED */
return 0;
}
|