1  /* Duplicate a file descriptor to a given number, with flags.  Hurd version.
       2     Copyright (C) 1991-2023 Free Software Foundation, Inc.
       3  
       4     This file is part of the GNU C Library.
       5  
       6     The GNU C Library is free software; you can redistribute it and/or
       7     modify it under the terms of the GNU Lesser General Public
       8     License as published by the Free Software Foundation; either
       9     version 2.1 of the License, or (at your option) any later version.
      10  
      11     The GNU C Library is distributed in the hope that it will be useful,
      12     but WITHOUT ANY WARRANTY; without even the implied warranty of
      13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14     Lesser General Public License for more details.
      15  
      16     You should have received a copy of the GNU Lesser General Public
      17     License along with the GNU C Library; see the file COPYING.LIB.  If
      18     not, see <https://www.gnu.org/licenses/>.  */
      19  
      20  #include <errno.h>
      21  #include <fcntl.h>
      22  #include <unistd.h>
      23  #include <hurd.h>
      24  #include <hurd/fd.h>
      25  
      26  /* Duplicate FD to FD2, closing the old FD2 and making FD2 be
      27     open on the same file as FD is, and setting FD2's flags according to FLAGS.
      28     Return FD2 or -1.  */
      29  int
      30  __dup3 (int fd, int fd2, int flags)
      31  {
      32    struct hurd_fd *d;
      33  
      34    /* Both passing flags different from O_CLOEXEC and FD2 being the same as FD
      35       are invalid.  */
      36    if ((flags & ~O_CLOEXEC
      37         || fd2 == fd)
      38        /* ... with the exception in case that dup2 behavior is requested: if FD
      39  	 is valid and FD2 is already the same then just return it.  */
      40        && ! (flags == -1
      41  	    && fd2 == fd))
      42      return __hurd_fail (EINVAL);
      43  
      44    /* Extract the ports and flags from FD.  */
      45    d = _hurd_fd_get (fd);
      46    if (d == NULL)
      47      return __hurd_fail (EBADF);
      48  
      49    HURD_CRITICAL_BEGIN;
      50  
      51    __spin_lock (&d->port.lock);
      52    if (d->port.port == MACH_PORT_NULL)
      53      {
      54        __spin_unlock (&d->port.lock);
      55        fd2 = __hurd_fail (EBADF);
      56      }
      57    else if (fd2 == fd)
      58      __spin_unlock (&d->port.lock);
      59    else
      60      {
      61        struct hurd_userlink ulink, ctty_ulink;
      62        int d_flags = d->flags;
      63        io_t ctty = _hurd_port_get (&d->ctty, &ctty_ulink);
      64        io_t port = _hurd_port_locked_get (&d->port, &ulink); /* Unlocks D.  */
      65  
      66        if (fd2 < 0)
      67  	fd2 = __hurd_fail (EBADF);
      68        else
      69  	{
      70  	  /* Get a hold of the destination descriptor.  */
      71  	  struct hurd_fd *d2;
      72  
      73  	  __mutex_lock (&_hurd_dtable_lock);
      74  
      75  	  if (fd2 >= _hurd_dtablesize)
      76  	    {
      77  	      /* The table is not large enough to hold the destination
      78  		 descriptor.  Enlarge it as necessary to allocate this
      79  		 descriptor.  */
      80  	      __mutex_unlock (&_hurd_dtable_lock);
      81  	      d2 = _hurd_alloc_fd (NULL, fd2);
      82  	      if (d2)
      83  		__spin_unlock (&d2->port.lock);
      84  	      __mutex_lock (&_hurd_dtable_lock);
      85  	    }
      86  	  else
      87  	    {
      88  	      d2 = _hurd_dtable[fd2];
      89  	      if (d2 == NULL)
      90  		{
      91  		  /* Must allocate a new one.  We don't initialize the port
      92  		     cells with this call so that if it fails (out of
      93  		     memory), we will not have already added user
      94  		     references for the ports, which we would then have to
      95  		     deallocate.  */
      96  		  d2 = _hurd_dtable[fd2] = _hurd_new_fd (MACH_PORT_NULL,
      97  							 MACH_PORT_NULL);
      98  		}
      99  	    }
     100  	  __mutex_unlock (&_hurd_dtable_lock);
     101  
     102  	  if (d2 == NULL)
     103  	    {
     104  	      fd2 = -1;
     105  	      if (errno == EINVAL)
     106  		__hurd_fail (EBADF);	/* POSIX.1-1990 6.2.1.2 ll 54-55.  */
     107  	    }
     108  	  else
     109  	    {
     110  	      /* Give the ports each a user ref for the new descriptor.  */
     111  	      __mach_port_mod_refs (__mach_task_self (), port,
     112  				    MACH_PORT_RIGHT_SEND, 1);
     113  	      if (ctty != MACH_PORT_NULL)
     114  		__mach_port_mod_refs (__mach_task_self (), ctty,
     115  				      MACH_PORT_RIGHT_SEND, 1);
     116  
     117  	      /* Install the ports and flags in the new descriptor slot.  */
     118  	      __spin_lock (&d2->port.lock);
     119  	      if (flags & O_CLOEXEC)
     120  		d2->flags = d_flags | FD_CLOEXEC;
     121  	      else
     122  		/* dup clears FD_CLOEXEC.  */
     123  		d2->flags = d_flags & ~FD_CLOEXEC;
     124  	      _hurd_port_set (&d2->ctty, ctty);
     125  	      _hurd_port_locked_set (&d2->port, port); /* Unlocks D2.  */
     126  	    }
     127  	}
     128  
     129        _hurd_port_free (&d->port, &ulink, port);
     130        if (ctty != MACH_PORT_NULL)
     131  	_hurd_port_free (&d->ctty, &ctty_ulink, port);
     132      }
     133  
     134    HURD_CRITICAL_END;
     135  
     136    return fd2;
     137  }
     138  libc_hidden_def (__dup3)
     139  weak_alias (__dup3, dup3)