1 /*
2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2015-2018 The strace developers.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #if defined HAVE_FTRUNCATE && defined HAVE_FUTIMENS
30
31 # ifndef TEST_SYSCALL_STR
32 # error TEST_SYSCALL_STR must be defined
33 # endif
34 # ifndef TEST_SYSCALL_INVOKE
35 # error TEST_SYSCALL_INVOKE must be defined
36 # endif
37 # ifndef PRINT_SYSCALL_HEADER
38 # error PRINT_SYSCALL_HEADER must be defined
39 # endif
40 # ifndef PRINT_SYSCALL_FOOTER
41 # error PRINT_SYSCALL_FOOTER must be defined
42 # endif
43
44 # include <errno.h>
45 # include <stdio.h>
46 # include <stddef.h>
47 # include <time.h>
48 # include <unistd.h>
49 # include <sys/sysmacros.h>
50
51 # include "print_fields.h"
52 # include "statx.h"
53
54 # ifndef STRUCT_STAT
55 # define STRUCT_STAT struct stat
56 # define STRUCT_STAT_STR "struct stat"
57 # define STRUCT_STAT_IS_STAT64 0
58 # endif
59 # ifndef SAMPLE_SIZE
60 # define SAMPLE_SIZE ((libc_off_t) 43147718418ULL)
61 # endif
62
63 typedef off_t libc_off_t;
64
65 # define stat libc_stat
66 # define stat64 libc_stat64
67 # define statx libc_statx
68 # define statx_timestamp libc_statx_timestamp
69 # include <fcntl.h>
70 # include <sys/stat.h>
71 # undef statx_timestamp
72 # undef statx
73 # undef stat64
74 # undef stat
75
76 # undef st_atime
77 # undef st_mtime
78 # undef st_ctime
79 # include "asm_stat.h"
80
81 # if STRUCT_STAT_IS_STAT64
82 # undef HAVE_STRUCT_STAT_ST_MTIME_NSEC
83 # if defined MPERS_IS_m32
84 # ifdef HAVE_M32_STRUCT_STAT64_ST_MTIME_NSEC
85 # define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
86 # endif
87 # elif defined MPERS_IS_mx32
88 # ifdef HAVE_MX32_STRUCT_STAT64_ST_MTIME_NSEC
89 # define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
90 # endif
91 # elif defined HAVE_STRUCT_STAT64_ST_MTIME_NSEC
92 # define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
93 # endif /* MPERS_IS_m32 || MPERS_IS_mx32 || HAVE_STRUCT_STAT64_ST_MTIME_NSEC */
94 # else /* !STRUCT_STAT_IS_STAT64 */
95 # if defined MPERS_IS_m32
96 # undef HAVE_STRUCT_STAT_ST_MTIME_NSEC
97 # ifdef HAVE_M32_STRUCT_STAT_ST_MTIME_NSEC
98 # define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
99 # endif
100 # elif defined MPERS_IS_mx32
101 # undef HAVE_STRUCT_STAT_ST_MTIME_NSEC
102 # ifdef HAVE_MX32_STRUCT_STAT_ST_MTIME_NSEC
103 # define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
104 # endif
105 # endif /* MPERS_IS_m32 || MPERS_IS_mx32 */
106 # endif /* STRUCT_STAT_IS_STAT64 */
107
108 # ifndef TEST_BOGUS_STRUCT_STAT
109 # define TEST_BOGUS_STRUCT_STAT 1
110 # endif
111
112 # ifndef IS_FSTAT
113 # define IS_FSTAT 0
114 # endif
115
116 # ifndef OLD_STAT
117 # define OLD_STAT 0
118 # endif
119
120 # ifndef IS_STATX
121 # define IS_STATX 0
122 # endif
123
124 static void
print_ftype(const unsigned int mode)125 print_ftype(const unsigned int mode)
126 {
127 if (S_ISREG(mode))
128 printf("S_IFREG");
129 else if (S_ISDIR(mode))
130 printf("S_IFDIR");
131 else if (S_ISCHR(mode))
132 printf("S_IFCHR");
133 else if (S_ISBLK(mode))
134 printf("S_IFBLK");
135 else
136 printf("%#o", mode & S_IFMT);
137 }
138
139 static void
print_perms(const unsigned int mode)140 print_perms(const unsigned int mode)
141 {
142 printf("%#o", mode & ~S_IFMT);
143 }
144
145 # if !IS_STATX
146
147 static void
print_stat(const STRUCT_STAT * st)148 print_stat(const STRUCT_STAT *st)
149 {
150 printf("{st_dev=makedev(%u, %u)",
151 (unsigned int) major(zero_extend_signed_to_ull(st->st_dev)),
152 (unsigned int) minor(zero_extend_signed_to_ull(st->st_dev)));
153 printf(", st_ino=%llu", zero_extend_signed_to_ull(st->st_ino));
154 printf(", st_mode=");
155 print_ftype(st->st_mode);
156 printf("|");
157 print_perms(st->st_mode);
158 printf(", st_nlink=%llu", zero_extend_signed_to_ull(st->st_nlink));
159 printf(", st_uid=%llu", zero_extend_signed_to_ull(st->st_uid));
160 printf(", st_gid=%llu", zero_extend_signed_to_ull(st->st_gid));
161 # if OLD_STAT
162 printf(", st_blksize=0, st_blocks=0");
163 # else /* !OLD_STAT */
164 printf(", st_blksize=%llu", zero_extend_signed_to_ull(st->st_blksize));
165 printf(", st_blocks=%llu", zero_extend_signed_to_ull(st->st_blocks));
166 # endif /* OLD_STAT */
167
168 switch (st->st_mode & S_IFMT) {
169 case S_IFCHR: case S_IFBLK:
170 printf(", st_rdev=makedev(%u, %u)",
171 (unsigned int) major(zero_extend_signed_to_ull(st->st_rdev)),
172 (unsigned int) minor(zero_extend_signed_to_ull(st->st_rdev)));
173 break;
174 default:
175 printf(", st_size=%llu", zero_extend_signed_to_ull(st->st_size));
176 }
177
178 # if defined(HAVE_STRUCT_STAT_ST_MTIME_NSEC) && !OLD_STAT
179 # define TIME_NSEC(val) zero_extend_signed_to_ull(val)
180 # define HAVE_NSEC 1
181 # else
182 # define TIME_NSEC(val) 0ULL
183 # define HAVE_NSEC 0
184 # endif
185
186 #define PRINT_ST_TIME(field) \
187 do { \
188 printf(", st_" #field "=%lld", \
189 sign_extend_unsigned_to_ll(st->st_ ## field)); \
190 print_time_t_nsec(sign_extend_unsigned_to_ll(st->st_ ## field), \
191 TIME_NSEC(st->st_ ## field ## _nsec), 1); \
192 if (HAVE_NSEC) \
193 printf(", st_" #field "_nsec=%llu", \
194 TIME_NSEC(st->st_ ## field ## _nsec)); \
195 } while (0)
196
197 PRINT_ST_TIME(atime);
198 PRINT_ST_TIME(mtime);
199 PRINT_ST_TIME(ctime);
200 printf("}");
201 }
202
203 # else /* !IS_STATX */
204
205 static void
print_stat(const STRUCT_STAT * st)206 print_stat(const STRUCT_STAT *st)
207 {
208 # define PRINT_FIELD_U32_UID(field) \
209 do { \
210 if (st->field == (uint32_t) -1) \
211 printf(", %s=-1", #field); \
212 else \
213 printf(", %s=%llu", #field, \
214 (unsigned long long) st->field); \
215 } while (0)
216
217 # define PRINT_FIELD_TIME(field) \
218 do { \
219 printf(", %s={tv_sec=%lld, tv_nsec=%u}", \
220 #field, (long long) st->field.tv_sec, \
221 (unsigned) st->field.tv_nsec); \
222 print_time_t_nsec(st->field.tv_sec, \
223 zero_extend_signed_to_ull(st->field.tv_nsec), \
224 1); \
225 } while (0)
226
227 printf("{stx_mask=");
228 printflags(statx_masks, st->stx_mask, "STATX_???");
229
230 PRINT_FIELD_U(", ", *st, stx_blksize);
231
232 printf(", stx_attributes=");
233 printflags(statx_attrs, st->stx_attributes, "STATX_ATTR_???");
234
235 PRINT_FIELD_U(", ", *st, stx_nlink);
236 PRINT_FIELD_U32_UID(stx_uid);
237 PRINT_FIELD_U32_UID(stx_gid);
238
239 printf(", stx_mode=");
240 print_ftype(st->stx_mode);
241 printf("|");
242 print_perms(st->stx_mode);
243
244 PRINT_FIELD_U(", ", *st, stx_ino);
245 PRINT_FIELD_U(", ", *st, stx_size);
246 PRINT_FIELD_U(", ", *st, stx_blocks);
247
248 printf(", stx_attributes_mask=");
249 printflags(statx_attrs, st->stx_attributes_mask, "STATX_ATTR_???");
250
251 PRINT_FIELD_TIME(stx_atime);
252 PRINT_FIELD_TIME(stx_btime);
253 PRINT_FIELD_TIME(stx_ctime);
254 PRINT_FIELD_TIME(stx_mtime);
255 PRINT_FIELD_U(", ", *st, stx_rdev_major);
256 PRINT_FIELD_U(", ", *st, stx_rdev_minor);
257 PRINT_FIELD_U(", ", *st, stx_dev_major);
258 PRINT_FIELD_U(", ", *st, stx_dev_minor);
259 printf("}");
260 }
261
262 # endif /* !IS_STATX */
263
264 static int
create_sample(const char * fname,const libc_off_t size)265 create_sample(const char *fname, const libc_off_t size)
266 {
267 static const struct timespec ts[] = {
268 {-10843, 135}, {-10841, 246}
269 };
270
271 (void) close(0);
272 if (open(fname, O_RDWR | O_CREAT | O_TRUNC, 0640)) {
273 perror(fname);
274 return 77;
275 }
276 if (ftruncate(0, size)) {
277 perror("ftruncate");
278 return 77;
279 }
280 if (futimens(0, ts)) {
281 perror("futimens");
282 return 77;
283 }
284 return 0;
285 }
286
287 int
main(void)288 main(void)
289 {
290 # if IS_FSTAT
291 skip_if_unavailable("/proc/self/fd/");
292 # else
293 static const char full[] = "/dev/full";
294 # endif
295 static const char sample[] = "stat.sample";
296 TAIL_ALLOC_OBJECT_CONST_PTR(STRUCT_STAT, st);
297
298 int rc;
299
300 rc = create_sample(sample, SAMPLE_SIZE);
301 if (rc)
302 return rc;
303
304 # if TEST_BOGUS_STRUCT_STAT
305 STRUCT_STAT *st_cut = tail_alloc(sizeof(long) * 4);
306 rc = TEST_SYSCALL_INVOKE(sample, st_cut);
307 PRINT_SYSCALL_HEADER(sample);
308 printf("%p", st_cut);
309 PRINT_SYSCALL_FOOTER(rc);
310 # endif
311
312 # if !IS_FSTAT
313 rc = TEST_SYSCALL_INVOKE(full, st);
314 PRINT_SYSCALL_HEADER(full);
315 if (rc)
316 printf("%p", st);
317 else
318 print_stat(st);
319 PRINT_SYSCALL_FOOTER(rc);
320 # endif
321
322 if ((rc = TEST_SYSCALL_INVOKE(sample, st))) {
323 if (errno != EOVERFLOW) {
324 rc = (errno == ENOSYS) ? 77 : 1;
325 perror(TEST_SYSCALL_STR);
326 return rc;
327 }
328 }
329
330 # if IS_STATX
331 # define ST_SIZE_FIELD stx_size
332 # else
333 # define ST_SIZE_FIELD st_size
334 # endif
335 if (!rc && zero_extend_signed_to_ull(SAMPLE_SIZE) !=
336 zero_extend_signed_to_ull(st->ST_SIZE_FIELD)) {
337 fprintf(stderr, "Size mismatch: "
338 "requested size(%llu) != st_size(%llu)\n",
339 zero_extend_signed_to_ull(SAMPLE_SIZE),
340 zero_extend_signed_to_ull(st->ST_SIZE_FIELD));
341 fprintf(stderr, "The most likely reason for this is incorrect"
342 " definition of %s.\n"
343 "Here is some diagnostics that might help:\n",
344 STRUCT_STAT_STR);
345
346 # define LOG_STAT_OFFSETOF_SIZEOF(object, member) \
347 fprintf(stderr, "offsetof(%s, %s) = %zu" \
348 ", sizeof(%s) = %zu\n", \
349 STRUCT_STAT_STR, #member, \
350 offsetof(STRUCT_STAT, member), \
351 #member, sizeof((object).member))
352
353 # if IS_STATX
354 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_mask);
355 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_blksize);
356 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_attributes);
357 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_nlink);
358 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_uid);
359 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_gid);
360 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_mode);
361 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_ino);
362 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_size);
363 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_blocks);
364 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_attributes_mask);
365 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_atime);
366 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_btime);
367 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_ctime);
368 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_mtime);
369 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_rdev_major);
370 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_rdev_minor);
371 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_dev_major);
372 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_dev_minor);
373 # else
374 LOG_STAT_OFFSETOF_SIZEOF(*st, st_dev);
375 LOG_STAT_OFFSETOF_SIZEOF(*st, st_ino);
376 LOG_STAT_OFFSETOF_SIZEOF(*st, st_mode);
377 LOG_STAT_OFFSETOF_SIZEOF(*st, st_nlink);
378 LOG_STAT_OFFSETOF_SIZEOF(*st, st_uid);
379 LOG_STAT_OFFSETOF_SIZEOF(*st, st_gid);
380 LOG_STAT_OFFSETOF_SIZEOF(*st, st_rdev);
381 LOG_STAT_OFFSETOF_SIZEOF(*st, st_size);
382 # if !OLD_STAT
383 LOG_STAT_OFFSETOF_SIZEOF(*st, st_blksize);
384 LOG_STAT_OFFSETOF_SIZEOF(*st, st_blocks);
385 # endif /* !OLD_STAT */
386
387 # endif /* IS_STATX */
388
389 return 1;
390 }
391
392 PRINT_SYSCALL_HEADER(sample);
393 if (rc)
394 printf("%p", st);
395 else
396 print_stat(st);
397 PRINT_SYSCALL_FOOTER(rc);
398
399 # if IS_STATX
400
401 # define INVOKE() \
402 do { \
403 rc = TEST_SYSCALL_INVOKE(sample, st); \
404 PRINT_SYSCALL_HEADER(sample); \
405 if (rc) \
406 printf("%p", st); \
407 else \
408 print_stat(st); \
409 PRINT_SYSCALL_FOOTER(rc); \
410 } while (0)
411
412 # define SET_FLAGS_INVOKE(flags, flags_str) \
413 do { \
414 TEST_SYSCALL_STATX_FLAGS = flags; \
415 TEST_SYSCALL_STATX_FLAGS_STR = flags_str; \
416 INVOKE(); \
417 } while (0)
418
419 # define SET_MASK_INVOKE(mask, mask_str) \
420 do { \
421 TEST_SYSCALL_STATX_MASK = mask; \
422 TEST_SYSCALL_STATX_MASK_STR = mask_str; \
423 INVOKE(); \
424 } while (0)
425
426 unsigned old_flags = TEST_SYSCALL_STATX_FLAGS;
427 const char *old_flags_str = TEST_SYSCALL_STATX_FLAGS_STR;
428 unsigned old_mask = TEST_SYSCALL_STATX_MASK;
429 const char *old_mask_str = TEST_SYSCALL_STATX_MASK_STR;
430
431 SET_FLAGS_INVOKE(AT_SYMLINK_FOLLOW | 0xffff0000U,
432 "AT_STATX_SYNC_AS_STAT|AT_SYMLINK_FOLLOW|0xffff0000");
433
434 SET_FLAGS_INVOKE(AT_STATX_SYNC_TYPE,
435 "AT_STATX_FORCE_SYNC|AT_STATX_DONT_SYNC");
436
437 SET_FLAGS_INVOKE(0xffffff,
438 "AT_STATX_FORCE_SYNC|AT_STATX_DONT_SYNC|AT_SYMLINK_NOFOLLOW|"
439 "AT_REMOVEDIR|AT_SYMLINK_FOLLOW|AT_NO_AUTOMOUNT|AT_EMPTY_PATH|"
440 "0xff80ff");
441
442 /* We're done playing with flags. */
443 TEST_SYSCALL_STATX_FLAGS = old_flags;
444 TEST_SYSCALL_STATX_FLAGS_STR = old_flags_str;
445
446 SET_MASK_INVOKE(0, "0");
447 SET_MASK_INVOKE(0xfffff000U, "0xfffff000 /* STATX_??? */");
448
449 SET_MASK_INVOKE(0xfffffffbU,
450 "STATX_TYPE|STATX_MODE|STATX_UID|STATX_GID|STATX_ATIME|"
451 "STATX_MTIME|STATX_CTIME|STATX_INO|STATX_SIZE|STATX_BLOCKS|"
452 "STATX_BTIME|0xfffff000");
453
454 SET_MASK_INVOKE(STATX_UID, "STATX_UID");
455
456 /* ...and with mask. */
457 TEST_SYSCALL_STATX_MASK = old_mask;
458 TEST_SYSCALL_STATX_MASK_STR = old_mask_str;
459
460 # endif /* IS_STATX */
461
462 puts("+++ exited with 0 +++");
463 return 0;
464 }
465
466 #else
467
468 SKIP_MAIN_UNDEFINED("HAVE_FTRUNCATE && HAVE_FUTIMENS")
469
470 #endif
471