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.
29 extern int DEBUGLEVEL;
32 #define SHM_MAGIC 0x53484100
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 */
46 shm_offset_t first_free_off;
47 shm_offset_t 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 ShmBlockDesc) size */
51 int cells_system; /* number of cells used as allocated block descriptors */
55 #define SHM_NOT_FREE_OFF (-1)
58 shm_offset_t next; /* offset of next block in the free list or SHM_NOT_FREE_OFF when block in use */
59 int size; /* user size in BlockDescSize units */
62 #define EOList_Addr (struct ShmBlockDesc *)( 0 )
63 #define EOList_Off (NULL_OFFSET)
65 #define CellSize sizeof(struct ShmBlockDesc)
67 /* HeaderSize aligned on 8 byte boundary */
68 #define AlignedHeaderSize ((sizeof(struct ShmHeader)+7) & ~7)
70 static int shm_fd = -1;
71 static pstring shm_processreg_name = "";
73 static struct ShmHeader *shm_header_p = (struct ShmHeader *)0;
74 static int shm_times_locked = 0;
76 static BOOL shm_register_process(char *processreg_file, pid_t pid, BOOL *other_processes)
78 int shm_processes_fd = -1;
84 shm_processes_fd = open(processreg_file, O_RDWR | O_CREAT, 0666);
85 if ( shm_processes_fd < 0 )
87 DEBUG(0,("ERROR shm_register_process : processreg_file open failed with code %d\n",errno));
91 *other_processes = False;
93 while ((nb_read = read(shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
97 if(process_exists(other_pid))
98 *other_processes = True;
102 DEBUG(2,("shm_register_process : erasing stale record for pid %d\n",other_pid));
103 other_pid = (pid_t)0;
104 erased_slot = lseek(shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
105 write(shm_processes_fd, &other_pid, sizeof(other_pid));
107 free_slot = erased_slot;
112 free_slot = lseek(shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
116 DEBUG(0,("ERROR shm_register_process : processreg_file read failed with code %d\n",errno));
117 close(shm_processes_fd);
122 free_slot = lseek(shm_processes_fd, 0, SEEK_END);
124 DEBUG(2,("shm_register_process : writing record for pid %d at offset %d\n",pid,free_slot));
125 lseek(shm_processes_fd, free_slot, SEEK_SET);
126 if(write(shm_processes_fd, &pid, sizeof(pid)) < 0)
128 DEBUG(0,("ERROR shm_register_process : processreg_file write failed with code %d\n",errno));
129 close(shm_processes_fd);
133 close(shm_processes_fd);
138 static BOOL shm_unregister_process(char *processreg_file, pid_t pid)
141 int shm_processes_fd = -1;
148 old_umask = umask(0);
149 shm_processes_fd = open(processreg_file, O_RDWR);
151 if ( shm_processes_fd < 0 )
153 DEBUG(0,("ERROR shm_unregister_process : processreg_file open failed with code %d\n",errno));
157 while ((nb_read = read(shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
162 DEBUG(2,("shm_unregister_process : erasing record for pid %d\n",other_pid));
163 other_pid = (pid_t)0;
164 erased_slot = lseek(shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
165 if(write(shm_processes_fd, &other_pid, sizeof(other_pid)) < 0)
167 DEBUG(0,("ERROR shm_unregister_process : processreg_file write failed with code %d\n",errno));
168 close(shm_processes_fd);
178 DEBUG(0,("ERROR shm_unregister_process : processreg_file read failed with code %d\n",errno));
179 close(shm_processes_fd);
185 DEBUG(0,("ERROR shm_unregister_process : couldn't find pid %d in file %s\n",pid,processreg_file));
186 close(shm_processes_fd);
191 close(shm_processes_fd);
197 static BOOL shm_validate_header(int size)
202 DEBUG(0,("ERROR shm_validate_header : shmem not mapped\n"));
206 if(shm_header_p->shm_magic != SHM_MAGIC)
208 DEBUG(0,("ERROR shm_validate_header : bad magic\n"));
211 if(shm_header_p->shm_version != SHM_VERSION)
213 DEBUG(0,("ERROR shm_validate_header : bad version %X\n",shm_header_p->shm_version));
217 if(shm_header_p->total_size != size)
219 DEBUG(0,("ERROR shm_validate_header : shmem size mismatch (old = %d, new = %d)\n",shm_header_p->total_size,size));
223 if(!shm_header_p->consistent)
225 DEBUG(0,("ERROR shm_validate_header : shmem not consistent\n"));
231 static BOOL shm_initialize(int size)
233 struct ShmBlockDesc * first_free_block_p;
235 DEBUG(2,("shm_initialize : initializing shmem file of size %d\n",size));
240 DEBUG(0,("ERROR shm_initialize : shmem not mapped\n"));
244 shm_header_p->shm_magic = SHM_MAGIC;
245 shm_header_p->shm_version = SHM_VERSION;
246 shm_header_p->total_size = size;
247 shm_header_p->first_free_off = AlignedHeaderSize;
248 shm_header_p->userdef_off = NULL_OFFSET;
250 first_free_block_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
251 first_free_block_p->next = EOList_Off;
252 first_free_block_p->size = ( size - AlignedHeaderSize - CellSize ) / CellSize ;
254 shm_header_p->statistics.cells_free = first_free_block_p->size;
255 shm_header_p->statistics.cells_used = 0;
256 shm_header_p->statistics.cells_system = 1;
258 shm_header_p->consistent = True;
263 static void shm_solve_neighbors(struct ShmBlockDesc *head_p )
265 struct ShmBlockDesc *next_p;
267 /* Check if head_p and head_p->next are neighbors and if so join them */
268 if ( head_p == EOList_Addr ) return ;
269 if ( head_p->next == EOList_Off ) return ;
271 next_p = (struct ShmBlockDesc *)shm_offset2addr(head_p->next);
272 if ( ( head_p + head_p->size + 1 ) == next_p)
274 head_p->size += next_p->size +1 ; /* adapt size */
275 head_p->next = next_p->next ; /* link out */
277 shm_header_p->statistics.cells_free += 1;
278 shm_header_p->statistics.cells_system -= 1;
284 BOOL shm_open( char *file_name, int size)
287 BOOL created_new = False;
288 BOOL other_processes = True;
291 DEBUG(2,("shm_open : using shmem file %s to be of size %d\n",file_name,size));
293 old_umask = umask(0);
294 shm_fd = open(file_name, O_RDWR | O_CREAT, 0666);
298 DEBUG(0,("ERROR shm_open : open failed with code %d\n",errno));
304 DEBUG(0,("ERROR shm_open : can't do shm_lock\n"));
308 if( (filesize = lseek(shm_fd, 0, SEEK_END)) < 0)
310 DEBUG(0,("ERROR shm_open : lseek failed with code %d\n",errno));
316 /* return the file offset to 0 to save on later seeks */
317 lseek(shm_fd,0,SEEK_SET);
321 /* we just created a new one */
325 /* to find out if some other process is already mapping the file,
326 we use a registration file containing the processids of the file mapping processes
329 /* construct processreg file name */
330 strcpy(shm_processreg_name, file_name);
331 strcat(shm_processreg_name, ".processes");
333 if (! shm_register_process(shm_processreg_name, getpid(), &other_processes))
340 if (created_new || !other_processes)
342 /* we just created a new one, or are the first opener, lets set it size */
343 if( ftruncate(shm_fd, size) <0)
345 DEBUG(0,("ERROR shm_open : ftruncate failed with code %d\n",errno));
346 shm_unregister_process(shm_processreg_name, getpid());
353 lseek(shm_fd,0,SEEK_SET);
358 if (size != filesize )
360 /* the existing file has a different size and we are not the first opener.
361 Since another process is still using it, we will use the file size */
362 DEBUG(0,("WARNING shm_open : filesize (%d) != expected size (%d), using filesize\n",filesize,size));
366 shm_header_p = (struct ShmHeader *)mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, shm_fd, 0);
367 /* WARNING, shm_header_p can be different for different processes mapping the same file ! */
368 if (shm_header_p == (struct ShmHeader *)(-1))
370 DEBUG(0,("ERROR shm_open : mmap failed with code %d\n",errno));
371 shm_unregister_process(shm_processreg_name, getpid());
378 if (created_new || !other_processes)
380 shm_initialize(size);
382 else if (!shm_validate_header(size) )
384 /* existing file is corrupt, samba admin should remove it by hand */
385 DEBUG(0,("ERROR shm_open : corrupt shared mem file, remove it manually\n"));
386 munmap((caddr_t)shm_header_p, size);
387 shm_unregister_process(shm_processreg_name, getpid());
399 BOOL shm_close( void )
402 DEBUG(2,("shm_close\n"));
403 if(shm_times_locked > 0)
404 DEBUG(0,("WARNING shm_close : shmem was still locked %d times\n",shm_times_locked));;
405 if ( munmap((caddr_t)shm_header_p, shm_header_p->total_size) < 0)
407 DEBUG(0,("ERROR shm_close : munmap failed with code %d\n",errno));
411 shm_unregister_process(shm_processreg_name, getpid());
417 shm_processreg_name[0] = '\0';
419 shm_header_p = (struct ShmHeader *)0;
420 shm_times_locked = 0;
425 shm_offset_t shm_alloc(int size)
428 struct ShmBlockDesc *scanner_p;
429 struct ShmBlockDesc *prev_p;
430 struct ShmBlockDesc *new_p;
431 shm_offset_t result_offset;
437 DEBUG(0,("ERROR shm_alloc : shmem not mapped\n"));
441 if( !shm_header_p->consistent)
443 DEBUG(0,("ERROR shm_alloc : shmem not consistent\n"));
448 /* calculate the number of cells */
449 num_cells = (size + CellSize -1) / CellSize;
451 /* set start of scan */
452 prev_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
455 /* scan the free list to find a matching free space */
456 while ( ( scanner_p != EOList_Addr ) && ( scanner_p->size < num_cells ) )
459 scanner_p = (struct ShmBlockDesc *)shm_offset2addr(scanner_p->next);
462 /* at this point scanner point to a block header or to the end of the list */
463 if ( scanner_p == EOList_Addr )
465 DEBUG(0,("ERROR shm_alloc : alloc of %d bytes failed, no free space found\n",size));
466 return (NULL_OFFSET);
469 /* going to modify shared mem */
470 shm_header_p->consistent = False;
472 /* if we found a good one : scanner == the good one */
473 if ( scanner_p->size <= num_cells + 2 )
475 /* there is no use in making a new one, it will be too small anyway
476 * we will link out scanner
478 if ( prev_p == scanner_p )
480 shm_header_p->first_free_off = scanner_p->next ;
484 prev_p->next = scanner_p->next ;
486 shm_header_p->statistics.cells_free -= scanner_p->size;
487 shm_header_p->statistics.cells_used += scanner_p->size;
492 new_p = scanner_p + 1 + num_cells;
493 new_p->size = scanner_p->size - num_cells - 1;
494 new_p->next = scanner_p->next;
495 scanner_p->size = num_cells;
496 scanner_p->next = shm_addr2offset(new_p);
498 if ( prev_p != scanner_p )
500 prev_p->next = shm_addr2offset(new_p) ;
504 shm_header_p->first_free_off = shm_addr2offset(new_p) ;
506 shm_header_p->statistics.cells_free -= num_cells+1;
507 shm_header_p->statistics.cells_used += num_cells;
508 shm_header_p->statistics.cells_system += 1;
511 result_offset = shm_addr2offset( &(scanner_p[1]) );
512 scanner_p->next = SHM_NOT_FREE_OFF ;
514 /* end modification of shared mem */
515 shm_header_p->consistent = True;
517 DEBUG(2,("shm_alloc : request for %d bytes, allocated %d bytes at offset %d\n",size,scanner_p->size*CellSize,result_offset ));
519 return ( result_offset );
524 BOOL shm_free(shm_offset_t offset)
526 struct ShmBlockDesc *header_p ; /* pointer to header of block to free */
527 struct ShmBlockDesc *scanner_p ; /* used to scan the list */
528 struct ShmBlockDesc *prev_p ; /* holds previous in the list */
533 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
537 if( !shm_header_p->consistent)
539 DEBUG(0,("ERROR shm_free : shmem not consistent\n"));
543 header_p = ( (struct ShmBlockDesc *)shm_offset2addr(offset) - 1); /* make pointer to header of block */
545 if (header_p->next != SHM_NOT_FREE_OFF)
547 DEBUG(0,("ERROR shm_free : bad offset (%d)\n",offset));
551 /* find a place in the free_list to put the header in */
553 /* set scanner and previous pointer to start of list */
554 prev_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
557 while ( ( scanner_p != EOList_Addr) && (scanner_p < header_p) ) /* while we didn't scan past its position */
560 scanner_p = (struct ShmBlockDesc *)shm_offset2addr(scanner_p->next);
563 shm_header_p->consistent = False;
565 DEBUG(2,("shm_free : freeing %d bytes at offset %d\n",header_p->size*CellSize,offset));
567 if ( scanner_p == prev_p )
569 shm_header_p->statistics.cells_free += header_p->size;
570 shm_header_p->statistics.cells_used -= header_p->size;
572 /* we must free it at the beginning of the list */
573 shm_header_p->first_free_off = shm_addr2offset(header_p); /* set the free_list_pointer to this block_header */
575 /* scanner is the one that was first in the list */
576 header_p->next = shm_addr2offset(scanner_p);
577 shm_solve_neighbors( header_p ); /* if neighbors then link them */
579 shm_header_p->consistent = True;
584 shm_header_p->statistics.cells_free += header_p->size;
585 shm_header_p->statistics.cells_used -= header_p->size;
587 prev_p->next = shm_addr2offset(header_p);
588 header_p->next = shm_addr2offset(scanner_p);
589 shm_solve_neighbors(header_p) ;
590 shm_solve_neighbors(prev_p) ;
592 shm_header_p->consistent = True;
597 shm_offset_t shm_get_userdef_off(void)
602 return shm_header_p->userdef_off;
605 BOOL shm_set_userdef_off(shm_offset_t userdef_off)
610 shm_header_p->userdef_off = userdef_off;
614 void * shm_offset2addr(shm_offset_t offset)
616 if (offset == NULL_OFFSET )
622 return (void *)((char *)shm_header_p + offset );
625 shm_offset_t shm_addr2offset(void *addr)
633 return (shm_offset_t)((char *)addr - (char *)shm_header_p);
640 DEBUG(0,("ERROR shm_lock : bad shm_fd (%d)\n",shm_fd));
646 if(shm_times_locked > 1)
648 DEBUG(2,("shm_lock : locked %d times\n",shm_times_locked));
652 if (lockf(shm_fd, F_LOCK, 0) < 0)
654 DEBUG(0,("ERROR shm_lock : lockf failed with code %d\n",errno));
665 BOOL shm_unlock(void)
669 DEBUG(0,("ERROR shm_unlock : bad shm_fd (%d)\n",shm_fd));
673 if(shm_times_locked == 0)
675 DEBUG(0,("ERROR shm_unlock : shmem not locked\n",shm_fd));
681 if(shm_times_locked > 0)
683 DEBUG(2,("shm_unlock : still locked %d times\n",shm_times_locked));
687 if (lockf(shm_fd, F_ULOCK, 0) < 0)
689 DEBUG(0,("ERROR shm_unlock : lockf failed with code %d\n",errno));
699 BOOL shm_get_usage(int *bytes_free,
706 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
709 *bytes_free = shm_header_p->statistics.cells_free * CellSize;
710 *bytes_used = shm_header_p->statistics.cells_used * CellSize;
711 *bytes_overhead = shm_header_p->statistics.cells_system * CellSize + AlignedHeaderSize;
716 #else /* FAST_SHARE_MODES */
717 int shmem_dummy_procedure(void)