Frage

I am trying to use inotify for monitoring changes to a file /dev/hvc0 using the following:

#include <stdio.h>
#include <sys/inotify.h>
#include <stdlib.h>

#define EVENT_SIZE  (sizeof(struct inotify_event))
#define BUF_LEN     (1024 * (EVENT_SIZE + 16))
#define WATCH_FILE "/dev/hvc0" /* This file should be present
                                  before this program is run */

int main() {
    int notify_fd;
    int length;
    int i = 0;
    char buffer[BUF_LEN];
    notify_fd = inotify_init();
    if (notify_fd < 0) {
        perror("inotify_init");
    }
    int wd = inotify_add_watch(notify_fd, WATCH_FILE, IN_MODIFY | IN_ACCESS);
    int length_read = read(notify_fd, buffer, BUF_LEN);
    if (length_read) {
        perror("read");
    }
    while (i < length_read) {
        struct inotify_event *event =
            (struct inotify_event *) &buffer[i];
        if (event->len) {
            if (event->mask & IN_ACCESS) {
                printf("The file %s was accessed.\n", event->name);
            } else if (event->mask & IN_MODIFY) {
                printf("The file %s was modified.\n", event->name);
            }
        }
    }

    (void) inotify_rm_watch(notify_fd, wd);
    (void) close(notify_fd);
    return 0;
}

However, this does not print if the file was accessed/modified. However whenever I change the path to be monitored to a directory and a file was changed, it prints the correct event occurred.

Does inotify work for monitoring file changes too? Or am I doing something wrong?

Thanks!

War es hilfreich?

Lösung 2

I am not understanding why the event->len is return zero may be because the file name is not returned. but for only testing purpose you just comment it. And in second point Your handling of i is broken, you never reset it to 0 in the loop. This causes any later inotify events to be considered only when they are longer than the longest event before them, which is not likely what you want.And please in while loop please put i += EVENT_SIZE + event->len;

int main() {
    int notify_fd;
    int length;
    int i = 0;
    char buffer[BUF_LEN];
    notify_fd = inotify_init();
    if (notify_fd < 0) {
        perror("inotify_init");
    }
    int wd = inotify_add_watch(notify_fd, WATCH_FILE, IN_MODIFY | IN_ACCESS);
    while(1)
    {
        length = read( notify_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_ACCESS) {
                printf("The file %s was accessed.\n", event->name);
            } else if (event->mask & IN_MODIFY) {
                printf("The file %s was modified.\n", event->name);
            }
            // }
            i += EVENT_SIZE + event->len;
        }
    }

  ( void ) inotify_rm_watch( notify_fd, wd );
  ( void ) close( notify_fd);

  exit( 0 );
}

Andere Tipps

You are missing to increase i . Add this before the end of the loop:

i += EVENT_SIZE + event->len;

If you want to monitor the changes you also need to wrap the read / print operation in an endless loop.

Per this answer, it appears that Linux does not watch on specific files. If you were to listen to specific files, you should listen to its parent directory first.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top