| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/mman.h> |
| 5 |
#include <sys/socket.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "sd-device.h" |
| 9 |
#include "sd-event.h" |
| 10 |
#include "sd-messages.h" |
| 11 |
|
| 12 |
#include "alloc-util.h" |
| 13 |
#include "device-util.h" |
| 14 |
#include "errno-util.h" |
| 15 |
#include "escape.h" |
| 16 |
#include "fd-util.h" |
| 17 |
#include "format-util.h" |
| 18 |
#include "iovec-util.h" |
| 19 |
#include "journal-internal.h" |
| 20 |
#include "journald-kmsg.h" |
| 21 |
#include "journald-manager.h" |
| 22 |
#include "journald-sync.h" |
| 23 |
#include "journald-syslog.h" |
| 24 |
#include "log.h" |
| 25 |
#include "log-ratelimit.h" |
| 26 |
#include "parse-util.h" |
| 27 |
#include "process-util.h" |
| 28 |
#include "stdio-util.h" |
| 29 |
#include "string-util.h" |
| 30 |
|
| 31 |
void manager_forward_kmsg( |
| 32 |
Manager *m, |
| 33 |
int priority, |
| 34 |
const char *identifier, |
| 35 |
const char *message, |
| 36 |
const struct ucred *ucred) { |
| 37 |
|
| 38 |
_cleanup_free_ char *ident_buf = NULL; |
| 39 |
struct iovec iovec[5]; |
| 40 |
char header_priority[DECIMAL_STR_MAX(priority) + 3], |
| 41 |
header_pid[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t) + 1]; |
| 42 |
size_t n = 0; |
| 43 |
|
| 44 |
assert(m); |
| 45 |
assert(priority >= 0); |
| 46 |
assert(priority <= 999); |
| 47 |
assert(message); |
| 48 |
|
| 49 |
if (_unlikely_(LOG_PRI(priority) > m->config.max_level_kmsg)) |
| 50 |
return; |
| 51 |
|
| 52 |
if (_unlikely_(m->dev_kmsg_fd < 0)) |
| 53 |
return; |
| 54 |
|
| 55 |
/* Never allow messages with kernel facility to be written to |
| 56 |
* kmsg, regardless where the data comes from. */ |
| 57 |
priority = syslog_fixup_facility(priority); |
| 58 |
|
| 59 |
/* First: priority field */ |
| 60 |
xsprintf(header_priority, "<%i>", priority); |
| 61 |
iovec[n++] = IOVEC_MAKE_STRING(header_priority); |
| 62 |
|
| 63 |
/* Second: identifier and PID */ |
| 64 |
if (ucred) { |
| 65 |
if (!identifier) { |
| 66 |
(void) pid_get_comm(ucred->pid, &ident_buf); |
| 67 |
identifier = ident_buf; |
| 68 |
} |
| 69 |
|
| 70 |
if (identifier) |
| 71 |
iovec[n++] = IOVEC_MAKE_STRING(identifier); |
| 72 |
|
| 73 |
xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid); |
| 74 |
iovec[n++] = IOVEC_MAKE_STRING(header_pid); |
| 75 |
} else if (identifier) { |
| 76 |
iovec[n++] = IOVEC_MAKE_STRING(identifier); |
| 77 |
iovec[n++] = IOVEC_MAKE_STRING(": "); |
| 78 |
} |
| 79 |
|
| 80 |
/* Fourth: message */ |
| 81 |
iovec[n++] = IOVEC_MAKE_STRING(message); |
| 82 |
iovec[n++] = IOVEC_MAKE_STRING("\n"); |
| 83 |
|
| 84 |
if (writev(m->dev_kmsg_fd, iovec, n) < 0) |
| 85 |
log_debug_errno(errno, "Failed to write to /dev/kmsg for logging, ignoring: %m"); |
| 86 |
} |
| 87 |
|
| 88 |
static bool is_us(const char *identifier, pid_t pid) { |
| 89 |
if (!identifier || !pid_is_valid(pid)) |
| 90 |
return false; |
| 91 |
|
| 92 |
return pid == getpid_cached() && |
| 93 |
streq(identifier, program_invocation_short_name); |
| 94 |
} |
| 95 |
|
| 96 |
void dev_kmsg_record(Manager *m, char *p, size_t l) { |
| 97 |
|
| 98 |
_cleanup_free_ char *message = NULL, *syslog_identifier = NULL; |
| 99 |
struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS + 2 + N_IOVEC_UDEV_FIELDS]; |
| 100 |
char *kernel_device = NULL; |
| 101 |
unsigned long long usec; |
| 102 |
size_t n = 0, z = 0, j; |
| 103 |
int priority, r; |
| 104 |
char *e, *k, syslog_pid[STRLEN("SYSLOG_PID=") + DECIMAL_STR_MAX(pid_t)]; |
| 105 |
uint64_t serial; |
| 106 |
size_t pl; |
| 107 |
int saved_log_max_level = INT_MAX; |
| 108 |
ClientContext *c = NULL; |
| 109 |
|
| 110 |
assert(m); |
| 111 |
assert(p); |
| 112 |
|
| 113 |
if (l <= 0) |
| 114 |
return; |
| 115 |
|
| 116 |
/* syslog prefix including priority and facility */ |
| 117 |
e = memchr(p, ',', l); |
| 118 |
if (!e) |
| 119 |
return; |
| 120 |
*e = 0; |
| 121 |
|
| 122 |
r = safe_atoi(p, &priority); |
| 123 |
if (r < 0 || priority < 0 || priority > 999) |
| 124 |
return; |
| 125 |
|
| 126 |
if (m->config.forward_to_kmsg && LOG_FAC(priority) != LOG_KERN) |
| 127 |
return; |
| 128 |
|
| 129 |
/* seqnum */ |
| 130 |
l -= (e - p) + 1; |
| 131 |
p = e + 1; |
| 132 |
e = memchr(p, ',', l); |
| 133 |
if (!e) |
| 134 |
return; |
| 135 |
*e = 0; |
| 136 |
|
| 137 |
r = safe_atou64(p, &serial); |
| 138 |
if (r < 0) |
| 139 |
return; |
| 140 |
|
| 141 |
if (m->kernel_seqnum) { |
| 142 |
/* We already read this one? */ |
| 143 |
if (serial < *m->kernel_seqnum) |
| 144 |
return; |
| 145 |
|
| 146 |
/* Did we lose any? */ |
| 147 |
if (serial > *m->kernel_seqnum) |
| 148 |
manager_driver_message(m, 0, |
| 149 |
LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_MISSED_STR), |
| 150 |
LOG_MESSAGE("Missed %"PRIu64" kernel messages", |
| 151 |
serial - *m->kernel_seqnum)); |
| 152 |
|
| 153 |
/* Make sure we never read this one again. Note that |
| 154 |
* we always store the next message serial we expect |
| 155 |
* here, simply because this makes handling the first |
| 156 |
* message with serial 0 easy. */ |
| 157 |
*m->kernel_seqnum = serial + 1; |
| 158 |
} |
| 159 |
|
| 160 |
/* CLOCK_BOOTTIME timestamp */ |
| 161 |
l -= (e - p) + 1; |
| 162 |
p = e + 1; |
| 163 |
e = memchr(p, ',', l); |
| 164 |
if (!e) |
| 165 |
return; |
| 166 |
*e = 0; |
| 167 |
|
| 168 |
r = safe_atollu(p, &usec); |
| 169 |
if (r < 0) |
| 170 |
return; |
| 171 |
|
| 172 |
/* ignore flags and any other fields, and find the beginning of the message */ |
| 173 |
l -= (e - p) + 1; |
| 174 |
p = e + 1; |
| 175 |
e = memchr(p, ';', l); |
| 176 |
if (!e) |
| 177 |
return; |
| 178 |
|
| 179 |
/* find the end of the message */ |
| 180 |
l -= (e - p) + 1; |
| 181 |
p = e + 1; |
| 182 |
e = memchr(p, '\n', l); |
| 183 |
if (!e) |
| 184 |
return; |
| 185 |
*e = 0; |
| 186 |
|
| 187 |
pl = e - p; |
| 188 |
l -= (e - p) + 1; |
| 189 |
k = e + 1; |
| 190 |
|
| 191 |
for (j = 0; l > 0 && j < N_IOVEC_KERNEL_FIELDS; j++) { |
| 192 |
char *mm; |
| 193 |
/* Metadata fields attached */ |
| 194 |
|
| 195 |
if (*k != ' ') |
| 196 |
break; |
| 197 |
|
| 198 |
k++, l--; |
| 199 |
|
| 200 |
e = memchr(k, '\n', l); |
| 201 |
if (!e) |
| 202 |
goto finish; |
| 203 |
|
| 204 |
*e = 0; |
| 205 |
|
| 206 |
if (cunescape_length_with_prefix(k, e - k, "_KERNEL_", UNESCAPE_RELAX, &mm) < 0) |
| 207 |
break; |
| 208 |
|
| 209 |
if (startswith(mm, "_KERNEL_DEVICE=")) |
| 210 |
kernel_device = mm + 15; |
| 211 |
|
| 212 |
iovec[n++] = IOVEC_MAKE_STRING(mm); |
| 213 |
z++; |
| 214 |
|
| 215 |
l -= (e - k) + 1; |
| 216 |
k = e + 1; |
| 217 |
} |
| 218 |
|
| 219 |
if (kernel_device) { |
| 220 |
_cleanup_(sd_device_unrefp) sd_device *d = NULL; |
| 221 |
|
| 222 |
if (sd_device_new_from_device_id(&d, kernel_device) >= 0) { |
| 223 |
const char *g; |
| 224 |
char *b; |
| 225 |
|
| 226 |
if (sd_device_get_devname(d, &g) >= 0) { |
| 227 |
b = strjoin("_UDEV_DEVNODE=", g); |
| 228 |
if (b) { |
| 229 |
iovec[n++] = IOVEC_MAKE_STRING(b); |
| 230 |
z++; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
if (sd_device_get_sysname(d, &g) >= 0) { |
| 235 |
b = strjoin("_UDEV_SYSNAME=", g); |
| 236 |
if (b) { |
| 237 |
iovec[n++] = IOVEC_MAKE_STRING(b); |
| 238 |
z++; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
j = 0; |
| 243 |
FOREACH_DEVICE_DEVLINK(d, link) { |
| 244 |
|
| 245 |
if (j >= N_IOVEC_UDEV_FIELDS) |
| 246 |
break; |
| 247 |
|
| 248 |
b = strjoin("_UDEV_DEVLINK=", link); |
| 249 |
if (b) { |
| 250 |
iovec[n++] = IOVEC_MAKE_STRING(b); |
| 251 |
z++; |
| 252 |
} |
| 253 |
|
| 254 |
j++; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
char source_boot_time[STRLEN("_SOURCE_BOOTTIME_TIMESTAMP=") + DECIMAL_STR_MAX(unsigned long long)]; |
| 260 |
xsprintf(source_boot_time, "_SOURCE_BOOTTIME_TIMESTAMP=%llu", usec); |
| 261 |
iovec[n++] = IOVEC_MAKE_STRING(source_boot_time); |
| 262 |
|
| 263 |
/* Historically, we stored the timestamp 'usec' as _SOURCE_MONOTONIC_TIMESTAMP, so we cannot remove |
| 264 |
* the field as it is already used in other projects. This is for backward compatibility. */ |
| 265 |
char source_monotonic_time[STRLEN("_SOURCE_MONOTONIC_TIMESTAMP=") + DECIMAL_STR_MAX(unsigned long long)]; |
| 266 |
xsprintf(source_monotonic_time, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec); |
| 267 |
iovec[n++] = IOVEC_MAKE_STRING(source_monotonic_time); |
| 268 |
|
| 269 |
iovec[n++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel"); |
| 270 |
|
| 271 |
char syslog_priority[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int)]; |
| 272 |
xsprintf(syslog_priority, "PRIORITY=%i", LOG_PRI(priority)); |
| 273 |
iovec[n++] = IOVEC_MAKE_STRING(syslog_priority); |
| 274 |
|
| 275 |
char syslog_facility[STRLEN("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)]; |
| 276 |
xsprintf(syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)); |
| 277 |
iovec[n++] = IOVEC_MAKE_STRING(syslog_facility); |
| 278 |
|
| 279 |
if (LOG_FAC(priority) == LOG_KERN) |
| 280 |
iovec[n++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel"); |
| 281 |
else { |
| 282 |
_cleanup_free_ char *identifier = NULL; |
| 283 |
pid_t pid; |
| 284 |
|
| 285 |
pl -= syslog_parse_identifier((const char**) &p, &identifier, &pid); |
| 286 |
|
| 287 |
/* Avoid logging any new messages when we're processing messages generated by ourselves via |
| 288 |
* log_info() and friends to avoid infinite loops. */ |
| 289 |
if (is_us(identifier, pid)) { |
| 290 |
if (!ratelimit_below(&m->kmsg_own_ratelimit)) |
| 291 |
return; |
| 292 |
|
| 293 |
saved_log_max_level = log_get_max_level(); |
| 294 |
c = m->my_context; |
| 295 |
log_set_max_level(LOG_NULL); |
| 296 |
} |
| 297 |
|
| 298 |
if (identifier) { |
| 299 |
syslog_identifier = strjoin("SYSLOG_IDENTIFIER=", identifier); |
| 300 |
if (syslog_identifier) |
| 301 |
iovec[n++] = IOVEC_MAKE_STRING(syslog_identifier); |
| 302 |
} |
| 303 |
|
| 304 |
if (pid_is_valid(pid)) { |
| 305 |
xsprintf(syslog_pid, "SYSLOG_PID="PID_FMT, pid); |
| 306 |
iovec[n++] = IOVEC_MAKE_STRING(syslog_pid); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
if (cunescape_length_with_prefix(p, pl, "MESSAGE=", UNESCAPE_RELAX, &message) >= 0) |
| 311 |
iovec[n++] = IOVEC_MAKE_STRING(message); |
| 312 |
|
| 313 |
manager_dispatch_message(m, iovec, n, ELEMENTSOF(iovec), c, NULL, priority, 0); |
| 314 |
|
| 315 |
if (saved_log_max_level != INT_MAX) |
| 316 |
log_set_max_level(saved_log_max_level); |
| 317 |
|
| 318 |
m->dev_kmsg_timestamp = usec; |
| 319 |
sync_req_revalidate_by_timestamp(m); |
| 320 |
|
| 321 |
finish: |
| 322 |
for (j = 0; j < z; j++) |
| 323 |
free(iovec[j].iov_base); |
| 324 |
} |
| 325 |
|
| 326 |
static int manager_read_dev_kmsg(Manager *m) { |
| 327 |
char buffer[8192+1]; /* the kernel-side limit per record is 8K currently */ |
| 328 |
ssize_t l; |
| 329 |
|
| 330 |
assert(m); |
| 331 |
assert(m->dev_kmsg_fd >= 0); |
| 332 |
assert(m->config.read_kmsg); |
| 333 |
|
| 334 |
l = read(m->dev_kmsg_fd, buffer, sizeof(buffer) - 1); |
| 335 |
if (l == 0) |
| 336 |
return 0; |
| 337 |
if (l < 0) { |
| 338 |
if (ERRNO_IS_TRANSIENT(errno) || errno == EPIPE) |
| 339 |
return 0; |
| 340 |
|
| 341 |
return log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT, "Failed to read from /dev/kmsg: %m"); |
| 342 |
} |
| 343 |
|
| 344 |
dev_kmsg_record(m, buffer, l); |
| 345 |
return 1; |
| 346 |
} |
| 347 |
|
| 348 |
int manager_flush_dev_kmsg(Manager *m) { |
| 349 |
int r; |
| 350 |
|
| 351 |
assert(m); |
| 352 |
|
| 353 |
if (m->dev_kmsg_fd < 0) |
| 354 |
return 0; |
| 355 |
|
| 356 |
if (!m->config.read_kmsg) |
| 357 |
return 0; |
| 358 |
|
| 359 |
log_debug("Flushing /dev/kmsg..."); |
| 360 |
|
| 361 |
for (;;) { |
| 362 |
r = manager_read_dev_kmsg(m); |
| 363 |
if (r <= 0) |
| 364 |
return r; |
| 365 |
} |
| 366 |
|
| 367 |
return 0; |
| 368 |
} |
| 369 |
|
| 370 |
static int dispatch_dev_kmsg(sd_event_source *es, int fd, uint32_t revents, void *userdata) { |
| 371 |
Manager *m = ASSERT_PTR(userdata); |
| 372 |
|
| 373 |
assert(es); |
| 374 |
assert(fd == m->dev_kmsg_fd); |
| 375 |
|
| 376 |
if (revents & EPOLLERR) |
| 377 |
log_ratelimit_warning(JOURNAL_LOG_RATELIMIT, |
| 378 |
"/dev/kmsg buffer overrun, some messages lost."); |
| 379 |
|
| 380 |
if (!(revents & EPOLLIN)) |
| 381 |
log_error("Got invalid event from epoll for /dev/kmsg: %"PRIx32, revents); |
| 382 |
|
| 383 |
return manager_read_dev_kmsg(m); |
| 384 |
} |
| 385 |
|
| 386 |
int manager_open_dev_kmsg(Manager *m) { |
| 387 |
int r; |
| 388 |
|
| 389 |
assert(m); |
| 390 |
assert(m->dev_kmsg_fd < 0); |
| 391 |
assert(!m->dev_kmsg_event_source); |
| 392 |
|
| 393 |
mode_t mode = O_CLOEXEC|O_NONBLOCK|O_NOCTTY|(m->config.read_kmsg ? O_RDWR : O_WRONLY); |
| 394 |
|
| 395 |
_cleanup_close_ int fd = open("/dev/kmsg", mode); |
| 396 |
if (fd < 0) { |
| 397 |
log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, |
| 398 |
errno, "Failed to open /dev/kmsg for %s access, ignoring: %m", accmode_to_string(mode)); |
| 399 |
return 0; |
| 400 |
} |
| 401 |
|
| 402 |
if (!m->config.read_kmsg) { |
| 403 |
m->dev_kmsg_fd = TAKE_FD(fd); |
| 404 |
return 0; |
| 405 |
} |
| 406 |
|
| 407 |
_cleanup_(sd_event_source_unrefp) sd_event_source *es = NULL; |
| 408 |
r = sd_event_add_io(m->event, &es, fd, EPOLLIN, dispatch_dev_kmsg, m); |
| 409 |
if (r < 0) |
| 410 |
return log_error_errno(r, "Failed to add /dev/kmsg fd to event loop: %m"); |
| 411 |
|
| 412 |
r = sd_event_source_set_priority(es, SD_EVENT_PRIORITY_NORMAL+5); |
| 413 |
if (r < 0) |
| 414 |
return log_error_errno(r, "Failed to adjust priority of kmsg event source: %m"); |
| 415 |
|
| 416 |
m->dev_kmsg_fd = TAKE_FD(fd); |
| 417 |
m->dev_kmsg_event_source = TAKE_PTR(es); |
| 418 |
return 0; |
| 419 |
} |
| 420 |
|
| 421 |
int manager_open_kernel_seqnum(Manager *m) { |
| 422 |
int r; |
| 423 |
|
| 424 |
assert(m); |
| 425 |
assert(!m->kernel_seqnum); |
| 426 |
|
| 427 |
/* We store the seqnum we last read in an mmapped file. That way we can just use it like a variable, |
| 428 |
* but it is persistent and automatically flushed at reboot. */ |
| 429 |
|
| 430 |
if (!m->config.read_kmsg) |
| 431 |
return 0; |
| 432 |
|
| 433 |
r = manager_map_seqnum_file(m, "kernel-seqnum", sizeof(uint64_t), (void**) &m->kernel_seqnum); |
| 434 |
if (r < 0) |
| 435 |
return log_error_errno(r, "Failed to map kernel seqnum file: %m"); |
| 436 |
|
| 437 |
return 0; |
| 438 |
} |
| 439 |
|
| 440 |
void manager_close_kernel_seqnum(Manager *m) { |
| 441 |
assert(m); |
| 442 |
|
| 443 |
manager_unmap_seqnum_file(m->kernel_seqnum, sizeof(*m->kernel_seqnum)); |
| 444 |
m->kernel_seqnum = NULL; |
| 445 |
} |
| 446 |
|
| 447 |
static int manager_unlink_kernel_seqnum(Manager *m) { |
| 448 |
assert(m); |
| 449 |
assert(!m->kernel_seqnum); /* The file must not be mmap()ed. */ |
| 450 |
|
| 451 |
return manager_unlink_seqnum_file(m, "kernel-seqnum"); |
| 452 |
} |
| 453 |
|
| 454 |
int manager_reopen_dev_kmsg(Manager *m, bool old_read_kmsg) { |
| 455 |
int r; |
| 456 |
|
| 457 |
assert(m); |
| 458 |
|
| 459 |
/* If the fd has not yet been initialized, let's shortcut and simply open /dev/kmsg. */ |
| 460 |
if (m->dev_kmsg_fd < 0) |
| 461 |
return manager_open_dev_kmsg(m); |
| 462 |
|
| 463 |
if (m->config.read_kmsg == old_read_kmsg) |
| 464 |
return 0; /* Setting is unchanged. */ |
| 465 |
|
| 466 |
if (!m->config.read_kmsg) { |
| 467 |
/* If reading kmsg was enabled but now disable, let's flush the buffer before disabling it. */ |
| 468 |
m->config.read_kmsg = true; |
| 469 |
(void) manager_flush_dev_kmsg(m); |
| 470 |
m->config.read_kmsg = false; |
| 471 |
|
| 472 |
/* seqnum file is not necessary anymore. Let's close it. */ |
| 473 |
manager_close_kernel_seqnum(m); |
| 474 |
|
| 475 |
/* Also, unlink the file name as we will not warn some kmsg are lost when reading kmsg is |
| 476 |
* re-enabled later. */ |
| 477 |
manager_unlink_kernel_seqnum(m); |
| 478 |
} |
| 479 |
|
| 480 |
/* Close previously configured event source and opened file descriptor. */ |
| 481 |
m->dev_kmsg_event_source = sd_event_source_disable_unref(m->dev_kmsg_event_source); |
| 482 |
m->dev_kmsg_fd = safe_close(m->dev_kmsg_fd); |
| 483 |
|
| 484 |
r = manager_open_dev_kmsg(m); |
| 485 |
if (r < 0) |
| 486 |
return r; |
| 487 |
|
| 488 |
return manager_open_kernel_seqnum(m); |
| 489 |
} |