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