Browse Source

Removed run hack.

Previously we used to attempt to run as a service only when the "run"
argument was given, and printed a usage message if no arguments were
provided.

A more appropriate strategy is to check for StartServiceCtrlDispatcher()
returning ERROR_FAILED_SERVICE_CONTROLLER_CONNECT, as that means the
application is not running in a service context.

As a result we no longer write the undocumented "run" argument to the
registry when installing a service.  Technically this means that we
sacrifice backward compatibility with older versions.
Iain Patterson 10 years ago
parent
commit
7b85809e2f
2 changed files with 19 additions and 20 deletions
  1. 17 17
      nssm.cpp
  2. 2 3
      service.cpp

+ 17 - 17
nssm.cpp

@@ -47,23 +47,20 @@ int check_admin(char *action) {
 }
 
 int main(int argc, char **argv) {
-  /* Require an argument since users may try to run nssm directly */
-  if (argc == 1) exit(usage(1));
-
   /* Elevate */
-  if (str_equiv(argv[1], "install") || str_equiv(argv[1], "remove")) {
-    if (check_admin(argv[1])) exit(100);
-  }
+  if (argc > 1) {
+    if (str_equiv(argv[1], "install") || str_equiv(argv[1], "remove")) {
+      if (check_admin(argv[1])) exit(100);
+    }
 
-  /* Valid commands are install or remove */
-  if (str_equiv(argv[1], "install")) {
-    exit(pre_install_service(argc - 2, argv + 2));
+    /* Valid commands are install or remove */
+    if (str_equiv(argv[1], "install")) {
+      exit(pre_install_service(argc - 2, argv + 2));
+    }
+    if (str_equiv(argv[1], "remove")) {
+      exit(pre_remove_service(argc - 2, argv + 2));
+    }
   }
-  if (str_equiv(argv[1], "remove")) {
-    exit(pre_remove_service(argc - 2, argv + 2));
-  }
-  /* Undocumented: "run" is used to actually do service stuff */
-  if (! str_equiv(argv[1], NSSM_RUN)) exit(usage(2));
 
   /* Thread local storage for error message buffer */
   tls_index = TlsAlloc();
@@ -74,10 +71,13 @@ int main(int argc, char **argv) {
   /* Start service magic */
   SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };
   if (! StartServiceCtrlDispatcher(table)) {
-    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(GetLastError()), 0);
-    return 100;
+    unsigned long error = GetLastError();
+    /* User probably ran nssm with no argument */
+    if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));
+    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);
+    exit(100);
   }
 
   /* And nothing more to do */
-  return 0;
+  exit(0);
 }

+ 2 - 3
service.cpp

@@ -96,13 +96,12 @@ int install_service(char *name, char *exe, char *flags) {
 
   /* Construct command */
   char command[CMD_LENGTH];
-  size_t runlen = strlen(NSSM_RUN);
   size_t pathlen = strlen(path);
-  if (pathlen + runlen + 2 >= VALUE_LENGTH) {
+  if (pathlen + 1 >= VALUE_LENGTH) {
     fprintf(stderr, "The full path to " NSSM " is too long!\n");
     return 3;
   }
-  if (_snprintf(command, sizeof(command), "\"%s\" %s", path, NSSM_RUN) < 0) {
+  if (_snprintf(command, sizeof(command), "\"%s\"", path) < 0) {
     fprintf(stderr, "Out of memory for ImagePath!\n");
     return 4;
   }