Skip to content

Commit 907d107

Browse files
mtjhrcslp
authored andcommitted
examples/chroot_vm: Add the ability to start passt process
This also adds the ability to specify passt socket path, if you do not want to start passt by chroot_vm. (this is useful to see passt log) Signed-off-by: Matej Hrica <[email protected]>
1 parent c2b6a44 commit 907d107

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

examples/chroot_vm.c

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ static void print_help(char *const name)
3232
fprintf(stderr,
3333
"Usage: %s [OPTIONS] NEWROOT COMMAND [COMMAND_ARGS...]\n"
3434
"OPTIONS: \n"
35-
" -h --help Show help\n"
36-
" --net=NET_MODE Set network mode\n"
35+
" -h --help Show help\n"
36+
" --net=NET_MODE Set network mode\n"
37+
" --passt-socket=PATH Instead of starting passt, connect to passt socket at PATH"
3738
"NET_MODE can be either TSI (default) or PASST\n"
3839
"\n"
3940
"NEWROOT: the root directory of the vm\n"
@@ -46,12 +47,14 @@ static void print_help(char *const name)
4647
static const struct option long_options[] = {
4748
{ "help", no_argument, NULL, 'h' },
4849
{ "net_mode", required_argument, NULL, 'N' },
50+
{ "passt-socket", required_argument, NULL, 'P' },
4951
{ NULL, 0, NULL, 0 }
5052
};
5153

5254
struct cmdline {
5355
bool show_help;
5456
enum net_mode net_mode;
57+
char const *passt_socket_path;
5558
char const *new_root;
5659
char *const *guest_argv;
5760
};
@@ -64,6 +67,7 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
6467
*cmdline = (struct cmdline){
6568
.show_help = false,
6669
.net_mode = NET_MODE_TSI,
70+
.passt_socket_path = NULL,
6771
.new_root = NULL,
6872
.guest_argv = NULL,
6973
};
@@ -86,6 +90,9 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
8690
return false;
8791
}
8892
break;
93+
case 'P':
94+
cmdline->passt_socket_path = optarg;
95+
break;
8996
case '?':
9097
return false;
9198
default:
@@ -132,6 +139,47 @@ int connect_to_passt()
132139
return socket_fd;
133140
}
134141

142+
int start_passt()
143+
{
144+
int socket_fds[2];
145+
const int PARENT = 0;
146+
const int CHILD = 1;
147+
148+
if (socketpair(AF_UNIX, SOCK_STREAM, 0, socket_fds) < 0) {
149+
perror("Failed to create passt socket fd");
150+
return -1;
151+
}
152+
153+
int pid = fork();
154+
if (pid < 0) {
155+
perror("fork");
156+
return -1;
157+
}
158+
159+
if (pid == 0) { // child
160+
if (close(socket_fds[PARENT]) < 0) {
161+
perror("close PARENT");
162+
}
163+
164+
char fd_as_str[16];
165+
snprintf(fd_as_str, sizeof(fd_as_str), "%d", socket_fds[CHILD]);
166+
167+
printf("passing fd %s to passt", fd_as_str);
168+
169+
if (execlp("passt", "passt", "-f", "--fd", fd_as_str, NULL) < 0) {
170+
perror("execlp");
171+
return -1;
172+
}
173+
174+
} else { // parent
175+
if (close(socket_fds[CHILD]) < 0) {
176+
perror("close CHILD");
177+
}
178+
179+
return socket_fds[PARENT];
180+
}
181+
}
182+
135183

136184
int main(int argc, char *const argv[])
137185
{
@@ -233,7 +281,8 @@ int main(int argc, char *const argv[])
233281
return -1;
234282
}
235283
} else {
236-
int passt_fd = connect_to_passt();
284+
int passt_fd = cmdline.passt_socket_path ? connect_to_passt(cmdline.passt_socket_path) : start_passt();
285+
237286
if (passt_fd < 0) {
238287
return -1;
239288
}

0 commit comments

Comments
 (0)