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
| | // SPDX-License-Identifier: EUPL-1.2+
// SPDX-FileCopyrightText: 2025 Alyssa Ross <hi@alyssa.is>
#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>
static int setup_server(void)
{
int fd;
struct ifreq ifr;
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(1234),
.sin_addr = { .s_addr = htonl(INADDR_LOOPBACK) },
};
sprintf(ifr.ifr_name, "lo");
if ((fd = socket(AF_INET, 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);
}
if (bind(fd, &addr, sizeof addr) == -1) {
perror("bind");
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);
}
|