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
51 #define SHMEM_HASH_SIZE 13
53 #define MIN_SHM_SIZE 0x1000
59 static int global_lock_count
;
64 int total_size
; /* in bytes */
67 int userdef_off
; /* a userdefined offset. can be used to store
68 root of tree or list */
69 struct { /* a cell is a range of bytes of sizeof(struct
73 int cells_system
; /* number of cells used as allocated
78 #define SHM_NOT_FREE_OFF (-1)
81 int next
; /* offset of next block in the free list or
82 SHM_NOT_FREE_OFF when block in use */
83 int size
; /* user size in BlockDescSize units */
86 #define EOList_Addr NULL
87 #define EOList_Off (0)
89 #define CellSize sizeof(struct ShmBlockDesc)
91 /* HeaderSize aligned on a 8 byte boundary */
92 #define AlignedHeaderSize ((sizeof(struct ShmHeader)+7) & ~7)
94 static struct ShmHeader
*shm_header_p
= NULL
;
98 static BOOL
sem_change(int i
, int op
)
100 #ifdef SECURE_SEMAPHORES
101 extern struct current_user current_user
;
107 if (read_only
) return True
;
109 #ifdef SECURE_SEMAPHORES
110 if (current_user
.uid
!= 0) {
120 ret
= semop(sem_id
, &sb
, 1);
123 DEBUG(0,("ERROR: sem_change(%d,%d) failed (%s)\n",
124 i
, op
, strerror(errno
)));
127 #ifdef SECURE_SEMAPHORES
136 static BOOL
global_lock(void)
139 if (global_lock_count
== 1)
140 return sem_change(0, -1);
144 static BOOL
global_unlock(void)
147 if (global_lock_count
== 0)
148 return sem_change(0, 1);
152 static void *shm_offset2addr(int offset
)
160 return (void *)((char *)shm_header_p
+ offset
);
163 static int shm_addr2offset(void *addr
)
171 return (int)((char *)addr
- (char *)shm_header_p
);
175 static int shm_alloc(int size
)
178 struct ShmBlockDesc
*scanner_p
;
179 struct ShmBlockDesc
*prev_p
;
180 struct ShmBlockDesc
*new_p
;
186 DEBUG(0,("ERROR shm_alloc : shmem not mapped\n"));
192 if (!shm_header_p
->consistent
) {
193 DEBUG(0,("ERROR shm_alloc : shmem not consistent\n"));
198 /* calculate the number of cells */
199 num_cells
= (size
+ (CellSize
-1)) / CellSize
;
201 /* set start of scan */
202 prev_p
= (struct ShmBlockDesc
*)shm_offset2addr(shm_header_p
->first_free_off
);
205 /* scan the free list to find a matching free space */
206 while ((scanner_p
!= EOList_Addr
) && (scanner_p
->size
< num_cells
)) {
208 scanner_p
= (struct ShmBlockDesc
*)shm_offset2addr(scanner_p
->next
);
211 /* at this point scanner point to a block header or to the end of
213 if (scanner_p
== EOList_Addr
) {
214 DEBUG(0,("ERROR shm_alloc : alloc of %d bytes failed\n",size
));
219 /* going to modify shared mem */
220 shm_header_p
->consistent
= False
;
222 /* if we found a good one : scanner == the good one */
223 if (scanner_p
->size
> num_cells
+ 2) {
225 new_p
= scanner_p
+ 1 + num_cells
;
226 new_p
->size
= scanner_p
->size
- (num_cells
+ 1);
227 new_p
->next
= scanner_p
->next
;
228 scanner_p
->size
= num_cells
;
229 scanner_p
->next
= shm_addr2offset(new_p
);
231 shm_header_p
->statistics
.cells_free
-= 1;
232 shm_header_p
->statistics
.cells_system
+= 1;
235 /* take it from the free list */
236 if (prev_p
== scanner_p
) {
237 shm_header_p
->first_free_off
= scanner_p
->next
;
239 prev_p
->next
= scanner_p
->next
;
241 shm_header_p
->statistics
.cells_free
-= scanner_p
->size
;
242 shm_header_p
->statistics
.cells_used
+= scanner_p
->size
;
244 result_offset
= shm_addr2offset(&(scanner_p
[1]));
245 scanner_p
->next
= SHM_NOT_FREE_OFF
;
247 /* end modification of shared mem */
248 shm_header_p
->consistent
= True
;
252 DEBUG(6,("shm_alloc : allocated %d bytes at offset %d\n",
253 size
,result_offset
));
255 return result_offset
;
258 static void shm_solve_neighbors(struct ShmBlockDesc
*head_p
)
260 struct ShmBlockDesc
*next_p
;
262 /* Check if head_p and head_p->next are neighbors and if so
264 if ( head_p
== EOList_Addr
) return ;
265 if ( head_p
->next
== EOList_Off
) return ;
267 next_p
= (struct ShmBlockDesc
*)shm_offset2addr(head_p
->next
);
268 if ((head_p
+ head_p
->size
+ 1) == next_p
) {
269 head_p
->size
+= next_p
->size
+ 1; /* adapt size */
270 head_p
->next
= next_p
->next
; /* link out */
272 shm_header_p
->statistics
.cells_free
+= 1;
273 shm_header_p
->statistics
.cells_system
-= 1;
278 static BOOL
shm_free(int offset
)
280 struct ShmBlockDesc
*header_p
; /* pointer to header of
282 struct ShmBlockDesc
*scanner_p
; /* used to scan the list */
283 struct ShmBlockDesc
*prev_p
; /* holds previous in the
288 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
294 if (!shm_header_p
->consistent
) {
295 DEBUG(0,("ERROR shm_free : shmem not consistent\n"));
300 /* make pointer to header of block */
301 header_p
= ((struct ShmBlockDesc
*)shm_offset2addr(offset
) - 1);
303 if (header_p
->next
!= SHM_NOT_FREE_OFF
) {
304 DEBUG(0,("ERROR shm_free : bad offset (%d)\n",offset
));
309 /* find a place in the free_list to put the header in */
311 /* set scanner and previous pointer to start of list */
312 prev_p
= (struct ShmBlockDesc
*)
313 shm_offset2addr(shm_header_p
->first_free_off
);
316 while ((scanner_p
!= EOList_Addr
) &&
317 (scanner_p
< header_p
)) {
318 /* while we didn't scan past its position */
320 scanner_p
= (struct ShmBlockDesc
*)
321 shm_offset2addr(scanner_p
->next
);
324 shm_header_p
->consistent
= False
;
326 DEBUG(6,("shm_free : freeing %d bytes at offset %d\n",
327 (int)(header_p
->size
*CellSize
),(int)offset
));
329 /* zero the area being freed - this allows us to find bugs faster */
330 memset(shm_offset2addr(offset
), 0, header_p
->size
*CellSize
);
332 if (scanner_p
== prev_p
) {
333 shm_header_p
->statistics
.cells_free
+= header_p
->size
;
334 shm_header_p
->statistics
.cells_used
-= header_p
->size
;
336 /* we must free it at the beginning of the list */
337 shm_header_p
->first_free_off
= shm_addr2offset(header_p
);
338 /* set the free_list_pointer to this block_header */
340 /* scanner is the one that was first in the list */
341 header_p
->next
= shm_addr2offset(scanner_p
);
342 shm_solve_neighbors(header_p
);
344 shm_header_p
->consistent
= True
;
346 shm_header_p
->statistics
.cells_free
+= header_p
->size
;
347 shm_header_p
->statistics
.cells_used
-= header_p
->size
;
349 prev_p
->next
= shm_addr2offset(header_p
);
350 header_p
->next
= shm_addr2offset(scanner_p
);
351 shm_solve_neighbors(header_p
) ;
352 shm_solve_neighbors(prev_p
) ;
354 shm_header_p
->consistent
= True
;
363 * Function to create the hash table for the share mode entries. Called
364 * when smb shared memory is global locked.
366 static BOOL
shm_create_hash_table(unsigned int hash_entries
)
368 int size
= hash_entries
* sizeof(int);
371 shm_header_p
->userdef_off
= shm_alloc(size
);
373 if(shm_header_p
->userdef_off
== 0) {
374 DEBUG(0,("shm_create_hash_table: Failed to create hash table of size %d\n",
380 /* Clear hash buckets. */
381 memset(shm_offset2addr(shm_header_p
->userdef_off
), '\0', size
);
387 static BOOL
shm_validate_header(int size
)
391 DEBUG(0,("ERROR shm_validate_header : shmem not mapped\n"));
395 if(shm_header_p
->shm_magic
!= SHM_MAGIC
) {
396 DEBUG(0,("ERROR shm_validate_header : bad magic\n"));
400 if(shm_header_p
->shm_version
!= SHM_VERSION
) {
401 DEBUG(0,("ERROR shm_validate_header : bad version %X\n",
402 shm_header_p
->shm_version
));
406 if(shm_header_p
->total_size
!= size
) {
407 DEBUG(0,("ERROR shmem size mismatch (old = %d, new = %d)\n",
408 shm_header_p
->total_size
,size
));
412 if(!shm_header_p
->consistent
) {
413 DEBUG(0,("ERROR shmem not consistent\n"));
420 static BOOL
shm_initialize(int size
)
422 struct ShmBlockDesc
* first_free_block_p
;
424 DEBUG(5,("shm_initialize : initializing shmem size %d\n",size
));
426 if( !shm_header_p
) {
428 DEBUG(0,("ERROR shm_initialize : shmem not mapped\n"));
432 shm_header_p
->shm_magic
= SHM_MAGIC
;
433 shm_header_p
->shm_version
= SHM_VERSION
;
434 shm_header_p
->total_size
= size
;
435 shm_header_p
->first_free_off
= AlignedHeaderSize
;
436 shm_header_p
->userdef_off
= 0;
438 first_free_block_p
= (struct ShmBlockDesc
*)
439 shm_offset2addr(shm_header_p
->first_free_off
);
440 first_free_block_p
->next
= EOList_Off
;
441 first_free_block_p
->size
=
442 (size
- (AlignedHeaderSize
+CellSize
))/CellSize
;
443 shm_header_p
->statistics
.cells_free
= first_free_block_p
->size
;
444 shm_header_p
->statistics
.cells_used
= 0;
445 shm_header_p
->statistics
.cells_system
= 1;
447 shm_header_p
->consistent
= True
;
452 static BOOL
shm_close( void )
458 static int shm_get_userdef_off(void)
463 return shm_header_p
->userdef_off
;
467 /*******************************************************************
468 Lock a particular hash bucket entry.
469 ******************************************************************/
470 static BOOL
shm_lock_hash_entry(unsigned int entry
)
472 return sem_change(entry
+1, -1);
475 /*******************************************************************
476 Unlock a particular hash bucket entry.
477 ******************************************************************/
478 static BOOL
shm_unlock_hash_entry(unsigned int entry
)
480 return sem_change(entry
+1, 1);
484 /*******************************************************************
485 Gather statistics on shared memory usage.
486 ******************************************************************/
487 static BOOL
shm_get_usage(int *bytes_free
,
493 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
497 *bytes_free
= shm_header_p
->statistics
.cells_free
* CellSize
;
498 *bytes_used
= shm_header_p
->statistics
.cells_used
* CellSize
;
499 *bytes_overhead
= shm_header_p
->statistics
.cells_system
* CellSize
+
506 /*******************************************************************
507 hash a number into a hash_entry
508 ******************************************************************/
509 static unsigned shm_hash_size(void)
515 static struct shmem_ops shmops
= {
523 shm_unlock_hash_entry
,
528 /*******************************************************************
529 open the shared memory
530 ******************************************************************/
532 struct shmem_ops
*sysv_shm_open(int ronly
)
534 BOOL other_processes
;
535 struct shmid_ds shm_ds
;
536 struct semid_ds sem_ds
;
540 struct passwd
*root_pwd
= sys_getpwuid((uid_t
)0);
541 gid_t root_gid
= root_pwd
? root_pwd
->pw_gid
: (gid_t
)0;
545 shm_size
= lp_shmem_size();
547 DEBUG(4,("Trying sysv shmem open of size %d\n", shm_size
));
549 /* first the semaphore */
550 sem_id
= semget(SEMAPHORE_KEY
, 0, 0);
552 if (read_only
) return NULL
;
554 hash_size
= SHMEM_HASH_SIZE
;
556 while (hash_size
> 1) {
557 sem_id
= semget(SEMAPHORE_KEY
, hash_size
+1,
558 IPC_CREAT
|IPC_EXCL
| SEMAPHORE_PERMS
);
560 (errno
!= EINVAL
&& errno
!= ENOSPC
)) break;
565 DEBUG(0,("Can't create or use semaphore [1]. Error was %s\n",
572 for (i
=0;i
<hash_size
+1;i
++) {
573 if (semctl(sem_id
, i
, SETVAL
, su
) != 0) {
574 DEBUG(1,("Failed to init semaphore %d. Error was %s\n",
575 i
, strerror(errno
)));
582 sem_id
= semget(SEMAPHORE_KEY
, 0, 0);
585 DEBUG(0,("Can't create or use semaphore [2]. Error was %s\n",
591 if (semctl(sem_id
, 0, IPC_STAT
, su
) != 0) {
592 DEBUG(0,("ERROR semctl: can't IPC_STAT. Error was %s\n",
596 hash_size
= sem_ds
.sem_nsems
-1;
599 if (sem_ds
.sem_perm
.cuid
!= 0 || ((sem_ds
.sem_perm
.cgid
!= root_gid
) && (sem_ds
.sem_perm
.cgid
!= 0))) {
600 DEBUG(0,("ERROR: root did not create the semaphore: semgid=%u, rootgid=%u\n",
601 (unsigned int)sem_ds
.sem_perm
.cgid
, (unsigned int)root_gid
));
605 if (semctl(sem_id
, 0, GETVAL
, su
) == 0 &&
606 !process_exists((pid
=(pid_t
)semctl(sem_id
, 0, GETPID
, su
)))) {
607 DEBUG(0,("WARNING: clearing global IPC lock set by dead process %d\n",
610 if (semctl(sem_id
, 0, SETVAL
, su
) != 0) {
611 DEBUG(0,("ERROR: Failed to clear global lock. Error was %s\n",
617 sem_ds
.sem_perm
.mode
= SEMAPHORE_PERMS
;
618 if (semctl(sem_id
, 0, IPC_SET
, su
) != 0) {
619 DEBUG(0,("ERROR shmctl : can't IPC_SET. Error was %s\n",
629 for (i
=1;i
<hash_size
+1;i
++) {
630 if (semctl(sem_id
, i
, GETVAL
, su
) == 0 &&
631 !process_exists((pid
=(pid_t
)semctl(sem_id
, i
, GETPID
, su
)))) {
632 DEBUG(1,("WARNING: clearing IPC lock %d set by dead process %d\n",
635 if (semctl(sem_id
, i
, SETVAL
, su
) != 0) {
636 DEBUG(0,("ERROR: Failed to clear IPC lock %d. Error was %s\n",
637 i
, strerror(errno
)));
645 * Try to use an existing key. Note that
646 * in order to use an existing key successfully
647 * size must be zero else shmget returns EINVAL.
648 * Thanks to Veselin Terzic <vterzic@systems.DHL.COM>
649 * for pointing this out.
652 shm_id
= shmget(SHMEM_KEY
, 0, 0);
654 /* if that failed then create one */
656 if (read_only
) return NULL
;
657 while (shm_size
> MIN_SHM_SIZE
) {
658 shm_id
= shmget(SHMEM_KEY
, shm_size
,
659 IPC_CREAT
| IPC_EXCL
| IPC_PERMS
);
661 (errno
!= EINVAL
&& errno
!= ENOSPC
)) break;
667 DEBUG(0,("Can't create or use IPC area. Error was %s\n", strerror(errno
)));
673 shm_header_p
= (struct ShmHeader
*)shmat(shm_id
, 0,
674 read_only
?SHM_RDONLY
:0);
675 if ((long)shm_header_p
== -1) {
676 DEBUG(0,("Can't attach to IPC area. Error was %s\n", strerror(errno
)));
682 * Get information on what process created the shared memory segment.
685 if (shmctl(shm_id
, IPC_STAT
, &shm_ds
) != 0) {
686 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n", strerror(errno
)));
692 if (shm_ds
.shm_perm
.cuid
!= 0 || ((shm_ds
.shm_perm
.cgid
!= root_gid
) && (shm_ds
.shm_perm
.cgid
!= 0))) {
693 DEBUG(0,("ERROR: root did not create the shmem\n"));
699 shm_size
= shm_ds
.shm_segsz
;
701 other_processes
= (shm_ds
.shm_nattch
> 1);
703 if (!read_only
&& !other_processes
) {
704 memset((char *)shm_header_p
, 0, shm_size
);
705 shm_initialize(shm_size
);
706 shm_create_hash_table(hash_size
);
707 DEBUG(3,("Initialised IPC area of size %d\n", shm_size
));
708 } else if (!shm_validate_header(shm_size
)) {
709 /* existing file is corrupt, samba admin should remove
711 DEBUG(0,("ERROR shm_open : corrupt IPC area - remove it!\n"));
723 int ipc_dummy_procedure(void)