~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to clients/upsmon.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
        /* sum of all power values from config file */
47
47
static  int     totalpv = 0;
48
48
 
49
 
        /* should we go into an infinite loop upon shutdown? */
50
 
static  int     playdead = 0;
51
 
 
52
49
        /* default replace battery warning interval (seconds) */
53
50
static  int     rbwarntime = 43200;
54
51
 
78
75
 
79
76
        /* signal handling things */
80
77
static  struct sigaction sa;
81
 
static  sigset_t upsm_sigmask;
 
78
static  sigset_t nut_upsmon_sigmask;
82
79
 
83
80
#ifdef SHUT_RDWR
84
81
#define shutdown_how SHUT_RDWR
86
83
#define shutdown_how 2
87
84
#endif
88
85
 
89
 
static void debug(char *format, ...)
 
86
static void debug(const char *format, ...)
90
87
{
91
88
#ifdef HAVE_STDARG_H
92
89
        va_list args;
106
103
{
107
104
        *val = (*val |= flag);
108
105
}
109
 
        
 
106
 
110
107
static void clearflag(int *val, int flag)  
111
108
{
112
109
        *val = (*val ^= (*val & flag));
174
171
                }
175
172
        }
176
173
 
177
 
        exit(0);
 
174
        exit(EXIT_SUCCESS);
178
175
}
179
176
 
180
177
static void do_notify(const utype *ups, int ntype)
212
209
 
213
210
        /* this shouldn't happen (LOGIN checks it earlier) */
214
211
        if ((ups->upsname == NULL) || (strlen(ups->upsname) == 0)) {
215
 
                upslogx(LOG_ERR, "Set master on UPS [%s] failed: empty upsname");
 
212
                upslogx(LOG_ERR, "Set master on UPS [%s] failed: empty upsname",
 
213
                        ups->sys);
216
214
                return 0;
217
215
        }
218
216
 
293
291
 
294
292
        /* we require a upsname now */
295
293
        if ((ups->upsname == NULL) || (strlen(ups->upsname) == 0)) {
296
 
                upslogx(LOG_ERR, "Login to UPS [%s] failed: empty upsname");
 
294
                upslogx(LOG_ERR, "Login to UPS [%s] failed: empty upsname",
 
295
                        ups->sys);
297
296
                return 0;
298
297
        }
299
298
 
414
413
}
415
414
 
416
415
/* create the flag file if necessary */
417
 
static void setpdflag(void)
 
416
static void set_pdflag(void)
418
417
{
419
418
        FILE    *pdf;
420
419
 
421
 
        if (powerdownflag != NULL) {
422
 
                pdf = fopen(powerdownflag, "w");
423
 
                if (!pdf) {
424
 
                        upslogx(LOG_ERR, "Failed to create power down flag!");
425
 
                        return;
426
 
                }
427
 
                fprintf(pdf, "%s", SDMAGIC);
428
 
                fclose(pdf);
 
420
        if (!powerdownflag)
 
421
                return;
 
422
 
 
423
        pdf = fopen(powerdownflag, "w");
 
424
        if (!pdf) {
 
425
                upslogx(LOG_ERR, "Failed to create power down flag!");
 
426
                return;
429
427
        }
 
428
 
 
429
        fprintf(pdf, "%s", SDMAGIC);
 
430
        fclose(pdf);
430
431
}
431
432
 
432
433
/* the actual shutdown procedure */
454
455
                if (geteuid() != 0)
455
456
                        upslogx(LOG_WARNING, "Not root, shutdown may fail");
456
457
 
457
 
                setpdflag();
 
458
                set_pdflag();
458
459
 
459
460
                ret = system(shutdowncmd);
460
461
 
461
462
                if (ret != 0)
462
463
                        upslogx(LOG_ERR, "Unable to call shutdown command: %s\n",
463
 
                                shutdowncmd);
 
464
                                shutdowncmd);
464
465
        }
465
466
 
466
 
        /* if instructed to go into the infinite loop, then do so */
467
 
        if (playdead == 1)
468
 
                for (;;)
469
 
                        sleep(100);
470
 
 
471
 
        /* hopefully not reached */
472
 
        exit(1);
 
467
        exit(EXIT_SUCCESS);
473
468
}
474
469
 
475
470
/* set forced shutdown flag so other upsmons know what's going on here */
478
473
        char    buf[SMALLBUF];
479
474
        int     ret;
480
475
 
 
476
        /* this shouldn't happen */
 
477
        if (!ups->upsname) {
 
478
                upslogx(LOG_ERR, "setfsd: programming error: no UPS name set [%s]",
 
479
                        ups->sys);
 
480
                return;
 
481
        }
 
482
 
481
483
        debug("Setting FSD on UPS %s\n", ups->sys);
482
484
 
483
 
        if (ups->upsname)
484
 
                snprintf(buf, sizeof(buf), "FSD %s\n", ups->upsname);
485
 
        else
486
 
                snprintf(buf, sizeof(buf), "FSD\n");
 
485
        snprintf(buf, sizeof(buf), "FSD %s\n", ups->upsname);
487
486
 
488
487
        ret = upscli_sendline(&ups->conn, buf, strlen(buf));
489
488
 
490
489
        if (ret < 0) {
491
490
                upslogx(LOG_ERR, "FSD set on UPS %s failed: %s", ups->sys,
492
 
                        upscli_strerror(&ups->conn));
 
491
                        upscli_strerror(&ups->conn));
493
492
                return;
494
493
        }
495
494
 
496
495
        ret = upscli_readline(&ups->conn, buf, sizeof(buf));
497
496
 
 
497
        if (ret < 0) {
 
498
                upslogx(LOG_ERR, "FSD set on UPS %s failed: %s", ups->sys,
 
499
                        upscli_strerror(&ups->conn));
 
500
                return;
 
501
        }
 
502
 
498
503
        if (!strncmp(buf, "OK", 2))
499
504
                return;
500
505
 
501
 
        if (ret < 0)
502
 
                upslogx(LOG_ERR, "FSD set on UPS %s failed: %s", ups->sys,
503
 
                        upscli_strerror(&ups->conn));
504
 
        else
505
 
                upslogx(LOG_ERR, "FSD set on UPS %s failed: %s", ups->sys,
506
 
                        buf);
 
506
        /* protocol error: upsd said something other than "OK" */
 
507
        upslogx(LOG_ERR, "FSD set on UPS %s failed: %s", ups->sys, buf);
507
508
}
508
509
 
509
510
static void set_alarm(void)
517
518
        alarm(0);
518
519
}
519
520
 
520
 
static int old_get_var(utype *ups, const char *var, char *buf, size_t bufsize)
521
 
{
522
 
        debug("old_get_var: %s / %s\n", ups->sys, var);
523
 
 
524
 
        if (!strcmp(var, "numlogins"))
525
 
                return upscli_getvar(&ups->conn, ups->upsname, "numlogins",
526
 
                        buf, bufsize);
527
 
 
528
 
        if (!strcmp(var, "status"))
529
 
                return upscli_getvar(&ups->conn, ups->upsname, "status",
530
 
                        buf, bufsize);
531
 
 
532
 
        upslogx(LOG_ERR, "old_get_var: programming error: var=%s", var);
533
 
        return -1;
534
 
}
535
 
 
536
 
static int new_get_var(utype *ups, const char *var, char *buf, size_t bufsize)
537
 
{
538
 
        int     ret, numq, numa;
 
521
static int get_var(utype *ups, const char *var, char *buf, size_t bufsize)
 
522
{
 
523
        int     ret;
 
524
        unsigned int    numq, numa;
539
525
        const   char    *query[4];
540
526
        char    **answer;
541
527
 
542
 
        /* can't do the new stuff without a UPS name set */
543
 
 
544
 
        /* this shouldn't happen (we now require upsnames) */
 
528
        /* this shouldn't happen */
545
529
        if (!ups->upsname) {
546
 
                upslogx(LOG_INFO, "UPS [%s]: no upsname set, forcing compatibility mode",
 
530
                upslogx(LOG_ERR, "get_var: programming error: no UPS name set [%s]",
547
531
                        ups->sys);
548
 
                ups->old = 1;
549
 
                return old_get_var(ups, var, buf, bufsize);
 
532
                return -1;
550
533
        }
551
534
 
552
535
        numq = 0;
565
548
        }
566
549
 
567
550
        if (numq == 0) {
568
 
                upslogx(LOG_ERR, "new_get_var: programming error: var=%s", var);
 
551
                upslogx(LOG_ERR, "get_var: programming error: var=%s", var);
569
552
                return -1;
570
553
        }
571
554
 
572
 
        debug("new_get_var: %s / %s\n", ups->sys, var);
 
555
        debug("get_var: %s / %s\n", ups->sys, var);
573
556
 
574
557
        ret = upscli_get(&ups->conn, numq, query, &numa, &answer);
575
558
 
578
561
                /* detect old upsd */
579
562
                if (upscli_upserror(&ups->conn) == UPSCLI_ERR_UNKCOMMAND) {
580
563
 
581
 
                        upslogx(LOG_INFO, "UPS [%s]: using compatibility mode",
 
564
                        upslogx(LOG_ERR, "UPS [%s]: Too old to monitor",
582
565
                                ups->sys);
583
 
 
584
 
                        ups->old = 1;
585
 
                        return old_get_var(ups, var, buf, bufsize);
 
566
                        return -1;
586
567
                }
587
568
 
588
569
                /* some other error */
600
581
        return 0;
601
582
}
602
583
 
603
 
static int get_var(utype *ups, const char *var, char *buf, size_t bufsize)
604
 
{
605
 
        if (ups->old)
606
 
                return old_get_var(ups, var, buf, bufsize);
607
 
 
608
 
        /* try it the new way */
609
 
        return new_get_var(ups, var, buf, bufsize);
610
 
}
611
 
 
612
584
static void slavesync(void)
613
585
{
614
586
        utype   *ups;
666
638
        for (ups = firstups; ups != NULL; ups = ups->next)
667
639
                if (flag_isset(ups->status, ST_MASTER)) {
668
640
                        isamaster = 1;
669
 
                        setfsd (ups);
 
641
                        setfsd(ups);
670
642
                }
671
643
 
672
644
        /* if we're not a master on anything, we should shut down now */
804
776
{
805
777
        debug("Dropping connection to UPS [%s]\n", ups->sys);
806
778
 
807
 
        /* reset this in case upsd gets upgraded */
808
 
        ups->old = 0;
809
 
 
810
779
        ups->commstate = 0;
811
780
        ups->linestate = 0;
812
781
        clearflag(&ups->status, ST_LOGIN);
991
960
        tmp->lastnoncrit = 0;
992
961
        tmp->lastrbwarn = 0;
993
962
        tmp->lastncwarn = 0;
994
 
        tmp->old = 0;
995
963
 
996
964
        if (!strcasecmp(master, "master"))
997
965
                setflag(&tmp->status, ST_MASTER);
1005
973
 
1006
974
        if (tmp->pv)
1007
975
                upslogx(LOG_INFO, "UPS: %s (%s) (power value %d)", tmp->sys, 
1008
 
                        flag_isset(tmp->status, ST_MASTER) ? "master" : "slave",
 
976
                        flag_isset(tmp->status, ST_MASTER) ? "master" : "slave",
1009
977
                        tmp->pv);
1010
978
        else
1011
979
                upslogx(LOG_INFO, "UPS: %s (monitoring only)", tmp->sys);
1012
980
 
1013
 
        upscli_splitname(tmp->sys, &tmp->upsname, &tmp->hostname, &tmp->port);
 
981
        tmp->upsname = tmp->hostname = NULL;    
 
982
 
 
983
        if (upscli_splitname(tmp->sys, &tmp->upsname, &tmp->hostname, 
 
984
                &tmp->port) != 0) {
 
985
                upslogx(LOG_ERR, "Error: unable to split UPS name [%s]",
 
986
                        tmp->sys);
 
987
        }
1014
988
 
1015
989
        if (!tmp->upsname)
1016
990
                upslogx(LOG_WARNING, "Warning: UPS [%s]: no upsname set!",
1272
1246
}
1273
1247
 
1274
1248
/* called for fatal errors in parseconf like malloc failures */
1275
 
void upsmon_err(const char *errmsg)
 
1249
static void upsmon_err(const char *errmsg)
1276
1250
{
1277
1251
        upslogx(LOG_ERR, "Fatal error in parseconf(upsmon.conf): %s", errmsg);
1278
1252
}
1294
1268
                        return;
1295
1269
                }
1296
1270
 
1297
 
                fatal("%s", ctx.errmsg);
 
1271
                fatalx("%s", ctx.errmsg);
1298
1272
        }
1299
1273
 
1300
1274
        while (pconf_file_next(&ctx)) {
1308
1282
                        continue;
1309
1283
 
1310
1284
                if (!parse_conf_arg(ctx.numargs, ctx.arglist)) {
1311
 
                        int     i;
 
1285
                        unsigned int    i;
1312
1286
                        char    errmsg[SMALLBUF];
1313
1287
 
1314
1288
                        snprintf(errmsg, sizeof(errmsg), 
1319
1293
                                snprintfcat(errmsg, sizeof(errmsg), " %s", 
1320
1294
                                        ctx.arglist[i]);
1321
1295
 
1322
 
                        upslogx(LOG_WARNING, errmsg);
 
1296
                        upslogx(LOG_WARNING, "%s", errmsg);
1323
1297
                }
1324
1298
        }
1325
1299
 
1335
1309
/* SIGQUIT, SIGTERM handler */
1336
1310
static void set_exit_flag(int sig)
1337
1311
{
1338
 
        upslogx(LOG_INFO, "Signal %d: exiting", sig);
1339
 
        exit_flag = 1;
 
1312
        exit_flag = sig;
1340
1313
}
1341
1314
 
1342
1315
static void ups_free(utype *ups)
1354
1327
        free(ups);
1355
1328
}
1356
1329
 
1357
 
static void upsmon_exit(void)
 
1330
static void upsmon_cleanup(void)
1358
1331
{
1359
1332
        int     i;
1360
1333
        utype   *utmp, *unext;
1383
1356
        for (i = 0; notifylist[i].name != NULL; i++)
1384
1357
                if (notifylist[i].msg != notifylist[i].stockmsg)
1385
1358
                        free(notifylist[i].msg);
1386
 
 
1387
 
        exit(0);
1388
1359
}
1389
1360
 
1390
1361
static void user_fsd(int sig)
1407
1378
/* install handlers for a few signals */
1408
1379
static void setup_signals(void)
1409
1380
{
1410
 
        sigemptyset(&upsm_sigmask);
1411
 
        sa.sa_mask = upsm_sigmask;
 
1381
        sigemptyset(&nut_upsmon_sigmask);
 
1382
        sa.sa_mask = nut_upsmon_sigmask;
1412
1383
        sa.sa_flags = 0;
1413
1384
 
1414
1385
        sa.sa_handler = sigpipe;
1542
1513
/* deal with the contents of STATUS or ups.status for this ups */
1543
1514
static void parse_status(utype *ups, char *status)
1544
1515
{
1545
 
        char    *stat, *ptr;
 
1516
        char    *statword, *ptr;
1546
1517
 
1547
1518
        clear_alarm();
1548
1519
 
1562
1533
        if (!strstr(status, "FSD"))
1563
1534
                clearflag(&ups->status, ST_FSD);
1564
1535
 
1565
 
        stat = status;
 
1536
        statword = status;
1566
1537
 
1567
1538
        /* split up the status words and parse each one separately */
1568
 
        while (stat != NULL) {
1569
 
                ptr = strchr(stat, ' ');
 
1539
        while (statword != NULL) {
 
1540
                ptr = strchr(statword, ' ');
1570
1541
                if (ptr)
1571
1542
                        *ptr++ = '\0';
1572
1543
 
1573
 
                debug("    parsing: [%s]: ", stat);
 
1544
                debug("    parsing: [%s]: ", statword);
1574
1545
 
1575
 
                if (!strcasecmp(stat, "OL"))
 
1546
                if (!strcasecmp(statword, "OL"))
1576
1547
                        ups_on_line(ups);
1577
 
                if (!strcasecmp(stat, "OB"))
 
1548
                if (!strcasecmp(statword, "OB"))
1578
1549
                        ups_on_batt(ups);
1579
 
                if (!strcasecmp(stat, "LB"))
 
1550
                if (!strcasecmp(statword, "LB"))
1580
1551
                        ups_low_batt(ups);
1581
 
                if (!strcasecmp(stat, "RB"))
 
1552
                if (!strcasecmp(statword, "RB"))
1582
1553
                        upsreplbatt(ups);
1583
1554
 
1584
1555
                /* do it last to override any possible OL */
1585
 
                if (!strcasecmp(stat, "FSD"))
 
1556
                if (!strcasecmp(statword, "FSD"))
1586
1557
                        ups_fsd(ups);
1587
1558
 
1588
1559
                update_crittimer(ups);
1589
1560
 
1590
 
                stat = ptr;
 
1561
                statword = ptr;
1591
1562
        } 
1592
1563
 
1593
1564
        debug("\n");
1645
1616
        }
1646
1617
}
1647
1618
 
1648
 
/* remove the power down flag if it exists and is the proper form */
1649
 
static void clearpdf(void)
 
1619
/* see if the powerdownflag file is there and proper */
 
1620
static int pdflag_status(void)
1650
1621
{
1651
1622
        FILE    *pdf;
1652
1623
        char    buf[SMALLBUF];
1653
1624
 
 
1625
        if (!powerdownflag)
 
1626
                return 0;       /* unusable */
 
1627
 
1654
1628
        pdf = fopen(powerdownflag, "r");
1655
1629
 
1656
 
        if (pdf == NULL)        /* no such file, nothing to do */
1657
 
                return;
 
1630
        if (pdf == NULL)
 
1631
                return 0;       /* not there */
1658
1632
 
1659
1633
        /* if it exists, see if it has the right text in it */
1660
1634
 
1669
1643
         * solution: don't let mere mortals edit that configuration file.
1670
1644
         */
1671
1645
 
1672
 
        if (!strncmp(buf, SDMAGIC, strlen(SDMAGIC)))    /* ok, it's from us */
 
1646
        if (!strncmp(buf, SDMAGIC, strlen(SDMAGIC)))
 
1647
                return 1;       /* exists and looks good */
 
1648
 
 
1649
        return -1;      /* error: something else is in there */
 
1650
}       
 
1651
 
 
1652
/* only remove the flag file if it's actually from us */
 
1653
static void clear_pdflag(void)
 
1654
{
 
1655
        int     ret;
 
1656
 
 
1657
        ret = pdflag_status();
 
1658
 
 
1659
        if (ret == -1)  {
 
1660
                upslogx(LOG_ERR, "POWERDOWNFLAG (%s) does not contain"
 
1661
                        "the upsmon magic string - disabling!", powerdownflag);
 
1662
                powerdownflag = NULL;
 
1663
                return;
 
1664
        }
 
1665
 
 
1666
        /* it's from us, so we can remove it */
 
1667
        if (ret == 1)
1673
1668
                unlink(powerdownflag);
1674
 
        else {
1675
 
                upslogx(LOG_INFO, "%s doesn't seem to be a proper flag file.  Disabling.",
1676
 
                          powerdownflag);
1677
 
                powerdownflag = NULL;
1678
 
        }
 
1669
}
 
1670
 
 
1671
/* exit with success only if it exists and is proper */
 
1672
static int check_pdflag(void)
 
1673
{
 
1674
        int     ret;
 
1675
 
 
1676
        ret = pdflag_status();
 
1677
 
 
1678
        if (ret == -1) {
 
1679
                upslogx(LOG_ERR, "POWERDOWNFLAG (%s) does not contain "
 
1680
                        "the upsmon magic string", powerdownflag);
 
1681
                return EXIT_FAILURE;
 
1682
        }
 
1683
 
 
1684
        if (ret == 0) {
 
1685
                /* not there - this is not a shutdown event */
 
1686
                printf("Power down flag is not set\n");
 
1687
                return EXIT_FAILURE;
 
1688
        }
 
1689
 
 
1690
        if (ret != 1) {
 
1691
                upslogx(LOG_ERR, "Programming error: pdflag_status returned %d",
 
1692
                        ret);
 
1693
                return EXIT_FAILURE;
 
1694
        }
 
1695
 
 
1696
        /* only thing left - must be time for a shutdown */
 
1697
        printf("Power down flag is set\n");
 
1698
        return EXIT_SUCCESS;
1679
1699
}
1680
1700
 
1681
1701
static void help(const char *progname)
1690
1710
        printf("                 - stop: stop monitoring and exit\n");
1691
1711
        printf("  -D            raise debugging level\n");
1692
1712
        printf("  -h            display this help\n");
1693
 
        printf("  -i            go into an infinite loop after calling shutdown\n");
 
1713
        printf("  -K            checks POWERDOWNFLAG, sets exit code to 0 if set\n");
1694
1714
        printf("  -p            always run privileged (disable privileged parent)\n");
1695
1715
        printf("  -u <user>     run child as user <user> (ignored when using -p)\n");
1696
 
        exit(0);
 
1716
 
 
1717
        exit(EXIT_SUCCESS);
1697
1718
}
1698
1719
 
1699
1720
/* set all the notify values to a default */
1707
1728
        }
1708
1729
}
1709
1730
 
1710
 
static void runparent(int pipe)
 
1731
static void runparent(int fd)
1711
1732
{
1712
1733
        int     ret;
1713
1734
        char    ch;
1717
1738
        signal(SIGUSR1, SIG_IGN);
1718
1739
        signal(SIGUSR2, SIG_IGN);
1719
1740
 
1720
 
        ret = read(pipe, &ch, 1);
 
1741
        ret = read(fd, &ch, 1);
1721
1742
 
1722
1743
        if (ret < 1) {
1723
1744
                if (errno == ENOENT)
1730
1751
                fatalx("upsmon parent: got bogus pipe command %c", ch);
1731
1752
 
1732
1753
        /* have to do this here - child is unprivileged */
1733
 
        setpdflag();
 
1754
        set_pdflag();
1734
1755
 
1735
1756
        ret = system(shutdowncmd);
1736
1757
 
1737
1758
        if (ret != 0)
1738
1759
                upslogx(LOG_ERR, "parent: Unable to call shutdown command: %s\n",
1739
 
                        shutdowncmd);
 
1760
                        shutdowncmd);
1740
1761
 
1741
 
        close(pipe);
1742
 
        exit(0);
 
1762
        close(fd);
 
1763
        exit(EXIT_SUCCESS);
1743
1764
}
1744
1765
 
1745
1766
/* fire up the split parent/child scheme */
1746
 
static void start_pipe(char *user)
 
1767
static void start_pipe(const char *user)
1747
1768
{
1748
1769
        int     ret;
1749
1770
        struct  passwd  *new_uid = NULL;
1750
1771
 
1751
 
        /* default to the --with-user value from configure */
1752
 
        if (!user)
1753
 
                user = RUN_AS_USER;
 
1772
        /* default user = the --with-user value from configure */
 
1773
        if (user)
 
1774
                new_uid = get_user_pwent(user);
 
1775
        else
 
1776
                new_uid = get_user_pwent(RUN_AS_USER);
1754
1777
 
1755
 
        /* grab user data early, while we still have the console */
1756
 
        if ((new_uid = get_user_pwent(user)) == NULL)
 
1778
        /* check user data while we still have the console */
 
1779
        if (!new_uid) 
1757
1780
                fatal("getpwnam(%s)", user);
1758
1781
 
1759
1782
        ret = pipe(pipefd);
1771
1794
                close(pipefd[1]);
1772
1795
                runparent(pipefd[0]);
1773
1796
 
1774
 
                exit(1);        /* NOTREACHED */
 
1797
                exit(EXIT_FAILURE);     /* NOTREACHED */
1775
1798
        }
1776
1799
 
1777
1800
        close(pipefd[0]);
1821
1844
}       
1822
1845
 
1823
1846
/* see if we can open a file */
1824
 
static int check_file(char *fn)
 
1847
static int check_file(const char *fn)
1825
1848
{
1826
1849
        char    chkfn[SMALLBUF];
1827
1850
        FILE    *f;
1927
1950
 
1928
1951
int main(int argc, char *argv[])  
1929
1952
{
1930
 
        int     i, cmd;
 
1953
        int     i, cmd, checking_flag = 0;
1931
1954
 
1932
1955
        cmd = 0;
1933
1956
 
1934
1957
        printf("Network UPS Tools upsmon %s\n", UPS_VERSION);
1935
1958
 
1936
 
        while ((i = getopt(argc, argv, "+Ddhic:pu:V")) != EOF) {
 
1959
        while ((i = getopt(argc, argv, "+Dhic:pu:VK")) != EOF) {
1937
1960
                switch (i) {
1938
 
                        case 'D':
1939
 
                        case 'd':
1940
 
                                debuglevel++;
1941
 
                                break;
1942
 
                        case 'h':
1943
 
                                help(argv[0]);
1944
 
                                break;
1945
 
                        case 'i':
1946
 
                                playdead = 1;
1947
 
                                break;
1948
 
                        case 'p':
1949
 
                                use_pipe = 0;
1950
 
                                break;
1951
1961
                        case 'c':
1952
1962
                                if (!strncmp(optarg, "fsd", strlen(optarg)))
1953
1963
                                        cmd = SIGCMD_FSD;
1960
1970
                                if (cmd == 0)
1961
1971
                                        help(argv[0]);
1962
1972
                                break;
 
1973
                        case 'D':
 
1974
                                debuglevel++;
 
1975
                                break;
 
1976
                        case 'h':
 
1977
                                help(argv[0]);
 
1978
                                break;
 
1979
 
 
1980
                        case 'K':
 
1981
                                checking_flag = 1;
 
1982
                                break;
 
1983
 
 
1984
                        case 'p':
 
1985
                                use_pipe = 0;
 
1986
                                break;
1963
1987
                        case 'u':
1964
1988
                                run_as_user = xstrdup(optarg);
1965
1989
                                break;
1966
1990
                        case 'V':
1967
1991
                                /* just show the banner */
1968
 
                                exit(0);
 
1992
                                exit(EXIT_SUCCESS);
1969
1993
                        default:
1970
1994
                                help(argv[0]);
1971
1995
                                break;
1974
1998
 
1975
1999
        if (cmd) {
1976
2000
                sendsignal("upsmon", cmd);
1977
 
                exit(0);
 
2001
                exit(EXIT_SUCCESS);
1978
2002
        }
1979
2003
 
1980
2004
        argc -= optind;
1985
2009
        initnotify();
1986
2010
        loadconfig();
1987
2011
 
 
2012
        if (checking_flag)
 
2013
                exit(check_pdflag());
 
2014
 
1988
2015
        if (shutdowncmd == NULL)
1989
2016
                printf("Warning: no shutdown command defined!\n");
1990
2017
 
1991
2018
        /* we may need to get rid of a flag from a previous shutdown */
1992
2019
        if (powerdownflag != NULL)
1993
 
                clearpdf();
 
2020
                clear_pdflag();
1994
2021
 
1995
2022
        if (totalpv < minsupplies) {
1996
2023
                printf("\nFatal error: insufficient power configured!\n\n");
1999
2026
                printf("Minimum value (MINSUPPLIES): %d\n", minsupplies);
2000
2027
 
2001
2028
                printf("\nEdit your upsmon.conf and change the values.\n");
2002
 
                exit(1);
 
2029
                exit(EXIT_FAILURE);
2003
2030
        }
2004
2031
 
2005
2032
        if (debuglevel < 1)
2022
2049
        closelog();
2023
2050
        openlog("upsmon", LOG_PID, LOG_FACILITY);
2024
2051
 
2025
 
        for (;;) {
 
2052
        while (exit_flag == 0) {
2026
2053
                utype   *ups;
2027
2054
 
2028
2055
                /* check flags from signal handlers */
2029
2056
                if (userfsd)
2030
2057
                        forceshutdown();
2031
2058
 
2032
 
                if (exit_flag)
2033
 
                        upsmon_exit();
2034
 
 
2035
2059
                if (reload_flag)
2036
2060
                        reload_conf();
2037
2061
 
2051
2075
                sleep(sleepval);
2052
2076
        }
2053
2077
 
2054
 
        /* NOTREACHED */
 
2078
        upslogx(LOG_INFO, "Signal %d: exiting", exit_flag);
 
2079
        upsmon_cleanup();
 
2080
 
 
2081
        exit(EXIT_SUCCESS);
2055
2082
}