// TODO: On signal event: close active connections, save world (when implemented), etc. void SignalHandler(int sig) { switch(sig) { case SIGHUP: signal(sig, SIG_IGN); /* TODO: Rehash a config? hmmnn */ std::cout << "SIGHUP caught, ignoring.." << std::endl; break; #ifdef HAVE_BACKTRACE case SIGSEGV: // Justasic: You can stop SIGSEGV's but i HIGHLY recommend against it unless you have good reason to. HandleSegfault(); break; #endif case SIGINT: case SIGKILL: case SIGTERM: signal(sig, SIG_IGN); signal(SIGHUP, SIG_IGN); /* TODO: Put code here for a slow kill shutdown, for now we (should) kick all clients :) */ std::cout << "Received SIGTERM, Exiting.." << std::endl; siggame->disconnectAllClients("Server Shutdown"); exit(0); // For now, exit. break; default: static_cast<void>(0); // TODO: fix this to do something useful } }
void HandleSegfault() { // Justasic: We only use this statment if we have the capibility of outputting a backtrace >.< // Justasic: import some crap from another project i did this to. #ifdef HAVE_BACKTRACE void *array[10]; char **strings; char tbuf[256]; size_t size; time_t now = time(NULL); size = backtrace(array, 10); #ifdef HAVE_SYS_UTSNAME_H struct utsname uts; if(uname(&uts) < 0) std::cerr << "uname() error: " << strerror(errno) << std::endl; #endif strftime(tbuf, sizeof(tbuf), "[%b %d %H:%M:%S %Y]", localtime(&now)); std::stringstream slog; slog << "====================== Segmentation Fault ======================" << std::endl; slog << "Please note that the Mineserver developers may ask you to re-run this under gdb!" << std::endl; slog << "Time of crash: " << tbuf << std::endl; slog << "Mineserver version: " << VERSION_COMPLETE << std::endl; #ifdef HAVE_SYS_UTSNAME_H slog << "System info: " << uts.sysname << " " << uts.nodename << " " << uts.release << " " << uts.machine << std::endl; slog << "System version: " << uts.version << std::endl; #endif slog << "C++ Version: " << __VERSION__ << std::endl; //slog << "Socket Buffer: " << LastBuf << std::endl; //slog << "Location: " << segv_location << std::endl; strings = backtrace_symbols(array, size); for(unsigned i=1; i < size; i++) slog << "BackTrace(" << (i - 1) << "): " << strings[i] << std::endl; free(strings); slog << "======================== END OF REPORT ==========================" << std::endl; std::cout << slog.str(); //Write to terminal. std::cout.flush(); //Clear output #endif // HAVE_BACKTRACE // Try and inform the clients of a server crash so we don't freeze/crash clients siggame->disconnectAllClients("Server Crash"); exit(SIGSEGV); // Exit so we're not still running }