port2fd.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright (C) 1994-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <hurd.h>
  15. #include <hurd/fd.h>
  16. #include <hurd/signal.h>
  17. #include <hurd/term.h>
  18. #include <fcntl.h>
  19. /* Store PORT in file descriptor D, doing appropriate ctty magic.
  20. FLAGS are as for `open'; only O_IGNORE_CTTY and O_CLOEXEC are meaningful.
  21. D should be locked, and will not be unlocked. */
  22. void
  23. _hurd_port2fd (struct hurd_fd *d, io_t dport, int flags)
  24. {
  25. mach_port_t cttyid;
  26. io_t ctty = MACH_PORT_NULL;
  27. if (!(flags & O_IGNORE_CTTY))
  28. __USEPORT (CTTYID,
  29. ({
  30. if (port != MACH_PORT_NULL /* Do we have a ctty? */
  31. && ! __term_getctty (dport, &cttyid))
  32. /* Could this be it? */
  33. {
  34. __mach_port_deallocate (__mach_task_self (), cttyid);
  35. /* This port is capable of being a controlling tty.
  36. Is it ours? */
  37. if (cttyid == port)
  38. __term_open_ctty (dport, _hurd_pid, _hurd_pgrp, &ctty);
  39. /* XXX if this port is our ctty, but we are not doing
  40. ctty style i/o because term_become_ctty barfed,
  41. what to do? */
  42. }
  43. 0;
  44. }));
  45. /* Install PORT in the descriptor cell, leaving it locked. */
  46. {
  47. mach_port_t old
  48. = _hurd_userlink_clear (&d->port.users) ? d->port.port : MACH_PORT_NULL;
  49. d->port.port = dport;
  50. d->flags = (flags & O_CLOEXEC) ? FD_CLOEXEC : 0;
  51. if (old != MACH_PORT_NULL)
  52. __mach_port_deallocate (__mach_task_self (), old);
  53. }
  54. _hurd_port_set (&d->ctty, ctty);
  55. }