* Handle Management
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
+ * Copyright 2014 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
LeaveCriticalSection(&_HandleCloseCbsLock);
- if (Type == HANDLE_TYPE_PROCESS)
- {
- WINPR_PROCESS* process;
- process = (WINPR_PROCESS*) Object;
- free(process);
- return TRUE;
- }
- else if (Type == HANDLE_TYPE_MUTEX)
+ if (Type == HANDLE_TYPE_MUTEX)
{
WINPR_MUTEX* mutex;
mutex = (WINPR_MUTEX*) Object;
#include "../handle/handle.h"
#include "../security/security.h"
+static HANDLE_CLOSE_CB _ProcessHandleCloseCb;
+static pthread_once_t process_initialized = PTHREAD_ONCE_INIT;
+
char** EnvironmentBlockToEnvpA(LPCH lpszEnvironmentBlock)
{
char* p;
return filename;
}
+HANDLE CreateProcessHandle(pid_t pid);
+
BOOL _CreateProcessExA(HANDLE hToken, DWORD dwLogonFlags,
LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment,
char** envp = NULL;
char* filename = NULL;
HANDLE thread;
- WINPR_PROCESS* process;
+ HANDLE process;
WINPR_ACCESS_TOKEN* token;
LPTCH lpszEnvironmentBlock;
BOOL ret = FALSE;
/* parent process */
}
- process = (WINPR_PROCESS*) malloc(sizeof(WINPR_PROCESS));
+ process = CreateProcessHandle(pid);
if (!process)
{
goto finish;
}
- ZeroMemory(process, sizeof(WINPR_PROCESS));
-
- WINPR_HANDLE_SET_TYPE(process, HANDLE_TYPE_PROCESS);
-
- process->pid = pid;
- process->status = 0;
- process->dwExitCode = 0;
-
thread = CreateNoneHandle();
if (!thread)
goto finish;
}
- lpProcessInformation->hProcess = (HANDLE) process;
+ lpProcessInformation->hProcess = process;
lpProcessInformation->hThread = thread;
lpProcessInformation->dwProcessId = (DWORD) pid;
lpProcessInformation->dwThreadId = (DWORD) pid;
return TRUE;
}
+
+static BOOL ProcessHandleCloseHandle(HANDLE handle)
+{
+ 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;
+}
+
+
#endif