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