libwinpr-file: added more error checking

This commit is contained in:
Marc-André Moreau 2013-10-01 12:03:08 -04:00
parent 480071cdeb
commit 8695c5226b
3 changed files with 41 additions and 11 deletions

View File

@ -31,7 +31,7 @@ set_target_properties(${MODULE_NAME} PROPERTIES VERSION ${WINPR_VERSION_FULL} SO
set_complex_link_libraries(VARIABLE ${MODULE_PREFIX}_LIBS
MONOLITHIC ${MONOLITHIC_BUILD} INTERNAL
MODULE winpr
MODULES winpr-crt winpr-handle winpr-path winpr-synch)
MODULES winpr-crt winpr-handle winpr-path winpr-error winpr-synch)
if(MONOLITHIC_BUILD)

View File

@ -24,6 +24,7 @@
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/synch.h>
#include <winpr/error.h>
#include <winpr/handle.h>
#include <winpr/platform.h>
@ -292,6 +293,17 @@ BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
status = read(pipe->clientfd, lpBuffer, nNumberOfBytesToRead);
if (status == 0)
{
switch (errno)
{
case ECONNRESET:
SetLastError(ERROR_BROKEN_PIPE);
status = -1;
break;
}
}
if (status < 0)
{
*lpNumberOfBytesRead = 0;

View File

@ -131,12 +131,21 @@ HANDLE CreateNamedPipeA(LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD
free(lpPipePath);
if (PathFileExistsA(pNamedPipe->lpFilePath))
{
DeleteFileA(pNamedPipe->lpFilePath);
}
pNamedPipe->clientfd = -1;
pNamedPipe->serverfd = socket(PF_LOCAL, SOCK_STREAM, 0);
pNamedPipe->ServerMode = TRUE;
if (PathFileExistsA(pNamedPipe->lpFilePath))
DeleteFileA(pNamedPipe->lpFilePath);
pNamedPipe->serverfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (pNamedPipe->serverfd == -1)
{
fprintf(stderr, "CreateNamedPipeA: socket error\n");
return NULL;
}
ZeroMemory(&s, sizeof(struct sockaddr_un));
s.sun_family = AF_UNIX;
@ -144,16 +153,22 @@ HANDLE CreateNamedPipeA(LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD
status = bind(pNamedPipe->serverfd, (struct sockaddr*) &s, sizeof(struct sockaddr_un));
if (status == 0)
if (status != 0)
{
status = listen(pNamedPipe->serverfd, 2);
if (status == 0)
{
UnixChangeFileMode(pNamedPipe->lpFilePath, 0xFFFF);
}
fprintf(stderr, "CreateNamedPipeA: bind error\n");
return NULL;
}
status = listen(pNamedPipe->serverfd, 2);
if (status != 0)
{
fprintf(stderr, "CreateNamedPipeA: listen error\n");
return NULL;
}
UnixChangeFileMode(pNamedPipe->lpFilePath, 0xFFFF);
return hNamedPipe;
}
@ -183,7 +198,10 @@ BOOL ConnectNamedPipe(HANDLE hNamedPipe, LPOVERLAPPED lpOverlapped)
status = accept(pNamedPipe->serverfd, (struct sockaddr*) &s, &length);
if (status < 0)
{
fprintf(stderr, "ConnectNamedPipe: accept error\n");
return FALSE;
}
pNamedPipe->clientfd = status;
pNamedPipe->ServerMode = FALSE;