Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C
int main(int argc, char *argv[]){
    int fan;
    char buf[4096];
    char fdpath[32];
    char path[PATH_MAX + 1];
    ssize_t buflen, linklen;
    struct fanotify_event_metadata *metadata;

    // Init fanotify structure
    fan = fanotify_init(FAN_REPORT_FID, O_RDWR);
    if(fan == -1){
        perror("fanotify_init");
        // exit(EXIT_FAILURE);
    }

    int ret = fanotify_mark(fan, FAN_MARK_ADD, FAN_EVENT_ON_CHILD,
        //FAN_ACCESS | FAN_OPEN | FAN_EVENT_ON_CHILD,
        AT_FDCWD, "/home/bilal/Pictures/folder/"
    );
    if(ret == -1){
        perror("fanotify_mark");
        exit(EXIT_FAILURE);
    }

    while(1){
        buflen = read(fan, buf, sizeof(buf));
        metadata = (struct fanotify_event_metadata*)&buf;

        while(FAN_EVENT_OK(metadata, buflen)){
            if (metadata->mask & FAN_Q_OVERFLOW){
                printf("Queue overflow!\n");
                continue;
            }

            // Resolve path, using automatically opened fd
            sprintf(fdpath, "/proc/self/fd/%d", metadata->fd);
            linklen = readlink(fdpath, path, sizeof(path) - 1);
            path[linklen] = '\0';
            printf("%s\n", path);

            close(metadata->fd);
            metadata = FAN_EVENT_NEXT(metadata, buflen);
        }
    }
}


What I have tried:

I searched online about the tutorials of fanotify but what I found was just the documentation and the large code examples. I am new to this filesystem monitoring API so it is a bit complicated for me. I took this code online and ran through sudo and the program started executing but when I made changes in the file, the result didn't show up.

I also debugged the code. I break the code from the main and when I came to the while loop the next command didn't execute after buflen = read(fan, buf, sizeof(buf)); Can you help me that why this is happening?
Posted
Updated 3-Feb-21 1:52am
v2

Your continue instruction causes the most recent loop to execute another iteration; it doesn't have any effect on the outer loop.

So if your if condition is ever true, your loop will never execute the call to FAN_EVENT_NEXT and so the chances are it will process the same data over and over again.

At a guess, you want to remove the continue and add an else clause - but since we can't run your code we can't tell.

As for the problem the debugger is showing you, the most likely reason is that the buffer never gets filled -and since I doubt that your fanotify_event_metadata struct is 4096 bytes wide it's probably waiting for that much data before continuing: read[^]
 
Share this answer
 
As I understood your posting the read command is hanging.

Possible reason are:
a) that isnt enough data to read your hugh buffer
b) or some missing resource to read.

My favorite is a ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900