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 <stdlib.h>
#include <string.h>
@ -35,37 +32,47 @@ void fatal(const char *msg)
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){
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_t pid = fork();
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));
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 res;
lsock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
if (lsock == -1) {
if (lsock == -1)
fatal("socket()");
}
sa.Family = AF_HYPERV;
sa.Reserved = 0;
@ -73,26 +80,25 @@ static int create_listening_socket(GUID serviceid) {
sa.ServiceId = serviceid;
res = bind(lsock, (const struct sockaddr *)&sa, sizeof(sa));
if (res == -1) {
if (res == -1)
fatal("bind()");
}
res = listen(lsock, 1);
if (res == -1) {
if (res == -1)
fatal("listen()");
}
return lsock;
}
static int connect_socket(GUID serviceid) {
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) {
if (sock == -1)
fatal("socket()");
}
sa.Family = AF_HYPERV;
sa.Reserved = 0;
@ -100,25 +106,25 @@ static int connect_socket(GUID serviceid) {
sa.ServiceId = serviceid;
res = connect(sock, (const struct sockaddr *)&sa, sizeof(sa));
if (res == -1) {
if (res == -1)
fatal("connect()");
}
return sock;
}
static int accept_socket(int lsock) {
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) {
if (csock == -1)
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));
return csock;
}
@ -153,27 +159,27 @@ int main(int argc, char **argv)
};
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) {
case 's':
serviceid = optarg;
break;
case 0:
break;
default:
usage (argv[0]);
exit (1);
usage(argv[0]);
exit(1);
}
}
if (optind < argc) {
if (strcmp(argv[optind], "listen") == 0) {
if (strcmp(argv[optind], "listen") == 0)
mode = LISTEN;
} else if (strcmp(argv[optind], "connect") == 0) {
else if (strcmp(argv[optind], "connect") == 0)
mode = CONNECT;
}
optind++;
}
if (mode == NONE) {
@ -181,48 +187,61 @@ int main(int argc, char **argv)
usage(argv[0]);
exit(1);
}
if (optind < argc) {
if (optind < argc)
tag = argv[optind++];
}
if (optind < argc) {
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);
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);
int lsocket = create_listening_socket(sid);
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);
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 */
/*
* 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 */
}

View File

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

View File

@ -33,8 +33,7 @@ extern int parseguid(const char *s, GUID *g);
#define AF_HYPERV 43
#define HV_PROTOCOL_RAW 1
typedef struct _SOCKADDR_HV
{
typedef struct _SOCKADDR_HV {
unsigned short Family;
unsigned short Reserved;
GUID VmId;