uclibc >=0.9.29 doesn't support threads after the call of fork in
linuxthreads mode without calling exec* first. We can now try to call
the binary again with execv to sanitise the current process and tell him
that he should not try to get into the daemon mode so we don't recursive
call batmand again.
This can fail when no linux procfs is mounted on /proc or /proc/self/exe
is a dangling symlink. In this case we continue without any changes.

Signed-off-by: Sven Eckelmann <sven.eckelm...@gmx.de>
---
 batman/posix/init.c |   28 ++++++++++++++++++++++++++--
 1 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/batman/posix/init.c b/batman/posix/init.c
index bd10438..5dbfe17 100644
--- a/batman/posix/init.c
+++ b/batman/posix/init.c
@@ -46,7 +46,28 @@ int8_t stop;
 
 
 
-static int my_daemon(void) {
+static void re_execute(int argc, char *argv[])
+{
+       char ** new_argv = NULL;
+       char* no_detach_flag = "-D";
+       int i;
+
+       new_argv = debugMalloc( sizeof(char *const) * (argc + 2), 5005 );
+       for (i = 0; i < argc; i++ ) {
+               new_argv[i] = argv[i];
+       }
+       new_argv[argc] = no_detach_flag;
+       new_argv[argc + 1] = NULL;
+
+
+       execv("/proc/self/exe", new_argv);
+
+       /* execv can fail due to different reasons
+          so we must be able to continue anyway */
+       debugFree(new_argv, 5005);
+}
+
+static int my_daemon(int argc, char *argv[]) {
 
        int fd;
 
@@ -83,6 +104,9 @@ static int my_daemon(void) {
 
        }
 
+       /* Try to restart batmand in daemon mode to be sure that it is 
sanitised */
+       re_execute(argc, argv);
+
        return 0;
 
 }
@@ -652,7 +676,7 @@ void apply_init_args( int argc, char *argv[] ) {
                /* daemonize */
                if (debug_level == 0) {
 
-                       if (no_detach == 0 && my_daemon() < 0) {
+                       if (no_detach == 0 && my_daemon(argc, argv) < 0) {
 
                                printf("Error - can't fork to background: 
%s\n", strerror(errno));
                                restore_defaults();
-- 
1.6.2.1

Reply via email to