libassa  3.5.1
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
ASSA::GenServer Class Referenceabstract

#include <GenServer.h>

Inheritance diagram for ASSA::GenServer:
ASSA::EventHandler ASSA::CmdLineOpts

Public Types

enum  LogFlag { KEEPLOG , RMLOG }
 
- Public Types inherited from ASSA::CmdLineOpts
typedef void(* OPTS_FUNC) (void)
 
typedef void(* OPTS_FUNC_ONE) (const string &)
 
typedef vector< OptionOptionSet
 

Public Member Functions

 GenServer ()
 Constructor. More...
 
virtual ~GenServer ()
 Destructor. More...
 
virtual void init (int *argc, char *argv[], const char *help_info)
 Provide an entry point into the service and perfom initialization of the service. More...
 
virtual int fini (void)
 This is an iterface function corresponding to the object moving back into IDLE state. More...
 
virtual int suspend (void)
 Temporarily suspend the execution of a service. More...
 
virtual int resume (void)
 Resume execution of a service. More...
 
virtual void init_service ()=0
 Interface function provided for derived classes as a place to initialize specifics of derived server. More...
 
virtual void process_events ()=0
 Interface function provided for derived classes as the main entry for data processing. More...
 
virtual void fatal_signal_hook ()
 Hook for derived class to do addition clean-up when terminating signal is delivered by OS. More...
 
int handle_signal (int signum_)
 Handle fatal signals. More...
 
bool service_is_active ()
 Normally called by the main loop to find out whether 'graceful quit' flag had been raised, signaling that some application's component requested to end data processing. More...
 
void stop_service ()
 Inform server that it has to stop data processing, clean up and exit. More...
 
void set_version (const string &release_, int revision_)
 Set Version and Revision number. More...
 
string get_version ()
 Obtain version information. More...
 
void set_author (const string &author_)
 Set author's name. More...
 
void set_flags (LogFlag logf_)
 New debug information is added to the old log file. More...
 
virtual void display_help ()
 List options and invocation syntax to stdout. More...
 
string get_proc_name ()
 Get name of process+instance_number. More...
 
void set_proc_name (string proc_name_)
 Change process name. More...
 
string get_cmdline_name ()
 Get command-line process name. More...
 
string get_default_config_file ()
 Get default configuration file name: $HOME/. More...
 
string get_config_file ()
 Get alternative configuration file name. More...
 
string get_port ()
 Return assumed name of the listening port. More...
 
void set_port (string port_)
 Set listening port name. More...
 
SigHandlersget_sig_manager ()
 Obtain reference to the Signal Manager, class SigHandls. More...
 
Reactorget_reactor ()
 Obtain reference to the Reactor. More...
 
int get_exit_value () const
 Retrieve exit value of the process. More...
 
- Public Member Functions inherited from ASSA::EventHandler
 EventHandler ()
 Constructor. More...
 
virtual ~EventHandler ()
 Virtual destructor. More...
 
virtual int handle_read (int fd)
 Read event callback. More...
 
virtual int handle_write (int fd)
 Write handler callback. More...
 
virtual int handle_except (int fd)
 Exception handler callback. More...
 
virtual int handle_timeout (TimerId tid)
 Timeout handler callback. More...
 
virtual int handle_close (int fd)
 EOF on peer socket handler callback. More...
 
virtual void resetState (void)
 A hook for derived class to reset internal state as needed. More...
 
void set_id (const std::string &id_)
 Set EventHandler ID. More...
 
std::string get_id () const
 Retrieve EventHandler ID. More...
 
- Public Member Functions inherited from ASSA::CmdLineOpts
 CmdLineOpts ()
 Default constructor. More...
 
virtual ~CmdLineOpts ()
 Do-nothing destructor. More...
 
bool add_flag_opt (const char c, const string &s, bool *f)
 Add binary flag option. More...
 
bool add_opt (const char c, const string &s, string *str)
 Add an option with STL string argument. More...
 
bool add_opt (const char c, const string &s, int *i)
 Add an option with integer argument. More...
 
bool add_opt (const char c, const string &s, unsigned int *ui)
 Add an option with unsigned integer argument. More...
 
bool add_opt (const char c, const string &s, long *l)
 Add an option with long argument. More...
 
bool add_opt (const char c, const string &s, unsigned long *ul)
 Add an option with unsigned long argument. More...
 
bool add_opt (const char c, const string &s, double *d)
 Add an option with double argument. More...
 
bool add_opt (const char c, const string &s, float *f)
 Add an option with float argument. More...
 
bool add_opt (const char c_, const string &s_, OPTS_FUNC f_)
 Add an option with static function argument. More...
 
bool add_opt (const char c_, const string &s_, OPTS_FUNC_ONE f_)
 Add an option with static function argument. More...
 
bool rm_opt (const char c_, const string &s_)
 Remove option for the option list. More...
 
bool parse_args (const char *argv[])
 Parse command line arguments based on installed options set. More...
 
int parse_config_file (IniFile &inifile_)
 Parse configuration parameters found in [options] section of the INI file. More...
 
const char * get_opt_error () const
 If previous call to one of member functions returned false, retrieve detailed error message. More...
 
void dump () const
 Write options set to the log file. More...
 

Static Public Member Functions

static bool become_daemon ()
 Become a daemon process. More...
 
- Static Public Member Functions inherited from ASSA::CmdLineOpts
static void str_to_argv (const string &src_, int &argc_, char **&argv_)
 Static function. More...
 
static void free_argv (char **&argv_)
 Free up memory allocated by str_to_argv() function
More...
 

Protected Member Functions

void set_exit_value (int v_)
 Set exit value of the process. This value is returned to the shell. More...
 
- Protected Member Functions inherited from ASSA::CmdLineOpts
bool is_valid (const char sopt_, const string &lopt_)
 Detect if supplied option is valid. More...
 
void set_error_none ()
 Reset error message to an empty string. More...
 
bool assign (Option *node_, const char *op_)
 Perform value assignment to the node. More...
 
Optionfind_option (const char *str_)
 Locate option in the options set. More...
 
Optionfind_option (const char letter_)
 Locate option in the options set. More...
 
virtual void pos_arg (const char *arg_)
 Process positional argument arg_. More...
 

Protected Attributes

string m_proc_name
 process name (considering instance_number) More...
 
string m_cmdline_name
 process name as appeared on command line More...
 
string m_port
 listening port name More...
 
string m_default_config_file
 standard configuration file name More...
 
string m_config_file
 alternative configuration file name More...
 
u_int m_log_size
 Max size of the log file. More...
 
int m_instance
 Process instance. More...
 
string m_log_file
 Full pathname of debug file. More...
 
string m_with_log_server
 If 'yes', send log messages to the log server. More...
 
string m_log_server
 Log server, assa-logd, address (port@host) More...
 
long m_mask
 Debug file mask to filter debug/error messages. More...
 
bool m_graceful_quit
 Flag that indicates wheather server outgh to stop and exit. More...
 
SigHandlers m_sig_dispatcher
 Signal handlers dispatcher. More...
 
SIGPOLLHandler m_sig_poll
 Function that swallows SIGPOLL calls. More...
 
Reactor m_reactor
 GenServer object has its very own personal Reactor object. More...
 
string m_version
 Software version. More...
 
int m_revision
 Software revision (patch) level. More...
 
string m_author
 Author's name. More...
 
const char * m_help_msg
 Help information. More...
 
LogFlag m_log_flag
 Log file initialization flag. If RM_LOG, remove old log file. More...
 
string m_log_stdout
 If 'yes', redirects all logging messages to std::cerr. More...
 
string m_daemon
 Daemon option flag. If 'yes', become a UNIX daemon process. More...
 
string m_ommit_pidfile
 If 'yes', skip PID file locking creation/locking step. More...
 
int m_log_level
 Logging level - an integer number that incrementally increases verbosity of the looing messages. More...
 
PidFileLock m_pidfile_lock
 PID File lock. More...
 
string m_pidfile
 PID File lock path name. More...
 
bool m_help_flag
 Help option flag. More...
 
bool m_version_flag
 Version option flag. More...
 
int m_exit_value
 Exit value of the process. More...
 
- Protected Attributes inherited from ASSA::EventHandler
std::string m_id
 

Private Member Functions

 GenServer (const GenServer &)
 No cloning. More...
 
GenServeroperator= (const GenServer &)
 
void init_internals ()
 Initialize internals. More...
 

Detailed Description

Definition at line 59 of file GenServer.h.

Member Enumeration Documentation

◆ LogFlag

Enumerator
KEEPLOG 

By default, append new log records to the existing log file.

This is operational mode.

RMLOG 

Remove existing log file and start afresh.

Convenient during development phase.

Definition at line 66 of file GenServer.h.

66  {
67  KEEPLOG,
70  RMLOG
72  };
@ KEEPLOG
By default, append new log records to the existing log file.
Definition: GenServer.h:67
@ RMLOG
Remove existing log file and start afresh.
Definition: GenServer.h:70

Constructor & Destructor Documentation

◆ GenServer() [1/2]

GenServer::GenServer ( )

Constructor.

Corresponds to the object entering the IDLE state.

Form a valid log server address

Definition at line 56 of file GenServer.cpp.

57  :
58  m_log_size (10485760), // 10 Mb
59  m_instance (-1),
60  m_with_log_server ("no"),
61  m_log_server ("assalogd@"),
62  m_mask (ALL),
63  m_graceful_quit (false),
64  m_version ("unknown"),
65  m_revision (0),
66  m_author ("John Doe"),
67  m_help_msg ("No help available"),
69  m_log_stdout ("no"),
70  m_daemon ("no"),
71  m_ommit_pidfile ("no"),
72  m_log_level (-1),
73  m_help_flag (false),
74  m_version_flag (false),
75  m_exit_value (0)
76 {
77  add_flag_opt ('h', "help", &m_help_flag);
78  add_flag_opt ('v', "version", &m_version_flag);
79 
80  add_opt ('d', "log-stdout", &m_log_stdout);
81  add_opt ('b', "daemon", &m_daemon);
82  add_opt ('L', "ommit-pidfile", &m_ommit_pidfile);
83  add_opt ('s', "with-log-server", &m_with_log_server);
84  add_opt ('m', "mask", &m_mask);
85  add_opt ('D', "log-file", &m_log_file);
86  add_opt ('f', "config-file", &m_config_file);
87  add_opt ('n', "instance", &m_instance);
88  add_opt ('p', "port", &m_port);
89  add_opt ('z', "log-size", &m_log_size);
90  add_opt ('l', "pidfile", &m_pidfile);
91  add_opt ('S', "log-server", &m_log_server);
92  add_opt ('c', "log-level", &m_log_level);
93 
96  char hn[64];
97  ::gethostname (hn, sizeof (hn)-1);
98  m_log_server += hn;
99 }
bool add_opt(const char c, const string &s, string *str)
Add an option with STL string argument.
bool add_flag_opt(const char c, const string &s, bool *f)
Add binary flag option.
string m_log_server
Log server, assa-logd, address (port@host)
Definition: GenServer.h:263
string m_author
Author's name.
Definition: GenServer.h:289
string m_version
Software version.
Definition: GenServer.h:283
int m_instance
Process instance.
Definition: GenServer.h:253
string m_log_file
Full pathname of debug file.
Definition: GenServer.h:256
bool m_graceful_quit
Flag that indicates wheather server outgh to stop and exit.
Definition: GenServer.h:269
string m_config_file
alternative configuration file name
Definition: GenServer.h:247
string m_ommit_pidfile
If 'yes', skip PID file locking creation/locking step.
Definition: GenServer.h:304
string m_pidfile
PID File lock path name.
Definition: GenServer.h:316
int m_revision
Software revision (patch) level.
Definition: GenServer.h:286
string m_with_log_server
If 'yes', send log messages to the log server.
Definition: GenServer.h:259
bool m_version_flag
Version option flag.
Definition: GenServer.h:326
u_int m_log_size
Max size of the log file.
Definition: GenServer.h:250
long m_mask
Debug file mask to filter debug/error messages.
Definition: GenServer.h:266
string m_log_stdout
If 'yes', redirects all logging messages to std::cerr.
Definition: GenServer.h:298
string m_port
listening port name
Definition: GenServer.h:241
LogFlag m_log_flag
Log file initialization flag. If RM_LOG, remove old log file.
Definition: GenServer.h:295
int m_exit_value
Exit value of the process.
Definition: GenServer.h:329
const char * m_help_msg
Help information.
Definition: GenServer.h:292
int m_log_level
Logging level - an integer number that incrementally increases verbosity of the looing messages.
Definition: GenServer.h:310
bool m_help_flag
Help option flag.
Definition: GenServer.h:321
string m_daemon
Daemon option flag. If 'yes', become a UNIX daemon process.
Definition: GenServer.h:301
@ ALL
All messages: library + application
Definition: LogMask.h:62

References ASSA::CmdLineOpts::add_flag_opt(), ASSA::CmdLineOpts::add_opt(), m_config_file, m_daemon, m_help_flag, m_instance, m_log_file, m_log_level, m_log_server, m_log_size, m_log_stdout, m_mask, m_ommit_pidfile, m_pidfile, m_port, m_version_flag, and m_with_log_server.

◆ ~GenServer()

GenServer::~GenServer ( )
virtual

Destructor.

Reactor needs to detach itself from the Logger before releasing memory.

Otherwise, a race condition between Logger (singleton) and GenServer (singleton) might yield core dump if Reactor was destroyed before Logger. Since Reactor is attached to the Logger with Logger::log_open () for the assa-logd connection, it is Reactor's responsibility to detach first. But, we only care about GenServer's Reactor. All others (such as those used by Connector and Acceptor classes) should not.

Definition at line 111 of file GenServer.cpp.

113 {
114  Log::log_close ();
115 }
int log_close(void)
Close logging stream.
Definition: Logger.h:359

References ASSA::Log::log_close().

◆ GenServer() [2/2]

ASSA::GenServer::GenServer ( const GenServer )
private

No cloning.

Member Function Documentation

◆ become_daemon()

bool GenServer::become_daemon ( )
static

Become a daemon process.

Definition at line 357 of file GenServer.cpp.

359 {
360 #if defined(WIN32)
361  return true;
362 #else
364 
365  if (!f.isChild ()) { // parent exits
366  exit (0);
367  }
368 
369  int size = 1024;
370  int i = 0;
371  pid_t nullfd;
372 
373  for (i = 0; i < size; i++) {
374  (void) close (i);
375  }
376 
377  nullfd = open ("/dev/null", O_WRONLY | O_CREAT, 0666);
378  if (nullfd == -1) {
379  syslog (LOG_ERR,"failed to open \"/dev/null\"");
380  return false;
381  }
382 
383  (void) dup2 (nullfd, 1);
384  (void) dup2 (nullfd, 2);
385  (void) close (nullfd);
386 
387  if ( setsid() == -1 ) {
388  syslog (LOG_ERR,"setsid() failed");
389  return false;
390  }
391 
392  /*---
393  Changing to root directory would be the right thing to do for a
394  server (so that it wouldn't possibly depend on any mounted file
395  systems. But, in practice, it might cause a lot of problems.
396  ---*/
397 #if 0
398  if ( chdir("/") == -1 ) {
399  return false;
400  }
401 #endif
402  return (true);
403 
404 #endif // defined(WIN32)
405 }
Fork class is a simple wrapper around C library function fork().
Definition: Fork.h:86
@ LEAVE_ALONE
Ignore all running children on exit.
Definition: Fork.h:94
@ IGNORE_STATUS
Don't wait for child to complete.
Definition: Fork.h:100

References ASSA::Fork::IGNORE_STATUS, ASSA::Fork::isChild(), and ASSA::Fork::LEAVE_ALONE.

Referenced by init().

◆ display_help()

void ASSA::GenServer::display_help ( )
inlinevirtual

List options and invocation syntax to stdout.

Definition at line 374 of file GenServer.h.

376 {
377  std::cout << m_help_msg << '\n'
378  << "Written by " << m_author << "\n" << std::endl;
379 }
Socket & endl(Socket &os_)
endl manipulator.
Definition: Socket.h:602

References ASSA::endl(), m_author, and m_help_msg.

Referenced by init().

◆ fatal_signal_hook()

virtual void ASSA::GenServer::fatal_signal_hook ( )
inlinevirtual

Hook for derived class to do addition clean-up when terminating signal is delivered by OS.

Note that signal handling is provided by default and no additional intervention is necessary. Use this method only to enhance it.

Definition at line 135 of file GenServer.h.

135 { /*--- empty ---*/ }

Referenced by handle_signal().

◆ fini()

virtual int ASSA::GenServer::fini ( void  )
inlinevirtual

This is an iterface function corresponding to the object moving back into IDLE state.

Derived class is expected to perform actions that terminate execution of the service.

Definition at line 107 of file GenServer.h.

107 { return 0; }

◆ get_cmdline_name()

string ASSA::GenServer::get_cmdline_name ( )
inline

Get command-line process name.

Definition at line 190 of file GenServer.h.

190 { return m_cmdline_name; }
string m_cmdline_name
process name as appeared on command line
Definition: GenServer.h:238

References m_cmdline_name.

Referenced by init_internals().

◆ get_config_file()

string ASSA::GenServer::get_config_file ( )
inline

Get alternative configuration file name.

This name is specified as command-line argument '-f'

Definition at line 203 of file GenServer.h.

203 { return m_config_file; }

References m_config_file.

◆ get_default_config_file()

string ASSA::GenServer::get_default_config_file ( )
inline

Get default configuration file name: $HOME/.

{command_line_name}.cfg If you want your configuration file name to be different, change the value of m_std_config_name in derived class

Definition at line 198 of file GenServer.h.

198 { return m_default_config_file; }
string m_default_config_file
standard configuration file name
Definition: GenServer.h:244

References m_default_config_file.

◆ get_exit_value()

int ASSA::GenServer::get_exit_value ( ) const
inline

Retrieve exit value of the process.

Definition at line 227 of file GenServer.h.

227 { return m_exit_value; }

References m_exit_value.

◆ get_port()

string ASSA::GenServer::get_port ( )
inline

Return assumed name of the listening port.

Definition at line 206 of file GenServer.h.

206 { return m_port; }

References m_port.

◆ get_proc_name()

string ASSA::GenServer::get_proc_name ( )
inline

Get name of process+instance_number.

Definition at line 182 of file GenServer.h.

182 { return m_proc_name; }
string m_proc_name
process name (considering instance_number)
Definition: GenServer.h:235

References m_proc_name.

Referenced by init_internals().

◆ get_reactor()

Reactor* ASSA::GenServer::get_reactor ( )
inline

Obtain reference to the Reactor.

Definition at line 221 of file GenServer.h.

221 { return &m_reactor; }
Reactor m_reactor
GenServer object has its very own personal Reactor object.
Definition: GenServer.h:280

References m_reactor.

Referenced by handle_signal(), and init_internals().

◆ get_sig_manager()

SigHandlers& ASSA::GenServer::get_sig_manager ( )
inline

Obtain reference to the Signal Manager, class SigHandls.

Definition at line 216 of file GenServer.h.

216 { return m_sig_dispatcher; }
SigHandlers m_sig_dispatcher
Signal handlers dispatcher.
Definition: GenServer.h:273

References m_sig_dispatcher.

◆ get_version()

string ASSA::GenServer::get_version ( )
inline

Obtain version information.

Definition at line 365 of file GenServer.h.

367 {
368  std::ostringstream v;
369  v << "Version: " << m_version << " Revision: " << m_revision << std::ends;
370  return (v.str ());
371 }
Socket & ends(Socket &os_)
ends manipulator.
Definition: Socket.h:622

References ASSA::ends(), m_revision, and m_version.

Referenced by init().

◆ handle_signal()

int GenServer::handle_signal ( int  signum_)
virtual

Handle fatal signals.

Hook (e.g. fatalSignalHook) is provided if derived class needs extra work before falling dead.

Reimplemented from ASSA::EventHandler.

Definition at line 408 of file GenServer.cpp.

410 {
411  trace("GenServer::handle_signal");
412  std::ostringstream m;
413 
414  switch (signum_)
415  {
416  case SIGTERM: m << "SIGTERM signal caugth. "; break;
417  case SIGINT: m << "SIGINT signal caugth. "; break;
418  default: m << "Unexpected signal caugth.";
419  }
420  m << "Signal # " << signum_ << std::ends;
421  DL((APP,"%s\n", m.str ().c_str () ));
422  DL((APP,"Initiating shutdown sequence...\n"));
423 
425 
426  DL((APP, "Shutdown sequence completed - Exiting !\n"));
427 
428  /* Calling stop_service () triggers a call to Reactor::stopReactor()
429  with subsequent call to Reactor::removeIOHandler() and then
430  EventHandler::handle_close(). If EventHandler is in the middle
431  of the *slow* system call such as read(2), handle_close() will
432  destry EventHandler, and after cotrol is returned from
433  GenServer::handle_signal(), *slow* system call is restarted
434  and proceeds to operate on the memory that has been deleted already.
435 
436  Calling Reactor::deactivate() instead delays memory release.
437  */
438  get_reactor()->deactivate ();
439  m_graceful_quit = true;
440 
441  return 0;
442 }
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition: Logger.h:429
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
virtual void fatal_signal_hook()
Hook for derived class to do addition clean-up when terminating signal is delivered by OS.
Definition: GenServer.h:135
Reactor * get_reactor()
Obtain reference to the Reactor.
Definition: GenServer.h:221
void deactivate(void)
Deactivate Reactor.
Definition: Reactor.h:234
@ APP
Application-level messages
Definition: LogMask.h:27

References ASSA::APP, ASSA::Reactor::deactivate(), DL, ASSA::ends(), fatal_signal_hook(), get_reactor(), m_graceful_quit, and trace.

◆ init()

void GenServer::init ( int *  argc,
char *  argv[],
const char *  help_info 
)
virtual

Provide an entry point into the service and perfom initialization of the service.

Open log file and log startup options. Process standard command-line arguments. Following signals are handled in uniform manner: SIGHUP, SIGPIPE, SIGCHLD, SIGCLD, SIGALRM, SIGINT, SIGPOLL, SIGTERM.

This function corresponds to the object moving from IDLE to RUNNING state as result of service initialization, or reconfiguration of the service and remaining in RUNNING state.

Parameters
argcPointer to number of command line arguments
argvCommand line arguments char* array
help_infoTitle that will be displayed with -h option

Solaris x86 whole path is returned. Scan through the path and get the process name.

Convert relative paths of all filepath options to absolute paths.

Daemonize the process if asked

Setting defaults if required

Setup signal handling. Ignore SIGHUP, SIGPIPE, SIGCHLD, SIGCLD, SIGALRM by default.

SIGHUP is generated by terminal driver (see termio(7I) for details) in response to modem hangup (or closing terminal session). I ignore it here with the assumption that GenServer is alway a daemon process that doesn't have associated controlling terminal anyway.

Catch SIGPOLL - sigPOLL handler just does nothing except of catching signal.

SIGINT is generated by the terminal driver when an interrupt key is pressed (DELETE or Ctrl-C). It is sent to all processes associated with the controlling terminal. We terminate process in this case.

Catch and handle SIGTERM signals. is the termination signal sent by kill command by default or internally as a part of fatal application exception handling to properly terminate GenServer process.

Initialize other internal stuff.

Definition at line 123 of file GenServer.cpp.

125 {
126  char* cp = argv [0];
127  m_help_msg = ht_;
128 
133  if (strchr(cp, ASSA_DIR_SEPARATOR)) {
134  cp += strlen(argv[0]); // position at the end
135  while (*cp-- != ASSA_DIR_SEPARATOR) {
136  ;
137  }
138  cp += 2;
139  }
140 
141 #if defined (WIN32) // get rid of '.exe'
142  char* extidx = cp;
143  while (*extidx) {
144  if (*extidx == '.') {
145  *extidx = '\0';
146  break;
147  }
148  extidx++;
149  }
150 #endif
151  m_cmdline_name = cp;
152 
153  if (!parse_args ((const char **)argv)) {
154  std::cerr << "Error in arguments: " << get_opt_error () << std::endl;
155  std::cerr << "Try '" << argv[0] << " --help' for details.\n";
156  exit (1);
157  }
158 
159  if (m_help_flag) {
160  display_help ();
161  exit (0);
162  }
163 
164  if (m_version_flag) {
165  std::cerr << '\n' << argv[0] << " " << get_version () << '\n' << '\n'
166  << "Written by " << m_author << "\n\n";
167  exit (0);
168  }
169 
173  std::string s;
174 
175  if (m_default_config_file.size ()) {
178  }
179 
180  if (m_config_file.size ()) {
181  s = ASSA::Utils::strenv (m_config_file.c_str ());
182  m_config_file = s;
183  }
184 
185  if (m_log_file.size ()) {
186  s = ASSA::Utils::strenv (m_log_file.c_str ());
187  m_log_file = s;
188  }
189 
190  if (m_pidfile.size ()) {
191  s = ASSA::Utils::strenv (m_pidfile.c_str ());
192  m_pidfile = s;
193  }
194 
197  if (m_daemon == "yes") {
198  assert(become_daemon ());
199  }
200 
203  char instbuf[16]; // INT_MAX [-]2147483647
204  sprintf(instbuf, "%d", m_instance);
205 
206  if (m_proc_name.length() == 0) {
208 
209  if (m_instance != -1) {
210  m_proc_name += instbuf;
211  }
212  }
213  if (m_port.length() == 0) {
215  }
216 
217 #if !defined(WIN32)
221  SigAction ignore_act( SIG_IGN );
222 
230  ignore_act.register_action( SIGHUP );
231 
232  ignore_act.register_action( SIGPIPE );
233  ignore_act.register_action( SIGCHLD );
234 #if !(defined (__FreeBSD__) || defined(__FreeBSD_kernel__) \
235  || defined (__NetBSD__))
236  ignore_act.register_action( SIGCLD );
237 #endif
238  ignore_act.register_action( SIGALRM );
239 
245 
252  m_sig_dispatcher.install ( SIGINT, (EventHandler*) this );
253 
260  m_sig_dispatcher.install ( SIGTERM, (EventHandler*) this );
261 
262 #endif // !defined(WIN32)
263 
266  init_internals ();
267 }
#define ASSA_DIR_SEPARATOR
CommonUtils.h.
Definition: CommonUtils.h:47
#define ASSAIOSIG
Definition: Handlers.h:38
bool parse_args(const char *argv[])
Parse command line arguments based on installed options set.
const char * get_opt_error() const
If previous call to one of member functions returned false, retrieve detailed error message.
Definition: CmdLineOpts.h:309
EventHandler class.
Definition: EventHandler.h:103
static bool become_daemon()
Become a daemon process.
Definition: GenServer.cpp:358
void init_internals()
Initialize internals.
Definition: GenServer.cpp:271
virtual void display_help()
List options and invocation syntax to stdout.
Definition: GenServer.h:375
SIGPOLLHandler m_sig_poll
Function that swallows SIGPOLL calls.
Definition: GenServer.h:276
string get_version()
Obtain version information.
Definition: GenServer.h:366
virtual int install(int signum_, EventHandler *new_hand_, SigAction *new_disp_=0, EventHandler **old_hand_=0, SigAction *old_disp_=0)
Register EventHandler with dispatching system.
Definition: SigHandlers.cpp:36
std::string strenv(const char *in_)
Expand the passed string in_ by substituting environment variable names for their values.

References ASSA_DIR_SEPARATOR, ASSAIOSIG, become_daemon(), display_help(), ASSA::endl(), ASSA::CmdLineOpts::get_opt_error(), get_version(), init_internals(), ASSA::SigHandlers::install(), m_author, m_cmdline_name, m_config_file, m_daemon, m_default_config_file, m_help_flag, m_help_msg, m_instance, m_log_file, m_pidfile, m_port, m_proc_name, m_sig_dispatcher, m_sig_poll, m_version_flag, ASSA::CmdLineOpts::parse_args(), ASSA::SigAction::register_action(), and ASSA::Utils::strenv().

◆ init_internals()

void GenServer::init_internals ( )
private

Initialize internals.

Set standard configuration file name. For POSIX systems, it is $HOME/.procname. For Win32, it is $cwd/procname.ini.

Remove existing log file if requested. Unlinking /dev/null character device and replacing it with a regular file leads to the system crash during consecutive reboots. See also assa/FileLogger.cpp.

Open logging facility:

–log-stdout="yes" takes precedence over –with-log-server="yes" which takes precedence over –log-file=/path/to/log

Definition at line 270 of file GenServer.cpp.

272 {
273  static const char self[] = "GenServer::init_internals";
274 
279 #if defined (WIN32)
280  m_default_config_file = this->get_cmdline_name () + ".ini";
281 #else
282  m_default_config_file = "$HOME/." + this->get_cmdline_name ();
284 #endif
285 
291  if (m_log_flag == RMLOG && m_log_stdout == "no")
292  {
293  struct stat fst;
294  if (::stat (m_log_file.c_str(), &fst) == 0)
295  {
296  if (S_ISREG (fst.st_mode)) {
297  ::unlink (m_log_file.c_str());
298  }
299  }
300  }
301 
310 
311  if (m_log_stdout == "yes") {
313  }
314  else {
315  if (m_with_log_server == "yes") {
317  m_log_file.c_str(),
318  get_reactor (),
319  m_mask,
320  m_log_size) ;
321  }
322  else {
324  }
325  }
326 
327  trace(self);
328 
329  if (m_ommit_pidfile == "no")
330  {
331  if (m_pidfile.size () == 0) {
332  string s ("~/." + m_proc_name + ".pid");
333  m_pidfile = ASSA::Utils::strenv (s.c_str ());
334  }
335  if (! m_pidfile_lock.lock (m_pidfile)) {
336  DL((ASSAERR,"Failed to lock PID file: %s\n",
338  exit (1);
339  }
340  }
341 
342  DL((APP,"\n" ));
343  DL((APP,"========================================================\n"));
344  DL((APP,"|| Server configuration settings ||\n"));
345  DL((APP,"========================================================\n"));
346  DL((APP," cmd_line_name = '%s'\n", m_cmdline_name.c_str() ));
347  DL((APP," name = '%s'\n", m_proc_name.c_str() ));
348  DL((APP," default config file = '%s'\n", m_default_config_file.c_str()));
349  DL((APP," config file = '%s'\n", m_config_file.c_str() ));
350  DL((APP," mask = 0x%X\n", m_mask ));
351  dump ();
352  DL((APP,"========================================================\n"));
353  DL((APP,"\n"));
354 }
void dump() const
Write options set to the log file.
string get_cmdline_name()
Get command-line process name.
Definition: GenServer.h:190
PidFileLock m_pidfile_lock
PID File lock.
Definition: GenServer.h:313
string get_proc_name()
Get name of process+instance_number.
Definition: GenServer.h:182
const char * get_error_msg() const
In case of error, return a verbal description of the last error.
Definition: PidFileLock.h:141
bool lock(const string &filename_)
Lock the file.
Definition: PidFileLock.cpp:62
int open_log_file(const char *logfname_, u_long groups_=ALL, u_long maxsize_=10485760)
Open log file.
Definition: Logger.h:319
void set_app_name(const std::string &appname_)
Set application name.
Definition: Logger.h:305
int open_log_server(const std::string &logsvraddr_, const char *logfname_, Reactor *reactor_, u_long groups_=ASSA::ALL, u_long maxsize_=10485760)
Open connection with and write log message to the log server.
Definition: Logger.h:344
int open_log_stdout(u_long groups_=ALL)
Write log message to standard output.
Definition: Logger.h:327
@ ASSAERR
ASSA and system errors
Definition: LogMask.h:34

References ASSA::APP, ASSA::ASSAERR, DL, ASSA::CmdLineOpts::dump(), get_cmdline_name(), ASSA::PidFileLock::get_error_msg(), get_proc_name(), get_reactor(), ASSA::PidFileLock::lock(), m_cmdline_name, m_config_file, m_default_config_file, m_log_file, m_log_flag, m_log_server, m_log_size, m_log_stdout, m_mask, m_ommit_pidfile, m_pidfile, m_pidfile_lock, m_proc_name, m_with_log_server, ASSA::Log::open_log_file(), ASSA::Log::open_log_server(), ASSA::Log::open_log_stdout(), RMLOG, ASSA::Log::set_app_name(), ASSA::Utils::strenv(), and trace.

Referenced by init().

◆ init_service()

virtual void ASSA::GenServer::init_service ( )
pure virtual

Interface function provided for derived classes as a place to initialize specifics of derived server.

◆ operator=()

GenServer& ASSA::GenServer::operator= ( const GenServer )
private

◆ process_events()

virtual void ASSA::GenServer::process_events ( )
pure virtual

Interface function provided for derived classes as the main entry for data processing.

This is the place to implement main event loop.

◆ resume()

virtual int ASSA::GenServer::resume ( void  )
inlinevirtual

Resume execution of a service.

Corresponds to the process returning back to RUNNING state from SUSPENDED state.

Definition at line 117 of file GenServer.h.

117 { return 0; }

◆ service_is_active()

bool ASSA::GenServer::service_is_active ( )
inline

Normally called by the main loop to find out whether 'graceful quit' flag had been raised, signaling that some application's component requested to end data processing.

Returns
true when active; false if 'graceful quit' flag has been raised;

Definition at line 149 of file GenServer.h.

149 { return (!m_graceful_quit); }

References m_graceful_quit.

◆ set_author()

void ASSA::GenServer::set_author ( const string &  author_)
inline

Set author's name.

Definition at line 358 of file GenServer.h.

360 {
361  m_author = author_;
362 }

References m_author.

◆ set_exit_value()

void ASSA::GenServer::set_exit_value ( int  v_)
inlineprotected

Set exit value of the process. This value is returned to the shell.

Definition at line 231 of file GenServer.h.

231 { m_exit_value = v_; }

References m_exit_value.

◆ set_flags()

void ASSA::GenServer::set_flags ( LogFlag  logf_)
inline

New debug information is added to the old log file.

To erase old log file, set flag to RMLOG.

Parameters
logf_Defaulted to KEEPLOG that adds log records to the existing log file; RMLOG - remove existing log file and start afresh.

Definition at line 176 of file GenServer.h.

176 { m_log_flag = logf_; }

References m_log_flag.

◆ set_port()

void ASSA::GenServer::set_port ( string  port_)
inline

Set listening port name.

Parameters
port_new listening port name

Definition at line 211 of file GenServer.h.

211 { m_port = port_; }

References m_port.

◆ set_proc_name()

void ASSA::GenServer::set_proc_name ( string  proc_name_)
inline

Change process name.

Parameters
proc_name_new process name

Definition at line 187 of file GenServer.h.

187 { m_proc_name = proc_name_; }

References m_proc_name.

◆ set_version()

void ASSA::GenServer::set_version ( const string &  release_,
int  revision_ 
)
inline

Set Version and Revision number.

Parameters
release_Release number.
revision_Patch level.

Definition at line 350 of file GenServer.h.

352 {
353  m_version = release_;
354  m_revision = revision_;
355 }

References m_revision, and m_version.

◆ stop_service()

void ASSA::GenServer::stop_service ( )
inline

Inform server that it has to stop data processing, clean up and exit.

This method will also stop internal Reactor.

Definition at line 342 of file GenServer.h.

344 {
345  m_graceful_quit = true;
347 }

References ASSA::Reactor::deactivate(), m_graceful_quit, and m_reactor.

◆ suspend()

virtual int ASSA::GenServer::suspend ( void  )
inlinevirtual

Temporarily suspend the execution of a service.

Corresponds to process leaving RUNNING state and entering SUSPENDED state.

Definition at line 112 of file GenServer.h.

112 { return 0; }

Member Data Documentation

◆ m_author

string ASSA::GenServer::m_author
protected

Author's name.

Definition at line 289 of file GenServer.h.

Referenced by display_help(), init(), and set_author().

◆ m_cmdline_name

string ASSA::GenServer::m_cmdline_name
protected

process name as appeared on command line

Definition at line 238 of file GenServer.h.

Referenced by get_cmdline_name(), init(), and init_internals().

◆ m_config_file

string ASSA::GenServer::m_config_file
protected

alternative configuration file name

Definition at line 247 of file GenServer.h.

Referenced by GenServer(), get_config_file(), init(), and init_internals().

◆ m_daemon

string ASSA::GenServer::m_daemon
protected

Daemon option flag. If 'yes', become a UNIX daemon process.

Definition at line 301 of file GenServer.h.

Referenced by GenServer(), and init().

◆ m_default_config_file

string ASSA::GenServer::m_default_config_file
protected

standard configuration file name

Definition at line 244 of file GenServer.h.

Referenced by get_default_config_file(), init(), and init_internals().

◆ m_exit_value

int ASSA::GenServer::m_exit_value
protected

Exit value of the process.

Definition at line 329 of file GenServer.h.

Referenced by get_exit_value(), and set_exit_value().

◆ m_graceful_quit

bool ASSA::GenServer::m_graceful_quit
protected

Flag that indicates wheather server outgh to stop and exit.

Definition at line 269 of file GenServer.h.

Referenced by handle_signal(), service_is_active(), and stop_service().

◆ m_help_flag

bool ASSA::GenServer::m_help_flag
protected

Help option flag.

If true, [-h, –help] option is being specified on command line.

Definition at line 321 of file GenServer.h.

Referenced by GenServer(), and init().

◆ m_help_msg

const char* ASSA::GenServer::m_help_msg
protected

Help information.

Definition at line 292 of file GenServer.h.

Referenced by display_help(), and init().

◆ m_instance

int ASSA::GenServer::m_instance
protected

Process instance.

Definition at line 253 of file GenServer.h.

Referenced by GenServer(), and init().

◆ m_log_file

string ASSA::GenServer::m_log_file
protected

Full pathname of debug file.

Definition at line 256 of file GenServer.h.

Referenced by GenServer(), init(), and init_internals().

◆ m_log_flag

LogFlag ASSA::GenServer::m_log_flag
protected

Log file initialization flag. If RM_LOG, remove old log file.

Definition at line 295 of file GenServer.h.

Referenced by init_internals(), and set_flags().

◆ m_log_level

int ASSA::GenServer::m_log_level
protected

Logging level - an integer number that incrementally increases verbosity of the looing messages.

The exact meaning of each level is application-specific.

Definition at line 310 of file GenServer.h.

Referenced by GenServer().

◆ m_log_server

string ASSA::GenServer::m_log_server
protected

Log server, assa-logd, address (port@host)

Definition at line 263 of file GenServer.h.

Referenced by GenServer(), and init_internals().

◆ m_log_size

u_int ASSA::GenServer::m_log_size
protected

Max size of the log file.

Definition at line 250 of file GenServer.h.

Referenced by GenServer(), and init_internals().

◆ m_log_stdout

string ASSA::GenServer::m_log_stdout
protected

If 'yes', redirects all logging messages to std::cerr.

Definition at line 298 of file GenServer.h.

Referenced by GenServer(), and init_internals().

◆ m_mask

long ASSA::GenServer::m_mask
protected

Debug file mask to filter debug/error messages.

Definition at line 266 of file GenServer.h.

Referenced by GenServer(), and init_internals().

◆ m_ommit_pidfile

string ASSA::GenServer::m_ommit_pidfile
protected

If 'yes', skip PID file locking creation/locking step.

Definition at line 304 of file GenServer.h.

Referenced by GenServer(), and init_internals().

◆ m_pidfile

string ASSA::GenServer::m_pidfile
protected

PID File lock path name.

Definition at line 316 of file GenServer.h.

Referenced by GenServer(), init(), and init_internals().

◆ m_pidfile_lock

PidFileLock ASSA::GenServer::m_pidfile_lock
protected

PID File lock.

Definition at line 313 of file GenServer.h.

Referenced by init_internals().

◆ m_port

string ASSA::GenServer::m_port
protected

listening port name

Definition at line 241 of file GenServer.h.

Referenced by GenServer(), get_port(), init(), and set_port().

◆ m_proc_name

string ASSA::GenServer::m_proc_name
protected

process name (considering instance_number)

Definition at line 235 of file GenServer.h.

Referenced by get_proc_name(), init(), init_internals(), and set_proc_name().

◆ m_reactor

Reactor ASSA::GenServer::m_reactor
protected

GenServer object has its very own personal Reactor object.

Definition at line 280 of file GenServer.h.

Referenced by get_reactor(), and stop_service().

◆ m_revision

int ASSA::GenServer::m_revision
protected

Software revision (patch) level.

Definition at line 286 of file GenServer.h.

Referenced by get_version(), and set_version().

◆ m_sig_dispatcher

SigHandlers ASSA::GenServer::m_sig_dispatcher
protected

Signal handlers dispatcher.

Definition at line 273 of file GenServer.h.

Referenced by get_sig_manager(), and init().

◆ m_sig_poll

SIGPOLLHandler ASSA::GenServer::m_sig_poll
protected

Function that swallows SIGPOLL calls.

Definition at line 276 of file GenServer.h.

Referenced by init().

◆ m_version

string ASSA::GenServer::m_version
protected

Software version.

Definition at line 283 of file GenServer.h.

Referenced by get_version(), and set_version().

◆ m_version_flag

bool ASSA::GenServer::m_version_flag
protected

Version option flag.

If true, [-v, –version] options is being specified on command line.

Definition at line 326 of file GenServer.h.

Referenced by GenServer(), and init().

◆ m_with_log_server

string ASSA::GenServer::m_with_log_server
protected

If 'yes', send log messages to the log server.

Definition at line 259 of file GenServer.h.

Referenced by GenServer(), and init_internals().


The documentation for this class was generated from the following files: