1998-10-16 09:00:18 +02:00
|
|
|
|
|
|
|
/*
|
1999-03-03 08:41:52 +01:00
|
|
|
* server.c Set up and handle communications with a server process.
|
1998-10-16 09:00:18 +02:00
|
|
|
*
|
2001-03-03 20:05:00 +01:00
|
|
|
* Server Handling copyright 1992-1999, 2001 The Free Software Foundation
|
1998-10-16 09:00:18 +02:00
|
|
|
*
|
|
|
|
* Server Handling is free software.
|
|
|
|
* You may redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License, as published by the Free Software
|
|
|
|
* Foundation; either version 2, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Server Handling is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Server Handling. See the file "COPYING". If not,
|
|
|
|
* write to: The Free Software Foundation, Inc.,
|
2005-08-15 02:50:43 +02:00
|
|
|
* 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
1998-10-16 09:00:18 +02:00
|
|
|
*
|
1999-03-03 08:41:52 +01:00
|
|
|
* As a special exception, The Free Software Foundation gives
|
|
|
|
* permission for additional uses of the text contained in his release
|
|
|
|
* of ServerHandler.
|
1998-10-16 09:00:18 +02:00
|
|
|
*
|
|
|
|
* The exception is that, if you link the ServerHandler library with other
|
|
|
|
* files to produce an executable, this does not by itself cause the
|
|
|
|
* resulting executable to be covered by the GNU General Public License.
|
|
|
|
* Your use of that executable is in no way restricted on account of
|
|
|
|
* linking the ServerHandler library code into it.
|
|
|
|
*
|
|
|
|
* This exception does not however invalidate any other reasons why
|
|
|
|
* the executable file might be covered by the GNU General Public License.
|
|
|
|
*
|
1999-03-03 08:41:52 +01:00
|
|
|
* This exception applies only to the code released by The Free
|
|
|
|
* Software Foundation under the name ServerHandler. If you copy code
|
|
|
|
* from other sources under the General Public License into a copy of
|
|
|
|
* ServerHandler, as the General Public License permits, the exception
|
|
|
|
* does not apply to the code that you add in this way. To avoid
|
|
|
|
* misleading anyone as to the status of such modified files, you must
|
|
|
|
* delete this exception notice from them.
|
1998-10-16 09:00:18 +02:00
|
|
|
*
|
|
|
|
* If you write modifications of your own for ServerHandler, it is your
|
|
|
|
* choice whether to permit this exception to apply to your modifications.
|
|
|
|
* If you do not wish that, delete this exception notice.
|
|
|
|
*/
|
|
|
|
|
2002-12-16 19:23:00 +01:00
|
|
|
#include "fixlib.h"
|
1998-10-16 09:00:18 +02:00
|
|
|
#include "server.h"
|
|
|
|
|
2001-03-03 20:05:00 +01:00
|
|
|
STATIC volatile enum t_bool read_pipe_timeout;
|
2000-06-02 19:25:47 +02:00
|
|
|
STATIC pid_t server_master_pid = NOPROCESS;
|
1998-10-16 09:00:18 +02:00
|
|
|
|
2000-12-13 21:07:46 +01:00
|
|
|
tSCC* def_args[] =
|
1999-03-03 08:41:52 +01:00
|
|
|
{ (char *) NULL, (char *) NULL };
|
|
|
|
STATIC t_pf_pair server_pair =
|
|
|
|
{ (FILE *) NULL, (FILE *) NULL };
|
|
|
|
STATIC pid_t server_id = NULLPROCESS;
|
1998-10-16 09:00:18 +02:00
|
|
|
/*
|
|
|
|
* Arbitrary text that should not be found in the shell output.
|
|
|
|
* It must be a single line and appear verbatim at the start of
|
|
|
|
* the terminating output line.
|
|
|
|
*/
|
1999-03-03 08:41:52 +01:00
|
|
|
tSCC z_done[] = "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd";
|
2000-12-13 21:07:46 +01:00
|
|
|
tSCC* p_cur_dir = (char *) NULL;
|
1998-10-16 09:00:18 +02:00
|
|
|
|
|
|
|
/*
|
1999-03-03 08:41:52 +01:00
|
|
|
* load_data
|
1998-10-16 09:00:18 +02:00
|
|
|
*
|
|
|
|
* Read data from a file pointer (a pipe to a process in this context)
|
|
|
|
* until we either get EOF or we get a marker line back.
|
|
|
|
* The read data are stored in a malloc-ed string that is truncated
|
|
|
|
* to size at the end. Input is assumed to be an ASCII string.
|
|
|
|
*/
|
1999-03-03 08:41:52 +01:00
|
|
|
static char *
|
2003-08-02 01:07:04 +02:00
|
|
|
load_data (FILE* fp)
|
1998-10-16 09:00:18 +02:00
|
|
|
{
|
1999-03-03 08:41:52 +01:00
|
|
|
char *pz_text;
|
|
|
|
size_t text_size;
|
|
|
|
char *pz_scan;
|
|
|
|
char z_line[1024];
|
2000-02-10 17:29:21 +01:00
|
|
|
t_bool got_done = BOOL_FALSE;
|
1998-10-16 09:00:18 +02:00
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
text_size = sizeof (z_line) * 2;
|
2005-05-15 20:28:36 +02:00
|
|
|
pz_scan = pz_text = XNEWVEC (char, text_size);
|
1998-10-16 09:00:18 +02:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
1999-03-03 08:41:52 +01:00
|
|
|
size_t used_ct;
|
1998-10-16 09:00:18 +02:00
|
|
|
|
|
|
|
alarm (10);
|
1999-03-03 08:41:52 +01:00
|
|
|
read_pipe_timeout = BOOL_FALSE;
|
|
|
|
if (fgets (z_line, sizeof (z_line), fp) == (char *) NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (strncmp (z_line, z_done, sizeof (z_done) - 1) == 0)
|
2000-02-10 17:29:21 +01:00
|
|
|
{
|
|
|
|
got_done = BOOL_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
1999-03-03 08:41:52 +01:00
|
|
|
|
|
|
|
strcpy (pz_scan, z_line);
|
|
|
|
pz_scan += strlen (z_line);
|
|
|
|
used_ct = (size_t) (pz_scan - pz_text);
|
|
|
|
|
|
|
|
if (text_size - used_ct < sizeof (z_line))
|
|
|
|
{
|
|
|
|
size_t off = (size_t) (pz_scan - pz_text);
|
|
|
|
|
|
|
|
text_size += 4096;
|
2005-05-15 20:28:36 +02:00
|
|
|
pz_text = XRESIZEVEC (char, pz_text, text_size);
|
1999-03-03 08:41:52 +01:00
|
|
|
pz_scan = pz_text + off;
|
|
|
|
}
|
1998-10-16 09:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
alarm (0);
|
2000-02-10 17:29:21 +01:00
|
|
|
if (read_pipe_timeout || ! got_done)
|
1998-10-16 09:00:18 +02:00
|
|
|
{
|
1999-03-03 08:41:52 +01:00
|
|
|
free ((void *) pz_text);
|
1998-10-16 09:00:18 +02:00
|
|
|
return (char *) NULL;
|
|
|
|
}
|
|
|
|
|
system.h (CTYPE_CONV, [...]): New macros.
* system.h (CTYPE_CONV, TOUPPER, TOLOWER): New macros. Use
CTYPE_CONV in all ctype macros.
* cccp.c (initialize_char_syntax): Use uppercase ctype macro
from system.h.
* cexp.y (initialize_random_junk): Likewise.
* c4x.c (c4x_interrupt_function_p, c4x_handle_pragma): Likewise.
* i370.c (handle_pragma): Likewise.
* i370.h (ASM_OUTPUT_LABELREF, ASM_OUTPUT_ASCII): Likewise.
* v850.c (override_options): Likewise.
* doprint.c (_doprnt): Likewise.
* fixinc/fixincl.c (main, quoted_file_exists, extract_quoted_files):
Likewise.
* fixinc/server.c (load_data): Likewise.
* fold-const.c (real_hex_to_f): Likewise.
* genattr.c (write_upcase, gen_attr): Likewise.
* genattrtab.c (convert_const_symbol_ref, evaluate_eq_attr,
write_upcase): Likewise.
* genemit.c (print_code): Likewise.
* genopinit.c (gen_insn): Likewise.
* genpeep.c (print_code): Likewise.
* genrecog.c (print_code): Likewise.
* optabs.c (init_libfuncs): Likewise.
ch:
* lex.c (maybe_downcase, getlc, handle_generic_pragma,
check_newline): Likewise.
f:
* bad.c (ffebad_finish): Likewise.
* fini.c (main): Likewise.
* intrin.c (ffeintrin_init_0): Likewise.
* lex.c (ffelex_hash_): Likewise.
* src.c (ffesrc_init_1): Likewise.
From-SVN: r29463
1999-09-17 00:20:44 +02:00
|
|
|
while ((pz_scan > pz_text) && ISSPACE (pz_scan[-1]))
|
1999-03-03 08:41:52 +01:00
|
|
|
pz_scan--;
|
|
|
|
*pz_scan = NUL;
|
2005-05-15 20:28:36 +02:00
|
|
|
return XRESIZEVEC (char, pz_text, strlen (pz_text) + 1);
|
1998-10-16 09:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1999-03-03 08:41:52 +01:00
|
|
|
* close_server
|
|
|
|
*
|
|
|
|
* Make certain the server process is dead, close the
|
|
|
|
* pipes to it and from it, finally NULL out the file pointers
|
1998-10-16 09:00:18 +02:00
|
|
|
*/
|
1999-04-26 12:38:38 +02:00
|
|
|
void
|
2003-08-02 01:07:04 +02:00
|
|
|
close_server (void)
|
1998-10-16 09:00:18 +02:00
|
|
|
{
|
2000-06-02 19:25:47 +02:00
|
|
|
if ( (server_id != NULLPROCESS)
|
|
|
|
&& (server_master_pid == getpid ()))
|
1999-04-26 12:38:38 +02:00
|
|
|
{
|
|
|
|
kill ((pid_t) server_id, SIGKILL);
|
|
|
|
server_id = NULLPROCESS;
|
2000-06-02 19:25:47 +02:00
|
|
|
server_master_pid = NOPROCESS;
|
1999-04-26 12:38:38 +02:00
|
|
|
fclose (server_pair.pf_read);
|
|
|
|
fclose (server_pair.pf_write);
|
|
|
|
server_pair.pf_read = server_pair.pf_write = (FILE *) NULL;
|
|
|
|
}
|
1998-10-16 09:00:18 +02:00
|
|
|
}
|
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
/*
|
|
|
|
* sig_handler really only handles the timeout and pipe signals.
|
|
|
|
* This ensures that we do not wait forever on a request
|
|
|
|
* to our server, and also that if the server dies, we do not
|
|
|
|
* die from a sigpipe problem.
|
|
|
|
*/
|
|
|
|
static void
|
2003-08-02 01:07:04 +02:00
|
|
|
sig_handler (int signo ATTRIBUTE_UNUSED)
|
1998-10-16 09:00:18 +02:00
|
|
|
{
|
1999-05-07 10:42:27 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
/* FIXME: this is illegal to do in a signal handler. */
|
|
|
|
fprintf (stderr,
|
|
|
|
"fixincl ERROR: sig_handler: killed pid %ld due to %s\n",
|
|
|
|
(long) server_id, signo == SIGPIPE ? "SIGPIPE" : "SIGALRM");
|
|
|
|
#endif
|
1999-03-03 08:41:52 +01:00
|
|
|
close_server ();
|
|
|
|
read_pipe_timeout = BOOL_TRUE;
|
1998-10-16 09:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
/*
|
|
|
|
* server_setup Establish the signal handler for PIPE and ALARM.
|
|
|
|
* Also establishes the current directory to give to the
|
|
|
|
* server process at the start of every server command.
|
|
|
|
*/
|
|
|
|
static void
|
2003-08-02 01:07:04 +02:00
|
|
|
server_setup (void)
|
1998-10-16 09:00:18 +02:00
|
|
|
{
|
1999-03-03 08:41:52 +01:00
|
|
|
static int atexit_done = 0;
|
2003-07-14 18:56:03 +02:00
|
|
|
char buff [MAXPATHLEN + 1];
|
1999-03-03 08:41:52 +01:00
|
|
|
|
|
|
|
if (atexit_done++ == 0)
|
1999-08-07 09:41:17 +02:00
|
|
|
atexit (close_server);
|
2000-05-17 16:56:13 +02:00
|
|
|
else
|
|
|
|
fputs ("NOTE: server restarted\n", stderr);
|
1998-10-16 09:00:18 +02:00
|
|
|
|
2000-06-02 19:25:47 +02:00
|
|
|
server_master_pid = getpid ();
|
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
signal (SIGPIPE, sig_handler);
|
|
|
|
signal (SIGALRM, sig_handler);
|
1998-10-16 09:00:18 +02:00
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
fputs ("trap : 1\n", server_pair.pf_write);
|
|
|
|
fflush (server_pair.pf_write);
|
2003-07-14 18:56:03 +02:00
|
|
|
getcwd (buff, MAXPATHLEN + 1);
|
|
|
|
p_cur_dir = xstrdup (buff);
|
1998-10-16 09:00:18 +02:00
|
|
|
}
|
|
|
|
|
2000-04-03 09:43:51 +02:00
|
|
|
/*
|
|
|
|
* find_shell
|
|
|
|
*
|
|
|
|
* Locate a shell suitable for use. For various reasons
|
|
|
|
* (like the use of "trap" in server_setup(), it must be a
|
|
|
|
* Bourne-like shell.
|
|
|
|
*
|
|
|
|
* Most of the time, /bin/sh is preferred, but sometimes
|
|
|
|
* it's quite broken (like on Ultrix). autoconf lets you
|
|
|
|
* override with $CONFIG_SHELL, so we do the same.
|
|
|
|
*/
|
|
|
|
|
2000-12-13 21:07:46 +01:00
|
|
|
static const char *
|
2003-08-02 01:07:04 +02:00
|
|
|
find_shell (void)
|
2000-04-03 09:43:51 +02:00
|
|
|
{
|
|
|
|
char * shell = getenv ("CONFIG_SHELL");
|
|
|
|
if (shell)
|
|
|
|
return shell;
|
|
|
|
|
|
|
|
return "/bin/sh";
|
|
|
|
}
|
|
|
|
|
1998-10-16 09:00:18 +02:00
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
/*
|
|
|
|
* run_shell
|
|
|
|
*
|
|
|
|
* Run a shell command on the server. The command string
|
|
|
|
* passed in is wrapped inside the sequence:
|
|
|
|
*
|
|
|
|
* cd <original directory>
|
|
|
|
* <command string>
|
|
|
|
* echo
|
|
|
|
* echo <end-of-command-marker>
|
|
|
|
*
|
|
|
|
* This ensures that all commands start at a known place in
|
|
|
|
* the directory structure, that any incomplete output lines
|
|
|
|
* are completed and that our special marker sequence appears on
|
|
|
|
* a line by itself. We have chosen a marker that is
|
|
|
|
* excessively unlikely to be reproduced in normal output:
|
|
|
|
*
|
|
|
|
* "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd"
|
|
|
|
*/
|
1998-10-16 09:00:18 +02:00
|
|
|
char *
|
2003-08-02 01:07:04 +02:00
|
|
|
run_shell (const char* pz_cmd)
|
1998-10-16 09:00:18 +02:00
|
|
|
{
|
2000-05-17 16:56:13 +02:00
|
|
|
tSCC zNoServer[] = "Server not running, cannot run:\n%s\n\n";
|
2000-02-10 17:29:21 +01:00
|
|
|
t_bool retry = BOOL_TRUE;
|
|
|
|
|
|
|
|
do_retry:
|
1999-03-03 08:41:52 +01:00
|
|
|
/* IF the shell server process is not running yet,
|
|
|
|
THEN try to start it. */
|
|
|
|
if (server_id == NULLPROCESS)
|
|
|
|
{
|
2000-04-03 09:43:51 +02:00
|
|
|
def_args[0] = find_shell ();
|
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
server_id = proc2_fopen (&server_pair, def_args);
|
|
|
|
if (server_id > 0)
|
|
|
|
server_setup ();
|
|
|
|
}
|
1998-10-16 09:00:18 +02:00
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
/* IF it is still not running, THEN return the nil string. */
|
|
|
|
if (server_id <= 0)
|
1998-10-16 09:00:18 +02:00
|
|
|
{
|
2000-05-17 16:56:13 +02:00
|
|
|
fprintf (stderr, zNoServer, pz_cmd);
|
2005-05-15 20:28:36 +02:00
|
|
|
return XCNEW (char);
|
1998-10-16 09:00:18 +02:00
|
|
|
}
|
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
/* Make sure the process will pay attention to us, send the
|
|
|
|
supplied command, and then have it output a special marker that
|
|
|
|
we can find. */
|
2009-03-27 22:56:01 +01:00
|
|
|
fprintf (server_pair.pf_write, "cd \"%s\"\n%s\n\necho\necho %s\n",
|
1999-03-03 08:41:52 +01:00
|
|
|
p_cur_dir, pz_cmd, z_done);
|
|
|
|
fflush (server_pair.pf_write);
|
1998-10-16 09:00:18 +02:00
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
/* IF the server died and we received a SIGPIPE,
|
|
|
|
THEN return an empty string. */
|
|
|
|
if (server_id == NULLPROCESS)
|
|
|
|
{
|
2000-05-17 16:56:13 +02:00
|
|
|
fprintf (stderr, zNoServer, pz_cmd);
|
2005-05-15 20:28:36 +02:00
|
|
|
return XCNEW (char);
|
1999-03-03 08:41:52 +01:00
|
|
|
}
|
1998-10-16 09:00:18 +02:00
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
/* Now try to read back all the data. If we fail due to either a
|
|
|
|
sigpipe or sigalrm (timeout), we will return the nil string. */
|
1998-10-16 09:00:18 +02:00
|
|
|
{
|
1999-03-03 08:41:52 +01:00
|
|
|
char *pz = load_data (server_pair.pf_read);
|
|
|
|
|
1998-10-16 09:00:18 +02:00
|
|
|
if (pz == (char *) NULL)
|
|
|
|
{
|
2000-02-10 17:29:21 +01:00
|
|
|
close_server ();
|
|
|
|
|
|
|
|
if (retry)
|
|
|
|
{
|
|
|
|
retry = BOOL_FALSE;
|
|
|
|
goto do_retry;
|
|
|
|
}
|
|
|
|
|
1999-03-03 08:41:52 +01:00
|
|
|
fprintf (stderr, "CLOSING SHELL SERVER - command failure:\n\t%s\n",
|
|
|
|
pz_cmd);
|
2005-05-15 20:28:36 +02:00
|
|
|
pz = XCNEW (char);
|
1998-10-16 09:00:18 +02:00
|
|
|
}
|
2000-05-17 16:56:13 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf( stderr, "run_shell command success: %s\n", pz );
|
|
|
|
#endif
|
1998-10-16 09:00:18 +02:00
|
|
|
return pz;
|
|
|
|
}
|
|
|
|
}
|