[net.sources] enabling/disabling login lines

paul@vcvax1.UUCP (paul) (08/14/85)

The following is a shell-script called "ttystate",
which is used to enable and disable tty lines.
It is standardly provided with our VENIX System V system.
It works only on systems based on UNIX System V, as
it relies heavily on the System V "telinit" functionality.
A manual page is also provided.

To install, extract the lines following the cut-line below;
run the resulting file through /bin/sh.  The result
will be a shell script called ttystate.sh, which should
be moved to /etc/ttystate, and a manual page called
ttystate.man.

Comments/enhancements are welcome.

Paul Kleppner
VenturCom, Inc.
{harvard,mit-eddie}!cybvax0!vcvax1!paul

----------------------------- CUT HERE -----------------------------
cat > ttystate.sh <<\END-OF-FILE
:
# @(#)ttystate.sh	2.1
#
# Courtesy of VenturCom, Inc.
#	      215 First Street
#	      Cambridge, MA 02142
#	      617/661-1230
#
# For UNIX System V-compatible systems only.

SPEED= FFLAG= KFLAG= MODE= ECOM1= ECOM2=
USAGE="Usage: ttystate [-edfk] [-s speed] line"
PATH=/bin:/etc:/usr/bin

fatal(){
	echo "ttystate: $@" >&2
	exit 1
}

set -- `getopt edfks: $* 2>&-`
if [ $? != 0 ]
then
	echo "$USAGE" >&2
	exit 1
fi
for ARG
do
	case $ARG in
	-e) MODE=on; shift ;;
	-d) MODE=off; shift ;;
	-s) SPEED=$2; shift 2 ;;
	-f) FFLAG=yes; shift ;;
	-k) KFLAG=yes; shift ;;
	--) shift ; break ;;
	esac
done
if [ ! "$1" -o  $# != 1 ]
then
	fatal "$USAGE"
fi
LINE=$1
case $LINE in
	/dev/*) LINE=`basename $LINE` ;;
esac
if [ ! -c "/dev/$LINE" ]
then
	fatal "no such tty (/dev/$LINE)"
fi
TTYFIND="getty.*$LINE[ 	]"	#requires that LINE be followed by space/tab
IENTRY=`grep "$TTYFIND" /etc/inittab`
if [ ! "$IENTRY" ]
then
	fatal "tty name ($LINE) not in /etc/inittab"
fi
set -- `who -ul | grep "[A-z]* *$LINE"` ""
USER=$1
PID=$7
if [ ! "$MODE$SPEED" ]
then
	case $USER in
	   "") echo "$LINE is off" ;;
	LOGIN) echo "$LINE is active (waiting for login)" ;;
	    *) echo "$LINE is active ($USER logged in)" ;;
	esac
	exit 0
fi
if [ "$MODE" = "off" -a "$FFLAG" != yes -a "$USER" -a "$USER" != LOGIN ]
then
	fatal "can't turn off $LINE: $USER logged in"
fi
IFS=":"
set -- $IENTRY
case "$MODE:$3" in
     on:off) ECOM1="/$TTYFIND/s/:off:/:respawn:/" ;;
off:respawn) ECOM1="/$TTYFIND/s/:respawn:/:off:/" ;;
 on:respawn) fatal "$LINE already enabled."; ;;
    off:off) fatal "$LINE already disabled."; ;;
	  *) fatal "Illegal run state ($3) for $LINE in /etc/inittab"; ;;
esac
if [ "$SPEED" -a `grep -c "^$SPEED#" /etc/gettydefs` -eq 0 ]
then
	fatal "speed label ($SPEED) not in /etc/gettydefs"
fi
if [ "$SPEED" ]
then
	ECOM2="/$TTYFIND/s/\($TTYFIND *\)[A-Za-z0-9]*/\1$SPEED/"
else
	ECOM2="ka"
fi
ed - /etc/inittab <<!
H
$ECOM1
$ECOM2
w
q
!
if [ $? != 0 ]
then
	fatal "error editing /etc/inittab"
fi
if [ "$MODE" = off ]
then
	chmod 666 /dev/$LINE
	if [ "$KFLAG" -a "$PID" ]
	then
		kill -9 $PID
	fi
fi
telinit q
END-OF-FILE
cat > ttystate.man <<\END-OF-FILE



     TTYSTATE(1M)      (Courtesy of VenturCom, Inc.)      TTYSTATE(1M)



     NAME
          ttystate - turn on and off terminal login process

     SYNOPSIS
          /etc/ttystate [ -edfk ] [ [ -s speed ] ttyname

     DESCRIPTION
          ttystate enables and disables login processes on  the  named
          tty.  Lines with login processes on them may not be used for
          dial-out purposes (e.g. by cu or uucp(1)), and  vice  versa,
          so  this command is useful for temporarily changing from one
          use to another.

          ttyname is the name of a device node in /dev.  The name  may
          be  given as either the full device name (e.g. ``/dev/com1")
          or simply its last element (``com1'').

          The flags available are:

          -e   Enable a login process on the device.

          -d   Disable a login process on the  device.   In  addition,
               the  device  mode  is set to 666 (i.e., full read/write
               access).

          -sspeed
               Sets the baud rate entry for the device to speed,  e.g.
               1200,  4800,  or  9600.  The given rate will be stored,
               but will only  become  effective  when  the  device  is
               changed  from  a  disabled to enabled state.  This will
               not change the baud rate of a device that  already  has
               an active login process.  speed is any argument that is
               valid as the speed to getty(1m).

          -f   When used with the -d  flag,  -f  disables  the  device
               login even if someone is currently logged in.  If -f is
               not given, ttystate refuses to disable a device that is
               in active use.

          -k   After ttystate disables a login or user  process  on  a
               line, a grace period of approximately thirty seconds is
               given before it is forcibly terminated.  The -k flag in
               conjunction  with  -d  forces  an immediate kill of the
               process.

          ttystate works by editing the /etc/inittab file and  running
          ``telinit Q.'' An entry for the device must already exist in
          /etc/inittab,  set  to  ``off''   or   ``respawn''   states.
          ttystate  takes  any  speed argument given it and uses it to
          substitute the getty argument  in  the  /etc/inittab  entry.
          Thus   the   speed   must   be  an  existing  label  in  the
          /etc/gettydefs table.



                                   - 1 -






     TTYSTATE(1M)      (Courtesy of VenturCom, Inc.)      TTYSTATE(1M)



     EXAMPLES
          Disable com1 for login use:

               /etc/ttystate -d com1

          Enable com1 for login use at 1200 baud:

               /etc/ttystate -e -s 1200 com1

          Report the current setting for com1 device:

               /etc/ttystate com1


     FILES
          /etc/inittab
          /etc/gettydefs

     SEE ALSO
          init(1m), getty(1m), inittab(4), gettydefs(4)
          kill(1), ps(1), who(1)

     BUGS
          Won't prevent turning on a login process for a line that  is
          already in use for dial-out purposes.






























                                   - 2 -



END-OF-FILE