OSDN Git Service

Double the backslash so sed gets a chance to see it.
[pf3gnuchains/gcc-fork.git] / gcc / fixinc / server.c
1
2 /*
3  *  server.c  Set up and handle communications with a server process.
4  *
5  *  Server Handling copyright 1992-1999 The Free Software Foundation
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  *
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.
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  *
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.
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  */
48 #include "auto-host.h"
49
50 #include "gansidecl.h"
51 #include "system.h"
52 #include <signal.h>
53
54 #include "server.h"
55
56 /* If this particular system's header files define the macro `MAXPATHLEN',
57    we happily take advantage of it; otherwise we use a value which ought
58    to be large enough.  */
59 #ifndef MAXPATHLEN
60 # define MAXPATHLEN     4096
61 #endif
62
63 #ifndef STDIN_FILENO
64 # define STDIN_FILENO   0
65 #endif
66 #ifndef STDOUT_FILENO
67 # define STDOUT_FILENO  1
68 #endif
69
70 #ifdef DEBUG
71 #define STATIC
72 #else
73 #define STATIC static
74 #endif
75 #ifndef tSCC
76 #define tSCC static const char
77 #endif
78 #ifndef NUL
79 #define NUL '\0'
80 #endif
81
82 #if !defined(volatile) && !defined(HAVE_VOLATILE)
83 # define volatile
84 #endif
85
86 STATIC volatile bool read_pipe_timeout;
87
88 static t_pchar def_args[] =
89 { (char *) NULL, (char *) NULL };
90 STATIC t_pf_pair server_pair =
91 { (FILE *) NULL, (FILE *) NULL };
92 STATIC pid_t server_id = NULLPROCESS;
93 /*
94  *  Arbitrary text that should not be found in the shell output.
95  *  It must be a single line and appear verbatim at the start of
96  *  the terminating output line.
97  */
98 tSCC z_done[] = "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd";
99 STATIC t_pchar p_cur_dir = (char *) NULL;
100
101 /*
102  *  load_data
103  *
104  *  Read data from a file pointer (a pipe to a process in this context)
105  *  until we either get EOF or we get a marker line back.
106  *  The read data are stored in a malloc-ed string that is truncated
107  *  to size at the end.  Input is assumed to be an ASCII string.
108  */
109 static char *
110 load_data (fp)
111      FILE *fp;
112 {
113   char *pz_text;
114   size_t text_size;
115   char *pz_scan;
116   char z_line[1024];
117
118   text_size = sizeof (z_line) * 2;
119   pz_scan = pz_text = malloc (text_size);
120
121   if (pz_text == (char *) NULL)
122     return (char *) NULL;
123
124   for (;;)
125     {
126       size_t used_ct;
127
128       alarm (10);
129       read_pipe_timeout = BOOL_FALSE;
130       if (fgets (z_line, sizeof (z_line), fp) == (char *) NULL)
131         break;
132
133       if (strncmp (z_line, z_done, sizeof (z_done) - 1) == 0)
134         break;
135
136       strcpy (pz_scan, z_line);
137       pz_scan += strlen (z_line);
138       used_ct = (size_t) (pz_scan - pz_text);
139
140       if (text_size - used_ct < sizeof (z_line))
141         {
142           size_t off = (size_t) (pz_scan - pz_text);
143           void *p;
144           
145           text_size += 4096;
146           p = realloc ((void *) pz_text, text_size);
147           if (p == (void *) NULL)
148             {
149               fprintf (stderr, "Failed to get 0x%08lX bytes\n",
150                       (long) text_size);
151               free ((void *) pz_text);
152               return (char *) NULL;
153             }
154           pz_text = (char *) p;
155           pz_scan = pz_text + off;
156         }
157     }
158
159   alarm (0);
160   if (read_pipe_timeout)
161     {
162       free ((void *) pz_text);
163       return (char *) NULL;
164     }
165
166   while ((pz_scan > pz_text) && isspace (pz_scan[-1]))
167     pz_scan--;
168   *pz_scan = NUL;
169   return realloc ((void *) pz_text, strlen (pz_text) + 1);
170 }
171
172
173 /*
174  *  close_server
175  *
176  *  Make certain the server process is dead, close the 
177  *  pipes to it and from it, finally NULL out the file pointers
178  */
179 void
180 close_server ()
181 {
182   if (server_id != NULLPROCESS)
183     {
184       kill ((pid_t) server_id, SIGKILL);
185       server_id = NULLPROCESS;
186       fclose (server_pair.pf_read);
187       fclose (server_pair.pf_write);
188       server_pair.pf_read = server_pair.pf_write = (FILE *) NULL;
189     }
190 }
191
192 /*
193  *  sig_handler really only handles the timeout and pipe signals.
194  *  This ensures that we do not wait forever on a request
195  *  to our server, and also that if the server dies, we do not
196  *  die from a sigpipe problem.
197  */
198 static void
199 sig_handler (signo)
200      int signo;
201 {
202 #ifdef DEBUG
203   /* FIXME: this is illegal to do in a signal handler.  */
204   fprintf (stderr,
205           "fixincl ERROR:  sig_handler: killed pid %ld due to %s\n",
206           (long) server_id, signo == SIGPIPE ? "SIGPIPE" : "SIGALRM");
207 #endif
208   close_server ();
209   read_pipe_timeout = BOOL_TRUE;
210 }
211
212
213 /*
214  *  server_setup  Establish the signal handler for PIPE and ALARM.
215  *  Also establishes the current directory to give to the
216  *  server process at the start of every server command.
217  */
218 static void
219 server_setup ()
220 {
221   static int atexit_done = 0;
222   
223   if (atexit_done++ == 0)
224     atexit (&close_server);
225
226   signal (SIGPIPE, sig_handler);
227   signal (SIGALRM, sig_handler);
228
229   fputs ("trap : 1\n", server_pair.pf_write);
230   fflush (server_pair.pf_write);
231   p_cur_dir = getcwd ((char *) NULL, MAXPATHLEN + 1);
232 }
233
234
235 /*
236  *  run_shell
237  *
238  *  Run a shell command on the server.  The command string
239  *  passed in is wrapped inside the sequence:
240  *
241  *     cd <original directory>
242  *     <command string>
243  *     echo
244  *     echo <end-of-command-marker>
245  *
246  *  This ensures that all commands start at a known place in
247  *  the directory structure, that any incomplete output lines
248  *  are completed and that our special marker sequence appears on
249  *  a line by itself.  We have chosen a marker that is
250  *  excessively unlikely to be reproduced in normal output:
251  *
252  *     "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd"
253  */
254 char *
255 run_shell (pz_cmd)
256      const char *pz_cmd;
257 {
258   /*  IF the shell server process is not running yet,
259       THEN try to start it.  */
260   if (server_id == NULLPROCESS)
261     {
262       server_id = proc2_fopen (&server_pair, def_args);
263       if (server_id > 0)
264         server_setup ();
265     }
266
267   /*  IF it is still not running, THEN return the nil string.  */
268   if (server_id <= 0)
269     {
270       char *pz = (char *) malloc (1);
271       
272       if (pz != (char *) NULL)
273         *pz = '\0';
274       return pz;
275     }
276
277   /*  Make sure the process will pay attention to us, send the
278      supplied command, and then have it output a special marker that
279      we can find.  */
280   fprintf (server_pair.pf_write, "cd %s\n%s\n\necho\necho %s\n",
281            p_cur_dir, pz_cmd, z_done);
282   fflush (server_pair.pf_write);
283
284   /*  IF the server died and we received a SIGPIPE,
285       THEN return an empty string.  */
286   if (server_id == NULLPROCESS)
287     {
288       char *pz = (char *) malloc (1);
289       
290       if (pz != (char *) NULL)
291         *pz = '\0';
292       return pz;
293     }
294
295   /*  Now try to read back all the data.  If we fail due to either a
296      sigpipe or sigalrm (timeout), we will return the nil string.  */
297   {
298     char *pz = load_data (server_pair.pf_read);
299     
300     if (pz == (char *) NULL)
301       {
302         fprintf (stderr, "CLOSING SHELL SERVER - command failure:\n\t%s\n",
303                  pz_cmd);
304         close_server ();
305         pz = (char *) malloc (1);
306         if (pz != (char *) NULL)
307           *pz = '\0';
308       }
309     return pz;
310   }
311 }