void acceptor::handle_accept(const boost::system::error_code& error) { BLOG(trace) << VIEW_FUNCTION; if (!error) { session_->start(upstream_host_, upstream_port_); if (!accept_connections()) { BLOG(error) << "Failure during call to accept."; } } else { BLOG(error) << "Error: " << error.message(); } }
void print_setting(const settings& st) { BLOG(debug) << "config: " << st.config_filename; BLOG(debug) << "section: " << st.section; BLOG(debug) << "log: " << st.log_filename; BLOG(debug) << "level: " << st.log_level; BLOG(debug) << "remote: " << st.remote.host << ":" << st.remote.port; BLOG(debug) << "cname: " << st.cn; BLOG(debug) << "local: " << st.local.host << ":" << st.local.port; BLOG(debug) << "cert: " << st.cert_filename; BLOG(debug) << "key: " << st.key_filename; BLOG(debug) << "dh: " << st.dh_filename; }
void bInput::call_event_key_up(uint32 key) { guard(bInput::call_event_key_up); BLOG( "-- call_event_key_up %d\n", list.size() ); list.lock(); for( bEventListenerVector::iterator it = list.begin(); it != list.end(); ++it ) { BLOG( "-- calling key_up event for (%d)\n", (long)(*it) ); (*it)->on_key_up( key ); } list.unlock(); unguard; }
void bBoard::commit_reflections() { guard(bBoard::commit_reflections); bVector v; bBall * b1, * b2; bBand * bd; int bdc; int flag; for( int i=0; i<ball_size; ++i ) { b1 = ball[i]; v.zero(); flag = 0; for( int j=0; j<ball_size; ++j ) { b2 = ball[j]; if( i!=j && luball.at(i,j) != 0 ) { BLOG( ">> collision %d with %d\n", i, j ); //std::cout << luball; v += b1->collision( b2 ); flag++; } } for( int j=0; j<band_size; ++j ) { bd = band[j]; if( (bdc = luband.at( i, j )) != 0 ) { flag++; if( bdc == 1 ) { v += b1->collision( bd ); } else if( bdc == 2 ) { v += b1->collision( bd, bBand::bEdge1 ); } else if( bdc == 3 ){ v += b1->collision( bd, bBand::bEdge2 ); } } } if( flag != 0 ) { BLOG( ">> ball %d -> setting velocity: %2.2f,%2.2f divided by %d\n", i, v.x, v.y, flag ); b1->set_v( v/(double)flag ); b1->report_collision(flag); } } unguard; }
bool acceptor::accept_connections() { BLOG(trace) << VIEW_FUNCTION; try { session_ = boost::shared_ptr<bridge>(new bridge(io_service_, cname_, context)); acceptor_->async_accept(session_->downstream_socket(), boost::bind(&acceptor::handle_accept, this, boost::asio::placeholders::error)); } catch(std::exception& e) { BLOG(error) << "acceptor exception: " << e.what(); return false; } return true; }
int main(int argc, char* argv[]) { setlocale(LC_ALL, ""); settings setting; try { if (!setting.load(argc, argv)) { return 0; } } catch (const boost::property_tree::ptree_error& error) { std::cerr << "Error: " << error.what() << std::endl; return 1; } catch (const boost::program_options::error& error) { std::cerr << "Error: " << error.what() << std::endl; return 1; } catch (const std::exception& error) { std::cerr << "Error: " << error.what() << std::endl; return 1; } print_setting(setting); if (setting.log_filename.size() != 0 && !boost::filesystem::exists(setting.log_filename)) { std::cerr << "Log file path error " + setting.log_filename; setting.log_filename = "logs/" + setting.section + ".log"; } init_log(setting.log_level, setting.log_filename); boost::asio::io_service ios; try { acceptor acceptor(ios, setting.local.host, setting.local.port, setting.remote.host, setting.remote.port, setting.cn, certs_path(setting.cert_filename, setting.key_filename, setting.dh_filename)); BLOG(info) << "accept connections on " << setting.local.host << ":" << setting.local.port << ", remote host " << setting.remote.host << ":" << setting.remote.port; acceptor.accept_connections(); ios.run(); } catch(std::exception& e) { std::cerr << std::string("Error: ") + e.what() << std::endl; return 1; } return 0; }
bool downloadFile(Socket *tcpSocket, std::string filename, bool zlib) { if(zlib) { return downloadZlibFile(tcpSocket, filename); } char buf[4096]; int wn = tcpSocket->readLine(buf); if(wn <= 0) { tcpSocket->close(); return false; } int code_len = atoi(buf); if(code_len <= 0) { return false; } int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0777); if(fd < 0) { BLOG("downloadFile() can not create file " + filename, SYSCALL_ERROR); return false; } while(code_len > 0) { int count_read = sizeof(buf); if(code_len < count_read) count_read = code_len; tcpSocket->read(buf, count_read); buf[count_read] = 0; code_len -= count_read; int nw = writen(fd, buf, count_read); if(nw != count_read) { BLOG("downloadFile() write error", SYSCALL_ERROR); close(fd); return false; } } close(fd); return true; }
bool acceptor::init_context(certs_path& crt) { BLOG(trace) << VIEW_FUNCTION; try { context = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23); context->set_options( boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 | boost::asio::ssl::context::single_dh_use); context->use_certificate_chain_file(crt.cert); context->use_private_key_file(crt.key, boost::asio::ssl::context::pem); context->use_tmp_dh_file(crt.dh); } catch(std::exception& e) { BLOG(error) << e.what(); std::cerr << e.what(); return false; } return true; }
UncompressFunc(std::string filename) { fd = -1; fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0777); if(fd < 0) BLOG("downloadFile() can not create file " + filename, SYSCALL_ERROR); strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; strm.avail_in = 0; strm.next_in = Z_NULL; inflateInit(&strm); }
bool bInput::unregister_listener(bEventListener * listener) { BLOG( "-- unregistering event listener\n" ); guard(bInput::unregister_listener); for( bEventListenerVector::iterator it = list.begin(); it != list.end(); ++it ) { if( *it == listener ) { list.erase( it ); return true; } } return false; unguard; }
bool TraceProcess::canOpen(const char *path, int mode) { std::string sp(path); if((mode & O_WRONLY) == O_WRONLY || (mode & O_RDWR) == O_RDWR || (mode & O_CREAT) == O_CREAT || (mode & O_APPEND) == O_APPEND) { BLOG("TraceProcess::canOpen(): open " + sp + " is not allowed", WARNING); return false; } if(sp.size() == 0) { DLOG(WARNING) << "TraceProcess::canOpen(): can't open an empty file\n"; return false; } return true; }
~bridge() { BLOG(trace) << VIEW_FUNCTION; delete []upstream_data_; delete []downstream_data_; if (downstream_data_io != nullptr) { delete []downstream_data_io; } if (upstream_data_io != nullptr) { delete []upstream_data_io; } if (isCredInit) { //FreeCredentialsHandle(&hClientCreds); } if (isContextInit) { DeleteSecurityContext(&hContext); isContextInit = false; } }
void JavaExecuter::run() { init(); std::string workingDir = combineString(Config::getInstant()->getTmpUserPath(), DIR_SEPARTOR, runId); int port = 0, status; int server_sock = CreateServerSocket(&port); struct sockaddr_un un; socklen_t len = sizeof(un); if(server_sock < 0) { BLOG("JavaExecuter::run(): create server sock error", SYSCALL_ERROR); this->_result = SERVER_ERROR; return; } pid_t pid = fork(); if(pid < 0) { BLOG("JavaExecuter::run() fork error", SYSCALL_ERROR); this->_result = SERVER_ERROR; return; } if(pid == 0) { close(server_sock); if(chdir(workingDir.c_str()) == -1) { LLOG("Fail to change working dir to " + workingDir, SYSCALL_ERROR); raise(SIGKILL); return; } std::string rootPath = Config::getInstant()->getRootPath(); if(stdinFileName[0] != '/' && stdinFileName[0] != '.') { stdinFileName = rootPath + stdinFileName; } if(stdoutFileName[0] != '/' && stdoutFileName[0] != '/') { stdoutFileName = rootPath + stdoutFileName; } std::string minMem = combineString("-Xms", limitMemory / 2 + 200, 'k'); std::string maxMem = combineString("-Xmx", limitMemory + 200, 'k'); std::string javaLib = combineString("-Djava.library.path=", rootPath, "bin"); std::string jarPath = combineString(rootPath, "bin/JavaSandbox.jar"); std::string portStr = combineString(port); std::string limitTimeStr = combineString(limitTime); std::string limitMemoryStr = combineString(limitMemory); std::string limitOutputStr = combineString(limitOutput); std::string uidStr = combineString(Config::getInstant()->getJobUID()); std::string gidStr = combineString(Config::getInstant()->getJobGID()); for(int i = 0; i < 100; i++) close(i); execlp("java", minMem.c_str(), maxMem.c_str(), javaLib.c_str(), "-jar", jarPath.c_str(), portStr.c_str(), limitTimeStr.c_str(), limitMemoryStr.c_str(), limitOutputStr.c_str(), uidStr.c_str(), gidStr.c_str(), stdinFileName.c_str(), stdoutFileName.c_str(), NULL); LLOG("JavaExecuter()::run() Fail to run java", SYSCALL_ERROR); exit(-1); } else { //parent while(1) { //the accept function will be interuppted by the SIGCHLD signal int client_sock = accept(server_sock, (struct sockaddr*)&un, &len); if(client_sock < 0) { if(errno != EINTR) { BLOG("JavaExecuter::run() fail to accept", SYSCALL_ERROR); close(server_sock); this->_result = SERVER_ERROR; return; }else if(waitpid(pid, &status, WNOHANG) > 0) { break; } } else { unsigned int _time, _memory; while(ReadUint32(client_sock, &_time) >= 0 && ReadUint32(client_sock, &_memory) >= 0) { this->_runnedTime = _time; this->_runnedMemory = _memory; judgeThread->updateRunInfo(_time, _memory); } close(client_sock); while(waitpid(pid, &status, 0) < 0 && errno != ECHILD) { } break; } } close(server_sock); if(WIFSIGNALED(status)) { switch(WTERMSIG(status)) { case SIGXCPU: this->_result = TIME_LIMIT_EXCEEDED; break; default: DLOG(ERROR) << "Java process was terminated by signal " << WTERMSIG(status); this->_result = RUNTIME_ERROR; } } else { this->_result = WEXITSTATUS(status); if(this->_result == 1) { this->_result = SERVER_ERROR; }else{ if(this->_result) this->_result += 1000; } if(this->_result == 0) { this->_result = TraceProcess::NORMAL; if(this->_runnedMemory > limitMemory) { this->_result = MEMORY_LIMIT_EXCEEDED; } if(this->_runnedTime > limitTime) { this->_result = TIME_LIMIT_EXCEEDED; } } } } }
bool bBoard::create() { guard(bBoard::create); ball_size = BALL_COUNT; ball = new bBall*[ball_size]; ball[0] = new bBall( bVector( ball_data[0].x, ball_data[0].y ), bVector( 0, 0 ), bVector( 0, 0 ), 0.3, 1.0, 1.0f, 1.0f, 1.0f ); for( int i=1; i<BALL_COUNT; ++i ) { ball[i] = new bBall( bVector( ball_data[i].x, ball_data[i].y ), bVector( 0, 0 ), bVector( 0, 0 ), 0.3, 1.0, 0.5f+((float)(rand()%10))/20.0f, 0.5f+((float)(rand()%10))/20.0f, 0.5f+((float)(rand()%10))/20.0f ); } band_size = BOARD_SEGMENTS; band = new bBand*[band_size]; for( int i=0; i<BOARD_SEGMENTS-1; ++i ) { band[i] = new bBand( bVector( board_data[i].x, board_data[i].y ), bVector( board_data[i+1].x, board_data[i+1].y ) ); } band[BOARD_SEGMENTS-1] = new bBand( bVector( board_data[BOARD_SEGMENTS-1].x, board_data[BOARD_SEGMENTS-1].y ), bVector( board_data[0].x, board_data[0].y ) ); luball.create( ball_size ); luband.create( ball_size, band_size, false ); glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP ); glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP ); if( !ball_tex.load( GETPATH("../../tex/ball.bmp") ) ) { if( !ball_tex.load( GETPATH("../tex/ball.bmp") ) ) { BLOG( "!! Error loading ball texture!\n" ); } } if( !ball_num.load( GETPATH("../../tex/xball.bmp") ) ) { if( !ball_num.load( GETPATH("../tex/xball.bmp") ) ) { BLOG( "!! Error loading ball texture!\n" ); } } if( !desk.load( GETPATH("../../tex/desk.bmp") ) ) { if( !desk.load( GETPATH("../tex/desk.bmp") ) ) { BLOG( "!! Error loading desk texture!\n" ); } } if( !band_tex.load( GETPATH("../../tex/band.bmp") ) ) { if( !band_tex.load( GETPATH("../tex/band.bmp") ) ) { BLOG( "!! Error loading band texture!\n" ); } } if( !shadow.load( GETPATH("../../tex/shadow.bmp") ) ) { if( !shadow.load( GETPATH("../tex/shadow.bmp") ) ) { BLOG( "!! Error loading shadow texture!\n" ); } } if( bShader::is_supported() ) { if( !ball_shader.load_fragment( GETPATH("../../shaders/ball_frag.cg") ) ) { if( !ball_shader.load_fragment( GETPATH("../shaders/ball_frag.cg") ) ) { BLOG( "!! Error loading ball shader!\n" ); } } if( !ball_shader.load_vertex( GETPATH("../../shaders/ball_vert.cg") ) ) { if( !ball_shader.load_vertex( GETPATH("../shaders/ball_vert.cg") ) ) { BLOG( "!! Error loading ball shader!\n" ); } } if( !board_shader.load_fragment( GETPATH("../../shaders/board_frag.cg") ) ) { if( !board_shader.load_fragment( GETPATH("../shaders/board_frag.cg") ) ) { BLOG( "!! Error loading board shader!\n" ); } } } else { BLOG( "!! Shaders are not supported!\n" ); } if( !glewIsSupported("GL_ARB_multitexture") ) { BLOG( "!! Multitexturing is not supported!\n" ); } return true; unguard; }
bool bInput::register_listener(bEventListener * listener) { BLOG( "-- registering event listener\n" ); list.push_back( listener ); return true; }
int main(int argc,char *argv[]) { daemon_init(); if(chdir(XJOJHOME) < 0) { BLOG("can't change dir to XJOJHOME", SYSCALL_ERROR); return 1; } if(write_pid("judge.pid") < 0) { return 1; } Config *config = Config::getInstant(); Log::getInstance(config->getLogRoot() + config->getLogFileName()); if(config->isIniError()) { BLOG(config->getErrorMesg(), ERROR); return 1; } if(!config->initConfig(argc, argv)) { BLOG(config->getErrorMesg(), ERROR); return 1; } removeDir(Config::getInstant()->getTmpUserPath().c_str(), false); int totThread = Config::getInstant()->getThreadNumber(); if(totThread <= 0) { BLOG("Thread number is less or equal than 0", ERROR); return 1; } int fd; if((fd = open("/dev/null", O_RDWR, 0600)) == -1 || dup2(fd, 0) == -1 || dup2(fd, 1) == -1 || dup2(fd, 2) == -1) return 1; // Write data to a closed socket will cause SIGPIPE signal installSignalHandler(SIGPIPE, SIG_IGN, 0); std::string host = Config::getInstant()->getServer().first; int port = Config::getInstant()->getServer().second; std::vector<JudgeThread *> threads(totThread); for(int i = 0; i < totThread; i++) { threads[i] = new JudgeThread(i + 1); threads[i]->setServerAddress(host); threads[i]->setPort(port); } for(int i = 0; i < totThread; i++) { threads[i]->start(); } for(int i = 0; i < totThread; i++) { threads[i]->join(); } return 0; }
int TraceProcess::createProcess(std::string cmd, const TraceProcessInfo& info) { init(); std::string filename[] = {info.stdinFileName, info.stdoutFileName, info.stderrFileName}; this->_exit = false; std::vector<std::string> vargv; std::istringstream iss(cmd, std::istringstream::in); std::string arg; while(iss >> arg) { vargv.push_back(arg); } char **argv = new char*[vargv.size() + 1]; argv[vargv.size()] = 0; char * dupProgName = strdup(vargv[0].c_str()); argv[0] = dupProgName; for(int i = 1; i < vargv.size(); i++) { argv[i] = strdup(vargv[i].c_str()); } this->pid = fork(); if(this->pid != 0) { for(int i = 0; i < vargv.size(); i++) free(argv[i]); delete []argv; } if(this->pid < 0) { BLOG("TraceProcess::createProcess(): Fail to fork a process", SYSCALL_ERROR); this->_result = SERVER_ERROR; return -1; } else if(this->pid > 0) { //parent return this->pid; } int mode[] = {O_RDONLY, O_RDWR | O_CREAT | O_TRUNC, O_RDWR}; int fd[] = {info.fdStdin, info.fdStdout, info.fdStderr }; int mask = S_IRUSR | S_IWUSR | S_IROTH; umask(0); for(int i = 0; i < 3; i++) { if(filename[i].length() > 0) { fd[i] = open(filename[i].c_str(), mode[i], mask); if(fd[i] == -1) { LLOG("TraceProcess::createProcess(): Fail to open " + filename[i], SYSCALL_ERROR); for(int j = 0; j < i; j++) { if(filename[i].length() > 0) { close(fd[j]); } } raise(SIGKILL); } } } for(int i = 0; i < 3; i++) { if(fd[i]) { if(dup2(fd[i], i) == -1) { LLOG("TraceProcess::createProcess(): Fail to dup a fd", SYSCALL_ERROR); raise(SIGKILL); } } } for(int i = 3; i < 100; i++) { close(i); } if(info.timeLimit) { int t = info.timeLimit / 1000; if(info.timeLimit % 1000) { t++; } if(setLimit(RLIMIT_CPU, t) == -1) { LLOG("TraceProcess::createProcess(): Fail to set cpu limit", SYSCALL_ERROR); raise(SIGKILL); } } if(info.memoryLimit) { if(setLimit(RLIMIT_DATA, info.memoryLimit * 1024) == -1) { LLOG("TraceProcess::createProcess(): Fail to set memory limit", SYSCALL_ERROR); raise(SIGKILL); } } if(info.outputLimit) { if(setLimit(RLIMIT_FSIZE, info.outputLimit * 1024) == -1) { LLOG("TraceProcess::createProcess(): Fail to set output limit", SYSCALL_ERROR); raise(SIGKILL); } } if(info.fileNoLimit) { if(setLimit(RLIMIT_NOFILE, info.fileNoLimit) == -1) { LLOG("TraceProcess::createProcess(): Fail to set fileno limit", SYSCALL_ERROR); raise(SIGKILL); } } if(info.procNoLimit) { if(setLimit(RLIMIT_NPROC, info.procNoLimit) == -1) { LLOG("TraceProcess::createProcess(): Fail to set procno limit", SYSCALL_ERROR); raise(SIGKILL); } } if(info.uid) { if(setuid(info.uid) == -1) { LLOG(combineString("TraceProcess::createProcess(): Fail to set uid to ", info.uid), SYSCALL_ERROR); raise(SIGKILL); } } if(info.gid) { if(setgid(info.gid) == -1) { LLOG(combineString("TraceProcess::createProcess(): Fail to set gid to ", info.gid), SYSCALL_ERROR); raise(SIGKILL); } } if(setLimit(RLIMIT_CORE, 0) == -1) { LLOG("TraceProcess::createProcess(): Fail to set core limit to 0", SYSCALL_ERROR); raise(SIGKILL); } if(info.workingDir.size() > 0) { if(chdir(info.workingDir.c_str()) == -1) { LLOG("TraceProcess::createProcess(): Fail to change woring dir to " + info.workingDir, SYSCALL_ERROR); raise(SIGKILL); } } if(info.trace) { if(ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1) { LLOG("TraceProcess::createProcess(): Fail to trace child process", SYSCALL_ERROR); raise(SIGKILL); } } if(execvp(dupProgName, argv) == -1) { LLOG("TraceProcess::createProcess(): Fail to execute commmand " + cmd, SYSCALL_ERROR); raise(SIGKILL); } exit(-1); return 0; }