xref: /netbsd-src/external/gpl3/gcc/dist/libcc1/connection.cc (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1 /* Connect implementation
2    Copyright (C) 2014-2022 Free Software Foundation, Inc.
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>
24 #include <string.h>
25 #include <errno.h>
26 #include "marshall.hh"
27 #include "connection.hh"
28 #include "rpc.hh"
29 
30 cc1_plugin::status
send(char c)31 cc1_plugin::connection::send (char c)
32 {
33   if (write (m_fd, &c, 1) != 1)
34     return FAIL;
35   return OK;
36 }
37 
38 cc1_plugin::status
send(const void * buf,int len)39 cc1_plugin::connection::send (const void *buf, int len)
40 {
41   if (write (m_fd, buf, len) != len)
42     return FAIL;
43   return OK;
44 }
45 
46 cc1_plugin::status
require(char c)47 cc1_plugin::connection::require (char c)
48 {
49   char result;
50 
51   if (read (m_fd, &result, 1) != 1
52       || result != c)
53     return FAIL;
54 
55   return OK;
56 }
57 
58 cc1_plugin::status
get(void * buf,int len)59 cc1_plugin::connection::get (void *buf, int len)
60 {
61   if (read (m_fd, buf, len) != len)
62     return FAIL;
63   return OK;
64 }
65 
66 cc1_plugin::status
do_wait(bool want_result)67 cc1_plugin::connection::do_wait (bool want_result)
68 {
69   while (true)
70     {
71       char result;
72       fd_set read_set;
73 
74       FD_ZERO (&read_set);
75       FD_SET (m_fd, &read_set);
76       if (m_aux_fd != -1)
77 	FD_SET (m_aux_fd, &read_set);
78 
79       int nfds = select (FD_SETSIZE, &read_set, NULL, NULL, NULL);
80       if (nfds == -1)
81 	{
82 	  if (errno == EINTR)
83 	    continue;
84 	  return FAIL;
85 	}
86 
87       // We have to check the stderr fd first, to avoid a possible
88       // blocking scenario when do_wait is called reentrantly.  In
89       // such a call, if we handle the primary fd first, then we may
90       // re-enter this function, read from gcc's stderr, causing the
91       // outer invocation of this function to block when trying to
92       // read.
93       if (m_aux_fd != -1 && FD_ISSET (m_aux_fd, &read_set))
94 	{
95 	  char buf[1024];
96 	  int n = read (m_aux_fd, buf, sizeof (buf) - 1);
97 	  if (n < 0)
98 	    return FAIL;
99 	  if (n > 0)
100 	    {
101 	      buf[n] = '\0';
102 	      print (buf);
103 	    }
104 	}
105 
106       if (FD_ISSET (m_fd, &read_set))
107 	{
108 	  int n = read (m_fd, &result, 1);
109 	  if (n == 0)
110 	    return want_result ? FAIL : OK;
111 	  if (n != 1)
112 	    return FAIL;
113 
114 	  switch (result)
115 	    {
116 	    case 'R':
117 	      // The reply is ready; the caller will unmarshall it.
118 	      return want_result ? OK : FAIL;
119 
120 	    case 'Q':
121 	      // While waiting for a reply, the other side made a method
122 	      // call.
123 	      {
124 		// Use an argument_wrapper here to simplify management
125 		// of the string's lifetime.
126 		argument_wrapper<char *> method_name;
127 
128 		if (!method_name.unmarshall (this))
129 		  return FAIL;
130 
131 		callback_ftype *callback
132 		  = m_callbacks.find_callback (method_name.get ());
133 		// The call to CALLBACK is where we may end up in a
134 		// reentrant call.
135 		if (callback == NULL || !callback (this))
136 		  return FAIL;
137 	      }
138 	      break;
139 
140 	    default:
141 	      return FAIL;
142 	    }
143 	}
144     }
145 }
146