From patchwork Fri Mar 23 11:34:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yang xu X-Patchwork-Id: 889919 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=2001:1418:10:5::2; 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=cn.fujitsu.com Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4071gW1lDwz9s0q for ; Fri, 23 Mar 2018 22:35:35 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 5BB643E7098 for ; Fri, 23 Mar 2018 12:35:32 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [217.194.8.4]) by picard.linux.it (Postfix) with ESMTP id E710E3E705B for ; Fri, 23 Mar 2018 12:35:26 +0100 (CET) Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by in-4.smtp.seeweb.it (Postfix) with ESMTP id B7F7010019D3 for ; Fri, 23 Mar 2018 12:35:24 +0100 (CET) X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="38103682" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 23 Mar 2018 19:35:03 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 0207348AE76C for ; Fri, 23 Mar 2018 19:35:01 +0800 (CST) Received: from localhost.localdomain (10.167.220.182) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.361.1; Fri, 23 Mar 2018 19:35:00 +0800 From: yang xu To: Date: Fri, 23 Mar 2018 19:34:51 +0800 Message-ID: <1521804892-5783-4-git-send-email-xuyang.jy@cn.fujitsu.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1521804892-5783-1-git-send-email-xuyang.jy@cn.fujitsu.com> References: <1521804892-5783-1-git-send-email-xuyang.jy@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.220.182] X-yoursite-MailScanner-ID: 0207348AE76C.AA76C X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: xuyang.jy@cn.fujitsu.com X-Spam-Status: No, score=-0.0 required=7.0 tests=T_RP_MATCHES_RCVD autolearn=disabled version=3.4.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH 4/5] syscalls/mkdir04: Cleanup && Convert to new API 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: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" 1) Take use of some safe macros 2 Remove temporary directory in loop Signed-off-by: yang xu --- testcases/kernel/syscalls/mkdir/mkdir04.c | 275 +++++++++--------------------- 1 file changed, 78 insertions(+), 197 deletions(-) diff --git a/testcases/kernel/syscalls/mkdir/mkdir04.c b/testcases/kernel/syscalls/mkdir/mkdir04.c index 3e0d199..562f784 100644 --- a/testcases/kernel/syscalls/mkdir/mkdir04.c +++ b/testcases/kernel/syscalls/mkdir/mkdir04.c @@ -1,241 +1,122 @@ -/* +/* Copyright (c) International Business Machines Corp., 2001 * - * Copyright (c) International Business Machines Corp., 2001 + * 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 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 will 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. * - * This program is distributed in the hope that it will 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 to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ -/* - * NAME - * mkdir04 - * - * DESCRIPTION - * - * ALGORITHM - * Setup: - * Setup signal handling. - * Pause for SIGUSR1 if option specified. - * Create temporary directory. - * - * Test: - * Loop if the proper options are given. - * fork the first child - * set to be ltpuser1 - * create a dirctory tstdir1 with 0700 permission - * fork the second child - * set to ltpuser2 - * try to create a subdirectory tstdir2 under tstdir1 - * check the returnvalue, if succeeded (return=0) - * Log the errno and Issue a FAIL message. - * Otherwise, - * Verify the errno - * if equals to EACCES, - * Issue Pass message. - * Otherwise, - * Issue Fail message. - * Cleanup: - * Print errno log and/or timing stats if options given - * Delete the temporary directory created. - * USAGE - * mkdir04 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t] - * where, -c n : Run n copies concurrently. - * -e : Turn on errno logging. - * -f : Turn off functionality Testing. - * -i n : Execute test n times. - * -I x : Execute test for x seconds. - * -P x : Pause for x seconds between iterations. - * -t : Turn on syscall timing. - * - * HISTORY - * 07/2001 Ported by Wayne Boyer - * - * RESTRICTIONS - * None. - * +/* DESCRIPTION + * fork the first child + * set to be ltpuser1 + * create a dirctory TESTDIR with 0700 permission + * fork the second child + * set to ltpuser2 + * try to create a subdirectory TESTDIR1 under tstdir1 + * check the returnvalue, if succeeded (return=0) + * Log the errno and Issue a FAIL message. + * Otherwise, + * Verify the errno + * if equals to EACCES, + * Issue Pass message. + * Otherwise, + * Issue Fail message. */ #include -#include -#include #include #include -#include #include #include #include - -#include "test.h" -#include "safe_macros.h" - -void setup(); -void cleanup(); -int fail; +#include +#include "tst_test.h" #define PERMS 0700 +#define TESTDIR "testdir" +#define TESTDIR1 "testdir/testfile" static uid_t nobody_uid, bin_uid; -char *TCID = "mkdir04"; -int TST_TOTAL = 1; -int fail; - -char tstdir1[100]; -char tstdir2[100]; - -int main(int ac, char **av) +static void verify_mkdir(void) { - int lc; - int rval; - pid_t pid, pid1; int status; + pid_t pid, pid1; - /* - * parse standard options - */ - tst_parse_opts(ac, av, NULL, NULL); - - /* - * perform global setup for test - */ - setup(); - - /* - * check looping state if -i option given - */ - for (lc = 0; TEST_LOOPING(lc); lc++) { - - tst_count = 0; + pid = SAFE_FORK(); + if (pid == 0) { + SAFE_SETREUID(nobody_uid, nobody_uid); - /* Initialize the test directories name */ - sprintf(tstdir1, "tstdir1.%d", getpid()); - if ((pid = FORK_OR_VFORK()) < 0) { - tst_brkm(TBROK, cleanup, "fork #1 failed"); + if (mkdir(TESTDIR, PERMS) == -1) { + tst_res(TFAIL | TERRNO, "mkdir(%s, %#o) Failed", + TESTDIR, PERMS); + exit(1); } - if (pid == 0) { /* first child */ - rval = setreuid(nobody_uid, nobody_uid); - if (rval < 0) { - tst_resm(TFAIL | TERRNO, "setreuid failed to " - "to set the real uid to %d and " - "effective uid to %d", - nobody_uid, nobody_uid); - exit(1); - } - /* create the parent directory with 0700 permits */ - if (mkdir(tstdir1, PERMS) == -1) { - tst_resm(TFAIL | TERRNO, - "mkdir(%s, %#o) Failed", - tstdir1, PERMS); - exit(1); - } - /* create tstdir1 succeeded */ - exit(0); - } - wait(&status); - if (WEXITSTATUS(status) != 0) { - tst_brkm(TFAIL, cleanup, - "Test to check mkdir EACCES failed" - "in create parent directory"); - } + exit(0); + } - sprintf(tstdir2, "%s/tst", tstdir1); + SAFE_WAITPID(pid, &status, 0); + if (WEXITSTATUS(status) != 0) { + tst_brk(TFAIL, "Test to check mkdir EACCES failed" + "in create parent directory"); + } - if ((pid1 = FORK_OR_VFORK()) < 0) { - tst_brkm(TBROK, cleanup, "fork #2 failed"); - } + pid1 = SAFE_FORK(); + if (pid1 == 0) { + SAFE_SETREUID(bin_uid, bin_uid); - if (pid1 == 0) { /* second child */ - rval = setreuid(bin_uid, bin_uid); - if (rval < 0) { - tst_resm(TFAIL | TERRNO, "setreuid failed to " - "to set the real uid to %d and " - "effective uid to %d", - bin_uid, bin_uid); - exit(1); - } - if (mkdir(tstdir2, PERMS) != -1) { - tst_resm(TFAIL, "mkdir(%s, %#o) unexpected " - "succeeded", tstdir2, PERMS); - exit(1); - } - if (errno != EACCES) { - tst_resm(TFAIL, "Expected EACCES got %d", - errno); - exit(1); - } - /* PASS */ - exit(0); + if (mkdir(TESTDIR1, PERMS) != -1) { + tst_res(TFAIL, "mkdir(%s, %#o) unexpected " + "succeeded", TESTDIR1, PERMS); + exit(1); } - waitpid(pid1, &status, 0); - if (WEXITSTATUS(status) == 0) { - tst_resm(TPASS, "Test to attempt to creat a directory " - "in a directory having no permissions " - "SUCCEEDED in setting errno to EACCES"); - } else { - tst_resm(TFAIL, "Test to attempt to creat a directory " - "in a directory having no permissions FAILED"); - cleanup(); + if (errno != EACCES) { + tst_res(TFAIL, "Expected EACCES got %d", errno); + exit(1); } + + exit(0); } - /* - * cleanup and exit - */ - cleanup(); - tst_exit(); + SAFE_WAITPID(pid1, &status, 0); + if (WEXITSTATUS(status) == 0) { + tst_res(TPASS, "Test to attempt to creat a directory " + "in a directory having no permissions " + "SUCCEEDED in setting errno to EACCES"); + } else { + tst_brk(TFAIL, "Test to attempt to creat a directory " + "in a directory having no permissions FAILED"); + } + /* rmdir() TESTDIR for loop test*/ + SAFE_RMDIR(TESTDIR); } -/* - * setup() - performs all ONE TIME setup for this test. - */ void setup(void) { struct passwd *pw; - tst_require_root(); - - pw = SAFE_GETPWNAM(NULL, "nobody"); + pw = SAFE_GETPWNAM("nobody"); nobody_uid = pw->pw_uid; - pw = SAFE_GETPWNAM(NULL, "bin"); + pw = SAFE_GETPWNAM("bin"); bin_uid = pw->pw_uid; - tst_sig(FORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - /* Create a temporary directory and make it current. */ - tst_tmpdir(); - umask(0); } -/* - * cleanup() - performs all ONE TIME cleanup for this test at - * completion or premature exit. - */ -void cleanup(void) -{ - - /* - * Remove the temporary directory. - */ - tst_rmdir(); - - /* - * Exit with return code appropriate for results. - */ - -} +static struct tst_test test = { + .test_all = verify_mkdir, + .needs_tmpdir = 1, + .needs_root = 1, + .setup = setup, + .forks_child = 1, +};