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