SERVICE_STATUS_HANDLE service_handle;\r
HANDLE wait_handle;\r
HANDLE pid;\r
+static char service_name[MAX_PATH];\r
char exe[MAX_PATH];\r
char flags[MAX_PATH];\r
char dir[MAX_PATH];\r
\r
+static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY } exit_actions;\r
+static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", 0 };\r
+\r
/* Connect to the service manager */\r
SC_HANDLE open_service_manager() {\r
SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);\r
return ret;\r
}\r
\r
+/* About to install the service */\r
+int pre_install_service(int argc, char **argv) {\r
+ /* Show the dialogue box if we didn't give the */\r
+ if (argc < 2) return nssm_gui(IDD_INSTALL, argv[0]);\r
+\r
+ /* Arguments are optional */\r
+ char *flags;\r
+ if (argc == 2) flags = "";\r
+ else flags = argv[2];\r
+\r
+ return install_service(argv[0], argv[1], flags);\r
+}\r
+\r
+/* About to remove the service */\r
+int pre_remove_service(int argc, char **argv) {\r
+ /* Show dialogue box if we didn't pass service name and "confirm" */\r
+ if (argc < 2) return nssm_gui(IDD_REMOVE, argv[0]);\r
+ if (str_equiv(argv[1], "confirm")) return remove_service(argv[0]);\r
+ fprintf(stderr, "To remove a service without confirmation: nssm remove <servicename> confirm\n");\r
+ return 100;\r
+}\r
+\r
/* Install the service */\r
-int install_service(char *name) {\r
-#ifdef GUI\r
- /* Show the dialogue box */\r
- return nssm_gui(IDD_INSTALL, name);\r
-#else\r
- fprintf(stderr, "Unimplemented\n");\r
- return 1;\r
-#endif\r
+int install_service(char *name, char *exe, char *flags) {\r
+ /* Open service manager */\r
+ SC_HANDLE services = open_service_manager();\r
+ if (! services) {\r
+ fprintf(stderr, "Error opening service manager!\n");\r
+ return 2;\r
+ }\r
+ \r
+ /* Get path of this program */\r
+ char path[MAX_PATH];\r
+ GetModuleFileName(0, path, MAX_PATH);\r
+\r
+ /* Construct command */\r
+ char command[MAX_PATH];\r
+ size_t runlen = strlen(NSSM_RUN);\r
+ size_t pathlen = strlen(path);\r
+ if (pathlen + runlen + 2 >= MAX_PATH) {\r
+ fprintf(stderr, "The full path to " NSSM " is too long!\n");\r
+ return 3;\r
+ }\r
+ if (snprintf(command, sizeof(command), "\"%s\" %s", path, NSSM_RUN) < 0) {\r
+ fprintf(stderr, "Out of memory for ImagePath!\n");\r
+ return 4;\r
+ }\r
+\r
+ /* Work out directory name */\r
+ size_t len = strlen(exe);\r
+ size_t i;\r
+ for (i = len; i && exe[i] != '\\' && exe[i] != '/'; i--);\r
+ char dir[MAX_PATH];\r
+ memmove(dir, exe, i);\r
+ dir[i] = '\0';\r
+\r
+ /* Create the service */\r
+ 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);\r
+ if (! service) {\r
+ fprintf(stderr, "Error creating service!\n");\r
+ CloseServiceHandle(services);\r
+ return 5;\r
+ }\r
+\r
+ /* Now we need to put the parameters into the registry */\r
+ if (create_parameters(name, exe, flags, dir)) {\r
+ fprintf(stderr, "Error setting startup parameters for the service!\n");\r
+ DeleteService(service);\r
+ CloseServiceHandle(services);\r
+ return 6;\r
+ }\r
+\r
+ /* Cleanup */\r
+ CloseServiceHandle(service);\r
+ CloseServiceHandle(services);\r
+\r
+ printf("Service \"%s\" installed successfully!\n", name);\r
+ return 0;\r
}\r
\r
/* Remove the service */\r
int remove_service(char *name) {\r
-#ifdef GUI\r
- return nssm_gui(IDD_REMOVE, name);\r
-#else\r
- fprintf(stderr, "Unimplemented\n");\r
- return 1;\r
-#endif\r
+ /* Open service manager */\r
+ SC_HANDLE services = open_service_manager();\r
+ if (! services) {\r
+ fprintf(stderr, "Error opening service manager!\n");\r
+ return 2;\r
+ }\r
+ \r
+ /* Try to open the service */\r
+ SC_HANDLE service = OpenService(services, name, SC_MANAGER_ALL_ACCESS);\r
+ if (! service) {\r
+ fprintf(stderr, "Can't open service!");\r
+ CloseServiceHandle(services);\r
+ return 3;\r
+ }\r
+\r
+ /* Try to delete the service */\r
+ if (! DeleteService(service)) {\r
+ fprintf(stderr, "Error deleting service!\n");\r
+ CloseServiceHandle(service);\r
+ CloseServiceHandle(services);\r
+ return 4;\r
+ }\r
+\r
+ /* Cleanup */\r
+ CloseServiceHandle(service);\r
+ CloseServiceHandle(services);\r
+\r
+ printf("Service \"%s\" removed successfully!\n", name);\r
+ return 0;\r
}\r
\r
/* Service initialisation */\r
void WINAPI service_main(unsigned long argc, char **argv) {\r
+ if (_snprintf(service_name, sizeof(service_name), "%s", argv[0]) < 0) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "service_main(): Out of memory for service_name!");\r
+ return;\r
+ }\r
+\r
/* Initialise status */\r
ZeroMemory(&service_status, sizeof(service_status));\r
service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;\r
return;\r
}\r
\r
+ /* Try to create the exit action parameters; we don't care if it fails */\r
+ create_exit_action(argv[0], exit_action_strings[0]);\r
+\r
monitor_service();\r
}\r
\r
/* Set service status to started */\r
int ret = start_service();\r
if (ret) {\r
- eventprintf(EVENTLOG_ERROR_TYPE, "Can't start service: error code %d", ret);\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't start service %s: error code %d", service_name, ret);\r
return ret;\r
}\r
- eventprintf(EVENTLOG_INFORMATION_TYPE, "Started process %s %s in %s", exe, flags, dir);\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Started process %s %s in %s for service %s", exe, flags, dir, service_name);\r
\r
/* Monitor service service */\r
if (! RegisterWaitForSingleObject(&wait_handle, pid, end_service, 0, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION)) {\r
unsigned long ret = 0;\r
GetExitCodeProcess(pid, &ret);\r
\r
- /* Force an error code if none given, so system can restart this service */\r
- /*if (! ret) {\r
- eventprintf(EVENTLOG_INFORMATION_TYPE, "Process exited with return code 0 - overriding with return code 111 so the service manager will notice the failure");\r
- ret = 111;\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Process %s for service %s exited with return code %u", exe, service_name, ret);\r
+\r
+ /* What action should we take? */\r
+ int action = NSSM_EXIT_RESTART;\r
+ unsigned char action_string[ACTION_LEN];\r
+ if (! get_exit_action(service_name, &ret, action_string)) {\r
+ for (int i = 0; exit_action_strings[i]; i++) {\r
+ if (! _strnicmp((const char *) action_string, exit_action_strings[i], ACTION_LEN)) {\r
+ action = i;\r
+ break;\r
+ }\r
+ }\r
}\r
- else */eventprintf(EVENTLOG_INFORMATION_TYPE, "Process %s exited with return code %u", exe, ret);\r
\r
- /* Try to restart the service or return failure code to service manager */\r
pid = 0;\r
- while (monitor_service()) {\r
- eventprintf(EVENTLOG_INFORMATION_TYPE, "Failed to restart %s - sleeping ", exe, ret);\r
- Sleep(30000);\r
+ switch (action) {\r
+ /* Try to restart the service or return failure code to service manager */\r
+ case NSSM_EXIT_RESTART:\r
+ 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);\r
+ while (monitor_service()) {\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Failed to restart %s - sleeping ", exe, ret);\r
+ Sleep(30000);\r
+ }\r
+ break;\r
+\r
+ /* Do nothing, just like srvany would */\r
+ case NSSM_EXIT_IGNORE:\r
+ 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);\r
+ Sleep(INFINITE);\r
+ break;\r
+\r
+ /* Tell the service manager we are finished */\r
+ case NSSM_EXIT_REALLY:\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Action for exit code %lu is %s: Stopping service %s", ret, exit_action_strings[action], service_name);\r
+ stop_service(ret);\r
+ break;\r
}\r
}\r