2013-07-31 21:24:59 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
2014-02-16 20:40:22 +00:00
|
|
|
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
2013-07-31 21:24:59 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Constants.hpp"
|
|
|
|
|
|
|
|
#ifndef __WINDOWS__
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <fcntl.h>
|
2013-08-02 18:25:23 +00:00
|
|
|
#include <errno.h>
|
2013-07-31 21:24:59 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include "Service.hpp"
|
|
|
|
#include "RuntimeEnvironment.hpp"
|
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "Logger.hpp"
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
Service::Service(const RuntimeEnvironment *renv,const char *name,const char *path,void (*handler)(void *,Service &,const Dictionary &),void *arg) :
|
|
|
|
_r(renv),
|
|
|
|
_path(path),
|
|
|
|
_name(name),
|
|
|
|
_arg(arg),
|
|
|
|
_handler(handler),
|
|
|
|
_pid(-1),
|
|
|
|
_childStdin(0),
|
|
|
|
_childStdout(0),
|
|
|
|
_childStderr(0),
|
|
|
|
_run(true)
|
|
|
|
{
|
2013-08-08 13:19:36 +00:00
|
|
|
_thread = Thread::start(this);
|
2013-07-31 21:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Service::~Service()
|
|
|
|
{
|
|
|
|
_run = false;
|
|
|
|
long pid = _pid;
|
|
|
|
if (pid > 0) {
|
|
|
|
int st = 0;
|
|
|
|
::kill(pid,SIGTERM);
|
|
|
|
for(int i=0;i<20;++i) {
|
|
|
|
if (waitpid(pid,&st,WNOHANG) == pid) {
|
|
|
|
pid = 0;
|
|
|
|
break;
|
|
|
|
}
|
2013-08-08 13:19:36 +00:00
|
|
|
Thread::sleep(100);
|
2013-07-31 21:24:59 +00:00
|
|
|
}
|
|
|
|
if (pid > 0) {
|
|
|
|
::kill(pid,SIGKILL);
|
|
|
|
waitpid(pid,&st,0);
|
|
|
|
}
|
|
|
|
}
|
2013-08-08 13:19:36 +00:00
|
|
|
Thread::join(_thread);
|
2013-07-31 21:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Service::send(const Dictionary &msg)
|
|
|
|
{
|
|
|
|
if (_childStdin <= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string mser = msg.toString();
|
|
|
|
if (mser.length() > ZT_SERVICE_MAX_MESSAGE_SIZE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// This can technically block. We'll fix this if it ends up being a
|
|
|
|
// problem.
|
|
|
|
uint32_t len = Utils::hton((uint32_t)mser.length());
|
|
|
|
if (write(_childStdin,&len,4) != 4)
|
|
|
|
return false;
|
|
|
|
if ((int)write(_childStdin,mser.data(),mser.length()) != (int)mser.length())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:06:16 +00:00
|
|
|
void Service::threadMain()
|
2013-07-31 21:24:59 +00:00
|
|
|
throw()
|
|
|
|
{
|
2013-08-02 18:25:23 +00:00
|
|
|
char buf[131072];
|
2013-07-31 21:24:59 +00:00
|
|
|
fd_set readfds,writefds,exceptfds;
|
|
|
|
struct timeval tv;
|
|
|
|
|
2013-08-01 14:11:59 +00:00
|
|
|
std::string stderrBuf;
|
|
|
|
std::string stdoutBuf;
|
|
|
|
unsigned int stdoutExpecting = 0;
|
|
|
|
|
2013-07-31 21:24:59 +00:00
|
|
|
while (_run) {
|
|
|
|
if (_pid <= 0) {
|
|
|
|
LOG("launching service %s...",_name.c_str());
|
|
|
|
|
|
|
|
int in[2],out[2],err[2];
|
|
|
|
pipe(in);
|
|
|
|
pipe(out);
|
|
|
|
pipe(err);
|
|
|
|
|
2013-08-02 18:25:23 +00:00
|
|
|
long pid = vfork();
|
2013-07-31 21:24:59 +00:00
|
|
|
if (pid < 0) {
|
|
|
|
LOG("service %s terminating: could not fork!",_name.c_str());
|
|
|
|
return;
|
|
|
|
} else if (pid) {
|
2013-08-02 18:25:23 +00:00
|
|
|
// Parent
|
|
|
|
close(in[0]);
|
|
|
|
close(out[1]);
|
|
|
|
close(err[1]);
|
2013-08-08 13:19:36 +00:00
|
|
|
Thread::sleep(500); // give child time to start
|
2013-07-31 21:24:59 +00:00
|
|
|
_childStdin = in[1];
|
|
|
|
_childStdout = out[0];
|
|
|
|
_childStderr = err[0];
|
2013-08-01 14:11:59 +00:00
|
|
|
fcntl(_childStdout,F_SETFL,O_NONBLOCK);
|
|
|
|
fcntl(_childStderr,F_SETFL,O_NONBLOCK);
|
2013-08-02 18:25:23 +00:00
|
|
|
_pid = pid;
|
2013-07-31 21:24:59 +00:00
|
|
|
} else {
|
2013-08-02 18:25:23 +00:00
|
|
|
// Child
|
2013-08-01 14:11:59 +00:00
|
|
|
close(in[1]);
|
|
|
|
close(out[0]);
|
|
|
|
close(err[0]);
|
2013-08-02 18:25:23 +00:00
|
|
|
dup2(in[0],STDIN_FILENO);
|
|
|
|
dup2(out[1],STDOUT_FILENO);
|
|
|
|
dup2(err[1],STDERR_FILENO);
|
2013-07-31 21:24:59 +00:00
|
|
|
execl(_path.c_str(),_path.c_str(),_r->homePath.c_str(),(const char *)0);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int st = 0;
|
|
|
|
if (waitpid(_pid,&st,WNOHANG) == _pid) {
|
|
|
|
if (_childStdin > 0) close(_childStdin);
|
|
|
|
_childStdin = 0;
|
|
|
|
if (_childStdout > 0) close(_childStdout);
|
|
|
|
if (_childStderr > 0) close(_childStderr);
|
|
|
|
_pid = 0;
|
|
|
|
|
|
|
|
if (!_run)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG("service %s exited with exit code: %d, delaying 1s to attempt relaunch",_name.c_str(),st);
|
|
|
|
|
2013-08-08 13:19:36 +00:00
|
|
|
Thread::sleep(1000); // wait to relaunch
|
2013-07-31 21:24:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 18:25:23 +00:00
|
|
|
// If we've made it here, _pid is running last we checked.
|
|
|
|
|
2013-07-31 21:24:59 +00:00
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_ZERO(&writefds);
|
|
|
|
FD_ZERO(&exceptfds);
|
|
|
|
|
|
|
|
FD_SET(_childStdout,&readfds);
|
|
|
|
FD_SET(_childStderr,&readfds);
|
|
|
|
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
select(std::max(_childStdout,_childStderr)+1,&readfds,&writefds,&exceptfds,&tv);
|
|
|
|
|
|
|
|
if (!_run) {
|
|
|
|
if (_childStdin > 0) close(_childStdin);
|
|
|
|
_childStdin = 0;
|
|
|
|
if (_childStdout > 0) close(_childStdout);
|
|
|
|
if (_childStderr > 0) close(_childStderr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((_childStderr > 0)&&(FD_ISSET(_childStderr,&readfds))) {
|
2013-08-01 14:11:59 +00:00
|
|
|
int n = (int)read(_childStderr,buf,sizeof(buf));
|
|
|
|
for(int i=0;i<n;++i) {
|
|
|
|
if ((buf[i] == '\r')||(buf[i] == '\n')) {
|
|
|
|
stderrBuf = Utils::trim(stderrBuf);
|
|
|
|
if (stderrBuf.length())
|
|
|
|
LOG("service %s: %s",_name.c_str(),stderrBuf.c_str());
|
|
|
|
stderrBuf = "";
|
|
|
|
} else stderrBuf.push_back(buf[i]);
|
|
|
|
}
|
2013-07-31 21:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((_childStdout > 0)&&(FD_ISSET(_childStdout,&readfds))) {
|
2013-08-01 14:11:59 +00:00
|
|
|
int n = (int)read(_childStdout,buf,sizeof(buf));
|
|
|
|
for(int i=0;i<n;++i) {
|
|
|
|
stdoutBuf.push_back(buf[i]);
|
|
|
|
if (stdoutExpecting) {
|
|
|
|
if (stdoutBuf.length() == stdoutExpecting) {
|
|
|
|
try {
|
|
|
|
_handler(_arg,*this,Dictionary(stdoutBuf));
|
|
|
|
} catch ( ... ) {
|
|
|
|
LOG("unexpected exception handling message from service %s",_name.c_str());
|
|
|
|
}
|
|
|
|
stdoutBuf = "";
|
|
|
|
stdoutExpecting = 0;
|
|
|
|
}
|
|
|
|
} else if (stdoutBuf.length() == 4) {
|
|
|
|
stdoutExpecting = Utils::ntoh(*((const uint32_t *)stdoutBuf.data()));
|
|
|
|
stdoutBuf = "";
|
|
|
|
if (stdoutExpecting > ZT_SERVICE_MAX_MESSAGE_SIZE) {
|
|
|
|
LOG("message size overrun from service %s: %u bytes -- restarting service",_name.c_str(),stdoutExpecting);
|
|
|
|
stdoutExpecting = 0;
|
|
|
|
kill(_pid,SIGKILL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-31 21:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif // __WINDOWS__
|
|
|
|
|