Interterminal Comumunications

utzoo!decvax!harpo!npois!houxi!ihnss!ihuxl!ignatz utzoo!decvax!harpo!npois!houxi!ihnss!ihuxl!ignatz
Fri May 7 12:09:18 AEST 1982


You have an interesting problem. You see, it's very simple to log only your
half of a terminal session. That is, you can do:

tee /dev/ttyXX >logfile

where ttyXX is the tty of the person you want to talk to. Now, any line you
enter will be logged in logfile, as well as going to the other person. However,
since they presumably will be writing to your device, as well, the characters
they write go directly onto the output queue for your device--i.e., your processnever sees them, and there's no way for you to trap them. However, a mechanism
is suggested by this: suppose you create a named pipe; issue the tee, and wait.
Meanwhile, the other person issues a background process which sucks data out
of the pipe, and then issues a tee to the SAME named pipe.
(Why not just both append to the same logfile, eg. "tee /dev/ttyXX >>logfile"?
UNIX buffering scotches the synchronization. The pipe write, however, is
atomic.) Of course, you have some whistles...

A fully working and (I believe) fully debugged shellscript implementation
follows...of course, if it were written in 'C', it would be more efficient.
But is it worth it?
-----------------------------------------------------------------------

#
# logwrite user  [ logpath ]
#
#	Dave Ihnat (AiC) 6 May 1982
#
#	This shellscript is available to any and all. Support, while likely
#	if politely requested, is neither implied nor guaranteed; and author
#	assumes no liability resulting from use of this program by others.
#
# 	Logwrite is used in exactly the same manner as write(1). It will
#	create a full log of the transactions in either the file `logpath`,
#	or in the current initiator's directory. Note that only the initiator
#	receives a copy of the transaction.
#

LOGPATH=logfile
LOGFILE=logfile

if [ $# -le 0 ]
then
	echo "Usage: logwrite user [logpath]"
	exit 1;
fi

USER=$1
shift;

if [ $# -gt 0 ]
then
     LOGPATH=$1
     LOGFILE=`basename $LOGPATH`
fi;


if [ "$LOGNAME" = "$USER" ]
then echo "You can't talk to yourself, unless you're schizophrenic..."
     exit 1;
fi

USERTTY=`who | fgrep $USER | tr -s " " " " | cut -f2 -d" "`
if [ ! "$USERTTY" ]
then echo "User $USER is not logged on.";
     exit 1;
fi;

MYTTY=`echo $LOGTTY | cut -f3 -d/`

if [ ! -w /dev/$USERTTY ]
then echo "You cannot talk to this user; permissions not set.";
     exit 2;
else echo "Logwrite from $LOGNAME..." >/dev/$USERTTY;
fi

if [ ! -p /usr/tmp/LOG$USERTTY -a ! -p /usr/tmp/LOG$MYTTY ]
then				# Initiator...make pipe, and hang on it.
	
	trap "rm -f /usr/tmp/LOG$USERTTY 2>/dev/null;\
	      rm -f /usr/tmp/Log$MYTTY 2>/dev/null;\
	      exit" 2 3;

	/etc/mknod /usr/tmp/LOG$USERTTY p
	chmod a+rw /usr/tmp/LOG$USERTTY
	tee /dev/$USERTTY >/usr/tmp/LOG$USERTTY
	echo "EOF" >/dev/$USERTTY;

	while [ -s /usr/tmp/LCK$USERTTY ]
	do
		:
	done;

	cp /usr/tmp/Log$USERTTY $LOGPATH 2>/dev/null;
	if [ $? ]
	then
		echo "Unable to copy to $LOGPATH...trying $HOME/$LOGFILE..."
		cp /usr/tmp/Log$USERTTY $HOME/$LOGFILE 2>/dev/null;
		if [ $? ]
		then
			echo "Unable to copy to $HOME/$LOGFILE...trying $HOME/logfile..."
			cp /usr/tmp/Log$USERTTY $HOME/logfile 2>/dev/null;
			if [ $? ]
			then
				echo "Unable to copy to logfile"
				echo "Log of transaction may be found in /usr/tmp/Log$USERTTY."
				exit 1;
			else
				echo "File in $HOME/logfile."
			fi;
		else
			echo "File in $HOME/$LOGFILE."
		fi;
	fi;
	rm -f /usr/tmp/Log$USERTTY;
	rm -f /usr/tmp/LOG$USERTTY
	exit;
else				# Recipient...
	(exec 0</usr/tmp/LOG$MYTTY;
	 echo "here..." >/usr/tmp/LCK$MYTTY;
	 while read line;
	 do
		echo "$line" >>/usr/tmp/Log$MYTTY;
	 done;
	 chmod a+rw /usr/tmp/Log$MYTTY;
	 rm -f /usr/tmp/LCK$MYTTY )&
	tee /dev/$USERTTY >/usr/tmp/LOG$MYTTY
	echo "EOF" >/dev/$USERTTY;
fi;
# ***** End of Shellscript Ver. 1.0 *****

---------------------------------------------------------------------

This is surprising...as a money-grubbing capitalistic consultant, I don't
often give away freebies like this. Maybe I need a drink...

				Dave Ihnat
				Analysts International Corporation
				Bell Telephone Laboratories
				Indian Hill, IL
				ihuxl!ignatz
				(312) 979-6747




More information about the Comp.unix.wizards mailing list