14fee23f9Smrg
24fee23f9Smrg /*
34fee23f9Smrg * server.c Set up and handle communications with a server process.
44fee23f9Smrg *
54fee23f9Smrg * Server Handling copyright 1992-1999, 2001 The Free Software Foundation
64fee23f9Smrg *
74fee23f9Smrg * Server Handling is free software.
84fee23f9Smrg * You may redistribute it and/or modify it under the terms of the
94fee23f9Smrg * GNU General Public License, as published by the Free Software
104fee23f9Smrg * Foundation; either version 2, or (at your option) any later version.
114fee23f9Smrg *
124fee23f9Smrg * Server Handling is distributed in the hope that it will be useful,
134fee23f9Smrg * but WITHOUT ANY WARRANTY; without even the implied warranty of
144fee23f9Smrg * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
154fee23f9Smrg * GNU General Public License for more details.
164fee23f9Smrg *
174fee23f9Smrg * You should have received a copy of the GNU General Public License
184fee23f9Smrg * along with Server Handling. See the file "COPYING". If not,
194fee23f9Smrg * write to: The Free Software Foundation, Inc.,
204fee23f9Smrg * 51 Franklin Street, Fifth Floor,
214fee23f9Smrg * Boston, MA 02110-1301, USA.
224fee23f9Smrg *
234fee23f9Smrg * As a special exception, The Free Software Foundation gives
244fee23f9Smrg * permission for additional uses of the text contained in his release
254fee23f9Smrg * of ServerHandler.
264fee23f9Smrg *
274fee23f9Smrg * The exception is that, if you link the ServerHandler library with other
284fee23f9Smrg * files to produce an executable, this does not by itself cause the
294fee23f9Smrg * resulting executable to be covered by the GNU General Public License.
304fee23f9Smrg * Your use of that executable is in no way restricted on account of
314fee23f9Smrg * linking the ServerHandler library code into it.
324fee23f9Smrg *
334fee23f9Smrg * This exception does not however invalidate any other reasons why
344fee23f9Smrg * the executable file might be covered by the GNU General Public License.
354fee23f9Smrg *
364fee23f9Smrg * This exception applies only to the code released by The Free
374fee23f9Smrg * Software Foundation under the name ServerHandler. If you copy code
384fee23f9Smrg * from other sources under the General Public License into a copy of
394fee23f9Smrg * ServerHandler, as the General Public License permits, the exception
404fee23f9Smrg * does not apply to the code that you add in this way. To avoid
414fee23f9Smrg * misleading anyone as to the status of such modified files, you must
424fee23f9Smrg * delete this exception notice from them.
434fee23f9Smrg *
444fee23f9Smrg * If you write modifications of your own for ServerHandler, it is your
454fee23f9Smrg * choice whether to permit this exception to apply to your modifications.
464fee23f9Smrg * If you do not wish that, delete this exception notice.
474fee23f9Smrg */
484fee23f9Smrg
494fee23f9Smrg #include "fixlib.h"
504fee23f9Smrg #include "server.h"
514fee23f9Smrg
524fee23f9Smrg STATIC volatile enum t_bool read_pipe_timeout;
534fee23f9Smrg STATIC pid_t server_master_pid = NOPROCESS;
544fee23f9Smrg
554fee23f9Smrg tSCC* def_args[] =
564fee23f9Smrg { (char *) NULL, (char *) NULL };
574fee23f9Smrg STATIC t_pf_pair server_pair =
584fee23f9Smrg { (FILE *) NULL, (FILE *) NULL };
594fee23f9Smrg STATIC pid_t server_id = NULLPROCESS;
604fee23f9Smrg /*
614fee23f9Smrg * Arbitrary text that should not be found in the shell output.
624fee23f9Smrg * It must be a single line and appear verbatim at the start of
634fee23f9Smrg * the terminating output line.
644fee23f9Smrg */
654fee23f9Smrg tSCC z_done[] = "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd";
664fee23f9Smrg tSCC* p_cur_dir = (char *) NULL;
674fee23f9Smrg
684fee23f9Smrg /*
694fee23f9Smrg * load_data
704fee23f9Smrg *
714fee23f9Smrg * Read data from a file pointer (a pipe to a process in this context)
724fee23f9Smrg * until we either get EOF or we get a marker line back.
734fee23f9Smrg * The read data are stored in a malloc-ed string that is truncated
744fee23f9Smrg * to size at the end. Input is assumed to be an ASCII string.
754fee23f9Smrg */
764fee23f9Smrg static char *
load_data(FILE * fp)774fee23f9Smrg load_data (FILE* fp)
784fee23f9Smrg {
794fee23f9Smrg char *pz_text;
804fee23f9Smrg size_t text_size;
814fee23f9Smrg char *pz_scan;
824fee23f9Smrg char z_line[1024];
834fee23f9Smrg t_bool got_done = BOOL_FALSE;
844fee23f9Smrg
854fee23f9Smrg text_size = sizeof (z_line) * 2;
864fee23f9Smrg pz_scan = pz_text = XNEWVEC (char, text_size);
874fee23f9Smrg
884fee23f9Smrg for (;;)
894fee23f9Smrg {
904fee23f9Smrg size_t used_ct;
914fee23f9Smrg
924fee23f9Smrg alarm (10);
934fee23f9Smrg read_pipe_timeout = BOOL_FALSE;
944fee23f9Smrg if (fgets (z_line, sizeof (z_line), fp) == (char *) NULL)
954fee23f9Smrg break;
964fee23f9Smrg
974fee23f9Smrg if (strncmp (z_line, z_done, sizeof (z_done) - 1) == 0)
984fee23f9Smrg {
994fee23f9Smrg got_done = BOOL_TRUE;
1004fee23f9Smrg break;
1014fee23f9Smrg }
1024fee23f9Smrg
1034fee23f9Smrg strcpy (pz_scan, z_line);
1044fee23f9Smrg pz_scan += strlen (z_line);
1054fee23f9Smrg used_ct = (size_t) (pz_scan - pz_text);
1064fee23f9Smrg
1074fee23f9Smrg if (text_size - used_ct < sizeof (z_line))
1084fee23f9Smrg {
1094fee23f9Smrg size_t off = (size_t) (pz_scan - pz_text);
1104fee23f9Smrg
1114fee23f9Smrg text_size += 4096;
1124fee23f9Smrg pz_text = XRESIZEVEC (char, pz_text, text_size);
1134fee23f9Smrg pz_scan = pz_text + off;
1144fee23f9Smrg }
1154fee23f9Smrg }
1164fee23f9Smrg
1174fee23f9Smrg alarm (0);
1184fee23f9Smrg if (read_pipe_timeout || ! got_done)
1194fee23f9Smrg {
1204fee23f9Smrg free ((void *) pz_text);
1214fee23f9Smrg return (char *) NULL;
1224fee23f9Smrg }
1234fee23f9Smrg
1244fee23f9Smrg while ((pz_scan > pz_text) && ISSPACE (pz_scan[-1]))
1254fee23f9Smrg pz_scan--;
1264fee23f9Smrg *pz_scan = NUL;
1274fee23f9Smrg return XRESIZEVEC (char, pz_text, strlen (pz_text) + 1);
1284fee23f9Smrg }
1294fee23f9Smrg
1304fee23f9Smrg
1314fee23f9Smrg /*
1324fee23f9Smrg * close_server
1334fee23f9Smrg *
1344fee23f9Smrg * Make certain the server process is dead, close the
1354fee23f9Smrg * pipes to it and from it, finally NULL out the file pointers
1364fee23f9Smrg */
1374fee23f9Smrg void
close_server(void)1384fee23f9Smrg close_server (void)
1394fee23f9Smrg {
1404fee23f9Smrg if ( (server_id != NULLPROCESS)
1414fee23f9Smrg && (server_master_pid == getpid ()))
1424fee23f9Smrg {
1434fee23f9Smrg kill ((pid_t) server_id, SIGKILL);
1444fee23f9Smrg server_id = NULLPROCESS;
1454fee23f9Smrg server_master_pid = NOPROCESS;
1464fee23f9Smrg fclose (server_pair.pf_read);
1474fee23f9Smrg fclose (server_pair.pf_write);
1484fee23f9Smrg server_pair.pf_read = server_pair.pf_write = (FILE *) NULL;
1494fee23f9Smrg }
1504fee23f9Smrg }
1514fee23f9Smrg
1524fee23f9Smrg /*
1534fee23f9Smrg * sig_handler really only handles the timeout and pipe signals.
1544fee23f9Smrg * This ensures that we do not wait forever on a request
1554fee23f9Smrg * to our server, and also that if the server dies, we do not
1564fee23f9Smrg * die from a sigpipe problem.
1574fee23f9Smrg */
1584fee23f9Smrg static void
sig_handler(int signo ATTRIBUTE_UNUSED)1594fee23f9Smrg sig_handler (int signo ATTRIBUTE_UNUSED)
1604fee23f9Smrg {
1614fee23f9Smrg #ifdef DEBUG
1624fee23f9Smrg /* FIXME: this is illegal to do in a signal handler. */
1634fee23f9Smrg fprintf (stderr,
1644fee23f9Smrg "fixincl ERROR: sig_handler: killed pid %ld due to %s\n",
1654fee23f9Smrg (long) server_id, signo == SIGPIPE ? "SIGPIPE" : "SIGALRM");
1664fee23f9Smrg #endif
1674fee23f9Smrg close_server ();
1684fee23f9Smrg read_pipe_timeout = BOOL_TRUE;
1694fee23f9Smrg }
1704fee23f9Smrg
1714fee23f9Smrg
1724fee23f9Smrg /*
1734fee23f9Smrg * server_setup Establish the signal handler for PIPE and ALARM.
1744fee23f9Smrg * Also establishes the current directory to give to the
1754fee23f9Smrg * server process at the start of every server command.
1764fee23f9Smrg */
1774fee23f9Smrg static void
server_setup(void)1784fee23f9Smrg server_setup (void)
1794fee23f9Smrg {
1804fee23f9Smrg static int atexit_done = 0;
1814fee23f9Smrg char buff [MAXPATHLEN + 1];
1824fee23f9Smrg
1834fee23f9Smrg if (atexit_done++ == 0)
1844fee23f9Smrg atexit (close_server);
1854fee23f9Smrg else
1864fee23f9Smrg fputs ("NOTE: server restarted\n", stderr);
1874fee23f9Smrg
1884fee23f9Smrg server_master_pid = getpid ();
1894fee23f9Smrg
1904fee23f9Smrg signal (SIGPIPE, sig_handler);
1914fee23f9Smrg signal (SIGALRM, sig_handler);
1924fee23f9Smrg
1934fee23f9Smrg fputs ("trap : 1\n", server_pair.pf_write);
1944fee23f9Smrg fflush (server_pair.pf_write);
195*4d5abbe8Smrg if (getcwd (buff, MAXPATHLEN + 1) == NULL)
196*4d5abbe8Smrg buff[0] = 0;
1974fee23f9Smrg p_cur_dir = xstrdup (buff);
1984fee23f9Smrg }
1994fee23f9Smrg
2004fee23f9Smrg /*
2014fee23f9Smrg * find_shell
2024fee23f9Smrg *
2034fee23f9Smrg * Locate a shell suitable for use. For various reasons
2044fee23f9Smrg * (like the use of "trap" in server_setup(), it must be a
2054fee23f9Smrg * Bourne-like shell.
2064fee23f9Smrg *
2074fee23f9Smrg * Most of the time, /bin/sh is preferred, but sometimes
2084fee23f9Smrg * it's quite broken (like on Ultrix). autoconf lets you
2094fee23f9Smrg * override with $CONFIG_SHELL, so we do the same.
2104fee23f9Smrg */
2114fee23f9Smrg
2124fee23f9Smrg static const char *
find_shell(void)2134fee23f9Smrg find_shell (void)
2144fee23f9Smrg {
2154fee23f9Smrg char * shell = getenv ("CONFIG_SHELL");
2164fee23f9Smrg if (shell)
2174fee23f9Smrg return shell;
2184fee23f9Smrg
2194fee23f9Smrg return "/bin/sh";
2204fee23f9Smrg }
2214fee23f9Smrg
2224fee23f9Smrg
2234fee23f9Smrg /*
2244fee23f9Smrg * run_shell
2254fee23f9Smrg *
2264fee23f9Smrg * Run a shell command on the server. The command string
2274fee23f9Smrg * passed in is wrapped inside the sequence:
2284fee23f9Smrg *
2294fee23f9Smrg * cd <original directory>
2304fee23f9Smrg * <command string>
2314fee23f9Smrg * echo
2324fee23f9Smrg * echo <end-of-command-marker>
2334fee23f9Smrg *
2344fee23f9Smrg * This ensures that all commands start at a known place in
2354fee23f9Smrg * the directory structure, that any incomplete output lines
2364fee23f9Smrg * are completed and that our special marker sequence appears on
2374fee23f9Smrg * a line by itself. We have chosen a marker that is
2384fee23f9Smrg * excessively unlikely to be reproduced in normal output:
2394fee23f9Smrg *
2404fee23f9Smrg * "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd"
2414fee23f9Smrg */
2424fee23f9Smrg char *
run_shell(const char * pz_cmd)2434fee23f9Smrg run_shell (const char* pz_cmd)
2444fee23f9Smrg {
2454fee23f9Smrg tSCC zNoServer[] = "Server not running, cannot run:\n%s\n\n";
2464fee23f9Smrg t_bool retry = BOOL_TRUE;
2474fee23f9Smrg
2484fee23f9Smrg do_retry:
2494fee23f9Smrg /* IF the shell server process is not running yet,
2504fee23f9Smrg THEN try to start it. */
2514fee23f9Smrg if (server_id == NULLPROCESS)
2524fee23f9Smrg {
2534fee23f9Smrg def_args[0] = find_shell ();
2544fee23f9Smrg
2554fee23f9Smrg server_id = proc2_fopen (&server_pair, def_args);
2564fee23f9Smrg if (server_id > 0)
2574fee23f9Smrg server_setup ();
2584fee23f9Smrg }
2594fee23f9Smrg
2604fee23f9Smrg /* IF it is still not running, THEN return the nil string. */
2614fee23f9Smrg if (server_id <= 0)
2624fee23f9Smrg {
2634fee23f9Smrg fprintf (stderr, zNoServer, pz_cmd);
2644fee23f9Smrg return XCNEW (char);
2654fee23f9Smrg }
2664fee23f9Smrg
2674fee23f9Smrg /* Make sure the process will pay attention to us, send the
2684fee23f9Smrg supplied command, and then have it output a special marker that
2694fee23f9Smrg we can find. */
2704fee23f9Smrg fprintf (server_pair.pf_write, "cd \"%s\"\n%s\n\necho\necho %s\n",
2714fee23f9Smrg p_cur_dir, pz_cmd, z_done);
2724fee23f9Smrg fflush (server_pair.pf_write);
2734fee23f9Smrg
2744fee23f9Smrg /* IF the server died and we received a SIGPIPE,
2754fee23f9Smrg THEN return an empty string. */
2764fee23f9Smrg if (server_id == NULLPROCESS)
2774fee23f9Smrg {
2784fee23f9Smrg fprintf (stderr, zNoServer, pz_cmd);
2794fee23f9Smrg return XCNEW (char);
2804fee23f9Smrg }
2814fee23f9Smrg
2824fee23f9Smrg /* Now try to read back all the data. If we fail due to either a
2834fee23f9Smrg sigpipe or sigalrm (timeout), we will return the nil string. */
2844fee23f9Smrg {
2854fee23f9Smrg char *pz = load_data (server_pair.pf_read);
2864fee23f9Smrg
2874fee23f9Smrg if (pz == (char *) NULL)
2884fee23f9Smrg {
2894fee23f9Smrg close_server ();
2904fee23f9Smrg
2914fee23f9Smrg if (retry)
2924fee23f9Smrg {
2934fee23f9Smrg retry = BOOL_FALSE;
2944fee23f9Smrg goto do_retry;
2954fee23f9Smrg }
2964fee23f9Smrg
2974fee23f9Smrg fprintf (stderr, "CLOSING SHELL SERVER - command failure:\n\t%s\n",
2984fee23f9Smrg pz_cmd);
2994fee23f9Smrg pz = XCNEW (char);
3004fee23f9Smrg }
3014fee23f9Smrg #ifdef DEBUG
3024fee23f9Smrg fprintf( stderr, "run_shell command success: %s\n", pz );
3034fee23f9Smrg #endif
3044fee23f9Smrg return pz;
3054fee23f9Smrg }
3064fee23f9Smrg }
307