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
+ int runlen = strlen(NSSM_RUN);\r
+ int 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
+ unsigned int len = strlen(exe);\r
+ unsigned int 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