From: Iain Patterson Date: Sun, 10 Sep 2006 01:33:00 +0000 (-0400) Subject: NSSM 2.0. X-Git-Tag: v2.0^0 X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=167b3e74003ed8f2b61bda7037f31447c488c29b;p=nssm.git NSSM 2.0. --- diff --git a/README.txt b/README.txt index ae081d2..21718ba 100644 --- a/README.txt +++ b/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. means you must insert the +appropriate string and [] means the string is optional. See the +examples below... + + +Installation using the GUI +-------------------------- To install a service, run - nssm install servicename + nssm install 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 [] + +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 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 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 diff --git a/gui.cpp b/gui.cpp index c094657..5041acd 100644 --- a/gui.cpp +++ b/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; diff --git a/nssm.cpp b/nssm.cpp index 225d038..f412579 100644 --- a/nssm.cpp +++ b/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