From 1b8917a57c189fda58e3900fa9ba798d3975fc26 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 9 Sep 2021 09:30:48 -0400 Subject: [PATCH] Fix for MacOS interface setup and config timing bugs. --- osdep/MacEthernetTap.cpp | 24 +++++++++++++++++++++++- osdep/MacEthernetTapAgent.c | 4 ++-- osdep/ManagedRoute.cpp | 4 +--- service/OneService.cpp | 3 --- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/osdep/MacEthernetTap.cpp b/osdep/MacEthernetTap.cpp index 4c51e9a8a..1ba82dcb7 100644 --- a/osdep/MacEthernetTap.cpp +++ b/osdep/MacEthernetTap.cpp @@ -209,8 +209,30 @@ MacEthernetTap::MacEthernetTap( ::_exit(-1); } else { _agentPid = apid; + + // Wait up to 10 seconds for the subprocess to actually create the device. This prevents + // things like routes from being created before the device exists. + for(int waitLoops=0;;++waitLoops) { + struct ifaddrs *ifa = (struct ifaddrs *)0; + if (!getifaddrs(&ifa)) { + struct ifaddrs *p = ifa; + while (p) { + if ((p->ifa_name)&&(!strcmp(devstr, p->ifa_name))) { + waitLoops = -1; + break; + } + p = p->ifa_next; + } + freeifaddrs(ifa); + } + if (waitLoops == -1) { + break; + } else if (waitLoops >= 100) { // 10 seconds + throw std::runtime_error("feth device creation timed out"); + } + Thread::sleep(100); + } } - Thread::sleep(100); // this causes them to come up in a more user-friendly order on launch _thread = Thread::start(this); } diff --git a/osdep/MacEthernetTapAgent.c b/osdep/MacEthernetTapAgent.c index 80339a9e1..43c4cf133 100644 --- a/osdep/MacEthernetTapAgent.c +++ b/osdep/MacEthernetTapAgent.c @@ -176,10 +176,10 @@ static void die() close(s_ndrvfd); if (s_bpffd >= 0) close(s_bpffd); - if (s_deviceName[0]) - run("/sbin/ifconfig",s_deviceName,"destroy",(char *)0); if (s_peerDeviceName[0]) run("/sbin/ifconfig",s_peerDeviceName,"destroy",(char *)0); + if (s_deviceName[0]) + run("/sbin/ifconfig",s_deviceName,"destroy",(char *)0); } static inline void close_inherited_fds() diff --git a/osdep/ManagedRoute.cpp b/osdep/ManagedRoute.cpp index 6c402e2b9..abe768930 100644 --- a/osdep/ManagedRoute.cpp +++ b/osdep/ManagedRoute.cpp @@ -405,9 +405,7 @@ ManagedRoute::ManagedRoute(const InetAddress &target,const InetAddress &via,cons } ManagedRoute::~ManagedRoute() -{ - this->remove(); -} +{} /* Linux NOTE: for default route override, some Linux distributions will * require a change to the rp_filter parameter. A value of '1' will prevent diff --git a/service/OneService.cpp b/service/OneService.cpp index 4421d1cb5..4dfe1bb79 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -542,10 +542,7 @@ public: ~NetworkState() { this->managedRoutes.clear(); -#ifdef __APPLE__ - Thread::sleep(10); this->tap.reset(); -#endif } std::shared_ptr tap;