diff options
author | Julien Chaffraix <julien.chaffraix@gmail.com> | 2011-04-23 07:53:54 -0700 |
---|---|---|
committer | Michael Meeks <michael.meeks@novell.com> | 2011-04-26 15:13:59 +0100 |
commit | 051bd8d4f2a1f8ddc2d207c158d4c4aa02667cbe (patch) | |
tree | 06acce277c4471f3dfdc12445a35a49140800d44 /sal | |
parent | 567bfd0e92fe3d1b0d899974c913a1f8b1b48cbc (diff) |
Introduced safeRead.
Same function as safeWrite. Converted some methods in process.c to use it.
This usually simplifies the code and add better error checking.
Diffstat (limited to 'sal')
-rw-r--r-- | sal/osl/unx/process.c | 10 | ||||
-rw-r--r-- | sal/osl/unx/readwrite_helper.c | 26 | ||||
-rw-r--r-- | sal/osl/unx/readwrite_helper.h | 5 |
3 files changed, 36 insertions, 5 deletions
diff --git a/sal/osl/unx/process.c b/sal/osl/unx/process.c index 6dd911f376a9..9ebe57a39ce6 100644 --- a/sal/osl/unx/process.c +++ b/sal/osl/unx/process.c @@ -283,15 +283,15 @@ static sal_Bool sendFdPipe(int PipeFD, int SocketFD) OSL_TRACE("sendFdPipe : sending failed (%s)",strerror(errno)); } - nSend=read(PipeFD,&RetCode,sizeof(RetCode)); + bRet = safeRead(PipeFD, &RetCode, sizeof(RetCode)); - if ( nSend > 0 && RetCode == 1 ) + if ( bRet && RetCode == 1 ) { OSL_TRACE("sendFdPipe : resource was received\n"); } else { - OSL_TRACE("sendFdPipe : resource wasn't received\n"); + OSL_TRACE("sendFdPipe : resource wasn't received (error %s)\n", strerror(errno)); } #if defined(IOCHANNEL_TRANSFER_BSD_RENO) @@ -1168,7 +1168,7 @@ sal_Bool osl_getProcStat(pid_t pid, struct osl_procStat* procstat) char* tmp=0; char prstatbuf[512]; memset(prstatbuf,0,512); - bRet = read(fd,prstatbuf,511) == 511; + bRet = safeRead(fd, prstatbuf, 511); close(fd); /*printf("%s\n\n",prstatbuf);*/ @@ -1222,7 +1222,7 @@ sal_Bool osl_getProcStatus(pid_t pid, struct osl_procStat* procstat) char* tmp=0; char prstatusbuf[512]; memset(prstatusbuf,0,512); - bRet = read(fd,prstatusbuf,511) == 511; + bRet = safeRead(fd, prstatusbuf, 511); close(fd); diff --git a/sal/osl/unx/readwrite_helper.c b/sal/osl/unx/readwrite_helper.c index 4777d08614a1..41aa41e7731a 100644 --- a/sal/osl/unx/readwrite_helper.c +++ b/sal/osl/unx/readwrite_helper.c @@ -48,3 +48,29 @@ sal_Bool safeWrite(int fd, void* data, sal_uInt32 dataSize) return sal_True; } + +sal_Bool safeRead( int fd, void* buffer, sal_uInt32 count ) +{ + sal_Int32 nToRead = count; + // Check for overflow as we convert a signed to an unsigned. + OSL_ASSERT(count == (sal_uInt32)nToRead); + while ( nToRead ) { + sal_Int32 nRead = read(fd, buffer, nToRead); + if ( nRead < 0 ) { + // We were interrupted before reading, retry. + if (errno == EINTR) + continue; + + return sal_False; + } + + // If we reach the EOF, we consider this a partial transfer and thus + // an error. + if ( nRead == 0 ) + return sal_False; + + nToRead -= nRead; + } + + return sal_True; +} diff --git a/sal/osl/unx/readwrite_helper.h b/sal/osl/unx/readwrite_helper.h index dd16f587b0c6..494c8861e35c 100644 --- a/sal/osl/unx/readwrite_helper.h +++ b/sal/osl/unx/readwrite_helper.h @@ -30,3 +30,8 @@ #include <sal/types.h> sal_Bool safeWrite( int fd, void* data, sal_uInt32 dataSize ); + +// This function *will* read |count| bytes from |fd|, busy looping +// if needed. Don't use it when you don't know if you can request enough +// data. It will return sal_False for any partial transfer or error. +sal_Bool safeRead( int fd, void* buffer, sal_uInt32 count ); |