2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-08-22 03:36:29 +02:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
2012-08-24 01:46:38 +02:00
|
|
|
#include <sys/socket.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <time.h>
|
2012-08-22 03:36:29 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "fd-util.h"
|
2013-11-13 15:27:19 +01:00
|
|
|
#include "fileio.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2015-10-27 01:02:30 +01:00
|
|
|
#include "io-util.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "journald-console.h"
|
|
|
|
#include "journald-server.h"
|
|
|
|
#include "parse-util.h"
|
2015-04-10 19:10:00 +02:00
|
|
|
#include "process-util.h"
|
2015-10-27 01:26:31 +01:00
|
|
|
#include "stdio-util.h"
|
2015-04-10 23:15:59 +02:00
|
|
|
#include "terminal-util.h"
|
2012-08-22 03:36:29 +02:00
|
|
|
|
2013-11-13 15:27:19 +01:00
|
|
|
static bool prefix_timestamp(void) {
|
|
|
|
|
|
|
|
static int cached_printk_time = -1;
|
|
|
|
|
|
|
|
if (_unlikely_(cached_printk_time < 0)) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
|
|
|
|
cached_printk_time =
|
|
|
|
read_one_line_file("/sys/module/printk/parameters/time", &p) >= 0
|
|
|
|
&& parse_boolean(p) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cached_printk_time;
|
|
|
|
}
|
|
|
|
|
2012-08-22 03:36:29 +02:00
|
|
|
void server_forward_console(
|
|
|
|
Server *s,
|
|
|
|
int priority,
|
|
|
|
const char *identifier,
|
|
|
|
const char *message,
|
2014-11-02 21:46:42 +01:00
|
|
|
const struct ucred *ucred) {
|
2012-08-22 03:36:29 +02:00
|
|
|
|
2013-11-13 15:27:19 +01:00
|
|
|
struct iovec iovec[5];
|
|
|
|
struct timespec ts;
|
2017-12-14 19:02:29 +01:00
|
|
|
char tbuf[STRLEN("[] ") + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1];
|
|
|
|
char header_pid[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t)];
|
2014-02-16 00:10:36 +01:00
|
|
|
_cleanup_free_ char *ident_buf = NULL;
|
2017-09-21 13:52:34 +02:00
|
|
|
_cleanup_close_ int fd = -1;
|
2012-08-22 03:36:29 +02:00
|
|
|
const char *tty;
|
2017-09-21 13:52:34 +02:00
|
|
|
int n = 0;
|
2012-08-22 03:36:29 +02:00
|
|
|
|
|
|
|
assert(s);
|
|
|
|
assert(message);
|
|
|
|
|
|
|
|
if (LOG_PRI(priority) > s->max_level_console)
|
|
|
|
return;
|
|
|
|
|
2013-11-13 15:27:19 +01:00
|
|
|
/* First: timestamp */
|
|
|
|
if (prefix_timestamp()) {
|
|
|
|
assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
|
2017-03-22 21:34:32 +01:00
|
|
|
xsprintf(tbuf, "[%5"PRI_TIME".%06"PRI_NSEC"] ",
|
2014-04-25 13:45:15 +02:00
|
|
|
ts.tv_sec,
|
2017-03-22 21:34:32 +01:00
|
|
|
(nsec_t)ts.tv_nsec / 1000);
|
2017-09-21 13:52:34 +02:00
|
|
|
|
|
|
|
iovec[n++] = IOVEC_MAKE_STRING(tbuf);
|
2013-11-13 15:27:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Second: identifier and PID */
|
2012-08-22 03:36:29 +02:00
|
|
|
if (ucred) {
|
|
|
|
if (!identifier) {
|
|
|
|
get_process_comm(ucred->pid, &ident_buf);
|
|
|
|
identifier = ident_buf;
|
|
|
|
}
|
|
|
|
|
2015-01-27 14:00:11 +01:00
|
|
|
xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
|
2012-08-22 03:36:29 +02:00
|
|
|
|
|
|
|
if (identifier)
|
2017-09-21 13:52:34 +02:00
|
|
|
iovec[n++] = IOVEC_MAKE_STRING(identifier);
|
2012-08-22 03:36:29 +02:00
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
iovec[n++] = IOVEC_MAKE_STRING(header_pid);
|
2012-08-22 03:36:29 +02:00
|
|
|
} else if (identifier) {
|
2017-09-21 13:52:34 +02:00
|
|
|
iovec[n++] = IOVEC_MAKE_STRING(identifier);
|
|
|
|
iovec[n++] = IOVEC_MAKE_STRING(": ");
|
2012-08-22 03:36:29 +02:00
|
|
|
}
|
|
|
|
|
2013-11-13 15:27:19 +01:00
|
|
|
/* Fourth: message */
|
2017-09-21 13:52:34 +02:00
|
|
|
iovec[n++] = IOVEC_MAKE_STRING(message);
|
|
|
|
iovec[n++] = IOVEC_MAKE_STRING("\n");
|
2012-08-22 03:36:29 +02:00
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
tty = s->tty_path ?: "/dev/console";
|
2012-08-22 03:36:29 +02:00
|
|
|
|
2016-10-19 23:30:50 +02:00
|
|
|
/* Before you ask: yes, on purpose we open/close the console for each log line we write individually. This is a
|
|
|
|
* good strategy to avoid journald getting killed by the kernel's SAK concept (it doesn't fix this entirely,
|
|
|
|
* but minimizes the time window the kernel might end up killing journald due to SAK). It also makes things
|
|
|
|
* easier for us so that we don't have to recover from hangups and suchlike triggered on the console. */
|
|
|
|
|
2012-08-22 03:36:29 +02:00
|
|
|
fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
|
|
|
|
if (fd < 0) {
|
2015-11-05 13:44:06 +01:00
|
|
|
log_debug_errno(fd, "Failed to open %s for logging: %m", tty);
|
2014-02-16 00:10:36 +01:00
|
|
|
return;
|
2012-08-22 03:36:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (writev(fd, iovec, n) < 0)
|
2014-11-28 19:29:59 +01:00
|
|
|
log_debug_errno(errno, "Failed to write to %s for logging: %m", tty);
|
2012-08-22 03:36:29 +02:00
|
|
|
}
|