From a5619dffccdb92bd1c4af2685d1bf9d281880b5c Mon Sep 17 00:00:00 2001 From: Charlie Fenton Date: Tue, 4 Sep 2007 10:33:50 +0000 Subject: [PATCH] make shared memory using memory mapped files more robust svn path=/trunk/boinc/; revision=13536 --- lib/shmem.C | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/shmem.C b/lib/shmem.C index 235b7dd655..178ee94430 100755 --- a/lib/shmem.C +++ b/lib/shmem.C @@ -172,6 +172,10 @@ int create_shmem(char *path, size_t size, void** pp) { int fd, retval; struct stat sbuf; + // Return NULL pointer if create_shmem fails + *pp = 0; + if (size == 0) return ERR_SHMGET; + // NOTE: in principle it should be 0660, not 0666 // (i.e. Apache should belong to the same group as the // project admin user, and should therefore be able to access the seg. @@ -193,7 +197,6 @@ int create_shmem(char *path, size_t size, void** pp) { write(fd, "\0", 1); } - // mmap willl return NULL if size is 0 *pp = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0); // Now close the file. The kernel doesnŐt use our file descriptor. @@ -215,11 +218,13 @@ int attach_shmem(char *path, void** pp) { int fd, retval; struct stat sbuf; - fd = open(path, O_RDWR); + // Return NULL pointer if attach_shmem fails + *pp = 0; fd = open(path, O_RDWR); if (fd < 0) return ERR_SHMGET; retval = fstat(fd, &sbuf); if (retval) return ERR_SHMGET; + if (sbuf.st_size == 0) return ERR_SHMGET; *pp = mmap(NULL, sbuf.st_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);