1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
#include "log_tail.h"
#include "client.h"
#include "unit.h"
#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/inotify.h>
#define MAX_LOG_WATCHES (MAX_UNITS * 4)
typedef struct {
int inotify_fd;
int watch_wd;
int log_fd;
Unit *unit;
char path[MAX_PATH];
} LogWatch;
static LogWatch watches[MAX_LOG_WATCHES];
static int watch_count = 0;
void log_tail_init(void) {
watch_count = 0;
log_info("log_tail_init: checking %d unit(s) for log paths", g.unit_count);
for (int i = 0; i < g.unit_count; i++) {
Unit *u = &g.units[i];
log_info("log_tail_init: unit '%s' has %d log path(s)",
u->name, u->log_count);
for (int j = 0; j < u->log_count; j++) {
const char *path = u->log_paths[j];
if (!path[0]) continue;
/* Resolve symlinks so inotify watches the real file, not the link */
char resolved[MAX_PATH];
const char *open_path = path;
if (realpath(path, resolved) != NULL)
open_path = resolved;
int log_fd = open(open_path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (log_fd < 0) {
log_warn("log_tail: cannot open %s: %s", open_path, strerror(errno));
continue;
}
lseek(log_fd, 0, SEEK_END);
int ifd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (ifd < 0) {
log_error("inotify_init1: %s", strerror(errno));
close(log_fd);
continue;
}
int wd = inotify_add_watch(ifd, open_path,
IN_MODIFY | IN_MOVE_SELF | IN_DELETE_SELF);
if (wd < 0) {
log_warn("log_tail: inotify_add_watch %s: %s",
open_path, strerror(errno));
close(ifd);
close(log_fd);
continue;
}
if (watch_count >= MAX_LOG_WATCHES) {
log_warn("log_tail: max watches reached, skipping %s", open_path);
close(ifd);
close(log_fd);
continue;
}
watches[watch_count].inotify_fd = ifd;
watches[watch_count].watch_wd = wd;
watches[watch_count].log_fd = log_fd;
watches[watch_count].unit = u;
strncpy(watches[watch_count].path, open_path, MAX_PATH - 1);
watch_count++;
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = ifd;
epoll_ctl(g.epoll_fd, EPOLL_CTL_ADD, ifd, &ev);
log_info("log_tail: watching %s for unit %s", open_path, u->name);
}
}
}
Unit *log_tail_fd_to_unit(int fd) {
for (int i = 0; i < watch_count; i++) {
if (watches[i].inotify_fd == fd)
return watches[i].unit;
}
return NULL;
}
static LogWatch *watch_for_inotify_fd(int fd) {
for (int i = 0; i < watch_count; i++) {
if (watches[i].inotify_fd == fd)
return &watches[i];
}
return NULL;
}
void log_tail_handle(int inotify_fd) {
LogWatch *w = watch_for_inotify_fd(inotify_fd);
if (!w) return;
/* Parse inotify events — detect log rotation (file moved or deleted) */
char evbuf[4096];
int rotated = 0;
ssize_t nread;
while ((nread = read(inotify_fd, evbuf, sizeof(evbuf))) > 0) {
char *p = evbuf;
while (p < evbuf + nread) {
struct inotify_event *ie = (struct inotify_event *)p;
if (ie->mask & (IN_MOVE_SELF | IN_DELETE_SELF))
rotated = 1;
p += sizeof(struct inotify_event) + ie->len;
}
}
/* Drain any new data from the current log fd before re-opening */
char buf[RING_BUF_LINE_MAX];
ssize_t n;
while ((n = read(w->log_fd, buf, sizeof(buf) - 1)) > 0) {
buf[n] = '\0';
ring_push(w->unit->output, buf, (int)n);
client_broadcast_output(w->unit->name, buf, 0);
}
if (!rotated) return;
/* Log file was rotated — re-open the path to follow the new file */
inotify_rm_watch(w->inotify_fd, w->watch_wd);
close(w->log_fd);
w->log_fd = -1;
int log_fd = open(w->path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (log_fd < 0) {
log_warn("log_tail: cannot re-open %s after rotation: %s",
w->path, strerror(errno));
return;
}
int wd = inotify_add_watch(w->inotify_fd, w->path,
IN_MODIFY | IN_MOVE_SELF | IN_DELETE_SELF);
if (wd < 0) {
log_warn("log_tail: inotify_add_watch after rotation %s: %s",
w->path, strerror(errno));
close(log_fd);
return;
}
w->log_fd = log_fd;
w->watch_wd = wd;
log_info("log_tail: re-opened %s after rotation", w->path);
}
void log_tail_cleanup(void) {
for (int i = 0; i < watch_count; i++) {
epoll_ctl(g.epoll_fd, EPOLL_CTL_DEL, watches[i].inotify_fd, NULL);
inotify_rm_watch(watches[i].inotify_fd, watches[i].watch_wd);
close(watches[i].inotify_fd);
close(watches[i].log_fd);
}
watch_count = 0;
}
|