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
|
/*******************************************************************/
/* bpowerd v1.2 : power daemon for Best Patriot under Linux */
/* Author : Christopher Craig <ccraig@cc.gatech.edu> */
/* Copyright : This program released under the GNU Public */
/* License */
/*******************************************************************/
#include <stdio.h>
#include <strings.h>
#include <ctype.h>
#include <signal.h>
#include <syslog.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <termio.h>
#include "config.h"
#ifdef SysV /* setup initctl defaults for SysV systems */
#include "initreq.h"
#endif
#define LINE_OK 0
#define POWER_FAIL 1
#define LOW_BATT 2
struct termio ttyparams, init_ttyparams; /* parameters of port */
char *device; /* name of device */
/* fd and state are made global so that they can be used by exit_proc */
/* during a shutdown */
int fd; /* file descriptor of device (if open) */
int state = LINE_OK; /* state of power */
void start_daemon(void);
void usage(void);
int check_state();
void change_state();
void exit_proc(int signum);
void killinverter(void);
#ifndef SysV
void init_state(int state);
#endif
int main (int argc, char *argv[])
{
int i=1; /* index for argument count */
int killinv=0; /* boolean to determine if -k was given */
/* input and parse arguments */
if (argc<2) {
usage();
exit(1);
} else if (!strcmp(argv[i], "-k")) {
if (argc!=3) {
usage();
exit(1);
} else {
i++;
killinv=1;
}
} else if (argc!=2) {
usage();
exit(1);
}
device = argv[i]; /* set device */
/* make port settings */
if((fd=open(device, O_RDWR))<0) {
printf("bpowerd: error opening device (%s), quitting...\n", device);
exit(1);
}
ioctl(fd, TCFLSH, 2); /* flush I/O queues */
ioctl(fd, TCSBRK, 1); /* wait for queues */
ioctl(fd, TCGETA, &init_ttyparams); /* get initial settings */
signal(SIGTERM, exit_proc); /* set up signal handler for exit */
signal(SIGINT, exit_proc); /* set up signal handler for shutdown */
ioctl(fd, TCGETA, &ttyparams); /* get port settings for program */
ttyparams.c_cflag = (B1200|CS8|CREAD|HUPCL); /* set control modes */
ttyparams.c_iflag = 0; /* set input modes */
ttyparams.c_oflag = 0; /* set output modes */
ttyparams.c_lflag = 0; /* set local modes */
ttyparams.c_line = 0; /* set line discipline */
ttyparams.c_cc[VMIN] = 1; /* set min chars to return */
ttyparams.c_cc[VTIME] = 0; /* no port timer */
close(fd); /* close line */
/* kill inverter if -k was given */
if (killinv) killinverter();
/* send start message */
openlog("bpowerd", LOG_CONS|LOG_PID, LOG_DAEMON); /* open syslog */
syslog(LOG_INFO, "monitoring UPS on %s\n", device);
#ifndef DEBUG
start_daemon(); /* spawn daemon only if not debugging */
#endif
check_state(); /* initialize ups state */
#ifndef SysV
init_state(); /* initialize system state */
#endif
for(;;) {
if (check_state()) change_state();
sleep(WAIT);
}
return(1);
}
/*******************************************************************/
/* start_daemon */
/* spawns daemon and kills parent */
/* No arguments, no return value, no variable changed */
/*******************************************************************/
void start_daemon(void)
{
pid_t pid;
if ((pid=fork())<0) {
puts("Error in fork.\n");
exit(1);
} else if (pid!=0) {
closelog();
exit(0);
}
setsid();
chdir("/");
umask(0);
return;
}
/*******************************************************************/
/* usage */
/* print message stating the correct usage for the program */
/* No args, no return, no changes */
/*******************************************************************/
void usage(void)
{
puts("Usage: bpowerd [-k] device");
puts(" -k - sends the kill signal to the inverter and exits, does not run daemon.");
puts(" device - port device the UPS is connected to. Must be disabled for logins.");
}
/*******************************************************************/
/* check_state */
/* checks the state of the power */
/* */
/* Side-effects : state : state of power */
/* LINE_OK - Power OK */
/* POWER_FAIL - Power down, battery OK */
/* LOW_BATT - battery in alarm state, */
/* power down */
/* returns : 1 if state changed else 0 */
/*******************************************************************/
int check_state()
{
int prevstate = state; /* state at opening */
int line = TIOCM_RTS; /* Line for alarm state */
int flags; /* flags from port */
int alarm = 0; /* Alarms Present? */
int powerfail = 0; /* Power failed? */
char input = ' '; /* input from UPS */
/* Check for alarm */
fd = open(device, O_RDWR|O_NDELAY); /* open line */
ioctl(fd, TCSETA, &ttyparams); /* set params */
ioctl(fd, TIOCMBIS, &line); /* set line high */
ioctl(fd, TIOCMGET, &flags); /* check line */
ioctl(fd, TCSETA, &init_ttyparams); /* reset line */
close(fd); /* close line */
alarm = !( TIOCM_CAR & flags); /* check for alarm */
#ifdef DEBUG
printf("Alarm is %s\n", (alarm) ? "present" : "absent");
#endif
if (!alarm) {
fd = open(device, O_RDWR|O_NDELAY);
ioctl(fd, TCSETA, &ttyparams);
write(fd, "t", 1); /* write output to line */
sleep(1);
read(fd, &input, 1); /* read loop */
ioctl(fd, TCSETA, &init_ttyparams);
close(fd);
powerfail = (input != 't');
#ifdef DEBUG
printf("Power is %s\n", (powerfail) ? "off" : "on");
#endif
}
return ((state = (alarm) ? LOW_BATT :
((powerfail) ? POWER_FAIL : LINE_OK)) != prevstate);
}
#ifndef SysV
/*******************************************************************/
/* init_state */
/* reset the files to represent the current state (BSD only) */
/* */
/* Side-effects: state : state of power */
/* LINE_OK : power on */
/* CABLE : possible problem with cable */
/*******************************************************************/
void init_state()
{
int pwrstat, upsstat;
unlink(PWRSTAT);
unlink(UPSSTAT);
pwrstat = open(PWRSTAT, O_CREAT|O_WRONLY, 0644);
upsstat = open(UPSSTAT, O_CREAT|O_WRONLY, 0644);
if (state) {
write(pwrstat, "FAIL\n", 5);
write(upsstat, "CABLE\n", 6);
} else {
write(pwrstat, "OK\n", 3);
write(upsstat, "OK\n", 3);
}
close(pwrstat);
close(upsstat);
if (state) kill(1, SIGPWR); /* signal init if bad cable */
}
#endif
/*******************************************************************/
/* change_state */
/* changes the system state and calls init */
/* */
/* Uses: state : state of power, see check_state for details */
/* returns : nothing */
/*******************************************************************/
void change_state()
{
#ifdef SysV
/* SysV initctl interface (i.e. everything in this ifdef)
written by Mitch Blevins <mblevin@debian.org> */
struct init_request req;
int devfd;
/* Better safe than sorry */
memset((void*) &req, '0', sizeof(req));
req.magic = INIT_MAGIC;
/* write UPS status to status file */
switch(state) {
case LINE_OK: /* Power OK */
syslog(LOG_ALERT, "Power returned.\n");
req.cmd = INIT_CMD_POWEROK;
break;
case POWER_FAIL: /* Line Fail */
syslog(LOG_ALERT, "Power Fail, system on battery power.\n");
req.cmd = INIT_CMD_POWERFAIL;
break;
case LOW_BATT: /* Line Fail and Low Batt */
syslog(LOG_CRIT, "Battery Low, please log off immediately.\n");
req.cmd = INIT_CMD_POWERFAILNOW;
break;
default:
syslog(LOG_CRIT, "Unknown error in UPS program.\n");
return;
break;
}
/* Open control pipe for writing */
if((devfd = open(INIT_FIFO, O_WRONLY)) < 0) {
syslog(LOG_CRIT, "Could not open init control pipe.\n");
return;
}
/* Write request to control pipe */
if(write(devfd, &req, sizeof(req)) < sizeof(req)) {
syslog(LOG_CRIT, "Error writing to init control pipe.\n");
}
close(devfd);
#else /* use old BSD style /etc status files */
int fdstat;
/* write power status to status file for init */
unlink(PWRSTAT);
if ((fdstat = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
if (state)
write(fdstat, "FAIL\n", 5);
else
write(fdstat, "OK\n", 3);
close(fdstat);
}
/* write UPS status to status file */
unlink(UPSSTAT);
if ((fdstat = open(UPSSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
switch(state) {
case LINE_OK: /* Power OK */
syslog(LOG_ALERT, "Power returned.\n");
write(fdstat, "OK\n", 3);
break;
case POWER_FAIL: /* Line Fail */
syslog(LOG_ALERT, "Power Fail, system on battery power.\n");
write(fdstat, "FAIL\n", 5);
break;
case LOW_BATT: /* Line Fail and Low Batt */
syslog(LOG_CRIT, "Battery Low, please log of immediately.\n");
write(fdstat, "SCRAM\n", 6);
break;
default:
syslog(LOG_CRIT, "Error in UPS program.\n");
write(fdstat, "ERROR\n", 6);
}
close(fdstat);
}
kill(1, SIGPWR); /* signal init */
#endif
}
/*******************************************************************/
/* exit_proc */
/* exits to program cleanly, no arguments does not return */
/*******************************************************************/
void exit_proc(int signum)
{
fd=open(device,O_NDELAY | O_RDWR);
ioctl(fd,TCSETA,&init_ttyparams); /* reset port */
close(fd);
if(signum==SIGINT){
/* this was called to shutdown instead of just quit */
if (state == POWER_FAIL) {
killinverter();
}
}
syslog(LOG_INFO, "Process Terminated.\n");
closelog(); /* close syslog */
exit(2);
}
/*******************************************************************/
/* killinverter */
/* sends shutdown signal to UPS and then quits */
/* No arguments, does not return */
/*******************************************************************/
void killinverter(void)
{
int line = TIOCM_DTR;
#ifdef DEBUG
printf("In shutdown, signaling UPS.\n");
#endif
fd=open(device, O_RDWR|O_NDELAY);
ioctl(fd, TCSETA, &ttyparams);
ioctl(fd, TIOCMBIS, &line);
sleep(6);
ioctl(fd, TCSETA, &init_ttyparams);
close(fd);
syslog(LOG_CRIT,
"UPS signaled for shutdown, system power off in 2 minutes.\n");
closelog();
exit(0);
}
|