1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2009-2021
4f08c3bdfSopenharmony_ci * Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
5f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2006
6f08c3bdfSopenharmony_ci * Author: Yi Yang <yyangcdl@cn.ibm.com>
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci/*\
10f08c3bdfSopenharmony_ci * [Description]
11f08c3bdfSopenharmony_ci * Basic unlinkat() test.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include "tst_test.h"
15f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
16f08c3bdfSopenharmony_ci#include "tst_safe_stdio.h"
17f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic const char pathname[] = "unlinkattestdir",
20f08c3bdfSopenharmony_ci		  subpathname[] = "unlinkatsubtestdir",
21f08c3bdfSopenharmony_ci		  subpathdir[] = "unlinkattestdir/unlinkatsubtestdir",
22f08c3bdfSopenharmony_ci		  testfile[] = "unlinkattestfile.txt",
23f08c3bdfSopenharmony_ci		  testfile2[] = "unlinkattestdir/unlinkattestfile.txt";
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic char *testfile3;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic int fd;
28f08c3bdfSopenharmony_cistatic int getfd(int i)
29f08c3bdfSopenharmony_ci{
30f08c3bdfSopenharmony_ci	if (i == 2)
31f08c3bdfSopenharmony_ci		fd = SAFE_OPEN(testfile3, O_CREAT | O_RDWR, 0600);
32f08c3bdfSopenharmony_ci	else
33f08c3bdfSopenharmony_ci		fd = SAFE_OPEN(pathname, O_DIRECTORY);
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	return fd;
36f08c3bdfSopenharmony_ci}
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic struct tcase {
39f08c3bdfSopenharmony_ci	int fd;
40f08c3bdfSopenharmony_ci	const char *filename;
41f08c3bdfSopenharmony_ci	int flag;
42f08c3bdfSopenharmony_ci	int exp_errno;
43f08c3bdfSopenharmony_ci} tc[] = {
44f08c3bdfSopenharmony_ci	{0, testfile, 0, 0},
45f08c3bdfSopenharmony_ci	{0, NULL, 0, 0},
46f08c3bdfSopenharmony_ci	{0, testfile, 0, ENOTDIR},
47f08c3bdfSopenharmony_ci	{100, testfile, 0, EBADF},
48f08c3bdfSopenharmony_ci	{0, testfile, 9999, EINVAL},
49f08c3bdfSopenharmony_ci	{AT_FDCWD, testfile, 0, 0},
50f08c3bdfSopenharmony_ci	{0, subpathname, AT_REMOVEDIR, 0},
51f08c3bdfSopenharmony_ci};
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic void run(unsigned int i)
54f08c3bdfSopenharmony_ci{
55f08c3bdfSopenharmony_ci	int fd3 = -1;
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	/* tesfile2 will be unlinked by test0. */
58f08c3bdfSopenharmony_ci	if (access(testfile2, F_OK))
59f08c3bdfSopenharmony_ci		SAFE_FILE_PRINTF(testfile2, testfile2);
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	/* testfile3 will be unlined by test1. */
62f08c3bdfSopenharmony_ci	if (access(testfile3, F_OK))
63f08c3bdfSopenharmony_ci		fd3 = SAFE_OPEN(testfile3, O_CREAT | O_RDWR, 0600);
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	/* subpathdir will be unlinked by test6. */
66f08c3bdfSopenharmony_ci	if (access(subpathdir, F_OK))
67f08c3bdfSopenharmony_ci		SAFE_MKDIR(subpathdir, 0700);
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	/* testfile must exist except test1 and test6. */
70f08c3bdfSopenharmony_ci	if (access(testfile, F_OK))
71f08c3bdfSopenharmony_ci		SAFE_FILE_PRINTF(testfile, testfile);
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	if (tc[i].fd)
74f08c3bdfSopenharmony_ci		TEST(unlinkat(tc[i].fd, tc[i].filename, tc[i].flag));
75f08c3bdfSopenharmony_ci	else
76f08c3bdfSopenharmony_ci		TEST(unlinkat(getfd(i), tc[i].filename, tc[i].flag));
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	if (TST_ERR == tc[i].exp_errno)
79f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "unlinkat() returned expected errno");
80f08c3bdfSopenharmony_ci	else
81f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "unlinkat() failed");
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	if (!tc[i].fd)
84f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	if (fd3 > 0)
87f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd3);
88f08c3bdfSopenharmony_ci}
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_cistatic void setup(void)
91f08c3bdfSopenharmony_ci{
92f08c3bdfSopenharmony_ci	char buf[PATH_MAX];
93f08c3bdfSopenharmony_ci	SAFE_GETCWD(buf, PATH_MAX);
94f08c3bdfSopenharmony_ci	SAFE_ASPRINTF(&testfile3, "%s/unlinkatfile3.txt", buf);
95f08c3bdfSopenharmony_ci	tc[1].filename = testfile3;
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci	SAFE_MKDIR(pathname, 0700);
98f08c3bdfSopenharmony_ci}
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_cistatic void cleanup(void)
101f08c3bdfSopenharmony_ci{
102f08c3bdfSopenharmony_ci	SAFE_UNLINK(testfile);
103f08c3bdfSopenharmony_ci	SAFE_UNLINK(testfile2);
104f08c3bdfSopenharmony_ci	SAFE_RMDIR(pathname);
105f08c3bdfSopenharmony_ci}
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_cistatic struct tst_test test = {
108f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
109f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tc),
110f08c3bdfSopenharmony_ci	.setup = setup,
111f08c3bdfSopenharmony_ci	.test = run,
112f08c3bdfSopenharmony_ci	.cleanup = cleanup,
113f08c3bdfSopenharmony_ci};
114