[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Sheflug] Running programs on file creation



>>>>> "Graham" == Graham Cox <ACA00GAC [at] sheffield.ac.uk> writes:

Graham> I've looked at it, and it isn't quite what I want. The problem
Graham> is that we've only got one phone line, and my modem is quite
Graham> stubborn about such things as detecting dial tones(It dials if
Graham> someone's using the phone :( ). Also, because of some of the
Graham> programs that are run on the network(e.g. MSN Messenger) it's
Graham> going to be trying to connect to the net the whole time(If
Graham> I've read things correctly that is :) -- Graham

Ah.  Well, the canonical solution then would be to run a daemon that
programs register themselves (either by opening a net connection to
it, or writing to a fifo or sending it a signal or some other IPC
method) and keeps count of the number of programs with requests for
the net, and brings it up/down as needed.  Creating files in a
directory isn't the best sort of mechanism under Unix for
inter-process signalling...

A simple (untested, I haven't even tried to compile it :) program
that does this using a fifo is attached.  Once it's running just do an
"echo -n u >> /var/connect/netd" to do the equivalent of adding a
file, and echo -n d to release the network.

  -Eric
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/poll.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
  int fd;
  char x;
  int locks = 0;
  int up = 0;
  struct pollfd poll_array[1];

  if (mkfifo ("/var/connect/netd", 0777))
    {
      if (errno != EEXIST)
        {
          fprintf(stderr, "Something bad happened\n");
        }
    }
  fd = open("/var/connect/netd", O_RDONLY);
  while(1)
    {
      read (fd,&x,1);
      if (x == 'u')
        {
          locks++;
        }
      else if (x == 'd')
        {
          locks--;
        }
      if (locks < 0)
        {
          syslog( LOG_WARNING, "More downs than ups.  How distressing");
          locks = 0;
        }
      else
        {
          if (locks > 0 && !up)
            {
              system ("/usr/bin/pon");
            }
          else if (up)
            {
              system ("/usr/bin/poff");
            }
        }
    }
}