• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.17
/src/network/networkctl-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <sys/stat.h>
4
#include <unistd.h>
5

6
#include "alloc-util.h"
7
#include "ansi-color.h"
8
#include "log.h"
9
#include "networkctl.h"
10
#include "networkctl-util.h"
11
#include "stdio-util.h"
12
#include "strv.h"
13
#include "varlink-util.h"
14

15
int varlink_connect_networkd(sd_varlink **ret_varlink) {
3,475✔
16
        _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL;
3,475✔
17
        sd_json_variant *reply;
3,475✔
18
        uint64_t id;
3,475✔
19
        int r;
3,475✔
20

21
        r = sd_varlink_connect_address(&vl, "/run/systemd/netif/io.systemd.Network");
3,475✔
22
        if (r < 0)
3,475✔
23
                return log_error_errno(r, "Failed to connect to network service /run/systemd/netif/io.systemd.Network: %m");
1✔
24

25
        (void) sd_varlink_set_description(vl, "varlink-network");
3,474✔
26

27
        r = sd_varlink_set_allow_fd_passing_output(vl, true);
3,474✔
28
        if (r < 0)
3,474✔
UNCOV
29
                return log_error_errno(r, "Failed to allow passing file descriptor through varlink: %m");
×
30

31
        r = varlink_call_and_log(vl, "io.systemd.Network.GetNamespaceId", /* parameters= */ NULL, &reply);
3,474✔
32
        if (r < 0)
3,474✔
33
                return r;
34

35
        static const sd_json_dispatch_field dispatch_table[] = {
3,474✔
36
                { "NamespaceId", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, 0, SD_JSON_MANDATORY },
37
                {},
38
        };
39

40
        r = sd_json_dispatch(reply, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &id);
3,474✔
41
        if (r < 0)
3,474✔
42
                return r;
43

44
        if (id == 0)
3,474✔
UNCOV
45
                log_debug("systemd-networkd.service not running in a network namespace (?), skipping netns check.");
×
46
        else {
47
                struct stat st;
3,474✔
48

49
                if (stat("/proc/self/ns/net", &st) < 0)
3,474✔
UNCOV
50
                        return log_error_errno(errno, "Failed to determine our own network namespace ID: %m");
×
51

52
                if (id != st.st_ino)
3,474✔
UNCOV
53
                        return log_error_errno(SYNTHETIC_ERRNO(EREMOTE),
×
54
                                               "networkctl must be invoked in same network namespace as systemd-networkd.service.");
55
        }
56

57
        if (ret_varlink)
3,474✔
58
                *ret_varlink = TAKE_PTR(vl);
2,038✔
59
        return 0;
60
}
61

62
bool networkd_is_running(void) {
1,456✔
63
        static int cached = -1;
1,456✔
64
        int r;
1,456✔
65

66
        if (cached < 0) {
1,456✔
67
                r = access("/run/systemd/netif/state", F_OK);
1,453✔
68
                if (r < 0) {
1,453✔
69
                        if (errno != ENOENT)
×
UNCOV
70
                                log_debug_errno(errno,
×
71
                                                "Failed to determine whether networkd is running, assuming it's not: %m");
72

UNCOV
73
                        cached = false;
×
74
                } else
75
                        cached = true;
1,453✔
76
        }
77

78
        return cached;
1,456✔
79
}
80

81
int acquire_bus(sd_bus **ret) {
1,436✔
82
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1,436✔
83
        int r;
1,436✔
84

85
        assert(ret);
1,436✔
86

87
        r = sd_bus_open_system(&bus);
1,436✔
88
        if (r < 0)
1,436✔
UNCOV
89
                return log_error_errno(r, "Failed to connect to system bus: %m");
×
90

91
        (void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1,436✔
92

93
        if (networkd_is_running()) {
1,436✔
94
                r = varlink_connect_networkd(/* ret_varlink = */ NULL);
1,436✔
95
                if (r < 0)
1,436✔
96
                        return r;
97
        } else
UNCOV
98
                log_warning("systemd-networkd is not running, output might be incomplete.");
×
99

100
        *ret = TAKE_PTR(bus);
1,436✔
101
        return 0;
1,436✔
102
}
103

104
int link_get_property(
2,380✔
105
                sd_bus *bus,
106
                int ifindex,
107
                sd_bus_error *error,
108
                sd_bus_message **reply,
109
                const char *iface,
110
                const char *propname,
111
                const char *type) {
112

113
        _cleanup_free_ char *path = NULL;
2,380✔
114
        char ifindex_str[DECIMAL_STR_MAX(int)];
2,380✔
115
        int r;
2,380✔
116

117
        assert(bus);
2,380✔
118
        assert(ifindex >= 0);
2,380✔
119
        assert(error);
2,380✔
120
        assert(reply);
2,380✔
121
        assert(iface);
2,380✔
122
        assert(propname);
2,380✔
123
        assert(type);
2,380✔
124

125
        xsprintf(ifindex_str, "%i", ifindex);
2,380✔
126

127
        r = sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str, &path);
2,380✔
128
        if (r < 0)
2,380✔
129
                return r;
130

131
        return sd_bus_get_property(bus, "org.freedesktop.network1", path, iface, propname, error, reply, type);
2,380✔
132
}
133

134
void operational_state_to_color(const char *name, const char *state, const char **on, const char **off) {
1,219✔
135
        if (STRPTR_IN_SET(state, "routable", "enslaved") ||
1,219✔
136
            (streq_ptr(name, "lo") && streq_ptr(state, "carrier"))) {
510✔
137
                if (on)
714✔
138
                        *on = ansi_highlight_green();
1,428✔
139
                if (off)
714✔
140
                        *off = ansi_normal();
1,422✔
141
        } else if (streq_ptr(state, "degraded")) {
505✔
142
                if (on)
324✔
143
                        *on = ansi_highlight_yellow();
324✔
144
                if (off)
324✔
145
                        *off = ansi_normal();
642✔
146
        } else {
147
                if (on)
181✔
148
                        *on = "";
181✔
149
                if (off)
181✔
150
                        *off = "";
159✔
151
        }
152
}
1,219✔
153

154
void setup_state_to_color(const char *state, const char **on, const char **off) {
1,218✔
155
        if (streq_ptr(state, "configured")) {
1,218✔
156
                if (on)
1,104✔
157
                        *on = ansi_highlight_green();
2,208✔
158
                if (off)
1,104✔
159
                        *off = ansi_normal();
2,196✔
160
        } else if (streq_ptr(state, "configuring")) {
114✔
161
                if (on)
41✔
162
                        *on = ansi_highlight_yellow();
41✔
163
                if (off)
41✔
164
                        *off = ansi_normal();
82✔
165
        } else if (STRPTR_IN_SET(state, "failed", "linger")) {
73✔
166
                if (on)
×
167
                        *on = ansi_highlight_red();
×
168
                if (off)
×
UNCOV
169
                        *off = ansi_normal();
×
170
        } else {
171
                if (on)
73✔
172
                        *on = "";
73✔
173
                if (off)
73✔
174
                        *off = "";
51✔
175
        }
176
}
1,218✔
177

178
void online_state_to_color(const char *state, const char **on, const char **off) {
1,191✔
179
        if (streq_ptr(state, "online")) {
1,191✔
180
                if (on)
1,022✔
181
                        *on = ansi_highlight_green();
2,044✔
182
                if (off)
1,022✔
UNCOV
183
                        *off = ansi_normal();
×
184
        } else if (streq_ptr(state, "partial")) {
169✔
185
                if (on)
×
186
                        *on = ansi_highlight_yellow();
×
187
                if (off)
×
UNCOV
188
                        *off = ansi_normal();
×
189
        } else {
190
                if (on)
169✔
191
                        *on = "";
169✔
192
                if (off)
169✔
UNCOV
193
                        *off = "";
×
194
        }
195
}
1,191✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc