From 274b2f8c4afaa1e78a6399e757844eb1b3b1cd4a Mon Sep 17 00:00:00 2001
From: Olivier Bertrand <bertrandop@gmail.com>
Date: Thu, 7 Mar 2013 23:05:54 +0100
Subject: [PATCH] - Fix a bug causing the server to crash when writing on a
 memory mapped   file on Linux.

modified:
  storage/connect/maputil.cpp
---
 storage/connect/maputil.cpp | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/storage/connect/maputil.cpp b/storage/connect/maputil.cpp
index c068c09b36..dd8f69cc79 100644
--- a/storage/connect/maputil.cpp
+++ b/storage/connect/maputil.cpp
@@ -117,6 +117,7 @@ HANDLE CreateFileMap(PGLOBAL g, LPCSTR fileName,
                          MEMMAP *mm, MODE mode, bool del) 
   {
   unsigned int openMode;
+  int          protmode;
   HANDLE       fd;
   size_t       filesize;
   struct stat  st; 
@@ -127,13 +128,16 @@ HANDLE CreateFileMap(PGLOBAL g, LPCSTR fileName,
   switch (mode) {
     case MODE_READ:
       openMode = O_RDONLY;
+      protmode = PROT_READ;
       break;
     case MODE_UPDATE:
     case MODE_DELETE:
       openMode = (del) ? (O_RDWR | O_TRUNC) : O_RDWR;
+      protmode = PROT_WRITE;
       break;
     case MODE_INSERT:
       openMode = (O_WRONLY | O_CREAT | O_APPEND);
+      protmode = PROT_WRITE;
       break;
      default:
       sprintf(g->Message, MSG(BAD_FUNC_MODE), "CreateFileMap", mode);
@@ -155,13 +159,20 @@ HANDLE CreateFileMap(PGLOBAL g, LPCSTR fileName,
     
     // Now we are ready to load the file.  If mmap() is available we try
     //   this first.  If not available or it failed we try to load it.
-    mm->memory = mmap(NULL, filesize, PROT_READ, MAP_PRIVATE, fd, 0);
-    mm->lenL = (mm->memory != 0) ? filesize : 0;
-    mm->lenH = 0;
+    mm->memory = mmap(NULL, filesize, protmode, MAP_PRIVATE, fd, 0);
+
+    if (mm->memory) {
+      mm->lenL = (mm->memory != 0) ? filesize : 0;
+      mm->lenH = 0;
+    } else {
+      strcpy(g->Message, "Memory mapping failed");
+      return INVALID_HANDLE_VALUE;
+    } endif // memory
+
     }  /* endif fd */
   
-  // mmap() call was successful. ??????????
-   return fd;
+    // mmap() call was successful. ??????????
+    return fd;
   }  // end of CreateFileMap
 
 bool CloseMemMap(void *memory, size_t dwSize) 
-- 
2.30.9