Module: xenomai-2.5
Branch: master
Commit: e70ce487ac7ab62cd8de28af8ddccf7309f1259d
URL:    
http://git.xenomai.org/?p=xenomai-2.5.git;a=commit;h=e70ce487ac7ab62cd8de28af8ddccf7309f1259d

Author: Jan Kiszka <jan.kis...@siemens.com>
Date:   Fri Feb 12 17:40:09 2010 +0100

Fix remapping of global sem_heap on fork

As xeno_init_sem_heaps might be called multiple times even without fork,
we must not remap the global heap here. Register an atfork handler
instead that handles this case and simple reject multiple heap creation
requests for the same process.

Signed-off-by: Jan Kiszka <jan.kis...@siemens.com>

---

 src/skins/common/sem_heap.c |   30 +++++++++++++++++++-----------
 1 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/src/skins/common/sem_heap.c b/src/skins/common/sem_heap.c
index 89f325a..723e031 100644
--- a/src/skins/common/sem_heap.c
+++ b/src/skins/common/sem_heap.c
@@ -1,6 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
-
+#include <pthread.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -65,24 +65,32 @@ static void unmap_sem_heap(unsigned long heap_addr, 
unsigned shared)
        munmap((void *) heap_addr, hinfo.size);
 }
 
+static void remap_on_fork(void)
+{
+       unmap_sem_heap(xeno_sem_heap[0], 0);
+
+       xeno_sem_heap[0] = (unsigned long) map_sem_heap(0);
+       if (xeno_sem_heap[0] == (unsigned long) MAP_FAILED) {
+               perror("Xenomai: mmap(local sem heap)");
+               exit(EXIT_FAILURE);
+       }
+}
+
 void xeno_init_sem_heaps(void)
 {
-       /* In case we forked, we need to map the new local semaphore heap */
        if (xeno_sem_heap[0])
-               unmap_sem_heap(xeno_sem_heap[0], 0);
+               return;
+
        xeno_sem_heap[0] = (unsigned long) map_sem_heap(0);
        if (xeno_sem_heap[0] == (unsigned long) MAP_FAILED) {
                perror("Xenomai: mmap(local sem heap)");
                exit(EXIT_FAILURE);
        }
+       pthread_atfork(NULL, NULL, remap_on_fork);
 
-       /* Even if we forked the global semaphore heap did not change, no need
-         to map it anew */
-       if (!xeno_sem_heap[1]) {
-               xeno_sem_heap[1] = (unsigned long) map_sem_heap(1);
-               if (xeno_sem_heap[1] == (unsigned long) MAP_FAILED) {
-                       perror("Xenomai: mmap(global sem heap)");
-                       exit(EXIT_FAILURE);
-               }
+       xeno_sem_heap[1] = (unsigned long) map_sem_heap(1);
+       if (xeno_sem_heap[1] == (unsigned long) MAP_FAILED) {
+               perror("Xenomai: mmap(global sem heap)");
+               exit(EXIT_FAILURE);
        }
 }


_______________________________________________
Xenomai-git mailing list
Xenomai-git@gna.org
https://mail.gna.org/listinfo/xenomai-git

Reply via email to