first cut at using the tdb code for the connections structure, the
[ira/wip.git] / source3 / locking / shmem.c
index 72d7c07751bab238913454b929b945e497ff61d5..bf2802c9ec51265c6ccfcdf47a8be733dac34735 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/Netbios implementation.
    Version 1.9.
    Shared memory functions
-   Copyright (C) Erik Devriendt 1996-1997
+   Copyright (C) Erik Devriendt 1996-1998
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -23,7 +23,7 @@
 #include "includes.h"
 
 
-#ifdef FAST_SHARE_MODES
+#ifdef USE_SHARED_MMAP
 
 
 extern int DEBUGLEVEL;
@@ -41,6 +41,9 @@ extern int DEBUGLEVEL;
 #define SHM_FILE_MODE 0644
 #endif
 
+#define SHMEM_HASH_SIZE 13
+
+
 /* WARNING : offsets are used because mmap() does not guarantee that all processes have the 
    shared memory mapped to the same address */
 
@@ -67,7 +70,7 @@ struct SmbShmBlockDesc
 };
 
 #define        EOList_Addr     (struct SmbShmBlockDesc *)( 0 )
-#define EOList_Off      (NULL_OFFSET)
+#define EOList_Off      0
 
 #define        CellSize        sizeof(struct SmbShmBlockDesc)
 
@@ -104,7 +107,7 @@ static BOOL smb_shm_global_lock(void)
           return True;
    
    /* Do an exclusive wait lock on the first byte of the file */
-   if (fcntl_lock(smb_shm_fd, F_SETLKW, 0, 1, F_WRLCK) == False)
+   if (fcntl_lock(smb_shm_fd, SMB_F_SETLKW, 0, 1, F_WRLCK) == False)
    {
       DEBUG(0,("ERROR smb_shm_global_lock : fcntl_lock failed with code %s\n",strerror(errno)));
       smb_shm_times_locked--;
@@ -125,7 +128,7 @@ static BOOL smb_shm_global_unlock(void)
    
    if(smb_shm_times_locked == 0)
    {
-      DEBUG(0,("ERROR smb_shm_global_unlock : shmem not locked\n",smb_shm_fd));
+      DEBUG(0,("ERROR smb_shm_global_unlock : shmem not locked\n"));
       return False;
    }
    
@@ -141,7 +144,7 @@ static BOOL smb_shm_global_unlock(void)
           return True;
    
    /* Do a wait unlock on the first byte of the file */
-   if (fcntl_lock(smb_shm_fd, F_SETLKW, 0, 1, F_UNLCK) == False)
+   if (fcntl_lock(smb_shm_fd, SMB_F_SETLKW, 0, 1, F_UNLCK) == False)
    {
       DEBUG(0,("ERROR smb_shm_global_unlock : fcntl_lock failed with code %s\n",strerror(errno)));
       smb_shm_times_locked++;
@@ -155,7 +158,7 @@ static BOOL smb_shm_global_unlock(void)
 
 static void *smb_shm_offset2addr(int offset)
 {
-   if (offset == NULL_OFFSET )
+   if (offset == 0 )
       return (void *)(0);
    
    if (!smb_shm_header_p)
@@ -167,10 +170,10 @@ static void *smb_shm_offset2addr(int offset)
 static int smb_shm_addr2offset(void *addr)
 {
    if (!addr)
-      return NULL_OFFSET;
+      return 0;
    
    if (!smb_shm_header_p)
-      return NULL_OFFSET;
+      return 0;
    
    return (int)((char *)addr - (char *)smb_shm_header_p);
 }
@@ -190,7 +193,7 @@ static int smb_shm_alloc(int size)
    {
       /* not mapped yet */
       DEBUG(0,("ERROR smb_shm_alloc : shmem not mapped\n"));
-      return NULL_OFFSET;
+      return 0;
    }
    
    smb_shm_global_lock();
@@ -199,7 +202,7 @@ static int smb_shm_alloc(int size)
    {
       DEBUG(0,("ERROR smb_shm_alloc : shmem not consistent\n"));
       smb_shm_global_unlock();
-      return NULL_OFFSET;
+      return 0;
    }
    
    
@@ -222,7 +225,7 @@ static int smb_shm_alloc(int size)
    {
       DEBUG(0,("ERROR smb_shm_alloc : alloc of %d bytes failed, no free space found\n",size));
       smb_shm_global_unlock();
-      return (NULL_OFFSET);
+      return (0);
    }
    
    /* going to modify shared mem */
@@ -293,7 +296,7 @@ static BOOL smb_shm_create_hash_table( unsigned int size )
   smb_shm_global_lock();
   smb_shm_header_p->userdef_off = smb_shm_alloc( size );
 
-  if(smb_shm_header_p->userdef_off == NULL_OFFSET)
+  if(smb_shm_header_p->userdef_off == 0)
     {
       DEBUG(0,("smb_shm_create_hash_table: Failed to create hash table of size %d\n",size));
       smb_shm_global_unlock();
@@ -308,69 +311,113 @@ static BOOL smb_shm_create_hash_table( unsigned int size )
 
 static BOOL smb_shm_register_process(char *processreg_file, pid_t pid, BOOL *other_processes)
 {
-   int smb_shm_processes_fd = -1;
-   int nb_read;
-   pid_t other_pid;
-   int seek_back = -((int)sizeof(other_pid));
-   int free_slot = -1;
-   int erased_slot;   
-   
-   smb_shm_processes_fd = open(processreg_file, 
-                              read_only?O_RDONLY:(O_RDWR|O_CREAT), 
-                              SHM_FILE_MODE);
-
-   if ( smb_shm_processes_fd < 0 )
-   {
-      DEBUG(0,("ERROR smb_shm_register_process : processreg_file open failed with code %s\n",strerror(errno)));
-      return False;
-   }
-   
-   *other_processes = False;
-   
-   while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
-   {
-      if(other_pid)
+  int smb_shm_processes_fd = -1;
+  int nb_read;
+  pid_t other_pid;
+  SMB_OFF_T seek_back = -((SMB_OFF_T)sizeof(other_pid));
+  SMB_OFF_T free_slot = -1;
+  SMB_OFF_T erased_slot;   
+   
+  smb_shm_processes_fd = sys_open(processreg_file, 
+                              read_only?O_RDONLY:(O_RDWR|O_CREAT), 
+                              SHM_FILE_MODE);
+
+  if ( smb_shm_processes_fd < 0 )
+  {
+     DEBUG(0, ("ERROR smb_shm_register_process : processreg_file \
+open failed with code %s\n",strerror(errno)));
+     return False;
+  }
+   
+  *other_processes = False;
+   
+  while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
+  {
+    if(other_pid)
+    {
+      if(process_exists(other_pid))
+        *other_processes = True;
+      else
       {
-        if(process_exists(other_pid))
-           *other_processes = True;
-        else
-        {
-           /* erase old pid */
-            DEBUG(5,("smb_shm_register_process : erasing stale record for pid %d (seek_back = %d)\n",
-                      other_pid, seek_back));
-           other_pid = (pid_t)0;
-           erased_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
-           write(smb_shm_processes_fd, &other_pid, sizeof(other_pid));
-           if(free_slot < 0)
-              free_slot = erased_slot;
-        }
+        /* erase old pid */
+        DEBUG(5,("smb_shm_register_process : erasing stale record \
+for pid %d (seek_back = %.0f)\n", (int)other_pid, (double)seek_back));
+        other_pid = (pid_t)0;
+        if((erased_slot = sys_lseek(smb_shm_processes_fd,
+                                    seek_back, SEEK_CUR)) == -1)
+        {
+          DEBUG(0, ("ERROR smb_shm_register_process : sys_lseek failed \
+with error %s\n", strerror(errno)));
+          close(smb_shm_processes_fd);
+          return False;
+        }
+        
+        if(write(smb_shm_processes_fd, &other_pid, sizeof(other_pid)) == -1)
+        {
+           DEBUG(0, ("ERROR smb_shm_register_process : write failed \
+with error %s\n", strerror(errno)));
+           close(smb_shm_processes_fd);
+           return False;
+        }
+
+        if(free_slot < 0)
+          free_slot = erased_slot;
       }
-      else 
-        if(free_slot < 0)
-           free_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
-   }
-   if (nb_read < 0)
-   {
-      DEBUG(0,("ERROR smb_shm_register_process : processreg_file read failed with code %s\n",strerror(errno)));
+    }
+    else 
+    {
+      if(free_slot < 0)
+      {
+        if((free_slot = sys_lseek(smb_shm_processes_fd, 
+                                  seek_back, SEEK_CUR))==-1)
+        {
+          DEBUG(0, ("ERROR smb_shm_register_process : sys_lseek \
+failed with error %s\n", strerror(errno)));
+          close(smb_shm_processes_fd);
+          return False; 
+        }
+      } /* end if free_slot */
+    } /* end else */
+  } /* end if other_pid */
+
+  if (nb_read < 0)
+  {
+    DEBUG(0,("ERROR smb_shm_register_process : processreg_file read \
+failed with code %s\n",strerror(errno)));
+    close(smb_shm_processes_fd);
+    return False;
+  }
+   
+  if(free_slot < 0)
+  {
+    if((free_slot = sys_lseek(smb_shm_processes_fd, 0, SEEK_END)) == -1)
+    {
+      DEBUG(0,("ERROR smb_shm_register_process : sys_lseek failed with code %s\n",strerror(errno)));
       close(smb_shm_processes_fd);
       return False;
-   }
-   
-   if(free_slot < 0)
-      free_slot = lseek(smb_shm_processes_fd, 0, SEEK_END);
+    }
+  }
 
-   DEBUG(5,("smb_shm_register_process : writing record for pid %d at offset %d\n",pid,free_slot));
-   lseek(smb_shm_processes_fd, free_slot, SEEK_SET);
-   if(write(smb_shm_processes_fd, &pid, sizeof(pid)) < 0)
-   {
-      DEBUG(0,("ERROR smb_shm_register_process : processreg_file write failed with code %s\n",strerror(errno)));
-      close(smb_shm_processes_fd);
-      return False;
-   }
+  DEBUG(5,("smb_shm_register_process : writing record for pid %d at offset %.0f\n",
+         (int)pid, (double)free_slot));
 
-   close(smb_shm_processes_fd);
+  if(sys_lseek(smb_shm_processes_fd, free_slot, SEEK_SET) == -1)
+  {
+    DEBUG(0,("ERROR smb_shm_register_process : sys_lseek failed with code %s\n",strerror(errno)));
+    close(smb_shm_processes_fd);
+    return False;
+  }
 
-   return True;
+  if(write(smb_shm_processes_fd, &pid, sizeof(pid)) == -1)
+  {
+    DEBUG(0,("ERROR smb_shm_register_process : processreg_file write failed with code %s\n",strerror(errno)));
+    close(smb_shm_processes_fd);
+    return False;
+  }
+
+  close(smb_shm_processes_fd);
+
+  return True;
 }
 
 static BOOL smb_shm_unregister_process(char *processreg_file, pid_t pid)
@@ -378,12 +425,11 @@ static BOOL smb_shm_unregister_process(char *processreg_file, pid_t pid)
    int smb_shm_processes_fd = -1;
    int nb_read;
    pid_t other_pid;
-   int seek_back = -((int)sizeof(other_pid));
-   int erased_slot;
+   SMB_OFF_T seek_back = -((SMB_OFF_T)sizeof(other_pid));
    BOOL found = False;
    
    
-   smb_shm_processes_fd = open(processreg_file, O_RDWR);
+   smb_shm_processes_fd = sys_open(processreg_file, O_RDWR, 0);
    if ( smb_shm_processes_fd < 0 )
    {
       DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file open failed with code %s\n",strerror(errno)));
@@ -392,23 +438,28 @@ static BOOL smb_shm_unregister_process(char *processreg_file, pid_t pid)
    
    while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
    {
-      DEBUG(5,("smb_shm_unregister_process : read record for pid %d\n",other_pid));
+      DEBUG(5,("smb_shm_unregister_process : read record for pid %d\n",(int)other_pid));
       if(other_pid == pid)
       {
-        /* erase pid */
-         DEBUG(5,("smb_shm_unregister_process : erasing record for pid %d (seek_val = %d)\n",
-                     other_pid, seek_back));
-        other_pid = (pid_t)0;
-        erased_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
-        if(write(smb_shm_processes_fd, &other_pid, sizeof(other_pid)) < 0)
-        {
-           DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file write failed with code %s\n",strerror(errno)));
-           close(smb_shm_processes_fd);
-           return False;
-        }
+        /* erase pid */
+        DEBUG(5,("smb_shm_unregister_process : erasing record for pid %d (seek_val = %.0f)\n",
+                     (int)other_pid, (double)seek_back));
+        other_pid = (pid_t)0;
+        if(sys_lseek(smb_shm_processes_fd, seek_back, SEEK_CUR) == -1)
+        {
+          DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file sys_lseek failed with code %s\n",strerror(errno)));
+          close(smb_shm_processes_fd);
+          return False;
+        }
+        if(write(smb_shm_processes_fd, &other_pid, sizeof(other_pid)) < 0)
+        {
+          DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file write failed with code %s\n",strerror(errno)));
+          close(smb_shm_processes_fd);
+          return False;
+        }
         
-        found = True;
-        break;
+        found = True;
+        break;
       }
    }
    if (nb_read < 0)
@@ -420,7 +471,8 @@ static BOOL smb_shm_unregister_process(char *processreg_file, pid_t pid)
    
    if(!found)
    {
-      DEBUG(0,("ERROR smb_shm_unregister_process : couldn't find pid %d in file %s\n",pid,processreg_file));
+      DEBUG(0,("ERROR smb_shm_unregister_process : couldn't find pid %d in file %s\n",
+            (int)pid,processreg_file));
       close(smb_shm_processes_fd);
       return False;
    }
@@ -483,7 +535,7 @@ static BOOL smb_shm_initialize(int size)
    smb_shm_header_p->smb_shm_version = SMB_SHM_VERSION;
    smb_shm_header_p->total_size = size;
    smb_shm_header_p->first_free_off = AlignedHeaderSize;
-   smb_shm_header_p->userdef_off = NULL_OFFSET;
+   smb_shm_header_p->userdef_off = 0;
    
    first_free_block_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
    first_free_block_p->next = EOList_Off;
@@ -537,7 +589,8 @@ static BOOL smb_shm_close( void )
    }
 
    smb_shm_global_lock();
-   DEBUG(5,("calling smb_shm_unregister_process(%s, %d)\n", smb_shm_processreg_name, getpid()));
+   DEBUG(5,("calling smb_shm_unregister_process(%s, %d)\n", 
+         smb_shm_processreg_name, (int)getpid()));
    smb_shm_unregister_process(smb_shm_processreg_name, getpid());
    smb_shm_global_unlock();
    
@@ -600,6 +653,9 @@ static BOOL smb_shm_free(int offset)
    
    DEBUG(6,("smb_shm_free : freeing %d bytes at offset %d\n",header_p->size*CellSize,offset));
 
+   /* zero the area being freed - this allows us to find bugs faster */
+   memset(smb_shm_offset2addr(offset), 0, header_p->size*CellSize);
+
    if ( scanner_p == prev_p )
    {
       smb_shm_header_p->statistics.cells_free += header_p->size;
@@ -635,7 +691,7 @@ static BOOL smb_shm_free(int offset)
 static int smb_shm_get_userdef_off(void)
 {
    if (!smb_shm_header_p)
-      return NULL_OFFSET;
+      return 0;
    else
       return smb_shm_header_p->userdef_off;
 }
@@ -653,14 +709,11 @@ static BOOL smb_shm_lock_hash_entry( unsigned int entry)
       return False;
     }
 
-  if(entry >= lp_shmem_hash_size())
-    {
-      DEBUG(0,("ERROR smb_shm_lock_hash_entry : hash entry size too big (%d)\n", entry));
-      return False;
-    }
-  
+  if (read_only)
+         return True;
+
   /* Do an exclusive wait lock on the 4 byte region mapping into this entry  */
-  if (fcntl_lock(smb_shm_fd, F_SETLKW, start, sizeof(int), F_WRLCK) == False)
+  if (fcntl_lock(smb_shm_fd, SMB_F_SETLKW, start, sizeof(int), F_WRLCK) == False)
     {
       DEBUG(0,("ERROR smb_shm_lock_hash_entry : fcntl_lock failed with code %s\n",strerror(errno)));
       return False;
@@ -682,15 +735,12 @@ static BOOL smb_shm_unlock_hash_entry( unsigned int entry )
       DEBUG(0,("ERROR smb_shm_unlock_hash_entry : bad smb_shm_fd (%d)\n",smb_shm_fd));
       return False;
     }
-   
-  if(entry >= lp_shmem_hash_size())
-    {
-      DEBUG(0,("ERROR smb_shm_unlock_hash_entry : hash entry size too big (%d)\n", entry));
-      return False;
-    }
 
+  if (read_only)
+         return True;
+   
   /* Do a wait lock on the 4 byte region mapping into this entry  */
-  if (fcntl_lock(smb_shm_fd, F_SETLKW, start, sizeof(int), F_UNLCK) == False)
+  if (fcntl_lock(smb_shm_fd, SMB_F_SETLKW, start, sizeof(int), F_UNLCK) == False)
     {
       DEBUG(0,("ERROR smb_shm_unlock_hash_entry : fcntl_lock failed with code %s\n",strerror(errno)));
       return False;
@@ -720,6 +770,13 @@ static BOOL smb_shm_get_usage(int *bytes_free,
    return True;
 }
 
+/*******************************************************************
+hash a number into a hash_entry
+  ******************************************************************/
+static unsigned smb_shm_hash_size(void)
+{
+       return SHMEM_HASH_SIZE;
+}
 
 static struct shmem_ops shmops = {
        smb_shm_close,
@@ -731,134 +788,176 @@ static struct shmem_ops shmops = {
        smb_shm_lock_hash_entry,
        smb_shm_unlock_hash_entry,
        smb_shm_get_usage,
+       smb_shm_hash_size,
 };
 
 /*******************************************************************
   open the shared memory
   ******************************************************************/
-struct shmem_ops *smb_shm_open(char *file_name, int size, int ronly)
+struct shmem_ops *smb_shm_open(int ronly)
 {
-   int filesize;
-   BOOL created_new = False;
-   BOOL other_processes = True;
+  pstring file_name;
+  SMB_OFF_T filesize;
+  BOOL created_new = False;
+  BOOL other_processes = True;
+  SMB_OFF_T size = (SMB_OFF_T)lp_shmem_size();
 
-   read_only = ronly;
-   
-   DEBUG(5,("smb_shm_open : using shmem file %s to be of size %d\n",file_name,size));
-
-   smb_shm_fd = open(file_name, read_only?O_RDONLY:(O_RDWR|O_CREAT),
-                    SHM_FILE_MODE);
+  read_only = ronly;
 
-   if ( smb_shm_fd < 0 )
-   {
-      DEBUG(0,("ERROR smb_shm_open : open failed with code %s\n",strerror(errno)));
-      return NULL;
-   }
-   
-   if (!smb_shm_global_lock())
-   {
-      DEBUG(0,("ERROR smb_shm_open : can't do smb_shm_global_lock\n"));
-      return NULL;
-   }
-   
-   if( (filesize = lseek(smb_shm_fd, 0, SEEK_END)) < 0)
-   {
-      DEBUG(0,("ERROR smb_shm_open : lseek failed with code %s\n",strerror(errno)));
-      smb_shm_global_unlock();
-      close(smb_shm_fd);
+  pstrcpy(file_name,lp_lockdir());
+  if (!directory_exist(file_name,NULL)) {
+    if (read_only)
       return NULL;
-   }
-
-   /* return the file offset to 0 to save on later seeks */
-   lseek(smb_shm_fd,0,SEEK_SET);
-
-   if (filesize == 0)
-   {
-      /* we just created a new one */
-      created_new = True;
-   }
-   
-   /* to find out if some other process is already mapping the file,
-      we use a registration file containing the processids of the file mapping processes
-      */
-
-   /* construct processreg file name */
-   strcpy(smb_shm_processreg_name, file_name);
-   strcat(smb_shm_processreg_name, ".processes");
-
-   if (!read_only && 
-       !smb_shm_register_process(smb_shm_processreg_name, getpid(), &other_processes))
-   {
+    mkdir(file_name,0755);
+  }
+  trim_string(file_name,"","/");
+  if (!*file_name)
+    return(False);
+  pstrcat(file_name, "/SHARE_MEM_FILE");
+   
+  DEBUG(5,("smb_shm_open : using shmem file %s to be of size %.0f\n",
+        file_name,(double)size));
+
+  smb_shm_fd = sys_open(file_name, read_only?O_RDONLY:(O_RDWR|O_CREAT),
+                    SHM_FILE_MODE);
+
+  if ( smb_shm_fd < 0 )
+  {
+    DEBUG(0,("ERROR smb_shm_open : open failed with code %s\n",strerror(errno)));
+    return NULL;
+  }
+   
+  if (!smb_shm_global_lock())
+  {
+    DEBUG(0,("ERROR smb_shm_open : can't do smb_shm_global_lock\n"));
+    return NULL;
+  }
+   
+  if( (filesize = sys_lseek(smb_shm_fd, 0, SEEK_END)) == -1)
+  {
+    DEBUG(0,("ERROR smb_shm_open : sys_lseek failed with code %s\n",
+          strerror(errno)));
+    smb_shm_global_unlock();
+    close(smb_shm_fd);
+    return NULL;
+  }
+
+  /* 
+   * Return the file offset to 0 to save on later seeks.
+   */
+  if(sys_lseek(smb_shm_fd,0,SEEK_SET) == -1)
+  {
+    DEBUG(0,("ERROR smb_shm_open : sys_lseek failed with code %s\n",
+          strerror(errno)));
+    smb_shm_global_unlock();
+    close(smb_shm_fd);
+    return NULL;
+  }
+
+  if (filesize == 0)
+  {
+    /* 
+     * We just created a new one.
+     */
+    created_new = True;
+  }
+   
+  /* 
+   * To find out if some other process is already mapping the file,
+   * we use a registration file containing the processids of the file
+   * mapping processes.
+   */
+
+  /* construct processreg file name */
+  pstrcpy(smb_shm_processreg_name, file_name);
+  pstrcat(smb_shm_processreg_name, ".processes");
+
+  if (!read_only && !smb_shm_register_process(smb_shm_processreg_name,
+                                              getpid(), &other_processes))
+  {
+    smb_shm_global_unlock();
+    close(smb_shm_fd);
+    return NULL;
+  }
+
+  if (!read_only && (created_new || !other_processes))
+  {
+    /* we just created a new one, or are the first opener, lets set it size */
+    if( sys_ftruncate(smb_shm_fd, size) <0)
+    {
+      DEBUG(0,("ERROR smb_shm_open : ftruncate failed with code %s\n",
+            strerror(errno)));
+      smb_shm_unregister_process(smb_shm_processreg_name, getpid());
       smb_shm_global_unlock();
       close(smb_shm_fd);
       return NULL;
-   }
-
-   if (!read_only && (created_new || !other_processes))
-   {
-      /* we just created a new one, or are the first opener, lets set it size */
-      if( ftruncate(smb_shm_fd, size) <0)
-      {
-         DEBUG(0,("ERROR smb_shm_open : ftruncate failed with code %s\n",strerror(errno)));
-        smb_shm_unregister_process(smb_shm_processreg_name, getpid());
-        smb_shm_global_unlock();
-        close(smb_shm_fd);
-        return NULL;
-      }
-
-      /* paranoia */
-      lseek(smb_shm_fd,0,SEEK_SET);
+    }
 
-      filesize = size;
-   }
-   
-   if (size != filesize )
-   {
-      /* the existing file has a different size and we are not the first opener.
-        Since another process is still using it, we will use the file size */
-      DEBUG(0,("WARNING smb_shm_open : filesize (%d) != expected size (%d), using filesize\n",filesize,size));
-      size = filesize;
-   }
-   
-   smb_shm_header_p = (struct SmbShmHeader *)mmap(NULL, size, 
-                                                 read_only?PROT_READ:
-                                                 (PROT_READ | PROT_WRITE), 
-                                                 MAP_FILE | MAP_SHARED, 
-                                                 smb_shm_fd, 0);
-   /* WARNING, smb_shm_header_p can be different for different processes mapping the same file ! */
-   if (smb_shm_header_p  == (struct SmbShmHeader *)(-1))
-   {
-      DEBUG(0,("ERROR smb_shm_open : mmap failed with code %s\n",strerror(errno)));
+    /* paranoia */
+    if(sys_lseek(smb_shm_fd,0,SEEK_SET) == -1)
+    {
+      DEBUG(0,("ERROR smb_shm_open : sys_lseek failed with code %s\n",
+            strerror(errno)));
       smb_shm_unregister_process(smb_shm_processreg_name, getpid());
       smb_shm_global_unlock();
       close(smb_shm_fd);
       return NULL;
-   }      
+    }
+
+    filesize = size;
+  }
+   
+  if (size != filesize )
+  {
+    /* the existing file has a different size and we are not the first opener.
+      Since another process is still using it, we will use the file size */
+    DEBUG(0,("WARNING smb_shm_open : filesize (%.0f) != expected \
+size (%.0f), using filesize\n", (double)filesize, (double)size));
+
+    size = filesize;
+  }
+   
+  smb_shm_header_p = (struct SmbShmHeader *)sys_mmap(NULL, size, 
+                               read_only?PROT_READ: (PROT_READ | PROT_WRITE), 
+                               MAP_FILE | MAP_SHARED, smb_shm_fd, (SMB_OFF_T)0);
+
+  /* 
+   * WARNING, smb_shm_header_p can be different for different
+   * processes mapping the same file !
+   */
+  if (smb_shm_header_p  == (struct SmbShmHeader *)(-1))
+  {
+    DEBUG(0,("ERROR smb_shm_open : mmap failed with code %s\n",strerror(errno)));
+    smb_shm_unregister_process(smb_shm_processreg_name, getpid());
+    smb_shm_global_unlock();
+    close(smb_shm_fd);
+    return NULL;
+  }      
    
       
-   if (!read_only && (created_new || !other_processes))
-   {
-      smb_shm_initialize(size);
-      /* Create the hash buckets for the share file entries. */
-      smb_shm_create_hash_table( lp_shmem_hash_size() );
-   }
-   else if (!smb_shm_validate_header(size) )
-   {
-      /* existing file is corrupt, samba admin should remove it by hand */
-      DEBUG(0,("ERROR smb_shm_open : corrupt shared mem file, remove it manually\n"));
-      munmap((caddr_t)smb_shm_header_p, size);
-      smb_shm_unregister_process(smb_shm_processreg_name, getpid());
-      smb_shm_global_unlock();
-      close(smb_shm_fd);
-      return NULL;
-   }
+  if (!read_only && (created_new || !other_processes))
+  {
+    smb_shm_initialize(size);
+    /* Create the hash buckets for the share file entries. */
+    smb_shm_create_hash_table(SHMEM_HASH_SIZE);
+  }
+  else if (!smb_shm_validate_header(size) )
+  {
+    /* existing file is corrupt, samba admin should remove it by hand */
+    DEBUG(0,("ERROR smb_shm_open : corrupt shared mem file, remove it manually\n"));
+    munmap((caddr_t)smb_shm_header_p, size);
+    smb_shm_unregister_process(smb_shm_processreg_name, getpid());
+    smb_shm_global_unlock();
+    close(smb_shm_fd);
+    return NULL;
+  }
    
-   smb_shm_global_unlock();
-   return &shmops;
+  smb_shm_global_unlock();
+  return &shmops;
 }
 
 
-#else /* FAST_SHARE_MODES */
+#else /* HAVE_SHARED_MMAP */
  int shmem_dummy_procedure(void)
 {return 0;}
-#endif /* FAST_SHARE_MODES */
+#endif /* HAVE_SHARED_MMAP */