Seconda parte della risistemazione delle dichiarazioni di inclusione.
[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
35 #include "Gapil.h"
36 #include "macros.h"
37
38 /* Help printing routine */
39 void usage(void);
40 /* computation function for DirScan */
41 int ComputeValues(struct dirent * direntry);
42 void HandSIGTERM(int signo);
43
44 /* global variables for shared memory segment */
45 struct DirProp {
46     int tot_size;    
47     int tot_files;   
48     int tot_regular; 
49     int tot_fifo;    
50     int tot_link;    
51     int tot_dir;     
52     int tot_block;   
53     int tot_char;    
54     int tot_sock;
55 } *shmptr;
56 key_t key;
57 int mutex;
58
59 int main(int argc, char *argv[]) 
60 {
61     int i, pause = 10;
62     /*
63      * Input section: decode command line parameters 
64      * Use getopt function
65      */
66     opterr = 0;  /* don't want writing to stderr */
67     while ( (i = getopt(argc, argv, "hp:")) != -1) {
68         switch (i) {
69         /* 
70          * Handling options 
71          */ 
72         case 'p':                                     /* set pause (in sec.) */
73             pause = strtol(optarg, NULL, 10);
74             break;
75         case 'h':                                             /* help option */
76             printf("Wrong -h option use\n");
77             usage();
78             return -1;
79             break;
80         case '?':                                    /* unrecognized options */
81             printf("Unrecognized options -%c\n",optopt);
82             usage();
83         default:                                       /* should not reached */
84             usage();
85         }
86     }
87     /* ***********************************************************
88      * 
89      *           Options processing completed
90      *
91      *                Main code beginning
92      * 
93      * ***********************************************************/
94     if ((argc - optind) != 1) {          /* There must be remaing parameters */
95         printf("Wrong number of arguments %d\n", argc - optind);
96         usage();
97     }
98     if (chdir(argv[1])) {                      /* chdir to be sure dir exist */
99         perror("Cannot find directory to monitor");
100         exit(1);
101     }
102     Signal(SIGTERM, HandSIGTERM);            /* set handlers for termination */
103     Signal(SIGINT, HandSIGTERM);
104     Signal(SIGQUIT, HandSIGTERM);
105     key = ftok("~/gapil/sources/DirMonitor.c", 1);  /* define a key, use dir */
106     shmptr = ShmCreate(key, 4096, 0666, 0);   /* get a shared memory segment */
107     if (!shmptr) {
108         perror("Cannot create shared memory");
109         exit(1);
110     }
111     if ((mutex = MutexCreate(key)) == -1) {                   /* get a Mutex */
112         perror("Cannot create mutex");
113         exit(1);
114     }
115     /* main loop, monitor directory properties each 10 sec */
116     daemon(1, 0);              /* demonize process, staying in monitored dir */
117     while (1) {
118         MutexLock(mutex);                              /* lock shared memory */
119         memset(shmptr, 0, sizeof(struct DirProp));    /* erase previous data */
120         DirScan(argv[1], ComputeValues);                     /* execute scan */
121         MutexUnlock(mutex);                          /* unlock shared memory */
122         sleep(pause);                              /* sleep until next watch */
123     }
124 }
125 /*
126  * Routine to compute directory properties inside DirScan
127  */
128 int ComputeValues(struct dirent * direntry) 
129 {
130     struct stat data;
131     stat(direntry->d_name, &data);                          /* get stat data */
132     shmptr->tot_size += data.st_size;
133     shmptr->tot_files++;
134     if (S_ISREG(data.st_mode)) shmptr->tot_regular++;
135     if (S_ISFIFO(data.st_mode)) shmptr->tot_fifo++;
136     if (S_ISLNK(data.st_mode)) shmptr->tot_link++;
137     if (S_ISDIR(data.st_mode)) shmptr->tot_dir++;
138     if (S_ISBLK(data.st_mode)) shmptr->tot_block++;
139     if (S_ISCHR(data.st_mode)) shmptr->tot_char++;
140     if (S_ISSOCK(data.st_mode)) shmptr->tot_sock++;
141     return 0;
142 }
143 /*
144  * routine to print usage info and exit
145  */
146 void usage(void) {
147     printf("Program myls: list file in a directory \n");
148     printf("Usage:\n");
149     printf("  DirMonitor [-h] [-p sec] dirname \n");
150     printf("  -h           print this help\n");
151     printf("  -p sec       set watch interval to sec seconds \n");
152     exit(1);
153 }
154 /*
155  * Signal Handler to manage termination
156  */
157 void HandSIGTERM(int signo) {
158     MutexLock(mutex);
159     debug("Terminated by %s\n", strsignal(signo));
160     ShmRemove(key, shmptr);
161     MutexRemove(mutex);
162     exit(0);
163 }