• 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 <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <signal.h>
36 #include "test.h"
37 #include "safe_macros.h"
38 #include "lapi/syscalls.h"
39 
40 #define TEST_CASES 7
41 #ifndef AT_FDCWD
42 #define AT_FDCWD -100
43 #endif
44 #ifndef AT_REMOVEDIR
45 #define AT_REMOVEDIR 0x200
46 #endif
47 
48 void setup();
49 void cleanup();
50 
51 char *TCID = "unlinkat01";
52 int TST_TOTAL = TEST_CASES;
53 
54 static const char pathname[] = "unlinkattestdir",
55 		  subpathname[] = "unlinkatsubtestdir",
56 		  subpathdir[] = "unlinkattestdir/unlinkatsubtestdir",
57 		  testfile[] = "unlinkattestfile.txt",
58 		  testfile2[] = "unlinkattestdir/unlinkattestfile.txt";
59 static char *testfile3;
60 
61 static int fds[TEST_CASES];
62 static const char *filenames[TEST_CASES];
63 static const int expected_errno[] = { 0, 0, ENOTDIR, EBADF, EINVAL, 0, 0 };
64 static const int flags[] = { 0, 0, 0, 0, 9999, 0, AT_REMOVEDIR };
65 
myunlinkat(int dirfd,const char * filename,int flags)66 int myunlinkat(int dirfd, const char *filename, int flags)
67 {
68 	return ltp_syscall(__NR_unlinkat, dirfd, filename, flags);
69 }
70 
main(int ac,char ** av)71 int main(int ac, char **av)
72 {
73 	int lc;
74 	int i;
75 
76 	if ((tst_kvercmp(2, 6, 16)) < 0)
77 		tst_brkm(TCONF, NULL, "Test must be run with kernel 2.6.16+");
78 
79 	tst_parse_opts(ac, av, NULL, NULL);
80 
81 	setup();
82 
83 	for (lc = 0; TEST_LOOPING(lc); lc++) {
84 		tst_count = 0;
85 
86 		for (i = 0; i < TST_TOTAL; i++) {
87 			TEST(myunlinkat(fds[i], filenames[i], flags[i]));
88 
89 			if (TEST_ERRNO == expected_errno[i]) {
90 				tst_resm(TPASS | TTERRNO,
91 					 "unlinkat() returned expected errno");
92 			} else {
93 				tst_resm(TFAIL | TTERRNO, "unlinkat() failed");
94 			}
95 		}
96 
97 	}
98 
99 	cleanup();
100 	tst_exit();
101 }
102 
setup(void)103 void setup(void)
104 {
105 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
106 
107 	tst_tmpdir();
108 
109 	char *abs_path = tst_get_tmpdir();
110 
111 	SAFE_ASPRINTF(cleanup, &testfile3, "%s/unlinkatfile3.txt", abs_path);
112 
113 	free(abs_path);
114 
115 	SAFE_MKDIR(cleanup, pathname, 0700);
116 	SAFE_MKDIR(cleanup, subpathdir, 0700);
117 
118 	fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
119 	fds[1] = fds[4] = fds[6] = fds[0];
120 
121 	SAFE_FILE_PRINTF(cleanup, testfile, testfile);
122 	SAFE_FILE_PRINTF(cleanup, testfile2, testfile2);
123 
124 	fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
125 
126 	fds[3] = 100;
127 	fds[5] = AT_FDCWD;
128 
129 	filenames[0] = filenames[2] = filenames[3] = filenames[4] =
130 	    filenames[5] = testfile;
131 	filenames[1] = testfile3;
132 	filenames[6] = subpathname;
133 
134 	TEST_PAUSE;
135 }
136 
cleanup(void)137 void cleanup(void)
138 {
139 	if (fds[0] > 0)
140 		close(fds[0]);
141 	if (fds[2] > 0)
142 		close(fds[2]);
143 
144 	free(testfile3);
145 	tst_rmdir();
146 }
147