3 SERVICE_STATUS service_status;
\r
4 SERVICE_STATUS_HANDLE service_handle;
\r
7 static char service_name[MAX_PATH];
\r
9 char flags[MAX_PATH];
\r
12 static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY } exit_actions;
\r
13 static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", 0 };
\r
15 /* Connect to the service manager */
\r
16 SC_HANDLE open_service_manager() {
\r
17 SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
\r
19 eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Unable to connect to service manager!\nPerhaps you need to be an administrator...");
\r
26 /* About to install the service */
\r
27 int pre_install_service(int argc, char **argv) {
\r
28 /* Show the dialogue box if we didn't give the */
\r
29 if (argc < 2) return nssm_gui(IDD_INSTALL, argv[0]);
\r
31 /* Arguments are optional */
\r
33 if (argc == 2) flags = "";
\r
34 else flags = argv[2];
\r
36 return install_service(argv[0], argv[1], flags);
\r
39 /* About to remove the service */
\r
40 int pre_remove_service(int argc, char **argv) {
\r
41 /* Show dialogue box if we didn't pass service name and "confirm" */
\r
42 if (argc < 2) return nssm_gui(IDD_REMOVE, argv[0]);
\r
43 if (str_equiv(argv[1], "confirm")) return remove_service(argv[0]);
\r
44 fprintf(stderr, "To remove a service without confirmation: nssm remove <servicename> confirm\n");
\r
48 /* Install the service */
\r
49 int install_service(char *name, char *exe, char *flags) {
\r
50 /* Open service manager */
\r
51 SC_HANDLE services = open_service_manager();
\r
53 fprintf(stderr, "Error opening service manager!\n");
\r
57 /* Get path of this program */
\r
58 char path[MAX_PATH];
\r
59 GetModuleFileName(0, path, MAX_PATH);
\r
61 /* Construct command */
\r
62 char command[MAX_PATH];
\r
63 size_t runlen = strlen(NSSM_RUN);
\r
64 size_t pathlen = strlen(path);
\r
65 if (pathlen + runlen + 2 >= MAX_PATH) {
\r
66 fprintf(stderr, "The full path to " NSSM " is too long!\n");
\r
69 if (snprintf(command, sizeof(command), "\"%s\" %s", path, NSSM_RUN) < 0) {
\r
70 fprintf(stderr, "Out of memory for ImagePath!\n");
\r
74 /* Work out directory name */
\r
75 size_t len = strlen(exe);
\r
77 for (i = len; i && exe[i] != '\\' && exe[i] != '/'; i--);
\r
79 memmove(dir, exe, i);
\r
82 /* Create the service */
\r
83 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
85 fprintf(stderr, "Error creating service!\n");
\r
86 CloseServiceHandle(services);
\r
90 /* Now we need to put the parameters into the registry */
\r
91 if (create_parameters(name, exe, flags, dir)) {
\r
92 fprintf(stderr, "Error setting startup parameters for the service!\n");
\r
93 DeleteService(service);
\r
94 CloseServiceHandle(services);
\r
99 CloseServiceHandle(service);
\r
100 CloseServiceHandle(services);
\r
102 printf("Service \"%s\" installed successfully!\n", name);
\r
106 /* Remove the service */
\r
107 int remove_service(char *name) {
\r
108 /* Open service manager */
\r
109 SC_HANDLE services = open_service_manager();
\r
111 fprintf(stderr, "Error opening service manager!\n");
\r
115 /* Try to open the service */
\r
116 SC_HANDLE service = OpenService(services, name, SC_MANAGER_ALL_ACCESS);
\r
118 fprintf(stderr, "Can't open service!");
\r
119 CloseServiceHandle(services);
\r
123 /* Try to delete the service */
\r
124 if (! DeleteService(service)) {
\r
125 fprintf(stderr, "Error deleting service!\n");
\r
126 CloseServiceHandle(service);
\r
127 CloseServiceHandle(services);
\r
132 CloseServiceHandle(service);
\r
133 CloseServiceHandle(services);
\r
135 printf("Service \"%s\" removed successfully!\n", name);
\r
139 /* Service initialisation */
\r
140 void WINAPI service_main(unsigned long argc, char **argv) {
\r
141 if (_snprintf(service_name, sizeof(service_name), "%s", argv[0]) < 0) {
\r
142 eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "service_main(): Out of memory for service_name!");
\r
146 /* Initialise status */
\r
147 ZeroMemory(&service_status, sizeof(service_status));
\r
148 service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;
\r
149 service_status.dwCurrentState = SERVICE_RUNNING;
\r
150 service_status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
\r
151 service_status.dwWin32ExitCode = NO_ERROR;
\r
152 service_status.dwServiceSpecificExitCode = 0;
\r
153 service_status.dwCheckPoint = 0;
\r
154 service_status.dwWaitHint = 1000;
\r
156 /* Signal we AREN'T running the server */
\r
159 /* Get startup parameters */
\r
160 int ret = get_parameters(argv[0], exe, sizeof(exe), flags, sizeof(flags), dir, sizeof(dir));
\r
162 eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "service_main(): Can't get startup parameters: error %d", ret);
\r
166 /* Register control handler */
\r
167 service_handle = RegisterServiceCtrlHandlerEx(NSSM, service_control_handler, 0);
\r
168 if (! service_handle) {
\r
169 eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "service_main(): RegisterServiceCtrlHandlerEx() failed: %s", error_string(GetLastError()));
\r
173 /* Try to create the exit action parameters; we don't care if it fails */
\r
174 create_exit_action(argv[0], exit_action_strings[0]);
\r
179 int monitor_service() {
\r
180 /* Set service status to started */
\r
181 int ret = start_service();
\r
183 eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't start service %s: error code %d", service_name, ret);
\r
186 eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Started process %s %s in %s for service %s", exe, flags, dir, service_name);
\r
188 /* Monitor service service */
\r
189 if (! RegisterWaitForSingleObject(&wait_handle, pid, end_service, 0, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION)) {
\r
190 eventprintf(EVENTLOG_WARNING_TYPE, NSSM_MESSAGE_DEFAULT, "RegisterWaitForSingleObject() returned %s - service may claim to be still running when %s exits ", error_string(GetLastError()), exe);
\r
196 /* Service control handler */
\r
197 unsigned long WINAPI service_control_handler(unsigned long control, unsigned long event, void *data, void *context) {
\r
199 case SERVICE_CONTROL_SHUTDOWN:
\r
200 case SERVICE_CONTROL_STOP:
\r
205 /* Unknown control */
\r
206 return ERROR_CALL_NOT_IMPLEMENTED;
\r
209 /* Start the service */
\r
210 int start_service() {
\r
213 /* Allocate a STARTUPINFO structure for a new process */
\r
215 ZeroMemory(&si, sizeof(si));
\r
216 si.cb = sizeof(si);
\r
218 /* Allocate a PROCESSINFO structure for the process */
\r
219 PROCESS_INFORMATION pi;
\r
220 ZeroMemory(&pi, sizeof(pi));
\r
222 /* Launch executable with arguments */
\r
223 char cmd[MAX_PATH];
\r
224 if (_snprintf(cmd, sizeof(cmd), "%s %s", exe, flags) < 0) {
\r
225 eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Error constructing command line");
\r
226 return stop_service(2);
\r
228 if (! CreateProcess(0, cmd, 0, 0, 0, 0, 0, dir, &si, &pi)) {
\r
229 eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't launch %s. CreateProcess() returned %s", exe, error_string(GetLastError()));
\r
230 return stop_service(3);
\r
234 /* Signal successful start */
\r
235 service_status.dwCurrentState = SERVICE_RUNNING;
\r
236 SetServiceStatus(service_handle, &service_status);
\r
241 /* Stop the service */
\r
242 int stop_service(unsigned long exitcode) {
\r
243 /* Signal we are stopping */
\r
244 service_status.dwCurrentState = SERVICE_STOP_PENDING;
\r
245 SetServiceStatus(service_handle, &service_status);
\r
247 /* Nothing to do if server isn't running */
\r
249 /* Shut down server */
\r
250 TerminateProcess(pid, 0);
\r
254 /* Signal we stopped */
\r
255 service_status.dwCurrentState = SERVICE_STOPPED;
\r
257 service_status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
\r
258 service_status.dwServiceSpecificExitCode = exitcode;
\r
261 service_status.dwWin32ExitCode = NO_ERROR;
\r
262 service_status.dwServiceSpecificExitCode = 0;
\r
264 SetServiceStatus(service_handle, &service_status);
\r
269 /* Callback function triggered when the server exits */
\r
270 void CALLBACK end_service(void *arg, unsigned char why) {
\r
271 /* Check exit code */
\r
272 unsigned long ret = 0;
\r
273 GetExitCodeProcess(pid, &ret);
\r
275 eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Process %s for service %s exited with return code %u", exe, service_name, ret);
\r
277 /* What action should we take? */
\r
278 int action = NSSM_EXIT_RESTART;
\r
279 unsigned char action_string[ACTION_LEN];
\r
280 if (! get_exit_action(service_name, &ret, action_string)) {
\r
281 for (int i = 0; exit_action_strings[i]; i++) {
\r
282 if (! _strnicmp((const char *) action_string, exit_action_strings[i], ACTION_LEN)) {
\r
291 /* Try to restart the service or return failure code to service manager */
\r
292 case NSSM_EXIT_RESTART:
\r
293 eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Action for exit code %lu is %s: Attempting to restart %s for service %s", ret, exit_action_strings[action], exe, service_name);
\r
294 while (monitor_service()) {
\r
295 eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Failed to restart %s - sleeping ", exe, ret);
\r
300 /* Do nothing, just like srvany would */
\r
301 case NSSM_EXIT_IGNORE:
\r
302 eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Action for exit code %lu is %s: Not attempting to restart %s for service %s", ret, exit_action_strings[action], exe, service_name);
\r
306 /* Tell the service manager we are finished */
\r
307 case NSSM_EXIT_REALLY:
\r
308 eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Action for exit code %lu is %s: Stopping service %s", ret, exit_action_strings[action], service_name);
\r