Debian Codegraph @

src/util/dirwatch.c

raw
1 /*
2 * Directory Watch
3 *
4 * The Dirwatch object implements a simple notification mechanism based on
5 * file-system monitoring. It allows to get notified whenever a file inside of
6 * a list of directories is modified.
7 *
8 * Since file-system monitoring is frowned upon, we only implement what is
9 * necessary to be compatible to dbus-daemon. This means, only very basic
10 * direct watches without detailed reporting are supported.
11 */
12
13 #include <c-stdaux.h>
14 #include <limits.h>
15 #include <stdlib.h>
16 #include <sys/inotify.h>
17 #include "util/dirwatch.h"
18 #include "util/error.h"
19
20 /**
21 * dirwatch_new() - initialize directory watch
22 * @dwp: output variable for directory watch
23 *
24 * This initializes a new directory watch.
25 *
26 * Return: 0 on success, negative error code on failure.
27 */
28 int dirwatch_new(Dirwatch **dwp) {
29 _c_cleanup_(dirwatch_freep) Dirwatch *dw = NULL;
30
31 dw = malloc(sizeof(*dw));
32 if (!dw)
33 return error_origin(-ENOMEM);
34
35 *dw = (Dirwatch)DIRWATCH_NULL(*dw);
36
37 dw->inotify_fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
38 if (dw->inotify_fd < 0)
39 return error_origin(-errno);
40
41 *dwp = dw;
42 dw = NULL;
43 return 0;
44 }
45
46 /**
47 * dirwatch_free() - deinitialize directory watch
48 * @dw: directory watch to operate on, or NULL
49 *
50 * This deinitializes @dw.
51 *
52 * Return: NULL is returned.
53 */
54 Dirwatch *dirwatch_free(Dirwatch *dw) {
55 if (dw) {
56 c_close(dw->inotify_fd);
57 free(dw);
58 }
59 return NULL;
60 }
61
62 /**
63 * dirwatch_get_fd() - return context FD
64 * @dw: directory watch to operate on
65 *
66 * Return the inotify context file-descriptor. The caller is supposed to
67 * monitor it for read-events and call dirwatch_dispatch() when ready.
68 *
69 * Return: File-descriptor is returned.
70 */
71 int dirwatch_get_fd(Dirwatch *dw) {
72 return dw->inotify_fd;
73 }
74
75 /**
76 * dirwatch_dispatch() - dispatch the directory watch
77 * @dw: directory watch to operate on
78 *
79 * This dispatches all active events on the dirwatch. If a file-system
80 * modification was detected, DIRWATCH_E_TRIGGERED is signalled to the caller.
81 *
82 * Return: 0 is returned on success, DIRWATCH_E_TRIGGERED if a file-system
83 * modification was detected, a negative error code on failure.
84 */
85 int dirwatch_dispatch(Dirwatch *dw) {
86 uint8_t buffer[16 * (sizeof(struct inotify_event) + NAME_MAX + 1)];
87 bool triggered = false;
88 ssize_t l;
89
90 do {
91 l = read(dw->inotify_fd, buffer, sizeof(buffer));
92 if (l > 0) {
93 triggered = true;
94 } else if (!l) {
95 return error_origin(-EINVAL);
96 } else if (errno != EAGAIN) {
97 return error_origin(-errno);
98 }
99 } while (l > 0);
100
101 return triggered ? DIRWATCH_E_TRIGGERED : 0;
102 }
103
104 /**
105 * dirwatch_add() - add path to watch-list
106 * @dw: directory watch to operate on
107 * @path: path to watch
108 */
109 int dirwatch_add(Dirwatch *dw, const char *path) {
110 int r;
111
112 r = inotify_add_watch(dw->inotify_fd,
113 path,
114 IN_CLOSE_WRITE | IN_DELETE
115 | IN_MOVED_TO
116 | IN_MOVED_FROM);
117 if (r < 0) {
118 /* non-existant dirs are silently ignored by dbus-daemon */
119 if (errno == ENOENT || errno == ENOTDIR)
120 return 0;
121
122 return error_origin(-errno);
123 }
124
125 return 0;
126 }