1 /*
2 * Check decoding of dup syscall.
3 *
4 * Copyright (c) 2016-2023 The strace developers.
5 * All rights reserved.
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10 #include "tests.h"
11 #include "scno.h"
12
13 #include <stdio.h>
14 #include <unistd.h>
15
16 #ifndef FD0_PATH
17 # define FD0_PATH ""
18 #endif
19 #ifndef FD9_PATH
20 # define FD9_PATH ""
21 #endif
22 #ifndef SKIP_IF_PROC_IS_UNAVAILABLE
23 # define SKIP_IF_PROC_IS_UNAVAILABLE
24 #endif
25
26 #ifndef TRACE_FDS
27 # define TRACE_FDS 0
28 #endif
29 #ifndef PATH_TRACING
30 # define PATH_TRACING 0
31 #endif
32 #ifndef TRACE_FD_0
33 # define TRACE_FD_0 (!TRACE_FDS && !PATH_TRACING)
34 #endif
35 #ifndef TRACE_OTHER_FDS
36 # define TRACE_OTHER_FDS (!TRACE_FDS && !PATH_TRACING)
37 #endif
38 #ifndef TRACE_FD_9
39 # define TRACE_FD_9 (!TRACE_FDS && !PATH_TRACING)
40 #endif
41
42 static const char *errstr;
43
44 static long
45 k_dup(const unsigned int fd)
46 {
47 const kernel_ulong_t fill = (kernel_ulong_t) 0xdefaced00000000ULL;
48 const kernel_ulong_t bad = (kernel_ulong_t) 0xbadc0dedbadc0dedULL;
49 const kernel_ulong_t arg1 = fill | fd;
50 const long rc = syscall(__NR_dup, arg1, bad, bad, bad, bad, bad);
51 errstr = sprintrc(rc);
52 return rc;
53 }
54
55 int
56 main(void)
57 {
58 SKIP_IF_PROC_IS_UNAVAILABLE;
59
60 k_dup(-1);
61 #if !PATH_TRACING && !TRACE_FDS
62 printf("dup(-1) = %s\n", errstr);
63 #endif
64
65 int d1 = k_dup(0);
66 #if TRACE_FD_0
67 printf("dup(0" FD0_PATH ") = %d" FD0_PATH "\n", d1);
68 #endif
69
70 int d2 = k_dup(d1);
71 #if TRACE_OTHER_FDS
72 printf("dup(%d" FD0_PATH ") = %d" FD0_PATH "\n", d1, d2);
73 #endif
74
75 d2 = k_dup(9);
76 #if PATH_TRACING || TRACE_FD_9
77 printf("dup(9" FD9_PATH ") = %d" FD9_PATH "\n", d2);
78 #endif
79
80 d1 = k_dup(d2);
81 #if PATH_TRACING || TRACE_OTHER_FDS
82 printf("dup(%d" FD9_PATH ") = %d" FD9_PATH "\n", d2, d1);
83 #endif
84
85 puts("+++ exited with 0 +++");
86 return 0;
87 }