X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=0c2368e7d37d29932f0586e4e780a59348eafd34;hb=5e7f7c21e254ba722d5d0d08c154792b82cf3f1d;hp=7e9a9d7d215ae4e6e21d6fc535331851fc7d7c91;hpb=39d56be3b1cbecfb903617a63355b57f61424a73;p=nssm.git diff --git a/service.cpp b/service.cpp index 7e9a9d7..0c2368e 100644 --- a/service.cpp +++ b/service.cpp @@ -1,5 +1,6 @@ #include "nssm.h" +bool is_admin; SERVICE_STATUS service_status; SERVICE_STATUS_HANDLE service_handle; HANDLE process_handle; @@ -29,7 +30,7 @@ static inline int throttle_milliseconds() { SC_HANDLE open_service_manager() { SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS); if (! ret) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENSCMANAGER_FAILED, 0); + if (is_admin) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENSCMANAGER_FAILED, 0); return 0; } @@ -38,7 +39,7 @@ SC_HANDLE open_service_manager() { /* About to install the service */ int pre_install_service(int argc, char **argv) { - /* Show the dialogue box if we didn't give the */ + /* Show the dialogue box if we didn't give the service name and path */ if (argc < 2) return nssm_gui(IDD_INSTALL, argv[0]); /* Arguments are optional */ @@ -77,7 +78,7 @@ int pre_remove_service(int argc, char **argv) { /* Show dialogue box if we didn't pass service name and "confirm" */ if (argc < 2) return nssm_gui(IDD_REMOVE, argv[0]); if (str_equiv(argv[1], "confirm")) return remove_service(argv[0]); - fprintf(stderr, "To remove a service without confirmation: nssm remove confirm\n"); + print_message(stderr, NSSM_MESSAGE_PRE_REMOVE_SERVICE); return 100; } @@ -86,7 +87,7 @@ int install_service(char *name, char *exe, char *flags) { /* Open service manager */ SC_HANDLE services = open_service_manager(); if (! services) { - fprintf(stderr, "Error opening service manager!\n"); + print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED); return 2; } @@ -98,11 +99,11 @@ int install_service(char *name, char *exe, char *flags) { char command[CMD_LENGTH]; size_t pathlen = strlen(path); if (pathlen + 1 >= VALUE_LENGTH) { - fprintf(stderr, "The full path to " NSSM " is too long!\n"); + print_message(stderr, NSSM_MESSAGE_PATH_TOO_LONG, NSSM); return 3; } if (_snprintf(command, sizeof(command), "\"%s\"", path) < 0) { - fprintf(stderr, "Out of memory for ImagePath!\n"); + print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY_FOR_IMAGEPATH); return 4; } @@ -117,24 +118,26 @@ int install_service(char *name, char *exe, char *flags) { /* Create the service */ SC_HANDLE service = CreateService(services, name, name, SC_MANAGER_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, command, 0, 0, 0, 0, 0); if (! service) { - fprintf(stderr, "Error creating service!\n"); + print_message(stderr, NSSM_MESSAGE_CREATESERVICE_FAILED); CloseServiceHandle(services); return 5; } /* Now we need to put the parameters into the registry */ if (create_parameters(name, exe, flags, dir)) { - fprintf(stderr, "Error setting startup parameters for the service!\n"); + print_message(stderr, NSSM_MESSAGE_CREATE_PARAMETERS_FAILED); DeleteService(service); CloseServiceHandle(services); return 6; } + set_service_recovery(service, name); + /* Cleanup */ CloseServiceHandle(service); CloseServiceHandle(services); - printf("Service \"%s\" installed successfully!\n", name); + print_message(stdout, NSSM_MESSAGE_SERVICE_INSTALLED, name); return 0; } @@ -143,21 +146,21 @@ int remove_service(char *name) { /* Open service manager */ SC_HANDLE services = open_service_manager(); if (! services) { - fprintf(stderr, "Error opening service manager!\n"); + print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED); return 2; } /* Try to open the service */ SC_HANDLE service = OpenService(services, name, SC_MANAGER_ALL_ACCESS); if (! service) { - fprintf(stderr, "Can't open service!"); + print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED); CloseServiceHandle(services); return 3; } /* Try to delete the service */ if (! DeleteService(service)) { - fprintf(stderr, "Error deleting service!\n"); + print_message(stderr, NSSM_MESSAGE_DELETESERVICE_FAILED); CloseServiceHandle(service); CloseServiceHandle(services); return 4; @@ -167,7 +170,7 @@ int remove_service(char *name) { CloseServiceHandle(service); CloseServiceHandle(services); - printf("Service \"%s\" removed successfully!\n", name); + print_message(stdout, NSSM_MESSAGE_SERVICE_REMOVED, name); return 0; } @@ -204,10 +207,12 @@ void WINAPI service_main(unsigned long argc, char **argv) { service_status.dwWaitHint = throttle_delay + NSSM_WAITHINT_MARGIN; SetServiceStatus(service_handle, &service_status); - /* Try to create the exit action parameters; we don't care if it fails */ - create_exit_action(argv[0], exit_action_strings[0]); + if (is_admin) { + /* Try to create the exit action parameters; we don't care if it fails */ + create_exit_action(argv[0], exit_action_strings[0]); - set_service_recovery(service_name); + set_service_recovery(0, service_name); + } /* Used for signalling a resume if the service pauses when throttled. */ throttle_timer = CreateWaitableTimer(0, 1, 0); @@ -219,12 +224,16 @@ void WINAPI service_main(unsigned long argc, char **argv) { } /* Make sure service recovery actions are taken where necessary */ -void set_service_recovery(char *service_name) { - SC_HANDLE services = open_service_manager(); - if (! services) return; +void set_service_recovery(SC_HANDLE service, char *service_name) { + SC_HANDLE services = 0; - SC_HANDLE service = OpenService(services, service_name, SC_MANAGER_ALL_ACCESS); - if (! service) return; + if (! service) { + services = open_service_manager(); + if (! services) return; + + service = OpenService(services, service_name, SC_MANAGER_ALL_ACCESS); + if (! service) return; + } SERVICE_FAILURE_ACTIONS_FLAG flag; ZeroMemory(&flag, sizeof(flag)); @@ -238,6 +247,11 @@ void set_service_recovery(char *service_name) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CHANGESERVICECONFIG2_FAILED, service_name, error_string(error), 0); } } + + if (services) { + CloseServiceHandle(service); + CloseServiceHandle(services); + } } int monitor_service() { @@ -439,8 +453,18 @@ void CALLBACK end_service(void *arg, unsigned char why) { /* Check exit code */ unsigned long exitcode = 0; + char code[16]; GetExitCodeProcess(process_handle, &exitcode); + /* + Log that the service ended BEFORE logging about killing the process + tree. See below for the possible values of the why argument. + */ + if (! why) { + _snprintf(code, sizeof(code), "%d", exitcode); + log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ENDED_SERVICE, exe, service_name, code, 0); + } + /* Clean up. */ kill_process_tree(service_name, pid, exitcode, pid); @@ -452,10 +476,6 @@ void CALLBACK end_service(void *arg, unsigned char why) { */ if (why) return; - char code[16]; - _snprintf(code, sizeof(code), "%d", exitcode); - log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ENDED_SERVICE, exe, service_name, code, 0); - /* What action should we take? */ int action = NSSM_EXIT_RESTART; unsigned char action_string[ACTION_LEN];