Debian Codegraph @

gcc/cp/mapper-client.cc

raw
1 /* C++ modules. Experimental!
2 Copyright (C) 2017-2025 Free Software Foundation, Inc.
3 Written by Nathan Sidwell <[email protected]> while at FaceBook
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #if defined (__unix__)
23 // Solaris11's socket header used bcopy, which we poison. cody.hh
24 // will include it later under the above check
25 #include <sys/socket.h>
26 #endif
27 #define INCLUDE_STRING
28 #define INCLUDE_VECTOR
29 #define INCLUDE_MAP
30 #include "system.h"
31
32 #include "line-map.h"
33 #include "rich-location.h"
34 #include "diagnostic-core.h"
35 #include "mapper-client.h"
36 #include "intl.h"
37 #include "mkdeps.h"
38
39 #include "../../c++tools/resolver.h"
40
41 #if !HOST_HAS_O_CLOEXEC
42 #define O_CLOEXEC 0
43 #endif
44
45 module_client::module_client (pex_obj *p, int fd_from, int fd_to)
46 : Client (fd_from, fd_to), pex (p)
47 {
48 }
49
50 static module_client *
51 spawn_mapper_program (char const **errmsg, std::string &name,
52 char const *full_program_name)
53 {
54 /* Split writable at white-space. No space-containing args for
55 you! */
56 // At most every other char could be an argument
57 char **argv = new char *[name.size () / 2 + 2];
58 unsigned arg_no = 0;
59 char *str = new char[name.size ()];
60 memcpy (str, name.c_str () + 1, name.size ());
61
62 for (auto ptr = str; ; ++ptr)
63 {
64 while (*ptr == ' ')
65 ptr++;
66 if (!*ptr)
67 break;
68
69 if (!arg_no)
70 {
71 /* @name means look in the compiler's install dir. */
72 if (ptr[0] == '@')
73 ptr++;
74 else
75 full_program_name = nullptr;
76 }
77
78 argv[arg_no++] = ptr;
79 while (*ptr && *ptr != ' ')
80 ptr++;
81 if (!*ptr)
82 break;
83 *ptr = 0;
84 }
85 argv[arg_no] = nullptr;
86
87 auto *pex = pex_init (PEX_USE_PIPES, progname, NULL);
88 FILE *to = pex_input_pipe (pex, false);
89 name = argv[0];
90 if (!to)
91 *errmsg = "connecting input";
92 else
93 {
94 int flags = PEX_SEARCH;
95
96 if (full_program_name)
97 {
98 /* Prepend the invoking path, if the mapper is a simple
99 file name. */
100 size_t dir_len = progname - full_program_name;
101 std::string argv0;
102 argv0.reserve (dir_len + name.size ());
103 argv0.append (full_program_name, dir_len).append (name);
104 name = std::move (argv0);
105 argv[0] = const_cast <char *> (name.c_str ());
106 flags = 0;
107 }
108 int err;
109 *errmsg = pex_run (pex, flags, argv[0], argv, NULL, NULL, &err);
110 }
111 delete[] str;
112 delete[] argv;
113
114 int fd_from = -1, fd_to = -1;
115 if (!*errmsg)
116 {
117 FILE *from = pex_read_output (pex, false);
118 if (from && (fd_to = dup (fileno (to))) >= 0)
119 fd_from = fileno (from);
120 else
121 *errmsg = "connecting output";
122 fclose (to);
123 }
124
125 if (*errmsg)
126 {
127 pex_free (pex);
128 return nullptr;
129 }
130
131 return new module_client (pex, fd_from, fd_to);
132 }
133
134 module_client *
135 module_client::open_module_client (location_t loc, const char *o,
136 class mkdeps *deps,
137 void (*set_repo) (const char *),
138 char const *full_program_name)
139 {
140 module_client *c = nullptr;
141 std::string ident;
142 std::string name;
143 char const *errmsg = nullptr;
144 unsigned line = 0;
145
146 if (o && o[0])
147 {
148 /* Maybe a local or ipv6 address. */
149 name = o;
150 auto last = name.find_last_of ('?');
151 if (last != name.npos)
152 {
153 ident = name.substr (last + 1);
154 name.erase (last);
155 }
156
157 if (name.size ())
158 {
159 switch (name[0])
160 {
161 case '<':
162 // <from>to or <>fromto, or <>
163 {
164 size_t pos = name.find ('>', 1);
165 if (pos == std::string::npos)
166 pos = name.size ();
167 std::string from (name, 1, pos - 1);
168 std::string to;
169 if (pos != name.size ())
170 to.append (name, pos + 1, std::string::npos);
171
172 int fd_from = -1, fd_to = -1;
173 if (from.empty () && to.empty ())
174 {
175 fd_from = fileno (stdin);
176 fd_to = fileno (stdout);
177 }
178 else
179 {
180 char *ptr;
181 if (!from.empty ())
182 {
183 /* Sadly str::stoul is not portable. */
184 const char *cstr = from.c_str ();
185 fd_from = strtoul (cstr, &ptr, 10);
186 if (*ptr)
187 {
188 /* Not a number -- a named pipe. */
189 int dir = to.empty ()
190 ? O_RDWR | O_CLOEXEC : O_RDONLY | O_CLOEXEC;
191 fd_from = open (cstr, dir);
192 }
193 if (to.empty ())
194 fd_to = fd_from;
195 }
196
197 if (!from.empty () && fd_from < 0)
198 ;
199 else if (to.empty ())
200 ;
201 else
202 {
203 const char *cstr = to.c_str ();
204 fd_to = strtoul (cstr, &ptr, 10);
205 if (*ptr)
206 {
207 /* Not a number, a named pipe. */
208 int dir = from.empty ()
209 ? O_RDWR | O_CLOEXEC : O_WRONLY | O_CLOEXEC;
210 fd_to = open (cstr, dir);
211 if (fd_to < 0)
212 close (fd_from);
213 }
214 if (from.empty ())
215 fd_from = fd_to;
216 }
217 }
218
219 if (fd_from < 0 || fd_to < 0)
220 errmsg = "opening";
221 else
222 c = new module_client (fd_from, fd_to);
223 }
224 break;
225
226 case '=':
227 // =localsocket
228 {
229 int fd = -1;
230 #if CODY_NETWORKING
231 fd = Cody::OpenLocal (&errmsg, name.c_str () + 1);
232 #else
233 errmsg = "disabled";
234 #endif
235 if (fd >= 0)
236 c = new module_client (fd, fd);
237 }
238 break;
239
240 case '|':
241 // |program and args
242 c = spawn_mapper_program (&errmsg, name, full_program_name);
243 break;
244
245 default:
246 // file or hostname:port
247 {
248 auto colon = name.find_last_of (':');
249 if (colon != name.npos)
250 {
251 char const *cptr = name.c_str () + colon;
252 char *endp;
253 unsigned port = strtoul (cptr + 1, &endp, 10);
254
255 if (port && endp != cptr + 1 && !*endp)
256 {
257 name[colon] = 0;
258 int fd = -1;
259 #if CODY_NETWORKING
260 fd = Cody::OpenInet6 (&errmsg, name.c_str (), port);
261 #else
262 errmsg = "disabled";
263 #endif
264 name[colon] = ':';
265
266 if (fd >= 0)
267 c = new module_client (fd, fd);
268 }
269 }
270
271 }
272 break;
273 }
274 }
275 }
276
277 if (!c)
278 {
279 // Make a default in-process client
280 bool file = !errmsg && !name.empty ();
281 auto r = new module_resolver (!file, true);
282
283 if (file)
284 {
285 int fd = open (name.c_str (), O_RDONLY | O_CLOEXEC);
286 if (fd < 0)
287 errmsg = "opening";
288 else
289 {
290 /* Add the mapper file to the dependency tracking. */
291 if (deps)
292 deps_add_dep (deps, name.c_str ());
293 if (int l = r->read_tuple_file (fd, ident, false))
294 {
295 if (l > 0)
296 line = l;
297 errmsg = "reading";
298 }
299
300 close (fd);
301 }
302 }
303 else
304 r->set_repo ("gcm.cache");
305
306 auto *s = new Cody::Server (r);
307 c = new module_client (s);
308 }
309
310 #ifdef SIGPIPE
311 if (!c->IsDirect ())
312 /* We need to ignore sig pipe for a while. */
313 c->sigpipe = signal (SIGPIPE, SIG_IGN);
314 #endif
315
316 if (errmsg)
317 error_at (loc, line ? G_("failed %s mapper %qs line %u")
318 : G_("failed %s mapper %qs"), errmsg, name.c_str (), line);
319
320 // now wave hello!
321 c->Cork ();
322 c->Connect (std::string ("GCC"), ident);
323 c->ModuleRepo ();
324 auto packets = c->Uncork ();
325
326 auto &connect = packets[0];
327 if (connect.GetCode () == Cody::Client::PC_CONNECT)
328 c->flags = Cody::Flags (connect.GetInteger ());
329 else if (connect.GetCode () == Cody::Client::PC_ERROR)
330 error_at (loc, "failed mapper handshake %s", connect.GetString ().c_str ());
331
332 auto &repo = packets[1];
333 if (repo.GetCode () == Cody::Client::PC_PATHNAME)
334 set_repo (repo.GetString ().c_str ());
335
336 return c;
337 }
338
339 void
340 module_client::close_module_client (location_t loc, module_client *mapper)
341 {
342 if (mapper->IsDirect ())
343 {
344 auto *s = mapper->GetServer ();
345 auto *r = s->GetResolver ();
346 delete s;
347 delete r;
348 }
349 else
350 {
351 if (mapper->pex)
352 {
353 int fd_write = mapper->GetFDWrite ();
354 if (fd_write >= 0)
355 close (fd_write);
356
357 int status;
358 pex_get_status (mapper->pex, 1, &status);
359
360 pex_free (mapper->pex);
361 mapper->pex = NULL;
362
363 if (WIFSIGNALED (status))
364 error_at (loc, "mapper died by signal %s",
365 strsignal (WTERMSIG (status)));
366 else if (WIFEXITED (status) && WEXITSTATUS (status) != 0)
367 error_at (loc, "mapper exit status %d",
368 WEXITSTATUS (status));
369 }
370 else
371 {
372 int fd_read = mapper->GetFDRead ();
373 close (fd_read);
374 }
375
376 #ifdef SIGPIPE
377 // Restore sigpipe
378 if (mapper->sigpipe != SIG_IGN)
379 signal (SIGPIPE, mapper->sigpipe);
380 #endif
381 }
382
383 delete mapper;
384 }