[LTP] [PATCH 2/4] syscalls: lchown01: Convert to new API

Ricardo B. Marlière rbm@suse.com
Wed Jul 2 12:25:42 CEST 2025


From: Ricardo B. Marlière <rbm@suse.com>

Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
---
 testcases/kernel/syscalls/lchown/lchown01.c | 198 +++++++++-------------------
 1 file changed, 59 insertions(+), 139 deletions(-)

diff --git a/testcases/kernel/syscalls/lchown/lchown01.c b/testcases/kernel/syscalls/lchown/lchown01.c
index aaa0ef4038a1aad23212ec2d188274ddd7fcf50d..992ff72949c11963e345928cb81efae5be93d128 100644
--- a/testcases/kernel/syscalls/lchown/lchown01.c
+++ b/testcases/kernel/syscalls/lchown/lchown01.c
@@ -1,59 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * 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 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
- */
-
-/*
- * Test Description:
- *  Verify that, lchown(2) succeeds to change the owner and group of a file
- *  specified by path to any numeric owner(uid)/group(gid) values when invoked
- *  by super-user.
- *
- * Expected Result:
- *  lchown(2) should return 0 and the ownership set on the file should match
- *  the numeric values contained in owner and group respectively.
- *
- * HISTORY
  *	07/2001 Ported by Wayne Boyer
  *	11/2010 Code cleanup by Cyril Hrubis chrubis@suse.cz
+ * Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <rbm@suse.com>
  */
 
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-
-#include "test.h"
-#include "safe_macros.h"
-
-/*
- * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
- * rewriting all tests to the new API.
+/*\
+ * Verify that, lchown(2) succeeds to change the owner and group of a file
+ * specified by path to any numeric owner(uid)/group(gid) values when invoked
+ * by super-user.
+ *
+ * lchown(2) should return 0 and the ownership set on the file should match
+ * the numeric values contained in owner and group respectively.
  */
-#include "compat_16.h"
 
-#define FILE_MODE	(S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
-#define TESTFILE	"testfile"
-#define SFILE		"slink_file"
+#include "tst_test.h"
 
-TCID_DEFINE(lchown01);
-int TST_TOTAL = 5;
+#define FILE_MODE (S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
+#define TESTFILE "testfile"
+#define SFILE "slink_file"
 
 struct test_case_t {
 	char *desc;
@@ -62,114 +28,68 @@ struct test_case_t {
 };
 
 static struct test_case_t test_cases[] = {
-	{"Change Owner/Group ids", 700, 701},
-	{"Change Owner id only", 702, -1},
-	{"Change Owner/Group ids", 703, 701},
-	{"Change Group id only", -1, 704},
-	{"Change Group/Group ids", 703, 705},
-	{"Change none", -1, -1},
-	{NULL, 0, 0}
+	{ "Change Owner/Group ids", 700, 701 },
+	{ "Change Owner id only", 702, -1 },
+	{ "Change Owner/Group ids", 703, 701 },
+	{ "Change Group id only", -1, 704 },
+	{ "Change Group/Group ids", 703, 705 },
+	{ "Change none", -1, -1 },
 };
 
-static void setup(void);
-static void cleanup(void);
-
-int main(int argc, char *argv[])
+static void run(unsigned int i)
 {
 	struct stat stat_buf;
-	int lc;
-	int i;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
+	struct test_case_t *tc = &test_cases[i];
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
+	uid_t user_id = tc->user_id;
+	gid_t group_id = tc->group_id;
+	char *test_desc = tc->desc;
 
-		tst_count = 0;
+	/*
+	 * Call lchown(2) with different user id and group id (numeric values)
+	 * to set it on symlink of testfile.
+	 */
+	SAFE_LCHOWN(SFILE, user_id, group_id);
 
-		for (i = 0; test_cases[i].desc != NULL; i++) {
-			uid_t user_id = test_cases[i].user_id;
-			gid_t group_id = test_cases[i].group_id;
-			char *test_desc = test_cases[i].desc;
+	SAFE_LSTAT(SFILE, &stat_buf);
 
-			/*
-			 * Call lchown(2) with different user id and
-			 * group id (numeric values) to set it on
-			 * symlink of testfile.
-			 */
-			TEST(LCHOWN(cleanup, SFILE, user_id, group_id));
-
-			if (TEST_RETURN == -1) {
-				tst_resm(TFAIL,
-					 "lchown() Fails to %s, errno %d",
-					 test_desc, TEST_ERRNO);
-				continue;
-			}
-
-			if (lstat(SFILE, &stat_buf) < 0) {
-				tst_brkm(TFAIL, cleanup, "lstat(2) "
-					 "%s failed, errno %d",
-					 SFILE, TEST_ERRNO);
-			}
-
-			if ((int)user_id == -1) {
-				if (i > 0)
-					user_id =
-					    test_cases[i - 1].user_id;
-				else
-					user_id = geteuid();
-			}
-
-			if ((int)group_id == -1) {
-				if (i > 0)
-					group_id =
-					    test_cases[i - 1].group_id;
-				else
-					group_id = getegid();
-			}
+	if ((int)user_id == -1) {
+		if (i > 0)
+			user_id = test_cases[i - 1].user_id;
+		else
+			user_id = geteuid();
+	}
 
-			/*
-			 * Check for expected Ownership ids
-			 * set on testfile.
-			 */
-			if ((stat_buf.st_uid != user_id) ||
-			    (stat_buf.st_gid != group_id)) {
-				tst_resm(TFAIL,
-					 "%s: incorrect ownership set, "
-					 "Expected %d %d", SFILE,
-					 user_id, group_id);
-			} else {
-				tst_resm(TPASS, "lchown() succeeds to "
-					 "%s of %s", test_desc, SFILE);
-			}
-		}
+	if ((int)group_id == -1) {
+		if (i > 0)
+			group_id = test_cases[i - 1].group_id;
+		else
+			group_id = getegid();
 	}
 
-	cleanup();
-	tst_exit();
+	/* Check for expected ownership ids set on testfile. */
+	if ((stat_buf.st_uid != user_id) || (stat_buf.st_gid != group_id)) {
+		tst_res(TFAIL, "%s: incorrect ownership set, expected %d %d",
+			SFILE, user_id, group_id);
+	} else {
+		tst_res(TPASS, "lchown() succeeds to %s of %s", test_desc,
+			SFILE);
+	}
 }
 
 static void setup(void)
 {
 	int fd;
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_require_root();
-
-	TEST_PAUSE;
-	tst_tmpdir();
-
-	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
-		tst_brkm(TBROK, cleanup, "open failed");
-	}
-	SAFE_CLOSE(cleanup, fd);
-
-	SAFE_SYMLINK(cleanup, TESTFILE, SFILE);
+	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
+	SAFE_CLOSE(fd);
+	SAFE_SYMLINK(TESTFILE, SFILE);
 }
 
-static void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(test_cases),
+	.test = run,
+	.setup = setup,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};

-- 
2.50.0



More information about the ltp mailing list