tap-vsockd: better error handling

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
Rolf Neugebauer 2016-09-05 13:54:03 +01:00
parent 839a65c0bd
commit e2e123e464
2 changed files with 25 additions and 8 deletions

View File

@ -74,8 +74,11 @@ err:
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));
memcpy(&m->hello[0], &expected_hello[0], sizeof(m->hello));
@ -91,9 +94,13 @@ char *print_init_message(struct init_message *m)
memcpy(&tmp[0], &m->commit[0], 40);
tmp[40] = '\000';
char *buffer = (char *)malloc(80);
char *buffer;
int n;
buffer = malloc(80);
if (!buffer)
return NULL;
n = snprintf(buffer, 80, "version %d, commit %s", m->version, tmp);
if (n < 0) {
perror("Failed to format init_message");

View File

@ -67,6 +67,8 @@ void set_macaddr(const char *dev, uint8_t *mac)
int fd;
fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd == -1)
fatal("Could not get socket to set MAC address");
strcpy(ifq.ifr_name, dev);
memcpy(&ifq.ifr_hwaddr.sa_data[0], mac, 6);
ifq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
@ -80,13 +82,16 @@ void set_macaddr(const char *dev, uint8_t *mac)
/* Negotiate a vmnet connection, returns 0 on success and 1 on error. */
int negotiate(int fd, struct vif_info *vif)
{
/* Negotiate with com.docker.slirp */
struct init_message *me = create_init_message();
enum command command = ethernet;
struct init_message *me;
struct ethernet_args args;
struct init_message you;
char *txt;
me = create_init_message();
if (!me)
goto err;
if (write_init_message(fd, me) == -1)
goto err;
@ -94,6 +99,9 @@ int negotiate(int fd, struct vif_info *vif)
goto err;
txt = print_init_message(&you);
if (!txt)
goto err;
syslog(LOG_INFO, "Server reports %s", txt);
free(txt);
@ -212,7 +220,7 @@ static void handle(struct connection *connection)
static int create_listening_socket(GUID serviceid)
{
SOCKADDR_HV sa;
int lsock = -1;
int lsock;
int res;
lsock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
@ -238,7 +246,7 @@ static int create_listening_socket(GUID serviceid)
static int connect_socket(GUID serviceid)
{
SOCKADDR_HV sa;
int sock = -1;
int sock;
int res;
sock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
@ -261,7 +269,7 @@ static int accept_socket(int lsock)
{
SOCKADDR_HV sac;
socklen_t socklen = sizeof(sac);
int csock = -1;
int csock;
csock = accept(lsock, (struct sockaddr *)&sac, &socklen);
if (csock == -1)
@ -315,6 +323,8 @@ void daemonize(const char *pidfile)
fatal("Failed to chdir()");
null = open("/dev/null", O_RDWR);
if (null == -1)
fatal("Failed to open /dev/null");
dup2(null, STDIN_FILENO);
dup2(null, STDOUT_FILENO);
dup2(null, STDERR_FILENO);