Skip to contentSkip to conversation

kernel oops using mq_open after fork

Xenomai-help Landau, Bracha 3 participants 4 messages Jul 19, 2006 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

I'm using xenomai 2.2 rc3 on a MPC8247, on linux 2.6.14.

I have a problem using mq_open. Please see the attached code. If I fork before the mq_open the kernel, I get a kernel oops.

Why this behaviour?

//*************************************************************** #include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/select.h>
#include <error.h>
#include <errno.h>

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/mount.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <unistd.h>
#include <mqueue.h>
#include <sys/mman.h>
pthread_t Main_Exec_thread_id;

#define MAIN_TASK_PRI 20
#define MAIN_STACK_SIZE 8192

pthread_attr_t Main_Exec_attr;
struct sched_param Main_Exec_parm;

int Main_PID = 0;
int Mains_Parent = 0;
int Child_PID = 0;

void split (void);

void *MainExecThread (void *dummy)
{

  	struct mq_attr buf;
	int perms = 0666;
	buf.mq_msgsize = 4;
	buf.mq_maxmsg = 20;
	printf ("mq_open\n");
	sleep (2);
	mq_open ("/con_to_R01", O_CREAT | O_RDWR , perms, &buf);

	pause ();

	return 0;

}

int child (void)
{

	Main_PID = getpid ();

	printf ("main pid = %08x\n", Main_PID) ;
	printf ("main parent = %08x\n", Mains_Parent);


	mlockall (MCL_CURRENT | MCL_FUTURE);

	pthread_attr_init (&Main_Exec_attr);

	pthread_attr_setinheritsched(&Main_Exec_attr, 1);
 	pthread_attr_setschedpolicy(&Main_Exec_attr, SCHED_FIFO);
	pthread_attr_setstacksize(&Main_Exec_attr, MAIN_STACK_SIZE);

Main_Exec_parm.sched_priority = MAIN_TASK_PRI; pthread_attr_setschedparam(&Main_Exec_attr, &Main_Exec_parm);

	pthread_create (&Main_Exec_thread_id, &Main_Exec_attr, (void*(*)(void*))MainExecThread, 0); 

	while (true)
		pause ();

	printf ("MAIN PAUSE RETURNED\n") ;

}

void parent (void)

{
	while (true)
		pause ();
}

void split (void)

{
	int pid = fork ();

	if (pid == 0) // child
	{
		child ();
	}
	else // parent
	{

		Child_PID = pid;
		parent ();
	}
}

int main (int argc, char *argv[])
{

	Mains_Parent = getpid ();
#if 1
	split ();

#endif

#if 0
	child ();

#endif

}
***********************************************************************************
This email message and any attachments thereto are intended only for use by the addressee(s) named above, and may contain legally privileged and/or confidential information. If the reader of this message is not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the [email protected] and destroy the original message.
***********************************************************************************

#2 lore raw
Quoted 6 lines · Bracha

I'm using xenomai 2.2 rc3 on a MPC8247, on linux 2.6.14.

I have a problem using mq_open. Please see the attached code. If I fork before the mq_open the kernel, I get a kernel oops.

Why this behaviour?

The child process has not "bound" the interface, so, it has no per-process data. If this is the cause of the oops, the solution is to add a pthread_atfork call in xeno_user_skin_init in order for the process children to also call xeno_user_skin_init.

Signature · 1 line
					    Gilles Chanteperdrix.
#3 lore raw
Quoted 6 lines · Gilles Chanteperdrix
Quoted 5 lines · Bracha

I'm using xenomai 2.2 rc3 on a MPC8247, on linux 2.6.14.

I have a problem using mq_open. Please see the attached code. If I fork before the mq_open the kernel, I get a kernel oops.

Quoted 6 lines
Quoted 1 line

Why this behaviour?

The child process has not "bound" the interface, so, it has no per-process data. If this is the cause of the oops, the solution is to add a pthread_atfork call in xeno_user_skin_init in order for the process children to also call xeno_user_skin_init.

Additionally, don't fiddle with SIGCHLD: we use it internally.

Quoted 1 line
Signature · 1 line
Philippe.
#4 lore raw
In reply to Gilles Chanteperdrix
Quoted 6 lines · Gilles Chanteperdrix
Quoted 5 lines · Bracha

I'm using xenomai 2.2 rc3 on a MPC8247, on linux 2.6.14.

I have a problem using mq_open. Please see the attached code. If I fork before the mq_open the kernel, I get a kernel oops.

Quoted 6 lines
Quoted 1 line

Why this behaviour?

The child process has not "bound" the interface, so, it has no per-process data. If this is the cause of the oops, the solution is to add a pthread_atfork call in xeno_user_skin_init in order for the process children to also call xeno_user_skin_init.

Actually, this issue is specific to skins using the per-process data feature, i.e. only the posix skin now. So, here is a fix that only fixes the posix skin initialization. If it solve your issue I will commit it. You only have to rebuild the user-space support, kernel-space support is unchanged.

Signature · 1 line
					    Gilles Chanteperdrix.
skins/posix/init.c +14 -3
Index: src/skins/posix/init.c
===================================================================
@@ -30,6 +30,7 @@
 int __pse51_muxid = -1;
 int __rtdm_muxid = -1;
 int __rtdm_fd_start = INT_MAX;
+static int fork_handler_registered;
 
 int __wrap_pthread_setschedparam(pthread_t, int, const struct sched_param *);
 
@@ -58,9 +59,9 @@
 	}
 
 	parm.sched_priority = 0;
-	if ((err =
-	     __wrap_pthread_setschedparam(pthread_self(), SCHED_OTHER,
-					  &parm))) {
+	err = __wrap_pthread_setschedparam(pthread_self(), SCHED_OTHER,
+					   &parm);
+	if (err) {
 		fprintf(stderr, "Xenomai Posix skin init: "
 			"pthread_setschedparam: %s\n", strerror(err));
 		exit(EXIT_FAILURE);
@@ -70,4 +71,14 @@
 		perror("Xenomai Posix skin init: munlockall");
 		exit(EXIT_FAILURE);
 	}
+
+	if (!fork_handler_registered) {
+		err = pthread_atfork(NULL, NULL, &__init_posix_interface);
+		if (err) {
+			fprintf(stderr, "Xenomai Posix skin init: "
+				"pthread_atfork: %s\n", strerror(err));
+			exit(EXIT_FAILURE);
+		}
+		fork_handler_registered = 1;
+	}
 }
testsuite/switchtest/Makefile.am +1 -2
Index: src/testsuite/switchtest/Makefile.am
===================================================================
@@ -9,8 +9,7 @@
 switch_LDFLAGS =  $(XENO_POSIX_WRAPPERS) $(XENO_USER_LDFLAGS)
 
 switch_LDADD = \
-	-lpthread -lrt \
-	../../skins/posix/.libs/libpthread_rt.a
+	../../skins/posix/.libs/libpthread_rt.a	-lpthread -lrt
 
 install-data-local:
 	$(mkinstalldirs) $(DESTDIR)$(testdir)
testsuite/switchtest/Makefile.in +4 -3
Index: src/testsuite/switchtest/Makefile.in
===================================================================
@@ -1,4 +1,4 @@
-# Makefile.in generated by automake 1.9.6 from Makefile.am.
+# Makefile.in generated by automake 1.9.5 from Makefile.am.
 # @configure_input@
 
 # Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
@@ -14,6 +14,8 @@
 
 @SET_MAKE@
 
+SOURCES = $(switch_SOURCES)
+
 srcdir = @srcdir@
 top_srcdir = @top_srcdir@
 VPATH = @srcdir@
@@ -229,8 +231,7 @@
 switch_CPPFLAGS = -I$(top_srcdir)/include/posix $(XENO_USER_CFLAGS) -g -I$(top_srcdir)/include
 switch_LDFLAGS = $(XENO_POSIX_WRAPPERS) $(XENO_USER_LDFLAGS)
 switch_LDADD = \
-	-lpthread -lrt \
-	../../skins/posix/.libs/libpthread_rt.a
+	../../skins/posix/.libs/libpthread_rt.a	-lpthread -lrt
 
 EXTRA_DIST = runinfo
 all: all-am
testsuite/cyclic/Makefile.am +1 -2
Index: src/testsuite/cyclic/Makefile.am
===================================================================
@@ -9,8 +9,7 @@
 cyclictest_LDFLAGS = $(XENO_POSIX_WRAPPERS) $(XENO_USER_LDFLAGS)
 
 cyclictest_LDADD = \
-	-lpthread -lrt \
-	../../skins/posix/.libs/libpthread_rt.a
+	../../skins/posix/.libs/libpthread_rt.a -lpthread -lrt
 
 install-data-local:
 	$(mkinstalldirs) $(DESTDIR)$(testdir)
testsuite/cyclic/Makefile.in +4 -3
Index: src/testsuite/cyclic/Makefile.in
===================================================================
@@ -1,4 +1,4 @@
-# Makefile.in generated by automake 1.9.6 from Makefile.am.
+# Makefile.in generated by automake 1.9.5 from Makefile.am.
 # @configure_input@
 
 # Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
@@ -14,6 +14,8 @@
 
 @SET_MAKE@
 
+SOURCES = $(cyclictest_SOURCES)
+
 srcdir = @srcdir@
 top_srcdir = @top_srcdir@
 VPATH = @srcdir@
@@ -229,8 +231,7 @@
 cyclictest_CPPFLAGS = -I$(top_srcdir)/include/posix $(XENO_USER_CFLAGS) -DIPIPE_TRACE=1 -I$(top_srcdir)/include
 cyclictest_LDFLAGS = $(XENO_POSIX_WRAPPERS) $(XENO_USER_LDFLAGS)
 cyclictest_LDADD = \
-	-lpthread -lrt \
-	../../skins/posix/.libs/libpthread_rt.a
+	../../skins/posix/.libs/libpthread_rt.a -lpthread -lrt
 
 EXTRA_DIST = runinfo
 all: all-am
testsuite/irqbench/Makefile.am +1 -2
Index: src/testsuite/irqbench/Makefile.am
===================================================================
@@ -19,8 +19,7 @@
 	$(XENO_USER_LDFLAGS)
 
 irqloop_LDADD = \
-	-lpthread \
-	../../skins/posix/.libs/libpthread_rt.a
+	../../skins/posix/.libs/libpthread_rt.a -lpthread
 
 
 irqbench_SOURCES = irqbench.c
testsuite/irqbench/Makefile.in +4 -3
Index: src/testsuite/irqbench/Makefile.in
===================================================================
@@ -1,4 +1,4 @@
-# Makefile.in generated by automake 1.9.6 from Makefile.am.
+# Makefile.in generated by automake 1.9.5 from Makefile.am.
 # @configure_input@
 
 # Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
@@ -14,6 +14,8 @@
 
 @SET_MAKE@
 
+SOURCES = $(irqbench_SOURCES) $(irqloop_SOURCES)
+
 srcdir = @srcdir@
 top_srcdir = @top_srcdir@
 VPATH = @srcdir@
@@ -241,8 +243,7 @@
 	$(XENO_USER_LDFLAGS)
 
 irqloop_LDADD = \
-	-lpthread \
-	../../skins/posix/.libs/libpthread_rt.a
+	../../skins/posix/.libs/libpthread_rt.a -lpthread
 
 irqbench_SOURCES = irqbench.c
 irqbench_CPPFLAGS = \