mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-08-05 08:49:39 +00:00
9pmount-vsock: add initial skeleton
Signed-off-by: David Scott <dave.scott@docker.com>
This commit is contained in:
parent
f8e3bd7c37
commit
ed616f8c26
214
alpine/packages/9pmount-vsock/9pmount-vsock.c
Normal file
214
alpine/packages/9pmount-vsock/9pmount-vsock.c
Normal file
@ -0,0 +1,214 @@
|
|||||||
|
/*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
#include <syslog.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <err.h>
|
||||||
|
|
||||||
|
#include "compat.h"
|
||||||
|
|
||||||
|
int listen_flag = 0;
|
||||||
|
int connect_flag = 0;
|
||||||
|
|
||||||
|
char *default_sid = "C378280D-DA14-42C8-A24E-0DE92A1028E2";
|
||||||
|
|
||||||
|
/* Helper macros for parsing/printing GUIDs */
|
||||||
|
#define GUID_FMT "%08x-%04hx-%04hx-%02x%02x-%02x%02x%02x%02x%02x%02x"
|
||||||
|
#define GUID_ARGS(_g) \
|
||||||
|
(_g).Data1, (_g).Data2, (_g).Data3, \
|
||||||
|
(_g).Data4[0], (_g).Data4[1], (_g).Data4[2], (_g).Data4[3], \
|
||||||
|
(_g).Data4[4], (_g).Data4[5], (_g).Data4[6], (_g).Data4[7]
|
||||||
|
#define GUID_SARGS(_g) \
|
||||||
|
&(_g).Data1, &(_g).Data2, &(_g).Data3, \
|
||||||
|
&(_g).Data4[0], &(_g).Data4[1], &(_g).Data4[2], &(_g).Data4[3], \
|
||||||
|
&(_g).Data4[4], &(_g).Data4[5], &(_g).Data4[6], &(_g).Data4[7]
|
||||||
|
|
||||||
|
|
||||||
|
int parseguid(const char *s, GUID *g)
|
||||||
|
{
|
||||||
|
int res;
|
||||||
|
int p0, p1, p2, p3, p4, p5, p6, p7;
|
||||||
|
|
||||||
|
res = sscanf(s, GUID_FMT,
|
||||||
|
&g->Data1, &g->Data2, &g->Data3,
|
||||||
|
&p0, &p1, &p2, &p3, &p4, &p5, &p6, &p7);
|
||||||
|
if (res != 11)
|
||||||
|
return 1;
|
||||||
|
g->Data4[0] = p0;
|
||||||
|
g->Data4[1] = p1;
|
||||||
|
g->Data4[2] = p2;
|
||||||
|
g->Data4[3] = p3;
|
||||||
|
g->Data4[4] = p4;
|
||||||
|
g->Data4[5] = p5;
|
||||||
|
g->Data4[6] = p6;
|
||||||
|
g->Data4[7] = p7;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sockerr(const char *msg)
|
||||||
|
{
|
||||||
|
syslog(LOG_CRIT, "%s Error: %d. %s", msg, errno, strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handle(SOCKET fd)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static int create_listening_socket(GUID serviceid) {
|
||||||
|
SOCKET lsock = INVALID_SOCKET;
|
||||||
|
SOCKADDR_HV sa;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
lsock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
||||||
|
if (lsock == INVALID_SOCKET) {
|
||||||
|
sockerr("socket()");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
sa.Family = AF_HYPERV;
|
||||||
|
sa.Reserved = 0;
|
||||||
|
sa.VmId = HV_GUID_WILDCARD;
|
||||||
|
sa.ServiceId = serviceid;
|
||||||
|
|
||||||
|
res = bind(lsock, (const struct sockaddr *)&sa, sizeof(sa));
|
||||||
|
if (res == SOCKET_ERROR) {
|
||||||
|
sockerr("bind()");
|
||||||
|
closesocket(lsock);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
res = listen(lsock, SOMAXCONN);
|
||||||
|
if (res == SOCKET_ERROR) {
|
||||||
|
sockerr("listen()");
|
||||||
|
closesocket(lsock);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return lsock;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int connect_socket(GUID serviceid) {
|
||||||
|
SOCKET sock = INVALID_SOCKET;
|
||||||
|
SOCKADDR_HV sa;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
sock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
||||||
|
if (sock == INVALID_SOCKET) {
|
||||||
|
sockerr("socket()");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
sa.Family = AF_HYPERV;
|
||||||
|
sa.Reserved = 0;
|
||||||
|
sa.VmId = HV_GUID_PARENT;
|
||||||
|
sa.ServiceId = serviceid;
|
||||||
|
|
||||||
|
res = connect(sock, (const struct sockaddr *)&sa, sizeof(sa));
|
||||||
|
if (res == SOCKET_ERROR) {
|
||||||
|
sockerr("connect()");
|
||||||
|
closesocket(sock);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int accept_socket(SOCKET lsock) {
|
||||||
|
SOCKET csock = INVALID_SOCKET;
|
||||||
|
SOCKADDR_HV sac;
|
||||||
|
socklen_t socklen = sizeof(sac);
|
||||||
|
|
||||||
|
csock = accept(lsock, (struct sockaddr *)&sac, &socklen);
|
||||||
|
if (csock == INVALID_SOCKET) {
|
||||||
|
sockerr("accept()");
|
||||||
|
closesocket(lsock);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Connect from: "GUID_FMT":"GUID_FMT"\n",
|
||||||
|
GUID_ARGS(sac.VmId), GUID_ARGS(sac.ServiceId));
|
||||||
|
return csock;
|
||||||
|
}
|
||||||
|
|
||||||
|
void usage(char *name)
|
||||||
|
{
|
||||||
|
printf("%s usage:\n", name);
|
||||||
|
printf("\t[--serviceid <guid>] [--listen | --connect]\n\n");
|
||||||
|
printf("where\n");
|
||||||
|
printf("\t--serviceid <guid>: use <guid> as the well-known service GUID\n");
|
||||||
|
printf("\t (defaults to %s)\n", default_sid);
|
||||||
|
printf("\t--listen: listen forever for incoming AF_HVSOCK connections\n");
|
||||||
|
printf("\t--connect: connect to the parent partition\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int __cdecl main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int res = 0;
|
||||||
|
GUID sid;
|
||||||
|
int c;
|
||||||
|
/* Defaults to a testing GUID */
|
||||||
|
char *serviceid = default_sid;
|
||||||
|
|
||||||
|
opterr = 0;
|
||||||
|
while (1) {
|
||||||
|
static struct option long_options[] = {
|
||||||
|
/* These options set a flag. */
|
||||||
|
{"serviceid", required_argument, NULL, 's'},
|
||||||
|
{"listen", no_argument, &listen_flag, 1},
|
||||||
|
{"connect", no_argument, &connect_flag, 1},
|
||||||
|
{0, 0, 0, 0}
|
||||||
|
};
|
||||||
|
int option_index = 0;
|
||||||
|
|
||||||
|
c = getopt_long (argc, argv, "s:", long_options, &option_index);
|
||||||
|
if (c == -1) break;
|
||||||
|
|
||||||
|
switch (c) {
|
||||||
|
|
||||||
|
case 's':
|
||||||
|
serviceid = optarg;
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
usage (argv[0]);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((listen_flag && connect_flag) || !(listen_flag || connect_flag)){
|
||||||
|
fprintf(stderr, "Please supply either the --listen or --connect flag, but not both.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int log_flags = LOG_CONS | LOG_NDELAY | LOG_PERROR;
|
||||||
|
|
||||||
|
openlog(argv[0], log_flags, LOG_DAEMON);
|
||||||
|
|
||||||
|
res = parseguid(serviceid, &sid);
|
||||||
|
if (res) {
|
||||||
|
syslog(LOG_CRIT, "Failed to parse serviceid as GUID: %s", serviceid);
|
||||||
|
usage(argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SOCKET sock = INVALID_SOCKET;
|
||||||
|
if (listen_flag) {
|
||||||
|
syslog(LOG_INFO, "starting in listening mode with serviceid=%s", serviceid);
|
||||||
|
SOCKET lsocket = create_listening_socket(sid);
|
||||||
|
sock = accept_socket(lsocket);
|
||||||
|
} else {
|
||||||
|
syslog(LOG_INFO, "starting in connect mode with serviceid=%s", serviceid);
|
||||||
|
sock = connect_socket(sid);
|
||||||
|
}
|
||||||
|
|
||||||
|
handle(sock);
|
||||||
|
exit(0);
|
||||||
|
}
|
10
alpine/packages/9pmount-vsock/Dockerfile
Normal file
10
alpine/packages/9pmount-vsock/Dockerfile
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
FROM alpine:3.3
|
||||||
|
|
||||||
|
RUN apk update && apk upgrade && apk add alpine-sdk util-linux-dev linux-headers
|
||||||
|
|
||||||
|
RUN mkdir -p /9pmount-vsock
|
||||||
|
WORKDIR /9pmount-vsock
|
||||||
|
|
||||||
|
COPY . /9pmount-vsock
|
||||||
|
|
||||||
|
RUN make 9pmount-vsock
|
18
alpine/packages/9pmount-vsock/Makefile
Normal file
18
alpine/packages/9pmount-vsock/Makefile
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
.PHONY: all
|
||||||
|
|
||||||
|
DEPS=9pmount-vsock.c compat.h
|
||||||
|
|
||||||
|
all: Dockerfile $(DEPS)
|
||||||
|
docker build -t 9pmount-vsock:build .
|
||||||
|
docker run --rm 9pmount-vsock:build cat 9pmount-vsock > 9pmount-vsock
|
||||||
|
chmod 755 9pmount-vsock
|
||||||
|
|
||||||
|
9pmount-vsock: 9pmount-vsock.o
|
||||||
|
gcc -Wall -Werror -o 9pmount-vsock 9pmount-vsock.o -lpthread
|
||||||
|
|
||||||
|
9pmount-vsock.o: 9pmount-vsock.c compat.h
|
||||||
|
gcc -Wall -Werror -c 9pmount-vsock.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f 9pmount-vsock
|
||||||
|
docker images -q 9pmount-vsock:build | xargs docker rmi -f
|
118
alpine/packages/9pmount-vsock/compat.h
Normal file
118
alpine/packages/9pmount-vsock/compat.h
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/*
|
||||||
|
* Compatibility layer between Windows and Linux
|
||||||
|
*/
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#undef UNICODE
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#include <windows.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#include <hvsocket.h>
|
||||||
|
|
||||||
|
#pragma comment (lib, "Ws2_32.lib")
|
||||||
|
#pragma comment (lib, "Mswsock.lib")
|
||||||
|
#pragma comment (lib, "AdvApi32.lib")
|
||||||
|
|
||||||
|
#else
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#endif /* !_MSC_VER */
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
typedef int socklen_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
/* Compat layer for Linux/Unix */
|
||||||
|
typedef int SOCKET;
|
||||||
|
|
||||||
|
#ifndef SOCKET_ERROR
|
||||||
|
#define SOCKET_ERROR -1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef INVALID_SOCKET
|
||||||
|
#define INVALID_SOCKET -1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define closesocket(_fd) close(_fd)
|
||||||
|
|
||||||
|
/* Shutdown flags are different too */
|
||||||
|
#define SD_SEND SHUT_WR
|
||||||
|
#define SD_RECEIVE SHUT_RD
|
||||||
|
#define SD_BOTH SHUT_RDWR
|
||||||
|
|
||||||
|
#define __cdecl
|
||||||
|
|
||||||
|
/* GUID handling */
|
||||||
|
typedef struct _GUID {
|
||||||
|
uint32_t Data1;
|
||||||
|
uint16_t Data2;
|
||||||
|
uint16_t Data3;
|
||||||
|
uint8_t Data4[8];
|
||||||
|
} GUID;
|
||||||
|
|
||||||
|
#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
|
||||||
|
const GUID name = {l, w1, w2, {b1, b2, b3, b4, b5, b6, b7, b8}}
|
||||||
|
|
||||||
|
|
||||||
|
/* HV Socket definitions */
|
||||||
|
#define AF_HYPERV 43
|
||||||
|
#define HV_PROTOCOL_RAW 1
|
||||||
|
|
||||||
|
typedef struct _SOCKADDR_HV
|
||||||
|
{
|
||||||
|
unsigned short Family;
|
||||||
|
unsigned short Reserved;
|
||||||
|
GUID VmId;
|
||||||
|
GUID ServiceId;
|
||||||
|
} SOCKADDR_HV;
|
||||||
|
|
||||||
|
DEFINE_GUID(HV_GUID_ZERO,
|
||||||
|
0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
|
||||||
|
DEFINE_GUID(HV_GUID_BROADCAST,
|
||||||
|
0xFFFFFFFF, 0xFFFF, 0xFFFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||||
|
DEFINE_GUID(HV_GUID_WILDCARD,
|
||||||
|
0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
|
||||||
|
|
||||||
|
DEFINE_GUID(HV_GUID_CHILDREN,
|
||||||
|
0x90db8b89, 0x0d35, 0x4f79, 0x8c, 0xe9, 0x49, 0xea, 0x0a, 0xc8, 0xb7, 0xcd);
|
||||||
|
DEFINE_GUID(HV_GUID_LOOPBACK,
|
||||||
|
0xe0e16197, 0xdd56, 0x4a10, 0x91, 0x95, 0x5e, 0xe7, 0xa1, 0x55, 0xa8, 0x38);
|
||||||
|
DEFINE_GUID(HV_GUID_PARENT,
|
||||||
|
0xa42e7cda, 0xd03f, 0x480c, 0x9c, 0xc2, 0xa4, 0xde, 0x20, 0xab, 0xb8, 0x78);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Thread wrappers */
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
typedef HANDLE THREAD_HANDLE;
|
||||||
|
|
||||||
|
static inline int thread_create(THREAD_HANDLE *t, void *(*f)(void *), void *arg)
|
||||||
|
{
|
||||||
|
*t = CreateThread(NULL, 0, f, arg, 0, NULL);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int thread_join(THREAD_HANDLE t)
|
||||||
|
{
|
||||||
|
WaitForSingleObject(t, INFINITE);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
typedef pthread_t THREAD_HANDLE;
|
||||||
|
|
||||||
|
static inline int thread_create(THREAD_HANDLE *t, void *(*f)(void *), void *arg)
|
||||||
|
{
|
||||||
|
return pthread_create(t, NULL, f, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int thread_join(THREAD_HANDLE t)
|
||||||
|
{
|
||||||
|
return pthread_join(t, NULL);
|
||||||
|
}
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user