| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "errno-util.h" |
| 7 |
#include "escape.h" |
| 8 |
#include "fd-util.h" |
| 9 |
#include "journal-file.h" |
| 10 |
#include "journal-importer.h" |
| 11 |
#include "log.h" |
| 12 |
#include "parse-util.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "strv.h" |
| 15 |
#include "unaligned.h" |
| 16 |
|
| 17 |
enum { |
| 18 |
IMPORTER_STATE_LINE = 0, /* waiting to read, or reading line */ |
| 19 |
IMPORTER_STATE_DATA_START, /* reading binary data header */ |
| 20 |
IMPORTER_STATE_DATA, /* reading binary data */ |
| 21 |
IMPORTER_STATE_DATA_FINISH, /* expecting newline */ |
| 22 |
IMPORTER_STATE_EOF, /* done */ |
| 23 |
}; |
| 24 |
|
| 25 |
void journal_importer_cleanup(JournalImporter *imp) { |
| 26 |
if (imp->fd >= 0 && !imp->passive_fd) { |
| 27 |
log_debug("Closing %s (fd=%d)", imp->name ?: "importer", imp->fd); |
| 28 |
safe_close(imp->fd); |
| 29 |
} |
| 30 |
|
| 31 |
free(imp->name); |
| 32 |
free(imp->buf); |
| 33 |
iovw_done(&imp->iovw); |
| 34 |
} |
| 35 |
|
| 36 |
static char* realloc_buffer(JournalImporter *imp, size_t size) { |
| 37 |
char *b, *old = ASSERT_PTR(imp)->buf; |
| 38 |
|
| 39 |
b = GREEDY_REALLOC(imp->buf, size); |
| 40 |
if (!b) |
| 41 |
return NULL; |
| 42 |
|
| 43 |
iovw_rebase(&imp->iovw, old, imp->buf); |
| 44 |
|
| 45 |
return b; |
| 46 |
} |
| 47 |
|
| 48 |
static int get_line(JournalImporter *imp, char **line, size_t *size) { |
| 49 |
ssize_t n; |
| 50 |
char *c = NULL; |
| 51 |
|
| 52 |
assert(imp); |
| 53 |
assert(line); |
| 54 |
assert(size); |
| 55 |
assert(imp->state == IMPORTER_STATE_LINE); |
| 56 |
assert(imp->offset <= imp->filled); |
| 57 |
assert(imp->filled <= MALLOC_SIZEOF_SAFE(imp->buf)); |
| 58 |
assert(imp->fd >= 0); |
| 59 |
|
| 60 |
for (;;) { |
| 61 |
if (imp->buf) { |
| 62 |
size_t start = MAX(imp->scanned, imp->offset); |
| 63 |
|
| 64 |
c = memchr(imp->buf + start, '\n', |
| 65 |
imp->filled - start); |
| 66 |
if (c) |
| 67 |
break; |
| 68 |
} |
| 69 |
|
| 70 |
imp->scanned = imp->filled; |
| 71 |
if (imp->scanned >= DATA_SIZE_MAX) |
| 72 |
return log_warning_errno(SYNTHETIC_ERRNO(ENOBUFS), |
| 73 |
"Entry is bigger than %u bytes.", |
| 74 |
DATA_SIZE_MAX); |
| 75 |
|
| 76 |
if (imp->passive_fd) |
| 77 |
/* we have to wait for some data to come to us */ |
| 78 |
return -EAGAIN; |
| 79 |
|
| 80 |
/* We know that imp->filled is at most DATA_SIZE_MAX, so if |
| 81 |
we reallocate it, we'll increase the size at least a bit. */ |
| 82 |
assert_cc(DATA_SIZE_MAX < ENTRY_SIZE_MAX); |
| 83 |
if (MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled < LINE_CHUNK && |
| 84 |
!realloc_buffer(imp, MIN(imp->filled + LINE_CHUNK, ENTRY_SIZE_MAX))) |
| 85 |
return log_oom(); |
| 86 |
|
| 87 |
assert(imp->buf); |
| 88 |
assert(MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled >= LINE_CHUNK || |
| 89 |
MALLOC_SIZEOF_SAFE(imp->buf) >= ENTRY_SIZE_MAX); |
| 90 |
|
| 91 |
n = read(imp->fd, |
| 92 |
imp->buf + imp->filled, |
| 93 |
MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled); |
| 94 |
if (n < 0) { |
| 95 |
if (ERRNO_IS_DISCONNECT(errno)) { |
| 96 |
log_debug_errno(errno, "Got disconnect for importer %s.", strna(imp->name)); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
if (!ERRNO_IS_TRANSIENT(errno)) |
| 101 |
log_error_errno(errno, "read(%d, ..., %zu): %m", |
| 102 |
imp->fd, |
| 103 |
MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled); |
| 104 |
return -errno; |
| 105 |
} else if (n == 0) |
| 106 |
return 0; |
| 107 |
|
| 108 |
imp->filled += n; |
| 109 |
} |
| 110 |
|
| 111 |
*line = imp->buf + imp->offset; |
| 112 |
*size = c + 1 - imp->buf - imp->offset; |
| 113 |
imp->offset += *size; |
| 114 |
|
| 115 |
return 1; |
| 116 |
} |
| 117 |
|
| 118 |
static int fill_fixed_size(JournalImporter *imp, void **data, size_t size) { |
| 119 |
|
| 120 |
assert(imp); |
| 121 |
assert(IN_SET(imp->state, IMPORTER_STATE_DATA_START, IMPORTER_STATE_DATA, IMPORTER_STATE_DATA_FINISH)); |
| 122 |
assert(size <= DATA_SIZE_MAX); |
| 123 |
assert(imp->offset <= imp->filled); |
| 124 |
assert(imp->filled <= MALLOC_SIZEOF_SAFE(imp->buf)); |
| 125 |
assert(imp->fd >= 0); |
| 126 |
assert(data); |
| 127 |
|
| 128 |
while (imp->filled - imp->offset < size) { |
| 129 |
ssize_t n; |
| 130 |
|
| 131 |
if (imp->passive_fd) |
| 132 |
/* we have to wait for some data to come to us */ |
| 133 |
return -EAGAIN; |
| 134 |
|
| 135 |
if (!realloc_buffer(imp, imp->offset + size)) |
| 136 |
return log_oom(); |
| 137 |
|
| 138 |
n = read(imp->fd, imp->buf + imp->filled, |
| 139 |
MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled); |
| 140 |
if (n < 0) { |
| 141 |
if (ERRNO_IS_DISCONNECT(errno)) { |
| 142 |
log_debug_errno(errno, "Got disconnect for importer %s.", strna(imp->name)); |
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
if (!ERRNO_IS_TRANSIENT(errno)) |
| 147 |
log_error_errno(errno, "read(%d, ..., %zu): %m", imp->fd, |
| 148 |
MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled); |
| 149 |
return -errno; |
| 150 |
} else if (n == 0) |
| 151 |
return 0; |
| 152 |
|
| 153 |
imp->filled += n; |
| 154 |
} |
| 155 |
|
| 156 |
*data = imp->buf + imp->offset; |
| 157 |
imp->offset += size; |
| 158 |
|
| 159 |
return 1; |
| 160 |
} |
| 161 |
|
| 162 |
static int get_data_size(JournalImporter *imp) { |
| 163 |
int r; |
| 164 |
void *data; |
| 165 |
|
| 166 |
assert(imp); |
| 167 |
assert(imp->state == IMPORTER_STATE_DATA_START); |
| 168 |
assert(imp->data_size == 0); |
| 169 |
|
| 170 |
r = fill_fixed_size(imp, &data, sizeof(uint64_t)); |
| 171 |
if (r <= 0) |
| 172 |
return r; |
| 173 |
|
| 174 |
imp->data_size = unaligned_read_le64(data); |
| 175 |
if (imp->data_size > DATA_SIZE_MAX) |
| 176 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 177 |
"Stream declares field with size %zu > DATA_SIZE_MAX = %u", |
| 178 |
imp->data_size, DATA_SIZE_MAX); |
| 179 |
if (imp->data_size == 0) |
| 180 |
log_warning("Binary field with zero length"); |
| 181 |
|
| 182 |
return 1; |
| 183 |
} |
| 184 |
|
| 185 |
static int get_data_data(JournalImporter *imp, void **data) { |
| 186 |
int r; |
| 187 |
|
| 188 |
assert(imp); |
| 189 |
assert(data); |
| 190 |
assert(imp->state == IMPORTER_STATE_DATA); |
| 191 |
|
| 192 |
r = fill_fixed_size(imp, data, imp->data_size); |
| 193 |
if (r <= 0) |
| 194 |
return r; |
| 195 |
|
| 196 |
return 1; |
| 197 |
} |
| 198 |
|
| 199 |
static int get_data_newline(JournalImporter *imp) { |
| 200 |
int r; |
| 201 |
char *data; |
| 202 |
|
| 203 |
assert(imp); |
| 204 |
assert(imp->state == IMPORTER_STATE_DATA_FINISH); |
| 205 |
|
| 206 |
r = fill_fixed_size(imp, (void**) &data, 1); |
| 207 |
if (r <= 0) |
| 208 |
return r; |
| 209 |
|
| 210 |
assert(data); |
| 211 |
if (*data != '\n') { |
| 212 |
char buf[4]; |
| 213 |
int l; |
| 214 |
|
| 215 |
l = cescape_char(*data, buf); |
| 216 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 217 |
"Expected newline, got '%.*s'", l, buf); |
| 218 |
} |
| 219 |
|
| 220 |
return 1; |
| 221 |
} |
| 222 |
|
| 223 |
static int process_special_field(JournalImporter *imp, char *line) { |
| 224 |
const char *value; |
| 225 |
char buf[CELLESCAPE_DEFAULT_LENGTH]; |
| 226 |
int r; |
| 227 |
|
| 228 |
assert(line); |
| 229 |
|
| 230 |
if (STARTSWITH_SET(line, "__CURSOR=", "__SEQNUM=", "__SEQNUM_ID=")) |
| 231 |
/* ignore __CURSOR=, __SEQNUM=, __SEQNUM_ID= which we cannot replicate */ |
| 232 |
return 1; |
| 233 |
|
| 234 |
value = startswith(line, "__REALTIME_TIMESTAMP="); |
| 235 |
if (value) { |
| 236 |
uint64_t x; |
| 237 |
|
| 238 |
r = safe_atou64(value, &x); |
| 239 |
if (r < 0) |
| 240 |
return log_warning_errno(r, "Failed to parse __REALTIME_TIMESTAMP '%s': %m", |
| 241 |
cellescape(buf, sizeof buf, value)); |
| 242 |
else if (!VALID_REALTIME(x)) { |
| 243 |
log_warning("__REALTIME_TIMESTAMP out of range, ignoring: %"PRIu64, x); |
| 244 |
return -ERANGE; |
| 245 |
} |
| 246 |
|
| 247 |
imp->ts.realtime = x; |
| 248 |
return 1; |
| 249 |
} |
| 250 |
|
| 251 |
value = startswith(line, "__MONOTONIC_TIMESTAMP="); |
| 252 |
if (value) { |
| 253 |
uint64_t x; |
| 254 |
|
| 255 |
r = safe_atou64(value, &x); |
| 256 |
if (r < 0) |
| 257 |
return log_warning_errno(r, "Failed to parse __MONOTONIC_TIMESTAMP '%s': %m", |
| 258 |
cellescape(buf, sizeof buf, value)); |
| 259 |
else if (!VALID_MONOTONIC(x)) { |
| 260 |
log_warning("__MONOTONIC_TIMESTAMP out of range, ignoring: %"PRIu64, x); |
| 261 |
return -ERANGE; |
| 262 |
} |
| 263 |
|
| 264 |
imp->ts.monotonic = x; |
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
/* Just a single underline, but it needs special treatment too. */ |
| 269 |
value = startswith(line, "_BOOT_ID="); |
| 270 |
if (value) { |
| 271 |
r = sd_id128_from_string(value, &imp->boot_id); |
| 272 |
if (r < 0) |
| 273 |
return log_warning_errno(r, "Failed to parse _BOOT_ID '%s': %m", |
| 274 |
cellescape(buf, sizeof buf, value)); |
| 275 |
|
| 276 |
/* store the field in the usual fashion too */ |
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
value = startswith(line, "__"); |
| 281 |
if (value) { |
| 282 |
log_notice("Unknown dunder line __%s, ignoring.", cellescape(buf, sizeof buf, value)); |
| 283 |
return 1; |
| 284 |
} |
| 285 |
|
| 286 |
/* no dunder */ |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
int journal_importer_process_data(JournalImporter *imp) { |
| 291 |
int r; |
| 292 |
|
| 293 |
switch (imp->state) { |
| 294 |
case IMPORTER_STATE_LINE: { |
| 295 |
char *line, *sep; |
| 296 |
size_t n = 0; |
| 297 |
|
| 298 |
assert(imp->data_size == 0); |
| 299 |
|
| 300 |
r = get_line(imp, &line, &n); |
| 301 |
if (r < 0) |
| 302 |
return r; |
| 303 |
if (r == 0) { |
| 304 |
imp->state = IMPORTER_STATE_EOF; |
| 305 |
return 0; |
| 306 |
} |
| 307 |
assert(n > 0); |
| 308 |
assert(line[n-1] == '\n'); |
| 309 |
|
| 310 |
if (n == 1) { |
| 311 |
log_trace("Received empty line, event is ready"); |
| 312 |
return 1; |
| 313 |
} |
| 314 |
|
| 315 |
/* MESSAGE=xxx\n |
| 316 |
or |
| 317 |
COREDUMP\n |
| 318 |
LLLLLLLL0011223344...\n |
| 319 |
*/ |
| 320 |
sep = memchr(line, '=', n); |
| 321 |
if (sep) { |
| 322 |
/* chomp newline */ |
| 323 |
n--; |
| 324 |
|
| 325 |
if (!journal_field_valid(line, sep - line, true)) { |
| 326 |
char buf[64], *t; |
| 327 |
|
| 328 |
t = strndupa_safe(line, MIN((size_t) (sep - line), sizeof buf)); |
| 329 |
log_debug("Ignoring invalid field: \"%s\"", |
| 330 |
cellescape(buf, sizeof buf, t)); |
| 331 |
|
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
line[n] = '\0'; |
| 336 |
r = process_special_field(imp, line); |
| 337 |
if (r != 0) |
| 338 |
return r < 0 ? r : 0; |
| 339 |
|
| 340 |
r = iovw_put(&imp->iovw, line, n); |
| 341 |
if (r < 0) |
| 342 |
return r; |
| 343 |
} else { |
| 344 |
if (!journal_field_valid(line, n - 1, true)) { |
| 345 |
char buf[64], *t; |
| 346 |
|
| 347 |
t = strndupa_safe(line, MIN(n - 1, sizeof buf)); |
| 348 |
log_debug("Ignoring invalid field: \"%s\"", |
| 349 |
cellescape(buf, sizeof buf, t)); |
| 350 |
|
| 351 |
return 0; |
| 352 |
} |
| 353 |
|
| 354 |
/* replace '\n' with '=' */ |
| 355 |
line[n-1] = '='; |
| 356 |
|
| 357 |
imp->field_len = n; |
| 358 |
imp->state = IMPORTER_STATE_DATA_START; |
| 359 |
|
| 360 |
/* we cannot put the field in iovec until we have all data */ |
| 361 |
} |
| 362 |
|
| 363 |
log_trace("Received: %.*s (%s)", (int) n, line, sep ? "text" : "binary"); |
| 364 |
|
| 365 |
return 0; /* continue */ |
| 366 |
} |
| 367 |
|
| 368 |
case IMPORTER_STATE_DATA_START: |
| 369 |
assert(imp->data_size == 0); |
| 370 |
|
| 371 |
r = get_data_size(imp); |
| 372 |
// log_debug("get_data_size() -> %d", r); |
| 373 |
if (r < 0) |
| 374 |
return r; |
| 375 |
if (r == 0) { |
| 376 |
imp->state = IMPORTER_STATE_EOF; |
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
imp->state = imp->data_size > 0 ? |
| 381 |
IMPORTER_STATE_DATA : IMPORTER_STATE_DATA_FINISH; |
| 382 |
|
| 383 |
return 0; /* continue */ |
| 384 |
|
| 385 |
case IMPORTER_STATE_DATA: { |
| 386 |
void *data; |
| 387 |
char *field; |
| 388 |
|
| 389 |
assert(imp->data_size > 0); |
| 390 |
|
| 391 |
r = get_data_data(imp, &data); |
| 392 |
// log_debug("get_data_data() -> %d", r); |
| 393 |
if (r < 0) |
| 394 |
return r; |
| 395 |
if (r == 0) { |
| 396 |
imp->state = IMPORTER_STATE_EOF; |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
assert(data); |
| 401 |
|
| 402 |
field = (char*) data - sizeof(uint64_t) - imp->field_len; |
| 403 |
memmove(field + sizeof(uint64_t), field, imp->field_len); |
| 404 |
|
| 405 |
r = iovw_put(&imp->iovw, field + sizeof(uint64_t), imp->field_len + imp->data_size); |
| 406 |
if (r < 0) |
| 407 |
return r; |
| 408 |
|
| 409 |
imp->state = IMPORTER_STATE_DATA_FINISH; |
| 410 |
|
| 411 |
return 0; /* continue */ |
| 412 |
} |
| 413 |
|
| 414 |
case IMPORTER_STATE_DATA_FINISH: |
| 415 |
r = get_data_newline(imp); |
| 416 |
// log_debug("get_data_newline() -> %d", r); |
| 417 |
if (r < 0) |
| 418 |
return r; |
| 419 |
if (r == 0) { |
| 420 |
imp->state = IMPORTER_STATE_EOF; |
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
imp->data_size = 0; |
| 425 |
imp->state = IMPORTER_STATE_LINE; |
| 426 |
|
| 427 |
return 0; /* continue */ |
| 428 |
default: |
| 429 |
assert_not_reached(); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
int journal_importer_push_data(JournalImporter *imp, const char *data, size_t size) { |
| 434 |
assert(imp); |
| 435 |
assert(imp->state != IMPORTER_STATE_EOF); |
| 436 |
|
| 437 |
if (!realloc_buffer(imp, imp->filled + size)) |
| 438 |
return log_error_errno(ENOMEM, |
| 439 |
"Failed to store received data of size %zu " |
| 440 |
"(in addition to existing %zu bytes with %zu filled): %m", |
| 441 |
size, MALLOC_SIZEOF_SAFE(imp->buf), imp->filled); |
| 442 |
|
| 443 |
memcpy(imp->buf + imp->filled, data, size); |
| 444 |
imp->filled += size; |
| 445 |
|
| 446 |
return 0; |
| 447 |
} |
| 448 |
|
| 449 |
void journal_importer_drop_iovw(JournalImporter *imp) { |
| 450 |
size_t remain, target; |
| 451 |
|
| 452 |
/* This function drops processed data that along with the iovw that points at it */ |
| 453 |
|
| 454 |
iovw_done(&imp->iovw); |
| 455 |
|
| 456 |
/* possibly reset buffer position */ |
| 457 |
remain = imp->filled - imp->offset; |
| 458 |
|
| 459 |
if (remain == 0) /* no brainer */ |
| 460 |
imp->offset = imp->scanned = imp->filled = 0; |
| 461 |
else if (imp->offset > MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled && |
| 462 |
imp->offset > remain) { |
| 463 |
memcpy(imp->buf, imp->buf + imp->offset, remain); |
| 464 |
imp->offset = imp->scanned = 0; |
| 465 |
imp->filled = remain; |
| 466 |
} |
| 467 |
|
| 468 |
target = MALLOC_SIZEOF_SAFE(imp->buf); |
| 469 |
while (target > 16 * LINE_CHUNK && imp->filled < target / 2) |
| 470 |
target /= 2; |
| 471 |
if (target < MALLOC_SIZEOF_SAFE(imp->buf)) { |
| 472 |
char *tmp; |
| 473 |
size_t old_size; |
| 474 |
|
| 475 |
old_size = MALLOC_SIZEOF_SAFE(imp->buf); |
| 476 |
|
| 477 |
tmp = realloc(imp->buf, target); |
| 478 |
if (!tmp) |
| 479 |
log_warning("Failed to reallocate buffer to (smaller) size %zu", |
| 480 |
target); |
| 481 |
else { |
| 482 |
log_debug("Reallocated buffer from %zu to %zu bytes", |
| 483 |
old_size, target); |
| 484 |
imp->buf = tmp; |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
bool journal_importer_eof(const JournalImporter *imp) { |
| 490 |
return imp->state == IMPORTER_STATE_EOF; |
| 491 |
} |