Branch data Line data Source code
1 : : /* C++ modules. Experimental!
2 : : Copyright (C) 2017-2025 Free Software Foundation, Inc.
3 : : Written by Nathan Sidwell <nathan@acm.org> 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 : 9 : module_client::module_client (pex_obj *p, int fd_from, int fd_to)
46 : 9 : : Client (fd_from, fd_to), pex (p)
47 : : {
48 : 9 : }
49 : :
50 : : static module_client *
51 : 12 : 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 : 12 : char **argv = new char *[name.size () / 2 + 2];
58 : 12 : unsigned arg_no = 0;
59 : 12 : char *str = new char[name.size ()];
60 : 12 : memcpy (str, name.c_str () + 1, name.size ());
61 : :
62 : 12 : for (auto ptr = str; ; ++ptr)
63 : : {
64 : 24 : while (*ptr == ' ')
65 : 0 : ptr++;
66 : 24 : if (!*ptr)
67 : : break;
68 : :
69 : 24 : if (!arg_no)
70 : : {
71 : : /* @name means look in the compiler's install dir. */
72 : 12 : if (ptr[0] == '@')
73 : 9 : ptr++;
74 : : else
75 : : full_program_name = nullptr;
76 : : }
77 : :
78 : 24 : argv[arg_no++] = ptr;
79 : 753 : while (*ptr && *ptr != ' ')
80 : 729 : ptr++;
81 : 24 : if (!*ptr)
82 : : break;
83 : 12 : *ptr = 0;
84 : : }
85 : 12 : argv[arg_no] = nullptr;
86 : :
87 : 12 : auto *pex = pex_init (PEX_USE_PIPES, progname, NULL);
88 : 12 : FILE *to = pex_input_pipe (pex, false);
89 : 12 : name = argv[0];
90 : 12 : if (!to)
91 : 0 : *errmsg = "connecting input";
92 : : else
93 : : {
94 : 12 : int flags = PEX_SEARCH;
95 : :
96 : 12 : if (full_program_name)
97 : : {
98 : : /* Prepend the invoking path, if the mapper is a simple
99 : : file name. */
100 : 9 : size_t dir_len = progname - full_program_name;
101 : 9 : std::string argv0;
102 : 9 : argv0.reserve (dir_len + name.size ());
103 : 9 : argv0.append (full_program_name, dir_len).append (name);
104 : 9 : name = std::move (argv0);
105 : 9 : argv[0] = const_cast <char *> (name.c_str ());
106 : 9 : flags = 0;
107 : 9 : }
108 : 12 : int err;
109 : 12 : *errmsg = pex_run (pex, flags, argv[0], argv, NULL, NULL, &err);
110 : : }
111 : 12 : delete[] str;
112 : 12 : delete[] argv;
113 : :
114 : 12 : int fd_from = -1, fd_to = -1;
115 : 12 : if (!*errmsg)
116 : : {
117 : 9 : FILE *from = pex_read_output (pex, false);
118 : 9 : if (from && (fd_to = dup (fileno (to))) >= 0)
119 : 9 : fd_from = fileno (from);
120 : : else
121 : 0 : *errmsg = "connecting output";
122 : 9 : fclose (to);
123 : : }
124 : :
125 : 12 : if (*errmsg)
126 : : {
127 : 3 : pex_free (pex);
128 : 3 : return nullptr;
129 : : }
130 : :
131 : 9 : return new module_client (pex, fd_from, fd_to);
132 : : }
133 : :
134 : : module_client *
135 : 4056 : 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 : 4056 : module_client *c = nullptr;
141 : 4056 : std::string ident;
142 : 4056 : std::string name;
143 : 4056 : char const *errmsg = nullptr;
144 : 4056 : unsigned line = 0;
145 : :
146 : 4056 : if (o && o[0])
147 : : {
148 : : /* Maybe a local or ipv6 address. */
149 : 42 : name = o;
150 : 42 : auto last = name.find_last_of ('?');
151 : 42 : if (last != name.npos)
152 : : {
153 : 3 : ident = name.substr (last + 1);
154 : 3 : name.erase (last);
155 : : }
156 : :
157 : 42 : if (name.size ())
158 : : {
159 : 42 : switch (name[0])
160 : : {
161 : 0 : case '<':
162 : : // <from>to or <>fromto, or <>
163 : 0 : {
164 : 0 : size_t pos = name.find ('>', 1);
165 : 0 : if (pos == std::string::npos)
166 : 0 : pos = name.size ();
167 : 0 : std::string from (name, 1, pos - 1);
168 : 0 : std::string to;
169 : 0 : if (pos != name.size ())
170 : 0 : to.append (name, pos + 1, std::string::npos);
171 : :
172 : 0 : int fd_from = -1, fd_to = -1;
173 : 0 : if (from.empty () && to.empty ())
174 : : {
175 : 0 : fd_from = fileno (stdin);
176 : 0 : fd_to = fileno (stdout);
177 : : }
178 : : else
179 : : {
180 : 0 : char *ptr;
181 : 0 : if (!from.empty ())
182 : : {
183 : : /* Sadly str::stoul is not portable. */
184 : 0 : const char *cstr = from.c_str ();
185 : 0 : fd_from = strtoul (cstr, &ptr, 10);
186 : 0 : if (*ptr)
187 : : {
188 : : /* Not a number -- a named pipe. */
189 : 0 : int dir = to.empty ()
190 : 0 : ? O_RDWR | O_CLOEXEC : O_RDONLY | O_CLOEXEC;
191 : 0 : fd_from = open (cstr, dir);
192 : : }
193 : 0 : if (to.empty ())
194 : 0 : fd_to = fd_from;
195 : : }
196 : :
197 : 0 : if (!from.empty () && fd_from < 0)
198 : : ;
199 : 0 : else if (to.empty ())
200 : : ;
201 : : else
202 : : {
203 : 0 : const char *cstr = to.c_str ();
204 : 0 : fd_to = strtoul (cstr, &ptr, 10);
205 : 0 : if (*ptr)
206 : : {
207 : : /* Not a number, a named pipe. */
208 : 0 : int dir = from.empty ()
209 : 0 : ? O_RDWR | O_CLOEXEC : O_WRONLY | O_CLOEXEC;
210 : 0 : fd_to = open (cstr, dir);
211 : 0 : if (fd_to < 0)
212 : 0 : close (fd_from);
213 : : }
214 : 0 : if (from.empty ())
215 : 0 : fd_from = fd_to;
216 : : }
217 : : }
218 : :
219 : 0 : if (fd_from < 0 || fd_to < 0)
220 : 0 : errmsg = "opening";
221 : : else
222 : 0 : c = new module_client (fd_from, fd_to);
223 : 0 : }
224 : 0 : break;
225 : :
226 : 0 : case '=':
227 : : // =localsocket
228 : 0 : {
229 : 0 : int fd = -1;
230 : : #if CODY_NETWORKING
231 : 0 : fd = Cody::OpenLocal (&errmsg, name.c_str () + 1);
232 : : #else
233 : : errmsg = "disabled";
234 : : #endif
235 : 0 : if (fd >= 0)
236 : 0 : c = new module_client (fd, fd);
237 : : }
238 : : break;
239 : :
240 : 12 : case '|':
241 : : // |program and args
242 : 12 : c = spawn_mapper_program (&errmsg, name, full_program_name);
243 : 12 : break;
244 : :
245 : 30 : default:
246 : : // file or hostname:port
247 : 30 : {
248 : 30 : auto colon = name.find_last_of (':');
249 : 30 : if (colon != name.npos)
250 : : {
251 : 6 : char const *cptr = name.c_str () + colon;
252 : 6 : char *endp;
253 : 6 : unsigned port = strtoul (cptr + 1, &endp, 10);
254 : :
255 : 6 : if (port && endp != cptr + 1 && !*endp)
256 : : {
257 : 6 : name[colon] = 0;
258 : 6 : int fd = -1;
259 : : #if CODY_NETWORKING
260 : 6 : fd = Cody::OpenInet6 (&errmsg, name.c_str (), port);
261 : : #else
262 : : errmsg = "disabled";
263 : : #endif
264 : 6 : name[colon] = ':';
265 : :
266 : 6 : if (fd >= 0)
267 : 0 : c = new module_client (fd, fd);
268 : : }
269 : : }
270 : :
271 : : }
272 : : break;
273 : : }
274 : : }
275 : : }
276 : :
277 : 18 : if (!c)
278 : : {
279 : : // Make a default in-process client
280 : 4047 : bool file = !errmsg && !name.empty ();
281 : 4047 : auto r = new module_resolver (!file, true);
282 : :
283 : 4047 : if (file)
284 : : {
285 : 24 : int fd = open (name.c_str (), O_RDONLY | O_CLOEXEC);
286 : 24 : if (fd < 0)
287 : 0 : errmsg = "opening";
288 : : else
289 : : {
290 : : /* Add the mapper file to the dependency tracking. */
291 : 24 : if (deps)
292 : 6 : deps_add_dep (deps, name.c_str ());
293 : 48 : if (int l = r->read_tuple_file (fd, ident, false))
294 : : {
295 : 3 : if (l > 0)
296 : : line = l;
297 : 3 : errmsg = "reading";
298 : : }
299 : :
300 : 24 : close (fd);
301 : : }
302 : : }
303 : : else
304 : 4023 : r->set_repo ("gcm.cache");
305 : :
306 : 4047 : auto *s = new Cody::Server (r);
307 : 4047 : c = new module_client (s);
308 : : }
309 : :
310 : : #ifdef SIGPIPE
311 : 4056 : if (!c->IsDirect ())
312 : : /* We need to ignore sig pipe for a while. */
313 : 9 : c->sigpipe = signal (SIGPIPE, SIG_IGN);
314 : : #endif
315 : :
316 : 4056 : if (errmsg)
317 : 21 : 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 : 4056 : c->Cork ();
322 : 4056 : c->Connect (std::string ("GCC"), ident);
323 : 4056 : c->ModuleRepo ();
324 : 4056 : auto packets = c->Uncork ();
325 : :
326 : 4056 : auto &connect = packets[0];
327 : 4056 : if (connect.GetCode () == Cody::Client::PC_CONNECT)
328 : 4056 : c->flags = Cody::Flags (connect.GetInteger ());
329 : 0 : else if (connect.GetCode () == Cody::Client::PC_ERROR)
330 : 0 : error_at (loc, "failed mapper handshake %s", connect.GetString ().c_str ());
331 : :
332 : 4056 : auto &repo = packets[1];
333 : 4056 : if (repo.GetCode () == Cody::Client::PC_PATHNAME)
334 : 4056 : set_repo (repo.GetString ().c_str ());
335 : :
336 : 8112 : return c;
337 : 4056 : }
338 : :
339 : : void
340 : 3908 : module_client::close_module_client (location_t loc, module_client *mapper)
341 : : {
342 : 3908 : if (mapper->IsDirect ())
343 : : {
344 : 3899 : auto *s = mapper->GetServer ();
345 : 3899 : auto *r = s->GetResolver ();
346 : 3899 : delete s;
347 : 3899 : delete r;
348 : : }
349 : : else
350 : : {
351 : 9 : if (mapper->pex)
352 : : {
353 : 9 : int fd_write = mapper->GetFDWrite ();
354 : 9 : if (fd_write >= 0)
355 : 9 : close (fd_write);
356 : :
357 : 9 : int status;
358 : 9 : pex_get_status (mapper->pex, 1, &status);
359 : :
360 : 9 : pex_free (mapper->pex);
361 : 9 : mapper->pex = NULL;
362 : :
363 : 9 : if (WIFSIGNALED (status))
364 : 0 : error_at (loc, "mapper died by signal %s",
365 : : strsignal (WTERMSIG (status)));
366 : 9 : else if (WIFEXITED (status) && WEXITSTATUS (status) != 0)
367 : 0 : error_at (loc, "mapper exit status %d",
368 : 0 : WEXITSTATUS (status));
369 : : }
370 : : else
371 : : {
372 : 0 : int fd_read = mapper->GetFDRead ();
373 : 0 : close (fd_read);
374 : : }
375 : :
376 : : #ifdef SIGPIPE
377 : : // Restore sigpipe
378 : 9 : if (mapper->sigpipe != SIG_IGN)
379 : 9 : signal (SIGPIPE, mapper->sigpipe);
380 : : #endif
381 : : }
382 : :
383 : 3917 : delete mapper;
384 : 3908 : }
|