X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FDirMonitor.c;h=8c7335233000a3e5fe0f1c3c9c816c27c908e768;hp=2f11833a800479a96f6c0bff6edbf34a03efc26d;hb=de83478a4dec2c8dd1b3721c4548b8d902555566;hpb=86daf7e40ebb0b24397b67e1a272100bcccd539a diff --git a/sources/DirMonitor.c b/sources/DirMonitor.c index 2f11833..8c73352 100644 --- a/sources/DirMonitor.c +++ b/sources/DirMonitor.c @@ -25,7 +25,7 @@ * * Author: S. Piccardi Jan. 2003 * - * $Id: DirMonitor.c,v 1.4 2003/01/10 09:28:49 piccardi Exp $ + * $Id: DirMonitor.c,v 1.6 2003/02/26 21:37:36 piccardi Exp $ * *****************************************************************************/ #include @@ -54,15 +54,13 @@ struct DirProp { int tot_block; int tot_char; int tot_sock; -}; -struct DirProp *shmptr; -int shmid; +} *shmptr; +key_t key; int mutex; int main(int argc, char *argv[]) { int i, pause = 10; - key_t key; /* * Input section: decode command line parameters * Use getopt function @@ -99,24 +97,25 @@ int main(int argc, char *argv[]) printf("Wrong number of arguments %d\n", argc - optind); usage(); } + if (chdir(argv[1])) { /* chdir to be sure dir exist */ + perror("Cannot find directory to monitor"); + exit(1); + } Signal(SIGTERM, HandSIGTERM); /* set handlers for termination */ Signal(SIGINT, HandSIGTERM); Signal(SIGQUIT, HandSIGTERM); - /* create needed IPC objects */ - key = ftok("./DirMonitor.c", 1); /* define a key */ - shmid = shmget(key, 4096, IPC_CREAT|0666); /* get a shared memory */ - if (shmid < 0) { + key = ftok("~/gapil/sources/DirMonitor.c", 1); /* define a key, use dir */ + shmptr = ShmCreate(key, 4096, 0666, 0); /* get a shared memory segment */ + if (!shmptr) { perror("Cannot create shared memory"); exit(1); } - if ( (shmptr = shmat(shmid, NULL, 0)) == NULL ) { /* attach to process */ - perror("Cannot attach segment"); - } if ((mutex = MutexCreate(key)) == -1) { /* get a Mutex */ perror("Cannot create mutex"); exit(1); } /* main loop, monitor directory properties each 10 sec */ + daemon(1, 0); /* demonize process, staying in monitored dir */ while (1) { MutexLock(mutex); /* lock shared memory */ memset(shmptr, 0, sizeof(struct DirProp)); /* erase previous data */ @@ -160,14 +159,7 @@ void usage(void) { void HandSIGTERM(int signo) { MutexLock(mutex); debug("Terminated by %s\n", strsignal(signo)); - if (shmdt(shmptr)) { - perror("Error detaching shared memory"); - exit(1); - } - if (shmctl(shmid, IPC_RMID, NULL)) { - perror("Cannot remove shared memory segment"); - exit(1); - } + ShmRemove(key, shmptr); MutexRemove(mutex); exit(0); }