X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=nssm.cpp;h=498a213abea9455ec7b14d58e4e5b8bdb8db7f4e;hb=2cd1c7c29ef4d2d3df3c5afd3ca6c788aede2bef;hp=7c956112065144957696bcc1e0dc6b85a2df413f;hpb=2c60e5334f6df07bf42e7a91cf59638453eca473;p=nssm.git diff --git a/nssm.cpp b/nssm.cpp index 7c95611..498a213 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,7 +39,8 @@ void strip_basename(TCHAR *buffer) { /* How to use me correctly */ int usage(int ret) { - print_message(stderr, 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); } @@ -39,13 +55,60 @@ 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(0, 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("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); @@ -53,12 +116,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) {