Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / DirMonitor.c
1 /* DirMonitor.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /*****************************************************************************
20  *
21  * File DirMonitor: 
22  *
23  * An example for shared memory use: monitor a directory status,
24  * saving data in a shared memory segment
25  *
26  * Author: S. Piccardi Jan. 2003
27  *
28  *****************************************************************************/
29 #include <sys/types.h>   /* primitive system data types */
30 #include <sys/stat.h>    /* file characteristics constants and functions */
31 #include <dirent.h>      /* directory operation constants and functions */
32 #include <stdlib.h>      /* C standard library */
33 #include <unistd.h>      /* unix standard library */
34 #include <string.h>      /* C strings library */
35
36 #include "Gapil.h"
37 #include "macros.h"
38
39 /* Help printing routine */
40 void usage(void);
41 /* computation function for dir_scan */
42 int ComputeValues(struct dirent * direntry);
43 void HandSIGTERM(int signo);
44
45 /* global variables for shared memory segment */
46 struct DirProp {
47     int tot_size;    
48     int tot_files;   
49     int tot_regular; 
50     int tot_fifo;    
51     int tot_link;    
52     int tot_dir;     
53     int tot_block;   
54     int tot_char;    
55     int tot_sock;
56 } *shmptr;
57 key_t key;
58 int mutex;
59
60 int main(int argc, char *argv[]) 
61 {
62     int i, pause = 10;
63     /*
64      * Input section: decode command line parameters 
65      * Use getopt function
66      */
67     opterr = 0;  /* don't want writing to stderr */
68     while ( (i = getopt(argc, argv, "hp:")) != -1) {
69         switch (i) {
70         /* 
71          * Handling options 
72          */ 
73         case 'p':                                     /* set pause (in sec.) */
74             pause = strtol(optarg, NULL, 10);
75             break;
76         case 'h':                                             /* help option */
77             printf("Wrong -h option use\n");
78             usage();
79             return -1;
80             break;
81         case '?':                                    /* unrecognized options */
82             printf("Unrecognized options -%c\n",optopt);
83             usage();
84         default:                                       /* should not reached */
85             usage();
86         }
87     }
88     /* ***********************************************************
89      * 
90      *           Options processing completed
91      *
92      *                Main code beginning
93      * 
94      * ***********************************************************/
95     if ((argc - optind) != 1) {          /* There must be remaing parameters */
96         printf("Wrong number of arguments %d\n", argc - optind);
97         usage();
98     }
99     if (chdir(argv[1])) {                      /* chdir to be sure dir exist */
100         perror("Cannot find directory to monitor");
101         exit(1);
102     }
103     Signal(SIGTERM, HandSIGTERM);            /* set handlers for termination */
104     Signal(SIGINT, HandSIGTERM);
105     Signal(SIGQUIT, HandSIGTERM);
106     key = ftok("~/gapil/sources/DirMonitor.c", 1);  /* define a key, use dir */
107     shmptr = ShmCreate(key, 4096, 0666, 0);   /* get a shared memory segment */
108     if (!shmptr) {
109         perror("Cannot create shared memory");
110         exit(1);
111     }
112     if ((mutex = MutexCreate(key)) == -1) {                   /* get a Mutex */
113         perror("Cannot create mutex");
114         exit(1);
115     }
116     /* main loop, monitor directory properties each 10 sec */
117     daemon(1, 0);              /* demonize process, staying in monitored dir */
118     while (1) {
119         MutexLock(mutex);                              /* lock shared memory */
120         memset(shmptr, 0, sizeof(struct DirProp));    /* erase previous data */
121         dir_scan(argv[1], ComputeValues);                    /* execute scan */
122         MutexUnlock(mutex);                          /* unlock shared memory */
123         sleep(pause);                              /* sleep until next watch */
124     }
125 }
126 /*
127  * Routine to compute directory properties inside dir_scan
128  */
129 int ComputeValues(struct dirent * direntry) 
130 {
131     struct stat data;
132     stat(direntry->d_name, &data);                          /* get stat data */
133     shmptr->tot_size += data.st_size;
134     shmptr->tot_files++;
135     if (S_ISREG(data.st_mode)) shmptr->tot_regular++;
136     if (S_ISFIFO(data.st_mode)) shmptr->tot_fifo++;
137     if (S_ISLNK(data.st_mode)) shmptr->tot_link++;
138     if (S_ISDIR(data.st_mode)) shmptr->tot_dir++;
139     if (S_ISBLK(data.st_mode)) shmptr->tot_block++;
140     if (S_ISCHR(data.st_mode)) shmptr->tot_char++;
141     if (S_ISSOCK(data.st_mode)) shmptr->tot_sock++;
142     return 0;
143 }
144 /*
145  * routine to print usage info and exit
146  */
147 void usage(void) {
148     printf("Program myls: list file in a directory \n");
149     printf("Usage:\n");
150     printf("  DirMonitor [-h] [-p sec] dirname \n");
151     printf("  -h           print this help\n");
152     printf("  -p sec       set watch interval to sec seconds \n");
153     exit(1);
154 }
155 /*
156  * Signal Handler to manage termination
157  */
158 void HandSIGTERM(int signo) {
159     MutexLock(mutex);
160     debug("Terminated by %s\n", strsignal(signo));
161     ShmRemove(key, shmptr);
162     MutexRemove(mutex);
163     exit(0);
164 }