Xref: utzoo comp.unix.wizards:19836 alt.sources:1327 Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!seth From: seth@ctr.columbia.edu (Seth Robertson) Newsgroups: comp.unix.wizards,alt.sources Subject: Re: RSH (remote shell) and waiting on program termination - SUMMARY Summary: unrsh SunOS 4.0 Message-ID: <1989Dec19.160908.21962@ctr.columbia.edu> Date: 19 Dec 89 16:09:08 GMT References: <1035@anasaz.UUCP> <1051@anasaz.UUCP> Reply-To: seth@ctr.columbia.edu (Seth Robertson) Organization: Columbia University Center for Telecommunications Research Lines: 114 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 '/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 X#include X#include X#include X#include X#include X#include X#include X#include X#include X#include X#include 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@ctr.columbia.edu