On Tue, 22 Sep 1998, James wrote:

->how do i write a daemon? I know what they are (little programs that do something
->useful in the background), but are they just normal programs that get pushed
->into the background?
->

Take a look at the c-programming FAQ available under
ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/comp/lang/c

->and how do i monitor a /dev/TTYS? device so that when my modem picks up
->the line and connects to a remote computer my daemon does something, then
->when the modem puts down the line the daemon does something else?
->

U need to watch for the DCD line. Glynn Clements proposed once using the
TIOCMGET ioctl call and I find this quite useful. Something like this:



#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <syslog.h>


#define SLEEP_INTERVAL  5
#define DCD_HIGH        1
#define DCD_LOW         0

extern char* program_invocation_short_name;

int TestDCD(int nDevice)
{
  int nStatus;
  
  if (ioctl(nDevice,TIOCMGET,&nStatus) == -1)
    {
      // oops something went wrong...

      syslog(LOG_ERR,"ioctl error!\n");
      exit(1);
    }
  else
    return nStatus & TIOCM_CD;   
}


int main(int argc,char** argv)
{
  int fd;
  
  openlog(program_invocation_short_name,0,LOG_DAEMON);  
  
  if (-1 == (fd = open( /*PUT THE TTYS DEVICE HERE*/ ,O_RDWR)))
    {
      syslog(LOG_ERR,"Canot open device\n");
      exit(1);
    };

  while ( DCD_LOW == TestDCD(fd) )
    sleep(SLEEP_INTERVAL);

  /* here u have the DCD high i.e. modem connected */
  /* do some stuff*/

  while ( DCD_HIGH == TestDCD(fd) )
    sleep(SLEEP_INTERVAL);

  /* here the modems are already disconnected */
  /* do other  stuff */

    
  closelog();
        
  return 0;
}


U may consider installing SIGALARM handler.

Regards,

        Marin




       "Knowledge is not a crime. Some of its applications are..."

                                                         - Unknown hacker



Reply via email to