Debian Codegraph @

src/network/networkctl-journal.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-journal.h"
4
5 #include "ansi-color.h"
6 #include "journal-internal.h"
7 #include "log.h"
8 #include "logs-show.h"
9 #include "networkctl.h"
10 #include "networkctl-journal.h"
11 #include "output-mode.h"
12 #include "terminal-util.h"
13
14 static OutputFlags get_output_flags(void) {
15 return
16 arg_all * OUTPUT_SHOW_ALL |
17 (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
18 colors_enabled() * OUTPUT_COLOR;
19 }
20
21 int show_logs(int ifindex, const char *ifname) {
22 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
23 int r;
24
25 assert(ifindex == 0 || ifname);
26
27 if (arg_lines == 0)
28 return 0;
29
30 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_ASSUME_IMMUTABLE);
31 if (r < 0)
32 return log_error_errno(r, "Failed to open journal: %m");
33
34 r = add_match_this_boot(j, NULL);
35 if (r < 0)
36 return log_error_errno(r, "Failed to add boot matches: %m");
37
38 if (ifindex > 0) {
39 (void) (
40 (r = journal_add_matchf(j, "_KERNEL_DEVICE=n%i", ifindex)) || /* kernel */
41 (r = sd_journal_add_disjunction(j)) ||
42 (r = journal_add_match_pair(j, "INTERFACE", ifname)) || /* networkd */
43 (r = sd_journal_add_disjunction(j)) ||
44 (r = journal_add_match_pair(j, "DEVICE", ifname)) /* udevd */
45 );
46 if (r < 0)
47 return log_error_errno(r, "Failed to add link matches: %m");
48 } else {
49 r = add_matches_for_unit(j, "systemd-networkd.service");
50 if (r < 0)
51 return log_error_errno(r, "Failed to add unit matches: %m");
52
53 r = add_matches_for_unit(j, "systemd-networkd-wait-online.service");
54 if (r < 0)
55 return log_error_errno(r, "Failed to add unit matches: %m");
56 }
57
58 return show_journal(
59 stdout,
60 j,
61 OUTPUT_SHORT,
62 0,
63 0,
64 arg_lines,
65 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
66 NULL);
67 }