#include #include #include #include #include #include #include #include #include int main() { int ino, watch, len, x; char path[128]; struct inotify_event *e; if ( (ino = inotify_init()) < 0) { perror(NULL); return 1; } sprintf(path, "/proc/self/fd/%d", ino); if ( (watch = inotify_add_watch(ino, path, IN_ALL_EVENTS)) < 0) { perror(NULL); return 2; } len = sizeof(struct inotify_event) + strlen(path) + 1; e = (struct inotify_event *)malloc(len); if (!e) { perror(NULL); return 3; } if ( (x = fork()) < 0) { perror(NULL); return 4; } else if (x == 0) { /* child */ chmod(path, 0); _Exit(0); } else { /* parent */ int status; ssize_t s; wait(&status); retry: s = read(ino, e, len); if (s < 0) { perror(NULL); _Exit(4); } printf("%d\n", x); goto retry; } free(e); return 0; }