Merge pull request #465 from rneugeba/c-style

Unify C coding style to the Linux kernel coding style (mostly)
This commit is contained in:
Justin Cormack 2016-09-16 13:18:36 +01:00 committed by GitHub
commit 94cf16b4e9
15 changed files with 2146 additions and 1868 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; {
SOCKADDR_HV sa; SOCKADDR_HV sa;
int lsock;
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; {
SOCKADDR_HV sa; SOCKADDR_HV sa;
int sock;
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; {
SOCKADDR_HV sac; SOCKADDR_HV sac;
socklen_t socklen = sizeof(sac); socklen_t socklen = sizeof(sac);
int csock;
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 sock = -1; int lsocket;
int sock;
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;

View File

@ -12,6 +12,7 @@ int parseguid(const char *s, GUID *g)
&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;
@ -20,6 +21,7 @@ int parseguid(const char *s, GUID *g)
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;
} }
@ -28,7 +30,7 @@ DEFINE_GUID(HV_GUID_ZERO,
DEFINE_GUID(HV_GUID_BROADCAST, 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);

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;

View File

@ -10,45 +10,56 @@
#include "protocol.h" #include "protocol.h"
/* Version 0 of the protocol used this */ /* Version 0 of the protocol used this */
char expected_hello_old[5] = { 'V', 'M', 'N', 'E', 'T' }; char expected_hello_old[5] = {'V', 'M', 'N', 'E', 'T'};
/* Version 1 and later of the protocol used this */ /* Version 1 and later of the protocol used this */
char expected_hello[5] = { 'V', 'M', 'N', '3', 'T' }; char expected_hello[5] = {'V', 'M', 'N', '3', 'T'};
int really_read(int fd, uint8_t *buffer, size_t total){ int really_read(int fd, uint8_t *buffer, size_t total)
{
size_t remaining = total; size_t remaining = total;
ssize_t n; ssize_t n;
while (remaining > 0){
while (remaining > 0) {
n = read(fd, buffer, remaining); n = read(fd, buffer, remaining);
if (n == 0){ if (n == 0) {
syslog(LOG_CRIT, "EOF reading from socket: closing\n"); syslog(LOG_CRIT, "EOF reading from socket: closing\n");
goto err; goto err;
} }
if (n < 0){ if (n < 0) {
syslog(LOG_CRIT, "Failure reading from socket: closing: %s", strerror(errno)); syslog(LOG_CRIT,
"Failure reading from socket: closing: %s",
strerror(errno));
goto err; goto err;
} }
remaining -= (size_t)n; remaining -= (size_t) n;
buffer = buffer + n; buffer = buffer + n;
} }
return 0; return 0;
err: err:
/* On error: stop reading from the socket and trigger a clean shutdown */ /*
* On error: stop reading from the socket and trigger a clean
* shutdown
*/
shutdown(fd, SHUT_RD); shutdown(fd, SHUT_RD);
return -1; return -1;
} }
int really_write(int fd, uint8_t *buffer, size_t total){ int really_write(int fd, uint8_t *buffer, size_t total)
{
size_t remaining = total; size_t remaining = total;
ssize_t n; ssize_t n;
while (remaining > 0){
while (remaining > 0) {
n = write(fd, buffer, remaining); n = write(fd, buffer, remaining);
if (n == 0){ if (n == 0) {
syslog(LOG_CRIT, "EOF writing to socket: closing"); syslog(LOG_CRIT, "EOF writing to socket: closing");
goto err; goto err;
} }
if (n < 0){ if (n < 0) {
syslog(LOG_CRIT, "Failure writing to socket: closing: %s", strerror(errno)); syslog(LOG_CRIT,
"Failure writing to socket: closing: %s",
strerror(errno));
goto err; goto err;
} }
remaining -= (size_t) n; remaining -= (size_t) n;
@ -61,21 +72,36 @@ err:
return -1; return -1;
} }
struct init_message *create_init_message(){ struct init_message *create_init_message()
struct init_message *m = (struct init_message*) malloc(sizeof(struct init_message)); {
struct init_message *m;
m = malloc(sizeof(struct init_message));
if (!m)
return NULL;
bzero(m, sizeof(struct init_message)); bzero(m, sizeof(struct init_message));
memcpy(&m->hello[0], &expected_hello[0], sizeof(m->hello)); memcpy(&m->hello[0], &expected_hello[0], sizeof(m->hello));
m->version = CURRENT_VERSION; m->version = CURRENT_VERSION;
memset(&m->commit[0], 0, sizeof(m->commit)); memset(&m->commit[0], 0, sizeof(m->commit));
return m; return m;
} }
char *print_init_message(struct init_message *m) { char *print_init_message(struct init_message *m)
{
char tmp[41]; char tmp[41];
memcpy(&tmp[0], &m->commit[0], 40); memcpy(&tmp[0], &m->commit[0], 40);
tmp[40] = '\000'; tmp[40] = '\000';
char *buffer = (char*) malloc(80); char *buffer;
int n = snprintf(buffer, 80, "version %d, commit %s", m->version, tmp); int n;
buffer = malloc(80);
if (!buffer)
return NULL;
n = snprintf(buffer, 80, "version %d, commit %s", m->version, tmp);
if (n < 0) { if (n < 0) {
perror("Failed to format init_message"); perror("Failed to format init_message");
exit(1); exit(1);
@ -83,87 +109,121 @@ char *print_init_message(struct init_message *m) {
return buffer; return buffer;
} }
int read_init_message(int fd, struct init_message *ci) { int read_init_message(int fd, struct init_message *ci)
{
int res;
bzero(ci, sizeof(struct init_message)); bzero(ci, sizeof(struct init_message));
if (really_read(fd, (uint8_t*) &ci->hello[0], sizeof(ci->hello)) == -1){
res = really_read(fd, (uint8_t *)&ci->hello[0], sizeof(ci->hello));
if (res == -1) {
syslog(LOG_CRIT, "Failed to read hello from client"); syslog(LOG_CRIT, "Failed to read hello from client");
return -1; return -1;
} }
if (memcmp(&ci->hello[0], &expected_hello_old[0], sizeof(expected_hello_old)) == 0) {
res = memcmp(&ci->hello[0],
&expected_hello_old[0], sizeof(expected_hello_old));
if (res == 0) {
ci->version = 0; ci->version = 0;
return 0; return 0;
} }
if (memcmp(&ci->hello[0], &expected_hello[0], sizeof(expected_hello)) != 0) {
res = memcmp(&ci->hello[0],
&expected_hello[0], sizeof(expected_hello));
if (res != 0) {
syslog(LOG_CRIT, "Failed to read header magic from client"); syslog(LOG_CRIT, "Failed to read header magic from client");
return -1; return -1;
} }
if (really_read(fd, (uint8_t*) &ci->version, sizeof(ci->version)) == -1){
res = really_read(fd, (uint8_t *)&ci->version, sizeof(ci->version));
if (res == -1) {
syslog(LOG_CRIT, "Failed to read header version from client"); syslog(LOG_CRIT, "Failed to read header version from client");
return -1; return -1;
} }
if (really_read(fd, (uint8_t*) &ci->commit[0], sizeof(ci->commit)) == -1){
res = really_read(fd, (uint8_t *)&ci->commit[0], sizeof(ci->commit));
if (res == -1) {
syslog(LOG_CRIT, "Failed to read header hash from client"); syslog(LOG_CRIT, "Failed to read header hash from client");
return -1; return -1;
} }
return 0; return 0;
} }
int write_init_message(int fd, struct init_message *ci) { int write_init_message(int fd, struct init_message *ci)
if (really_write(fd, (uint8_t*) &ci->hello[0], sizeof(ci->hello)) == -1){ {
int res;
res = really_write(fd, (uint8_t *)&ci->hello[0], sizeof(ci->hello));
if (res == -1) {
syslog(LOG_CRIT, "Failed to write hello to client"); syslog(LOG_CRIT, "Failed to write hello to client");
return -1; return -1;
} }
if (ci->version > 0) { if (ci->version > 0) {
if (really_write(fd, (uint8_t*) &ci->version, sizeof(ci->version)) == -1){ res = really_write(fd, (uint8_t *)&ci->version,
sizeof(ci->version));
if (res == -1) {
syslog(LOG_CRIT, "Failed to write version to client"); syslog(LOG_CRIT, "Failed to write version to client");
return -1; return -1;
} }
if (really_write(fd, (uint8_t*) &ci->commit[0], sizeof(ci->commit)) == -1){ res = really_write(fd, (uint8_t *)&ci->commit[0],
syslog(LOG_CRIT, "Failed to write header hash to client"); sizeof(ci->commit));
if (res == -1) {
syslog(LOG_CRIT,
"Failed to write header hash to client");
return -1; return -1;
} }
} }
return 0; return 0;
} }
int read_vif_info(int fd, struct vif_info *vif) { int read_vif_info(int fd, struct vif_info *vif)
{
uint8_t buffer[10]; uint8_t buffer[10];
if (really_read(fd, &buffer[0], sizeof(buffer)) == -1){
if (really_read(fd, &buffer[0], sizeof(buffer)) == -1) {
syslog(LOG_CRIT, "Failed to read vif info from client"); syslog(LOG_CRIT, "Failed to read vif info from client");
return -1; return -1;
} }
vif->mtu = (size_t) (buffer[0] | (buffer[1] << 8));
vif->max_packet_size = (size_t) (buffer[2] | (buffer[3] << 8)); vif->mtu = (size_t)(buffer[0] | (buffer[1] << 8));
vif->max_packet_size = (size_t)(buffer[2] | (buffer[3] << 8));
memcpy(vif->mac, &buffer[4], 6); memcpy(vif->mac, &buffer[4], 6);
return 0; return 0;
} }
int write_vif_info(int fd, struct vif_info *vif) { int write_vif_info(int fd, struct vif_info *vif)
{
uint8_t buffer[10]; uint8_t buffer[10];
buffer[0] = (uint8_t) ((vif->mtu >> 0) & 0xff); buffer[0] = (uint8_t) ((vif->mtu >> 0) & 0xff);
buffer[1] = (uint8_t) ((vif->mtu >> 8) & 0xff); buffer[1] = (uint8_t) ((vif->mtu >> 8) & 0xff);
buffer[2] = (uint8_t) ((vif->max_packet_size >> 0) & 0xff); buffer[2] = (uint8_t) ((vif->max_packet_size >> 0) & 0xff);
buffer[3] = (uint8_t) ((vif->max_packet_size >> 8) & 0xff); buffer[3] = (uint8_t) ((vif->max_packet_size >> 8) & 0xff);
memcpy(&buffer[0] + 4, &(vif->mac)[0], 6); memcpy(&buffer[0] + 4, &(vif->mac)[0], 6);
if (really_write(fd, &buffer[0], sizeof(buffer)) == -1){
if (really_write(fd, &buffer[0], sizeof(buffer)) == -1) {
syslog(LOG_CRIT, "Failed to write vif into to client"); syslog(LOG_CRIT, "Failed to write vif into to client");
return -1; return -1;
} }
return 0; return 0;
} }
int write_command(int fd, enum command *c) { int write_command(int fd, enum command *c)
{
uint8_t command = *c; uint8_t command = *c;
if (really_write(fd, (uint8_t*) &command, sizeof(command)) == -1){
if (really_write(fd, (uint8_t *)&command, sizeof(command)) == -1) {
syslog(LOG_CRIT, "Failed to write command to client"); syslog(LOG_CRIT, "Failed to write command to client");
return -1; return -1;
} }
return 0; return 0;
} }
int write_ethernet_args(int fd, struct ethernet_args *args){ int write_ethernet_args(int fd, struct ethernet_args *args)
if (really_write(fd, (uint8_t*) &args->uuid_string[0], 36) == -1){ {
if (really_write(fd, (uint8_t *)&args->uuid_string[0], 36) == -1) {
syslog(LOG_CRIT, "Failed to write ethernet args to client"); syslog(LOG_CRIT, "Failed to write ethernet args to client");
return -1; return -1;
} }

View File

@ -13,8 +13,11 @@ struct init_message {
char commit[40]; /* git sha of the compiled commit */ char commit[40]; /* git sha of the compiled commit */
}; };
/* This should be bumped whenever we add something (like a feature or a bugfix) /*
and we wish the UI to be able to detect when to trigger a reinstall. */ * This should be bumped whenever we add something (like a feature or a
* bugfix) and we wish the UI to be able to detect when to trigger a
* reinstall.
*/
#define CURRENT_VERSION 13 #define CURRENT_VERSION 13
extern struct init_message *create_init_message(void); extern struct init_message *create_init_message(void);

View File

@ -1,6 +1,3 @@
/*
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -25,13 +22,12 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include "hvsock.h" #include "hvsock.h"
#include "protocol.h" #include "protocol.h"
int daemon_flag = 0; int daemon_flag;
int listen_flag = 0; int listen_flag;
int connect_flag = 0; int connect_flag;
char *default_sid = "30D48B34-7D27-4B0B-AAAF-BBBED334DD59"; char *default_sid = "30D48B34-7D27-4B0B-AAAF-BBBED334DD59";
@ -41,39 +37,44 @@ void fatal(const char *msg)
exit(1); exit(1);
} }
int alloc_tap(const char *dev) { int alloc_tap(const char *dev)
int fd; {
struct ifreq ifr;
const char *clonedev = "/dev/net/tun"; const char *clonedev = "/dev/net/tun";
if ((fd = open(clonedev, O_RDWR)) == -1) { struct ifreq ifr;
int persist = 1;
int fd;
fd = open(clonedev, O_RDWR);
if (fd == -1)
fatal("Failed to open /dev/net/tun"); fatal("Failed to open /dev/net/tun");
}
memset(&ifr, 0, sizeof(ifr)); memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
strncpy(ifr.ifr_name, dev, IFNAMSIZ); strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if (ioctl(fd, TUNSETIFF, (void*) &ifr) < 0) { if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0)
fatal("TUNSETIFF failed"); fatal("TUNSETIFF failed");
}
int persist = 1; if (ioctl(fd, TUNSETPERSIST, persist) < 0)
if (ioctl(fd, TUNSETPERSIST, persist) < 0) {
fatal("TUNSETPERSIST failed"); fatal("TUNSETPERSIST failed");
}
syslog(LOG_INFO, "successfully created TAP device %s", dev); syslog(LOG_INFO, "successfully created TAP device %s", dev);
return fd; return fd;
} }
void set_macaddr(const char *dev, uint8_t *mac) { void set_macaddr(const char *dev, uint8_t *mac)
int fd; {
struct ifreq ifq; struct ifreq ifq;
int fd;
fd = socket(PF_INET, SOCK_DGRAM, 0); fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd == -1)
fatal("Could not get socket to set MAC address");
strcpy(ifq.ifr_name, dev); strcpy(ifq.ifr_name, dev);
memcpy(&ifq.ifr_hwaddr.sa_data[0], mac, 6); memcpy(&ifq.ifr_hwaddr.sa_data[0], mac, 6);
ifq.ifr_hwaddr.sa_family = ARPHRD_ETHER; ifq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
if (ioctl(fd, SIOCSIFHWADDR, &ifq) == -1) { if (ioctl(fd, SIOCSIFHWADDR, &ifq) == -1)
fatal("SIOCSIFHWADDR failed"); fatal("SIOCSIFHWADDR failed");
}
close(fd); close(fd);
} }
@ -81,31 +82,40 @@ void set_macaddr(const char *dev, uint8_t *mac) {
/* Negotiate a vmnet connection, returns 0 on success and 1 on error. */ /* Negotiate a vmnet connection, returns 0 on success and 1 on error. */
int negotiate(int fd, struct vif_info *vif) int negotiate(int fd, struct vif_info *vif)
{ {
/* Negotiate with com.docker.slirp */ enum command command = ethernet;
struct init_message *me = create_init_message(); struct init_message *me;
if (write_init_message(fd, me) == -1) { struct ethernet_args args;
goto err;
}
struct init_message you; struct init_message you;
if (read_init_message(fd, &you) == -1) { char *txt;
me = create_init_message();
if (!me)
goto err; goto err;
}
char *txt = print_init_message(&you); if (write_init_message(fd, me) == -1)
goto err;
if (read_init_message(fd, &you) == -1)
goto err;
txt = print_init_message(&you);
if (!txt)
goto err;
syslog(LOG_INFO, "Server reports %s", txt); syslog(LOG_INFO, "Server reports %s", txt);
free(txt); free(txt);
enum command command = ethernet;
if (write_command(fd, &command) == -1) { if (write_command(fd, &command) == -1)
goto err; goto err;
}
struct ethernet_args args;
/* We don't need a uuid */ /* We don't need a uuid */
memset(&args.uuid_string[0], 0, sizeof(args.uuid_string)); memset(&args.uuid_string[0], 0, sizeof(args.uuid_string));
if (write_ethernet_args(fd, &args) == -1) { if (write_ethernet_args(fd, &args) == -1)
goto err; goto err;
}
if (read_vif_info(fd, vif) == -1) { if (read_vif_info(fd, vif) == -1)
goto err; goto err;
}
return 0; return 0;
err: err:
syslog(LOG_CRIT, "Failed to negotiate vmnet connection"); syslog(LOG_CRIT, "Failed to negotiate vmnet connection");
@ -117,95 +127,105 @@ err:
struct connection { struct connection {
int fd; /* Hyper-V socket with vmnet protocol */ int fd; /* Hyper-V socket with vmnet protocol */
int tapfd; /* TAP device with ethernet frames */ int tapfd; /* TAP device with ethernet frames */
struct vif_info vif; /* Contains VIF MAC, MTU etc, received from server */ struct vif_info vif; /* Contains MAC, MTU etc, received from server */
}; };
static void* vmnet_to_tap(void *arg) static void *vmnet_to_tap(void *arg)
{ {
int length, n; struct connection *connection = (struct connection *)arg;
struct connection *connection = (struct connection*) arg;
uint8_t header[2];
uint8_t buffer[2048]; uint8_t buffer[2048];
uint8_t header[2];
int length, n;
for (;;) { for (;;) {
if (really_read(connection->fd, &header[0], 2) == -1){ if (really_read(connection->fd, &header[0], 2) == -1)
fatal("Failed to read a packet header from host"); fatal("Failed to read a packet header from host");
}
length = (header[0] & 0xff) | ((header[1] & 0xff) << 8); length = (header[0] & 0xff) | ((header[1] & 0xff) << 8);
if (length > sizeof(buffer)) { if (length > sizeof(buffer)) {
syslog(LOG_CRIT, "Received an over-large packet: %d > %ld", length, sizeof(buffer)); syslog(LOG_CRIT,
"Received an over-large packet: %d > %ld",
length, sizeof(buffer));
exit(1); exit(1);
} }
if (really_read(connection->fd, &buffer[0], length) == -1){
syslog(LOG_CRIT, "Failed to read packet contents from host"); if (really_read(connection->fd, &buffer[0], length) == -1) {
syslog(LOG_CRIT,
"Failed to read packet contents from host");
exit(1); exit(1);
} }
n = write(connection->tapfd, &buffer[0], length); n = write(connection->tapfd, &buffer[0], length);
if (n != length) { if (n != length) {
syslog(LOG_CRIT, "Failed to write %d bytes to tap device (wrote %d)", length, n); syslog(LOG_CRIT,
"Failed to write %d bytes to tap device (wrote %d)", length, n);
exit(1); exit(1);
} }
} }
} }
static void* tap_to_vmnet(void *arg) static void *tap_to_vmnet(void *arg)
{ {
int length; struct connection *connection = (struct connection *)arg;
struct connection *connection = (struct connection*) arg;
uint8_t header[2];
uint8_t buffer[2048]; uint8_t buffer[2048];
uint8_t header[2];
int length;
for (;;) { for (;;) {
length = read(connection->tapfd, &buffer[0], sizeof(buffer)); length = read(connection->tapfd, &buffer[0], sizeof(buffer));
if (length == -1) { if (length == -1) {
if (errno == ENXIO) { if (errno == ENXIO)
fatal("tap device has gone down"); fatal("tap device has gone down");
}
syslog(LOG_WARNING, "ignoring error %d", errno); syslog(LOG_WARNING, "ignoring error %d", errno);
/* This is what mirage-net-unix does. Is it a good idea really? */ /*
* This is what mirage-net-unix does. Is it a good
* idea really?
*/
continue; continue;
} }
header[0] = (length >> 0) & 0xff; header[0] = (length >> 0) & 0xff;
header[1] = (length >> 8) & 0xff; header[1] = (length >> 8) & 0xff;
if (really_write(connection->fd, &header[0], 2) == -1){ if (really_write(connection->fd, &header[0], 2) == -1)
fatal("Failed to write packet header"); fatal("Failed to write packet header");
}
if (really_write(connection->fd, &buffer[0], length) == -1) { if (really_write(connection->fd, &buffer[0], length) == -1)
fatal("Failed to write packet body"); fatal("Failed to write packet body");
} }
}
return NULL; return NULL;
} }
/* Handle a connection by exchanging ethernet frames forever. /*
* Handle a connection by exchanging ethernet frames forever.
*/ */
static void handle(struct connection *connection) static void handle(struct connection *connection)
{ {
pthread_t v2t, t2v; pthread_t v2t, t2v;
if (pthread_create(&v2t, NULL, vmnet_to_tap, connection) != 0){ if (pthread_create(&v2t, NULL, vmnet_to_tap, connection) != 0)
fatal("Failed to create the vmnet_to_tap thread"); fatal("Failed to create the vmnet_to_tap thread");
}
if (pthread_create(&t2v, NULL, tap_to_vmnet, connection) != 0){ if (pthread_create(&t2v, NULL, tap_to_vmnet, connection) != 0)
fatal("Failed to create the tap_to_vmnet thread"); fatal("Failed to create the tap_to_vmnet thread");
}
if (pthread_join(v2t, NULL) != 0){ if (pthread_join(v2t, NULL) != 0)
fatal("Failed to join the vmnet_to_tap thread"); fatal("Failed to join the vmnet_to_tap thread");
}
if (pthread_join(t2v, NULL) != 0){ if (pthread_join(t2v, NULL) != 0)
fatal("Failed to join the tap_to_vmnet thread"); fatal("Failed to join the tap_to_vmnet thread");
}
} }
static int create_listening_socket(GUID serviceid) { static int create_listening_socket(GUID serviceid)
int lsock = -1; {
SOCKADDR_HV sa; SOCKADDR_HV sa;
int lsock;
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;
@ -213,26 +233,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, SOMAXCONN); res = listen(lsock, SOMAXCONN);
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; SOCKADDR_HV sa;
int sock;
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;
@ -240,37 +259,38 @@ 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; {
SOCKADDR_HV sac; SOCKADDR_HV sac;
socklen_t socklen = sizeof(sac); socklen_t socklen = sizeof(sac);
int csock;
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 write_pidfile(const char *pidfile) { void write_pidfile(const char *pidfile)
{
pid_t pid = getpid(); pid_t pid = getpid();
char * pid_s; char *pid_s;
FILE *file; FILE *file;
int len; int len;
if (asprintf(&pid_s, "%lld", (long long) pid) == -1) { if (asprintf(&pid_s, "%lld", (long long)pid) == -1)
fatal("Failed to allocate pidfile string"); fatal("Failed to allocate pidfile string");
}
len = strlen(pid_s); len = strlen(pid_s);
file = fopen(pidfile, "w"); file = fopen(pidfile, "w");
if (file == NULL) { if (file == NULL) {
@ -278,32 +298,40 @@ void write_pidfile(const char *pidfile) {
exit(1); exit(1);
} }
if (fwrite(pid_s, 1, len, file) != len) { if (fwrite(pid_s, 1, len, file) != len)
fatal("Failed to write pid to pidfile"); fatal("Failed to write pid to pidfile");
}
fclose(file); fclose(file);
free(pid_s); free(pid_s);
} }
void daemonize(const char *pidfile){ void daemonize(const char *pidfile)
pid_t pid = fork (); {
if (pid == -1) { pid_t pid;
int null;
pid = fork();
if (pid == -1)
fatal("Failed to fork()"); fatal("Failed to fork()");
}
else if (pid != 0) else if (pid != 0)
exit(0); exit(0);
if (setsid () == -1) {
if (setsid() == -1)
fatal("Failed to setsid()"); fatal("Failed to setsid()");
}
if (chdir ("/") == -1) { if (chdir("/") == -1)
fatal("Failed to chdir()"); fatal("Failed to chdir()");
}
int null = open("/dev/null", O_RDWR); null = open("/dev/null", O_RDWR);
if (null == -1)
fatal("Failed to open /dev/null");
dup2(null, STDIN_FILENO); dup2(null, STDIN_FILENO);
dup2(null, STDOUT_FILENO); dup2(null, STDOUT_FILENO);
dup2(null, STDERR_FILENO); dup2(null, STDERR_FILENO);
close(null); close(null);
if (pidfile) write_pidfile(pidfile);
if (pidfile)
write_pidfile(pidfile);
} }
void usage(char *name) void usage(char *name)
@ -324,16 +352,21 @@ void usage(char *name)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int res = 0;
GUID sid;
int c;
/* Defaults to a testing GUID */
char *serviceid = default_sid; char *serviceid = default_sid;
struct connection connection;
char *tap = "eth1"; char *tap = "eth1";
char *pidfile = NULL; char *pidfile = NULL;
int lsocket = -1;
int sock = -1;
int res = 0;
int status;
pid_t child;
int tapfd;
GUID sid;
int c;
opterr = 0; int option_index;
while (1) { int log_flags = LOG_CONS | LOG_NDELAY;
static struct option long_options[] = { static struct option long_options[] = {
/* These options set a flag. */ /* These options set a flag. */
{"daemon", no_argument, &daemon_flag, 1}, {"daemon", no_argument, &daemon_flag, 1},
@ -344,10 +377,15 @@ int main(int argc, char **argv)
{"connect", no_argument, &connect_flag, 1}, {"connect", no_argument, &connect_flag, 1},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
int option_index = 0;
c = getopt_long (argc, argv, "ds:t:p:", long_options, &option_index); opterr = 0;
if (c == -1) break; while (1) {
option_index = 0;
c = getopt_long(argc, argv, "ds:t:p:",
long_options, &option_index);
if (c == -1)
break;
switch (c) { switch (c) {
case 'd': case 'd':
@ -365,18 +403,21 @@ int main(int argc, char **argv)
case 0: case 0:
break; break;
default: default:
usage (argv[0]); usage(argv[0]);
exit (1); exit(1);
} }
} }
if ((listen_flag && connect_flag) || !(listen_flag || connect_flag)){
if ((listen_flag && connect_flag) || !(listen_flag || connect_flag)) {
fprintf(stderr, "Please supply either the --listen or --connect flag, but not both.\n"); fprintf(stderr, "Please supply either the --listen or --connect flag, but not both.\n");
exit(1); exit(1);
} }
if (daemon_flag && !pidfile){
if (daemon_flag && !pidfile) {
fprintf(stderr, "For daemon mode, please supply a --pidfile argument.\n"); fprintf(stderr, "For daemon mode, please supply a --pidfile argument.\n");
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);
@ -384,19 +425,14 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
int log_flags = LOG_CONS | LOG_NDELAY; if (!daemon_flag)
if (!daemon_flag) {
log_flags |= LOG_PERROR; log_flags |= LOG_PERROR;
}
openlog(argv[0], log_flags, LOG_DAEMON); openlog(argv[0], log_flags, LOG_DAEMON);
int tapfd = alloc_tap(tap); tapfd = alloc_tap(tap);
struct connection connection;
connection.tapfd = tapfd; connection.tapfd = tapfd;
int sock = -1;
int lsocket = -1;
if (listen_flag) { if (listen_flag) {
syslog(LOG_INFO, "starting in listening mode with serviceid=%s and tap=%s", serviceid, tap); syslog(LOG_INFO, "starting in listening mode with serviceid=%s and tap=%s", serviceid, tap);
lsocket = create_listening_socket(sid); lsocket = create_listening_socket(sid);
@ -409,17 +445,18 @@ int main(int argc, char **argv)
close(sock); close(sock);
sock = -1; sock = -1;
} }
if (listen_flag) {
if (listen_flag)
sock = accept_socket(lsocket); sock = accept_socket(lsocket);
} else { else
sock = connect_socket(sid); sock = connect_socket(sid);
}
connection.fd = sock; connection.fd = sock;
if (negotiate(sock, &connection.vif) != 0) { if (negotiate(sock, &connection.vif) != 0) {
sleep(1); sleep(1);
continue; continue;
} }
syslog(LOG_INFO, "VMNET VIF has MAC %02x:%02x:%02x:%02x:%02x:%02x", syslog(LOG_INFO, "VMNET VIF has MAC %02x:%02x:%02x:%02x:%02x:%02x",
connection.vif.mac[0], connection.vif.mac[1], connection.vif.mac[2], connection.vif.mac[0], connection.vif.mac[1], connection.vif.mac[2],
connection.vif.mac[3], connection.vif.mac[4], connection.vif.mac[5] connection.vif.mac[3], connection.vif.mac[4], connection.vif.mac[5]
@ -431,15 +468,24 @@ int main(int argc, char **argv)
daemon_flag = 0; daemon_flag = 0;
daemonize(pidfile); daemonize(pidfile);
} }
/* Run the multithreaded part in a subprocess. On error the process will
exit() which tears down all the threads */ /*
pid_t child = fork(); * Run the multithreaded part in a subprocess. On error the
* process will exit() which tears down all the threads
*/
child = fork();
if (child == 0) { if (child == 0) {
handle(&connection); handle(&connection);
/* should never happen but just in case of a logic bug in handle */ /*
* should never happen but just in case of a logic
* bug in handle
*/
exit(1); exit(1);
} }
int status;
while (waitpid(child, &status, 0) == -1) { } for (;;) {
if (waitpid(child, &status, 0) != -1)
break;
}
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,37 +1,9 @@
#ifndef _TRANSFUSED_H_
#define _TRANSFUSED_H_
#include <pthread.h> #include <pthread.h>
#include <sys/socket.h> #include <sys/socket.h>
typedef struct {
char * server;
char * socket;
char * fusermount;
char * pidfile;
char * logfile;
int logfile_fd;
int ctl_sock;
int data_sock;
pthread_mutex_t ctl_lock;
} parameters;
typedef struct {
parameters * params;
char * type_descr;
char * mount_point;
struct sockaddr sa_client;
socklen_t socklen_client;
int sock;
} connection_t;
pthread_attr_t detached;
void * must_malloc(char *const descr, size_t size);
void lock(char *const descr, pthread_mutex_t * mutex);
void unlock(char *const descr, pthread_mutex_t * mutex);
void write_exactly(char * descr, int fd, void * buf, size_t nbyte);
#define IN_BUFSZ ((1 << 20) + 16) #define IN_BUFSZ ((1 << 20) + 16)
#define OUT_BUFSZ ((1 << 20) + 64) #define OUT_BUFSZ ((1 << 20) + 64)
#define EVENT_BUFSZ 4096 #define EVENT_BUFSZ 4096
@ -49,7 +21,7 @@ void write_exactly(char * descr, int fd, void * buf, size_t nbyte);
#define TRUNCATE_SYSCALL 4 #define TRUNCATE_SYSCALL 4
#define CHMOD_SYSCALL 5 #define CHMOD_SYSCALL 5
#define MKNOD_REG_SYSCALL 6 #define MKNOD_REG_SYSCALL 6
// these could be turned into an enum probably but... C standard nausea /* these could be turned into an enum probably but...C standard nausea */
#define MOUNT_SUITABILITY_REQUEST 1 #define MOUNT_SUITABILITY_REQUEST 1
#define EXPORT_SUITABILITY_REQUEST 2 #define EXPORT_SUITABILITY_REQUEST 2
@ -59,3 +31,33 @@ void write_exactly(char * descr, int fd, void * buf, size_t nbyte);
#define PONG_REPLY 3 #define PONG_REPLY 3
#define MOUNT_SUITABILITY_REPLY 4 #define MOUNT_SUITABILITY_REPLY 4
#define TRANSFUSE_NOTIFY_CHANNEL 5 #define TRANSFUSE_NOTIFY_CHANNEL 5
typedef struct {
char *server;
char *socket;
char *fusermount;
char *pidfile;
char *logfile;
int logfile_fd;
int ctl_sock;
int data_sock;
pthread_mutex_t ctl_lock;
} parameters;
typedef struct {
parameters *params;
char *type_descr;
char *mount_point;
struct sockaddr sa_client;
socklen_t socklen_client;
int sock;
} connection_t;
pthread_attr_t detached;
void *must_malloc(char *const descr, size_t size);
void lock(char *const descr, pthread_mutex_t *mutex);
void unlock(char *const descr, pthread_mutex_t *mutex);
void write_exactly(char *descr, int fd, void *buf, size_t nbyte);
#endif /* _TRANSFUSED_H_ */

View File

@ -15,70 +15,73 @@
#include <inttypes.h> #include <inttypes.h>
#include "transfused.h" #include "transfused.h"
#include "transfused_log.h"
void log_timestamp(int fd) { void log_timestamp(int fd)
{
char timestamp[26]; char timestamp[26];
int msec; int msec;
struct tm* tm_info; struct tm *tm_info;
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
msec = lrint(tv.tv_usec/1000.0); msec = lrint(tv.tv_usec / 1000.0);
if (msec >= 1000) { if (msec >= 1000) {
msec -= 1000; msec -= 1000;
tv.tv_sec++; tv.tv_sec++;
} }
tm_info = localtime(&tv.tv_sec); tm_info = localtime(&tv.tv_sec);
strftime(timestamp, 26, "%Y-%m-%d %H:%M:%S", tm_info); strftime(timestamp, 26, "%Y-%m-%d %H:%M:%S", tm_info);
dprintf(fd, "%s.%03d ", timestamp, msec); dprintf(fd, "%s.%03d ", timestamp, msec);
} }
void die void vlog_sock_locked(int fd, uint16_t msg_type, const char *fmt, va_list args)
(int exit_code, parameters * params, const char * parg, const char * fmt, ...); {
void vlog_sock_locked
(int fd, uint16_t msg_type, const char * fmt, va_list args) {
int rc, len; int rc, len;
va_list targs; va_list targs;
char * fill; char *fill;
va_copy(targs, args); va_copy(targs, args);
len = vsnprintf(NULL, 0, fmt, targs); len = vsnprintf(NULL, 0, fmt, targs);
if (len < 0) die(1, NULL, NULL, "Couldn't log due to vsnprintf failure"); if (len < 0)
die(1, NULL, NULL, "Couldn't log due to vsnprintf failure");
va_end(targs); va_end(targs);
rc = len + 4 + 2; // 4 for length itself and 2 for message type /* 4 for length itself and 2 for message type */
write_exactly("vlog_sock_locked", fd, (uint32_t *) &rc, sizeof(uint32_t)); rc = len + 4 + 2;
write_exactly("vlog_sock_locked", fd,
(uint32_t *)&rc, sizeof(uint32_t));
write_exactly("vlog_sock_locked", fd, &msg_type, sizeof(uint16_t)); write_exactly("vlog_sock_locked", fd, &msg_type, sizeof(uint16_t));
va_copy(targs, args); va_copy(targs, args);
rc = vdprintf(fd, fmt, targs); rc = vdprintf(fd, fmt, targs);
if (rc < 0) die(1, NULL, "Couldn't send log message with vdprintf", ""); if (rc < 0)
die(1, NULL, "Couldn't send log message with vdprintf", "");
va_end(targs); va_end(targs);
if (rc < len) { // we didn't write the whole message :-( if (rc < len) {
/* we didn't write the whole message :-( */
rc = len - rc; rc = len - rc;
fill = (char *) calloc(rc, 1); fill = (char *)calloc(rc, 1);
if (fill == NULL) die(1, NULL, "vlog_sock_locked fill", ""); if (fill == NULL)
die(1, NULL, "vlog_sock_locked fill", "");
write_exactly("vlog_sock_locked fill", fd, fill, rc); write_exactly("vlog_sock_locked fill", fd, fill, rc);
} }
} }
void log_sock_locked(int fd, uint16_t msg_type, const char * fmt, ...) { void log_sock_locked(int fd, uint16_t msg_type, const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
vlog_sock_locked(fd, msg_type, fmt, args); vlog_sock_locked(fd, msg_type, fmt, args);
va_end(args); va_end(args);
} }
void die void die(int exit_code, parameters *params, const char *parg,
(int exit_code, parameters * params, const char * parg, const char * fmt, ...) const char *fmt, ...)
{ {
va_list argp, targs; va_list argp, targs;
int in_errno = errno; int in_errno = errno;
@ -94,7 +97,8 @@ void die
vsyslog(LOG_CRIT, fmt, targs); vsyslog(LOG_CRIT, fmt, targs);
va_end(targs); va_end(targs);
if (fd != 0) vlog_sock_locked(fd, TRANSFUSE_LOG_ERROR, fmt, argp); if (fd != 0)
vlog_sock_locked(fd, TRANSFUSE_LOG_ERROR, fmt, argp);
va_end(argp); va_end(argp);
if (parg != NULL) { if (parg != NULL) {
@ -106,76 +110,81 @@ void die
} else { } else {
syslog(LOG_CRIT, "%s", strerror(in_errno)); syslog(LOG_CRIT, "%s", strerror(in_errno));
if (fd != 0) if (fd != 0)
log_sock_locked(fd, TRANSFUSE_LOG_ERROR, "%s", strerror(in_errno)); log_sock_locked(fd, TRANSFUSE_LOG_ERROR,
"%s", strerror(in_errno));
} }
} }
if (fd != 0)
if (fd != 0) close(fd); // flush close(fd); /* flush */
exit(exit_code); exit(exit_code);
// Nobody else should die before we terminate everything /* Nobody else should die before we terminate everything */
unlock("die ctl_lock", &params->ctl_lock); unlock("die ctl_lock", &params->ctl_lock);
} }
void vlog_locked void vlog_locked(parameters *params, uint16_t msg_type,
(parameters * params, uint16_t msg_type, const char * fmt, va_list args) { const char *fmt, va_list args)
{
int rc; int rc;
int fd = params->ctl_sock; int fd = params->ctl_sock;
va_list targs; va_list targs;
if (fd != 0) vlog_sock_locked(fd, msg_type, fmt, args); if (fd != 0) {
else { vlog_sock_locked(fd, msg_type, fmt, args);
} else {
va_copy(targs, args); va_copy(targs, args);
// TODO: translate msg_type to syslog message type /* TODO: translate msg_type to syslog message type */
vsyslog(LOG_INFO, fmt, targs); vsyslog(LOG_INFO, fmt, targs);
va_end(targs); va_end(targs);
fd = params->logfile_fd; fd = params->logfile_fd;
if (fd != 0) { if (fd != 0) {
va_copy(targs, args); va_copy(targs, args);
// TODO: include message type? /* TODO: include message type? */
rc = vdprintf(fd, fmt, targs); rc = vdprintf(fd, fmt, targs);
if (rc < 0) die(1, NULL, "Couldn't write log message with vdprintf", ""); if (rc < 0)
die(1, NULL,
"Couldn't write log message with vdprintf", "");
va_end(targs); va_end(targs);
} }
} }
} }
void vlog_time_locked void vlog_time_locked(parameters *params, uint16_t msg_type,
(parameters * params, uint16_t msg_type, const char * fmt, va_list args) { const char *fmt, va_list args)
{
int fd = params->logfile_fd; int fd = params->logfile_fd;
if (fd != 0 && params->ctl_sock == 0) log_timestamp(fd); if (fd != 0 && params->ctl_sock == 0)
log_timestamp(fd);
vlog_locked(params, msg_type, fmt, args); vlog_locked(params, msg_type, fmt, args);
} }
void log_time_locked void log_time_locked(parameters *params, uint16_t msg_type,
(parameters * params, uint16_t msg_type, const char * fmt, ...) { const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
vlog_time_locked(params, msg_type, fmt, args); vlog_time_locked(params, msg_type, fmt, args);
va_end(args); va_end(args);
} }
void log_time(parameters * params, const char * fmt, ...) { void log_time(parameters *params, const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
lock("log_time ctl_lock", &params->ctl_lock); lock("log_time ctl_lock", &params->ctl_lock);
vlog_time_locked(params, TRANSFUSE_LOG_ERROR, fmt, args); vlog_time_locked(params, TRANSFUSE_LOG_ERROR, fmt, args);
unlock("log_time ctl_lock", &params->ctl_lock); unlock("log_time ctl_lock", &params->ctl_lock);
va_end(args); va_end(args);
} }
void log_notice_time(parameters * params, const char * fmt, ...) { void log_notice_time(parameters *params, const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
lock("log_time ctl_lock", &params->ctl_lock); lock("log_time ctl_lock", &params->ctl_lock);
vlog_time_locked(params, TRANSFUSE_LOG_NOTICE, fmt, args); vlog_time_locked(params, TRANSFUSE_LOG_NOTICE, fmt, args);
unlock("log_time ctl_lock", &params->ctl_lock); unlock("log_time ctl_lock", &params->ctl_lock);
@ -184,25 +193,26 @@ void log_notice_time(parameters * params, const char * fmt, ...) {
} }
typedef struct { typedef struct {
parameters * params; parameters *params;
char * msg; char *msg;
} log_thread_state; } log_thread_state;
void * log_time_thread(void * log_state_ptr) { void *log_time_thread(void *log_state_ptr)
log_thread_state * log_state = log_state_ptr; {
log_thread_state *log_state = log_state_ptr;
log_time(log_state->params, log_state->msg); log_time(log_state->params, log_state->msg);
free(log_state->msg); free(log_state->msg);
free(log_state); free(log_state);
return NULL; return NULL;
} }
void thread_log_time(connection_t * conn, const char * fmt, ...) { void thread_log_time(connection_t *conn, const char *fmt, ...)
{
log_thread_state *log_state;
pthread_t logger; pthread_t logger;
va_list args; va_list args;
log_thread_state * log_state;
log_state = must_malloc("thread_log_time log_state", log_state = must_malloc("thread_log_time log_state",
sizeof(log_thread_state)); sizeof(log_thread_state));
@ -210,30 +220,31 @@ void thread_log_time(connection_t * conn, const char * fmt, ...) {
va_start(args, fmt); va_start(args, fmt);
if (vasprintf(&log_state->msg, fmt, args) == -1) if (vasprintf(&log_state->msg, fmt, args) == -1)
die(1, conn->params, "Couldn't allocate thread_log_time message", ""); die(1, conn->params,
"Couldn't allocate thread_log_time message", "");
va_end(args); va_end(args);
// TODO: We currently spawn a new thread for every message. This is /* TODO: We currently spawn a new thread for every
// far from ideal but fine for now as we anticipate thread-sensitive * message. This is far from ideal but fine for now as we
// log demand to be low. * anticipate thread-sensitive log demand to be low. */
errno = pthread_create(&logger, &detached, log_time_thread, log_state);
if ((errno = pthread_create(&logger, &detached, log_time_thread, log_state))) if (errno)
die(1, conn->params, "", die(1, conn->params, "",
"Couldn't create log thread for %s connection %s: ", "Couldn't create log thread for %s connection %s: ",
conn->type_descr, conn->mount_point); conn->type_descr, conn->mount_point);
} }
void log_continue_locked(parameters * params, const char * fmt, ...) { void log_continue_locked(parameters *params, const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
vlog_locked(params, TRANSFUSE_LOG_ERROR, fmt, args); vlog_locked(params, TRANSFUSE_LOG_ERROR, fmt, args);
va_end(args); va_end(args);
} }
void log_continue(parameters * params, const char * fmt, ...) { void log_continue(parameters *params, const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);

View File

@ -1,27 +1,26 @@
#ifndef _TRANSFUSED_LOG_H_
#define _TRANSFUSED_LOG_H_
#include <stdarg.h> #include <stdarg.h>
#include <inttypes.h> #include <inttypes.h>
#include "transfused.h" #include "transfused.h"
void die void die(int exit_code, parameters *params, const char *perror_arg,
(int exit_code, parameters * params, const char * perror_arg, const char *fmt, ...);
const char * fmt, ...);
void vlog_locked void vlog_locked(parameters *params, uint16_t msg_type,
(parameters * params, uint16_t msg_type, const char * fmt, va_list args); const char *fmt, va_list args);
void vlog_time_locked(parameters *params, uint16_t msg_type,
const char *fmt, va_list args);
void vlog_time_locked void log_time_locked(parameters *params, uint16_t msg_type,
(parameters * params, uint16_t msg_type, const char * fmt, va_list args); const char *fmt, ...);
void log_time_locked void log_time(parameters *params, const char *fmt, ...);
(parameters * params, uint16_t msg_type, const char * fmt, ...); void log_notice_time(parameters *params, const char *fmt, ...);
void thread_log_time(connection_t *conn, const char *fmt, ...);
void log_continue_locked(parameters *params, const char *fmt, ...);
void log_continue(parameters *params, const char *fmt, ...);
void log_time(parameters * params, const char * fmt, ...); #endif /* _TRANSFUSED_LOG_H_ */
void log_notice_time(parameters * params, const char * fmt, ...);
void thread_log_time(connection_t * conn, const char * fmt, ...);
void log_continue_locked(parameters * params, const char * fmt, ...);
void log_continue(parameters * params, const char * fmt, ...);

View File

@ -7,9 +7,11 @@
#include "transfused_log.h" #include "transfused_log.h"
long parse_cid(const char * address) { long parse_cid(const char *address)
char * end = NULL; {
char *end = NULL;
long cid = strtol(address, &end, 10); long cid = strtol(address, &end, 10);
if (address == end || *end != ':') { if (address == end || *end != ':') {
*end = 0; *end = 0;
die(2, NULL, NULL, "Invalid vsock cid: %s", address); die(2, NULL, NULL, "Invalid vsock cid: %s", address);
@ -17,9 +19,11 @@ long parse_cid(const char * address) {
return cid; return cid;
} }
long parse_port(const char * port_str) { long parse_port(const char *port_str)
char * end = NULL; {
char *end = NULL;
long port = strtol(port_str, &end, 10); long port = strtol(port_str, &end, 10);
if (port_str == end || *end != '\0') { if (port_str == end || *end != '\0') {
*end = 0; *end = 0;
die(2, NULL, NULL, "Invalid vsock port: %s", port_str); die(2, NULL, NULL, "Invalid vsock port: %s", port_str);
@ -27,12 +31,15 @@ long parse_port(const char * port_str) {
return port; return port;
} }
int find_colon(const char * address) { int find_colon(const char *address)
{
int colon = 0; int colon = 0;
while (address[colon] != '\0') while (address[colon] != '\0')
if (address[colon] == ':') break; if (address[colon] == ':')
else colon++; break;
else
colon++;
if (address[colon] == '\0') if (address[colon] == '\0')
die(2, NULL, NULL, "Missing port in vsock address %s", address); die(2, NULL, NULL, "Missing port in vsock address %s", address);
@ -40,20 +47,21 @@ int find_colon(const char * address) {
return colon; return colon;
} }
int bind_vsock(const char * address) { int bind_vsock(const char *address)
{
long cid, port; long cid, port;
int colon; int colon;
struct sockaddr_vm sa_listen = { struct sockaddr_vm sa_listen = {
.svm_family = AF_VSOCK, .svm_family = AF_VSOCK,
}; };
int sock_fd; int sock_fd;
colon = find_colon(address); colon = find_colon(address);
if (address[0] == '_' && colon == 1) cid = VMADDR_CID_ANY; if (address[0] == '_' && colon == 1)
else cid = parse_cid(address); cid = VMADDR_CID_ANY;
else
cid = parse_cid(address);
port = parse_port(address + colon + 1); port = parse_port(address + colon + 1);
@ -64,20 +72,19 @@ int bind_vsock(const char * address) {
if (sock_fd < 0) if (sock_fd < 0)
die(1, NULL, "socket(AF_VSOCK)", ""); die(1, NULL, "socket(AF_VSOCK)", "");
if (bind(sock_fd, (struct sockaddr *) &sa_listen, sizeof(sa_listen))) if (bind(sock_fd, (struct sockaddr *)&sa_listen, sizeof(sa_listen)))
die(1, NULL, "bind(AF_VSOCK)", ""); die(1, NULL, "bind(AF_VSOCK)", "");
return sock_fd; return sock_fd;
} }
int connect_vsock(const char * address) { int connect_vsock(const char *address)
{
long cid, port; long cid, port;
int colon; int colon;
struct sockaddr_vm sa_connect = { struct sockaddr_vm sa_connect = {
.svm_family = AF_VSOCK, .svm_family = AF_VSOCK,
}; };
int sock_fd; int sock_fd;
colon = find_colon(address); colon = find_colon(address);
@ -92,7 +99,7 @@ int connect_vsock(const char * address) {
if (sock_fd < 0) if (sock_fd < 0)
die(1, NULL, "socket(AF_VSOCK)", ""); die(1, NULL, "socket(AF_VSOCK)", "");
if (connect(sock_fd, (struct sockaddr *) &sa_connect, sizeof(sa_connect))) if (connect(sock_fd, (struct sockaddr *)&sa_connect, sizeof(sa_connect)))
die(1, NULL, "connect(AF_VSOCK)", ""); die(1, NULL, "connect(AF_VSOCK)", "");
return sock_fd; return sock_fd;

View File

@ -1,2 +1,7 @@
int bind_vsock(const char * address); #ifndef _TRANSFUSED_VSOCK_H_
int connect_vsock(const char * address); #define _TRANSFUSED_VSOCK_H_
int bind_vsock(const char *address);
int connect_vsock(const char *address);
#endif /* _TRANSFUSED_VSOCK_H_ */

15
docs/coding-style.md Normal file
View File

@ -0,0 +1,15 @@
## Shell scripts
Shell scripts should loosely follow the general Alpine style which is derived from the Linux Kernel guidelines, i.e. tabs for indentation etc.
It's also useful to run `shellcheck` on the scripts.
## Go code
New Go code should be formatted with `gofmt`
## C code
C code written from scratch should follow the
[Linux kernel coding guidelines](https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/Documentation/CodingStyle)
as much as it makes sense for userspace code. You can check your code with [checkpatch.pl](https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/scripts/checkpatch.pl) like this:
```
checkpatch.pl --no-tree --file <sourcefile>
```