* The entry point for the "daemon" CLI command. * * @returns An exit status. */
| 621 | * @returns An exit status. |
| 622 | */ |
| 623 | int DaemonCommand::Run(const po::variables_map& vm, [[maybe_unused]] const std::vector<std::string>& ap) const |
| 624 | { |
| 625 | #ifdef _WIN32 |
| 626 | SetConsoleOutputCP(65001); |
| 627 | #endif /* _WIN32 */ |
| 628 | |
| 629 | Logger::EnableTimestamp(); |
| 630 | |
| 631 | Log(LogInformation, "cli") |
| 632 | << "Icinga application loader (version: " << Application::GetAppVersion() |
| 633 | #ifdef I2_DEBUG |
| 634 | << "; debug" |
| 635 | #endif /* I2_DEBUG */ |
| 636 | << ")"; |
| 637 | |
| 638 | std::vector<std::string> configs; |
| 639 | if (vm.count("config") > 0) |
| 640 | configs = vm["config"].as<std::vector<std::string> >(); |
| 641 | else if (!vm.count("no-config")) { |
| 642 | /* The implicit string assignment is needed for Windows builds. */ |
| 643 | String configDir = Configuration::ConfigDir; |
| 644 | configs.push_back(configDir + "/icinga2.conf"); |
| 645 | } |
| 646 | |
| 647 | if (vm.count("dump-objects")) { |
| 648 | if (!vm.count("validate")) { |
| 649 | Log(LogCritical, "cli", "--dump-objects is not allowed without -C"); |
| 650 | return EXIT_FAILURE; |
| 651 | } |
| 652 | |
| 653 | l_ObjectsPath = Configuration::ObjectsPath; |
| 654 | } |
| 655 | |
| 656 | if (vm.count("validate")) { |
| 657 | Log(LogInformation, "cli", "Loading configuration file(s)."); |
| 658 | |
| 659 | std::vector<ConfigItem::Ptr> newItems; |
| 660 | |
| 661 | if (!DaemonUtility::LoadConfigFiles(configs, newItems, l_ObjectsPath, Configuration::VarsPath)) { |
| 662 | Log(LogCritical, "cli", "Config validation failed. Re-run with 'icinga2 daemon -C' after fixing the config."); |
| 663 | return EXIT_FAILURE; |
| 664 | } |
| 665 | |
| 666 | Log(LogInformation, "cli", "Finished validating the configuration file(s)."); |
| 667 | return EXIT_SUCCESS; |
| 668 | } |
| 669 | |
| 670 | { |
| 671 | pid_t runningpid = Application::ReadPidFile(Configuration::PidPath); |
| 672 | if (runningpid > 0) { |
| 673 | Log(LogCritical, "cli") |
| 674 | << "Another instance of Icinga already running with PID " << runningpid; |
| 675 | return EXIT_FAILURE; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | if (vm.count("daemonize")) { |
| 680 | // this subroutine either succeeds, or logs an error |
no test coverage detected