X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=4dc6fa2da4e2dd872b61a1bf133afc2e52ad230c;hb=c425384408cb392f23cbef66d385e2e589a5e516;hp=9dbb310523b450c956b72c526a1d689ee50b76c2;hpb=d007ff22cecf2ce85cc68a1de81a010d4a65badc;p=nssm.git diff --git a/service.cpp b/service.cpp index 9dbb310..4dc6fa2 100644 --- a/service.cpp +++ b/service.cpp @@ -4,10 +4,14 @@ SERVICE_STATUS service_status; SERVICE_STATUS_HANDLE service_handle; HANDLE wait_handle; HANDLE pid; +static char service_name[MAX_PATH]; char exe[MAX_PATH]; char flags[MAX_PATH]; char dir[MAX_PATH]; +static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY } exit_actions; +static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", 0 }; + /* Connect to the service manager */ SC_HANDLE open_service_manager() { SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS); @@ -19,29 +23,126 @@ SC_HANDLE open_service_manager() { return ret; } +/* About to install the service */ +int pre_install_service(int argc, char **argv) { + /* Show the dialogue box if we didn't give the */ + if (argc < 2) return nssm_gui(IDD_INSTALL, argv[0]); + + /* Arguments are optional */ + char *flags; + if (argc == 2) flags = ""; + else flags = argv[2]; + + return install_service(argv[0], argv[1], flags); +} + +/* About to remove the service */ +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"); + return 100; +} + /* Install the service */ -int install_service(char *name) { -#ifdef GUI - /* Show the dialogue box */ - return nssm_gui(IDD_INSTALL, name); -#else - fprintf(stderr, "Unimplemented\n"); - return 1; -#endif +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"); + return 2; + } + + /* Get path of this program */ + char path[MAX_PATH]; + GetModuleFileName(0, path, MAX_PATH); + + /* Construct command */ + char command[MAX_PATH]; + size_t runlen = strlen(NSSM_RUN); + size_t pathlen = strlen(path); + if (pathlen + runlen + 2 >= MAX_PATH) { + fprintf(stderr, "The full path to " NSSM " is too long!\n"); + return 3; + } + if (snprintf(command, sizeof(command), "\"%s\" %s", path, NSSM_RUN) < 0) { + fprintf(stderr, "Out of memory for ImagePath!\n"); + return 4; + } + + /* Work out directory name */ + size_t len = strlen(exe); + size_t i; + for (i = len; i && exe[i] != '\\' && exe[i] != '/'; i--); + char dir[MAX_PATH]; + memmove(dir, exe, i); + dir[i] = '\0'; + + /* 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"); + 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"); + DeleteService(service); + CloseServiceHandle(services); + return 6; + } + + /* Cleanup */ + CloseServiceHandle(service); + CloseServiceHandle(services); + + printf("Service \"%s\" installed successfully!\n", name); + return 0; } /* Remove the service */ int remove_service(char *name) { -#ifdef GUI - return nssm_gui(IDD_REMOVE, name); -#else - fprintf(stderr, "Unimplemented\n"); - return 1; -#endif + /* Open service manager */ + SC_HANDLE services = open_service_manager(); + if (! services) { + fprintf(stderr, "Error opening service manager!\n"); + 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!"); + CloseServiceHandle(services); + return 3; + } + + /* Try to delete the service */ + if (! DeleteService(service)) { + fprintf(stderr, "Error deleting service!\n"); + CloseServiceHandle(service); + CloseServiceHandle(services); + return 4; + } + + /* Cleanup */ + CloseServiceHandle(service); + CloseServiceHandle(services); + + printf("Service \"%s\" removed successfully!\n", name); + return 0; } /* Service initialisation */ void WINAPI service_main(unsigned long argc, char **argv) { + if (_snprintf(service_name, sizeof(service_name), "%s", argv[0]) < 0) { + eventprintf(EVENTLOG_ERROR_TYPE, "service_main(): Out of memory for service_name!"); + return; + } + /* Initialise status */ ZeroMemory(&service_status, sizeof(service_status)); service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS; @@ -69,6 +170,9 @@ void WINAPI service_main(unsigned long argc, char **argv) { return; } + /* Try to create the exit action parameters; we don't care if it fails */ + create_exit_action(argv[0], exit_action_strings[0]); + monitor_service(); } @@ -76,10 +180,10 @@ int monitor_service() { /* Set service status to started */ int ret = start_service(); if (ret) { - eventprintf(EVENTLOG_ERROR_TYPE, "Can't start service: error code %d", ret); + eventprintf(EVENTLOG_ERROR_TYPE, "Can't start service %s: error code %d", service_name, ret); return ret; } - eventprintf(EVENTLOG_INFORMATION_TYPE, "Started process %s %s in %s", exe, flags, dir); + eventprintf(EVENTLOG_INFORMATION_TYPE, "Started process %s %s in %s for service %s", exe, flags, dir, service_name); /* Monitor service service */ if (! RegisterWaitForSingleObject(&wait_handle, pid, end_service, 0, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION)) { @@ -168,17 +272,41 @@ void CALLBACK end_service(void *arg, unsigned char why) { unsigned long ret = 0; GetExitCodeProcess(pid, &ret); - /* Force an error code if none given, so system can restart this service */ - /*if (! ret) { - eventprintf(EVENTLOG_INFORMATION_TYPE, "Process exited with return code 0 - overriding with return code 111 so the service manager will notice the failure"); - ret = 111; + eventprintf(EVENTLOG_INFORMATION_TYPE, "Process %s for service %s exited with return code %u", exe, service_name, ret); + + /* What action should we take? */ + int action = NSSM_EXIT_RESTART; + unsigned char action_string[ACTION_LEN]; + if (! get_exit_action(service_name, &ret, action_string)) { + for (int i = 0; exit_action_strings[i]; i++) { + if (! _strnicmp((const char *) action_string, exit_action_strings[i], ACTION_LEN)) { + action = i; + break; + } + } } - else */eventprintf(EVENTLOG_INFORMATION_TYPE, "Process %s exited with return code %u", exe, ret); - /* Try to restart the service or return failure code to service manager */ pid = 0; - while (monitor_service()) { - eventprintf(EVENTLOG_INFORMATION_TYPE, "Failed to restart %s - sleeping ", exe, ret); - Sleep(30000); + switch (action) { + /* Try to restart the service or return failure code to service manager */ + case NSSM_EXIT_RESTART: + eventprintf(EVENTLOG_INFORMATION_TYPE, "Action for exit code %lu is %s: Attempting to restart %s for service %s", ret, exit_action_strings[action], exe, service_name); + while (monitor_service()) { + eventprintf(EVENTLOG_INFORMATION_TYPE, "Failed to restart %s - sleeping ", exe, ret); + Sleep(30000); + } + break; + + /* Do nothing, just like srvany would */ + case NSSM_EXIT_IGNORE: + eventprintf(EVENTLOG_INFORMATION_TYPE, "Action for exit code %lu is %s: Not attempting to restart %s for service %s", ret, exit_action_strings[action], exe, service_name); + Sleep(INFINITE); + break; + + /* Tell the service manager we are finished */ + case NSSM_EXIT_REALLY: + eventprintf(EVENTLOG_INFORMATION_TYPE, "Action for exit code %lu is %s: Stopping service %s", ret, exit_action_strings[action], service_name); + stop_service(ret); + break; } }