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