]>
Commit | Line | Data |
---|---|---|
86aa7ba4 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2012 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 <time.h> | |
23 | #include <assert.h> | |
24 | #include <errno.h> | |
df50185b | 25 | #include <sys/poll.h> |
86aa7ba4 LP |
26 | |
27 | #include "logs-show.h" | |
28 | #include "log.h" | |
29 | #include "util.h" | |
30 | ||
31 | #define PRINT_THRESHOLD 128 | |
32 | ||
33 | static bool contains_unprintable(const void *p, size_t l) { | |
34 | const char *j; | |
35 | ||
36 | for (j = p; j < (const char *) p + l; j++) | |
37 | if (*j < ' ' || *j >= 127) | |
38 | return true; | |
39 | ||
40 | return false; | |
41 | } | |
42 | ||
43 | static int output_short(sd_journal *j, unsigned line, bool show_all) { | |
44 | int r; | |
45 | uint64_t realtime; | |
46 | time_t t; | |
47 | struct tm tm; | |
48 | char buf[64]; | |
49 | const void *data; | |
50 | size_t length; | |
51 | size_t n = 0; | |
52 | ||
53 | assert(j); | |
54 | ||
55 | r = sd_journal_get_realtime_usec(j, &realtime); | |
56 | if (r < 0) { | |
57 | log_error("Failed to get realtime: %s", strerror(-r)); | |
58 | return r; | |
59 | } | |
60 | ||
61 | t = (time_t) (realtime / USEC_PER_SEC); | |
62 | if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) { | |
63 | log_error("Failed to format time."); | |
64 | return -EINVAL; | |
65 | } | |
66 | ||
67 | fputs(buf, stdout); | |
68 | n += strlen(buf); | |
69 | ||
70 | if (sd_journal_get_data(j, "_HOSTNAME", &data, &length) >= 0 && | |
71 | (show_all || (!contains_unprintable(data, length) && | |
72 | length < PRINT_THRESHOLD))) { | |
73 | printf(" %.*s", (int) length - 10, ((const char*) data) + 10); | |
74 | n += length - 10 + 1; | |
75 | } | |
76 | ||
77 | if (sd_journal_get_data(j, "MESSAGE", &data, &length) >= 0) { | |
78 | if (show_all) | |
79 | printf(" %.*s", (int) length - 8, ((const char*) data) + 8); | |
80 | else if (contains_unprintable(data, length)) | |
81 | fputs(" [blob data]", stdout); | |
82 | else if (length - 8 + n < columns()) | |
83 | printf(" %.*s", (int) length - 8, ((const char*) data) + 8); | |
84 | else if (n < columns()) { | |
85 | char *e; | |
86 | ||
87 | e = ellipsize_mem((const char *) data + 8, length - 8, columns() - n - 2, 90); | |
88 | ||
89 | if (!e) | |
90 | printf(" %.*s", (int) length - 8, ((const char*) data) + 8); | |
91 | else | |
92 | printf(" %s", e); | |
93 | ||
94 | free(e); | |
95 | } | |
96 | } | |
97 | ||
98 | fputc('\n', stdout); | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
103 | static int output_verbose(sd_journal *j, unsigned line, bool show_all) { | |
104 | const void *data; | |
105 | size_t length; | |
106 | char *cursor; | |
107 | uint64_t realtime; | |
108 | char ts[FORMAT_TIMESTAMP_MAX]; | |
109 | int r; | |
110 | ||
111 | assert(j); | |
112 | ||
113 | r = sd_journal_get_realtime_usec(j, &realtime); | |
114 | if (r < 0) { | |
115 | log_error("Failed to get realtime timestamp: %s", strerror(-r)); | |
116 | return r; | |
117 | } | |
118 | ||
119 | r = sd_journal_get_cursor(j, &cursor); | |
120 | if (r < 0) { | |
121 | log_error("Failed to get cursor: %s", strerror(-r)); | |
122 | return r; | |
123 | } | |
124 | ||
125 | printf("%s [%s]\n", | |
126 | format_timestamp(ts, sizeof(ts), realtime), | |
127 | cursor); | |
128 | ||
129 | free(cursor); | |
130 | ||
131 | SD_JOURNAL_FOREACH_DATA(j, data, length) { | |
132 | if (!show_all && (length > PRINT_THRESHOLD || | |
133 | contains_unprintable(data, length))) { | |
134 | const char *c; | |
135 | ||
136 | c = memchr(data, '=', length); | |
137 | if (!c) { | |
138 | log_error("Invalid field."); | |
139 | return -EINVAL; | |
140 | } | |
141 | ||
142 | printf("\t%.*s=[blob data]\n", | |
143 | (int) (c - (const char*) data), | |
144 | (const char*) data); | |
145 | } else | |
146 | printf("\t%.*s\n", (int) length, (const char*) data); | |
147 | } | |
148 | ||
149 | return 0; | |
150 | } | |
151 | ||
152 | static int output_export(sd_journal *j, unsigned line, bool show_all) { | |
153 | sd_id128_t boot_id; | |
154 | char sid[33]; | |
155 | int r; | |
156 | usec_t realtime, monotonic; | |
157 | char *cursor; | |
158 | const void *data; | |
159 | size_t length; | |
160 | ||
161 | assert(j); | |
162 | ||
163 | r = sd_journal_get_realtime_usec(j, &realtime); | |
164 | if (r < 0) { | |
165 | log_error("Failed to get realtime timestamp: %s", strerror(-r)); | |
166 | return r; | |
167 | } | |
168 | ||
169 | r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id); | |
170 | if (r < 0) { | |
171 | log_error("Failed to get monotonic timestamp: %s", strerror(-r)); | |
172 | return r; | |
173 | } | |
174 | ||
175 | r = sd_journal_get_cursor(j, &cursor); | |
176 | if (r < 0) { | |
177 | log_error("Failed to get cursor: %s", strerror(-r)); | |
178 | return r; | |
179 | } | |
180 | ||
181 | printf(".cursor=%s\n" | |
182 | ".realtime=%llu\n" | |
183 | ".monotonic=%llu\n" | |
184 | ".boot_id=%s\n", | |
185 | cursor, | |
186 | (unsigned long long) realtime, | |
187 | (unsigned long long) monotonic, | |
188 | sd_id128_to_string(boot_id, sid)); | |
189 | ||
190 | free(cursor); | |
191 | ||
192 | SD_JOURNAL_FOREACH_DATA(j, data, length) { | |
193 | ||
194 | if (contains_unprintable(data, length)) { | |
195 | const char *c; | |
196 | uint64_t le64; | |
197 | ||
198 | c = memchr(data, '=', length); | |
199 | if (!c) { | |
200 | log_error("Invalid field."); | |
201 | return -EINVAL; | |
202 | } | |
203 | ||
204 | fwrite(data, c - (const char*) data, 1, stdout); | |
205 | fputc('\n', stdout); | |
206 | le64 = htole64(length - (c - (const char*) data) - 1); | |
207 | fwrite(&le64, sizeof(le64), 1, stdout); | |
208 | fwrite(c + 1, length - (c - (const char*) data) - 1, 1, stdout); | |
209 | } else | |
210 | fwrite(data, length, 1, stdout); | |
211 | ||
212 | fputc('\n', stdout); | |
213 | } | |
214 | ||
215 | fputc('\n', stdout); | |
216 | ||
217 | return 0; | |
218 | } | |
219 | ||
220 | static void json_escape(const char* p, size_t l) { | |
221 | ||
222 | if (contains_unprintable(p, l)) { | |
223 | bool not_first = false; | |
224 | ||
225 | fputs("[ ", stdout); | |
226 | ||
227 | while (l > 0) { | |
228 | if (not_first) | |
229 | printf(", %u", (uint8_t) *p); | |
230 | else { | |
231 | not_first = true; | |
232 | printf("%u", (uint8_t) *p); | |
233 | } | |
234 | ||
235 | p++; | |
236 | l--; | |
237 | } | |
238 | ||
239 | fputs(" ]", stdout); | |
240 | } else { | |
241 | fputc('\"', stdout); | |
242 | ||
243 | while (l > 0) { | |
244 | if (*p == '"' || *p == '\\') { | |
245 | fputc('\\', stdout); | |
246 | fputc(*p, stdout); | |
247 | } else | |
248 | fputc(*p, stdout); | |
249 | ||
250 | p++; | |
251 | l--; | |
252 | } | |
253 | ||
254 | fputc('\"', stdout); | |
255 | } | |
256 | } | |
257 | ||
258 | static int output_json(sd_journal *j, unsigned line, bool show_all) { | |
259 | uint64_t realtime, monotonic; | |
260 | char *cursor; | |
261 | const void *data; | |
262 | size_t length; | |
263 | sd_id128_t boot_id; | |
264 | char sid[33]; | |
265 | int r; | |
266 | ||
267 | assert(j); | |
268 | ||
269 | r = sd_journal_get_realtime_usec(j, &realtime); | |
270 | if (r < 0) { | |
271 | log_error("Failed to get realtime timestamp: %s", strerror(-r)); | |
272 | return r; | |
273 | } | |
274 | ||
275 | r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id); | |
276 | if (r < 0) { | |
277 | log_error("Failed to get monotonic timestamp: %s", strerror(-r)); | |
278 | return r; | |
279 | } | |
280 | ||
281 | r = sd_journal_get_cursor(j, &cursor); | |
282 | if (r < 0) { | |
283 | log_error("Failed to get cursor: %s", strerror(-r)); | |
284 | return r; | |
285 | } | |
286 | ||
287 | if (line == 1) | |
288 | fputc('\n', stdout); | |
289 | else | |
290 | fputs(",\n", stdout); | |
291 | ||
292 | printf("{\n" | |
293 | "\t\".cursor\" : \"%s\",\n" | |
294 | "\t\".realtime\" : %llu,\n" | |
295 | "\t\".monotonic\" : %llu,\n" | |
296 | "\t\".boot_id\" : \"%s\"", | |
297 | cursor, | |
298 | (unsigned long long) realtime, | |
299 | (unsigned long long) monotonic, | |
300 | sd_id128_to_string(boot_id, sid)); | |
301 | ||
302 | free(cursor); | |
303 | ||
304 | SD_JOURNAL_FOREACH_DATA(j, data, length) { | |
305 | const char *c; | |
306 | ||
307 | c = memchr(data, '=', length); | |
308 | if (!c) { | |
309 | log_error("Invalid field."); | |
310 | return -EINVAL; | |
311 | } | |
312 | ||
313 | fputs(",\n\t", stdout); | |
314 | json_escape(data, c - (const char*) data); | |
315 | fputs(" : ", stdout); | |
316 | json_escape(c + 1, length - (c - (const char*) data) - 1); | |
317 | } | |
318 | ||
319 | fputs("\n}", stdout); | |
320 | fflush(stdout); | |
321 | ||
322 | return 0; | |
323 | } | |
324 | ||
325 | static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line, bool show_all) = { | |
326 | [OUTPUT_SHORT] = output_short, | |
327 | [OUTPUT_VERBOSE] = output_verbose, | |
328 | [OUTPUT_EXPORT] = output_export, | |
329 | [OUTPUT_JSON] = output_json | |
330 | }; | |
331 | ||
df50185b LP |
332 | int output_journal(sd_journal *j, OutputMode mode, unsigned line, bool show_all) { |
333 | assert(mode >= 0); | |
86aa7ba4 LP |
334 | assert(mode < _OUTPUT_MODE_MAX); |
335 | ||
336 | return output_funcs[mode](j, line, show_all); | |
337 | } | |
338 | ||
df50185b LP |
339 | int show_journal_by_unit( |
340 | const char *unit, | |
341 | OutputMode mode, | |
86aa7ba4 LP |
342 | const char *prefix, |
343 | unsigned n_columns, | |
344 | usec_t not_before, | |
345 | unsigned how_many, | |
df50185b LP |
346 | bool show_all, |
347 | bool follow) { | |
86aa7ba4 LP |
348 | |
349 | char *m = NULL; | |
350 | sd_journal *j; | |
351 | int r; | |
df50185b LP |
352 | int fd; |
353 | unsigned line = 0; | |
354 | bool need_seek = false; | |
86aa7ba4 | 355 | |
df50185b LP |
356 | assert(mode >= 0); |
357 | assert(mode < _OUTPUT_MODE_MAX); | |
358 | assert(unit); | |
359 | ||
360 | if (!endswith(unit, ".service") && | |
361 | !endswith(unit, ".socket") && | |
362 | !endswith(unit, ".mount") && | |
363 | !endswith(unit, ".swap")) | |
364 | return 0; | |
86aa7ba4 | 365 | |
df50185b | 366 | if (how_many <= 0) |
f4fb21c1 LP |
367 | return 0; |
368 | ||
86aa7ba4 LP |
369 | if (n_columns <= 0) |
370 | n_columns = columns(); | |
371 | ||
86aa7ba4 LP |
372 | if (!prefix) |
373 | prefix = ""; | |
374 | ||
df50185b | 375 | if (asprintf(&m, "_SYSTEMD_UNIT=%s", unit) < 0) { |
86aa7ba4 LP |
376 | r = -ENOMEM; |
377 | goto finish; | |
378 | } | |
379 | ||
380 | r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM_ONLY); | |
381 | if (r < 0) | |
382 | goto finish; | |
383 | ||
df50185b LP |
384 | fd = sd_journal_get_fd(j); |
385 | if (fd < 0) | |
386 | goto finish; | |
387 | ||
86aa7ba4 LP |
388 | r = sd_journal_add_match(j, m, strlen(m)); |
389 | if (r < 0) | |
390 | goto finish; | |
391 | ||
392 | r = sd_journal_seek_tail(j); | |
393 | if (r < 0) | |
394 | goto finish; | |
395 | ||
df50185b LP |
396 | r = sd_journal_previous_skip(j, how_many); |
397 | if (r < 0) | |
398 | goto finish; | |
86aa7ba4 | 399 | |
df50185b LP |
400 | if (mode == OUTPUT_JSON) { |
401 | fputc('[', stdout); | |
402 | fflush(stdout); | |
403 | } | |
86aa7ba4 | 404 | |
df50185b LP |
405 | for (;;) { |
406 | for (;;) { | |
407 | usec_t usec; | |
408 | ||
409 | if (need_seek) { | |
410 | r = sd_journal_next(j); | |
411 | if (r < 0) | |
412 | goto finish; | |
413 | } | |
414 | ||
415 | if (r == 0) | |
416 | break; | |
86aa7ba4 | 417 | |
df50185b LP |
418 | need_seek = true; |
419 | ||
420 | if (not_before > 0) { | |
421 | r = sd_journal_get_monotonic_usec(j, &usec, NULL); | |
422 | ||
423 | /* -ESTALE is returned if the | |
424 | timestamp is not from this boot */ | |
425 | if (r == -ESTALE) | |
426 | continue; | |
427 | else if (r < 0) | |
428 | goto finish; | |
429 | ||
430 | if (usec < not_before) | |
431 | continue; | |
432 | } | |
433 | ||
434 | line ++; | |
435 | ||
436 | r = output_journal(j, mode, line, show_all); | |
437 | if (r < 0) | |
438 | goto finish; | |
439 | } | |
440 | ||
441 | if (!follow) | |
86aa7ba4 LP |
442 | break; |
443 | ||
df50185b | 444 | r = fd_wait_for_event(fd, POLLIN); |
86aa7ba4 LP |
445 | if (r < 0) |
446 | goto finish; | |
df50185b LP |
447 | |
448 | r = sd_journal_process(j); | |
449 | if (r < 0) | |
450 | goto finish; | |
451 | ||
86aa7ba4 LP |
452 | } |
453 | ||
df50185b LP |
454 | if (mode == OUTPUT_JSON) |
455 | fputs("\n]\n", stdout); | |
456 | ||
86aa7ba4 LP |
457 | finish: |
458 | if (m) | |
459 | free(m); | |
460 | ||
461 | if (j) | |
462 | sd_journal_close(j); | |
463 | ||
464 | return r; | |
465 | } | |
df50185b LP |
466 | |
467 | static const char *const output_mode_table[_OUTPUT_MODE_MAX] = { | |
468 | [OUTPUT_SHORT] = "short", | |
469 | [OUTPUT_VERBOSE] = "verbose", | |
470 | [OUTPUT_EXPORT] = "export", | |
471 | [OUTPUT_JSON] = "json" | |
472 | }; | |
473 | ||
474 | DEFINE_STRING_TABLE_LOOKUP(output_mode, OutputMode); |