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
| | // SPDX-License-Identifier: EUPL-1.2+
// SPDX-FileCopyrightText: 2025 Alyssa Ross <hi@alyssa.is>
// SPDX-FileCopyrightText: 2025 Yureka Lilian <yureka@cyberchaos.dev>
#include "lib.h"
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <linux/ipv6.h>
static int setup_server(void)
{
int fd;
struct ifreq ifr;
struct in6_ifreq ifr6;
struct sockaddr_in6 addr = {
.sin6_family = AF_INET6,
.sin6_port = htons(1234),
.sin6_addr = { .s6_addr = { 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } },
};
sprintf(ifr.ifr_name, "lo");
if ((fd = socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
if (ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) {
perror("SIOCGIFFLAGS");
exit(EXIT_FAILURE);
}
ifr.ifr_flags |= IFF_UP;
if (ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) {
perror("SIOCSIFFLAGS");
exit(EXIT_FAILURE);
}
ifr6.ifr6_ifindex = 1;
ifr6.ifr6_addr = addr.sin6_addr;
ifr6.ifr6_prefixlen = 128;
if (ioctl(fd, SIOCSIFADDR, &ifr6) == -1) {
perror("SIOCSIFADDR");
exit(EXIT_FAILURE);
}
if ((fd = socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
int tries = 0;
while (bind(fd, &addr, sizeof addr) == -1) {
perror("bind");
if (tries++ >= 5)
exit(EXIT_FAILURE);
}
if (listen(fd, 1) == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
return fd;
}
static void expect_connection(int listener)
{
int conn_fd;
FILE *conn;
char msg[7];
size_t len;
fputs("waiting for server connection\n", stderr);
if ((conn_fd = accept(listener, nullptr, nullptr)) == -1) {
perror("accept");
exit(EXIT_FAILURE);
}
fputs("accepted connection!\n", stderr);
if (!(conn = fdopen(conn_fd, "r"))) {
perror("fdopen(server connection)");
exit(EXIT_FAILURE);
}
len = fread(msg, 1, sizeof msg, conn);
if (len != 6 || memcmp("hello\n", msg, 6)) {
if (ferror(conn))
perror("fread(server connection)");
else
fprintf(stderr, "unexpected connection data: %.*s",
(int)len, msg);
exit(EXIT_FAILURE);
}
fclose(conn);
}
static void drain_connections(int listener)
{
int r;
struct pollfd pollfd = { .fd = listener, .events = POLLIN };
for (;;) {
switch (poll(&pollfd, 1, 0)) {
case -1:
perror("poll");
exit(EXIT_FAILURE);
case 0:
return;
}
if ((r = accept4(listener, nullptr, nullptr, SOCK_CLOEXEC)) == -1) {
perror("accept");
exit(EXIT_FAILURE);
}
close(r);
}
}
void test(struct config c)
{
int server = setup_server();
struct vm *vm = start_qemu(c);
start_console_thread(vm, "~ # ");
wait_for_prompt(vm);
if (fputs("set -euxo pipefail && "
"mkdir /run/mnt && "
"mount \"$(findfs UUID=a7834806-2f82-4faf-8ac4-4f8fd8a474ca)\" /run/mnt && "
"s6-rc -bu change vmm-env && "
"vm-import user /run/mnt/vms && "
"vm-start \"$(basename \"$(readlink /run/vm/by-name/user.nc)\")\" && "
"tail -Fc +0 /run/log/current /run/*.log &\n",
vm_console_writer(vm)) == EOF) {
fputs("error writing to console\n", stderr);
exit(EXIT_FAILURE);
}
expect_connection(server);
wait_for_prompt(vm);
if (fputs("s6-svc -wR -r /run/vm/by-name/sys.netvm/service\n",
vm_console_writer(vm)) == EOF) {
fputs("error writing to console\n", stderr);
exit(EXIT_FAILURE);
}
wait_for_prompt(vm);
drain_connections(server);
if (fputs("vm-start \"$(basename \"$(readlink /run/vm/by-name/sys.netvm)\")\"\n",
vm_console_writer(vm)) == EOF) {
fputs("error writing to console\n", stderr);
exit(EXIT_FAILURE);
}
expect_connection(server);
}
|