Updated translation from François-Régis Tardy.
[nssm.git] / service.cpp
index fd25163..d44998a 100644 (file)
@@ -1,5 +1,6 @@
 #include "nssm.h"\r
 \r
+bool is_admin;\r
 SERVICE_STATUS service_status;\r
 SERVICE_STATUS_HANDLE service_handle;\r
 HANDLE process_handle;\r
@@ -29,7 +30,7 @@ static inline int throttle_milliseconds() {
 SC_HANDLE open_service_manager() {\r
   SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);\r
   if (! ret) {\r
-    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENSCMANAGER_FAILED, 0);\r
+    if (is_admin) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENSCMANAGER_FAILED, 0);\r
     return 0;\r
   }\r
 \r
@@ -130,6 +131,8 @@ int install_service(char *name, char *exe, char *flags) {
     return 6;\r
   }\r
 \r
+  set_service_recovery(service, name);\r
+\r
   /* Cleanup */\r
   CloseServiceHandle(service);\r
   CloseServiceHandle(services);\r
@@ -204,10 +207,12 @@ void WINAPI service_main(unsigned long argc, char **argv) {
   service_status.dwWaitHint = throttle_delay + NSSM_WAITHINT_MARGIN;\r
   SetServiceStatus(service_handle, &service_status);\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
+  if (is_admin) {\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
-  set_service_recovery(service_name);\r
+    set_service_recovery(0, service_name);\r
+  }\r
 \r
   /* Used for signalling a resume if the service pauses when throttled. */\r
   throttle_timer = CreateWaitableTimer(0, 1, 0);\r
@@ -219,20 +224,34 @@ void WINAPI service_main(unsigned long argc, char **argv) {
 }\r
 \r
 /* Make sure service recovery actions are taken where necessary */\r
-void set_service_recovery(char *service_name) {\r
-  SC_HANDLE services = open_service_manager();\r
-  if (! services) return;\r
+void set_service_recovery(SC_HANDLE service, char *service_name) {\r
+  SC_HANDLE services = 0;\r
 \r
-  SC_HANDLE service = OpenService(services, service_name, SC_MANAGER_ALL_ACCESS);\r
-  if (! service) return;\r
-  return;\r
+  if (! service) {\r
+    services = open_service_manager();\r
+    if (! services) return;\r
+\r
+    service = OpenService(services, service_name, SC_MANAGER_ALL_ACCESS);\r
+    if (! service) return;\r
+  }\r
 \r
   SERVICE_FAILURE_ACTIONS_FLAG flag;\r
   ZeroMemory(&flag, sizeof(flag));\r
   flag.fFailureActionsOnNonCrashFailures = true;\r
 \r
   /* This functionality was added in Vista so the call may fail */\r
-  ChangeServiceConfig2(service, SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, &flag);\r
+  if (! ChangeServiceConfig2(service, SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, &flag)) {\r
+    unsigned long error = GetLastError();\r
+    /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */\r
+    if (error != ERROR_INVALID_LEVEL) {\r
+      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CHANGESERVICECONFIG2_FAILED, service_name, error_string(error), 0);\r
+    }\r
+  }\r
+\r
+  if (services) {\r
+    CloseServiceHandle(service);\r
+    CloseServiceHandle(services);\r
+  }\r
 }\r
 \r
 int monitor_service() {\r