2012-09-18 23:51:33 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Process Thread Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2014-12-02 10:53:58 +03:00
|
|
|
* Copyright 2014 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2012-09-18 23:51:33 +04:00
|
|
|
*
|
|
|
|
* 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>
|
2014-12-02 10:53:58 +03:00
|
|
|
#include "../handle/nonehandle.h"
|
2012-09-18 23:51:33 +04:00
|
|
|
|
|
|
|
#include <winpr/thread.h>
|
2013-10-02 19:21:56 +04:00
|
|
|
#include <fcntl.h>
|
2012-09-18 23:51:33 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-24 19:03:42 +04:00
|
|
|
#include <winpr/heap.h>
|
|
|
|
#include <winpr/path.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
|
|
|
|
2013-09-24 21:08:29 +04:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
2013-09-23 01:23:00 +04:00
|
|
|
#include <errno.h>
|
2013-09-24 19:03:42 +04:00
|
|
|
#include <string.h>
|
2013-10-25 18:03:57 +04:00
|
|
|
#include <signal.h>
|
2013-09-23 01:23:00 +04:00
|
|
|
#include <sys/wait.h>
|
2013-10-25 18:03:57 +04:00
|
|
|
#include <sys/types.h>
|
2013-09-23 01:23:00 +04:00
|
|
|
|
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"
|
2013-09-24 19:03:42 +04:00
|
|
|
#include "../security/security.h"
|
2012-09-18 23:51:33 +04:00
|
|
|
|
2014-12-02 11:08:10 +03:00
|
|
|
static HANDLE_CLOSE_CB _ProcessHandleCloseCb;
|
|
|
|
static pthread_once_t process_initialized = PTHREAD_ONCE_INIT;
|
|
|
|
|
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;
|
|
|
|
|
2014-11-16 19:02:36 +03:00
|
|
|
envp = (char**) calloc(count + 1, sizeof(char*));
|
2013-09-23 22:44:59 +04:00
|
|
|
envp[count] = NULL;
|
|
|
|
|
|
|
|
while (p[0] && p[1])
|
|
|
|
{
|
|
|
|
length = strlen(p);
|
|
|
|
envp[index] = _strdup(p);
|
|
|
|
p += (length + 1);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return envp;
|
|
|
|
}
|
|
|
|
|
2013-09-24 19:03:42 +04:00
|
|
|
/**
|
|
|
|
* If the file name does not contain a directory path, the system searches for the executable file in the following sequence:
|
|
|
|
*
|
|
|
|
* 1) The directory from which the application loaded.
|
|
|
|
* 2) The current directory for the parent process.
|
|
|
|
* 3) The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
|
|
|
|
* 4) The 16-bit Windows system directory. There is no function that obtains the path of this directory,
|
|
|
|
* but it is searched. The name of this directory is System.
|
|
|
|
* 5) The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
|
|
|
|
* 6) The directories that are listed in the PATH environment variable. Note that this function
|
|
|
|
* does not search the per-application path specified by the App Paths registry key. To include
|
|
|
|
* this per-application path in the search sequence, use the ShellExecute function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char* FindApplicationPath(char* application)
|
|
|
|
{
|
|
|
|
char* path;
|
|
|
|
char* save;
|
|
|
|
DWORD nSize;
|
|
|
|
LPSTR lpSystemPath;
|
|
|
|
char* filename = NULL;
|
|
|
|
|
|
|
|
if (!application)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (application[0] == '/')
|
2013-10-31 13:43:59 +04:00
|
|
|
return _strdup(application);
|
2013-09-24 19:03:42 +04:00
|
|
|
|
|
|
|
nSize = GetEnvironmentVariableA("PATH", NULL, 0);
|
|
|
|
|
|
|
|
if (!nSize)
|
|
|
|
return application;
|
|
|
|
|
|
|
|
lpSystemPath = (LPSTR) malloc(nSize);
|
|
|
|
nSize = GetEnvironmentVariableA("PATH", lpSystemPath, nSize);
|
|
|
|
|
|
|
|
save = NULL;
|
|
|
|
path = strtok_s(lpSystemPath, ":", &save);
|
|
|
|
|
|
|
|
while (path)
|
|
|
|
{
|
|
|
|
filename = GetCombinedPath(path, application);
|
|
|
|
|
|
|
|
if (PathFileExistsA(filename))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(filename);
|
|
|
|
filename = NULL;
|
|
|
|
|
|
|
|
path = strtok_s(NULL, ":", &save);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(lpSystemPath);
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2014-12-07 03:15:49 +03:00
|
|
|
static HANDLE CreateProcessHandle(pid_t pid);
|
|
|
|
static BOOL ProcessHandleCloseHandle(HANDLE handle);
|
2014-12-02 11:08:10 +03:00
|
|
|
|
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;
|
2013-09-24 19:03:42 +04:00
|
|
|
int flags;
|
2013-09-23 01:23:00 +04:00
|
|
|
int numArgs;
|
2013-11-21 23:06:21 +04:00
|
|
|
LPSTR* pArgs = NULL;
|
|
|
|
char** envp = NULL;
|
2013-10-31 13:19:55 +04:00
|
|
|
char* filename = NULL;
|
2014-12-02 10:53:58 +03:00
|
|
|
HANDLE thread;
|
2014-12-02 11:08:10 +03:00
|
|
|
HANDLE process;
|
2013-09-24 19:03:42 +04:00
|
|
|
WINPR_ACCESS_TOKEN* token;
|
2013-09-23 22:44:59 +04:00
|
|
|
LPTCH lpszEnvironmentBlock;
|
2013-11-21 23:06:21 +04:00
|
|
|
BOOL ret = FALSE;
|
2013-09-23 01:23:00 +04:00
|
|
|
|
|
|
|
pid = 0;
|
|
|
|
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-24 19:03:42 +04:00
|
|
|
flags = 0;
|
|
|
|
|
|
|
|
token = (WINPR_ACCESS_TOKEN*) hToken;
|
|
|
|
|
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-24 19:03:42 +04:00
|
|
|
filename = FindApplicationPath(pArgs[0]);
|
2013-11-21 23:06:21 +04:00
|
|
|
if (NULL == filename)
|
|
|
|
goto finish;
|
2013-09-24 19:03:42 +04:00
|
|
|
|
2013-09-24 21:08:29 +04:00
|
|
|
/* fork and exec */
|
|
|
|
|
|
|
|
pid = fork();
|
2013-09-23 01:23:00 +04:00
|
|
|
|
2013-09-24 21:08:29 +04:00
|
|
|
if (pid < 0)
|
|
|
|
{
|
|
|
|
/* fork failure */
|
2013-11-21 23:06:21 +04:00
|
|
|
goto finish;
|
2013-09-24 21:08:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
/* child process */
|
2013-10-02 19:21:56 +04:00
|
|
|
#ifdef __sun
|
|
|
|
closefrom(3);
|
|
|
|
#else
|
|
|
|
int maxfd;
|
|
|
|
#ifdef F_MAXFD // on some BSD derivates
|
|
|
|
maxfd = fcntl(0, F_MAXFD);
|
|
|
|
#else
|
|
|
|
maxfd = sysconf(_SC_OPEN_MAX);
|
|
|
|
#endif
|
|
|
|
int fd;
|
|
|
|
for(fd=3; fd<maxfd; fd++)
|
|
|
|
close(fd);
|
|
|
|
#endif // __sun
|
2013-09-24 21:08:29 +04:00
|
|
|
|
|
|
|
if (token)
|
|
|
|
{
|
|
|
|
if (token->GroupId)
|
|
|
|
{
|
2014-11-16 17:59:26 +03:00
|
|
|
int rc = setgid((gid_t) token->GroupId);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
initgroups(token->Username, (gid_t) token->GroupId);
|
|
|
|
}
|
2013-09-24 21:08:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (token->UserId)
|
|
|
|
setuid((uid_t) token->UserId);
|
2014-02-13 17:26:26 +04:00
|
|
|
|
|
|
|
/* TODO: add better cwd handling and error checking */
|
|
|
|
if (lpCurrentDirectory && strlen(lpCurrentDirectory) > 0)
|
|
|
|
chdir(lpCurrentDirectory);
|
2013-09-24 21:08:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (execve(filename, pArgs, envp) < 0)
|
|
|
|
{
|
2013-11-21 23:06:21 +04:00
|
|
|
/* execve failed - end the process */
|
|
|
|
_exit(1);
|
2013-09-24 21:08:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* parent process */
|
|
|
|
}
|
2013-09-23 01:23:00 +04:00
|
|
|
|
2014-12-02 11:08:10 +03:00
|
|
|
process = CreateProcessHandle(pid);
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2013-09-23 01:23:00 +04:00
|
|
|
if (!process)
|
2013-11-21 23:06:21 +04:00
|
|
|
{
|
|
|
|
goto finish;
|
|
|
|
}
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2014-12-02 10:53:58 +03:00
|
|
|
thread = CreateNoneHandle();
|
2013-09-23 01:23:00 +04:00
|
|
|
|
|
|
|
if (!thread)
|
2013-11-21 23:06:21 +04:00
|
|
|
{
|
2014-12-07 03:04:30 +03:00
|
|
|
ProcessHandleCloseHandle(process);
|
2013-11-21 23:06:21 +04:00
|
|
|
goto finish;
|
|
|
|
}
|
2013-09-23 01:23:00 +04:00
|
|
|
|
2014-12-02 11:08:10 +03:00
|
|
|
lpProcessInformation->hProcess = process;
|
2014-12-02 10:53:58 +03:00
|
|
|
lpProcessInformation->hThread = thread;
|
2013-09-23 01:23:00 +04:00
|
|
|
lpProcessInformation->dwProcessId = (DWORD) pid;
|
|
|
|
lpProcessInformation->dwThreadId = (DWORD) pid;
|
2013-09-22 01:16:49 +04:00
|
|
|
|
2013-11-21 23:06:21 +04:00
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
if (filename)
|
|
|
|
{
|
|
|
|
free(filename);
|
|
|
|
}
|
2013-09-24 19:03:42 +04:00
|
|
|
|
|
|
|
if (pArgs)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, pArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpszEnvironmentBlock)
|
|
|
|
FreeEnvironmentStrings(lpszEnvironmentBlock);
|
|
|
|
|
|
|
|
if (envp)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (envp[i])
|
|
|
|
{
|
|
|
|
free(envp[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(envp);
|
|
|
|
}
|
|
|
|
|
2013-11-21 23:06:21 +04:00
|
|
|
return ret;
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
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-09-24 22:35:03 +04:00
|
|
|
VOID ExitProcess(UINT uExitCode)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
2013-09-24 22:35:03 +04:00
|
|
|
exit((int) 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)
|
|
|
|
{
|
2013-09-24 22:35:03 +04:00
|
|
|
WINPR_PROCESS* process;
|
|
|
|
|
|
|
|
process = (WINPR_PROCESS*) Process;
|
|
|
|
|
|
|
|
if (!process)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (DWORD) process->pid;
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL TerminateProcess(HANDLE hProcess, UINT uExitCode)
|
|
|
|
{
|
2013-09-24 22:35:03 +04:00
|
|
|
WINPR_PROCESS* process;
|
|
|
|
|
|
|
|
process = (WINPR_PROCESS*) hProcess;
|
|
|
|
|
|
|
|
if (!process)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (kill(process->pid, SIGTERM))
|
|
|
|
return FALSE;
|
|
|
|
|
2012-09-18 23:51:33 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-12-02 11:08:10 +03:00
|
|
|
|
2014-12-07 03:15:49 +03:00
|
|
|
BOOL ProcessHandleCloseHandle(HANDLE handle)
|
2014-12-02 11:08:10 +03:00
|
|
|
{
|
|
|
|
WINPR_PROCESS* process = (WINPR_PROCESS*) handle;
|
|
|
|
free(process);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL ProcessHandleIsHandle(HANDLE handle)
|
|
|
|
{
|
|
|
|
WINPR_PROCESS* process = (WINPR_PROCESS*) handle;
|
|
|
|
|
|
|
|
if (!process || process->Type != HANDLE_TYPE_PROCESS)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProcessHandleInitialize(void)
|
|
|
|
{
|
|
|
|
_ProcessHandleCloseCb.IsHandled = ProcessHandleIsHandle;
|
|
|
|
_ProcessHandleCloseCb.CloseHandle = ProcessHandleCloseHandle;
|
|
|
|
RegisterHandleCloseCb(&_ProcessHandleCloseCb);
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE CreateProcessHandle(pid_t pid)
|
|
|
|
{
|
|
|
|
WINPR_PROCESS* process;
|
|
|
|
process = (WINPR_PROCESS*) calloc(1, sizeof(WINPR_PROCESS));
|
|
|
|
|
|
|
|
if (!process)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pthread_once(&process_initialized, ProcessHandleInitialize);
|
|
|
|
process->pid = pid;
|
|
|
|
process->Type = HANDLE_TYPE_PROCESS;
|
|
|
|
|
|
|
|
return (HANDLE)process;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-18 23:51:33 +04:00
|
|
|
#endif
|
|
|
|
|