9pmount: Use Linux coding style (mostly)

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
Rolf Neugebauer 2016-09-01 17:22:26 +01:00
parent 530125e0f9
commit 6df7fae1a9
3 changed files with 212 additions and 195 deletions

View File

@ -1,6 +1,3 @@
/*
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -35,37 +32,47 @@ void fatal(const char *msg)
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;
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()"); fatal("asprintf()");
}
char *argv[] = { char *argv[] = {
mount, mount,
"-t", "9p", "-o", options, "-t", "9p", "-o", options,
tag, path, tag, path,
NULL NULL
}; };
pid_t pid = fork();
pid = fork();
if (pid == 0) { if (pid == 0) {
execv(mount, argv); execv(mount, argv);
fatal("execv()"); fatal("execv()");
} }
int status;
if (waitpid(pid, &status, 0) == -1) { res = waitpid(pid, &status, 0);
syslog(LOG_CRIT, "waitpid failed: %d. %s", errno, strerror(errno)); if (res == -1) {
syslog(LOG_CRIT,
"waitpid failed: %d. %s", errno, strerror(errno));
exit(1); exit(1);
} }
return WEXITSTATUS(status); return WEXITSTATUS(status);
} }
static int create_listening_socket(GUID serviceid) { static int create_listening_socket(GUID serviceid)
{
int lsock = -1; int lsock = -1;
SOCKADDR_HV sa; SOCKADDR_HV sa;
int res; 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;
@ -73,26 +80,25 @@ static int create_listening_socket(GUID serviceid) {
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; int sock = -1;
SOCKADDR_HV sa; SOCKADDR_HV sa;
int res; 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;
@ -100,25 +106,25 @@ static int connect_socket(GUID serviceid) {
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; int csock = -1;
SOCKADDR_HV sac; SOCKADDR_HV sac;
socklen_t socklen = sizeof(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;
} }
@ -153,27 +159,27 @@ int main(int argc, char **argv)
}; };
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': case 's':
serviceid = optarg; serviceid = optarg;
break; break;
case 0: case 0:
break; break;
default: default:
usage (argv[0]); usage(argv[0]);
exit (1); exit(1);
} }
} }
if (optind < argc) { if (optind < argc) {
if (strcmp(argv[optind], "listen") == 0) { if (strcmp(argv[optind], "listen") == 0)
mode = LISTEN; mode = LISTEN;
} else if (strcmp(argv[optind], "connect") == 0) { else if (strcmp(argv[optind], "connect") == 0)
mode = CONNECT; mode = CONNECT;
}
optind++; optind++;
} }
if (mode == NONE) { if (mode == NONE) {
@ -181,48 +187,61 @@ int main(int argc, char **argv)
usage(argv[0]); usage(argv[0]);
exit(1); exit(1);
} }
if (optind < argc) {
if (optind < argc)
tag = argv[optind++]; tag = argv[optind++];
}
if (optind < argc) { if (optind < argc)
path = argv[optind++]; path = argv[optind++];
}
if (!tag) { if (!tag) {
fprintf(stderr, "Please supply a tag name\n"); fprintf(stderr, "Please supply a tag name\n");
usage(argv[0]); usage(argv[0]);
exit(1); exit(1);
} }
if (!path) { if (!path) {
fprintf(stderr, "Please supply a path\n"); fprintf(stderr, "Please supply a path\n");
usage(argv[0]); usage(argv[0]);
exit(1); exit(1);
} }
res = parseguid(serviceid, &sid); res = parseguid(serviceid, &sid);
if (res) { if (res) {
fprintf(stderr, "Failed to parse serviceid as GUID: %s\n", serviceid); fprintf(stderr,
"Failed to parse serviceid as GUID: %s\n", serviceid);
usage(argv[0]); usage(argv[0]);
exit(1); exit(1);
} }
openlog(argv[0], LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_DAEMON); openlog(argv[0], LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
for (;;) { for (;;) {
int lsocket;
int sock = -1; int sock = -1;
int r;
if (mode == LISTEN) { if (mode == LISTEN) {
syslog(LOG_INFO, "starting in listening mode with serviceid=%s, tag=%s, path=%s", serviceid, tag, path); syslog(LOG_INFO, "starting in listening mode with serviceid=%s, tag=%s, path=%s", serviceid, tag, path);
int lsocket = create_listening_socket(sid); lsocket = create_listening_socket(sid);
sock = accept_socket(lsocket); sock = accept_socket(lsocket);
close(lsocket); close(lsocket);
} else { } else {
syslog(LOG_INFO, "starting in connect mode with serviceid=%s, tag=%s, path=%s", serviceid, tag, path); syslog(LOG_INFO, "starting in connect mode with serviceid=%s, tag=%s, path=%s", serviceid, tag, path);
sock = connect_socket(sid); sock = connect_socket(sid);
} }
int r = handle(sock, tag, path);
r = handle(sock, tag, path);
close(sock); close(sock);
if (r == 0) { if (r == 0) {
syslog(LOG_INFO, "mount successful for serviceid=%s tag=%s path=%s", serviceid, tag, path); syslog(LOG_INFO, "mount successful for serviceid=%s tag=%s path=%s", serviceid, tag, path);
exit(0); exit(0);
} }
/* This can happen if the client times out the connection after we accept it */
/*
* 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); syslog(LOG_CRIT, "mount failed with %d for serviceid=%s tag=%s path=%s", r, serviceid, tag, path);
sleep(1); /* retry */ sleep(1); /* retry */
} }

View File

@ -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,

View File

@ -33,8 +33,7 @@ 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;