From f4df7fdd4e5b9e0d2461aa0184b2b5df1f4a2e6d Mon Sep 17 00:00:00 2001 From: HanzZ Date: Thu, 8 Sep 2011 15:10:17 +0200 Subject: [PATCH] spectrum is daemon now, hope there's no Buffy --- spectrum/src/main.cpp | 98 +++++++++++++++++++++++++++++++++++++---- spectrum/src/sample.cfg | 2 +- src/config.cpp | 6 +++ 3 files changed, 96 insertions(+), 10 deletions(-) diff --git a/spectrum/src/main.cpp b/spectrum/src/main.cpp index c78cd805..a55a5ad9 100644 --- a/spectrum/src/main.cpp +++ b/spectrum/src/main.cpp @@ -33,9 +33,74 @@ static void spectrum_sigterm_handler(int sig) { eventLoop_->stop(); } +#ifndef WIN32 +static void daemonize(const char *cwd, const char *lock_file) { + pid_t pid, sid; + FILE* lock_file_f; + char process_pid[20]; + + /* already a daemon */ + if ( getppid() == 1 ) return; + + /* Fork off the parent process */ + pid = fork(); + if (pid < 0) { + exit(1); + } + /* If we got a good PID, then we can exit the parent process. */ + if (pid > 0) { + exit(0); + } + + /* At this point we are executing as the child process */ + + /* Change the file mode mask */ + umask(0); + + /* Create a new SID for the child process */ + sid = setsid(); + if (sid < 0) { + exit(1); + } + + /* Change the current working directory. This prevents the current + directory from being locked; hence not being able to remove it. */ + if ((chdir(cwd)) < 0) { + exit(1); + } + + if (lock_file) { + /* write our pid into it & close the file. */ + lock_file_f = fopen(lock_file, "w+"); + if (lock_file_f == NULL) { + std::cout << "EE cannot create lock file " << lock_file << ". Exiting\n"; + exit(1); + } + sprintf(process_pid,"%d\n",getpid()); + if (fwrite(process_pid,1,strlen(process_pid),lock_file_f) < strlen(process_pid)) { + std::cout << "EE cannot write to lock file " << lock_file << ". Exiting\n"; + exit(1); + } + fclose(lock_file_f); + } + + if (freopen( "/dev/null", "r", stdin) == NULL) { + std::cout << "EE cannot open /dev/null. Exiting\n"; + exit(1); + } +} + +#endif + int main(int argc, char **argv) { Config config; + + boost::program_options::variables_map vm; + bool no_daemon = false; + std::string config_file; + + #ifndef WIN32 if (signal(SIGINT, spectrum_sigint_handler) == SIG_ERR) { std::cout << "SIGINT handler can't be set\n"; @@ -51,17 +116,32 @@ int main(int argc, char **argv) desc.add_options() ("help,h", "help") ("no-daemonize,n", "Do not run spectrum as daemon") + ("config", boost::program_options::value(&config_file)->default_value(""), "Config file") ; try { - boost::program_options::variables_map vm; - boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); + boost::program_options::positional_options_description p; + p.add("config", -1); + boost::program_options::store(boost::program_options::command_line_parser(argc, argv). + options(desc).positional(p).run(), vm); boost::program_options::notify(vm); + + + if(vm.count("help")) { std::cout << desc << "\n"; return 1; } + + if(vm.count("config") == 0) { + std::cout << desc << "\n"; + return 1; + } + + if(vm.count("no-daemonize")) { + no_daemon = true; + } } catch (std::runtime_error& e) { @@ -74,17 +154,17 @@ int main(int argc, char **argv) return 1; } - if (argc != 2) { - std::cout << desc << "\n"; - return 1; - } - - - if (!config.load(argv[1])) { + if (!config.load(vm["config"].as())) { std::cerr << "Can't load configuration file.\n"; return 1; } +#ifndef WIN32 + if (!no_daemon) { + daemonize("/", NULL); + } +#endif + if (CONFIG_STRING(&config, "logging.config").empty()) { LoggerPtr root = log4cxx::Logger::getRootLogger(); #ifdef WIN32 diff --git a/spectrum/src/sample.cfg b/spectrum/src/sample.cfg index e0ef9d71..f9489aaa 100644 --- a/spectrum/src/sample.cfg +++ b/spectrum/src/sample.cfg @@ -12,7 +12,7 @@ admin_password=test cert=server.pfx #patch to PKCS#12 certificate cert_password=test #password to that certificate if any users_per_backend=10 -backend=../../backends/libpurple/spectrum_libpurple_backend +backend=/home/hanzz/code/libtransport/backends/libpurple/spectrum_libpurple_backend #backend=../../backends/libircclient-qt/spectrum_libircclient-qt_backend #protocol=prpl-msn protocol=any diff --git a/src/config.cpp b/src/config.cpp index aa370efb..5999af3e 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -34,6 +34,12 @@ bool Config::load(const std::string &configfile, boost::program_options::options bool ret = load(ifs, opts); ifs.close(); + char path[PATH_MAX] = ""; + if (m_file.find_first_of("/") != 0) { + getcwd(path, PATH_MAX); + m_file = std::string(path) + "/" + m_file; + } + return ret; }