[unix-pc.sources] A replacement for setgetty

jbm@uncle.UUCP (John B. Milton) (11/05/88)

Well, here it is. Read the source. Let me know.

#! /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:  sg.c
# Wrapped by jbm@uncle on Fri Nov  4 23:00:15 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'sg.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'sg.c'\"
else
echo shar: Extracting \"'sg.c'\" \(3071 characters\)
sed "s/^X//" >'sg.c' <<'END_OF_FILE'
X/* vi:set ts=2 sw=2:
XThis is a replacement for the UNIXpc program "setgetty". This program is run
Xby the files /usr/bin/geto{n|ff}.sh, which are run by the dial(3) library call
Xto get the getty off a tty line when dialing out, and to put it back when done.
XThat's all well and good, except it screws up sometimes and just sits there
Xwith the getty offm chewing up CPU and NOT turning the getty back on. I'm not
Xentirely sure why it gets stuck. I think it has something to do with the fact
Xthat it's looking at the /etc/utmp file to decide if there is currectly a user
Xor login process on the line. This program does not check the utmp file. This
Xis what it DOES do:
X
X1. Open /etc/inittab r/w
X2. Read the whole thing into a buffer in one call
X3. Finds the first occurance of the right label
X3. Sets the first character to a space or colon
X4. If a change was done, it writes the inittab back out in one call.
X
XYou may ask "What, documentation, what is this?". Well, this program, like
Xsetgetty has to have the executable owned by and setuid root:
X-rwsr-xr-x  1 root    bin        2156 Jan  1  1970 /usr/bin/setgetty
X
XInstalltion:
X1. cc -cO -o sg.o sg.c
X2. ld -s -o sg /lib/crt0s.o /lib/shlib.ifile sg.o
X3. chown root sg
X4. chmod 4755 sg
X5. mv /usr/bin/setgetty /usr/bin/osetgetty
X6. mv sg /usr/bin/setgetty
X
XFor some reason, cu and uucico through dial(3) don't run getoff.sh when the
Xprogram running on the line is not a LOGIN process. Hmm, beats me. As always,
Xlet me know if you have any problems: John B. Milton, jbm@uncle
X*/
X
X#include <fcntl.h>
X#include <stdio.h>
X#include <string.h>
X
X#define BUFSIZE 4096
X
Xextern int errno;
X
Xvoid qperrorf(format,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10)
Xchar *format,*a1,*a2,*a3,*a4,*a5,*a6,*a7,*a8,*a9;
X{
X	char line[200];
X	int curerrno=errno;
X	sprintf(line,format,a1,a2,a3,a4,a5,a6,a7,a8,a9);
X	errno=curerrno;
X	perror(line);
X	exit(curerrno);
X}
X
Xstatic char *usage="Usage: %s xxx 0|1\n";
Xstatic char *it="/etc/inittab";
X
Xint main(argc,argv)
Xint argc;
Xchar *argv[];
X{
X	int f,len;
X	char look,new,*s,buf[BUFSIZE];
X
X	if (argc!=3 || strlen(argv[1])!=3 || (argv[2][0]!='0' && argv[2][0]!='1')) {
X		fprintf(stderr,usage,argv[0]);
X		exit(0);
X	}
X	if ((f=open(it,O_RDWR))==-1)
X		qperrorf("%s: open r/w %s",argv[0],it);
X	if ((len=read(f,buf,BUFSIZE))==-1)
X		qperrorf("%s: read %s",argv[0],it);
X	if (len==BUFSIZE) {
X		fprintf(stderr,"%s: error, BUFSIZE too small\n",argv[0]);
X		exit(3); /* new status, setgetty didn't have this one */
X	}
X	if (argv[2][0]-'0') { look=':'; new=' '; } else { look=' '; new=':'; }
X	for (s=buf; s<buf+BUFSIZE-20; s++)
X		if (*s=='\n' && (*(s+1)==' ' || *(s+1)==':') && strncmp(s+2,argv[1],3)==0) {
X			if (*(s+1)!=new) {
X				*(s+1)=new;
X				lseek(f,0,0);
X				if ((len=write(f,buf,len))==-1)
X					qperrorf("%s: re-write %s",argv[0],it);
X				if (close(f)==-1)
X					qperrorf("%s: close %s",argv[0],it);
X				kill(1,1); /* very quick telinit Q */
X			}
X			if (new==' ')
X				exit(0); /* dial(3) doesn't care about turning the getty on */
X			else
X				exit(2); /* this is success, believe it or not */
X		}
X	exit(1); /* line not found */
X}
END_OF_FILE
if test 3071 -ne `wc -c <'sg.c'`; then
    echo shar: \"'sg.c'\" unpacked with wrong size!
fi
# end of 'sg.c'
fi
echo shar: End of shell archive.
exit 0

John
-- 
John Bly Milton IV, jbm@uncle.UUCP, n8emr!uncle!jbm@osu-cis.cis.ohio-state.edu
(614) h:294-4823, w:764-4272;  MS-DOS is a beautiful flower that smells bad!