3 * Copyright (C) 2002 Simone Piccardi
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.
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.
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.
19 /*****************************************************************************
23 * An example for shared memory use: monitor a directory status,
24 * saving data in a shared memory segment
26 * Author: S. Piccardi Jan. 2003
28 * $Id: DirMonitor.c,v 1.4 2003/01/10 09:28:49 piccardi Exp $
30 *****************************************************************************/
31 #include <sys/types.h>
33 #include <dirent.h> /* directory */
34 #include <stdlib.h> /* C standard library */
40 /* Help printing routine */
42 /* computation function for DirScan */
43 int ComputeValues(struct dirent * direntry);
44 void HandSIGTERM(int signo);
46 /* global variables for shared memory segment */
58 struct DirProp *shmptr;
62 int main(int argc, char *argv[])
67 * Input section: decode command line parameters
70 opterr = 0; /* don't want writing to stderr */
71 while ( (i = getopt(argc, argv, "hp:")) != -1) {
76 case 'p': /* set pause (in sec.) */
77 pause = strtol(optarg, NULL, 10);
79 case 'h': /* help option */
80 printf("Wrong -h option use\n");
84 case '?': /* unrecognized options */
85 printf("Unrecognized options -%c\n",optopt);
87 default: /* should not reached */
91 /* ***********************************************************
93 * Options processing completed
97 * ***********************************************************/
98 if ((argc - optind) != 1) { /* There must be remaing parameters */
99 printf("Wrong number of arguments %d\n", argc - optind);
102 Signal(SIGTERM, HandSIGTERM); /* set handlers for termination */
103 Signal(SIGINT, HandSIGTERM);
104 Signal(SIGQUIT, HandSIGTERM);
105 /* create needed IPC objects */
106 key = ftok("./DirMonitor.c", 1); /* define a key */
107 shmid = shmget(key, 4096, IPC_CREAT|0666); /* get a shared memory */
109 perror("Cannot create shared memory");
112 if ( (shmptr = shmat(shmid, NULL, 0)) == NULL ) { /* attach to process */
113 perror("Cannot attach segment");
115 if ((mutex = MutexCreate(key)) == -1) { /* get a Mutex */
116 perror("Cannot create mutex");
119 /* main loop, monitor directory properties each 10 sec */
121 MutexLock(mutex); /* lock shared memory */
122 memset(shmptr, 0, sizeof(struct DirProp)); /* erase previous data */
123 DirScan(argv[1], ComputeValues); /* execute scan */
124 MutexUnlock(mutex); /* unlock shared memory */
125 sleep(pause); /* sleep until next watch */
129 * Routine to compute directory properties inside DirScan
131 int ComputeValues(struct dirent * direntry)
134 stat(direntry->d_name, &data); /* get stat data */
135 shmptr->tot_size += data.st_size;
137 if (S_ISREG(data.st_mode)) shmptr->tot_regular++;
138 if (S_ISFIFO(data.st_mode)) shmptr->tot_fifo++;
139 if (S_ISLNK(data.st_mode)) shmptr->tot_link++;
140 if (S_ISDIR(data.st_mode)) shmptr->tot_dir++;
141 if (S_ISBLK(data.st_mode)) shmptr->tot_block++;
142 if (S_ISCHR(data.st_mode)) shmptr->tot_char++;
143 if (S_ISSOCK(data.st_mode)) shmptr->tot_sock++;
147 * routine to print usage info and exit
150 printf("Program myls: list file in a directory \n");
152 printf(" DirMonitor [-h] [-p sec] dirname \n");
153 printf(" -h print this help\n");
154 printf(" -p sec set watch interval to sec seconds \n");
158 * Signal Handler to manage termination
160 void HandSIGTERM(int signo) {
162 debug("Terminated by %s\n", strsignal(signo));
164 perror("Error detaching shared memory");
167 if (shmctl(shmid, IPC_RMID, NULL)) {
168 perror("Cannot remove shared memory segment");