*
* Author: S. Piccardi Jan. 2003
*
- * $Id: DirMonitor.c,v 1.2 2003/01/04 17:24:30 piccardi Exp $
- *
*****************************************************************************/
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <dirent.h> /* directory */
-#include <stdlib.h> /* C standard library */
-#include <unistd.h>
+#include <sys/types.h> /* primitive system data types */
+#include <sys/stat.h> /* file characteristics constants and functions */
+#include <dirent.h> /* directory operation constants and functions */
+#include <stdlib.h> /* C standard library */
+#include <unistd.h> /* unix standard library */
#include "Gapil.h"
#include "macros.h"
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
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 */
}
}
/*
- * Routine to print file name and size inside DirScan
+ * Routine to compute directory properties inside DirScan
*/
int ComputeValues(struct dirent * direntry)
{
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;
+ 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;
}
/*
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);
}