Created
March 16, 2016 23:23
-
-
Save arundurvasula/53f6d430152a175e348e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <sys/types.h> | |
#include <sys/inotify.h> | |
#define EVENT_SIZE ( sizeof (struct inotify_event ) ) | |
#define BUF_LEN ( 1024 * (EVENT_SIZE + 16 ) ) | |
int main( int argc, char **argv) | |
{ | |
int length, i= 0; | |
int fd; | |
int wd; | |
char buffer[BUF_LEN]; | |
FILE *output; | |
char path[4096]; | |
fd = inotify_init(); | |
if ( fd < 0 ) { | |
perror( "inotify_init" ); | |
} | |
wd = inotify_add_watch( fd, "/home/adurvasu",IN_MODIFY | IN_CREATE | IN_DELETE | IN_OPEN); | |
length = read( fd, buffer, BUF_LEN ); | |
if ( length < 0 ) { | |
perror("read"); | |
} | |
while ( i < length ) { | |
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ]; | |
if ( event->len ) { | |
if (event->mask & IN_CREATE ) { | |
if (event->mask & IN_ISDIR ) { | |
printf("The directory %s was created.\n", event->name ); | |
} | |
else { | |
printf("The file %s was created.\n", event->name ); | |
} | |
} | |
else if ( event->mask & IN_DELETE ) { | |
if (event->mask & IN_ISDIR ) { | |
printf("The directory %s was deleted.\n", event->name ); | |
} | |
else { | |
printf("The file %s was deleted.\n", event->name ); | |
} | |
} | |
else if (event->mask & IN_OPEN) { | |
if (event->mask & IN_ISDIR ) { | |
printf("The directory %s was modified.\n", event->name); | |
} | |
else { | |
//printf("The file %s was modified.\n", event->name ); | |
output = popen ("cat /proc/$(lsof -t /home/adurvasu/a.txt)/cmdline", "r"); | |
if (output == NULL) { | |
printf("Failed to run command\n" ); | |
exit(1); | |
} | |
while (fgets(path, sizeof(path)-1, output) != NULL) { | |
printf("Command: %s \n", path); | |
} | |
pclose(output); | |
} | |
} | |
} | |
i += EVENT_SIZE + event->len; | |
} | |
( void ) inotify_rm_watch( fd, wd); | |
( void ) close(fd); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment