connection.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Connect implementation
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <cc1plugin-config.h>
  16. #include <string>
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include "marshall.hh"
  22. #include "connection.hh"
  23. #include "rpc.hh"
  24. cc1_plugin::status
  25. cc1_plugin::connection::send (char c)
  26. {
  27. if (write (m_fd, &c, 1) != 1)
  28. return FAIL;
  29. return OK;
  30. }
  31. cc1_plugin::status
  32. cc1_plugin::connection::send (const void *buf, int len)
  33. {
  34. if (write (m_fd, buf, len) != len)
  35. return FAIL;
  36. return OK;
  37. }
  38. cc1_plugin::status
  39. cc1_plugin::connection::require (char c)
  40. {
  41. char result;
  42. if (read (m_fd, &result, 1) != 1
  43. || result != c)
  44. return FAIL;
  45. return OK;
  46. }
  47. cc1_plugin::status
  48. cc1_plugin::connection::get (void *buf, int len)
  49. {
  50. if (read (m_fd, buf, len) != len)
  51. return FAIL;
  52. return OK;
  53. }
  54. cc1_plugin::status
  55. cc1_plugin::connection::do_wait (bool want_result)
  56. {
  57. while (true)
  58. {
  59. char result;
  60. fd_set read_set;
  61. FD_ZERO (&read_set);
  62. FD_SET (m_fd, &read_set);
  63. if (m_aux_fd != -1)
  64. FD_SET (m_aux_fd, &read_set);
  65. int nfds = select (FD_SETSIZE, &read_set, NULL, NULL, NULL);
  66. if (nfds == -1)
  67. {
  68. if (errno == EINTR)
  69. continue;
  70. return FAIL;
  71. }
  72. // We have to check the stderr fd first, to avoid a possible
  73. // blocking scenario when do_wait is called reentrantly. In
  74. // such a call, if we handle the primary fd first, then we may
  75. // re-enter this function, read from gcc's stderr, causing the
  76. // outer invocation of this function to block when trying to
  77. // read.
  78. if (m_aux_fd != -1 && FD_ISSET (m_aux_fd, &read_set))
  79. {
  80. char buf[1024];
  81. int n = read (m_aux_fd, buf, sizeof (buf) - 1);
  82. if (n < 0)
  83. return FAIL;
  84. if (n > 0)
  85. {
  86. buf[n] = '\0';
  87. print (buf);
  88. }
  89. }
  90. if (FD_ISSET (m_fd, &read_set))
  91. {
  92. int n = read (m_fd, &result, 1);
  93. if (n == 0)
  94. return want_result ? FAIL : OK;
  95. if (n != 1)
  96. return FAIL;
  97. switch (result)
  98. {
  99. case 'R':
  100. // The reply is ready; the caller will unmarshall it.
  101. return want_result ? OK : FAIL;
  102. case 'Q':
  103. // While waiting for a reply, the other side made a method
  104. // call.
  105. {
  106. // Use an argument_wrapper here to simplify management
  107. // of the string's lifetime.
  108. argument_wrapper<char *> method_name;
  109. if (!method_name.unmarshall (this))
  110. return FAIL;
  111. callback_ftype *callback
  112. = m_callbacks.find_callback (method_name.get ());
  113. // The call to CALLBACK is where we may end up in a
  114. // reentrant call.
  115. if (callback == NULL || !callback (this))
  116. return FAIL;
  117. }
  118. break;
  119. default:
  120. return FAIL;
  121. }
  122. }
  123. }
  124. }