(root)/
gettext-0.22.4/
gettext-tools/
gnulib-tests/
test-dup2.c
       1  /* Test duplicating file descriptors.
       2     Copyright (C) 2009-2023 Free Software Foundation, Inc.
       3  
       4     This program is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation, either version 3 of the License, or
       7     (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
      18  
      19  #include <config.h>
      20  
      21  #include <unistd.h>
      22  
      23  #include "signature.h"
      24  SIGNATURE_CHECK (dup2, int, (int, int));
      25  
      26  #include <errno.h>
      27  #include <fcntl.h>
      28  
      29  #if HAVE_SYS_RESOURCE_H
      30  # include <sys/resource.h>
      31  #endif
      32  
      33  #include "binary-io.h"
      34  
      35  #if GNULIB_TEST_CLOEXEC
      36  # include "cloexec.h"
      37  #endif
      38  
      39  #if defined _WIN32 && ! defined __CYGWIN__
      40  /* Get declarations of the native Windows API functions.  */
      41  # define WIN32_LEAN_AND_MEAN
      42  # include <windows.h>
      43  /* Get _get_osfhandle.  */
      44  # if GNULIB_MSVC_NOTHROW
      45  #  include "msvc-nothrow.h"
      46  # else
      47  #  include <io.h>
      48  # endif
      49  #endif
      50  
      51  #include "macros.h"
      52  
      53  /* Tell GCC not to warn about the specific edge cases tested here.  */
      54  #if __GNUC__ >= 13
      55  # pragma GCC diagnostic ignored "-Wanalyzer-fd-leak"
      56  # pragma GCC diagnostic ignored "-Wanalyzer-fd-use-without-check"
      57  #endif
      58  
      59  /* Return non-zero if FD is open.  */
      60  static int
      61  is_open (int fd)
      62  {
      63  #if defined _WIN32 && ! defined __CYGWIN__
      64    /* On native Windows, the initial state of unassigned standard file
      65       descriptors is that they are open but point to an
      66       INVALID_HANDLE_VALUE, and there is no fcntl.  */
      67    return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
      68  #else
      69  # ifndef F_GETFL
      70  #  error Please port fcntl to your platform
      71  # endif
      72    return 0 <= fcntl (fd, F_GETFL);
      73  #endif
      74  }
      75  
      76  #if GNULIB_TEST_CLOEXEC
      77  /* Return non-zero if FD is open and inheritable across exec/spawn.  */
      78  static int
      79  is_inheritable (int fd)
      80  {
      81  # if defined _WIN32 && ! defined __CYGWIN__
      82    /* On native Windows, the initial state of unassigned standard file
      83       descriptors is that they are open but point to an
      84       INVALID_HANDLE_VALUE, and there is no fcntl.  */
      85    HANDLE h = (HANDLE) _get_osfhandle (fd);
      86    DWORD flags;
      87    if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
      88      return 0;
      89    return (flags & HANDLE_FLAG_INHERIT) != 0;
      90  # else
      91  #  ifndef F_GETFD
      92  #   error Please port fcntl to your platform
      93  #  endif
      94    int i = fcntl (fd, F_GETFD);
      95    return 0 <= i && (i & FD_CLOEXEC) == 0;
      96  # endif
      97  }
      98  #endif /* GNULIB_TEST_CLOEXEC */
      99  
     100  #if !O_BINARY
     101  # define set_binary_mode my_set_binary_mode
     102  static int
     103  set_binary_mode (_GL_UNUSED int fd, _GL_UNUSED int mode)
     104  {
     105    return 0;
     106  }
     107  #endif
     108  
     109  /* Return non-zero if FD is open in the given MODE, which is either
     110     O_TEXT or O_BINARY.  */
     111  static int
     112  is_mode (int fd, int mode)
     113  {
     114    int value = set_binary_mode (fd, O_BINARY);
     115    set_binary_mode (fd, value);
     116    return mode == value;
     117  }
     118  
     119  int
     120  main (void)
     121  {
     122    const char *file = "test-dup2.tmp";
     123    char buffer[1];
     124    int bad_fd = getdtablesize ();
     125    int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
     126  
     127    /* Assume std descriptors were provided by invoker.  */
     128    ASSERT (STDERR_FILENO < fd);
     129    ASSERT (is_open (fd));
     130    /* Ignore any other fd's leaked into this process.  */
     131    close (fd + 1);
     132    close (fd + 2);
     133    ASSERT (!is_open (fd + 1));
     134    ASSERT (!is_open (fd + 2));
     135  
     136    /* Assigning to self must be a no-op.  */
     137    ASSERT (dup2 (fd, fd) == fd);
     138    ASSERT (is_open (fd));
     139  
     140    /* The source must be valid.  */
     141    errno = 0;
     142    ASSERT (dup2 (-1, fd) == -1);
     143    ASSERT (errno == EBADF);
     144    close (99);
     145    errno = 0;
     146    ASSERT (dup2 (99, fd) == -1);
     147    ASSERT (errno == EBADF);
     148    errno = 0;
     149    ASSERT (dup2 (AT_FDCWD, fd) == -1);
     150    ASSERT (errno == EBADF);
     151    ASSERT (is_open (fd));
     152  
     153    /* If the source is not open, then the destination is unaffected.  */
     154    errno = 0;
     155    ASSERT (dup2 (fd + 1, fd + 1) == -1);
     156    ASSERT (errno == EBADF);
     157    ASSERT (!is_open (fd + 1));
     158    errno = 0;
     159    ASSERT (dup2 (fd + 1, fd) == -1);
     160    ASSERT (errno == EBADF);
     161    ASSERT (is_open (fd));
     162  
     163    /* The destination must be valid.  */
     164    errno = 0;
     165    ASSERT (dup2 (fd, -2) == -1);
     166    ASSERT (errno == EBADF);
     167    if (bad_fd > 256)
     168      {
     169        ASSERT (dup2 (fd, 255) == 255);
     170        ASSERT (dup2 (fd, 256) == 256);
     171        ASSERT (close (255) == 0);
     172        ASSERT (close (256) == 0);
     173      }
     174    ASSERT (dup2 (fd, bad_fd - 1) == bad_fd - 1);
     175    ASSERT (close (bad_fd - 1) == 0);
     176    errno = 0;
     177    ASSERT (dup2 (fd, bad_fd) == -1);
     178    ASSERT (errno == EBADF);
     179  
     180    /* Using dup2 can skip fds.  */
     181    ASSERT (dup2 (fd, fd + 2) == fd + 2);
     182    ASSERT (is_open (fd));
     183    ASSERT (!is_open (fd + 1));
     184    ASSERT (is_open (fd + 2));
     185  
     186    /* Verify that dup2 closes the previous occupant of a fd.  */
     187    ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
     188    ASSERT (dup2 (fd + 1, fd) == fd);
     189    ASSERT (close (fd + 1) == 0);
     190    ASSERT (write (fd, "1", 1) == 1);
     191    ASSERT (dup2 (fd + 2, fd) == fd);
     192    ASSERT (lseek (fd, 0, SEEK_END) == 0);
     193    ASSERT (write (fd + 2, "2", 1) == 1);
     194    ASSERT (lseek (fd, 0, SEEK_SET) == 0);
     195    ASSERT (read (fd, buffer, 1) == 1);
     196    ASSERT (*buffer == '2');
     197  
     198  #if GNULIB_TEST_CLOEXEC
     199    /* Any new fd created by dup2 must not be cloexec.  */
     200    ASSERT (close (fd + 2) == 0);
     201    ASSERT (dup_cloexec (fd) == fd + 1);
     202    ASSERT (!is_inheritable (fd + 1));
     203    ASSERT (dup2 (fd + 1, fd + 1) == fd + 1);
     204    ASSERT (!is_inheritable (fd + 1));
     205    ASSERT (dup2 (fd + 1, fd + 2) == fd + 2);
     206    ASSERT (!is_inheritable (fd + 1));
     207    ASSERT (is_inheritable (fd + 2));
     208    errno = 0;
     209    ASSERT (dup2 (fd + 1, -1) == -1);
     210    ASSERT (errno == EBADF);
     211    ASSERT (!is_inheritable (fd + 1));
     212  #endif
     213  
     214    /* On systems that distinguish between text and binary mode, dup2
     215       reuses the mode of the source.  */
     216    set_binary_mode (fd, O_BINARY);
     217    ASSERT (is_mode (fd, O_BINARY));
     218    ASSERT (dup2 (fd, fd + 1) == fd + 1);
     219    ASSERT (is_mode (fd + 1, O_BINARY));
     220    set_binary_mode (fd, O_TEXT);
     221    ASSERT (is_mode (fd, O_TEXT));
     222    ASSERT (dup2 (fd, fd + 1) == fd + 1);
     223    ASSERT (is_mode (fd + 1, O_TEXT));
     224  
     225    /* Clean up.  */
     226    ASSERT (close (fd + 2) == 0);
     227    ASSERT (close (fd + 1) == 0);
     228    ASSERT (close (fd) == 0);
     229    ASSERT (unlink (file) == 0);
     230  
     231    return 0;
     232  }