Back to index

glibc  2.9
port2fd.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994, 1997, 1999, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <hurd.h>
00020 #include <hurd/fd.h>
00021 #include <hurd/signal.h>
00022 #include <hurd/term.h>
00023 #include <fcntl.h>
00024 
00025 /* Store PORT in file descriptor D, doing appropriate ctty magic.
00026    FLAGS are as for `open'; only O_IGNORE_CTTY is meaningful.
00027    D should be locked, and will not be unlocked.  */
00028 
00029 void
00030 _hurd_port2fd (struct hurd_fd *d, io_t dport, int flags)
00031 {
00032   mach_port_t cttyid;
00033   io_t ctty = MACH_PORT_NULL;
00034 
00035   if (!(flags & O_IGNORE_CTTY))
00036     __USEPORT (CTTYID,
00037               ({
00038                if (port != MACH_PORT_NULL && /* Do we have a ctty? */
00039                    ! __term_getctty (dport, &cttyid))   /* Could this be it? */
00040                  {
00041                    __mach_port_deallocate (__mach_task_self (), cttyid);
00042                    /* This port is capable of being a controlling tty.
00043                      Is it ours?  */
00044                    if (cttyid == port)
00045                      __term_open_ctty (dport, _hurd_pid, _hurd_pgrp, &ctty);
00046                    /* XXX if this port is our ctty, but we are not doing
00047                      ctty style i/o because term_become_ctty barfed,
00048                      what to do?  */
00049                  }
00050                0;
00051               }));
00052 
00053   /* Install PORT in the descriptor cell, leaving it locked.  */
00054   {
00055     mach_port_t old
00056       = _hurd_userlink_clear (&d->port.users) ? d->port.port : MACH_PORT_NULL;
00057     d->port.port = dport;
00058     d->flags = (flags & O_CLOEXEC) ? FD_CLOEXEC : 0;
00059     if (old != MACH_PORT_NULL)
00060       __mach_port_deallocate (__mach_task_self (), old);
00061   }
00062 
00063   _hurd_port_set (&d->ctty, ctty);
00064 }