From patchwork Wed Apr 1 10:31:42 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 5381 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by open-mesh.net (8.14.3/8.13.4/Debian-3sarge3) with SMTP id n31Aeioo003482 for ; Wed, 1 Apr 2009 10:40:45 GMT Received: (qmail invoked by alias); 01 Apr 2009 10:31:47 -0000 Received: from i59F6AC37.versanet.de (EHLO localhost) [89.246.172.55] by mail.gmx.net (mp022) with SMTP; 01 Apr 2009 12:31:47 +0200 X-Authenticated: #15668376 X-Provags-ID: V01U2FsdGVkX18NUOJezi0NAHre1/PMwYF+bw5Pc1IYtF74+/wIwU QjCo8J++i5iVfU From: Sven Eckelmann To: b.a.t.m.a.n@open-mesh.net Date: Wed, 1 Apr 2009 12:31:42 +0200 Message-Id: <1238581902-21257-2-git-send-email-sven.eckelmann@gmx.de> X-Mailer: git-send-email 1.6.2.1 In-Reply-To: <200904011229.01236.sven.eckelmann@gmx.de> References: <200904011229.01236.sven.eckelmann@gmx.de> X-Y-GMX-Trusted: 0 X-FuHaFi: 0.51 Subject: [B.A.T.M.A.N.] [PATCH 2/2] Sanitise batmand process by reexecuting it X-BeenThere: b.a.t.m.a.n@open-mesh.net X-Mailman-Version: 2.1.11 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Apr 2009 10:40:45 -0000 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 --- 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();