]> granicus.if.org Git - strace/blob - tests/xstatx.c
tests: add a list of executables without side effects
[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         PRINT_FIELD_TIME(stx_atime);
228         PRINT_FIELD_TIME(stx_btime);
229         PRINT_FIELD_TIME(stx_ctime);
230         PRINT_FIELD_TIME(stx_mtime);
231         PRINT_FIELD_U(stx_rdev_major);
232         PRINT_FIELD_U(stx_rdev_minor);
233         PRINT_FIELD_U(stx_dev_major);
234         PRINT_FIELD_U(stx_dev_minor);
235         printf("}");
236 }
237
238 # endif /* !IS_STATX */
239
240 static int
241 create_sample(const char *fname, const libc_off_t size)
242 {
243         static const struct timespec ts[] = {
244                 {-10843, 135}, {-10841, 246}
245         };
246
247         (void) close(0);
248         if (open(fname, O_RDWR | O_CREAT | O_TRUNC, 0640)) {
249                 perror(fname);
250                 return 77;
251         }
252         if (ftruncate(0, size)) {
253                 perror("ftruncate");
254                 return 77;
255         }
256         if (futimens(0, ts)) {
257                 perror("futimens");
258                 return 77;
259         }
260         return 0;
261 }
262
263 int
264 main(void)
265 {
266 # if !IS_FSTAT
267         static const char full[] = "/dev/full";
268 # endif
269         static const char sample[] = TEST_SYSCALL_STR ".sample";
270         TAIL_ALLOC_OBJECT_CONST_PTR(STRUCT_STAT, st);
271
272         int rc;
273
274         rc = create_sample(sample, SAMPLE_SIZE);
275         if (rc) {
276                 (void) unlink(sample);
277                 return rc;
278         }
279
280 # if TEST_BOGUS_STRUCT_STAT
281         STRUCT_STAT *st_cut = tail_alloc(sizeof(long) * 4);
282         rc = TEST_SYSCALL_INVOKE(sample, st_cut);
283         PRINT_SYSCALL_HEADER(sample);
284         printf("%p", st_cut);
285         PRINT_SYSCALL_FOOTER(rc);
286 # endif
287
288 # if !IS_FSTAT
289         rc = TEST_SYSCALL_INVOKE(full, st);
290         PRINT_SYSCALL_HEADER(full);
291         if (rc)
292                 printf("%p", st);
293         else
294                 print_stat(st);
295         PRINT_SYSCALL_FOOTER(rc);
296 # endif
297
298         if ((rc = TEST_SYSCALL_INVOKE(sample, st))) {
299                 if (errno != EOVERFLOW) {
300                         rc = (errno == ENOSYS) ? 77 : 1;
301                         perror(TEST_SYSCALL_STR);
302                         (void) unlink(sample);
303                         return rc;
304                 }
305         }
306
307 # if IS_STATX
308 #  define ST_SIZE_FIELD stx_size
309 # else
310 #  define ST_SIZE_FIELD st_size
311 # endif
312         if (!rc && zero_extend_signed_to_ull(SAMPLE_SIZE) !=
313             zero_extend_signed_to_ull(st->ST_SIZE_FIELD)) {
314                 fprintf(stderr, "Size mismatch: "
315                                 "requested size(%llu) != st_size(%llu)\n",
316                         zero_extend_signed_to_ull(SAMPLE_SIZE),
317                         zero_extend_signed_to_ull(st->ST_SIZE_FIELD));
318                 fprintf(stderr, "The most likely reason for this is incorrect"
319                                 " definition of %s.\n"
320                                 "Here is some diagnostics that might help:\n",
321                         STRUCT_STAT_STR);
322
323 # define LOG_STAT_OFFSETOF_SIZEOF(object, member)                       \
324                 fprintf(stderr, "offsetof(%s, %s) = %zu"                \
325                                 ", sizeof(%s) = %zu\n",                 \
326                                 STRUCT_STAT_STR, #member,               \
327                                 offsetof(STRUCT_STAT, member),          \
328                                 #member, sizeof((object).member))
329
330 # if IS_STATX
331                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_mask);
332                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_blksize);
333                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_attributes);
334                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_nlink);
335                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_uid);
336                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_gid);
337                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_mode);
338                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_ino);
339                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_size);
340                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_blocks);
341                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_atime);
342                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_btime);
343                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_ctime);
344                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_mtime);
345                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_rdev_major);
346                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_rdev_minor);
347                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_dev_major);
348                 LOG_STAT_OFFSETOF_SIZEOF(*st, stx_dev_minor);
349 # else
350                 LOG_STAT_OFFSETOF_SIZEOF(*st, st_dev);
351                 LOG_STAT_OFFSETOF_SIZEOF(*st, st_ino);
352                 LOG_STAT_OFFSETOF_SIZEOF(*st, st_mode);
353                 LOG_STAT_OFFSETOF_SIZEOF(*st, st_nlink);
354                 LOG_STAT_OFFSETOF_SIZEOF(*st, st_uid);
355                 LOG_STAT_OFFSETOF_SIZEOF(*st, st_gid);
356                 LOG_STAT_OFFSETOF_SIZEOF(*st, st_rdev);
357                 LOG_STAT_OFFSETOF_SIZEOF(*st, st_size);
358 #  if !OLD_STAT
359                 LOG_STAT_OFFSETOF_SIZEOF(*st, st_blksize);
360                 LOG_STAT_OFFSETOF_SIZEOF(*st, st_blocks);
361 #  endif /* !OLD_STAT */
362
363 # endif /* IS_STATX */
364
365                 (void) unlink(sample);
366                 return 1;
367         }
368
369         PRINT_SYSCALL_HEADER(sample);
370         if (rc)
371                 printf("%p", st);
372         else
373                 print_stat(st);
374         PRINT_SYSCALL_FOOTER(rc);
375
376 # if IS_STATX
377
378 #  define INVOKE() \
379         rc = TEST_SYSCALL_INVOKE(sample, st); \
380         PRINT_SYSCALL_HEADER(sample); \
381         if (rc) \
382                 printf("%p", st); \
383         else \
384                 print_stat(st); \
385         PRINT_SYSCALL_FOOTER(rc)
386
387 #  define SET_FLAGS_INVOKE(flags, flags_str) \
388         TEST_SYSCALL_STATX_FLAGS = flags; \
389         TEST_SYSCALL_STATX_FLAGS_STR = flags_str; \
390         INVOKE()
391
392 #  define SET_MASK_INVOKE(mask, mask_str) \
393         TEST_SYSCALL_STATX_MASK = mask; \
394         TEST_SYSCALL_STATX_MASK_STR = mask_str; \
395         INVOKE()
396
397         unsigned old_flags = TEST_SYSCALL_STATX_FLAGS;
398         const char *old_flags_str = TEST_SYSCALL_STATX_FLAGS_STR;
399         unsigned old_mask = TEST_SYSCALL_STATX_MASK;
400         const char *old_mask_str = TEST_SYSCALL_STATX_MASK_STR;
401
402         SET_FLAGS_INVOKE(AT_SYMLINK_FOLLOW | 0xffff0000U,
403                 "AT_STATX_SYNC_AS_STAT|AT_SYMLINK_FOLLOW|0xffff0000");
404
405         SET_FLAGS_INVOKE(AT_STATX_SYNC_TYPE,
406                 "AT_STATX_FORCE_SYNC|AT_STATX_DONT_SYNC");
407
408         SET_FLAGS_INVOKE(0xffffff,
409                 "AT_STATX_FORCE_SYNC|AT_STATX_DONT_SYNC|AT_SYMLINK_NOFOLLOW|"
410                 "AT_REMOVEDIR|AT_SYMLINK_FOLLOW|AT_NO_AUTOMOUNT|AT_EMPTY_PATH|"
411                 "0xff80ff");
412
413         /* We're done playing with flags. */
414         TEST_SYSCALL_STATX_FLAGS = old_flags;
415         TEST_SYSCALL_STATX_FLAGS_STR = old_flags_str;
416
417         SET_MASK_INVOKE(0, "0");
418         SET_MASK_INVOKE(0xfffff000U, "0xfffff000 /* STATX_??? */");
419
420         SET_MASK_INVOKE(0xfffffffbU,
421                 "STATX_TYPE|STATX_MODE|STATX_UID|STATX_GID|STATX_ATIME|"
422                 "STATX_MTIME|STATX_CTIME|STATX_INO|STATX_SIZE|STATX_BLOCKS|"
423                 "STATX_BTIME|0xfffff000");
424
425         SET_MASK_INVOKE(STATX_UID, "STATX_UID");
426
427         /* ...and with mask. */
428         TEST_SYSCALL_STATX_MASK = old_mask;
429         TEST_SYSCALL_STATX_MASK_STR = old_mask_str;
430
431 # endif /* IS_STATX */
432
433         (void) unlink(sample);
434
435         puts("+++ exited with 0 +++");
436         return 0;
437 }
438
439 #else
440
441 SKIP_MAIN_UNDEFINED("HAVE_FTRUNCATE && HAVE_FUTIMENS")
442
443 #endif