mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-27 04:28:20 +00:00
9pmount: Use Linux coding style (mostly)
Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
parent
530125e0f9
commit
6df7fae1a9
@ -1,6 +1,3 @@
|
|||||||
/*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -28,202 +25,224 @@ char *mount = "/bin/mount";
|
|||||||
|
|
||||||
void fatal(const char *msg)
|
void fatal(const char *msg)
|
||||||
{
|
{
|
||||||
syslog(LOG_CRIT, "%s Error: %d. %s", msg, errno, strerror(errno));
|
syslog(LOG_CRIT, "%s Error: %d. %s", msg, errno, strerror(errno));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle(int fd, char *tag, char *path)
|
static int handle(int fd, char *tag, char *path)
|
||||||
{
|
{
|
||||||
char *options = NULL;
|
char *options = NULL;
|
||||||
if (asprintf(&options, "trans=fd,dfltuid=1001,dfltgid=50,version=9p2000,msize=4096,rfdno=%d,wfdno=%d", fd, fd) < 0){
|
int status;
|
||||||
fatal("asprintf()");
|
pid_t pid;
|
||||||
}
|
int res;
|
||||||
char *argv[] = {
|
|
||||||
mount,
|
res = asprintf(&options,
|
||||||
"-t", "9p", "-o", options,
|
"trans=fd,dfltuid=1001,dfltgid=50,version=9p2000,msize=4096,rfdno=%d,wfdno=%d",
|
||||||
tag, path,
|
fd, fd);
|
||||||
NULL
|
if (res < 0)
|
||||||
};
|
fatal("asprintf()");
|
||||||
pid_t pid = fork();
|
|
||||||
if (pid == 0) {
|
char *argv[] = {
|
||||||
execv(mount, argv);
|
mount,
|
||||||
fatal("execv()");
|
"-t", "9p", "-o", options,
|
||||||
}
|
tag, path,
|
||||||
int status;
|
NULL
|
||||||
if (waitpid(pid, &status, 0) == -1) {
|
};
|
||||||
syslog(LOG_CRIT, "waitpid failed: %d. %s", errno, strerror(errno));
|
|
||||||
exit(1);
|
pid = fork();
|
||||||
}
|
if (pid == 0) {
|
||||||
return WEXITSTATUS(status);
|
execv(mount, argv);
|
||||||
|
fatal("execv()");
|
||||||
|
}
|
||||||
|
|
||||||
|
res = waitpid(pid, &status, 0);
|
||||||
|
if (res == -1) {
|
||||||
|
syslog(LOG_CRIT,
|
||||||
|
"waitpid failed: %d. %s", errno, strerror(errno));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return WEXITSTATUS(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int create_listening_socket(GUID serviceid) {
|
static int create_listening_socket(GUID serviceid)
|
||||||
int lsock = -1;
|
{
|
||||||
SOCKADDR_HV sa;
|
int lsock = -1;
|
||||||
int res;
|
SOCKADDR_HV sa;
|
||||||
|
int res;
|
||||||
|
|
||||||
lsock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
lsock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
||||||
if (lsock == -1) {
|
if (lsock == -1)
|
||||||
fatal("socket()");
|
fatal("socket()");
|
||||||
}
|
|
||||||
|
|
||||||
sa.Family = AF_HYPERV;
|
sa.Family = AF_HYPERV;
|
||||||
sa.Reserved = 0;
|
sa.Reserved = 0;
|
||||||
sa.VmId = HV_GUID_WILDCARD;
|
sa.VmId = HV_GUID_WILDCARD;
|
||||||
sa.ServiceId = serviceid;
|
sa.ServiceId = serviceid;
|
||||||
|
|
||||||
res = bind(lsock, (const struct sockaddr *)&sa, sizeof(sa));
|
res = bind(lsock, (const struct sockaddr *)&sa, sizeof(sa));
|
||||||
if (res == -1) {
|
if (res == -1)
|
||||||
fatal("bind()");
|
fatal("bind()");
|
||||||
}
|
|
||||||
|
|
||||||
res = listen(lsock, 1);
|
res = listen(lsock, 1);
|
||||||
if (res == -1) {
|
if (res == -1)
|
||||||
fatal("listen()");
|
fatal("listen()");
|
||||||
}
|
|
||||||
return lsock;
|
return lsock;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int connect_socket(GUID serviceid) {
|
static int connect_socket(GUID serviceid)
|
||||||
int sock = -1;
|
{
|
||||||
SOCKADDR_HV sa;
|
int sock = -1;
|
||||||
int res;
|
SOCKADDR_HV sa;
|
||||||
|
int res;
|
||||||
|
|
||||||
sock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
sock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
||||||
if (sock == -1) {
|
if (sock == -1)
|
||||||
fatal("socket()");
|
fatal("socket()");
|
||||||
}
|
|
||||||
|
|
||||||
sa.Family = AF_HYPERV;
|
sa.Family = AF_HYPERV;
|
||||||
sa.Reserved = 0;
|
sa.Reserved = 0;
|
||||||
sa.VmId = HV_GUID_PARENT;
|
sa.VmId = HV_GUID_PARENT;
|
||||||
sa.ServiceId = serviceid;
|
sa.ServiceId = serviceid;
|
||||||
|
|
||||||
res = connect(sock, (const struct sockaddr *)&sa, sizeof(sa));
|
res = connect(sock, (const struct sockaddr *)&sa, sizeof(sa));
|
||||||
if (res == -1) {
|
if (res == -1)
|
||||||
fatal("connect()");
|
fatal("connect()");
|
||||||
}
|
|
||||||
|
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int accept_socket(int lsock) {
|
static int accept_socket(int lsock)
|
||||||
int csock = -1;
|
{
|
||||||
SOCKADDR_HV sac;
|
int csock = -1;
|
||||||
socklen_t socklen = sizeof(sac);
|
SOCKADDR_HV sac;
|
||||||
|
socklen_t socklen = sizeof(sac);
|
||||||
|
|
||||||
csock = accept(lsock, (struct sockaddr *)&sac, &socklen);
|
csock = accept(lsock, (struct sockaddr *)&sac, &socklen);
|
||||||
if (csock == -1) {
|
if (csock == -1)
|
||||||
fatal("accept()");
|
fatal("accept()");
|
||||||
}
|
|
||||||
|
|
||||||
syslog(LOG_INFO, "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));
|
GUID_ARGS(sac.VmId), GUID_ARGS(sac.ServiceId));
|
||||||
return csock;
|
|
||||||
|
return csock;
|
||||||
}
|
}
|
||||||
|
|
||||||
void usage(char *name)
|
void usage(char *name)
|
||||||
{
|
{
|
||||||
printf("%s: mount a 9P filesystem from an hvsock connection\n", name);
|
printf("%s: mount a 9P filesystem from an hvsock connection\n", name);
|
||||||
printf("usage:\n");
|
printf("usage:\n");
|
||||||
printf("\t[--serviceid <guid>] <listen | connect> <tag> <path>\n");
|
printf("\t[--serviceid <guid>] <listen | connect> <tag> <path>\n");
|
||||||
printf("where\n");
|
printf("where\n");
|
||||||
printf("\t--serviceid <guid>: use <guid> as the well-known service GUID\n");
|
printf("\t--serviceid <guid>: use <guid> as the well-known service GUID\n");
|
||||||
printf("\t (defaults to %s)\n", default_sid);
|
printf("\t (defaults to %s)\n", default_sid);
|
||||||
printf("\t--listen: listen forever for incoming AF_HVSOCK connections\n");
|
printf("\t--listen: listen forever for incoming AF_HVSOCK connections\n");
|
||||||
printf("\t--connect: connect to the parent partition\n");
|
printf("\t--connect: connect to the parent partition\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int res = 0;
|
int res = 0;
|
||||||
GUID sid;
|
GUID sid;
|
||||||
int c;
|
int c;
|
||||||
/* Defaults to a testing GUID */
|
/* Defaults to a testing GUID */
|
||||||
char *serviceid = default_sid;
|
char *serviceid = default_sid;
|
||||||
char *tag = NULL;
|
char *tag = NULL;
|
||||||
char *path = NULL;
|
char *path = NULL;
|
||||||
|
|
||||||
opterr = 0;
|
opterr = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
static struct option long_options[] = {
|
static struct option long_options[] = {
|
||||||
/* These options set a flag. */
|
/* These options set a flag. */
|
||||||
{"serviceid", required_argument, NULL, 's'},
|
{"serviceid", required_argument, NULL, 's'},
|
||||||
{0, 0, 0, 0}
|
{0, 0, 0, 0}
|
||||||
};
|
};
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
|
|
||||||
c = getopt_long (argc, argv, "s:", long_options, &option_index);
|
c = getopt_long(argc, argv, "s:", long_options, &option_index);
|
||||||
if (c == -1) break;
|
if (c == -1)
|
||||||
|
break;
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
case 's':
|
||||||
|
serviceid = optarg;
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
usage(argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case 's':
|
if (optind < argc) {
|
||||||
serviceid = optarg;
|
if (strcmp(argv[optind], "listen") == 0)
|
||||||
break;
|
mode = LISTEN;
|
||||||
case 0:
|
else if (strcmp(argv[optind], "connect") == 0)
|
||||||
break;
|
mode = CONNECT;
|
||||||
default:
|
optind++;
|
||||||
usage (argv[0]);
|
}
|
||||||
exit (1);
|
if (mode == NONE) {
|
||||||
}
|
fprintf(stderr, "Please supply either listen or connect\n");
|
||||||
}
|
usage(argv[0]);
|
||||||
if (optind < argc) {
|
exit(1);
|
||||||
if (strcmp(argv[optind], "listen") == 0) {
|
}
|
||||||
mode = LISTEN;
|
|
||||||
} else if (strcmp(argv[optind], "connect") == 0) {
|
|
||||||
mode = CONNECT;
|
|
||||||
}
|
|
||||||
optind++;
|
|
||||||
}
|
|
||||||
if (mode == NONE) {
|
|
||||||
fprintf(stderr, "Please supply either listen or connect\n");
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if (optind < argc) {
|
|
||||||
tag = argv[optind++];
|
|
||||||
}
|
|
||||||
if (optind < argc) {
|
|
||||||
path = argv[optind++];
|
|
||||||
}
|
|
||||||
if (!tag) {
|
|
||||||
fprintf(stderr, "Please supply a tag name\n");
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if (!path) {
|
|
||||||
fprintf(stderr, "Please supply a path\n");
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
res = parseguid(serviceid, &sid);
|
|
||||||
if (res) {
|
|
||||||
fprintf(stderr, "Failed to parse serviceid as GUID: %s\n", serviceid);
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
openlog(argv[0], LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
|
if (optind < argc)
|
||||||
for (;;) {
|
tag = argv[optind++];
|
||||||
int sock = -1;
|
|
||||||
if (mode == LISTEN) {
|
if (optind < argc)
|
||||||
syslog(LOG_INFO, "starting in listening mode with serviceid=%s, tag=%s, path=%s", serviceid, tag, path);
|
path = argv[optind++];
|
||||||
int lsocket = create_listening_socket(sid);
|
|
||||||
sock = accept_socket(lsocket);
|
if (!tag) {
|
||||||
close(lsocket);
|
fprintf(stderr, "Please supply a tag name\n");
|
||||||
} else {
|
usage(argv[0]);
|
||||||
syslog(LOG_INFO, "starting in connect mode with serviceid=%s, tag=%s, path=%s", serviceid, tag, path);
|
exit(1);
|
||||||
sock = connect_socket(sid);
|
}
|
||||||
}
|
|
||||||
int r = handle(sock, tag, path);
|
if (!path) {
|
||||||
close(sock);
|
fprintf(stderr, "Please supply a path\n");
|
||||||
if (r == 0) {
|
usage(argv[0]);
|
||||||
syslog(LOG_INFO, "mount successful for serviceid=%s tag=%s path=%s", serviceid, tag, path);
|
exit(1);
|
||||||
exit(0);
|
}
|
||||||
}
|
|
||||||
/* This can happen if the client times out the connection after we accept it */
|
res = parseguid(serviceid, &sid);
|
||||||
syslog(LOG_CRIT, "mount failed with %d for serviceid=%s tag=%s path=%s", r, serviceid, tag, path);
|
if (res) {
|
||||||
sleep(1); /* retry */
|
fprintf(stderr,
|
||||||
}
|
"Failed to parse serviceid as GUID: %s\n", serviceid);
|
||||||
|
usage(argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
openlog(argv[0], LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
|
||||||
|
for (;;) {
|
||||||
|
int lsocket;
|
||||||
|
int sock = -1;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
if (mode == LISTEN) {
|
||||||
|
syslog(LOG_INFO, "starting in listening mode with serviceid=%s, tag=%s, path=%s", serviceid, tag, path);
|
||||||
|
lsocket = create_listening_socket(sid);
|
||||||
|
sock = accept_socket(lsocket);
|
||||||
|
close(lsocket);
|
||||||
|
} else {
|
||||||
|
syslog(LOG_INFO, "starting in connect mode with serviceid=%s, tag=%s, path=%s", serviceid, tag, path);
|
||||||
|
sock = connect_socket(sid);
|
||||||
|
}
|
||||||
|
|
||||||
|
r = handle(sock, tag, path);
|
||||||
|
close(sock);
|
||||||
|
|
||||||
|
if (r == 0) {
|
||||||
|
syslog(LOG_INFO, "mount successful for serviceid=%s tag=%s path=%s", serviceid, tag, path);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This can happen if the client times out the connection
|
||||||
|
* after we accept it
|
||||||
|
*/
|
||||||
|
syslog(LOG_CRIT, "mount failed with %d for serviceid=%s tag=%s path=%s", r, serviceid, tag, path);
|
||||||
|
sleep(1); /* retry */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,23 +4,23 @@
|
|||||||
|
|
||||||
int parseguid(const char *s, GUID *g)
|
int parseguid(const char *s, GUID *g)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
int p0, p1, p2, p3, p4, p5, p6, p7;
|
int p0, p1, p2, p3, p4, p5, p6, p7;
|
||||||
|
|
||||||
res = sscanf(s, GUID_FMT,
|
res = sscanf(s, GUID_FMT,
|
||||||
&g->Data1, &g->Data2, &g->Data3,
|
&g->Data1, &g->Data2, &g->Data3,
|
||||||
&p0, &p1, &p2, &p3, &p4, &p5, &p6, &p7);
|
&p0, &p1, &p2, &p3, &p4, &p5, &p6, &p7);
|
||||||
if (res != 11)
|
if (res != 11)
|
||||||
return 1;
|
return 1;
|
||||||
g->Data4[0] = p0;
|
g->Data4[0] = p0;
|
||||||
g->Data4[1] = p1;
|
g->Data4[1] = p1;
|
||||||
g->Data4[2] = p2;
|
g->Data4[2] = p2;
|
||||||
g->Data4[3] = p3;
|
g->Data4[3] = p3;
|
||||||
g->Data4[4] = p4;
|
g->Data4[4] = p4;
|
||||||
g->Data4[5] = p5;
|
g->Data4[5] = p5;
|
||||||
g->Data4[6] = p6;
|
g->Data4[6] = p6;
|
||||||
g->Data4[7] = p7;
|
g->Data4[7] = p7;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEFINE_GUID(HV_GUID_ZERO,
|
DEFINE_GUID(HV_GUID_ZERO,
|
||||||
@ -29,7 +29,6 @@ DEFINE_GUID(HV_GUID_BROADCAST,
|
|||||||
0xFFFFFFFF, 0xFFFF, 0xFFFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
|
0xFFFFFFFF, 0xFFFF, 0xFFFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||||
DEFINE_GUID(HV_GUID_WILDCARD,
|
DEFINE_GUID(HV_GUID_WILDCARD,
|
||||||
0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
|
0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
|
||||||
|
|
||||||
DEFINE_GUID(HV_GUID_CHILDREN,
|
DEFINE_GUID(HV_GUID_CHILDREN,
|
||||||
0x90db8b89, 0x0d35, 0x4f79, 0x8c, 0xe9, 0x49, 0xea, 0x0a, 0xc8, 0xb7, 0xcd);
|
0x90db8b89, 0x0d35, 0x4f79, 0x8c, 0xe9, 0x49, 0xea, 0x0a, 0xc8, 0xb7, 0xcd);
|
||||||
DEFINE_GUID(HV_GUID_LOOPBACK,
|
DEFINE_GUID(HV_GUID_LOOPBACK,
|
||||||
|
@ -7,10 +7,10 @@
|
|||||||
|
|
||||||
/* GUID handling */
|
/* GUID handling */
|
||||||
typedef struct _GUID {
|
typedef struct _GUID {
|
||||||
uint32_t Data1;
|
uint32_t Data1;
|
||||||
uint16_t Data2;
|
uint16_t Data2;
|
||||||
uint16_t Data3;
|
uint16_t Data3;
|
||||||
uint8_t Data4[8];
|
uint8_t Data4[8];
|
||||||
} GUID;
|
} GUID;
|
||||||
|
|
||||||
#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
|
#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
|
||||||
@ -33,12 +33,11 @@ extern int parseguid(const char *s, GUID *g);
|
|||||||
#define AF_HYPERV 43
|
#define AF_HYPERV 43
|
||||||
#define HV_PROTOCOL_RAW 1
|
#define HV_PROTOCOL_RAW 1
|
||||||
|
|
||||||
typedef struct _SOCKADDR_HV
|
typedef struct _SOCKADDR_HV {
|
||||||
{
|
unsigned short Family;
|
||||||
unsigned short Family;
|
unsigned short Reserved;
|
||||||
unsigned short Reserved;
|
GUID VmId;
|
||||||
GUID VmId;
|
GUID ServiceId;
|
||||||
GUID ServiceId;
|
|
||||||
} SOCKADDR_HV;
|
} SOCKADDR_HV;
|
||||||
|
|
||||||
extern const GUID HV_GUID_ZERO;
|
extern const GUID HV_GUID_ZERO;
|
||||||
|
Loading…
Reference in New Issue
Block a user