Debian Codegraph @

testsuite/systemtap.syscall/rename.c

raw
1 /* COVERAGE: rename renameat renameat2 */
2
3 #define _GNU_SOURCE
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <sys/syscall.h>
10 #include <linux/fs.h>
11
12 // To test for glibc support for renameat():
13 //
14 // Since glibc 2.10:
15 // _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
16 // Before glibc 2.10:
17 // _ATFILE_SOURCE
18
19 #define GLIBC_SUPPORT \
20 (_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L \
21 || defined(_ATFILE_SOURCE))
22
23 // renameat2() was added in kernel 3.15. Since glibc support hasn't
24 // been added yet, we'll have to add our own.
25 #if defined(SYS_renameat2)
26 static inline int __renameat2(int olddirfd, const char *oldpath,
27 int newdirfd, const char *newpath,
28 unsigned int flags)
29 {
30 return syscall(SYS_renameat2, olddirfd, oldpath, newdirfd, newpath, flags);
31 }
32 #endif
33
34 int main()
35 {
36 int fd1;
37
38 fd1 = creat("file1", S_IREAD|S_IWRITE);
39 close (fd1);
40 fd1 = creat("file3", S_IREAD|S_IWRITE);
41 close (fd1);
42
43 mkdir("dir1", S_IREAD|S_IWRITE|S_IEXEC);
44 mkdir("dir3", S_IREAD|S_IWRITE|S_IEXEC);
45 mkdir("dir5", S_IREAD|S_IWRITE|S_IEXEC);
46
47 fd1 = creat("dir3/file", S_IREAD|S_IWRITE);
48 close (fd1);
49
50 rename("file1", "file2");
51 //staptest// [[[[rename ("file1", "file2"!!!!renameat (AT_FDCWD, "file1", AT_FDCWD, "file2"]]]]) = 0
52
53 rename("dir1", "dir2");
54 //staptest// [[[[rename ("dir1", "dir2"!!!!renameat (AT_FDCWD, "dir1", AT_FDCWD, "dir2"]]]]) = 0
55
56 // This will fail since the target isn't empty.
57 rename("dir2", "dir3");
58 //staptest// [[[[rename ("dir2", "dir3"!!!!renameat (AT_FDCWD, "dir2", AT_FDCWD, "dir3"]]]]) = -NNNN (ENOTEMPTY!!!!EEXIST)
59
60 // This will fail since you can't rename a file to a directory.
61 rename("file2", "dir2");
62 //staptest// [[[[rename ("file2", "dir2"!!!!renameat (AT_FDCWD, "file2", AT_FDCWD, "dir2"]]]]) = -NNNN (EISDIR)
63
64 // You can't rename a directory to a subdirectory of itself.
65 rename("dir2", "dir2/dir4");
66 //staptest// [[[[rename ("dir2", "dir2/dir4"!!!!renameat (AT_FDCWD, "dir2", AT_FDCWD, "dir2/dir4"]]]]) = -NNNN (EINVAL)
67
68 // You can't rename a directory to a file.
69 rename("dir2", "file2");
70 //staptest// [[[[rename ("dir2", "file2"!!!!renameat (AT_FDCWD, "dir2", AT_FDCWD, "file2"]]]]) = -NNNN (ENOTDIR)
71
72 rename("file2", (char *)-1);
73 #ifdef __s390__
74 //staptest// rename ("file2", 0x[7]?[f]+) = -NNNN (EFAULT)
75 #else
76 //staptest// [[[[rename ("file2", 0x[f]+!!!!renameat (AT_FDCWD, "file2", AT_FDCWD, 0x[f]+]]]]) = -NNNN (EFAULT)
77 #endif
78
79 rename((char *)-1, "file2");
80 #ifdef __s390__
81 //staptest// rename (0x[7]?[f]+, "file2") = -NNNN (EFAULT)
82 #else
83 //staptest// [[[[rename (0x[f]+, "file2"!!!!renameat (AT_FDCWD, 0x[f]+, AT_FDCWD, "file2"]]]]) = -NNNN (EFAULT)
84 #endif
85
86 #if GLIBC_SUPPORT
87 renameat(AT_FDCWD, "file2", AT_FDCWD, "file1");
88 //staptest// renameat (AT_FDCWD, "file2", AT_FDCWD, "file1") = 0
89
90 renameat(AT_FDCWD, "dir2", AT_FDCWD, "dir1");
91 //staptest// renameat (AT_FDCWD, "dir2", AT_FDCWD, "dir1") = 0
92
93 // This will fail since the target isn't empty.
94 renameat(AT_FDCWD, "dir1", AT_FDCWD, "dir3");
95 //staptest// renameat (AT_FDCWD, "dir1", AT_FDCWD, "dir3") = -NNNN (ENOTEMPTY!!!!EEXIST)
96
97 // This will fail since you can't rename a file to a directory.
98 renameat(AT_FDCWD, "file1", AT_FDCWD, "dir1");
99 //staptest// renameat (AT_FDCWD, "file1", AT_FDCWD, "dir1") = -NNNN (EISDIR)
100
101 // You can't rename a directory to a subdirectory of itself.
102 renameat(AT_FDCWD, "dir1", AT_FDCWD, "dir1/dir4");
103 //staptest// renameat (AT_FDCWD, "dir1", AT_FDCWD, "dir1/dir4") = -NNNN (EINVAL)
104
105 // You can't rename a directory to a file.
106 renameat(AT_FDCWD, "dir1", AT_FDCWD, "file1");
107 //staptest// renameat (AT_FDCWD, "dir1", AT_FDCWD, "file1") = -NNNN (ENOTDIR)
108
109 renameat(-1, "dir1", AT_FDCWD, "file1");
110 //staptest// renameat (-1, "dir1", AT_FDCWD, "file1") = -NNNN
111
112 renameat(AT_FDCWD, (char *)-1, AT_FDCWD, "file1");
113 #ifdef __s390__
114 //staptest// renameat (AT_FDCWD, 0x[7]?[f]+, AT_FDCWD, "file1") = -NNNN
115 #else
116 //staptest// renameat (AT_FDCWD, 0x[f]+, AT_FDCWD, "file1") = -NNNN
117 #endif
118
119 renameat(AT_FDCWD, "dir1", -1, "file1");
120 //staptest// renameat (AT_FDCWD, "dir1", -1, "file1") = -NNNN
121
122 renameat(AT_FDCWD, "file1", AT_FDCWD, (char *)-1);
123 #ifdef __s390__
124 //staptest// renameat (AT_FDCWD, "file1", AT_FDCWD, 0x[7]?[f]+) = -NNNN (EFAULT)
125 #else
126 //staptest// renameat (AT_FDCWD, "file1", AT_FDCWD, 0x[f]+) = -NNNN (EFAULT)
127 #endif
128 #endif
129
130 #if defined(SYS_renameat2)
131 // the renameat2 syscall is not implemented on ppc64le (kernel-3.10.0-294).
132 // arch/powerpc/include/asm/systbl.h has 'SYSCALL(ni_syscall) /* sys_renameat2 */'
133
134 __renameat2(AT_FDCWD, "file3", AT_FDCWD, "file4", 0);
135 //staptest// [[[[renameat2 (AT_FDCWD, "file3", AT_FDCWD, "file4", 0x0) = 0!!!!ni_syscall () = -NNNN]]]]
136
137 __renameat2(AT_FDCWD, "dir5", AT_FDCWD, "dir6", 0);
138 //staptest// [[[[renameat2 (AT_FDCWD, "dir5", AT_FDCWD, "dir6", 0x0) = 0!!!!ni_syscall () = -NNNN]]]]
139
140 // This should fail since the target exists (when using RENAME_NOREPLACE).
141 #ifdef RENAME_NOREPLACE
142 __renameat2(AT_FDCWD, "file4", AT_FDCWD, "file2", RENAME_NOREPLACE);
143 //staptest// [[[[renameat2 (AT_FDCWD, "file4", AT_FDCWD, "file2", RENAME_NOREPLACE)!!!!ni_syscall ()]]]] = NNNN
144 #endif
145
146 // This will fail since the target isn't empty.
147 __renameat2(AT_FDCWD, "dir6", AT_FDCWD, "dir3", 0);
148 //staptest// [[[[renameat2 (AT_FDCWD, "dir6", AT_FDCWD, "dir3", 0x0)!!!!ni_syscall ()]]]] = -NNNN (ENOTEMPTY!!!!EEXIST!!!!ENOSYS)
149
150 // This will fail since you can't rename a file to a directory.
151 __renameat2(AT_FDCWD, "file4", AT_FDCWD, "dir6", 0);
152 //staptest// [[[[renameat2 (AT_FDCWD, "file4", AT_FDCWD, "dir6", 0x0)!!!!ni_syscall ()]]]] = -NNNN (EISDIR!!!!ENOENT!!!!ENOSYS)
153
154 // Normally, this would fail since you can't rename a file to a
155 // directory. But with RENAME_EXCHANGE, the kernel will atomically
156 // exchange them. Note that since NFS filesystem's don't support
157 // RENAME_EXCHANGE, this call can fail.
158 #ifdef RENAME_EXCHANGE
159 __renameat2(AT_FDCWD, "file2", AT_FDCWD, "dir1", RENAME_EXCHANGE);
160 //staptest// [[[[renameat2 (AT_FDCWD, "file2", AT_FDCWD, "dir1", RENAME_EXCHANGE) = NNNN!!!!ni_syscall () = -NNNN]]]]
161 #endif
162
163 // You can't rename a directory to a subdirectory of itself.
164 __renameat2(AT_FDCWD, "dir6", AT_FDCWD, "dir6/dir4", 0);
165 //staptest// [[[[renameat2 (AT_FDCWD, "dir6", AT_FDCWD, "dir6/dir4", 0x0)!!!!ni_syscall ()]]]] = -NNNN (EINVAL!!!!ENOSYS)
166
167 // You can't rename a directory to a file without RENAME_EXCHANGE.
168 __renameat2(AT_FDCWD, "dir6", AT_FDCWD, "file4", 0);
169 #ifdef RENAME_EXCHANGE
170 //staptest// [[[[renameat2 (AT_FDCWD, "dir6", AT_FDCWD, "file4", 0x0)!!!!ni_syscall ()]]]] = NNNN
171 #else
172 //staptest// [[[[renameat2 (AT_FDCWD, "dir6", AT_FDCWD, "file4", 0x0)!!!!ni_syscall ()]]]] = -NNNN (ENOTDIR!!!!ENOSYS)
173 #endif
174
175 __renameat2(-1, "dir6", AT_FDCWD, "file4", 0);
176 //staptest// [[[[renameat2 (-1, "dir6", AT_FDCWD, "file4", 0x0)!!!!ni_syscall ()]]]] = -NNNN
177
178 __renameat2(AT_FDCWD, (char *)-1, AT_FDCWD, "file4", 0);
179 #ifdef __s390__
180 //staptest// renameat2 (AT_FDCWD, 0x[7]?[f]+, AT_FDCWD, "file4", 0x0) = -NNNN
181 #else
182 //staptest// [[[[renameat2 (AT_FDCWD, 0x[f]+, AT_FDCWD, "file4", 0x0)!!!!ni_syscall ()]]]] = -NNNN
183 #endif
184
185 __renameat2(AT_FDCWD, "dir6", -1, "file4", 0);
186 //staptest// [[[[renameat2 (AT_FDCWD, "dir6", -1, "file4", 0x0)!!!!ni_syscall ()]]]] = -NNNN
187
188 __renameat2(AT_FDCWD, "file4", AT_FDCWD, (char *)-1, 0);
189 #ifdef __s390__
190 //staptest// renameat2 (AT_FDCWD, "file4", AT_FDCWD, 0x[7]?[f]+, 0x0) = -NNNN (EFAULT)
191 #else
192 //staptest// [[[[renameat2 (AT_FDCWD, "file4", AT_FDCWD, 0x[f]+, 0x0)!!!!ni_syscall ()]]]] = -NNNN (EFAULT!!!!ENOSYS)
193 #endif
194
195 __renameat2(AT_FDCWD, "dir6", AT_FDCWD, "file4", -1);
196 //staptest// [[[[renameat2 (AT_FDCWD, "dir6", AT_FDCWD, "file4", RENAME_[^ ]+|XXXX)!!!!ni_syscall ()]]]] = -NNNN
197 #endif
198
199 return 0;
200 }