I wrote this simple daemon in order to know when i use the modem. It should
work whith every porogram such as ppp, hylafax, minicom and so on.
But with pp i have some problems. When the connection stops non syslog()
call is done and so nothing is wrote in the /var/log/messages file.
The start line instead is well recorded.
Any idea?
Simone
/******************************************************************
Modemd: trace status line of /dev/ttyS[x] and logs through syslog calls
******************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#define LINE "/dev/ttyS1"
#define Facility LOG_DAEMON
#define Level LOG_INFO
void main(int argc, char *argv[])
{
int Priority = (Facility && Level), fh, w = 0;
unsigned int status;
unsigned int ioret;
char *strip_name;
strip_name = strrchr(argv[0], '/');
if(strip_name == NULL) strip_name=argv[0];
else strip_name++;
openlog(strip_name,LOG_NDELAY,Facility);
syslog(Priority, "starting.");
switch(fork()) {
case 0: setsid();
break;
case -1: syslog(Priority, "can't fork.");
closelog();
exit(2);
default: exit(0);
}
fh = open(LINE,O_RDONLY);
if(fh == -1) {
syslog(Priority, "can't open %s.", LINE);
closelog();
exit(3);
}
for (;;) {
status = ioctl(fh, TIOCMGET, &ioret);
if(status) {
close(fh);
fh = open(LINE,O_RDONLY);
if(fh == -1) {
syslog(Priority, "can't open %s.", LINE);
closelog();
exit(2);
}
}
if (ioret & TIOCM_CAR) {
if(!w) syslog(Priority, "Carrier is HIGH (connection starts).");
w = 1;
}
else
{
if(w) syslog(Priority, "Carrier is LOW (connection stops ).");
w = 0;
}
fflush(stdout);
sleep(1);
}
--Quote:}
Simone Chemelli
http://www.dei.unipd.it/~genius