Browse Source

NSSM 2.0.

Iain Patterson 16 years ago
parent
commit
167b3e7400
6 changed files with 216 additions and 103 deletions
  1. 56 8
      README.txt
  2. 31 72
      gui.cpp
  3. 16 6
      nssm.cpp
  4. 4 2
      nssm.h
  5. 106 14
      service.cpp
  6. 3 1
      service.h

+ 56 - 8
README.txt

@@ -1,5 +1,5 @@
 NSSM: The Non-Sucking Service Manager
-Version 1.0, 2003-05-30
+Version 2.0, 2006-09-09
 
 NSSM is a service helper program similar to srvany and cygrunsrv.  It can 
 start any application as an NT service and will restart the service if it 
@@ -7,21 +7,44 @@ fails for any reason.
 
 NSSM also has a graphical service installer and remover.
 
+Since version 2.0, the GUI can be bypassed by entering all appropriate 
+options on the command line.
 
-Installation
-------------
+
+Usage
+-----
+In the usage notes below, arguments to the program may be written in angle 
+brackets and/or square brackets.  <string> means you must insert the 
+appropriate string and [<string>] means the string is optional.  See the 
+examples below...
+
+
+Installation using the GUI
+--------------------------
 To install a service, run
 
-    nssm install servicename
+    nssm install <servicename>
 
 You will be prompted to enter the full path to the application you wish 
-to run and any commandline options to pass to that application.
+to run and any command line options to pass to that application.
 
 Use the system service manager (services.msc) to control advanced service 
 properties such as startup method and desktop interaction.  NSSM may 
 support these options at a later time...
 
 
+Installation using the command line
+-----------------------------------
+To install a service, run
+
+    nssm install <servicename> <application> [<options>]
+
+NSSM will then attempt to install a service which runs the named application 
+with the given options (if you specified any).
+
+Don't forget to enclose paths in "quotes" if they contain spaces!
+
+
 Managing the service
 --------------------
 NSSM will launch the application listed in the registry when you send it a 
@@ -35,16 +58,41 @@ between each attempt, until the service is successfully started or you send
 it a stop signal.
 
 
-Removing services
------------------
+Removing services using the GUI
+-------------------------------
 NSSM can also remove services.  Run
 
-    nssm remove servicename
+    nssm remove <servicename>
 
 to remove a service.  You will prompted for confirmation before the service 
 is removed.  Try not to remove essential system services...
 
 
+Removing service using the command line
+---------------------------------------
+To remove a service without confirmation from the GUI, run
+
+    nssm remove <servicename> confirm
+
+Try not to remove essential system services...
+
+
+Example usage
+-------------
+To install an Unreal Tournament server:
+
+    nssm install UT2004 c:\games\ut2004\system\ucc.exe server
+
+To remove the server:
+
+    nssm remove UT2004 confirm
+
+
+Building NSSM from source
+-------------------------
+NSSM is known to compile with Visual Studio 6 and Visual Studio 2005.
+
+
 Licence
 -------
 NSSM is public domain.  You may unconditionally use it and/or its source code 

+ 31 - 72
gui.cpp

@@ -69,7 +69,6 @@ int install(HWND window) {
   char name[STRING_SIZE];
   char exe[MAX_PATH];
   char flags[STRING_SIZE];
-  char dir[MAX_PATH];
 
   /* Get service name */
   if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {
@@ -92,56 +91,28 @@ int install(HWND window) {
   }
   else ZeroMemory(&flags, sizeof(flags));
 
-  /* Work out directory name */
-  unsigned int len = strlen(exe);
-  unsigned int i;
-  for (i = len; i && exe[i] != '\\' && exe[i] != '/'; i--);
-  memmove(dir, exe, i);
-  dir[i] = '\0';
-
-  /* Open service manager */
-  SC_HANDLE services = open_service_manager();
-  if (! services) {
-    MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);
-    return 2;
-  }
-  
-  /* Get path of this program */
-  char path[MAX_PATH];
-  GetModuleFileName(0, path, MAX_PATH);
-
-  /* Construct command */
-  char command[MAX_PATH];
-  int runlen = strlen(NSSM_RUN);
-  int pathlen = strlen(path);
-  if (pathlen + runlen + 2 >= MAX_PATH) {
-    MessageBox(0, "Path too long!\nThe full path to " NSSM " is too long.\nPlease install " NSSM " somewhere else...\n", NSSM, MB_OK);
-    return 3;
-  }
-  if (snprintf(command, sizeof(command), "%s %s", path, NSSM_RUN) < 0) {
-     MessageBox(0, "Error constructing ImagePath!\nThis really shouldn't happen.  You could be out of memory\nor the world may be about to end or something equally bad.", NSSM, MB_OK);
-     return 4;
-  }
+  /* See if it works */
+  switch (install_service(name, exe, flags)) {
+    case 2:
+      MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);
+      return 2;
 
-  /* Create the service */
-  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);
-  if (! service) {
-    MessageBox(0, "Couldn't create service!\nPerhaps it is already installed...", NSSM, MB_OK);
-    CloseServiceHandle(services);
-    return 5;
-  }
+    case 3:
+      MessageBox(0, "Path too long!\nThe full path to " NSSM " is too long.\nPlease install " NSSM " somewhere else...\n", NSSM, MB_OK);
+      return 3;
 
-  /* Now we need to put the parameters into the registry */
-  if (create_parameters(name, exe, flags, dir)) {
-    MessageBox(0, "Couldn't set startup parameters for the service!\nDeleting the service...", NSSM, MB_OK);
-    DeleteService(service);
-    CloseServiceHandle(services);
-    return 6;
-  }
+    case 4:
+      MessageBox(0, "Error constructing ImagePath!\nThis really shouldn't happen.  You could be out of memory\nor the world may be about to end or something equally bad.", NSSM, MB_OK);
+      return 4;
 
-  /* Cleanup */
-  CloseServiceHandle(service);
-  CloseServiceHandle(services);
+    case 5:
+      MessageBox(0, "Couldn't create service!\nPerhaps it is already installed...", NSSM, MB_OK);
+      return 5;
+
+    case 6:
+      MessageBox(0, "Couldn't set startup parameters for the service!\nDeleting the service...", NSSM, MB_OK);
+      return 6;
+  }
 
   MessageBox(0, "Service successfully installed!", NSSM, MB_OK);
   return 0;
@@ -167,32 +138,20 @@ int remove(HWND window) {
   }
   else if (MessageBox(0, blurb, NSSM, MB_YESNO) != IDYES) return 0;
 
-  /* Open service manager */
-  SC_HANDLE services = open_service_manager();
-  if (! services) {
-    MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);
-    return 2;
-  }
-  
-  /* Try to open the service */
-  SC_HANDLE service = OpenService(services, name, SC_MANAGER_ALL_ACCESS);
-  if (! service) {
-    MessageBox(0, "Can't open service!\nPerhaps it isn't installed...", NSSM, MB_OK);
-    CloseServiceHandle(services);
-    return 3;
-  }
+  /* See if it works */
+  switch (remove_service(name)) {
+    case 2:
+      MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);
+      return 2;
 
-  /* Try to delete the service */
-  if (! DeleteService(service)) {
-    MessageBox(0, "Can't delete service!  Make sure the service is stopped and try again.\nIf this error persists, you may need to set the service NOT to start\nautomatically, reboot your computer and try removing it again.", NSSM, MB_OK);
-    CloseServiceHandle(service);
-    CloseServiceHandle(services);
-    return 4;
-  }
+    case 3:
+      MessageBox(0, "Can't open service!\nPerhaps it isn't installed...", NSSM, MB_OK);
+      return 3;
 
-  /* Cleanup */
-  CloseServiceHandle(service);
-  CloseServiceHandle(services);
+    case 4:
+      MessageBox(0, "Can't delete service!  Make sure the service is stopped and try again.\nIf this error persists, you may need to set the service NOT to start\nautomatically, reboot your computer and try removing it again.", NSSM, MB_OK);
+      return 4;
+  }
 
   MessageBox(0, "Service successfully removed!", NSSM, MB_OK);
   return 0;

+ 16 - 6
nssm.cpp

@@ -13,10 +13,16 @@ int str_equiv(const char *a, const char *b) {
 int usage(int ret) {
   fprintf(stderr, "NSSM: The non-sucking service manager\n");
   fprintf(stderr, "Version %s, %s\n", NSSM_VERSION, NSSM_DATE);
-  fprintf(stderr, "Usage: nssm option [args]\n");
-  fprintf(stderr, "To install a service: nssm install [servicename]\n");
-  fprintf(stderr, "To remove a service: nssm remove [servicename]\n");
-  exit(ret);
+  fprintf(stderr, "Usage: nssm <option> [args]\n\n");
+  fprintf(stderr, "To show service installation GUI:\n\n");
+  fprintf(stderr, "        nssm install [<servicename>]\n\n");
+  fprintf(stderr, "To install a service without confirmation:\n\n");
+  fprintf(stderr, "        nssm install <servicename> <app> [<args>]\n\n");
+  fprintf(stderr, "To show service removal GUI:\n\n");
+  fprintf(stderr, "        nssm remove [<servicename>]\n\n");
+  fprintf(stderr, "To remove a service without confirmation:\n\n");
+  fprintf(stderr, "        nssm remove <servicename> confirm\n");
+  return(ret);
 }
 
 int main(int argc, char **argv) {
@@ -24,8 +30,12 @@ int main(int argc, char **argv) {
   if (argc == 1) exit(usage(1));
 
   /* Valid commands are install or remove */
-  if (str_equiv(argv[1], "install")) exit(install_service(argv[2]));
-  if (str_equiv(argv[1], "remove")) exit(remove_service(argv[2]));
+  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));
+  }
   /* Undocumented: "run" is used to actually do service stuff */
   if (! str_equiv(argv[1], NSSM_RUN)) exit(usage(2));
 

+ 4 - 2
nssm.h

@@ -10,9 +10,11 @@
 #include "service.h"
 #include "gui.h"
 
+int str_equiv(const char *, const char *);
+
 #define NSSM "nssm"
-#define NSSM_VERSION "1.0"
-#define NSSM_DATE "2003-05-30"
+#define NSSM_VERSION "2.0"
+#define NSSM_DATE "2006-09-09"
 #define NSSM_RUN "run"
 
 #endif

+ 106 - 14
service.cpp

@@ -19,25 +19,117 @@ SC_HANDLE open_service_manager() {
   return ret;
 }
 
+/* About to install the service */
+int pre_install_service(int argc, char **argv) {
+  /* Show the dialogue box if we didn't give the */
+  if (argc < 2) return nssm_gui(IDD_INSTALL, argv[0]);
+
+  /* Arguments are optional */
+  char *flags;
+  if (argc == 2) flags = "";
+  else flags = argv[2];
+
+  return install_service(argv[0], argv[1], flags);
+}
+
+/* About to remove the service */
+int pre_remove_service(int argc, char **argv) {
+  /* Show dialogue box if we didn't pass service name and "confirm" */
+  if (argc < 2) return nssm_gui(IDD_REMOVE, argv[0]);
+  if (str_equiv(argv[1], "confirm")) return remove_service(argv[0]);
+  fprintf(stderr, "To remove a service without confirmation: nssm remove <servicename> confirm\n");
+  return 100;
+}
+
 /* Install the service */
-int install_service(char *name) {
-#ifdef GUI
-  /* Show the dialogue box */
-  return nssm_gui(IDD_INSTALL, name);
-#else
-  fprintf(stderr, "Unimplemented\n");
-  return 1;
-#endif
+int install_service(char *name, char *exe, char *flags) {
+  /* Open service manager */
+  SC_HANDLE services = open_service_manager();
+  if (! services) {
+    fprintf(stderr, "Error opening service manager!\n");
+    return 2;
+  }
+  
+  /* Get path of this program */
+  char path[MAX_PATH];
+  GetModuleFileName(0, path, MAX_PATH);
+
+  /* Construct command */
+  char command[MAX_PATH];
+  int runlen = strlen(NSSM_RUN);
+  int pathlen = strlen(path);
+  if (pathlen + runlen + 2 >= MAX_PATH) {
+    fprintf(stderr, "The full path to " NSSM " is too long!\n");
+    return 3;
+  }
+  if (snprintf(command, sizeof(command), "\"%s\" %s", path, NSSM_RUN) < 0) {
+    fprintf(stderr, "Out of memory for ImagePath!\n");
+    return 4;
+  }
+
+  /* Work out directory name */
+  unsigned int len = strlen(exe);
+  unsigned int i;
+  for (i = len; i && exe[i] != '\\' && exe[i] != '/'; i--);
+  char dir[MAX_PATH];
+  memmove(dir, exe, i);
+  dir[i] = '\0';
+
+  /* Create the service */
+  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);
+  if (! service) {
+    fprintf(stderr, "Error creating service!\n");
+    CloseServiceHandle(services);
+    return 5;
+  }
+
+  /* Now we need to put the parameters into the registry */
+  if (create_parameters(name, exe, flags, dir)) {
+    fprintf(stderr, "Error setting startup parameters for the service!\n");
+    DeleteService(service);
+    CloseServiceHandle(services);
+    return 6;
+  }
+
+  /* Cleanup */
+  CloseServiceHandle(service);
+  CloseServiceHandle(services);
+
+  printf("Service \"%s\" installed successfully!\n", name);
+  return 0;
 }
 
 /* Remove the service */
 int remove_service(char *name) {
-#ifdef GUI
-  return nssm_gui(IDD_REMOVE, name);
-#else
-  fprintf(stderr, "Unimplemented\n");
-  return 1;
-#endif
+  /* Open service manager */
+  SC_HANDLE services = open_service_manager();
+  if (! services) {
+    fprintf(stderr, "Error opening service manager!\n");
+    return 2;
+  }
+  
+  /* Try to open the service */
+  SC_HANDLE service = OpenService(services, name, SC_MANAGER_ALL_ACCESS);
+  if (! service) {
+    fprintf(stderr, "Can't open service!");
+    CloseServiceHandle(services);
+    return 3;
+  }
+
+  /* Try to delete the service */
+  if (! DeleteService(service)) {
+    fprintf(stderr, "Error deleting service!\n");
+    CloseServiceHandle(service);
+    CloseServiceHandle(services);
+    return 4;
+  }
+
+  /* Cleanup */
+  CloseServiceHandle(service);
+  CloseServiceHandle(services);
+
+  printf("Service \"%s\" removed successfully!\n", name);
+  return 0;
 }
 
 /* Service initialisation */

+ 3 - 1
service.h

@@ -5,7 +5,9 @@ void WINAPI service_main(unsigned long, char **);
 unsigned long WINAPI service_control_handler(unsigned long, unsigned long, void *, void *);
 
 SC_HANDLE open_service_manager();
-int install_service(char *);
+int pre_install_service(int, char **);
+int pre_remove_service(int, char **);
+int install_service(char *, char *, char *);
 int remove_service(char *);
 int monitor_service();
 int start_service();