wobblyhead hg > freebsd > botblockd
comparison src/logtail.c @ 0:9f8d79c98fd2 0.4.0
initial public release
| author | Arkadiusz Juszczyk <ark@wobblyhead.net> |
|---|---|
| date | 2026-10-26 11:29 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:9f8d79c98fd2 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2026 Arkadiusz Juszczyk <ark@wobblyhead.net> | |
| 3 * | |
| 4 * SPDX-License-Identifier: BSD-2-Clause | |
| 5 */ | |
| 6 | |
| 7 #include <sys/param.h> | |
| 8 #include <sys/event.h> | |
| 9 #include <sys/stat.h> | |
| 10 | |
| 11 #include <errno.h> | |
| 12 #include <fcntl.h> | |
| 13 #include <stdint.h> | |
| 14 #include <stdlib.h> | |
| 15 #include <string.h> | |
| 16 #include <syslog.h> | |
| 17 #include <time.h> | |
| 18 #include <unistd.h> | |
| 19 | |
| 20 #include "botblockd.h" | |
| 21 | |
| 22 /* | |
| 23 * Two arrangements of the standard nginx combined log are accepted on | |
| 24 * the same input file: | |
| 25 * | |
| 26 * raw combined, IP at offset 0: | |
| 27 * <ip> - <user> [<time>] "<req>" <status> <bytes> "<ref>" "<ua>" | |
| 28 * | |
| 29 * syslog-aggregate combined, with a BSD-syslog header prepended by | |
| 30 * the host syslogd ahead of the original combined line: | |
| 31 * <mon> <day> <hh:mm:ss> <host> <tag>: <ip> - - [<time>] "<req>" ... | |
| 32 * | |
| 33 * parse_line tries the IP at offset 0 first; on failure it tries once | |
| 34 * more right after the first ": ", which ends the syslog header. Both | |
| 35 * formats yield the same (ip, status) extraction. Unrelated lines | |
| 36 * that share the file (newsyslog rotation markers, other facilities) | |
| 37 * lack a parseable IPv4 at either position and drop out without | |
| 38 * touching counters. Text further into a line is never searched for | |
| 39 * an address, since other programs may log text chosen by a client. | |
| 40 */ | |
| 41 | |
| 42 /* | |
| 43 * The log is tracked by the file it refers to, not by name. Once a | |
| 44 * second, the file INPUT_LOG names is compared with the file being read. | |
| 45 * When it names a different file (rotation), the rest of the current | |
| 46 * file is read and the new one is read from its start. Lines written | |
| 47 * to the old file after that are not seen. While INPUT_LOG cannot be | |
| 48 * accessed, the current file is still read. | |
| 49 */ | |
| 50 | |
| 51 static int log_fd = -1; | |
| 52 static bool path_missing; /* INPUT_LOG could not be accessed */ | |
| 53 static size_t line_len; /* bytes held in linebuf */ | |
| 54 static char linebuf[8192]; /* partial line carried across reads */ | |
| 55 static char readbuf[65536]; | |
| 56 static int kq = -1; | |
| 57 | |
| 58 /* | |
| 59 * parse_line - extract IP and status from a combined-log line | |
| 60 * | |
| 61 * Parameters: | |
| 62 * line - input log line (not modified) | |
| 63 * ip_out - output for parsed IP (network byte order) | |
| 64 * status_out - output pointer for HTTP status code | |
| 65 * | |
| 66 * Returns: 0 on success, -1 if line cannot be parsed | |
| 67 */ | |
| 68 static int | |
| 69 parse_line(const char *line, uint32_t *ip_out, int *status_out) | |
| 70 { | |
| 71 const char *p, *q; | |
| 72 char *endp; | |
| 73 long st; | |
| 74 | |
| 75 p = line; | |
| 76 if (parse_ipv4(p, ip_out) != 0) { | |
| 77 p = strstr(line, ": "); | |
| 78 if (p == NULL) | |
| 79 return (-1); | |
| 80 p += 2; | |
| 81 if (parse_ipv4(p, ip_out) != 0) | |
| 82 return (-1); | |
| 83 } | |
| 84 | |
| 85 /* status: find closing quote of request, then read number */ | |
| 86 q = strchr(p, '"'); | |
| 87 if (q == NULL) | |
| 88 return (-1); | |
| 89 q = strchr(q + 1, '"'); | |
| 90 if (q == NULL) | |
| 91 return (-1); | |
| 92 q++; | |
| 93 while (*q == ' ') | |
| 94 q++; | |
| 95 st = strtol(q, &endp, 10); | |
| 96 if (endp == q || st < 0 || st >= NSTATUS || | |
| 97 (*endp != ' ' && *endp != '\n' && *endp != '\0')) | |
| 98 return (-1); | |
| 99 *status_out = (int)st; | |
| 100 return (0); | |
| 101 } | |
| 102 | |
| 103 /* | |
| 104 * feed - split read data into lines and process each complete line | |
| 105 * | |
| 106 * A line longer than the buffer is cut; the fields parse_line needs | |
| 107 * come first, so the rest is dropped. | |
| 108 * | |
| 109 * Parameters: | |
| 110 * p - data | |
| 111 * n - length of data | |
| 112 */ | |
| 113 static void | |
| 114 feed(const char *p, size_t n) | |
| 115 { | |
| 116 uint32_t ip; | |
| 117 int status; | |
| 118 size_t i; | |
| 119 | |
| 120 for (i = 0; i < n; i++) { | |
| 121 if (line_len < sizeof(linebuf) - 1) | |
| 122 linebuf[line_len++] = p[i]; | |
| 123 if (p[i] != '\n') | |
| 124 continue; | |
| 125 linebuf[line_len] = '\0'; | |
| 126 line_len = 0; | |
| 127 if (parse_line(linebuf, &ip, &status) == 0) | |
| 128 process_request(ip, status); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 /* | |
| 133 * drain - read and process what has been appended to the log | |
| 134 */ | |
| 135 static void | |
| 136 drain(void) | |
| 137 { | |
| 138 ssize_t r; | |
| 139 | |
| 140 while ((r = read(log_fd, readbuf, sizeof(readbuf))) > 0) | |
| 141 feed(readbuf, (size_t)r); | |
| 142 } | |
| 143 | |
| 144 /* | |
| 145 * watch - register a log file descriptor with the kqueue | |
| 146 * | |
| 147 * Writes to the file wake the loop to read. | |
| 148 * | |
| 149 * Parameters: | |
| 150 * fd - open log file | |
| 151 * | |
| 152 * Returns: 0 on success, -1 on failure (logged) | |
| 153 */ | |
| 154 static int | |
| 155 watch(int fd) | |
| 156 { | |
| 157 struct kevent ev; | |
| 158 | |
| 159 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, | |
| 160 NOTE_WRITE | NOTE_EXTEND, 0, NULL); | |
| 161 if (kevent(kq, &ev, 1, NULL, 0, NULL) < 0) { | |
| 162 syslog(LOG_ERR, "kevent add: %s", strerror(errno)); | |
| 163 return (-1); | |
| 164 } | |
| 165 return (0); | |
| 166 } | |
| 167 | |
| 168 /* | |
| 169 * open_log - open INPUT_LOG for reading | |
| 170 * | |
| 171 * Parameters: | |
| 172 * at_end - true to skip the existing content | |
| 173 * | |
| 174 * Returns: file descriptor, or -1 with errno set | |
| 175 */ | |
| 176 static int | |
| 177 open_log(bool at_end) | |
| 178 { | |
| 179 int fd, saved; | |
| 180 | |
| 181 fd = open(cfg.input_log, O_RDONLY | O_CLOEXEC); | |
| 182 if (fd < 0) | |
| 183 return (-1); | |
| 184 if (at_end && lseek(fd, 0, SEEK_END) < 0) { | |
| 185 saved = errno; | |
| 186 close(fd); | |
| 187 errno = saved; | |
| 188 return (-1); | |
| 189 } | |
| 190 return (fd); | |
| 191 } | |
| 192 | |
| 193 /* | |
| 194 * path_gone - note that INPUT_LOG cannot be accessed | |
| 195 * | |
| 196 * Logs a warning only the first time, so a lasting problem is logged | |
| 197 * once. | |
| 198 * | |
| 199 * Returns: 0, for check_path to pass on | |
| 200 */ | |
| 201 static int | |
| 202 path_gone(void) | |
| 203 { | |
| 204 if (!path_missing) | |
| 205 syslog(LOG_WARNING, "cannot access %s: %s", cfg.input_log, | |
| 206 strerror(errno)); | |
| 207 path_missing = true; | |
| 208 return (0); | |
| 209 } | |
| 210 | |
| 211 /* | |
| 212 * check_path - follow rotation and truncation of the log | |
| 213 * | |
| 214 * If INPUT_LOG names the file being read and that file has shrunk | |
| 215 * below the read position (truncated in place), reading restarts from | |
| 216 * its beginning. If INPUT_LOG names a different file, the rest of the | |
| 217 * current file is read, then the new file becomes current and is read | |
| 218 * from its start. If INPUT_LOG cannot be accessed, a warning is | |
| 219 * logged once and the current file stays in use. | |
| 220 * | |
| 221 * Returns: 0 on success, -1 on a fatal error (logged) | |
| 222 */ | |
| 223 static int | |
| 224 check_path(void) | |
| 225 { | |
| 226 struct stat sf, sp; | |
| 227 int fd; | |
| 228 | |
| 229 if (stat(cfg.input_log, &sp) != 0) | |
| 230 return (path_gone()); | |
| 231 if (path_missing) | |
| 232 syslog(LOG_NOTICE, "%s is available again", cfg.input_log); | |
| 233 path_missing = false; | |
| 234 | |
| 235 if (fstat(log_fd, &sf) != 0) { | |
| 236 syslog(LOG_ERR, "fstat %s: %s", cfg.input_log, | |
| 237 strerror(errno)); | |
| 238 return (-1); | |
| 239 } | |
| 240 | |
| 241 /* same file: only check for truncation */ | |
| 242 if (sp.st_dev == sf.st_dev && sp.st_ino == sf.st_ino) { | |
| 243 if (lseek(log_fd, 0, SEEK_CUR) > sf.st_size) { | |
| 244 syslog(LOG_NOTICE, "%s truncated, reading from start", | |
| 245 cfg.input_log); | |
| 246 lseek(log_fd, 0, SEEK_SET); | |
| 247 line_len = 0; | |
| 248 drain(); | |
| 249 } | |
| 250 return (0); | |
| 251 } | |
| 252 | |
| 253 fd = open_log(false); | |
| 254 if (fd < 0) | |
| 255 return (path_gone()); | |
| 256 if (watch(fd) != 0) { | |
| 257 close(fd); | |
| 258 return (-1); | |
| 259 } | |
| 260 drain(); | |
| 261 close(log_fd); /* also removes its kqueue watch */ | |
| 262 log_fd = fd; | |
| 263 line_len = 0; | |
| 264 syslog(LOG_INFO, "%s rotated, reading new file", cfg.input_log); | |
| 265 | |
| 266 /* lines written before the watch was set raise no event */ | |
| 267 drain(); | |
| 268 return (0); | |
| 269 } | |
| 270 | |
| 271 /* | |
| 272 * tail_open - open the configured log, positioned at its end | |
| 273 * | |
| 274 * Called before the daemon detaches, so a wrong INPUT_LOG fails the | |
| 275 * start with an exit status, and by the check verb. | |
| 276 * | |
| 277 * Returns: 0 on success, -1 on failure (logged) | |
| 278 */ | |
| 279 int | |
| 280 tail_open(void) | |
| 281 { | |
| 282 log_fd = open_log(true); | |
| 283 if (log_fd < 0) { | |
| 284 syslog(LOG_ERR, "cannot open %s: %s", cfg.input_log, | |
| 285 strerror(errno)); | |
| 286 return (-1); | |
| 287 } | |
| 288 line_len = 0; | |
| 289 return (0); | |
| 290 } | |
| 291 | |
| 292 /* | |
| 293 * tail_log - main event loop: follow the log and run the sweep | |
| 294 * | |
| 295 * Needs a log opened by tail_open. Sleeps in kevent until the log is | |
| 296 * written to, or for at most a second. Every wakeup reads the log; | |
| 297 * once a second INPUT_LOG is checked for rotation and truncation. | |
| 298 * | |
| 299 * The sweep runs at most once per sweep interval: half of | |
| 300 * inspect_secs, but between 1 and 10 seconds, so the cost of the | |
| 301 * suspect walk stays bounded regardless of log activity. | |
| 302 * | |
| 303 * A SIGHUP reloads the configuration. SIGTERM and SIGINT interrupt | |
| 304 * kevent, so the loop ends promptly. | |
| 305 * | |
| 306 * Returns: 0 on shutdown by signal, -1 on a fatal error (logged) | |
| 307 */ | |
| 308 int | |
| 309 tail_log(void) | |
| 310 { | |
| 311 const struct timespec timeout = { .tv_sec = 1 }; | |
| 312 struct kevent ev; | |
| 313 time_t now, last_sweep_at, last_check_at; | |
| 314 int rc, sweep_interval; | |
| 315 | |
| 316 rc = -1; | |
| 317 kq = kqueue(); | |
| 318 if (kq < 0) { | |
| 319 syslog(LOG_ERR, "kqueue: %s", strerror(errno)); | |
| 320 goto out; | |
| 321 } | |
| 322 if (watch(log_fd) != 0) | |
| 323 goto out; | |
| 324 syslog(LOG_INFO, "reading logfile %s", cfg.input_log); | |
| 325 | |
| 326 rc = 0; | |
| 327 last_sweep_at = 0; | |
| 328 last_check_at = 0; | |
| 329 | |
| 330 while (running) { | |
| 331 if (reload_pending) { | |
| 332 reload_pending = 0; | |
| 333 config_reload(); | |
| 334 } | |
| 335 | |
| 336 if (kevent(kq, NULL, 0, &ev, 1, &timeout) < 0) { | |
| 337 if (errno == EINTR) | |
| 338 continue; | |
| 339 syslog(LOG_ERR, "kevent wait: %s", strerror(errno)); | |
| 340 rc = -1; | |
| 341 break; | |
| 342 } | |
| 343 | |
| 344 drain(); | |
| 345 | |
| 346 now = monotime(); | |
| 347 if (now != last_check_at) { | |
| 348 last_check_at = now; | |
| 349 if (check_path() != 0) { | |
| 350 rc = -1; | |
| 351 break; | |
| 352 } | |
| 353 } | |
| 354 | |
| 355 /* recompute each pass so a reload takes effect */ | |
| 356 sweep_interval = MAX(1, MIN(10, cfg.inspect_secs / 2)); | |
| 357 if (now - last_sweep_at >= sweep_interval) { | |
| 358 sweep(); | |
| 359 last_sweep_at = now; | |
| 360 } | |
| 361 } | |
| 362 | |
| 363 out: | |
| 364 if (log_fd >= 0) | |
| 365 close(log_fd); | |
| 366 if (kq >= 0) | |
| 367 close(kq); | |
| 368 return (rc); | |
| 369 } |
