[LTP] [PATCH v3 7/8] syscalls/mknod07: Convert to new API

Ricardo B. Marlière rbm@suse.com
Mon Apr 14 22:09:38 CEST 2025


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

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

diff --git a/testcases/kernel/syscalls/mknod/mknod07.c b/testcases/kernel/syscalls/mknod/mknod07.c
index 829199061532fabffb1ba2c892cc8cccfd1d4b6a..13b883c36684bab7bf3933777656f74a057495c6 100644
--- a/testcases/kernel/syscalls/mknod/mknod07.c
+++ b/testcases/kernel/syscalls/mknod/mknod07.c
@@ -1,184 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
- *   Copyright (c) International Business Machines  Corp., 2001
- *   07/2001 Ported by Wayne Boyer
- *
- *   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., 2001
+ *	07/2001 Ported by Wayne Boyer
+ * Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <rbm@suse.com>
  */
 
-/*
- *
- * Test Description:
- * Verify that,
- *   1) mknod(2) returns -1 and sets errno to EPERM if the process id of
- *	the caller is not super-user.
- *   2) mknod(2) returns -1 and sets errno to EACCES if parent directory
- *	does not allow  write  permission  to  the process.
- *   3) mknod(2) returns -1 and sets errno to EROFS if pathname refers to
- *	a file on a read-only file system.
- *   4) mknod(2) returns -1 and sets errno to ELOOP if too many symbolic
- *	links were encountered in resolving pathname.
+/*\
+ * Verify that mknod(2) fails with the correct error codes:
  *
+ * - EACCES if parent directory does not allow write permission to the process.
+ * - EPERM if the process id of the caller is not super-user.
+ * - EROFS if pathname refers to a file on a read-only file system.
+ * - ELOOP if too many symbolic links were encountered in resolving pathname.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
 #include <pwd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/mount.h>
 #include <sys/sysmacros.h>
 
-#include "test.h"
-#include "safe_macros.h"
+#include "tst_test.h"
 
-#define DIR_TEMP		"testdir_1"
-#define DIR_TEMP_MODE		(S_IRUSR | S_IXUSR)
-#define DIR_MODE		(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
-				 S_IXGRP|S_IROTH|S_IXOTH)
-#define MNT_POINT		"mntpoint"
+#define TEMP_MNT "mnt"
+#define TEMP_DIR "testdir"
+#define TEMP_DIR_MODE 0500
 
-#define FIFO_MODE	(S_IFIFO | S_IRUSR | S_IRGRP | S_IROTH)
-#define SOCKET_MODE	(S_IFSOCK | S_IRWXU | S_IRWXG | S_IRWXO)
-#define CHR_MODE	(S_IFCHR | S_IRUSR | S_IWUSR)
-#define BLK_MODE	(S_IFBLK | S_IRUSR | S_IWUSR)
+#define ELOOP_DIR "test_eloop"
+#define ELOOP_FILE "/test_eloop"
+#define ELOOP_DIR_MODE 0755
+#define ELOOP_MAX 43
 
-#define ELOPFILE	"/test_eloop"
+#define FIFO_MODE 0010444
+#define SOCKET_MODE 00140777
+#define CHR_MODE 0020500
+#define BLK_MODE 0060500
 
-static char elooppathname[sizeof(ELOPFILE) * 43] = ".";
+static char *elooppathname;
 
-static const char *device;
-static int mount_flag;
-
-static struct test_case_t {
+static struct tcase {
 	char *pathname;
 	int mode;
 	int exp_errno;
 	int major, minor;
-} test_cases[] = {
-	{ "testdir_1/tnode_1", SOCKET_MODE, EACCES, 0, 0 },
-	{ "testdir_1/tnode_2", FIFO_MODE, EACCES, 0, 0 },
-	{ "tnode_3", CHR_MODE, EPERM, 1, 3 },
-	{ "tnode_4", BLK_MODE, EPERM, 0, 0 },
-	{ "mntpoint/tnode_5", SOCKET_MODE, EROFS, 0, 0 },
-	{ elooppathname, FIFO_MODE, ELOOP, 0, 0 },
+} tcases[] = {
+	{ NULL, FIFO_MODE, ELOOP, 0, 0 },
+	{ TEMP_DIR "/tnode1", SOCKET_MODE, EACCES, 0, 0 },
+	{ TEMP_DIR "/tnode2", FIFO_MODE, EACCES, 0, 0 },
+	{ "tnode3", CHR_MODE, EPERM, 1, 3 },
+	{ "tnode4", BLK_MODE, EPERM, 0, 0 },
+	{ TEMP_MNT "/tnode5", SOCKET_MODE, EROFS, 0, 0 },
 };
 
-char *TCID = "mknod07";
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-static void setup(void);
-static void mknod_verify(const struct test_case_t *test_case);
-static void cleanup(void);
+#define TEST_SIZE ARRAY_SIZE(tcases)
 
-int main(int ac, char **av)
+static void run(unsigned int i)
 {
-	int lc;
-	int i;
+	struct tcase *tc = &tcases[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++)
-			mknod_verify(&test_cases[i]);
-	}
-
-	cleanup();
-	tst_exit();
+	TST_EXP_FAIL(mknod(tc->pathname, tc->mode,
+			   makedev(tc->major, tc->minor)),
+		     tc->exp_errno);
 }
 
 static void setup(void)
 {
-	int i;
-	struct passwd *ltpuser;
-	const char *fs_type;
-
-	tst_require_root();
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_tmpdir();
+	struct passwd *ltpuser = SAFE_GETPWNAM("nobody");
 
-	fs_type = tst_dev_fs_type();
-	device = tst_acquire_device(cleanup);
+	SAFE_SETEUID(ltpuser->pw_uid);
+	SAFE_MKDIR(TEMP_DIR, TEMP_DIR_MODE);
 
-	if (!device)
-		tst_brkm(TCONF, cleanup, "Failed to acquire device");
-
-	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
-
-	TEST_PAUSE;
-
-	/* mount a read-only file system for EROFS test */
-	SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
-	SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type, MS_RDONLY, NULL);
-	mount_flag = 1;
-
-	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
-	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
-
-	SAFE_MKDIR(cleanup, DIR_TEMP, DIR_TEMP_MODE);
+	SAFE_MKDIR(ELOOP_DIR, ELOOP_DIR_MODE);
+	SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
 
 	/*
-	 * NOTE: the ELOOP test is written based on that the consecutive
-	 * symlinks limits in kernel is hardwired to 40.
+	 * The kernel limits symlink resolution hop amount to 40,
+	 * create a pathname with more than that
 	 */
-	SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
-	SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
-	for (i = 0; i < 43; i++)
-		strcat(elooppathname, ELOPFILE);
-}
-
-static void mknod_verify(const struct test_case_t *test_case)
-{
-	TEST(mknod(test_case->pathname, test_case->mode,
-		makedev(test_case->major, test_case->minor)));
-
-	if (TEST_RETURN != -1) {
-		tst_resm(TFAIL, "mknod succeeded unexpectedly");
-		return;
-	}
-
-	if (TEST_ERRNO == test_case->exp_errno) {
-		tst_resm(TPASS | TTERRNO, "mknod failed as expected");
-	} else {
-		tst_resm(TFAIL | TTERRNO,
-			 "mknod failed unexpectedly; expected: "
-			 "%d - %s", test_case->exp_errno,
-			 strerror(test_case->exp_errno));
-	}
+	strcpy(elooppathname, ".");
+	for (int i = 0; i < ELOOP_MAX; i++)
+		strcat(elooppathname, ELOOP_FILE);
+	tcases[0].pathname = elooppathname;
 }
 
-static void cleanup(void)
-{
-	if (seteuid(0) == -1)
-		tst_resm(TWARN | TERRNO, "seteuid(0) failed");
-
-	if (mount_flag && tst_umount(MNT_POINT) < 0)
-		tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
-
-	if (device)
-		tst_release_device(device);
-
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.setup = setup,
+	.test = run,
+	.tcnt = ARRAY_SIZE(tcases),
+	.mntpoint = TEMP_MNT,
+	.needs_rofs = 1,
+	.bufs = (struct tst_buffers[]){
+		{ &elooppathname, .size = sizeof(ELOOP_FILE) * ELOOP_MAX },
+		{},
+	},
+};

-- 
2.49.0



More information about the ltp mailing list