Debian Codegraph @

src/utils/file_watch.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 #include <errno.h>
5 #include <string.h>
6 #ifdef HAS_INOTIFY
7 #include <sys/inotify.h>
8 #elif HAS_KQUEUE
9 // clang-format off
10 #include <sys/types.h>
11 // clang-format on
12 #include <sys/event.h>
13 #undef EV_ERROR // Avoid clashing with libev's EV_ERROR
14 #include <fcntl.h> // For O_RDONLY
15 #include <sys/time.h> // For struct timespec
16 #include <unistd.h> // For open
17 #endif
18
19 #include <ev.h>
20 #include <uthash.h>
21
22 #include "file_watch.h"
23 #include "log.h"
24 #include "misc.h"
25
26 struct watched_file {
27 int wd;
28 void *ud;
29 file_watch_cb_t cb;
30
31 UT_hash_handle hh;
32 };
33
34 struct file_watch_registry {
35 struct ev_io w;
36
37 struct watched_file *reg;
38 };
39
40 static void file_watch_ev_cb(EV_P attr_unused, struct ev_io *w, int revent attr_unused) {
41 auto fwr = (struct file_watch_registry *)w;
42
43 while (true) {
44 int wd = -1;
45 #ifdef HAS_INOTIFY
46 struct inotify_event inotify_event;
47 auto ret = read(w->fd, &inotify_event, sizeof(struct inotify_event));
48 if (ret < 0) {
49 if (errno != EAGAIN) {
50 log_error_errno("Failed to read from inotify fd");
51 }
52 break;
53 }
54 wd = inotify_event.wd;
55 #elif HAS_KQUEUE
56 struct kevent ev;
57 struct timespec timeout = {0};
58 int ret = kevent(fwr->w.fd, NULL, 0, &ev, 1, &timeout);
59 if (ret <= 0) {
60 if (ret < 0) {
61 log_error_errno("Failed to get kevent");
62 }
63 break;
64 }
65 wd = (int)ev.ident;
66 #else
67 assert(false);
68 #endif
69
70 struct watched_file *wf = NULL;
71 HASH_FIND_INT(fwr->reg, &wd, wf);
72 if (!wf) {
73 log_warn("Got notification for a file I didn't watch.");
74 continue;
75 }
76 wf->cb(wf->ud);
77 }
78 }
79
80 void *file_watch_init(EV_P) {
81 log_debug("Starting watching for file changes");
82 int fd = -1;
83 #ifdef HAS_INOTIFY
84 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
85 if (fd < 0) {
86 log_error_errno("inotify_init1 failed");
87 return NULL;
88 }
89 #elif HAS_KQUEUE
90 fd = kqueue();
91 if (fd < 0) {
92 log_error_errno("Failed to create kqueue");
93 return NULL;
94 }
95 #else
96 log_info("No file watching support found on the host system.");
97 return NULL;
98 #endif
99 auto fwr = ccalloc(1, struct file_watch_registry);
100 ev_io_init(&fwr->w, file_watch_ev_cb, fd, EV_READ);
101 ev_io_start(EV_A_ & fwr->w);
102
103 return fwr;
104 }
105
106 void file_watch_destroy(EV_P_ void *_fwr) {
107 log_debug("Stopping watching for file changes");
108 auto fwr = (struct file_watch_registry *)_fwr;
109 struct watched_file *i, *tmp;
110
111 HASH_ITER(hh, fwr->reg, i, tmp) {
112 HASH_DEL(fwr->reg, i);
113 #ifdef HAS_KQUEUE
114 // kqueue watch descriptors are file descriptors of
115 // the files we are watching, so we need to close
116 // them
117 close(i->wd);
118 #endif
119 free(i);
120 }
121
122 ev_io_stop(EV_A_ & fwr->w);
123 close(fwr->w.fd);
124 free(fwr);
125 }
126
127 bool file_watch_add(void *_fwr, const char *filename, file_watch_cb_t cb, void *ud) {
128 log_debug("Adding \"%s\" to watched files", filename);
129 auto fwr = (struct file_watch_registry *)_fwr;
130 int wd = -1;
131
132 struct stat statbuf;
133 int ret = stat(filename, &statbuf);
134 if (ret < 0) {
135 log_error_errno("Failed to retrieve information about file \"%s\"", filename);
136 return false;
137 }
138 if (!S_ISREG(statbuf.st_mode)) {
139 log_info("\"%s\" is not a regular file, not watching it.", filename);
140 return false;
141 }
142
143 #ifdef HAS_INOTIFY
144 wd = inotify_add_watch(fwr->w.fd, filename,
145 IN_CLOSE_WRITE | IN_MOVE_SELF | IN_DELETE_SELF);
146 if (wd < 0) {
147 log_error_errno("Failed to watch file \"%s\"", filename);
148 return false;
149 }
150 #elif HAS_KQUEUE
151 wd = open(filename, O_RDONLY);
152 if (wd < 0) {
153 log_error_errno("Cannot open file \"%s\" for watching", filename);
154 return false;
155 }
156
157 uint32_t fflags = NOTE_DELETE | NOTE_RENAME | NOTE_REVOKE | NOTE_ATTRIB;
158 // NOTE_CLOSE_WRITE is relatively new, so we cannot just use it
159 #ifdef NOTE_CLOSE_WRITE
160 fflags |= NOTE_CLOSE_WRITE;
161 #else
162 // NOTE_WRITE will receive notification more frequent than necessary, so is less
163 // preferable
164 fflags |= NOTE_WRITE;
165 #endif
166 struct kevent ev = {
167 .ident = (unsigned int)wd, // the wd < 0 case is checked above
168 .filter = EVFILT_VNODE,
169 .flags = EV_ADD | EV_CLEAR,
170 .fflags = fflags,
171 .data = 0,
172 .udata = NULL,
173 };
174 if (kevent(fwr->w.fd, &ev, 1, NULL, 0, NULL) < 0) {
175 log_error_errno("Failed to register kevent");
176 close(wd);
177 return false;
178 }
179 #else
180 assert(false);
181 #endif // HAS_KQUEUE
182
183 auto w = ccalloc(1, struct watched_file);
184 w->wd = wd;
185 w->cb = cb;
186 w->ud = ud;
187
188 HASH_ADD_INT(fwr->reg, wd, w);
189 return true;
190 }