libwinpr-pipe: add server-side named pipe waiting
authorMarc-André Moreau <marcandre.moreau@gmail.com>
Wed, 25 Sep 2013 23:13:39 +0000 (19:13 -0400)
committerMarc-André Moreau <marcandre.moreau@gmail.com>
Wed, 25 Sep 2013 23:13:39 +0000 (19:13 -0400)
winpr/include/winpr/pipe.h
winpr/libwinpr/file/file.c
winpr/libwinpr/pipe/pipe.c
winpr/libwinpr/pipe/pipe.h
winpr/libwinpr/synch/wait.c

index 188adf8..5c5b12f 100644 (file)
@@ -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
index feb673c..7720bb8 100644 (file)
@@ -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
index 4c984a3..fa01fff 100644 (file)
@@ -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)
        {
index efc7998..51b423d 100644 (file)
@@ -45,6 +45,7 @@ struct winpr_named_pipe
        const char* lpFileName;
        const char* lpFilePath;
 
+       BOOL ServerMode;
        DWORD dwOpenMode;
        DWORD dwPipeMode;
        DWORD nMaxInstances;
index 823a07f..0467d66 100644 (file)
@@ -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))