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 /* WARNING : offsets are used because mmap() does not guarantee that all processes have the
38 shared memory mapped to the same address */
44 int total_size; /* in bytes */
47 int userdef_off; /* a userdefined offset. can be used to store root of tree or list */
48 struct { /* a cell is a range of bytes of sizeof(struct SmbShmBlockDesc) size */
51 int cells_system; /* number of cells used as allocated block descriptors */
55 #define SMB_SHM_NOT_FREE_OFF (-1)
56 struct SmbShmBlockDesc
58 int next; /* offset of next block in the free list or SMB_SHM_NOT_FREE_OFF when block in use */
59 int size; /* user size in BlockDescSize units */
62 #define EOList_Addr (struct SmbShmBlockDesc *)( 0 )
63 #define EOList_Off (NULL_OFFSET)
65 #define CellSize sizeof(struct SmbShmBlockDesc)
67 /* HeaderSize aligned on 8 byte boundary */
68 #define AlignedHeaderSize ((sizeof(struct SmbShmHeader)+7) & ~7)
70 static int smb_shm_fd = -1;
71 static pstring smb_shm_processreg_name = "";
73 static struct SmbShmHeader *smb_shm_header_p = (struct SmbShmHeader *)0;
74 static int smb_shm_times_locked = 0;
76 static BOOL smb_shm_initialize_called = False;
78 static BOOL smb_shm_global_lock(void)
82 DEBUG(0,("ERROR smb_shm_global_lock : bad smb_shm_fd (%d)\n",smb_shm_fd));
86 smb_shm_times_locked++;
88 if(smb_shm_times_locked > 1)
90 DEBUG(5,("smb_shm_global_lock : locked %d times\n",smb_shm_times_locked));
94 /* Do an exclusive wait lock on the first byte of the file */
95 if (fcntl_lock(smb_shm_fd, F_SETLKW, 0, 1, F_WRLCK) == False)
97 DEBUG(0,("ERROR smb_shm_global_lock : fcntl_lock failed with code %s\n",strerror(errno)));
98 smb_shm_times_locked--;
106 static BOOL smb_shm_global_unlock(void)
110 DEBUG(0,("ERROR smb_shm_global_unlock : bad smb_shm_fd (%d)\n",smb_shm_fd));
114 if(smb_shm_times_locked == 0)
116 DEBUG(0,("ERROR smb_shm_global_unlock : shmem not locked\n",smb_shm_fd));
120 smb_shm_times_locked--;
122 if(smb_shm_times_locked > 0)
124 DEBUG(5,("smb_shm_global_unlock : still locked %d times\n",smb_shm_times_locked));
128 /* Do a wait unlock on the first byte of the file */
129 if (fcntl_lock(smb_shm_fd, F_SETLKW, 0, 1, F_UNLCK) == False)
131 DEBUG(0,("ERROR smb_shm_global_unlock : fcntl_lock failed with code %s\n",strerror(errno)));
132 smb_shm_times_locked++;
141 * Function to create the hash table for the share mode entries. Called
142 * when smb shared memory is global locked.
144 static BOOL smb_shm_create_hash_table( unsigned int size )
148 smb_shm_global_lock();
149 smb_shm_header_p->userdef_off = smb_shm_alloc( size );
151 if(smb_shm_header_p->userdef_off == NULL_OFFSET)
153 DEBUG(0,("smb_shm_create_hash_table: Failed to create hash table of size %d\n",size));
154 smb_shm_global_unlock();
158 /* Clear hash buckets. */
159 memset( smb_shm_offset2addr(smb_shm_header_p->userdef_off), '\0', size);
160 smb_shm_global_unlock();
164 static BOOL smb_shm_register_process(char *processreg_file, pid_t pid, BOOL *other_processes)
166 int smb_shm_processes_fd = -1;
169 int seek_back = -((int)sizeof(other_pid));
173 #ifndef SECURE_SHARE_MODES
174 smb_shm_processes_fd = open(processreg_file, O_RDWR | O_CREAT, 0666);
175 #else /* SECURE_SHARE_MODES */
176 smb_shm_processes_fd = open(processreg_file, O_RDWR | O_CREAT, 0600);
177 #endif /* SECURE_SHARE_MODES */
178 if ( smb_shm_processes_fd < 0 )
180 DEBUG(0,("ERROR smb_shm_register_process : processreg_file open failed with code %s\n",strerror(errno)));
184 *other_processes = False;
186 while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
190 if(process_exists(other_pid))
191 *other_processes = True;
195 DEBUG(5,("smb_shm_register_process : erasing stale record for pid %d (seek_back = %d)\n",
196 other_pid, seek_back));
197 other_pid = (pid_t)0;
198 erased_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
199 write(smb_shm_processes_fd, &other_pid, sizeof(other_pid));
201 free_slot = erased_slot;
206 free_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
210 DEBUG(0,("ERROR smb_shm_register_process : processreg_file read failed with code %s\n",strerror(errno)));
211 close(smb_shm_processes_fd);
216 free_slot = lseek(smb_shm_processes_fd, 0, SEEK_END);
218 DEBUG(5,("smb_shm_register_process : writing record for pid %d at offset %d\n",pid,free_slot));
219 lseek(smb_shm_processes_fd, free_slot, SEEK_SET);
220 if(write(smb_shm_processes_fd, &pid, sizeof(pid)) < 0)
222 DEBUG(0,("ERROR smb_shm_register_process : processreg_file write failed with code %s\n",strerror(errno)));
223 close(smb_shm_processes_fd);
227 close(smb_shm_processes_fd);
232 static BOOL smb_shm_unregister_process(char *processreg_file, pid_t pid)
235 int smb_shm_processes_fd = -1;
238 int seek_back = -((int)sizeof(other_pid));
243 old_umask = umask(0);
244 smb_shm_processes_fd = open(processreg_file, O_RDWR);
246 if ( smb_shm_processes_fd < 0 )
248 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file open failed with code %s\n",strerror(errno)));
252 while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
254 DEBUG(5,("smb_shm_unregister_process : read record for pid %d\n",other_pid));
258 DEBUG(5,("smb_shm_unregister_process : erasing record for pid %d (seek_val = %d)\n",
259 other_pid, seek_back));
260 other_pid = (pid_t)0;
261 erased_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
262 if(write(smb_shm_processes_fd, &other_pid, sizeof(other_pid)) < 0)
264 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file write failed with code %s\n",strerror(errno)));
265 close(smb_shm_processes_fd);
275 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file read failed with code %s\n",strerror(errno)));
276 close(smb_shm_processes_fd);
282 DEBUG(0,("ERROR smb_shm_unregister_process : couldn't find pid %d in file %s\n",pid,processreg_file));
283 close(smb_shm_processes_fd);
288 close(smb_shm_processes_fd);
294 static BOOL smb_shm_validate_header(int size)
296 if( !smb_shm_header_p )
299 DEBUG(0,("ERROR smb_shm_validate_header : shmem not mapped\n"));
303 if(smb_shm_header_p->smb_shm_magic != SMB_SHM_MAGIC)
305 DEBUG(0,("ERROR smb_shm_validate_header : bad magic\n"));
308 if(smb_shm_header_p->smb_shm_version != SMB_SHM_VERSION)
310 DEBUG(0,("ERROR smb_shm_validate_header : bad version %X\n",smb_shm_header_p->smb_shm_version));
314 if(smb_shm_header_p->total_size != size)
316 DEBUG(0,("ERROR smb_shm_validate_header : shmem size mismatch (old = %d, new = %d)\n",smb_shm_header_p->total_size,size));
320 if(!smb_shm_header_p->consistent)
322 DEBUG(0,("ERROR smb_shm_validate_header : shmem not consistent\n"));
328 static BOOL smb_shm_initialize(int size)
330 struct SmbShmBlockDesc * first_free_block_p;
332 DEBUG(5,("smb_shm_initialize : initializing shmem file of size %d\n",size));
334 if( !smb_shm_header_p )
337 DEBUG(0,("ERROR smb_shm_initialize : shmem not mapped\n"));
341 smb_shm_header_p->smb_shm_magic = SMB_SHM_MAGIC;
342 smb_shm_header_p->smb_shm_version = SMB_SHM_VERSION;
343 smb_shm_header_p->total_size = size;
344 smb_shm_header_p->first_free_off = AlignedHeaderSize;
345 smb_shm_header_p->userdef_off = NULL_OFFSET;
347 first_free_block_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
348 first_free_block_p->next = EOList_Off;
349 first_free_block_p->size = ( size - AlignedHeaderSize - CellSize ) / CellSize ;
351 smb_shm_header_p->statistics.cells_free = first_free_block_p->size;
352 smb_shm_header_p->statistics.cells_used = 0;
353 smb_shm_header_p->statistics.cells_system = 1;
355 smb_shm_header_p->consistent = True;
357 smb_shm_initialize_called = True;
362 static void smb_shm_solve_neighbors(struct SmbShmBlockDesc *head_p )
364 struct SmbShmBlockDesc *next_p;
366 /* Check if head_p and head_p->next are neighbors and if so join them */
367 if ( head_p == EOList_Addr ) return ;
368 if ( head_p->next == EOList_Off ) return ;
370 next_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(head_p->next);
371 if ( ( head_p + head_p->size + 1 ) == next_p)
373 head_p->size += next_p->size +1 ; /* adapt size */
374 head_p->next = next_p->next ; /* link out */
376 smb_shm_header_p->statistics.cells_free += 1;
377 smb_shm_header_p->statistics.cells_system -= 1;
383 BOOL smb_shm_open(char *file_name, int size)
386 BOOL created_new = False;
387 BOOL other_processes = True;
390 DEBUG(5,("smb_shm_open : using shmem file %s to be of size %d\n",file_name,size));
392 old_umask = umask(0);
393 #ifndef SECURE_SHARE_MODES
394 smb_shm_fd = open(file_name, O_RDWR | O_CREAT, 0666);
395 #else /* SECURE_SHARE_MODES */
396 smb_shm_fd = open(file_name, O_RDWR | O_CREAT, 0600);
397 #endif /* SECURE_SHARE_MODE */
399 if ( smb_shm_fd < 0 )
401 DEBUG(0,("ERROR smb_shm_open : open failed with code %s\n",strerror(errno)));
405 if (!smb_shm_global_lock())
407 DEBUG(0,("ERROR smb_shm_open : can't do smb_shm_global_lock\n"));
411 if( (filesize = lseek(smb_shm_fd, 0, SEEK_END)) < 0)
413 DEBUG(0,("ERROR smb_shm_open : lseek failed with code %s\n",strerror(errno)));
414 smb_shm_global_unlock();
419 /* return the file offset to 0 to save on later seeks */
420 lseek(smb_shm_fd,0,SEEK_SET);
424 /* we just created a new one */
428 /* to find out if some other process is already mapping the file,
429 we use a registration file containing the processids of the file mapping processes
432 /* construct processreg file name */
433 strcpy(smb_shm_processreg_name, file_name);
434 strcat(smb_shm_processreg_name, ".processes");
436 if (! smb_shm_register_process(smb_shm_processreg_name, getpid(), &other_processes))
438 smb_shm_global_unlock();
443 if (created_new || !other_processes)
445 /* we just created a new one, or are the first opener, lets set it size */
446 if( ftruncate(smb_shm_fd, size) <0)
448 DEBUG(0,("ERROR smb_shm_open : ftruncate failed with code %s\n",strerror(errno)));
449 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
450 smb_shm_global_unlock();
456 lseek(smb_shm_fd,0,SEEK_SET);
461 if (size != filesize )
463 /* the existing file has a different size and we are not the first opener.
464 Since another process is still using it, we will use the file size */
465 DEBUG(0,("WARNING smb_shm_open : filesize (%d) != expected size (%d), using filesize\n",filesize,size));
469 smb_shm_header_p = (struct SmbShmHeader *)mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, smb_shm_fd, 0);
470 /* WARNING, smb_shm_header_p can be different for different processes mapping the same file ! */
471 if (smb_shm_header_p == (struct SmbShmHeader *)(-1))
473 DEBUG(0,("ERROR smb_shm_open : mmap failed with code %s\n",strerror(errno)));
474 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
475 smb_shm_global_unlock();
481 if (created_new || !other_processes)
483 smb_shm_initialize(size);
484 /* Create the hash buckets for the share file entries. */
485 smb_shm_create_hash_table( lp_shmem_hash_size() );
487 else if (!smb_shm_validate_header(size) )
489 /* existing file is corrupt, samba admin should remove it by hand */
490 DEBUG(0,("ERROR smb_shm_open : corrupt shared mem file, remove it manually\n"));
491 munmap((caddr_t)smb_shm_header_p, size);
492 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
493 smb_shm_global_unlock();
498 smb_shm_global_unlock();
504 BOOL smb_shm_close( void )
507 if(smb_shm_initialize_called == False)
510 DEBUG(5,("smb_shm_close\n"));
511 if(smb_shm_times_locked > 0)
512 DEBUG(0,("WARNING smb_shm_close : shmem was still locked %d times\n",smb_shm_times_locked));;
513 if ((smb_shm_header_p != NULL) &&
514 (munmap((caddr_t)smb_shm_header_p, smb_shm_header_p->total_size) < 0))
516 DEBUG(0,("ERROR smb_shm_close : munmap failed with code %s\n",strerror(errno)));
519 smb_shm_global_lock();
520 DEBUG(5,("calling smb_shm_unregister_process(%s, %d)\n", smb_shm_processreg_name, getpid()));
521 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
522 smb_shm_global_unlock();
527 smb_shm_processreg_name[0] = '\0';
529 smb_shm_header_p = (struct SmbShmHeader *)0;
530 smb_shm_times_locked = 0;
535 int smb_shm_alloc(int size)
538 struct SmbShmBlockDesc *scanner_p;
539 struct SmbShmBlockDesc *prev_p;
540 struct SmbShmBlockDesc *new_p;
544 if( !smb_shm_header_p )
547 DEBUG(0,("ERROR smb_shm_alloc : shmem not mapped\n"));
551 smb_shm_global_lock();
553 if( !smb_shm_header_p->consistent)
555 DEBUG(0,("ERROR smb_shm_alloc : shmem not consistent\n"));
556 smb_shm_global_unlock();
561 /* calculate the number of cells */
562 num_cells = (size + CellSize -1) / CellSize;
564 /* set start of scan */
565 prev_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
568 /* scan the free list to find a matching free space */
569 while ( ( scanner_p != EOList_Addr ) && ( scanner_p->size < num_cells ) )
572 scanner_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(scanner_p->next);
575 /* at this point scanner point to a block header or to the end of the list */
576 if ( scanner_p == EOList_Addr )
578 DEBUG(0,("ERROR smb_shm_alloc : alloc of %d bytes failed, no free space found\n",size));
579 smb_shm_global_unlock();
580 return (NULL_OFFSET);
583 /* going to modify shared mem */
584 smb_shm_header_p->consistent = False;
586 /* if we found a good one : scanner == the good one */
587 if ( scanner_p->size <= num_cells + 2 )
589 /* there is no use in making a new one, it will be too small anyway
590 * we will link out scanner
592 if ( prev_p == scanner_p )
594 smb_shm_header_p->first_free_off = scanner_p->next ;
598 prev_p->next = scanner_p->next ;
600 smb_shm_header_p->statistics.cells_free -= scanner_p->size;
601 smb_shm_header_p->statistics.cells_used += scanner_p->size;
606 new_p = scanner_p + 1 + num_cells;
607 new_p->size = scanner_p->size - num_cells - 1;
608 new_p->next = scanner_p->next;
609 scanner_p->size = num_cells;
610 scanner_p->next = smb_shm_addr2offset(new_p);
612 if ( prev_p != scanner_p )
614 prev_p->next = smb_shm_addr2offset(new_p) ;
618 smb_shm_header_p->first_free_off = smb_shm_addr2offset(new_p) ;
620 smb_shm_header_p->statistics.cells_free -= num_cells+1;
621 smb_shm_header_p->statistics.cells_used += num_cells;
622 smb_shm_header_p->statistics.cells_system += 1;
625 result_offset = smb_shm_addr2offset( &(scanner_p[1]) );
626 scanner_p->next = SMB_SHM_NOT_FREE_OFF ;
628 /* end modification of shared mem */
629 smb_shm_header_p->consistent = True;
631 DEBUG(6,("smb_shm_alloc : request for %d bytes, allocated %d bytes at offset %d\n",size,scanner_p->size*CellSize,result_offset ));
633 smb_shm_global_unlock();
634 return ( result_offset );
639 BOOL smb_shm_free(int offset)
641 struct SmbShmBlockDesc *header_p ; /* pointer to header of block to free */
642 struct SmbShmBlockDesc *scanner_p ; /* used to scan the list */
643 struct SmbShmBlockDesc *prev_p ; /* holds previous in the list */
645 if( !smb_shm_header_p )
648 DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
652 smb_shm_global_lock();
654 if( !smb_shm_header_p->consistent)
656 DEBUG(0,("ERROR smb_shm_free : shmem not consistent\n"));
657 smb_shm_global_unlock();
661 header_p = ( (struct SmbShmBlockDesc *)smb_shm_offset2addr(offset) - 1); /* make pointer to header of block */
663 if (header_p->next != SMB_SHM_NOT_FREE_OFF)
665 DEBUG(0,("ERROR smb_shm_free : bad offset (%d)\n",offset));
666 smb_shm_global_unlock();
670 /* find a place in the free_list to put the header in */
672 /* set scanner and previous pointer to start of list */
673 prev_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
676 while ( ( scanner_p != EOList_Addr) && (scanner_p < header_p) ) /* while we didn't scan past its position */
679 scanner_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(scanner_p->next);
682 smb_shm_header_p->consistent = False;
684 DEBUG(6,("smb_shm_free : freeing %d bytes at offset %d\n",header_p->size*CellSize,offset));
686 if ( scanner_p == prev_p )
688 smb_shm_header_p->statistics.cells_free += header_p->size;
689 smb_shm_header_p->statistics.cells_used -= header_p->size;
691 /* we must free it at the beginning of the list */
692 smb_shm_header_p->first_free_off = smb_shm_addr2offset(header_p); /* set the free_list_pointer to this block_header */
694 /* scanner is the one that was first in the list */
695 header_p->next = smb_shm_addr2offset(scanner_p);
696 smb_shm_solve_neighbors( header_p ); /* if neighbors then link them */
698 smb_shm_header_p->consistent = True;
699 smb_shm_global_unlock();
704 smb_shm_header_p->statistics.cells_free += header_p->size;
705 smb_shm_header_p->statistics.cells_used -= header_p->size;
707 prev_p->next = smb_shm_addr2offset(header_p);
708 header_p->next = smb_shm_addr2offset(scanner_p);
709 smb_shm_solve_neighbors(header_p) ;
710 smb_shm_solve_neighbors(prev_p) ;
712 smb_shm_header_p->consistent = True;
713 smb_shm_global_unlock();
718 int smb_shm_get_userdef_off(void)
720 if (!smb_shm_header_p)
723 return smb_shm_header_p->userdef_off;
726 void *smb_shm_offset2addr(int offset)
728 if (offset == NULL_OFFSET )
731 if (!smb_shm_header_p)
734 return (void *)((char *)smb_shm_header_p + offset );
737 int smb_shm_addr2offset(void *addr)
742 if (!smb_shm_header_p)
745 return (int)((char *)addr - (char *)smb_shm_header_p);
748 /*******************************************************************
749 Lock a particular hash bucket entry.
750 ******************************************************************/
752 BOOL smb_shm_lock_hash_entry( unsigned int entry)
754 int start = (smb_shm_header_p->userdef_off + (entry * sizeof(int)));
758 DEBUG(0,("ERROR smb_shm_lock_hash_entry : bad smb_shm_fd (%d)\n",smb_shm_fd));
762 if(entry >= lp_shmem_hash_size())
764 DEBUG(0,("ERROR smb_shm_lock_hash_entry : hash entry size too big (%d)\n", entry));
768 /* Do an exclusive wait lock on the 4 byte region mapping into this entry */
769 if (fcntl_lock(smb_shm_fd, F_SETLKW, start, sizeof(int), F_WRLCK) == False)
771 DEBUG(0,("ERROR smb_shm_lock_hash_entry : fcntl_lock failed with code %s\n",strerror(errno)));
775 DEBUG(9,("smb_shm_lock_hash_entry: locked hash bucket %d\n", entry));
779 /*******************************************************************
780 Unlock a particular hash bucket entry.
781 ******************************************************************/
783 BOOL smb_shm_unlock_hash_entry( unsigned int entry )
785 int start = (smb_shm_header_p->userdef_off + (entry * sizeof(int)));
789 DEBUG(0,("ERROR smb_shm_unlock_hash_entry : bad smb_shm_fd (%d)\n",smb_shm_fd));
793 if(entry >= lp_shmem_hash_size())
795 DEBUG(0,("ERROR smb_shm_unlock_hash_entry : hash entry size too big (%d)\n", entry));
799 /* Do a wait lock on the 4 byte region mapping into this entry */
800 if (fcntl_lock(smb_shm_fd, F_SETLKW, start, sizeof(int), F_UNLCK) == False)
802 DEBUG(0,("ERROR smb_shm_unlock_hash_entry : fcntl_lock failed with code %s\n",strerror(errno)));
806 DEBUG(9,("smb_shm_unlock_hash_entry: unlocked hash bucket %d\n", entry));
810 /*******************************************************************
811 Gather statistics on shared memory usage.
812 ******************************************************************/
814 BOOL smb_shm_get_usage(int *bytes_free,
818 if( !smb_shm_header_p )
821 DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
824 *bytes_free = smb_shm_header_p->statistics.cells_free * CellSize;
825 *bytes_used = smb_shm_header_p->statistics.cells_used * CellSize;
826 *bytes_overhead = smb_shm_header_p->statistics.cells_system * CellSize + AlignedHeaderSize;
831 #else /* FAST_SHARE_MODES */
832 int shmem_dummy_procedure(void)
834 #endif /* FAST_SHARE_MODES */