2 Unix SMB/Netbios implementation.
4 Shared memory functions - SYSV IPC implementation
5 Copyright (C) Andrew Tridgell 1997-1998
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.
28 extern int DEBUGLEVEL;
30 #define SHMEM_KEY ((key_t)0x280267)
31 #define SEMAPHORE_KEY (SHMEM_KEY+2)
33 #define SHM_MAGIC 0x53484100
36 #define IPC_PERMS ((SHM_R | SHM_W) | (SHM_R>>3) | (SHM_R>>6))
39 #ifdef SECURE_SEMAPHORES
40 /* secure semaphores are slow because we have to do a become_root()
42 #define SEMAPHORE_PERMS IPC_PERMS
44 #define SEMAPHORE_PERMS 0666
47 #define SHMEM_HASH_SIZE 13
49 #define MIN_SHM_SIZE 0x1000
55 static int global_lock_count;
60 int total_size; /* in bytes */
63 int userdef_off; /* a userdefined offset. can be used to store
64 root of tree or list */
65 struct { /* a cell is a range of bytes of sizeof(struct
69 int cells_system; /* number of cells used as allocated
74 #define SHM_NOT_FREE_OFF (-1)
77 int next; /* offset of next block in the free list or
78 SHM_NOT_FREE_OFF when block in use */
79 int size; /* user size in BlockDescSize units */
82 #define EOList_Addr NULL
83 #define EOList_Off (0)
85 #define CellSize sizeof(struct ShmBlockDesc)
87 /* HeaderSize aligned on a 8 byte boundary */
88 #define AlignedHeaderSize ((sizeof(struct ShmHeader)+7) & ~7)
90 static struct ShmHeader *shm_header_p = NULL;
94 static BOOL sem_change(int i, int op)
96 #ifdef SECURE_SEMAPHORES
97 extern struct current_user current_user;
103 if (read_only) return True;
105 #ifdef SECURE_SEMAPHORES
106 if (current_user.uid != 0) {
116 ret = semop(sem_id, &sb, 1);
119 DEBUG(0,("ERROR: sem_change(%d,%d) failed (%s)\n",
120 i, op, strerror(errno)));
123 #ifdef SECURE_SEMAPHORES
132 static BOOL global_lock(void)
135 if (global_lock_count == 1)
136 return sem_change(0, -1);
140 static BOOL global_unlock(void)
143 if (global_lock_count == 0)
144 return sem_change(0, 1);
148 static void *shm_offset2addr(int offset)
156 return (void *)((char *)shm_header_p + offset);
159 static int shm_addr2offset(void *addr)
167 return (int)((char *)addr - (char *)shm_header_p);
171 static int shm_alloc(int size)
174 struct ShmBlockDesc *scanner_p;
175 struct ShmBlockDesc *prev_p;
176 struct ShmBlockDesc *new_p;
182 DEBUG(0,("ERROR shm_alloc : shmem not mapped\n"));
188 if (!shm_header_p->consistent) {
189 DEBUG(0,("ERROR shm_alloc : shmem not consistent\n"));
194 /* calculate the number of cells */
195 num_cells = (size + (CellSize-1)) / CellSize;
197 /* set start of scan */
198 prev_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
201 /* scan the free list to find a matching free space */
202 while ((scanner_p != EOList_Addr) && (scanner_p->size < num_cells)) {
204 scanner_p = (struct ShmBlockDesc *)shm_offset2addr(scanner_p->next);
207 /* at this point scanner point to a block header or to the end of
209 if (scanner_p == EOList_Addr) {
210 DEBUG(0,("ERROR shm_alloc : alloc of %d bytes failed\n",size));
215 /* going to modify shared mem */
216 shm_header_p->consistent = False;
218 /* if we found a good one : scanner == the good one */
219 if (scanner_p->size > num_cells + 2) {
221 new_p = scanner_p + 1 + num_cells;
222 new_p->size = scanner_p->size - (num_cells + 1);
223 new_p->next = scanner_p->next;
224 scanner_p->size = num_cells;
225 scanner_p->next = shm_addr2offset(new_p);
227 shm_header_p->statistics.cells_free -= 1;
228 shm_header_p->statistics.cells_system += 1;
231 /* take it from the free list */
232 if (prev_p == scanner_p) {
233 shm_header_p->first_free_off = scanner_p->next;
235 prev_p->next = scanner_p->next;
237 shm_header_p->statistics.cells_free -= scanner_p->size;
238 shm_header_p->statistics.cells_used += scanner_p->size;
240 result_offset = shm_addr2offset(&(scanner_p[1]));
241 scanner_p->next = SHM_NOT_FREE_OFF;
243 /* end modification of shared mem */
244 shm_header_p->consistent = True;
248 DEBUG(6,("shm_alloc : allocated %d bytes at offset %d\n",
249 size,result_offset));
251 return result_offset;
254 static void shm_solve_neighbors(struct ShmBlockDesc *head_p )
256 struct ShmBlockDesc *next_p;
258 /* Check if head_p and head_p->next are neighbors and if so
260 if ( head_p == EOList_Addr ) return ;
261 if ( head_p->next == EOList_Off ) return ;
263 next_p = (struct ShmBlockDesc *)shm_offset2addr(head_p->next);
264 if ((head_p + head_p->size + 1) == next_p) {
265 head_p->size += next_p->size + 1; /* adapt size */
266 head_p->next = next_p->next; /* link out */
268 shm_header_p->statistics.cells_free += 1;
269 shm_header_p->statistics.cells_system -= 1;
274 static BOOL shm_free(int offset)
276 struct ShmBlockDesc *header_p; /* pointer to header of
278 struct ShmBlockDesc *scanner_p; /* used to scan the list */
279 struct ShmBlockDesc *prev_p; /* holds previous in the
284 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
290 if (!shm_header_p->consistent) {
291 DEBUG(0,("ERROR shm_free : shmem not consistent\n"));
296 /* make pointer to header of block */
297 header_p = ((struct ShmBlockDesc *)shm_offset2addr(offset) - 1);
299 if (header_p->next != SHM_NOT_FREE_OFF) {
300 DEBUG(0,("ERROR shm_free : bad offset (%d)\n",offset));
305 /* find a place in the free_list to put the header in */
307 /* set scanner and previous pointer to start of list */
308 prev_p = (struct ShmBlockDesc *)
309 shm_offset2addr(shm_header_p->first_free_off);
312 while ((scanner_p != EOList_Addr) &&
313 (scanner_p < header_p)) {
314 /* while we didn't scan past its position */
316 scanner_p = (struct ShmBlockDesc *)
317 shm_offset2addr(scanner_p->next);
320 shm_header_p->consistent = False;
322 DEBUG(6,("shm_free : freeing %d bytes at offset %d\n",
323 header_p->size*CellSize,offset));
325 /* zero the area being freed - this allows us to find bugs faster */
326 memset(shm_offset2addr(offset), 0, header_p->size*CellSize);
328 if (scanner_p == prev_p) {
329 shm_header_p->statistics.cells_free += header_p->size;
330 shm_header_p->statistics.cells_used -= header_p->size;
332 /* we must free it at the beginning of the list */
333 shm_header_p->first_free_off = shm_addr2offset(header_p);
334 /* set the free_list_pointer to this block_header */
336 /* scanner is the one that was first in the list */
337 header_p->next = shm_addr2offset(scanner_p);
338 shm_solve_neighbors(header_p);
340 shm_header_p->consistent = True;
342 shm_header_p->statistics.cells_free += header_p->size;
343 shm_header_p->statistics.cells_used -= header_p->size;
345 prev_p->next = shm_addr2offset(header_p);
346 header_p->next = shm_addr2offset(scanner_p);
347 shm_solve_neighbors(header_p) ;
348 shm_solve_neighbors(prev_p) ;
350 shm_header_p->consistent = True;
359 * Function to create the hash table for the share mode entries. Called
360 * when smb shared memory is global locked.
362 static BOOL shm_create_hash_table(unsigned int hash_entries)
364 int size = hash_entries * sizeof(int);
367 shm_header_p->userdef_off = shm_alloc(size);
369 if(shm_header_p->userdef_off == 0) {
370 DEBUG(0,("shm_create_hash_table: Failed to create hash table of size %d\n",
376 /* Clear hash buckets. */
377 memset(shm_offset2addr(shm_header_p->userdef_off), '\0', size);
383 static BOOL shm_validate_header(int size)
387 DEBUG(0,("ERROR shm_validate_header : shmem not mapped\n"));
391 if(shm_header_p->shm_magic != SHM_MAGIC) {
392 DEBUG(0,("ERROR shm_validate_header : bad magic\n"));
396 if(shm_header_p->shm_version != SHM_VERSION) {
397 DEBUG(0,("ERROR shm_validate_header : bad version %X\n",
398 shm_header_p->shm_version));
402 if(shm_header_p->total_size != size) {
403 DEBUG(0,("ERROR shmem size mismatch (old = %d, new = %d)\n",
404 shm_header_p->total_size,size));
408 if(!shm_header_p->consistent) {
409 DEBUG(0,("ERROR shmem not consistent\n"));
416 static BOOL shm_initialize(int size)
418 struct ShmBlockDesc * first_free_block_p;
420 DEBUG(5,("shm_initialize : initializing shmem size %d\n",size));
422 if( !shm_header_p ) {
424 DEBUG(0,("ERROR shm_initialize : shmem not mapped\n"));
428 shm_header_p->shm_magic = SHM_MAGIC;
429 shm_header_p->shm_version = SHM_VERSION;
430 shm_header_p->total_size = size;
431 shm_header_p->first_free_off = AlignedHeaderSize;
432 shm_header_p->userdef_off = 0;
434 first_free_block_p = (struct ShmBlockDesc *)
435 shm_offset2addr(shm_header_p->first_free_off);
436 first_free_block_p->next = EOList_Off;
437 first_free_block_p->size =
438 (size - (AlignedHeaderSize+CellSize))/CellSize;
439 shm_header_p->statistics.cells_free = first_free_block_p->size;
440 shm_header_p->statistics.cells_used = 0;
441 shm_header_p->statistics.cells_system = 1;
443 shm_header_p->consistent = True;
448 static BOOL shm_close( void )
454 static int shm_get_userdef_off(void)
459 return shm_header_p->userdef_off;
463 /*******************************************************************
464 Lock a particular hash bucket entry.
465 ******************************************************************/
466 static BOOL shm_lock_hash_entry(unsigned int entry)
468 return sem_change(entry+1, -1);
471 /*******************************************************************
472 Unlock a particular hash bucket entry.
473 ******************************************************************/
474 static BOOL shm_unlock_hash_entry(unsigned int entry)
476 return sem_change(entry+1, 1);
480 /*******************************************************************
481 Gather statistics on shared memory usage.
482 ******************************************************************/
483 static BOOL shm_get_usage(int *bytes_free,
489 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
493 *bytes_free = shm_header_p->statistics.cells_free * CellSize;
494 *bytes_used = shm_header_p->statistics.cells_used * CellSize;
495 *bytes_overhead = shm_header_p->statistics.cells_system * CellSize +
502 /*******************************************************************
503 hash a number into a hash_entry
504 ******************************************************************/
505 static unsigned shm_hash_size(void)
511 static struct shmem_ops shmops = {
519 shm_unlock_hash_entry,
524 /*******************************************************************
525 open the shared memory
526 ******************************************************************/
527 struct shmem_ops *sysv_shm_open(int ronly)
529 BOOL other_processes;
530 struct shmid_ds shm_ds;
531 struct semid_ds sem_ds;
538 shm_size = lp_shmem_size();
540 DEBUG(4,("Trying sysv shmem open of size %d\n", shm_size));
542 /* first the semaphore */
543 sem_id = semget(SEMAPHORE_KEY, 0, 0);
545 if (read_only) return NULL;
547 hash_size = SHMEM_HASH_SIZE;
549 while (hash_size > 1) {
550 sem_id = semget(SEMAPHORE_KEY, hash_size+1,
551 IPC_CREAT|IPC_EXCL| SEMAPHORE_PERMS);
553 (errno != EINVAL && errno != ENOSPC)) break;
558 DEBUG(0,("Can't create or use semaphore [1]. Error was %s\n",
565 for (i=0;i<hash_size+1;i++) {
566 if (semctl(sem_id, i, SETVAL, su) != 0) {
567 DEBUG(1,("Failed to init semaphore %d. Error was %s\n",
568 i, strerror(errno)));
575 sem_id = semget(SEMAPHORE_KEY, 0, 0);
578 DEBUG(0,("Can't create or use semaphore [2]. Error was %s\n",
584 if (semctl(sem_id, 0, IPC_STAT, su) != 0) {
585 DEBUG(0,("ERROR semctl: can't IPC_STAT. Error was %s\n",
589 hash_size = sem_ds.sem_nsems-1;
592 if (sem_ds.sem_perm.cuid != 0 || sem_ds.sem_perm.cgid != 0) {
593 DEBUG(0,("ERROR: root did not create the semaphore\n"));
597 if (semctl(sem_id, 0, GETVAL, su) == 0 &&
598 !process_exists((pid=semctl(sem_id, 0, GETPID, su)))) {
599 DEBUG(0,("WARNING: clearing global IPC lock set by dead process %d\n",
602 if (semctl(sem_id, 0, SETVAL, su) != 0) {
603 DEBUG(0,("ERROR: Failed to clear global lock. Error was %s\n",
609 sem_ds.sem_perm.mode = SEMAPHORE_PERMS;
610 if (semctl(sem_id, 0, IPC_SET, su) != 0) {
611 DEBUG(0,("ERROR shmctl : can't IPC_SET. Error was %s\n",
621 for (i=1;i<hash_size+1;i++) {
622 if (semctl(sem_id, i, GETVAL, su) == 0 &&
623 !process_exists((pid=semctl(sem_id, i, GETPID, su)))) {
624 DEBUG(1,("WARNING: clearing IPC lock %d set by dead process %d\n",
627 if (semctl(sem_id, i, SETVAL, su) != 0) {
628 DEBUG(0,("ERROR: Failed to clear IPC lock %d. Error was %s\n",
629 i, strerror(errno)));
637 * Try to use an existing key. Note that
638 * in order to use an existing key successfully
639 * size must be zero else shmget returns EINVAL.
640 * Thanks to Veselin Terzic <vterzic@systems.DHL.COM>
641 * for pointing this out.
644 shm_id = shmget(SHMEM_KEY, 0, 0);
646 /* if that failed then create one */
648 if (read_only) return NULL;
649 while (shm_size > MIN_SHM_SIZE) {
650 shm_id = shmget(SHMEM_KEY, shm_size,
651 IPC_CREAT | IPC_EXCL | IPC_PERMS);
653 (errno != EINVAL && errno != ENOSPC)) break;
659 DEBUG(0,("Can't create or use IPC area. Error was %s\n", strerror(errno)));
665 shm_header_p = (struct ShmHeader *)shmat(shm_id, 0,
666 read_only?SHM_RDONLY:0);
667 if ((long)shm_header_p == -1) {
668 DEBUG(0,("Can't attach to IPC area. Error was %s\n", strerror(errno)));
673 /* to find out if some other process is already mapping the file,
674 we use a registration file containing the processids of the file
676 if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
677 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n", strerror(errno)));
683 if (shm_ds.shm_perm.cuid != 0 || shm_ds.shm_perm.cgid != 0) {
684 DEBUG(0,("ERROR: root did not create the shmem\n"));
690 shm_size = shm_ds.shm_segsz;
692 other_processes = (shm_ds.shm_nattch > 1);
694 if (!read_only && !other_processes) {
695 memset((char *)shm_header_p, 0, shm_size);
696 shm_initialize(shm_size);
697 shm_create_hash_table(hash_size);
698 DEBUG(3,("Initialised IPC area of size %d\n", shm_size));
699 } else if (!shm_validate_header(shm_size)) {
700 /* existing file is corrupt, samba admin should remove
702 DEBUG(0,("ERROR shm_open : corrupt IPC area - remove it!\n"));
714 int ipc_dummy_procedure(void)