dbgif.c 17.5 KB
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#ifdef __sgi__
#include <bstring.h>
#endif
#include <getopt.h>
#include <termio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/prctl.h>
#include <signal.h>
#include <rklog.h>
#include <assert.h>

#include "dbgif.h"
#include "rkfsn.h"
#include "dbgproto.h"
#include "sys/u64gio.h"
#include "ramrom.h"
#include "driverd.h"

#define PRLOGFILE	"/usr/tmp/dbgif.netprf"
#define CONSLOGFILE	"/usr/tmp/dbgif.console"
#define DEFAULT_TTY	"/dev/ttyd2"
#define DEFAULT_MASTER_PORT	5777
#define DEFAULT_SETTOP_PORT	5778
#ifdef __GNUC__
#define offsetof(s_type,field) ((size_t)&(((s_type*)0)->field))
#endif

/*
 * the maximum packet size is the maximum request size
 * plus the size of the the header that proceeds it.
 */
#define TV_DBG_MAX_PACKET_SIZE	1236992 /* 302x4096 bytes */

static char* tty = DEFAULT_TTY;
static int master_port = DEFAULT_MASTER_PORT;
static int settop_port = DEFAULT_SETTOP_PORT;
static struct termios ttyState;
static pid_t child=0;

/* XXX delay count used to slow serial output so don't overrun settop */
int slowsend = 22000;

int usetcp;
int ttyFD;
int consolelog;
int verbose;
char* progName;

FILE *fp = NULL;	/* debug log file */
FILE *prfp;		/* file to write user printfs */
FILE *consfp;		/* file to log console output */
FILE *tvlogfp = NULL;	/* file to flush tvLog events */

int st_debugFD = 0;	/* TCP socket to communicate with settop */
int ts_sockFD = -1;	/* TCP socket to accept cnxns from teleshop clients */
int st_remoteFD = -1;	/* TCP socket used to accept connections from settop */
int currentFD = -1;     /* TCP socket to communicate with teleshop client */
int masterFD = -1;	/* where to send exceptions, and exit messages */
int useFileForTTY = 1;	/* sends packets to socket file for debugging */
int useBProtocol = 1;	/* if set, do not encode for 7-bit tty xfers */
int reality = 1;	/* if set, use Project Reality defaults */
int useEmulatorIF = 0;	/* if set, use emulator interface via shared memory */
int useUltra64IF = 1;	/* if set, use real ultra64 hardware interface */
int emupid;		/* the PID of the emulator, for sending it signals */

int debug = 0; /* =1 => trace requests and events */

char	tvLogFileName[] = "/usr/tmp/tvlogXXXXXX";
char	*tvLogUserFileName = NULL;

extern int stOpenTCPconnection (int cvd_port);
extern int stSetupTCPconnection (int settop_port);
extern int stAcceptConnection (int settop_port);
extern int stHandleRequestFromClient (int);
extern void prEvent (KKHeader *);
extern void netServe (void *);


unsigned char *tmpReadBuf;

static void usage(char* progName)
{
    fprintf(stderr, "Usage: %s [-t ttyname | -e key | -u] [-v] [-z] [-a | -b] [-p portnum] [-h]\n",
	    progName);

    fprintf(stderr,
	    "  -e key pid     emulator interface, use key for shared memory (requires -k & -b)\n");
    fprintf(stderr,
	    "  -t tty     set tty port to use; %s is the default\n",
	    DEFAULT_TTY);
    fprintf(stderr,
	    "  -u         ultra64 hardware interface (requires -k & -b) [default]\n");
    fprintf(stderr,
	    "  -v         verbose; show all packets sent/received\n");
    fprintf(stderr,
	    "  -p port    set master port socket; %d is the default\n",
	    DEFAULT_MASTER_PORT);
    fprintf(stderr,
	    "  -a         use ASCII (7-bit) mode for tty data\n");
    fprintf(stderr,
	    "  -b         use binary (8-bit) mode for tty data [default]\n");
    fprintf(stderr,
	    "  -d         trace requests and events\n");
    fprintf(stderr,
	    "  -z         Use Z8530 SCC serial interface instead of socket\n");
    fprintf(stderr,
	    "  -n         access remote using a tcp connection on port %d\n",
	    DEFAULT_SETTOP_PORT);
    if ( debug )	/* describe debug commands */
    {
    	fprintf(stderr,
	    	"  -c         enable console logging\n");
    	fprintf(stderr,
	    	"  -k         use (socket) file instead of tty [default]\n");
    	fprintf(stderr,
	    	"  -s del     enable slow send, del = delay count\n");
    }
    fprintf(stderr,
	    "  -h         print this help info\n");
}

void perror2(char* reason)
{
    fprintf(stderr, "%s: ", progName);
    perror(reason);
    fprintf(stderr, "\r");
    exit(-1);
}

static char* ctoa(int c)
{
    static char buf[8];
    char* bp = buf;

    if (c & 0x80) {
	*bp++ = 'M';
	*bp++ = '-';
	c &= 0x7f;
    }
    if (c == 0x7f) {
	*bp++ = '^';
	*bp++ = '?';
    }
    if (c < 32) {
	*bp++ = '^';
	*bp++ = c + '@';
    } else {
	*bp++ = c;
    }
    *bp++ = 0;
    return buf;
}

static void ResetTTY(int fd, struct termios *old)
{
    printf("ResetTTY ioctl call\n");
    if (ioctl(fd, TCSETA, old) < 0) {
	perror2("ioctl");
    }
}

static void SetupTTY(int fd, struct termios* old)
{
    struct termios term;

    printf("SetupTTY ioctl call\n");
    if (ioctl(fd, TCGETA, old) < 0) {
	perror2("TCGETA: ioctl");
    }
    term.c_iflag = IGNBRK;
    term.c_oflag = 0;
    term.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
    term.c_lflag = 0;
    term.c_cc[VMIN] = 1;
    term.c_cc[VTIME] = 0;
    printf("second SetupTTY ioctl call\n");
    if (ioctl(fd, TCSETA, &term) < 0) {
	perror2("TCSETA: ioctl");
    }
}

static char ioctlWriteBuffer[2048];
static char * ioctlWritePtr = ioctlWriteBuffer;

/*
 * Code assumes that read data comes in response to another request
 * driver inforces that count from game is not greater than the limit
 */
int ioctlRead( unsigned char ** buffer)
{
    int             count,bytesRead;
    unsigned char   readInt[4],*tbuf;

    bytesRead = 0;
    while(bytesRead < 4)
	bytesRead += read(ttyFD,&readInt[bytesRead],4-bytesRead);

    count = *((int*)&readInt[0]);
    
    tbuf = (unsigned char*)malloc(count);

    bytesRead = 0;
    while(bytesRead < count)
	bytesRead += read(ttyFD, &tbuf[bytesRead],count-bytesRead);

    *buffer = tbuf;
    return (count);
}

void exitWithParent(int sig, int code, struct sigcontext *sc)
{
    /* parent was killed or exited (or we were sent a SIGHUP)
     * exit quietly */
    _exit(0);
}

void writeLogData(KKFlushEvent *event)
{
    TVLogHeader		header;
    TVLogEntryHeader	entry;
    int			amount;
    int			len;
    int			i;

    amount = event->header.length - offsetof(KKFlushEvent, buffer);

    /* assume buffer size is at least 1MB. */
    if (tvlogfp == NULL) {
	
	/* try to open user specified file first */
	if (tvLogUserFileName) {
	    tvlogfp = fopen(tvLogUserFileName, "w+");
	    if (tvlogfp == NULL) {
		if (fp) {
		    fprintf(fp, "Cannot open log file %s\n", tvLogUserFileName);
		    fflush(fp);
		}
	    }
	}	

	/* otherwise use temp file */
	if (tvlogfp == NULL) {
	    /* 
	     * Setup temp file name in the format that mktemp wants.
	     * mktemp change it to a unique filename after the call.
	     */
	    len = strlen(tvLogFileName);
	    for (i = len - 6; i < len; i++) {
		tvLogFileName[i] = 'X';
	    }
    
	    tvlogfp = fopen(mktemp(tvLogFileName), "w+");
	    if (tvlogfp == NULL) {
		if (fp) {
		    fprintf(fp, "Cannot open log file %s\n", tvLogFileName);
		    fflush(fp);
		}
		return;
	    }
	}

	/* write header information */
	header.magic = TV_LOG_MAGIC_NUMBER;
	header.version = TV_LOG_VERSION;
	if (fwrite(&header, sizeof(TVLogHeader), 1, tvlogfp) != 1) {
	    if (fp) {
		fprintf(fp, "Failed to write to log file %s\n", tvLogFileName);
		fflush(fp);
	    }
	}
    }

    if (fwrite(event->buffer, amount, 1, tvlogfp) != 1) {
	if (fp) {
	    fprintf(fp, "Failed to write to log file %s\n", tvLogFileName);
	    fflush(fp);
	}
    }
    fflush(tvlogfp);
}

void netServe (void *unused)
{
    fd_set r, w;
    int nfds, rv, recvd;
    static char netBuf[TV_DBG_MAX_PACKET_SIZE];
    KKHeader *event;

    /* Determine max fd */
    nfds = 1;
    if (st_remoteFD >= nfds) {
	nfds = st_remoteFD + 1;
    }
    if (st_debugFD >= nfds) {
	nfds = st_remoteFD + 1;
    }

    signal(SIGHUP, exitWithParent);
#ifdef __sgi__
    prctl(PR_TERMCHILD);
#endif

    while (1) {
	FD_ZERO (&r);
        FD_SET (st_remoteFD, &r);
	if (st_debugFD) {
            FD_SET (st_debugFD, &r);
	}
	rv = select (nfds, &r, 0, 0, 0);
	if (rv < 0)
	    perror2 ("dbgif: netServe select: ");
	
        if (FD_ISSET (st_remoteFD, &r)) {
	    struct sockaddr_in from;
	    int fromlen;
	    /*
	     * connection requests from settop to dbgif
	     */
	    fromlen = sizeof from;
	    if (st_debugFD) {
		close (st_debugFD);
		st_debugFD = 0;
	    }
	    st_debugFD = accept(st_remoteFD, &from, &fromlen);
	    if (st_debugFD <= 0) {
		perror("dbgif: accept");
		printf("accept connection failed %d\n\r", st_debugFD);
		st_debugFD = 0;
	    }
	    else {
		printf("dbgif: Connected to %s on sock %d\n\r",
		       inet_ntoa(from.sin_addr), st_debugFD);
    		if (st_debugFD >= nfds)
		    nfds = st_debugFD + 1;
	    }
	}
	else if ((st_debugFD) && (FD_ISSET (st_debugFD, &r))) {
	    bzero(netBuf, 128);
	    recvd = netRead(st_debugFD, netBuf,
			    TV_DBG_MAX_PACKET_SIZE);
	    if (recvd == 0) {
		fprintf (stderr, "recvd = 0\n");
		st_debugFD = 0;
		continue;
	    }
	    if (recvd < 0) {
		/* got an error */
		/* wait for a new connection */
		fprintf (stderr,"dbgif: network receive failed: ");
		close (st_debugFD);
		st_debugFD = 0;
		continue;
	    }

	    event = (KKHeader *)netBuf;
	    if (debug) {
		prEvent(event);
	    }
	    
	    if (event->type == _KK_CONSOLE) {
		writeLogData((KKFlushEvent *)event);
		continue;
	    }

	    if (recvd) {
		unsigned int bytes_remain, amount, i;
		int sendexception = 0;
		KKHeader *event = (KKHeader *)netBuf;

		bytes_remain = event->length;
		i = 0;
		if (event->type == _KK_PROCESS_EXIT ||
		    event->type == _KK_THREAD_EXIT ||
		    event->type == _KK_EXCEPTION) {
		    sendexception = 1;
		}
		while (bytes_remain) {
		    amount = (bytes_remain > TV_DBG_MAXREQ_SIZE) ?
			TV_DBG_MAXREQ_SIZE : bytes_remain;
		    /*
		     * Note, 0 is a valid file descriptor but it isn't
		     * interesting to send debugger messages to it.
		     */
		    if (sendexception == 1) {
		        if (masterFD != -1) {
		            if (send(masterFD, (void *)&netBuf[i], amount, 0) ==
			    	-1) {
			        fprintf(stderr, "dbgif: currentFD %d\n\r",
			    	     currentFD);
			        perror("dbgif: send");
			        fprintf(stderr, "\r");
			    }
		        } 
		        goto readmore;
		    }
		    if (currentFD > 0) {
		        if (send(currentFD, (void *)&netBuf[i], amount, 0) ==
				-1) {
			    fprintf(stderr, "dbgif: currentFD %d\n\r",
				     currentFD);
			    perror("dbgif: send");
			    fprintf(stderr, "\r");
			}
		    }
		readmore:
		    bytes_remain -= amount;
		    i += amount;
		}
	    }
	}
    }
}

static void serve(void)
{
    fd_set r, w;
    int nfds, rv, i;
    static int connections[FD_SETSIZE];

    unsigned char *dPtr;

    /* Initialize connections array */
    bzero (connections, FD_SETSIZE * sizeof (int));
    
    /* Determine max fd */
    nfds = 1;
    if (ttyFD >= nfds) {
	nfds = ttyFD + 1;
    }
    if (ts_sockFD >= nfds) {
	nfds = ts_sockFD + 1;
    }

    /* loop forever */
    for (;;) 
    {
	FD_ZERO (&r);
	FD_SET (ttyFD, &r);
	FD_SET (ts_sockFD, &r);
	for (i = 0; i < nfds; i++) {
	    if (connections[i]) {
		FD_SET (i, &r);
	    }
	}
	rv = select(nfds, &r, 0, 0, 0);
	if (rv < 0)  /* this would be an error */
	{
	    for (i = 0; i < nfds; i++) 
	    {
		FD_ZERO (&r);
		if (connections[i]) 
		{
		    FD_SET (i, &r);
		    rv = select (nfds, &r, 0, 0, 0);
		    if (rv < 0) 
		    {
			close (i);
			connections[i] = 0;
			break;
		    }
		}
	    }
	}

	if (FD_ISSET (ttyFD, &r)) 
	{
	    int recvd;
	    unsigned char   *readBuf;
	    recvd = ioctlRead (&readBuf);
	    if (recvd) 
	    {
		KKHeader *event = (KKHeader *)readBuf;
			    
		/* if there isn't a valid receiver, drop it on the floor */
		if (currentFD > 0) 
		{
                    if (send(currentFD, event, event->length, 0) == -1) 
		    {
			printf("Failure sending message\n");
		    }
	 	}
		else
		    printf("Dropping message\n");

		free(readBuf);
	    }

	} else if (FD_ISSET (ts_sockFD, &r)) {
	    struct sockaddr from;
	    int fromlen = sizeof (from);
	    int newsockFD = accept (ts_sockFD, &from, &fromlen);

	    if (newsockFD == -1) {
		perror2 ("accept");
	    }
	    connections[newsockFD] = 1;
	    if (newsockFD >= nfds) {
		nfds = newsockFD + 1;
	    }
	} else {
            KKHeader *request;
	    extern KKHeader *stReadRequestPacket(int);

	    for (i = 0; i < nfds; i++) {
		if ((connections[i])&& (FD_ISSET (i, &r))) {
		    /*
		     * requests from tvd to dbgif,
		     * forward to settop via tcp if avialable, else serial port
		     */
		    request = stReadRequestPacket(i);

		    if (request == NULL) {
			close (i);
			connections[i] = 0;
			if (currentFD == masterFD) {
			    masterFD = -1;
			}
			currentFD = -1;
			break;
		    }

		    currentFD = i;

		    /* mark the master client FD for exception events */
		    if (request->error == 1) {
			masterFD = currentFD;
		    }
		    (void) stDispatchRequest(request);
		    break;
		}
	    }
	}
    }
}

void main(int argc, char** argv)
{
    int o;
    char* t;
    FILE *tfp;

    progName = argv[0];
    t = strrchr(progName, '/');
    if (t) {
	progName = t + 1;
    }
    while ((o = getopt(argc, argv, "e:t:uvp:dnhcs:kbraz")) >= 0) {
	switch (o) {
#ifdef __sgi__
	  case GETOPTHUH:
#else
	  case '?':
#endif
	    usage(progName);
	    exit(-1);
	    break;
	  case 's':
	    slowsend = atoi(optarg);
            break;
	  case 'e':
	    emupid = atoi(optarg);
	    if ( !emupid )
	    {
		fprintf( stderr, "PID %s not valid\n", argv[optind] );
		usage(progName);
		exit(-1);
	    }
	    useEmulatorIF = 1;
		useUltra64IF = 0;
	    break;
	  case 'u':
	    useUltra64IF = 1;
		useEmulatorIF = 0;
	    break;
	  case 't':
	    tty = strdup(optarg);
	    useUltra64IF = 0;
		useEmulatorIF = 0;
	    break;
	  case 'v':
	    verbose = 1;
	    break;
	  case 'p':
	    master_port = atoi(optarg);
	    break;
	  case 'n':
	    usetcp = 1;
	    break;
	  case 'c':
	    consolelog = 1;
		printf( "Console logging enabled\n" );
	    break;
	  case 'd':
	    debug++;
	    fp = fopen ("/usr/tmp/dbgif.log", "w+");
	    if (fp == NULL) {
		printf("debug logging not possible\n");
		perror("couldn't open /usr/tmp/dbgif.log");
	    }
	    else
	    {
		printf( "Debug logging enabled\n" );
		chmod( "/usr/tmp/dbgif.log", 0666 );
	    }
	    break;
	  case 'k':
		printf( "Socket connection is the default; use -z to turn it off\n" );
	    break;
	  case 'z':
		useFileForTTY = 0;
		printf( "Using Z8530 SCC interface\n" );
		break;
	  case 'a':
		useBProtocol = 0;
		printf( "B protocol suppressed\n" );
		break;
	  case 'b':
		printf( "B protocol is the default; use -a to turn it off\n" );
	    break;
	  case 'r':
	    break;
	  case 'h':
	    usage(progName);
	    exit(0);
	    break;
	}
    }
    if ( (useEmulatorIF && (!useFileForTTY || !useBProtocol)) ||
	    (useUltra64IF && (!useFileForTTY || !useBProtocol)) ){
	usage(progName);
	exit(0);
    }


    if ( fp )
	fprintf( fp, "%s created 95.04.27\n\n", progName );
    /* open a log file for console output */
    if (consolelog) {
	tfp = fopen(CONSLOGFILE, "w+");
	if (tfp != NULL)
	    consfp = tfp;
	else
	    printf("dbgif: couldn't open %s\n", CONSLOGFILE);
    }

    /* Open up tty, normally connected to console on settop */

    if ( useFileForTTY )	/* then ttyFD is the socket */
    {
	if ( useEmulatorIF )
	{
	    int i, reply;
	    RegisterSession session;
    
	    ttyFD = emopen("/dev/u64gio");
	    if (ttyFD == -1) {
		fprintf(stderr, "dbgif: Unable to connect to driverd\n");
		exit(1);
	    }

	    session.dd_header = DRIVERD_REGISTER_SESSION;
	    session.id =  ((id_t)getpid());
	    for (i=0; i < 8; i++) {
		write(ttyFD, &session, sizeof(session));
		read(ttyFD, &reply, sizeof(reply));
		if (reply == 1)
		    break;
		sleep(1);
	    }
	    if (i == 8) {
		fprintf(stderr, "dbgif: Unable to register with driverd (no emulate?)\n");
		exit(1);
	    }
	    
	    emioctl(ttyFD, U64_LISTEN, HOST_DBG_CMD_READY);
	    emioctl(ttyFD, U64_LISTEN, HOST_DBG_DATA_ACK);
	    emioctl(ttyFD, U64_LISTEN, GAME_DBG_DATA_SEND);
	} else if ( useUltra64IF ) 
	{
	    ttyFD = open(DEV_U64_DEBUG, O_RDWR);
	    if (ttyFD == -1) {
		fprintf(stderr, "dbgif: Unable to open %s ", DEV_U64_DEBUG);
		perror("");
		exit(1);
	    }
	}
	else
	{
	    struct sockaddr sockName;
    
	    ttyFD = socket( AF_UNIX, SOCK_STREAM, 0 );
	    if (ttyFD < 0)	/* forget it */
		perror2("socket");
	    if ( fp )
		fprintf( fp, "socket open as device %d\n", ttyFD );
	    sockName.sa_family = AF_UNIX;
	    strcpy( sockName.sa_data, tty );
    
	    /* try to connect to the monitor socket */
    
	    for ( ; ; )
	    {
		if ( connect( ttyFD, &sockName, sizeof( struct sockaddr ) ) < 0 )
		{
		    fprintf( stderr, "dbgif: Trying to connect to rmon server\n" );
		    sleep( 5 );
		}
		else
		    break;
	    }
	    if ( fp )
		fprintf( fp, "connected to socket %s\n", tty );
	}
    }
    else	/* it's a real tty */
    {
	ttyFD = open(tty, O_RDWR);
	if (ttyFD < 0) {
	    perror2("open");
	}
    }


    /* Open up TCP socket to master */
    ts_sockFD = stOpenTCPconnection(master_port);

    /* Setup for a connection via tcp to the settop */
    if (usetcp)
        st_remoteFD = stSetupTCPconnection(settop_port);

    if ( !useFileForTTY )
    	SetupTTY(ttyFD, &ttyState);

#ifdef __sgi__
    if (usetcp)
	    child = sproc (netServe, PR_SALL, 0);
#else
    if (usetcp) {
	    fprintf(stderr, "dbgif: sproc replacement not implemented, exiting ...\n");
	    exit(1);
    }
#endif
    serve();

    printf("-- dbgif interrupted, exit\r\n");

    if (child > 0)
	kill(child, SIGHUP);
    if ( !useFileForTTY )
    	ResetTTY(ttyFD, &ttyState);

    close(ttyFD);
    close(ts_sockFD);
    close(currentFD);

    if (usetcp)
	close(st_remoteFD);

    printf("Done!\n");
}