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 General Public License as published by
10
the Free Software Foundation; either version 2 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
General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with systemd; If not, see <http://www.gnu.org/licenses/>.
33
#include <systemd/sd-journal.h>
39
#include "logs-show.h"
41
static OutputMode arg_output = OUTPUT_SHORT;
42
static bool arg_follow = false;
43
static bool arg_show_all = false;
44
static bool arg_no_pager = false;
45
static int arg_lines = -1;
46
static bool arg_no_tail = false;
47
static bool arg_new_id128 = false;
48
static bool arg_quiet = false;
50
static int help(void) {
52
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
53
"Send control commands to or query the journal.\n\n"
54
" -h --help Show this help\n"
55
" --version Show package version\n"
56
" --no-pager Do not pipe output into a pager\n"
57
" -a --all Show all fields, including long and unprintable\n"
58
" -f --follow Follow journal\n"
59
" -n --lines=INTEGER Journal entries to show\n"
60
" --no-tail Show all lines, even in follow mode\n"
61
" -o --output=STRING Change journal output mode (short, short-monotonic,\n"
62
" verbose, export, json, cat)\n"
63
" -q --quiet Don't show privilege warning\n"
64
" --new-id128 Generate a new 128 Bit id\n",
65
program_invocation_short_name);
70
static int parse_argv(int argc, char *argv[]) {
79
static const struct option options[] = {
80
{ "help", no_argument, NULL, 'h' },
81
{ "version" , no_argument, NULL, ARG_VERSION },
82
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
83
{ "follow", no_argument, NULL, 'f' },
84
{ "output", required_argument, NULL, 'o' },
85
{ "all", no_argument, NULL, 'a' },
86
{ "lines", required_argument, NULL, 'n' },
87
{ "no-tail", no_argument, NULL, ARG_NO_TAIL },
88
{ "new-id128", no_argument, NULL, ARG_NEW_ID128 },
89
{ "quiet", no_argument, NULL, 'q' },
98
while ((c = getopt_long(argc, argv, "hfo:an:q", options, NULL)) >= 0) {
107
puts(PACKAGE_STRING);
109
puts(SYSTEMD_FEATURES);
121
arg_output = output_mode_from_string(optarg);
122
if (arg_output < 0) {
123
log_error("Unknown output '%s'.", optarg);
134
r = safe_atoi(optarg, &arg_lines);
135
if (r < 0 || arg_lines < 0) {
136
log_error("Failed to parse lines '%s'", optarg);
146
arg_new_id128 = true;
157
log_error("Unknown option code %c", c);
162
if (arg_follow && !arg_no_tail && arg_lines < 0)
168
static int generate_new_id128(void) {
173
r = sd_id128_randomize(&id);
175
log_error("Failed to generate ID: %s", strerror(-r));
179
printf("As string:\n"
180
SD_ID128_FORMAT_STR "\n\n"
182
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n\n"
184
"#define MESSAGE_XYZ SD_ID128_MAKE(",
185
SD_ID128_FORMAT_VAL(id),
186
SD_ID128_FORMAT_VAL(id));
188
for (i = 0; i < 16; i++)
189
printf("%02x%s", id.bytes[i], i != 15 ? "," : "");
191
fputs(")\n", stdout);
196
int main(int argc, char *argv[]) {
198
sd_journal *j = NULL;
200
bool need_seek = false;
202
log_parse_environment();
205
r = parse_argv(argc, argv);
210
r = generate_new_id128();
215
if (!arg_quiet && geteuid() != 0 && in_group("adm") <= 0)
216
log_warning("Showing user generated messages only. Users in the group 'adm' can see all messages. Pass -q to turn this message off.");
219
r = sd_journal_open(&j, 0);
221
log_error("Failed to open journal: %s", strerror(-r));
225
for (i = optind; i < argc; i++) {
226
r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
228
log_error("Failed to add match: %s", strerror(-r));
233
fd = sd_journal_get_fd(j);
235
log_error("Failed to get wakeup fd: %s", strerror(-fd));
239
if (arg_lines >= 0) {
240
r = sd_journal_seek_tail(j);
242
log_error("Failed to seek to tail: %s", strerror(-r));
246
r = sd_journal_previous_skip(j, arg_lines);
248
r = sd_journal_seek_head(j);
250
log_error("Failed to seek to head: %s", strerror(-r));
254
r = sd_journal_next(j);
258
log_error("Failed to iterate through journal: %s", strerror(-r));
262
if (!arg_no_pager && !arg_follow) {
267
if (arg_output == OUTPUT_JSON) {
275
r = sd_journal_next(j);
277
log_error("Failed to iterate through journal: %s", strerror(-r));
287
r = output_journal(j, arg_output, line, 0, arg_show_all);
297
r = fd_wait_for_event(fd, POLLIN, (usec_t) -1);
299
log_error("Couldn't wait for event: %s", strerror(-r));
303
r = sd_journal_process(j);
305
log_error("Failed to process: %s", strerror(-r));
310
if (arg_output == OUTPUT_JSON)
311
fputs("\n]\n", stdout);
319
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;