Skip to content

Commit

Permalink
main: do not leak sockfd on errors
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Feb 6, 2024
1 parent f5287ef commit fa1f950
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static int configure_network(const char *tapname,
.ifr_flags = IFF_UP | IFF_RUNNING };
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr_lo) < 0) {
perror("cannot set device up");
return -1;
goto fail;
}

memset(&ifr, 0, sizeof(ifr));
Expand All @@ -174,20 +174,20 @@ static int configure_network(const char *tapname,

if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
perror("cannot set device up");
return -1;
goto fail;
}

ifr.ifr_mtu = (int)cfg->mtu;
if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
perror("cannot set MTU");
return -1;
goto fail;
}

if (cfg->vmacaddress_len > 0) {
ifr.ifr_ifru.ifru_hwaddr = cfg->vmacaddress;
if (ioctl(sockfd, SIOCSIFHWADDR, &ifr) < 0) {
perror("cannot set MAC address");
return -1;
goto fail;
}
}

Expand All @@ -197,13 +197,13 @@ static int configure_network(const char *tapname,

if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
perror("cannot set device address");
return -1;
goto fail;
}

sai->sin_addr = cfg->vnetmask;
if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
perror("cannot set device netmask");
return -1;
goto fail;
}

memset(&route, 0, sizeof(route));
Expand All @@ -223,9 +223,12 @@ static int configure_network(const char *tapname,

if (ioctl(sockfd, SIOCADDRT, &route) < 0) {
perror("set route");
return -1;
goto fail;
}
return 0;
fail:
close(sockfd);
return -1;
}

/* Child (--target-type=netns) */
Expand Down

0 comments on commit fa1f950

Please sign in to comment.