[comp.windows.x] Cheap hack for starting remote xterms

grindef@chianti.cc.umich.edu (Wes Craig) (07/08/88)

Ever wonder about all those rsh's you've been spawning from your
window manager that never go away? We were bored enough one night
to try and do something about 'em. So.... Here's "exeqt.c" - clip out
and enjoy...
####### cut here and at end. Not a shar file. (No kidding, eh?  :) ########
/*
 * This is a grungy little program for executing programs in the
 * background, without use of a control terminal. (In the style
 * of most common daemon processes...) The intent was to create a
 * program one could start via rsh, to initiate xterm sessions,
 * without keeping extra local rsh & remote rshd and shell processes
 * alive.
 *
 * Could be a bit less cavalier about return codes...
 *
 * No warrantees. Use at your own risk. The authors are not responsible
 * in any way for anything at all.
 *   -- Howard Chu & Wes Craig, University of Michigan    July 7 1988
 *	hyc@umix.cc.umich.edu	grindef@umix.cc.umich.edu
 */

#ifndef lint
static char rcsid[] = "$Header: exeqt.c,v 1.2 88/07/07 21:05:39 grindef Exp $";
#endif

#include <sys/file.h>
#include <sys/ioctl.h>

main(argc, argv)
	int             argc;
	char           *argv[];
{
	char           *path, *getenv(), *index(), *i, j[1024];
	path = getenv("PATH");
	while (path) {
		i = index(path, ':');
		if (i) {
			*i = 0;
			i++;
		}
		strcpy(j, path);
		strcat(j, "/");
		strcat(j, argv[1]);
		if (!access(j, X_OK || F_OK)) {
			if (fork())
				exit();
			else{
				int s;
				for (s=0; s < 10; s++)
					close(s);
				open("/", O_RDONLY);
				dup2(0, 1);
				dup2(0, 2);
				s=open("/dev/tty", O_RDWR);
				ioctl(s, TIOCNOTTY, 0);
				close(s);

				execv(j, &argv[1]);
			}
		}
		path = i;
	}
}
##### cut here too... #####

dpb@hpak.UUCP (07/13/88)

Has been hacked a little more,esp. not to munge the 
environment.
Still no warrantees.

/*
 * A few changes from the original - a lot of daemon stuff taken
 * from "How to Write a UNIX Daemon", by Dave Lennert, in the 
 * USENIX Newsletter ";login:" V12#4.
 *
 * Don't search the path if a pathname is given
 *  (if a '/' appears in argv[1]);
 * exit if argc==1;
 * Don't trash the PATH variable (put back colons!)
 *
 *   -Don Bennett-      (408)433-3311
 *    dpb%frame@sun.com      or, sun!frame!dpb
 *    Frame Technology
 */ 

/*
 * This is a grungy little program for executing programs in the
 * background, without use of a control terminal. (In the style
 * of most common daemon processes...) The intent was to create a
 * program one could start via rsh, to initiate xterm sessions,
 * without keeping extra local rsh & remote rshd and shell processes
 * alive.
 *
 * Could be a bit less cavalier about return codes...
 *
 * No warrantees. Use at your own risk. The authors are not responsible
 * in any way for anything at all.
 *   -- Howard Chu & Wes Craig, University of Michigan    July 7 1988
 *	hyc@umix.cc.umich.edu	grindef@umix.cc.umich.edu
 */

#ifndef lint
static char rcsid[] = "$Header: daemon.c,v 1.6 88/07/12 18:57:09 dpb Exp $";
#endif

#ifdef SYS_V
#include <fcntl.h>
#endif  /* SYS_V */

#include <signal.h>
#include <sys/file.h>
#include <sys/ioctl.h>

#ifdef SYS_V
#define index strchr
#endif /* SYS_V */

detach(cmd, args)
char *cmd;
char **args;
{
    int fd;

#ifdef SIGTTOU
signal(SIGTTOU, SIG_IGN);
#endif /* SIGTTOU */

#ifdef SIGTTIN
signal(SIGTTIN, SIG_IGN);
#endif /* SIGTTIN */

#ifdef SIGTSTP
signal(SIGTSTP, SIG_IGN);
#endif /* SIGTSTP */

    if (fork())
	exit();	/* parent */

#ifdef SYS_V
    /* child */
    setpgrp();			/* lose controlling terminal & */
					/*  change process group       */
    signal(SIGHUP, SIG_IGN);	/* immune from pgrp leader death */
    if (fork())			/* become non-pgrp-leader */
	exit(0);

    /* second child */
#else
    setpgrp(0, getpid());	/* change process group */
    if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
	ioctl(fd, TIOCNOTTY, 0); /* lose controlling terminal */
	close(fd);
    }
#endif /* SYS_V */
    for (fd=0; fd < 10; fd++)
	close(fd);

    /* don't know what this does, but xterm (or ksh in xterm)
     * wont start up without it;
     */

    open("/", O_RDONLY);
    dup2(0, 1);
    dup2(0, 2);

    umask(0);
    execv(cmd, args);
}

main(argc, argv)
int             argc;
char           *argv[];
{
    char           *path, *getenv(), *index(), *i, *fixcolon, j[1024];

    if (argc == 1)
        exit(0);

    /* if the cmd to run in daemon mode has a '/' in it, the path
     * has been specified, so don't search the path for it;
     */
    if (index(argv[1], '/')) {
	detach(argv[1],&argv[1]);
    }
    else {
	path = getenv("PATH");
	while (path) {
	    fixcolon=0;
	    i = index(path, ':');
	    if (i) {
		*i = 0;
		fixcolon=i;
		i++;
	    }
	    strcpy(j, path);
	    strcat(j, "/");
	    strcat(j, argv[1]);
	    
	    if (fixcolon)
		*fixcolon = ':';
	    
	    if (!access(j, X_OK || F_OK)) {
		detach(j, &argv[1]);
	    }
	    path = i;
	}
    }
}

clyde@ut-emx.UUCP (Head UNIX Hacquer) (07/19/88)

CODA=">/dev/null 2>&1"		# This is the magic

xhost +$host
echo "xterm -ls -n $host -T $host (...) $CODA &" | rsh $host sh

This does not leave (at last check) either a local rsh OR a remote rshd
running.
-- 
Shouter-To-Dead-Parrots @ Univ. of Texas Computation Center; Austin, Texas  
	clyde@emx.utexas.edu; ...!ut-sally!ut-emx!clyde

"That would be the easy way, but it wouldn't be The Cowboy Way."
	-Riders in the Sky