daemonize on ./gb 0 etc.

This commit is contained in:
mwells 2014-04-06 15:57:38 -07:00
parent c20c30c53f
commit 5ee79a4c2f
7 changed files with 85 additions and 28 deletions

View File

@ -247,10 +247,10 @@ bool Collectiondb::addExistingColl ( char *coll, collnum_t collnum ) {
// load if not new
if ( ! cr->load ( coll , i ) ) {
mdelete ( cr, sizeof(CollectionRec), "CollectionRec" );
log("admin: Failed to load coll.%s.%li/coll.conf",coll,i);
delete ( cr );
m_recs[i] = NULL;
return log("admin: Failed to load conf for collection "
"\"%s\".",coll);
if ( m_recs ) m_recs[i] = NULL;
return false;
}
if ( ! registerCollRec ( cr , false ) ) return false;

View File

@ -429,6 +429,7 @@ pid_t s_pid = (pid_t) -1;
void Mem::setPid() {
s_pid = getpid();
log("mem: pid is %li",(long)s_pid);
if(s_pid == -1 ) { log("monitor: bad s_pid"); char *xx=NULL;*xx=0; }
}
@ -452,7 +453,7 @@ bool Mem::init ( long long maxMem ) {
//lim.rlim_max = maxMem;
//setrlimit ( RLIMIT_AS , &lim ); // ulimit -v
// note
log(LOG_INIT,"mem: Max memory usage set to %lli bytes.", maxMem);
//log(LOG_INIT,"mem: Max memory usage set to %lli bytes.", maxMem);
// warning msg
if ( g_conf.m_detectMemLeaks )
log(LOG_INIT,"mem: Memory leak checking is enabled.");

View File

@ -2976,7 +2976,7 @@ bool Parms::setFromFile ( void *THIS ,
// make sure we're init'd
init();
// let em know
if ( THIS == &g_conf ) log (LOG_INIT,"conf: Reading %s." , filename );
//if ( THIS == &g_conf) log (LOG_INIT,"conf: Reading %s." , filename );
// . let the log know what we are doing
// . filename is NULL if a call from CollectionRec::setToDefaults()
Xml xml;
@ -3155,10 +3155,12 @@ bool Parms::setFromFile ( void *THIS ,
// log("conf: %s does not have <%s> tag. "
// "Ommitting.",filename,m->m_xml);
//else
/*
if ( ! m->m_def ) //m->m_def[0] )
log("conf: %s does not have <%s> tag. Using "
"default value of \"%s\".", filename,
m->m_xml,m->m_def);
*/
continue;
}
// . next node is the value of this tag

View File

@ -324,8 +324,11 @@ retry19:
g_errno = errno;
//if ( g_errno == EINVAL ) { port++; goto again; }
close ( m_sock );
return log("tcp: Failed to bind socket: %s.",
mstrerror(g_errno));
fprintf(stderr,"Failed to bind socket on port %li: %s."
"\n"
"Are you already running gb?\n",
(long)port,mstrerror(g_errno));
return false;
}
close ( m_sock );
return true;

View File

@ -173,7 +173,16 @@ void Threads::reset ( ) {
m_threadQueues[i].reset();
}
Threads::Threads ( ) {
m_numQueues = 0;
m_initialized = false;
}
bool Threads::init ( ) {
if ( m_initialized ) return true;
m_initialized = true;
m_needsCleanup = false;
//m_needBottom = false;
@ -183,18 +192,18 @@ bool Threads::init ( ) {
// set s_pid to the main process id
#ifdef PTHREADS
s_pid = pthread_self();
log(LOG_INFO,
"threads: main process THREAD id = %lu",(long unsigned)s_pid);
//log(LOG_INFO,
// "threads: main process THREAD id = %lu",(long unsigned)s_pid);
pthread_t tid = pthread_self();
sched_param param;
int policy;
// scheduling parameters of target thread
pthread_getschedparam ( tid, &policy, &param);
log(LOG_INFO,
"threads: min/max thread priority settings = %li/%li (policy=%li)",
(long)sched_get_priority_min(policy),
(long)sched_get_priority_max(policy),
(long)policy);
//log(LOG_INFO,
// "threads: min/max thread priority settings = %li/%li (policy=%li)",
// (long)sched_get_priority_min(policy),
// (long)sched_get_priority_max(policy),
// (long)policy);
#else
s_pid = getpid();
#endif
@ -422,6 +431,10 @@ bool Threads::call ( char type ,
// don't spawn any if disabled
if ( m_disabled ) return false;
if ( ! g_conf.m_useThreads ) return false;
if ( ! m_initialized && ! init() )
return log("db: Threads init failed." );
// . sanity check
// . a thread can NOT call this
//if ( getpid() != s_pid ) {
@ -447,7 +460,7 @@ bool Threads::call ( char type ,
if ( i == m_numQueues ) {
g_errno = EBADENGINEER;
return log(LOG_LOGIC,"thread: addtoqueue: Unregistered "
"thread type");
"thread type %li",(long)type);
}
// debug msg
//log("thread: call: adding entry for thread");

View File

@ -165,7 +165,7 @@ class Threads {
public:
Threads() { m_numQueues = 0; };
Threads();
// returns false and sets errno on error, true otherwise
bool init();
@ -257,6 +257,7 @@ class Threads {
bool m_needsCleanup;
//bool m_needBottom;
bool m_initialized;
// private:

View File

@ -361,8 +361,19 @@ void stack_test(){
}
#endif
int main2 ( int argc , char *argv[] ) ;
int main ( int argc , char *argv[] ) {
//fprintf(stderr,"Starting gb.\n");
int ret = main2 ( argc , argv );
if ( ret ) fprintf(stderr,"Failed to start gb. Exiting.\n");
}
int main2 ( int argc , char *argv[] ) {
// appears that linux 2.4.17 kernel would crash with this?
// let's try again on gk127 to make sure
// YES! gk0 cluster has run for months with this just fine!!
@ -2230,10 +2241,6 @@ int main ( int argc , char *argv[] ) {
}
*/
// start up log file
if ( ! g_log.init( g_hostdb.m_logFilename ) ) {
fprintf (stderr,"db: Log file init failed.\n" ); return 1; }
// gb [-h hostsConf] <hid>
// mainStart:
@ -2274,7 +2281,8 @@ int main ( int argc , char *argv[] ) {
g_conf.m_sendEmailAlerts = false;
// log how much mem we can use
log(LOG_INIT,"conf: Max mem allowed to use is %lli\n",g_conf.m_maxMem);
//log(LOG_INIT,"conf: Max mem allowed to use is %lli\n",
//g_conf.m_maxMem);
// load the language specific pages
g_languagePages.reloadPages();
@ -2295,8 +2303,11 @@ int main ( int argc , char *argv[] ) {
// set up the threads, might need g_conf
if ( ! g_threads.init() ) {
log("db: Threads init failed." ); return 1; }
// avoid logging threads msgs to stderr if not actually starting up
// a gb daemon...
//if(cmd && cmd[0] && ! is_digit(cmd[0]) && ! g_threads.init() ) {
//if ( ! g_threads.init() ) {
// log("db: Threads init failed." ); return 1; }
// gb gendict
if ( strcmp ( cmd , "gendict" ) == 0 ) {
@ -2667,6 +2678,12 @@ int main ( int argc , char *argv[] ) {
// fixTfndb ( coll ); // coll
//}
// make sure port is available, no use loading everything up then
// failing because another process is already running using this port
//if ( ! g_udpServer.testBind ( g_hostdb.getMyPort() ) )
if ( ! g_httpServer.m_tcp.testBind(g_hostdb.getMyHost()->m_httpPort))
return 1;
//if ( strcmp ( cmd , "gendbs" ) == 0 ) goto jump;
//if ( strcmp ( cmd , "gentfndb" ) == 0 ) goto jump;
if ( strcmp ( cmd , "gencatdb" ) == 0 ) goto jump;
@ -2674,6 +2691,8 @@ int main ( int argc , char *argv[] ) {
// if ( cmd && ! is_digit(cmd[0]) ) goto printHelp;
fprintf(stderr,"Logging to file %s.\n",g_hostdb.m_logFilename );
/*
// tmp stuff to generate new query log
if ( ! ucInit(g_hostdb.m_dir, true)) return 1;
@ -2686,14 +2705,32 @@ int main ( int argc , char *argv[] ) {
return 0;
*/
// make sure port is available, no use loading everything up then
// failing because another process is already running using this port
//if ( ! g_udpServer.testBind ( g_hostdb.getMyPort() ) )
if ( ! g_httpServer.m_tcp.testBind(g_hostdb.getMyHost()->m_httpPort))
return 1;
// start up log file
if ( ! g_log.init( g_hostdb.m_logFilename ) ) {
fprintf (stderr,"db: Log file init failed.\n" ); return 1; }
g_errno = 0;
//
// run as daemon now
//
//fprintf(stderr,"running as daemon\n");
pid_t pid, sid;
pid = fork();
if ( pid < 0 ) exit(EXIT_FAILURE);
if ( pid > 0 ) exit(EXIT_SUCCESS);
// change file mode mask
umask(0);
sid = setsid();
if ( sid < 0 ) exit(EXIT_FAILURE);
//fprintf(stderr,"done\n");
// set our new pid
g_mem.setPid();
// initialize threads down here now so it logs to the logfile and
// not stderr
//if ( ( ! cmd || !cmd[0]) && ! g_threads.init() ) {
// log("db: Threads init failed." ); return 1; }
if (!ucInit(g_hostdb.m_dir, true)) {
log("Unicode initialization failed!");