]>
Commit | Line | Data |
---|---|---|
0083c904 BK |
1 | |
2 | /* | |
1f414ac4 | 3 | * server.c Set up and handle communications with a server process. |
0083c904 | 4 | * |
1f414ac4 | 5 | * Server Handling copyright 1992-1999 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 | */ | |
7b33bb99 | 48 | #include "auto-host.h" |
0083c904 | 49 | |
2583397b RL |
50 | #include "gansidecl.h" |
51 | #include "system.h" | |
0083c904 | 52 | #include <signal.h> |
0083c904 BK |
53 | |
54 | #include "server.h" | |
55 | ||
1f414ac4 BK |
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 | ||
0083c904 BK |
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 | ||
48ac9ce2 AO |
82 | #if !defined(volatile) && !defined(HAVE_VOLATILE) |
83 | # define volatile | |
84 | #endif | |
85 | ||
9ffe22f9 | 86 | STATIC volatile t_bool read_pipe_timeout; |
56ce79f7 | 87 | STATIC pid_t server_master_pid = NOPROCESS; |
0083c904 | 88 | |
f6a65b92 | 89 | static t_pchar def_args[] = |
1f414ac4 BK |
90 | { (char *) NULL, (char *) NULL }; |
91 | STATIC t_pf_pair server_pair = | |
92 | { (FILE *) NULL, (FILE *) NULL }; | |
93 | STATIC pid_t server_id = NULLPROCESS; | |
0083c904 BK |
94 | /* |
95 | * Arbitrary text that should not be found in the shell output. | |
96 | * It must be a single line and appear verbatim at the start of | |
97 | * the terminating output line. | |
98 | */ | |
1f414ac4 BK |
99 | tSCC z_done[] = "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd"; |
100 | STATIC t_pchar p_cur_dir = (char *) NULL; | |
0083c904 BK |
101 | |
102 | /* | |
1f414ac4 | 103 | * load_data |
0083c904 BK |
104 | * |
105 | * Read data from a file pointer (a pipe to a process in this context) | |
106 | * until we either get EOF or we get a marker line back. | |
107 | * The read data are stored in a malloc-ed string that is truncated | |
108 | * to size at the end. Input is assumed to be an ASCII string. | |
109 | */ | |
6864a6c6 | 110 | static char *load_data PARAMS ((FILE *)); |
1f414ac4 BK |
111 | static char * |
112 | load_data (fp) | |
0083c904 BK |
113 | FILE *fp; |
114 | { | |
1f414ac4 BK |
115 | char *pz_text; |
116 | size_t text_size; | |
117 | char *pz_scan; | |
118 | char z_line[1024]; | |
d1922b48 | 119 | t_bool got_done = BOOL_FALSE; |
0083c904 | 120 | |
1f414ac4 BK |
121 | text_size = sizeof (z_line) * 2; |
122 | pz_scan = pz_text = malloc (text_size); | |
0083c904 | 123 | |
1f414ac4 BK |
124 | if (pz_text == (char *) NULL) |
125 | return (char *) NULL; | |
0083c904 BK |
126 | |
127 | for (;;) | |
128 | { | |
1f414ac4 | 129 | size_t used_ct; |
0083c904 BK |
130 | |
131 | alarm (10); | |
1f414ac4 BK |
132 | read_pipe_timeout = BOOL_FALSE; |
133 | if (fgets (z_line, sizeof (z_line), fp) == (char *) NULL) | |
134 | break; | |
135 | ||
136 | if (strncmp (z_line, z_done, sizeof (z_done) - 1) == 0) | |
d1922b48 AO |
137 | { |
138 | got_done = BOOL_TRUE; | |
139 | break; | |
140 | } | |
1f414ac4 BK |
141 | |
142 | strcpy (pz_scan, z_line); | |
143 | pz_scan += strlen (z_line); | |
144 | used_ct = (size_t) (pz_scan - pz_text); | |
145 | ||
146 | if (text_size - used_ct < sizeof (z_line)) | |
147 | { | |
148 | size_t off = (size_t) (pz_scan - pz_text); | |
149 | void *p; | |
150 | ||
151 | text_size += 4096; | |
152 | p = realloc ((void *) pz_text, text_size); | |
153 | if (p == (void *) NULL) | |
154 | { | |
f6a65b92 RO |
155 | fprintf (stderr, "Failed to get 0x%08lX bytes\n", |
156 | (long) text_size); | |
1f414ac4 BK |
157 | free ((void *) pz_text); |
158 | return (char *) NULL; | |
159 | } | |
160 | pz_text = (char *) p; | |
161 | pz_scan = pz_text + off; | |
162 | } | |
0083c904 BK |
163 | } |
164 | ||
165 | alarm (0); | |
d1922b48 | 166 | if (read_pipe_timeout || ! got_done) |
0083c904 | 167 | { |
1f414ac4 | 168 | free ((void *) pz_text); |
0083c904 BK |
169 | return (char *) NULL; |
170 | } | |
171 | ||
92a438d1 | 172 | while ((pz_scan > pz_text) && ISSPACE (pz_scan[-1])) |
1f414ac4 BK |
173 | pz_scan--; |
174 | *pz_scan = NUL; | |
175 | return realloc ((void *) pz_text, strlen (pz_text) + 1); | |
0083c904 BK |
176 | } |
177 | ||
178 | ||
179 | /* | |
1f414ac4 BK |
180 | * close_server |
181 | * | |
182 | * Make certain the server process is dead, close the | |
183 | * pipes to it and from it, finally NULL out the file pointers | |
0083c904 | 184 | */ |
d1c6a037 | 185 | void |
1f414ac4 | 186 | close_server () |
0083c904 | 187 | { |
56ce79f7 BK |
188 | if ( (server_id != NULLPROCESS) |
189 | && (server_master_pid == getpid ())) | |
d1c6a037 BK |
190 | { |
191 | kill ((pid_t) server_id, SIGKILL); | |
192 | server_id = NULLPROCESS; | |
56ce79f7 | 193 | server_master_pid = NOPROCESS; |
d1c6a037 BK |
194 | fclose (server_pair.pf_read); |
195 | fclose (server_pair.pf_write); | |
196 | server_pair.pf_read = server_pair.pf_write = (FILE *) NULL; | |
197 | } | |
0083c904 | 198 | } |
0083c904 | 199 | |
1f414ac4 BK |
200 | /* |
201 | * sig_handler really only handles the timeout and pipe signals. | |
202 | * This ensures that we do not wait forever on a request | |
203 | * to our server, and also that if the server dies, we do not | |
204 | * die from a sigpipe problem. | |
205 | */ | |
6864a6c6 | 206 | static void sig_handler PARAMS ((int)); |
1f414ac4 BK |
207 | static void |
208 | sig_handler (signo) | |
6864a6c6 | 209 | int signo ATTRIBUTE_UNUSED; |
0083c904 | 210 | { |
f6a65b92 RO |
211 | #ifdef DEBUG |
212 | /* FIXME: this is illegal to do in a signal handler. */ | |
213 | fprintf (stderr, | |
214 | "fixincl ERROR: sig_handler: killed pid %ld due to %s\n", | |
215 | (long) server_id, signo == SIGPIPE ? "SIGPIPE" : "SIGALRM"); | |
216 | #endif | |
1f414ac4 BK |
217 | close_server (); |
218 | read_pipe_timeout = BOOL_TRUE; | |
0083c904 BK |
219 | } |
220 | ||
221 | ||
1f414ac4 BK |
222 | /* |
223 | * server_setup Establish the signal handler for PIPE and ALARM. | |
224 | * Also establishes the current directory to give to the | |
225 | * server process at the start of every server command. | |
226 | */ | |
6864a6c6 | 227 | static void server_setup PARAMS ((void)); |
1f414ac4 BK |
228 | static void |
229 | server_setup () | |
0083c904 | 230 | { |
1f414ac4 BK |
231 | static int atexit_done = 0; |
232 | ||
233 | if (atexit_done++ == 0) | |
4bc49b49 | 234 | atexit (close_server); |
e9099386 BK |
235 | else |
236 | fputs ("NOTE: server restarted\n", stderr); | |
0083c904 | 237 | |
56ce79f7 BK |
238 | server_master_pid = getpid (); |
239 | ||
1f414ac4 BK |
240 | signal (SIGPIPE, sig_handler); |
241 | signal (SIGALRM, sig_handler); | |
0083c904 | 242 | |
1f414ac4 BK |
243 | fputs ("trap : 1\n", server_pair.pf_write); |
244 | fflush (server_pair.pf_write); | |
245 | p_cur_dir = getcwd ((char *) NULL, MAXPATHLEN + 1); | |
0083c904 BK |
246 | } |
247 | ||
bebac701 FL |
248 | /* |
249 | * find_shell | |
250 | * | |
251 | * Locate a shell suitable for use. For various reasons | |
252 | * (like the use of "trap" in server_setup(), it must be a | |
253 | * Bourne-like shell. | |
254 | * | |
255 | * Most of the time, /bin/sh is preferred, but sometimes | |
256 | * it's quite broken (like on Ultrix). autoconf lets you | |
257 | * override with $CONFIG_SHELL, so we do the same. | |
258 | */ | |
259 | ||
6864a6c6 | 260 | static char *find_shell PARAMS ((void)); |
bebac701 FL |
261 | static char * |
262 | find_shell () | |
263 | { | |
264 | char * shell = getenv ("CONFIG_SHELL"); | |
265 | if (shell) | |
266 | return shell; | |
267 | ||
268 | return "/bin/sh"; | |
269 | } | |
270 | ||
0083c904 | 271 | |
1f414ac4 BK |
272 | /* |
273 | * run_shell | |
274 | * | |
275 | * Run a shell command on the server. The command string | |
276 | * passed in is wrapped inside the sequence: | |
277 | * | |
278 | * cd <original directory> | |
279 | * <command string> | |
280 | * echo | |
281 | * echo <end-of-command-marker> | |
282 | * | |
283 | * This ensures that all commands start at a known place in | |
284 | * the directory structure, that any incomplete output lines | |
285 | * are completed and that our special marker sequence appears on | |
286 | * a line by itself. We have chosen a marker that is | |
287 | * excessively unlikely to be reproduced in normal output: | |
288 | * | |
289 | * "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd" | |
290 | */ | |
0083c904 | 291 | char * |
1f414ac4 BK |
292 | run_shell (pz_cmd) |
293 | const char *pz_cmd; | |
0083c904 | 294 | { |
e9099386 | 295 | tSCC zNoServer[] = "Server not running, cannot run:\n%s\n\n"; |
d1922b48 AO |
296 | t_bool retry = BOOL_TRUE; |
297 | ||
298 | do_retry: | |
1f414ac4 BK |
299 | /* IF the shell server process is not running yet, |
300 | THEN try to start it. */ | |
301 | if (server_id == NULLPROCESS) | |
302 | { | |
bebac701 FL |
303 | def_args[0] = find_shell (); |
304 | ||
1f414ac4 BK |
305 | server_id = proc2_fopen (&server_pair, def_args); |
306 | if (server_id > 0) | |
307 | server_setup (); | |
308 | } | |
0083c904 | 309 | |
1f414ac4 BK |
310 | /* IF it is still not running, THEN return the nil string. */ |
311 | if (server_id <= 0) | |
0083c904 | 312 | { |
1f414ac4 | 313 | char *pz = (char *) malloc (1); |
e9099386 | 314 | fprintf (stderr, zNoServer, pz_cmd); |
1f414ac4 BK |
315 | if (pz != (char *) NULL) |
316 | *pz = '\0'; | |
317 | return pz; | |
0083c904 BK |
318 | } |
319 | ||
1f414ac4 BK |
320 | /* Make sure the process will pay attention to us, send the |
321 | supplied command, and then have it output a special marker that | |
322 | we can find. */ | |
f6a65b92 | 323 | fprintf (server_pair.pf_write, "cd %s\n%s\n\necho\necho %s\n", |
1f414ac4 BK |
324 | p_cur_dir, pz_cmd, z_done); |
325 | fflush (server_pair.pf_write); | |
0083c904 | 326 | |
1f414ac4 BK |
327 | /* IF the server died and we received a SIGPIPE, |
328 | THEN return an empty string. */ | |
329 | if (server_id == NULLPROCESS) | |
330 | { | |
331 | char *pz = (char *) malloc (1); | |
e9099386 | 332 | fprintf (stderr, zNoServer, pz_cmd); |
1f414ac4 BK |
333 | if (pz != (char *) NULL) |
334 | *pz = '\0'; | |
335 | return pz; | |
336 | } | |
0083c904 | 337 | |
1f414ac4 BK |
338 | /* Now try to read back all the data. If we fail due to either a |
339 | sigpipe or sigalrm (timeout), we will return the nil string. */ | |
0083c904 | 340 | { |
1f414ac4 BK |
341 | char *pz = load_data (server_pair.pf_read); |
342 | ||
0083c904 BK |
343 | if (pz == (char *) NULL) |
344 | { | |
d1922b48 AO |
345 | close_server (); |
346 | ||
347 | if (retry) | |
348 | { | |
349 | retry = BOOL_FALSE; | |
350 | goto do_retry; | |
351 | } | |
352 | ||
1f414ac4 BK |
353 | fprintf (stderr, "CLOSING SHELL SERVER - command failure:\n\t%s\n", |
354 | pz_cmd); | |
1f414ac4 BK |
355 | pz = (char *) malloc (1); |
356 | if (pz != (char *) NULL) | |
357 | *pz = '\0'; | |
0083c904 | 358 | } |
e9099386 BK |
359 | #ifdef DEBUG |
360 | fprintf( stderr, "run_shell command success: %s\n", pz ); | |
361 | #endif | |
0083c904 BK |
362 | return pz; |
363 | } | |
364 | } |