Debian Codegraph @

src/network/networkctl-description.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-varlink.h"
4
5 #include "alloc-util.h"
6 #include "glob-util.h"
7 #include "json-util.h"
8 #include "log.h"
9 #include "networkctl.h"
10 #include "networkctl-description.h"
11 #include "networkctl-util.h"
12 #include "stdio-util.h"
13 #include "strv.h"
14 #include "varlink-util.h"
15
16 static int dump_manager_description(sd_varlink *vl) {
17 sd_json_variant *v;
18 int r;
19
20 assert(vl);
21
22 r = varlink_call_and_log(vl, "io.systemd.Network.Describe", /* parameters= */ NULL, &v);
23 if (r < 0)
24 return r;
25
26 r = sd_json_variant_dump(v, arg_json_format_flags, /* f= */ NULL, /* prefix= */ NULL);
27 if (r < 0)
28 return log_error_errno(r, "Failed to dump json object: %m");
29 return 0;
30 }
31
32 static int dump_link_description(sd_varlink *vl, char * const *patterns) {
33 _cleanup_free_ bool *matched_patterns = NULL;
34 sd_json_variant *i, *v;
35 size_t c = 0;
36 int r;
37
38 assert(vl);
39 assert(patterns);
40
41 r = varlink_call_and_log(vl, "io.systemd.Network.Describe", /* parameters= */ NULL, &v);
42 if (r < 0)
43 return r;
44
45 matched_patterns = new0(bool, strv_length(patterns));
46 if (!matched_patterns)
47 return log_oom();
48
49 JSON_VARIANT_ARRAY_FOREACH(i, sd_json_variant_by_key(v, "Interfaces")) {
50 char ifindex_str[DECIMAL_STR_MAX(int64_t)];
51 const char *name;
52 int64_t index;
53 size_t pos;
54
55 name = sd_json_variant_string(sd_json_variant_by_key(i, "Name"));
56 index = sd_json_variant_integer(sd_json_variant_by_key(i, "Index"));
57 xsprintf(ifindex_str, "%" PRIi64, index);
58
59 if (!strv_fnmatch_full(patterns, ifindex_str, 0, &pos) &&
60 !strv_fnmatch_full(patterns, name, 0, &pos)) {
61 bool match = false;
62 sd_json_variant *a;
63
64 JSON_VARIANT_ARRAY_FOREACH(a, sd_json_variant_by_key(i, "AlternativeNames"))
65 if (strv_fnmatch_full(patterns, sd_json_variant_string(a), 0, &pos)) {
66 match = true;
67 break;
68 }
69
70 if (!match)
71 continue;
72 }
73
74 matched_patterns[pos] = true;
75 sd_json_variant_dump(i, arg_json_format_flags, NULL, NULL);
76 c++;
77 }
78
79 /* Look if we matched all our arguments that are not globs. It is OK for a glob to match
80 * nothing, but not for an exact argument. */
81 for (size_t pos = 0; pos < strv_length(patterns); pos++) {
82 if (matched_patterns[pos])
83 continue;
84
85 if (string_is_glob(patterns[pos]))
86 log_debug("Pattern \"%s\" doesn't match any interface, ignoring.",
87 patterns[pos]);
88 else
89 return log_error_errno(SYNTHETIC_ERRNO(ENODEV),
90 "Interface \"%s\" not found.", patterns[pos]);
91 }
92
93 if (c == 0)
94 log_warning("No interfaces matched.");
95
96 return 0;
97 }
98
99 int dump_description(int argc, char *argv[]) {
100 _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL;
101 int r;
102
103 if (!sd_json_format_enabled(arg_json_format_flags))
104 return 0;
105
106 r = varlink_connect_networkd(&vl);
107 if (r < 0)
108 return r;
109
110 if (arg_all || argc <= 1)
111 r = dump_manager_description(vl);
112 else
113 r = dump_link_description(vl, strv_skip(argv, 1));
114 if (r < 0)
115 return r;
116
117 return 1; /* done */
118 }