#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
static fd = 0;
help()
{
printf("0xEaa13`s UPS Daemon Version 1.0\n\
Usage: ./ups-daemon device-name\n\
\nWritten by 0xEaa13\n\
Host:http://eaa13.dns0755.net\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;
}
}
}
}
本功能正在开发中,目前不能使用,敬请原谅。
√ 期刊在线投稿: /journal/contribute.html
√ 本文打印于《CNFUG期刊》,欢迎访问 http://www.cnfug.net 获取更多技术文章。
© 2003-2006 CNFUG(China FreeBSD User Group) All rights reserved.
Powered by FreeBSD
看不到图了?