1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci * Ported to LTP: Wayne Boyer
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci * DESCRIPTION
8f08c3bdfSopenharmony_ci *	check mkdir() with various error conditions that should produce
9f08c3bdfSopenharmony_ci *	EFAULT, ENAMETOOLONG, EEXIST, ENOENT, ENOTDIR, ELOOP and EROFS
10f08c3bdfSopenharmony_ci */
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#include <errno.h>
13f08c3bdfSopenharmony_ci#include <sys/types.h>
14f08c3bdfSopenharmony_ci#include <sys/stat.h>
15f08c3bdfSopenharmony_ci#include <sys/mman.h>
16f08c3bdfSopenharmony_ci#include <fcntl.h>
17f08c3bdfSopenharmony_ci#include <sys/mount.h>
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#define TST_EEXIST	"tst_eexist"
22f08c3bdfSopenharmony_ci#define TST_ENOENT	"tst_enoent/tst"
23f08c3bdfSopenharmony_ci#define TST_ENOTDIR_FILE "tst_enotdir"
24f08c3bdfSopenharmony_ci#define TST_ENOTDIR_DIR	"tst_enotdir/tst"
25f08c3bdfSopenharmony_ci#define MODE		0777
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#define MNT_POINT	"mntpoint"
28f08c3bdfSopenharmony_ci#define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
29f08c3bdfSopenharmony_ci			 S_IXGRP|S_IROTH|S_IXOTH)
30f08c3bdfSopenharmony_ci#define TST_EROFS      "mntpoint/tst_erofs"
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_cistatic char long_dir[PATH_MAX + 2] = {[0 ... PATH_MAX + 1] = 'a'};
33f08c3bdfSopenharmony_cistatic char loop_dir[PATH_MAX] = ".";
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistruct tcase;
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic struct tcase {
38f08c3bdfSopenharmony_ci	char *pathname;
39f08c3bdfSopenharmony_ci	int exp_errno;
40f08c3bdfSopenharmony_ci} TC[] = {
41f08c3bdfSopenharmony_ci	{NULL, EFAULT},
42f08c3bdfSopenharmony_ci	{long_dir, ENAMETOOLONG},
43f08c3bdfSopenharmony_ci	{TST_EEXIST, EEXIST},
44f08c3bdfSopenharmony_ci	{TST_ENOENT, ENOENT},
45f08c3bdfSopenharmony_ci	{TST_ENOTDIR_DIR, ENOTDIR},
46f08c3bdfSopenharmony_ci	{loop_dir, ELOOP},
47f08c3bdfSopenharmony_ci	{TST_EROFS, EROFS},
48f08c3bdfSopenharmony_ci};
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic void verify_mkdir(unsigned int n)
51f08c3bdfSopenharmony_ci{
52f08c3bdfSopenharmony_ci	struct tcase *tc = TC + n;
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	TEST(mkdir(tc->pathname, MODE));
55f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
56f08c3bdfSopenharmony_ci		tst_res(TFAIL, "mkdir() returned %ld, expected -1, errno=%d",
57f08c3bdfSopenharmony_ci			TST_RET, tc->exp_errno);
58f08c3bdfSopenharmony_ci		return;
59f08c3bdfSopenharmony_ci	}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	if (TST_ERR == tc->exp_errno) {
62f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "mkdir() failed as expected");
63f08c3bdfSopenharmony_ci	} else {
64f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
65f08c3bdfSopenharmony_ci			"mkdir() failed unexpectedly; expected: %d - %s",
66f08c3bdfSopenharmony_ci			 tc->exp_errno, strerror(tc->exp_errno));
67f08c3bdfSopenharmony_ci	}
68f08c3bdfSopenharmony_ci}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cistatic void setup(void)
71f08c3bdfSopenharmony_ci{
72f08c3bdfSopenharmony_ci	unsigned int i;
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	SAFE_TOUCH(TST_EEXIST, MODE, NULL);
75f08c3bdfSopenharmony_ci	SAFE_TOUCH(TST_ENOTDIR_FILE, MODE, NULL);
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	for (i = 0; i < ARRAY_SIZE(TC); i++) {
78f08c3bdfSopenharmony_ci		if (TC[i].exp_errno == EFAULT)
79f08c3bdfSopenharmony_ci			TC[i].pathname = tst_get_bad_addr(NULL);
80f08c3bdfSopenharmony_ci	}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	SAFE_MKDIR("test_eloop", DIR_MODE);
83f08c3bdfSopenharmony_ci	SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
84f08c3bdfSopenharmony_ci	for (i = 0; i < 43; i++)
85f08c3bdfSopenharmony_ci		strcat(loop_dir, "/test_eloop");
86f08c3bdfSopenharmony_ci}
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_cistatic struct tst_test test = {
89f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(TC),
90f08c3bdfSopenharmony_ci	.needs_root = 1,
91f08c3bdfSopenharmony_ci	.needs_rofs = 1,
92f08c3bdfSopenharmony_ci	.mntpoint = MNT_POINT,
93f08c3bdfSopenharmony_ci	.setup = setup,
94f08c3bdfSopenharmony_ci	.test = verify_mkdir,
95f08c3bdfSopenharmony_ci};
96