/** * @RDE_HEADER@ * main for ServiceManagement * $Id$ * $HeadURL$ * $Author$ */ #include #include #include #include "main_handler.h" #include "logger.h" #ifndef WIN32 #include #include #include #endif // --------------------------------------------------------------- // Become a daemon // --------------------------------------------------------------- static void daemonize() { #ifndef WIN32 pid_t pid, sid; /* already a daemon */ if (getppid() == 1) return; /* Fork off the parent process */ pid = fork(); if (pid < 0) exit(EXIT_FAILURE); /* If we got a good PID, then we can exit the parent process. */ if (pid > 0) exit(EXIT_SUCCESS); /* 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(EXIT_FAILURE); /* Change the current working directory. This prevents the current directory from being locked; hence not being able to remove it. */ if ((chdir("/")) < 0) exit(EXIT_FAILURE); /* Redirect standard files to /dev/null */ freopen("/dev/null", "r", stdin); freopen("/dev/null", "w", stdout); freopen("/dev/null", "w", stderr); #endif } class CmdLineParser : public LtsCommandLineParser::LtsCommandLineParser { public: CmdLineParser() : LtsCommandLineParser(ProvideCommandLineParameterOptions(CreateDomainIdFromIp|DomainId|CustomConfigFile)) , m_defLicenceFile(QLatin1String("licence_file.crl")) {} bool daemonize() const { return isSet(QStringLiteral("z")); } QString licenceFile() const { return isSet(QLatin1String("l")) ? value(QLatin1String("l")) : m_defLicenceFile; } bool doShutdown() const { return isSet(QLatin1String("s")); } bool init() { addOption({{QStringLiteral("l"), QStringLiteral("licence_file")}, QStringLiteral("Default: \"") + m_defLicenceFile + QStringLiteral("\""), QStringLiteral("filename"), m_defLicenceFile }); addOption({{QStringLiteral("s"), QStringLiteral("shutdown")}, QStringLiteral("Shut down running service management") }); addOption({{QStringLiteral("z"), QStringLiteral("daemonize")}, QStringLiteral("Start in background") }); return (LtsCommandLineParser::init()); } private: QString m_defLicenceFile; }; int main(int argc, char **argv) { NGLSingleCoreApplication app(argc, argv); CmdLineParser options; if (!options.init()) return -1; app.createLogger("SCM", options.exerciseId()); if (options.doShutdown()) { if (app.isRunning()) { if (app.sendMessage(QStringLiteral("shutdown"))) std::cout << "Sent shutdown message to " << argv[0] << std::endl; else std::cerr << "Could not send shutdown message to " << argv[0] << std::endl; return 0; } std::cerr << argv[0] << " is not running - can not shutdown app" << std::endl; return -3; } // vor app.isRunning() da sonst die file-locks durcheinander geraten und ein shutdown mittels '-z' nicht mehr möglich ist if (options.daemonize()) daemonize(); if (app.isRunning()) { std::cerr << argv[0] << " is already running - can not be started twice!" << std::endl; return -2; } // relock lock file which was unlocked due to daemonizing WARNING_LOG(app.baseLogger()) << "app is running: " << app.isRunning(); int iRet = 0; { ServiceManagement::MainHandler mainHandler; if (!mainHandler.init(options.domainId(), options.licenceFile())) return -1; STARTUP_LOG(app.baseLogger()); iRet = app.exec(); SHUTDOWN_LOG(app.baseLogger()); } return iRet; }