/*******************************************************************************
* Main.cc Tms process
* T.Barnaby, BEAM Ltd, 2007-02-07
*******************************************************************************
*/
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/mman.h>
#include <main.h>
#include <Control.h>
#include <Debug.h>
#define COREDUMP 0
Config config;
int realTime;
void usage(){
fprintf(stderr, "Usage: tmsPuServer [-d n]\n");
fprintf(stderr, " -f : Run in the foreground\n");
fprintf(stderr, " -d n : Debug level n\n");
fprintf(stderr, " -n n : Set the number of this PuServer\n");
}
int main(int argc, char** argv){
BError err;
Control* control;
int a;
BString s;
int db = 0;
int dbFixed = 0;
int foreground = 0;
int number = 0;
sigset_t sigm;
struct rlimit rlim;
// Allow process to lock 500M of available memory
rlim.rlim_max = 500*1024*1024;
rlim.rlim_cur = rlim.rlim_max;
if(setrlimit(RLIMIT_MEMLOCK, &rlim))
perror("Unable to enable memory locking");
#if COREDUMP
rlim.rlim_max = 1024*1024*1024;
rlim.rlim_cur = rlim.rlim_max;
if(setrlimit(RLIMIT_CORE, &rlim))
perror("Unable to enable core dump");
printf("Set to core dump\n");
#endif
// Relinqish root ownership
if(geteuid() == 0){
realTime = 1;
seteuid(getuid());
setegid(getgid());
}
// Lock in all of the pages of this application
if(mlockall(MCL_CURRENT | MCL_FUTURE) < 0)
fprintf(stderr, "Warning: unable to lock in memory pages\n");
// Make sure master thread ignores termination signals sent to the other threads
// Also ignore file size signal, the write function will return an error instead.
sigemptyset(&sigm);
sigaddset(&sigm, SIGUSR1);
sigaddset(&sigm, SIGXFSZ);
sigaddset(&sigm, SIGPIPE);
sigaddset(&sigm, SIGALRM);
sigprocmask(SIG_BLOCK, &sigm, 0);
openlog("TmsPuServer", LOG_CONS, LOG_DAEMON);
nprintf("TmsPuServer version: %s started\n", VERSION);
for(a = 1; a < argc; a++){
if(argv[a][0] == '-'){
switch(argv[a][1]){
case 'f':
foreground = 1;
break;
case 'd':
if(argc <= (a + 1)){
usage();
return 1;
}
setDebug(strtol(argv[++a], 0, 0));
dbFixed = 1;
break;
case 'n':
if(argc <= (a + 1)){
usage();
return 1;
}
number = strtol(argv[++a], 0, 0);
break;
default: usage(); return 1;
}
}
}
// Open and read configuration
if(config.open("tmsPuServerDebug.conf"))
if(config.open("tmsPuServer.conf"))
config.open("/etc/tmsPuServer.conf");
config.read();
if(!dbFixed){
s = config.findValue("Debug:");
if(s != ""){
db = strtol(s, 0, 0);
setDebug(db);
}
}
// Create daemon process
if(!foreground)
daemon(0, 0);
// Create main control class
control = new Control(dbFixed);
if(err = control->init(number)){
eprintf("Error: %s\n", err.getString().retStr());
printf("Error: %s\n", err.getString().retStr());
return 1;
}
control->run();
return 0;
}