2 Unix SMB/Netbios implementation.
4 Shared memory functions
5 Copyright (C) Erik Devriendt 1996
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 #ifdef FAST_SHARE_MODES
28 extern int DEBUGLEVEL;
31 #define SHM_MAGIC 0x53484100
36 /* WARNING : offsets are used because mmap() does not guarantee that all processes have the
37 shared memory mapped to the same address */
43 int total_size; /* in bytes */
45 shm_offset_t first_free_off;
46 shm_offset_t userdef_off; /* a userdefined offset. can be used to store root of tree or list */
47 struct { /* a cell is a range of bytes of sizeof(struct ShmBlockDesc) size */
50 int cells_system; /* number of cells used as allocated block descriptors */
54 #define SHM_NOT_FREE_OFF (-1)
57 shm_offset_t next; /* offset of next block in the free list or SHM_NOT_FREE_OFF when block in use */
58 int size; /* user size in BlockDescSize units */
61 #define EOList_Addr (struct ShmBlockDesc *)( 0 )
62 #define EOList_Off (NULL_OFFSET)
64 #define CellSize sizeof(struct ShmBlockDesc)
66 /* HeaderSize aligned on 8 byte boundary */
67 #define AlignedHeaderSize ((sizeof(struct ShmHeader)+7) & ~7)
69 static int shm_fd = -1;
70 static pstring shm_processreg_name = "";
72 static struct ShmHeader *shm_header_p = (struct ShmHeader *)0;
73 static int shm_times_locked = 0;
75 static BOOL shm_register_process(char *processreg_file, pid_t pid, BOOL *other_processes)
78 int shm_processes_fd = -1;
86 shm_processes_fd = open(processreg_file, O_RDWR | O_CREAT, 0666);
88 if ( shm_processes_fd < 0 )
90 DEBUG(0,("ERROR shm_register_process : processreg_file open failed with code %d\n",errno));
94 *other_processes = False;
96 while ((nb_read = read(shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
100 if(process_exists(other_pid))
101 *other_processes = True;
105 DEBUG(2,("shm_register_process : erasing stale record for pid %d\n",other_pid));
106 other_pid = (pid_t)0;
107 erased_slot = lseek(shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
108 write(shm_processes_fd, &other_pid, sizeof(other_pid));
110 free_slot = erased_slot;
115 free_slot = lseek(shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
119 DEBUG(0,("ERROR shm_register_process : processreg_file read failed with code %d\n",errno));
120 close(shm_processes_fd);
125 free_slot = lseek(shm_processes_fd, 0, SEEK_END);
127 DEBUG(2,("shm_register_process : writing record for pid %d at offset %d\n",pid,free_slot));
128 lseek(shm_processes_fd, free_slot, SEEK_SET);
129 if(write(shm_processes_fd, &pid, sizeof(pid)) < 0)
131 DEBUG(0,("ERROR shm_register_process : processreg_file write failed with code %d\n",errno));
132 close(shm_processes_fd);
136 close(shm_processes_fd);
141 static BOOL shm_unregister_process(char *processreg_file, pid_t pid)
144 int shm_processes_fd = -1;
151 old_umask = umask(0);
152 shm_processes_fd = open(processreg_file, O_RDWR);
154 if ( shm_processes_fd < 0 )
156 DEBUG(0,("ERROR shm_unregister_process : processreg_file open failed with code %d\n",errno));
160 while ((nb_read = read(shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
165 DEBUG(2,("shm_unregister_process : erasing record for pid %d\n",other_pid));
166 other_pid = (pid_t)0;
167 erased_slot = lseek(shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
168 if(write(shm_processes_fd, &other_pid, sizeof(other_pid)) < 0)
170 DEBUG(0,("ERROR shm_unregister_process : processreg_file write failed with code %d\n",errno));
171 close(shm_processes_fd);
181 DEBUG(0,("ERROR shm_unregister_process : processreg_file read failed with code %d\n",errno));
182 close(shm_processes_fd);
188 DEBUG(0,("ERROR shm_unregister_process : couldn't find pid %d in file %s\n",pid,processreg_file));
189 close(shm_processes_fd);
194 close(shm_processes_fd);
200 static BOOL shm_validate_header(int size)
205 DEBUG(0,("ERROR shm_validate_header : shmem not mapped\n"));
209 if(shm_header_p->shm_magic != SHM_MAGIC)
211 DEBUG(0,("ERROR shm_validate_header : bad magic\n"));
214 if(shm_header_p->shm_version != SHM_VERSION)
216 DEBUG(0,("ERROR shm_validate_header : bad version %X\n",shm_header_p->shm_version));
220 if(shm_header_p->total_size != size)
222 DEBUG(0,("ERROR shm_validate_header : shmem size mismatch (old = %d, new = %d)\n",shm_header_p->total_size,size));
226 if(!shm_header_p->consistent)
228 DEBUG(0,("ERROR shm_validate_header : shmem not consistent\n"));
234 static BOOL shm_initialize(int size)
236 struct ShmBlockDesc * first_free_block_p;
238 DEBUG(2,("shm_initialize : initializing shmem file of size %d\n",size));
243 DEBUG(0,("ERROR shm_initialize : shmem not mapped\n"));
247 shm_header_p->shm_magic = SHM_MAGIC;
248 shm_header_p->shm_version = SHM_VERSION;
249 shm_header_p->total_size = size;
250 shm_header_p->first_free_off = AlignedHeaderSize;
251 shm_header_p->userdef_off = NULL_OFFSET;
253 first_free_block_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
254 first_free_block_p->next = EOList_Off;
255 first_free_block_p->size = ( size - AlignedHeaderSize - CellSize ) / CellSize ;
257 shm_header_p->statistics.cells_free = first_free_block_p->size;
258 shm_header_p->statistics.cells_used = 0;
259 shm_header_p->statistics.cells_system = 1;
261 shm_header_p->consistent = True;
266 static void shm_solve_neighbors(struct ShmBlockDesc *head_p )
268 struct ShmBlockDesc *next_p;
270 /* Check if head_p and head_p->next are neighbors and if so join them */
271 if ( head_p == EOList_Addr ) return ;
272 if ( head_p->next == EOList_Off ) return ;
274 next_p = (struct ShmBlockDesc *)shm_offset2addr(head_p->next);
275 if ( ( head_p + head_p->size + 1 ) == next_p)
277 head_p->size += next_p->size +1 ; /* adapt size */
278 head_p->next = next_p->next ; /* link out */
280 shm_header_p->statistics.cells_free += 1;
281 shm_header_p->statistics.cells_system -= 1;
287 BOOL shm_open( char *file_name, int size)
290 BOOL created_new = False;
291 BOOL other_processes = True;
294 DEBUG(2,("shm_open : using shmem file %s to be of size %d\n",file_name,size));
296 old_umask = umask(0);
297 shm_fd = open(file_name, O_RDWR | O_CREAT, 0666);
301 DEBUG(0,("ERROR shm_open : open failed with code %d\n",errno));
307 DEBUG(0,("ERROR shm_open : can't do shm_lock\n"));
311 if( (filesize = lseek(shm_fd, 0, SEEK_END)) < 0)
313 DEBUG(0,("ERROR shm_open : lseek failed with code %d\n",errno));
319 /* return the file offset to 0 to save on later seeks */
320 lseek(shm_fd,0,SEEK_SET);
324 /* we just created a new one */
328 /* to find out if some other process is already mapping the file,
329 we use a registration file containing the processids of the file mapping processes
332 /* construct processreg file name */
333 strcpy(shm_processreg_name, file_name);
334 strcat(shm_processreg_name, ".processes");
336 if (! shm_register_process(shm_processreg_name, getpid(), &other_processes))
343 if (created_new || !other_processes)
345 /* we just created a new one, or are the first opener, lets set it size */
346 if( ftruncate(shm_fd, size) <0)
348 DEBUG(0,("ERROR shm_open : ftruncate failed with code %d\n",errno));
349 shm_unregister_process(shm_processreg_name, getpid());
356 lseek(shm_fd,0,SEEK_SET);
361 if (size != filesize )
363 /* the existing file has a different size and we are not the first opener.
364 Since another process is still using it, we will use the file size */
365 DEBUG(0,("WARNING shm_open : filesize (%d) != expected size (%d), using filesize\n",filesize,size));
369 shm_header_p = (struct ShmHeader *)mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, shm_fd, 0);
370 /* WARNING, shm_header_p can be different for different processes mapping the same file ! */
371 if (shm_header_p == (struct ShmHeader *)(-1))
373 DEBUG(0,("ERROR shm_open : mmap failed with code %d\n",errno));
374 shm_unregister_process(shm_processreg_name, getpid());
381 if (created_new || !other_processes)
383 shm_initialize(size);
385 else if (!shm_validate_header(size) )
387 /* existing file is corrupt, samba admin should remove it by hand */
388 DEBUG(0,("ERROR shm_open : corrupt shared mem file, remove it manually\n"));
389 munmap((caddr_t)shm_header_p, size);
390 shm_unregister_process(shm_processreg_name, getpid());
402 BOOL shm_close( void )
405 DEBUG(2,("shm_close\n"));
406 if(shm_times_locked > 0)
407 DEBUG(0,("WARNING shm_close : shmem was still locked %d times\n",shm_times_locked));;
408 if ( munmap((caddr_t)shm_header_p, shm_header_p->total_size) < 0)
410 DEBUG(0,("ERROR shm_close : munmap failed with code %d\n",errno));
414 shm_unregister_process(shm_processreg_name, getpid());
420 shm_processreg_name[0] = '\0';
422 shm_header_p = (struct ShmHeader *)0;
423 shm_times_locked = 0;
428 shm_offset_t shm_alloc(int size)
431 struct ShmBlockDesc *scanner_p;
432 struct ShmBlockDesc *prev_p;
433 struct ShmBlockDesc *new_p;
434 shm_offset_t result_offset;
440 DEBUG(0,("ERROR shm_alloc : shmem not mapped\n"));
444 if( !shm_header_p->consistent)
446 DEBUG(0,("ERROR shm_alloc : shmem not consistent\n"));
451 /* calculate the number of cells */
452 num_cells = (size + CellSize -1) / CellSize;
454 /* set start of scan */
455 prev_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
458 /* scan the free list to find a matching free space */
459 while ( ( scanner_p != EOList_Addr ) && ( scanner_p->size < num_cells ) )
462 scanner_p = (struct ShmBlockDesc *)shm_offset2addr(scanner_p->next);
465 /* at this point scanner point to a block header or to the end of the list */
466 if ( scanner_p == EOList_Addr )
468 DEBUG(0,("ERROR shm_alloc : alloc of %d bytes failed, no free space found\n",size));
469 return (NULL_OFFSET);
472 /* going to modify shared mem */
473 shm_header_p->consistent = False;
475 /* if we found a good one : scanner == the good one */
476 if ( scanner_p->size <= num_cells + 2 )
478 /* there is no use in making a new one, it will be too small anyway
479 * we will link out scanner
481 if ( prev_p == scanner_p )
483 shm_header_p->first_free_off = scanner_p->next ;
487 prev_p->next = scanner_p->next ;
489 shm_header_p->statistics.cells_free -= scanner_p->size;
490 shm_header_p->statistics.cells_used += scanner_p->size;
495 new_p = scanner_p + 1 + num_cells;
496 new_p->size = scanner_p->size - num_cells - 1;
497 new_p->next = scanner_p->next;
498 scanner_p->size = num_cells;
499 scanner_p->next = shm_addr2offset(new_p);
501 if ( prev_p != scanner_p )
503 prev_p->next = shm_addr2offset(new_p) ;
507 shm_header_p->first_free_off = shm_addr2offset(new_p) ;
509 shm_header_p->statistics.cells_free -= num_cells+1;
510 shm_header_p->statistics.cells_used += num_cells;
511 shm_header_p->statistics.cells_system += 1;
514 result_offset = shm_addr2offset( &(scanner_p[1]) );
515 scanner_p->next = SHM_NOT_FREE_OFF ;
517 /* end modification of shared mem */
518 shm_header_p->consistent = True;
520 DEBUG(2,("shm_alloc : request for %d bytes, allocated %d bytes at offset %d\n",size,scanner_p->size*CellSize,result_offset ));
522 return ( result_offset );
527 BOOL shm_free(shm_offset_t offset)
529 struct ShmBlockDesc *header_p ; /* pointer to header of block to free */
530 struct ShmBlockDesc *scanner_p ; /* used to scan the list */
531 struct ShmBlockDesc *prev_p ; /* holds previous in the list */
536 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
540 if( !shm_header_p->consistent)
542 DEBUG(0,("ERROR shm_free : shmem not consistent\n"));
546 header_p = ( (struct ShmBlockDesc *)shm_offset2addr(offset) - 1); /* make pointer to header of block */
548 if (header_p->next != SHM_NOT_FREE_OFF)
550 DEBUG(0,("ERROR shm_free : bad offset (%d)\n",offset));
554 /* find a place in the free_list to put the header in */
556 /* set scanner and previous pointer to start of list */
557 prev_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
560 while ( ( scanner_p != EOList_Addr) && (scanner_p < header_p) ) /* while we didn't scan past its position */
563 scanner_p = (struct ShmBlockDesc *)shm_offset2addr(scanner_p->next);
566 shm_header_p->consistent = False;
568 DEBUG(2,("shm_free : freeing %d bytes at offset %d\n",header_p->size*CellSize,offset));
570 if ( scanner_p == prev_p )
572 shm_header_p->statistics.cells_free += header_p->size;
573 shm_header_p->statistics.cells_used -= header_p->size;
575 /* we must free it at the beginning of the list */
576 shm_header_p->first_free_off = shm_addr2offset(header_p); /* set the free_list_pointer to this block_header */
578 /* scanner is the one that was first in the list */
579 header_p->next = shm_addr2offset(scanner_p);
580 shm_solve_neighbors( header_p ); /* if neighbors then link them */
582 shm_header_p->consistent = True;
587 shm_header_p->statistics.cells_free += header_p->size;
588 shm_header_p->statistics.cells_used -= header_p->size;
590 prev_p->next = shm_addr2offset(header_p);
591 header_p->next = shm_addr2offset(scanner_p);
592 shm_solve_neighbors(header_p) ;
593 shm_solve_neighbors(prev_p) ;
595 shm_header_p->consistent = True;
600 shm_offset_t shm_get_userdef_off(void)
605 return shm_header_p->userdef_off;
608 BOOL shm_set_userdef_off(shm_offset_t userdef_off)
613 shm_header_p->userdef_off = userdef_off;
617 void * shm_offset2addr(shm_offset_t offset)
619 if (offset == NULL_OFFSET )
625 return (void *)((char *)shm_header_p + offset );
628 shm_offset_t shm_addr2offset(void *addr)
636 return (shm_offset_t)((char *)addr - (char *)shm_header_p);
643 DEBUG(0,("ERROR shm_lock : bad shm_fd (%d)\n",shm_fd));
649 if(shm_times_locked > 1)
651 DEBUG(2,("shm_lock : locked %d times\n",shm_times_locked));
655 if (lockf(shm_fd, F_LOCK, 0) < 0)
657 DEBUG(0,("ERROR shm_lock : lockf failed with code %d\n",errno));
668 BOOL shm_unlock(void)
672 DEBUG(0,("ERROR shm_unlock : bad shm_fd (%d)\n",shm_fd));
676 if(shm_times_locked == 0)
678 DEBUG(0,("ERROR shm_unlock : shmem not locked\n",shm_fd));
684 if(shm_times_locked > 0)
686 DEBUG(2,("shm_unlock : still locked %d times\n",shm_times_locked));
690 if (lockf(shm_fd, F_ULOCK, 0) < 0)
692 DEBUG(0,("ERROR shm_unlock : lockf failed with code %d\n",errno));
702 BOOL shm_get_usage(int *bytes_free,
709 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
712 *bytes_free = shm_header_p->statistics.cells_free * CellSize;
713 *bytes_used = shm_header_p->statistics.cells_used * CellSize;
714 *bytes_overhead = shm_header_p->statistics.cells_system * CellSize + AlignedHeaderSize;
719 #else /* FAST_SHARE_MODES */
720 int shmem_dummy_procedure(void)