Debian Codegraph @

runtime/rswatch.h

raw
1 #ifndef INCLUDED_RSWATCH_H
2 #define INCLUDED_RSWATCH_H
3
4 /*
5 * rswatch is the runtime-internal watched-file scheduler used by subsystems
6 * that can reload a single configuration file without doing a full HUP sweep.
7 *
8 * Algorithm overview
9 * - Each registration describes one concrete file path plus a debounce window
10 * and a callback.
11 * - On inotify-capable builds, rswatch watches the file's parent directory and
12 * matches events by basename. This deliberately handles the common
13 * write-temp-and-rename save pattern, where the file inode changes but the
14 * configured path stays the same.
15 * - I/O readiness is integrated into rsyslogd's main housekeeping loop via
16 * rswatchGetWaitFd(), rswatchComputeTimeoutMs(), rswatchProcessIo(), and
17 * rswatchDispatchDue(). rswatch itself does not create a worker thread.
18 * - When rswatchProcessIo() sees an event for a watched basename, it marks the
19 * registration pending and stores due_at = now + debounce_ms.
20 * - rswatchDispatchDue() later pops registrations whose debounce deadline has
21 * expired and invokes their callback.
22 *
23 * Concurrency and locking
24 * - The subsystem has one process-global state object guarded by one mutex.
25 * - The mutex protects the registration list, the backend fd, backend-disable
26 * state, and per-registration pending/due bookkeeping.
27 * - Callbacks are never executed while the rswatch mutex is held. This keeps
28 * reload code free to take its own locks and avoids turning rswatch into a
29 * lock-ordering bottleneck in the main loop.
30 * - Registrations and unregistrations are expected on configuration/control
31 * paths, not on message-processing hot paths.
32 *
33 * Failure model
34 * - On builds without inotify support, registration returns
35 * RS_RET_NOT_IMPLEMENTED so callers can degrade to HUP-only behavior.
36 * - If the backend fails after registrations exist, rswatch disables itself,
37 * clears pending state, and leaves already-registered users in place so the
38 * rest of rsyslog continues to operate without watched reloads.
39 */
40
41 #include <stdint.h>
42 #include "rsyslog.h"
43
44 typedef struct rswatch_handle_s rswatch_handle_t;
45
46 typedef void (*rswatch_cb_t)(void *ctx, const char *trigger);
47
48 typedef struct rswatch_desc_s {
49 const char *id;
50 const char *path;
51 unsigned int debounce_ms;
52 rswatch_cb_t cb;
53 void *ctx;
54 } rswatch_desc_t;
55
56 rsRetVal rswatchRegister(const rswatch_desc_t *desc, rswatch_handle_t **out);
57 void rswatchUnregister(rswatch_handle_t *handle);
58 int rswatchGetWaitFd(void);
59 int rswatchComputeTimeoutMs(uint64_t now_ms, int default_timeout_ms);
60 void rswatchProcessIo(uint64_t now_ms);
61 void rswatchDispatchDue(uint64_t now_ms);
62 void rswatchExit(void);
63
64 #endif /* INCLUDED_RSWATCH_H */