1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
34 #include <systemd/sd-journal.h>
38 #include "path-util.h"
41 #include "logs-show.h"
43 #include "journal-internal.h"
45 static OutputMode arg_output = OUTPUT_SHORT;
46 static bool arg_follow = false;
47 static bool arg_show_all = false;
48 static bool arg_no_pager = false;
49 static int arg_lines = -1;
50 static bool arg_no_tail = false;
51 static bool arg_new_id128 = false;
52 static bool arg_print_header = false;
53 static bool arg_quiet = false;
54 static bool arg_local = false;
55 static bool arg_this_boot = false;
56 static const char *arg_directory = NULL;
58 static int help(void) {
60 printf("%s [OPTIONS...] [MATCH]\n\n"
61 "Send control commands to or query the journal.\n\n"
62 " -h --help Show this help\n"
63 " --version Show package version\n"
64 " --no-pager Do not pipe output into a pager\n"
65 " -a --all Show all fields, including long and unprintable\n"
66 " -f --follow Follow journal\n"
67 " -n --lines=INTEGER Journal entries to show\n"
68 " --no-tail Show all lines, even in follow mode\n"
69 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
70 " verbose, export, json, cat)\n"
71 " -q --quiet Don't show privilege warning\n"
72 " -l --local Only local entries\n"
73 " -b --this-boot Show data only from current boot\n"
74 " -D --directory=PATH Show journal files from directory\n"
75 " --header Show journal header information\n"
76 " --new-id128 Generate a new 128 Bit id\n",
77 program_invocation_short_name);
82 static int parse_argv(int argc, char *argv[]) {
92 static const struct option options[] = {
93 { "help", no_argument, NULL, 'h' },
94 { "version" , no_argument, NULL, ARG_VERSION },
95 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
96 { "follow", no_argument, NULL, 'f' },
97 { "output", required_argument, NULL, 'o' },
98 { "all", no_argument, NULL, 'a' },
99 { "lines", required_argument, NULL, 'n' },
100 { "no-tail", no_argument, NULL, ARG_NO_TAIL },
101 { "new-id128", no_argument, NULL, ARG_NEW_ID128 },
102 { "quiet", no_argument, NULL, 'q' },
103 { "local", no_argument, NULL, 'l' },
104 { "this-boot", no_argument, NULL, 'b' },
105 { "directory", required_argument, NULL, 'D' },
106 { "header", no_argument, NULL, ARG_HEADER },
115 while ((c = getopt_long(argc, argv, "hfo:an:qlbD:", options, NULL)) >= 0) {
124 puts(PACKAGE_STRING);
126 puts(SYSTEMD_FEATURES);
138 arg_output = output_mode_from_string(optarg);
139 if (arg_output < 0) {
140 log_error("Unknown output '%s'.", optarg);
151 r = safe_atoi(optarg, &arg_lines);
152 if (r < 0 || arg_lines < 0) {
153 log_error("Failed to parse lines '%s'", optarg);
163 arg_new_id128 = true;
175 arg_this_boot = true;
179 arg_directory = optarg;
183 arg_print_header = true;
190 log_error("Unknown option code %c", c);
195 if (arg_follow && !arg_no_tail && arg_lines < 0)
201 static int generate_new_id128(void) {
206 r = sd_id128_randomize(&id);
208 log_error("Failed to generate ID: %s", strerror(-r));
212 printf("As string:\n"
213 SD_ID128_FORMAT_STR "\n\n"
215 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n\n"
217 "#define MESSAGE_XYZ SD_ID128_MAKE(",
218 SD_ID128_FORMAT_VAL(id),
219 SD_ID128_FORMAT_VAL(id));
221 for (i = 0; i < 16; i++)
222 printf("%02x%s", id.bytes[i], i != 15 ? "," : "");
224 fputs(")\n", stdout);
229 static int add_matches(sd_journal *j, char **args) {
235 STRV_FOREACH(i, args) {
238 r = sd_journal_add_disjunction(j);
239 else if (path_is_absolute(*i)) {
244 p = canonicalize_file_name(*i);
247 if (stat(path, &st) < 0) {
249 log_error("Couldn't stat file: %m");
253 if (S_ISREG(st.st_mode) && (0111 & st.st_mode)) {
256 t = strappend("_EXE=", path);
259 log_error("Out of memory");
263 r = sd_journal_add_match(j, t, 0);
267 log_error("File is not a regular file or is not executable: %s", *i);
273 r = sd_journal_add_match(j, *i, 0);
276 log_error("Failed to add match '%s': %s", *i, strerror(-r));
284 static int add_this_boot(sd_journal *j) {
285 char match[9+32+1] = "_BOOT_ID=";
292 r = sd_id128_get_boot(&boot_id);
294 log_error("Failed to get boot id: %s", strerror(-r));
298 sd_id128_to_string(boot_id, match + 9);
299 r = sd_journal_add_match(j, match, strlen(match));
301 log_error("Failed to add match: %s", strerror(-r));
308 int main(int argc, char *argv[]) {
310 sd_journal *j = NULL;
312 bool need_seek = false;
313 sd_id128_t previous_boot_id;
314 bool previous_boot_id_valid = false;
317 log_parse_environment();
320 r = parse_argv(argc, argv);
325 r = generate_new_id128();
330 if (!arg_quiet && geteuid() != 0 && in_group("adm") <= 0)
331 log_warning("Showing user generated messages only. Users in the group 'adm' can see all messages. Pass -q to turn this message off.");
335 r = sd_journal_open_directory(&j, arg_directory, 0);
337 r = sd_journal_open(&j, arg_local ? SD_JOURNAL_LOCAL_ONLY : 0);
340 log_error("Failed to open journal: %s", strerror(-r));
344 if (arg_print_header) {
345 journal_print_header(j);
350 r = add_this_boot(j);
354 r = add_matches(j, argv + optind);
360 char start_buf[FORMAT_TIMESTAMP_MAX], end_buf[FORMAT_TIMESTAMP_MAX];
362 r = sd_journal_get_cutoff_realtime_usec(j, &start, &end);
364 log_error("Failed to get cutoff: %s", strerror(-r));
370 printf("Logs begin at %s.\n", format_timestamp(start_buf, sizeof(start_buf), start));
372 printf("Logs begin at %s, end at %s.\n",
373 format_timestamp(start_buf, sizeof(start_buf), start),
374 format_timestamp(end_buf, sizeof(end_buf), end));
378 if (arg_lines >= 0) {
379 r = sd_journal_seek_tail(j);
381 log_error("Failed to seek to tail: %s", strerror(-r));
385 r = sd_journal_previous_skip(j, arg_lines);
387 r = sd_journal_seek_head(j);
389 log_error("Failed to seek to head: %s", strerror(-r));
393 r = sd_journal_next(j);
397 log_error("Failed to iterate through journal: %s", strerror(-r));
401 have_pager = !arg_no_pager && !arg_follow;
407 if (arg_output == OUTPUT_JSON) {
415 int flags = (arg_show_all*OUTPUT_SHOW_ALL |
416 have_pager*OUTPUT_FULL_WIDTH);
419 r = sd_journal_next(j);
421 log_error("Failed to iterate through journal: %s", strerror(-r));
429 r = sd_journal_get_monotonic_usec(j, NULL, &boot_id);
431 if (previous_boot_id_valid &&
432 !sd_id128_equal(boot_id, previous_boot_id))
433 printf(ANSI_HIGHLIGHT_ON "----- Reboot -----" ANSI_HIGHLIGHT_OFF "\n");
435 previous_boot_id = boot_id;
436 previous_boot_id_valid = true;
441 r = output_journal(j, arg_output, line, 0, flags);
451 r = sd_journal_wait(j, (uint64_t) -1);
453 log_error("Couldn't wait for log event: %s", strerror(-r));
458 if (arg_output == OUTPUT_JSON)
459 fputs("\n]\n", stdout);
467 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;