2 Unix SMB/Netbios implementation.
4 Shared memory functions
5 Copyright (C) Erik Devriendt 1996-1997
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.
26 #ifdef FAST_SHARE_MODES
29 extern int DEBUGLEVEL;
32 #define SMB_SHM_MAGIC 0x53484100
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
41 #define SHM_FILE_MODE 0644
44 /* WARNING : offsets are used because mmap() does not guarantee that all processes have the
45 shared memory mapped to the same address */
51 int total_size; /* in bytes */
54 int userdef_off; /* a userdefined offset. can be used to store root of tree or list */
55 struct { /* a cell is a range of bytes of sizeof(struct SmbShmBlockDesc) size */
58 int cells_system; /* number of cells used as allocated block descriptors */
62 #define SMB_SHM_NOT_FREE_OFF (-1)
63 struct SmbShmBlockDesc
65 int next; /* offset of next block in the free list or SMB_SHM_NOT_FREE_OFF when block in use */
66 int size; /* user size in BlockDescSize units */
69 #define EOList_Addr (struct SmbShmBlockDesc *)( 0 )
70 #define EOList_Off (NULL_OFFSET)
72 #define CellSize sizeof(struct SmbShmBlockDesc)
74 /* HeaderSize aligned on 8 byte boundary */
75 #define AlignedHeaderSize ((sizeof(struct SmbShmHeader)+7) & ~7)
77 static int smb_shm_fd = -1;
78 static pstring smb_shm_processreg_name = "";
80 static struct SmbShmHeader *smb_shm_header_p = (struct SmbShmHeader *)0;
81 static int smb_shm_times_locked = 0;
83 static BOOL smb_shm_initialize_called = False;
87 static BOOL smb_shm_global_lock(void)
91 DEBUG(0,("ERROR smb_shm_global_lock : bad smb_shm_fd (%d)\n",smb_shm_fd));
95 smb_shm_times_locked++;
97 if(smb_shm_times_locked > 1)
99 DEBUG(5,("smb_shm_global_lock : locked %d times\n",smb_shm_times_locked));
106 /* Do an exclusive wait lock on the first byte of the file */
107 if (fcntl_lock(smb_shm_fd, F_SETLKW, 0, 1, F_WRLCK) == False)
109 DEBUG(0,("ERROR smb_shm_global_lock : fcntl_lock failed with code %s\n",strerror(errno)));
110 smb_shm_times_locked--;
118 static BOOL smb_shm_global_unlock(void)
122 DEBUG(0,("ERROR smb_shm_global_unlock : bad smb_shm_fd (%d)\n",smb_shm_fd));
126 if(smb_shm_times_locked == 0)
128 DEBUG(0,("ERROR smb_shm_global_unlock : shmem not locked\n",smb_shm_fd));
132 smb_shm_times_locked--;
134 if(smb_shm_times_locked > 0)
136 DEBUG(5,("smb_shm_global_unlock : still locked %d times\n",smb_shm_times_locked));
143 /* Do a wait unlock on the first byte of the file */
144 if (fcntl_lock(smb_shm_fd, F_SETLKW, 0, 1, F_UNLCK) == False)
146 DEBUG(0,("ERROR smb_shm_global_unlock : fcntl_lock failed with code %s\n",strerror(errno)));
147 smb_shm_times_locked++;
156 static void *smb_shm_offset2addr(int offset)
158 if (offset == NULL_OFFSET )
161 if (!smb_shm_header_p)
164 return (void *)((char *)smb_shm_header_p + offset );
167 static int smb_shm_addr2offset(void *addr)
172 if (!smb_shm_header_p)
175 return (int)((char *)addr - (char *)smb_shm_header_p);
180 static int smb_shm_alloc(int size)
183 struct SmbShmBlockDesc *scanner_p;
184 struct SmbShmBlockDesc *prev_p;
185 struct SmbShmBlockDesc *new_p;
189 if( !smb_shm_header_p )
192 DEBUG(0,("ERROR smb_shm_alloc : shmem not mapped\n"));
196 smb_shm_global_lock();
198 if( !smb_shm_header_p->consistent)
200 DEBUG(0,("ERROR smb_shm_alloc : shmem not consistent\n"));
201 smb_shm_global_unlock();
206 /* calculate the number of cells */
207 num_cells = (size + CellSize -1) / CellSize;
209 /* set start of scan */
210 prev_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
213 /* scan the free list to find a matching free space */
214 while ( ( scanner_p != EOList_Addr ) && ( scanner_p->size < num_cells ) )
217 scanner_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(scanner_p->next);
220 /* at this point scanner point to a block header or to the end of the list */
221 if ( scanner_p == EOList_Addr )
223 DEBUG(0,("ERROR smb_shm_alloc : alloc of %d bytes failed, no free space found\n",size));
224 smb_shm_global_unlock();
225 return (NULL_OFFSET);
228 /* going to modify shared mem */
229 smb_shm_header_p->consistent = False;
231 /* if we found a good one : scanner == the good one */
232 if ( scanner_p->size <= num_cells + 2 )
234 /* there is no use in making a new one, it will be too small anyway
235 * we will link out scanner
237 if ( prev_p == scanner_p )
239 smb_shm_header_p->first_free_off = scanner_p->next ;
243 prev_p->next = scanner_p->next ;
245 smb_shm_header_p->statistics.cells_free -= scanner_p->size;
246 smb_shm_header_p->statistics.cells_used += scanner_p->size;
251 new_p = scanner_p + 1 + num_cells;
252 new_p->size = scanner_p->size - num_cells - 1;
253 new_p->next = scanner_p->next;
254 scanner_p->size = num_cells;
255 scanner_p->next = smb_shm_addr2offset(new_p);
257 if ( prev_p != scanner_p )
259 prev_p->next = smb_shm_addr2offset(new_p) ;
263 smb_shm_header_p->first_free_off = smb_shm_addr2offset(new_p) ;
265 smb_shm_header_p->statistics.cells_free -= num_cells+1;
266 smb_shm_header_p->statistics.cells_used += num_cells;
267 smb_shm_header_p->statistics.cells_system += 1;
270 result_offset = smb_shm_addr2offset( &(scanner_p[1]) );
271 scanner_p->next = SMB_SHM_NOT_FREE_OFF ;
273 /* end modification of shared mem */
274 smb_shm_header_p->consistent = True;
276 DEBUG(6,("smb_shm_alloc : request for %d bytes, allocated %d bytes at offset %d\n",size,scanner_p->size*CellSize,result_offset ));
278 smb_shm_global_unlock();
279 return ( result_offset );
286 * Function to create the hash table for the share mode entries. Called
287 * when smb shared memory is global locked.
289 static BOOL smb_shm_create_hash_table( unsigned int size )
293 smb_shm_global_lock();
294 smb_shm_header_p->userdef_off = smb_shm_alloc( size );
296 if(smb_shm_header_p->userdef_off == NULL_OFFSET)
298 DEBUG(0,("smb_shm_create_hash_table: Failed to create hash table of size %d\n",size));
299 smb_shm_global_unlock();
303 /* Clear hash buckets. */
304 memset( smb_shm_offset2addr(smb_shm_header_p->userdef_off), '\0', size);
305 smb_shm_global_unlock();
309 static BOOL smb_shm_register_process(char *processreg_file, pid_t pid, BOOL *other_processes)
311 int smb_shm_processes_fd = -1;
314 int seek_back = -((int)sizeof(other_pid));
318 smb_shm_processes_fd = open(processreg_file,
319 read_only?O_RDONLY:(O_RDWR|O_CREAT),
322 if ( smb_shm_processes_fd < 0 )
324 DEBUG(0,("ERROR smb_shm_register_process : processreg_file open failed with code %s\n",strerror(errno)));
328 *other_processes = False;
330 while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
334 if(process_exists(other_pid))
335 *other_processes = True;
339 DEBUG(5,("smb_shm_register_process : erasing stale record for pid %d (seek_back = %d)\n",
340 other_pid, seek_back));
341 other_pid = (pid_t)0;
342 erased_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
343 write(smb_shm_processes_fd, &other_pid, sizeof(other_pid));
345 free_slot = erased_slot;
350 free_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
354 DEBUG(0,("ERROR smb_shm_register_process : processreg_file read failed with code %s\n",strerror(errno)));
355 close(smb_shm_processes_fd);
360 free_slot = lseek(smb_shm_processes_fd, 0, SEEK_END);
362 DEBUG(5,("smb_shm_register_process : writing record for pid %d at offset %d\n",pid,free_slot));
363 lseek(smb_shm_processes_fd, free_slot, SEEK_SET);
364 if(write(smb_shm_processes_fd, &pid, sizeof(pid)) < 0)
366 DEBUG(0,("ERROR smb_shm_register_process : processreg_file write failed with code %s\n",strerror(errno)));
367 close(smb_shm_processes_fd);
371 close(smb_shm_processes_fd);
376 static BOOL smb_shm_unregister_process(char *processreg_file, pid_t pid)
378 int smb_shm_processes_fd = -1;
381 int seek_back = -((int)sizeof(other_pid));
386 smb_shm_processes_fd = open(processreg_file, O_RDWR);
387 if ( smb_shm_processes_fd < 0 )
389 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file open failed with code %s\n",strerror(errno)));
393 while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
395 DEBUG(5,("smb_shm_unregister_process : read record for pid %d\n",other_pid));
399 DEBUG(5,("smb_shm_unregister_process : erasing record for pid %d (seek_val = %d)\n",
400 other_pid, seek_back));
401 other_pid = (pid_t)0;
402 erased_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
403 if(write(smb_shm_processes_fd, &other_pid, sizeof(other_pid)) < 0)
405 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file write failed with code %s\n",strerror(errno)));
406 close(smb_shm_processes_fd);
416 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file read failed with code %s\n",strerror(errno)));
417 close(smb_shm_processes_fd);
423 DEBUG(0,("ERROR smb_shm_unregister_process : couldn't find pid %d in file %s\n",pid,processreg_file));
424 close(smb_shm_processes_fd);
429 close(smb_shm_processes_fd);
435 static BOOL smb_shm_validate_header(int size)
437 if( !smb_shm_header_p )
440 DEBUG(0,("ERROR smb_shm_validate_header : shmem not mapped\n"));
444 if(smb_shm_header_p->smb_shm_magic != SMB_SHM_MAGIC)
446 DEBUG(0,("ERROR smb_shm_validate_header : bad magic\n"));
449 if(smb_shm_header_p->smb_shm_version != SMB_SHM_VERSION)
451 DEBUG(0,("ERROR smb_shm_validate_header : bad version %X\n",smb_shm_header_p->smb_shm_version));
455 if(smb_shm_header_p->total_size != size)
457 DEBUG(0,("ERROR smb_shm_validate_header : shmem size mismatch (old = %d, new = %d)\n",smb_shm_header_p->total_size,size));
461 if(!smb_shm_header_p->consistent)
463 DEBUG(0,("ERROR smb_shm_validate_header : shmem not consistent\n"));
469 static BOOL smb_shm_initialize(int size)
471 struct SmbShmBlockDesc * first_free_block_p;
473 DEBUG(5,("smb_shm_initialize : initializing shmem file of size %d\n",size));
475 if( !smb_shm_header_p )
478 DEBUG(0,("ERROR smb_shm_initialize : shmem not mapped\n"));
482 smb_shm_header_p->smb_shm_magic = SMB_SHM_MAGIC;
483 smb_shm_header_p->smb_shm_version = SMB_SHM_VERSION;
484 smb_shm_header_p->total_size = size;
485 smb_shm_header_p->first_free_off = AlignedHeaderSize;
486 smb_shm_header_p->userdef_off = NULL_OFFSET;
488 first_free_block_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
489 first_free_block_p->next = EOList_Off;
490 first_free_block_p->size = ( size - AlignedHeaderSize - CellSize ) / CellSize ;
492 smb_shm_header_p->statistics.cells_free = first_free_block_p->size;
493 smb_shm_header_p->statistics.cells_used = 0;
494 smb_shm_header_p->statistics.cells_system = 1;
496 smb_shm_header_p->consistent = True;
498 smb_shm_initialize_called = True;
503 static void smb_shm_solve_neighbors(struct SmbShmBlockDesc *head_p )
505 struct SmbShmBlockDesc *next_p;
507 /* Check if head_p and head_p->next are neighbors and if so join them */
508 if ( head_p == EOList_Addr ) return ;
509 if ( head_p->next == EOList_Off ) return ;
511 next_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(head_p->next);
512 if ( ( head_p + head_p->size + 1 ) == next_p)
514 head_p->size += next_p->size +1 ; /* adapt size */
515 head_p->next = next_p->next ; /* link out */
517 smb_shm_header_p->statistics.cells_free += 1;
518 smb_shm_header_p->statistics.cells_system -= 1;
524 static BOOL smb_shm_close( void )
527 if(smb_shm_initialize_called == False)
530 DEBUG(5,("smb_shm_close\n"));
531 if(smb_shm_times_locked > 0)
532 DEBUG(0,("WARNING smb_shm_close : shmem was still locked %d times\n",smb_shm_times_locked));;
533 if ((smb_shm_header_p != NULL) &&
534 (munmap((caddr_t)smb_shm_header_p, smb_shm_header_p->total_size) < 0))
536 DEBUG(0,("ERROR smb_shm_close : munmap failed with code %s\n",strerror(errno)));
539 smb_shm_global_lock();
540 DEBUG(5,("calling smb_shm_unregister_process(%s, %d)\n", smb_shm_processreg_name, getpid()));
541 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
542 smb_shm_global_unlock();
547 smb_shm_processreg_name[0] = '\0';
549 smb_shm_header_p = (struct SmbShmHeader *)0;
550 smb_shm_times_locked = 0;
556 static BOOL smb_shm_free(int offset)
558 struct SmbShmBlockDesc *header_p ; /* pointer to header of block to free */
559 struct SmbShmBlockDesc *scanner_p ; /* used to scan the list */
560 struct SmbShmBlockDesc *prev_p ; /* holds previous in the list */
562 if( !smb_shm_header_p )
565 DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
569 smb_shm_global_lock();
571 if( !smb_shm_header_p->consistent)
573 DEBUG(0,("ERROR smb_shm_free : shmem not consistent\n"));
574 smb_shm_global_unlock();
578 header_p = ( (struct SmbShmBlockDesc *)smb_shm_offset2addr(offset) - 1); /* make pointer to header of block */
580 if (header_p->next != SMB_SHM_NOT_FREE_OFF)
582 DEBUG(0,("ERROR smb_shm_free : bad offset (%d)\n",offset));
583 smb_shm_global_unlock();
587 /* find a place in the free_list to put the header in */
589 /* set scanner and previous pointer to start of list */
590 prev_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
593 while ( ( scanner_p != EOList_Addr) && (scanner_p < header_p) ) /* while we didn't scan past its position */
596 scanner_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(scanner_p->next);
599 smb_shm_header_p->consistent = False;
601 DEBUG(6,("smb_shm_free : freeing %d bytes at offset %d\n",header_p->size*CellSize,offset));
603 if ( scanner_p == prev_p )
605 smb_shm_header_p->statistics.cells_free += header_p->size;
606 smb_shm_header_p->statistics.cells_used -= header_p->size;
608 /* we must free it at the beginning of the list */
609 smb_shm_header_p->first_free_off = smb_shm_addr2offset(header_p); /* set the free_list_pointer to this block_header */
611 /* scanner is the one that was first in the list */
612 header_p->next = smb_shm_addr2offset(scanner_p);
613 smb_shm_solve_neighbors( header_p ); /* if neighbors then link them */
615 smb_shm_header_p->consistent = True;
616 smb_shm_global_unlock();
621 smb_shm_header_p->statistics.cells_free += header_p->size;
622 smb_shm_header_p->statistics.cells_used -= header_p->size;
624 prev_p->next = smb_shm_addr2offset(header_p);
625 header_p->next = smb_shm_addr2offset(scanner_p);
626 smb_shm_solve_neighbors(header_p) ;
627 smb_shm_solve_neighbors(prev_p) ;
629 smb_shm_header_p->consistent = True;
630 smb_shm_global_unlock();
635 static int smb_shm_get_userdef_off(void)
637 if (!smb_shm_header_p)
640 return smb_shm_header_p->userdef_off;
643 /*******************************************************************
644 Lock a particular hash bucket entry.
645 ******************************************************************/
646 static BOOL smb_shm_lock_hash_entry( unsigned int entry)
648 int start = (smb_shm_header_p->userdef_off + (entry * sizeof(int)));
652 DEBUG(0,("ERROR smb_shm_lock_hash_entry : bad smb_shm_fd (%d)\n",smb_shm_fd));
656 if(entry >= lp_shmem_hash_size())
658 DEBUG(0,("ERROR smb_shm_lock_hash_entry : hash entry size too big (%d)\n", entry));
662 /* Do an exclusive wait lock on the 4 byte region mapping into this entry */
663 if (fcntl_lock(smb_shm_fd, F_SETLKW, start, sizeof(int), F_WRLCK) == False)
665 DEBUG(0,("ERROR smb_shm_lock_hash_entry : fcntl_lock failed with code %s\n",strerror(errno)));
669 DEBUG(9,("smb_shm_lock_hash_entry: locked hash bucket %d\n", entry));
673 /*******************************************************************
674 Unlock a particular hash bucket entry.
675 ******************************************************************/
676 static BOOL smb_shm_unlock_hash_entry( unsigned int entry )
678 int start = (smb_shm_header_p->userdef_off + (entry * sizeof(int)));
682 DEBUG(0,("ERROR smb_shm_unlock_hash_entry : bad smb_shm_fd (%d)\n",smb_shm_fd));
686 if(entry >= lp_shmem_hash_size())
688 DEBUG(0,("ERROR smb_shm_unlock_hash_entry : hash entry size too big (%d)\n", entry));
692 /* Do a wait lock on the 4 byte region mapping into this entry */
693 if (fcntl_lock(smb_shm_fd, F_SETLKW, start, sizeof(int), F_UNLCK) == False)
695 DEBUG(0,("ERROR smb_shm_unlock_hash_entry : fcntl_lock failed with code %s\n",strerror(errno)));
699 DEBUG(9,("smb_shm_unlock_hash_entry: unlocked hash bucket %d\n", entry));
703 /*******************************************************************
704 Gather statistics on shared memory usage.
705 ******************************************************************/
706 static BOOL smb_shm_get_usage(int *bytes_free,
710 if( !smb_shm_header_p )
713 DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
716 *bytes_free = smb_shm_header_p->statistics.cells_free * CellSize;
717 *bytes_used = smb_shm_header_p->statistics.cells_used * CellSize;
718 *bytes_overhead = smb_shm_header_p->statistics.cells_system * CellSize + AlignedHeaderSize;
724 static struct shmem_ops shmops = {
728 smb_shm_get_userdef_off,
731 smb_shm_lock_hash_entry,
732 smb_shm_unlock_hash_entry,
736 /*******************************************************************
737 open the shared memory
738 ******************************************************************/
739 struct shmem_ops *smb_shm_open(char *file_name, int size, int ronly)
742 BOOL created_new = False;
743 BOOL other_processes = True;
747 DEBUG(5,("smb_shm_open : using shmem file %s to be of size %d\n",file_name,size));
749 smb_shm_fd = open(file_name, read_only?O_RDONLY:(O_RDWR|O_CREAT),
752 if ( smb_shm_fd < 0 )
754 DEBUG(0,("ERROR smb_shm_open : open failed with code %s\n",strerror(errno)));
758 if (!smb_shm_global_lock())
760 DEBUG(0,("ERROR smb_shm_open : can't do smb_shm_global_lock\n"));
764 if( (filesize = lseek(smb_shm_fd, 0, SEEK_END)) < 0)
766 DEBUG(0,("ERROR smb_shm_open : lseek failed with code %s\n",strerror(errno)));
767 smb_shm_global_unlock();
772 /* return the file offset to 0 to save on later seeks */
773 lseek(smb_shm_fd,0,SEEK_SET);
777 /* we just created a new one */
781 /* to find out if some other process is already mapping the file,
782 we use a registration file containing the processids of the file mapping processes
785 /* construct processreg file name */
786 strcpy(smb_shm_processreg_name, file_name);
787 strcat(smb_shm_processreg_name, ".processes");
790 !smb_shm_register_process(smb_shm_processreg_name, getpid(), &other_processes))
792 smb_shm_global_unlock();
797 if (!read_only && (created_new || !other_processes))
799 /* we just created a new one, or are the first opener, lets set it size */
800 if( ftruncate(smb_shm_fd, size) <0)
802 DEBUG(0,("ERROR smb_shm_open : ftruncate failed with code %s\n",strerror(errno)));
803 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
804 smb_shm_global_unlock();
810 lseek(smb_shm_fd,0,SEEK_SET);
815 if (size != filesize )
817 /* the existing file has a different size and we are not the first opener.
818 Since another process is still using it, we will use the file size */
819 DEBUG(0,("WARNING smb_shm_open : filesize (%d) != expected size (%d), using filesize\n",filesize,size));
823 smb_shm_header_p = (struct SmbShmHeader *)mmap(NULL, size,
825 (PROT_READ | PROT_WRITE),
826 MAP_FILE | MAP_SHARED,
828 /* WARNING, smb_shm_header_p can be different for different processes mapping the same file ! */
829 if (smb_shm_header_p == (struct SmbShmHeader *)(-1))
831 DEBUG(0,("ERROR smb_shm_open : mmap failed with code %s\n",strerror(errno)));
832 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
833 smb_shm_global_unlock();
839 if (!read_only && (created_new || !other_processes))
841 smb_shm_initialize(size);
842 /* Create the hash buckets for the share file entries. */
843 smb_shm_create_hash_table( lp_shmem_hash_size() );
845 else if (!smb_shm_validate_header(size) )
847 /* existing file is corrupt, samba admin should remove it by hand */
848 DEBUG(0,("ERROR smb_shm_open : corrupt shared mem file, remove it manually\n"));
849 munmap((caddr_t)smb_shm_header_p, size);
850 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
851 smb_shm_global_unlock();
856 smb_shm_global_unlock();
861 #else /* FAST_SHARE_MODES */
862 int shmem_dummy_procedure(void)
864 #endif /* FAST_SHARE_MODES */