分类: BSD
2008-04-07 18:08:31
#include
#include
#include
#include
#include
#include
#include
#include
#include
static fd = 0;
help()
{
printf("0xEaa13`s UPS Daemon Version 1.0\n\
Usage: ./ups-daemon device-name\n\
\nWritten by 0xEaa13\n\
Host:\n\
E-MAIL: eaa13_at_bsdmail.org\n\
");
exit(0);
}
int
main(int argc, char **argv)
{
int count = 0;
int ledon;
int ledoff;
pid_t pid;
int status;
ledon |= TIOCM_DTR;
ledoff &= ~TIOCM_DTR;
if (argc != 2)
help();
/* open device */
fd = open(argv[1], O_RDWR | O_NDELAY);
if (fd < 0) {
printf("Open device %s error!\n", argv[1]);
exit(2);
}
if ((pid = fork()) < 0) {
printf("Fork error!\n");
exit(1);
} else if (pid > 0) {
printf("Start daemon OK!\nPid is:%d\n", pid);
exit(0);
}
while (1) {
ioctl(fd, TIOCMSET, &ledoff);
sleep(1);
ioctl(fd, TIOCMSET, &ledon);
sleep(1);
ioctl(fd, TIOCMGET, &status);
if (status & TIOCM_CAR) {
int i;
for (i = 0; i < 2; i++) {
sleep(60);
ioctl(fd, TIOCMGET, &status);
if (status & TIOCM_CAR) {
count++;
}
}
if (count == 2) {
system("/sbin/shutdown -p now");
} else {
count = 0;
}
}
}
}