]>
Commit | Line | Data |
---|---|---|
0e37c169 ZJS |
1 | #include <stdio.h> |
2 | #include <string.h> | |
3 | #include <sys/inotify.h> | |
4 | ||
5 | #include <systemd/sd-event.h> | |
6 | ||
7 | #define _cleanup_(f) __attribute__((cleanup(f))) | |
8 | ||
9 | static int inotify_handler(sd_event_source *source, | |
10 | const struct inotify_event *event, | |
11 | void *userdata) { | |
12 | ||
13 | const char *desc = NULL; | |
14 | ||
15 | sd_event_source_get_description(source, &desc); | |
16 | ||
17 | if (event->mask & IN_Q_OVERFLOW) | |
18 | printf("inotify-handler <%s>: overflow\n", desc); | |
19 | else if (event->mask & IN_CREATE) | |
20 | printf("inotify-handler <%s>: create on %s\n", desc, event->name); | |
21 | else if (event->mask & IN_DELETE) | |
22 | printf("inotify-handler <%s>: delete on %s\n", desc, event->name); | |
23 | else if (event->mask & IN_MOVED_TO) | |
24 | printf("inotify-handler <%s>: moved-to on %s\n", desc, event->name); | |
25 | ||
26 | /* Terminate the program if an "exit" file appears */ | |
27 | if ((event->mask & (IN_CREATE|IN_MOVED_TO)) && | |
28 | strcmp(event->name, "exit") == 0) | |
29 | sd_event_exit(sd_event_source_get_event(source), 0); | |
30 | ||
31 | return 1; | |
32 | } | |
33 | ||
34 | int main(int argc, char **argv) { | |
35 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
36 | _cleanup_(sd_event_source_unrefp) sd_event_source *source1 = NULL, *source2 = NULL; | |
37 | ||
38 | const char *path1 = argc > 1 ? argv[1] : "/tmp"; | |
39 | const char *path2 = argc > 2 ? argv[2] : NULL; | |
40 | ||
41 | /* Note: failure handling is omitted for brevity */ | |
42 | ||
43 | sd_event_default(&event); | |
44 | ||
45 | sd_event_add_inotify(event, &source1, path1, | |
46 | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO, | |
47 | inotify_handler, NULL); | |
48 | if (path2) | |
49 | sd_event_add_inotify(event, &source2, path2, | |
50 | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO, | |
51 | inotify_handler, NULL); | |
52 | ||
53 | sd_event_loop(event); | |
54 | ||
55 | return 0; | |
56 | } |