Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */ 2 : 3 : #include <fcntl.h> 4 : #include <sys/socket.h> 5 : #include <time.h> 6 : 7 : #include "alloc-util.h" 8 : #include "fd-util.h" 9 : #include "fileio.h" 10 : #include "format-util.h" 11 : #include "io-util.h" 12 : #include "journald-console.h" 13 : #include "journald-server.h" 14 : #include "parse-util.h" 15 : #include "process-util.h" 16 : #include "stdio-util.h" 17 : #include "terminal-util.h" 18 : 19 0 : static bool prefix_timestamp(void) { 20 : 21 : static int cached_printk_time = -1; 22 : 23 0 : if (_unlikely_(cached_printk_time < 0)) { 24 0 : _cleanup_free_ char *p = NULL; 25 : 26 0 : cached_printk_time = 27 0 : read_one_line_file("/sys/module/printk/parameters/time", &p) >= 0 28 0 : && parse_boolean(p) > 0; 29 : } 30 : 31 0 : return cached_printk_time; 32 : } 33 : 34 0 : void server_forward_console( 35 : Server *s, 36 : int priority, 37 : const char *identifier, 38 : const char *message, 39 : const struct ucred *ucred) { 40 : 41 : struct iovec iovec[5]; 42 : struct timespec ts; 43 : char tbuf[STRLEN("[] ") + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1]; 44 : char header_pid[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t)]; 45 0 : _cleanup_free_ char *ident_buf = NULL; 46 0 : _cleanup_close_ int fd = -1; 47 : const char *tty; 48 0 : int n = 0; 49 : 50 0 : assert(s); 51 0 : assert(message); 52 : 53 0 : if (LOG_PRI(priority) > s->max_level_console) 54 0 : return; 55 : 56 : /* First: timestamp */ 57 0 : if (prefix_timestamp()) { 58 0 : assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0); 59 0 : xsprintf(tbuf, "[%5"PRI_TIME".%06"PRI_NSEC"] ", 60 : ts.tv_sec, 61 : (nsec_t)ts.tv_nsec / 1000); 62 : 63 0 : iovec[n++] = IOVEC_MAKE_STRING(tbuf); 64 : } 65 : 66 : /* Second: identifier and PID */ 67 0 : if (ucred) { 68 0 : if (!identifier) { 69 0 : get_process_comm(ucred->pid, &ident_buf); 70 0 : identifier = ident_buf; 71 : } 72 : 73 0 : xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid); 74 : 75 0 : if (identifier) 76 0 : iovec[n++] = IOVEC_MAKE_STRING(identifier); 77 : 78 0 : iovec[n++] = IOVEC_MAKE_STRING(header_pid); 79 0 : } else if (identifier) { 80 0 : iovec[n++] = IOVEC_MAKE_STRING(identifier); 81 0 : iovec[n++] = IOVEC_MAKE_STRING(": "); 82 : } 83 : 84 : /* Fourth: message */ 85 0 : iovec[n++] = IOVEC_MAKE_STRING(message); 86 0 : iovec[n++] = IOVEC_MAKE_STRING("\n"); 87 : 88 0 : tty = s->tty_path ?: "/dev/console"; 89 : 90 : /* Before you ask: yes, on purpose we open/close the console for each log line we write individually. This is a 91 : * good strategy to avoid journald getting killed by the kernel's SAK concept (it doesn't fix this entirely, 92 : * but minimizes the time window the kernel might end up killing journald due to SAK). It also makes things 93 : * easier for us so that we don't have to recover from hangups and suchlike triggered on the console. */ 94 : 95 0 : fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC); 96 0 : if (fd < 0) { 97 0 : log_debug_errno(fd, "Failed to open %s for logging: %m", tty); 98 0 : return; 99 : } 100 : 101 0 : if (writev(fd, iovec, n) < 0) 102 0 : log_debug_errno(errno, "Failed to write to %s for logging: %m", tty); 103 : }