first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[bbaumbach/samba-autobuild/.git] / source3 / locking / shmem_sysv.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Shared memory functions - SYSV IPC implementation
5    Copyright (C) Andrew Tridgell 1997-1998
6    
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.
11    
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.
16    
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.
20
21 */
22
23 #include "includes.h"
24
25
26 #ifdef USE_SYSV_IPC
27
28 extern int DEBUGLEVEL;
29
30 #define SHMEM_KEY ((key_t)0x280267)
31 #define SEMAPHORE_KEY (SHMEM_KEY+2)
32
33 #define SHM_MAGIC 0x53484100
34 #define SHM_VERSION 2
35
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
41
42
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
50
51 #define SHMEM_HASH_SIZE 13
52
53 #define MIN_SHM_SIZE 0x1000
54
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;
60
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;
76 };
77
78 #define SHM_NOT_FREE_OFF (-1)
79 struct ShmBlockDesc
80 {
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 */
84 };
85
86 #define EOList_Addr     NULL
87 #define EOList_Off      (0)
88
89 #define CellSize        sizeof(struct ShmBlockDesc)
90
91 /* HeaderSize aligned on a 8 byte boundary */
92 #define AlignedHeaderSize ((sizeof(struct ShmHeader)+7) & ~7)
93
94 static struct ShmHeader *shm_header_p = NULL;
95
96 static int read_only;
97
98 static BOOL sem_change(int i, int op)
99 {
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;
106
107         if (read_only) return True;
108
109 #ifdef SECURE_SEMAPHORES
110         if (current_user.uid != 0) {
111                 become_root(0);
112                 became_root = 1;
113         }
114 #endif
115
116         sb.sem_num = i;
117         sb.sem_op = op;
118         sb.sem_flg = 0;
119
120         ret = semop(sem_id, &sb, 1);
121
122         if (ret != 0) {
123                 DEBUG(0,("ERROR: sem_change(%d,%d) failed (%s)\n", 
124                          i, op, strerror(errno)));
125         }
126
127 #ifdef SECURE_SEMAPHORES
128         if (became_root) {
129                 unbecome_root(0);
130         }
131 #endif
132
133         return ret == 0;
134 }
135
136 static BOOL global_lock(void)
137 {
138         global_lock_count++;
139         if (global_lock_count == 1)
140                 return sem_change(0, -1);
141         return True;
142 }
143
144 static BOOL global_unlock(void)
145 {
146         global_lock_count--;
147         if (global_lock_count == 0)
148                 return sem_change(0, 1);
149         return True;
150 }
151
152 static void *shm_offset2addr(int offset)
153 {
154    if (offset == 0 )
155       return (void *)(0);
156    
157    if (!shm_header_p)
158       return (void *)(0);
159    
160    return (void *)((char *)shm_header_p + offset);
161 }
162
163 static int shm_addr2offset(void *addr)
164 {
165    if (!addr)
166       return 0;
167    
168    if (!shm_header_p)
169       return 0;
170    
171    return (int)((char *)addr - (char *)shm_header_p);
172 }
173
174
175 static int shm_alloc(int size)
176 {
177         unsigned num_cells ;
178         struct ShmBlockDesc *scanner_p;
179         struct ShmBlockDesc *prev_p;
180         struct ShmBlockDesc *new_p;
181         int result_offset;
182    
183    
184         if (!shm_header_p) {
185                 /* not mapped yet */
186                 DEBUG(0,("ERROR shm_alloc : shmem not mapped\n"));
187                 return 0;
188         }
189         
190         global_lock();
191         
192         if (!shm_header_p->consistent) {
193                 DEBUG(0,("ERROR shm_alloc : shmem not consistent\n"));
194                 global_unlock();
195                 return 0;
196         }
197         
198         /* calculate the number of cells */
199         num_cells = (size + (CellSize-1)) / CellSize;
200         
201         /* set start of scan */
202         prev_p = (struct ShmBlockDesc *)shm_offset2addr(shm_header_p->first_free_off);
203         scanner_p =     prev_p ;
204         
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);
209         }
210    
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);
217         }
218    
219         /* going to modify shared mem */
220         shm_header_p->consistent = False;
221         
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);
230
231                 shm_header_p->statistics.cells_free -= 1;
232                 shm_header_p->statistics.cells_system += 1;
233         }
234
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;
240         }
241         shm_header_p->statistics.cells_free -= scanner_p->size;
242         shm_header_p->statistics.cells_used += scanner_p->size;
243
244         result_offset = shm_addr2offset(&(scanner_p[1]));
245         scanner_p->next = SHM_NOT_FREE_OFF;
246
247         /* end modification of shared mem */
248         shm_header_p->consistent = True;
249
250         global_unlock();
251         
252         DEBUG(6,("shm_alloc : allocated %d bytes at offset %d\n",
253                  size,result_offset));
254
255         return result_offset;
256 }   
257
258 static void shm_solve_neighbors(struct ShmBlockDesc *head_p )
259 {
260         struct ShmBlockDesc *next_p;
261    
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 ;
266    
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 */
271       
272                 shm_header_p->statistics.cells_free += 1;
273                 shm_header_p->statistics.cells_system -= 1;
274         }
275 }
276
277
278 static BOOL shm_free(int offset)
279 {
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 */
285    
286         if (!shm_header_p) {
287                 /* not mapped yet */
288                 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
289                 return False;
290         }
291         
292         global_lock();
293         
294         if (!shm_header_p->consistent) {
295                 DEBUG(0,("ERROR shm_free : shmem not consistent\n"));
296                 global_unlock();
297                 return False;
298         }
299         
300         /* make pointer to header of block */
301         header_p = ((struct ShmBlockDesc *)shm_offset2addr(offset) - 1); 
302         
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;
307         }
308         
309         /* find a place in the free_list to put the header in */
310         
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 ;
315         
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);
322         }
323         
324         shm_header_p->consistent = False;
325         
326         DEBUG(6,("shm_free : freeing %d bytes at offset %d\n",
327                  (int)(header_p->size*CellSize),(int)offset));
328
329         /* zero the area being freed - this allows us to find bugs faster */
330         memset(shm_offset2addr(offset), 0, header_p->size*CellSize);
331         
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;
335                 
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 */
339                 
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); 
343                 
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;
348                 
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) ;
353            
354                 shm_header_p->consistent = True;
355         }
356
357         global_unlock();
358         return True;
359 }
360
361
362 /* 
363  * Function to create the hash table for the share mode entries. Called
364  * when smb shared memory is global locked.
365  */
366 static BOOL shm_create_hash_table(unsigned int hash_entries)
367 {
368         int size = hash_entries * sizeof(int);
369
370         global_lock();
371         shm_header_p->userdef_off = shm_alloc(size);
372
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;
378         }
379
380         /* Clear hash buckets. */
381         memset(shm_offset2addr(shm_header_p->userdef_off), '\0', size);
382         global_unlock();
383         return True;
384 }
385
386
387 static BOOL shm_validate_header(int size)
388 {
389         if(!shm_header_p) {
390                 /* not mapped yet */
391                 DEBUG(0,("ERROR shm_validate_header : shmem not mapped\n"));
392                 return False;
393         }
394    
395         if(shm_header_p->shm_magic != SHM_MAGIC) {
396                 DEBUG(0,("ERROR shm_validate_header : bad magic\n"));
397                 return False;
398         }
399
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;
404         }
405    
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;
410         }
411
412         if(!shm_header_p->consistent) {
413                 DEBUG(0,("ERROR shmem not consistent\n"));
414                 return False;
415         }
416         return True;
417 }
418
419
420 static BOOL shm_initialize(int size)
421 {
422         struct ShmBlockDesc * first_free_block_p;
423         
424         DEBUG(5,("shm_initialize : initializing shmem size %d\n",size));
425    
426         if( !shm_header_p ) {
427                 /* not mapped yet */
428                 DEBUG(0,("ERROR shm_initialize : shmem not mapped\n"));
429                 return False;
430         }
431    
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;
437         
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;
446    
447         shm_header_p->consistent = True;
448    
449         return True;
450 }
451    
452 static BOOL shm_close( void )
453 {
454         return True;
455 }
456
457
458 static int shm_get_userdef_off(void)
459 {
460    if (!shm_header_p)
461       return 0;
462    else
463       return shm_header_p->userdef_off;
464 }
465
466
467 /*******************************************************************
468   Lock a particular hash bucket entry.
469   ******************************************************************/
470 static BOOL shm_lock_hash_entry(unsigned int entry)
471 {
472         return sem_change(entry+1, -1);
473 }
474
475 /*******************************************************************
476   Unlock a particular hash bucket entry.
477   ******************************************************************/
478 static BOOL shm_unlock_hash_entry(unsigned int entry)
479 {
480         return sem_change(entry+1, 1);
481 }
482
483
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)
490 {
491         if(!shm_header_p) {
492                 /* not mapped yet */
493                 DEBUG(0,("ERROR shm_free : shmem not mapped\n"));
494                 return False;
495         }
496
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;
501         
502         return True;
503 }
504
505
506 /*******************************************************************
507 hash a number into a hash_entry
508   ******************************************************************/
509 static unsigned shm_hash_size(void)
510 {
511         return hash_size;
512 }
513
514
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,
526 };
527
528 /*******************************************************************
529   open the shared memory
530   ******************************************************************/
531
532 struct shmem_ops *sysv_shm_open(int ronly)
533 {
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;
542
543         read_only = ronly;
544
545         shm_size = lp_shmem_size();
546
547         DEBUG(4,("Trying sysv shmem open of size %d\n", shm_size));
548
549         /* first the semaphore */
550         sem_id = semget(SEMAPHORE_KEY, 0, 0);
551         if (sem_id == -1) {
552                 if (read_only) return NULL;
553
554                 hash_size = SHMEM_HASH_SIZE;
555
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;
562                 }
563
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;
568                 }   
569
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;
577                                 }
578                         }
579                 }
580         }
581         if (shm_id == -1) {
582                 sem_id = semget(SEMAPHORE_KEY, 0, 0);
583         }
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;
588         }   
589
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;
595         }
596         hash_size = sem_ds.sem_nsems-1;
597
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;
603                 }
604
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;
614                         }
615                 }
616
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;
622                 }
623         }
624
625         if (!global_lock())
626                 return NULL;
627
628
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;
640                         }
641                 }
642         }
643         
644         /* 
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.
650      */
651
652         shm_id = shmget(SHMEM_KEY, 0, 0);
653         
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;
663                 }
664         }
665         
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;
670         }   
671         
672         
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;
679         }
680
681         /* to find out if some other process is already mapping the file,
682            we use a registration file containing the processids of the file
683            mapping processes */
684         if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
685                 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n", strerror(errno)));
686         global_unlock();
687         return NULL;
688         }
689
690         if (!read_only) {
691                 if (shm_ds.shm_perm.cuid != 0 || ((shm_ds.shm_perm.cgid != root_gid) && (shm_ds.shm_perm.cgid != 0))) {
692                         DEBUG(0,("ERROR: root did not create the shmem\n"));
693                         global_unlock();
694                         return NULL;
695                 }
696         }
697
698         shm_size = shm_ds.shm_segsz;
699
700         other_processes = (shm_ds.shm_nattch > 1);
701
702         if (!read_only && !other_processes) {
703                 memset((char *)shm_header_p, 0, shm_size);
704                 shm_initialize(shm_size);
705                 shm_create_hash_table(hash_size);
706                 DEBUG(3,("Initialised IPC area of size %d\n", shm_size));
707         } else if (!shm_validate_header(shm_size)) {
708                 /* existing file is corrupt, samba admin should remove
709                    it by hand */
710                 DEBUG(0,("ERROR shm_open : corrupt IPC area - remove it!\n"));
711                 global_unlock();
712                 return NULL;
713         }
714    
715         global_unlock();
716         return &shmops;
717 }
718
719
720
721 #else 
722  int ipc_dummy_procedure(void)
723 {return 0;}
724 #endif