X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=nssm.cpp;h=6456c9735c4066a8f7fed4222def6077714a8b36;hb=b0a6672810ee06052dc3dcf268274d06f0e82a50;hp=02ad21f28c2000040f093367265a4dbddec7e47d;hpb=8754d6de6c36a1859ad0adfef1e456b4a2ebd3a6;p=nssm.git diff --git a/nssm.cpp b/nssm.cpp index 02ad21f..6456c97 100644 --- a/nssm.cpp +++ b/nssm.cpp @@ -12,6 +12,21 @@ int str_equiv(const TCHAR *a, const TCHAR *b) { return 1; } +/* Convert a string to a number. */ +int str_number(const TCHAR *string, unsigned long *number, TCHAR **bogus) { + if (! string) return 1; + + *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); @@ -24,8 +39,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); } @@ -40,35 +55,45 @@ 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; - - unsigned long pid; - if (! GetWindowThreadProcessId(console, &pid)) return; - - /* - 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; - - /* We close our new console so that subsequent messages appear in a popup. */ - FreeConsole(); +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; } 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(); /* 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); @@ -76,12 +101,15 @@ int _tmain(int argc, TCHAR **argv) { } exit(pre_install_service(argc - 2, argv + 2)); } - if (str_equiv(argv[1], _T("edit"))) { + 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"))) { if (! is_admin) { print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT); exit(100); } - exit(pre_edit_service(argc - 2, argv + 2)); + int ret = pre_edit_service(argc - 1, argv + 1); + /* 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("remove"))) { if (! is_admin) {