event-pipe.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Event pipe for GDB, the GNU debugger.
  2. Copyright (C) 2021 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "gdbsupport/common-defs.h"
  15. #include "gdbsupport/event-pipe.h"
  16. #include "gdbsupport/filestuff.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. event_pipe::~event_pipe ()
  21. {
  22. if (is_open ())
  23. close_pipe ();
  24. }
  25. /* See event-pipe.h. */
  26. bool
  27. event_pipe::open_pipe ()
  28. {
  29. if (is_open ())
  30. return false;
  31. if (gdb_pipe_cloexec (m_fds) == -1)
  32. return false;
  33. if (fcntl (m_fds[0], F_SETFL, O_NONBLOCK) == -1
  34. || fcntl (m_fds[1], F_SETFL, O_NONBLOCK) == -1)
  35. {
  36. close_pipe ();
  37. return false;
  38. }
  39. return true;
  40. }
  41. /* See event-pipe.h. */
  42. void
  43. event_pipe::close_pipe ()
  44. {
  45. ::close (m_fds[0]);
  46. ::close (m_fds[1]);
  47. m_fds[0] = -1;
  48. m_fds[1] = -1;
  49. }
  50. /* See event-pipe.h. */
  51. void
  52. event_pipe::flush ()
  53. {
  54. int ret;
  55. char buf;
  56. do
  57. {
  58. ret = read (m_fds[0], &buf, 1);
  59. }
  60. while (ret >= 0 || (ret == -1 && errno == EINTR));
  61. }
  62. /* See event-pipe.h. */
  63. void
  64. event_pipe::mark ()
  65. {
  66. int ret;
  67. /* It doesn't really matter what the pipe contains, as long we end
  68. up with something in it. Might as well flush the previous
  69. left-overs. */
  70. flush ();
  71. do
  72. {
  73. ret = write (m_fds[1], "+", 1);
  74. }
  75. while (ret == -1 && errno == EINTR);
  76. /* Ignore EAGAIN. If the pipe is full, the event loop will already
  77. be awakened anyway. */
  78. }