Blame hurd/port2fd.c

Packit 6c4009
/* Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/fd.h>
Packit 6c4009
#include <hurd/signal.h>
Packit 6c4009
#include <hurd/term.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
Packit 6c4009
/* Store PORT in file descriptor D, doing appropriate ctty magic.
Packit 6c4009
   FLAGS are as for `open'; only O_IGNORE_CTTY and O_CLOEXEC are meaningful.
Packit 6c4009
   D should be locked, and will not be unlocked.  */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_hurd_port2fd (struct hurd_fd *d, io_t dport, int flags)
Packit 6c4009
{
Packit 6c4009
  mach_port_t cttyid;
Packit 6c4009
  io_t ctty = MACH_PORT_NULL;
Packit 6c4009
Packit 6c4009
  if (!(flags & O_IGNORE_CTTY))
Packit 6c4009
    __USEPORT (CTTYID,
Packit 6c4009
	       ({
Packit 6c4009
		 if (port != MACH_PORT_NULL && /* Do we have a ctty? */
Packit 6c4009
		     ! __term_getctty (dport, &cttyid))	/* Could this be it? */
Packit 6c4009
		   {
Packit 6c4009
		     __mach_port_deallocate (__mach_task_self (), cttyid);
Packit 6c4009
		     /* This port is capable of being a controlling tty.
Packit 6c4009
			Is it ours?  */
Packit 6c4009
		     if (cttyid == port)
Packit 6c4009
		       __term_open_ctty (dport, _hurd_pid, _hurd_pgrp, &ctty);
Packit 6c4009
		     /* XXX if this port is our ctty, but we are not doing
Packit 6c4009
			ctty style i/o because term_become_ctty barfed,
Packit 6c4009
			what to do?  */
Packit 6c4009
		   }
Packit 6c4009
		 0;
Packit 6c4009
	       }));
Packit 6c4009
Packit 6c4009
  /* Install PORT in the descriptor cell, leaving it locked.  */
Packit 6c4009
  {
Packit 6c4009
    mach_port_t old
Packit 6c4009
      = _hurd_userlink_clear (&d->port.users) ? d->port.port : MACH_PORT_NULL;
Packit 6c4009
    d->port.port = dport;
Packit 6c4009
    d->flags = (flags & O_CLOEXEC) ? FD_CLOEXEC : 0;
Packit 6c4009
    if (old != MACH_PORT_NULL)
Packit 6c4009
      __mach_port_deallocate (__mach_task_self (), old);
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  _hurd_port_set (&d->ctty, ctty);
Packit 6c4009
}