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

systemd / systemd / 15057632786

15 May 2025 09:01PM UTC coverage: 72.267% (+0.02%) from 72.244%
15057632786

push

github

bluca
man: document how to hook stuff into system wakeup

Fixes: #6364

298523 of 413084 relevant lines covered (72.27%)

738132.88 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 <unistd.h>
5

6
#include "sd-varlink.h"
7

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

20
static int process_vsock_cid(unsigned cid, const char *port) {
×
21
        int r;
×
22

23
        assert(cid != VMADDR_CID_ANY);
×
24
        assert(port);
×
25

26
        union sockaddr_union sa = {
×
27
                .vm.svm_cid = cid,
28
                .vm.svm_family = AF_VSOCK,
29
        };
30

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

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

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

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

47
        log_debug("Successfully sent AF_VSOCK socket via STDOUT.");
×
48
        return 0;
49
}
50

51
static int process_vsock_string(const char *host, const char *port) {
×
52
        unsigned cid;
×
53
        int r;
×
54

55
        assert(host);
×
56
        assert(port);
×
57

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

62
        return process_vsock_cid(cid, port);
×
63
}
64

65
static int process_unix(const char *path) {
3✔
66
        int r;
3✔
67

68
        assert(path);
3✔
69

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

77
                path = prefixed;
78
        }
79

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

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

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

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

96
static int process_vsock_mux(const char *path, const char *port) {
×
97
        int r;
×
98

99
        assert(path);
×
100
        assert(port);
×
101

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

109
                path = prefixed;
110
        }
111

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

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

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

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

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

136
        log_debug("Successfully sent AF_UNIX socket via STDOUT.");
×
137
        return 0;
138
}
139

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

144
        assert(machine);
×
145
        assert(port);
×
146

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

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

160
        uint32_t cid = VMADDR_CID_ANY;
×
161

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

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

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

174
        return process_vsock_cid(cid, port);
×
175
}
176

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

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

183
        return NULL;
184
}
185

186
static int run(int argc, char* argv[]) {
3✔
187

188
        log_setup();
3✔
189

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

193
        const char *host = argv[1], *port = argv[2];
3✔
194

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

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

203
        p = startswith_sep(host, "vsock-mux");
×
204
        if (p)
×
205
                return process_vsock_mux(p, port);
×
206

207
        p = startswith_sep(host, "machine");
×
208
        if (p)
×
209
                return process_machine(p, port);
×
210

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

214
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