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;
exception thrown if an internal error is found and is fatal to the BES
static void SetUp(const string &values)
Sets up debugging for the bes.
#define BESLISTENER_RUNNING
static int initialize(int argc, char **argv)
#define SERVER_EXIT_NORMAL_SHUTDOWN
virtual int main(int argC, char **argV)
main method of the BES application
virtual void dump(ostream &strm) const
dumps information about this object
static BESMemoryGlobalArea * initialize_memory_pool()
virtual void dump(ostream &strm) const
dumps information about this object
#define BESLISTENER_PIPE_FD
virtual int terminate(int sig=0)
clean up after the application
static int terminate(void)
Removes the default set of BES XML commands from the list of possible commands.
static void show_usage(const string &app_name)
string appName(void) const
Returns the name of the application.
virtual string get_message()
get the error message for this exception
static int initialize(int argc, char **argv)
Loads the default set of BES XML commands.
virtual void initConnection()
Using the info passed into the SocketLister, wait for an inbound request (see SocketListener::accept(...
Abstract exception class for the BES with basic string message.
virtual void closeConnection()
static int terminate(void)
static ostream & LMarg(ostream &strm)
virtual int terminate(int sig=0)
clean up after the application
virtual void dump(ostream &strm) const
dumps information about this object
#define SERVER_EXIT_FATAL_CAN_NOT_START
virtual int initialize(int argC, char **argV)
Load and initialize any BES modules.
Base application object for all BES applications.
virtual int initialize(int argC, char **argV)
Load and initialize any BES modules.
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
static void show_version(const string &app_name)
int main(int argc, char **argv)
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
#define SERVER_EXIT_RESTART
virtual void listen(Socket *s)
static BESKeys * TheKeys()
static void Register(const string &flagName)
register the specified debug flag
static BESApp * TheApplication(void)
Returns the BESApp application object for this application.
virtual void dump(ostream &strm) const
dumps information about this object
virtual int run()
the applications functionality is implemented in the run method
virtual int terminate(int sig=0)=0
Clean up after the application.
virtual void dump(ostream &strm) const
dumps information about this object