Fix for GitHub issue #122 and other fixes

This commit is contained in:
Adam Ierymenko 2014-09-18 10:01:30 -07:00
parent 924f030994
commit 4708231046
8 changed files with 102 additions and 35 deletions

View file

@ -36,16 +36,20 @@
#include "IpcConnection.hpp" #include "IpcConnection.hpp"
#ifndef __WINDOWS__ #ifndef __WINDOWS__
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <sys/socket.h>
#include <sys/select.h>
#endif #endif
namespace ZeroTier { namespace ZeroTier {
IpcConnection::IpcConnection(const char *endpoint,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) : IpcConnection::IpcConnection(const char *endpoint,unsigned int timeout,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
_handler(commandHandler), _handler(commandHandler),
_arg(arg), _arg(arg),
_timeout(timeout),
#ifdef __WINDOWS__ #ifdef __WINDOWS__
_sock(INVALID_HANDLE_VALUE), _sock(INVALID_HANDLE_VALUE),
_incoming(false), _incoming(false),
@ -81,12 +85,13 @@ IpcConnection::IpcConnection(const char *endpoint,void (*commandHandler)(void *,
} }
#ifdef __WINDOWS__ #ifdef __WINDOWS__
IpcConnection::IpcConnection(HANDLE s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) : IpcConnection::IpcConnection(HANDLE s,unsigned int timeout,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
#else #else
IpcConnection::IpcConnection(int s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) : IpcConnection::IpcConnection(int s,unsigned int timeout,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
#endif #endif
_handler(commandHandler), _handler(commandHandler),
_arg(arg), _arg(arg),
_timeout(timeout),
_sock(s), _sock(s),
#ifdef __WINDOWS__ #ifdef __WINDOWS__
_incoming(true), _incoming(true),
@ -104,18 +109,23 @@ IpcConnection::~IpcConnection()
_writeLock.unlock(); _writeLock.unlock();
#ifdef __WINDOWS__ #ifdef __WINDOWS__
while (_running) { while (_running) {
Thread::cancelIO(_thread); Thread::cancelIO(_thread); // cause Windows to break from blocking read and detect shutdown
Sleep(100); Sleep(100);
} }
#else
#else // !__WINDOWS__
int s = _sock; int s = _sock;
_sock = 0; _sock = 0;
if (s > 0) { if (s > 0) {
::shutdown(s,SHUT_RDWR); ::shutdown(s,SHUT_RDWR);
::close(s); ::close(s);
} }
#endif Thread::join(_thread);
#endif // __WINDOWS__ / !__WINDOWS__
} }
void IpcConnection::printf(const char *format,...) void IpcConnection::printf(const char *format,...)
@ -134,7 +144,7 @@ void IpcConnection::printf(const char *format,...)
#ifdef __WINDOWS__ #ifdef __WINDOWS__
_writeBuf.append(tmp,n); _writeBuf.append(tmp,n);
Thread::cancelIO(_thread); Thread::cancelIO(_thread); // cause Windows to break from blocking read and service write buffer
#else #else
if (_sock > 0) if (_sock > 0)
::write(_sock,tmp,n); ::write(_sock,tmp,n);
@ -144,20 +154,39 @@ void IpcConnection::printf(const char *format,...)
void IpcConnection::threadMain() void IpcConnection::threadMain()
throw() throw()
{ {
char tmp[65536]; char tmp[16384];
char linebuf[65536]; char linebuf[16384];
unsigned int lineptr = 0; unsigned int lineptr = 0;
char c; char c;
#ifdef __WINDOWS__ #ifdef __WINDOWS__
DWORD n,i; DWORD n,i;
std::string wbuf; std::string wbuf;
#else
#else // !__WINDOWS__
int s,n,i; int s,n,i;
#endif fd_set readfds,writefds,errorfds;
struct timeval tout;
#ifdef SO_NOSIGPIPE
if (_sock > 0) {
i = 1;
::setsockopt(_sock,SOL_SOCKET,SO_NOSIGPIPE,(char *)&i,sizeof(i));
}
#endif // SO_NOSIGPIPE
#endif // __WINDOWS__ / !__WINDOWS__
while (_run) { while (_run) {
#ifdef __WINDOWS__ #ifdef __WINDOWS__
/* Note that we do not use fucking timeouts in Windows, since it does seem
* to properly detect named pipe endpoint close. But we do use a write buffer
* because Windows won't let you divorce reading and writing threads without
* all that OVERLAPPED cruft. */
{ {
Mutex::Lock _l(_writeLock); Mutex::Lock _l(_writeLock);
if (!_run) if (!_run)
@ -187,16 +216,42 @@ void IpcConnection::threadMain()
} }
if (!_run) if (!_run)
break; break;
#else
#else // !__WINDOWS__
/* So today I learned that there is no reliable way to detect a half-closed
* Unix domain socket. So to make sure we don't leave orphaned sockets around
* we just use fucking timeouts. If a socket fucking times out, we break from
* the I/O loop and terminate the thread. But this IpcConnection code is ugly
* so maybe the OS is simply offended by it and refuses to reveal its mysteries
* to me. Oh well... this IPC code will probably get canned when we go to
* local HTTP RESTful interfaces or soemthing like that. */
if ((s = _sock) <= 0) if ((s = _sock) <= 0)
break; break;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&errorfds);
FD_SET(s,&readfds);
FD_SET(s,&errorfds);
tout.tv_sec = _timeout; // use a fucking timeout
tout.tv_usec = 0;
if (select(s+1,&readfds,&writefds,&errorfds,&tout) <= 0) {
break; // socket has fucking timed out
} else {
if (FD_ISSET(s,&errorfds))
break; // socket has an exception... sometimes works
else {
n = (int)::read(s,tmp,sizeof(tmp)); n = (int)::read(s,tmp,sizeof(tmp));
if ((n <= 0)||(_sock <= 0)) if ((n <= 0)||(_sock <= 0))
break; break; // read returned error... sometimes works
#endif }
}
#endif // __WINDOWS__ / !__WINDOWS__
for(i=0;i<n;++i) { for(i=0;i<n;++i) {
c = (linebuf[lineptr] = tmp[i]); c = (linebuf[lineptr] = tmp[i]);
if ((c == '\r')||(c == '\n')||(lineptr == (sizeof(linebuf) - 1))) { if ((c == '\r')||(c == '\n')||(c == (char)0)||(lineptr == (sizeof(linebuf) - 1))) {
if (lineptr) { if (lineptr) {
linebuf[lineptr] = (char)0; linebuf[lineptr] = (char)0;
_handler(_arg,this,IPC_EVENT_COMMAND,linebuf); _handler(_arg,this,IPC_EVENT_COMMAND,linebuf);
@ -211,11 +266,13 @@ void IpcConnection::threadMain()
_writeLock.unlock(); _writeLock.unlock();
#ifdef __WINDOWS__ #ifdef __WINDOWS__
if (_incoming) if (_incoming)
DisconnectNamedPipe(_sock); DisconnectNamedPipe(_sock);
CloseHandle(_sock); CloseHandle(_sock);
_running = false; _running = false;
#endif
#endif // __WINDOWS__
if (r) if (r)
_handler(_arg,this,IPC_EVENT_CONNECTION_CLOSED,(const char *)0); _handler(_arg,this,IPC_EVENT_CONNECTION_CLOSED,(const char *)0);

View file

@ -61,11 +61,12 @@ public:
* Connect to an IPC endpoint * Connect to an IPC endpoint
* *
* @param endpoint Endpoint path * @param endpoint Endpoint path
* @param timeout Inactivity timeout in seconds
* @param commandHandler Command handler function * @param commandHandler Command handler function
* @param arg First argument to command handler * @param arg First argument to command handler
* @throws std::runtime_error Unable to connect * @throws std::runtime_error Unable to connect
*/ */
IpcConnection(const char *endpoint,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg); IpcConnection(const char *endpoint,unsigned int timeout,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg);
~IpcConnection(); ~IpcConnection();
/** /**
@ -80,13 +81,14 @@ public:
private: private:
// Used by IpcListener to construct incoming connections // Used by IpcListener to construct incoming connections
#ifdef __WINDOWS__ #ifdef __WINDOWS__
IpcConnection(HANDLE s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg); IpcConnection(HANDLE s,unsigned int timeout,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg);
#else #else
IpcConnection(int s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg); IpcConnection(int s,unsigned int timeout,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg);
#endif #endif
void (*_handler)(void *,IpcConnection *,IpcConnection::EventType,const char *); void (*_handler)(void *,IpcConnection *,IpcConnection::EventType,const char *);
void *_arg; void *_arg;
unsigned int _timeout;
#ifdef __WINDOWS__ #ifdef __WINDOWS__
HANDLE _sock; HANDLE _sock;
std::string _writeBuf; std::string _writeBuf;

View file

@ -42,10 +42,11 @@
namespace ZeroTier { namespace ZeroTier {
IpcListener::IpcListener(const char *ep,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) : IpcListener::IpcListener(const char *ep,unsigned int timeout,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
_endpoint(ep), _endpoint(ep),
_handler(commandHandler), _handler(commandHandler),
_arg(arg), _arg(arg),
_timeout(timeout),
#ifdef __WINDOWS__ #ifdef __WINDOWS__
_run(true), _run(true),
_running(true) _running(true)
@ -130,7 +131,7 @@ void IpcListener::threadMain()
break; break;
} }
try { try {
_handler(_arg,new IpcConnection(s,_handler,_arg),IpcConnection::IPC_EVENT_NEW_CONNECTION,(const char *)0); _handler(_arg,new IpcConnection(s,_timeout,_handler,_arg),IpcConnection::IPC_EVENT_NEW_CONNECTION,(const char *)0);
} catch ( ... ) {} // handlers should not throw } catch ( ... ) {} // handlers should not throw
} else { } else {
CloseHandle(s); CloseHandle(s);
@ -155,7 +156,7 @@ void IpcListener::threadMain()
break; break;
} }
try { try {
_handler(_arg,new IpcConnection(s,_handler,_arg),IpcConnection::IPC_EVENT_NEW_CONNECTION,(const char *)0); _handler(_arg,new IpcConnection(s,_timeout,_handler,_arg),IpcConnection::IPC_EVENT_NEW_CONNECTION,(const char *)0);
} catch ( ... ) {} // handlers should not throw } catch ( ... ) {} // handlers should not throw
} }
#endif #endif

View file

@ -50,7 +50,7 @@ public:
* The supplied handler is passed on to incoming instances of IpcConnection. When * The supplied handler is passed on to incoming instances of IpcConnection. When
* a connection is first opened, it is called with IPC_EVENT_NEW_CONNECTION. The * a connection is first opened, it is called with IPC_EVENT_NEW_CONNECTION. The
* receiver must take ownership of the connection object. When a connection is * receiver must take ownership of the connection object. When a connection is
* closed, IPC_EVENT_CONNECTION_CLOSING is generated. At this point (or after) the * closed, IPC_EVENT_CONNECTION_CLOSED is generated. At this point (or after) the
* receiver must delete the object. IPC_EVENT_COMMAND is generated when lines of * receiver must delete the object. IPC_EVENT_COMMAND is generated when lines of
* text are read, and in this cases the last argument is not NULL. No closed event * text are read, and in this cases the last argument is not NULL. No closed event
* is generated in the event of manual delete if the connection is still open. * is generated in the event of manual delete if the connection is still open.
@ -60,11 +60,12 @@ public:
* use cases are simple enough that it's not too bad. * use cases are simple enough that it's not too bad.
* *
* @param IPC endpoint name (OS-specific) * @param IPC endpoint name (OS-specific)
* @param timeout Endpoint inactivity timeout in seconds
* @param commandHandler Function to call for each command * @param commandHandler Function to call for each command
* @param arg First argument to pass to handler * @param arg First argument to pass to handler
* @throws std::runtime_error Unable to bind to endpoint * @throws std::runtime_error Unable to bind to endpoint
*/ */
IpcListener(const char *ep,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg); IpcListener(const char *ep,unsigned int timeout,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg);
~IpcListener(); ~IpcListener();
@ -75,6 +76,7 @@ private:
std::string _endpoint; std::string _endpoint;
void (*_handler)(void *,IpcConnection *,IpcConnection::EventType,const char *); void (*_handler)(void *,IpcConnection *,IpcConnection::EventType,const char *);
void *_arg; void *_arg;
unsigned int _timeout;
#ifdef __WINDOWS__ #ifdef __WINDOWS__
volatile bool _run; volatile bool _run;
volatile bool _running; volatile bool _running;

View file

@ -59,7 +59,7 @@ NodeControlClient::NodeControlClient(const char *ep,const char *authToken,void (
impl->resultHandler = resultHandler; impl->resultHandler = resultHandler;
impl->arg = arg; impl->arg = arg;
try { try {
impl->ipcc = new IpcConnection(ep,&_CBipcResultHandler,_impl); impl->ipcc = new IpcConnection(ep,ZT_IPC_TIMEOUT,&_CBipcResultHandler,_impl);
impl->ipcc->printf("auth %s"ZT_EOL_S,authToken); impl->ipcc->printf("auth %s"ZT_EOL_S,authToken);
} catch ( ... ) { } catch ( ... ) {
impl->ipcc = (IpcConnection *)0; impl->ipcc = (IpcConnection *)0;

View file

@ -70,7 +70,7 @@ void NodeControlService::threadMain()
break; break;
} else if ((_node->initialized())&&(_node->address())) { } else if ((_node->initialized())&&(_node->address())) {
Utils::snprintf(tmp,sizeof(tmp),"%s%.10llx",ZT_IPC_ENDPOINT_BASE,(unsigned long long)_node->address()); Utils::snprintf(tmp,sizeof(tmp),"%s%.10llx",ZT_IPC_ENDPOINT_BASE,(unsigned long long)_node->address());
_listener = new IpcListener(tmp,&_CBcommandHandler,this); _listener = new IpcListener(tmp,ZT_IPC_TIMEOUT,&_CBcommandHandler,this);
break; break;
} }
Thread::sleep(100); // wait for Node to start Thread::sleep(100); // wait for Node to start
@ -83,18 +83,18 @@ void NodeControlService::threadMain()
void NodeControlService::_CBcommandHandler(void *arg,IpcConnection *ipcc,IpcConnection::EventType event,const char *commandLine) void NodeControlService::_CBcommandHandler(void *arg,IpcConnection *ipcc,IpcConnection::EventType event,const char *commandLine)
{ {
if (!((NodeControlService *)arg)->_running)
return;
if ((!commandLine)||(!commandLine[0]))
return;
switch(event) { switch(event) {
case IpcConnection::IPC_EVENT_COMMAND: { case IpcConnection::IPC_EVENT_COMMAND: {
if ((!((NodeControlService *)arg)->_running)||(!commandLine)||(!commandLine[0]))
return;
((NodeControlService *)arg)->_doCommand(ipcc,commandLine); ((NodeControlService *)arg)->_doCommand(ipcc,commandLine);
} break; } break;
case IpcConnection::IPC_EVENT_NEW_CONNECTION: { case IpcConnection::IPC_EVENT_NEW_CONNECTION: {
Mutex::Lock _l(((NodeControlService *)arg)->_connections_m); Mutex::Lock _l(((NodeControlService *)arg)->_connections_m);
((NodeControlService *)arg)->_connections[ipcc] = false; // not yet authenticated ((NodeControlService *)arg)->_connections[ipcc] = false; // not yet authenticated
} break; } break;
case IpcConnection::IPC_EVENT_CONNECTION_CLOSED: { case IpcConnection::IPC_EVENT_CONNECTION_CLOSED: {
Mutex::Lock _l(((NodeControlService *)arg)->_connections_m); Mutex::Lock _l(((NodeControlService *)arg)->_connections_m);
((NodeControlService *)arg)->_connections.erase(ipcc); ((NodeControlService *)arg)->_connections.erase(ipcc);

View file

@ -415,4 +415,9 @@
*/ */
#define ZT_MAX_BRIDGE_SPAM 16 #define ZT_MAX_BRIDGE_SPAM 16
/**
* Timeout for IPC connections (e.g. unix domain sockets) in seconds
*/
#define ZT_IPC_TIMEOUT 600
#endif #endif

View file

@ -493,9 +493,9 @@ Node::ReasonForTermination Node::run()
lastShutdownIfUnreadableCheck = now; lastShutdownIfUnreadableCheck = now;
if (Utils::fileExists(shutdownIfUnreadablePath.c_str(),false)) { if (Utils::fileExists(shutdownIfUnreadablePath.c_str(),false)) {
int tmpfd = ::open(shutdownIfUnreadablePath.c_str(),O_RDONLY,0); int tmpfd = ::open(shutdownIfUnreadablePath.c_str(),O_RDONLY,0);
if (tmpfd < 0) if (tmpfd < 0) {
return impl->terminateBecause(Node::NODE_NORMAL_TERMINATION,"shutdownIfUnreadable exists but is not readable"); return impl->terminateBecause(Node::NODE_NORMAL_TERMINATION,"shutdownIfUnreadable exists but is not readable");
else ::close(tmpfd); } else ::close(tmpfd);
} }
} }
#endif #endif