]> git.ipfire.org Git - thirdparty/glibc.git/blob - hurd/port2fd.c
initial import
[thirdparty/glibc.git] / hurd / port2fd.c
1 /* Copyright (C) 1994 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library 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 GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
18
19 #include <hurd.h>
20 #include <hurd/fd.h>
21 #include <hurd/signal.h>
22 #include <hurd/term.h>
23 #include <fcntl.h>
24
25 /* Store PORT in file descriptor D, doing appropriate ctty magic.
26 FLAGS are as for `open'; only O_IGNORE_CTTY is meaningful.
27 D should be locked, and will not be unlocked. */
28
29 void
30 _hurd_port2fd (struct hurd_fd *d, io_t port, int flags)
31 {
32 io_t ctty;
33 mach_port_t cttyid;
34 int is_ctty = !(flags & O_IGNORE_CTTY) && ! __term_getctty (port, &cttyid);
35
36 if (is_ctty)
37 {
38 /* This port is capable of being a controlling tty.
39 Is it ours? */
40 struct hurd_port *const id = &_hurd_ports[INIT_PORT_CTTYID];
41 __spin_lock (&id->lock);
42 if (id->port == MACH_PORT_NULL)
43 /* We have no controlling tty, so make this one it. */
44 _hurd_port_locked_set (id, cttyid);
45 else
46 {
47 if (cttyid != id->port)
48 /* We have a controlling tty and this is not it. */
49 is_ctty = 0;
50 /* Either we don't want CTTYID, or ID->port already is it.
51 So we don't need to change ID->port, and we can release
52 the reference to CTTYID. */
53 __spin_unlock (&id->lock);
54 __mach_port_deallocate (__mach_task_self (), cttyid);
55 }
56 }
57
58 if (!is_ctty || __term_open_ctty (port, _hurd_pid, _hurd_pgrp, &ctty) != 0)
59 /* XXX if IS_CTTY, then this port is our ctty, but we are
60 not doing ctty style i/o because term_become_ctty barfed.
61 What to do? */
62 /* No ctty magic happening here. */
63 ctty = MACH_PORT_NULL;
64
65 /* Install PORT in the descriptor cell, leaving it locked. */
66 {
67 mach_port_t old
68 = _hurd_userlink_clear (&d->port.users) ? d->port.port : MACH_PORT_NULL;
69 d->port.port = port;
70 if (old != MACH_PORT_NULL)
71 __mach_port_deallocate (__mach_task_self (), old);
72 }
73
74 _hurd_port_set (&d->ctty, ctty);
75 }