make shared memory using memory mapped files more robust

svn path=/trunk/boinc/; revision=13534
This commit is contained in:
Charlie Fenton 2007-09-04 07:41:25 +00:00
parent 74f04844da
commit 9c939288df
4 changed files with 65 additions and 80 deletions

View File

@ -169,9 +169,7 @@ static int setup_shared_mem() {
}
#else
#ifdef USE_FILE_MAPPED_SHMEM
if (create_shmem(
MMAPPED_FILE_NAME, sizeof(SHARED_MEM), (void**)&app_client_shm->shm
)) {
if (attach_shmem( MMAPPED_FILE_NAME, (void**)&app_client_shm->shm)) {
delete app_client_shm;
app_client_shm = NULL;
}

View File

@ -181,23 +181,16 @@ int create_shmem(char *path, size_t size, void** pp) {
// and it's not a significant security issue.
//
fd = open(path, O_RDWR | O_CREAT, 0666);
if (fd < 0)
return ERR_SHMGET;
if (fd < 0) return ERR_SHMGET;
retval = fstat(fd, &sbuf);
if (retval == 0) {
if (sbuf.st_size < size) {
// The following 2 lines extend the file and clear its new
// area to all zeros because they write beyond the old EOF.
// See the lseek man page for details.
lseek(fd, size-1, SEEK_SET);
write(fd, "\0", 1);
}
}
if (retval) {
perror("fcntl returned ");
return ERR_SHMGET;
if (retval) return ERR_SHMGET;
if (sbuf.st_size < size) {
// The following 2 lines extend the file and clear its new
// area to all zeros because they write beyond the old EOF.
// See the lseek man page for details.
lseek(fd, size-1, SEEK_SET);
write(fd, "\0", 1);
}
// mmap willl return NULL if size is 0
@ -206,9 +199,11 @@ int create_shmem(char *path, size_t size, void** pp) {
// Now close the file. The kernel doesnÕt use our file descriptor.
close(fd);
if (*pp == MAP_FAILED)
if (*pp == MAP_FAILED) {
*pp = 0;
return ERR_SHMGET;
}
return 0;
}
@ -216,6 +211,30 @@ int destroy_shmem(key_t key){
return 0;
}
int attach_shmem(char *path, void** pp) {
int fd, retval;
struct stat sbuf;
fd = open(path, O_RDWR);
if (fd < 0) return ERR_SHMGET;
retval = fstat(fd, &sbuf);
if (retval) return ERR_SHMGET;
*pp = mmap(NULL, sbuf.st_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
// Now close the file. The kernel doesnÕt use our file descriptor.
close(fd);
if (*pp == MAP_FAILED) {
*pp = 0;
return ERR_SHMGET;
}
return 0;
}
int detach_shmem(void* p, size_t size) {
return munmap(p, size);
}

View File

@ -52,6 +52,10 @@ int detach_shmem(HANDLE hSharedMem, void* p);
//
extern int create_shmem(char *path, size_t size, void** pp);
// attach to a shared-memory segment
//
int attach_shmem(char *path, void** pp);
// detach from a shared-mem segment
//
extern int detach_shmem(void* p, size_t size);

View File

@ -3622,8 +3622,7 @@
OTHER_CFLAGS = (
"-DMAC_OS_X_VERSION_MAX_ALLOWED=1040",
"-D_THREAD_SAFE",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -3658,8 +3657,7 @@
OTHER_CFLAGS = (
"-D_THREAD_SAFE",
"-DNDEBUG",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -3764,10 +3762,7 @@
LIBRARY_STYLE = STATIC;
MACOSX_DEPLOYMENT_TARGET = "";
OPTIMIZATION_CFLAGS = "-O0";
OTHER_CFLAGS = (
"-D",
USE_FILE_MAPPED_SHMEM,
);
OTHER_CFLAGS = "-DUSE_FILE_MAPPED_SHMEM";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "";
OTHER_REZFLAGS = "";
@ -3792,10 +3787,7 @@
INSTALL_PATH = /usr/local/lib;
LIBRARY_STYLE = STATIC;
MACOSX_DEPLOYMENT_TARGET = 10.4;
OTHER_CFLAGS = (
"-D",
USE_FILE_MAPPED_SHMEM,
);
OTHER_CFLAGS = "-DUSE_FILE_MAPPED_SHMEM";
OTHER_CFLAGS_i386 = "-DMAC_OS_X_VERSION_MAX_ALLOWED=1040 -D_THREAD_SAFE -include ../clientgui/mac/config.h";
OTHER_CFLAGS_ppc = "-DMAC_OS_X_VERSION_MAX_ALLOWED=1030 -D_THREAD_SAFE -include ../clientgui/mac/config.h";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
@ -3958,8 +3950,7 @@
"-DMAC_OS_X_VERSION_MAX_ALLOWED=1040",
"-D_THREAD_SAFE",
"-D_DEBUG",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -4008,8 +3999,7 @@
OTHER_CFLAGS = (
"-D_THREAD_SAFE",
"-DNDEBUG",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -4797,8 +4787,7 @@
OTHER_CFLAGS = (
"-D_THREAD_SAFE",
"-DNDEBUG",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -4865,10 +4854,7 @@
INSTALL_PATH = /usr/local/lib;
LIBRARY_STYLE = STATIC;
MACOSX_DEPLOYMENT_TARGET = 10.4;
OTHER_CFLAGS = (
"-D",
USE_FILE_MAPPED_SHMEM,
);
OTHER_CFLAGS = "-DUSE_FILE_MAPPED_SHMEM";
OTHER_CFLAGS_i386 = "-DMAC_OS_X_VERSION_MAX_ALLOWED=1040 -D_THREAD_SAFE -include ../clientgui/mac/config.h";
OTHER_CFLAGS_ppc = "-DMAC_OS_X_VERSION_MAX_ALLOWED=1030 -D_THREAD_SAFE -include ../clientgui/mac/config.h";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
@ -4969,8 +4955,7 @@
"-D_THREAD_SAFE",
"-DSANDBOX",
"-DNDEBUG",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -5183,8 +5168,7 @@
OTHER_CFLAGS = (
"-DMAC_OS_X_VERSION_MAX_ALLOWED=1040",
"-D_THREAD_SAFE",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -5219,8 +5203,7 @@
OTHER_CFLAGS = (
"-DMAC_OS_X_VERSION_MAX_ALLOWED=1030",
"-D_THREAD_SAFE",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-DNDEBUG",
"-include",
../clientgui/mac/config.h,
@ -5323,10 +5306,7 @@
LIBRARY_STYLE = STATIC;
MACOSX_DEPLOYMENT_TARGET = "";
OPTIMIZATION_CFLAGS = "-O0";
OTHER_CFLAGS = (
"-D",
USE_FILE_MAPPED_SHMEM,
);
OTHER_CFLAGS = "-DUSE_FILE_MAPPED_SHMEM";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "";
OTHER_REZFLAGS = "";
@ -5351,10 +5331,7 @@
INSTALL_PATH = /usr/local/lib;
LIBRARY_STYLE = STATIC;
MACOSX_DEPLOYMENT_TARGET = 10.3;
OTHER_CFLAGS = (
"-D",
USE_FILE_MAPPED_SHMEM,
);
OTHER_CFLAGS = "-DUSE_FILE_MAPPED_SHMEM";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "";
OTHER_REZFLAGS = "";
@ -5515,8 +5492,7 @@
"-D_THREAD_SAFE",
"-DSANDBOX",
"-D_DEBUG",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -5564,8 +5540,7 @@
"-D_THREAD_SAFE",
"-DSANDBOX",
"-DNDEBUG",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -5929,8 +5904,7 @@
OTHER_CFLAGS = (
"-DMAC_OS_X_VERSION_MAX_ALLOWED=1040",
"-D_THREAD_SAFE",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-DNDEBUG",
"-include",
../clientgui/mac/config.h,
@ -5995,10 +5969,7 @@
INSTALL_PATH = /usr/local/lib;
LIBRARY_STYLE = STATIC;
MACOSX_DEPLOYMENT_TARGET = 10.4;
OTHER_CFLAGS = (
"-D",
USE_FILE_MAPPED_SHMEM,
);
OTHER_CFLAGS = "-DUSE_FILE_MAPPED_SHMEM";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = "";
OTHER_REZFLAGS = "";
@ -6093,8 +6064,7 @@
"-D_THREAD_SAFE",
"-DSANDBOX",
"-DNDEBUG",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -6294,8 +6264,7 @@
OTHER_CFLAGS = (
"-DMAC_OS_X_VERSION_MAX_ALLOWED=1040",
"-D_THREAD_SAFE",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -6330,8 +6299,7 @@
OTHER_CFLAGS = (
"-DMAC_OS_X_VERSION_MAX_ALLOWED=1040",
"-D_THREAD_SAFE",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -6364,8 +6332,7 @@
OTHER_CFLAGS = (
"-DMAC_OS_X_VERSION_MAX_ALLOWED=1030",
"-D_THREAD_SAFE",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-DNDEBUG",
"-include",
../clientgui/mac/config.h,
@ -6399,8 +6366,7 @@
OTHER_CFLAGS = (
"-DMAC_OS_X_VERSION_MAX_ALLOWED=1040",
"-D_THREAD_SAFE",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-DNDEBUG",
"-include",
../clientgui/mac/config.h,
@ -6434,8 +6400,7 @@
OTHER_CFLAGS = (
"-D_THREAD_SAFE",
"-DNDEBUG",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);
@ -6470,8 +6435,7 @@
OTHER_CFLAGS = (
"-D_THREAD_SAFE",
"-DNDEBUG",
"-D",
USE_FILE_MAPPED_SHMEM,
"-DUSE_FILE_MAPPED_SHMEM",
"-include",
../clientgui/mac/config.h,
);