chiark / gitweb /
journalctl: add -n switch
[elogind.git] / src / journal / journalctl.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <sys/poll.h>
30 #include <time.h>
31 #include <getopt.h>
32
33 #include "sd-journal.h"
34 #include "log.h"
35 #include "util.h"
36 #include "build.h"
37 #include "pager.h"
38 #include "logs-show.h"
39
40 static output_mode arg_output = OUTPUT_SHORT;
41
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
47 static int help(void) {
48
49         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
50                "Send control commands to or query the login manager.\n\n"
51                "  -h --help           Show this help\n"
52                "     --version        Show package version\n"
53                "     --no-pager       Do not pipe output into a pager\n"
54                "  -a --all            Show all properties, including long and unprintable\n"
55                "  -f --follow         Follow journal\n"
56                "  -n --lines=INTEGER  Lines to show\n"
57                "  -o --output=STRING  Change output mode (short, verbose, export, json)\n",
58                program_invocation_short_name);
59
60         return 0;
61 }
62
63 static int parse_argv(int argc, char *argv[]) {
64
65         enum {
66                 ARG_VERSION = 0x100,
67                 ARG_NO_PAGER
68         };
69
70         static const struct option options[] = {
71                 { "help",      no_argument,       NULL, 'h'           },
72                 { "version" ,  no_argument,       NULL, ARG_VERSION   },
73                 { "no-pager",  no_argument,       NULL, ARG_NO_PAGER  },
74                 { "follow",    no_argument,       NULL, 'f'           },
75                 { "output",    required_argument, NULL, 'o'           },
76                 { "all",       no_argument,       NULL, 'a'           },
77                 { "lines",     required_argument, NULL, 'n'           },
78                 { NULL,        0,                 NULL, 0             }
79         };
80
81         int c, r;
82
83         assert(argc >= 0);
84         assert(argv);
85
86         while ((c = getopt_long(argc, argv, "hfo:an:", options, NULL)) >= 0) {
87
88                 switch (c) {
89
90                 case 'h':
91                         help();
92                         return 0;
93
94                 case ARG_VERSION:
95                         puts(PACKAGE_STRING);
96                         puts(DISTRIBUTION);
97                         puts(SYSTEMD_FEATURES);
98                         return 0;
99
100                 case ARG_NO_PAGER:
101                         arg_no_pager = true;
102                         break;
103
104                 case 'f':
105                         arg_follow = true;
106                         break;
107
108                 case 'o':
109                         if (streq(optarg, "short"))
110                                 arg_output = OUTPUT_SHORT;
111                         else if (streq(optarg, "verbose"))
112                                 arg_output = OUTPUT_VERBOSE;
113                         else if (streq(optarg, "export"))
114                                 arg_output = OUTPUT_EXPORT;
115                         else if (streq(optarg, "json"))
116                                 arg_output = OUTPUT_JSON;
117                         else {
118                                 log_error("Unknown output '%s'.", optarg);
119                                 return -EINVAL;
120                         }
121                         break;
122
123                 case 'a':
124                         arg_show_all = true;
125                         break;
126
127                 case 'n':
128                         r = safe_atoi(optarg, &arg_lines);
129                         if (r < 0) {
130                                 log_error("Failed to parse lines '%s'", optarg);
131                                 return -EINVAL;
132                         }
133                         break;
134
135                 case '?':
136                         return -EINVAL;
137
138                 default:
139                         log_error("Unknown option code %c", c);
140                         return -EINVAL;
141                 }
142         }
143
144         return 1;
145 }
146
147 int main(int argc, char *argv[]) {
148         int r, i, fd;
149         sd_journal *j = NULL;
150         unsigned line = 0;
151
152         log_parse_environment();
153         log_open();
154
155         r = parse_argv(argc, argv);
156         if (r <= 0)
157                 goto finish;
158
159         r = sd_journal_open(&j, 0);
160         if (r < 0) {
161                 log_error("Failed to open journal: %s", strerror(-r));
162                 goto finish;
163         }
164
165         for (i = optind; i < argc; i++) {
166                 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
167                 if (r < 0) {
168                         log_error("Failed to add match: %s", strerror(-r));
169                         goto finish;
170                 }
171         }
172
173         fd = sd_journal_get_fd(j);
174         if (fd < 0) {
175                 log_error("Failed to get wakeup fd: %s", strerror(-fd));
176                 goto finish;
177         }
178
179         if (arg_lines >= 0) {
180                 r = sd_journal_seek_tail(j);
181                 if (r < 0) {
182                         log_error("Failed to seek to tail: %s", strerror(-r));
183                         goto finish;
184                 }
185
186                 r = sd_journal_previous_skip(j, arg_lines);
187                 if (r < 0) {
188                         log_error("Failed to iterate through journal: %s", strerror(-r));
189                         goto finish;
190                 }
191         } else {
192                 r = sd_journal_seek_head(j);
193                 if (r < 0) {
194                         log_error("Failed to seek to head: %s", strerror(-r));
195                         goto finish;
196                 }
197         }
198
199         if (!arg_no_pager && !arg_follow) {
200                 columns();
201                 pager_open();
202         }
203
204         if (arg_output == OUTPUT_JSON) {
205                 fputc('[', stdout);
206                 fflush(stdout);
207         }
208
209         for (;;) {
210                 struct pollfd pollfd;
211
212                 for (;;) {
213                         r = sd_journal_next(j);
214
215                         if (r < 0) {
216                                 log_error("Failed to iterate through journal: %s", strerror(-r));
217                                 goto finish;
218                         }
219
220                         if (r == 0)
221                                 break;
222
223                         line ++;
224
225                         r = output_journal(j, arg_output, line, arg_show_all);
226                         if (r < 0)
227                                 goto finish;
228                 }
229
230                 if (!arg_follow)
231                         break;
232
233                 zero(pollfd);
234                 pollfd.fd = fd;
235                 pollfd.events = POLLIN;
236
237                 if (poll(&pollfd, 1, -1) < 0) {
238                         if (errno == EINTR)
239                                 break;
240
241                         log_error("poll(): %m");
242                         r = -errno;
243                         goto finish;
244                 }
245
246                 r = sd_journal_process(j);
247                 if (r < 0) {
248                         log_error("Failed to process: %s", strerror(-r));
249                         goto finish;
250                 }
251         }
252
253         if (arg_output == OUTPUT_JSON)
254                 fputs("\n]\n", stdout);
255
256 finish:
257         if (j)
258                 sd_journal_close(j);
259
260         pager_close();
261
262         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
263 }