X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=process.cpp;h=69aea030a5d60e5950843cc2ac5f28207df759fc;hb=53371f115d94fbbc7e5cb60853b9e4a5d356d4b0;hp=1cc6a73dfbdb163453be30797756c3686056ea6a;hpb=5b9e64a9ae1fbf1254c9c246e5b123d3aa77a37a;p=nssm.git diff --git a/process.cpp b/process.cpp index 1cc6a73..69aea03 100644 --- a/process.cpp +++ b/process.cpp @@ -306,3 +306,39 @@ void kill_process_tree(nssm_service_t *service, unsigned long pid, unsigned long CloseHandle(process_handle); } + +/* + Verify an environment block. + Returns: 1 if environment is invalid. + 0 if environment is OK. + -1 on error. +*/ +int test_environment(TCHAR *env) { + TCHAR path[MAX_PATH]; + GetModuleFileName(0, path, _countof(path)); + STARTUPINFO si; + ZeroMemory(&si, sizeof(si)); + si.cb = sizeof(si); + PROCESS_INFORMATION pi; + ZeroMemory(&pi, sizeof(pi)); + unsigned long flags = CREATE_SUSPENDED; +#ifdef UNICODE + flags |= CREATE_UNICODE_ENVIRONMENT; +#endif + + /* + Try to relaunch ourselves but with the candidate environment set. + Assuming no solar flare activity, the only reason this would fail is if + the environment were invalid. + */ + if (CreateProcess(0, path, 0, 0, 0, flags, env, 0, &si, &pi)) { + TerminateProcess(pi.hProcess, 0); + } + else { + unsigned long error = GetLastError(); + if (error == ERROR_INVALID_PARAMETER) return 1; + else return -1; + } + + return 0; +}