X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=nssm.cpp;h=647ea40c57a33afa35543f559af7deb11471e19a;hb=5da5a6b4bfd384deee6276e02bf578c57ff0eff1;hp=781a228fc153be31cc2d2357964176e7aac77cbc;hpb=da3d37ae16f1af9735699ae444128b1224a7103f;p=nssm.git diff --git a/nssm.cpp b/nssm.cpp index 781a228..647ea40 100644 --- a/nssm.cpp +++ b/nssm.cpp @@ -4,6 +4,9 @@ extern unsigned long tls_index; extern bool is_admin; extern imports_t imports; +static TCHAR unquoted_imagepath[PATH_LENGTH]; +static TCHAR imagepath[PATH_LENGTH]; + /* Are two strings case-insensitively equivalent? */ int str_equiv(const TCHAR *a, const TCHAR *b) { size_t len = _tcslen(a); @@ -13,16 +16,20 @@ int str_equiv(const TCHAR *a, const TCHAR *b) { } /* Convert a string to a number. */ -int str_number(const TCHAR *string, unsigned long *number) { +int str_number(const TCHAR *string, unsigned long *number, TCHAR **bogus) { if (! string) return 1; - TCHAR *bogus; - *number = _tcstoul(string, &bogus, 0); - if (*bogus) return 2; + *number = _tcstoul(string, bogus, 0); + if (**bogus) return 2; return 0; } +int str_number(const TCHAR *string, unsigned long *number) { + TCHAR *bogus; + return str_number(string, number, &bogus); +} + /* Remove basename of a path. */ void strip_basename(TCHAR *buffer) { size_t len = _tcslen(buffer); @@ -35,8 +42,8 @@ void strip_basename(TCHAR *buffer) { /* How to use me correctly */ int usage(int ret) { - if (GetConsoleWindow()) print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE); - else popup_message(0, MB_OK, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE); + if (GetConsoleWindow()) print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_CONFIGURATION, NSSM_DATE); + else popup_message(0, MB_OK, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_CONFIGURATION, NSSM_DATE); return(ret); } @@ -51,54 +58,107 @@ void check_admin() { FreeSid(AdministratorsGroup); } -/* See if we were launched from a console window. */ -static void check_console() { - /* If we're running in a service context there will be no console window. */ - HWND console = GetConsoleWindow(); - if (! console) return; +static int elevate(int argc, TCHAR **argv, unsigned long message) { + print_message(stderr, message); - unsigned long pid; - if (! GetWindowThreadProcessId(console, &pid)) return; + SHELLEXECUTEINFO sei; + ZeroMemory(&sei, sizeof(sei)); + sei.cbSize = sizeof(sei); + sei.lpVerb = _T("runas"); + sei.lpFile = (TCHAR *) nssm_imagepath(); - /* - If the process associated with the console window handle is the same as - this process, we were not launched from an existing console. The user - probably double-clicked our executable. - */ - if (GetCurrentProcessId() != pid) return; + TCHAR *args = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, EXE_LENGTH * sizeof(TCHAR)); + if (! args) { + print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("GetCommandLine()"), _T("elevate()")); + return 111; + } + + /* Get command line, which includes the path to NSSM, and skip that part. */ + _sntprintf_s(args, EXE_LENGTH, _TRUNCATE, _T("%s"), GetCommandLine()); + size_t s = _tcslen(argv[0]) + 1; + if (args[0] == _T('"')) s += 2; + while (isspace(args[s])) s++; + + sei.lpParameters = args + s; + sei.nShow = SW_SHOW; + + unsigned long exitcode = 0; + if (! ShellExecuteEx(&sei)) exitcode = 100; - /* We close our new console so that subsequent messages appear in a popup. */ - FreeConsole(); + HeapFree(GetProcessHeap(), 0, (void *) args); + return exitcode; +} + +int num_cpus() { + DWORD_PTR i, affinity, system_affinity; + if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, &system_affinity)) return 64; + for (i = 0; system_affinity & (1LL << i); i++); + return (int) i; +} + +const TCHAR *nssm_unquoted_imagepath() { + return unquoted_imagepath; +} + +const TCHAR *nssm_imagepath() { + return imagepath; } int _tmain(int argc, TCHAR **argv) { check_console(); +#ifdef UNICODE + /* + Ensure we write in UTF-16 mode, so that non-ASCII characters don't get + mangled. If we were compiled in ANSI mode it won't work. + */ + _setmode(_fileno(stdout), _O_U16TEXT); + _setmode(_fileno(stderr), _O_U16TEXT); +#endif + /* Remember if we are admin */ check_admin(); + /* Set up function pointers. */ + if (get_imports()) exit(111); + + /* Remember our path for later. */ + GetModuleFileName(0, unquoted_imagepath, _countof(unquoted_imagepath)); + GetModuleFileName(0, imagepath, _countof(imagepath)); + PathQuoteSpaces(imagepath); + /* Elevate */ if (argc > 1) { - /* Valid commands are install, edit or remove */ + /* + Valid commands are: + start, stop, pause, continue, install, edit, get, set, reset, unset, remove + */ + if (str_equiv(argv[1], _T("start"))) exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2)); + if (str_equiv(argv[1], _T("stop"))) exit(control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2)); + if (str_equiv(argv[1], _T("restart"))) { + int ret = control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2); + if (ret) exit(ret); + exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2)); + } + if (str_equiv(argv[1], _T("pause"))) exit(control_service(SERVICE_CONTROL_PAUSE, argc - 2, argv + 2)); + if (str_equiv(argv[1], _T("continue"))) exit(control_service(SERVICE_CONTROL_CONTINUE, argc - 2, argv + 2)); + if (str_equiv(argv[1], _T("status"))) exit(control_service(SERVICE_CONTROL_INTERROGATE, argc - 2, argv + 2)); + if (str_equiv(argv[1], _T("rotate"))) exit(control_service(NSSM_SERVICE_CONTROL_ROTATE, argc - 2, argv + 2)); if (str_equiv(argv[1], _T("install"))) { - if (! is_admin) { - print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL); - exit(100); - } + if (! is_admin) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL)); + create_messages(); exit(pre_install_service(argc - 2, argv + 2)); } - if (str_equiv(argv[1], _T("edit"))) { - if (! is_admin) { - print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT); - exit(100); - } - exit(pre_edit_service(argc - 2, argv + 2)); + if (str_equiv(argv[1], _T("edit")) || str_equiv(argv[1], _T("get")) || str_equiv(argv[1], _T("set")) || str_equiv(argv[1], _T("reset")) || str_equiv(argv[1], _T("unset"))) { + int ret = pre_edit_service(argc - 1, argv + 1); + if (ret == 3 && ! is_admin && argc == 3) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT)); + /* There might be a password here. */ + for (int i = 0; i < argc; i++) SecureZeroMemory(argv[i], _tcslen(argv[i]) * sizeof(TCHAR)); + exit(ret); } + if (str_equiv(argv[1], _T("list"))) exit(list_nssm_services()); if (str_equiv(argv[1], _T("remove"))) { - if (! is_admin) { - print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE); - exit(100); - } + if (! is_admin) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE)); exit(pre_remove_service(argc - 2, argv + 2)); } } @@ -119,10 +179,7 @@ int _tmain(int argc, TCHAR **argv) { actually running as a service. This will save time when running with no arguments from a command prompt. */ - if (_fileno(stdin) < 0) { - /* Set up function pointers. */ - if (get_imports()) exit(111); - + if (! GetStdHandle(STD_INPUT_HANDLE)) { /* Start service magic */ SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } }; if (! StartServiceCtrlDispatcher(table)) {