b63db1f168dc7fa0b422a2f57de4b1233bc5cec7
[samba.git] / source / locking / shmem.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Shared memory functions
5    Copyright (C) Erik Devriendt 1996-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 */
22
23 #include "includes.h"
24
25
26 #ifdef HAVE_SHARED_MMAP
27
28
29 extern int DEBUGLEVEL;
30
31
32 #define SMB_SHM_MAGIC 0x53484100
33 /* = "SHM" in hex */
34
35 #define SMB_SHM_VERSION 2
36
37 /* we need world read for smbstatus to function correctly */
38 #ifdef SECURE_SHARE_MODES
39 #define SHM_FILE_MODE 0600
40 #else
41 #define SHM_FILE_MODE 0644
42 #endif
43
44 #define SHMEM_HASH_SIZE 13
45
46
47 /* WARNING : offsets are used because mmap() does not guarantee that all processes have the 
48    shared memory mapped to the same address */
49
50 struct SmbShmHeader
51 {
52    int smb_shm_magic;
53    int smb_shm_version;
54    int total_size;      /* in bytes */
55    BOOL consistent;
56    int first_free_off;
57    int userdef_off;    /* a userdefined offset. can be used to store root of tree or list */
58    struct {             /* a cell is a range of bytes of sizeof(struct SmbShmBlockDesc) size */
59       int cells_free;
60       int cells_used;
61       int cells_system; /* number of cells used as allocated block descriptors */
62    } statistics;
63 };
64
65 #define SMB_SHM_NOT_FREE_OFF (-1)
66 struct SmbShmBlockDesc
67 {
68    int next;    /* offset of next block in the free list or SMB_SHM_NOT_FREE_OFF when block in use  */
69    int          size;   /* user size in BlockDescSize units */
70 };
71
72 #define EOList_Addr     (struct SmbShmBlockDesc *)( 0 )
73 #define EOList_Off      0
74
75 #define CellSize        sizeof(struct SmbShmBlockDesc)
76
77 /* HeaderSize aligned on 8 byte boundary */
78 #define AlignedHeaderSize       ((sizeof(struct SmbShmHeader)+7) & ~7)
79
80 static int  smb_shm_fd = -1;
81 static pstring smb_shm_processreg_name = "";
82
83 static struct SmbShmHeader *smb_shm_header_p = (struct SmbShmHeader *)0;
84 static int smb_shm_times_locked = 0;
85
86 static BOOL smb_shm_initialize_called = False;
87
88 static int read_only;
89
90 static BOOL smb_shm_global_lock(void)
91 {
92    if (smb_shm_fd < 0)
93    {
94       DEBUG(0,("ERROR smb_shm_global_lock : bad smb_shm_fd (%d)\n",smb_shm_fd));
95       return False;
96    }
97    
98    smb_shm_times_locked++;
99    
100    if(smb_shm_times_locked > 1)
101    {
102       DEBUG(5,("smb_shm_global_lock : locked %d times\n",smb_shm_times_locked));
103       return True;
104    }
105
106    if (read_only)
107            return True;
108    
109    /* Do an exclusive wait lock on the first byte of the file */
110    if (fcntl_lock(smb_shm_fd, SMB_F_SETLKW, 0, 1, F_WRLCK) == False)
111    {
112       DEBUG(0,("ERROR smb_shm_global_lock : fcntl_lock failed with code %s\n",strerror(errno)));
113       smb_shm_times_locked--;
114       return False;
115    }
116    
117    return True;
118    
119 }
120
121 static BOOL smb_shm_global_unlock(void)
122 {
123    if (smb_shm_fd < 0)
124    {
125       DEBUG(0,("ERROR smb_shm_global_unlock : bad smb_shm_fd (%d)\n",smb_shm_fd));
126       return False;
127    }
128    
129    if(smb_shm_times_locked == 0)
130    {
131       DEBUG(0,("ERROR smb_shm_global_unlock : shmem not locked\n"));
132       return False;
133    }
134    
135    smb_shm_times_locked--;
136    
137    if(smb_shm_times_locked > 0)
138    {
139       DEBUG(5,("smb_shm_global_unlock : still locked %d times\n",smb_shm_times_locked));
140       return True;
141    }
142
143    if (read_only)
144            return True;
145    
146    /* Do a wait unlock on the first byte of the file */
147    if (fcntl_lock(smb_shm_fd, SMB_F_SETLKW, 0, 1, F_UNLCK) == False)
148    {
149       DEBUG(0,("ERROR smb_shm_global_unlock : fcntl_lock failed with code %s\n",strerror(errno)));
150       smb_shm_times_locked++;
151       return False;
152    }
153    
154    return True;
155    
156 }
157
158
159 static void *smb_shm_offset2addr(int offset)
160 {
161    if (offset == 0 )
162       return (void *)(0);
163    
164    if (!smb_shm_header_p)
165       return (void *)(0);
166    
167    return (void *)((char *)smb_shm_header_p + offset );
168 }
169
170 static int smb_shm_addr2offset(void *addr)
171 {
172    if (!addr)
173       return 0;
174    
175    if (!smb_shm_header_p)
176       return 0;
177    
178    return (int)((char *)addr - (char *)smb_shm_header_p);
179 }
180
181
182
183 static int smb_shm_alloc(int size)
184 {
185    unsigned num_cells ;
186    struct SmbShmBlockDesc *scanner_p;
187    struct SmbShmBlockDesc *prev_p;
188    struct SmbShmBlockDesc *new_p;
189    int result_offset;
190    
191    
192    if( !smb_shm_header_p )
193    {
194       /* not mapped yet */
195       DEBUG(0,("ERROR smb_shm_alloc : shmem not mapped\n"));
196       return 0;
197    }
198    
199    smb_shm_global_lock();
200
201    if( !smb_shm_header_p->consistent)
202    {
203       DEBUG(0,("ERROR smb_shm_alloc : shmem not consistent\n"));
204       smb_shm_global_unlock();
205       return 0;
206    }
207    
208    
209    /* calculate the number of cells */
210    num_cells = (size + CellSize -1) / CellSize;
211
212    /* set start of scan */
213    prev_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
214    scanner_p =  prev_p ;
215    
216    /* scan the free list to find a matching free space */
217    while ( ( scanner_p != EOList_Addr ) && ( scanner_p->size < num_cells ) )
218    {
219       prev_p = scanner_p;
220       scanner_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(scanner_p->next);
221    }
222    
223    /* at this point scanner point to a block header or to the end of the list */
224    if ( scanner_p == EOList_Addr )      
225    {
226       DEBUG(0,("ERROR smb_shm_alloc : alloc of %d bytes failed, no free space found\n",size));
227       smb_shm_global_unlock();
228       return (0);
229    }
230    
231    /* going to modify shared mem */
232    smb_shm_header_p->consistent = False;
233    
234    /* if we found a good one : scanner == the good one */
235    if ( scanner_p->size <= num_cells + 2 )
236    {
237       /* there is no use in making a new one, it will be too small anyway 
238       *  we will link out scanner
239       */
240       if ( prev_p == scanner_p )
241       {
242          smb_shm_header_p->first_free_off = scanner_p->next ;
243       }
244       else
245       {
246          prev_p->next = scanner_p->next ;
247       }
248       smb_shm_header_p->statistics.cells_free -= scanner_p->size;
249       smb_shm_header_p->statistics.cells_used += scanner_p->size;
250    }
251    else
252    {
253       /* Make a new one */
254       new_p = scanner_p + 1 + num_cells;
255       new_p->size = scanner_p->size - num_cells - 1;
256       new_p->next = scanner_p->next;
257       scanner_p->size = num_cells;
258       scanner_p->next = smb_shm_addr2offset(new_p);
259       
260       if ( prev_p       != scanner_p )
261       {
262          prev_p->next      = smb_shm_addr2offset(new_p)  ;
263       }
264       else
265       {
266          smb_shm_header_p->first_free_off = smb_shm_addr2offset(new_p)  ;
267       }
268       smb_shm_header_p->statistics.cells_free -= num_cells+1;
269       smb_shm_header_p->statistics.cells_used += num_cells;
270       smb_shm_header_p->statistics.cells_system += 1;
271    }
272
273    result_offset = smb_shm_addr2offset( &(scanner_p[1]) );
274    scanner_p->next =    SMB_SHM_NOT_FREE_OFF ;
275
276    /* end modification of shared mem */
277    smb_shm_header_p->consistent = True;
278
279    DEBUG(6,("smb_shm_alloc : request for %d bytes, allocated %d bytes at offset %d\n",size,scanner_p->size*CellSize,result_offset ));
280
281    smb_shm_global_unlock();
282    return ( result_offset );
283 }   
284
285
286
287
288 /* 
289  * Function to create the hash table for the share mode entries. Called
290  * when smb shared memory is global locked.
291  */
292 static BOOL smb_shm_create_hash_table( unsigned int size )
293 {
294   size *= sizeof(int);
295
296   smb_shm_global_lock();
297   smb_shm_header_p->userdef_off = smb_shm_alloc( size );
298
299   if(smb_shm_header_p->userdef_off == 0)
300     {
301       DEBUG(0,("smb_shm_create_hash_table: Failed to create hash table of size %d\n",size));
302       smb_shm_global_unlock();
303       return False;
304     }
305
306   /* Clear hash buckets. */
307   memset( smb_shm_offset2addr(smb_shm_header_p->userdef_off), '\0', size);
308   smb_shm_global_unlock();
309   return True;
310 }
311
312 static BOOL smb_shm_register_process(char *processreg_file, pid_t pid, BOOL *other_processes)
313 {
314    int smb_shm_processes_fd = -1;
315    int nb_read;
316    pid_t other_pid;
317    SMB_OFF_T seek_back = -((SMB_OFF_T)sizeof(other_pid));
318    SMB_OFF_T free_slot = -1;
319    SMB_OFF_T erased_slot;   
320    
321    smb_shm_processes_fd = open(processreg_file, 
322                                read_only?O_RDONLY:(O_RDWR|O_CREAT), 
323                                SHM_FILE_MODE);
324
325    if ( smb_shm_processes_fd < 0 )
326    {
327       DEBUG(0,("ERROR smb_shm_register_process : processreg_file open failed with code %s\n",strerror(errno)));
328       return False;
329    }
330    
331    *other_processes = False;
332    
333    while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
334    {
335       if(other_pid)
336       {
337          if(process_exists(other_pid))
338             *other_processes = True;
339          else
340          {
341             /* erase old pid */
342             DEBUG(5,("smb_shm_register_process : erasing stale record for pid %d (seek_back = %.0f)\n",
343                       (int)other_pid, (double)seek_back));
344             other_pid = (pid_t)0;
345             erased_slot = sys_lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
346             write(smb_shm_processes_fd, &other_pid, sizeof(other_pid));
347             if(free_slot < 0)
348                free_slot = erased_slot;
349          }
350       }
351       else 
352          if(free_slot < 0)
353             free_slot = sys_lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
354    }
355    if (nb_read < 0)
356    {
357       DEBUG(0,("ERROR smb_shm_register_process : processreg_file read failed with code %s\n",strerror(errno)));
358       close(smb_shm_processes_fd);
359       return False;
360    }
361    
362    if(free_slot < 0)
363       free_slot = sys_lseek(smb_shm_processes_fd, 0, SEEK_END);
364
365    DEBUG(5,("smb_shm_register_process : writing record for pid %d at offset %.0f\n",
366          (int)pid, (double)free_slot));
367
368    sys_lseek(smb_shm_processes_fd, free_slot, SEEK_SET);
369    if(write(smb_shm_processes_fd, &pid, sizeof(pid)) < 0)
370    {
371       DEBUG(0,("ERROR smb_shm_register_process : processreg_file write failed with code %s\n",strerror(errno)));
372       close(smb_shm_processes_fd);
373       return False;
374    }
375
376    close(smb_shm_processes_fd);
377
378    return True;
379 }
380
381 static BOOL smb_shm_unregister_process(char *processreg_file, pid_t pid)
382 {
383    int smb_shm_processes_fd = -1;
384    int nb_read;
385    pid_t other_pid;
386    SMB_OFF_T seek_back = -((SMB_OFF_T)sizeof(other_pid));
387    BOOL found = False;
388    
389    
390    smb_shm_processes_fd = open(processreg_file, O_RDWR);
391    if ( smb_shm_processes_fd < 0 )
392    {
393       DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file open failed with code %s\n",strerror(errno)));
394       return False;
395    }
396    
397    while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
398    {
399       DEBUG(5,("smb_shm_unregister_process : read record for pid %d\n",(int)other_pid));
400       if(other_pid == pid)
401       {
402         /* erase pid */
403         DEBUG(5,("smb_shm_unregister_process : erasing record for pid %d (seek_val = %.0f)\n",
404                      (int)other_pid, (double)seek_back));
405         other_pid = (pid_t)0;
406         if(sys_lseek(smb_shm_processes_fd, seek_back, SEEK_CUR) == -1)
407         {
408           DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file sys_lseek failed with code %s\n",strerror(errno)));
409           close(smb_shm_processes_fd);
410           return False;
411         }
412         if(write(smb_shm_processes_fd, &other_pid, sizeof(other_pid)) < 0)
413         {
414           DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file write failed with code %s\n",strerror(errno)));
415           close(smb_shm_processes_fd);
416           return False;
417         }
418          
419         found = True;
420         break;
421       }
422    }
423    if (nb_read < 0)
424    {
425       DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file read failed with code %s\n",strerror(errno)));
426       close(smb_shm_processes_fd);
427       return False;
428    }
429    
430    if(!found)
431    {
432       DEBUG(0,("ERROR smb_shm_unregister_process : couldn't find pid %d in file %s\n",
433             (int)pid,processreg_file));
434       close(smb_shm_processes_fd);
435       return False;
436    }
437       
438    
439    close(smb_shm_processes_fd);
440
441    return True;
442 }
443
444
445 static BOOL smb_shm_validate_header(int size)
446 {
447    if( !smb_shm_header_p )
448    {
449       /* not mapped yet */
450       DEBUG(0,("ERROR smb_shm_validate_header : shmem not mapped\n"));
451       return False;
452    }
453    
454    if(smb_shm_header_p->smb_shm_magic != SMB_SHM_MAGIC)
455    {
456       DEBUG(0,("ERROR smb_shm_validate_header : bad magic\n"));
457       return False;
458    }
459    if(smb_shm_header_p->smb_shm_version != SMB_SHM_VERSION)
460    {
461       DEBUG(0,("ERROR smb_shm_validate_header : bad version %X\n",smb_shm_header_p->smb_shm_version));
462       return False;
463    }
464    
465    if(smb_shm_header_p->total_size != size)
466    {
467       DEBUG(0,("ERROR smb_shm_validate_header : shmem size mismatch (old = %d, new = %d)\n",smb_shm_header_p->total_size,size));
468       return False;
469    }
470
471    if(!smb_shm_header_p->consistent)
472    {
473       DEBUG(0,("ERROR smb_shm_validate_header : shmem not consistent\n"));
474       return False;
475    }
476    return True;
477 }
478
479 static BOOL smb_shm_initialize(int size)
480 {
481    struct SmbShmBlockDesc * first_free_block_p;
482    
483    DEBUG(5,("smb_shm_initialize : initializing shmem file of size %d\n",size));
484    
485    if( !smb_shm_header_p )
486    {
487       /* not mapped yet */
488       DEBUG(0,("ERROR smb_shm_initialize : shmem not mapped\n"));
489       return False;
490    }
491    
492    smb_shm_header_p->smb_shm_magic = SMB_SHM_MAGIC;
493    smb_shm_header_p->smb_shm_version = SMB_SHM_VERSION;
494    smb_shm_header_p->total_size = size;
495    smb_shm_header_p->first_free_off = AlignedHeaderSize;
496    smb_shm_header_p->userdef_off = 0;
497    
498    first_free_block_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
499    first_free_block_p->next = EOList_Off;
500    first_free_block_p->size = ( size - AlignedHeaderSize - CellSize ) / CellSize ;
501    
502    smb_shm_header_p->statistics.cells_free = first_free_block_p->size;
503    smb_shm_header_p->statistics.cells_used = 0;
504    smb_shm_header_p->statistics.cells_system = 1;
505    
506    smb_shm_header_p->consistent = True;
507    
508    smb_shm_initialize_called = True;
509
510    return True;
511 }
512    
513 static void smb_shm_solve_neighbors(struct SmbShmBlockDesc *head_p )
514 {
515    struct SmbShmBlockDesc *next_p;
516    
517    /* Check if head_p and head_p->next are neighbors and if so join them */
518    if ( head_p == EOList_Addr ) return ;
519    if ( head_p->next == EOList_Off ) return ;
520    
521    next_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(head_p->next);
522    if ( ( head_p + head_p->size + 1 ) == next_p)
523    {
524       head_p->size += next_p->size +1 ; /* adapt size */
525       head_p->next = next_p->next         ; /* link out */
526       
527       smb_shm_header_p->statistics.cells_free += 1;
528       smb_shm_header_p->statistics.cells_system -= 1;
529    }
530 }
531
532
533
534 static BOOL smb_shm_close( void )
535 {
536    
537    if(smb_shm_initialize_called == False)
538      return True;
539
540    DEBUG(5,("smb_shm_close\n"));
541    if(smb_shm_times_locked > 0)
542       DEBUG(0,("WARNING smb_shm_close : shmem was still locked %d times\n",smb_shm_times_locked));;
543    if ((smb_shm_header_p != NULL) && 
544               (munmap((caddr_t)smb_shm_header_p, smb_shm_header_p->total_size) < 0))
545    {
546       DEBUG(0,("ERROR smb_shm_close : munmap failed with code %s\n",strerror(errno)));
547    }
548
549    smb_shm_global_lock();
550    DEBUG(5,("calling smb_shm_unregister_process(%s, %d)\n", 
551          smb_shm_processreg_name, (int)getpid()));
552    smb_shm_unregister_process(smb_shm_processreg_name, getpid());
553    smb_shm_global_unlock();
554    
555    close(smb_shm_fd);
556    
557    smb_shm_fd = -1;
558    smb_shm_processreg_name[0] = '\0';
559
560    smb_shm_header_p = (struct SmbShmHeader *)0;
561    smb_shm_times_locked = 0;
562    
563    return True;
564 }
565
566
567 static BOOL smb_shm_free(int offset)
568 {
569    struct SmbShmBlockDesc *header_p  ; /*       pointer to header of block to free */
570    struct SmbShmBlockDesc *scanner_p ; /*       used to scan the list                      */
571    struct SmbShmBlockDesc *prev_p          ; /* holds previous in the list                 */
572    
573    if( !smb_shm_header_p )
574    {
575       /* not mapped yet */
576       DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
577       return False;
578    }
579    
580    smb_shm_global_lock();
581
582    if( !smb_shm_header_p->consistent)
583    {
584       DEBUG(0,("ERROR smb_shm_free : shmem not consistent\n"));
585       smb_shm_global_unlock();
586       return False;
587    }
588    
589    header_p = ( (struct SmbShmBlockDesc *)smb_shm_offset2addr(offset) - 1); /* make pointer to header of block */
590
591    if (header_p->next != SMB_SHM_NOT_FREE_OFF)
592    {
593       DEBUG(0,("ERROR smb_shm_free : bad offset (%d)\n",offset));
594       smb_shm_global_unlock();
595       return False;
596    }
597    
598    /* find a place in the free_list to put the header in */
599    
600    /* set scanner and previous pointer to start of list */
601    prev_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
602    scanner_p = prev_p ;
603    
604    while ( ( scanner_p != EOList_Addr) && (scanner_p < header_p) ) /* while we didn't scan past its position */
605    {
606       prev_p = scanner_p ;
607       scanner_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(scanner_p->next);
608    }
609    
610    smb_shm_header_p->consistent = False;
611    
612    DEBUG(6,("smb_shm_free : freeing %d bytes at offset %d\n",header_p->size*CellSize,offset));
613
614    /* zero the area being freed - this allows us to find bugs faster */
615    memset(smb_shm_offset2addr(offset), 0, header_p->size*CellSize);
616
617    if ( scanner_p == prev_p )
618    {
619       smb_shm_header_p->statistics.cells_free += header_p->size;
620       smb_shm_header_p->statistics.cells_used -= header_p->size;
621
622       /* we must free it at the beginning of the list */
623       smb_shm_header_p->first_free_off = smb_shm_addr2offset(header_p);                                          /*     set     the free_list_pointer to this block_header */
624
625       /* scanner is the one that was first in the list */
626       header_p->next = smb_shm_addr2offset(scanner_p);
627       smb_shm_solve_neighbors( header_p ); /* if neighbors then link them */
628       
629       smb_shm_header_p->consistent = True;
630       smb_shm_global_unlock();
631       return True;
632    } 
633    else
634    {
635       smb_shm_header_p->statistics.cells_free += header_p->size;
636       smb_shm_header_p->statistics.cells_used -= header_p->size;
637
638       prev_p->next = smb_shm_addr2offset(header_p);
639       header_p->next = smb_shm_addr2offset(scanner_p);
640       smb_shm_solve_neighbors(header_p) ;
641       smb_shm_solve_neighbors(prev_p) ;
642
643       smb_shm_header_p->consistent = True;
644       smb_shm_global_unlock();
645       return True;
646    }
647 }
648
649 static int smb_shm_get_userdef_off(void)
650 {
651    if (!smb_shm_header_p)
652       return 0;
653    else
654       return smb_shm_header_p->userdef_off;
655 }
656
657 /*******************************************************************
658   Lock a particular hash bucket entry.
659   ******************************************************************/
660 static BOOL smb_shm_lock_hash_entry( unsigned int entry)
661 {
662   int start = (smb_shm_header_p->userdef_off + (entry * sizeof(int)));
663
664   if (smb_shm_fd < 0)
665     {
666       DEBUG(0,("ERROR smb_shm_lock_hash_entry : bad smb_shm_fd (%d)\n",smb_shm_fd));
667       return False;
668     }
669
670   if (read_only)
671           return True;
672
673   /* Do an exclusive wait lock on the 4 byte region mapping into this entry  */
674   if (fcntl_lock(smb_shm_fd, SMB_F_SETLKW, start, sizeof(int), F_WRLCK) == False)
675     {
676       DEBUG(0,("ERROR smb_shm_lock_hash_entry : fcntl_lock failed with code %s\n",strerror(errno)));
677       return False;
678     }
679   
680   DEBUG(9,("smb_shm_lock_hash_entry: locked hash bucket %d\n", entry)); 
681   return True;
682 }
683
684 /*******************************************************************
685   Unlock a particular hash bucket entry.
686   ******************************************************************/
687 static BOOL smb_shm_unlock_hash_entry( unsigned int entry )
688 {
689   int start = (smb_shm_header_p->userdef_off + (entry * sizeof(int)));
690
691   if (smb_shm_fd < 0)
692     {
693       DEBUG(0,("ERROR smb_shm_unlock_hash_entry : bad smb_shm_fd (%d)\n",smb_shm_fd));
694       return False;
695     }
696
697   if (read_only)
698           return True;
699    
700   /* Do a wait lock on the 4 byte region mapping into this entry  */
701   if (fcntl_lock(smb_shm_fd, SMB_F_SETLKW, start, sizeof(int), F_UNLCK) == False)
702     {
703       DEBUG(0,("ERROR smb_shm_unlock_hash_entry : fcntl_lock failed with code %s\n",strerror(errno)));
704       return False;
705     }
706   
707   DEBUG(9,("smb_shm_unlock_hash_entry: unlocked hash bucket %d\n", entry)); 
708   return True;
709 }
710
711 /*******************************************************************
712   Gather statistics on shared memory usage.
713   ******************************************************************/
714 static BOOL smb_shm_get_usage(int *bytes_free,
715                    int *bytes_used,
716                    int *bytes_overhead)
717 {
718    if( !smb_shm_header_p )
719    {
720       /* not mapped yet */
721       DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
722       return False;
723    }
724    *bytes_free = smb_shm_header_p->statistics.cells_free * CellSize;
725    *bytes_used = smb_shm_header_p->statistics.cells_used * CellSize;
726    *bytes_overhead = smb_shm_header_p->statistics.cells_system * CellSize + AlignedHeaderSize;
727    
728    return True;
729 }
730
731 /*******************************************************************
732 hash a number into a hash_entry
733   ******************************************************************/
734 static unsigned smb_shm_hash_size(void)
735 {
736         return SHMEM_HASH_SIZE;
737 }
738
739 static struct shmem_ops shmops = {
740         smb_shm_close,
741         smb_shm_alloc,
742         smb_shm_free,
743         smb_shm_get_userdef_off,
744         smb_shm_offset2addr,
745         smb_shm_addr2offset,
746         smb_shm_lock_hash_entry,
747         smb_shm_unlock_hash_entry,
748         smb_shm_get_usage,
749         smb_shm_hash_size,
750 };
751
752 /*******************************************************************
753   open the shared memory
754   ******************************************************************/
755 struct shmem_ops *smb_shm_open(int ronly)
756 {
757         pstring file_name;
758         SMB_OFF_T filesize;
759         BOOL created_new = False;
760         BOOL other_processes = True;
761         SMB_OFF_T size = (SMB_OFF_T)lp_shmem_size();
762
763         read_only = ronly;
764
765         pstrcpy(file_name,lp_lockdir());
766         if (!directory_exist(file_name,NULL)) {
767                 if (read_only) return NULL;
768                 mkdir(file_name,0755);
769         }
770         trim_string(file_name,"","/");
771         if (!*file_name) return(False);
772         pstrcat(file_name, "/SHARE_MEM_FILE");
773    
774    DEBUG(5,("smb_shm_open : using shmem file %s to be of size %.0f\n",file_name,(double)size));
775
776    smb_shm_fd = open(file_name, read_only?O_RDONLY:(O_RDWR|O_CREAT),
777                      SHM_FILE_MODE);
778
779    if ( smb_shm_fd < 0 )
780    {
781       DEBUG(0,("ERROR smb_shm_open : open failed with code %s\n",strerror(errno)));
782       return NULL;
783    }
784    
785    if (!smb_shm_global_lock())
786    {
787       DEBUG(0,("ERROR smb_shm_open : can't do smb_shm_global_lock\n"));
788       return NULL;
789    }
790    
791    if( (filesize = sys_lseek(smb_shm_fd, 0, SEEK_END)) < 0)
792    {
793       DEBUG(0,("ERROR smb_shm_open : lseek failed with code %s\n",strerror(errno)));
794       smb_shm_global_unlock();
795       close(smb_shm_fd);
796       return NULL;
797    }
798
799    /* return the file offset to 0 to save on later seeks */
800    sys_lseek(smb_shm_fd,0,SEEK_SET);
801
802    if (filesize == 0)
803    {
804       /* we just created a new one */
805       created_new = True;
806    }
807    
808    /* to find out if some other process is already mapping the file,
809       we use a registration file containing the processids of the file mapping processes
810       */
811
812    /* construct processreg file name */
813    pstrcpy(smb_shm_processreg_name, file_name);
814    pstrcat(smb_shm_processreg_name, ".processes");
815
816    if (!read_only && 
817        !smb_shm_register_process(smb_shm_processreg_name, getpid(), &other_processes))
818    {
819       smb_shm_global_unlock();
820       close(smb_shm_fd);
821       return NULL;
822    }
823
824    if (!read_only && (created_new || !other_processes))
825    {
826       /* we just created a new one, or are the first opener, lets set it size */
827       if( sys_ftruncate(smb_shm_fd, size) <0)
828       {
829         DEBUG(0,("ERROR smb_shm_open : ftruncate failed with code %s\n",strerror(errno)));
830         smb_shm_unregister_process(smb_shm_processreg_name, getpid());
831         smb_shm_global_unlock();
832         close(smb_shm_fd);
833         return NULL;
834       }
835
836       /* paranoia */
837       sys_lseek(smb_shm_fd,0,SEEK_SET);
838
839       filesize = size;
840    }
841    
842    if (size != filesize )
843    {
844       /* the existing file has a different size and we are not the first opener.
845          Since another process is still using it, we will use the file size */
846       DEBUG(0,("WARNING smb_shm_open : filesize (%.0f) != expected size (%.0f), using filesize\n",
847             (double)filesize, (double)size));
848
849       size = filesize;
850    }
851    
852    smb_shm_header_p = (struct SmbShmHeader *)mmap(NULL, size, 
853                                                   read_only?PROT_READ:
854                                                   (PROT_READ | PROT_WRITE), 
855                                                   MAP_FILE | MAP_SHARED, 
856                                                   smb_shm_fd, 0);
857    /* WARNING, smb_shm_header_p can be different for different processes mapping the same file ! */
858    if (smb_shm_header_p  == (struct SmbShmHeader *)(-1))
859    {
860       DEBUG(0,("ERROR smb_shm_open : mmap failed with code %s\n",strerror(errno)));
861       smb_shm_unregister_process(smb_shm_processreg_name, getpid());
862       smb_shm_global_unlock();
863       close(smb_shm_fd);
864       return NULL;
865    }      
866    
867       
868    if (!read_only && (created_new || !other_processes))
869    {
870       smb_shm_initialize(size);
871       /* Create the hash buckets for the share file entries. */
872       smb_shm_create_hash_table(SHMEM_HASH_SIZE);
873    }
874    else if (!smb_shm_validate_header(size) )
875    {
876       /* existing file is corrupt, samba admin should remove it by hand */
877       DEBUG(0,("ERROR smb_shm_open : corrupt shared mem file, remove it manually\n"));
878       munmap((caddr_t)smb_shm_header_p, size);
879       smb_shm_unregister_process(smb_shm_processreg_name, getpid());
880       smb_shm_global_unlock();
881       close(smb_shm_fd);
882       return NULL;
883    }
884    
885    smb_shm_global_unlock();
886    return &shmops;
887 }
888
889
890 #else /* HAVE_SHARED_MMAP */
891  int shmem_dummy_procedure(void)
892 {return 0;}
893 #endif /* HAVE_SHARED_MMAP */