aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/mapper-client.cc
blob: fe9544b5ba43e02d47e2685fd54243abd0ed7161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* C++ modules.  Experimental!
   Copyright (C) 2017-2022 Free Software Foundation, Inc.
   Written by Nathan Sidwell <nathan@acm.org> while at FaceBook

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GCC is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#if defined (__unix__)
// Solaris11's socket header used bcopy, which we poison.  cody.hh
// will include it later under the above check
#include <sys/socket.h>
#endif
#define INCLUDE_STRING
#define INCLUDE_VECTOR
#define INCLUDE_MAP
#define INCLUDE_MEMORY
#include "system.h"

#include "line-map.h"
#include "diagnostic-core.h"
#include "mapper-client.h"
#include "intl.h"

#include "../../c++tools/resolver.h"

#if !HOST_HAS_O_CLOEXEC
#define O_CLOEXEC 0
#endif

module_client::module_client (pex_obj *p, int fd_from, int fd_to)
  : Client (fd_from, fd_to), pex (p)
{
}

static module_client *
spawn_mapper_program (char const **errmsg, std::string &name,
		      char const *full_program_name)
{
  /* Split writable at white-space.  No space-containing args for
     you!  */
  // At most every other char could be an argument
  char **argv = new char *[name.size () / 2 + 2];
  unsigned arg_no = 0;
  char *str = new char[name.size ()];
  memcpy (str, name.c_str () + 1, name.size ());

  for (auto ptr = str; ; ++ptr)
    {
      while (*ptr == ' ')
	ptr++;
      if (!*ptr)
	break;

      if (!arg_no)
	{
	  /* @name means look in the compiler's install dir.  */
	  if (ptr[0] == '@')
	    ptr++;
	  else
	    full_program_name = nullptr;
	}

      argv[arg_no++] = ptr;
      while (*ptr && *ptr != ' ')
	ptr++;
      if (!*ptr)
	break;
      *ptr = 0;
    }
  argv[arg_no] = nullptr;

  auto *pex = pex_init (PEX_USE_PIPES, progname, NULL);
  FILE *to = pex_input_pipe (pex, false);
  name = argv[0];
  if (!to)
    *errmsg = "connecting input";
  else
    {
      int flags = PEX_SEARCH;

      if (full_program_name)
	{
	  /* Prepend the invoking path, if the mapper is a simple
	     file name.  */
	  size_t dir_len = progname - full_program_name;
	  std::string argv0;
	  argv0.reserve (dir_len + name.size ());
	  argv0.append (full_program_name, dir_len).append (name);
	  name = std::move (argv0);
	  argv[0] = const_cast <char *> (name.c_str ());
	  flags = 0;
	}
      int err;
      *errmsg = pex_run (pex, flags, argv[0], argv, NULL, NULL, &err);
    }
  delete[] str;
  delete[] argv;

  int fd_from = -1, fd_to = -1;
  if (!*errmsg)
    {
      FILE *from = pex_read_output (pex, false);
      if (from && (fd_to = dup (fileno (to))) >= 0)
	fd_from = fileno (from);
      else
	*errmsg = "connecting output";
      fclose (to);
    }

  if (*errmsg)
    {
      pex_free (pex);
      return nullptr;
    }

  return new module_client (pex, fd_from, fd_to);
}

module_client *
module_client::open_module_client (location_t loc, const char *o,
				   void (*set_repo) (const char *),
				   char const *full_program_name)
{
  module_client *c = nullptr;
  std::string ident;
  std::string name;
  char const *errmsg = nullptr;
  unsigned line = 0;

  if (o && o[0])
    {
      /* Maybe a local or ipv6 address.  */
      name = o;
      auto last = name.find_last_of ('?');
      if (last != name.npos)
	{
	  ident = name.substr (last + 1);
	  name.erase (last);
	}

      if (name.size ())
	{
	  switch (name[0])
	    {
	    case '<':
	      // <from>to or <>fromto, or <>
	      {
		size_t pos = name.find ('>', 1);
		if (pos == std::string::npos)
		  pos = name.size ();
		std::string from (name, 1, pos - 1);
		std::string to;
		if (pos != name.size ())
		  to.append (name, pos + 1, std::string::npos);

		int fd_from = -1, fd_to = -1;
		if (from.empty () && to.empty ())
		  {
		    fd_from = fileno (stdin);
		    fd_to = fileno (stdout);
		  }
		else
		  {
		    char *ptr;
		    if (!from.empty ())
		      {
			/* Sadly str::stoul is not portable.  */
			const char *cstr = from.c_str ();
			fd_from = strtoul (cstr, &ptr, 10);
			if (*ptr)
			  {
			    /* Not a number -- a named pipe.  */
			    int dir = to.empty ()
			      ? O_RDWR | O_CLOEXEC : O_RDONLY | O_CLOEXEC;
			    fd_from = open (cstr, dir);
			  }
			if (to.empty ())
			  fd_to = fd_from;
		      }

		    if (!from.empty () && fd_from < 0)
		      ;
		    else if (to.empty ())
		      ;
		    else
		      {
			const char *cstr = to.c_str ();
			fd_to = strtoul (cstr, &ptr, 10);
			if (*ptr)
			  {
			    /* Not a number, a named pipe.  */
			    int dir = from.empty ()
			      ? O_RDWR | O_CLOEXEC : O_WRONLY | O_CLOEXEC;
			    fd_to = open (cstr, dir);
			    if (fd_to < 0)
			      close (fd_from);
			  }
			if (from.empty ())
			  fd_from = fd_to;
		      }
		  }

		if (fd_from < 0 || fd_to < 0)
		  errmsg = "opening";
		else
		  c = new module_client (fd_from, fd_to);
	      }
	      break;

	    case '=':
	      // =localsocket
	      {
		int fd = -1;
#if CODY_NETWORKING
		fd = Cody::OpenLocal (&errmsg, name.c_str () + 1);
#endif
		if (fd >= 0)
		  c = new module_client (fd, fd);
	      }
	      break;

	    case '|':
	      // |program and args
	      c = spawn_mapper_program (&errmsg, name, full_program_name);
	      break;

	    default:
	      // file or hostname:port
	      {
		auto colon = name.find_last_of (':');
		if (colon != name.npos)
		  {
		    char const *cptr = name.c_str () + colon;
		    char *endp;
		    unsigned port = strtoul (cptr + 1, &endp, 10);

		    if (port && endp != cptr + 1 && !*endp)
		      {
			name[colon] = 0;
			int fd = -1;
#if CODY_NETWORKING
			fd = Cody::OpenInet6 (&errmsg, name.c_str (), port);
#endif
			name[colon] = ':';

			if (fd >= 0)
			  c = new module_client (fd, fd);
		      }
		  }
		
	      }
	      break;
	    }
	}
    }

  if (!c)
    {
      // Make a default in-process client
      bool file = !errmsg && !name.empty ();
      auto r = new module_resolver (!file, true);

      if (file)
	{
	int fd = open (name.c_str (), O_RDONLY | O_CLOEXEC);
	if (fd < 0)
	  errmsg = "opening";
	else
	  {
	    if (int l = r->read_tuple_file (fd, ident, false))
	      {
		if (l > 0)
		  line = l;
		errmsg = "reading";
	      }
	      
	    close (fd);
	  }
	}
      else
	r->set_repo ("gcm.cache");

      auto *s = new Cody::Server (r);
      c = new module_client (s);
    }

#ifdef SIGPIPE
  if (!c->IsDirect ())
    /* We need to ignore sig pipe for a while.  */
    c->sigpipe = signal (SIGPIPE, SIG_IGN);
#endif

  if (errmsg)
    error_at (loc, line ? G_("failed %s mapper %qs line %u")
	      : G_("failed %s mapper %qs"), errmsg, name.c_str (), line);

  // now wave hello!
  c->Cork ();
  c->Connect (std::string ("GCC"), ident);
  c->ModuleRepo ();
  auto packets = c->Uncork ();

  auto &connect = packets[0];
  if (connect.GetCode () == Cody::Client::PC_CONNECT)
    c->flags = Cody::Flags (connect.GetInteger ());
  else if (connect.GetCode () == Cody::Client::PC_ERROR)
    error_at (loc, "failed mapper handshake %s", connect.GetString ().c_str ());

  auto &repo = packets[1];
  if (repo.GetCode () == Cody::Client::PC_PATHNAME)
    set_repo (repo.GetString ().c_str ());

  return c;
}

void
module_client::close_module_client (location_t loc, module_client *mapper)
{
  if (mapper->IsDirect ())
    {
      auto *s = mapper->GetServer ();
      auto *r = s->GetResolver ();
      delete s;
      delete r;
    }
  else
    {
      if (mapper->pex)
	{
	  int fd_write = mapper->GetFDWrite ();
	  if (fd_write >= 0)
	    close (fd_write);

	  int status;
	  pex_get_status (mapper->pex, 1, &status);

	  pex_free (mapper->pex);
	  mapper->pex = NULL;

	  if (WIFSIGNALED (status))
	    error_at (loc, "mapper died by signal %s",
		      strsignal (WTERMSIG (status)));
	  else if (WIFEXITED (status) && WEXITSTATUS (status) != 0)
	    error_at (loc, "mapper exit status %d",
		      WEXITSTATUS (status));
	}
      else
	{
	  int fd_read = mapper->GetFDRead ();
	  close (fd_read);
	}

#ifdef SIGPIPE
      // Restore sigpipe
      if (mapper->sigpipe != SIG_IGN)
	signal (SIGPIPE, mapper->sigpipe);
#endif
    }

  delete mapper;
}