1

Is to possible to use libevent to monitor a file removal/update?

Assuming that /var/log/file.1 is removed I want to report it in my program. Is this possible? If yes, how?

My operating system is Linux and I want to create a demon that fires messages during some operating system actions.

Where can I find more information?

cateof
  • 6,608
  • 25
  • 79
  • 153
  • You can look the source of tail (-f and -F) which are able to tell if a file was truncated. IIRC, they use `select(2)` – Aif Oct 05 '11 at 12:04
  • accepted the inotify solution, looks very close to what I am looking for – cateof Oct 05 '11 at 12:25

2 Answers2

1

If your code is particularly targeted for Linux, you can make use of the inotify(7) facility instead. It has a very simple programming interface and is meant to monitor file changes and deletions, while the purpose of libevent is slightly different: to watch over a group of descriptors and be notified whenever any of them is ready for reading/writing.

I am not sure if libevent wraps inotify at all, because similar facilities are not provided by all other OS'es libevent supports.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
0

just using some simple logic you can check whether this file is deleted or not ?

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

struct stat st = {0};

if (stat("/var/log/file.1", &st) == -1) {
    printf("file is deleted or not present");
}

you can also check more property of file by checking another element of struct stat

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222