RSH (remote shell) and waiting on program termination - SUMMARY

Seth Robertson seth at ctr.columbia.edu
Wed Dec 20 03:09:08 AEST 1989


I had the same problem under SunOS 4.0 (just closing stdin/out/err didn't
work) so I wrote a small program to close everything.  The major part of
the program is this:

  for (fd = 0; fd < getdtablesize(); fd++)
    close (fd);

everything else is just support and other things which are nice but
not necessary.

Here is the complete program:
[Usage:  rsh -n host unrsh rprocess]

#! /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:  /home/sirius/seth/c/unrsh.c
# Wrapped by seth at sirius on Tue Dec 19 11:04:17 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f '/home/sirius/seth/c/unrsh.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'/home/sirius/seth/c/unrsh.c'\"
else
echo shar: Extracting \"'/home/sirius/seth/c/unrsh.c'\" \(1750 characters\)
sed "s/^X//" >'/home/sirius/seth/c/unrsh.c' <<'END_OF_FILE'
X/*
X** Program to unrshfy a process
X**
X** (if a process was started by rsh, this will make the rsh stuff die)
X**
X** This program has been tested under SunOS4.0
X**
X** This program has been placed in the public domain.  There are no
X** restrictions on its distribution, and no one is responsible for
X** anything that might occur do to the use of this program.
X*/
X
X/* I probably don't need all of these... */
X#include <stdio.h>
X#include <ctype.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <sys/file.h>
X#include <time.h>
X#include <errno.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
Xmain(argc,argv,envp)
X
Xint argc;
Xchar **argv;
Xchar **envp;
X
X{
X  int	pid;			/* Process id of child */
X  register int fd;		/* File desc. */
X  int 	err = 0;		/* Error variable */
X
X				/* bail on a controlling tty, if any */
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  /* The following command will get rid of the stupid in.rshd
X   * process
X   */
X				/* close all files */
X  for (fd = 0; fd < getdtablesize(); fd++)
X    close (fd);
X
X				/* Open stdin/out/err */
X  err = (err << 1) | (open ("/dev/null", O_RDONLY) == -1);
X  err = (err << 1) | (open ("/dev/null", O_WRONLY) == -1);
X  err = (err << 1) | (open ("/dev/null", O_WRONLY) == -1);
X
X				/* detach from process group */
X  err = (err << 1) | (setpgrp (0, (pid = getpid ())) == -1);
X  
X  
X				/* check for errors before running program */
X  err <<= 1;
X
X  if (err)			/* Run the program if no error */
X    exit(1);			/* Not much we can do */
X
X  argv++;
X
X  execvp(*argv, argv);
X
X  perror("s: execvp");
X  exit(-1);
X}
END_OF_FILE
if test 1750 -ne `wc -c <'/home/sirius/seth/c/unrsh.c'`; then
    echo shar: \"'/home/sirius/seth/c/unrsh.c'\" unpacked with wrong size!
fi
# end of '/home/sirius/seth/c/unrsh.c'
fi
echo shar: End of shell archive.
exit 0

                                        -Seth Robertson
                                         seth at ctr.columbia.edu



More information about the Comp.unix.wizards mailing list