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 static OutputMode arg_output = OUTPUT_SHORT;
44 static bool arg_follow = false;
45 static bool arg_show_all = false;
46 static bool arg_no_pager = false;
47 static int arg_lines = -1;
48 static bool arg_no_tail = false;
49 static bool arg_new_id128 = false;
50 static bool arg_quiet = false;
51 static bool arg_local = false;
53 static int help(void) {
55 printf("%s [OPTIONS...] [MATCH]\n\n"
56 "Send control commands to or query the journal.\n\n"
57 " -h --help Show this help\n"
58 " --version Show package version\n"
59 " --no-pager Do not pipe output into a pager\n"
60 " -a --all Show all fields, including long and unprintable\n"
61 " -f --follow Follow journal\n"
62 " -n --lines=INTEGER Journal entries to show\n"
63 " --no-tail Show all lines, even in follow mode\n"
64 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
65 " verbose, export, json, cat)\n"
66 " -q --quiet Don't show privilege warning\n"
67 " --new-id128 Generate a new 128 Bit id\n"
68 " -l --local Only local entries\n",
69 program_invocation_short_name);
74 static int parse_argv(int argc, char *argv[]) {
83 static const struct option options[] = {
84 { "help", no_argument, NULL, 'h' },
85 { "version" , no_argument, NULL, ARG_VERSION },
86 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
87 { "follow", no_argument, NULL, 'f' },
88 { "output", required_argument, NULL, 'o' },
89 { "all", no_argument, NULL, 'a' },
90 { "lines", required_argument, NULL, 'n' },
91 { "no-tail", no_argument, NULL, ARG_NO_TAIL },
92 { "new-id128", no_argument, NULL, ARG_NEW_ID128 },
93 { "quiet", no_argument, NULL, 'q' },
94 { "local", no_argument, NULL, 'l' },
103 while ((c = getopt_long(argc, argv, "hfo:an:ql", options, NULL)) >= 0) {
112 puts(PACKAGE_STRING);
114 puts(SYSTEMD_FEATURES);
126 arg_output = output_mode_from_string(optarg);
127 if (arg_output < 0) {
128 log_error("Unknown output '%s'.", optarg);
139 r = safe_atoi(optarg, &arg_lines);
140 if (r < 0 || arg_lines < 0) {
141 log_error("Failed to parse lines '%s'", optarg);
151 arg_new_id128 = true;
166 log_error("Unknown option code %c", c);
171 if (arg_follow && !arg_no_tail && arg_lines < 0)
177 static int generate_new_id128(void) {
182 r = sd_id128_randomize(&id);
184 log_error("Failed to generate ID: %s", strerror(-r));
188 printf("As string:\n"
189 SD_ID128_FORMAT_STR "\n\n"
191 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n\n"
193 "#define MESSAGE_XYZ SD_ID128_MAKE(",
194 SD_ID128_FORMAT_VAL(id),
195 SD_ID128_FORMAT_VAL(id));
197 for (i = 0; i < 16; i++)
198 printf("%02x%s", id.bytes[i], i != 15 ? "," : "");
200 fputs(")\n", stdout);
205 int main(int argc, char *argv[]) {
207 sd_journal *j = NULL;
209 bool need_seek = false;
212 log_parse_environment();
215 r = parse_argv(argc, argv);
220 r = generate_new_id128();
225 if (!arg_quiet && geteuid() != 0 && in_group("adm") <= 0)
226 log_warning("Showing user generated messages only. Users in the group 'adm' can see all messages. Pass -q to turn this message off.");
229 r = sd_journal_open(&j, arg_local ? SD_JOURNAL_LOCAL_ONLY : 0);
231 log_error("Failed to open journal: %s", strerror(-r));
235 for (i = optind; i < argc; i++) {
236 if (path_is_absolute(argv[i])) {
240 p = canonicalize_file_name(argv[i]);
241 path = p ? p : argv[i];
243 if (stat(path, &st) < 0) {
245 log_error("Couldn't stat file: %m");
250 if (S_ISREG(st.st_mode) && (0111 & st.st_mode)) {
253 t = strappend("_EXE=", path);
256 log_error("Out of memory");
260 r = sd_journal_add_match(j, t, strlen(t));
264 log_error("File is not a regular file or is not executable: %s", argv[i]);
270 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
273 log_error("Failed to add match: %s", strerror(-r));
278 fd = sd_journal_get_fd(j);
280 log_error("Failed to get wakeup fd: %s", strerror(-fd));
284 if (arg_lines >= 0) {
285 r = sd_journal_seek_tail(j);
287 log_error("Failed to seek to tail: %s", strerror(-r));
291 r = sd_journal_previous_skip(j, arg_lines);
293 r = sd_journal_seek_head(j);
295 log_error("Failed to seek to head: %s", strerror(-r));
299 r = sd_journal_next(j);
303 log_error("Failed to iterate through journal: %s", strerror(-r));
307 if (!arg_no_pager && !arg_follow) {
312 if (arg_output == OUTPUT_JSON) {
320 r = sd_journal_next(j);
322 log_error("Failed to iterate through journal: %s", strerror(-r));
332 r = output_journal(j, arg_output, line, 0, arg_show_all);
342 r = fd_wait_for_event(fd, POLLIN, (usec_t) -1);
344 log_error("Couldn't wait for event: %s", strerror(-r));
348 r = sd_journal_process(j);
350 log_error("Failed to process: %s", strerror(-r));
355 if (arg_output == OUTPUT_JSON)
356 fputs("\n]\n", stdout);
364 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;