Debian Codegraph @

src/network/networkctl-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4 #include <unistd.h>
5
6 #include "ansi-color.h"
7 #include "bus-util.h"
8 #include "log.h"
9 #include "networkctl.h"
10 #include "networkctl-util.h"
11 #include "polkit-agent.h"
12 #include "string-util.h"
13 #include "strv.h"
14 #include "varlink-util.h"
15
16 int varlink_connect_networkd(sd_varlink **ret_varlink) {
17 _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL;
18 sd_json_variant *reply;
19 uint64_t id;
20 int r;
21
22 r = sd_varlink_connect_address(&vl, "/run/systemd/netif/io.systemd.Network");
23 if (r < 0)
24 return log_error_errno(r, "Failed to connect to network service /run/systemd/netif/io.systemd.Network: %m");
25
26 (void) sd_varlink_set_description(vl, "varlink-network");
27
28 r = sd_varlink_set_allow_fd_passing_output(vl, true);
29 if (r < 0)
30 return log_error_errno(r, "Failed to allow passing file descriptor through varlink: %m");
31
32 r = varlink_call_and_log(vl, "io.systemd.Network.GetNamespaceId", /* parameters= */ NULL, &reply);
33 if (r < 0)
34 return r;
35
36 static const sd_json_dispatch_field dispatch_table[] = {
37 { "NamespaceId", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, 0, SD_JSON_MANDATORY },
38 {},
39 };
40
41 r = sd_json_dispatch(reply, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &id);
42 if (r < 0)
43 return r;
44
45 if (id == 0)
46 log_debug("systemd-networkd.service not running in a network namespace (?), skipping netns check.");
47 else {
48 struct stat st;
49
50 if (stat("/proc/self/ns/net", &st) < 0)
51 return log_error_errno(errno, "Failed to determine our own network namespace ID: %m");
52
53 if (id != st.st_ino)
54 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE),
55 "networkctl must be invoked in same network namespace as systemd-networkd.service.");
56 }
57
58 if (ret_varlink)
59 *ret_varlink = TAKE_PTR(vl);
60 return 0;
61 }
62
63 int reload_networkd(void) {
64 _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL;
65 int r;
66
67 r = varlink_connect_networkd(&vl);
68 if (r < 0)
69 return r;
70
71 (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password);
72
73 return varlink_callbo_and_log(
74 vl,
75 "io.systemd.service.Reload",
76 /* reply= */ NULL,
77 SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password));
78 }
79
80 int reload_udevd(void) {
81 _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL;
82 int r;
83
84 r = sd_varlink_connect_address(&vl, "/run/udev/io.systemd.Udev");
85 if (r == -ENOENT) {
86 log_debug("systemd-udevd is not running, skipping reload.");
87 return 0;
88 }
89 if (r < 0)
90 return log_error_errno(r, "Failed to connect to udev: %m");
91
92 (void) sd_varlink_set_description(vl, "udev");
93
94 return varlink_call_and_log(vl, "io.systemd.service.Reload", /* parameters= */ NULL, /* reply= */ NULL);
95 }
96
97 bool networkd_is_running(void) {
98 static int cached = -1;
99 int r;
100
101 if (cached < 0) {
102 r = access("/run/systemd/netif/state", F_OK);
103 if (r < 0) {
104 if (errno != ENOENT)
105 log_debug_errno(errno,
106 "Failed to determine whether networkd is running, assuming it's not: %m");
107
108 cached = false;
109 } else
110 cached = true;
111 }
112
113 return cached;
114 }
115
116 void operational_state_to_color(const char *name, const char *state, const char **on, const char **off) {
117 if (STRPTR_IN_SET(state, "routable", "enslaved") ||
118 (streq_ptr(name, "lo") && streq_ptr(state, "carrier"))) {
119 if (on)
120 *on = ansi_highlight_green();
121 if (off)
122 *off = ansi_normal();
123 } else if (streq_ptr(state, "degraded")) {
124 if (on)
125 *on = ansi_highlight_yellow();
126 if (off)
127 *off = ansi_normal();
128 } else {
129 if (on)
130 *on = "";
131 if (off)
132 *off = "";
133 }
134 }
135
136 void setup_state_to_color(const char *state, const char **on, const char **off) {
137 if (streq_ptr(state, "configured")) {
138 if (on)
139 *on = ansi_highlight_green();
140 if (off)
141 *off = ansi_normal();
142 } else if (streq_ptr(state, "configuring")) {
143 if (on)
144 *on = ansi_highlight_yellow();
145 if (off)
146 *off = ansi_normal();
147 } else if (STRPTR_IN_SET(state, "failed", "linger")) {
148 if (on)
149 *on = ansi_highlight_red();
150 if (off)
151 *off = ansi_normal();
152 } else {
153 if (on)
154 *on = "";
155 if (off)
156 *off = "";
157 }
158 }
159
160 void online_state_to_color(const char *state, const char **on, const char **off) {
161 if (streq_ptr(state, "online")) {
162 if (on)
163 *on = ansi_highlight_green();
164 if (off)
165 *off = ansi_normal();
166 } else if (streq_ptr(state, "partial")) {
167 if (on)
168 *on = ansi_highlight_yellow();
169 if (off)
170 *off = ansi_normal();
171 } else {
172 if (on)
173 *on = "";
174 if (off)
175 *off = "";
176 }
177 }
178
179 int acquire_link_description(sd_varlink *vl, int ifindex, sd_json_variant **ret) {
180 int r;
181
182 assert(vl);
183 assert(ifindex > 0);
184 assert(ret);
185
186 sd_json_variant *v; /* borrowed from vl, do not unref */
187 r = varlink_callbo_and_log(
188 vl,
189 "io.systemd.Network.Link.Describe",
190 &v,
191 SD_JSON_BUILD_PAIR_INTEGER("InterfaceIndex", ifindex));
192 if (r < 0)
193 return r;
194
195 *ret = sd_json_variant_ref(v);
196 return 0;
197 }
198
199 int json_variant_find_object(sd_json_variant *v, char * const *object_names, sd_json_variant **ret) {
200 assert(object_names);
201 assert(ret);
202
203 if (!v || sd_json_variant_is_null(v))
204 return -ENODATA;
205
206 STRV_FOREACH(name, object_names) {
207 v = sd_json_variant_by_key(v, *name);
208 if (!v || sd_json_variant_is_null(v))
209 return -ENODATA;
210 }
211
212 *ret = v;
213 return 0;
214 }