]> granicus.if.org Git - strace/blob - tests/futimesat.c
661c6c3678791302a33d9bf0d7b104cd9047cfd4
[strace] / tests / futimesat.c
1 /*
2  * Check decoding of futimesat syscall.
3  *
4  * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "tests.h"
11 #include <asm/unistd.h>
12
13 #ifdef __NR_futimesat
14
15 # include <stdint.h>
16 # include <stdio.h>
17 # include <sys/time.h>
18 # include <unistd.h>
19
20 static void
21 print_tv(const struct timeval *tv)
22 {
23         printf("{tv_sec=%lld, tv_usec=%llu}",
24                (long long) tv->tv_sec,
25                zero_extend_signed_to_ull(tv->tv_usec));
26         print_time_t_usec(tv->tv_sec,
27                           zero_extend_signed_to_ull(tv->tv_usec), 1);
28 }
29
30 static const char *errstr;
31
32 static long
33 k_futimesat(const kernel_ulong_t dirfd,
34             const kernel_ulong_t pathname,
35             const kernel_ulong_t times)
36 {
37         long rc = syscall(__NR_futimesat, dirfd, pathname, times);
38         errstr = sprintrc(rc);
39         return rc;
40 }
41
42 int
43 main(void)
44 {
45         static const kernel_ulong_t bogus_fd =
46                 (kernel_ulong_t) 0xbadfaceddeadbeaf;
47         static const kernel_ulong_t kfdcwd =
48                 (kernel_ulong_t) 0xdefaced00000000 | -100U;
49         static const char proto_fname[] = "futimesat_sample";
50         static const char qname[] = "\"futimesat_sample\"";
51
52         char *const fname = tail_memdup(proto_fname, sizeof(proto_fname));
53         const kernel_ulong_t kfname = (uintptr_t) fname;
54         struct timeval *const tv = tail_alloc(sizeof(*tv) * 2);
55
56         (void) close(0);
57
58         /* dirfd */
59         k_futimesat(0, kfname, 0);
60         printf("futimesat(0, %s, NULL) = %s\n", qname, errstr);
61
62         k_futimesat(bogus_fd, kfname, 0);
63         printf("futimesat(%d, %s, NULL) = %s\n", (int) bogus_fd, qname, errstr);
64
65         k_futimesat(-100U, kfname, 0);
66         printf("futimesat(AT_FDCWD, %s, NULL) = %s\n", qname, errstr);
67
68         k_futimesat(kfdcwd, kfname, 0);
69         printf("futimesat(AT_FDCWD, %s, NULL) = %s\n", qname, errstr);
70
71         /* pathname */
72         k_futimesat(kfdcwd, 0, 0);
73         printf("futimesat(AT_FDCWD, NULL, NULL) = %s\n", errstr);
74
75         k_futimesat(kfdcwd, kfname + sizeof(proto_fname) - 1, 0);
76         printf("futimesat(AT_FDCWD, \"\", NULL) = %s\n", errstr);
77
78         fname[sizeof(proto_fname) - 1] = '+';
79         k_futimesat(kfdcwd, kfname, 0);
80         fname[sizeof(proto_fname) - 1] = '\0';
81         printf("futimesat(AT_FDCWD, %p, NULL) = %s\n", fname, errstr);
82
83         if (F8ILL_KULONG_SUPPORTED) {
84                 k_futimesat(kfdcwd, f8ill_ptr_to_kulong(fname), 0);
85                 printf("futimesat(AT_FDCWD, %#jx, NULL) = %s\n",
86                        (uintmax_t) f8ill_ptr_to_kulong(fname), errstr);
87         }
88
89         /* times */
90         k_futimesat(kfdcwd, kfname, (uintptr_t) (tv + 1));
91         printf("futimesat(AT_FDCWD, %s, %p) = %s\n",
92                qname, tv + 1, errstr);
93
94         k_futimesat(kfdcwd, kfname, (uintptr_t) (tv + 2));
95         printf("futimesat(AT_FDCWD, %s, %p) = %s\n",
96                qname, tv + 2, errstr);
97
98         tv[0].tv_sec = 0xdeadbeefU;
99         tv[0].tv_usec = 0xfacefeedU;
100         tv[1].tv_sec = (time_t) 0xcafef00ddeadbeefLL;
101         tv[1].tv_usec = (suseconds_t) 0xbadc0dedfacefeedLL;
102
103         k_futimesat(kfdcwd, kfname, (uintptr_t) tv);
104         printf("futimesat(AT_FDCWD, %s, [", qname);
105         print_tv(&tv[0]);
106         printf(", ");
107         print_tv(&tv[1]);
108         printf("]) = %s\n", errstr);
109
110         tv[0].tv_sec = 1492356708;
111         tv[0].tv_usec = 567891234;
112         tv[1].tv_sec = 1492357086;
113         tv[1].tv_usec = 678902345;
114
115         k_futimesat(kfdcwd, kfname, (uintptr_t) tv);
116         printf("futimesat(AT_FDCWD, %s, [", qname);
117         print_tv(&tv[0]);
118         printf(", ");
119         print_tv(&tv[1]);
120         printf("]) = %s\n", errstr);
121
122         tv[0].tv_usec = 567891;
123         tv[1].tv_usec = 678902;
124
125         k_futimesat(kfdcwd, kfname, (uintptr_t) tv);
126         printf("futimesat(AT_FDCWD, %s, [", qname);
127         print_tv(&tv[0]);
128         printf(", ");
129         print_tv(&tv[1]);
130         printf("]) = %s\n", errstr);
131
132         if (F8ILL_KULONG_SUPPORTED) {
133                 k_futimesat(kfdcwd, kfname, f8ill_ptr_to_kulong(tv));
134                 printf("futimesat(AT_FDCWD, %s, %#jx) = %s\n",
135                        qname, (uintmax_t) f8ill_ptr_to_kulong(tv), errstr);
136         }
137
138         puts("+++ exited with 0 +++");
139         return 0;
140 }
141
142 #else
143
144 SKIP_MAIN_UNDEFINED("__NR_futimesat")
145
146 #endif