]> gcc.gnu.org Git - gcc.git/blame - gcc/fixinc/server.c
Makefile.in (fixinc.sh): Remove gnu-regex.[ch] from dependencies.
[gcc.git] / gcc / fixinc / server.c
CommitLineData
0083c904
BK
1
2/*
1f414ac4 3 * server.c Set up and handle communications with a server process.
0083c904 4 *
f6242907 5 * Server Handling copyright 1992-1999, 2001 The Free Software Foundation
0083c904
BK
6 *
7 * Server Handling is free software.
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2, or (at your option) any later version.
11 *
12 * Server Handling is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Server Handling. See the file "COPYING". If not,
19 * write to: The Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *
1f414ac4
BK
23 * As a special exception, The Free Software Foundation gives
24 * permission for additional uses of the text contained in his release
25 * of ServerHandler.
0083c904
BK
26 *
27 * The exception is that, if you link the ServerHandler library with other
28 * files to produce an executable, this does not by itself cause the
29 * resulting executable to be covered by the GNU General Public License.
30 * Your use of that executable is in no way restricted on account of
31 * linking the ServerHandler library code into it.
32 *
33 * This exception does not however invalidate any other reasons why
34 * the executable file might be covered by the GNU General Public License.
35 *
1f414ac4
BK
36 * This exception applies only to the code released by The Free
37 * Software Foundation under the name ServerHandler. If you copy code
38 * from other sources under the General Public License into a copy of
39 * ServerHandler, as the General Public License permits, the exception
40 * does not apply to the code that you add in this way. To avoid
41 * misleading anyone as to the status of such modified files, you must
42 * delete this exception notice from them.
0083c904
BK
43 *
44 * If you write modifications of your own for ServerHandler, it is your
45 * choice whether to permit this exception to apply to your modifications.
46 * If you do not wish that, delete this exception notice.
47 */
0083c904 48
4977bab6 49#include "fixlib.h"
0083c904
BK
50#include "server.h"
51
f6242907 52STATIC volatile enum t_bool read_pipe_timeout;
56ce79f7 53STATIC pid_t server_master_pid = NOPROCESS;
0083c904 54
2629a114 55tSCC* def_args[] =
1f414ac4
BK
56{ (char *) NULL, (char *) NULL };
57STATIC t_pf_pair server_pair =
58{ (FILE *) NULL, (FILE *) NULL };
59STATIC pid_t server_id = NULLPROCESS;
0083c904
BK
60/*
61 * Arbitrary text that should not be found in the shell output.
62 * It must be a single line and appear verbatim at the start of
63 * the terminating output line.
64 */
1f414ac4 65tSCC z_done[] = "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd";
2629a114 66tSCC* p_cur_dir = (char *) NULL;
0083c904
BK
67
68/*
1f414ac4 69 * load_data
0083c904
BK
70 *
71 * Read data from a file pointer (a pipe to a process in this context)
72 * until we either get EOF or we get a marker line back.
73 * The read data are stored in a malloc-ed string that is truncated
74 * to size at the end. Input is assumed to be an ASCII string.
75 */
6864a6c6 76static char *load_data PARAMS ((FILE *));
1f414ac4
BK
77static char *
78load_data (fp)
0083c904
BK
79 FILE *fp;
80{
1f414ac4
BK
81 char *pz_text;
82 size_t text_size;
83 char *pz_scan;
84 char z_line[1024];
d1922b48 85 t_bool got_done = BOOL_FALSE;
0083c904 86
1f414ac4 87 text_size = sizeof (z_line) * 2;
f4ce9d90 88 pz_scan = pz_text = xmalloc (text_size);
0083c904
BK
89
90 for (;;)
91 {
1f414ac4 92 size_t used_ct;
0083c904
BK
93
94 alarm (10);
1f414ac4
BK
95 read_pipe_timeout = BOOL_FALSE;
96 if (fgets (z_line, sizeof (z_line), fp) == (char *) NULL)
97 break;
98
99 if (strncmp (z_line, z_done, sizeof (z_done) - 1) == 0)
d1922b48
AO
100 {
101 got_done = BOOL_TRUE;
102 break;
103 }
1f414ac4
BK
104
105 strcpy (pz_scan, z_line);
106 pz_scan += strlen (z_line);
107 used_ct = (size_t) (pz_scan - pz_text);
108
109 if (text_size - used_ct < sizeof (z_line))
110 {
111 size_t off = (size_t) (pz_scan - pz_text);
1f414ac4
BK
112
113 text_size += 4096;
f4ce9d90 114 pz_text = xrealloc ((void *) pz_text, text_size);
1f414ac4
BK
115 pz_scan = pz_text + off;
116 }
0083c904
BK
117 }
118
119 alarm (0);
d1922b48 120 if (read_pipe_timeout || ! got_done)
0083c904 121 {
1f414ac4 122 free ((void *) pz_text);
0083c904
BK
123 return (char *) NULL;
124 }
125
92a438d1 126 while ((pz_scan > pz_text) && ISSPACE (pz_scan[-1]))
1f414ac4
BK
127 pz_scan--;
128 *pz_scan = NUL;
f4ce9d90 129 return xrealloc ((void *) pz_text, strlen (pz_text) + 1);
0083c904
BK
130}
131
132
133/*
1f414ac4
BK
134 * close_server
135 *
136 * Make certain the server process is dead, close the
137 * pipes to it and from it, finally NULL out the file pointers
0083c904 138 */
d1c6a037 139void
1f414ac4 140close_server ()
0083c904 141{
56ce79f7
BK
142 if ( (server_id != NULLPROCESS)
143 && (server_master_pid == getpid ()))
d1c6a037
BK
144 {
145 kill ((pid_t) server_id, SIGKILL);
146 server_id = NULLPROCESS;
56ce79f7 147 server_master_pid = NOPROCESS;
d1c6a037
BK
148 fclose (server_pair.pf_read);
149 fclose (server_pair.pf_write);
150 server_pair.pf_read = server_pair.pf_write = (FILE *) NULL;
151 }
0083c904 152}
0083c904 153
1f414ac4
BK
154/*
155 * sig_handler really only handles the timeout and pipe signals.
156 * This ensures that we do not wait forever on a request
157 * to our server, and also that if the server dies, we do not
158 * die from a sigpipe problem.
159 */
6864a6c6 160static void sig_handler PARAMS ((int));
1f414ac4
BK
161static void
162sig_handler (signo)
6864a6c6 163 int signo ATTRIBUTE_UNUSED;
0083c904 164{
f6a65b92
RO
165#ifdef DEBUG
166 /* FIXME: this is illegal to do in a signal handler. */
167 fprintf (stderr,
168 "fixincl ERROR: sig_handler: killed pid %ld due to %s\n",
169 (long) server_id, signo == SIGPIPE ? "SIGPIPE" : "SIGALRM");
170#endif
1f414ac4
BK
171 close_server ();
172 read_pipe_timeout = BOOL_TRUE;
0083c904
BK
173}
174
175
1f414ac4
BK
176/*
177 * server_setup Establish the signal handler for PIPE and ALARM.
178 * Also establishes the current directory to give to the
179 * server process at the start of every server command.
180 */
6864a6c6 181static void server_setup PARAMS ((void));
1f414ac4
BK
182static void
183server_setup ()
0083c904 184{
1f414ac4
BK
185 static int atexit_done = 0;
186
187 if (atexit_done++ == 0)
4bc49b49 188 atexit (close_server);
e9099386
BK
189 else
190 fputs ("NOTE: server restarted\n", stderr);
0083c904 191
56ce79f7
BK
192 server_master_pid = getpid ();
193
1f414ac4
BK
194 signal (SIGPIPE, sig_handler);
195 signal (SIGALRM, sig_handler);
0083c904 196
1f414ac4
BK
197 fputs ("trap : 1\n", server_pair.pf_write);
198 fflush (server_pair.pf_write);
199 p_cur_dir = getcwd ((char *) NULL, MAXPATHLEN + 1);
0083c904
BK
200}
201
bebac701
FL
202/*
203 * find_shell
204 *
205 * Locate a shell suitable for use. For various reasons
206 * (like the use of "trap" in server_setup(), it must be a
207 * Bourne-like shell.
208 *
209 * Most of the time, /bin/sh is preferred, but sometimes
210 * it's quite broken (like on Ultrix). autoconf lets you
211 * override with $CONFIG_SHELL, so we do the same.
212 */
213
2629a114
BK
214static const char *find_shell PARAMS ((void));
215static const char *
bebac701
FL
216find_shell ()
217{
218 char * shell = getenv ("CONFIG_SHELL");
219 if (shell)
220 return shell;
221
222 return "/bin/sh";
223}
224
0083c904 225
1f414ac4
BK
226/*
227 * run_shell
228 *
229 * Run a shell command on the server. The command string
230 * passed in is wrapped inside the sequence:
231 *
232 * cd <original directory>
233 * <command string>
234 * echo
235 * echo <end-of-command-marker>
236 *
237 * This ensures that all commands start at a known place in
238 * the directory structure, that any incomplete output lines
239 * are completed and that our special marker sequence appears on
240 * a line by itself. We have chosen a marker that is
241 * excessively unlikely to be reproduced in normal output:
242 *
243 * "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd"
244 */
0083c904 245char *
1f414ac4
BK
246run_shell (pz_cmd)
247 const char *pz_cmd;
0083c904 248{
e9099386 249 tSCC zNoServer[] = "Server not running, cannot run:\n%s\n\n";
d1922b48
AO
250 t_bool retry = BOOL_TRUE;
251
252 do_retry:
1f414ac4
BK
253 /* IF the shell server process is not running yet,
254 THEN try to start it. */
255 if (server_id == NULLPROCESS)
256 {
bebac701
FL
257 def_args[0] = find_shell ();
258
1f414ac4
BK
259 server_id = proc2_fopen (&server_pair, def_args);
260 if (server_id > 0)
261 server_setup ();
262 }
0083c904 263
1f414ac4
BK
264 /* IF it is still not running, THEN return the nil string. */
265 if (server_id <= 0)
0083c904 266 {
e9099386 267 fprintf (stderr, zNoServer, pz_cmd);
f4ce9d90 268 return xcalloc (1, 1);
0083c904
BK
269 }
270
1f414ac4
BK
271 /* Make sure the process will pay attention to us, send the
272 supplied command, and then have it output a special marker that
273 we can find. */
f6a65b92 274 fprintf (server_pair.pf_write, "cd %s\n%s\n\necho\necho %s\n",
1f414ac4
BK
275 p_cur_dir, pz_cmd, z_done);
276 fflush (server_pair.pf_write);
0083c904 277
1f414ac4
BK
278 /* IF the server died and we received a SIGPIPE,
279 THEN return an empty string. */
280 if (server_id == NULLPROCESS)
281 {
e9099386 282 fprintf (stderr, zNoServer, pz_cmd);
f4ce9d90 283 return xcalloc (1, 1);
1f414ac4 284 }
0083c904 285
1f414ac4
BK
286 /* Now try to read back all the data. If we fail due to either a
287 sigpipe or sigalrm (timeout), we will return the nil string. */
0083c904 288 {
1f414ac4
BK
289 char *pz = load_data (server_pair.pf_read);
290
0083c904
BK
291 if (pz == (char *) NULL)
292 {
d1922b48
AO
293 close_server ();
294
295 if (retry)
296 {
297 retry = BOOL_FALSE;
298 goto do_retry;
299 }
300
1f414ac4
BK
301 fprintf (stderr, "CLOSING SHELL SERVER - command failure:\n\t%s\n",
302 pz_cmd);
f4ce9d90 303 pz = xcalloc (1, 1);
0083c904 304 }
e9099386
BK
305#ifdef DEBUG
306 fprintf( stderr, "run_shell command success: %s\n", pz );
307#endif
0083c904
BK
308 return pz;
309 }
310}
This page took 1.139282 seconds and 5 git commands to generate.