From a6f27f48eaf2337cb694092e371c92aeed859925 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 11 Mar 2015 15:11:03 +0100 Subject: [PATCH] Using handle close callback now. --- winpr/libwinpr/pipe/pipe.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/winpr/libwinpr/pipe/pipe.c b/winpr/libwinpr/pipe/pipe.c index c5524d2..4b23225 100644 --- a/winpr/libwinpr/pipe/pipe.c +++ b/winpr/libwinpr/pipe/pipe.c @@ -42,6 +42,7 @@ #include #include #include +#include #include "pipe.h" @@ -70,6 +71,47 @@ typedef struct _NamedPipeServerSocketEntry int references; } NamedPipeServerSocketEntry; +static pthread_once_t pipe_initialized = PTHREAD_ONCE_INIT; + +static HANDLE_CLOSE_CB _PipeHandleCloseCb; + +static BOOL PipeCloseHandle(HANDLE handle); + +static BOOL PipeIsHandled(HANDLE handle) +{ + WINPR_PIPE* pPipe = (WINPR_PIPE*) handle; + + if (!pPipe || pPipe->Type != HANDLE_TYPE_ANONYMOUS_PIPE) + { + SetLastError(ERROR_INVALID_HANDLE); + return FALSE; + } + + return TRUE; +} + +static void PipeInitialize(void) +{ + _PipeHandleCloseCb.IsHandled = PipeIsHandled; + _PipeHandleCloseCb.CloseHandle = PipeCloseHandle; + RegisterHandleCloseCb(&_PipeHandleCloseCb); +} + +BOOL PipeCloseHandle(HANDLE handle) { + WINPR_PIPE* pipe = (WINPR_PIPE *)handle; + + if (!PipeIsHandled(handle)) + return FALSE; + + if (pipe->fd != -1) + { + close(pipe->fd); + } + + free(handle); + return TRUE; +} + static void InitWinPRPipeModule() { if (g_NamedPipeServerSockets) @@ -91,6 +133,9 @@ BOOL CreatePipe(PHANDLE hReadPipe, PHANDLE hWritePipe, LPSECURITY_ATTRIBUTES lpP pipe_fd[0] = -1; pipe_fd[1] = -1; + if (pthread_once(&pipe_initialized, PipeInitialize)) + return FALSE; + if (pipe(pipe_fd) < 0) { WLog_ERR(TAG, "failed to create pipe"); -- 2.7.4