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 SMB_SHM_MAGIC 0x53484100
35 #define SMB_SHM_VERSION 1
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 smb_shm_offset_t first_free_off;
47 smb_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 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 smb_shm_offset_t 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_register_process(char *processreg_file, pid_t pid, BOOL *other_processes)
78 int smb_shm_processes_fd = -1;
84 smb_shm_processes_fd = open(processreg_file, O_RDWR | O_CREAT, 0666);
85 if ( smb_shm_processes_fd < 0 )
87 DEBUG(0,("ERROR smb_shm_register_process : processreg_file open failed with code %d\n",errno));
91 *other_processes = False;
93 while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
97 if(process_exists(other_pid))
98 *other_processes = True;
102 DEBUG(2,("smb_shm_register_process : erasing stale record for pid %d\n",other_pid));
103 other_pid = (pid_t)0;
104 erased_slot = lseek(smb_shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
105 write(smb_shm_processes_fd, &other_pid, sizeof(other_pid));
107 free_slot = erased_slot;
112 free_slot = lseek(smb_shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
116 DEBUG(0,("ERROR smb_shm_register_process : processreg_file read failed with code %d\n",errno));
117 close(smb_shm_processes_fd);
122 free_slot = lseek(smb_shm_processes_fd, 0, SEEK_END);
124 DEBUG(2,("smb_shm_register_process : writing record for pid %d at offset %d\n",pid,free_slot));
125 lseek(smb_shm_processes_fd, free_slot, SEEK_SET);
126 if(write(smb_shm_processes_fd, &pid, sizeof(pid)) < 0)
128 DEBUG(0,("ERROR smb_shm_register_process : processreg_file write failed with code %d\n",errno));
129 close(smb_shm_processes_fd);
133 close(smb_shm_processes_fd);
138 static BOOL smb_shm_unregister_process(char *processreg_file, pid_t pid)
141 int smb_shm_processes_fd = -1;
148 old_umask = umask(0);
149 smb_shm_processes_fd = open(processreg_file, O_RDWR);
151 if ( smb_shm_processes_fd < 0 )
153 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file open failed with code %d\n",errno));
157 while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
162 DEBUG(2,("smb_shm_unregister_process : erasing record for pid %d\n",other_pid));
163 other_pid = (pid_t)0;
164 erased_slot = lseek(smb_shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
165 if(write(smb_shm_processes_fd, &other_pid, sizeof(other_pid)) < 0)
167 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file write failed with code %d\n",errno));
168 close(smb_shm_processes_fd);
178 DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file read failed with code %d\n",errno));
179 close(smb_shm_processes_fd);
185 DEBUG(0,("ERROR smb_shm_unregister_process : couldn't find pid %d in file %s\n",pid,processreg_file));
186 close(smb_shm_processes_fd);
191 close(smb_shm_processes_fd);
197 static BOOL smb_shm_validate_header(int size)
199 if( !smb_shm_header_p )
202 DEBUG(0,("ERROR smb_shm_validate_header : shmem not mapped\n"));
206 if(smb_shm_header_p->smb_shm_magic != SMB_SHM_MAGIC)
208 DEBUG(0,("ERROR smb_shm_validate_header : bad magic\n"));
211 if(smb_shm_header_p->smb_shm_version != SMB_SHM_VERSION)
213 DEBUG(0,("ERROR smb_shm_validate_header : bad version %X\n",smb_shm_header_p->smb_shm_version));
217 if(smb_shm_header_p->total_size != size)
219 DEBUG(0,("ERROR smb_shm_validate_header : shmem size mismatch (old = %d, new = %d)\n",smb_shm_header_p->total_size,size));
223 if(!smb_shm_header_p->consistent)
225 DEBUG(0,("ERROR smb_shm_validate_header : shmem not consistent\n"));
231 static BOOL smb_shm_initialize(int size)
233 struct SmbShmBlockDesc * first_free_block_p;
235 DEBUG(2,("smb_shm_initialize : initializing shmem file of size %d\n",size));
237 if( !smb_shm_header_p )
240 DEBUG(0,("ERROR smb_shm_initialize : shmem not mapped\n"));
244 smb_shm_header_p->smb_shm_magic = SMB_SHM_MAGIC;
245 smb_shm_header_p->smb_shm_version = SMB_SHM_VERSION;
246 smb_shm_header_p->total_size = size;
247 smb_shm_header_p->first_free_off = AlignedHeaderSize;
248 smb_shm_header_p->userdef_off = NULL_OFFSET;
250 first_free_block_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_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 smb_shm_header_p->statistics.cells_free = first_free_block_p->size;
255 smb_shm_header_p->statistics.cells_used = 0;
256 smb_shm_header_p->statistics.cells_system = 1;
258 smb_shm_header_p->consistent = True;
263 static void smb_shm_solve_neighbors(struct SmbShmBlockDesc *head_p )
265 struct SmbShmBlockDesc *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 SmbShmBlockDesc *)smb_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 smb_shm_header_p->statistics.cells_free += 1;
278 smb_shm_header_p->statistics.cells_system -= 1;
284 BOOL smb_shm_open( char *file_name, int size)
287 BOOL created_new = False;
288 BOOL other_processes = True;
291 DEBUG(2,("smb_shm_open : using shmem file %s to be of size %d\n",file_name,size));
293 old_umask = umask(0);
294 smb_shm_fd = open(file_name, O_RDWR | O_CREAT, 0666);
296 if ( smb_shm_fd < 0 )
298 DEBUG(0,("ERROR smb_shm_open : open failed with code %d\n",errno));
304 DEBUG(0,("ERROR smb_shm_open : can't do smb_shm_lock\n"));
308 if( (filesize = lseek(smb_shm_fd, 0, SEEK_END)) < 0)
310 DEBUG(0,("ERROR smb_shm_open : lseek failed with code %d\n",errno));
316 /* return the file offset to 0 to save on later seeks */
317 lseek(smb_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(smb_shm_processreg_name, file_name);
331 strcat(smb_shm_processreg_name, ".processes");
333 if (! smb_shm_register_process(smb_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(smb_shm_fd, size) <0)
345 DEBUG(0,("ERROR smb_shm_open : ftruncate failed with code %d\n",errno));
346 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
353 lseek(smb_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 smb_shm_open : filesize (%d) != expected size (%d), using filesize\n",filesize,size));
366 smb_shm_header_p = (struct SmbShmHeader *)mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, smb_shm_fd, 0);
367 /* WARNING, smb_shm_header_p can be different for different processes mapping the same file ! */
368 if (smb_shm_header_p == (struct SmbShmHeader *)(-1))
370 DEBUG(0,("ERROR smb_shm_open : mmap failed with code %d\n",errno));
371 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
378 if (created_new || !other_processes)
380 smb_shm_initialize(size);
382 else if (!smb_shm_validate_header(size) )
384 /* existing file is corrupt, samba admin should remove it by hand */
385 DEBUG(0,("ERROR smb_shm_open : corrupt shared mem file, remove it manually\n"));
386 munmap((caddr_t)smb_shm_header_p, size);
387 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
399 BOOL smb_shm_close( void )
402 DEBUG(2,("smb_shm_close\n"));
403 if(smb_shm_times_locked > 0)
404 DEBUG(0,("WARNING smb_shm_close : shmem was still locked %d times\n",smb_shm_times_locked));;
405 if ( munmap((caddr_t)smb_shm_header_p, smb_shm_header_p->total_size) < 0)
407 DEBUG(0,("ERROR smb_shm_close : munmap failed with code %d\n",errno));
411 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
417 smb_shm_processreg_name[0] = '\0';
419 smb_shm_header_p = (struct SmbShmHeader *)0;
420 smb_shm_times_locked = 0;
425 smb_shm_offset_t smb_shm_alloc(int size)
428 struct SmbShmBlockDesc *scanner_p;
429 struct SmbShmBlockDesc *prev_p;
430 struct SmbShmBlockDesc *new_p;
431 smb_shm_offset_t result_offset;
434 if( !smb_shm_header_p )
437 DEBUG(0,("ERROR smb_shm_alloc : shmem not mapped\n"));
441 if( !smb_shm_header_p->consistent)
443 DEBUG(0,("ERROR smb_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 SmbShmBlockDesc *)smb_shm_offset2addr(smb_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 SmbShmBlockDesc *)smb_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 smb_shm_alloc : alloc of %d bytes failed, no free space found\n",size));
466 return (NULL_OFFSET);
469 /* going to modify shared mem */
470 smb_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 smb_shm_header_p->first_free_off = scanner_p->next ;
484 prev_p->next = scanner_p->next ;
486 smb_shm_header_p->statistics.cells_free -= scanner_p->size;
487 smb_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 = smb_shm_addr2offset(new_p);
498 if ( prev_p != scanner_p )
500 prev_p->next = smb_shm_addr2offset(new_p) ;
504 smb_shm_header_p->first_free_off = smb_shm_addr2offset(new_p) ;
506 smb_shm_header_p->statistics.cells_free -= num_cells+1;
507 smb_shm_header_p->statistics.cells_used += num_cells;
508 smb_shm_header_p->statistics.cells_system += 1;
511 result_offset = smb_shm_addr2offset( &(scanner_p[1]) );
512 scanner_p->next = SMB_SHM_NOT_FREE_OFF ;
514 /* end modification of shared mem */
515 smb_shm_header_p->consistent = True;
517 DEBUG(2,("smb_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 smb_shm_free(smb_shm_offset_t offset)
526 struct SmbShmBlockDesc *header_p ; /* pointer to header of block to free */
527 struct SmbShmBlockDesc *scanner_p ; /* used to scan the list */
528 struct SmbShmBlockDesc *prev_p ; /* holds previous in the list */
530 if( !smb_shm_header_p )
533 DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
537 if( !smb_shm_header_p->consistent)
539 DEBUG(0,("ERROR smb_shm_free : shmem not consistent\n"));
543 header_p = ( (struct SmbShmBlockDesc *)smb_shm_offset2addr(offset) - 1); /* make pointer to header of block */
545 if (header_p->next != SMB_SHM_NOT_FREE_OFF)
547 DEBUG(0,("ERROR smb_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 SmbShmBlockDesc *)smb_shm_offset2addr(smb_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 SmbShmBlockDesc *)smb_shm_offset2addr(scanner_p->next);
563 smb_shm_header_p->consistent = False;
565 DEBUG(2,("smb_shm_free : freeing %d bytes at offset %d\n",header_p->size*CellSize,offset));
567 if ( scanner_p == prev_p )
569 smb_shm_header_p->statistics.cells_free += header_p->size;
570 smb_shm_header_p->statistics.cells_used -= header_p->size;
572 /* we must free it at the beginning of the list */
573 smb_shm_header_p->first_free_off = smb_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 = smb_shm_addr2offset(scanner_p);
577 smb_shm_solve_neighbors( header_p ); /* if neighbors then link them */
579 smb_shm_header_p->consistent = True;
584 smb_shm_header_p->statistics.cells_free += header_p->size;
585 smb_shm_header_p->statistics.cells_used -= header_p->size;
587 prev_p->next = smb_shm_addr2offset(header_p);
588 header_p->next = smb_shm_addr2offset(scanner_p);
589 smb_shm_solve_neighbors(header_p) ;
590 smb_shm_solve_neighbors(prev_p) ;
592 smb_shm_header_p->consistent = True;
597 smb_shm_offset_t smb_shm_get_userdef_off(void)
599 if (!smb_shm_header_p)
602 return smb_shm_header_p->userdef_off;
605 BOOL smb_shm_set_userdef_off(smb_shm_offset_t userdef_off)
607 if (!smb_shm_header_p)
610 smb_shm_header_p->userdef_off = userdef_off;
614 void * smb_shm_offset2addr(smb_shm_offset_t offset)
616 if (offset == NULL_OFFSET )
619 if (!smb_shm_header_p)
622 return (void *)((char *)smb_shm_header_p + offset );
625 smb_shm_offset_t smb_shm_addr2offset(void *addr)
630 if (!smb_shm_header_p)
633 return (smb_shm_offset_t)((char *)addr - (char *)smb_shm_header_p);
636 BOOL smb_shm_lock(void)
640 DEBUG(0,("ERROR smb_shm_lock : bad smb_shm_fd (%d)\n",smb_shm_fd));
644 smb_shm_times_locked++;
646 if(smb_shm_times_locked > 1)
648 DEBUG(2,("smb_shm_lock : locked %d times\n",smb_shm_times_locked));
652 if (lockf(smb_shm_fd, F_LOCK, 0) < 0)
654 DEBUG(0,("ERROR smb_shm_lock : lockf failed with code %d\n",errno));
655 smb_shm_times_locked--;
665 BOOL smb_shm_unlock(void)
669 DEBUG(0,("ERROR smb_shm_unlock : bad smb_shm_fd (%d)\n",smb_shm_fd));
673 if(smb_shm_times_locked == 0)
675 DEBUG(0,("ERROR smb_shm_unlock : shmem not locked\n",smb_shm_fd));
679 smb_shm_times_locked--;
681 if(smb_shm_times_locked > 0)
683 DEBUG(2,("smb_shm_unlock : still locked %d times\n",smb_shm_times_locked));
687 if (lockf(smb_shm_fd, F_ULOCK, 0) < 0)
689 DEBUG(0,("ERROR smb_shm_unlock : lockf failed with code %d\n",errno));
690 smb_shm_times_locked++;
699 BOOL smb_shm_get_usage(int *bytes_free,
703 if( !smb_shm_header_p )
706 DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
709 *bytes_free = smb_shm_header_p->statistics.cells_free * CellSize;
710 *bytes_used = smb_shm_header_p->statistics.cells_used * CellSize;
711 *bytes_overhead = smb_shm_header_p->statistics.cells_system * CellSize + AlignedHeaderSize;
716 #else /* FAST_SHARE_MODES */
717 int shmem_dummy_procedure(void)