From f4e98f29ece25e475eada840e09bee49080a7efa Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marc-Andr=C3=A9=20Moreau?= Date: Wed, 25 Sep 2013 19:13:39 -0400 Subject: [PATCH] libwinpr-pipe: add server-side named pipe waiting --- winpr/include/winpr/pipe.h | 2 ++ winpr/libwinpr/file/file.c | 20 ++++++++++++++++++++ winpr/libwinpr/pipe/pipe.c | 4 ++++ winpr/libwinpr/pipe/pipe.h | 1 + winpr/libwinpr/synch/wait.c | 17 +++++++++++++---- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/winpr/include/winpr/pipe.h b/winpr/include/winpr/pipe.h index 188adf8..5c5b12f 100644 --- a/winpr/include/winpr/pipe.h +++ b/winpr/include/winpr/pipe.h @@ -114,6 +114,8 @@ WINPR_API char* GetNamedPipeNameWithoutPrefixA(LPCSTR lpName); WINPR_API char* GetNamedPipeUnixDomainSocketBaseFilePathA(); WINPR_API char* GetNamedPipeUnixDomainSocketFilePathA(LPCSTR lpName); +WINPR_API int GetNamePipeFileDescriptor(HANDLE hNamedPipe); + #ifdef __cplusplus } #endif diff --git a/winpr/libwinpr/file/file.c b/winpr/libwinpr/file/file.c index feb673c..7720bb8 100644 --- a/winpr/libwinpr/file/file.c +++ b/winpr/libwinpr/file/file.c @@ -216,6 +216,7 @@ HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, pNamedPipe->clientfd = socket(PF_LOCAL, SOCK_STREAM, 0); pNamedPipe->serverfd = -1; + pNamedPipe->ServerMode = FALSE; if (0) { @@ -733,6 +734,25 @@ char* GetNamedPipeUnixDomainSocketFilePathA(LPCSTR lpName) return lpFilePath; } +int GetNamePipeFileDescriptor(HANDLE hNamedPipe) +{ +#ifndef _WIN32 + int fd; + WINPR_NAMED_PIPE* pNamedPipe; + + pNamedPipe = (WINPR_NAMED_PIPE*) hNamedPipe; + + if (!pNamedPipe) + return -1; + + fd = (pNamedPipe->ServerMode) ? pNamedPipe->serverfd : pNamedPipe->clientfd; + + return fd; +#else + return -1; +#endif +} + int UnixChangeFileMode(const char* filename, int flags) { #ifndef _WIN32 diff --git a/winpr/libwinpr/pipe/pipe.c b/winpr/libwinpr/pipe/pipe.c index 4c984a3..fa01fff 100644 --- a/winpr/libwinpr/pipe/pipe.c +++ b/winpr/libwinpr/pipe/pipe.c @@ -123,12 +123,16 @@ HANDLE CreateNamedPipeA(LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD lpPipePath = GetNamedPipeUnixDomainSocketBaseFilePathA(); if (!PathFileExistsA(lpPipePath)) + { CreateDirectoryA(lpPipePath, 0); + UnixChangeFileMode(lpPipePath, 0xFFFF); + } free(lpPipePath); pNamedPipe->clientfd = -1; pNamedPipe->serverfd = socket(PF_LOCAL, SOCK_STREAM, 0); + pNamedPipe->ServerMode = TRUE; if (0) { diff --git a/winpr/libwinpr/pipe/pipe.h b/winpr/libwinpr/pipe/pipe.h index efc7998..51b423d 100644 --- a/winpr/libwinpr/pipe/pipe.h +++ b/winpr/libwinpr/pipe/pipe.h @@ -45,6 +45,7 @@ struct winpr_named_pipe const char* lpFileName; const char* lpFilePath; + BOOL ServerMode; DWORD dwOpenMode; DWORD dwPipeMode; DWORD nMaxInstances; diff --git a/winpr/libwinpr/synch/wait.c b/winpr/libwinpr/synch/wait.c index 823a07f..0467d66 100644 --- a/winpr/libwinpr/synch/wait.c +++ b/winpr/libwinpr/synch/wait.c @@ -260,13 +260,19 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds) } else if (Type == HANDLE_TYPE_NAMED_PIPE) { + int fd; int status; fd_set rfds; struct timeval timeout; WINPR_NAMED_PIPE* pipe = (WINPR_NAMED_PIPE*) Object; + fd = (pipe->ServerMode) ? pipe->serverfd : pipe->clientfd; + + if (fd == -1) + return WAIT_FAILED; + FD_ZERO(&rfds); - FD_SET(pipe->clientfd, &rfds); + FD_SET(fd, &rfds); ZeroMemory(&timeout, sizeof(timeout)); if ((dwMilliseconds != INFINITE) && (dwMilliseconds != 0)) @@ -274,7 +280,7 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds) timeout.tv_usec = dwMilliseconds * 1000; } - status = select(pipe->clientfd + 1, &rfds, NULL, NULL, + status = select(fd + 1, &rfds, NULL, NULL, (dwMilliseconds == INFINITE) ? NULL : &timeout); if (status < 0) @@ -347,7 +353,10 @@ DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAl else if (Type == HANDLE_TYPE_NAMED_PIPE) { WINPR_NAMED_PIPE* pipe = (WINPR_NAMED_PIPE*) Object; - fd = pipe->clientfd; + fd = (pipe->ServerMode) ? pipe->serverfd : pipe->clientfd; + + if (fd == -1) + return WAIT_FAILED; } else { @@ -397,7 +406,7 @@ DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAl else if (Type == HANDLE_TYPE_NAMED_PIPE) { WINPR_NAMED_PIPE* pipe = (WINPR_NAMED_PIPE*) Object; - fd = pipe->clientfd; + fd = (pipe->ServerMode) ? pipe->serverfd : pipe->clientfd; } if (FD_ISSET(fd, &fds)) -- 2.7.4