Skip to contentSkip to conversation

syscalls/link: Convert to new API and merge link06 and link07 into link04

PATCH In review tangmeng 2 participants 4 messages Nov 3, 2021 – Nov 4, 2021

lore
Share & tools
lore

tangmeng

author
#0 lore raw

PATCH v4 1/2 syscalls/link: Convert to new API and merge link06 and link07 into link04

---
 runtest/syscalls                        |   2 -
 testcases/kernel/syscalls/link/link04.c | 208 +++++++++++-------------
 testcases/kernel/syscalls/link/link06.c | 113 -------------
 testcases/kernel/syscalls/link/link07.c | 118 --------------
 4 files changed, 93 insertions(+), 348 deletions(-)
 delete mode 100644 testcases/kernel/syscalls/link/link06.c
 delete mode 100644 testcases/kernel/syscalls/link/link07.c
runtest/syscalls +0 -2
index b19316805..1e6d46744 100644
@@ -673,8 +673,6 @@ link02 link02
 link03 link03
 link04 link04
 link05 link05
-link06 link06
-link07 link07
 link08 link08
 
 #linkat test cases
testcases/kernel/syscalls/link/link04.c +93 -115

250 lines not on this page · raw message

testcases/kernel/syscalls/link/link06.c deleted +0 -113

113 lines not on this page · raw message

testcases/kernel/syscalls/link/link07.c deleted +0 -118

118 lines not on this page · raw message

--
2.20.1

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#1 lore raw

PATCH v4 2/2 link/link05: Convert to new API

---
 testcases/kernel/syscalls/link/link05.c | 159 ++++++------------------
 1 file changed, 37 insertions(+), 122 deletions(-)
testcases/kernel/syscalls/link/link05.c +37 -122
index 5981ae62f..28d6079f5 100644
@@ -1,161 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
  *  AUTHOR		: Richard Logan
  *  CO-PILOT		: William Roske
  * Copyright (c) 2014 Cyril Hrubis <[email protected]>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * 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.
- *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
  */
 
 /*
  * Test if link(2) fails with EMLINK.
  */
 
-#include <sys/types.h>
-#include <fcntl.h>
+#include <stdio.h>
 #include <sys/stat.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include "test.h"
-#include "safe_macros.h"
-
-static void setup(void);
-static void cleanup(void);
-static void help(void);
-
-char *TCID = "link05";
-int TST_TOTAL = 1;
+#include "tst_test.h"
 
 #define BASENAME	"lkfile"
 
 static char fname[255];
 
-static char *links_arg;
-
-option_t options[] = {
-	{"N:", NULL, &links_arg},
-	{NULL, NULL, NULL}
-};
-
 static int nlinks = 1000;
 
-int main(int ac, char **av)
+static void verify_link(void)
 {
-	int lc;
 	struct stat fbuf, lbuf;
 	int cnt;
 	char lname[255];
 
-	tst_parse_opts(ac, av, options, &help);
-
-	if (links_arg) {
-		nlinks = atoi(links_arg);
-
-		if (nlinks == 0) {
-			tst_brkm(TBROK, NULL,
-			         "nlinks is not a positive number");
-		}
+	for (cnt = 1; cnt < nlinks; cnt++) {
+		sprintf(lname, "%s%d", fname, cnt);
+		TST_EXP_PASS(link(fname, lname), "link(%s, %s)", fname, lname);
 	}
 
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		for (cnt = 1; cnt < nlinks; cnt++) {
-			sprintf(lname, "%s%d", fname, cnt);
-			TEST(link(fname, lname));
-
-			if (TEST_RETURN == -1) {
-				tst_resm(TFAIL,
-					 "link(%s, %s) Failed, errno=%d : %s",
-					 fname, lname, TEST_ERRNO,
-					 strerror(TEST_ERRNO));
-			}
-		}
-
-		SAFE_STAT(cleanup, fname, &fbuf);
+	SAFE_STAT(fname, &fbuf);
 
-		for (cnt = 1; cnt < nlinks; cnt++) {
-			sprintf(lname, "%s%d", fname, cnt);
+	for (cnt = 1; cnt < nlinks; cnt++) {
+		sprintf(lname, "%s%d", fname, cnt);
 
-			SAFE_STAT(cleanup, lname, &lbuf);
-			if (fbuf.st_nlink <= 1 || lbuf.st_nlink <= 1 ||
-			    (fbuf.st_nlink != lbuf.st_nlink)) {
+		SAFE_STAT(lname, &lbuf);
+		if (fbuf.st_nlink <= 1 || lbuf.st_nlink <= 1 ||
+		    (fbuf.st_nlink != lbuf.st_nlink)) {
 
-				tst_resm(TFAIL,
-					 "link(%s, %s[1-%d]) ret %ld for %d "
-				         "files, stat values do not match %d %d",
-					 fname, fname, nlinks,
-					 TEST_RETURN, nlinks,
-					 (int)fbuf.st_nlink, (int)lbuf.st_nlink);
-				break;
-			}
-		}
-		if (cnt >= nlinks) {
-			tst_resm(TPASS,
-				 "link(%s, %s[1-%d]) ret %ld for %d files, "
-			         "stat linkcounts match %d",
-				 fname, fname, nlinks, TEST_RETURN,
-				 nlinks, (int)fbuf.st_nlink);
-		}
-
-		for (cnt = 1; cnt < nlinks; cnt++) {
-			sprintf(lname, "%s%d", fname, cnt);
-			SAFE_UNLINK(cleanup, lname);
+			tst_res(TFAIL,
+				 "link(%s, %s[1-%d]) ret %ld for %d "
+			         "files, stat values do not match %d %d",
+				 fname, fname, nlinks,
+				 TST_RET, nlinks,
+				 (int)fbuf.st_nlink, (int)lbuf.st_nlink);
+			break;
 		}
 	}
+	if (cnt >= nlinks) {
+		tst_res(TPASS,
+			 "link(%s, %s[1-%d]) ret %ld for %d files, "
+		         "stat linkcounts match %d",
+			 fname, fname, nlinks, TST_RET,
+			 nlinks, (int)fbuf.st_nlink);
+	}
 
-	cleanup();
-	tst_exit();
-}
-
-static void help(void)
-{
-	printf("  -N #links : create #links hard links every iteration\n");
+	for (cnt = 1; cnt < nlinks; cnt++) {
+		sprintf(lname, "%s%d", fname, cnt);
+		SAFE_UNLINK(lname);
+	}
 }
 
 static void setup(void)
 {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
 	sprintf(fname, "%s_%d", BASENAME, getpid());
-	SAFE_TOUCH(cleanup, fname, 0700, NULL);
+	SAFE_TOUCH(fname, 0700, NULL);
 }
 
-static void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+        .test_all = verify_link,
+        .setup = setup,
+        .needs_tmpdir = 1,
+};
Signature · 7 lines
2.20.1




-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] [PATCH v4 2/2] link/link05: Convert to new API

Patch pushed with the following improvements. Thanks~

testcases/kernel/syscalls/link/link05.c +2 -4
@@ -22,19 +22,19 @@ static int nlinks = 1000;
 
 static void verify_link(void)
 {
-       struct stat fbuf, lbuf;
        int cnt;
-       char lname[255];
+       char lname[1024];
+       struct stat fbuf, lbuf;
 
        for (cnt = 1; cnt < nlinks; cnt++) {
-               sprintf(lname, "%s%d", fname, cnt);
-               TST_EXP_PASS(link(fname, lname), "link(%s, %s)", fname,

lname);
+ sprintf(lname, "%s_%d", fname, cnt);
+ TST_EXP_PASS_SILENT(link(fname, lname), "link(%s, %s)",

fname, lname);
        }

        SAFE_STAT(fname, &fbuf);

        for (cnt = 1; cnt < nlinks; cnt++) {

- sprintf(lname, "%s%d", fname, cnt);
+ sprintf(lname, "%s_%d", fname, cnt);

                SAFE_STAT(lname, &lbuf);
                if (fbuf.st_nlink <= 1 || lbuf.st_nlink <= 1 ||
@@ -49,6 +49,7 @@ static void verify_link(void)
                        break;
                }
        }
+
        if (cnt >= nlinks) {
                tst_res(TPASS,
                         "link(%s, %s[1-%d]) ret %ld for %d files, "
@@ -58,7 +59,7 @@ static void verify_link(void)
        }

        for (cnt = 1; cnt < nlinks; cnt++) {
-               sprintf(lname, "%s%d", fname, cnt);
+               sprintf(lname, "%s_%d", fname, cnt);
                SAFE_UNLINK(lname);
        }

}

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
In reply to tangmeng

Hi Meng,

I made some improvements work inline below and pushed.

Quoted 15 lines · tangmeng
---
 runtest/syscalls                        |   2 -
 testcases/kernel/syscalls/link/link04.c | 208 +++++++++++-------------
 testcases/kernel/syscalls/link/link06.c | 113 -------------
 testcases/kernel/syscalls/link/link07.c | 118 --------------
 4 files changed, 93 insertions(+), 348 deletions(-)
 delete mode 100644 testcases/kernel/syscalls/link/link06.c
 delete mode 100644 testcases/kernel/syscalls/link/link07.c
runtest/syscalls +0 -0
index b19316805..1e6d46744 100644

runtest/syscalls link02 link02 · line 676

Quoted 6 lines
runtest/syscalls +0 -2
@@ -673,8 +673,6 @@ link02 link02
 link03 link03
 link04 link04
 link05 link05
-link06 link06
-link07 link07
 

And we should remove them from the .gitignore file as well.

Quoted 92 lines
runtest/syscalls +0 -0
 link08 link08
 

#linkat test cases

testcases/kernel/syscalls/link/link04.c +4 -30
index d9408845e..9a1c32859 100644
@@ -1,65 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
  *  AUTHOR             : Richard Logan
  *  CO-PILOT           : William Roske
  * Copyright (c) 2014 Cyril Hrubis <[email protected]>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * 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.
- *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
- *
  */
 
-/*
+/*\
+ * [Description]
+ *
  * Negative test cases for link(2).
  *
  * This test program should contain test cases where link will fail

regardless
* of who executed it (i.e. joe-user or root)
*/

testcases/kernel/syscalls/link/link04.c +6 -8
-#include <sys/types.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
+#include <pwd.h>
 #include <sys/param.h>
 #include <sys/mman.h>
-#include "test.h"
-#include "safe_macros.h"
+#include "tst_test.h"
+
+#define NOBODY_USER     99
+#define MODE_TO1 S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IXOTH|S_IROTH
+#define MODE_TO2

S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IXOTH|S_IROTH|S_IWOTH

testcases/kernel/syscalls/link/link04.c +5 -2
+#define MODE_TE S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
+#define MODE_RWX        S_IRWXU | S_IRWXG | S_IRWXO
+#define DIR_TEMP        "dir2/testdir_1"
 
 static char longpath[PATH_MAX + 2];
 
-struct test_case_t {
+static struct tcase {
        char *file1;
        char *desc1;
        char *file2;
        char *desc2;
        int exp_errno;
-} test_cases[] = {
+} tcases[] = {
        /* first path is invalid */
        {"nonexistfile", "non-existent file", "nefile", "nefile", ENOENT},
        {"", "path is empty string", "nefile", "nefile", ENOENT},

testcases/kernel/syscalls/link/link04.c struct test_case_t { · line 62

Quoted 9 lines
testcases/kernel/syscalls/link/link04.c +3 -0
@@ -79,92 +54,95 @@ struct test_case_t {
        {"regfile", "regfile", NULL, "invalid address", EFAULT},
        /* two existing files */
        {"regfile", "regfile", "regfile2", "regfile2", EEXIST},
+       {"dir1/oldpath", "Write access diretory", "dir1/newpath",
 "newpath", EACCES},
+       {"dir2/testdir_1/tfile_2", "Search access diretory",
 "dir2/testdir_1/new_tfile_2",
+       "dir2/testdir_1/new_tfile_2", EACCES},
 

Reset the layout for more readable.

Quoted 109 lines
testcases/kernel/syscalls/link/link04.c +26 -71
 };
 
-char *TCID = "link04";
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void verify_link(unsigned int i)
 {
-       int lc;
-       char *fname1, *fname2;
-       char *desc1, *desc2;
-       int i;
-
-       tst_parse_opts(ac, av, NULL, NULL);
-
-       setup();
-
-       for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-               tst_count = 0;
-
-               for (i = 0; i < TST_TOTAL; i++) {
-
-                       fname1 = test_cases[i].file1;
-                       desc1 = test_cases[i].desc1;
-                       fname2 = test_cases[i].file2;
-                       desc2 = test_cases[i].desc2;
-
-                       TEST(link(fname1, fname2));
-
-                       if (TEST_RETURN == -1) {
-                               if (TEST_ERRNO == test_cases[i].exp_errno)
 {
-                                       tst_resm(TPASS | TTERRNO,
-                                                "link(<%s>, <%s>)",
-                                                desc1, desc2);
-                               } else {
-                                       tst_resm(TFAIL | TTERRNO,
-                                                "link(<%s>, <%s>) Failed "
-                                                "expected errno: %d",
-                                                desc1, desc2,
-                                                test_cases[i].exp_errno);
-                               }
-                       } else {
-                               tst_resm(TFAIL,
-                                        "link(<%s>, <%s>) returned %ld, "
-                                        "expected -1, errno:%d",
-                                        desc1, desc2, TEST_RETURN,
-                                        test_cases[i].exp_errno);
-                       }
+       struct tcase *tc = &tcases[i];
+
+       TEST(link(tc->file1, tc->file2));
+
+       if (TST_RET == -1) {
+               if (TST_ERR == tc->exp_errno) {
+                       tst_res(TPASS | TTERRNO,
+                       "link(<%s>, <%s>)",
+                       tc->desc1, tc->desc2);
+       } else {
+               tst_res(TFAIL | TTERRNO,
+                       "link(<%s>, <%s>) Failed "
+                       "expected errno: %d",
+                       tc->desc1, tc->desc2,
+                       tc->exp_errno);
                }
-
+       } else {
+               tst_res(TFAIL,
+                        "link(<%s>, <%s>) returned %ld, "
+                       "expected -1, errno:%d",
+                       tc->desc1, tc->desc2, TST_RET,
+                       tc->exp_errno);
        }
-
-       cleanup();
-       tst_exit();
 }
 
 static void setup(void)
 {
-       int n;
-
-       tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-       TEST_PAUSE;
-
-       tst_tmpdir();
-
        memset(longpath, 'a', PATH_MAX+1);
-       SAFE_TOUCH(cleanup, "regfile", 0777, NULL);
-       SAFE_TOUCH(cleanup, "regfile2", 0777, NULL);
-       SAFE_MKDIR(cleanup, "dir", 0777);
-
-       void *bad_addr = tst_get_bad_addr(cleanup);
 
-       for (n = 0; n < TST_TOTAL; n++) {
-               if (!test_cases[n].file1)
-                       test_cases[n].file1 = bad_addr;
-
-               if (!test_cases[n].file2)
-                       test_cases[n].file2 = bad_addr;
+       SAFE_TOUCH("regfile", 0777, NULL);
+       SAFE_TOUCH("regfile2", 0777, NULL);
+       SAFE_MKDIR("dir", 0777);
+

testcases/kernel/syscalls/link/link04.c struct test_case_t { · line 110

Quoted 10 lines
testcases/kernel/syscalls/link/link04.c +9 -0
+       unsigned int i;
+       struct passwd *nobody_pwd;
+
+       for (i = 0; i < ARRAY_SIZE(tcases); i++) {
+               struct tcase *tc = &tcases[i];
+
+               switch (tc->exp_errno) {
+               case EACCES:
+                       if (tc->desc1 == "Write access diretory") {
 

"==" is not a suggested way to compare strings in C programming. We should use strcmp() for instead.

Quoted 20 lines
testcases/kernel/syscalls/link/link04.c +3 -0
+                               SAFE_SETEUID(0);
+                               SAFE_MKDIR("dir1", MODE_RWX);
+                               /* Modify mode permissions on test

directory */

testcases/kernel/syscalls/link/link04.c +8 -0
+                               SAFE_CHMOD("dir1", MODE_TO1);
+                               SAFE_TOUCH(tc->file1, 0777, NULL);
+                               nobody_pwd = SAFE_GETPWNAM("nobody");
+                               SAFE_SETEUID(nobody_pwd->pw_uid);
+                       } else if (tc->desc1 == "Search access diretory") {
+                               SAFE_SETEUID(0);
+                               SAFE_MKDIR("dir2", MODE_RWX);
+                               /* Modify mode permissions on test

directory */

testcases/kernel/syscalls/link/link04.c +5 -0
+                               SAFE_CHMOD("dir2", MODE_TO2);
+                               SAFE_MKDIR(DIR_TEMP, MODE_RWX);
+                               SAFE_TOUCH(tc->file1, 0666, NULL);
+
+                               /* Modify mode permissions on test

directory - test conditions */

testcases/kernel/syscalls/link/link04.c struct test_case_t { · line 124

Quoted 3 lines
testcases/kernel/syscalls/link/link04.c +3 -0
+                               SAFE_CHMOD(DIR_TEMP, MODE_TE);
+                               nobody_pwd = SAFE_GETPWNAM("nobody");
+                               SAFE_SETEUID(nobody_pwd->pw_uid);

I help reduce some duplicated code and move this part to the main thread.

Quoted 29 lines
testcases/kernel/syscalls/link/link04.c +21 -4
+                       }
+                       break;
+               case ENOENT:
+               case ENOTDIR:
+               case ENAMETOOLONG:
+               case EFAULT:
+               case EEXIST:
+                       if (!tc->file1)
+                               tc->file1 = tst_get_bad_addr(NULL);
+
+                       if (!tc->file2)
+                               tc->file2 = tst_get_bad_addr(NULL);
+                       break;
+               }
        }
 }
 
-static void cleanup(void)
-{
-       tst_rmdir();
-}
+static struct tst_test test = {
+       .tcnt = ARRAY_SIZE(tcases),
+       .test = verify_link,
+       .setup = setup,
+       .needs_tmpdir = 1,
+       .needs_root = 1,
+};
Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp