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