--- /dev/null
+NSSM: The Non-Sucking Service Manager\r
+Version 1.0, 2003-05-30\r
+\r
+NSSM is a service helper program similar to srvany and cygrunsrv. It can \r
+start any application as an NT service and will restart the service if it \r
+fails for any reason.\r
+\r
+NSSM also has a graphical service installer and remover.\r
+\r
+\r
+Installation\r
+------------\r
+To install a service, run\r
+\r
+ nssm install servicename\r
+\r
+You will be prompted to enter the full path to the application you wish \r
+to run and any commandline options to pass to that application.\r
+\r
+Use the system service manager (services.msc) to control advanced service \r
+properties such as startup method and desktop interaction. NSSM may \r
+support these options at a later time...\r
+\r
+\r
+Managing the service\r
+--------------------\r
+NSSM will launch the application listed in the registry when you send it a \r
+start signal and will terminate it when you send a stop signal. So far, so \r
+much like srvany. But NSSM is the Non-Sucking service manager and will take \r
+action if/when the application dies.\r
+\r
+NSSM will try to restart itself if it notices that the application died but \r
+you didn't send it a stop signal. NSSM will keep trying, pausing 30 seconds \r
+between each attempt, until the service is successfully started or you send \r
+it a stop signal.\r
+\r
+\r
+Removing services\r
+-----------------\r
+NSSM can also remove services. Run\r
+\r
+ nssm remove servicename\r
+\r
+to remove a service. You will prompted for confirmation before the service \r
+is removed. Try not to remove essential system services...\r
+\r
+\r
+Licence\r
+-------\r
+NSSM is public domain. You may unconditionally use it and/or its source code \r
+for any purpose you wish.\r
--- /dev/null
+#include "nssm.h"\r
+\r
+/* Convert error code to error string - must call LocalFree() on return value */\r
+char *error_string(unsigned long error) {\r
+ char *message;\r
+ if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) &message, 0, 0)) return 0;\r
+ return message;\r
+}\r
+\r
+/* Log a message to the Event Log */\r
+void eventprintf(unsigned short type, char *format, ...) {\r
+ char message[4096];\r
+ char *strings[2];\r
+ int n, size;\r
+ va_list arg;\r
+\r
+ /* Construct the message */\r
+ size = sizeof(message);\r
+ va_start(arg, format);\r
+ n = _vsnprintf(message, size, format, arg);\r
+ va_end(arg);\r
+\r
+ /* Check success */\r
+ if (n < 0 || n >= size) return;\r
+\r
+ /* Construct strings array */\r
+ strings[0] = message;\r
+ strings[1] = 0;\r
+ \r
+ /* Open event log */\r
+ HANDLE handle = RegisterEventSource(0, TEXT(NSSM));\r
+ if (! handle) return;\r
+\r
+ /* Log it */\r
+ if (! ReportEvent(handle, type, 0, 0, 0, 1, 0, (const char **) strings, 0)) {\r
+ printf("ReportEvent(): %s\n", error_string(GetLastError()));\r
+ }\r
+\r
+ /* Close event log */\r
+ DeregisterEventSource(handle);\r
+}\r
--- /dev/null
+#ifndef EVENT_H\r
+#define EVENT_H\r
+\r
+char *error_string(unsigned long);\r
+void eventprintf(unsigned short, char *, ...);\r
+\r
+#endif\r
--- /dev/null
+#include "nssm.h"\r
+\r
+int nssm_gui(int resource, char *name) {\r
+ char blurb[256];\r
+\r
+ /* Create window */\r
+ HWND dlg = CreateDialog(0, MAKEINTRESOURCE(resource), 0, install_dlg);\r
+ if (! dlg) {\r
+ snprintf(blurb, sizeof(blurb), "CreateDialog() failed with error code %d", GetLastError());\r
+ MessageBox(0, blurb, NSSM, MB_OK);\r
+ return 1;\r
+ }\r
+\r
+ /* Display the window */\r
+ centre_window(dlg);\r
+ ShowWindow(dlg, SW_SHOW);\r
+\r
+ /* Set service name if given */\r
+ if (name) {\r
+ SetDlgItemText(dlg, IDC_NAME, name);\r
+ /* No point making user click remove if the name is already entered */\r
+ if (resource == IDD_REMOVE) {\r
+ HWND button = GetDlgItem(dlg, IDC_REMOVE);\r
+ if (button) {\r
+ SendMessage(button, WM_LBUTTONDOWN, 0, 0);\r
+ SendMessage(button, WM_LBUTTONUP, 0, 0);\r
+ }\r
+ }\r
+ }\r
+\r
+ /* Go! */\r
+ MSG message;\r
+ while (GetMessage(&message, 0, 0, 0)) {\r
+ TranslateMessage(&message);\r
+ DispatchMessage(&message);\r
+ }\r
+\r
+ return message.wParam;\r
+}\r
+\r
+void centre_window(HWND window) {\r
+ HWND desktop;\r
+ RECT size, desktop_size;\r
+ unsigned long x, y;\r
+\r
+ if (! window) return;\r
+\r
+ /* Find window size */\r
+ if (! GetWindowRect(window, &size)) return;\r
+ \r
+ /* Find desktop window */\r
+ desktop = GetDesktopWindow();\r
+ if (! desktop) return;\r
+\r
+ /* Find desktop window size */\r
+ if (! GetWindowRect(desktop, &desktop_size)) return;\r
+\r
+ /* Centre window */\r
+ x = (desktop_size.right - size.right) / 2;\r
+ y = (desktop_size.bottom - size.bottom) / 2;\r
+ MoveWindow(window, x, y, size.right, size.bottom, 0);\r
+}\r
+\r
+/* Install the service */\r
+int install(HWND window) {\r
+ if (! window) return 1;\r
+\r
+ /* Check parameters in the window */\r
+ char name[STRING_SIZE];\r
+ char exe[MAX_PATH];\r
+ char flags[STRING_SIZE];\r
+ char dir[MAX_PATH];\r
+\r
+ /* Get service name */\r
+ if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {\r
+ MessageBox(0, "No valid service name was specified!", NSSM, MB_OK);\r
+ return 2;\r
+ }\r
+\r
+ /* Get executable name */\r
+ if (! GetDlgItemText(window, IDC_PATH, exe, sizeof(exe))) {\r
+ MessageBox(0, "No valid executable path was specified!", NSSM, MB_OK);\r
+ return 3;\r
+ }\r
+\r
+ /* Get flags */\r
+ if (SendMessage(GetDlgItem(window, IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) {\r
+ if (! GetDlgItemText(window, IDC_FLAGS, flags, sizeof(flags))) {\r
+ MessageBox(0, "No valid options were specified!", NSSM, MB_OK);\r
+ return 4;\r
+ }\r
+ }\r
+ else ZeroMemory(&flags, sizeof(flags));\r
+\r
+ /* Work out directory name */\r
+ unsigned int len = strlen(exe);\r
+ unsigned int i;\r
+ for (i = len; i && exe[i] != '\\' && exe[i] != '/'; i--);\r
+ memmove(dir, exe, i);\r
+ dir[i] = '\0';\r
+\r
+ /* Open service manager */\r
+ SC_HANDLE services = open_service_manager();\r
+ if (! services) {\r
+ MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);\r
+ return 2;\r
+ }\r
+ \r
+ /* Get path of this program */\r
+ char path[MAX_PATH];\r
+ GetModuleFileName(0, path, MAX_PATH);\r
+\r
+ /* Construct command */\r
+ char command[MAX_PATH];\r
+ int runlen = strlen(NSSM_RUN);\r
+ int pathlen = strlen(path);\r
+ if (pathlen + runlen + 2 >= MAX_PATH) {\r
+ MessageBox(0, "Path too long!\nThe full path to " NSSM " is too long.\nPlease install " NSSM " somewhere else...\n", NSSM, MB_OK);\r
+ return 3;\r
+ }\r
+ if (snprintf(command, sizeof(command), "%s %s", path, NSSM_RUN) < 0) {\r
+ 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);\r
+ return 4;\r
+ }\r
+\r
+ /* 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
+ MessageBox(0, "Couldn't create service!\nPerhaps it is already installed...", NSSM, MB_OK);\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
+ MessageBox(0, "Couldn't set startup parameters for the service!\nDeleting the service...", NSSM, MB_OK);\r
+ DeleteService(service);\r
+ CloseServiceHandle(services);\r
+ return 6;\r
+ }\r
+\r
+ /* Cleanup */\r
+ CloseServiceHandle(service);\r
+ CloseServiceHandle(services);\r
+\r
+ MessageBox(0, "Service successfully installed!", NSSM, MB_OK);\r
+ return 0;\r
+}\r
+\r
+/* Remove the service */\r
+int remove(HWND window) {\r
+ if (! window) return 1;\r
+\r
+ /* Check parameters in the window */\r
+ char name[STRING_SIZE];\r
+\r
+ /* Get service name */\r
+ if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {\r
+ MessageBox(0, "No valid service name was specified!", NSSM, MB_OK);\r
+ return 2;\r
+ }\r
+\r
+ /* Confirm */\r
+ char blurb[MAX_PATH];\r
+ if (snprintf(blurb, sizeof(blurb), "Remove the \"%s\" service?", name) < 0) {\r
+ if (MessageBox(0, "Remove the service?", NSSM, MB_YESNO) != IDYES) return 0;\r
+ }\r
+ else if (MessageBox(0, blurb, NSSM, MB_YESNO) != IDYES) return 0;\r
+\r
+ /* Open service manager */\r
+ SC_HANDLE services = open_service_manager();\r
+ if (! services) {\r
+ MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);\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
+ MessageBox(0, "Can't open service!\nPerhaps it isn't installed...", NSSM, MB_OK);\r
+ CloseServiceHandle(services);\r
+ return 3;\r
+ }\r
+\r
+ /* Try to delete the service */\r
+ if (! DeleteService(service)) {\r
+ 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);\r
+ CloseServiceHandle(service);\r
+ CloseServiceHandle(services);\r
+ return 4;\r
+ }\r
+\r
+ /* Cleanup */\r
+ CloseServiceHandle(service);\r
+ CloseServiceHandle(services);\r
+\r
+ MessageBox(0, "Service successfully removed!", NSSM, MB_OK);\r
+ return 0;\r
+}\r
+\r
+/* Browse for game */\r
+void browse(HWND window) {\r
+ if (! window) return;\r
+\r
+ OPENFILENAME ofn;\r
+ ZeroMemory(&ofn, sizeof(ofn));\r
+ ofn.lStructSize = sizeof(ofn);\r
+ ofn.lpstrFilter = "Applications\0*.exe\0All files\0*.*\0\0";\r
+ ofn.lpstrFile = new char[MAX_PATH];\r
+ ofn.lpstrFile[0] = '\0';\r
+ ofn.lpstrTitle = "Locate application file";\r
+ ofn.nMaxFile = MAX_PATH;\r
+ ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;\r
+\r
+ if (GetOpenFileName(&ofn)) {\r
+ SendMessage(window, WM_SETTEXT, 0, (LPARAM) ofn.lpstrFile);\r
+ }\r
+\r
+ delete[] ofn.lpstrFile;\r
+}\r
+\r
+/* Install/remove dialogue callback */\r
+int CALLBACK install_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {\r
+ switch (message) {\r
+ /* Creating the dialogue */\r
+ case WM_INITDIALOG:\r
+ return 1;\r
+\r
+ /* Button was pressed or control was controlled */\r
+ case WM_COMMAND:\r
+ switch (LOWORD(w)) {\r
+ /* OK button */\r
+ case IDC_OK:\r
+ PostQuitMessage(install(window));\r
+ break;\r
+\r
+ /* Cancel button */\r
+ case IDC_CANCEL:\r
+ DestroyWindow(window);\r
+ break;\r
+\r
+ /* Browse button */\r
+ case IDC_BROWSE:\r
+ browse(GetDlgItem(window, IDC_PATH));\r
+ break;\r
+\r
+ /* Remove button */\r
+ case IDC_REMOVE:\r
+ PostQuitMessage(remove(window));\r
+ break;\r
+ }\r
+ return 1;\r
+\r
+ /* Window closing */\r
+ case WM_CLOSE:\r
+ DestroyWindow(window);\r
+ return 0;\r
+ case WM_DESTROY:\r
+ PostQuitMessage(0);\r
+ }\r
+ return 0;\r
+}\r
--- /dev/null
+#ifndef GUI_H\r
+#define GUI_H\r
+\r
+#include <stdio.h>\r
+#include <windows.h>\r
+#include <commctrl.h>\r
+#include "resource.h"\r
+\r
+#define GUI\r
+#ifndef snprintf\r
+#define snprintf _snprintf\r
+#endif\r
+\r
+#define STRING_SIZE 256\r
+\r
+int nssm_gui(int, char *);\r
+void centre_window(HWND);\r
+int install(HWND);\r
+int remove(HWND);\r
+void browse(HWND);\r
+int CALLBACK install_dlg(HWND, UINT, WPARAM, LPARAM);\r
+\r
+#endif\r
--- /dev/null
+#include "nssm.h"\r
+\r
+/* String function */\r
+int str_equiv(const char *a, const char *b) {\r
+ int i;\r
+ for (i = 0; ; i++) {\r
+ if (tolower(b[i]) != tolower(a[i])) return 0;\r
+ if (! a[i]) return 1;\r
+ }\r
+}\r
+\r
+/* How to use me correctly */\r
+int usage(int ret) {\r
+ fprintf(stderr, "NSSM: The non-sucking service manager\n");\r
+ fprintf(stderr, "Version %s, %s\n", NSSM_VERSION, NSSM_DATE);\r
+ fprintf(stderr, "Usage: nssm option [args]\n");\r
+ fprintf(stderr, "To install a service: nssm install [servicename]\n");\r
+ fprintf(stderr, "To remove a service: nssm remove [servicename]\n");\r
+ exit(ret);\r
+}\r
+\r
+int main(int argc, char **argv) {\r
+ /* Require an argument since users may try to run nssm directly */\r
+ if (argc == 1) exit(usage(1));\r
+\r
+ /* Valid commands are install or remove */\r
+ if (str_equiv(argv[1], "install")) exit(install_service(argv[2]));\r
+ if (str_equiv(argv[1], "remove")) exit(remove_service(argv[2]));\r
+ /* Undocumented: "run" is used to actually do service stuff */\r
+ if (! str_equiv(argv[1], NSSM_RUN)) exit(usage(2));\r
+\r
+ /* Start service magic */\r
+ SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };\r
+ if (! StartServiceCtrlDispatcher(table)) {\r
+ char *message = error_string(GetLastError());\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "StartServiceCtrlDispatcher() failed: %s", message);\r
+ if (message) LocalFree(message);\r
+ return 100;\r
+ }\r
+\r
+ /* And nothing more to do */\r
+ return 0;\r
+}\r
--- /dev/null
+# Microsoft Developer Studio Project File - Name="nssm" - Package Owner=<4>\r
+# Microsoft Developer Studio Generated Build File, Format Version 6.00\r
+# ** DO NOT EDIT **\r
+\r
+# TARGTYPE "Win32 (x86) Console Application" 0x0103\r
+\r
+CFG=nssm - Win32 Debug\r
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r
+!MESSAGE use the Export Makefile command and run\r
+!MESSAGE \r
+!MESSAGE NMAKE /f "nssm.mak".\r
+!MESSAGE \r
+!MESSAGE You can specify a configuration when running NMAKE\r
+!MESSAGE by defining the macro CFG on the command line. For example:\r
+!MESSAGE \r
+!MESSAGE NMAKE /f "nssm.mak" CFG="nssm - Win32 Debug"\r
+!MESSAGE \r
+!MESSAGE Possible choices for configuration are:\r
+!MESSAGE \r
+!MESSAGE "nssm - Win32 Release" (based on "Win32 (x86) Console Application")\r
+!MESSAGE "nssm - Win32 Debug" (based on "Win32 (x86) Console Application")\r
+!MESSAGE \r
+\r
+# Begin Project\r
+# PROP AllowPerConfigDependencies 0\r
+# PROP Scc_ProjName ""\r
+# PROP Scc_LocalPath ""\r
+CPP=cl.exe\r
+RSC=rc.exe\r
+\r
+!IF "$(CFG)" == "nssm - Win32 Release"\r
+\r
+# PROP BASE Use_MFC 0\r
+# PROP BASE Use_Debug_Libraries 0\r
+# PROP BASE Output_Dir "Release"\r
+# PROP BASE Intermediate_Dir "Release"\r
+# PROP BASE Target_Dir ""\r
+# PROP Use_MFC 0\r
+# PROP Use_Debug_Libraries 0\r
+# PROP Output_Dir "Release"\r
+# PROP Intermediate_Dir "Release"\r
+# PROP Ignore_Export_Lib 0\r
+# PROP Target_Dir ""\r
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c\r
+# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c\r
+# ADD BASE RSC /l 0x809 /d "NDEBUG"\r
+# ADD RSC /l 0x809 /d "NDEBUG"\r
+BSC32=bscmake.exe\r
+# ADD BASE BSC32 /nologo\r
+# ADD BSC32 /nologo\r
+LINK32=link.exe\r
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386\r
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /out:"nssm.exe"\r
+\r
+!ELSEIF "$(CFG)" == "nssm - Win32 Debug"\r
+\r
+# PROP BASE Use_MFC 0\r
+# PROP BASE Use_Debug_Libraries 1\r
+# PROP BASE Output_Dir "Debug"\r
+# PROP BASE Intermediate_Dir "Debug"\r
+# PROP BASE Target_Dir ""\r
+# PROP Use_MFC 0\r
+# PROP Use_Debug_Libraries 1\r
+# PROP Output_Dir "Debug"\r
+# PROP Intermediate_Dir "Debug"\r
+# PROP Ignore_Export_Lib 0\r
+# PROP Target_Dir ""\r
+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c\r
+# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c\r
+# SUBTRACT CPP /YX\r
+# ADD BASE RSC /l 0x809 /d "_DEBUG"\r
+# ADD RSC /l 0x809 /d "_DEBUG"\r
+BSC32=bscmake.exe\r
+# ADD BASE BSC32 /nologo\r
+# ADD BSC32 /nologo\r
+LINK32=link.exe\r
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept\r
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /out:"nssm_debug.exe" /pdbtype:sept\r
+\r
+!ENDIF \r
+\r
+# Begin Target\r
+\r
+# Name "nssm - Win32 Release"\r
+# Name "nssm - Win32 Debug"\r
+# Begin Group "Source Files"\r
+\r
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"\r
+# Begin Source File\r
+\r
+SOURCE=.\event.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\gui.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\nssm.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\registry.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\service.cpp\r
+# End Source File\r
+# End Group\r
+# Begin Group "Header Files"\r
+\r
+# PROP Default_Filter "h;hpp;hxx;hm;inl"\r
+# Begin Source File\r
+\r
+SOURCE=.\event.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\gui.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\nssm.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\registry.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\service.h\r
+# End Source File\r
+# End Group\r
+# Begin Group "Resource Files"\r
+\r
+# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"\r
+# Begin Source File\r
+\r
+SOURCE=.\nssm.ico\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\nssm.rc\r
+# End Source File\r
+# End Group\r
+# End Target\r
+# End Project\r
--- /dev/null
+Microsoft Developer Studio Workspace File, Format Version 6.00\r
+# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r
+\r
+###############################################################################\r
+\r
+Project: "nssm"=.\nssm.dsp - Package Owner=<4>\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<4>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
+Global:\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<3>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
--- /dev/null
+#ifndef NSSM_H\r
+#define NSSM_H\r
+\r
+#define _WIN32_WINNT 0x0500\r
+#include <stdarg.h>\r
+#include <stdio.h>\r
+#include <windows.h>\r
+#include "event.h"\r
+#include "registry.h"\r
+#include "service.h"\r
+#include "gui.h"\r
+\r
+#define NSSM "nssm"\r
+#define NSSM_VERSION "1.0"\r
+#define NSSM_DATE "2003-05-30"\r
+#define NSSM_RUN "run"\r
+\r
+#endif\r
--- /dev/null
+//Microsoft Developer Studio generated resource script.\r
+//\r
+#include "resource.h"\r
+\r
+#define APSTUDIO_READONLY_SYMBOLS\r
+/////////////////////////////////////////////////////////////////////////////\r
+//\r
+// Generated from the TEXTINCLUDE 2 resource.\r
+//\r
+#include "afxres.h"\r
+\r
+/////////////////////////////////////////////////////////////////////////////\r
+#undef APSTUDIO_READONLY_SYMBOLS\r
+\r
+/////////////////////////////////////////////////////////////////////////////\r
+// English (U.K.) resources\r
+\r
+#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENG)\r
+#ifdef _WIN32\r
+LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK\r
+#pragma code_page(1252)\r
+#endif //_WIN32\r
+\r
+#ifdef APSTUDIO_INVOKED\r
+/////////////////////////////////////////////////////////////////////////////\r
+//\r
+// TEXTINCLUDE\r
+//\r
+\r
+1 TEXTINCLUDE DISCARDABLE \r
+BEGIN\r
+ "resource.h\0"\r
+END\r
+\r
+2 TEXTINCLUDE DISCARDABLE \r
+BEGIN\r
+ "#include ""afxres.h""\r\n"\r
+ "\0"\r
+END\r
+\r
+3 TEXTINCLUDE DISCARDABLE \r
+BEGIN\r
+ "\r\n"\r
+ "\0"\r
+END\r
+\r
+#endif // APSTUDIO_INVOKED\r
+\r
+\r
+/////////////////////////////////////////////////////////////////////////////\r
+//\r
+// Icon\r
+//\r
+\r
+// Icon with lowest ID value placed first to ensure application icon\r
+// remains consistent on all systems.\r
+IDI_NSSM ICON DISCARDABLE "nssm.ico"\r
+\r
+/////////////////////////////////////////////////////////////////////////////\r
+//\r
+// Dialog\r
+//\r
+\r
+IDD_INSTALL DIALOG DISCARDABLE 0, 0, 220, 90\r
+STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU\r
+CAPTION "NSSM service installer"\r
+FONT 8, "MS Sans Serif"\r
+BEGIN\r
+ DEFPUSHBUTTON "Install service",IDC_OK,55,69,50,14\r
+ PUSHBUTTON "Cancel",IDC_CANCEL,111,69,50,14\r
+ EDITTEXT IDC_PATH,48,7,110,14,ES_AUTOHSCROLL\r
+ PUSHBUTTON "Browse",IDC_BROWSE,163,7,50,14\r
+ EDITTEXT IDC_FLAGS,48,28,165,14,ES_AUTOHSCROLL\r
+ LTEXT "Options:",IDC_STATIC,7,31,27,8\r
+ LTEXT "Service\nname:",IDC_STATIC,7,49,41,18\r
+ EDITTEXT IDC_NAME,48,49,77,14,ES_AUTOHSCROLL\r
+ LTEXT "Application:",IDC_STATIC,7,9,38,8\r
+END\r
+\r
+IDD_REMOVE DIALOG DISCARDABLE 0, 0, 223, 28\r
+STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU\r
+CAPTION "NSSM service remover"\r
+FONT 8, "MS Sans Serif"\r
+BEGIN\r
+ DEFPUSHBUTTON "Remove service",IDC_REMOVE,154,7,62,14\r
+ LTEXT "Service name:",IDC_STATIC,8,9,46,8\r
+ EDITTEXT IDC_NAME,59,7,87,14,ES_AUTOHSCROLL\r
+END\r
+\r
+\r
+/////////////////////////////////////////////////////////////////////////////\r
+//\r
+// DESIGNINFO\r
+//\r
+\r
+#ifdef APSTUDIO_INVOKED\r
+GUIDELINES DESIGNINFO DISCARDABLE \r
+BEGIN\r
+ IDD_INSTALL, DIALOG\r
+ BEGIN\r
+ VERTGUIDE, 7\r
+ VERTGUIDE, 48\r
+ VERTGUIDE, 213\r
+ BOTTOMMARGIN, 83\r
+ HORZGUIDE, 21\r
+ HORZGUIDE, 42\r
+ END\r
+END\r
+#endif // APSTUDIO_INVOKED\r
+\r
+#endif // English (U.K.) resources\r
+/////////////////////////////////////////////////////////////////////////////\r
+\r
+\r
+\r
+#ifndef APSTUDIO_INVOKED\r
+/////////////////////////////////////////////////////////////////////////////\r
+//\r
+// Generated from the TEXTINCLUDE 3 resource.\r
+//\r
+\r
+\r
+/////////////////////////////////////////////////////////////////////////////\r
+#endif // not APSTUDIO_INVOKED\r
+\r
--- /dev/null
+#include "nssm.h"\r
+\r
+int create_parameters(char *service_name, char *exe, char *flags, char *dir) {\r
+ /* Get registry */\r
+ char registry[MAX_PATH];\r
+ if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY, service_name) < 0) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Out of memory for NSSM_REGISTRY in create_parameters()!");\r
+ return 1;\r
+ }\r
+\r
+ /* Try to open the registry */\r
+ HKEY key;\r
+ if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, 0) != ERROR_SUCCESS) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't open service registry settings!", NSSM_REGISTRY);\r
+ return 2;\r
+ }\r
+\r
+ /* Try to create the parameters */\r
+ if (RegSetValueEx(key, NSSM_REG_EXE, 0, REG_SZ, (const unsigned char *) exe, strlen(exe) + 1) != ERROR_SUCCESS) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't add registry value %s: %s", NSSM_REG_EXE, error_string(GetLastError()));\r
+ RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
+ RegCloseKey(key);\r
+ return 3;\r
+ }\r
+ if (RegSetValueEx(key, NSSM_REG_FLAGS, 0, REG_SZ, (const unsigned char *) flags, strlen(flags) + 1) != ERROR_SUCCESS) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't add registry value %s: %s", NSSM_REG_FLAGS, error_string(GetLastError()));\r
+ RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
+ RegCloseKey(key);\r
+ return 4;\r
+ }\r
+ if (RegSetValueEx(key, NSSM_REG_DIR, 0, REG_SZ, (const unsigned char *) dir, strlen(dir) + 1) != ERROR_SUCCESS) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't add registry value %s: %s", NSSM_REG_DIR, error_string(GetLastError()));\r
+ RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
+ RegCloseKey(key);\r
+ return 5;\r
+ }\r
+\r
+ /* Close registry */\r
+ RegCloseKey(key);\r
+\r
+ return 0;\r
+}\r
+\r
+int get_parameters(char *service_name, char *exe, int exelen, char *flags, int flagslen, char *dir, int dirlen) {\r
+ /* Get registry */\r
+ char registry[MAX_PATH];\r
+ if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY, service_name) < 0) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Out of memory for NSSM_REGISTRY in get_parameters()!");\r
+ return 1;\r
+ }\r
+\r
+ /* Try to open the registry */\r
+ HKEY key;\r
+ if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, KEY_READ, &key) != ERROR_SUCCESS) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't open service registry settings!", NSSM_REGISTRY);\r
+ return 2;\r
+ }\r
+\r
+ unsigned long type = REG_SZ;\r
+\r
+ /* Try to get executable file - MUST succeed */\r
+ if (RegQueryValueEx(key, NSSM_REG_EXE, 0, &type, (unsigned char *) exe, (unsigned long *) &exelen) != ERROR_SUCCESS) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't get application path (registry value %s): %s", NSSM_REG_EXE, error_string(GetLastError()));\r
+ RegCloseKey(key);\r
+ return 3;\r
+ }\r
+\r
+ /* Try to get flags - may fail */\r
+ if (RegQueryValueEx(key, NSSM_REG_FLAGS, 0, &type, (unsigned char *) flags, (unsigned long *) &flagslen) != ERROR_SUCCESS) {\r
+ eventprintf(EVENTLOG_WARNING_TYPE, "Can't get application flags (registry value %s): %s", NSSM_REG_FLAGS, error_string(GetLastError()));\r
+ RegCloseKey(key);\r
+ return 4;\r
+ }\r
+\r
+ /* Try to get startup directory - may fail */\r
+ if (RegQueryValueEx(key, NSSM_REG_DIR, 0, &type, (unsigned char *) dir, (unsigned long *) &dirlen) != ERROR_SUCCESS) {\r
+ eventprintf(EVENTLOG_WARNING_TYPE, "Can't get application startup directory (registry value %s): %s", NSSM_REG_DIR, error_string(GetLastError()));\r
+ RegCloseKey(key);\r
+ return 5;\r
+ }\r
+\r
+ /* Close registry */\r
+ RegCloseKey(key);\r
+\r
+ return 0;\r
+}\r
--- /dev/null
+#ifndef REGISTRY_H\r
+#define REGISTRY_H\r
+\r
+#define NSSM_REGISTRY "SYSTEM\\CurrentControlSet\\Services\\%s\\Parameters"\r
+#define NSSM_REG_EXE "Application"\r
+#define NSSM_REG_FLAGS "AppParameters"\r
+#define NSSM_REG_DIR "AppDirectory"\r
+\r
+int create_parameters(char *, char *, char *, char *);\r
+int get_parameters(char *, char *, int, char *, int, char *, int);\r
+\r
+#endif\r
--- /dev/null
+//{{NO_DEPENDENCIES}}\r
+// Microsoft Developer Studio generated include file.\r
+// Used by nssm.rc\r
+//\r
+#define IDI_NSSM 101\r
+#define IDD_INSTALL 102\r
+#define IDD_REMOVE 103\r
+#define IDC_PATH 1000\r
+#define IDC_OK 1001\r
+#define IDC_CANCEL 1002\r
+#define IDC_BROWSE 1003\r
+#define IDC_FLAGS 1004\r
+#define IDC_NAME 1005\r
+#define IDC_REMOVE 1007\r
+\r
+// Next default values for new objects\r
+// \r
+#ifdef APSTUDIO_INVOKED\r
+#ifndef APSTUDIO_READONLY_SYMBOLS\r
+#define _APS_NEXT_RESOURCE_VALUE 104\r
+#define _APS_NEXT_COMMAND_VALUE 40001\r
+#define _APS_NEXT_CONTROL_VALUE 1009\r
+#define _APS_NEXT_SYMED_VALUE 101\r
+#endif\r
+#endif\r
--- /dev/null
+#include "nssm.h"\r
+\r
+SERVICE_STATUS service_status;\r
+SERVICE_STATUS_HANDLE service_handle;\r
+HANDLE wait_handle;\r
+HANDLE pid;\r
+char exe[MAX_PATH];\r
+char flags[MAX_PATH];\r
+char dir[MAX_PATH];\r
+\r
+/* Connect to the service manager */\r
+SC_HANDLE open_service_manager() {\r
+ SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);\r
+ if (! ret) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Unable to connect to service manager!\nPerhaps you need to be an administrator...");\r
+ return 0;\r
+ }\r
+\r
+ return ret;\r
+}\r
+\r
+/* Install the service */\r
+int install_service(char *name) {\r
+#ifdef GUI\r
+ /* Show the dialogue box */\r
+ return nssm_gui(IDD_INSTALL, name);\r
+#else\r
+ fprintf(stderr, "Unimplemented\n");\r
+ return 1;\r
+#endif\r
+}\r
+\r
+/* Remove the service */\r
+int remove_service(char *name) {\r
+#ifdef GUI\r
+ return nssm_gui(IDD_REMOVE, name);\r
+#else\r
+ fprintf(stderr, "Unimplemented\n");\r
+ return 1;\r
+#endif\r
+}\r
+\r
+/* Service initialisation */\r
+void WINAPI service_main(unsigned long argc, char **argv) {\r
+ /* Initialise status */\r
+ ZeroMemory(&service_status, sizeof(service_status));\r
+ service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;\r
+ service_status.dwCurrentState = SERVICE_RUNNING;\r
+ service_status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;\r
+ service_status.dwWin32ExitCode = NO_ERROR;\r
+ service_status.dwServiceSpecificExitCode = 0;\r
+ service_status.dwCheckPoint = 0;\r
+ service_status.dwWaitHint = 1000;\r
+\r
+ /* Signal we AREN'T running the server */\r
+ pid = 0;\r
+\r
+ /* Get startup parameters */\r
+ int ret = get_parameters(argv[0], exe, sizeof(exe), flags, sizeof(flags), dir, sizeof(dir));\r
+ if (ret) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "service_main(): Can't get startup parameters: error %d", ret);\r
+ return;\r
+ }\r
+\r
+ /* Register control handler */\r
+ service_handle = RegisterServiceCtrlHandlerEx(NSSM, service_control_handler, 0);\r
+ if (! service_handle) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "service_main(): RegisterServiceCtrlHandlerEx() failed: %s", error_string(GetLastError()));\r
+ return;\r
+ }\r
+\r
+ monitor_service();\r
+}\r
+\r
+int monitor_service() {\r
+ /* Set service status to started */\r
+ int ret = start_service();\r
+ if (ret) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't start service: error code %d", ret);\r
+ return ret;\r
+ }\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Started process %s %s in %s", exe, flags, dir);\r
+\r
+ /* Monitor service service */\r
+ if (! RegisterWaitForSingleObject(&wait_handle, pid, end_service, 0, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION)) {\r
+ eventprintf(EVENTLOG_WARNING_TYPE, "RegisterWaitForSingleObject() returned %s - service may claim to be still running when %s exits ", error_string(GetLastError()), exe);\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
+/* Service control handler */\r
+unsigned long WINAPI service_control_handler(unsigned long control, unsigned long event, void *data, void *context) {\r
+ switch (control) {\r
+ case SERVICE_CONTROL_SHUTDOWN:\r
+ case SERVICE_CONTROL_STOP:\r
+ stop_service(0);\r
+ return NO_ERROR;\r
+ }\r
+\r
+ /* Unknown control */\r
+ return ERROR_CALL_NOT_IMPLEMENTED;\r
+}\r
+\r
+/* Start the service */\r
+int start_service() {\r
+ if (pid) return 0;\r
+\r
+ /* Allocate a STARTUPINFO structure for a new process */\r
+ STARTUPINFO si;\r
+ ZeroMemory(&si, sizeof(si));\r
+ si.cb = sizeof(si);\r
+\r
+ /* Allocate a PROCESSINFO structure for the process */\r
+ PROCESS_INFORMATION pi;\r
+ ZeroMemory(&pi, sizeof(pi));\r
+\r
+ /* Launch executable with arguments */\r
+ char cmd[MAX_PATH];\r
+ if (_snprintf(cmd, sizeof(cmd), "%s %s", exe, flags) < 0) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Error constructing command line");\r
+ return stop_service(2);\r
+ }\r
+ if (! CreateProcess(0, cmd, 0, 0, 0, 0, 0, dir, &si, &pi)) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't launch %s. CreateProcess() returned %s", exe, error_string(GetLastError()));\r
+ return stop_service(3);\r
+ }\r
+ pid = pi.hProcess;\r
+\r
+ /* Signal successful start */\r
+ service_status.dwCurrentState = SERVICE_RUNNING;\r
+ SetServiceStatus(service_handle, &service_status);\r
+\r
+ return 0;\r
+}\r
+\r
+/* Stop the service */\r
+int stop_service(unsigned long exitcode) {\r
+ /* Signal we are stopping */\r
+ service_status.dwCurrentState = SERVICE_STOP_PENDING;\r
+ SetServiceStatus(service_handle, &service_status);\r
+\r
+ /* Nothing to do if server isn't running */\r
+ if (pid) {\r
+ /* Shut down server */\r
+ TerminateProcess(pid, 0);\r
+ pid = 0;\r
+ }\r
+\r
+ /* Signal we stopped */\r
+ service_status.dwCurrentState = SERVICE_STOPPED;\r
+ if (exitcode) {\r
+ service_status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;\r
+ service_status.dwServiceSpecificExitCode = exitcode;\r
+ }\r
+ else {\r
+ service_status.dwWin32ExitCode = NO_ERROR;\r
+ service_status.dwServiceSpecificExitCode = 0;\r
+ }\r
+ SetServiceStatus(service_handle, &service_status);\r
+\r
+ return exitcode;\r
+}\r
+\r
+/* Callback function triggered when the server exits */\r
+void CALLBACK end_service(void *arg, unsigned char why) {\r
+ /* Check exit code */\r
+ unsigned long ret = 0;\r
+ GetExitCodeProcess(pid, &ret);\r
+\r
+ /* Force an error code if none given, so system can restart this service */\r
+ /*if (! ret) {\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Process exited with return code 0 - overriding with return code 111 so the service manager will notice the failure");\r
+ ret = 111;\r
+ }\r
+ else */eventprintf(EVENTLOG_INFORMATION_TYPE, "Process %s exited with return code %u", exe, ret);\r
+\r
+ /* Try to restart the service or return failure code to service manager */\r
+ pid = 0;\r
+ while (monitor_service()) {\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Failed to restart %s - sleeping ", exe, ret);\r
+ Sleep(30000);\r
+ }\r
+}\r
--- /dev/null
+#ifndef SERVICE_H\r
+#define SERVICE_H\r
+\r
+void WINAPI service_main(unsigned long, char **);\r
+unsigned long WINAPI service_control_handler(unsigned long, unsigned long, void *, void *);\r
+\r
+SC_HANDLE open_service_manager();\r
+int install_service(char *);\r
+int remove_service(char *);\r
+int monitor_service();\r
+int start_service();\r
+int stop_service(unsigned long);\r
+void CALLBACK end_service(void *, unsigned char);\r
+\r
+#endif\r