summaryrefslogtreecommitdiffstats
path: root/sanity/dev_sanity/inotify.c
blob: 5e2311ff2df3ed6f7d05bbdb58ca9742c1152fc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/inotify.h>
/* This will watch for any new arrivals at TARGET directory and if found,it will invoke the SANITY_TEST script. */

#define TARGET "/sanity/test/incoming"
#define SANITY_TEST "/opt/qa/tools/dev_sanity/sanity_test.sh "

void get_event (int fd, const char * target);


/* ----------------------------------------------------------------- */

int main (int argc, char *argv[])
{
   char target[FILENAME_MAX];
   int result;
   int fd;
   int wd;   /* watch descriptor */

   if (argc < 2) {
      strcpy (target,TARGET);
	
   }
   else {
      fprintf (stderr, "Watching %s\n", TARGET);
      strcpy (target, TARGET);
   }

   fd = inotify_init();
   if (fd < 0) {
      fprintf (stderr, "Error: %s\n", strerror(errno));
      return 1;
   }
   
   wd = inotify_add_watch (fd, target, IN_ALL_EVENTS);
   if (wd < 0) {
      fprintf (stderr, "Error: %s\n", strerror(errno));
      return 1;
   }
   
   while (1) {
      get_event(fd, target);
   }

   return 0;
}

/* ----------------------------------------------------------------- */
/* Allow for 1024 simultanious events */
#define BUFF_SIZE ((sizeof(struct inotify_event)+FILENAME_MAX)*1024)

void get_event (int fd, const char * target)
{
   ssize_t len, i = 0;
   int status;
   char action[81+FILENAME_MAX] = {0};
   char buff[BUFF_SIZE] = {0};

   len = read (fd, buff, BUFF_SIZE);
   
   while (i < len) {
      struct inotify_event *pevent = (struct inotify_event *)&buff[i];
      char action[81+FILENAME_MAX] = {0};

      if (pevent->len) 
         strcpy (action, pevent->name);
      else
         strcpy (action, target);
    
      if (pevent->mask & IN_CLOSE_WRITE){ 
         strcat(action, " opened for writing was closed");
	/*invoke the script to process newly arrived tar file*/
	if (fork()==0){//child process.
	printf("Child running...");
	if(fork()==0){//grandchild
	system(SANITY_TEST);
	}else
	exit(0);
//	waitpid(-1, &status, 0);
	}
	}
//      waitpid(-1,&status,0);
      printf ("%s\n", action);
      i += sizeof(struct inotify_event) + pevent->len;

   }

}  /* get_event */