aboutsummaryrefslogtreecommitdiff
path: root/gcc/mapper-client.cc
blob: 6e7093f91337d8af3c565b4c21240756b6ce6a39 (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
/* C++ modules.  Experimental!
   Copyright (C) 2017-2020 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"
#include "system.h"

#include "line-map.h"
#include "diagnostic-core.h"
#define MAPPER_FOR_GCC 1
#include "mapper.h"

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;
      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.  */
	  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;

  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 <>
	      {
		int fd_from = -1, fd_to = -1;
		char const *ptr = name.c_str ();
		char *eptr;

		fd_from = strtoul (++ptr, &eptr, 0);
		if (*eptr == '>')
		  {
		    ptr = eptr;
		    fd_to = strtoul (++ptr, &eptr, 0);
		    if (eptr != ptr && ptr == name.c_str () + 1)
		      fd_from = fd_to;
		  }

		if (*eptr)
		  errmsg = "parsing";
		else
		  {
		    if (name.size () == 2)
		      {
			fd_from = fileno (stdin);
			fd_to = fileno (stdout);
		      }
		    c = new module_client (fd_from, fd_to);
		  }
	      }
	      break;

	    case '=':
	      // =localsocket
	      {
		int fd = Cody::OpenLocal (&errmsg, name.c_str () + 1);
		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 = Cody::OpenInet6 (&errmsg, name.c_str (), port);
			name[colon] = ':';

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

  if (!c)
    {
      // Make a default in-process client
      auto r = new module_resolver ();
      auto *s = new Cody::Server (r);
      c = new module_client (s);
      if (!errmsg && !name.empty ())
	{
	int fd = open (name.c_str (), O_RDONLY | O_CLOEXEC);
	if (fd < 0)
	  errmsg = "opening";
	else
	  {
	    if (r->read_tuple_file (fd, ident, false))
	      errmsg = "reading";
	      
	    close (fd);
	  }
	}
      else
	{
	  r->set_repo ("gcm.cache");
	  r->set_default (true);
	}
    }

#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, "failed %s mapper %qs", errmsg, name.c_str ());

  // 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)
    ;
  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_MODULE_REPO)
  //  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;
}