Skip to contentSkip to conversation

fchownat01: Convert to new API

PATCH In review Shiyang Ruan via ltp 1 participant 9 messages Jan 16, 2025 – Feb 3, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/3 fchownat01: Convert to new API

1. convert to new API
2. move error tests to fchownat03
3. check if uid/gid is correctly set

---
V2: https://lore.kernel.org/ltp/[email protected]/

Changes:
* Initialize fd to -1, and check fd != -1 when cleanup
* Do not use TST_*_SILENT()
* fchownat01: describe what this case does in DESCRIPTION
* fchownat01: use TST_EXP_EQ_LI(); do not use test case struct
* fchownat02: use TST_EXP_EXPR()
* fchownat03: write a proper commit message, add more errnos to test

---
 .../kernel/syscalls/fchownat/fchownat01.c     | 153 +++++-------------
 1 file changed, 42 insertions(+), 111 deletions(-)
testcases/kernel/syscalls/fchownat/fchownat01.c +42 -111
index 7771c111a..fedcb8ab2 100644
@@ -1,133 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *   Copyright (c) International Business Machines  Corp., 2006
- *   AUTHOR: Yi Yang <[email protected]>
- *
- *   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.
- *
- *   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
+ * Copyright (c) International Business Machines  Corp., 2006
+ * Copyright (c) Linux Test Project, 2007-2025
  */
-/*
- * DESCRIPTION
- *	This test case will verify basic function of fchownat
- *	added by kernel 2.6.16 or up.
+
+/*\
+ * [Description]
+ *
+ * Verify that fchownat() succeeds to change file's ownership if the file
+ * descriptor is AT_FDCWD or set by opening a directory.
  */
 
 #define _GNU_SOURCE
+#include "tst_test.h"
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/fcntl.h"
+#define TESTFILE1	"testfile1"
+#define TESTFILE2	"testfile2"
 
-#define TESTFILE	"testfile"
+static uid_t set_uid = 1000;
+static gid_t set_gid = 1000;
+static int dir_fd = -1;
 
-static void setup(void);
-static void cleanup(void);
-
-static int dir_fd;
-static int fd;
-static int no_fd = -1;
-static int cu_fd = AT_FDCWD;
-
-static struct test_case_t {
-	int exp_ret;
-	int exp_errno;
-	int flag;
-	int *fds;
-	char *filenames;
-} test_cases[] = {
-	{0, 0, 0, &dir_fd, TESTFILE},
-	{-1, ENOTDIR, 0, &fd, TESTFILE},
-	{-1, EBADF, 0, &no_fd, TESTFILE},
-	{-1, EINVAL, 9999, &dir_fd, TESTFILE},
-	{0, 0, 0, &cu_fd, TESTFILE},
-};
-
-char *TCID = "fchownat01";
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-static void fchownat_verify(const struct test_case_t *);
-
-int main(int ac, char **av)
+static void fchownat_verify(void)
 {
-	int lc;
-	int i;
+	struct stat stat_buf;
 
-	tst_parse_opts(ac, av, NULL, NULL);
+	TST_EXP_PASS(fchownat(AT_FDCWD, TESTFILE1, set_uid, set_gid, 0),
+		     "fchownat(%d, %s, %d, %d, 0)",
+		     AT_FDCWD, TESTFILE1, set_uid, set_gid);
 
-	setup();
+	SAFE_STAT(TESTFILE1, &stat_buf);
+	TST_EXP_EQ_LI(stat_buf.st_uid, set_uid);
+	TST_EXP_EQ_LI(stat_buf.st_gid, set_gid);
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		for (i = 0; i < TST_TOTAL; i++)
-			fchownat_verify(&test_cases[i]);
-	}
+	TST_EXP_PASS(fchownat(dir_fd, TESTFILE2, set_uid, set_gid, 0),
+		     "fchownat(%d, %s, %d, %d, 0)",
+		     dir_fd, TESTFILE2, set_uid, set_gid);
 
-	cleanup();
-	tst_exit();
+	SAFE_STAT(TESTFILE2, &stat_buf);
+	TST_EXP_EQ_LI(stat_buf.st_uid, set_uid);
+	TST_EXP_EQ_LI(stat_buf.st_gid, set_gid);
 }
 
 static void setup(void)
 {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
-
-	SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
-
-	fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
-}
-
-static void fchownat_verify(const struct test_case_t *test)
-{
-	TEST(fchownat(*(test->fds), test->filenames, geteuid(),
-		      getegid(), test->flag));
-
-	if (TEST_RETURN != test->exp_ret) {
-		tst_resm(TFAIL | TTERRNO,
-			 "fchownat() returned %ld, expected %d, errno=%d",
-			 TEST_RETURN, test->exp_ret, test->exp_errno);
-		return;
-	}
-
-	if (TEST_ERRNO == test->exp_errno) {
-		tst_resm(TPASS | TTERRNO,
-			 "fchownat() returned the expected errno %d: %s",
-			 test->exp_ret, strerror(test->exp_errno));
-	} else {
-		tst_resm(TFAIL | TTERRNO,
-			 "fchownat() failed unexpectedly; expected: %d - %s",
-			 test->exp_errno, strerror(test->exp_errno));
-	}
+	dir_fd = SAFE_OPEN("./", O_DIRECTORY);
+	SAFE_TOUCH(TESTFILE1, 0600, NULL);
+	SAFE_TOUCH(TESTFILE2, 0600, NULL);
 }
 
 static void cleanup(void)
 {
-	if (fd > 0)
-		close(fd);
-
-	if (dir_fd > 0)
-		close(dir_fd);
-
-	tst_rmdir();
+	if (dir_fd != -1)
+		SAFE_CLOSE(dir_fd);
 }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.test_all = fchownat_verify,
+	.setup = setup,
+	.cleanup = cleanup,
+};
Signature · 5 lines
2.43.0


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

PATCH v3 2/3 fchownat02: Convert to new API

---
 .../kernel/syscalls/fchownat/fchownat02.c     | 138 ++++--------------
 1 file changed, 32 insertions(+), 106 deletions(-)
testcases/kernel/syscalls/fchownat/fchownat02.c +32 -106
index 6dd1e024e..10b4f5e3c 100644
@@ -1,135 +1,61 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) 2014 Fujitsu Ltd.
- * Author: Zeng Linggang <[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.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program.
+ * Copyright (c) Linux Test Project, 2014-2025
  */
-/*
- * Test Description:
- *   Verify that,
- *   The flag of fchownat() is AT_SYMLINK_NOFOLLOW and the pathname would
- *   not be dereferenced if the pathname is a symbolic link.
+
+/*\
+ * [Description]
+ *
+ * Verify that fchownat() will operate on symbolic links when
+ * AT_SYMLINK_NOFOLLOW is used.
  */
 
 #define _GNU_SOURCE
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/fcntl.h"
+#include "tst_test.h"
 
 #define TESTFILE	"testfile"
 #define TESTFILE_LINK	"testfile_link"
 
-char *TCID = "fchownat02";
-int TST_TOTAL = 1;
-
-static int dir_fd;
 static uid_t set_uid = 1000;
 static gid_t set_gid = 1000;
-static void setup(void);
-static void cleanup(void);
-static void test_verify(void);
-static void fchownat_verify(void);
-
-int main(int ac, char **av)
-{
-	int lc;
-	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++)
-			fchownat_verify();
-	}
-
-	cleanup();
-	tst_exit();
-}
 
 static void setup(void)
 {
 	struct stat c_buf, l_buf;
 
-	tst_require_root();
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
-
-	SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
-
-	SAFE_SYMLINK(cleanup, TESTFILE, TESTFILE_LINK);
-
-	SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf);
-
-	SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf);
+	SAFE_TOUCH(TESTFILE, 0600, NULL);
+	SAFE_SYMLINK(TESTFILE, TESTFILE_LINK);
+	SAFE_STAT(TESTFILE_LINK, &c_buf);
+	SAFE_LSTAT(TESTFILE_LINK, &l_buf);
 
 	if (l_buf.st_uid == set_uid || l_buf.st_gid == set_gid) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "link_uid(%d) == set_uid(%d) or link_gid(%d) == "
-			 "set_gid(%d)", l_buf.st_uid, set_uid, l_buf.st_gid,
-			 set_gid);
+		tst_brk(TBROK,
+			"uid link(%d) == set(%d) or gid link(%d) == set(%d)",
+			l_buf.st_uid, set_uid, l_buf.st_gid, set_gid);
 	}
 }
 
-static void fchownat_verify(void)
-{
-	TEST(fchownat(dir_fd, TESTFILE_LINK, set_uid, set_gid,
-		      AT_SYMLINK_NOFOLLOW));
-
-	if (TEST_RETURN != 0) {
-		tst_resm(TFAIL | TTERRNO, "fchownat() failed, errno=%d : %s",
-			 TEST_ERRNO, strerror(TEST_ERRNO));
-	} else {
-		test_verify();
-	}
-}
-
-static void test_verify(void)
+static void run(void)
 {
 	struct stat c_buf, l_buf;
 
-	SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf);
+	TST_EXP_PASS(fchownat(AT_FDCWD, TESTFILE_LINK, set_uid, set_gid, AT_SYMLINK_NOFOLLOW),
+		     "fchownat(%d, %s, %d, %d, %d)",
+		     AT_FDCWD, TESTFILE_LINK, set_uid, set_gid, AT_SYMLINK_NOFOLLOW);
 
-	SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf);
+	SAFE_STAT(TESTFILE_LINK, &c_buf);
+	SAFE_LSTAT(TESTFILE_LINK, &l_buf);
 
-	if (c_buf.st_uid != set_uid && l_buf.st_uid == set_uid &&
-	    c_buf.st_gid != set_gid && l_buf.st_gid == set_gid) {
-		tst_resm(TPASS, "fchownat() test AT_SYMLINK_NOFOLLOW success");
-	} else {
-		tst_resm(TFAIL,
-			 "fchownat() test AT_SYMLINK_NOFOLLOW fail with uid=%d "
-			 "link_uid=%d set_uid=%d | gid=%d link_gid=%d "
-			 "set_gid=%d", c_buf.st_uid, l_buf.st_uid, set_uid,
-			 c_buf.st_gid, l_buf.st_gid, set_gid);
-	}
+	TST_EXP_EXPR(c_buf.st_uid != set_uid && l_buf.st_uid == set_uid,
+		     "fchownat() correctly operated on symlink user ID");
+	TST_EXP_EXPR(c_buf.st_gid != set_gid && l_buf.st_gid == set_gid,
+		     "fchownat() correctly operated on symlink group ID");
 }
 
-static void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.test_all = run,
+	.setup = setup,
+};
Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author Reviewed-by Andrea Cervesato on v3 2/3
#2 lore raw

Re: [LTP] [PATCH v3 2/3] fchownat02: Convert to new API

Hi!

Quoted 177 lines · Shiyang Ruan
---
  .../kernel/syscalls/fchownat/fchownat02.c     | 138 ++++--------------
  1 file changed, 32 insertions(+), 106 deletions(-)
testcases/kernel/syscalls/fchownat/fchownat02.c +32 -106
index 6dd1e024e..10b4f5e3c 100644
@@ -1,135 +1,61 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
  /*
   * Copyright (c) 2014 Fujitsu Ltd.
- * Author: Zeng Linggang <[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.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program.
+ * Copyright (c) Linux Test Project, 2014-2025
   */
-/*
- * Test Description:
- *   Verify that,
- *   The flag of fchownat() is AT_SYMLINK_NOFOLLOW and the pathname would
- *   not be dereferenced if the pathname is a symbolic link.
+
+/*\
+ * [Description]
+ *
+ * Verify that fchownat() will operate on symbolic links when
+ * AT_SYMLINK_NOFOLLOW is used.
   */
 
  #define _GNU_SOURCE
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/fcntl.h"
+#include "tst_test.h"
 
  #define TESTFILE	"testfile"
  #define TESTFILE_LINK	"testfile_link"
 
-char *TCID = "fchownat02";
-int TST_TOTAL = 1;
-
-static int dir_fd;
  static uid_t set_uid = 1000;
  static gid_t set_gid = 1000;
-static void setup(void);
-static void cleanup(void);
-static void test_verify(void);
-static void fchownat_verify(void);
-
-int main(int ac, char **av)
-{
-	int lc;
-	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++)
-			fchownat_verify();
-	}
-
-	cleanup();
-	tst_exit();
-}
 
  static void setup(void)
  {
  	struct stat c_buf, l_buf;
 
-	tst_require_root();
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
-
-	SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
-
-	SAFE_SYMLINK(cleanup, TESTFILE, TESTFILE_LINK);
-
-	SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf);
-
-	SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf);
+	SAFE_TOUCH(TESTFILE, 0600, NULL);
+	SAFE_SYMLINK(TESTFILE, TESTFILE_LINK);
+	SAFE_STAT(TESTFILE_LINK, &c_buf);
+	SAFE_LSTAT(TESTFILE_LINK, &l_buf);
 
  	if (l_buf.st_uid == set_uid || l_buf.st_gid == set_gid) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "link_uid(%d) == set_uid(%d) or link_gid(%d) == "
-			 "set_gid(%d)", l_buf.st_uid, set_uid, l_buf.st_gid,
-			 set_gid);
+		tst_brk(TBROK,
+			"uid link(%d) == set(%d) or gid link(%d) == set(%d)",
+			l_buf.st_uid, set_uid, l_buf.st_gid, set_gid);
  	}
  }
 
-static void fchownat_verify(void)
-{
-	TEST(fchownat(dir_fd, TESTFILE_LINK, set_uid, set_gid,
-		      AT_SYMLINK_NOFOLLOW));
-
-	if (TEST_RETURN != 0) {
-		tst_resm(TFAIL | TTERRNO, "fchownat() failed, errno=%d : %s",
-			 TEST_ERRNO, strerror(TEST_ERRNO));
-	} else {
-		test_verify();
-	}
-}
-
-static void test_verify(void)
+static void run(void)
  {
  	struct stat c_buf, l_buf;
 
-	SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf);
+	TST_EXP_PASS(fchownat(AT_FDCWD, TESTFILE_LINK, set_uid, set_gid, AT_SYMLINK_NOFOLLOW),
+		     "fchownat(%d, %s, %d, %d, %d)",
+		     AT_FDCWD, TESTFILE_LINK, set_uid, set_gid, AT_SYMLINK_NOFOLLOW);
 
-	SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf);
+	SAFE_STAT(TESTFILE_LINK, &c_buf);
+	SAFE_LSTAT(TESTFILE_LINK, &l_buf);
 
-	if (c_buf.st_uid != set_uid && l_buf.st_uid == set_uid &&
-	    c_buf.st_gid != set_gid && l_buf.st_gid == set_gid) {
-		tst_resm(TPASS, "fchownat() test AT_SYMLINK_NOFOLLOW success");
-	} else {
-		tst_resm(TFAIL,
-			 "fchownat() test AT_SYMLINK_NOFOLLOW fail with uid=%d "
-			 "link_uid=%d set_uid=%d | gid=%d link_gid=%d "
-			 "set_gid=%d", c_buf.st_uid, l_buf.st_uid, set_uid,
-			 c_buf.st_gid, l_buf.st_gid, set_gid);
-	}
+	TST_EXP_EXPR(c_buf.st_uid != set_uid && l_buf.st_uid == set_uid,
+		     "fchownat() correctly operated on symlink user ID");
+	TST_EXP_EXPR(c_buf.st_gid != set_gid && l_buf.st_gid == set_gid,
+		     "fchownat() correctly operated on symlink group ID");
  }
 
-static void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.test_all = run,
+	.setup = setup,
+};
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v3 3/3 fchownat03: Add error tests for fchownat()

In reply to Shiyang Ruan via ltp

Move the 3 error tests form fchownat01 to here, and add tests for rest possible errnos per the man page.

---
 testcases/kernel/syscalls/fchownat/.gitignore |   1 +
 .../kernel/syscalls/fchownat/fchownat03.c     | 148 ++++++++++++++++++
 2 files changed, 149 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fchownat/fchownat03.c
testcases/kernel/syscalls/fchownat/.gitignore +1 -0
index 35c00345b..60fac7e69 100644
@@ -1,2 +1,3 @@
 /fchownat01
 /fchownat02
+/fchownat03
testcases/kernel/syscalls/fchownat/fchownat03.c new file +148 -0

148 lines not on this page · raw message

Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author Reviewed-by Andrea Cervesato on v3 3/3
#4 lore raw

Re: [LTP] [PATCH v3 3/3] fchownat03: Add error tests for fchownat()

Hi!

Quoted 172 lines · Shiyang Ruan

Move the 3 error tests form fchownat01 to here, and add tests for rest possible errnos per the man page.

---
  testcases/kernel/syscalls/fchownat/.gitignore |   1 +
  .../kernel/syscalls/fchownat/fchownat03.c     | 148 ++++++++++++++++++
  2 files changed, 149 insertions(+)

create mode 100644 testcases/kernel/syscalls/fchownat/fchownat03.c

testcases/kernel/syscalls/fchownat/.gitignore +1 -0
index 35c00345b..60fac7e69 100644
@@ -1,2 +1,3 @@
  /fchownat01
  /fchownat02
+/fchownat03
testcases/kernel/syscalls/fchownat/fchownat03.c new file +148 -0
new file mode 100644
index 000000000..06928490b
@@ -0,0 +1,148 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines  Corp., 2006
+ * Copyright (c) Linux Test Project, 2007-2024
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that,
+ * - fchownat() returns -1 and sets errno to EACCES if there is no permission
+     to access to the file.
+ * - fchownat() returns -1 and sets errno to EBADF if the file descriptor
+ *   of the specified file is not valid.
+ * - fchownat() returns -1 and sets errno to EFAULT if the filename points
+     outside of accessable address space.
+ * - fchownat() returns -1 and sets errno to EINVAL if the flag is invalid.
+ * - fchownat() returns -1 and sets errno to ELOOP if too many symbolic links
+ *   were encountered in resolving filename.
+ * - fchownat() returns -1 and sets errno to ENAMETOOLONG if the filename is
+     too long.
+ * - fchownat() returns -1 and sets errno to ENOENT if the specified file does
+ *   not exist.
+ * - fchownat() returns -1 and sets errno to ENOTDIR if the file descriptor is
+ *   a file.
+ * - fchownat() returns -1 and sets errno to EPERM if the effective user id
+ *   of process does not match the owner of the file and the process is not
+ *   super user.
+ * - fchownat() returns -1 and sets errno to EROFS if the file is readonly.
+ */
+
+#define _GNU_SOURCE
+#include <pwd.h>
+#include "tst_test.h"
+
+#define TESTFILE		"testfile"
+#define TESTFILE_EACCESS_DIR	"eaccess"
+#define TESTFILE_EACCESS	TESTFILE_EACCESS_DIR"/eaccess"
+#define TESTFILE_ELOOP		"testfile_eloop"
+#define TESTFILE_ENOENT		"/tmp/does/not/exist"
+#define TESTFILE_EPERM		"/dev/null"
+#define TESTFILE_EROFS_MNT	"ro_mntpoint"
+#define TESTFILE_EROFS		TESTFILE_EROFS_MNT"/file"
+
+static int file_fd = -1;
+static int no_fd = -1;
+static int dir_fd = -1;
+
+static char *file_eaccess;
+static char *file_ebadf;
+static char *file_efault;
+static char *file_einval;
+static char *file_eloop;
+static char *file_enametoolong;
+static char *file_enoent;
+static char *file_enotdir;
+static char *file_eperm;
+static char *file_erofs;
+
+static struct tcase {
+	char **filename;
+	char *desc;
+	int *fd;
+	int flag;
+	int exp_errno;
+} tcases[] = {
+	{&file_eaccess,      TESTFILE_EACCESS,  &dir_fd,  0,    EACCES},
+	{&file_ebadf,        TESTFILE,          &no_fd,   0,    EBADF},
+	{&file_efault,       "Invalid address", &dir_fd,  0,    EFAULT},
+	{&file_einval,       TESTFILE,          &dir_fd,  9999, EINVAL},
+	{&file_eloop,        TESTFILE_ELOOP,    &dir_fd,  0,    ELOOP},
+	{&file_enametoolong, "aaaa...",         &dir_fd,  0,    ENAMETOOLONG},
+	{&file_enoent,       TESTFILE_ENOENT,   &dir_fd,  0,    ENOENT},
+	{&file_enotdir,      TESTFILE,          &file_fd, 0,    ENOTDIR},
+	{&file_eperm,        TESTFILE_EPERM,    &dir_fd,  0,    EPERM},
+	{&file_erofs,        TESTFILE_EROFS,    &dir_fd,  0,    EROFS},
+};
+
+static void fchownat_verify(unsigned int n)
+{
+	uid_t euid = geteuid();
+	gid_t egid = getegid();
+
+	TST_EXP_FAIL(fchownat(*tcases[n].fd, *tcases[n].filename, euid, egid, tcases[n].flag),
+		     tcases[n].exp_errno,
+		     "fchownat(%d, %s, %d, %d, %d)",
+		     *tcases[n].fd, tcases[n].desc, euid, egid, tcases[n].flag);
+}
+
+static void setup(void)
+{
+	struct passwd *ltpuser;
+
+	SAFE_TOUCH(TESTFILE, 0600, NULL);
+	dir_fd = SAFE_OPEN("./", O_DIRECTORY);
+
+	/* EACCES setting */
+	SAFE_SETEUID(0);
+	SAFE_MKDIR(TESTFILE_EACCESS_DIR, S_IRWXU);
+	SAFE_TOUCH(TESTFILE_EACCESS, 0666, NULL);
+	ltpuser = SAFE_GETPWNAM("nobody");
+	SAFE_SETEUID(ltpuser->pw_uid);
+
+	/* EFAULT setting */
+	file_efault = tst_get_bad_addr(NULL);
+
+	/* ENOTDIR setting */
+	file_fd = SAFE_OPEN("file_fd", O_CREAT | O_RDWR, 0600);
+
+	/* ELOOP setting */
+	SAFE_SYMLINK(TESTFILE_ELOOP, "test_file_eloop2");
+	SAFE_SYMLINK("test_file_eloop2", TESTFILE_ELOOP);
+
+	/* ENAMETOOLONG setting */
+	memset(file_enametoolong, 'a', PATH_MAX+1);
+	file_enametoolong[PATH_MAX+1] = 0;
+}
+
+static void cleanup(void)
+{
+	if (dir_fd != -1)
+		SAFE_CLOSE(dir_fd);
+	if (file_fd != -1)
+		SAFE_CLOSE(file_fd);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.needs_rofs = 1,
+	.mntpoint = TESTFILE_EROFS_MNT,
+	.test = fchownat_verify,
+	.tcnt = ARRAY_SIZE(tcases),
+	.setup = setup,
+	.cleanup = cleanup,
+	.bufs = (struct tst_buffers []) {
+		{&file_eaccess,      .str = TESTFILE_EACCESS},
+		{&file_ebadf,        .str = TESTFILE},
+		{&file_einval,       .str = TESTFILE},
+		{&file_eloop,        .str = TESTFILE_ELOOP},
+		{&file_enametoolong, .size = PATH_MAX+2},
+		{&file_enoent,       .str = TESTFILE_ENOENT},
+		{&file_enotdir,      .str = TESTFILE},
+		{&file_eperm,        .str = TESTFILE_EPERM},
+		{&file_erofs,        .str = TESTFILE_EROFS},
+		{}
+	},
+};
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw
In reply to Shiyang Ruan via ltp

ping~

Quoted 201 lines · Shiyang Ruan

1. convert to new API
2. move error tests to fchownat03
3. check if uid/gid is correctly set

---
V2: https://lore.kernel.org/ltp/[email protected]/

Changes:
* Initialize fd to -1, and check fd != -1 when cleanup
* Do not use TST_*_SILENT()
* fchownat01: describe what this case does in DESCRIPTION
* fchownat01: use TST_EXP_EQ_LI(); do not use test case struct
* fchownat02: use TST_EXP_EXPR()
* fchownat03: write a proper commit message, add more errnos to test

---
  .../kernel/syscalls/fchownat/fchownat01.c     | 153 +++++-------------
  1 file changed, 42 insertions(+), 111 deletions(-)
testcases/kernel/syscalls/fchownat/fchownat01.c +42 -111
index 7771c111a..fedcb8ab2 100644
@@ -1,133 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
  /*
- *   Copyright (c) International Business Machines  Corp., 2006
- *   AUTHOR: Yi Yang <[email protected]>
- *
- *   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.
- *
- *   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
+ * Copyright (c) International Business Machines  Corp., 2006
+ * Copyright (c) Linux Test Project, 2007-2025
   */
-/*
- * DESCRIPTION
- *	This test case will verify basic function of fchownat
- *	added by kernel 2.6.16 or up.
+
+/*\
+ * [Description]
+ *
+ * Verify that fchownat() succeeds to change file's ownership if the file
+ * descriptor is AT_FDCWD or set by opening a directory.
   */
 
  #define _GNU_SOURCE
+#include "tst_test.h"
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/fcntl.h"
+#define TESTFILE1	"testfile1"
+#define TESTFILE2	"testfile2"
 
-#define TESTFILE	"testfile"
+static uid_t set_uid = 1000;
+static gid_t set_gid = 1000;
+static int dir_fd = -1;
 
-static void setup(void);
-static void cleanup(void);
-
-static int dir_fd;
-static int fd;
-static int no_fd = -1;
-static int cu_fd = AT_FDCWD;
-
-static struct test_case_t {
-	int exp_ret;
-	int exp_errno;
-	int flag;
-	int *fds;
-	char *filenames;
-} test_cases[] = {
-	{0, 0, 0, &dir_fd, TESTFILE},
-	{-1, ENOTDIR, 0, &fd, TESTFILE},
-	{-1, EBADF, 0, &no_fd, TESTFILE},
-	{-1, EINVAL, 9999, &dir_fd, TESTFILE},
-	{0, 0, 0, &cu_fd, TESTFILE},
-};
-
-char *TCID = "fchownat01";
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-static void fchownat_verify(const struct test_case_t *);
-
-int main(int ac, char **av)
+static void fchownat_verify(void)
  {
-	int lc;
-	int i;
+	struct stat stat_buf;
 
-	tst_parse_opts(ac, av, NULL, NULL);
+	TST_EXP_PASS(fchownat(AT_FDCWD, TESTFILE1, set_uid, set_gid, 0),
+		     "fchownat(%d, %s, %d, %d, 0)",
+		     AT_FDCWD, TESTFILE1, set_uid, set_gid);
 
-	setup();
+	SAFE_STAT(TESTFILE1, &stat_buf);
+	TST_EXP_EQ_LI(stat_buf.st_uid, set_uid);
+	TST_EXP_EQ_LI(stat_buf.st_gid, set_gid);
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		for (i = 0; i < TST_TOTAL; i++)
-			fchownat_verify(&test_cases[i]);
-	}
+	TST_EXP_PASS(fchownat(dir_fd, TESTFILE2, set_uid, set_gid, 0),
+		     "fchownat(%d, %s, %d, %d, 0)",
+		     dir_fd, TESTFILE2, set_uid, set_gid);
 
-	cleanup();
-	tst_exit();
+	SAFE_STAT(TESTFILE2, &stat_buf);
+	TST_EXP_EQ_LI(stat_buf.st_uid, set_uid);
+	TST_EXP_EQ_LI(stat_buf.st_gid, set_gid);
  }
 
  static void setup(void)
  {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
-
-	SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
-
-	fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
-}
-
-static void fchownat_verify(const struct test_case_t *test)
-{
-	TEST(fchownat(*(test->fds), test->filenames, geteuid(),
-		      getegid(), test->flag));
-
-	if (TEST_RETURN != test->exp_ret) {
-		tst_resm(TFAIL | TTERRNO,
-			 "fchownat() returned %ld, expected %d, errno=%d",
-			 TEST_RETURN, test->exp_ret, test->exp_errno);
-		return;
-	}
-
-	if (TEST_ERRNO == test->exp_errno) {
-		tst_resm(TPASS | TTERRNO,
-			 "fchownat() returned the expected errno %d: %s",
-			 test->exp_ret, strerror(test->exp_errno));
-	} else {
-		tst_resm(TFAIL | TTERRNO,
-			 "fchownat() failed unexpectedly; expected: %d - %s",
-			 test->exp_errno, strerror(test->exp_errno));
-	}
+	dir_fd = SAFE_OPEN("./", O_DIRECTORY);
+	SAFE_TOUCH(TESTFILE1, 0600, NULL);
+	SAFE_TOUCH(TESTFILE2, 0600, NULL);
  }
 
  static void cleanup(void)
  {
-	if (fd > 0)
-		close(fd);
-
-	if (dir_fd > 0)
-		close(dir_fd);
-
-	tst_rmdir();
+	if (dir_fd != -1)
+		SAFE_CLOSE(dir_fd);
  }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.test_all = fchownat_verify,
+	.setup = setup,
+	.cleanup = cleanup,
+};
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Hi Shiyang,

we are now facing LTP code freeze for the next release, so we are pretty busy. I will review it and merge it after the release.

Kind regards,
Andrea

Quoted 198 lines · Shiyang Ruan

ping~

Quoted 195 lines · Shiyang Ruan

1. convert to new API
2. move error tests to fchownat03
3. check if uid/gid is correctly set

---
V2: https://lore.kernel.org/ltp/[email protected]/

Changes:
  * Initialize fd to -1, and check fd != -1 when cleanup
  * Do not use TST_*_SILENT()
  * fchownat01: describe what this case does in DESCRIPTION
  * fchownat01: use TST_EXP_EQ_LI(); do not use test case struct
  * fchownat02: use TST_EXP_EXPR()
  * fchownat03: write a proper commit message, add more errnos to test

---
  .../kernel/syscalls/fchownat/fchownat01.c     | 153 +++++-------------

  1 file changed, 42 insertions(+), 111 deletions(-)
testcases/kernel/syscalls/fchownat/fchownat01.c +1 -4
index 7771c111a..fedcb8ab2 100644
@@ -1,133 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
   /*
- *   Copyright (c) International Business Machines  Corp., 2006
- *   AUTHOR: Yi Yang <[email protected]>
- *
- *   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.
- *
- *   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
+ * Copyright (c) International Business Machines  Corp., 2006
+ * Copyright (c) Linux Test Project, 2007-2025
   */

testcases/kernel/syscalls/fchownat/fchownat01.c +5 -4
-/*
- * DESCRIPTION
- *    This test case will verify basic function of fchownat
- *    added by kernel 2.6.16 or up.
+
+/*\
+ * [Description]
+ *
+ * Verify that fchownat() succeeds to change file's ownership if the 

file
+ * descriptor is AT_FDCWD or set by opening a directory. */
    #define _GNU_SOURCE
+#include "tst_test.h"
  -#include <sys/types.h>

testcases/kernel/syscalls/fchownat/fchownat01.c +2 -10
-#include <sys/stat.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/fcntl.h"
+#define TESTFILE1    "testfile1"
+#define TESTFILE2    "testfile2"

  -#define TESTFILE    "testfile"

testcases/kernel/syscalls/fchownat/fchownat01.c +3 -0
+static uid_t set_uid = 1000;
+static gid_t set_gid = 1000;
+static int dir_fd = -1;

  -static void setup(void);

testcases/kernel/syscalls/fchownat/fchownat01.c +1 -26
-static void cleanup(void);
-
-static int dir_fd;
-static int fd;
-static int no_fd = -1;
-static int cu_fd = AT_FDCWD;
-
-static struct test_case_t {
-    int exp_ret;
-    int exp_errno;
-    int flag;
-    int *fds;
-    char *filenames;
-} test_cases[] = {
-    {0, 0, 0, &dir_fd, TESTFILE},
-    {-1, ENOTDIR, 0, &fd, TESTFILE},
-    {-1, EBADF, 0, &no_fd, TESTFILE},
-    {-1, EINVAL, 9999, &dir_fd, TESTFILE},
-    {0, 0, 0, &cu_fd, TESTFILE},
-};
-
-char *TCID = "fchownat01";
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-static void fchownat_verify(const struct test_case_t *);
-
-int main(int ac, char **av)
+static void fchownat_verify(void)

  {
-    int lc;
-    int i;
+    struct stat stat_buf;
  -    tst_parse_opts(ac, av, NULL, NULL);
+    TST_EXP_PASS(fchownat(AT_FDCWD, TESTFILE1, set_uid, set_gid, 0), +             "fchownat(%d, %s, %d, %d, 0)",
+             AT_FDCWD, TESTFILE1, set_uid, set_gid);
  -    setup();
+    SAFE_STAT(TESTFILE1, &stat_buf);
+    TST_EXP_EQ_LI(stat_buf.st_uid, set_uid);
+    TST_EXP_EQ_LI(stat_buf.st_gid, set_gid);
  -    for (lc = 0; TEST_LOOPING(lc); lc++) {
-        tst_count = 0;
-        for (i = 0; i < TST_TOTAL; i++)
-            fchownat_verify(&test_cases[i]);
-    }
+    TST_EXP_PASS(fchownat(dir_fd, TESTFILE2, set_uid, set_gid, 0), +             "fchownat(%d, %s, %d, %d, 0)",
+             dir_fd, TESTFILE2, set_uid, set_gid);
  -    cleanup();
-    tst_exit();
+    SAFE_STAT(TESTFILE2, &stat_buf);
+    TST_EXP_EQ_LI(stat_buf.st_uid, set_uid);
+    TST_EXP_EQ_LI(stat_buf.st_gid, set_gid);
  }
    static void setup(void)
  {
-    tst_sig(NOFORK, DEF_HANDLER, cleanup);

testcases/kernel/syscalls/fchownat/fchownat01.c +3 -33
-
-    TEST_PAUSE;
-
-    tst_tmpdir();
-
-    dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
-
-    SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
-
-    fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
-}
-
-static void fchownat_verify(const struct test_case_t *test)
-{
-    TEST(fchownat(*(test->fds), test->filenames, geteuid(),
-              getegid(), test->flag));
-
-    if (TEST_RETURN != test->exp_ret) {
-        tst_resm(TFAIL | TTERRNO,
-             "fchownat() returned %ld, expected %d, errno=%d",
-             TEST_RETURN, test->exp_ret, test->exp_errno);
-        return;
-    }
-
-    if (TEST_ERRNO == test->exp_errno) {
-        tst_resm(TPASS | TTERRNO,
-             "fchownat() returned the expected errno %d: %s",
-             test->exp_ret, strerror(test->exp_errno));
-    } else {
-        tst_resm(TFAIL | TTERRNO,
-             "fchownat() failed unexpectedly; expected: %d - %s",
-             test->exp_errno, strerror(test->exp_errno));
-    }
+    dir_fd = SAFE_OPEN("./", O_DIRECTORY);
+    SAFE_TOUCH(TESTFILE1, 0600, NULL);
+    SAFE_TOUCH(TESTFILE2, 0600, NULL);

  }
    static void cleanup(void)
  {
-    if (fd > 0)
-        close(fd);

testcases/kernel/syscalls/fchownat/fchownat01.c +2 -5
-
-    if (dir_fd > 0)
-        close(dir_fd);
-
-    tst_rmdir();
+    if (dir_fd != -1)
+        SAFE_CLOSE(dir_fd);

  }

testcases/kernel/syscalls/fchownat/fchownat01.c +8 -0
+
+static struct tst_test test = {
+    .needs_tmpdir = 1,
+    .needs_root = 1,
+    .test_all = fchownat_verify,
+    .setup = setup,
+    .cleanup = cleanup,
+};
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw
Quoted 3 lines · Andrea Cervesato

Hi Shiyang,

we are now facing LTP code freeze for the next release, so we are pretty busy.

Got it. Sorry for bothering.

Quoted 1 line

I will review it and merge it after the release.

Thanks!

--
Ruan.

Quoted 203 lines

Kind regards,
Andrea

Quoted 198 lines · Shiyang Ruan

ping~

Quoted 195 lines · Shiyang Ruan

1. convert to new API
2. move error tests to fchownat03
3. check if uid/gid is correctly set

---
V2: https://lore.kernel.org/ltp/[email protected]/

Changes:
  * Initialize fd to -1, and check fd != -1 when cleanup
  * Do not use TST_*_SILENT()
  * fchownat01: describe what this case does in DESCRIPTION
  * fchownat01: use TST_EXP_EQ_LI(); do not use test case struct
  * fchownat02: use TST_EXP_EXPR()
  * fchownat03: write a proper commit message, add more errnos to test

---
  .../kernel/syscalls/fchownat/fchownat01.c     | 153 +++++-------------

  1 file changed, 42 insertions(+), 111 deletions(-)
testcases/kernel/syscalls/fchownat/fchownat01.c +1 -4
index 7771c111a..fedcb8ab2 100644
@@ -1,133 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
   /*
- *   Copyright (c) International Business Machines  Corp., 2006
- *   AUTHOR: Yi Yang <[email protected]>
- *
- *   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.
- *
- *   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
+ * Copyright (c) International Business Machines  Corp., 2006
+ * Copyright (c) Linux Test Project, 2007-2025
   */

testcases/kernel/syscalls/fchownat/fchownat01.c +5 -4
-/*
- * DESCRIPTION
- *    This test case will verify basic function of fchownat
- *    added by kernel 2.6.16 or up.
+
+/*\
+ * [Description]
+ *
+ * Verify that fchownat() succeeds to change file's ownership if the 

file
+ * descriptor is AT_FDCWD or set by opening a directory. */
    #define _GNU_SOURCE
+#include "tst_test.h"
  -#include <sys/types.h>

testcases/kernel/syscalls/fchownat/fchownat01.c +2 -10
-#include <sys/stat.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/fcntl.h"
+#define TESTFILE1    "testfile1"
+#define TESTFILE2    "testfile2"

  -#define TESTFILE    "testfile"

testcases/kernel/syscalls/fchownat/fchownat01.c +3 -0
+static uid_t set_uid = 1000;
+static gid_t set_gid = 1000;
+static int dir_fd = -1;

  -static void setup(void);

testcases/kernel/syscalls/fchownat/fchownat01.c +1 -26
-static void cleanup(void);
-
-static int dir_fd;
-static int fd;
-static int no_fd = -1;
-static int cu_fd = AT_FDCWD;
-
-static struct test_case_t {
-    int exp_ret;
-    int exp_errno;
-    int flag;
-    int *fds;
-    char *filenames;
-} test_cases[] = {
-    {0, 0, 0, &dir_fd, TESTFILE},
-    {-1, ENOTDIR, 0, &fd, TESTFILE},
-    {-1, EBADF, 0, &no_fd, TESTFILE},
-    {-1, EINVAL, 9999, &dir_fd, TESTFILE},
-    {0, 0, 0, &cu_fd, TESTFILE},
-};
-
-char *TCID = "fchownat01";
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-static void fchownat_verify(const struct test_case_t *);
-
-int main(int ac, char **av)
+static void fchownat_verify(void)

  {
-    int lc;
-    int i;
+    struct stat stat_buf;
  -    tst_parse_opts(ac, av, NULL, NULL);
+    TST_EXP_PASS(fchownat(AT_FDCWD, TESTFILE1, set_uid, set_gid, 0), +             "fchownat(%d, %s, %d, %d, 0)",
+             AT_FDCWD, TESTFILE1, set_uid, set_gid);
  -    setup();
+    SAFE_STAT(TESTFILE1, &stat_buf);
+    TST_EXP_EQ_LI(stat_buf.st_uid, set_uid);
+    TST_EXP_EQ_LI(stat_buf.st_gid, set_gid);
  -    for (lc = 0; TEST_LOOPING(lc); lc++) {
-        tst_count = 0;
-        for (i = 0; i < TST_TOTAL; i++)
-            fchownat_verify(&test_cases[i]);
-    }
+    TST_EXP_PASS(fchownat(dir_fd, TESTFILE2, set_uid, set_gid, 0), +             "fchownat(%d, %s, %d, %d, 0)",
+             dir_fd, TESTFILE2, set_uid, set_gid);
  -    cleanup();
-    tst_exit();
+    SAFE_STAT(TESTFILE2, &stat_buf);
+    TST_EXP_EQ_LI(stat_buf.st_uid, set_uid);
+    TST_EXP_EQ_LI(stat_buf.st_gid, set_gid);
  }
    static void setup(void)
  {
-    tst_sig(NOFORK, DEF_HANDLER, cleanup);

testcases/kernel/syscalls/fchownat/fchownat01.c +3 -33
-
-    TEST_PAUSE;
-
-    tst_tmpdir();
-
-    dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
-
-    SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
-
-    fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
-}
-
-static void fchownat_verify(const struct test_case_t *test)
-{
-    TEST(fchownat(*(test->fds), test->filenames, geteuid(),
-              getegid(), test->flag));
-
-    if (TEST_RETURN != test->exp_ret) {
-        tst_resm(TFAIL | TTERRNO,
-             "fchownat() returned %ld, expected %d, errno=%d",
-             TEST_RETURN, test->exp_ret, test->exp_errno);
-        return;
-    }
-
-    if (TEST_ERRNO == test->exp_errno) {
-        tst_resm(TPASS | TTERRNO,
-             "fchownat() returned the expected errno %d: %s",
-             test->exp_ret, strerror(test->exp_errno));
-    } else {
-        tst_resm(TFAIL | TTERRNO,
-             "fchownat() failed unexpectedly; expected: %d - %s",
-             test->exp_errno, strerror(test->exp_errno));
-    }
+    dir_fd = SAFE_OPEN("./", O_DIRECTORY);
+    SAFE_TOUCH(TESTFILE1, 0600, NULL);
+    SAFE_TOUCH(TESTFILE2, 0600, NULL);

  }
    static void cleanup(void)
  {
-    if (fd > 0)
-        close(fd);

testcases/kernel/syscalls/fchownat/fchownat01.c +2 -5
-
-    if (dir_fd > 0)
-        close(dir_fd);
-
-    tst_rmdir();
+    if (dir_fd != -1)
+        SAFE_CLOSE(dir_fd);

  }

testcases/kernel/syscalls/fchownat/fchownat01.c +8 -0
+
+static struct tst_test test = {
+    .needs_tmpdir = 1,
+    .needs_root = 1,
+    .test_all = fchownat_verify,
+    .setup = setup,
+    .cleanup = cleanup,
+};
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author Reviewed-by Andrea Cervesato on v3 1/3
#8 lore raw
In reply to Shiyang Ruan via ltp

Hi!

All patch-set looks good. I'm gonna merge it, linking fchownat03 test inside runtest/syscalls file because it's missing from fchownat03 patch.

Thanks for the good job,
Andrea

Quoted 201 lines · Shiyang Ruan

1. convert to new API
2. move error tests to fchownat03
3. check if uid/gid is correctly set

---
V2: https://lore.kernel.org/ltp/[email protected]/

Changes:
* Initialize fd to -1, and check fd != -1 when cleanup
* Do not use TST_*_SILENT()
* fchownat01: describe what this case does in DESCRIPTION
* fchownat01: use TST_EXP_EQ_LI(); do not use test case struct
* fchownat02: use TST_EXP_EXPR()
* fchownat03: write a proper commit message, add more errnos to test

---
  .../kernel/syscalls/fchownat/fchownat01.c     | 153 +++++-------------
  1 file changed, 42 insertions(+), 111 deletions(-)
testcases/kernel/syscalls/fchownat/fchownat01.c +42 -111
index 7771c111a..fedcb8ab2 100644
@@ -1,133 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
  /*
- *   Copyright (c) International Business Machines  Corp., 2006
- *   AUTHOR: Yi Yang <[email protected]>
- *
- *   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.
- *
- *   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
+ * Copyright (c) International Business Machines  Corp., 2006
+ * Copyright (c) Linux Test Project, 2007-2025
   */
-/*
- * DESCRIPTION
- *	This test case will verify basic function of fchownat
- *	added by kernel 2.6.16 or up.
+
+/*\
+ * [Description]
+ *
+ * Verify that fchownat() succeeds to change file's ownership if the file
+ * descriptor is AT_FDCWD or set by opening a directory.
   */
 
  #define _GNU_SOURCE
+#include "tst_test.h"
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/fcntl.h"
+#define TESTFILE1	"testfile1"
+#define TESTFILE2	"testfile2"
 
-#define TESTFILE	"testfile"
+static uid_t set_uid = 1000;
+static gid_t set_gid = 1000;
+static int dir_fd = -1;
 
-static void setup(void);
-static void cleanup(void);
-
-static int dir_fd;
-static int fd;
-static int no_fd = -1;
-static int cu_fd = AT_FDCWD;
-
-static struct test_case_t {
-	int exp_ret;
-	int exp_errno;
-	int flag;
-	int *fds;
-	char *filenames;
-} test_cases[] = {
-	{0, 0, 0, &dir_fd, TESTFILE},
-	{-1, ENOTDIR, 0, &fd, TESTFILE},
-	{-1, EBADF, 0, &no_fd, TESTFILE},
-	{-1, EINVAL, 9999, &dir_fd, TESTFILE},
-	{0, 0, 0, &cu_fd, TESTFILE},
-};
-
-char *TCID = "fchownat01";
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-static void fchownat_verify(const struct test_case_t *);
-
-int main(int ac, char **av)
+static void fchownat_verify(void)
  {
-	int lc;
-	int i;
+	struct stat stat_buf;
 
-	tst_parse_opts(ac, av, NULL, NULL);
+	TST_EXP_PASS(fchownat(AT_FDCWD, TESTFILE1, set_uid, set_gid, 0),
+		     "fchownat(%d, %s, %d, %d, 0)",
+		     AT_FDCWD, TESTFILE1, set_uid, set_gid);
 
-	setup();
+	SAFE_STAT(TESTFILE1, &stat_buf);
+	TST_EXP_EQ_LI(stat_buf.st_uid, set_uid);
+	TST_EXP_EQ_LI(stat_buf.st_gid, set_gid);
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		for (i = 0; i < TST_TOTAL; i++)
-			fchownat_verify(&test_cases[i]);
-	}
+	TST_EXP_PASS(fchownat(dir_fd, TESTFILE2, set_uid, set_gid, 0),
+		     "fchownat(%d, %s, %d, %d, 0)",
+		     dir_fd, TESTFILE2, set_uid, set_gid);
 
-	cleanup();
-	tst_exit();
+	SAFE_STAT(TESTFILE2, &stat_buf);
+	TST_EXP_EQ_LI(stat_buf.st_uid, set_uid);
+	TST_EXP_EQ_LI(stat_buf.st_gid, set_gid);
  }
 
  static void setup(void)
  {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
-
-	SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
-
-	fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
-}
-
-static void fchownat_verify(const struct test_case_t *test)
-{
-	TEST(fchownat(*(test->fds), test->filenames, geteuid(),
-		      getegid(), test->flag));
-
-	if (TEST_RETURN != test->exp_ret) {
-		tst_resm(TFAIL | TTERRNO,
-			 "fchownat() returned %ld, expected %d, errno=%d",
-			 TEST_RETURN, test->exp_ret, test->exp_errno);
-		return;
-	}
-
-	if (TEST_ERRNO == test->exp_errno) {
-		tst_resm(TPASS | TTERRNO,
-			 "fchownat() returned the expected errno %d: %s",
-			 test->exp_ret, strerror(test->exp_errno));
-	} else {
-		tst_resm(TFAIL | TTERRNO,
-			 "fchownat() failed unexpectedly; expected: %d - %s",
-			 test->exp_errno, strerror(test->exp_errno));
-	}
+	dir_fd = SAFE_OPEN("./", O_DIRECTORY);
+	SAFE_TOUCH(TESTFILE1, 0600, NULL);
+	SAFE_TOUCH(TESTFILE2, 0600, NULL);
  }
 
  static void cleanup(void)
  {
-	if (fd > 0)
-		close(fd);
-
-	if (dir_fd > 0)
-		close(dir_fd);
-
-	tst_rmdir();
+	if (dir_fd != -1)
+		SAFE_CLOSE(dir_fd);
  }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.test_all = fchownat_verify,
+	.setup = setup,
+	.cleanup = cleanup,
+};
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp