ctty-output.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* _hurd_ctty_output -- Do an output RPC and generate SIGTTOU if necessary.
  2. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <hurd.h>
  16. #include <hurd/signal.h>
  17. /* Call *RPC on PORT and/or CTTY. If a call on CTTY returns EBACKGROUND,
  18. generate SIGTTOU if appropriate. */
  19. error_t
  20. _hurd_ctty_output (io_t port, io_t ctty, error_t (*rpc) (io_t))
  21. {
  22. if (ctty == MACH_PORT_NULL)
  23. return (*rpc) (port);
  24. else
  25. {
  26. struct hurd_sigstate *ss = _hurd_self_sigstate ();
  27. error_t err;
  28. do
  29. {
  30. struct sigaction *actions;
  31. /* Don't use the ctty io port if we are blocking or ignoring
  32. SIGTTOU. We redo this check at the top of the loop in case
  33. the signal handler changed the state. */
  34. _hurd_sigstate_lock (ss);
  35. actions = _hurd_sigstate_actions (ss);
  36. if (__sigismember (&ss->blocked, SIGTTOU)
  37. || actions[SIGTTOU].sa_handler == SIG_IGN)
  38. err = EIO;
  39. else
  40. err = 0;
  41. _hurd_sigstate_unlock (ss);
  42. if (err)
  43. return (*rpc) (port);
  44. err = (*rpc) (ctty);
  45. if (err == EBACKGROUND)
  46. {
  47. if (_hurd_orphaned)
  48. /* Our process group is orphaned, so we never generate a
  49. signal; we just fail. */
  50. err = EIO;
  51. else
  52. {
  53. /* Send a SIGTTOU signal to our process group.
  54. We must remember here not to clobber ERR, since
  55. the loop condition below uses it to recall that
  56. we should retry after a stop. */
  57. __USEPORT (CTTYID, _hurd_sig_post (0, SIGTTOU, port));
  58. /* XXX what to do if error here? */
  59. /* At this point we should have just run the handler for
  60. SIGTTOU or resumed after being stopped. Now this is
  61. still a "system call", so check to see if we should
  62. restart it. */
  63. _hurd_sigstate_lock (ss);
  64. actions = _hurd_sigstate_actions (ss);
  65. if (!(actions[SIGTTOU].sa_flags & SA_RESTART))
  66. err = EINTR;
  67. _hurd_sigstate_unlock (ss);
  68. }
  69. }
  70. /* If the last RPC generated a SIGTTOU, loop to try it again. */
  71. } while (err == EBACKGROUND);
  72. return err;
  73. }
  74. }