• 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

25.0
/src/ssh-generator/ssh-proxy.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <net/if_arp.h>
4
#include <stdio.h>
5
#include <unistd.h>
6

7
#include "sd-varlink.h"
8

9
#include "alloc-util.h"
10
#include "fd-util.h"
11
#include "io-util.h"
12
#include "iovec-util.h"
13
#include "log.h"
14
#include "main-func.h"
15
#include "missing_socket.h"
16
#include "parse-util.h"
17
#include "socket-util.h"
18
#include "string-util.h"
19
#include "strv.h"
20
#include "varlink-util.h"
21

22
static int process_vsock_cid(unsigned cid, const char *port) {
×
UNCOV
23
        int r;
×
24

25
        assert(cid != VMADDR_CID_ANY);
×
UNCOV
26
        assert(port);
×
27

UNCOV
28
        union sockaddr_union sa = {
×
29
                .vm.svm_cid = cid,
30
                .vm.svm_family = AF_VSOCK,
31
        };
32

33
        r = vsock_parse_port(port, &sa.vm.svm_port);
×
34
        if (r < 0)
×
UNCOV
35
                return log_error_errno(r, "Failed to parse vsock port: %s", port);
×
36

37
        _cleanup_close_ int fd = socket(AF_VSOCK, SOCK_STREAM|SOCK_CLOEXEC, 0);
×
38
        if (fd < 0)
×
UNCOV
39
                return log_error_errno(errno, "Failed to allocate AF_VSOCK socket: %m");
×
40

41
        if (connect(fd, &sa.sa, sockaddr_len(&sa)) < 0)
×
UNCOV
42
                return log_error_errno(errno, "Failed to connect to vsock:%u:%u: %m", sa.vm.svm_cid, sa.vm.svm_port);
×
43

44
        /* OpenSSH wants us to send a single byte along with the file descriptor, hence do so */
45
        r = send_one_fd_iov(STDOUT_FILENO, fd, &iovec_nul_byte, /* iovlen= */ 1, /* flags= */ 0);
×
46
        if (r < 0)
×
UNCOV
47
                return log_error_errno(r, "Failed to send socket via STDOUT: %m");
×
48

UNCOV
49
        log_debug("Successfully sent AF_VSOCK socket via STDOUT.");
×
50
        return 0;
51
}
52

53
static int process_vsock_string(const char *host, const char *port) {
×
54
        unsigned cid;
×
UNCOV
55
        int r;
×
56

57
        assert(host);
×
UNCOV
58
        assert(port);
×
59

60
        r = vsock_parse_cid(host, &cid);
×
61
        if (r < 0)
×
UNCOV
62
                return log_error_errno(r, "Failed to parse vsock cid: %s", host);
×
63

UNCOV
64
        return process_vsock_cid(cid, port);
×
65
}
66

67
static int process_unix(const char *path) {
3✔
68
        int r;
3✔
69

70
        assert(path);
3✔
71

72
        /* We assume the path is absolute unless it starts with a dot (or is already explicitly absolute) */
73
        _cleanup_free_ char *prefixed = NULL;
3✔
74
        if (!STARTSWITH_SET(path, "/", "./")) {
3✔
75
                prefixed = strjoin("/", path);
3✔
76
                if (!prefixed)
3✔
UNCOV
77
                        return log_oom();
×
78

79
                path = prefixed;
80
        }
81

82
        _cleanup_close_ int fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
6✔
83
        if (fd < 0)
3✔
UNCOV
84
                return log_error_errno(errno, "Failed to allocate AF_UNIX socket: %m");
×
85

86
        r = connect_unix_path(fd, AT_FDCWD, path);
3✔
87
        if (r < 0)
3✔
UNCOV
88
                return log_error_errno(r, "Failed to connect to AF_UNIX socket %s: %m", path);
×
89

90
        r = send_one_fd_iov(STDOUT_FILENO, fd, &iovec_nul_byte, /* iovlen= */ 1, /* flags= */ 0);
3✔
91
        if (r < 0)
3✔
UNCOV
92
                return log_error_errno(r, "Failed to send socket via STDOUT: %m");
×
93

94
        log_debug("Successfully sent AF_UNIX socket via STDOUT.");
3✔
95
        return 0;
96
}
97

98
static int process_vsock_mux(const char *path, const char *port) {
×
UNCOV
99
        int r;
×
100

101
        assert(path);
×
UNCOV
102
        assert(port);
×
103

104
        /* We assume the path is absolute unless it starts with a dot (or is already explicitly absolute) */
105
        _cleanup_free_ char *prefixed = NULL;
×
106
        if (!STARTSWITH_SET(path, "/", "./")) {
×
107
                prefixed = strjoin("/", path);
×
108
                if (!prefixed)
×
UNCOV
109
                        return log_oom();
×
110

111
                path = prefixed;
112
        }
113

114
        _cleanup_close_ int fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
×
115
        if (fd < 0)
×
UNCOV
116
                return log_error_errno(errno, "Failed to allocate AF_UNIX socket: %m");
×
117

118
        r = connect_unix_path(fd, AT_FDCWD, path);
×
119
        if (r < 0)
×
UNCOV
120
                return log_error_errno(r, "Failed to connect to AF_UNIX socket %s: %m", path);
×
121

122
        /* Based on the protocol as defined here:
123
         * https://github.com/cloud-hypervisor/cloud-hypervisor/blob/main/docs/vsock.md
124
         * https://github.com/firecracker-microvm/firecracker/blob/main/docs/vsock.md */
125
        _cleanup_free_ char *connect_cmd = NULL;
×
126
        connect_cmd = strjoin("CONNECT ", port, "\n");
×
127
        if (!connect_cmd)
×
UNCOV
128
                return log_oom();
×
129

130
        r = loop_write(fd, connect_cmd, SIZE_MAX);
×
131
        if (r < 0)
×
UNCOV
132
                return log_error_errno(r, "Failed to send CONNECT to %s:%s: %m", path, port);
×
133

134
        r = send_one_fd_iov(STDOUT_FILENO, fd, &iovec_nul_byte, /* iovlen= */ 1, /* flags= */ 0);
×
135
        if (r < 0)
×
UNCOV
136
                return log_error_errno(r, "Failed to send socket via STDOUT: %m");
×
137

UNCOV
138
        log_debug("Successfully sent AF_UNIX socket via STDOUT.");
×
139
        return 0;
140
}
141

142
static int process_machine(const char *machine, const char *port) {
×
143
        _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL;
×
UNCOV
144
        int r;
×
145

146
        assert(machine);
×
UNCOV
147
        assert(port);
×
148

149
        r = sd_varlink_connect_address(&vl, "/run/systemd/machine/io.systemd.Machine");
×
150
        if (r < 0)
×
UNCOV
151
                return log_error_errno(r, "Failed to connect to machined on /run/systemd/machine/io.systemd.Machine: %m");
×
152

153
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *result = NULL;
×
UNCOV
154
        r = varlink_callbo_and_log(
×
155
                        vl,
156
                        "io.systemd.Machine.List",
157
                        &result,
158
                        SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(machine)));
UNCOV
159
        if (r < 0)
×
160
                return r;
161

UNCOV
162
        uint32_t cid = VMADDR_CID_ANY;
×
163

UNCOV
164
        static const sd_json_dispatch_field dispatch_table[] = {
×
165
                { "vSockCid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint32, 0, 0 },
166
                {}
167
        };
168

169
        r = sd_json_dispatch(result, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &cid);
×
170
        if (r < 0)
×
UNCOV
171
                return log_error_errno(r, "Failed to parse Varlink reply: %m");
×
172

173
        if (cid == VMADDR_CID_ANY)
×
UNCOV
174
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Machine has no AF_VSOCK CID assigned.");
×
175

UNCOV
176
        return process_vsock_cid(cid, port);
×
177
}
178

179
static char *startswith_sep(const char *s, const char *prefix) {
6✔
180
        const char *p = startswith(s, prefix);
6✔
181

182
        if (p && IN_SET(*p, '/', '%'))
6✔
183
                return (char*) p + 1;
3✔
184

185
        return NULL;
186
}
187

188
static int run(int argc, char* argv[]) {
3✔
189

190
        log_setup();
3✔
191

192
        if (argc != 3)
3✔
UNCOV
193
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Expected two arguments: host and port.");
×
194

195
        const char *host = argv[1], *port = argv[2];
3✔
196

197
        const char *p = startswith_sep(host, "vsock");
3✔
198
        if (p)
3✔
UNCOV
199
                return process_vsock_string(p, port);
×
200

201
        p = startswith_sep(host, "unix");
3✔
202
        if (p)
3✔
203
                return process_unix(p);
3✔
204

205
        p = startswith_sep(host, "vsock-mux");
×
206
        if (p)
×
UNCOV
207
                return process_vsock_mux(p, port);
×
208

209
        p = startswith_sep(host, "machine");
×
210
        if (p)
×
UNCOV
211
                return process_machine(p, port);
×
212

UNCOV
213
        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Don't know how to parse host name specification: %s", host);
×
214
}
215

216
DEFINE_MAIN_FUNCTION(run);
3✔
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