preparing for release of 2.0.0beta3
[Samba.git] / source / locking / shmem.c
blob435c0d4c7894f59f9df69eee77ecb0f64e67966f
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Shared memory functions
5 Copyright (C) Erik Devriendt 1996-1998
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.
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.
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.
23 #include "includes.h"
26 #ifdef HAVE_SHARED_MMAP
29 extern int DEBUGLEVEL;
32 #define SMB_SHM_MAGIC 0x53484100
33 /* = "SHM" in hex */
35 #define SMB_SHM_VERSION 2
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
44 #define SHMEM_HASH_SIZE 13
47 /* WARNING : offsets are used because mmap() does not guarantee that all processes have the
48 shared memory mapped to the same address */
50 struct SmbShmHeader
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;
65 #define SMB_SHM_NOT_FREE_OFF (-1)
66 struct SmbShmBlockDesc
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 */
72 #define EOList_Addr (struct SmbShmBlockDesc *)( 0 )
73 #define EOList_Off 0
75 #define CellSize sizeof(struct SmbShmBlockDesc)
77 /* HeaderSize aligned on 8 byte boundary */
78 #define AlignedHeaderSize ((sizeof(struct SmbShmHeader)+7) & ~7)
80 static int smb_shm_fd = -1;
81 static pstring smb_shm_processreg_name = "";
83 static struct SmbShmHeader *smb_shm_header_p = (struct SmbShmHeader *)0;
84 static int smb_shm_times_locked = 0;
86 static BOOL smb_shm_initialize_called = False;
88 static int read_only;
90 static BOOL smb_shm_global_lock(void)
92 if (smb_shm_fd < 0)
94 DEBUG(0,("ERROR smb_shm_global_lock : bad smb_shm_fd (%d)\n",smb_shm_fd));
95 return False;
98 smb_shm_times_locked++;
100 if(smb_shm_times_locked > 1)
102 DEBUG(5,("smb_shm_global_lock : locked %d times\n",smb_shm_times_locked));
103 return True;
106 if (read_only)
107 return True;
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)
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;
117 return True;
121 static BOOL smb_shm_global_unlock(void)
123 if (smb_shm_fd < 0)
125 DEBUG(0,("ERROR smb_shm_global_unlock : bad smb_shm_fd (%d)\n",smb_shm_fd));
126 return False;
129 if(smb_shm_times_locked == 0)
131 DEBUG(0,("ERROR smb_shm_global_unlock : shmem not locked\n"));
132 return False;
135 smb_shm_times_locked--;
137 if(smb_shm_times_locked > 0)
139 DEBUG(5,("smb_shm_global_unlock : still locked %d times\n",smb_shm_times_locked));
140 return True;
143 if (read_only)
144 return True;
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)
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;
154 return True;
159 static void *smb_shm_offset2addr(int offset)
161 if (offset == 0 )
162 return (void *)(0);
164 if (!smb_shm_header_p)
165 return (void *)(0);
167 return (void *)((char *)smb_shm_header_p + offset );
170 static int smb_shm_addr2offset(void *addr)
172 if (!addr)
173 return 0;
175 if (!smb_shm_header_p)
176 return 0;
178 return (int)((char *)addr - (char *)smb_shm_header_p);
183 static int smb_shm_alloc(int size)
185 unsigned num_cells ;
186 struct SmbShmBlockDesc *scanner_p;
187 struct SmbShmBlockDesc *prev_p;
188 struct SmbShmBlockDesc *new_p;
189 int result_offset;
192 if( !smb_shm_header_p )
194 /* not mapped yet */
195 DEBUG(0,("ERROR smb_shm_alloc : shmem not mapped\n"));
196 return 0;
199 smb_shm_global_lock();
201 if( !smb_shm_header_p->consistent)
203 DEBUG(0,("ERROR smb_shm_alloc : shmem not consistent\n"));
204 smb_shm_global_unlock();
205 return 0;
209 /* calculate the number of cells */
210 num_cells = (size + CellSize -1) / CellSize;
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 ;
216 /* scan the free list to find a matching free space */
217 while ( ( scanner_p != EOList_Addr ) && ( scanner_p->size < num_cells ) )
219 prev_p = scanner_p;
220 scanner_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(scanner_p->next);
223 /* at this point scanner point to a block header or to the end of the list */
224 if ( scanner_p == EOList_Addr )
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);
231 /* going to modify shared mem */
232 smb_shm_header_p->consistent = False;
234 /* if we found a good one : scanner == the good one */
235 if ( scanner_p->size <= num_cells + 2 )
237 /* there is no use in making a new one, it will be too small anyway
238 * we will link out scanner
240 if ( prev_p == scanner_p )
242 smb_shm_header_p->first_free_off = scanner_p->next ;
244 else
246 prev_p->next = scanner_p->next ;
248 smb_shm_header_p->statistics.cells_free -= scanner_p->size;
249 smb_shm_header_p->statistics.cells_used += scanner_p->size;
251 else
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);
260 if ( prev_p != scanner_p )
262 prev_p->next = smb_shm_addr2offset(new_p) ;
264 else
266 smb_shm_header_p->first_free_off = smb_shm_addr2offset(new_p) ;
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;
273 result_offset = smb_shm_addr2offset( &(scanner_p[1]) );
274 scanner_p->next = SMB_SHM_NOT_FREE_OFF ;
276 /* end modification of shared mem */
277 smb_shm_header_p->consistent = True;
279 DEBUG(6,("smb_shm_alloc : request for %d bytes, allocated %d bytes at offset %d\n",size,scanner_p->size*CellSize,result_offset ));
281 smb_shm_global_unlock();
282 return ( result_offset );
289 * Function to create the hash table for the share mode entries. Called
290 * when smb shared memory is global locked.
292 static BOOL smb_shm_create_hash_table( unsigned int size )
294 size *= sizeof(int);
296 smb_shm_global_lock();
297 smb_shm_header_p->userdef_off = smb_shm_alloc( size );
299 if(smb_shm_header_p->userdef_off == 0)
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;
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;
312 static BOOL smb_shm_register_process(char *processreg_file, pid_t pid, BOOL *other_processes)
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;
321 smb_shm_processes_fd = sys_open(processreg_file,
322 read_only?O_RDONLY:(O_RDWR|O_CREAT),
323 SHM_FILE_MODE);
325 if ( smb_shm_processes_fd < 0 )
327 DEBUG(0, ("ERROR smb_shm_register_process : processreg_file \
328 open failed with code %s\n",strerror(errno)));
329 return False;
332 *other_processes = False;
334 while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
336 if(other_pid)
338 if(process_exists(other_pid))
339 *other_processes = True;
340 else
342 /* erase old pid */
343 DEBUG(5,("smb_shm_register_process : erasing stale record \
344 for pid %d (seek_back = %.0f)\n", (int)other_pid, (double)seek_back));
345 other_pid = (pid_t)0;
346 if((erased_slot = sys_lseek(smb_shm_processes_fd,
347 seek_back, SEEK_CUR)) == -1)
349 DEBUG(0, ("ERROR smb_shm_register_process : sys_lseek failed \
350 with error %s\n", strerror(errno)));
351 close(smb_shm_processes_fd);
352 return False;
355 if(write(smb_shm_processes_fd, &other_pid, sizeof(other_pid)) == -1)
357 DEBUG(0, ("ERROR smb_shm_register_process : write failed \
358 with error %s\n", strerror(errno)));
359 close(smb_shm_processes_fd);
360 return False;
363 if(free_slot < 0)
364 free_slot = erased_slot;
367 else
369 if(free_slot < 0)
371 if((free_slot = sys_lseek(smb_shm_processes_fd,
372 seek_back, SEEK_CUR))==-1)
374 DEBUG(0, ("ERROR smb_shm_register_process : sys_lseek \
375 failed with error %s\n", strerror(errno)));
376 close(smb_shm_processes_fd);
377 return False;
379 } /* end if free_slot */
380 } /* end else */
381 } /* end if other_pid */
383 if (nb_read < 0)
385 DEBUG(0,("ERROR smb_shm_register_process : processreg_file read \
386 failed with code %s\n",strerror(errno)));
387 close(smb_shm_processes_fd);
388 return False;
391 if(free_slot < 0)
393 if((free_slot = sys_lseek(smb_shm_processes_fd, 0, SEEK_END)) == -1)
395 DEBUG(0,("ERROR smb_shm_register_process : sys_lseek failed with code %s\n",strerror(errno)));
396 close(smb_shm_processes_fd);
397 return False;
401 DEBUG(5,("smb_shm_register_process : writing record for pid %d at offset %.0f\n",
402 (int)pid, (double)free_slot));
404 if(sys_lseek(smb_shm_processes_fd, free_slot, SEEK_SET) == -1)
406 DEBUG(0,("ERROR smb_shm_register_process : sys_lseek failed with code %s\n",strerror(errno)));
407 close(smb_shm_processes_fd);
408 return False;
411 if(write(smb_shm_processes_fd, &pid, sizeof(pid)) == -1)
413 DEBUG(0,("ERROR smb_shm_register_process : processreg_file write failed with code %s\n",strerror(errno)));
414 close(smb_shm_processes_fd);
415 return False;
418 close(smb_shm_processes_fd);
420 return True;
423 static BOOL smb_shm_unregister_process(char *processreg_file, pid_t pid)
425 int smb_shm_processes_fd = -1;
426 int nb_read;
427 pid_t other_pid;
428 SMB_OFF_T seek_back = -((SMB_OFF_T)sizeof(other_pid));
429 BOOL found = False;
432 smb_shm_processes_fd = sys_open(processreg_file, O_RDWR, 0);
433 if ( smb_shm_processes_fd < 0 )
435 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file open failed with code %s\n",strerror(errno)));
436 return False;
439 while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
441 DEBUG(5,("smb_shm_unregister_process : read record for pid %d\n",(int)other_pid));
442 if(other_pid == pid)
444 /* erase pid */
445 DEBUG(5,("smb_shm_unregister_process : erasing record for pid %d (seek_val = %.0f)\n",
446 (int)other_pid, (double)seek_back));
447 other_pid = (pid_t)0;
448 if(sys_lseek(smb_shm_processes_fd, seek_back, SEEK_CUR) == -1)
450 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file sys_lseek failed with code %s\n",strerror(errno)));
451 close(smb_shm_processes_fd);
452 return False;
454 if(write(smb_shm_processes_fd, &other_pid, sizeof(other_pid)) < 0)
456 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file write failed with code %s\n",strerror(errno)));
457 close(smb_shm_processes_fd);
458 return False;
461 found = True;
462 break;
465 if (nb_read < 0)
467 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file read failed with code %s\n",strerror(errno)));
468 close(smb_shm_processes_fd);
469 return False;
472 if(!found)
474 DEBUG(0,("ERROR smb_shm_unregister_process : couldn't find pid %d in file %s\n",
475 (int)pid,processreg_file));
476 close(smb_shm_processes_fd);
477 return False;
481 close(smb_shm_processes_fd);
483 return True;
487 static BOOL smb_shm_validate_header(int size)
489 if( !smb_shm_header_p )
491 /* not mapped yet */
492 DEBUG(0,("ERROR smb_shm_validate_header : shmem not mapped\n"));
493 return False;
496 if(smb_shm_header_p->smb_shm_magic != SMB_SHM_MAGIC)
498 DEBUG(0,("ERROR smb_shm_validate_header : bad magic\n"));
499 return False;
501 if(smb_shm_header_p->smb_shm_version != SMB_SHM_VERSION)
503 DEBUG(0,("ERROR smb_shm_validate_header : bad version %X\n",smb_shm_header_p->smb_shm_version));
504 return False;
507 if(smb_shm_header_p->total_size != size)
509 DEBUG(0,("ERROR smb_shm_validate_header : shmem size mismatch (old = %d, new = %d)\n",smb_shm_header_p->total_size,size));
510 return False;
513 if(!smb_shm_header_p->consistent)
515 DEBUG(0,("ERROR smb_shm_validate_header : shmem not consistent\n"));
516 return False;
518 return True;
521 static BOOL smb_shm_initialize(int size)
523 struct SmbShmBlockDesc * first_free_block_p;
525 DEBUG(5,("smb_shm_initialize : initializing shmem file of size %d\n",size));
527 if( !smb_shm_header_p )
529 /* not mapped yet */
530 DEBUG(0,("ERROR smb_shm_initialize : shmem not mapped\n"));
531 return False;
534 smb_shm_header_p->smb_shm_magic = SMB_SHM_MAGIC;
535 smb_shm_header_p->smb_shm_version = SMB_SHM_VERSION;
536 smb_shm_header_p->total_size = size;
537 smb_shm_header_p->first_free_off = AlignedHeaderSize;
538 smb_shm_header_p->userdef_off = 0;
540 first_free_block_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
541 first_free_block_p->next = EOList_Off;
542 first_free_block_p->size = ( size - AlignedHeaderSize - CellSize ) / CellSize ;
544 smb_shm_header_p->statistics.cells_free = first_free_block_p->size;
545 smb_shm_header_p->statistics.cells_used = 0;
546 smb_shm_header_p->statistics.cells_system = 1;
548 smb_shm_header_p->consistent = True;
550 smb_shm_initialize_called = True;
552 return True;
555 static void smb_shm_solve_neighbors(struct SmbShmBlockDesc *head_p )
557 struct SmbShmBlockDesc *next_p;
559 /* Check if head_p and head_p->next are neighbors and if so join them */
560 if ( head_p == EOList_Addr ) return ;
561 if ( head_p->next == EOList_Off ) return ;
563 next_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(head_p->next);
564 if ( ( head_p + head_p->size + 1 ) == next_p)
566 head_p->size += next_p->size +1 ; /* adapt size */
567 head_p->next = next_p->next ; /* link out */
569 smb_shm_header_p->statistics.cells_free += 1;
570 smb_shm_header_p->statistics.cells_system -= 1;
576 static BOOL smb_shm_close( void )
579 if(smb_shm_initialize_called == False)
580 return True;
582 DEBUG(5,("smb_shm_close\n"));
583 if(smb_shm_times_locked > 0)
584 DEBUG(0,("WARNING smb_shm_close : shmem was still locked %d times\n",smb_shm_times_locked));;
585 if ((smb_shm_header_p != NULL) &&
586 (munmap((caddr_t)smb_shm_header_p, smb_shm_header_p->total_size) < 0))
588 DEBUG(0,("ERROR smb_shm_close : munmap failed with code %s\n",strerror(errno)));
591 smb_shm_global_lock();
592 DEBUG(5,("calling smb_shm_unregister_process(%s, %d)\n",
593 smb_shm_processreg_name, (int)getpid()));
594 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
595 smb_shm_global_unlock();
597 close(smb_shm_fd);
599 smb_shm_fd = -1;
600 smb_shm_processreg_name[0] = '\0';
602 smb_shm_header_p = (struct SmbShmHeader *)0;
603 smb_shm_times_locked = 0;
605 return True;
609 static BOOL smb_shm_free(int offset)
611 struct SmbShmBlockDesc *header_p ; /* pointer to header of block to free */
612 struct SmbShmBlockDesc *scanner_p ; /* used to scan the list */
613 struct SmbShmBlockDesc *prev_p ; /* holds previous in the list */
615 if( !smb_shm_header_p )
617 /* not mapped yet */
618 DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
619 return False;
622 smb_shm_global_lock();
624 if( !smb_shm_header_p->consistent)
626 DEBUG(0,("ERROR smb_shm_free : shmem not consistent\n"));
627 smb_shm_global_unlock();
628 return False;
631 header_p = ( (struct SmbShmBlockDesc *)smb_shm_offset2addr(offset) - 1); /* make pointer to header of block */
633 if (header_p->next != SMB_SHM_NOT_FREE_OFF)
635 DEBUG(0,("ERROR smb_shm_free : bad offset (%d)\n",offset));
636 smb_shm_global_unlock();
637 return False;
640 /* find a place in the free_list to put the header in */
642 /* set scanner and previous pointer to start of list */
643 prev_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
644 scanner_p = prev_p ;
646 while ( ( scanner_p != EOList_Addr) && (scanner_p < header_p) ) /* while we didn't scan past its position */
648 prev_p = scanner_p ;
649 scanner_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(scanner_p->next);
652 smb_shm_header_p->consistent = False;
654 DEBUG(6,("smb_shm_free : freeing %d bytes at offset %d\n",header_p->size*CellSize,offset));
656 /* zero the area being freed - this allows us to find bugs faster */
657 memset(smb_shm_offset2addr(offset), 0, header_p->size*CellSize);
659 if ( scanner_p == prev_p )
661 smb_shm_header_p->statistics.cells_free += header_p->size;
662 smb_shm_header_p->statistics.cells_used -= header_p->size;
664 /* we must free it at the beginning of the list */
665 smb_shm_header_p->first_free_off = smb_shm_addr2offset(header_p); /* set the free_list_pointer to this block_header */
667 /* scanner is the one that was first in the list */
668 header_p->next = smb_shm_addr2offset(scanner_p);
669 smb_shm_solve_neighbors( header_p ); /* if neighbors then link them */
671 smb_shm_header_p->consistent = True;
672 smb_shm_global_unlock();
673 return True;
675 else
677 smb_shm_header_p->statistics.cells_free += header_p->size;
678 smb_shm_header_p->statistics.cells_used -= header_p->size;
680 prev_p->next = smb_shm_addr2offset(header_p);
681 header_p->next = smb_shm_addr2offset(scanner_p);
682 smb_shm_solve_neighbors(header_p) ;
683 smb_shm_solve_neighbors(prev_p) ;
685 smb_shm_header_p->consistent = True;
686 smb_shm_global_unlock();
687 return True;
691 static int smb_shm_get_userdef_off(void)
693 if (!smb_shm_header_p)
694 return 0;
695 else
696 return smb_shm_header_p->userdef_off;
699 /*******************************************************************
700 Lock a particular hash bucket entry.
701 ******************************************************************/
702 static BOOL smb_shm_lock_hash_entry( unsigned int entry)
704 int start = (smb_shm_header_p->userdef_off + (entry * sizeof(int)));
706 if (smb_shm_fd < 0)
708 DEBUG(0,("ERROR smb_shm_lock_hash_entry : bad smb_shm_fd (%d)\n",smb_shm_fd));
709 return False;
712 if (read_only)
713 return True;
715 /* Do an exclusive wait lock on the 4 byte region mapping into this entry */
716 if (fcntl_lock(smb_shm_fd, SMB_F_SETLKW, start, sizeof(int), F_WRLCK) == False)
718 DEBUG(0,("ERROR smb_shm_lock_hash_entry : fcntl_lock failed with code %s\n",strerror(errno)));
719 return False;
722 DEBUG(9,("smb_shm_lock_hash_entry: locked hash bucket %d\n", entry));
723 return True;
726 /*******************************************************************
727 Unlock a particular hash bucket entry.
728 ******************************************************************/
729 static BOOL smb_shm_unlock_hash_entry( unsigned int entry )
731 int start = (smb_shm_header_p->userdef_off + (entry * sizeof(int)));
733 if (smb_shm_fd < 0)
735 DEBUG(0,("ERROR smb_shm_unlock_hash_entry : bad smb_shm_fd (%d)\n",smb_shm_fd));
736 return False;
739 if (read_only)
740 return True;
742 /* Do a wait lock on the 4 byte region mapping into this entry */
743 if (fcntl_lock(smb_shm_fd, SMB_F_SETLKW, start, sizeof(int), F_UNLCK) == False)
745 DEBUG(0,("ERROR smb_shm_unlock_hash_entry : fcntl_lock failed with code %s\n",strerror(errno)));
746 return False;
749 DEBUG(9,("smb_shm_unlock_hash_entry: unlocked hash bucket %d\n", entry));
750 return True;
753 /*******************************************************************
754 Gather statistics on shared memory usage.
755 ******************************************************************/
756 static BOOL smb_shm_get_usage(int *bytes_free,
757 int *bytes_used,
758 int *bytes_overhead)
760 if( !smb_shm_header_p )
762 /* not mapped yet */
763 DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
764 return False;
766 *bytes_free = smb_shm_header_p->statistics.cells_free * CellSize;
767 *bytes_used = smb_shm_header_p->statistics.cells_used * CellSize;
768 *bytes_overhead = smb_shm_header_p->statistics.cells_system * CellSize + AlignedHeaderSize;
770 return True;
773 /*******************************************************************
774 hash a number into a hash_entry
775 ******************************************************************/
776 static unsigned smb_shm_hash_size(void)
778 return SHMEM_HASH_SIZE;
781 static struct shmem_ops shmops = {
782 smb_shm_close,
783 smb_shm_alloc,
784 smb_shm_free,
785 smb_shm_get_userdef_off,
786 smb_shm_offset2addr,
787 smb_shm_addr2offset,
788 smb_shm_lock_hash_entry,
789 smb_shm_unlock_hash_entry,
790 smb_shm_get_usage,
791 smb_shm_hash_size,
794 /*******************************************************************
795 open the shared memory
796 ******************************************************************/
797 struct shmem_ops *smb_shm_open(int ronly)
799 pstring file_name;
800 SMB_OFF_T filesize;
801 BOOL created_new = False;
802 BOOL other_processes = True;
803 SMB_OFF_T size = (SMB_OFF_T)lp_shmem_size();
805 read_only = ronly;
807 pstrcpy(file_name,lp_lockdir());
808 if (!directory_exist(file_name,NULL)) {
809 if (read_only)
810 return NULL;
811 mkdir(file_name,0755);
813 trim_string(file_name,"","/");
814 if (!*file_name)
815 return(False);
816 pstrcat(file_name, "/SHARE_MEM_FILE");
818 DEBUG(5,("smb_shm_open : using shmem file %s to be of size %.0f\n",
819 file_name,(double)size));
821 smb_shm_fd = sys_open(file_name, read_only?O_RDONLY:(O_RDWR|O_CREAT),
822 SHM_FILE_MODE);
824 if ( smb_shm_fd < 0 )
826 DEBUG(0,("ERROR smb_shm_open : open failed with code %s\n",strerror(errno)));
827 return NULL;
830 if (!smb_shm_global_lock())
832 DEBUG(0,("ERROR smb_shm_open : can't do smb_shm_global_lock\n"));
833 return NULL;
836 if( (filesize = sys_lseek(smb_shm_fd, 0, SEEK_END)) == -1)
838 DEBUG(0,("ERROR smb_shm_open : sys_lseek failed with code %s\n",
839 strerror(errno)));
840 smb_shm_global_unlock();
841 close(smb_shm_fd);
842 return NULL;
846 * Return the file offset to 0 to save on later seeks.
848 if(sys_lseek(smb_shm_fd,0,SEEK_SET) == -1)
850 DEBUG(0,("ERROR smb_shm_open : sys_lseek failed with code %s\n",
851 strerror(errno)));
852 smb_shm_global_unlock();
853 close(smb_shm_fd);
854 return NULL;
857 if (filesize == 0)
860 * We just created a new one.
862 created_new = True;
866 * To find out if some other process is already mapping the file,
867 * we use a registration file containing the processids of the file
868 * mapping processes.
871 /* construct processreg file name */
872 pstrcpy(smb_shm_processreg_name, file_name);
873 pstrcat(smb_shm_processreg_name, ".processes");
875 if (!read_only && !smb_shm_register_process(smb_shm_processreg_name,
876 getpid(), &other_processes))
878 smb_shm_global_unlock();
879 close(smb_shm_fd);
880 return NULL;
883 if (!read_only && (created_new || !other_processes))
885 /* we just created a new one, or are the first opener, lets set it size */
886 if( sys_ftruncate(smb_shm_fd, size) <0)
888 DEBUG(0,("ERROR smb_shm_open : ftruncate failed with code %s\n",
889 strerror(errno)));
890 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
891 smb_shm_global_unlock();
892 close(smb_shm_fd);
893 return NULL;
896 /* paranoia */
897 if(sys_lseek(smb_shm_fd,0,SEEK_SET) == -1)
899 DEBUG(0,("ERROR smb_shm_open : sys_lseek failed with code %s\n",
900 strerror(errno)));
901 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
902 smb_shm_global_unlock();
903 close(smb_shm_fd);
904 return NULL;
907 filesize = size;
910 if (size != filesize )
912 /* the existing file has a different size and we are not the first opener.
913 Since another process is still using it, we will use the file size */
914 DEBUG(0,("WARNING smb_shm_open : filesize (%.0f) != expected \
915 size (%.0f), using filesize\n", (double)filesize, (double)size));
917 size = filesize;
920 smb_shm_header_p = (struct SmbShmHeader *)sys_mmap(NULL, size,
921 read_only?PROT_READ: (PROT_READ | PROT_WRITE),
922 MAP_FILE | MAP_SHARED, smb_shm_fd, (SMB_OFF_T)0);
925 * WARNING, smb_shm_header_p can be different for different
926 * processes mapping the same file !
928 if (smb_shm_header_p == (struct SmbShmHeader *)(-1))
930 DEBUG(0,("ERROR smb_shm_open : mmap failed with code %s\n",strerror(errno)));
931 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
932 smb_shm_global_unlock();
933 close(smb_shm_fd);
934 return NULL;
938 if (!read_only && (created_new || !other_processes))
940 smb_shm_initialize(size);
941 /* Create the hash buckets for the share file entries. */
942 smb_shm_create_hash_table(SHMEM_HASH_SIZE);
944 else if (!smb_shm_validate_header(size) )
946 /* existing file is corrupt, samba admin should remove it by hand */
947 DEBUG(0,("ERROR smb_shm_open : corrupt shared mem file, remove it manually\n"));
948 munmap((caddr_t)smb_shm_header_p, size);
949 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
950 smb_shm_global_unlock();
951 close(smb_shm_fd);
952 return NULL;
955 smb_shm_global_unlock();
956 return &shmops;
960 #else /* HAVE_SHARED_MMAP */
961 int shmem_dummy_procedure(void)
962 {return 0;}
963 #endif /* HAVE_SHARED_MMAP */