ptysh2-sysv.c 5 KB
Newer Older
O'Reilly Media, Inc. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
/*
ptysh.c  "Pseudo Terminal Shell"  v2
"Using & Managing PPP," O'Reilly & Associates, March 1999
http://www.oreilly.com/


This example is for UNIX System V variants (Sun Microsystem's Solaris 2.6).
Compile with "cc -o ptysh ptysh.c"


Author: Andrew Sun
Revision: January 10, 1998
Revision: July 13, 1999, Link dynamic pty to user specified symbolic link
Revision: July 15, 1999, Trap handle SIGINT only if initially set to default


Description:

This program enables the /bin/sh command interpreter to
emulate a "terminal device".  Tip, cu, the UUCP environment,
and PPP normally interact with serial interfaces via terminal
device files, such as /dev/cua/b.  Rather than limiting these
applications to physical serial interfaces, "ptysh" enables them
to interact with the shell (/bin/sh), and with TCP/IP communication
programs, including telnet and rlogin.


Usage:

Once ptysh is active, ptysh maintains a user specified symbolic
link to a pseudo terminal.  Applications accessing the symbolic
link as a serial device will interact with /bin/sh.  At this point,
an application's chat script can "exec telnet," "exec rlogin,"
or invoke an alternate utility to establish communications over a network.
*/

#include <stdio.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stropts.h>
#include <poll.h>

#include <signal.h>

char *ptsname(int fildes);
char *ptyalias;

void exitreq(sig)
int sig;
{
	unlink(ptyalias);
	exit(0);
}

main(argc,argv)
int argc;
char *argv[];
{

int shellpid;

char iobuf12[2000], iobuf21[2000];
char *iobuf12p, *iobuf21p;
int iofdcnt, iowrcnt, iordcnt12, iordcnt21;

int fd1m, fd1s, fd2m, fd2s;
char *slavename;
struct stat statbuf;

struct pollfd fds[2];
unsigned long nfds;
int timeout;

void (*sigvold)();

/* Command line options */
if (argc < 2 ) {
	fprintf(stderr, "Usage: %s /dev/ptyalias\n", argv[0]);
	exit(1);
}
ptyalias = argv[1];

/* Pty alias file must not already exist */
if ( lstat(ptyalias, &statbuf) >= 0) {
	fprintf(stderr, "File %s exists\n", ptyalias);
	exit(1);
}

/* Set signal handling */
sigvold = signal(SIGINT, SIG_IGN);
if ( sigvold == SIG_DFL ) {
	signal(SIGINT, exitreq);
}
signal(SIGTERM, exitreq);
signal(SIGHUP, SIG_IGN);

for (;;) {

/* Create serial interface ptys */
if ( (fd1m = open("/dev/ptmx", O_RDWR)) < 0 ) {
	perror("open serial pty master"); exit(1);
}
	grantpt(fd1m); unlockpt(fd1m);
if ( (slavename = ptsname(fd1m)) == NULL ) {
	perror("can't determine serial pty"); exit(1);
}
	/* printf("serial pty: %s\n", slavename); */
if ( (fd1s = open(slavename,O_RDWR)) < 0 ) {
	perror("open serial pty slave"); exit(1);
}
	ioctl(fd1s, I_PUSH, "ptem");
	ioctl(fd1s, I_PUSH, "ldterm");

/* Symlink pty slave */
if (symlink(slavename, ptyalias) < 0 ) {
	perror("symlink"); exit(1);
}

/* Detect "open" for serial pty slave */
fds[0].fd = fd1m;
fds[0].events = POLLIN;
nfds = 1; timeout = -1;
poll(fds, nfds, timeout);
close (fd1s);	/* Application has serial pty slave now */

/* Create shell ptys */
if ( (fd2m = open("/dev/ptmx", O_RDWR)) < 0 ) {
	perror("open shell pty master"); exit(1);
}
	grantpt(fd2m); unlockpt(fd2m);
if ( (slavename = ptsname(fd2m)) == NULL ) {
	perror("can't determine shell pty"); exit(1);
}
	/* printf("shell pty: %s\n", slavename); */

/* Invoke /bin/sh as a subprocess */
if (! (shellpid = fork()) ) {
	close (fd1m);
	close (fd2m);
	setsid();
	if ( (fd2s = open(slavename,O_RDWR)) < 0 ) {
		perror("open shell pty slave"); exit(1);
	}
	ioctl(fd2s, I_PUSH, "ptem");
	ioctl(fd2s, I_PUSH, "ldterm");
	dup2 (fd2s, 0);		/* stdin */
	dup2 (fd2s, 1);		/* stdout */
	dup2 (fd2s, 2);		/* stderr */
	system ("stty sane");
	execl ("/bin/sh", "sh", (char *) 0);
}

iordcnt12 = 0;	/* I/O read character count, fd1m to fd2m */
iordcnt21 = 0;	/* I/O read character count, fd2m to fd1m */

for (;;) {

fds[0].fd = fd1m; fds[1].fd = fd2m;
fds[0].events = fds[1].events = 0;
nfds = 2; timeout = -1;
if (iordcnt12) {
	fds[1].events |= POLLOUT;
} else {
	fds[0].events |= POLLIN;
}
if (iordcnt21) {
	fds[0].events |= POLLOUT;
} else {
	fds[1].events |= POLLIN;
}

iofdcnt = poll(fds, nfds, timeout);
if ( iofdcnt < 0 )
	if ( errno == EINTR ) continue;
	else {
		perror("poll"); exit(1);
	}

if ( fds[0].revents & POLLIN ) {

	/* Read from fd1m data to be written to fd2m */
	iobuf12p = iobuf12;
	iordcnt12 = read (fd1m, iobuf12, 2000);
	if ( iordcnt12 <= 0 ) break;
}
if ( fds[0].revents & POLLOUT ) {

	/* Write to fd1m with data read from fd2m */
	iowrcnt = write (fd1m, iobuf21p, iordcnt21);
	if ( iowrcnt <= 0 ) break;
	iordcnt21 -= iowrcnt;
	iobuf21p += iowrcnt;
}
if ( fds[1].revents & POLLIN ) {

	/* Read from fd2m data to be written to fd1m */
	iobuf21p = iobuf21;
	iordcnt21 = read (fd2m, iobuf21, 2000);
	if ( iordcnt21 <= 0 ) break;
}
if ( fds[1].revents & POLLOUT ) {

	/* Write to fd2m with data read from fd1m */
	iowrcnt = write (fd2m, iobuf12p, iordcnt12);
	if ( iowrcnt <= 0 ) break;
	iordcnt12 -= iowrcnt;
	iobuf12p += iowrcnt;
}

}
close (fd1m);
close (fd2m);
if (unlink(ptyalias) < 0) {
	perror("unlink"); exit(1);
}
sleep(3);	/* Delay for pty close */
wait(NULL);
}

}