bool ATVersion::IsAppRunning(const char *appName, std::list<unsigned long> &pids) { struct dirent **namelist; FILE *stat; char name_str[AT_APPNAME_MAXSIZE + 1]; int num_entries; char status_path[256]; unsigned long pid; unsigned long selfpid = 0; bool res = false; int ret; pids.clear(); memset(status_path, '\0', sizeof(status_path)); snprintf(status_path, sizeof(status_path), AT_PIDFILE_NAME_FORMAT, appName); std::ifstream pidf(status_path); if (pidf.is_open()) { pidf >> pid; pidf.close(); if (!(GetAppPathByPid(pid).empty())) { pids.push_back(pid); return true; } }
static qint64 readPidFileOfApplication(const QString &appRoot = QString()) { QFile pidf(pidFilePath(appRoot)); if (pidf.open(QIODevice::ReadOnly)) { qint64 pid = pidf.readLine(100).toLongLong(); if (pid > 0) { return pid; } } return -1; }
void pidfile::write(){ // open pidfile for writing pidfile_fd = open(pidfile_path.c_str(), O_WRONLY|O_CREAT|O_NOFOLLOW, 0644); if (0 > pidfile_fd) { std::ostringstream msg; msg << "Cannot open pidfile '" << pidfile_path.c_str() << "': "; throw std::runtime_error(msg.str()); } // lock pidfile for writing int rc = lockf(pidfile_fd, F_TLOCK, 0); if (-1 == rc) { std::ostringstream msg; msg << "Cannot lock pidfile '" << pidfile_path << "': "; throw std::runtime_error(msg.str()); } // truncate pidfile at 0 length ftruncate(pidfile_fd, 0); // write our pid try { std::ofstream pidf(pidfile_path.c_str()); pidf << getpid(); } catch(std::exception x) { std::ostringstream msg; msg << "Cannot write pidfile '" << pidfile_path << "': " << x.what(); throw std::runtime_error(msg.str()); } }
int main(int argc,char *argv[]) { if(!BaseSock::BaseInit()) { cout << "base init failed\n"; return 0; } int arganz = 0; string arg1 = ""; string arg2 = ""; arganz = argc -1; if(arganz > 0) arg1 = argv[1]; if(arganz > 1) arg2 = argv[2]; cout << "Simple Win32/Linux traffic bouncer v0.2.2 2011/02/07 (c) _hawk_/PPX\n"; cout << "Using " << version << "\n"; if(arganz < 1 && arganz > 2) { cout << "usage: tbnc <configfile> or tbnc -u <configfile> for uncrypted conf\n"; return 0; } if(arganz == 1) { string key; getpassword("Enter blowfish key",key); if(!options.config.Init(arg1,key)) { cout << "error reading conf\n"; return 0; } } else { if(arg1 == "-u") { if(!options.config.Init(arg2,"")) { cout << "error reading conf\n"; return 0; } } else { cout << "usage: tbnc <configfile> or tbnc -u <configfile> for uncrypted conf\n"; return 0; } } options.GetOptional(); if(!options.GetRequired()) { cout << "one or more basic options not set\n"; return 0; } if(options.buffersize > 0) { BaseSock::Buffersize(options.buffersize); } if(options.sndrcvbufsize > 0) { BaseSock::SndRcvBufSize(options.sndrcvbufsize); } if(!BaseSock::BaseSslInit(options.certpath, options.certpath)) { cout << "Ssl server init failed\n"; return 0; } if(options.delay > 0) { BaseSock::Delay(options.delay); } if(options.retrycount > 0) { BaseSock::Retrycount(options.retrycount); } ServerSock ss; if(!ss.Init()) { cout << "socket init failed\n"; return 0; } if(!ss.Bind(options.listenip, options.listenport)) { cout << "could not bind\n"; return 0; } ss.Listen(100); if(!options.logToScreen) { #ifdef _WIN32 cout << "Close console window now - tbnc running in background now\n"; #endif daemon(1,1); #ifdef _WIN32 #else if(options.pidfile != "") { int pid = getpid(); ofstream pidf(options.pidfile.c_str(), ios::out | ios::trunc); if (!pidf) { cout << "can not create pid file\n"; } else { pidf << pid << "\n"; pidf.close(); } } #endif } while(1) { EntryThread *et = NULL; et = new EntryThread(&options); if(et != NULL) { if(et->cs.Init(false)) { if(ss.Accept(et->cs,et->clientip,et->clientport)) { et->start(et); } else { options.Log("main: Accept failed"); delete et; } } else { options.Log("main: init entry thread failed"); delete et; } } else { options.Log("main: failed to create entry thread"); } #ifdef _WIN32 Sleep(10); #else usleep(10 * 1000); #endif } return 1; }