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