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
37 #define IPC_PERMS ((SHM_R | SHM_W) | (SHM_R>>3) | (SHM_R>>6))
39 #define IPC_PERMS 0644
43 #ifdef SECURE_SEMAPHORES
44 /* secure semaphores are slow because we have to do a become_root()
46 #define SEMAPHORE_PERMS IPC_PERMS
48 #define SEMAPHORE_PERMS 0666
52 #define SHMEM_HASH_SIZE (SEMMSL-1)
54 #define SHMEM_HASH_SIZE 63
57 #define MIN_SHM_SIZE 0x1000
63 static int global_lock_count;
68 int total_size; /* in bytes */
71 int userdef_off; /* a userdefined offset. can be used to store
72 root of tree or list */
73 struct { /* a cell is a range of bytes of sizeof(struct
77 int cells_system; /* number of cells used as allocated
82 #define SHM_NOT_FREE_OFF (-1)
85 int next; /* offset of next block in the free list or
86 SHM_NOT_FREE_OFF when block in use */
87 int size; /* user size in BlockDescSize units */
90 #define EOList_Addr NULL
91 #define EOList_Off (0)
93 #define CellSize sizeof(struct ShmBlockDesc)
95 /* HeaderSize aligned on a 8 byte boundary */
96 #define AlignedHeaderSize ((sizeof(struct ShmHeader)+7) & ~7)
98 static struct ShmHeader *shm_header_p = NULL;
99 static BOOL shm_initialize_called = False;
101 static int read_only;
103 static BOOL sem_change(int i, int op)
105 #ifdef SECURE_SEMAPHORES
106 extern struct current_user current_user;
112 if (read_only) return True;
114 #ifdef SECURE_SEMAPHORES
115 if (current_user.uid != 0) {
125 ret = semop(sem_id, &sb, 1);
128 DEBUG(0,("ERROR: sem_change(%d,%d) failed (%s)\n",
129 i, op, strerror(errno)));
132 #ifdef SECURE_SEMAPHORES
141 static BOOL global_lock(void)
144 if (global_lock_count == 1)
145 return sem_change(0, -1);
149 static BOOL global_unlock(void)
152 if (global_lock_count == 0)
153 return sem_change(0, 1);
157 static void *shm_offset2addr(int offset)
165 return (void *)((char *)shm_header_p + offset);
168 static int shm_addr2offset(void *addr)
176 return (int)((char *)addr - (char *)shm_header_p);
180 static int shm_alloc(int size)
183 struct ShmBlockDesc *scanner_p;
184 struct ShmBlockDesc *prev_p;
185 struct ShmBlockDesc *new_p;
191 DEBUG(0,("ERROR shm_alloc : shmem not mapped\n"));
197 if (!shm_header_p->consistent) {
198 DEBUG(0,("ERROR shm_alloc : shmem not consistent\n"));
203 /* calculate the number of cells */
204 num_cells = (size + (CellSize-1)) / CellSize;
206 /* set start of scan */
207 prev_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
210 /* scan the free list to find a matching free space */
211 while ((scanner_p != EOList_Addr) && (scanner_p->size < num_cells)) {
213 scanner_p = (struct ShmBlockDesc *)shm_offset2addr(scanner_p->next);
216 /* at this point scanner point to a block header or to the end of
218 if (scanner_p == EOList_Addr) {
219 DEBUG(0,("ERROR shm_alloc : alloc of %d bytes failed\n",size));
224 /* going to modify shared mem */
225 shm_header_p->consistent = False;
227 /* if we found a good one : scanner == the good one */
228 if (scanner_p->size > num_cells + 2) {
230 new_p = scanner_p + 1 + num_cells;
231 new_p->size = scanner_p->size - (num_cells + 1);
232 new_p->next = scanner_p->next;
233 scanner_p->size = num_cells;
234 scanner_p->next = shm_addr2offset(new_p);
236 shm_header_p->statistics.cells_free -= 1;
237 shm_header_p->statistics.cells_system += 1;
240 /* take it from the free list */
241 if (prev_p == scanner_p) {
242 shm_header_p->first_free_off = scanner_p->next;
244 prev_p->next = scanner_p->next;
246 shm_header_p->statistics.cells_free -= scanner_p->size;
247 shm_header_p->statistics.cells_used += scanner_p->size;
249 result_offset = shm_addr2offset(&(scanner_p[1]));
250 scanner_p->next = SHM_NOT_FREE_OFF;
252 /* end modification of shared mem */
253 shm_header_p->consistent = True;
257 DEBUG(6,("shm_alloc : allocated %d bytes at offset %d\n",
258 size,result_offset));
260 return result_offset;
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
269 if ( head_p == EOList_Addr ) return ;
270 if ( head_p->next == EOList_Off ) return ;
272 next_p = (struct ShmBlockDesc *)shm_offset2addr(head_p->next);
273 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;
283 static BOOL shm_free(int offset)
285 struct ShmBlockDesc *header_p; /* pointer to header of
287 struct ShmBlockDesc *scanner_p; /* used to scan the list */
288 struct ShmBlockDesc *prev_p; /* holds previous in the
293 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
299 if (!shm_header_p->consistent) {
300 DEBUG(0,("ERROR shm_free : shmem not consistent\n"));
305 /* make pointer to header of block */
306 header_p = ((struct ShmBlockDesc *)shm_offset2addr(offset) - 1);
308 if (header_p->next != SHM_NOT_FREE_OFF) {
309 DEBUG(0,("ERROR shm_free : bad offset (%d)\n",offset));
314 /* find a place in the free_list to put the header in */
316 /* set scanner and previous pointer to start of list */
317 prev_p = (struct ShmBlockDesc *)
318 shm_offset2addr(shm_header_p->first_free_off);
321 while ((scanner_p != EOList_Addr) &&
322 (scanner_p < header_p)) {
323 /* while we didn't scan past its position */
325 scanner_p = (struct ShmBlockDesc *)
326 shm_offset2addr(scanner_p->next);
329 shm_header_p->consistent = False;
331 DEBUG(6,("shm_free : freeing %d bytes at offset %d\n",
332 header_p->size*CellSize,offset));
334 if (scanner_p == prev_p) {
335 shm_header_p->statistics.cells_free += header_p->size;
336 shm_header_p->statistics.cells_used -= header_p->size;
338 /* we must free it at the beginning of the list */
339 shm_header_p->first_free_off = shm_addr2offset(header_p);
340 /* set the free_list_pointer to this block_header */
342 /* scanner is the one that was first in the list */
343 header_p->next = shm_addr2offset(scanner_p);
344 shm_solve_neighbors(header_p);
346 shm_header_p->consistent = True;
348 shm_header_p->statistics.cells_free += header_p->size;
349 shm_header_p->statistics.cells_used -= header_p->size;
351 prev_p->next = shm_addr2offset(header_p);
352 header_p->next = shm_addr2offset(scanner_p);
353 shm_solve_neighbors(header_p) ;
354 shm_solve_neighbors(prev_p) ;
356 shm_header_p->consistent = True;
365 * Function to create the hash table for the share mode entries. Called
366 * when smb shared memory is global locked.
368 static BOOL shm_create_hash_table(unsigned int hash_entries)
370 int size = hash_entries * sizeof(int);
373 shm_header_p->userdef_off = shm_alloc(size);
375 if(shm_header_p->userdef_off == 0) {
376 DEBUG(0,("shm_create_hash_table: Failed to create hash table of size %d\n",
382 /* Clear hash buckets. */
383 memset(shm_offset2addr(shm_header_p->userdef_off), '\0', size);
389 static BOOL shm_validate_header(int size)
393 DEBUG(0,("ERROR shm_validate_header : shmem not mapped\n"));
397 if(shm_header_p->shm_magic != SHM_MAGIC) {
398 DEBUG(0,("ERROR shm_validate_header : bad magic\n"));
402 if(shm_header_p->shm_version != SHM_VERSION) {
403 DEBUG(0,("ERROR shm_validate_header : bad version %X\n",
404 shm_header_p->shm_version));
408 if(shm_header_p->total_size != size) {
409 DEBUG(0,("ERROR shmem size mismatch (old = %d, new = %d)\n",
410 shm_header_p->total_size,size));
414 if(!shm_header_p->consistent) {
415 DEBUG(0,("ERROR shmem not consistent\n"));
422 static BOOL shm_initialize(int size)
424 struct ShmBlockDesc * first_free_block_p;
426 DEBUG(5,("shm_initialize : initializing shmem size %d\n",size));
428 if( !shm_header_p ) {
430 DEBUG(0,("ERROR shm_initialize : shmem not mapped\n"));
434 shm_header_p->shm_magic = SHM_MAGIC;
435 shm_header_p->shm_version = SHM_VERSION;
436 shm_header_p->total_size = size;
437 shm_header_p->first_free_off = AlignedHeaderSize;
438 shm_header_p->userdef_off = 0;
440 first_free_block_p = (struct ShmBlockDesc *)
441 shm_offset2addr(shm_header_p->first_free_off);
442 first_free_block_p->next = EOList_Off;
443 first_free_block_p->size =
444 (size - (AlignedHeaderSize+CellSize))/CellSize;
445 shm_header_p->statistics.cells_free = first_free_block_p->size;
446 shm_header_p->statistics.cells_used = 0;
447 shm_header_p->statistics.cells_system = 1;
449 shm_header_p->consistent = True;
451 shm_initialize_called = True;
456 static BOOL shm_close( void )
462 static int shm_get_userdef_off(void)
467 return shm_header_p->userdef_off;
471 /*******************************************************************
472 Lock a particular hash bucket entry.
473 ******************************************************************/
474 static BOOL shm_lock_hash_entry(unsigned int entry)
476 return sem_change(entry+1, -1);
479 /*******************************************************************
480 Unlock a particular hash bucket entry.
481 ******************************************************************/
482 static BOOL shm_unlock_hash_entry(unsigned int entry)
484 return sem_change(entry+1, 1);
488 /*******************************************************************
489 Gather statistics on shared memory usage.
490 ******************************************************************/
491 static BOOL shm_get_usage(int *bytes_free,
497 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
501 *bytes_free = shm_header_p->statistics.cells_free * CellSize;
502 *bytes_used = shm_header_p->statistics.cells_used * CellSize;
503 *bytes_overhead = shm_header_p->statistics.cells_system * CellSize +
510 /*******************************************************************
511 hash a number into a hash_entry
512 ******************************************************************/
513 static unsigned shm_hash_size(void)
519 static struct shmem_ops shmops = {
527 shm_unlock_hash_entry,
532 /*******************************************************************
533 open the shared memory
534 ******************************************************************/
535 struct shmem_ops *sysv_shm_open(int ronly)
537 BOOL created_new = False;
538 BOOL other_processes;
539 struct shmid_ds shm_ds;
540 struct semid_ds sem_ds;
547 shm_size = lp_shmem_size();
549 DEBUG(4,("Trying sysv shmem open of size %d\n", shm_size));
551 /* first the semaphore */
552 sem_id = semget(SEMAPHORE_KEY, 0, 0);
554 if (read_only) return NULL;
556 hash_size = SHMEM_HASH_SIZE;
558 while (hash_size > 1) {
559 sem_id = semget(SEMAPHORE_KEY, hash_size+1,
560 IPC_CREAT|IPC_EXCL| SEMAPHORE_PERMS);
562 (errno != EINVAL && errno != ENOSPC)) break;
567 DEBUG(0,("Can't create or use semaphore [1]. Error was %s\n",
574 for (i=0;i<hash_size+1;i++) {
575 if (semctl(sem_id, i, SETVAL, su) != 0) {
576 DEBUG(1,("Failed to init semaphore %d. Error was %s\n",
577 i, strerror(errno)));
584 sem_id = semget(SEMAPHORE_KEY, 0, 0);
587 DEBUG(0,("Can't create or use semaphore [2]. Error was %s\n",
593 if (semctl(sem_id, 0, IPC_STAT, su) != 0) {
594 DEBUG(0,("ERROR semctl: can't IPC_STAT. Error was %s\n",
598 hash_size = sem_ds.sem_nsems-1;
601 if (sem_ds.sem_perm.cuid != 0 || sem_ds.sem_perm.cgid != 0) {
602 DEBUG(0,("ERROR: root did not create the semaphore\n"));
606 if (semctl(sem_id, 0, GETVAL, su) == 0 &&
607 !process_exists((pid=semctl(sem_id, 0, GETPID, su)))) {
608 DEBUG(0,("WARNING: clearing global IPC lock set by dead process %d\n",
611 if (semctl(sem_id, 0, SETVAL, su) != 0) {
612 DEBUG(0,("ERROR: Failed to clear global lock. Error was %s\n",
618 sem_ds.sem_perm.mode = SEMAPHORE_PERMS;
619 if (semctl(sem_id, 0, IPC_SET, su) != 0) {
620 DEBUG(0,("ERROR shmctl : can't IPC_SET. Error was %s\n",
630 for (i=1;i<hash_size+1;i++) {
631 if (semctl(sem_id, i, GETVAL, su) == 0 &&
632 !process_exists((pid=semctl(sem_id, i, GETPID, su)))) {
633 DEBUG(1,("WARNING: clearing IPC lock %d set by dead process %d\n",
636 if (semctl(sem_id, i, SETVAL, su) != 0) {
637 DEBUG(0,("ERROR: Failed to clear IPC lock %d. Error was %s\n",
638 i, strerror(errno)));
646 * Try to use an existing key. Note that
647 * in order to use an existing key successfully
648 * size must be zero else shmget returns EINVAL.
649 * Thanks to Veselin Terzic <vterzic@systems.DHL.COM>
650 * for pointing this out.
653 shm_id = shmget(SHMEM_KEY, 0, 0);
655 /* if that failed then create one */
657 if (read_only) return NULL;
658 while (shm_size > MIN_SHM_SIZE) {
659 shm_id = shmget(SHMEM_KEY, shm_size,
660 IPC_CREAT | IPC_EXCL | IPC_PERMS);
662 (errno != EINVAL && errno != ENOSPC)) break;
665 created_new = (shm_id != -1);
669 DEBUG(0,("Can't create or use IPC area. Error was %s\n", strerror(errno)));
675 shm_header_p = (struct ShmHeader *)shmat(shm_id, 0,
676 read_only?SHM_RDONLY:0);
677 if ((long)shm_header_p == -1) {
678 DEBUG(0,("Can't attach to IPC area. Error was %s\n", strerror(errno)));
683 /* to find out if some other process is already mapping the file,
684 we use a registration file containing the processids of the file
686 if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
687 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n", strerror(errno)));
693 if (shm_ds.shm_perm.cuid != 0 || shm_ds.shm_perm.cgid != 0) {
694 DEBUG(0,("ERROR: root did not create the shmem\n"));
700 shm_size = shm_ds.shm_segsz;
702 other_processes = (shm_ds.shm_nattch > 1);
704 if (!read_only && !other_processes) {
705 memset((char *)shm_header_p, 0, shm_size);
706 shm_initialize(shm_size);
707 shm_create_hash_table(hash_size);
708 DEBUG(3,("Initialised IPC area of size %d\n", shm_size));
709 } else if (!shm_validate_header(shm_size)) {
710 /* existing file is corrupt, samba admin should remove
712 DEBUG(0,("ERROR shm_open : corrupt IPC area - remove it!\n"));
724 int ipc_dummy_procedure(void)