Debian Codegraph @

testsuite/systemtap.syscall/stat.c

raw
1 /* COVERAGE: getcwd fstat stat lstat fstatat fstatat64 utime */
2 /* COVERAGE: fstat64 stat64 lstat64 */
3 /* COVERAGE: newfstat newfstatat newlstat newstat */
4 /* COVERAGE: statx */
5 #define _GNU_SOURCE
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/syscall.h>
10 #include <fcntl.h>
11 #include <utime.h>
12 #include <string.h>
13 #include <time.h>
14 #include <stdio.h>
15 #include <sys/mman.h>
16
17 #ifdef __NR_statx
18 /* For struct statx */
19 #include <linux/stat.h>
20 /* The AT_STATX_FORCE_SYNC is defined within linux/fcntl.h,
21 * but including that would cause conflict with already included fcntl.h
22 */
23 #ifndef AT_STATX_FORCE_SYNC
24 #define AT_STATX_FORCE_SYNC 0x2000
25 #endif
26 #endif
27
28 // To test for glibc support for fstatat():
29 //
30 // Since glibc 2.10:
31 // _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
32 // Before glibc 2.10:
33 // _ATFILE_SOURCE
34
35 #define GLIBC_SUPPORT \
36 (_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L \
37 || defined(_ATFILE_SOURCE))
38
39 #if !__GLIBC_PREREQ(2, 28)
40 #ifdef __NR_statx
41 static inline
42 ssize_t statx(int dfd, const char *filename, unsigned flags,
43 unsigned int mask, struct statx *buffer)
44 {
45 return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
46 }
47 #endif
48 #endif
49
50 int main()
51 {
52 int fd;
53 char cwd[128];
54 struct stat sbuf;
55 struct utimbuf ubuf;
56 #ifdef __NR_statx
57 struct statx stx;
58 #endif
59
60 mlockall(MCL_CURRENT);
61
62 getcwd(cwd, 128);
63 //staptest// getcwd (XXXX, 128) = NNNN
64
65 fd = creat("foobar", S_IREAD|S_IWRITE);
66 //staptest// [[[[open ("foobar", O_WRONLY|O_CREAT[[[[.O_LARGEFILE]]]]?|O_TRUNC!!!!creat ("foobar"!!!!openat (AT_FDCWD, "foobar", O_WRONLY|O_CREAT|O_TRUNC]]]], 0600) = NNNN
67
68 fstat(fd, &sbuf);
69 //staptest// fstat (NNNN, XXXX) = 0
70
71 fstat(-1, &sbuf);
72 //staptest// fstat (-1, XXXX) = -NNNN (EBADF)
73
74 // Here we specify -1 to both arguments, to avoid a SIGSEGV.
75 fstat(-1, (struct stat *)-1);
76 #if __WORDSIZE != 64
77 // Notice we're not checking for 0x[f]+ here for the 2nd
78 // argument. On RHEL[67] {x86_64,s390x}, for a 32-bit exe, glibc
79 // substituted a real structure address (verified with strace).
80 // staptest// fstat (-1, XXXX) = -NNNN
81 #else
82 //staptest// fstat (-1, 0x[f]+) = -NNNN
83 #endif
84
85 #ifdef __NR_statx
86 memset(&stx, 0xbf, sizeof(stx));
87 statx(AT_FDCWD, "foobar", AT_SYMLINK_NOFOLLOW, AT_STATX_FORCE_SYNC, &stx);
88 //staptest// statx (AT_FDCWD, "foobar", AT_SYMLINK_NOFOLLOW, AT_STATX_FORCE_SYNC, XXXX) = 0
89
90 statx((int)-1, "foobar", AT_SYMLINK_NOFOLLOW, AT_STATX_FORCE_SYNC, &stx);
91 //staptest// statx (-1, "foobar", AT_SYMLINK_NOFOLLOW, AT_STATX_FORCE_SYNC, XXXX) = -NNNN (EBADF)
92
93 statx(AT_FDCWD, (const char *)-1, AT_SYMLINK_NOFOLLOW, AT_STATX_FORCE_SYNC, &stx);
94 #ifdef __s390__
95 //staptest// statx (AT_FDCWD, 0x[7]?[f]+, AT_SYMLINK_NOFOLLOW, AT_STATX_FORCE_SYNC, XXXX) = -NNNN (EFAULT)
96 #else
97 //staptest// statx (AT_FDCWD, 0x[f]+, AT_SYMLINK_NOFOLLOW, AT_STATX_FORCE_SYNC, XXXX) = -NNNN (EFAULT)
98 #endif
99
100 statx(AT_FDCWD, "foobar", (unsigned)-1, AT_STATX_FORCE_SYNC, &stx);
101 //staptest// statx (AT_FDCWD, "foobar", AT_SYMLINK_NOFOLLOW|AT_REMOVEDIR|AT_SYMLINK_FOLLOW|AT_NO_AUTOMOUNT|AT_EMPTY_PATH|XXXX, AT_STATX_FORCE_SYNC, XXXX) = -NNNN (EINVAL)
102
103 statx(AT_FDCWD, "foobar", AT_SYMLINK_NOFOLLOW, (unsigned)-1, &stx);
104 //staptest// statx (AT_FDCWD, "foobar", AT_SYMLINK_NOFOLLOW, 0x[f]+, XXXX) = NNNN
105
106 statx(AT_FDCWD, "foobar", AT_SYMLINK_NOFOLLOW, AT_STATX_FORCE_SYNC, (struct statx *)-1);
107 #ifdef __s390__
108 //staptest// statx (AT_FDCWD, "foobar", AT_SYMLINK_NOFOLLOW, AT_STATX_FORCE_SYNC, 0x[7]?[f]+) = -NNNN (EFAULT)
109 #else
110 //staptest// statx (AT_FDCWD, "foobar", AT_SYMLINK_NOFOLLOW, AT_STATX_FORCE_SYNC, 0x[f]+) = -NNNN (EFAULT)
111 #endif
112 #endif
113
114 close(fd);
115
116 stat("foobar", &sbuf);
117 //staptest// [[[[stat ("foobar", XXXX!!!!fstatat (AT_FDCWD, "foobar", XXXX, 0x0]]]]) = 0
118
119 stat((char *)-1, &sbuf);
120 #if defined(__s390__)
121 //staptest// stat (0x[7]?[f]+, XXXX) = -NNNN
122 #else
123 //staptest// [[[[stat (0x[f]+, XXXX!!!!fstatat (AT_FDCWD, 0x[f]+, XXXX, 0x0]]]]) = -NNNN
124 #endif
125
126 // Here we specify -1 to both arguments, to avoid a SIGSEGV.
127 stat((char *)-1, (struct stat *)-1);
128 #if __WORDSIZE != 64
129 // Notice we're not checking for 0x[f]+ here for the 2nd
130 // argument. On RHEL[67] {x86_64,s390x}, for a 32-bit exe, glibc
131 // substituted a real structure address (verified with strace).
132 //staptest// stat (0x[7]?[f]+, XXXX) = -NNNN
133 #else
134 //staptest// [[[[stat (0x[f]+, 0x[f]+!!!!fstatat (AT_FDCWD, 0x[f]+, 0x[f]+, 0x0]]]]) = -NNNN
135 #endif
136
137 lstat("foobar", &sbuf);
138 //staptest// [[[[lstat ("foobar", XXXX!!!!fstatat (AT_FDCWD, "foobar", XXXX, AT_SYMLINK_NOFOLLOW]]]]) = 0
139
140 lstat((char *)-1, &sbuf);
141 #if defined(__s390__)
142 //staptest// lstat (0x[7]?[f]+, XXXX) = -NNNN (EFAULT)
143 #else
144 //staptest// [[[[lstat (0x[f]+, XXXX!!!!fstatat (AT_FDCWD, 0x[f]+, XXXX, AT_SYMLINK_NOFOLLOW]]]]) = -NNNN (EFAULT)
145 #endif
146
147 // Here we specify -1 to both arguments, to avoid a SIGSEGV.
148 lstat((char *)-1, (struct stat *)-1);
149 #if __WORDSIZE != 64
150 // Notice we're not checking for 0x[f]+ here for the 2nd
151 // argument. On RHEL[67] {x86_64,s390x}, for a 32-bit exe, glibc
152 // substituted a real structure address (verified with strace).
153 //staptest// lstat (0x[7]?[f]+, XXXX) = -NNNN
154 #else
155 //staptest// [[[[lstat (0x[f]+, 0x[f]+!!!!fstatat (AT_FDCWD, 0x[f]+, 0x[f]+, AT_SYMLINK_NOFOLLOW]]]]) = -NNNN
156 #endif
157
158 #if GLIBC_SUPPORT
159 fstatat(AT_FDCWD, "foobar", &sbuf, AT_SYMLINK_NOFOLLOW);
160 //staptest// fstatat (AT_FDCWD, "foobar", XXXX, AT_SYMLINK_NOFOLLOW) = 0
161
162 fstatat(-1, "foobar", &sbuf, AT_SYMLINK_NOFOLLOW);
163 //staptest// fstatat (-1, "foobar", XXXX, AT_SYMLINK_NOFOLLOW) = -NNNN (EBADF)
164
165 fstatat(AT_FDCWD, (char *)-1, &sbuf, AT_SYMLINK_NOFOLLOW);
166 #if defined(__s390__)
167 //staptest// fstatat (AT_FDCWD, 0x[7]?[f]+, XXXX, AT_SYMLINK_NOFOLLOW) = -NNNN (EFAULT)
168 #else
169 //staptest// fstatat (AT_FDCWD, 0x[f]+, XXXX, AT_SYMLINK_NOFOLLOW) = -NNNN (EFAULT)
170 #endif
171
172 // Try to avoid a SIGSEGV.
173 fstatat(-1, "foobar", (struct stat *)-1, AT_SYMLINK_NOFOLLOW);
174 #if __WORDSIZE != 64
175 // Notice we're not checking for 0x[f]+ here for the 3rd
176 // argument. On RHEL[67] {x86_64,s390x}, for a 32-bit exe, glibc
177 // substituted a real structure address (verified with strace).
178 //staptest// fstatat (-1, "foobar", XXXX, AT_SYMLINK_NOFOLLOW) = -NNNN
179 #else
180 //staptest// fstatat (-1, "foobar", 0x[f]+, AT_SYMLINK_NOFOLLOW) = -NNNN
181 #endif
182
183 fstatat(AT_FDCWD, "foobar", &sbuf, -1);
184 //staptest// fstatat (AT_FDCWD, "foobar", XXXX, AT_[^ ]+|XXXX) = -NNNN (EINVAL)
185 #endif
186
187 ubuf.actime = 1;
188 ubuf.modtime = 1135641600;
189 utime("foobar", &ubuf);
190 #if defined(__aarch64__)
191 //staptest// utimensat (AT_FDCWD, "foobar", \[1.[0]+\]\[1135641600.[0]+\], 0x0) =
192 #elif defined(__ia64__) || defined(__arm__)
193 //staptest// utimes ("foobar", \[1.000000\]\[1135641600.000000\]) =
194 #else
195 //staptest// utime ("foobar", \[Thu Jan 1 00:00:01 1970, Tue Dec 27 00:00:00 2005\]) = 0
196 #endif
197
198 ubuf.actime = 1135690000;
199 ubuf.modtime = 1135700000;
200 utime("foobar", &ubuf);
201 #if defined(__aarch64__)
202 //staptest// utimensat (AT_FDCWD, "foobar", \[1135690000.[0]+\]\[1135700000.[0]+\], 0x0) =
203 #elif defined(__ia64__) || defined(__arm__)
204 //staptest// utimes ("foobar", \[1135690000.000000\]\[1135700000.000000\]) =
205 #else
206 //staptest// utime ("foobar", \[Tue Dec 27 13:26:40 2005, Tue Dec 27 16:13:20 2005\]) = 0
207 #endif
208
209 ubuf.actime = 1;
210 ubuf.modtime = 1135641600;
211 utime((char *)-1, &ubuf);
212 #if defined(__aarch64__)
213 //staptest// utimensat (AT_FDCWD, 0x[f]+, \[1.[0]+\]\[1135641600.[0]+\], 0x0) = -NNNN
214 #elif defined(__ia64__) || defined(__arm__)
215 //staptest// utimes (0x[f]+, \[1.000000\]\[1135641600.000000\]) = -NNNN
216 #elif defined(__s390__)
217 //staptest// utime (0x[7]?[f]+, \[Thu Jan 1 00:00:01 1970, Tue Dec 27 00:00:00 2005\]) = -NNNN
218 #else
219 //staptest// utime (0x[f]+, \[Thu Jan 1 00:00:01 1970, Tue Dec 27 00:00:00 2005\]) = -NNNN
220 #endif
221
222 // Avoid a SIGSEGV by specifying NULL, not -1.
223 utime("foobar", (struct utimbuf *)NULL);
224 //staptest// [[[[utimes ("foobar", NULL!!!!utimensat (AT_FDCWD, "foobar", NULL, 0x0]]]]) = NNNN
225
226 return 0;
227 }