mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-06 20:43:44 +02:00
Ethernet tap cleanup.
This commit is contained in:
parent
c9c093777f
commit
f9d31605b8
1 changed files with 11 additions and 12 deletions
|
@ -270,8 +270,8 @@ EthernetTap::EthernetTap(
|
||||||
_dhcp6(false),
|
_dhcp6(false),
|
||||||
_fd(0)
|
_fd(0)
|
||||||
{
|
{
|
||||||
char devpath[64],ethaddr[64],mtustr[16];
|
char devpath[64],ethaddr[64],mtustr[16],tmp[4096];
|
||||||
struct stat tmp;
|
struct stat stattmp;
|
||||||
Mutex::Lock _l(__tapCreateLock); // create only one tap at a time, globally
|
Mutex::Lock _l(__tapCreateLock); // create only one tap at a time, globally
|
||||||
|
|
||||||
if (mtu > 4096)
|
if (mtu > 4096)
|
||||||
|
@ -279,27 +279,26 @@ EthernetTap::EthernetTap(
|
||||||
|
|
||||||
// Check for existence of ZT tap devices, try to load module if not there
|
// Check for existence of ZT tap devices, try to load module if not there
|
||||||
const char *kextload = UNIX_COMMANDS[ZT_MAC_KEXTLOAD_COMMAND];
|
const char *kextload = UNIX_COMMANDS[ZT_MAC_KEXTLOAD_COMMAND];
|
||||||
if ((stat("/dev/zt0",&tmp))&&(kextload)) {
|
if ((stat("/dev/zt0",&stattmp))&&(kextload)) {
|
||||||
long kextpid;
|
|
||||||
char tmp[4096];
|
|
||||||
strcpy(tmp,_r->homePath.c_str());
|
strcpy(tmp,_r->homePath.c_str());
|
||||||
if ((kextpid = (long)vfork()) == 0) {
|
long kextpid = (long)vfork();
|
||||||
|
if (kextpid == 0) {
|
||||||
chdir(tmp);
|
chdir(tmp);
|
||||||
execl(kextload,kextload,"-q","-repository",tmp,"tap.kext",(const char *)0);
|
execl(kextload,kextload,"-q","-repository",tmp,"tap.kext",(const char *)0);
|
||||||
_exit(-1);
|
_exit(-1);
|
||||||
} else {
|
} else if (kextpid > 0) {
|
||||||
int exitcode = -1;
|
int exitcode = -1;
|
||||||
waitpid(kextpid,&exitcode,0);
|
waitpid(kextpid,&exitcode,0);
|
||||||
usleep(500);
|
usleep(500);
|
||||||
|
} else throw std::runtime_error("unable to create subprocess with fork()");
|
||||||
}
|
}
|
||||||
}
|
if (stat("/dev/zt0",&stattmp))
|
||||||
if (stat("/dev/zt0",&tmp))
|
|
||||||
throw std::runtime_error("/dev/zt# tap devices do not exist and unable to load kernel extension");
|
throw std::runtime_error("/dev/zt# tap devices do not exist and unable to load kernel extension");
|
||||||
|
|
||||||
// Open the first available device (ones in use will fail with resource busy)
|
// Open the first available device (ones in use will fail with resource busy)
|
||||||
for(int i=0;i<256;++i) {
|
for(int i=0;i<256;++i) {
|
||||||
Utils::snprintf(devpath,sizeof(devpath),"/dev/zt%d",i);
|
Utils::snprintf(devpath,sizeof(devpath),"/dev/zt%d",i);
|
||||||
if (stat(devpath,&tmp))
|
if (stat(devpath,&stattmp))
|
||||||
throw std::runtime_error("no more TAP devices available");
|
throw std::runtime_error("no more TAP devices available");
|
||||||
_fd = ::open(devpath,O_RDWR);
|
_fd = ::open(devpath,O_RDWR);
|
||||||
if (_fd > 0) {
|
if (_fd > 0) {
|
||||||
|
@ -364,10 +363,10 @@ EthernetTap::~EthernetTap()
|
||||||
// instance.
|
// instance.
|
||||||
const char *kextunload = UNIX_COMMANDS[ZT_MAC_KEXTUNLOAD_COMMAND];
|
const char *kextunload = UNIX_COMMANDS[ZT_MAC_KEXTUNLOAD_COMMAND];
|
||||||
if (kextunload) {
|
if (kextunload) {
|
||||||
long kextpid;
|
|
||||||
char tmp[4096];
|
char tmp[4096];
|
||||||
sprintf(tmp,"%s/tap.kext",_r->homePath.c_str());
|
sprintf(tmp,"%s/tap.kext",_r->homePath.c_str());
|
||||||
if ((kextpid = (long)vfork()) == 0) {
|
long kextpid = (long)vfork();
|
||||||
|
if (kextpid == 0) {
|
||||||
execl(kextunload,kextunload,tmp,(const char *)0);
|
execl(kextunload,kextunload,tmp,(const char *)0);
|
||||||
_exit(-1);
|
_exit(-1);
|
||||||
} else if (kextpid > 0) {
|
} else if (kextpid > 0) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue