connection.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Connect implementation
  2. Copyright (C) 2014 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::connection::~connection ()
  25. {
  26. }
  27. void
  28. cc1_plugin::connection::print (const char *)
  29. {
  30. }
  31. cc1_plugin::status
  32. cc1_plugin::connection::send (char c)
  33. {
  34. if (write (m_fd, &c, 1) != 1)
  35. return FAIL;
  36. return OK;
  37. }
  38. cc1_plugin::status
  39. cc1_plugin::connection::send (const void *buf, int len)
  40. {
  41. if (write (m_fd, buf, len) != len)
  42. return FAIL;
  43. return OK;
  44. }
  45. cc1_plugin::status
  46. cc1_plugin::connection::require (char c)
  47. {
  48. char result;
  49. if (read (m_fd, &result, 1) != 1
  50. || result != c)
  51. return FAIL;
  52. return OK;
  53. }
  54. cc1_plugin::status
  55. cc1_plugin::connection::get (void *buf, int len)
  56. {
  57. if (read (m_fd, buf, len) != len)
  58. return FAIL;
  59. return OK;
  60. }
  61. cc1_plugin::status
  62. cc1_plugin::connection::do_wait (bool want_result)
  63. {
  64. while (true)
  65. {
  66. char result;
  67. fd_set read_set;
  68. FD_ZERO (&read_set);
  69. FD_SET (m_fd, &read_set);
  70. if (m_aux_fd != -1)
  71. FD_SET (m_aux_fd, &read_set);
  72. int nfds = select (FD_SETSIZE, &read_set, NULL, NULL, NULL);
  73. if (nfds == -1)
  74. {
  75. if (errno == EINTR)
  76. continue;
  77. return FAIL;
  78. }
  79. // We have to check the stderr fd first, to avoid a possible
  80. // blocking scenario when do_wait is called reentrantly. In
  81. // such a call, if we handle the primary fd first, then we may
  82. // re-enter this function, read from gcc's stderr, causing the
  83. // outer invocation of this function to block when trying to
  84. // read.
  85. if (m_aux_fd != -1 && FD_ISSET (m_aux_fd, &read_set))
  86. {
  87. char buf[1024];
  88. int n = read (m_aux_fd, buf, sizeof (buf) - 1);
  89. if (n < 0)
  90. return FAIL;
  91. if (n > 0)
  92. {
  93. buf[n] = '\0';
  94. print (buf);
  95. }
  96. }
  97. if (FD_ISSET (m_fd, &read_set))
  98. {
  99. int n = read (m_fd, &result, 1);
  100. if (n == 0)
  101. return want_result ? FAIL : OK;
  102. if (n != 1)
  103. return FAIL;
  104. switch (result)
  105. {
  106. case 'R':
  107. // The reply is ready; the caller will unmarshall it.
  108. return want_result ? OK : FAIL;
  109. case 'Q':
  110. // While waiting for a reply, the other side made a method
  111. // call.
  112. {
  113. // Use an argument_wrapper here to simplify management
  114. // of the string's lifetime.
  115. argument_wrapper<char *> method_name;
  116. if (!method_name.unmarshall (this))
  117. return FAIL;
  118. callback_ftype *callback
  119. = m_callbacks.find_callback (method_name);
  120. // The call to CALLBACK is where we may end up in a
  121. // reentrant call.
  122. if (callback == NULL || !callback (this))
  123. return FAIL;
  124. }
  125. break;
  126. default:
  127. return FAIL;
  128. }
  129. }
  130. }
  131. }