00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <sys/types.h>
00043 #include <sys/stat.h>
00044 #include <unistd.h>
00045
00046 #ifdef __CYGWIN32__ // to resolve h_errno dependency
00047 # include <errno.h>
00048 # include <netdb.h>
00049 #endif
00050
00051 #include "assa/GenServer.h"
00052 #include "assa/CommonUtils.h"
00053
00054 using namespace ASSA;
00055
00056 GenServer::GenServer () :
00057 m_log_size (10485760),
00058 m_instance (-1),
00059 m_with_log_server ("no"),
00060 m_log_server ("assalogd@"),
00061 m_mask (ALL),
00062 m_graceful_quit (false),
00063 m_version ("unknown"),
00064 m_revision (0),
00065 m_author ("John Doe"),
00066 m_help_msg ("No help available"),
00067 m_log_flag (KEEPLOG),
00068 m_log_stdout ("no"),
00069 m_daemon ("no"),
00070 m_ommit_pidfile ("no"),
00071 m_log_level (-1),
00072 m_help_flag (false),
00073 m_version_flag (false),
00074 m_exit_value (0)
00075 {
00076 add_flag_opt ('h', "help", &m_help_flag);
00077 add_flag_opt ('v', "version", &m_version_flag);
00078
00079 add_opt ('d', "log-stdout", &m_log_stdout);
00080 add_opt ('b', "daemon", &m_daemon);
00081 add_opt ('L', "ommit-pidfile", &m_ommit_pidfile);
00082 add_opt ('s', "with-log-server", &m_with_log_server);
00083 add_opt ('m', "mask", &m_mask);
00084 add_opt ('D', "log-file", &m_log_file);
00085 add_opt ('f', "config-file", &m_config_file);
00086 add_opt ('n', "instance", &m_instance);
00087 add_opt ('p', "port", &m_port);
00088 add_opt ('z', "log-size", &m_log_size);
00089 add_opt ('l', "pidfile", &m_pidfile);
00090 add_opt ('S', "log-server", &m_log_server);
00091 add_opt ('c', "log-level", &m_log_level);
00092 }
00093
00094
00095
00096
00097
00098
00099 void
00100 GenServer::
00101 init (int* argc, char* argv [], const char* ht_)
00102 {
00103 char* cp = argv [0];
00104 m_help_msg = ht_;
00105
00110 if ( strchr(cp,'/') ) {
00111 cp += strlen(argv[0]);
00112 while ( *cp-- != '/' ) ;
00113 cp += 2;
00114 }
00115 m_cmdline_name = cp;
00116
00117 if (!parse_args ((const char **)argv)) {
00118 std::cerr << "Error in arguments: " << get_opt_error () << std::endl;
00119 std::cerr << "Try '" << argv[0] << " --help' for details.\n";
00120 exit (1);
00121 }
00122 if (m_help_flag) {
00123 display_help ();
00124 exit (0);
00125 }
00126 if (m_version_flag) {
00127 std::cerr << '\n' << argv[0] << " " << get_version () << '\n' << '\n'
00128 << "Written by " << m_author << "\n\n";
00129 exit (0);
00130 }
00131 if (m_daemon == "yes") {
00132 assert(become_daemon ());
00133 }
00136 char instbuf[16];
00137 sprintf(instbuf, "%d", m_instance);
00138
00139 if (m_proc_name.length() == 0) {
00140 m_proc_name = m_cmdline_name;
00141
00142 if (m_instance != -1) {
00143 m_proc_name += instbuf;
00144 }
00145 }
00146 if (m_port.length() == 0) {
00147 m_port = m_proc_name;
00148 }
00149
00153 SigAction ignore_act( SIG_IGN );
00154
00162 ignore_act.register_action( SIGHUP );
00163
00164 ignore_act.register_action( SIGPIPE );
00165 ignore_act.register_action( SIGCHLD );
00166 #if !defined (__FreeBSD__) && !defined(__FreeBSD_kernel__)
00167 ignore_act.register_action( SIGCLD );
00168 #endif
00169 ignore_act.register_action( SIGALRM );
00170
00175 m_sig_dispatcher.install ( ASSAIOSIG, &m_sig_poll );
00176
00183 m_sig_dispatcher.install ( SIGINT, (EventHandler*) this );
00184
00191 m_sig_dispatcher.install ( SIGTERM, (EventHandler*) this );
00192
00195 init_internals ();
00196 }
00197
00198 void
00199 GenServer::
00200 init_internals ()
00201 {
00202 static const char self[] = "GenServer::init_internals";
00203 char* chp;
00204
00207 m_default_config_file = "$HOME/." + this->get_cmdline_name ();
00208 m_default_config_file = Utils::strenv (m_default_config_file.c_str ());
00209
00215 if (m_log_flag == RMLOG && m_log_stdout == "no") {
00216 struct stat fst;
00217 if (::stat (m_log_file.c_str(), &fst) == 0) {
00218 if (S_ISREG (fst.st_mode)) {
00219 ::unlink (m_log_file.c_str());
00220 }
00221 }
00222 }
00223
00230 char hn[64];
00231 ::gethostname (hn, sizeof (hn)-1);
00232 m_log_server += hn;
00233
00234 Log::set_app_name (get_proc_name ());
00235
00236 if (m_log_stdout == "yes") {
00237 Log::open_log_stdout (m_mask);
00238 }
00239 else {
00240 if (m_with_log_server == "yes") {
00241 Log::open_log_server (m_log_server,
00242 m_log_file.c_str(),
00243 get_reactor (),
00244 m_mask,
00245 m_log_size) ;
00246 }
00247 else {
00248 Log::open_log_file (m_log_file.c_str(), m_mask, m_log_size);
00249 }
00250 }
00251
00252 trace(self);
00253
00254 if (m_ommit_pidfile == "no") {
00255 if (m_pidfile.size () == 0) {
00256 m_pidfile = "~/." + m_proc_name + ".pid";
00257 }
00258 if (! m_pidfile_lock.lock (m_pidfile)) {
00259 DL((ERROR,"Failed to lock PID file: %s\n",
00260 m_pidfile_lock.get_error_msg ()));
00261 exit (1);
00262 }
00263 }
00264
00265 DL((APP,"\n" ));
00266 DL((APP,"========================================================\n"));
00267 DL((APP,"|| Server configuration settings ||\n"));
00268 DL((APP,"========================================================\n"));
00269 DL((APP," cmd_line_name = '%s'\n", m_cmdline_name.c_str() ));
00270 DL((APP," name = '%s'\n", m_proc_name.c_str() ));
00271 DL((APP," default config file = '%s'\n", m_default_config_file.c_str()));
00272 DL((APP," config file = '%s'\n", m_config_file.c_str() ));
00273 DL((APP," mask = 0x%X\n", m_mask ));
00274 dump ();
00275 DL((APP,"========================================================\n"));
00276 DL((APP,"\n"));
00277 }
00278
00279 bool
00280 GenServer::
00281 become_daemon ()
00282 {
00283 Fork f (Fork::LEAVE_ALONE, Fork::IGNORE_STATUS);
00284
00285 if (!f.isChild ()) {
00286 exit (0);
00287 }
00288
00289 int size = 1024;
00290 int i = 0;
00291 pid_t nullfd;
00292
00293 for (i = 0; i < size; i++) {
00294 (void) close (i);
00295 }
00296
00297 nullfd = open ("/dev/null", O_WRONLY | O_CREAT, 0666);
00298 if (nullfd == -1) {
00299 syslog (LOG_ERR,"failed to open \"/dev/null\"");
00300 return false;
00301 }
00302
00303 (void) dup2 (nullfd, 1);
00304 (void) dup2 (nullfd, 2);
00305 (void) close (nullfd);
00306
00307 if ( setsid() == -1 ) {
00308 syslog (LOG_ERR,"setsid() failed");
00309 return false;
00310 }
00311
00312
00313
00314
00315
00316
00317 #if 0
00318 if ( chdir("/") == -1 ) {
00319 return false;
00320 }
00321 #endif
00322 return (true);
00323 }
00324
00325 int
00326 GenServer::
00327 handle_signal (int signum_)
00328 {
00329 trace("GenServer::handle_signal");
00330 std::ostringstream m;
00331
00332 switch (signum_)
00333 {
00334 case SIGTERM: m << "SIGTERM signal caugth. "; break;
00335 case SIGINT: m << "SIGINT signal caugth. "; break;
00336 default: m << "Unexpected signal caugth.";
00337 }
00338 m << "Signal # " << signum_ << std::ends;
00339 DL((APP,"%s\n", m.str ().c_str () ));
00340 DL((APP,"Initiating shutdown sequence...\n"));
00341
00342 fatal_signal_hook ();
00343
00344 DL((APP, "Shutdown sequence completed - Exiting !\n"));
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 get_reactor()->deactivate ();
00357 m_graceful_quit = true;
00358
00359 return 0;
00360 }
00361