Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / samba / source / locking / shmem_sysv.c
bloba7240789e1b7e6f8fe626e8b61aa53028cfcc556
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
23 #include "includes.h"
26 #ifdef USE_SYSV_IPC
28 extern int DEBUGLEVEL;
30 #define SHMEM_KEY ((key_t)0x280267)
31 #define SEMAPHORE_KEY (SHMEM_KEY+2)
33 #define SHM_MAGIC 0x53484100
34 #define SHM_VERSION 2
36 #ifdef SHM_R
37 #define IPC_PERMS ((SHM_R | SHM_W) | (SHM_R>>3) | (SHM_R>>6))
38 #else
39 #define IPC_PERMS 0644
40 #endif
43 #ifdef SECURE_SEMAPHORES
44 /* secure semaphores are slow because we have to do a become_root()
45 on every call! */
46 #define SEMAPHORE_PERMS IPC_PERMS
47 #else
48 #define SEMAPHORE_PERMS 0666
49 #endif
51 #define SHMEM_HASH_SIZE 13
53 #define MIN_SHM_SIZE 0x1000
55 static int shm_id;
56 static int sem_id;
57 static int shm_size;
58 static int hash_size;
59 static int global_lock_count;
61 struct ShmHeader {
62 int shm_magic;
63 int shm_version;
64 int total_size; /* in bytes */
65 BOOL consistent;
66 int first_free_off;
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
70 ShmBlockDesc) size */
71 int cells_free;
72 int cells_used;
73 int cells_system; /* number of cells used as allocated
74 block descriptors */
75 } statistics;
78 #define SHM_NOT_FREE_OFF (-1)
79 struct ShmBlockDesc
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;
96 static int read_only;
98 static BOOL sem_change(int i, int op)
100 #ifdef SECURE_SEMAPHORES
101 extern struct current_user current_user;
102 int became_root=0;
103 #endif
104 struct sembuf sb;
105 int ret;
107 if (read_only) return True;
109 #ifdef SECURE_SEMAPHORES
110 if (current_user.uid != 0) {
111 become_root(0);
112 became_root = 1;
114 #endif
116 sb.sem_num = i;
117 sb.sem_op = op;
118 sb.sem_flg = 0;
120 ret = semop(sem_id, &sb, 1);
122 if (ret != 0) {
123 DEBUG(0,("ERROR: sem_change(%d,%d) failed (%s)\n",
124 i, op, strerror(errno)));
127 #ifdef SECURE_SEMAPHORES
128 if (became_root) {
129 unbecome_root(0);
131 #endif
133 return ret == 0;
136 static BOOL global_lock(void)
138 global_lock_count++;
139 if (global_lock_count == 1)
140 return sem_change(0, -1);
141 return True;
144 static BOOL global_unlock(void)
146 global_lock_count--;
147 if (global_lock_count == 0)
148 return sem_change(0, 1);
149 return True;
152 static void *shm_offset2addr(int offset)
154 if (offset == 0 )
155 return (void *)(0);
157 if (!shm_header_p)
158 return (void *)(0);
160 return (void *)((char *)shm_header_p + offset);
163 static int shm_addr2offset(void *addr)
165 if (!addr)
166 return 0;
168 if (!shm_header_p)
169 return 0;
171 return (int)((char *)addr - (char *)shm_header_p);
175 static int shm_alloc(int size)
177 unsigned num_cells ;
178 struct ShmBlockDesc *scanner_p;
179 struct ShmBlockDesc *prev_p;
180 struct ShmBlockDesc *new_p;
181 int result_offset;
184 if (!shm_header_p) {
185 /* not mapped yet */
186 DEBUG(0,("ERROR shm_alloc : shmem not mapped\n"));
187 return 0;
190 global_lock();
192 if (!shm_header_p->consistent) {
193 DEBUG(0,("ERROR shm_alloc : shmem not consistent\n"));
194 global_unlock();
195 return 0;
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);
203 scanner_p = prev_p ;
205 /* scan the free list to find a matching free space */
206 while ((scanner_p != EOList_Addr) && (scanner_p->size < num_cells)) {
207 prev_p = scanner_p;
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
212 the list */
213 if (scanner_p == EOList_Addr) {
214 DEBUG(0,("ERROR shm_alloc : alloc of %d bytes failed\n",size));
215 global_unlock();
216 return (0);
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) {
224 /* Make a new one */
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;
238 } else {
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;
250 global_unlock();
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
263 join them */
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
281 block to free */
282 struct ShmBlockDesc *scanner_p; /* used to scan the list */
283 struct ShmBlockDesc *prev_p; /* holds previous in the
284 list */
286 if (!shm_header_p) {
287 /* not mapped yet */
288 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
289 return False;
292 global_lock();
294 if (!shm_header_p->consistent) {
295 DEBUG(0,("ERROR shm_free : shmem not consistent\n"));
296 global_unlock();
297 return False;
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));
305 global_unlock();
306 return False;
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);
314 scanner_p = prev_p ;
316 while ((scanner_p != EOList_Addr) &&
317 (scanner_p < header_p)) {
318 /* while we didn't scan past its position */
319 prev_p = scanner_p ;
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;
345 } else {
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;
357 global_unlock();
358 return 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);
370 global_lock();
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",
375 size));
376 global_unlock();
377 return False;
380 /* Clear hash buckets. */
381 memset(shm_offset2addr(shm_header_p->userdef_off), '\0', size);
382 global_unlock();
383 return True;
387 static BOOL shm_validate_header(int size)
389 if(!shm_header_p) {
390 /* not mapped yet */
391 DEBUG(0,("ERROR shm_validate_header : shmem not mapped\n"));
392 return False;
395 if(shm_header_p->shm_magic != SHM_MAGIC) {
396 DEBUG(0,("ERROR shm_validate_header : bad magic\n"));
397 return False;
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));
403 return False;
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));
409 return False;
412 if(!shm_header_p->consistent) {
413 DEBUG(0,("ERROR shmem not consistent\n"));
414 return False;
416 return True;
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 ) {
427 /* not mapped yet */
428 DEBUG(0,("ERROR shm_initialize : shmem not mapped\n"));
429 return False;
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;
449 return True;
452 static BOOL shm_close( void )
454 return True;
458 static int shm_get_userdef_off(void)
460 if (!shm_header_p)
461 return 0;
462 else
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,
488 int *bytes_used,
489 int *bytes_overhead)
491 if(!shm_header_p) {
492 /* not mapped yet */
493 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
494 return False;
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 +
500 AlignedHeaderSize;
502 return True;
506 /*******************************************************************
507 hash a number into a hash_entry
508 ******************************************************************/
509 static unsigned shm_hash_size(void)
511 return hash_size;
515 static struct shmem_ops shmops = {
516 shm_close,
517 shm_alloc,
518 shm_free,
519 shm_get_userdef_off,
520 shm_offset2addr,
521 shm_addr2offset,
522 shm_lock_hash_entry,
523 shm_unlock_hash_entry,
524 shm_get_usage,
525 shm_hash_size,
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;
537 union semun su;
538 int i;
539 pid_t pid;
540 struct passwd *root_pwd = sys_getpwuid((uid_t)0);
541 gid_t root_gid = root_pwd ? root_pwd->pw_gid : (gid_t)0;
543 read_only = ronly;
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);
551 if (sem_id == -1) {
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);
559 if (sem_id != -1 ||
560 (errno != EINVAL && errno != ENOSPC)) break;
561 hash_size -= 5;
564 if (sem_id == -1) {
565 DEBUG(0,("Can't create or use semaphore [1]. Error was %s\n",
566 strerror(errno)));
567 return NULL;
570 if (sem_id != -1) {
571 su.val = 1;
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)));
576 return NULL;
581 if (shm_id == -1) {
582 sem_id = semget(SEMAPHORE_KEY, 0, 0);
584 if (sem_id == -1) {
585 DEBUG(0,("Can't create or use semaphore [2]. Error was %s\n",
586 strerror(errno)));
587 return NULL;
590 su.buf = &sem_ds;
591 if (semctl(sem_id, 0, IPC_STAT, su) != 0) {
592 DEBUG(0,("ERROR semctl: can't IPC_STAT. Error was %s\n",
593 strerror(errno)));
594 return NULL;
596 hash_size = sem_ds.sem_nsems-1;
598 if (!read_only) {
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));
602 return NULL;
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",
608 (int)pid));
609 su.val = 1;
610 if (semctl(sem_id, 0, SETVAL, su) != 0) {
611 DEBUG(0,("ERROR: Failed to clear global lock. Error was %s\n",
612 strerror(errno)));
613 return NULL;
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",
620 strerror(errno)));
621 return NULL;
625 if (!global_lock())
626 return NULL;
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",
633 i, (int)pid));
634 su.val = 1;
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)));
638 global_unlock();
639 return NULL;
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 */
655 if (shm_id == -1) {
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);
660 if (shm_id != -1 ||
661 (errno != EINVAL && errno != ENOSPC)) break;
662 shm_size *= 0.8;
666 if (shm_id == -1) {
667 DEBUG(0,("Can't create or use IPC area. Error was %s\n", strerror(errno)));
668 global_unlock();
669 return NULL;
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)));
677 global_unlock();
678 return NULL;
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)));
687 global_unlock();
688 return NULL;
691 if (!read_only) {
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"));
694 global_unlock();
695 return NULL;
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
710 it by hand */
711 DEBUG(0,("ERROR shm_open : corrupt IPC area - remove it!\n"));
712 global_unlock();
713 return NULL;
716 global_unlock();
717 return &shmops;
722 #else
723 int ipc_dummy_procedure(void)
724 {return 0;}
725 #endif