• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
3  * Copyright (c) International Business Machines  Corp., 2006
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it would be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * DESCRIPTION
19  *	This test case will verify basic function of unlinkat
20  *	added by kernel 2.6.16 or up.
21  *
22  * Author
23  *	Yi Yang <yyangcdl@cn.ibm.com>
24  */
25 
26 #define _GNU_SOURCE
27 
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <fcntl.h>
32 #include <error.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <signal.h>
37 #include "test.h"
38 #include "safe_macros.h"
39 #include "linux_syscall_numbers.h"
40 
41 #define TEST_CASES 7
42 #ifndef AT_FDCWD
43 #define AT_FDCWD -100
44 #endif
45 #ifndef AT_REMOVEDIR
46 #define AT_REMOVEDIR 0x200
47 #endif
48 
49 void setup();
50 void cleanup();
51 
52 char *TCID = "unlinkat01";
53 int TST_TOTAL = TEST_CASES;
54 
55 static const char pathname[] = "unlinkattestdir",
56 		  subpathname[] = "unlinkatsubtestdir",
57 		  subpathdir[] = "unlinkattestdir/unlinkatsubtestdir",
58 		  testfile[] = "unlinkattestfile.txt",
59 		  testfile2[] = "unlinkattestdir/unlinkattestfile.txt";
60 static char *testfile3;
61 
62 static int fds[TEST_CASES];
63 static const char *filenames[TEST_CASES];
64 static const int expected_errno[] = { 0, 0, ENOTDIR, EBADF, EINVAL, 0, 0 };
65 static const int flags[] = { 0, 0, 0, 0, 9999, 0, AT_REMOVEDIR };
66 
myunlinkat(int dirfd,const char * filename,int flags)67 int myunlinkat(int dirfd, const char *filename, int flags)
68 {
69 	return ltp_syscall(__NR_unlinkat, dirfd, filename, flags);
70 }
71 
main(int ac,char ** av)72 int main(int ac, char **av)
73 {
74 	int lc;
75 	int i;
76 
77 	if ((tst_kvercmp(2, 6, 16)) < 0)
78 		tst_brkm(TCONF, NULL, "Test must be run with kernel 2.6.16+");
79 
80 	tst_parse_opts(ac, av, NULL, NULL);
81 
82 	setup();
83 
84 	for (lc = 0; TEST_LOOPING(lc); lc++) {
85 		tst_count = 0;
86 
87 		for (i = 0; i < TST_TOTAL; i++) {
88 			TEST(myunlinkat(fds[i], filenames[i], flags[i]));
89 
90 			if (TEST_ERRNO == expected_errno[i]) {
91 				tst_resm(TPASS | TTERRNO,
92 					 "unlinkat() returned expected errno");
93 			} else {
94 				tst_resm(TFAIL | TTERRNO, "unlinkat() failed");
95 			}
96 		}
97 
98 	}
99 
100 	cleanup();
101 	tst_exit();
102 }
103 
setup(void)104 void setup(void)
105 {
106 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
107 
108 	tst_tmpdir();
109 
110 	char *abs_path = tst_get_tmpdir();
111 
112 	SAFE_ASPRINTF(cleanup, &testfile3, "%s/unlinkatfile3.txt", abs_path);
113 
114 	free(abs_path);
115 
116 	SAFE_MKDIR(cleanup, pathname, 0700);
117 	SAFE_MKDIR(cleanup, subpathdir, 0700);
118 
119 	fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
120 	fds[1] = fds[4] = fds[6] = fds[0];
121 
122 	SAFE_FILE_PRINTF(cleanup, testfile, testfile);
123 	SAFE_FILE_PRINTF(cleanup, testfile2, testfile2);
124 
125 	fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
126 
127 	fds[3] = 100;
128 	fds[5] = AT_FDCWD;
129 
130 	filenames[0] = filenames[2] = filenames[3] = filenames[4] =
131 	    filenames[5] = testfile;
132 	filenames[1] = testfile3;
133 	filenames[6] = subpathname;
134 
135 	TEST_PAUSE;
136 }
137 
cleanup(void)138 void cleanup(void)
139 {
140 	if (fds[0] > 0)
141 		close(fds[0]);
142 	if (fds[2] > 0)
143 		close(fds[2]);
144 
145 	free(testfile3);
146 	tst_rmdir();
147 }
148