47 using std::ostringstream ;
118 ServerApp::set_group_id()
120 #if !defined(OS2) && !defined(TPF) 126 BESDEBUG(
"server",
"ServerApp: Setting group id ... " << endl ) ;
128 string key =
"BES.Group" ;
136 BESDEBUG(
"server",
"FAILED" << endl ) ;
137 string err = string(
"FAILED: ") + e.
get_message() ;
138 cerr << err << endl ;
142 if( !found || group_str.empty() )
144 BESDEBUG(
"server",
"FAILED" << endl ) ;
145 string err =
"FAILED: Group not specified in BES configuration file" ;
146 cerr << err << endl ;
150 BESDEBUG(
"server",
"to " << group_str <<
" ... " << endl ) ;
153 if( group_str[0] ==
'#' )
156 const char *group_c = group_str.c_str() ;
158 new_gid = atoi( group_c ) ;
164 ent = getgrnam( group_str.c_str() ) ;
167 BESDEBUG(
"server",
"FAILED" << endl ) ;
168 string err = (string)
"FAILED: Group " + group_str
169 +
" does not exist" ;
170 cerr << err << endl ;
174 new_gid = ent->gr_gid ;
179 BESDEBUG(
"server",
"FAILED" << endl ) ;
181 err <<
"FAILED: Group id " << new_gid
182 <<
" not a valid group id for BES" ;
183 cerr << err.str() << endl ;
188 BESDEBUG(
"server",
"to id " << new_gid <<
" ... " << endl ) ;
189 if( setgid( new_gid ) == -1 )
191 BESDEBUG(
"server",
"FAILED" << endl ) ;
193 err <<
"FAILED: unable to set the group id to " << new_gid ;
194 cerr << err.str() << endl ;
199 BESDEBUG(
"server",
"OK" << endl ) ;
201 BESDEBUG(
"server",
"ServerApp: Groups not supported in this OS" << endl ) ;
206 ServerApp::set_user_id()
208 BESDEBUG(
"server",
"ServerApp: Setting user id ... " << endl ) ;
215 string key =
"BES.User" ;
223 BESDEBUG(
"server",
"FAILED" << endl ) ;
224 string err = (string)
"FAILED: " + e.
get_message() ;
225 cerr << err << endl ;
229 if( !found || user_str.empty() )
231 BESDEBUG(
"server",
"FAILED" << endl ) ;
232 string err = (string)
"FAILED: User not specified in BES config file" ;
233 cerr << err << endl ;
237 BESDEBUG(
"server",
"to " << user_str <<
" ... " << endl ) ;
240 if( user_str[0] ==
'#' )
242 const char *user_str_c = user_str.c_str() ;
244 new_id = atoi( user_str_c ) ;
249 ent = getpwnam( user_str.c_str() ) ;
252 BESDEBUG(
"server",
"FAILED" << endl ) ;
253 string err = (string)
"FAILED: Bad user name specified: " 255 cerr << err << endl ;
259 new_id = ent->pw_uid ;
265 BESDEBUG(
"server",
"FAILED" << endl ) ;
266 string err = (string)
"FAILED: BES cannot run as root" ;
267 cerr << err << endl ;
272 BESDEBUG(
"server",
"to " << new_id <<
" ... " << endl ) ;
273 if( setuid( new_id ) == -1 )
275 BESDEBUG(
"server",
"FAILED" << endl ) ;
277 err <<
"FAILED: Unable to set user id to " << new_id ;
278 cerr << err.str() << endl ;
288 bool needhelp = false ;
294 while( ( c = getopt( argc, argv,
"hvsd:c:p:u:i:r:" ) ) != EOF )
307 _portVal = atoi( optarg ) ;
311 _unixSocket = optarg ;
345 if( dashc.empty() && !dashi.empty() )
347 if( dashi[dashi.length()-1] !=
'/' )
351 string conf_file = dashi +
"etc/bes/bes.conf" ;
358 uid_t curr_euid = geteuid() ;
359 #ifndef BES_DEVELOPER 363 string err =
"FAILED: Must be root to run BES" ;
364 cerr << err << endl ;
369 cout <<
"Developer Mode: not testing if BES is run by root" << endl ;
384 cout <<
"Developer Mode: Running as root - setting group and user ids" 392 cout <<
"Developer Mode: Not setting group or user ids" << endl ;
400 string port_key =
"BES.ServerPort" ;
410 BESDEBUG(
"server",
"FAILED" << endl ) ;
411 string err = (string)
"FAILED: " + e.
get_message() ;
412 cerr << err << endl ;
418 _portVal = atoi( sPort.c_str() ) ;
427 string socket_key =
"BES.ServerUnixSocket" ;
428 if( _unixSocket ==
"" )
436 BESDEBUG(
"server",
"FAILED" << endl ) ;
437 string err = (string)
"FAILED: " + e.
get_message() ;
438 cerr << err << endl ;
444 if( !_gotPort && _unixSocket ==
"" )
446 string msg =
"Must specify a tcp port or a unix socket or both\n" ;
447 msg +=
"Please specify on the command line with -p <port>" ;
448 msg +=
" and/or -u <unix_socket>\n" ;
449 msg +=
"Or specify in the bes configuration file with " 450 + port_key +
" and/or " + socket_key +
"\n" ;
451 cout << endl << msg ;
457 if( _secure ==
false )
459 string key =
"BES.ServerSecure" ;
467 BESDEBUG(
"server",
"FAILED" << endl ) ;
468 string err = (string)
"FAILED: " + e.
get_message() ;
469 cerr << err << endl ;
473 if( isSecure ==
"Yes" || isSecure ==
"YES" || isSecure ==
"yes" )
479 BESDEBUG(
"server",
"ServerApp: Registering signal SIGTERM ... " << endl ) ;
482 BESDEBUG(
"server",
"FAILED" << endl ) ;
483 string err =
"FAILED: cannot register SIGTERM signal handler" ;
484 cerr << err << endl ;
488 BESDEBUG(
"server",
"OK" << endl ) ;
490 BESDEBUG(
"server",
"ServerApp: Registering signal SIGINT ... " << endl ) ;
493 BESDEBUG(
"server",
"FAILED" << endl ) ;
494 string err =
"FAILED: cannot register SIGINT signal handler" ;
495 cerr << err << endl ;
499 BESDEBUG(
"server",
"OK" << endl ) ;
501 BESDEBUG(
"server",
"ServerApp: Registering signal SIGUSR1 ... " << endl ) ;
504 BESDEBUG(
"server",
"FAILED" << endl ) ;
505 string err =
"FAILED: cannot register SIGUSR1 signal handler" ;
506 cerr << err << endl ;
510 BESDEBUG(
"server",
"OK" << endl ) ;
512 BESDEBUG(
"server",
"ServerApp: initializing default module ... " 515 BESDEBUG(
"server",
"OK" << endl ) ;
517 BESDEBUG(
"server",
"ServerApp: initializing default commands ... " 520 BESDEBUG(
"server",
"OK" << endl ) ;
525 BESDEBUG(
"server",
"ServerApp: initialized settings:" << *
this ) ;
540 BESDEBUG(
"server",
"ServerApp: initializing memory pool ... " 543 BESDEBUG(
"server",
"OK" << endl ) ;
551 BESDEBUG(
"server",
"ServerApp: listening on port (" 552 << _portVal <<
")" << endl ) ;
555 if( !_unixSocket.empty() )
559 BESDEBUG(
"server",
"ServerApp: listening on unix socket (" 560 << _unixSocket <<
")" << endl ) ;
565 _ps =
new PPTServer( &handler, &listener, _secure ) ;
576 cerr <<
"caught unknown exception" << endl ;
577 (*
BESLog::TheLog()) <<
"caught unknown exception initializing sockets" 588 pid_t apppid = getpid() ;
589 if( apppid == _mypid )
607 BESDEBUG(
"server",
"ServerApp: terminating default module ... " 610 BESDEBUG(
"server",
"OK" << endl ) ;
612 BESDEBUG(
"server",
"ServerApp: terminating default commands ... " 615 BESDEBUG(
"server",
"OK" << endl ) ;
632 << (
void *)
this <<
")" << endl ;
682 return app.
main( argc, argv ) ;
686 cerr <<
"Caught unhandled exception: " << endl ;
692 cerr <<
"Caught unhandled, unknown exception" << endl ;
static void SetUp(const string &values)
Sets up debugging for the bes.
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
static void signalRestart(int sig)
static BESMemoryGlobalArea * initialize_memory_pool()
static void signalInterrupt(int sig)
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)
static void signalTerminate(int sig)
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 void dump(ostream &strm) const
dumps information about this object
virtual void dump(ostream &strm) const
dumps information about this object
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.
virtual void dump(ostream &strm) const
dumps information about this object
string appName(void) const
Returns the name of the application.
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)
#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 int run()
the applications functionality is implemented in the run method
virtual void dump(ostream &strm) const
dumps information about this object
virtual int terminate(int sig=0)=0
Clean up after the application.