You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/02/24 14:41:09 UTC
[incubator-nuttx] 02/02: arch/sim: Handle tap device initializition
failure gracefully
This is an automated email from the ASF dual-hosted git repository.
gnutt pushed a commit to branch pr367
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
commit c7945bbc2939668119f112789c4524b1cb07d911
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Mon Feb 24 17:33:23 2020 +0800
arch/sim: Handle tap device initializition failure gracefully
Change-Id: I502fbe6bdbf625090e427ce00cea19567c01a592
Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
arch/sim/src/sim/up_tapdev.c | 38 ++++++++++++++++++++++++++++++--------
1 file changed, 30 insertions(+), 8 deletions(-)
diff --git a/arch/sim/src/sim/up_tapdev.c b/arch/sim/src/sim/up_tapdev.c
index 8ea4cca..4ef2975 100644
--- a/arch/sim/src/sim/up_tapdev.c
+++ b/arch/sim/src/sim/up_tapdev.c
@@ -100,7 +100,7 @@ void netdriver_setmacaddr(unsigned char *macaddr);
#ifdef TAPDEV_DEBUG
static int gdrop = 0;
#endif
-static int gtapdevfd;
+static int gtapdevfd = -1;
static char gdevname[IFNAMSIZ];
#ifdef CONFIG_SIM_NET_HOST_ROUTE
@@ -167,6 +167,7 @@ static void set_macaddr(void)
void tapdev_init(void)
{
struct ifreq ifr;
+ int tapdevfd;
int ret;
#ifdef CONFIG_SIM_NET_BRIDGE
@@ -175,10 +176,10 @@ void tapdev_init(void)
/* Open the tap device */
- gtapdevfd = open(DEVTAP, O_RDWR, 0644);
- if (gtapdevfd < 0)
+ tapdevfd = open(DEVTAP, O_RDWR, 0644);
+ if (tapdevfd < 0)
{
- printf("TAPDEV: open failed: %d\r\n", -gtapdevfd);
+ printf("TAPDEV: open failed: %d\r\n", -tapdevfd);
return;
}
@@ -186,10 +187,11 @@ void tapdev_init(void)
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
- ret = ioctl(gtapdevfd, TUNSETIFF, (unsigned long) &ifr);
+ ret = ioctl(tapdevfd, TUNSETIFF, (unsigned long) &ifr);
if (ret < 0)
{
printf("TAPDEV: ioctl failed: %d\r\n", -ret);
+ close(tapdevfd);
return;
}
@@ -206,6 +208,7 @@ void tapdev_init(void)
if (sockfd < 0)
{
printf("TAPDEV: Can't open socket: %d\r\n", -sockfd);
+ close(tapdevfd);
return;
}
@@ -216,16 +219,19 @@ void tapdev_init(void)
ifr.ifr_ifindex = if_nametoindex(gdevname);
ret = ioctl(sockfd, SIOCBRADDIF, &ifr);
+ close(sockfd);
if (ret < 0)
{
printf("TAPDEV: ioctl failed (can't add interface %s to "
"bridge %s): %d\r\n",
gdevname, CONFIG_SIM_NET_BRIDGE_DEVICE, -ret);
- }
-
- close(sockfd);
+ close(tapdevfd);
+ return;
+ }
#endif
+ gtapdevfd = tapdevfd;
+
/* Set the MAC address */
set_macaddr();
@@ -277,6 +283,12 @@ unsigned int tapdev_read(unsigned char *buf, unsigned int buflen)
void tapdev_send(unsigned char *buf, unsigned int buflen)
{
int ret;
+
+ if (gtapdevfd < 0)
+ {
+ return;
+ }
+
#ifdef TAPDEV_DEBUG
printf("tapdev_send: sending %d bytes\r\n", buflen);
@@ -308,6 +320,11 @@ void tapdev_ifup(in_addr_t ifaddr)
struct sockaddr_in *addr;
#endif
+ if (gtapdevfd < 0)
+ {
+ return;
+ }
+
/* Get a socket with which to manipulate the tap device */
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
@@ -369,6 +386,11 @@ void tapdev_ifdown(void)
int sockfd;
int ret;
+ if (gtapdevfd < 0)
+ {
+ return;
+ }
+
if (((struct sockaddr_in *)&ghostroute.rt_dst)->sin_addr.s_addr != 0)
{
/* Get a socket with which to manipulate the tap device */