2004-05-22 00:05:20 +00:00
|
|
|
/**
|
|
|
|
* Engine.cpp
|
|
|
|
* This file is part of the YATE Project http://YATE.null.ro
|
2004-11-29 03:56:41 +00:00
|
|
|
*
|
|
|
|
* Yet Another Telephony Engine - a fully featured software PBX and IVR
|
2005-04-29 22:05:07 +00:00
|
|
|
* Copyright (C) 2004, 2005 Null Team
|
2004-11-29 03:56:41 +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 2 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2004-05-22 00:05:20 +00:00
|
|
|
*/
|
|
|
|
|
2005-03-18 18:16:59 +00:00
|
|
|
#include "yatengine.h"
|
2004-12-01 15:01:30 +00:00
|
|
|
#include "yateversn.h"
|
2005-04-02 00:49:38 +00:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
#include <process.h>
|
|
|
|
#define RTLD_NOW 0
|
|
|
|
#define dlopen(name,flags) LoadLibrary(name)
|
|
|
|
#define dlclose !FreeLibrary
|
|
|
|
#define dlerror() "LoadLibrary error"
|
2005-05-20 23:08:16 +00:00
|
|
|
#define YSERV_RUN 1
|
|
|
|
#define YSERV_INS 2
|
|
|
|
#define YSERV_DEL 4
|
2005-05-24 15:09:19 +00:00
|
|
|
#define PATH_SEP "\\"
|
2005-04-02 00:49:38 +00:00
|
|
|
#else
|
|
|
|
#include "yatepaths.h"
|
2004-05-22 00:05:20 +00:00
|
|
|
#include <dirent.h>
|
2005-04-02 00:49:38 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
typedef void* HMODULE;
|
2005-05-24 15:09:19 +00:00
|
|
|
#define PATH_SEP "/"
|
2005-04-02 00:49:38 +00:00
|
|
|
#endif
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
namespace TelEngine {
|
|
|
|
|
|
|
|
class EnginePrivate : public Thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EnginePrivate()
|
|
|
|
: Thread("EnginePrivate")
|
|
|
|
{ count++; }
|
|
|
|
~EnginePrivate()
|
|
|
|
{ count--; }
|
|
|
|
virtual void run();
|
|
|
|
static int count;
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
using namespace TelEngine;
|
|
|
|
|
|
|
|
#ifndef MOD_PATH
|
2005-05-24 15:09:19 +00:00
|
|
|
#define MOD_PATH "." PATH_SEP "modules"
|
2004-05-22 00:05:20 +00:00
|
|
|
#endif
|
|
|
|
#ifndef CFG_PATH
|
2005-05-24 15:09:19 +00:00
|
|
|
#define CFG_PATH "." PATH_SEP "conf.d"
|
2004-05-22 00:05:20 +00:00
|
|
|
#endif
|
|
|
|
#define DLL_SUFFIX ".yate"
|
|
|
|
#define CFG_SUFFIX ".conf"
|
|
|
|
|
2005-04-23 19:33:37 +00:00
|
|
|
#define MAX_SANITY 5
|
|
|
|
#define INIT_SANITY 10
|
2004-10-21 13:50:45 +00:00
|
|
|
|
2005-04-02 00:49:38 +00:00
|
|
|
static u_int64_t s_nextinit = 0;
|
|
|
|
static u_int64_t s_restarts = 0;
|
2004-05-22 00:05:20 +00:00
|
|
|
static bool s_makeworker = true;
|
|
|
|
static bool s_keepclosing = false;
|
2004-10-21 13:50:45 +00:00
|
|
|
static int s_super_handle = -1;
|
2004-05-22 00:05:20 +00:00
|
|
|
|
|
|
|
static void sighandler(int signal)
|
|
|
|
{
|
|
|
|
switch (signal) {
|
2005-04-02 00:49:38 +00:00
|
|
|
#ifndef _WINDOWS
|
2005-09-29 19:34:23 +00:00
|
|
|
case SIGCHLD:
|
|
|
|
::waitpid(-1,0,WNOHANG);
|
|
|
|
break;
|
|
|
|
case SIGUSR1:
|
|
|
|
Engine::restart(0,true);
|
|
|
|
break;
|
|
|
|
case SIGUSR2:
|
|
|
|
Engine::restart(0,false);
|
|
|
|
break;
|
2004-05-22 00:05:20 +00:00
|
|
|
case SIGHUP:
|
|
|
|
case SIGQUIT:
|
|
|
|
if (s_nextinit <= Time::now())
|
|
|
|
Engine::init();
|
|
|
|
s_nextinit = Time::now() + 2000000;
|
|
|
|
break;
|
2005-04-02 00:49:38 +00:00
|
|
|
#endif
|
2004-05-22 00:05:20 +00:00
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
Engine::halt(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String Engine::s_cfgpath(CFG_PATH);
|
|
|
|
String Engine::s_cfgsuffix(CFG_SUFFIX);
|
|
|
|
String Engine::s_modpath(MOD_PATH);
|
2005-07-01 21:12:16 +00:00
|
|
|
String Engine::s_extramod;
|
2004-05-22 00:05:20 +00:00
|
|
|
String Engine::s_modsuffix(DLL_SUFFIX);
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
Engine* Engine::s_self = 0;
|
2004-05-22 00:05:20 +00:00
|
|
|
int Engine::s_haltcode = -1;
|
|
|
|
int EnginePrivate::count = 0;
|
2005-04-28 22:46:59 +00:00
|
|
|
bool s_init = false;
|
|
|
|
bool s_dynplugin = false;
|
|
|
|
int s_maxworkers = 10;
|
2004-05-22 00:05:20 +00:00
|
|
|
|
2005-05-20 23:14:11 +00:00
|
|
|
static bool s_sigabrt = false;
|
2005-01-10 17:57:37 +00:00
|
|
|
const char* s_cfgfile = 0;
|
2005-05-24 15:09:19 +00:00
|
|
|
const char* s_logfile = 0;
|
2005-04-28 22:46:59 +00:00
|
|
|
Configuration s_cfg;
|
2004-05-22 00:05:20 +00:00
|
|
|
ObjList plugins;
|
2005-03-20 03:11:53 +00:00
|
|
|
ObjList* s_cmds = 0;
|
2004-05-22 00:05:20 +00:00
|
|
|
|
|
|
|
class SLib : public GenObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~SLib();
|
2005-03-20 03:11:53 +00:00
|
|
|
static SLib* load(const char* file);
|
2004-05-22 00:05:20 +00:00
|
|
|
private:
|
2005-04-02 00:49:38 +00:00
|
|
|
SLib(HMODULE handle, const char* file);
|
2005-03-20 03:11:53 +00:00
|
|
|
const char* m_file;
|
2005-04-02 00:49:38 +00:00
|
|
|
HMODULE m_handle;
|
2004-05-22 00:05:20 +00:00
|
|
|
};
|
|
|
|
|
2004-10-21 13:50:45 +00:00
|
|
|
class EngineSuperHandler : public MessageHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EngineSuperHandler() : MessageHandler("engine.timer",0), m_seq(0) { }
|
|
|
|
virtual bool received(Message &msg)
|
|
|
|
{ ::write(s_super_handle,&m_seq,1); m_seq++; return false; }
|
|
|
|
char m_seq;
|
|
|
|
};
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
class EngineStatusHandler : public MessageHandler
|
|
|
|
{
|
|
|
|
public:
|
2004-12-21 04:16:09 +00:00
|
|
|
EngineStatusHandler() : MessageHandler("engine.status",0) { }
|
2004-05-22 00:05:20 +00:00
|
|
|
virtual bool received(Message &msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool EngineStatusHandler::received(Message &msg)
|
|
|
|
{
|
|
|
|
const char *sel = msg.getValue("module");
|
|
|
|
if (sel && ::strcmp(sel,"engine"))
|
|
|
|
return false;
|
2004-12-01 14:25:30 +00:00
|
|
|
msg.retValue() << "name=engine,type=system";
|
2005-06-13 12:08:56 +00:00
|
|
|
msg.retValue() << ",version=" << YATE_VERSION;
|
2004-12-01 14:25:30 +00:00
|
|
|
msg.retValue() << ";plugins=" << plugins.count();
|
2004-11-01 00:07:00 +00:00
|
|
|
msg.retValue() << ",inuse=" << Engine::self()->usedPlugins();
|
2005-06-07 16:12:25 +00:00
|
|
|
msg.retValue() << ",handlers=" << Engine::self()->handlerCount();
|
|
|
|
msg.retValue() << ",messages=" << Engine::self()->messageCount();
|
2005-04-14 03:14:20 +00:00
|
|
|
msg.retValue() << ",supervised=" << (s_super_handle >= 0);
|
2004-09-29 00:15:52 +00:00
|
|
|
msg.retValue() << ",threads=" << Thread::count();
|
2004-05-22 00:05:20 +00:00
|
|
|
msg.retValue() << ",workers=" << EnginePrivate::count;
|
2004-10-14 15:47:50 +00:00
|
|
|
msg.retValue() << ",mutexes=" << Mutex::count();
|
|
|
|
msg.retValue() << ",locks=" << Mutex::locks();
|
2004-05-22 00:05:20 +00:00
|
|
|
msg.retValue() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnginePrivate::run()
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
s_makeworker = false;
|
|
|
|
Engine::self()->m_dispatcher.dequeue();
|
2005-04-11 21:20:12 +00:00
|
|
|
msleep(5,true);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-24 15:09:19 +00:00
|
|
|
static bool logFileOpen()
|
|
|
|
{
|
|
|
|
if (s_logfile) {
|
|
|
|
int fd = ::open(s_logfile,O_WRONLY|O_CREAT|O_APPEND,0640);
|
|
|
|
if (fd >= 0) {
|
|
|
|
// Redirect stdout and stderr to the new file
|
|
|
|
::fflush(stdout);
|
|
|
|
::dup2(fd,1);
|
|
|
|
::fflush(stderr);
|
|
|
|
::dup2(fd,2);
|
|
|
|
::close(fd);
|
|
|
|
Debugger::enableOutput(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-05-20 23:08:16 +00:00
|
|
|
static int engineRun()
|
|
|
|
{
|
|
|
|
time_t t = ::time(0);
|
|
|
|
Output("Yate (%u) is starting %s",::getpid(),::ctime(&t));
|
|
|
|
int retcode = Engine::self()->run();
|
|
|
|
t = ::time(0);
|
|
|
|
Output("Yate (%u) is stopping %s",::getpid(),::ctime(&t));
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WINDOWS
|
|
|
|
|
|
|
|
static SERVICE_STATUS_HANDLE s_handler = 0;
|
|
|
|
static SERVICE_STATUS s_status;
|
|
|
|
|
|
|
|
static void setStatus(DWORD state)
|
|
|
|
{
|
|
|
|
if (!s_handler)
|
|
|
|
return;
|
2005-06-02 09:29:09 +00:00
|
|
|
switch (state) {
|
|
|
|
case SERVICE_START_PENDING:
|
|
|
|
case SERVICE_STOP_PENDING:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s_status.dwCheckPoint = 0;
|
|
|
|
}
|
2005-05-20 23:08:16 +00:00
|
|
|
s_status.dwCurrentState = state;
|
2005-06-02 09:29:09 +00:00
|
|
|
::SetServiceStatus(s_handler,&s_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkPoint()
|
|
|
|
{
|
|
|
|
if (!s_handler)
|
|
|
|
return;
|
|
|
|
s_status.dwCheckPoint++;
|
|
|
|
::SetServiceStatus(s_handler,&s_status);
|
2005-05-20 23:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void WINAPI serviceHandler(DWORD code)
|
|
|
|
{
|
|
|
|
switch (code) {
|
|
|
|
case SERVICE_CONTROL_STOP:
|
2005-06-02 09:29:09 +00:00
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
2005-05-20 23:08:16 +00:00
|
|
|
Engine::halt(0);
|
2005-06-02 09:29:09 +00:00
|
|
|
setStatus(SERVICE_STOP_PENDING);
|
2005-05-20 23:08:16 +00:00
|
|
|
break;
|
|
|
|
case SERVICE_CONTROL_PARAMCHANGE:
|
|
|
|
Engine::init();
|
|
|
|
break;
|
2005-06-02 09:29:09 +00:00
|
|
|
case SERVICE_CONTROL_INTERROGATE:
|
|
|
|
break;
|
2005-05-20 23:08:16 +00:00
|
|
|
default:
|
|
|
|
Debug(DebugWarn,"Got unexpected service control code %u",code);
|
|
|
|
}
|
2005-06-02 09:29:09 +00:00
|
|
|
if (s_handler)
|
|
|
|
::SetServiceStatus(s_handler,&s_status);
|
2005-05-20 23:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void serviceMain(DWORD argc, LPTSTR* argv)
|
|
|
|
{
|
2005-05-24 15:09:19 +00:00
|
|
|
logFileOpen();
|
2005-05-20 23:08:16 +00:00
|
|
|
s_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
|
|
s_status.dwCurrentState = SERVICE_START_PENDING;
|
|
|
|
s_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PARAMCHANGE;
|
|
|
|
s_status.dwWin32ExitCode = NO_ERROR;
|
|
|
|
s_status.dwServiceSpecificExitCode = 0;
|
|
|
|
s_status.dwCheckPoint = 0;
|
|
|
|
s_status.dwWaitHint = 0;
|
2005-06-02 09:29:09 +00:00
|
|
|
s_handler = ::RegisterServiceCtrlHandler("yate",serviceHandler);
|
2005-05-20 23:08:16 +00:00
|
|
|
if (!s_handler) {
|
2005-06-02 09:29:09 +00:00
|
|
|
Debug(DebugFail,"Could not register service control handler \"yate\", code %u\n",::GetLastError());
|
2005-05-20 23:08:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
setStatus(SERVICE_START_PENDING);
|
|
|
|
engineRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
static SERVICE_TABLE_ENTRY dispatchTable[] =
|
|
|
|
{
|
|
|
|
{ TEXT("yate"), (LPSERVICE_MAIN_FUNCTION)serviceMain },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
#else /* _WINDOWS */
|
|
|
|
|
|
|
|
#define setStatus(s)
|
2005-06-02 09:29:09 +00:00
|
|
|
#define checkPoint()
|
2005-05-20 23:08:16 +00:00
|
|
|
|
|
|
|
static bool s_runagain = true;
|
|
|
|
static pid_t s_childpid = -1;
|
|
|
|
|
|
|
|
static void superhandler(int signal)
|
|
|
|
{
|
|
|
|
switch (signal) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
case SIGABRT:
|
|
|
|
s_runagain = false;
|
|
|
|
}
|
|
|
|
if (s_childpid > 0)
|
|
|
|
::kill(s_childpid,signal);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int supervise(void)
|
|
|
|
{
|
|
|
|
::fprintf(stderr,"Supervisor (%u) is starting\n",::getpid());
|
|
|
|
::signal(SIGINT,superhandler);
|
|
|
|
::signal(SIGTERM,superhandler);
|
|
|
|
::signal(SIGHUP,superhandler);
|
|
|
|
::signal(SIGQUIT,superhandler);
|
|
|
|
::signal(SIGABRT,superhandler);
|
2005-09-29 19:34:23 +00:00
|
|
|
::signal(SIGUSR1,superhandler);
|
|
|
|
::signal(SIGUSR2,superhandler);
|
2005-05-20 23:08:16 +00:00
|
|
|
int retcode = 0;
|
|
|
|
while (s_runagain) {
|
|
|
|
int wdogfd[2];
|
|
|
|
if (::pipe(wdogfd)) {
|
|
|
|
int err = errno;
|
|
|
|
::fprintf(stderr,"Supervisor: pipe failed: %s (%d)\n",::strerror(err),err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
::fcntl(wdogfd[0],F_SETFL,O_NONBLOCK);
|
|
|
|
::fcntl(wdogfd[1],F_SETFL,O_NONBLOCK);
|
|
|
|
s_childpid = ::fork();
|
|
|
|
if (s_childpid < 0) {
|
|
|
|
int err = errno;
|
|
|
|
::fprintf(stderr,"Supervisor: fork failed: %s (%d)\n",::strerror(err),err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (s_childpid == 0) {
|
|
|
|
s_super_handle = wdogfd[1];
|
|
|
|
::close(wdogfd[0]);
|
|
|
|
::signal(SIGINT,SIG_DFL);
|
|
|
|
::signal(SIGTERM,SIG_DFL);
|
|
|
|
::signal(SIGHUP,SIG_DFL);
|
|
|
|
::signal(SIGQUIT,SIG_DFL);
|
|
|
|
::signal(SIGABRT,SIG_DFL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
::close(wdogfd[1]);
|
|
|
|
// Wait for the child to die or block
|
|
|
|
for (int sanity = INIT_SANITY; sanity > 0; sanity--) {
|
|
|
|
int status = -1;
|
|
|
|
int tmp = ::waitpid(s_childpid,&status,WNOHANG);
|
|
|
|
if (tmp > 0) {
|
|
|
|
// Child exited for some reason
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
retcode = WEXITSTATUS(status);
|
|
|
|
if (retcode <= 127)
|
|
|
|
s_runagain = false;
|
|
|
|
else
|
|
|
|
retcode &= 127;
|
|
|
|
}
|
|
|
|
else if (WIFSIGNALED(status)) {
|
|
|
|
retcode = WTERMSIG(status);
|
|
|
|
::fprintf(stderr,"Supervisor: child %d died on signal %d\n",s_childpid,retcode);
|
|
|
|
}
|
|
|
|
s_childpid = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[MAX_SANITY];
|
|
|
|
tmp = ::read(wdogfd[0],buf,sizeof(buf));
|
|
|
|
if (tmp >= 0) {
|
|
|
|
// Timer messages add one sanity point every second
|
|
|
|
sanity += tmp;
|
|
|
|
if (sanity > MAX_SANITY)
|
|
|
|
sanity = MAX_SANITY;
|
|
|
|
}
|
|
|
|
else if ((errno != EINTR) && (errno != EAGAIN))
|
|
|
|
break;
|
|
|
|
// Consume sanity points slightly slower than added
|
|
|
|
::usleep(1200000);
|
|
|
|
}
|
|
|
|
::close(wdogfd[0]);
|
|
|
|
if (s_childpid > 0) {
|
|
|
|
// Child failed to proof sanity. Kill it - no need to be gentle.
|
|
|
|
::fprintf(stderr,"Supervisor: killing unresponsive child %d\n",s_childpid);
|
|
|
|
// If -Da was specified try to get a corefile
|
|
|
|
if (s_sigabrt) {
|
|
|
|
::kill(s_childpid,SIGABRT);
|
|
|
|
::usleep(250000);
|
|
|
|
}
|
|
|
|
::kill(s_childpid,SIGKILL);
|
|
|
|
::usleep(10000);
|
|
|
|
::waitpid(s_childpid,0,WNOHANG);
|
|
|
|
s_childpid = -1;
|
|
|
|
}
|
|
|
|
if (s_runagain)
|
|
|
|
::usleep(1000000);
|
|
|
|
}
|
|
|
|
::fprintf(stderr,"Supervisor (%d) exiting with code %d\n",::getpid(),retcode);
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
#endif /* _WINDOWS */
|
|
|
|
|
2005-06-02 09:29:09 +00:00
|
|
|
SLib::SLib(HMODULE handle, const char* file)
|
|
|
|
: m_handle(handle)
|
|
|
|
{
|
|
|
|
DDebug(DebugAll,"SLib::SLib(%p,'%s') [%p]",handle,file,this);
|
|
|
|
checkPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
SLib::~SLib()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
Debugger debug("SLib::~SLib()"," [%p]",this);
|
|
|
|
#endif
|
|
|
|
int err = dlclose(m_handle);
|
|
|
|
if (err)
|
|
|
|
Debug(DebugGoOn,"Error %d on dlclose(%p)",err,m_handle);
|
|
|
|
else if (s_keepclosing) {
|
|
|
|
int tries;
|
|
|
|
for (tries=0; tries<10; tries++)
|
|
|
|
if (dlclose(m_handle))
|
|
|
|
break;
|
|
|
|
if (tries)
|
|
|
|
Debug(DebugGoOn,"Made %d attempts to dlclose(%p)",tries,m_handle);
|
|
|
|
}
|
|
|
|
checkPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
SLib* SLib::load(const char* file)
|
|
|
|
{
|
|
|
|
DDebug(DebugAll,"SLib::load('%s')",file);
|
|
|
|
HMODULE handle = ::dlopen(file,RTLD_NOW);
|
|
|
|
if (handle)
|
|
|
|
return new SLib(handle,file);
|
|
|
|
#ifdef _WINDOWS
|
|
|
|
Debug(DebugWarn,"LoadLibrary error %u in '%s'",::GetLastError(),file);
|
|
|
|
#else
|
|
|
|
Debug(DebugWarn,dlerror());
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
Engine::Engine()
|
|
|
|
{
|
2005-04-02 13:28:14 +00:00
|
|
|
DDebug(DebugAll,"Engine::Engine() [%p]",this);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Engine::~Engine()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
Debugger debug("Engine::~Engine()"," [%p]",this);
|
|
|
|
#endif
|
|
|
|
assert(this == s_self);
|
|
|
|
m_dispatcher.clear();
|
|
|
|
plugins.clear();
|
2004-05-24 00:38:23 +00:00
|
|
|
m_libs.clear();
|
2004-05-22 00:05:20 +00:00
|
|
|
s_self = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Engine::run()
|
|
|
|
{
|
2005-04-09 22:10:00 +00:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
// In Windows we must initialize the socket library very early because even trivial
|
|
|
|
// functions like endianess swapping - ntohl and family - need it to be initialized
|
|
|
|
WSADATA wsaData;
|
|
|
|
int errc = ::WSAStartup(MAKEWORD(2,2), &wsaData);
|
|
|
|
if (errc) {
|
2005-07-01 22:35:06 +00:00
|
|
|
Debug(DebugGoOn,"Failed to initialize the Windows Sockets library, error code %d",errc);
|
2005-04-09 22:10:00 +00:00
|
|
|
return errc & 127;
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-28 22:46:59 +00:00
|
|
|
s_cfg = configFile(s_cfgfile);
|
|
|
|
s_cfg.load();
|
2004-10-19 16:01:48 +00:00
|
|
|
Debug(DebugAll,"Engine::run()");
|
2004-05-22 00:05:20 +00:00
|
|
|
install(new EngineStatusHandler);
|
|
|
|
loadPlugins();
|
2005-07-01 22:35:06 +00:00
|
|
|
Debug(DebugAll,"Loaded %d plugins",plugins.count());
|
2004-11-01 00:07:00 +00:00
|
|
|
if (s_super_handle >= 0) {
|
|
|
|
install(new EngineSuperHandler);
|
|
|
|
if (s_restarts)
|
|
|
|
s_restarts = 1000000 * s_restarts + Time::now();
|
|
|
|
}
|
|
|
|
else if (s_restarts) {
|
|
|
|
Debug(DebugWarn,"No supervisor - disabling automatic restarts");
|
|
|
|
s_restarts = 0;
|
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
initPlugins();
|
2005-06-02 09:29:09 +00:00
|
|
|
checkPoint();
|
2004-05-22 00:05:20 +00:00
|
|
|
::signal(SIGINT,sighandler);
|
|
|
|
::signal(SIGTERM,sighandler);
|
2005-07-01 22:35:06 +00:00
|
|
|
Debug(DebugAll,"Engine dispatching start message");
|
2004-05-22 00:05:20 +00:00
|
|
|
dispatch("engine.start");
|
2005-05-20 23:08:16 +00:00
|
|
|
setStatus(SERVICE_RUNNING);
|
2005-05-31 19:04:18 +00:00
|
|
|
long corr = 0;
|
2005-04-02 00:49:38 +00:00
|
|
|
#ifndef _WINDOWS
|
2004-05-22 00:05:20 +00:00
|
|
|
::signal(SIGHUP,sighandler);
|
|
|
|
::signal(SIGQUIT,sighandler);
|
2005-01-22 00:40:20 +00:00
|
|
|
::signal(SIGPIPE,SIG_IGN);
|
2005-09-29 19:34:23 +00:00
|
|
|
::signal(SIGCHLD,sighandler);
|
|
|
|
::signal(SIGUSR1,sighandler);
|
|
|
|
::signal(SIGUSR2,sighandler);
|
2005-04-02 00:49:38 +00:00
|
|
|
#endif
|
2005-01-09 06:42:56 +00:00
|
|
|
Output("Yate engine is initialized and starting up");
|
2004-05-22 00:05:20 +00:00
|
|
|
while (s_haltcode == -1) {
|
2005-01-24 15:02:14 +00:00
|
|
|
if (s_cmds) {
|
|
|
|
Output("Executing initial commands");
|
2005-04-08 12:45:19 +00:00
|
|
|
for (ObjList* c = s_cmds->skipNull(); c; c=c->skipNext()) {
|
2005-01-24 15:02:14 +00:00
|
|
|
String* s = static_cast<String*>(c->get());
|
2005-04-08 12:45:19 +00:00
|
|
|
Message m("engine.command");
|
|
|
|
m.addParam("line",*s);
|
|
|
|
if (dispatch(m)) {
|
|
|
|
if (m.retValue())
|
|
|
|
Output("%s",m.retValue().c_str());
|
2005-01-24 15:02:14 +00:00
|
|
|
}
|
2005-04-08 12:45:19 +00:00
|
|
|
else
|
|
|
|
Debug(DebugWarn,"Unrecognized command '%s'",s->c_str());
|
2005-01-24 15:02:14 +00:00
|
|
|
}
|
|
|
|
s_cmds->destruct();
|
|
|
|
s_cmds = 0;
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
if (s_init) {
|
|
|
|
s_init = false;
|
|
|
|
initPlugins();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create worker thread if we didn't hear about any of them in a while
|
|
|
|
if (s_makeworker && (EnginePrivate::count < s_maxworkers)) {
|
2005-08-02 02:20:00 +00:00
|
|
|
Debug(EnginePrivate::count ? DebugMild : DebugInfo,
|
|
|
|
"Creating new message dispatching thread (%d running)",EnginePrivate::count);
|
2004-11-01 00:07:00 +00:00
|
|
|
EnginePrivate *prv = new EnginePrivate;
|
|
|
|
prv->startup();
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
2004-06-03 22:26:29 +00:00
|
|
|
else
|
|
|
|
s_makeworker = true;
|
2004-05-22 00:05:20 +00:00
|
|
|
|
2004-11-01 00:07:00 +00:00
|
|
|
if (s_restarts && (Time::now() >= s_restarts)) {
|
|
|
|
if (!(usedPlugins() || dispatch("engine.busy"))) {
|
|
|
|
s_haltcode = 128;
|
|
|
|
break;
|
|
|
|
}
|
2005-09-29 19:34:23 +00:00
|
|
|
DDebug(DebugAll,"Engine busy - will try to restart later");
|
2004-11-01 00:07:00 +00:00
|
|
|
// If we cannot restart now try again in 10s
|
|
|
|
s_restarts = Time::now() + 10000000;
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
// Attempt to sleep until the next full second
|
2005-05-31 19:04:18 +00:00
|
|
|
long t = 1000000 - (long)(Time::now() % 1000000) - corr;
|
|
|
|
if (t < 250000)
|
|
|
|
t += 1000000;
|
|
|
|
XDebug(DebugAll,"Sleeping for %ld",t);
|
|
|
|
Thread::usleep(t);
|
2004-06-03 22:26:29 +00:00
|
|
|
Message *m = new Message("engine.timer");
|
|
|
|
m->addParam("time",String((int)m->msgTime().sec()));
|
2004-05-22 00:05:20 +00:00
|
|
|
// Try to fine tune the ticker
|
2005-05-31 19:04:18 +00:00
|
|
|
t = (long)(m->msgTime().usec() % 1000000);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (t > 500000)
|
|
|
|
corr -= (1000000-t)/10;
|
|
|
|
else
|
|
|
|
corr += t/10;
|
2005-05-31 19:04:18 +00:00
|
|
|
XDebug(DebugAll,"Adjustment at %ld, corr %ld",t,corr);
|
2004-06-03 22:26:29 +00:00
|
|
|
enqueue(m);
|
2004-10-21 13:50:45 +00:00
|
|
|
Thread::yield();
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
2004-12-01 14:25:30 +00:00
|
|
|
s_haltcode &= 0xff;
|
2005-01-09 06:42:56 +00:00
|
|
|
Output("Yate engine is shutting down with code %d",s_haltcode);
|
2005-05-20 23:08:16 +00:00
|
|
|
setStatus(SERVICE_STOP_PENDING);
|
2004-05-22 00:05:20 +00:00
|
|
|
dispatch("engine.halt");
|
2005-06-02 09:29:09 +00:00
|
|
|
checkPoint();
|
2005-04-23 19:33:37 +00:00
|
|
|
Thread::msleep(200);
|
2004-05-22 00:05:20 +00:00
|
|
|
m_dispatcher.dequeue();
|
2005-06-02 09:29:09 +00:00
|
|
|
checkPoint();
|
2004-05-22 00:05:20 +00:00
|
|
|
Thread::killall();
|
2005-06-02 09:29:09 +00:00
|
|
|
checkPoint();
|
2004-05-22 00:05:20 +00:00
|
|
|
m_dispatcher.dequeue();
|
|
|
|
::signal(SIGINT,SIG_DFL);
|
|
|
|
::signal(SIGTERM,SIG_DFL);
|
2005-04-02 00:49:38 +00:00
|
|
|
#ifndef _WINDOWS
|
2004-05-22 00:05:20 +00:00
|
|
|
::signal(SIGHUP,SIG_DFL);
|
|
|
|
::signal(SIGQUIT,SIG_DFL);
|
2005-04-02 00:49:38 +00:00
|
|
|
#endif
|
2004-05-22 00:05:20 +00:00
|
|
|
delete this;
|
2005-07-01 22:35:06 +00:00
|
|
|
Debug(DebugAll,"Exiting with %d locked mutexes",Mutex::locks());
|
2005-04-09 22:10:00 +00:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
::WSACleanup();
|
|
|
|
#endif
|
2005-05-20 23:08:16 +00:00
|
|
|
setStatus(SERVICE_STOPPED);
|
2004-05-22 00:05:20 +00:00
|
|
|
return s_haltcode;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
Engine* Engine::self()
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (!s_self)
|
|
|
|
s_self = new Engine;
|
|
|
|
return s_self;
|
|
|
|
}
|
|
|
|
|
2005-07-04 12:53:04 +00:00
|
|
|
const char* Engine::pathSeparator()
|
|
|
|
{
|
|
|
|
return PATH_SEP;
|
|
|
|
}
|
|
|
|
|
2005-04-02 00:49:38 +00:00
|
|
|
String Engine::configFile(const char* name)
|
|
|
|
{
|
2005-05-24 15:09:19 +00:00
|
|
|
return s_cfgpath + PATH_SEP + name + s_cfgsuffix;
|
2005-04-02 00:49:38 +00:00
|
|
|
}
|
|
|
|
|
2005-04-28 22:46:59 +00:00
|
|
|
const Configuration& Engine::config()
|
|
|
|
{
|
|
|
|
return s_cfg;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool Engine::Register(const Plugin* plugin, bool reg)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2004-11-29 21:01:04 +00:00
|
|
|
DDebug(DebugInfo,"Engine::Register(%p,%d)",plugin,reg);
|
2004-05-22 00:05:20 +00:00
|
|
|
ObjList *p = plugins.find(plugin);
|
|
|
|
if (reg) {
|
|
|
|
if (p)
|
|
|
|
return false;
|
|
|
|
p = plugins.append(plugin);
|
|
|
|
p->setDelete(s_dynplugin);
|
|
|
|
}
|
|
|
|
else if (p)
|
|
|
|
p->remove(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool Engine::loadPlugin(const char* file)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
s_dynplugin = false;
|
|
|
|
SLib *lib = SLib::load(file);
|
|
|
|
s_dynplugin = true;
|
|
|
|
if (lib) {
|
|
|
|
m_libs.append(lib);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-07-01 21:12:16 +00:00
|
|
|
bool Engine::loadPluginDir(const String& relPath)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2005-08-19 20:23:51 +00:00
|
|
|
Debugger debug("Engine::loadPluginDir","('%s')",relPath.c_str());
|
2004-05-22 00:05:20 +00:00
|
|
|
#endif
|
2005-04-28 22:46:59 +00:00
|
|
|
bool defload = s_cfg.getBoolValue("general","modload",true);
|
2005-07-01 21:12:16 +00:00
|
|
|
String path = s_modpath;
|
|
|
|
if (relPath) {
|
|
|
|
if (!path.endsWith(PATH_SEP))
|
|
|
|
path += PATH_SEP;
|
|
|
|
path += relPath;
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
2005-07-01 21:12:16 +00:00
|
|
|
if (path.endsWith(PATH_SEP))
|
|
|
|
path = path.substr(0,path.length()-1);
|
2005-04-02 00:49:38 +00:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
WIN32_FIND_DATA entry;
|
2005-07-01 21:12:16 +00:00
|
|
|
HANDLE hf = ::FindFirstFile(path + PATH_SEP "*",&entry);
|
2005-04-02 00:49:38 +00:00
|
|
|
if (hf == INVALID_HANDLE_VALUE) {
|
2005-07-01 21:12:16 +00:00
|
|
|
Debug(DebugWarn,"Engine::loadPlugins() failed directory '%s'",path.safe());
|
|
|
|
return false;
|
2005-04-02 00:49:38 +00:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
XDebug(DebugInfo,"Found dir entry %s",entry.cFileName);
|
|
|
|
int n = ::strlen(entry.cFileName) - s_modsuffix.length();
|
|
|
|
if ((n > 0) && !::strcmp(entry.cFileName+n,s_modsuffix)) {
|
2005-04-28 22:46:59 +00:00
|
|
|
if (s_cfg.getBoolValue("modules",entry.cFileName,defload))
|
2005-07-01 21:12:16 +00:00
|
|
|
loadPlugin(path + PATH_SEP + entry.cFileName);
|
2005-04-02 00:49:38 +00:00
|
|
|
}
|
|
|
|
} while (::FindNextFile(hf,&entry));
|
|
|
|
::FindClose(hf);
|
|
|
|
#else
|
2005-07-01 21:12:16 +00:00
|
|
|
DIR *dir = ::opendir(path);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (!dir) {
|
2005-07-01 21:12:16 +00:00
|
|
|
Debug(DebugWarn,"Engine::loadPlugins() failed directory '%s'",path.safe());
|
|
|
|
return false;
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
struct dirent *entry;
|
|
|
|
while ((entry = ::readdir(dir)) != 0) {
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugInfo,"Found dir entry %s",entry->d_name);
|
2004-05-22 00:05:20 +00:00
|
|
|
int n = ::strlen(entry->d_name) - s_modsuffix.length();
|
|
|
|
if ((n > 0) && !::strcmp(entry->d_name+n,s_modsuffix)) {
|
2005-04-28 22:46:59 +00:00
|
|
|
if (s_cfg.getBoolValue("modules",entry->d_name,defload))
|
2005-07-01 21:12:16 +00:00
|
|
|
loadPlugin(path + PATH_SEP + entry->d_name);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
::closedir(dir);
|
2005-04-02 00:49:38 +00:00
|
|
|
#endif
|
2005-07-01 21:12:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::loadPlugins()
|
|
|
|
{
|
|
|
|
const char *name = s_cfg.getValue("general","modpath");
|
|
|
|
if (name)
|
|
|
|
s_modpath = name;
|
|
|
|
name = s_cfg.getValue("general","extrapath");
|
|
|
|
if (name)
|
|
|
|
s_extramod = name;
|
|
|
|
s_maxworkers = s_cfg.getIntValue("general","maxworkers",s_maxworkers);
|
|
|
|
s_restarts = s_cfg.getIntValue("general","restarts");
|
|
|
|
NamedList *l = s_cfg.getSection("preload");
|
|
|
|
if (l) {
|
|
|
|
unsigned int len = l->length();
|
|
|
|
for (unsigned int i=0; i<len; i++) {
|
|
|
|
NamedString *n = l->getParam(i);
|
|
|
|
if (n && n->toBoolean())
|
|
|
|
loadPlugin(n->name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadPluginDir(String::empty());
|
|
|
|
if (s_extramod)
|
|
|
|
loadPluginDir(s_extramod);
|
2005-04-28 22:46:59 +00:00
|
|
|
l = s_cfg.getSection("postload");
|
2004-05-22 00:05:20 +00:00
|
|
|
if (l) {
|
|
|
|
unsigned int len = l->length();
|
|
|
|
for (unsigned int i=0; i<len; i++) {
|
|
|
|
NamedString *n = l->getParam(i);
|
|
|
|
if (n && n->toBoolean())
|
|
|
|
loadPlugin(n->name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::initPlugins()
|
|
|
|
{
|
2005-06-20 20:51:17 +00:00
|
|
|
Output("Initializing plugins");
|
2004-05-22 00:05:20 +00:00
|
|
|
dispatch("engine.init");
|
2005-04-08 12:45:19 +00:00
|
|
|
ObjList *l = plugins.skipNull();
|
|
|
|
for (; l; l = l->skipNext()) {
|
2004-05-22 00:05:20 +00:00
|
|
|
Plugin *p = static_cast<Plugin *>(l->get());
|
2005-04-08 12:45:19 +00:00
|
|
|
p->initialize();
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
2005-06-20 20:51:17 +00:00
|
|
|
Output("Initialization complete");
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2004-11-01 00:07:00 +00:00
|
|
|
int Engine::usedPlugins()
|
|
|
|
{
|
|
|
|
int used = 0;
|
2005-04-08 12:45:19 +00:00
|
|
|
ObjList *l = plugins.skipNull();
|
|
|
|
for (; l; l = l->skipNext()) {
|
2004-11-01 00:07:00 +00:00
|
|
|
Plugin *p = static_cast<Plugin *>(l->get());
|
2005-04-08 12:45:19 +00:00
|
|
|
if (p->isBusy())
|
2004-11-01 00:07:00 +00:00
|
|
|
used++;
|
|
|
|
}
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
void Engine::halt(unsigned int code)
|
|
|
|
{
|
2005-06-24 23:32:58 +00:00
|
|
|
if (s_haltcode == -1)
|
|
|
|
s_haltcode = code;
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2005-09-29 19:34:23 +00:00
|
|
|
bool Engine::restart(unsigned int code, bool gracefull)
|
2005-07-29 23:32:19 +00:00
|
|
|
{
|
|
|
|
if ((s_super_handle < 0) || (s_haltcode != -1))
|
|
|
|
return false;
|
2005-09-29 19:34:23 +00:00
|
|
|
if (gracefull)
|
|
|
|
s_restarts = 1;
|
|
|
|
else
|
|
|
|
s_haltcode = (code & 0xff) | 0x80;
|
2005-07-29 23:32:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
void Engine::init()
|
|
|
|
{
|
|
|
|
s_init = true;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool Engine::install(MessageHandler* handler)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
return s_self ? s_self->m_dispatcher.install(handler) : false;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool Engine::uninstall(MessageHandler* handler)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
return s_self ? s_self->m_dispatcher.uninstall(handler) : false;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool Engine::enqueue(Message* msg)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
return (msg && s_self) ? s_self->m_dispatcher.enqueue(msg) : false;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool Engine::dispatch(Message* msg)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
return (msg && s_self) ? s_self->m_dispatcher.dispatch(*msg) : false;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool Engine::dispatch(Message& msg)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
return s_self ? s_self->m_dispatcher.dispatch(msg) : false;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool Engine::dispatch(const char* name)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (!(s_self && name && *name))
|
|
|
|
return false;
|
|
|
|
Message msg(name);
|
|
|
|
return s_self->m_dispatcher.dispatch(msg);
|
|
|
|
}
|
|
|
|
|
2005-05-05 17:02:18 +00:00
|
|
|
static void usage(bool client, FILE* f)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
::fprintf(f,
|
2005-01-24 15:02:14 +00:00
|
|
|
"Usage: yate [options] [commands ...]\n"
|
2005-06-13 12:08:56 +00:00
|
|
|
" -h, --help Display help message (this one) and exit\n"
|
|
|
|
" -V, --version Display program version and exit\n"
|
2004-05-22 00:05:20 +00:00
|
|
|
" -v Verbose debugging (you can use more than once)\n"
|
|
|
|
" -q Quieter debugging (you can use more than once)\n"
|
2005-05-05 17:02:18 +00:00
|
|
|
"%s"
|
2004-09-05 23:55:26 +00:00
|
|
|
" -p filename Write PID to file\n"
|
2005-04-02 00:49:38 +00:00
|
|
|
" -l filename Log to file\n"
|
2005-01-10 17:57:37 +00:00
|
|
|
" -n configname Use specified configuration name (%s)\n"
|
2004-05-22 00:05:20 +00:00
|
|
|
" -c pathname Path to conf files directory (" CFG_PATH ")\n"
|
|
|
|
" -m pathname Path to modules directory (" MOD_PATH ")\n"
|
2005-05-24 15:09:19 +00:00
|
|
|
" -w directory Change working directory\n"
|
2004-05-22 00:05:20 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
" -D[options] Special debugging options\n"
|
2004-10-14 16:36:24 +00:00
|
|
|
" a Abort if bugs are encountered\n"
|
2005-08-02 02:20:00 +00:00
|
|
|
" m Attempt to debug mutex deadlocks\n"
|
2004-05-22 00:05:20 +00:00
|
|
|
" c Call dlclose() until it gets an error\n"
|
|
|
|
" i Reinitialize after 1st initialization\n"
|
|
|
|
" x Exit immediately after initialization\n"
|
|
|
|
" w Delay creation of 1st worker thread\n"
|
2005-04-22 20:07:26 +00:00
|
|
|
" t Timestamp debugging messages\n"
|
2004-05-22 00:05:20 +00:00
|
|
|
#endif
|
2005-05-05 17:02:18 +00:00
|
|
|
,client ? "" :
|
2005-05-20 23:08:16 +00:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
" --service Run as Windows service\n"
|
|
|
|
" --install Install the Windows service\n"
|
|
|
|
" --remove Remove the Windows service\n"
|
|
|
|
#else
|
2005-05-05 17:02:18 +00:00
|
|
|
" -d Daemonify, suppress output unless logged\n"
|
|
|
|
" -s Supervised, restart if crashes or locks up\n"
|
2005-05-20 23:08:16 +00:00
|
|
|
#endif
|
2005-01-10 17:57:37 +00:00
|
|
|
,s_cfgfile);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2005-05-05 17:02:18 +00:00
|
|
|
static void badopt(bool client, char chr, const char* opt)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (chr)
|
|
|
|
::fprintf(stderr,"Invalid character '%c' in option '%s'\n",chr,opt);
|
|
|
|
else
|
|
|
|
::fprintf(stderr,"Invalid option '%s'\n",opt);
|
2005-05-05 17:02:18 +00:00
|
|
|
usage(client,stderr);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2005-05-05 17:02:18 +00:00
|
|
|
static void noarg(bool client, const char* opt)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
::fprintf(stderr,"Missing parameter to option '%s'\n",opt);
|
2005-05-05 17:02:18 +00:00
|
|
|
usage(client,stderr);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2005-06-13 12:08:56 +00:00
|
|
|
static void version()
|
|
|
|
{
|
|
|
|
::fprintf(stdout,"Yate " YATE_VERSION "\n");
|
|
|
|
}
|
|
|
|
|
2005-05-20 23:08:16 +00:00
|
|
|
int Engine::main(int argc, const char** argv, const char** env, RunMode mode, bool fail)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-05-20 23:08:16 +00:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
int service = 0;
|
|
|
|
#else
|
2004-05-22 00:05:20 +00:00
|
|
|
bool daemonic = false;
|
2004-10-21 13:50:45 +00:00
|
|
|
bool supervised = false;
|
2005-04-02 00:49:38 +00:00
|
|
|
#endif
|
2005-05-20 23:08:16 +00:00
|
|
|
bool client = (mode == Client);
|
2005-04-22 20:07:26 +00:00
|
|
|
bool tstamp = false;
|
2005-05-24 15:09:19 +00:00
|
|
|
const char* pidfile = 0;
|
|
|
|
const char* workdir = 0;
|
2005-04-02 00:49:38 +00:00
|
|
|
int debug_level = debugLevel();
|
2005-01-10 17:57:37 +00:00
|
|
|
|
|
|
|
s_cfgfile = ::strrchr(argv[0],'/');
|
2005-04-09 22:10:00 +00:00
|
|
|
if (!s_cfgfile)
|
|
|
|
s_cfgfile = ::strrchr(argv[0],'\\');
|
2005-01-10 17:57:37 +00:00
|
|
|
if (s_cfgfile)
|
|
|
|
s_cfgfile++;
|
2005-04-09 22:10:00 +00:00
|
|
|
|
|
|
|
if (!s_cfgfile)
|
|
|
|
s_cfgfile = argv[0][0] ? argv[0] : "yate";
|
2005-01-10 17:57:37 +00:00
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
int i;
|
|
|
|
bool inopt = true;
|
|
|
|
for (i=1;i<argc;i++) {
|
|
|
|
const char *pc = argv[i];
|
|
|
|
if (inopt && (pc[0] == '-') && pc[1]) {
|
|
|
|
while (pc && *++pc) {
|
|
|
|
switch (*pc) {
|
|
|
|
case '-':
|
|
|
|
if (!*++pc) {
|
|
|
|
inopt=false;
|
|
|
|
pc=0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!::strcmp(pc,"help")) {
|
2005-05-05 17:02:18 +00:00
|
|
|
usage(client,stdout);
|
2004-05-22 00:05:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-13 12:08:56 +00:00
|
|
|
else if (!::strcmp(pc,"version")) {
|
|
|
|
version();
|
|
|
|
return 0;
|
|
|
|
}
|
2005-05-20 23:08:16 +00:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
else if (!(client ||::strcmp(pc,"service"))) {
|
|
|
|
service |= YSERV_RUN;
|
|
|
|
pc = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!(client || ::strcmp(pc,"install"))) {
|
|
|
|
service |= YSERV_INS;
|
|
|
|
pc = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!(client || ::strcmp(pc,"remove"))) {
|
|
|
|
service |= YSERV_DEL;
|
|
|
|
pc = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2005-05-05 17:02:18 +00:00
|
|
|
badopt(client,0,argv[i]);
|
2004-05-22 00:05:20 +00:00
|
|
|
return EINVAL;
|
|
|
|
break;
|
|
|
|
case 'h':
|
2005-05-05 17:02:18 +00:00
|
|
|
usage(client,stdout);
|
2004-05-22 00:05:20 +00:00
|
|
|
return 0;
|
|
|
|
case 'v':
|
|
|
|
debug_level++;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
debug_level--;
|
|
|
|
break;
|
2005-04-02 00:49:38 +00:00
|
|
|
#ifndef _WINDOWS
|
2004-05-22 00:05:20 +00:00
|
|
|
case 'd':
|
|
|
|
daemonic = true;
|
|
|
|
break;
|
2004-10-21 13:50:45 +00:00
|
|
|
case 's':
|
|
|
|
supervised = true;
|
|
|
|
break;
|
2005-04-02 00:49:38 +00:00
|
|
|
#endif
|
|
|
|
case 'p':
|
2004-05-22 00:05:20 +00:00
|
|
|
if (i+1 >= argc) {
|
2005-05-05 17:02:18 +00:00
|
|
|
noarg(client,argv[i]);
|
2004-05-22 00:05:20 +00:00
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
pc = 0;
|
2005-04-02 00:49:38 +00:00
|
|
|
pidfile=argv[++i];
|
2004-05-22 00:05:20 +00:00
|
|
|
break;
|
2005-04-02 00:49:38 +00:00
|
|
|
case 'l':
|
2004-09-05 23:55:26 +00:00
|
|
|
if (i+1 >= argc) {
|
2005-05-05 17:02:18 +00:00
|
|
|
noarg(client,argv[i]);
|
2004-09-05 23:55:26 +00:00
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
pc = 0;
|
2005-05-24 15:09:19 +00:00
|
|
|
s_logfile=argv[++i];
|
2004-09-05 23:55:26 +00:00
|
|
|
break;
|
2005-01-09 06:05:02 +00:00
|
|
|
case 'n':
|
|
|
|
if (i+1 >= argc) {
|
2005-05-05 17:02:18 +00:00
|
|
|
noarg(client,argv[i]);
|
2005-01-09 06:05:02 +00:00
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
pc = 0;
|
|
|
|
s_cfgfile=argv[++i];
|
|
|
|
break;
|
2004-05-22 00:05:20 +00:00
|
|
|
case 'c':
|
|
|
|
if (i+1 >= argc) {
|
2005-05-05 17:02:18 +00:00
|
|
|
noarg(client,argv[i]);
|
2004-05-22 00:05:20 +00:00
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
pc = 0;
|
|
|
|
s_cfgpath=argv[++i];
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
if (i+1 >= argc) {
|
2005-05-05 17:02:18 +00:00
|
|
|
noarg(client,argv[i]);
|
2004-05-22 00:05:20 +00:00
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
pc = 0;
|
|
|
|
s_modpath=argv[++i];
|
|
|
|
break;
|
2005-05-24 15:09:19 +00:00
|
|
|
case 'w':
|
|
|
|
if (i+1 >= argc) {
|
|
|
|
noarg(client,argv[i]);
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
pc = 0;
|
|
|
|
workdir = argv[++i];
|
|
|
|
break;
|
2004-05-22 00:05:20 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
case 'D':
|
|
|
|
while (*++pc) {
|
|
|
|
switch (*pc) {
|
2004-10-14 16:36:24 +00:00
|
|
|
case 'a':
|
2004-10-24 00:51:51 +00:00
|
|
|
s_sigabrt = true;
|
2004-10-14 16:36:24 +00:00
|
|
|
break;
|
2005-08-02 02:20:00 +00:00
|
|
|
case 'm':
|
|
|
|
Mutex::wait(10000000);
|
|
|
|
break;
|
2004-05-22 00:05:20 +00:00
|
|
|
case 'c':
|
|
|
|
s_keepclosing = true;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
s_init = true;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
s_haltcode++;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
s_makeworker = false;
|
|
|
|
break;
|
2005-04-22 20:07:26 +00:00
|
|
|
case 't':
|
|
|
|
tstamp = true;
|
|
|
|
break;
|
2004-05-22 00:05:20 +00:00
|
|
|
default:
|
2005-05-05 17:02:18 +00:00
|
|
|
badopt(client,*pc,argv[i]);
|
2004-05-22 00:05:20 +00:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pc = 0;
|
|
|
|
break;
|
|
|
|
#endif
|
2005-06-13 12:08:56 +00:00
|
|
|
case 'V':
|
|
|
|
version();
|
|
|
|
return 0;
|
2004-05-22 00:05:20 +00:00
|
|
|
default:
|
2005-05-05 17:02:18 +00:00
|
|
|
badopt(client,*pc,argv[i]);
|
2004-05-22 00:05:20 +00:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2005-01-24 15:02:14 +00:00
|
|
|
if (!s_cmds)
|
|
|
|
s_cmds = new ObjList;
|
|
|
|
s_cmds->append(new String(argv[i]));
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
2005-01-10 17:57:37 +00:00
|
|
|
|
2005-05-05 17:02:18 +00:00
|
|
|
if (fail)
|
|
|
|
return EINVAL;
|
|
|
|
|
2005-05-24 15:09:19 +00:00
|
|
|
if (workdir)
|
|
|
|
::chdir(workdir);
|
|
|
|
|
2005-05-20 23:08:16 +00:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
if ((mode == Server) && !service)
|
|
|
|
service = YSERV_RUN;
|
|
|
|
|
|
|
|
if (service & YSERV_DEL) {
|
|
|
|
if (service & (YSERV_RUN|YSERV_INS)) {
|
|
|
|
::fprintf(stderr,"Option --remove prohibits --install and --service\n");
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
SC_HANDLE sc = OpenSCManager(0,0,SC_MANAGER_ALL_ACCESS);
|
|
|
|
if (!sc) {
|
2005-06-02 09:29:09 +00:00
|
|
|
::fprintf(stderr,"Could not open Service Manager, code %u\n",::GetLastError());
|
2005-05-20 23:08:16 +00:00
|
|
|
return EPERM;
|
|
|
|
}
|
|
|
|
SC_HANDLE sv = OpenService(sc,"yate",DELETE|SERVICE_STOP);
|
|
|
|
if (sv) {
|
|
|
|
ControlService(sv,SERVICE_CONTROL_STOP,0);
|
|
|
|
if (!DeleteService(sv)) {
|
2005-06-02 09:29:09 +00:00
|
|
|
DWORD err = ::GetLastError();
|
2005-05-20 23:08:16 +00:00
|
|
|
if (err != ERROR_SERVICE_MARKED_FOR_DELETE)
|
|
|
|
::fprintf(stderr,"Could not delete Service, code %u\n",err);
|
|
|
|
}
|
|
|
|
CloseServiceHandle(sv);
|
|
|
|
}
|
|
|
|
else {
|
2005-06-02 09:29:09 +00:00
|
|
|
DWORD err = ::GetLastError();
|
2005-05-20 23:08:16 +00:00
|
|
|
if (err != ERROR_SERVICE_DOES_NOT_EXIST)
|
|
|
|
::fprintf(stderr,"Could not open Service, code %u\n",err);
|
|
|
|
}
|
|
|
|
CloseServiceHandle(sc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (service & YSERV_INS) {
|
|
|
|
char buf[1024];
|
|
|
|
if (!GetModuleFileName(0,buf,sizeof(buf))) {
|
2005-06-02 09:29:09 +00:00
|
|
|
::fprintf(stderr,"Could not find my own executable file, code %u\n",::GetLastError());
|
2005-05-20 23:08:16 +00:00
|
|
|
return EINVAL;
|
|
|
|
}
|
2005-05-24 15:09:19 +00:00
|
|
|
String s(buf);
|
2005-05-20 23:08:16 +00:00
|
|
|
if (mode != Server)
|
2005-05-24 15:09:19 +00:00
|
|
|
s << " --service";
|
|
|
|
if (workdir)
|
|
|
|
s << " -w \"" << workdir << "\"";
|
|
|
|
|
2005-05-20 23:08:16 +00:00
|
|
|
SC_HANDLE sc = OpenSCManager(0,0,SC_MANAGER_ALL_ACCESS);
|
|
|
|
if (!sc) {
|
2005-06-02 09:29:09 +00:00
|
|
|
::fprintf(stderr,"Could not open Service Manager, code %u\n",::GetLastError());
|
2005-05-20 23:08:16 +00:00
|
|
|
return EPERM;
|
|
|
|
}
|
|
|
|
SC_HANDLE sv = CreateService(sc,"yate","Yet Another Telephony Engine",GENERIC_EXECUTE,
|
|
|
|
SERVICE_WIN32_OWN_PROCESS,SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL,
|
2005-05-24 15:09:19 +00:00
|
|
|
s.c_str(),0,0,0,0,0);
|
2005-05-20 23:08:16 +00:00
|
|
|
if (sv)
|
|
|
|
CloseServiceHandle(sv);
|
|
|
|
else
|
2005-06-02 09:29:09 +00:00
|
|
|
::fprintf(stderr,"Could not create Service, code %u\n",::GetLastError());
|
2005-05-20 23:08:16 +00:00
|
|
|
CloseServiceHandle(sc);
|
|
|
|
if (!(service & YSERV_RUN))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
2005-05-05 17:02:18 +00:00
|
|
|
if (client && (daemonic || supervised)) {
|
|
|
|
::fprintf(stderr,"Options -d and -s not supported in client mode\n");
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
if (daemonic) {
|
|
|
|
Debugger::enableOutput(false);
|
2005-01-19 08:52:02 +00:00
|
|
|
// Make sure X client modules fail initialization in daemon mode
|
|
|
|
::unsetenv("DISPLAY");
|
2004-05-22 00:05:20 +00:00
|
|
|
if (::daemon(1,0) == -1) {
|
|
|
|
int err = errno;
|
|
|
|
::fprintf(stderr,"Daemonification failed: %s (%d)\n",::strerror(err),err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2005-04-02 00:49:38 +00:00
|
|
|
#endif
|
2005-01-10 17:57:37 +00:00
|
|
|
|
2004-09-05 23:55:26 +00:00
|
|
|
if (pidfile) {
|
|
|
|
int fd = ::open(pidfile,O_WRONLY|O_CREAT,0644);
|
|
|
|
if (fd >= 0) {
|
|
|
|
char pid[32];
|
|
|
|
::snprintf(pid,sizeof(pid),"%u\n",::getpid());
|
|
|
|
::write(fd,pid,::strlen(pid));
|
|
|
|
::close(fd);
|
|
|
|
}
|
|
|
|
}
|
2005-01-10 17:57:37 +00:00
|
|
|
|
2005-05-24 15:09:19 +00:00
|
|
|
if (s_logfile) {
|
|
|
|
int fd = ::open(s_logfile,O_WRONLY|O_CREAT|O_APPEND,0640);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (fd >= 0) {
|
|
|
|
// Redirect stdout and stderr to the new file
|
|
|
|
::fflush(stdout);
|
|
|
|
::dup2(fd,1);
|
|
|
|
::fflush(stderr);
|
|
|
|
::dup2(fd,2);
|
|
|
|
::close(fd);
|
|
|
|
Debugger::enableOutput(true);
|
|
|
|
}
|
|
|
|
}
|
2005-01-10 17:57:37 +00:00
|
|
|
|
2005-05-24 15:09:19 +00:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
if (!service)
|
|
|
|
#endif
|
|
|
|
logFileOpen();
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
debugLevel(debug_level);
|
2004-11-01 00:07:00 +00:00
|
|
|
abortOnBug(s_sigabrt);
|
2004-10-21 13:50:45 +00:00
|
|
|
|
2005-04-02 00:49:38 +00:00
|
|
|
int retcode = -1;
|
|
|
|
#ifndef _WINDOWS
|
|
|
|
if (supervised)
|
2005-04-02 13:28:14 +00:00
|
|
|
retcode = supervise();
|
2004-10-21 13:50:45 +00:00
|
|
|
if (retcode >= 0)
|
|
|
|
return retcode;
|
2005-04-02 00:49:38 +00:00
|
|
|
#endif
|
2004-10-21 13:50:45 +00:00
|
|
|
|
2005-04-22 20:07:26 +00:00
|
|
|
if (tstamp)
|
|
|
|
setDebugTimestamp();
|
2005-05-20 23:08:16 +00:00
|
|
|
|
|
|
|
#ifdef _WINDOWS
|
|
|
|
if (service)
|
2005-06-02 09:29:09 +00:00
|
|
|
retcode = ::StartServiceCtrlDispatcher(dispatchTable) ? 0 : ::GetLastError();
|
2005-05-20 23:08:16 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
retcode = engineRun();
|
|
|
|
|
2004-10-21 13:50:45 +00:00
|
|
|
return retcode;
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
2005-03-28 00:58:26 +00:00
|
|
|
|
2005-05-05 17:02:18 +00:00
|
|
|
void Engine::help(bool client, bool errout)
|
|
|
|
{
|
|
|
|
usage(client, errout ? stderr : stdout);
|
|
|
|
}
|
|
|
|
|
2005-03-28 00:58:26 +00:00
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|