ctty-input.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* _hurd_ctty_input -- Do an input RPC and generate SIGTTIN 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 SIGTTIN or EIO as appropriate. */
  19. error_t
  20. _hurd_ctty_input (io_t port, io_t ctty, error_t (*rpc) (io_t))
  21. {
  22. error_t err;
  23. if (ctty == MACH_PORT_NULL)
  24. return (*rpc) (port);
  25. do
  26. {
  27. err = (*rpc) (ctty);
  28. if (err == EBACKGROUND)
  29. {
  30. /* We are a background job and tried to read from the tty.
  31. We should probably get a SIGTTIN signal. */
  32. if (_hurd_orphaned)
  33. /* Our process group is orphaned. Don't stop; just fail. */
  34. err = EIO;
  35. else
  36. {
  37. struct hurd_sigstate *ss = _hurd_self_sigstate ();
  38. struct sigaction *actions;
  39. _hurd_sigstate_lock (ss);
  40. actions = _hurd_sigstate_actions (ss);
  41. if (__sigismember (&ss->blocked, SIGTTIN)
  42. || actions[SIGTTIN].sa_handler == SIG_IGN)
  43. /* We are blocking or ignoring SIGTTIN. Just fail. */
  44. err = EIO;
  45. _hurd_sigstate_unlock (ss);
  46. if (err == EBACKGROUND)
  47. {
  48. /* Send a SIGTTIN signal to our process group.
  49. We must remember here not to clobber ERR, since
  50. the loop condition below uses it to recall that
  51. we should retry after a stop. */
  52. __USEPORT (CTTYID, _hurd_sig_post (0, SIGTTIN, port));
  53. /* XXX what to do if error here? */
  54. /* At this point we should have just run the handler for
  55. SIGTTIN or resumed after being stopped. Now this is
  56. still a "system call", so check to see if we should
  57. restart it. */
  58. _hurd_sigstate_lock (ss);
  59. actions = _hurd_sigstate_actions (ss);
  60. if (!(actions[SIGTTIN].sa_flags & SA_RESTART))
  61. err = EINTR;
  62. _hurd_sigstate_unlock (ss);
  63. }
  64. }
  65. }
  66. /* If the last RPC generated a SIGTTIN, loop to try it again. */
  67. } while (err == EBACKGROUND);
  68. return err;
  69. }