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