| 684 | |
| 685 | #ifdef _WIN32 |
| 686 | static int SetupService(bool install, int argc, char **argv) |
| 687 | { |
| 688 | SC_HANDLE schSCManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_ALL_ACCESS); |
| 689 | |
| 690 | if (!schSCManager) { |
| 691 | printf("OpenSCManager failed (%d)\n", GetLastError()); |
| 692 | return 1; |
| 693 | } |
| 694 | |
| 695 | TCHAR szPath[MAX_PATH]; |
| 696 | |
| 697 | if (!GetModuleFileName(nullptr, szPath, MAX_PATH)) { |
| 698 | printf("Cannot install service (%d)\n", GetLastError()); |
| 699 | return 1; |
| 700 | } |
| 701 | |
| 702 | String szArgs; |
| 703 | szArgs = Utility::EscapeShellArg(szPath) + " --scm"; |
| 704 | |
| 705 | std::string scmUser = "NT AUTHORITY\\NetworkService"; |
| 706 | std::ifstream initf(Utility::GetIcingaDataPath() + "\\etc\\icinga2\\user"); |
| 707 | if (initf.good()) { |
| 708 | std::getline(initf, scmUser); |
| 709 | } |
| 710 | initf.close(); |
| 711 | |
| 712 | for (int i = 0; i < argc; i++) { |
| 713 | if (!strcmp(argv[i], "--scm-user") && i + 1 < argc) { |
| 714 | scmUser = argv[i + 1]; |
| 715 | i++; |
| 716 | } else |
| 717 | szArgs += " " + Utility::EscapeShellArg(argv[i]); |
| 718 | } |
| 719 | |
| 720 | SC_HANDLE schService = OpenService(schSCManager, "icinga2", SERVICE_ALL_ACCESS); |
| 721 | |
| 722 | if (schService) { |
| 723 | SERVICE_STATUS status; |
| 724 | ControlService(schService, SERVICE_CONTROL_STOP, &status); |
| 725 | |
| 726 | double start = Utility::GetTime(); |
| 727 | while (status.dwCurrentState != SERVICE_STOPPED) { |
| 728 | double end = Utility::GetTime(); |
| 729 | |
| 730 | if (end - start > 30) { |
| 731 | printf("Could not stop the service.\n"); |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | Utility::Sleep(5); |
| 736 | |
| 737 | if (!QueryServiceStatus(schService, &status)) { |
| 738 | printf("QueryServiceStatus failed (%d)\n", GetLastError()); |
| 739 | return 1; |
| 740 | } |
| 741 | } |
| 742 | } else if (install) { |
| 743 | schService = CreateService( |
no test coverage detected