server.c 8.51 KB
Newer Older
1 2

/*
Bruce Korb committed
3
 *  server.c  Set up and handle communications with a server process.
4
 *
5
 *  Server Handling copyright 1992-1999, 2001 The Free Software Foundation
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 *  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.,
20 21
 *             51 Franklin Street, Fifth Floor,
 *             Boston,  MA  02110-1301, USA.
22
 *
Bruce Korb committed
23 24 25
 * As a special exception, The Free Software Foundation gives
 * permission for additional uses of the text contained in his release
 * of ServerHandler.
26 27 28 29 30 31 32 33 34 35
 *
 * 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.
 *
Bruce Korb committed
36 37 38 39 40 41 42
 * 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.
43 44 45 46 47 48
 *
 * 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.
 */

49
#include "fixlib.h"
50 51
#include "server.h"

52
STATIC volatile enum t_bool read_pipe_timeout;
53
STATIC pid_t server_master_pid = NOPROCESS;
54

Bruce Korb committed
55
tSCC* def_args[] =
Bruce Korb committed
56 57 58 59
{ (char *) NULL, (char *) NULL };
STATIC t_pf_pair server_pair =
{ (FILE *) NULL, (FILE *) NULL };
STATIC pid_t server_id = NULLPROCESS;
60 61 62 63 64
/*
 *  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.
 */
Bruce Korb committed
65
tSCC z_done[] = "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd";
Bruce Korb committed
66
tSCC* p_cur_dir = (char *) NULL;
67 68

/*
Bruce Korb committed
69
 *  load_data
70 71 72 73 74 75
 *
 *  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.
 */
Bruce Korb committed
76
static char *
77
load_data (FILE* fp)
78
{
Bruce Korb committed
79 80 81 82
  char *pz_text;
  size_t text_size;
  char *pz_scan;
  char z_line[1024];
83
  t_bool got_done = BOOL_FALSE;
84

Bruce Korb committed
85
  text_size = sizeof (z_line) * 2;
86
  pz_scan = pz_text = XNEWVEC (char, text_size);
87 88 89

  for (;;)
    {
Bruce Korb committed
90
      size_t used_ct;
91 92

      alarm (10);
Bruce Korb committed
93 94 95 96 97
      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)
98 99 100 101
	{
	  got_done = BOOL_TRUE;
	  break;
	}
Bruce Korb committed
102 103 104 105 106 107 108 109 110 111

      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;
112
          pz_text = XRESIZEVEC (char, pz_text, text_size);
Bruce Korb committed
113 114
          pz_scan = pz_text + off;
        }
115 116 117
    }

  alarm (0);
118
  if (read_pipe_timeout || ! got_done)
119
    {
Bruce Korb committed
120
      free ((void *) pz_text);
121 122 123
      return (char *) NULL;
    }

124
  while ((pz_scan > pz_text) && ISSPACE (pz_scan[-1]))
Bruce Korb committed
125 126
    pz_scan--;
  *pz_scan = NUL;
127
  return XRESIZEVEC (char, pz_text, strlen (pz_text) + 1);
128 129 130 131
}


/*
Bruce Korb committed
132 133 134 135
 *  close_server
 *
 *  Make certain the server process is dead, close the 
 *  pipes to it and from it, finally NULL out the file pointers
136
 */
137
void
138
close_server (void)
139
{
140 141
  if (  (server_id != NULLPROCESS)
     && (server_master_pid == getpid ()))
142 143 144
    {
      kill ((pid_t) server_id, SIGKILL);
      server_id = NULLPROCESS;
145
      server_master_pid = NOPROCESS;
146 147 148 149
      fclose (server_pair.pf_read);
      fclose (server_pair.pf_write);
      server_pair.pf_read = server_pair.pf_write = (FILE *) NULL;
    }
150 151
}

Bruce Korb committed
152 153 154 155 156 157 158
/*
 *  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
159
sig_handler (int signo ATTRIBUTE_UNUSED)
160
{
161 162 163 164 165 166
#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
Bruce Korb committed
167 168
  close_server ();
  read_pipe_timeout = BOOL_TRUE;
169 170 171
}


Bruce Korb committed
172 173 174 175 176 177
/*
 *  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
178
server_setup (void)
179
{
Bruce Korb committed
180
  static int atexit_done = 0;
Nathanael Nerode committed
181
  char buff [MAXPATHLEN + 1];
Bruce Korb committed
182 183
  
  if (atexit_done++ == 0)
184
    atexit (close_server);
185 186
  else
    fputs ("NOTE: server restarted\n", stderr);
187

188 189
  server_master_pid = getpid ();

Bruce Korb committed
190 191
  signal (SIGPIPE, sig_handler);
  signal (SIGALRM, sig_handler);
192

Bruce Korb committed
193 194
  fputs ("trap : 1\n", server_pair.pf_write);
  fflush (server_pair.pf_write);
195 196
  if (getcwd (buff, MAXPATHLEN + 1) == NULL)
    buff[0] = 0;
Nathanael Nerode committed
197
  p_cur_dir = xstrdup (buff);
198 199
}

200 201 202 203 204 205 206 207 208 209 210 211
/*
 *  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.
 */

Bruce Korb committed
212
static const char *
213
find_shell (void)
214 215 216 217 218 219 220 221
{
  char * shell = getenv ("CONFIG_SHELL");
  if (shell)
    return shell;

  return "/bin/sh";
}

222

Bruce Korb committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
/*
 *  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"
 */
242
char *
243
run_shell (const char* pz_cmd)
244
{
245
  tSCC zNoServer[] = "Server not running, cannot run:\n%s\n\n";
246 247 248
  t_bool retry = BOOL_TRUE;

 do_retry:
Bruce Korb committed
249 250 251 252
  /*  IF the shell server process is not running yet,
      THEN try to start it.  */
  if (server_id == NULLPROCESS)
    {
253 254
      def_args[0] = find_shell ();

Bruce Korb committed
255 256 257 258
      server_id = proc2_fopen (&server_pair, def_args);
      if (server_id > 0)
        server_setup ();
    }
259

Bruce Korb committed
260 261
  /*  IF it is still not running, THEN return the nil string.  */
  if (server_id <= 0)
262
    {
263
      fprintf (stderr, zNoServer, pz_cmd);
264
      return XCNEW (char);
265 266
    }

Bruce Korb committed
267 268 269
  /*  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.  */
270
  fprintf (server_pair.pf_write, "cd \"%s\"\n%s\n\necho\necho %s\n",
Bruce Korb committed
271 272
           p_cur_dir, pz_cmd, z_done);
  fflush (server_pair.pf_write);
273

Bruce Korb committed
274 275 276 277
  /*  IF the server died and we received a SIGPIPE,
      THEN return an empty string.  */
  if (server_id == NULLPROCESS)
    {
278
      fprintf (stderr, zNoServer, pz_cmd);
279
      return XCNEW (char);
Bruce Korb committed
280
    }
281

Bruce Korb committed
282 283
  /*  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.  */
284
  {
Bruce Korb committed
285 286
    char *pz = load_data (server_pair.pf_read);
    
287 288
    if (pz == (char *) NULL)
      {
289 290 291 292 293 294 295 296
	close_server ();

	if (retry)
	  {
	    retry = BOOL_FALSE;
	    goto do_retry;
	  }

Bruce Korb committed
297 298
        fprintf (stderr, "CLOSING SHELL SERVER - command failure:\n\t%s\n",
                 pz_cmd);
299
        pz = XCNEW (char);
300
      }
301 302 303
#ifdef DEBUG
    fprintf( stderr, "run_shell command success:  %s\n", pz );
#endif
304 305 306
    return pz;
  }
}