(root)/
strace-6.5/
tests-mx32/
dup3.c
       1  /*
       2   * Check decoding of dup3 syscall.
       3   *
       4   * Copyright (c) 2016-2021 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  #include "kernel_fcntl.h"
      16  
      17  #ifndef FD0_PATH
      18  # define FD0_PATH ""
      19  #endif
      20  #ifndef FD7_PATH
      21  # define FD7_PATH ""
      22  #endif
      23  #ifndef SKIP_IF_PROC_IS_UNAVAILABLE
      24  # define SKIP_IF_PROC_IS_UNAVAILABLE
      25  #endif
      26  
      27  static const char *errstr;
      28  
      29  static long
      30  k_dup3(const unsigned int fd1, const unsigned int fd2, const unsigned int flags)
      31  {
      32  	const kernel_ulong_t fill = (kernel_ulong_t) 0xdefaced00000000ULL;
      33  	const kernel_ulong_t bad = (kernel_ulong_t) 0xbadc0dedbadc0dedULL;
      34  	const kernel_ulong_t arg1 = fill | fd1;
      35  	const kernel_ulong_t arg2 = fill | fd2;
      36  	const kernel_ulong_t arg3 = fill | flags;
      37  	const long rc = syscall(__NR_dup3, arg1, arg2, arg3, bad, bad, bad);
      38  	errstr = sprintrc(rc);
      39  	return rc;
      40  }
      41  
      42  int
      43  main(void)
      44  {
      45  	SKIP_IF_PROC_IS_UNAVAILABLE;
      46  
      47  	int fd0 = dup(0);
      48  	int fd7 = dup(7);
      49  
      50  	k_dup3(0, 0, 0);
      51  #ifndef PATH_TRACING
      52  	printf("dup3(0" FD0_PATH ", 0" FD0_PATH ", 0) = %s\n", errstr);
      53  #endif
      54  
      55  	k_dup3(-1, -2, O_CLOEXEC);
      56  #ifndef PATH_TRACING
      57  	printf("dup3(-1, -2, O_CLOEXEC) = %s\n", errstr);
      58  #endif
      59  
      60  	k_dup3(-2, -1, O_TRUNC);
      61  #ifndef PATH_TRACING
      62  	printf("dup3(-2, -1, O_TRUNC) = %s\n", errstr);
      63  #endif
      64  
      65  	k_dup3(-3, 0, O_TRUNC | O_CLOEXEC);
      66  #ifndef PATH_TRACING
      67  	printf("dup3(-3, 0" FD0_PATH ", O_TRUNC|O_CLOEXEC) = %s\n", errstr);
      68  #endif
      69  
      70  	k_dup3(0, -4, O_RDONLY);
      71  #ifndef PATH_TRACING
      72  	printf("dup3(0" FD0_PATH ", -4, 0) = %s\n", errstr);
      73  #endif
      74  
      75  	k_dup3(-5, 7, O_WRONLY);
      76  	printf("dup3(-5, 7" FD7_PATH ", 0x1 /* O_??? */) = %s\n", errstr);
      77  
      78  	k_dup3(7, -6, O_RDWR);
      79  	printf("dup3(7" FD7_PATH ", -6, 0x2 /* O_??? */) = %s\n", errstr);
      80  
      81  	k_dup3(7, 7, O_CLOEXEC);
      82  	printf("dup3(7" FD7_PATH ", 7" FD7_PATH ", O_CLOEXEC) = %s\n", errstr);
      83  
      84  	k_dup3(-7, -7, 7);
      85  #ifndef PATH_TRACING
      86  	printf("dup3(-7, -7, 0x7 /* O_??? */) = %s\n", errstr);
      87  #endif
      88  
      89  	if (k_dup3(0, fd0, O_CLOEXEC) != fd0)
      90  		perror_msg_and_skip("dup3");
      91  #ifndef PATH_TRACING
      92  	printf("dup3(0" FD0_PATH ", %d" FD0_PATH ", O_CLOEXEC) = %d" FD0_PATH
      93  	       "\n", fd0, fd0);
      94  #endif
      95  
      96  	k_dup3(7, fd7, 0);
      97  	printf("dup3(7" FD7_PATH ", %d" FD7_PATH ", 0) = %d" FD7_PATH
      98  	       "\n", fd7, fd7);
      99  
     100  	k_dup3(0, fd7, O_CLOEXEC);
     101  	printf("dup3(0" FD0_PATH ", %d" FD7_PATH ", O_CLOEXEC) = %d" FD0_PATH
     102  	       "\n", fd7, fd7);
     103  
     104  	k_dup3(7, fd0, 0);
     105  	printf("dup3(7" FD7_PATH ", %d" FD0_PATH ", 0) = %d" FD7_PATH
     106  	       "\n", fd0, fd0);
     107  
     108  	close(fd0);
     109  	close(fd7);
     110  
     111  	k_dup3(0, fd0, O_CLOEXEC);
     112  #ifndef PATH_TRACING
     113  	printf("dup3(0" FD0_PATH ", %d, O_CLOEXEC) = %d" FD0_PATH "\n",
     114  	       fd0, fd0);
     115  #endif
     116  
     117  	k_dup3(7, fd7, 0);
     118  	printf("dup3(7" FD7_PATH ", %d, 0) = %d" FD7_PATH "\n",
     119  	       fd7, fd7);
     120  
     121  	puts("+++ exited with 0 +++");
     122  	return 0;
     123  }