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
| | // SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2020-2021 Alyssa Ross <hi@alyssa.is>
#define _GNU_SOURCE
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <sys/socket.h>
#include <sysexits.h>
#include <unistd.h>
#include <linux/vm_sockets.h>
#include "env.h"
#include "log.h"
#include "util.h"
#include "vsock.h"
noreturn static void ex_usage(void)
{
if (verbosity)
fprintf(stderr, "Usage: %s [ -q | -Q | -v ] cid port prog...\n",
program_invocation_short_name);
exit(EX_USAGE);
}
int main(int argc, char *argv[])
{
int opt, fd;
uint32_t rcid, rport;
while ((opt = getopt(argc, argv, "+qQv")) != -1) {
switch (opt) {
case 'q':
case 'Q':
case 'v':
set_verbosity(opt);
break;
default:
ex_usage();
}
}
// Check there are enough positional arguments (two for the
// address and at least one to exec into).
if (optind > argc - 3)
ex_usage();
if (getu32(argv[optind++], 0, UINT32_MAX, &rcid))
ex_usage();
if (getu32(argv[optind++], 0, UINT32_MAX, &rport))
ex_usage();
setenvf("VSOCKREMOTECID", 1, "%" PRIu32, rcid);
setenvf("VSOCKREMOTEPORT", 1, "%" PRIu32, rport);
ilog("connecting to %" PRIu32 ":%" PRIu32 "...", rcid, rport);
// Linux will sometimes randomly time out connections, even on
// the same host with no backlog, so retry a few times.
{
int i = 0;
do fd = vsock_open(rcid, rport);
while (i++ < 3 && fd == -1 && errno == ETIMEDOUT);
}
if (fd == -1)
diee(EX_UNAVAILABLE,
"connect to %" PRIu32 ":%" PRIu32 " failed", rcid, rport);
ilog("connected to %" PRIu32 ":%" PRIu32 "!", rcid, rport);
// Set up the file descriptors to the well-known UCSPI numbers.
if (dup2(fd, 6) == -1)
diee(EX_OSERR, "dup2");
if (dup2(fd, 7) == -1)
diee(EX_OSERR, "dup2");
if (fd != 6 && fd != 7)
close(fd);
execvp(argv[optind], &argv[optind]);
diee(EX_OSERR, "exec");
}
|