Debian Codegraph @

runtime/rswatch.c

raw
1 /* rswatch.c
2 * generic watched-file support for runtime-managed reloaders
3 *
4 * Copyright 2026 Rainer Gerhards and Adiscon GmbH.
5 *
6 * This file is part of the rsyslog runtime library.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * -or-
14 * see COPYING.ASL20 in the source distribution
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22 #include "config.h"
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #ifdef HAVE_SYS_INOTIFY_H
31 #include <sys/inotify.h>
32 #endif
33
34 #include "rswatch.h"
35
36 struct rswatch_handle_s {
37 char *id;
38 char *path;
39 char *dir;
40 char *base;
41 unsigned int debounce_ms;
42 rswatch_cb_t cb;
43 void *ctx;
44 #if defined(HAVE_INOTIFY_INIT) && defined(HAVE_SYS_INOTIFY_H)
45 int wd;
46 sbool pending;
47 uint64_t due_at_ms;
48 #endif
49 struct rswatch_handle_s *next;
50 };
51
52 #if defined(HAVE_INOTIFY_INIT) && defined(HAVE_SYS_INOTIFY_H)
53 typedef struct rswatch_state_s {
54 pthread_mutex_t mut;
55 rswatch_handle_t *handles;
56 int ino_fd;
57 sbool disabled;
58 } rswatch_state_t;
59
60 /* One process-global watcher state is sufficient because all activity is
61 * multiplexed through the main housekeeping loop.
62 */
63
64 static rswatch_state_t g_rswatch = {PTHREAD_MUTEX_INITIALIZER, NULL, -1, 0};
65 #endif
66
67 static char *rswatchDupDirname(const char *path) {
68 const char *slash;
69 size_t len;
70 char *out;
71
72 if (path == NULL || *path == '\0') {
73 return NULL;
74 }
75
76 slash = strrchr(path, '/');
77 if (slash == NULL) {
78 return strdup(".");
79 }
80 if (slash == path) {
81 return strdup("/");
82 }
83
84 len = (size_t)(slash - path);
85 out = malloc(len + 1);
86 if (out == NULL) {
87 return NULL;
88 }
89 memcpy(out, path, len);
90 out[len] = '\0';
91 return out;
92 }
93
94 static char *rswatchDupBasename(const char *path) {
95 const char *slash;
96
97 if (path == NULL || *path == '\0') {
98 return NULL;
99 }
100
101 slash = strrchr(path, '/');
102 return strdup((slash == NULL) ? path : slash + 1);
103 }
104
105 static void rswatchFreeHandle(rswatch_handle_t *handle) {
106 if (handle == NULL) {
107 return;
108 }
109 free(handle->id);
110 free(handle->path);
111 free(handle->dir);
112 free(handle->base);
113 free(handle);
114 }
115
116 #if defined(HAVE_INOTIFY_INIT) && defined(HAVE_SYS_INOTIFY_H)
117 static rsRetVal rswatchSetNonBlocking(int fd) {
118 int flags;
119 DEFiRet;
120
121 if (fd == -1) {
122 ABORT_FINALIZE(RS_RET_PARAM_ERROR);
123 }
124
125 if ((flags = fcntl(fd, F_GETFL)) == -1) {
126 ABORT_FINALIZE(RS_RET_IO_ERROR);
127 }
128 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
129 ABORT_FINALIZE(RS_RET_IO_ERROR);
130 }
131
132 finalize_it:
133 RETiRet;
134 }
135
136 static rsRetVal rswatchSetCloseOnExec(int fd) {
137 int flags;
138 DEFiRet;
139
140 if (fd == -1) {
141 ABORT_FINALIZE(RS_RET_PARAM_ERROR);
142 }
143
144 if ((flags = fcntl(fd, F_GETFD)) == -1) {
145 ABORT_FINALIZE(RS_RET_IO_ERROR);
146 }
147 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
148 ABORT_FINALIZE(RS_RET_IO_ERROR);
149 }
150
151 finalize_it:
152 RETiRet;
153 }
154
155 static rsRetVal rswatchEnsureInfraLocked(void) {
156 int saved_errno = 0;
157 DEFiRet;
158
159 if (g_rswatch.disabled) {
160 ABORT_FINALIZE(RS_RET_IO_ERROR);
161 }
162
163 if (g_rswatch.ino_fd == -1) {
164 g_rswatch.ino_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
165 saved_errno = errno;
166 if (g_rswatch.ino_fd == -1) {
167 #ifndef IN_CLOEXEC
168 g_rswatch.ino_fd = inotify_init1(IN_NONBLOCK);
169 if (g_rswatch.ino_fd != -1) {
170 CHKiRet(rswatchSetCloseOnExec(g_rswatch.ino_fd));
171 }
172 saved_errno = errno;
173 #endif
174 if (g_rswatch.ino_fd == -1 && saved_errno == ENOSYS) {
175 g_rswatch.ino_fd = inotify_init();
176 if (g_rswatch.ino_fd != -1) {
177 CHKiRet(rswatchSetNonBlocking(g_rswatch.ino_fd));
178 CHKiRet(rswatchSetCloseOnExec(g_rswatch.ino_fd));
179 }
180 }
181 if (g_rswatch.ino_fd == -1) {
182 ABORT_FINALIZE(RS_RET_INOTIFY_INIT_FAILED);
183 }
184 }
185 }
186
187 finalize_it:
188 if (iRet != RS_RET_OK && g_rswatch.ino_fd != -1) {
189 close(g_rswatch.ino_fd);
190 g_rswatch.ino_fd = -1;
191 }
192 RETiRet;
193 }
194
195 static int rswatchFindDirWatchLocked(const char *dir) {
196 rswatch_handle_t *handle;
197
198 for (handle = g_rswatch.handles; handle != NULL; handle = handle->next) {
199 if (!strcmp(handle->dir, dir)) {
200 return handle->wd;
201 }
202 }
203 return -1;
204 }
205
206 static sbool rswatchDirInUseLocked(int wd) {
207 rswatch_handle_t *handle;
208
209 for (handle = g_rswatch.handles; handle != NULL; handle = handle->next) {
210 if (handle->wd == wd) {
211 return 1;
212 }
213 }
214 return 0;
215 }
216
217 static void rswatchDisableLocked(int err, const char *reason) {
218 rswatch_handle_t *handle;
219
220 if (!g_rswatch.disabled) {
221 LogError(err, RS_RET_IO_ERROR, "rswatch: %s, disabling watched-file reload support", reason);
222 }
223 g_rswatch.disabled = 1;
224 if (g_rswatch.ino_fd != -1) {
225 close(g_rswatch.ino_fd);
226 g_rswatch.ino_fd = -1;
227 }
228 for (handle = g_rswatch.handles; handle != NULL; handle = handle->next) {
229 handle->wd = -1;
230 handle->pending = 0;
231 handle->due_at_ms = 0;
232 }
233 }
234
235 /* Each matching event resets the quiet-period deadline. This coalesces bursty
236 * editor save patterns into a single later reload callback.
237 */
238 static void rswatchMarkEventLocked(int wd, const char *name, uint64_t now_ms) {
239 rswatch_handle_t *handle;
240
241 if (name == NULL || *name == '\0') {
242 return;
243 }
244
245 for (handle = g_rswatch.handles; handle != NULL; handle = handle->next) {
246 if (handle->wd == wd && !strcmp(handle->base, name)) {
247 handle->pending = 1;
248 handle->due_at_ms = now_ms + handle->debounce_ms;
249 }
250 }
251 }
252
253 static rswatch_handle_t *rswatchPopDueLocked(uint64_t now_ms) {
254 rswatch_handle_t *handle;
255
256 for (handle = g_rswatch.handles; handle != NULL; handle = handle->next) {
257 if (handle->pending && handle->due_at_ms <= now_ms) {
258 handle->pending = 0;
259 return handle;
260 }
261 }
262 return NULL;
263 }
264 #endif
265
266 rsRetVal rswatchRegister(const rswatch_desc_t *desc, rswatch_handle_t **out) {
267 rswatch_handle_t *handle = NULL;
268 sbool bLocked = 0;
269 int wd;
270 DEFiRet;
271
272 if (out != NULL) {
273 *out = NULL;
274 }
275 if (desc == NULL || desc->id == NULL || desc->path == NULL || *desc->path == '\0' || desc->cb == NULL) {
276 ABORT_FINALIZE(RS_RET_PARAM_ERROR);
277 }
278
279 #if !defined(HAVE_INOTIFY_INIT) || !defined(HAVE_SYS_INOTIFY_H)
280 ABORT_FINALIZE(RS_RET_NOT_IMPLEMENTED);
281 #else
282 CHKmalloc(handle = calloc(1, sizeof(*handle)));
283 CHKmalloc(handle->id = strdup(desc->id));
284 CHKmalloc(handle->path = strdup(desc->path));
285 CHKmalloc(handle->dir = rswatchDupDirname(desc->path));
286 CHKmalloc(handle->base = rswatchDupBasename(desc->path));
287 handle->debounce_ms = desc->debounce_ms;
288 handle->cb = desc->cb;
289 handle->ctx = desc->ctx;
290 handle->wd = -1;
291
292 pthread_mutex_lock(&g_rswatch.mut);
293 bLocked = 1;
294 CHKiRet(rswatchEnsureInfraLocked());
295 wd = rswatchFindDirWatchLocked(handle->dir);
296 if (wd == -1) {
297 wd = inotify_add_watch(g_rswatch.ino_fd, handle->dir, IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB | IN_CREATE);
298 if (wd == -1) {
299 ABORT_FINALIZE(RS_RET_IO_ERROR);
300 }
301 }
302 handle->wd = wd;
303 handle->next = g_rswatch.handles;
304 g_rswatch.handles = handle;
305 if (out != NULL) {
306 *out = handle;
307 }
308 handle = NULL;
309 #endif
310
311 finalize_it:
312 #if defined(HAVE_INOTIFY_INIT) && defined(HAVE_SYS_INOTIFY_H)
313 if (bLocked) {
314 pthread_mutex_unlock(&g_rswatch.mut);
315 }
316 #endif
317 if (handle != NULL) {
318 rswatchFreeHandle(handle);
319 }
320 RETiRet;
321 }
322
323 void rswatchUnregister(rswatch_handle_t *handle) {
324 #if defined(HAVE_INOTIFY_INIT) && defined(HAVE_SYS_INOTIFY_H)
325 rswatch_handle_t **pp;
326
327 if (handle == NULL) {
328 return;
329 }
330
331 pthread_mutex_lock(&g_rswatch.mut);
332 pp = &g_rswatch.handles;
333 while (*pp != NULL) {
334 if (*pp != handle) {
335 pp = &(*pp)->next;
336 continue;
337 }
338
339 *pp = handle->next;
340 if (!g_rswatch.disabled && g_rswatch.ino_fd != -1 && handle->wd != -1 && !rswatchDirInUseLocked(handle->wd)) {
341 inotify_rm_watch(g_rswatch.ino_fd, handle->wd);
342 }
343 if (g_rswatch.handles == NULL && g_rswatch.ino_fd != -1) {
344 close(g_rswatch.ino_fd);
345 g_rswatch.ino_fd = -1;
346 g_rswatch.disabled = 0;
347 }
348 pthread_mutex_unlock(&g_rswatch.mut);
349 rswatchFreeHandle(handle);
350 return;
351 }
352 pthread_mutex_unlock(&g_rswatch.mut);
353 #else
354 (void)handle;
355 #endif
356 }
357
358 int rswatchGetWaitFd(void) {
359 #if defined(HAVE_INOTIFY_INIT) && defined(HAVE_SYS_INOTIFY_H)
360 int fd;
361 pthread_mutex_lock(&g_rswatch.mut);
362 fd = (g_rswatch.disabled || g_rswatch.handles == NULL) ? -1 : g_rswatch.ino_fd;
363 pthread_mutex_unlock(&g_rswatch.mut);
364 return fd;
365 #else
366 return -1;
367 #endif
368 }
369
370 int rswatchComputeTimeoutMs(uint64_t now_ms, int default_timeout_ms) {
371 #if defined(HAVE_INOTIFY_INIT) && defined(HAVE_SYS_INOTIFY_H)
372 rswatch_handle_t *handle;
373 uint64_t diff;
374 int timeout_ms = default_timeout_ms;
375
376 pthread_mutex_lock(&g_rswatch.mut);
377 if (g_rswatch.disabled || g_rswatch.handles == NULL) {
378 pthread_mutex_unlock(&g_rswatch.mut);
379 return timeout_ms;
380 }
381
382 for (handle = g_rswatch.handles; handle != NULL; handle = handle->next) {
383 if (!handle->pending) {
384 continue;
385 }
386 if (handle->due_at_ms <= now_ms) {
387 timeout_ms = 0;
388 break;
389 }
390 diff = handle->due_at_ms - now_ms;
391 if (diff > (uint64_t)INT_MAX) {
392 diff = (uint64_t)INT_MAX;
393 }
394 if (timeout_ms < 0 || (int)diff < timeout_ms) {
395 timeout_ms = (int)diff;
396 }
397 }
398 pthread_mutex_unlock(&g_rswatch.mut);
399 return timeout_ms;
400 #else
401 (void)now_ms;
402 return default_timeout_ms;
403 #endif
404 }
405
406 void rswatchProcessIo(uint64_t now_ms) {
407 #if defined(HAVE_INOTIFY_INIT) && defined(HAVE_SYS_INOTIFY_H)
408 char buf[4096];
409 ssize_t rd;
410
411 pthread_mutex_lock(&g_rswatch.mut);
412 if (g_rswatch.disabled || g_rswatch.ino_fd == -1) {
413 pthread_mutex_unlock(&g_rswatch.mut);
414 return;
415 }
416 pthread_mutex_unlock(&g_rswatch.mut);
417
418 while ((rd = read(g_rswatch.ino_fd, buf, sizeof(buf))) > 0) {
419 ssize_t off = 0;
420
421 pthread_mutex_lock(&g_rswatch.mut);
422 /* inotify read() is expected to return complete events, but keep the
423 * parser defensive in case a short trailing fragment is ever seen.
424 */
425 while (off + (ssize_t)sizeof(struct inotify_event) <= rd) {
426 struct inotify_event evhdr;
427 const char *name = NULL;
428
429 memcpy(&evhdr, buf + off, sizeof(evhdr));
430 /* This is not expected from inotify read(), but keep the parser
431 * defensive if the trailing name payload is ever truncated.
432 */
433 if (off + (ssize_t)sizeof(struct inotify_event) + (ssize_t)evhdr.len > rd) {
434 break;
435 }
436 if (evhdr.len > 0) {
437 name = buf + off + sizeof(evhdr);
438 }
439 if ((evhdr.mask & (IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE | IN_ATTRIB)) != 0) {
440 rswatchMarkEventLocked(evhdr.wd, name, now_ms);
441 }
442 off += (ssize_t)(sizeof(struct inotify_event) + evhdr.len);
443 }
444 pthread_mutex_unlock(&g_rswatch.mut);
445 }
446
447 if (rd < 0 && errno != EAGAIN && errno != EINTR) {
448 pthread_mutex_lock(&g_rswatch.mut);
449 rswatchDisableLocked(errno, "watch backend read failed");
450 pthread_mutex_unlock(&g_rswatch.mut);
451 }
452 #else
453 (void)now_ms;
454 #endif
455 }
456
457 void rswatchDispatchDue(uint64_t now_ms) {
458 #if defined(HAVE_INOTIFY_INIT) && defined(HAVE_SYS_INOTIFY_H)
459 while (1) {
460 rswatch_handle_t *handle;
461 rswatch_cb_t cb;
462 void *ctx;
463
464 pthread_mutex_lock(&g_rswatch.mut);
465 if (g_rswatch.disabled) {
466 pthread_mutex_unlock(&g_rswatch.mut);
467 return;
468 }
469 handle = rswatchPopDueLocked(now_ms);
470 if (handle == NULL) {
471 pthread_mutex_unlock(&g_rswatch.mut);
472 return;
473 }
474 cb = handle->cb;
475 ctx = handle->ctx;
476 pthread_mutex_unlock(&g_rswatch.mut);
477
478 /* Dispatch unlocked so reload code can take its own locks without
479 * inheriting rswatch's internal lock ordering.
480 */
481 cb(ctx, "watch");
482 }
483 #else
484 (void)now_ms;
485 #endif
486 }
487
488 void rswatchExit(void) {
489 #if defined(HAVE_INOTIFY_INIT) && defined(HAVE_SYS_INOTIFY_H)
490 rswatch_handle_t *handle;
491
492 pthread_mutex_lock(&g_rswatch.mut);
493 handle = g_rswatch.handles;
494 g_rswatch.handles = NULL;
495 if (g_rswatch.ino_fd != -1) {
496 close(g_rswatch.ino_fd);
497 g_rswatch.ino_fd = -1;
498 }
499 g_rswatch.disabled = 0;
500 pthread_mutex_unlock(&g_rswatch.mut);
501
502 while (handle != NULL) {
503 rswatch_handle_t *next = handle->next;
504 rswatchFreeHandle(handle);
505 handle = next;
506 }
507 #endif
508 }