Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!husc6!sri-unix!ctnews!pyramid!uccba!hal!ncoast!allbery From: allbery@ncoast.UUCP Newsgroups: comp.sources.misc Subject: program to force commands on another terminal (BSD only) Message-ID: <8705291811.AA23508@ubvax.ub.com> Date: Fri, 29-May-87 14:11:32 EDT Article-I.D.: ubvax.8705291811.AA23508 Posted: Fri May 29 14:11:32 1987 Date-Received: Thu, 4-Jun-87 01:49:21 EDT Lines: 144 Approved: allbery@ncoast.UUCP This program lets a super-user enter commands or data on a terminal other than his own. Handy for logging people off -- just force ttyB4 logout instead of 'ps auxt' and 'kill -HUP'. Also more fun :-). It is known to run on 4.3 BSD; it will definitely run on 4.2 BSD; it may run on V7 or SIII or even S5 -- basically, if you have TIOCSTI, you can use this. I found this program in our public sources directory; it has no authorship in the comments, which means it probably belongs to ATT :-), but it's simple and short enough that it looks like it was done locally -- just way, way before my time. It was called 'type.c', but because it matches the function of a program in my old Alpha-Micro days called "force", I call it that here. The usage message thinks it's called "type", so you can call it that, too, if you want. Actually I don't care what you call it. ----------------------------cut here for force.c--------------------------- #include #include #include #include #include /* * type - make the system think that a command was entered on * another user's terminal. * * type [-n] * * is the line that the command should be entered on. * examples: "/dev/ttyj4" ; "ttyj4" ; "j4" * * is the command that should be entered on that terminal. * examples: "logout" ; "jobs > /dev/console" * * a newline will be appended to unless type is given a "-n" * option. * * type can only be used on the terminal you are on, unless you are the * superuser, in which case you can send commands to any terminal. */ static int fd; /* file descriptor for terminal line */ extern int wakeup(); /* routine to wakeup if open of terminal line hangs */ main(argc, argv, environ) int argc; char *argv[], *environ[]; { int flag_n; /* do not append newline to */ if ( argc <= 1 ) { usage(); } if ( argv[1][0] == '-' ) { switch ( argv[1][1] ) { case 'n' : flag_n++; break; case '-' : break; default: usage(); break; } argc--, argv++; } if ( argc < 3 ) { usage(); } if ( index( argv[1], '/' ) == NULL ) { char terminal[MAXPATHLEN+1]; /* * since a full pathname was not given, assume the * file is in the "/dev" directory, and if only 2 * characters were given, assume that means a "tty". */ strcpy( terminal, "/dev/" ); if (strlen(argv[1]) == 2) strcat( terminal, "tty" ); argv[1] = strcat( terminal, argv[1] ); } /* * O_NDELAY does not work (?), so set a timer to * wake up if the open hangs. */ signal( SIGALRM, wakeup ); alarm( 30 ); if ( (fd = open(argv[1], O_WRONLY|O_NDELAY, 0)) < 0 ) { perror( "open" ); exit( 2 ); } else { char *cp = argv[2]; alarm( 0 ); while ( *cp != '\0' ) { send( *cp ); cp++; } if ( ! flag_n ) send( '\n' ); } } usage() { fprintf( stderr, "usage: type [-n] \n" ); exit( 1 ); } send( c ) char c; { register int status; status = ioctl( fd, TIOCSTI, &c ); if ( status != 0 ) { perror( "ioctl" ); exit( 3 ); } return status; } wakeup() { fprintf( stderr, "open hung\n" ); exit( 4 ); } -----------------------------------cut here-------------------------------- -- Brandon S. Allbery {decvax,cbatt,cbosgd}!cwruecmp!ncoast!allbery Tridelta Industries {ames,mit-eddie,talcott}!necntc!ncoast!allbery 7350 Corporate Blvd. necntc!ncoast!allbery@harvard.HARVARD.EDU Mentor, OH 44060 +01 216 255 1080 (also eddie.MIT.EDU)