DRDE/AusRegCliever/server/cliever-md.cpp

221 lines
6.3 KiB
C++
Raw Normal View History

2014-01-03 05:52:15 +00:00
#define MD_MAIN
#include "cliever-md.h"
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
using namespace std;
bool mdHasEPPTk;
const char *mdOrAC; // Process capability based on boolean above
void setSignals();
void signal_handler(int);
void usage();
void md() {
bool daemonized=false;
int i,lfp,rc; // log file pointer
char str[32];
if (thisConfig->daemonized) {
rc = fork();
if (rc < 0) {
puts("Can't initialize process!");
exit(1);
}
if (getpid() == thisConfig->shellProcess)
exit(0);
setsid(); /* obtain a new process group */
for (i=getdtablesize();i>=0;--i) close(i); /* close all descriptors */
i=open("/dev/null",O_RDWR); dup(i); dup(i); /* handle standard I/O */
umask(027); /* set newly created file permissions */
chdir(RUNNING_DIR); /* change running directory */
lfp=open(MD_LOCK_FILE,O_RDWR|O_CREAT,0640); /* make sure were The MD */
if (lfp<0) {printf("Can't open lockfile(%d)! Ctl-C me."); exit(1);}
if ((rc=lockf(lfp,F_TLOCK,0))<0)
{printf("Can't lock lockfile (%d)! Ctl-C me.",rc); exit(0);}
thisConfig->daemonProcess = getpid();
sprintf(str,"%d\n",getpid());
write(lfp,str,strlen(str)); /* record pid to lockfile */
daemonized = true;
}
setSignals();
try {
theseLogs = new mdLogger();
2014-01-19 20:56:09 +00:00
theseLogs->init(true);
2014-01-03 05:52:15 +00:00
theseLogs->logN(1,MD_NAME " " MD_VERSION " compiled on " __DATE__ " @ " __TIME__ "(%d)",thisConfig->daemonProcess);
if (daemonized) theseLogs->logN(1,"(detached from %d)",thisConfig->shellProcess);
if (mdHasEPPTk) {
theseLogs->logN(0,"AusReg Cliever <- MasterDaemon.");
}
2014-01-16 15:35:39 +00:00
else
theseLogs->logN(0,"Unit testing md without toolkit.");
2014-01-03 05:52:15 +00:00
boost::thread foreground(runMasterDaemon);
boost::thread background(runAPILayer);
2014-01-03 05:52:15 +00:00
if (!foreground.joinable()) {
theseLogs->logN(0,"Fatal Error: couldn't start master daemon!");
exit(1);
}
if (background.joinable()) {
theseLogs->logN(2,"%s %d","Accepting API Requests on Port",thisConfig->clientPort);
background.join(); // normally unreachable
} else
theseLogs->logN(0,"Fatal Error: couldn't start client service layer!");
}
//catch (XmlRpcFault::XmlRpcFault& e)
//{
// theseLogs->logN(1,"xmlrpc_c fault: %s",e.getFaultString().c_str());
// exit(1);
//}
catch (std::exception& e)
{
theseLogs->logN(1,"Exception: %s",e.what());
exit(1);
}
catch (...) {
theseLogs->logN(1,"General Fault!");
exit(1);
}
}
int
main(int const argc,
const char ** const argv) {
2014-01-16 15:35:39 +00:00
char msg[512],*c;
2014-01-03 05:52:15 +00:00
const char *banner = MD_NAME " " MD_VERSION " compiled on " __DATE__ " @ " __TIME__ "(%d)\n";
int mthParm, rc = 0;
thisConfig = new masterDaemonConfig();
thisConfig->shellProcess = getpid();
2014-01-03 08:27:31 +00:00
mdOrAC = (strcspn(argv[0],"./") == strlen(argv[0])) ? argv[0] : strrchr(argv[0],'/') + 1;
2014-01-16 15:35:39 +00:00
mdHasEPPTk = strstr(argv[0],"ausreg-md") ? false : true;
2014-01-17 16:51:06 +00:00
thisConfig->tkScenario = 1;
2014-01-03 05:52:15 +00:00
2014-01-16 15:35:39 +00:00
if (argc < 3 || argc > 7) usage();
2014-01-03 05:52:15 +00:00
thisConfig->servicePort = atoi(argv[1]);
thisConfig->clientPort = atoi(argv[2]);
if (thisConfig->clientPort < 1000 || thisConfig->clientPort > 65535)
{
std::cerr << "The <client-udp-port> value is invalid.\n";
return 1;
}
if (thisConfig->servicePort < 1000 || thisConfig->servicePort > 65535)
{
std::cerr << "The <service-socket-port> value is invalid.\n";
return 1;
}
if (thisConfig->servicePort == thisConfig->clientPort)
{
std::cerr << "The <service-socket-port> and <client-udp-port> cannot be the same.\n";
return 1;
}
for (mthParm=3;mthParm < argc;mthParm++) {
if (*argv[mthParm] == '!') {
thisConfig->daemonized = false;
}
else
if (!strncmp(argv[mthParm],"logs=",4)) {
thisConfig->log_path = std::string(argv[mthParm]+4);
thisConfig->logPath = thisConfig->log_path.c_str();
}
else
2014-01-16 15:35:39 +00:00
if (!strncmp(argv[mthParm],"tk=",2)) {
2014-01-17 16:51:06 +00:00
c = (char *)argv[mthParm]+3;
2014-01-16 15:35:39 +00:00
thisConfig->tkScenario = atoi(c);
}
else
2014-01-03 05:52:15 +00:00
if (!strncmp(argv[mthParm],"cfg=",3)) {
thisConfig->cfg_path = std::string(argv[mthParm]+3);
thisConfig->configPath = thisConfig->cfg_path.c_str();
}
else usage();
}
if (!thisConfig->daemonized) printf(banner,thisConfig->shellProcess);
md();
while(!thisConfig->halt) sleep(5);
}
void setSignals() {
signal(SIGCHLD,SIG_IGN); /* ignore child */
signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
signal(SIGTTOU,SIG_IGN); // both input
signal(SIGTTIN,SIG_IGN); // and output
2014-02-04 19:58:44 +00:00
#ifdef AC_ASSERTIONS
signal(SIGINT, signal_handler); // debug
#endif
2014-01-03 05:52:15 +00:00
signal(SIGSEGV,signal_handler);
signal(SIGUSR1,signal_handler); // commander log messages
signal(SIGUSR2,signal_handler); // reserved
signal(SIGHUP,signal_handler); /* catch hangup signal */
signal(SIGTERM,signal_handler); /* catch kill signal */
}
void signal_handler(int sig)
2014-01-30 16:09:58 +00:00
{ bool waitForGDB=true; long long iSaidWaitBiatch = 100000000000L, i;
2014-01-03 05:52:15 +00:00
switch(sig) {
case SIGSEGV:
theseLogs->logN(0,"SEGV ABEND.");
2014-02-04 19:58:44 +00:00
case SIGINT:
2014-01-30 16:09:58 +00:00
if (waitForGDB) {
2014-01-30 16:20:54 +00:00
for (i = 0;
i < iSaidWaitBiatch;
i++)
{}
2014-01-30 16:09:58 +00:00
}
2014-01-03 05:52:15 +00:00
exit(NOT_OK);
break;
case SIGUSR1:
break;
case SIGUSR2:
break;
case SIGHUP:
theseLogs->logN(0,"hangup signal caught, currently md ignores this.");
break;
case SIGTERM:
theseLogs->logN(0,"terminate signal caught, auc-md will shutdown.");
exit(0);
break;
}
}
void usage() {
2014-01-16 15:35:39 +00:00
std::cerr << "Usage: " << mdOrAC << " <client-udp-port> <service-socket-port> [tk=<n>] [cfg=<path>] [log=<path>] [!] \n\n"
" where tk is the Toolkit Scenario (default ACTK_PROD) \n"
"\nconfig directory defaults to ./etc/toolkit2.conf \n"
"log directory to /tmp \nand ! to not daemonize (for diagnostic purposes)\n\n";
2014-01-03 05:52:15 +00:00
exit(1);
}
2014-01-30 23:27:18 +00:00