rsh backgrounding

Brian Gregory bgregory at megatest.UUCP
Thu Jan 19 10:36:28 AEST 1989


Here is a small program that solves the rsh backgrounding problem for me.
This version runs on BSD.  Essentially, it detaches a child process from
its controlling environment.  You may find that some of the detachment
steps may be more than needed, but they're all here for completeness.

To solve the specific problem, try

rsh <machine> daemon <cmd> [args]

this will return immediately and rsh will exit, leaving only the detached
process running on the remote machine.

Enjoy,
  Brian.

...!sun!megatest!bgregory

----8<--------8<--------8<--------8<--------8<--------8<--------8<--------8<---
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  daemon.c
# Wrapped by bgregory at hal9000 on Wed Jan 18 15:35:58 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f daemon.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"daemon.c\"
else
echo shar: Extracting \"daemon.c\" \(1911 characters\)
sed "s/^X//" >daemon.c <<'END_OF_daemon.c'
X#include	<stdio.h>
X#include	<fcntl.h>
X#include	<signal.h>
X#include	<sys/file.h>
X#include	<sys/ioctl.h>
X#include	<sys/param.h>
X
X
X/*ARGSUSED*/
Xmain (argc, argv, envp)
Xint argc;
Xchar *argv[];
Xchar *envp[];
X
X{
X
X  extern int errno;
X  extern char *sys_errlist[];
X  int pid;
X
X  /* check for a program to run */
X  if (argc < 2)
X  {
X    (void) fprintf (stderr, "usage: %s <cmd> [args]\n", argv[0]);
X    exit (2);
X  }
X
X  switch (pid = fork ())
X  {
X
X    /* failed */ case -1:
X      perror (argv[0]);
X      exit (1);
X      break;
X
X    /* child */ case 0:
X      {
X	int fd;
X	int err = 0;
X
X	errno = 0;
X
X	/* no terminal-related job control signals */
X#ifdef SIGTTOU
X	err = (err << 1) | ((int) signal (SIGTTOU, SIG_IGN) == -1);
X#endif
X#ifdef SIGTTIN
X	err = (err << 1) | ((int) signal (SIGTTIN, SIG_IGN) == -1);
X#endif
X#ifdef SIGTSTP
X	err = (err << 1) | ((int) signal (SIGTSTP, SIG_IGN) == -1);
X#endif
X
X	/* detach from process group */
X	err = (err << 1) | (setpgrp (0, (pid = getpid ())) == -1);
X
X	/* bail on a controlling tty */
X	if ((fd = open ("/dev/tty", O_RDWR)) >= 0)
X	{
X	  err = (err << 1) | (ioctl (fd, TIOCNOTTY, (char *) 0) == -1);
X	  err = (err << 1) | (close (fd) == -1);
X	}
X
X	/* close all files */
X	for (fd = 0; fd < getdtablesize (); fd++)
X	  (void) close (fd);
X
X	/* move off a possibly mounted file system */
X	err = (err << 1) | (chdir ("/") == -1);
X
X	/* no inherited umask */
X	err = (err << 1) | (umask (0) == -1);
X
X	/* check for errors before running program */
X	err <<= 1;
X	if (err)
X	  goto derr;
X
X	/* run the program */
X	err |= (execvp (argv[1], &argv[1]) == -1);
X
X	/* process error */
X    derr:
X	fd = errno;
X	*stderr = *fopen ("/dev/console", "w");
X	(void) fprintf (stderr, "%s \"%s\" (%d): 0x%x, last %d (%s)\n",
X			argv[0], argv[1], pid, err, fd, sys_errlist[fd]);
X	exit (1);
X
X      }
X      break;
X
X    /* parent */ default:
X      (void) fprintf (stderr, "[%d]\n", pid);
X      exit (0);
X      break;
X
X  }
X}
END_OF_daemon.c
if test 1911 -ne `wc -c <daemon.c`; then
    echo shar: \"daemon.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0



More information about the Comp.unix.wizards mailing list