mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-23 11:00:25 +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 <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -28,202 +25,224 @@ char *mount = "/bin/mount";
|
||||
|
||||
void fatal(const char *msg)
|
||||
{
|
||||
syslog(LOG_CRIT, "%s Error: %d. %s", msg, errno, strerror(errno));
|
||||
exit(1);
|
||||
syslog(LOG_CRIT, "%s Error: %d. %s", msg, errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static int handle(int fd, char *tag, char *path)
|
||||
{
|
||||
char *options = NULL;
|
||||
if (asprintf(&options, "trans=fd,dfltuid=1001,dfltgid=50,version=9p2000,msize=4096,rfdno=%d,wfdno=%d", fd, fd) < 0){
|
||||
fatal("asprintf()");
|
||||
}
|
||||
char *argv[] = {
|
||||
mount,
|
||||
"-t", "9p", "-o", options,
|
||||
tag, path,
|
||||
NULL
|
||||
};
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
execv(mount, argv);
|
||||
fatal("execv()");
|
||||
}
|
||||
int status;
|
||||
if (waitpid(pid, &status, 0) == -1) {
|
||||
syslog(LOG_CRIT, "waitpid failed: %d. %s", errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
return WEXITSTATUS(status);
|
||||
char *options = NULL;
|
||||
int status;
|
||||
pid_t pid;
|
||||
int res;
|
||||
|
||||
res = asprintf(&options,
|
||||
"trans=fd,dfltuid=1001,dfltgid=50,version=9p2000,msize=4096,rfdno=%d,wfdno=%d",
|
||||
fd, fd);
|
||||
if (res < 0)
|
||||
fatal("asprintf()");
|
||||
|
||||
char *argv[] = {
|
||||
mount,
|
||||
"-t", "9p", "-o", options,
|
||||
tag, path,
|
||||
NULL
|
||||
};
|
||||
|
||||
pid = fork();
|
||||
if (pid == 0) {
|
||||
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) {
|
||||
int lsock = -1;
|
||||
SOCKADDR_HV sa;
|
||||
int res;
|
||||
static int create_listening_socket(GUID serviceid)
|
||||
{
|
||||
int lsock = -1;
|
||||
SOCKADDR_HV sa;
|
||||
int res;
|
||||
|
||||
lsock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
||||
if (lsock == -1) {
|
||||
fatal("socket()");
|
||||
}
|
||||
lsock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
||||
if (lsock == -1)
|
||||
fatal("socket()");
|
||||
|
||||
sa.Family = AF_HYPERV;
|
||||
sa.Reserved = 0;
|
||||
sa.VmId = HV_GUID_WILDCARD;
|
||||
sa.ServiceId = serviceid;
|
||||
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 == -1) {
|
||||
fatal("bind()");
|
||||
}
|
||||
res = bind(lsock, (const struct sockaddr *)&sa, sizeof(sa));
|
||||
if (res == -1)
|
||||
fatal("bind()");
|
||||
|
||||
res = listen(lsock, 1);
|
||||
if (res == -1) {
|
||||
fatal("listen()");
|
||||
}
|
||||
return lsock;
|
||||
res = listen(lsock, 1);
|
||||
if (res == -1)
|
||||
fatal("listen()");
|
||||
|
||||
return lsock;
|
||||
}
|
||||
|
||||
static int connect_socket(GUID serviceid) {
|
||||
int sock = -1;
|
||||
SOCKADDR_HV sa;
|
||||
int res;
|
||||
static int connect_socket(GUID serviceid)
|
||||
{
|
||||
int sock = -1;
|
||||
SOCKADDR_HV sa;
|
||||
int res;
|
||||
|
||||
sock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
||||
if (sock == -1) {
|
||||
fatal("socket()");
|
||||
}
|
||||
sock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
||||
if (sock == -1)
|
||||
fatal("socket()");
|
||||
|
||||
sa.Family = AF_HYPERV;
|
||||
sa.Reserved = 0;
|
||||
sa.VmId = HV_GUID_PARENT;
|
||||
sa.ServiceId = serviceid;
|
||||
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 == -1) {
|
||||
fatal("connect()");
|
||||
}
|
||||
res = connect(sock, (const struct sockaddr *)&sa, sizeof(sa));
|
||||
if (res == -1)
|
||||
fatal("connect()");
|
||||
|
||||
return sock;
|
||||
return sock;
|
||||
}
|
||||
|
||||
static int accept_socket(int lsock) {
|
||||
int csock = -1;
|
||||
SOCKADDR_HV sac;
|
||||
socklen_t socklen = sizeof(sac);
|
||||
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 == -1) {
|
||||
fatal("accept()");
|
||||
}
|
||||
csock = accept(lsock, (struct sockaddr *)&sac, &socklen);
|
||||
if (csock == -1)
|
||||
fatal("accept()");
|
||||
|
||||
syslog(LOG_INFO, "Connect from: "GUID_FMT":"GUID_FMT"\n",
|
||||
GUID_ARGS(sac.VmId), GUID_ARGS(sac.ServiceId));
|
||||
return csock;
|
||||
syslog(LOG_INFO, "Connect from: " GUID_FMT ":" GUID_FMT "\n",
|
||||
GUID_ARGS(sac.VmId), GUID_ARGS(sac.ServiceId));
|
||||
|
||||
return csock;
|
||||
}
|
||||
|
||||
void usage(char *name)
|
||||
{
|
||||
printf("%s: mount a 9P filesystem from an hvsock connection\n", name);
|
||||
printf("usage:\n");
|
||||
printf("\t[--serviceid <guid>] <listen | connect> <tag> <path>\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");
|
||||
printf("%s: mount a 9P filesystem from an hvsock connection\n", name);
|
||||
printf("usage:\n");
|
||||
printf("\t[--serviceid <guid>] <listen | connect> <tag> <path>\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 main(int argc, char **argv)
|
||||
{
|
||||
int res = 0;
|
||||
GUID sid;
|
||||
int c;
|
||||
/* Defaults to a testing GUID */
|
||||
char *serviceid = default_sid;
|
||||
char *tag = NULL;
|
||||
char *path = NULL;
|
||||
int res = 0;
|
||||
GUID sid;
|
||||
int c;
|
||||
/* Defaults to a testing GUID */
|
||||
char *serviceid = default_sid;
|
||||
char *tag = NULL;
|
||||
char *path = NULL;
|
||||
|
||||
opterr = 0;
|
||||
while (1) {
|
||||
static struct option long_options[] = {
|
||||
/* These options set a flag. */
|
||||
{"serviceid", required_argument, NULL, 's'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
int option_index = 0;
|
||||
opterr = 0;
|
||||
while (1) {
|
||||
static struct option long_options[] = {
|
||||
/* These options set a flag. */
|
||||
{"serviceid", required_argument, NULL, 's'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
int option_index = 0;
|
||||
|
||||
c = getopt_long (argc, argv, "s:", long_options, &option_index);
|
||||
if (c == -1) break;
|
||||
c = getopt_long(argc, argv, "s:", long_options, &option_index);
|
||||
if (c == -1)
|
||||
break;
|
||||
|
||||
switch (c) {
|
||||
switch (c) {
|
||||
case 's':
|
||||
serviceid = optarg;
|
||||
break;
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
case 's':
|
||||
serviceid = optarg;
|
||||
break;
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
usage (argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
}
|
||||
if (optind < argc) {
|
||||
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);
|
||||
}
|
||||
if (optind < argc) {
|
||||
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);
|
||||
}
|
||||
|
||||
openlog(argv[0], LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
|
||||
for (;;) {
|
||||
int sock = -1;
|
||||
if (mode == LISTEN) {
|
||||
syslog(LOG_INFO, "starting in listening mode with serviceid=%s, tag=%s, path=%s", serviceid, tag, path);
|
||||
int 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);
|
||||
}
|
||||
int 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 */
|
||||
}
|
||||
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);
|
||||
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 res;
|
||||
int p0, p1, p2, p3, p4, p5, p6, p7;
|
||||
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;
|
||||
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,
|
||||
@ -29,7 +29,6 @@ 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,
|
||||
|
@ -7,10 +7,10 @@
|
||||
|
||||
/* GUID handling */
|
||||
typedef struct _GUID {
|
||||
uint32_t Data1;
|
||||
uint16_t Data2;
|
||||
uint16_t Data3;
|
||||
uint8_t Data4[8];
|
||||
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) \
|
||||
@ -33,12 +33,11 @@ extern int parseguid(const char *s, GUID *g);
|
||||
#define AF_HYPERV 43
|
||||
#define HV_PROTOCOL_RAW 1
|
||||
|
||||
typedef struct _SOCKADDR_HV
|
||||
{
|
||||
unsigned short Family;
|
||||
unsigned short Reserved;
|
||||
GUID VmId;
|
||||
GUID ServiceId;
|
||||
typedef struct _SOCKADDR_HV {
|
||||
unsigned short Family;
|
||||
unsigned short Reserved;
|
||||
GUID VmId;
|
||||
GUID ServiceId;
|
||||
} SOCKADDR_HV;
|
||||
|
||||
extern const GUID HV_GUID_ZERO;
|
||||
|
Loading…
Reference in New Issue
Block a user