//Respond to client, let them know we have the command. int svPipeSend(int pipedirection, char *message, ...){ int num; va_list ap; FILE *pipefile; char DIRCHECKER[PATH_MAX]; char formatuffer[128] = {0}; va_start(ap, message); vsnprintf(formatuffer, 128, message, ap); va_end(ap); sprintf( DIRCHECKER, "%s/%d.%s", TMPDIR, options.port[PORT_HTTP], ( pipedirection ? "pipe" : "client.pipe" ) ); if( file_exist(DIRCHECKER) && strlen(formatuffer) ) { if( ( pipefile = fopen(DIRCHECKER, "w") ) < 0) { loghandle(LOG_ERR, errno, "Piping Error: unable to open pipe for write: %s", DIRCHECKER ); return 0; } if( ( num = fprintf(pipefile, "%s\r\n", formatuffer) ) < 0) { loghandle(LOG_ERR, errno, "Piping Responce Error: unable to write to pipe: %s", DIRCHECKER ); return 0; } fflush(pipefile); fclose(pipefile); } else { loghandle(LOG_ERR, false, "%s", "Piping Error: message to send but no pipe avaliable" ); return 0; } if( options.verbose ) fflush(stdout); return 1; }
//Read from pipe file... command execution latter to come... void svPipeScan(int pipefileid){ int num, stop; char buffer[128] = {0}, bufferstrip[128] = {0}; if(pipefileid < 0 ) return; num = read(pipefileid, buffer, sizeof(buffer)); buffer[num] = '\0'; stop = 0; sprintf(bufferstrip,"%s",trimwhitespace(buffer)); sprintf(buffer,"%s",bufferstrip); if ( ( num > 0 ) && strlen(buffer) ) { if( !(strcmp(buffer,"stop") ) ) { sysconfig.shutdown = true; stop = 1; } #if IRCBOT_SUPPORT else if( !( strncmp(buffer, "bot", 3) ) ) { if( !ircbot_command(buffer) ) { loghandle(LOG_INFO, false, "Bot subfunction reported error with command: \"%s\"", buffer); svPipeSend(0, "Bot subfunction reported error with command: \"%s\"\n",buffer ); } } #endif else { loghandle(LOG_INFO, false, "Piping Error Unrecognized command size \"%d\" line \"%s\"", num, buffer); } } if( stop ) { svPipeSend(0,"Server is shutting down as requested.."); info( "Shutdown command recived from Pipe." ); } if ( num > 0 ) { svPipeSend(0,"<<<END>>>"); } return; }
/** * fprintf-like helper function for logging debug * messages. */ void MHD_DLOG (const struct MHD_Daemon *daemon, const char *format, ...) { va_list va; char sbuf[512]; va_start(va, format); vsnprintf(sbuf, 512, format, va); va_end(va); if ((daemon->options & MHD_USE_DEBUG) == 0) return; //va_start (va, format); //daemon->custom_error_log (daemon->custom_error_log_cls, format, va); loghandle(LOG_ERR, FALSE, "%s", trimwhitespace(sbuf) ); //va_end (va); }
int main(int argc, char* argv[]) { log_error("----------------------------------------------------"); log_error("---------------DEBUG MODE --------------------------"); log_error("----------------------------------------------------"); /*Read in configuration*/ server_config* tmp = NULL; try { #ifdef CASCADE_DEBUG tmp = new server_config("default.config",true); #else tmp = new server_config("default.config",false); #endif } catch(server_config_exception &e) { cerr << "Invalid Configuration file" << endl; return -3; } catch(...) { cerr << "Invalid Configuration file" << endl; return -3; } boost::scoped_ptr<server_config> config(tmp); /*Load logging module*/ logger* lh = NULL; try { lh = new logger(config->getOpt("LOGFILE"),logger::DEBUG); } catch(logger_exception &e) { cerr << e.what() << endl; return -4; } boost::scoped_ptr<logger> loghandle(lh); lh = NULL; loghandle->consolelog(stringtobool(config->getOpt("LOGCONSOLE"))); loghandle->entry("Starting up server",logger::WARNING); /*Start up data module*/ //server_storage* storagehandle = new mysql_storage(config->dbuser,config->dbpasswd,config->dbhost,config->db); mysql_storage* sh; try { sh = new mysql_storage(config->getOpt("DBUSER"),config->getOpt("DBPASSWORD"),config->getOpt("DBHOST"),config->getOpt("DB")); sh->setLog(loghandle.get()); sh->setAuthenticated(config->getOpt("AUTHENTICATED_MODE")); #ifdef CASCADE_chaintest sh->testrun(); #else sh->run(); #endif } catch(server_storage_exception &e) { cerr << "ERROR starting up storage module (" << e.what() << ")." << endl; //loghandle->entry(string("Server storage module failed (") + string(e.what()) + string(")."),logger::FATAL); return -1; } catch(...) { cerr << "UNHANDLED EXCEPTION IN STORAGE MODULE" << endl; //loghandle->entry(string("Unhandled exception in server storage."),logger::FATAL); return -1; } boost::scoped_ptr<mysql_storage> storagehandle(sh); sh = NULL; /*start up network module*/ server_network* nh; try { nh = new server_network(); nh->setLog(loghandle.get()); nh->setPort(config->getOpt("CONNPORT")); nh->setMaxConn(config->getOpt("MAXCONCURRENT")); nh->setRecvTimeout(config->getOpt("MAXRECVTIMEOUT")); nh->setDataModule(storagehandle.get()); nh->setMinMaxProx(config->getOpt("MINPROXY"),config->getOpt("MAXPROXY")); nh->setUpdateInt(config->getOpt("UPDATEINTERVAL")); storagehandle->setNetworkModule(nh); nh->run(); } catch(server_network_exception &e) { cerr << "ERROR starting up network module (" << e.what() << ")." << endl; loghandle->entry(string("Network module failed") + string(e.what()) + string(")."),logger::FATAL); sig_death = 1; } catch(...) { cerr << "UNHANDLED EXCEPTION IN NETWORK MODULE" << endl; loghandle->entry(string("Unhandled exception in network module."),logger::FATAL); sig_death = 1; } boost::scoped_ptr<server_network> networkhandle(nh); nh = NULL; signal(SIGINT,handle_sigint); /*Old way but eh*/ string input; #ifdef CASCADE_DEBUG storagehandle->purgedb(); #endif while(sig_death == 0) { cout << "Console>>> "; getline(cin,input); if(input == string("quit")) { /*Wait for threads to die off*/ sig_death = 1; } else if(input == string("dumpmap")) { networkhandle->dumpmap(); } else if(input == string("purgedb")) { storagehandle->purgedb(); } else { cout << "Unknown command" << endl;; } } loghandle->entry("Shutting down server",logger::WARNING); // delete networkhandle; // delete config; // delete storagehandle; // delete loghandle; }