/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include "sd-id128.h"
#include "sd-journal.h"
#include "alloc-util.h"
#include "fd-util.h"
#include "formats-util.h"
#include "hashmap.h"
#include "hostname-util.h"
#include "io-util.h"
#include "journal-internal.h"
#include "log.h"
#include "logs-show.h"
#include "macro.h"
#include "output-mode.h"
#include "parse-util.h"
#include "process-util.h"
#include "sparse-endian.h"
#include "string-table.h"
#include "string-util.h"
#include "terminal-util.h"
#include "time-util.h"
#include "utf8.h"
#include "util.h"
/* up to three lines (each up to 100 characters) or 300 characters, whichever is less */
int r;
r = sd_journal_get_catalog(j, &t);
if (r < 0)
return r;
if (!z)
return log_oom();
fputs("-- ", f);
fputs(z, f);
fputc('\n', f);
return 0;
}
static int parse_field(const void *data, size_t length, const char *field, char **target, size_t *target_size) {
char *buf;
return 0;
return 0;
if (!buf)
return log_oom();
if (target_size)
*target_size = nl;
return 1;
}
assert(p);
if (flags & OUTPUT_SHOW_ALL)
return true;
if (l >= PRINT_CHAR_THRESHOLD)
return false;
if (!utf8_is_printable(p, l))
return false;
return true;
}
static bool print_multiline(FILE *f, unsigned prefix, unsigned n_columns, OutputFlags flags, int priority, const char* message, size_t message_len) {
bool ellipsized = false;
int line = 0;
if (flags & OUTPUT_COLOR) {
} else if (priority <= LOG_NOTICE) {
}
}
/* A special case: make sure that we print a newline when
the message is empty. */
if (message_len == 0)
fputs("\n", f);
bool tail_line;
int len;
;
/* We need to figure out when we are showing not-last line, *and*
* will skip subsequent lines. In that case, we will put the dots
* at the end of the line, instead of putting dots in the middle
* or not at all.
*/
fprintf(f, "%*s%s%.*s%s\n",
continue;
}
/* Beyond this point, ellipsization will happen. */
ellipsized = true;
fprintf(f, "%*s%s%.*s...%s\n",
else {
_cleanup_free_ char *e;
if (!e)
fprintf(f, "%*s%s%.*s%s\n",
else
fprintf(f, "%*s%s%s%s\n",
}
} else
fputs("...\n", f);
if (tail_line)
break;
}
return ellipsized;
}
static int output_short(
FILE *f,
sd_journal *j,
unsigned n_columns,
OutputFlags flags) {
int r;
const void *data;
size_t n = 0;
_cleanup_free_ char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = NULL, *priority = NULL;
size_t hostname_len = 0, identifier_len = 0, comm_len = 0, pid_len = 0, fake_pid_len = 0, message_len = 0, realtime_len = 0, monotonic_len = 0, priority_len = 0;
int p = LOG_INFO;
bool ellipsized = false;
assert(f);
assert(j);
/* Set the threshold to one bigger than the actual print
* threshold, so that if the line is actually longer than what
* we're willing to print, ellipsization will occur. This way
* we won't output a misleading line without any indication of
* truncation.
*/
sd_journal_set_data_threshold(j, flags & (OUTPUT_SHOW_ALL|OUTPUT_FULL_WIDTH) ? 0 : PRINT_CHAR_THRESHOLD + 1);
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to get journal fields: %m");
if (!message) {
log_debug("Skipping message without MESSAGE= field.");
return 0;
}
if (!(flags & OUTPUT_SHOW_ALL))
p = *priority - '0';
if (mode == OUTPUT_SHORT_MONOTONIC) {
uint64_t t;
r = -ENOENT;
if (monotonic)
r = safe_atou64(monotonic, &t);
if (r < 0)
r = sd_journal_get_monotonic_usec(j, &t, &boot_id);
if (r < 0)
return log_error_errno(r, "Failed to get monotonic timestamp: %m");
fprintf(f, "[%5llu.%06llu]",
(unsigned long long) (t / USEC_PER_SEC),
(unsigned long long) (t % USEC_PER_SEC));
n += 1 + 5 + 1 + 6 + 1;
} else {
uint64_t x;
time_t t;
r = -ENOENT;
if (realtime)
r = safe_atou64(realtime, &x);
if (r < 0)
r = sd_journal_get_realtime_usec(j, &x);
if (r < 0)
return log_error_errno(r, "Failed to get realtime timestamp: %m");
t = (time_t) (x / USEC_PER_SEC);
switch(mode) {
case OUTPUT_SHORT_ISO:
break;
case OUTPUT_SHORT_PRECISE:
if (r > 0)
".%06llu", (unsigned long long) (x % USEC_PER_SEC));
break;
default:
}
if (r <= 0) {
log_error("Failed to format time.");
return -EINVAL;
}
}
n += hostname_len + 1;
}
n += identifier_len + 1;
n += comm_len + 1;
} else
fputs(" unknown", f);
n += pid_len + 2;
n += fake_pid_len + 2;
}
} else {
fputs(": ", f);
ellipsized |=
}
if (flags & OUTPUT_CATALOG)
print_catalog(f, j);
return ellipsized;
}
static int output_verbose(
FILE *f,
sd_journal *j,
unsigned n_columns,
OutputFlags flags) {
const void *data;
int r;
assert(f);
assert(j);
if (r == -ENOENT)
log_debug("Source realtime timestamp not found");
else if (r < 0)
return log_full_errno(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR, r, "Failed to get source realtime timestamp: %m");
else {
if (r < 0)
return r;
assert(r > 0);
if (r < 0)
log_debug_errno(r, "Failed to parse realtime timestamp: %m");
}
if (r < 0) {
r = sd_journal_get_realtime_usec(j, &realtime);
if (r < 0)
return log_full_errno(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR, r, "Failed to get realtime timestamp: %m");
}
r = sd_journal_get_cursor(j, &cursor);
if (r < 0)
return log_error_errno(r, "Failed to get cursor: %m");
fprintf(f, "%s [%s]\n",
flags & OUTPUT_UTC ?
cursor);
const char *c;
int fieldlen;
if (!c) {
log_error("Invalid field.");
return -EINVAL;
}
on = ANSI_HIGHLIGHT;
off = ANSI_NORMAL;
}
if (flags & OUTPUT_SHOW_ALL ||
} else {
fprintf(f, " %s%.*s=[%s blob data]%s\n",
on,
(int) (c - (const char*) data),
(const char*) data,
off);
}
}
if (r < 0)
return r;
if (flags & OUTPUT_CATALOG)
print_catalog(f, j);
return 0;
}
static int output_export(
FILE *f,
sd_journal *j,
unsigned n_columns,
OutputFlags flags) {
int r;
const void *data;
assert(j);
r = sd_journal_get_realtime_usec(j, &realtime);
if (r < 0)
return log_error_errno(r, "Failed to get realtime timestamp: %m");
if (r < 0)
return log_error_errno(r, "Failed to get monotonic timestamp: %m");
r = sd_journal_get_cursor(j, &cursor);
if (r < 0)
return log_error_errno(r, "Failed to get cursor: %m");
fprintf(f,
"__CURSOR=%s\n"
"_BOOT_ID=%s\n",
/* We already printed the boot id, from the data in
* the header, hence let's suppress it here */
if (length >= 9 &&
continue;
else {
const char *c;
if (!c) {
log_error("Invalid field.");
return -EINVAL;
}
fputc('\n', f);
}
fputc('\n', f);
}
if (r < 0)
return r;
fputc('\n', f);
return 0;
}
void json_escape(
FILE *f,
const char* p,
size_t l,
OutputFlags flags) {
assert(f);
assert(p);
fputs("null", f);
else if (!utf8_is_printable(p, l)) {
bool not_first = false;
fputs("[ ", f);
while (l > 0) {
if (not_first)
else {
not_first = true;
}
p++;
l--;
}
fputs(" ]", f);
} else {
fputc('\"', f);
while (l > 0) {
if (*p == '"' || *p == '\\') {
fputc('\\', f);
fputc(*p, f);
} else if (*p == '\n')
fputs("\\n", f);
else if ((uint8_t) *p < ' ')
else
fputc(*p, f);
p++;
l--;
}
fputc('\"', f);
}
}
static int output_json(
FILE *f,
sd_journal *j,
unsigned n_columns,
OutputFlags flags) {
const void *data;
int r;
assert(j);
r = sd_journal_get_realtime_usec(j, &realtime);
if (r < 0)
return log_error_errno(r, "Failed to get realtime timestamp: %m");
if (r < 0)
return log_error_errno(r, "Failed to get monotonic timestamp: %m");
r = sd_journal_get_cursor(j, &cursor);
if (r < 0)
return log_error_errno(r, "Failed to get cursor: %m");
if (mode == OUTPUT_JSON_PRETTY)
fprintf(f,
"{\n"
"\t\"__CURSOR\" : \"%s\",\n"
"\t\"_BOOT_ID\" : \"%s\"",
else {
if (mode == OUTPUT_JSON_SSE)
fputs("data: ", f);
fprintf(f,
"{ \"__CURSOR\" : \"%s\", "
"\"_BOOT_ID\" : \"%s\"",
}
h = hashmap_new(&string_hash_ops);
if (!h)
return log_oom();
/* First round, iterate through the entry and count how often each field appears */
const char *eq;
char *n;
unsigned u;
if (length >= 9 &&
continue;
if (!eq)
continue;
if (!n) {
r = log_oom();
goto finish;
}
u = PTR_TO_UINT(hashmap_get(h, n));
if (u == 0) {
if (r < 0) {
free(n);
log_oom();
goto finish;
}
} else {
free(n);
if (r < 0) {
log_oom();
goto finish;
}
}
}
if (r < 0)
return r;
separator = true;
do {
done = true;
const char *eq;
char *kk, *n;
size_t m;
unsigned u;
/* We already printed the boot id, from the data in
* the header, hence let's suppress it here */
if (length >= 9 &&
continue;
if (!eq)
continue;
if (separator) {
if (mode == OUTPUT_JSON_PRETTY)
fputs(",\n\t", f);
else
fputs(", ", f);
}
if (!n) {
r = log_oom();
goto finish;
}
if (u == 0) {
/* We already printed this, let's jump to the next */
free(n);
separator = false;
continue;
} else if (u == 1) {
/* Field only appears once, output it directly */
fputs(" : ", f);
hashmap_remove(h, n);
free(n);
separator = true;
continue;
} else {
/* Field appears multiple times, output it as array */
fputs(" : [ ", f);
/* Iterate through the end of the list */
if (length < m + 1)
continue;
continue;
if (((const char*) data)[m] != '=')
continue;
fputs(", ", f);
}
fputs(" ]", f);
hashmap_remove(h, n);
free(n);
/* Iterate data fields form the beginning */
done = false;
separator = true;
break;
}
}
} while (!done);
if (mode == OUTPUT_JSON_PRETTY)
fputs("\n}\n", f);
else if (mode == OUTPUT_JSON_SSE)
fputs("}\n\n", f);
else
fputs(" }\n", f);
r = 0;
while ((k = hashmap_steal_first_key(h)))
free(k);
hashmap_free(h);
return r;
}
static int output_cat(
FILE *f,
sd_journal *j,
unsigned n_columns,
OutputFlags flags) {
const void *data;
size_t l;
int r;
assert(j);
assert(f);
if (r < 0) {
/* An entry without MESSAGE=? */
if (r == -ENOENT)
return 0;
return log_error_errno(r, "Failed to get data: %m");
}
assert(l >= 8);
fputc('\n', f);
return 0;
}
FILE *f,
sd_journal*j,
unsigned n_columns,
OutputFlags flags) = {
[OUTPUT_SHORT] = output_short,
[OUTPUT_JSON] = output_json,
[OUTPUT_CAT] = output_cat
};
int output_journal(
FILE *f,
sd_journal *j,
unsigned n_columns,
bool *ellipsized) {
int ret;
if (n_columns <= 0)
if (ellipsized && ret > 0)
*ellipsized = true;
return ret;
}
assert(f);
if (!(*flags & OUTPUT_BEGIN_NEWLINE))
return 0;
/* Print a beginning new line if that's request, but only once
* on the first line we print. */
fputc('\n', f);
*flags &= ~OUTPUT_BEGIN_NEWLINE;
return 0;
}
sd_journal *j,
unsigned n_columns,
unsigned how_many,
bool *ellipsized) {
int r;
unsigned line = 0;
bool need_seek = false;
assert(j);
/* Seek to end */
r = sd_journal_seek_tail(j);
if (r < 0)
return log_error_errno(r, "Failed to seek to tail: %m");
r = sd_journal_previous_skip(j, how_many);
if (r < 0)
return log_error_errno(r, "Failed to skip previous: %m");
for (;;) {
for (;;) {
if (need_seek) {
r = sd_journal_next(j);
if (r < 0)
return log_error_errno(r, "Failed to iterate through journal: %m");
}
if (r == 0)
break;
need_seek = true;
if (not_before > 0) {
/* -ESTALE is returned if the
timestamp is not from this boot */
if (r == -ESTALE)
continue;
else if (r < 0)
return log_error_errno(r, "Failed to get journal time: %m");
if (usec < not_before)
continue;
}
line ++;
if (r < 0)
return r;
}
/* Check whether the cutoff line is too early */
r = sd_id128_get_boot(&boot_id);
if (r < 0)
return log_error_errno(r, "Failed to get boot id: %m");
if (r < 0)
return log_error_errno(r, "Failed to get journal cutoff time: %m");
if (r > 0 && not_before < cutoff) {
fprintf(f, "Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n");
}
warn_cutoff = false;
}
if (!(flags & OUTPUT_FOLLOW))
break;
r = sd_journal_wait(j, USEC_INFINITY);
if (r < 0)
return log_error_errno(r, "Failed to wait for journal: %m");
}
return 0;
}
int r;
assert(j);
(void)(
/* Look for messages from the service itself */
(r = sd_journal_add_match(j, m1, 0)) ||
/* Look for coredumps of the service */
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
(r = sd_journal_add_match(j, "_UID=0", 0)) ||
(r = sd_journal_add_match(j, m2, 0)) ||
/* Look for messages from PID 1 about this service */
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j, "_PID=1", 0)) ||
(r = sd_journal_add_match(j, m3, 0)) ||
/* Look for messages from authorized daemons about this service */
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j, "_UID=0", 0)) ||
(r = sd_journal_add_match(j, m4, 0))
);
/* Show all messages belonging to a slice */
(void)(
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j, m5, 0))
);
}
return r;
}
int r;
assert(j);
(void) (
/* Look for messages from the user service itself */
(r = sd_journal_add_match(j, m1, 0)) ||
(r = sd_journal_add_match(j, muid, 0)) ||
/* Look for messages from systemd about this service */
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j, m2, 0)) ||
(r = sd_journal_add_match(j, muid, 0)) ||
/* Look for coredumps of the service */
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j, m3, 0)) ||
(r = sd_journal_add_match(j, muid, 0)) ||
(r = sd_journal_add_match(j, "_UID=0", 0)) ||
/* Look for messages from authorized daemons about this service */
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j, m4, 0)) ||
(r = sd_journal_add_match(j, muid, 0)) ||
(r = sd_journal_add_match(j, "_UID=0", 0))
);
/* Show all messages belonging to a slice */
(void)(
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j, m5, 0)) ||
(r = sd_journal_add_match(j, muid, 0))
);
}
return r;
}
ssize_t k;
int r;
if (!machine_name_is_valid(machine))
return -EINVAL;
if (r < 0)
return r;
if (r < 0)
return r;
return -errno;
if (child < 0)
return -errno;
if (child == 0) {
int fd;
if (r < 0)
if (fd < 0)
safe_close(fd);
if (r < 0)
if (k != 36)
}
return r < 0 ? r : -EIO;
if (k != 36)
return -EIO;
buf[36] = 0;
if (r < 0)
return r;
return 0;
}
int r;
assert(j);
if (machine) {
if (r < 0)
} else {
r = sd_id128_get_boot(&boot_id);
if (r < 0)
return log_error_errno(r, "Failed to get boot id: %m");
}
if (r < 0)
return log_error_errno(r, "Failed to add match: %m");
r = sd_journal_add_conjunction(j);
if (r < 0)
return log_error_errno(r, "Failed to add conjunction: %m");
return 0;
}
int show_journal_by_unit(
FILE *f,
const char *unit,
unsigned n_columns,
unsigned how_many,
int journal_open_flags,
bool system_unit,
bool *ellipsized) {
int r;
if (how_many <= 0)
return 0;
r = sd_journal_open(&j, journal_open_flags);
if (r < 0)
return log_error_errno(r, "Failed to open journal: %m");
r = add_match_this_boot(j, NULL);
if (r < 0)
return r;
if (system_unit)
r = add_matches_for_unit(j, unit);
else
if (r < 0)
return log_error_errno(r, "Failed to add unit matches: %m");
if (!filter)
return log_oom();
}
}
[OUTPUT_SHORT] = "short",
[OUTPUT_SHORT_ISO] = "short-iso",
[OUTPUT_SHORT_PRECISE] = "short-precise",
[OUTPUT_SHORT_MONOTONIC] = "short-monotonic",
[OUTPUT_VERBOSE] = "verbose",
[OUTPUT_EXPORT] = "export",
[OUTPUT_JSON] = "json",
[OUTPUT_JSON_PRETTY] = "json-pretty",
[OUTPUT_JSON_SSE] = "json-sse",
[OUTPUT_CAT] = "cat"
};