35 #include <sys/types.h>
46 using std::ostringstream;
69 static int session_id = 0;
72 BESModuleApp(), _portVal(0), _gotPort(false), _unixSocket(
""), _secure(false), _mypid(0), _ts(0), _us(0), _ps(0)
83 static void CatchSigChild(
int sig)
87 BESDEBUG(
"besdaemon",
"beslistener: caught sig chld" << endl);
89 pid_t pid = wait(&stat);
90 BESDEBUG(
"besdaemon",
"beslistener: child pid: " << pid <<
" exited with status: " << stat << endl);
97 static void CatchSigHup(
int sig)
102 BESDEBUG(
"besdaemon",
"beslisterner: " << pid <<
" caught SIGHUP." << endl);
106 BESDEBUG(
"besdaemon",
"beslisterner: " << pid <<
" past terminate (SIGHUP)." << endl);
114 static void CatchSigTerm(
int sig)
119 BESDEBUG(
"besdaemon",
"beslisterner: " << pid <<
" caught SIGTERM" << endl);
123 BESDEBUG(
"besdaemon",
"beslisterner: " << pid <<
" past terminate (SIGTERM)." << endl);
137 static void register_signal_handlers()
139 struct sigaction act;
140 sigemptyset(&act.sa_mask);
141 sigaddset(&act.sa_mask, SIGCHLD);
142 sigaddset(&act.sa_mask, SIGTERM);
143 sigaddset(&act.sa_mask, SIGHUP);
146 BESDEBUG(
"besdaemon" ,
"besdaemon: setting restart for sigchld." << endl);
147 act.sa_flags |= SA_RESTART;
150 BESDEBUG(
"server",
"beslisterner: Registering signal handlers ... " << endl );
152 act.sa_handler = CatchSigChild;
153 if (sigaction(SIGCHLD, &act, 0))
154 throw BESInternalFatalError(
"Could not register a handler to catch beslistener child process status.", __FILE__, __LINE__);
158 act.sa_handler = CatchSigTerm;
159 if (sigaction(SIGTERM, &act, 0) < 0)
160 throw BESInternalFatalError(
"Could not register a handler to catch beslistener terminate signal.", __FILE__, __LINE__);
162 act.sa_handler = CatchSigHup;
163 if (sigaction(SIGHUP, &act, 0) < 0)
164 throw BESInternalFatalError(
"Could not register a handler to catch beslistener hup signal.", __FILE__, __LINE__);
166 BESDEBUG(
"server",
"beslisterner: OK" << endl );
172 bool needhelp =
false;
179 while ((c = getopt(argc, argv,
"hvsd:c:p:u:i:r:")) != EOF)
192 _portVal = atoi(optarg);
196 _unixSocket = optarg;
231 if (dashc.empty() && !dashi.empty())
233 if (dashi[dashi.length() - 1] !=
'/')
237 string conf_file = dashi +
"etc/bes/bes.conf";
254 string port_key =
"BES.ServerPort";
264 BESDEBUG(
"server",
"beslisterner: FAILED" << endl );
265 string err = (string)
"FAILED: " + e.
get_message();
272 _portVal = atoi(sPort.c_str());
281 string socket_key =
"BES.ServerUnixSocket";
282 if (_unixSocket ==
"")
290 BESDEBUG(
"server",
"beslisterner: FAILED" << endl );
291 string err = (string)
"FAILED: " + e.
get_message();
298 if (!_gotPort && _unixSocket ==
"")
300 string msg =
"Must specify a tcp port or a unix socket or both\n";
301 msg +=
"Please specify on the command line with -p <port>";
302 msg +=
" and/or -u <unix_socket>\n";
303 msg +=
"Or specify in the bes configuration file with " + port_key +
" and/or " + socket_key +
"\n";
310 if (_secure ==
false)
312 string key =
"BES.ServerSecure";
320 BESDEBUG(
"server",
"beslisterner: FAILED" << endl );
321 string err = (string)
"FAILED: " + e.
get_message();
326 if (isSecure ==
"Yes" || isSecure ==
"YES" || isSecure ==
"yes")
334 register_signal_handlers();
343 BESDEBUG(
"server",
"beslisterner: initializing default module ... "
346 BESDEBUG(
"server",
"beslisterner: done initializing default module"
349 BESDEBUG(
"server",
"beslisterner: initializing default commands ... "
352 BESDEBUG(
"server",
"beslisterner: done initializing default commands"
356 BESDEBUG(
"server",
"beslisterner: initializing loaded modules ... "
359 BESDEBUG(
"server",
"beslisterner: done initializing loaded modules"
362 BESDEBUG(
"server",
"beslisterner: initialized settings:" << *
this );
372 session_id = setsid();
373 BESDEBUG(
"besdaemon",
"beslisterner: The master beslistener session id (group id): " << session_id << endl);
382 BESDEBUG(
"server",
"beslisterner: initializing memory pool ... "
393 BESDEBUG(
"server",
"beslisterner: listening on port (" << _portVal <<
")" << endl );
395 BESDEBUG(
"server",
"beslisterner: about to write status (4)" << endl );
403 BESDEBUG(
"server",
"beslisterner: wrote status (" << res <<
")" << endl );
406 if (!_unixSocket.empty())
410 BESDEBUG(
"server",
"beslisterner: listening on unix socket ("
411 << _unixSocket <<
")" << endl );
416 _ps =
new PPTServer(&handler, &listener, _secure);
432 cerr <<
"caught unknown exception" << endl;
433 (*
BESLog::TheLog()) <<
"caught unknown exception initializing sockets" << endl;
446 pid_t apppid = getpid();
447 if (apppid == _mypid)
468 BESDEBUG(
"server",
"beslisterner: terminating loaded modules ... "
471 BESDEBUG(
"server",
"beslisterner: done terminating loaded modules"
474 BESDEBUG(
"server",
"beslisterner: terminating default commands ... "
477 BESDEBUG(
"server",
"beslisterner: done terminating default commands ... "
480 BESDEBUG(
"server",
"beslisterner: terminating default module ... "
483 BESDEBUG(
"server",
"beslisterner: done terminating default module ... "
497 strm <<
BESIndent::LMarg <<
"ServerApp::dump - (" << (
void *)
this <<
")" << endl;
541 int main(
int argc,
char **argv)
546 return app.
main(argc, argv);
550 cerr <<
"Caught unhandled exception: " << endl;
556 cerr <<
"Caught unhandled, unknown exception" << endl;