]>
git.ipfire.org Git - thirdparty/systemd.git/blob - man/inotify-watch-tmp.c
3 #include <sys/inotify.h>
5 #include <systemd/sd-event.h>
7 #define _cleanup_(f) __attribute__((cleanup(f)))
9 static int inotify_handler(sd_event_source
*source
,
10 const struct inotify_event
*event
,
13 const char *desc
= NULL
;
15 sd_event_source_get_description(source
, &desc
);
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
);
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);
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
;
38 const char *path1
= argc
> 1 ? argv
[1] : "/tmp";
39 const char *path2
= argc
> 2 ? argv
[2] : NULL
;
41 /* Note: failure handling is omitted for brevity */
43 sd_event_default(&event
);
45 sd_event_add_inotify(event
, &source1
, path1
,
46 IN_CREATE
| IN_DELETE
| IN_MODIFY
| IN_MOVED_TO
,
47 inotify_handler
, NULL
);
49 sd_event_add_inotify(event
, &source2
, path2
,
50 IN_CREATE
| IN_DELETE
| IN_MODIFY
| IN_MOVED_TO
,
51 inotify_handler
, NULL
);