Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!mailrus!tut.cis.ohio-state.edu!bloom-beacon!hpak.UUCP!dpb
From: dpb@hpak.UUCP
Newsgroups: comp.windows.x
Subject: Re: Cheap hack for starting remote xterms
Message-ID: <8807130234.AA03099@hpak>
Date: 13 Jul 88 02:34:24 GMT
Sender: daemon@bloom-beacon.MIT.EDU
Reply-To: dpb%frame%LOCAL@sun.com
Organization: The Internet
Lines: 141

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 
#endif  /* SYS_V */

#include 
#include 
#include 

#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;
	}
    }
}