X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FDirMonitor.c;h=2f11833a800479a96f6c0bff6edbf34a03efc26d;hp=c8fad95d3f079c29aee3aa52f9b57b3e9cb770d1;hb=86daf7e40ebb0b24397b67e1a272100bcccd539a;hpb=e18f3ea57b725bc148f0d3443e384bf5270b2b9f diff --git a/sources/DirMonitor.c b/sources/DirMonitor.c index c8fad95..2f11833 100644 --- a/sources/DirMonitor.c +++ b/sources/DirMonitor.c @@ -25,7 +25,7 @@ * * Author: S. Piccardi Jan. 2003 * - * $Id: DirMonitor.c,v 1.1 2003/01/03 23:16:05 piccardi Exp $ + * $Id: DirMonitor.c,v 1.4 2003/01/10 09:28:49 piccardi Exp $ * *****************************************************************************/ #include @@ -35,30 +35,48 @@ #include #include "Gapil.h" +#include "macros.h" /* Help printing routine */ void usage(void); /* computation function for DirScan */ int ComputeValues(struct dirent * direntry); +void HandSIGTERM(int signo); -/* global variable for shared memory segment */ -int shmid; /* Shared memory identifier */ +/* global variables for shared memory segment */ +struct DirProp { + int tot_size; + int tot_files; + int tot_regular; + int tot_fifo; + int tot_link; + int tot_dir; + int tot_block; + int tot_char; + int tot_sock; +}; +struct DirProp *shmptr; +int shmid; +int mutex; int main(int argc, char *argv[]) { - int i; - int mutex; + int i, pause = 10; + key_t key; /* * Input section: decode command line parameters * Use getopt function */ opterr = 0; /* don't want writing to stderr */ - while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) { + while ( (i = getopt(argc, argv, "hp:")) != -1) { switch (i) { /* * Handling options */ - case 'h': /* help option */ + case 'p': /* set pause (in sec.) */ + pause = strtol(optarg, NULL, 10); + break; + case 'h': /* help option */ printf("Wrong -h option use\n"); usage(); return -1; @@ -81,18 +99,48 @@ int main(int argc, char *argv[]) printf("Wrong number of arguments %d\n", argc - optind); usage(); } - - DirScan(argv[1], ComputeValues); - exit(0); + 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) { + 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 */ + while (1) { + MutexLock(mutex); /* lock shared memory */ + memset(shmptr, 0, sizeof(struct DirProp)); /* erase previous data */ + DirScan(argv[1], ComputeValues); /* execute scan */ + MutexUnlock(mutex); /* unlock shared memory */ + sleep(pause); /* sleep until next watch */ + } } /* - * Routine to print file name and size inside DirScan + * Routine to compute directory properties inside DirScan */ -int do_ls(struct dirent * direntry) +int ComputeValues(struct dirent * direntry) { struct stat data; stat(direntry->d_name, &data); /* get stat data */ - printf("File: %s \t size: %d\n", direntry->d_name, data.st_size); + shmptr->tot_size += data.st_size; + shmptr->tot_files++; + if (S_ISREG(data.st_mode)) shmptr->tot_regular++; + if (S_ISFIFO(data.st_mode)) shmptr->tot_fifo++; + if (S_ISLNK(data.st_mode)) shmptr->tot_link++; + if (S_ISDIR(data.st_mode)) shmptr->tot_dir++; + if (S_ISBLK(data.st_mode)) shmptr->tot_block++; + if (S_ISCHR(data.st_mode)) shmptr->tot_char++; + if (S_ISSOCK(data.st_mode)) shmptr->tot_sock++; return 0; } /* @@ -101,7 +149,25 @@ int do_ls(struct dirent * direntry) void usage(void) { printf("Program myls: list file in a directory \n"); printf("Usage:\n"); - printf(" myls [-h] dirname \n"); + printf(" DirMonitor [-h] [-p sec] dirname \n"); printf(" -h print this help\n"); + printf(" -p sec set watch interval to sec seconds \n"); exit(1); } +/* + * Signal Handler to manage termination + */ +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); + } + MutexRemove(mutex); + exit(0); +}