2012-05-29 22:14:26 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Handle Management
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:20:53 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-05-29 22:14:26 +04:00
|
|
|
#include <winpr/handle.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
2014-06-19 21:07:45 +04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2012-09-19 01:33:52 +04:00
|
|
|
#include "../synch/synch.h"
|
2012-12-13 07:03:40 +04:00
|
|
|
#include "../thread/thread.h"
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "../pipe/pipe.h"
|
2014-04-02 23:51:28 +04:00
|
|
|
#include "../comm/comm.h"
|
2013-09-24 08:07:48 +04:00
|
|
|
#include "../security/security.h"
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-05-26 23:24:34 +04:00
|
|
|
#include <assert.h>
|
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "../handle/handle.h"
|
|
|
|
|
2014-07-02 18:57:20 +04:00
|
|
|
/* _HandleCreators is a NULL-terminated array with a maximun of HANDLE_CREATOR_MAX HANDLE_CREATOR */
|
|
|
|
#define HANDLE_CLOSE_CB_MAX 128
|
|
|
|
static HANDLE_CLOSE_CB **_HandleCloseCbs = NULL;
|
2014-07-03 13:24:37 +04:00
|
|
|
static CRITICAL_SECTION _HandleCloseCbsLock;
|
2014-07-02 18:57:20 +04:00
|
|
|
|
|
|
|
static pthread_once_t _HandleCloseCbsInitialized = PTHREAD_ONCE_INIT;
|
|
|
|
static void _HandleCloseCbsInit()
|
|
|
|
{
|
|
|
|
/* NB: error management to be done outside of this function */
|
|
|
|
assert(_HandleCloseCbs == NULL);
|
2014-08-18 19:22:22 +04:00
|
|
|
_HandleCloseCbs = (HANDLE_CLOSE_CB **)calloc(HANDLE_CLOSE_CB_MAX+1, sizeof(HANDLE_CLOSE_CB *));
|
2014-07-03 13:24:37 +04:00
|
|
|
InitializeCriticalSection(&_HandleCloseCbsLock);
|
2014-07-02 18:57:20 +04:00
|
|
|
assert(_HandleCloseCbs != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns TRUE on success, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
BOOL RegisterHandleCloseCb(HANDLE_CLOSE_CB *pHandleCloseCb)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (pthread_once(&_HandleCloseCbsInitialized, _HandleCloseCbsInit) != 0)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_HandleCloseCbs == NULL)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:24:37 +04:00
|
|
|
EnterCriticalSection(&_HandleCloseCbsLock);
|
2014-07-02 18:57:20 +04:00
|
|
|
|
|
|
|
for (i=0; i<HANDLE_CLOSE_CB_MAX; i++)
|
|
|
|
{
|
|
|
|
if (_HandleCloseCbs[i] == NULL)
|
|
|
|
{
|
|
|
|
_HandleCloseCbs[i] = pHandleCloseCb;
|
2014-07-03 13:24:37 +04:00
|
|
|
LeaveCriticalSection(&_HandleCloseCbsLock);
|
2014-07-02 18:57:20 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:24:37 +04:00
|
|
|
LeaveCriticalSection(&_HandleCloseCbsLock);
|
2014-07-02 18:57:20 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-29 22:14:26 +04:00
|
|
|
BOOL CloseHandle(HANDLE hObject)
|
|
|
|
{
|
2014-07-02 18:57:20 +04:00
|
|
|
int i;
|
2012-09-19 01:33:52 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hObject, &Type, &Object))
|
|
|
|
return FALSE;
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2014-07-02 18:57:20 +04:00
|
|
|
if (pthread_once(&_HandleCloseCbsInitialized, _HandleCloseCbsInit) != 0)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_HandleCloseCbs == NULL)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:24:37 +04:00
|
|
|
EnterCriticalSection(&_HandleCloseCbsLock);
|
|
|
|
|
2014-07-02 18:57:20 +04:00
|
|
|
for (i=0; _HandleCloseCbs[i] != NULL; i++)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
HANDLE_CLOSE_CB *close_cb = (HANDLE_CLOSE_CB *)_HandleCloseCbs[i];
|
|
|
|
|
2014-07-02 18:57:20 +04:00
|
|
|
if (close_cb && close_cb->IsHandled(hObject))
|
|
|
|
{
|
2014-07-03 13:24:37 +04:00
|
|
|
BOOL result = close_cb->CloseHandle(hObject);
|
|
|
|
LeaveCriticalSection(&_HandleCloseCbsLock);
|
|
|
|
return result;
|
2014-07-02 18:57:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:24:37 +04:00
|
|
|
LeaveCriticalSection(&_HandleCloseCbsLock);
|
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
if (Type == HANDLE_TYPE_THREAD)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WINPR_THREAD *thread;
|
|
|
|
thread = (WINPR_THREAD *) Object;
|
2012-12-13 07:03:40 +04:00
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
if (thread->started)
|
|
|
|
{
|
2014-07-10 14:28:35 +04:00
|
|
|
pthread_detach(thread->thread);
|
|
|
|
}
|
2012-12-13 07:03:40 +04:00
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
free(thread);
|
2012-09-19 03:24:03 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
2013-09-23 01:23:00 +04:00
|
|
|
else if (Type == HANDLE_TYPE_PROCESS)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WINPR_PROCESS *process;
|
|
|
|
process = (WINPR_PROCESS *) Object;
|
2013-09-23 01:23:00 +04:00
|
|
|
free(process);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-09-19 03:24:03 +04:00
|
|
|
else if (Type == HANDLE_TYPE_MUTEX)
|
2012-09-19 01:33:52 +04:00
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WINPR_MUTEX *mutex;
|
|
|
|
mutex = (WINPR_MUTEX *) Object;
|
2013-05-17 01:32:58 +04:00
|
|
|
pthread_mutex_destroy(&mutex->mutex);
|
2012-09-19 01:33:52 +04:00
|
|
|
free(Object);
|
2012-09-19 02:36:13 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (Type == HANDLE_TYPE_EVENT)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WINPR_EVENT *event;
|
|
|
|
event = (WINPR_EVENT *) Object;
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2012-11-27 03:02:41 +04:00
|
|
|
if (!event->bAttached)
|
2012-09-19 02:36:13 +04:00
|
|
|
{
|
2012-11-27 03:02:41 +04:00
|
|
|
if (event->pipe_fd[0] != -1)
|
|
|
|
{
|
|
|
|
close(event->pipe_fd[0]);
|
|
|
|
event->pipe_fd[0] = -1;
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2012-11-27 03:02:41 +04:00
|
|
|
if (event->pipe_fd[1] != -1)
|
|
|
|
{
|
|
|
|
close(event->pipe_fd[1]);
|
|
|
|
event->pipe_fd[1] = -1;
|
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
}
|
|
|
|
|
2013-05-17 02:27:26 +04:00
|
|
|
free(Object);
|
2012-09-19 01:33:52 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
2012-09-19 03:24:03 +04:00
|
|
|
else if (Type == HANDLE_TYPE_SEMAPHORE)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WINPR_SEMAPHORE *semaphore;
|
|
|
|
semaphore = (WINPR_SEMAPHORE *) Object;
|
2012-11-28 21:47:04 +04:00
|
|
|
#ifdef WINPR_PIPE_SEMAPHORE
|
|
|
|
|
|
|
|
if (semaphore->pipe_fd[0] != -1)
|
|
|
|
{
|
|
|
|
close(semaphore->pipe_fd[0]);
|
|
|
|
semaphore->pipe_fd[0] = -1;
|
|
|
|
|
|
|
|
if (semaphore->pipe_fd[1] != -1)
|
|
|
|
{
|
|
|
|
close(semaphore->pipe_fd[1]);
|
|
|
|
semaphore->pipe_fd[1] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2012-09-19 03:24:03 +04:00
|
|
|
#if defined __APPLE__
|
2014-08-18 19:22:22 +04:00
|
|
|
semaphore_destroy(mach_task_self(), *((winpr_sem_t *) semaphore->sem));
|
2012-09-19 03:24:03 +04:00
|
|
|
#else
|
2014-08-18 19:22:22 +04:00
|
|
|
sem_destroy((winpr_sem_t *) semaphore->sem);
|
2012-11-28 21:47:04 +04:00
|
|
|
#endif
|
2012-09-19 03:24:03 +04:00
|
|
|
#endif
|
|
|
|
free(Object);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-07-29 19:57:29 +04:00
|
|
|
else if (Type == HANDLE_TYPE_TIMER)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WINPR_TIMER *timer;
|
|
|
|
timer = (WINPR_TIMER *) Object;
|
2013-07-29 19:57:29 +04:00
|
|
|
#ifdef __linux__
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-07-29 19:57:29 +04:00
|
|
|
if (timer->fd != -1)
|
|
|
|
close(timer->fd);
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#endif
|
2013-07-29 19:57:29 +04:00
|
|
|
free(Object);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-10-08 04:25:50 +04:00
|
|
|
else if (Type == HANDLE_TYPE_ANONYMOUS_PIPE)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WINPR_PIPE *pipe;
|
|
|
|
pipe = (WINPR_PIPE *) Object;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
if (pipe->fd != -1)
|
2012-10-08 04:25:50 +04:00
|
|
|
{
|
2013-05-17 01:32:58 +04:00
|
|
|
close(pipe->fd);
|
2012-10-08 04:25:50 +04:00
|
|
|
}
|
|
|
|
|
2013-05-17 02:27:26 +04:00
|
|
|
free(Object);
|
2012-10-08 04:25:50 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
2013-07-23 02:20:34 +04:00
|
|
|
else if (Type == HANDLE_TYPE_NAMED_PIPE)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WINPR_NAMED_PIPE *pNamedPipe = (WINPR_NAMED_PIPE *) Object;
|
2014-05-28 18:22:32 +04:00
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
if (pNamedPipe->clientfd != -1)
|
|
|
|
{
|
|
|
|
//WLOG_DBG(TAG, "%s: closing clientfd %d\n", __FUNCTION__, pNamedPipe->clientfd);
|
2014-05-28 18:22:32 +04:00
|
|
|
close(pNamedPipe->clientfd);
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
|
|
|
if (pNamedPipe->serverfd != -1)
|
|
|
|
{
|
|
|
|
//WLOG_DBG(TAG, "%s: closing serverfd %d\n", __FUNCTION__, pNamedPipe->serverfd);
|
2014-05-28 18:22:32 +04:00
|
|
|
close(pNamedPipe->serverfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pNamedPipe->pfnUnrefNamedPipe)
|
|
|
|
pNamedPipe->pfnUnrefNamedPipe(pNamedPipe);
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
free((void *)pNamedPipe->lpFileName);
|
|
|
|
free((void *)pNamedPipe->lpFilePath);
|
|
|
|
free((void *)pNamedPipe->name);
|
2014-05-28 18:22:32 +04:00
|
|
|
free(pNamedPipe);
|
|
|
|
return TRUE;
|
2013-07-23 02:20:34 +04:00
|
|
|
}
|
2013-09-24 08:07:48 +04:00
|
|
|
else if (Type == HANDLE_TYPE_ACCESS_TOKEN)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WINPR_ACCESS_TOKEN *token;
|
|
|
|
token = (WINPR_ACCESS_TOKEN *) Object;
|
2013-09-24 08:07:48 +04:00
|
|
|
|
|
|
|
if (token->Username)
|
|
|
|
free(token->Username);
|
|
|
|
|
|
|
|
if (token->Domain)
|
|
|
|
free(token->Domain);
|
|
|
|
|
|
|
|
free(token);
|
2014-04-27 21:41:25 +04:00
|
|
|
return TRUE;
|
2013-09-24 08:07:48 +04:00
|
|
|
}
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-09-19 01:33:52 +04:00
|
|
|
return FALSE;
|
2012-05-29 22:14:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL DuplicateHandle(HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle,
|
2014-08-18 19:22:22 +04:00
|
|
|
LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions)
|
2012-05-29 22:14:26 +04:00
|
|
|
{
|
2012-09-19 01:33:52 +04:00
|
|
|
return TRUE;
|
2012-05-29 22:14:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL GetHandleInformation(HANDLE hObject, LPDWORD lpdwFlags)
|
|
|
|
{
|
2012-09-19 01:33:52 +04:00
|
|
|
return TRUE;
|
2012-05-29 22:14:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetHandleInformation(HANDLE hObject, DWORD dwMask, DWORD dwFlags)
|
|
|
|
{
|
2012-09-19 01:33:52 +04:00
|
|
|
return TRUE;
|
2012-05-29 22:14:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|