X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=nssm.cpp;h=e29ead6531ab653f835ed445cc95daa405d6724b;hb=cca8d28295ce27b7c996b47badc6a1e3a6a34e65;hp=9f3174fb2417857a4dccc78bf1517799fb982699;hpb=03f6899464b863fc2dd05e7c8e5047e0c1b9281d;p=nssm.git diff --git a/nssm.cpp b/nssm.cpp index 9f3174f..e29ead6 100644 --- a/nssm.cpp +++ b/nssm.cpp @@ -55,43 +55,48 @@ 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(); + /* Set up function pointers. */ + if (get_imports()) exit(111); + /* Elevate */ if (argc > 1) { /* 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("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); @@ -134,10 +139,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)) {