2012-09-18 23:51:33 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Process Thread Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/handle.h>
|
|
|
|
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CreateProcessA
|
|
|
|
* CreateProcessW
|
|
|
|
* CreateProcessAsUserA
|
|
|
|
* CreateProcessAsUserW
|
|
|
|
* ExitProcess
|
|
|
|
* GetCurrentProcess
|
|
|
|
* GetCurrentProcessId
|
|
|
|
* GetExitCodeProcess
|
|
|
|
* GetProcessHandleCount
|
|
|
|
* GetProcessId
|
|
|
|
* GetProcessIdOfThread
|
|
|
|
* GetProcessMitigationPolicy
|
|
|
|
* GetProcessTimes
|
|
|
|
* GetProcessVersion
|
|
|
|
* OpenProcess
|
|
|
|
* OpenProcessToken
|
|
|
|
* ProcessIdToSessionId
|
|
|
|
* SetProcessAffinityUpdateMode
|
|
|
|
* SetProcessMitigationPolicy
|
|
|
|
* SetProcessShutdownParameters
|
|
|
|
* TerminateProcess
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
2013-01-18 22:50:35 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2013-09-23 01:23:00 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-09-23 22:44:59 +04:00
|
|
|
#include <winpr/tchar.h>
|
|
|
|
#include <winpr/environment.h>
|
2013-09-23 01:23:00 +04:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <spawn.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2012-09-18 23:51:33 +04:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2013-09-23 01:23:00 +04:00
|
|
|
#include "thread.h"
|
|
|
|
|
|
|
|
#include "../handle/handle.h"
|
2012-09-18 23:51:33 +04:00
|
|
|
|
2013-09-23 22:44:59 +04:00
|
|
|
char** EnvironmentBlockToEnvpA(LPCH lpszEnvironmentBlock)
|
|
|
|
{
|
|
|
|
char* p;
|
|
|
|
int index;
|
|
|
|
int count;
|
|
|
|
int length;
|
|
|
|
char** envp = NULL;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
p = (char*) lpszEnvironmentBlock;
|
|
|
|
|
|
|
|
while (p[0] && p[1])
|
|
|
|
{
|
|
|
|
length = strlen(p);
|
|
|
|
p += (length + 1);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
p = (char*) lpszEnvironmentBlock;
|
|
|
|
|
|
|
|
envp = (char**) malloc(sizeof(char*) * (count + 1));
|
|
|
|
envp[count] = NULL;
|
|
|
|
|
|
|
|
while (p[0] && p[1])
|
|
|
|
{
|
|
|
|
length = strlen(p);
|
|
|
|
envp[index] = _strdup(p);
|
|
|
|
p += (length + 1);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return envp;
|
|
|
|
}
|
|
|
|
|
2013-09-24 08:07:48 +04:00
|
|
|
BOOL _CreateProcessExA(HANDLE hToken, DWORD dwLogonFlags,
|
|
|
|
LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
2012-09-18 23:51:33 +04:00
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment,
|
|
|
|
LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
|
|
|
{
|
2013-09-23 01:23:00 +04:00
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
int numArgs;
|
|
|
|
LPSTR* pArgs;
|
2013-09-23 22:44:59 +04:00
|
|
|
char** envp;
|
2013-09-23 01:23:00 +04:00
|
|
|
WINPR_THREAD* thread;
|
|
|
|
WINPR_PROCESS* process;
|
2013-09-23 22:44:59 +04:00
|
|
|
LPTCH lpszEnvironmentBlock;
|
2013-09-23 01:23:00 +04:00
|
|
|
|
|
|
|
pid = 0;
|
2013-09-23 22:44:59 +04:00
|
|
|
envp = NULL;
|
2013-09-23 01:23:00 +04:00
|
|
|
numArgs = 0;
|
2013-09-23 22:44:59 +04:00
|
|
|
lpszEnvironmentBlock = NULL;
|
2013-09-23 01:23:00 +04:00
|
|
|
|
|
|
|
pArgs = CommandLineToArgvA(lpCommandLine, &numArgs);
|
|
|
|
|
2013-09-23 22:44:59 +04:00
|
|
|
if (lpEnvironment)
|
|
|
|
{
|
|
|
|
envp = EnvironmentBlockToEnvpA(lpEnvironment);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lpszEnvironmentBlock = GetEnvironmentStrings();
|
|
|
|
envp = EnvironmentBlockToEnvpA(lpszEnvironmentBlock);
|
|
|
|
}
|
2013-09-23 01:23:00 +04:00
|
|
|
|
2013-09-23 22:44:59 +04:00
|
|
|
status = posix_spawnp(&pid, pArgs[0], NULL, NULL, pArgs, envp);
|
2013-09-23 01:23:00 +04:00
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
process = (WINPR_PROCESS*) malloc(sizeof(WINPR_PROCESS));
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2013-09-23 01:23:00 +04:00
|
|
|
if (!process)
|
2013-09-22 01:16:49 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2013-09-23 01:23:00 +04:00
|
|
|
ZeroMemory(process, sizeof(WINPR_PROCESS));
|
|
|
|
|
|
|
|
WINPR_HANDLE_SET_TYPE(process, HANDLE_TYPE_PROCESS);
|
|
|
|
|
|
|
|
process->pid = pid;
|
|
|
|
process->status = 0;
|
2013-09-23 22:44:59 +04:00
|
|
|
process->dwExitCode = 0;
|
2013-09-23 01:23:00 +04:00
|
|
|
|
|
|
|
thread = (WINPR_THREAD*) malloc(sizeof(WINPR_THREAD));
|
|
|
|
|
|
|
|
ZeroMemory(thread, sizeof(WINPR_THREAD));
|
|
|
|
|
|
|
|
if (!thread)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
WINPR_HANDLE_SET_TYPE(thread, HANDLE_TYPE_THREAD);
|
|
|
|
|
|
|
|
thread->mainProcess = TRUE;
|
|
|
|
|
|
|
|
lpProcessInformation->hProcess = (HANDLE) process;
|
|
|
|
lpProcessInformation->hThread = (HANDLE) thread;
|
|
|
|
lpProcessInformation->dwProcessId = (DWORD) pid;
|
|
|
|
lpProcessInformation->dwThreadId = (DWORD) pid;
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2012-09-18 23:51:33 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-09-24 08:07:48 +04:00
|
|
|
BOOL CreateProcessA(LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment,
|
|
|
|
LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
|
|
|
{
|
|
|
|
return _CreateProcessExA(NULL, 0,
|
|
|
|
lpApplicationName, lpCommandLine, lpProcessAttributes,
|
|
|
|
lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment,
|
|
|
|
lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
|
|
|
|
}
|
|
|
|
|
2012-09-18 23:51:33 +04:00
|
|
|
BOOL CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment,
|
|
|
|
LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CreateProcessAsUserA(HANDLE hToken, LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment,
|
|
|
|
LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
|
|
|
{
|
2013-09-24 08:07:48 +04:00
|
|
|
return _CreateProcessExA(hToken, 0,
|
|
|
|
lpApplicationName, lpCommandLine, lpProcessAttributes,
|
|
|
|
lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment,
|
|
|
|
lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CreateProcessAsUserW(HANDLE hToken, LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment,
|
|
|
|
LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-09-24 06:39:28 +04:00
|
|
|
BOOL CreateProcessWithLogonA(LPCSTR lpUsername, LPCSTR lpDomain, LPCSTR lpPassword, DWORD dwLogonFlags,
|
|
|
|
LPCSTR lpApplicationName, LPSTR lpCommandLine, DWORD dwCreationFlags, LPVOID lpEnvironment,
|
|
|
|
LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CreateProcessWithLogonW(LPCWSTR lpUsername, LPCWSTR lpDomain, LPCWSTR lpPassword, DWORD dwLogonFlags,
|
|
|
|
LPCWSTR lpApplicationName, LPWSTR lpCommandLine, DWORD dwCreationFlags, LPVOID lpEnvironment,
|
|
|
|
LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CreateProcessWithTokenA(HANDLE hToken, DWORD dwLogonFlags,
|
|
|
|
LPCSTR lpApplicationName, LPSTR lpCommandLine, DWORD dwCreationFlags, LPVOID lpEnvironment,
|
|
|
|
LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
|
|
|
{
|
2013-09-24 08:07:48 +04:00
|
|
|
return _CreateProcessExA(NULL, 0,
|
|
|
|
lpApplicationName, lpCommandLine, NULL,
|
|
|
|
NULL, FALSE, dwCreationFlags, lpEnvironment,
|
|
|
|
lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
|
2013-09-24 06:39:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CreateProcessWithTokenW(HANDLE hToken, DWORD dwLogonFlags,
|
|
|
|
LPCWSTR lpApplicationName, LPWSTR lpCommandLine, DWORD dwCreationFlags, LPVOID lpEnvironment,
|
|
|
|
LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-08-10 05:23:08 +04:00
|
|
|
DECLSPEC_NORETURN VOID ExitProcess(UINT uExitCode)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-23 22:44:59 +04:00
|
|
|
BOOL GetExitCodeProcess(HANDLE hProcess, LPDWORD lpExitCode)
|
|
|
|
{
|
|
|
|
WINPR_PROCESS* process;
|
|
|
|
|
|
|
|
if (!hProcess)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!lpExitCode)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
process = (WINPR_PROCESS*) hProcess;
|
|
|
|
|
|
|
|
*lpExitCode = process->dwExitCode;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-01-20 05:32:14 +04:00
|
|
|
HANDLE _GetCurrentProcess(VOID)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD GetCurrentProcessId(VOID)
|
|
|
|
{
|
2013-01-18 22:50:35 +04:00
|
|
|
return ((DWORD) getpid());
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD GetProcessId(HANDLE Process)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL TerminateProcess(HANDLE hProcess, UINT uExitCode)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|