aboutsummaryrefslogtreecommitdiff
path: root/gcc/fixinc/server.c
diff options
context:
space:
mode:
authorBruce Korb <korbb@gcc.gnu.org>1999-03-03 07:41:52 +0000
committerBruce Korb <korbb@gcc.gnu.org>1999-03-03 07:41:52 +0000
commit1f414ac48f4a946930e35c5003b5f020ad8168c4 (patch)
tree479ea57b8451b885c972eec0fba1e784cb5fca7c /gcc/fixinc/server.c
parent9a9f6326f2f7d333567dbe114edd245672eff566 (diff)
downloadgcc-1f414ac48f4a946930e35c5003b5f020ad8168c4.zip
gcc-1f414ac48f4a946930e35c5003b5f020ad8168c4.tar.gz
gcc-1f414ac48f4a946930e35c5003b5f020ad8168c4.tar.bz2
Merge from fixincl-branch
From-SVN: r25558
Diffstat (limited to 'gcc/fixinc/server.c')
-rw-r--r--gcc/fixinc/server.c524
1 files changed, 182 insertions, 342 deletions
diff --git a/gcc/fixinc/server.c b/gcc/fixinc/server.c
index 296e307..a80be74 100644
--- a/gcc/fixinc/server.c
+++ b/gcc/fixinc/server.c
@@ -1,8 +1,8 @@
/*
- * $Id: server.c,v 1.2 1998/12/16 21:19:16 law Exp $
+ * server.c Set up and handle communications with a server process.
*
- * Server Handling copyright 1992-1998 Bruce Korb
+ * Server Handling copyright 1992-1999 The Free Software Foundation
*
* Server Handling is free software.
* You may redistribute it and/or modify it under the terms of the
@@ -20,8 +20,9 @@
* 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
- * As a special exception, Bruce Korb gives permission for additional
- * uses of the text contained in his release of ServerHandler.
+ * As a special exception, The Free Software Foundation gives
+ * permission for additional uses of the text contained in his release
+ * of ServerHandler.
*
* The exception is that, if you link the ServerHandler library with other
* files to produce an executable, this does not by itself cause the
@@ -32,19 +33,24 @@
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
- * This exception applies only to the code released by Bruce Korb 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.
+ * 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.
*
* 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.
*/
+#include "config.h"
+#include <sys/types.h>
+#ifdef HAVE_FCNTL_H
#include <fcntl.h>
+#endif
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
@@ -54,6 +60,20 @@
#include "server.h"
+/* If this particular system's header files define the macro `MAXPATHLEN',
+ we happily take advantage of it; otherwise we use a value which ought
+ to be large enough. */
+#ifndef MAXPATHLEN
+# define MAXPATHLEN 4096
+#endif
+
+#ifndef STDIN_FILENO
+# define STDIN_FILENO 0
+#endif
+#ifndef STDOUT_FILENO
+# define STDOUT_FILENO 1
+#endif
+
#ifdef DEBUG
#define STATIC
#else
@@ -66,398 +86,218 @@
#define NUL '\0'
#endif
-STATIC bool readPipeTimeout;
+STATIC bool read_pipe_timeout;
-STATIC tpChar defArgs[] =
-{(char *) NULL, "-p", (char *) NULL};
-STATIC tpfPair serverPair =
-{(FILE *) NULL, (FILE *) NULL};
-STATIC pid_t serverId = NULLPROCESS;
+STATIC t_pchar def_args[] =
+{ (char *) NULL, (char *) NULL };
+STATIC t_pf_pair server_pair =
+{ (FILE *) NULL, (FILE *) NULL };
+STATIC pid_t server_id = NULLPROCESS;
/*
* 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.
*/
-tSCC zDone[] = "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd";
-STATIC tpChar pCurDir = (char *) NULL;
-
-/*
- * chainOpen
- *
- * Given an FD for an inferior process to use as stdin,
- * start that process and return a NEW FD that that process
- * will use for its stdout. Requires the argument vector
- * for the new process and, optionally, a pointer to a place
- * to store the child's process id.
- */
-int
-chainOpen (stdinFd, ppArgs, pChild)
- int stdinFd;
- tpChar *ppArgs;
- pid_t *pChild;
-{
- tFdPair stdoutPair =
- {-1, -1};
- pid_t chId;
- char *pzCmd;
-
- /*
- * Create a pipe it will be the child process' stdout,
- * and the parent will read from it.
- */
- if ((pipe ((int *) &stdoutPair) < 0))
- {
- if (pChild != (pid_t *) NULL)
- *pChild = NOPROCESS;
- return -1;
- }
-
- /*
- * If we did not get an arg list, use the default
- */
- if (ppArgs == (tpChar *) NULL)
- ppArgs = defArgs;
-
- /*
- * If the arg list does not have a program,
- * assume the "SHELL" from the environment, or, failing
- * that, then sh. Set argv[0] to whatever we decided on.
- */
- if (pzCmd = *ppArgs,
- (pzCmd == (char *) NULL) || (*pzCmd == '\0'))
- {
-
- pzCmd = getenv ("SHELL");
- if (pzCmd == (char *) NULL)
- pzCmd = "sh";
- }
-#ifdef DEBUG_PRINT
- printf ("START: %s\n", pzCmd);
- {
- int idx = 0;
- while (ppArgs[++idx] != (char *) NULL)
- printf (" ARG %2d: %s\n", idx, ppArgs[idx]);
- }
-#endif
- /*
- * Call fork() and see which process we become
- */
- chId = fork ();
- switch (chId)
- {
- case NOPROCESS: /* parent - error in call */
- close (stdoutPair.readFd);
- close (stdoutPair.writeFd);
- if (pChild != (pid_t *) NULL)
- *pChild = NOPROCESS;
- return -1;
-
- default: /* parent - return opposite FD's */
- if (pChild != (pid_t *) NULL)
- *pChild = chId;
-#ifdef DEBUG_PRINT
- printf ("for pid %d: stdin from %d, stdout to %d\n"
- "for parent: read from %d\n",
- chId, stdinFd, stdoutPair.writeFd, stdoutPair.readFd);
-#endif
- close (stdinFd);
- close (stdoutPair.writeFd);
- return stdoutPair.readFd;
-
- case NULLPROCESS: /* child - continue processing */
- break;
- }
-
- /*
- * Close the pipe end handed back to the parent process
- */
- close (stdoutPair.readFd);
-
- /*
- * Close our current stdin and stdout
- */
- close (STDIN_FILENO);
- close (STDOUT_FILENO);
-
- /*
- * Make the fd passed in the stdin, and the write end of
- * the new pipe become the stdout.
- */
- fcntl (stdoutPair.writeFd, F_DUPFD, STDOUT_FILENO);
- fcntl (stdinFd, F_DUPFD, STDIN_FILENO);
-
- if (*ppArgs == (char *) NULL)
- *ppArgs = pzCmd;
-
- execvp (pzCmd, ppArgs);
- fprintf (stderr, "Error %d: Could not execvp( '%s', ... ): %s\n",
- errno, pzCmd, strerror (errno));
- exit (EXIT_PANIC);
-}
-
-
-/*
- * p2open
- *
- * Given a pointer to an argument vector, start a process and
- * place its stdin and stdout file descriptors into an fd pair
- * structure. The "writeFd" connects to the inferior process
- * stdin, and the "readFd" connects to its stdout. The calling
- * process should write to "writeFd" and read from "readFd".
- * The return value is the process id of the created process.
- */
-pid_t
-p2open (pPair, ppArgs)
- tFdPair *pPair;
- tpChar *ppArgs;
-{
- pid_t chId;
-
- /*
- * Create a bi-directional pipe. Writes on 0 arrive on 1
- * and vice versa, so the parent and child processes will
- * read and write to opposite FD's.
- */
- if (pipe ((int *) pPair) < 0)
- return NOPROCESS;
-
- pPair->readFd = chainOpen (pPair->readFd, ppArgs, &chId);
- if (chId == NOPROCESS)
- close (pPair->writeFd);
-
- return chId;
-}
-
+tSCC z_done[] = "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd";
+STATIC t_pchar p_cur_dir = (char *) NULL;
/*
- * p2fopen
- *
- * Identical to "p2open()", except that the "fd"'s are "fdopen(3)"-ed
- * into file pointers instead.
- */
-pid_t
-p2fopen (pfPair, ppArgs)
- tpfPair *pfPair;
- tpChar *ppArgs;
-{
- tFdPair fdPair;
- pid_t chId = p2open (&fdPair, ppArgs);
-
- if (chId == NOPROCESS)
- return chId;
-
- pfPair->pfRead = fdopen (fdPair.readFd, "r");
- pfPair->pfWrite = fdopen (fdPair.writeFd, "w");
- return chId;
-}
-
-
-/*
- * loadData
+ * load_data
*
* 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.
*/
-STATIC char *
-loadData (fp)
+static char *
+load_data (fp)
FILE *fp;
{
- char *pzText;
- size_t textSize;
- char *pzScan;
- char zLine[1024];
+ char *pz_text;
+ size_t text_size;
+ char *pz_scan;
+ char z_line[1024];
- textSize = sizeof (zLine) * 2;
- pzScan = \
- pzText = malloc (textSize);
+ text_size = sizeof (z_line) * 2;
+ pz_scan = pz_text = malloc (text_size);
- if (pzText == (char *) NULL)
- return pzText;
+ if (pz_text == (char *) NULL)
+ return (char *) NULL;
for (;;)
{
- size_t usedCt;
+ size_t used_ct;
alarm (10);
- readPipeTimeout = BOOL_FALSE;
- if (fgets (zLine, sizeof (zLine), fp) == (char *) NULL)
- break;
-
- if (strncmp (zLine, zDone, sizeof (zDone) - 1) == 0)
- break;
-
- strcpy (pzScan, zLine);
- pzScan += strlen (zLine);
- usedCt = (size_t) (pzScan - pzText);
-
- if (textSize - usedCt < sizeof (zLine))
- {
-
- size_t off = (size_t) (pzScan - pzText);
- void *p;
- textSize += 4096;
- p = realloc ((void *) pzText, textSize);
- if (p == (void *) NULL)
- {
- fprintf (stderr, "Failed to get 0x%08X bytes\n", textSize);
- free ((void *) pzText);
- return (char *) NULL;
- }
-
- pzText = (char *) p;
- pzScan = pzText + off;
- }
+ 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)
+ break;
+
+ 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);
+ void *p;
+
+ text_size += 4096;
+ p = realloc ((void *) pz_text, text_size);
+ if (p == (void *) NULL)
+ {
+ fprintf (stderr, "Failed to get 0x%08X bytes\n", text_size);
+ free ((void *) pz_text);
+ return (char *) NULL;
+ }
+ pz_text = (char *) p;
+ pz_scan = pz_text + off;
+ }
}
alarm (0);
- if (readPipeTimeout)
+ if (read_pipe_timeout)
{
- free ((void *) pzText);
+ free ((void *) pz_text);
return (char *) NULL;
}
- while ((pzScan > pzText) && isspace (pzScan[-1]))
- pzScan--;
- *pzScan = NUL;
- return realloc ((void *) pzText, strlen (pzText) + 1);
+ while ((pz_scan > pz_text) && isspace (pz_scan[-1]))
+ pz_scan--;
+ *pz_scan = NUL;
+ return realloc ((void *) pz_text, strlen (pz_text) + 1);
}
/*
- * SHELL SERVER PROCESS CODE
+ * close_server
+ *
+ * Make certain the server process is dead, close the
+ * pipes to it and from it, finally NULL out the file pointers
*/
-
-#ifdef DONT_HAVE_SIGSEND
-typedef enum
+static void
+close_server ()
{
- P_ALL, P_PID, P_GID, P_UID, P_PGID, P_SID, P_CID
+ kill ((pid_t) server_id, SIGKILL);
+ server_id = NULLPROCESS;
+ fclose (server_pair.pf_read);
+ fclose (server_pair.pf_write);
+ server_pair.pf_read = server_pair.pf_write = (FILE *) NULL;
}
-idtype_t;
-typedef long id_t;
-
-STATIC int
-sigsend (idtype, id, sig)
- idtype_t idtype;
- id_t id;
- int sig;
-{
- switch (idtype)
- {
- case P_PID:
- kill ((pid_t) id, sig);
- break;
-
- case P_ALL:
- case P_GID:
- case P_UID:
- case P_PGID:
- case P_SID:
- case P_CID:
- errno = EINVAL;
- return -1;
- /*NOTREACHED */
- }
-
- return 0;
-}
-#endif /* HAVE_SIGSEND */
-
-STATIC void
-closeServer ()
-{
- kill( (pid_t) serverId, SIGKILL);
- serverId = NULLPROCESS;
- fclose (serverPair.pfRead);
- fclose (serverPair.pfWrite);
- serverPair.pfRead = serverPair.pfWrite = (FILE *) NULL;
-}
-
-
-struct sigaction savePipeAction;
-struct sigaction saveAlrmAction;
-struct sigaction currentAction;
-
-STATIC void
-sigHandler (signo)
+/*
+ * 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
+sig_handler (signo)
int signo;
{
- closeServer ();
- readPipeTimeout = BOOL_TRUE;
+ close_server ();
+ read_pipe_timeout = BOOL_TRUE;
}
-STATIC void
-serverSetup ()
+/*
+ * 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
+server_setup ()
{
-#ifndef SA_SIGINFO
-# define SA_SIGINFO 0
-#else
- currentAction.sa_sigaction =
-#endif
- currentAction.sa_handler = sigHandler;
- currentAction.sa_flags = SA_SIGINFO;
- sigemptyset( &currentAction.sa_mask );
+ static int atexit_done = 0;
+
+ if (atexit_done++ == 0)
+ atexit (&close_server);
- sigaction( SIGPIPE, &currentAction, &savePipeAction );
- sigaction( SIGALRM, &currentAction, &saveAlrmAction );
- atexit( &closeServer );
+ signal (SIGPIPE, sig_handler);
+ signal (SIGALRM, sig_handler);
- fputs ("trap : INT\n", serverPair.pfWrite);
- fflush (serverPair.pfWrite);
- pCurDir = getcwd ((char *) NULL, MAXPATHLEN + 1);
+ fputs ("trap : 1\n", server_pair.pf_write);
+ fflush (server_pair.pf_write);
+ p_cur_dir = getcwd ((char *) NULL, MAXPATHLEN + 1);
}
+/*
+ * 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"
+ */
char *
-runShell (pzCmd)
- const char *pzCmd;
+run_shell (pz_cmd)
+ const char *pz_cmd;
{
- tSCC zNil[] = "";
+ /* IF the shell server process is not running yet,
+ THEN try to start it. */
+ if (server_id == NULLPROCESS)
+ {
+ server_id = proc2_fopen (&server_pair, def_args);
+ if (server_id > 0)
+ server_setup ();
+ }
- /*
- * IF the shell server process is not running yet,
- * THEN try to start it.
- */
- if (serverId == NULLPROCESS)
+ /* IF it is still not running, THEN return the nil string. */
+ if (server_id <= 0)
{
- serverId = p2fopen (&serverPair, defArgs);
- if (serverId > 0)
- serverSetup ();
+ char *pz = (char *) malloc (1);
+
+ if (pz != (char *) NULL)
+ *pz = '\0';
+ return pz;
}
- /*
- * IF it is still not running,
- * THEN return the nil string.
- */
- if (serverId <= 0)
- return (char *) zNil;
+ /* 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. */
+ fprintf (server_pair.pf_write, "\\cd %s\n%s\n\necho\necho %s\n",
+ p_cur_dir, pz_cmd, z_done);
+ fflush (server_pair.pf_write);
- /*
- * 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.
- */
- fprintf (serverPair.pfWrite, "\\cd %s\n%s\n\necho\necho %s\n",
- pCurDir, pzCmd, zDone);
- fflush (serverPair.pfWrite);
- if (serverId == NULLPROCESS)
- return (char *) NULL;
+ /* IF the server died and we received a SIGPIPE,
+ THEN return an empty string. */
+ if (server_id == NULLPROCESS)
+ {
+ char *pz = (char *) malloc (1);
+
+ if (pz != (char *) NULL)
+ *pz = '\0';
+ return pz;
+ }
- /*
- * 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.
- */
+ /* 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. */
{
- char *pz = loadData (serverPair.pfRead);
+ char *pz = load_data (server_pair.pf_read);
+
if (pz == (char *) NULL)
{
- fprintf (stderr, "CLOSING SHELL SERVER - command failure:\n\t%s\n",
- pzCmd);
- closeServer ();
- pz = (char *) zNil;
+ fprintf (stderr, "CLOSING SHELL SERVER - command failure:\n\t%s\n",
+ pz_cmd);
+ close_server ();
+ pz = (char *) malloc (1);
+ if (pz != (char *) NULL)
+ *pz = '\0';
}
return pz;
}