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)
79 int shm_processes_fd = -1;
87 shm_processes_fd = open(processreg_file, O_RDWR | O_CREAT, 0666);
89 if ( shm_processes_fd < 0 )
91 DEBUG(0,("ERROR shm_register_process : processreg_file open failed with code %d\n",errno));
95 *other_processes = False;
97 while ((nb_read = read(shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
101 if(process_exists(other_pid))
102 *other_processes = True;
106 DEBUG(2,("shm_register_process : erasing stale record for pid %d\n",other_pid));
107 other_pid = (pid_t)0;
108 erased_slot = lseek(shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
109 write(shm_processes_fd, &other_pid, sizeof(other_pid));
111 free_slot = erased_slot;
116 free_slot = lseek(shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
120 DEBUG(0,("ERROR shm_register_process : processreg_file read failed with code %d\n",errno));
121 close(shm_processes_fd);
126 free_slot = lseek(shm_processes_fd, 0, SEEK_END);
128 DEBUG(2,("shm_register_process : writing record for pid %d at offset %d\n",pid,free_slot));
129 lseek(shm_processes_fd, free_slot, SEEK_SET);
130 if(write(shm_processes_fd, &pid, sizeof(pid)) < 0)
132 DEBUG(0,("ERROR shm_register_process : processreg_file write failed with code %d\n",errno));
133 close(shm_processes_fd);
137 close(shm_processes_fd);
142 static BOOL shm_unregister_process(char *processreg_file, pid_t pid)
145 int shm_processes_fd = -1;
152 old_umask = umask(0);
153 shm_processes_fd = open(processreg_file, O_RDWR);
155 if ( shm_processes_fd < 0 )
157 DEBUG(0,("ERROR shm_unregister_process : processreg_file open failed with code %d\n",errno));
161 while ((nb_read = read(shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
166 DEBUG(2,("shm_unregister_process : erasing record for pid %d\n",other_pid));
167 other_pid = (pid_t)0;
168 erased_slot = lseek(shm_processes_fd, -sizeof(other_pid), SEEK_CUR);
169 if(write(shm_processes_fd, &other_pid, sizeof(other_pid)) < 0)
171 DEBUG(0,("ERROR shm_unregister_process : processreg_file write failed with code %d\n",errno));
172 close(shm_processes_fd);
182 DEBUG(0,("ERROR shm_unregister_process : processreg_file read failed with code %d\n",errno));
183 close(shm_processes_fd);
189 DEBUG(0,("ERROR shm_unregister_process : couldn't find pid %d in file %s\n",pid,processreg_file));
190 close(shm_processes_fd);
195 close(shm_processes_fd);
201 static BOOL shm_validate_header(int size)
206 DEBUG(0,("ERROR shm_validate_header : shmem not mapped\n"));
210 if(shm_header_p->shm_magic != SHM_MAGIC)
212 DEBUG(0,("ERROR shm_validate_header : bad magic\n"));
215 if(shm_header_p->shm_version != SHM_VERSION)
217 DEBUG(0,("ERROR shm_validate_header : bad version %X\n",shm_header_p->shm_version));
221 if(shm_header_p->total_size != size)
223 DEBUG(0,("ERROR shm_validate_header : shmem size mismatch (old = %d, new = %d)\n",shm_header_p->total_size,size));
227 if(!shm_header_p->consistent)
229 DEBUG(0,("ERROR shm_validate_header : shmem not consistent\n"));
235 static BOOL shm_initialize(int size)
237 struct ShmBlockDesc * first_free_block_p;
239 DEBUG(2,("shm_initialize : initializing shmem file of size %d\n",size));
244 DEBUG(0,("ERROR shm_initialize : shmem not mapped\n"));
248 shm_header_p->shm_magic = SHM_MAGIC;
249 shm_header_p->shm_version = SHM_VERSION;
250 shm_header_p->total_size = size;
251 shm_header_p->first_free_off = AlignedHeaderSize;
252 shm_header_p->userdef_off = NULL_OFFSET;
254 first_free_block_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
255 first_free_block_p->next = EOList_Off;
256 first_free_block_p->size = ( size - AlignedHeaderSize - CellSize ) / CellSize ;
258 shm_header_p->statistics.cells_free = first_free_block_p->size;
259 shm_header_p->statistics.cells_used = 0;
260 shm_header_p->statistics.cells_system = 1;
262 shm_header_p->consistent = True;
267 static void shm_solve_neighbors(struct ShmBlockDesc *head_p )
269 struct ShmBlockDesc *next_p;
271 /* Check if head_p and head_p->next are neighbors and if so join them */
272 if ( head_p == EOList_Addr ) return ;
273 if ( head_p->next == EOList_Off ) return ;
275 next_p = (struct ShmBlockDesc *)shm_offset2addr(head_p->next);
276 if ( ( head_p + head_p->size + 1 ) == next_p)
278 head_p->size += next_p->size +1 ; /* adapt size */
279 head_p->next = next_p->next ; /* link out */
281 shm_header_p->statistics.cells_free += 1;
282 shm_header_p->statistics.cells_system -= 1;
288 BOOL shm_open( char *file_name, int size)
291 BOOL created_new = False;
292 BOOL other_processes = True;
295 DEBUG(2,("shm_open : using shmem file %s to be of size %d\n",file_name,size));
297 old_umask = umask(0);
298 shm_fd = open(file_name, O_RDWR | O_CREAT, 0666);
302 DEBUG(0,("ERROR shm_open : open failed with code %d\n",errno));
308 DEBUG(0,("ERROR shm_open : can't do shm_lock\n"));
312 if( (filesize = lseek(shm_fd, 0, SEEK_END)) < 0)
314 DEBUG(0,("ERROR shm_open : lseek failed with code %d\n",errno));
320 /* return the file offset to 0 to save on later seeks */
321 lseek(shm_fd,0,SEEK_SET);
325 /* we just created a new one */
329 /* to find out if some other process is already mapping the file,
330 we use a registration file containing the processids of the file mapping processes
333 /* construct processreg file name */
334 strcpy(shm_processreg_name, file_name);
335 strcat(shm_processreg_name, ".processes");
337 if (! shm_register_process(shm_processreg_name, getpid(), &other_processes))
344 if (created_new || !other_processes)
346 /* we just created a new one, or are the first opener, lets set it size */
347 if( ftruncate(shm_fd, size) <0)
349 DEBUG(0,("ERROR shm_open : ftruncate failed with code %d\n",errno));
350 shm_unregister_process(shm_processreg_name, getpid());
357 lseek(shm_fd,0,SEEK_SET);
362 if (size != filesize )
364 /* the existing file has a different size and we are not the first opener.
365 Since another process is still using it, we will use the file size */
366 DEBUG(0,("WARNING shm_open : filesize (%d) != expected size (%d), using filesize\n",filesize,size));
370 shm_header_p = (struct ShmHeader *)mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, shm_fd, 0);
371 /* WARNING, shm_header_p can be different for different processes mapping the same file ! */
372 if (shm_header_p == (struct ShmHeader *)(-1))
374 DEBUG(0,("ERROR shm_open : mmap failed with code %d\n",errno));
375 shm_unregister_process(shm_processreg_name, getpid());
382 if (created_new || !other_processes)
384 shm_initialize(size);
386 else if (!shm_validate_header(size) )
388 /* existing file is corrupt, samba admin should remove it by hand */
389 DEBUG(0,("ERROR shm_open : corrupt shared mem file, remove it manually\n"));
390 munmap((caddr_t)shm_header_p, size);
391 shm_unregister_process(shm_processreg_name, getpid());
403 BOOL shm_close( void )
406 DEBUG(2,("shm_close\n"));
407 if(shm_times_locked > 0)
408 DEBUG(0,("WARNING shm_close : shmem was still locked %d times\n",shm_times_locked));;
409 if ( munmap((caddr_t)shm_header_p, shm_header_p->total_size) < 0)
411 DEBUG(0,("ERROR shm_close : munmap failed with code %d\n",errno));
415 shm_unregister_process(shm_processreg_name, getpid());
421 shm_processreg_name[0] = '\0';
423 shm_header_p = (struct ShmHeader *)0;
424 shm_times_locked = 0;
429 shm_offset_t shm_alloc(int size)
432 struct ShmBlockDesc *scanner_p;
433 struct ShmBlockDesc *prev_p;
434 struct ShmBlockDesc *new_p;
435 shm_offset_t result_offset;
441 DEBUG(0,("ERROR shm_alloc : shmem not mapped\n"));
445 if( !shm_header_p->consistent)
447 DEBUG(0,("ERROR shm_alloc : shmem not consistent\n"));
452 /* calculate the number of cells */
453 num_cells = (size + CellSize -1) / CellSize;
455 /* set start of scan */
456 prev_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
459 /* scan the free list to find a matching free space */
460 while ( ( scanner_p != EOList_Addr ) && ( scanner_p->size < num_cells ) )
463 scanner_p = (struct ShmBlockDesc *)shm_offset2addr(scanner_p->next);
466 /* at this point scanner point to a block header or to the end of the list */
467 if ( scanner_p == EOList_Addr )
469 DEBUG(0,("ERROR shm_alloc : alloc of %d bytes failed, no free space found\n",size));
470 return (NULL_OFFSET);
473 /* going to modify shared mem */
474 shm_header_p->consistent = False;
476 /* if we found a good one : scanner == the good one */
477 if ( scanner_p->size <= num_cells + 2 )
479 /* there is no use in making a new one, it will be too small anyway
480 * we will link out scanner
482 if ( prev_p == scanner_p )
484 shm_header_p->first_free_off = scanner_p->next ;
488 prev_p->next = scanner_p->next ;
490 shm_header_p->statistics.cells_free -= scanner_p->size;
491 shm_header_p->statistics.cells_used += scanner_p->size;
496 new_p = scanner_p + 1 + num_cells;
497 new_p->size = scanner_p->size - num_cells - 1;
498 new_p->next = scanner_p->next;
499 scanner_p->size = num_cells;
500 scanner_p->next = shm_addr2offset(new_p);
502 if ( prev_p != scanner_p )
504 prev_p->next = shm_addr2offset(new_p) ;
508 shm_header_p->first_free_off = shm_addr2offset(new_p) ;
510 shm_header_p->statistics.cells_free -= num_cells+1;
511 shm_header_p->statistics.cells_used += num_cells;
512 shm_header_p->statistics.cells_system += 1;
515 result_offset = shm_addr2offset( &(scanner_p[1]) );
516 scanner_p->next = SHM_NOT_FREE_OFF ;
518 /* end modification of shared mem */
519 shm_header_p->consistent = True;
521 DEBUG(2,("shm_alloc : request for %d bytes, allocated %d bytes at offset %d\n",size,scanner_p->size*CellSize,result_offset ));
523 return ( result_offset );
528 BOOL shm_free(shm_offset_t offset)
530 struct ShmBlockDesc *header_p ; /* pointer to header of block to free */
531 struct ShmBlockDesc *scanner_p ; /* used to scan the list */
532 struct ShmBlockDesc *prev_p ; /* holds previous in the list */
537 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
541 if( !shm_header_p->consistent)
543 DEBUG(0,("ERROR shm_free : shmem not consistent\n"));
547 header_p = ( (struct ShmBlockDesc *)shm_offset2addr(offset) - 1); /* make pointer to header of block */
549 if (header_p->next != SHM_NOT_FREE_OFF)
551 DEBUG(0,("ERROR shm_free : bad offset (%d)\n",offset));
555 /* find a place in the free_list to put the header in */
557 /* set scanner and previous pointer to start of list */
558 prev_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
561 while ( ( scanner_p != EOList_Addr) && (scanner_p < header_p) ) /* while we didn't scan past its position */
564 scanner_p = (struct ShmBlockDesc *)shm_offset2addr(scanner_p->next);
567 shm_header_p->consistent = False;
569 DEBUG(2,("shm_free : freeing %d bytes at offset %d\n",header_p->size*CellSize,offset));
571 if ( scanner_p == prev_p )
573 shm_header_p->statistics.cells_free += header_p->size;
574 shm_header_p->statistics.cells_used -= header_p->size;
576 /* we must free it at the beginning of the list */
577 shm_header_p->first_free_off = shm_addr2offset(header_p); /* set the free_list_pointer to this block_header */
579 /* scanner is the one that was first in the list */
580 header_p->next = shm_addr2offset(scanner_p);
581 shm_solve_neighbors( header_p ); /* if neighbors then link them */
583 shm_header_p->consistent = True;
588 shm_header_p->statistics.cells_free += header_p->size;
589 shm_header_p->statistics.cells_used -= header_p->size;
591 prev_p->next = shm_addr2offset(header_p);
592 header_p->next = shm_addr2offset(scanner_p);
593 shm_solve_neighbors(header_p) ;
594 shm_solve_neighbors(prev_p) ;
596 shm_header_p->consistent = True;
601 shm_offset_t shm_get_userdef_off(void)
606 return shm_header_p->userdef_off;
609 BOOL shm_set_userdef_off(shm_offset_t userdef_off)
614 shm_header_p->userdef_off = userdef_off;
618 void * shm_offset2addr(shm_offset_t offset)
620 if (offset == NULL_OFFSET )
626 return (void *)((char *)shm_header_p + offset );
629 shm_offset_t shm_addr2offset(void *addr)
637 return (shm_offset_t)((char *)addr - (char *)shm_header_p);
644 DEBUG(0,("ERROR shm_lock : bad shm_fd (%d)\n",shm_fd));
650 if(shm_times_locked > 1)
652 DEBUG(2,("shm_lock : locked %d times\n",shm_times_locked));
656 if (lockf(shm_fd, F_LOCK, 0) < 0)
658 DEBUG(0,("ERROR shm_lock : lockf failed with code %d\n",errno));
669 BOOL shm_unlock(void)
673 DEBUG(0,("ERROR shm_unlock : bad shm_fd (%d)\n",shm_fd));
677 if(shm_times_locked == 0)
679 DEBUG(0,("ERROR shm_unlock : shmem not locked\n",shm_fd));
685 if(shm_times_locked > 0)
687 DEBUG(2,("shm_unlock : still locked %d times\n",shm_times_locked));
691 if (lockf(shm_fd, F_ULOCK, 0) < 0)
693 DEBUG(0,("ERROR shm_unlock : lockf failed with code %d\n",errno));
703 BOOL shm_get_usage(int *bytes_free,
710 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
713 *bytes_free = shm_header_p->statistics.cells_free * CellSize;
714 *bytes_used = shm_header_p->statistics.cells_used * CellSize;
715 *bytes_overhead = shm_header_p->statistics.cells_system * CellSize + AlignedHeaderSize;
720 #else /* FAST_SHARE_MODES */
721 int shmem_dummy_procedure(void)