Download The UPS Howto

Transcript
The UPS Howto
/* Start syslog. */
openlog("powerd", LOG_CONS|LOG_PERROR, LOG_DAEMON);
/* Open monitor device. */
if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
syslog(LOG_ERR, "%s: %s", argv[1], sys_errlist[errno]);
closelog();
exit(1);
}
/* Line is opened, so DTR is high. Force it anyway to be sure. */
ioctl(fd, TIOCMBIS, &dtr_bit);
/* Daemonize. */
switch(fork()) {
case 0: /* Child */
closelog();
setsid();
break;
case −1: /* Error */
syslog(LOG_ERR, "can't fork.");
closelog();
exit(1);
default: /* Parent */
closelog();
exit(0);
}
/* Restart syslog. */
openlog("powerd", LOG_CONS, LOG_DAEMON);
/* Now sample the DCD line. */
while(1) {
/* Get the status. */
ioctl(fd, TIOCMGET, &flags);
/* Check the connection: DSR should be high. */
tries = 0;
while((flags & TIOCM_DSR) == 0) {
/* Keep on trying, and warn every two minutes. */
if ((tries % DSR_TRIES) == 0)
syslog(LOG_ALERT, "UPS connection error");
sleep(DSR_SLEEP);
tries++;
ioctl(fd, TIOCMGET, &flags);
}
if (tries > 0)
syslog(LOG_ALERT, "UPS connection OK");
/* Calculate present status. */
DCD = flags & TIOCM_CAR;
CTS = flags & TIOCM_CTS;
if (status == −1)
{
status = (DCD != 0) ? 0 : 1;
if (DCD == 0)
{
syslog(LOG_ALERT, "Power Failure. UPS active.");
powerfail(1);
}
}
Feedback
31