Ensure we call free_imports() when exiting.
[nssm.git] / service.cpp
index 20ebcd9..d66b916 100644 (file)
@@ -83,10 +83,11 @@ static inline int service_control_response(unsigned long control, unsigned long
   return 0;\r
 }\r
 \r
-static inline int await_service_control_response(unsigned long control, SC_HANDLE service_handle, SERVICE_STATUS *service_status, unsigned long initial_status) {\r
+static inline int await_service_control_response(unsigned long control, SC_HANDLE service_handle, SERVICE_STATUS *service_status, unsigned long initial_status, unsigned long cutoff) {\r
   int tries = 0;\r
   unsigned long checkpoint = 0;\r
   unsigned long waithint = 0;\r
+  unsigned long waited = 0;\r
   while (QueryServiceStatus(service_handle, service_status)) {\r
     int response = service_control_response(control, service_status->dwCurrentState);\r
     /* Alas we can't WaitForSingleObject() on an SC_HANDLE. */\r
@@ -96,13 +97,22 @@ static inline int await_service_control_response(unsigned long control, SC_HANDL
       checkpoint = service_status->dwCheckPoint;\r
       waithint = service_status->dwWaitHint;\r
       if (++tries > 10) tries = 10;\r
-      Sleep(50 * tries);\r
+      unsigned long wait = 50 * tries;\r
+      if (cutoff) {\r
+        if (waited > cutoff) return response;\r
+        waited += wait;\r
+      }\r
+      Sleep(wait);\r
     }\r
     else return response;\r
   }\r
   return -1;\r
 }\r
 \r
+static inline int await_service_control_response(unsigned long control, SC_HANDLE service_handle, SERVICE_STATUS *service_status, unsigned long initial_status) {\r
+  return await_service_control_response(control, service_handle, service_status, initial_status, 0);\r
+}\r
+\r
 static inline void wait_for_hooks(nssm_service_t *service, bool notify) {\r
   SERVICE_STATUS_HANDLE status_handle;\r
   SERVICE_STATUS *status;\r
@@ -1274,19 +1284,33 @@ int edit_service(nssm_service_t *service, bool editing) {
   TCHAR *username = 0;\r
   TCHAR *canon = 0;\r
   TCHAR *password = 0;\r
+  boolean virtual_account = false;\r
   if (service->usernamelen) {\r
     username = service->username;\r
-    if (canonicalise_username(username, &canon)) return 5;\r
-    if (service->passwordlen) password = service->password;\r
+    if (is_virtual_account(service->name, username)) {\r
+      virtual_account = true;\r
+      canon = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (service->usernamelen + 1) * sizeof(TCHAR));\r
+      if (! canon) {\r
+        print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("edit_service()"));\r
+        return 5;\r
+      }\r
+      memmove(canon, username, (service->usernamelen + 1) * sizeof(TCHAR));\r
+    }\r
+    else {\r
+      if (canonicalise_username(username, &canon)) return 5;\r
+      if (service->passwordlen) password = service->password;\r
+    }\r
   }\r
   else if (editing) username = canon = NSSM_LOCALSYSTEM_ACCOUNT;\r
 \r
-  if (well_known_username(canon)) password = _T("");\r
-  else {\r
-    if (grant_logon_as_service(canon)) {\r
-      if (canon != username) HeapFree(GetProcessHeap(), 0, canon);\r
-      print_message(stderr, NSSM_MESSAGE_GRANT_LOGON_AS_SERVICE_FAILED, username);\r
-      return 5;\r
+  if (! virtual_account) {\r
+    if (well_known_username(canon)) password = _T("");\r
+    else {\r
+      if (grant_logon_as_service(canon)) {\r
+        if (canon != username) HeapFree(GetProcessHeap(), 0, canon);\r
+        print_message(stderr, NSSM_MESSAGE_GRANT_LOGON_AS_SERVICE_FAILED, username);\r
+        return 5;\r
+      }\r
     }\r
   }\r
 \r
@@ -1393,7 +1417,16 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
     }\r
 \r
     if (ret) {\r
-      int response = await_service_control_response(control, service_handle, &service_status, initial_status);\r
+      unsigned long cutoff = 0;\r
+\r
+      /* If we manage the service, respect the throttle time. */\r
+      HKEY key = open_registry(service_name, 0, KEY_READ, false);\r
+      if (key) {\r
+        if (get_number(key, NSSM_REG_THROTTLE, &cutoff, false) != 1) cutoff = NSSM_RESET_THROTTLE_RESTART;\r
+        RegCloseKey(key);\r
+      }\r
+\r
+      int response = await_service_control_response(control, service_handle, &service_status, initial_status, cutoff);\r
       CloseServiceHandle(service_handle);\r
 \r
       if (response) {\r
@@ -2077,8 +2110,7 @@ void CALLBACK end_service(void *arg, unsigned char why) {
       log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_UNCLEAN, service->name, code, exit_action_strings[action], 0);\r
       stop_service(service, exitcode, false, default_action);\r
       wait_for_hooks(service, false);\r
-      free_imports();\r
-      exit(exitcode);\r
+      nssm_exit(exitcode);\r
   }\r
 }\r
 \r