main.cc (3073B)
1 2 #include "sys/sys" 3 #include "config/config" 4 #include "balancer/balancer" 5 #include "error/error" 6 #include "profiler/profiler" 7 #include "ThreadsAndMutexes/mutex/mutex" 8 #include "ThreadsAndMutexes/mutextable/mutextable" 9 10 using namespace std; 11 12 // Global variables (definition) 13 Config config; 14 Balancer balancer; 15 MutexTable mt(100); 16 Mutex cout_lock, cerr_lock; 17 18 static void showlimits() { 19 typedef struct { 20 int resource; 21 string description; 22 } Limit; 23 static Limit limit[] = { 24 { RLIMIT_CORE, "coredump size (bytes)" }, 25 { RLIMIT_CPU, "cpu time (sec)" }, 26 { RLIMIT_DATA, "data segment size (bytes)" }, 27 { RLIMIT_FSIZE, "max file size (bytes)" }, 28 # ifdef RLIMIT_MEMLOCK 29 { RLIMIT_MEMLOCK, "locked mem size (bytes)" }, 30 # endif 31 { RLIMIT_NOFILE, "max open files" }, 32 # ifdef RLIMIT_NPROC 33 { RLIMIT_NPROC, "max processes" }, 34 # endif 35 # ifdef RLIMIT_RSS 36 { RLIMIT_RSS, "max resident set size (bytes)" }, 37 # endif 38 { RLIMIT_STACK, "max stack size (bytes)" } 39 }; 40 41 for (unsigned i = 0; i < sizeof(limit) / sizeof(Limit); i++) { 42 struct rlimit rl; 43 if (getrlimit(limit[i].resource, &rl)) 44 throw Error(string("Failed to request limit: ") + 45 strerror(errno)); 46 ostringstream o; 47 o << "Limits for " << limit[i].description 48 << ": hard limit " << unsigned(rl.rlim_max) 49 << ", soft limit " << unsigned(rl.rlim_cur) << '\n'; 50 msg(o.str()); 51 } 52 } 53 54 static int org_argc; 55 static char **org_argv; 56 static void sigcatcher (int sig) { 57 debugmsg("Seen signal " << sig << '\n'); 58 switch (sig) { 59 60 case SIGHUP: 61 // Generate a report to the log. 62 balancer.report(true); 63 break; 64 65 case SIGUSR1: 66 // Toggle verbosity. 67 if (config.verbose()) 68 config.verbose(false); 69 else 70 config.verbose(true); 71 break; 72 case SIGUSR2: 73 74 // Toggle debugging. 75 if (config.debug()) 76 config.debug(false); 77 else 78 config.debug(true); 79 break; 80 81 case SIGINT: 82 case SIGQUIT: 83 case SIGABRT: 84 case SIGTERM: 85 case SIGSTOP: 86 // Stop the balancer 87 balancer.terminate(true); 88 break; 89 90 default: 91 // Ignore 92 ostringstream o; 93 o << "Signal " << sig << " ignored\n"; 94 msg(o.str()); 95 break; 96 } 97 } 98 99 int main (int argc, char **argv) { 100 101 PROFILE("main"); 102 103 static int relevant_sig[] = { 104 SIGHUP, // Back end states to log 105 SIGINT, SIGQUIT, SIGABRT, SIGTERM, SIGSTOP, // Terminate 106 SIGUSR1, SIGUSR2, // Toggle verbose/debug 107 SIGPIPE, // Ignore 108 }; 109 110 try { 111 // Save original commandline 112 org_argc = argc; 113 org_argv = argv; 114 115 // Load configuration from the commandline, promote verbosity 116 config.parsecmdline (argc, argv); 117 118 if (config.verbose()) 119 showlimits(); 120 121 msg("XR running as PID " << getpid() << '\n'); 122 123 // Load the signal handler. 124 for (unsigned i = 0; i < sizeof(relevant_sig) / sizeof(int); i++) 125 signal (relevant_sig[i], sigcatcher); 126 127 // Configure the balancer and start serving. 128 balancer.init(); 129 balancer.serve(); 130 } catch (Error const &e) { 131 cerr << e.what() << endl; 132 return (1); 133 } catch (...) { 134 cerr << "ERROR: Unidentified exception caught" << endl; 135 } 136 137 return (0); 138 }