9pmount-vsock: remove Win32 compatibility code

There's no point having an ability to compile this code on
Windows, so simplify it.

Signed-off-by: David Scott <dave.scott@docker.com>
This commit is contained in:
David Scott 2016-05-15 18:38:20 +01:00
parent a772c2b7d6
commit 672d611c8f
5 changed files with 120 additions and 192 deletions

View File

@ -14,7 +14,7 @@
#include <fcntl.h>
#include <err.h>
#include "compat.h"
#include "hvsock.h"
#define NONE 0
#define LISTEN 1
@ -25,50 +25,17 @@ int mode = NONE;
char *default_sid = "C378280D-DA14-42C8-A24E-0DE92A1028E2";
char *mount = "/bin/mount";
/* 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)
void fatal(const char *msg)
{
syslog(LOG_CRIT, "%s Error: %d. %s", msg, errno, strerror(errno));
exit(1);
}
static void handle(SOCKET fd)
static void handle(int fd)
{
char *options = NULL;
if (asprintf(&options, "trans=fd,dfltuid=1001,dfltgid=50,version=9p2000,rfdno=%d,wfdno=%d", fd, fd) < 0){
syslog(LOG_CRIT, "asprintf(): %s", strerror(errno));
exit(1);
fatal("asprintf()");
}
char *argv[] = {
mount,
@ -77,19 +44,17 @@ static void handle(SOCKET fd)
NULL
};
execv(mount, argv);
syslog(LOG_CRIT, "failed to execute %s: %s", mount, strerror(errno));
exit(1);
fatal("execv()");
}
static int create_listening_socket(GUID serviceid) {
SOCKET lsock = INVALID_SOCKET;
int lsock = -1;
SOCKADDR_HV sa;
int res;
lsock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
if (lsock == INVALID_SOCKET) {
sockerr("socket()");
exit(1);
if (lsock == -1) {
fatal("socket()");
}
sa.Family = AF_HYPERV;
@ -98,30 +63,25 @@ static int create_listening_socket(GUID serviceid) {
sa.ServiceId = serviceid;
res = bind(lsock, (const struct sockaddr *)&sa, sizeof(sa));
if (res == SOCKET_ERROR) {
sockerr("bind()");
closesocket(lsock);
exit(1);
if (res == -1) {
fatal("bind()");
}
res = listen(lsock, 1);
if (res == SOCKET_ERROR) {
sockerr("listen()");
closesocket(lsock);
exit(1);
if (res == -1) {
fatal("listen()");
}
return lsock;
}
static int connect_socket(GUID serviceid) {
SOCKET sock = INVALID_SOCKET;
int sock = -1;
SOCKADDR_HV sa;
int res;
sock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
if (sock == INVALID_SOCKET) {
sockerr("socket()");
exit(1);
if (sock == -1) {
fatal("socket()");
}
sa.Family = AF_HYPERV;
@ -130,28 +90,24 @@ static int connect_socket(GUID serviceid) {
sa.ServiceId = serviceid;
res = connect(sock, (const struct sockaddr *)&sa, sizeof(sa));
if (res == SOCKET_ERROR) {
sockerr("connect()");
closesocket(sock);
exit(1);
if (res == -1) {
fatal("connect()");
}
return sock;
}
static int accept_socket(SOCKET lsock) {
SOCKET csock = INVALID_SOCKET;
static int accept_socket(int lsock) {
int csock = -1;
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);
if (csock == -1) {
fatal("accept()");
}
printf("Connect from: "GUID_FMT":"GUID_FMT"\n",
syslog(LOG_INFO, "Connect from: "GUID_FMT":"GUID_FMT"\n",
GUID_ARGS(sac.VmId), GUID_ARGS(sac.ServiceId));
return csock;
}
@ -167,7 +123,7 @@ void usage(char *name)
printf("\t--connect: connect to the parent partition\n");
}
int __cdecl main(int argc, char **argv)
int main(int argc, char **argv)
{
int res = 0;
GUID sid;
@ -221,10 +177,10 @@ int __cdecl main(int argc, char **argv)
exit(1);
}
SOCKET sock = INVALID_SOCKET;
int sock = -1;
if (mode == LISTEN) {
syslog(LOG_INFO, "starting in listening mode with serviceid=%s", serviceid);
SOCKET lsocket = create_listening_socket(sid);
int lsocket = create_listening_socket(sid);
sock = accept_socket(lsocket);
close(lsocket);
} else {

View File

@ -1,18 +1,21 @@
.PHONY: all
DEPS=9pmount-vsock.c compat.h
DEPS=9pmount-vsock.c hvsock.c hvsock.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: 9pmount-vsock.o hvsock.o
gcc -Wall -Werror -o 9pmount-vsock 9pmount-vsock.o hvsock.o -lpthread
9pmount-vsock.o: 9pmount-vsock.c compat.h
9pmount-vsock.o: 9pmount-vsock.c hvsock.h
gcc -Wall -Werror -c 9pmount-vsock.c
hvsock.o: hvsock.c hvsock.h
gcc -Wall -Werror -c hvsock.c
clean:
rm -f 9pmount-vsock
docker images -q 9pmount-vsock:build | xargs docker rmi -f

View File

@ -1,118 +0,0 @@
/*
* 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

View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include "hvsock.h"
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;
}
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);

View File

@ -0,0 +1,49 @@
/* AF_HYPERV definitions and utilities */
#include <errno.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/socket.h>
/* 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}}
/* 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]
extern int parseguid(const char *s, GUID *g);
/* 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;
extern const GUID HV_GUID_ZERO;
extern const GUID HV_GUID_BROADCAST;
extern const GUID HV_GUID_WILDCARD;
extern const GUID HV_GUID_CHILDREN;
extern const GUID HV_GUID_LOOPBACK;
extern const GUID HV_GUID_PARENT;