chiark / gitweb /
socket: add option for SO_PASSEC
[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 <systemd/sd-journal.h>
34
35 #include "log.h"
36 #include "util.h"
37 #include "build.h"
38 #include "pager.h"
39 #include "logs-show.h"
40
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
49 static int help(void) {
50
51         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
52                "Send control commands to or query the journal.\n\n"
53                "  -h --help           Show this help\n"
54                "     --version        Show package version\n"
55                "     --no-pager       Do not pipe output into a pager\n"
56                "  -a --all            Show all properties, including long and unprintable\n"
57                "  -f --follow         Follow journal\n"
58                "  -n --lines=INTEGER  Journal entries to show\n"
59                "     --no-tail        Show all lines, even in follow mode\n"
60                "  -o --output=STRING  Change journal output mode (short, short-monotonic,\n"
61                "                      verbose, export, json, cat)\n"
62                "     --new-id128      Generate a new 128 Bit id\n",
63                program_invocation_short_name);
64
65         return 0;
66 }
67
68 static int parse_argv(int argc, char *argv[]) {
69
70         enum {
71                 ARG_VERSION = 0x100,
72                 ARG_NO_PAGER,
73                 ARG_NO_TAIL,
74                 ARG_NEW_ID128
75         };
76
77         static const struct option options[] = {
78                 { "help",      no_argument,       NULL, 'h'           },
79                 { "version" ,  no_argument,       NULL, ARG_VERSION   },
80                 { "no-pager",  no_argument,       NULL, ARG_NO_PAGER  },
81                 { "follow",    no_argument,       NULL, 'f'           },
82                 { "output",    required_argument, NULL, 'o'           },
83                 { "all",       no_argument,       NULL, 'a'           },
84                 { "lines",     required_argument, NULL, 'n'           },
85                 { "no-tail",   no_argument,       NULL, ARG_NO_TAIL   },
86                 { "new-id128", no_argument,       NULL, ARG_NEW_ID128 },
87                 { NULL,        0,                 NULL, 0             }
88         };
89
90         int c, r;
91
92         assert(argc >= 0);
93         assert(argv);
94
95         while ((c = getopt_long(argc, argv, "hfo:an:", options, NULL)) >= 0) {
96
97                 switch (c) {
98
99                 case 'h':
100                         help();
101                         return 0;
102
103                 case ARG_VERSION:
104                         puts(PACKAGE_STRING);
105                         puts(DISTRIBUTION);
106                         puts(SYSTEMD_FEATURES);
107                         return 0;
108
109                 case ARG_NO_PAGER:
110                         arg_no_pager = true;
111                         break;
112
113                 case 'f':
114                         arg_follow = true;
115                         break;
116
117                 case 'o':
118                         arg_output =  output_mode_from_string(optarg);
119                         if (arg_output < 0) {
120                                 log_error("Unknown output '%s'.", optarg);
121                                 return -EINVAL;
122                         }
123
124                         break;
125
126                 case 'a':
127                         arg_show_all = true;
128                         break;
129
130                 case 'n':
131                         r = safe_atoi(optarg, &arg_lines);
132                         if (r < 0 || arg_lines < 0) {
133                                 log_error("Failed to parse lines '%s'", optarg);
134                                 return -EINVAL;
135                         }
136                         break;
137
138                 case ARG_NO_TAIL:
139                         arg_no_tail = true;
140                         break;
141
142                 case ARG_NEW_ID128:
143                         arg_new_id128 = true;
144                         break;
145
146                 case '?':
147                         return -EINVAL;
148
149                 default:
150                         log_error("Unknown option code %c", c);
151                         return -EINVAL;
152                 }
153         }
154
155         if (arg_follow && !arg_no_tail && arg_lines < 0)
156                 arg_lines = 10;
157
158         return 1;
159 }
160
161 static int generate_new_id128(void) {
162         sd_id128_t id;
163         int r;
164         unsigned i;
165
166         r = sd_id128_randomize(&id);
167         if (r < 0) {
168                 log_error("Failed to generate ID: %s", strerror(-r));
169                 return r;
170         }
171
172         printf("As string:\n"
173                SD_ID128_FORMAT_STR "\n\n"
174                "As UUID:\n"
175                "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n\n"
176                "As macro:\n"
177               "#define MESSAGE_XYZ SD_ID128_MAKE(",
178                SD_ID128_FORMAT_VAL(id),
179                SD_ID128_FORMAT_VAL(id));
180
181         for (i = 0; i < 16; i++)
182                 printf("%02x%s", id.bytes[i], i != 15 ? "," : "");
183
184         fputs(")\n", stdout);
185
186         return 0;
187 }
188
189 int main(int argc, char *argv[]) {
190         int r, i, fd;
191         sd_journal *j = NULL;
192         unsigned line = 0;
193         bool need_seek = false;
194
195         log_parse_environment();
196         log_open();
197
198         r = parse_argv(argc, argv);
199         if (r <= 0)
200                 goto finish;
201
202         if (arg_new_id128) {
203                 r = generate_new_id128();
204                 goto finish;
205         }
206
207         r = sd_journal_open(&j, 0);
208         if (r < 0) {
209                 log_error("Failed to open journal: %s", strerror(-r));
210                 goto finish;
211         }
212
213         for (i = optind; i < argc; i++) {
214                 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
215                 if (r < 0) {
216                         log_error("Failed to add match: %s", strerror(-r));
217                         goto finish;
218                 }
219         }
220
221         fd = sd_journal_get_fd(j);
222         if (fd < 0) {
223                 log_error("Failed to get wakeup fd: %s", strerror(-fd));
224                 goto finish;
225         }
226
227         if (arg_lines >= 0) {
228                 r = sd_journal_seek_tail(j);
229                 if (r < 0) {
230                         log_error("Failed to seek to tail: %s", strerror(-r));
231                         goto finish;
232                 }
233
234                 r = sd_journal_previous_skip(j, arg_lines);
235         } else {
236                 r = sd_journal_seek_head(j);
237                 if (r < 0) {
238                         log_error("Failed to seek to head: %s", strerror(-r));
239                         goto finish;
240                 }
241
242                 r = sd_journal_next(j);
243         }
244
245         if (r < 0) {
246                 log_error("Failed to iterate through journal: %s", strerror(-r));
247                 goto finish;
248         }
249
250         if (!arg_no_pager && !arg_follow) {
251                 columns();
252                 pager_open();
253         }
254
255         if (arg_output == OUTPUT_JSON) {
256                 fputc('[', stdout);
257                 fflush(stdout);
258         }
259
260         for (;;) {
261                 for (;;) {
262                         if (need_seek) {
263                                 r = sd_journal_next(j);
264                                 if (r < 0) {
265                                         log_error("Failed to iterate through journal: %s", strerror(-r));
266                                         goto finish;
267                                 }
268                         }
269
270                         if (r == 0)
271                                 break;
272
273                         line ++;
274
275                         r = output_journal(j, arg_output, line, arg_show_all);
276                         if (r < 0)
277                                 goto finish;
278
279                         need_seek = true;
280                 }
281
282                 if (!arg_follow)
283                         break;
284
285                 r = fd_wait_for_event(fd, POLLIN, (usec_t) -1);
286                 if (r < 0) {
287                         log_error("Couldn't wait for event: %s", strerror(-r));
288                         goto finish;
289                 }
290
291                 r = sd_journal_process(j);
292                 if (r < 0) {
293                         log_error("Failed to process: %s", strerror(-r));
294                         goto finish;
295                 }
296         }
297
298         if (arg_output == OUTPUT_JSON)
299                 fputs("\n]\n", stdout);
300
301 finish:
302         if (j)
303                 sd_journal_close(j);
304
305         pager_close();
306
307         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
308 }