From patchwork Tue Aug 7 14:25:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 954542 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41lH0G0FZnz9s5K for ; Wed, 8 Aug 2018 00:27:09 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 44D2E3E749A for ; Tue, 7 Aug 2018 16:27:07 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [IPv6:2001:4b78:1:20::5]) by picard.linux.it (Postfix) with ESMTP id CB6FD3E602B for ; Tue, 7 Aug 2018 16:27:04 +0200 (CEST) Received: from mx1.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-5.smtp.seeweb.it (Postfix) with ESMTPS id 3C1F0600BD9 for ; Tue, 7 Aug 2018 16:27:02 +0200 (CEST) Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 5E84EAF44; Tue, 7 Aug 2018 14:27:02 +0000 (UTC) From: Cyril Hrubis To: ltp@lists.linux.it Date: Tue, 7 Aug 2018 16:25:49 +0200 Message-Id: <20180807142549.13877-1-chrubis@suse.cz> X-Mailer: git-send-email 2.13.6 X-Virus-Scanned: clamav-milter 0.99.2 at in-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-5.smtp.seeweb.it Cc: Eddie Horng Subject: [LTP] [PATCH] lib: Fix result propagation after exec() + tst_reinit() X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" The result pointer wasn't initialized in tst_reinit(), which haven't allowed for test result propagation to the main test process and there is actually no reason not to do that. So this commit initializes the results pointer in tst_reinit() and also adds a child_needs_reinit flag to tst_test structure, which causes the library not to unlink the IPC file after it has been mapped and also inserts LTP_IPC_PATH to the environment. This commit also adds a test that also shows the usage of the API and documentation. Signed-off-by: Cyril Hrubis CC: Eddie Horng --- doc/test-writing-guidelines.txt | 40 +++++++++++++++++++++++++++++++++++ include/tst_test.h | 1 + lib/newlib_tests/.gitignore | 2 ++ lib/newlib_tests/test_exec.c | 43 ++++++++++++++++++++++++++++++++++++++ lib/newlib_tests/test_exec_child.c | 27 ++++++++++++++++++++++++ lib/tst_test.c | 7 +++---- 6 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 lib/newlib_tests/test_exec.c create mode 100644 lib/newlib_tests/test_exec_child.c diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt index b6b394590..a16972430 100644 --- a/doc/test-writing-guidelines.txt +++ b/doc/test-writing-guidelines.txt @@ -692,6 +692,46 @@ The 'tst_reap_children()' function makes the process wait for all of its children and exits with 'tst_brk(TBROK, ...)' if any of them returned a non zero exit code. +.Using tst_res() from binaries started by exec() +[source,c] +------------------------------------------------------------------------------- +/* test.c */ +#define _GNU_SOURCE +#include +#include "tst_test.h" + +static void do_test(void) +{ + char *const argv[] = {"test_exec_child", NULL}; + + execvpe(argv[0], argv, environ); + + tst_res(TBROK | TERRNO, "EXEC!"); +} + +static struct tst_test test = { + .test_all = do_test, + .child_needs_reinit = 1, +}; + +/* test_exec_child.c */ +#define TST_NO_DEFAULT_MAIN +#include "tst_test.h" + +int main(void) +{ + tst_reinit(); + tst_res(TPASS, "Child passed!"); + return 0; +} +------------------------------------------------------------------------------- + +The 'tst_res()' function can be also used from binaries started by 'exec()', +the parent test process has to set the '.child_needs_reinit' flag so that the +library prepares for it and has to make sure the 'LTP_IPC_PATH' environment +vairiable is passed down, then the very fist thing the program has to call in +'main()' is 'tst_reinit()' that sets up the IPC. + 2.2.9 Fork() and Parent-child synchronization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/include/tst_test.h b/include/tst_test.h index 90938bc29..98dacf387 100644 --- a/include/tst_test.h +++ b/include/tst_test.h @@ -127,6 +127,7 @@ struct tst_test { int format_device:1; int mount_device:1; int needs_rofs:1; + int child_needs_reinit:1; /* * If set the test function will be executed for all available * filesystems and the current filesytem type would be set in the diff --git a/lib/newlib_tests/.gitignore b/lib/newlib_tests/.gitignore index 8c0981ec8..99edc6404 100644 --- a/lib/newlib_tests/.gitignore +++ b/lib/newlib_tests/.gitignore @@ -20,3 +20,5 @@ tst_res_hexd tst_strstatus test17 tst_expiration_timer +test_exec +test_exec_child diff --git a/lib/newlib_tests/test_exec.c b/lib/newlib_tests/test_exec.c new file mode 100644 index 000000000..8aef62176 --- /dev/null +++ b/lib/newlib_tests/test_exec.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 Cyril Hrubis + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it would be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + * Assert that tst_res() from child started by exec() is propagated to the main + * test process. + * + * This test should be executed as: + * $ PATH=$PATH:$PWD ./test_exec + */ + +#define _GNU_SOURCE +#include +#include "tst_test.h" + +static void do_test(void) +{ + char *const argv[] = {"test_exec_child", NULL}; + + execvpe(argv[0], argv, environ); + + tst_res(TBROK | TERRNO, "EXEC!"); +} + +static struct tst_test test = { + .test_all = do_test, + .child_needs_reinit = 1, +}; diff --git a/lib/newlib_tests/test_exec_child.c b/lib/newlib_tests/test_exec_child.c new file mode 100644 index 000000000..696ff5be2 --- /dev/null +++ b/lib/newlib_tests/test_exec_child.c @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018 Cyril Hrubis + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it would be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define TST_NO_DEFAULT_MAIN +#include "tst_test.h" + +int main(void) +{ + tst_reinit(); + tst_res(TPASS, "Child passed!"); + return 0; +} diff --git a/lib/tst_test.c b/lib/tst_test.c index 008bcefe0..2f3d357d2 100644 --- a/lib/tst_test.c +++ b/lib/tst_test.c @@ -105,7 +105,7 @@ static void setup_ipc(void) results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0); /* Checkpoints needs to be accessible from processes started by exec() */ - if (tst_test->needs_checkpoints) { + if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) { sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path); putenv(ipc_path); } else { @@ -141,7 +141,6 @@ void tst_reinit(void) const char *path = getenv(IPC_ENV_VAR); size_t size = getpagesize(); int fd; - void *ptr; if (!path) tst_brk(TBROK, IPC_ENV_VAR" is not defined"); @@ -151,8 +150,8 @@ void tst_reinit(void) fd = SAFE_OPEN(path, O_RDWR); - ptr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - tst_futexes = (char*)ptr + sizeof(struct results); + results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + tst_futexes = (char*)results + sizeof(struct results); tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); SAFE_CLOSE(fd);