Localised (almost) all messages.
[nssm.git] / service.cpp
index 7e9a9d7..766031e 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
@@ -38,7 +39,7 @@ SC_HANDLE open_service_manager() {
 \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
+  /* Show the dialogue box if we didn't give the service name and path */\r
   if (argc < 2) return nssm_gui(IDD_INSTALL, argv[0]);\r
 \r
   /* Arguments are optional */\r
@@ -77,7 +78,7 @@ int pre_remove_service(int argc, char **argv) {
   /* 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
+  print_message(stderr, NSSM_MESSAGE_PRE_REMOVE_SERVICE);\r
   return 100;\r
 }\r
 \r
@@ -86,7 +87,7 @@ int install_service(char *name, char *exe, char *flags) {
   /* Open service manager */\r
   SC_HANDLE services = open_service_manager();\r
   if (! services) {\r
-    fprintf(stderr, "Error opening service manager!\n");\r
+    print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);\r
     return 2;\r
   }\r
   \r
@@ -98,11 +99,11 @@ int install_service(char *name, char *exe, char *flags) {
   char command[CMD_LENGTH];\r
   size_t pathlen = strlen(path);\r
   if (pathlen + 1 >= VALUE_LENGTH) {\r
-    fprintf(stderr, "The full path to " NSSM " is too long!\n");\r
+    print_message(stderr, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);\r
     return 3;\r
   }\r
   if (_snprintf(command, sizeof(command), "\"%s\"", path) < 0) {\r
-    fprintf(stderr, "Out of memory for ImagePath!\n");\r
+    print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY_FOR_IMAGEPATH);\r
     return 4;\r
   }\r
 \r
@@ -117,24 +118,26 @@ int install_service(char *name, char *exe, char *flags) {
   /* 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
+    print_message(stderr, NSSM_MESSAGE_CREATESERVICE_FAILED);\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
+    print_message(stderr, NSSM_MESSAGE_CREATE_PARAMETERS_FAILED);\r
     DeleteService(service);\r
     CloseServiceHandle(services);\r
     return 6;\r
   }\r
 \r
+  set_service_recovery(service, name);\r
+\r
   /* Cleanup */\r
   CloseServiceHandle(service);\r
   CloseServiceHandle(services);\r
 \r
-  printf("Service \"%s\" installed successfully!\n", name);\r
+  print_message(stdout, NSSM_MESSAGE_SERVICE_INSTALLED, name);\r
   return 0;\r
 }\r
 \r
@@ -143,21 +146,21 @@ int remove_service(char *name) {
   /* Open service manager */\r
   SC_HANDLE services = open_service_manager();\r
   if (! services) {\r
-    fprintf(stderr, "Error opening service manager!\n");\r
+    print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);\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
+    print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED);\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
+    print_message(stderr, NSSM_MESSAGE_DELETESERVICE_FAILED);\r
     CloseServiceHandle(service);\r
     CloseServiceHandle(services);\r
     return 4;\r
@@ -167,7 +170,7 @@ int remove_service(char *name) {
   CloseServiceHandle(service);\r
   CloseServiceHandle(services);\r
 \r
-  printf("Service \"%s\" removed successfully!\n", name);\r
+  print_message(stdout, NSSM_MESSAGE_SERVICE_REMOVED, name);\r
   return 0;\r
 }\r
 \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,12 +224,16 @@ 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
+  if (! service) {\r
+    services = open_service_manager();\r
+    if (! services) return;\r
 \r
-  SC_HANDLE service = OpenService(services, service_name, SC_MANAGER_ALL_ACCESS);\r
-  if (! service) return;\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
@@ -238,6 +247,11 @@ void set_service_recovery(char *service_name) {
       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