diff --git a/winpr/libwinpr/handle/CMakeLists.txt b/winpr/libwinpr/handle/CMakeLists.txt index d58ff5a37..09ea2f13d 100644 --- a/winpr/libwinpr/handle/CMakeLists.txt +++ b/winpr/libwinpr/handle/CMakeLists.txt @@ -2,6 +2,7 @@ # libwinpr-handle cmake build script # # Copyright 2012 Marc-Andre Moreau +# Copyright 2014 DI (FH) Martin Haimberger # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,7 +16,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -winpr_module_add(handle.c handle.h) +winpr_module_add(handle.c handle.h nonehandle.c nonehandle.h) if(${CMAKE_SYSTEM_NAME} MATCHES SunOS) winpr_library_add(rt) diff --git a/winpr/libwinpr/handle/nonehandle.c b/winpr/libwinpr/handle/nonehandle.c new file mode 100644 index 000000000..bbb2cb4f7 --- /dev/null +++ b/winpr/libwinpr/handle/nonehandle.c @@ -0,0 +1,73 @@ +/** + * WinPR: Windows Portable Runtime + * NoneHandle a.k.a. brathandle should be used where a handle is needed, but + * functionality is not implemented yet or not implementable. + * + * Copyright 2014 DI (FH) Martin Haimberger + * + * 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 "nonehandle.h" +#include + +#ifndef _WIN32 + +static HANDLE_CLOSE_CB _NoneHandleCloseCb; +static pthread_once_t none_initialized = PTHREAD_ONCE_INIT; + +static BOOL NoneHandleCloseHandle(HANDLE handle) +{ + WINPR_NONE_HANDLE* none = (WINPR_NONE_HANDLE*) handle; + free(none); + return TRUE; +} + +static BOOL NoneHandleIsHandle(HANDLE handle) +{ + WINPR_NONE_HANDLE* none = (WINPR_NONE_HANDLE*) handle; + + if (!none || none->Type != HANDLE_TYPE_NONE) + { + SetLastError(ERROR_INVALID_HANDLE); + return FALSE; + } + + return TRUE; +} + +static void NoneHandleInitialize(void) +{ + _NoneHandleCloseCb.IsHandled = NoneHandleIsHandle; + _NoneHandleCloseCb.CloseHandle = NoneHandleCloseHandle; + RegisterHandleCloseCb(&_NoneHandleCloseCb); +} + +HANDLE CreateNoneHandle() +{ + WINPR_NONE_HANDLE* none; + none = (WINPR_NONE_HANDLE*) calloc(1, sizeof(WINPR_NONE_HANDLE)); + + if (!none) + return NULL; + + pthread_once(&none_initialized, NoneHandleInitialize); + + return (HANDLE)none; +} + +#endif diff --git a/winpr/libwinpr/handle/nonehandle.h b/winpr/libwinpr/handle/nonehandle.h new file mode 100644 index 000000000..1aa509888 --- /dev/null +++ b/winpr/libwinpr/handle/nonehandle.h @@ -0,0 +1,40 @@ +/** + * WinPR: Windows Portable Runtime + * NoneHandle a.k.a. brathandle should be used where a handle is needed, but + * functionality is not implemented yet or not implementable. + * + * Copyright 2014 DI (FH) Martin Haimberger + * + * 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. + */ + +#ifndef WINPR_NONE_HANDLE_PRIVATE_H +#define WINPR_NONE_HANDLE_PRIVATE_H + +#ifndef _WIN32 + +#include +#include "handle.h" + +struct winpr_none_handle +{ + WINPR_HANDLE_DEF(); +}; + +typedef struct winpr_none_handle WINPR_NONE_HANDLE; + +HANDLE CreateNoneHandle(); + +#endif /*_WIN32*/ + +#endif /* WINPR_NONE_HANDLE_PRIVATE_H */ diff --git a/winpr/libwinpr/thread/process.c b/winpr/libwinpr/thread/process.c index 4f5adc026..b7b8bef11 100644 --- a/winpr/libwinpr/thread/process.c +++ b/winpr/libwinpr/thread/process.c @@ -3,6 +3,7 @@ * Process Thread Functions * * Copyright 2012 Marc-Andre Moreau + * Copyright 2014 DI (FH) Martin Haimberger * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +23,7 @@ #endif #include +#include "../handle/nonehandle.h" #include #include @@ -183,7 +185,7 @@ BOOL _CreateProcessExA(HANDLE hToken, DWORD dwLogonFlags, LPSTR* pArgs = NULL; char** envp = NULL; char* filename = NULL; - WINPR_THREAD* thread; + HANDLE thread; WINPR_PROCESS* process; WINPR_ACCESS_TOKEN* token; LPTCH lpszEnvironmentBlock; @@ -288,21 +290,15 @@ BOOL _CreateProcessExA(HANDLE hToken, DWORD dwLogonFlags, process->status = 0; process->dwExitCode = 0; - thread = (WINPR_THREAD*) malloc(sizeof(WINPR_THREAD)); - - ZeroMemory(thread, sizeof(WINPR_THREAD)); + thread = CreateNoneHandle(); if (!thread) { goto finish; } - WINPR_HANDLE_SET_TYPE(thread, HANDLE_TYPE_THREAD); - - thread->mainProcess = TRUE; - lpProcessInformation->hProcess = (HANDLE) process; - lpProcessInformation->hThread = (HANDLE) thread; + lpProcessInformation->hThread = thread; lpProcessInformation->dwProcessId = (DWORD) pid; lpProcessInformation->dwThreadId = (DWORD) pid;