patches and low-level development discussion
 help / color / mirror / code / Atom feed
dd9a74a970ea9cfc0ec69a2748f98bb466ef32e0 blob 2737 bytes (raw)

  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
 
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2020 Alyssa Ross <hi@alyssa.is>

#define _GNU_SOURCE

#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sysexits.h>
#include <unistd.h>

#include <linux/vm_sockets.h>

#include "env.h"
#include "log.h"
#include "util.h"
#include "vsock.h"

static void ex_usage(void) __attribute__((noreturn));
static void ex_usage(void)
{
	if (verbosity)
		fprintf(stderr, "Usage: %s [ -1 ] [ -q | -Q | -v ] cid port prog...\n",
			program_invocation_short_name);
	exit(EX_USAGE);
}

int main(int argc, char *argv[])
{
	bool notify = false;
	int opt, conn;
	pid_t child;
	uint32_t lcid, lport, rcid, rport;

	while ((opt = getopt(argc, argv, "+1qQv")) != -1) {
		switch (opt) {
		case '1':
			notify = true;
			break;
		case 'q':
			verbosity = nothing;
			break;
		case 'Q':
			verbosity = errors;
			break;
		case 'v':
			verbosity = all;
			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 (!strcmp(argv[optind], "-1"))
		lcid = VMADDR_CID_ANY;
	else if (getu32(argv[optind], 0, UINT32_MAX, &lcid))
		ex_usage();
	optind++;

	if (!strcmp(argv[optind], "-1"))
		lport = VMADDR_PORT_ANY;
	else if (getu32(argv[optind], 0, UINT32_MAX, &lport))
		ex_usage();
	optind++;

	int fd = socket(AF_VSOCK, SOCK_STREAM, 0);
	if (fd == -1)
		diee(EX_OSERR, "socket");

	if (vsock_bind(fd, lcid, lport) == -1)
		diee(EX_OSERR, "bind");

	if (listen(fd, 40) == -1)
		diee(EX_OSERR, "listen");

	if (lport == VMADDR_PORT_ANY)
		if (vsock_get_port(fd, &lport) == -1)
			diee(EX_OSERR, "getsockname");

	if (notify) {
		printf("%" PRIu32 "\n", lport);
		close(STDOUT_FILENO);
	}

	setenvf("VSOCKLOCALCID", 1, "%" PRIu32, lcid);
	setenvf("VSOCKLOCALPORT", 1, "%" PRIu32, lport);

	ilog("listening as %" PRIu32 " on port %" PRIu32, lcid, lport);

	while ((conn = vsock_accept(fd, &rcid, &rport)) != -1) {
		setenvf("VSOCKREMOTECID", 1, "%" PRIu32, rcid);
		setenvf("VSOCKREMOTEPORT", 1, "%" PRIu32, rport);

		ilog("connection from %" PRIu32 " port %" PRIu32, rcid, rport);

		switch (child = fork()) {
		case -1: err(EX_OSERR, "fork");
		case 0:
			if (dup2(conn, STDIN_FILENO) == -1)
				err(EX_OSERR, "dup2");
			if (dup2(conn, STDOUT_FILENO) == -1)
				err(EX_OSERR, "dup2");
			if (conn != 0 && conn != 1)
				close(conn);
			execvp(argv[optind], &argv[optind]);
			err(EX_OSERR, "exec");
		}

		if (waitpid(child, NULL, 0) == -1)
			err(EX_OSERR, "waitpid");
		
		close(conn);
	}
	err(EX_OSERR, "accept");
}
debug log:

solving dd9a74a ...
found dd9a74a in https://spectrum-os.org/git/ucspi-vsock

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).