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 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
|
/* pty_termios.c - routines to allocate ptys - termios version
Written by: Don Libes, NIST, 2/6/90
This file is in the public domain. However, the author and NIST
would appreciate credit if you use this file or parts of it.
*/
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <pty.h>
#include <utmp.h>
#if defined(SIGCLD) && !defined(SIGCHLD)
#define SIGCHLD SIGCLD
#endif
#include "expect_cf.h"
#include <tcl.h>
#include "expect.h"
#include "exp_tty_in.h"
#include "exp_int.h" /* expErrnoMsg() prototype */
/*
The following functions are linked from the Tcl library. They
don't cause anything else in the library to be dragged in, so it
shouldn't cause any problems (e.g., bloat).
The functions are relatively small but painful enough that I don't care
to recode them. You may, if you absolutely want to get rid of any
vestiges of Tcl.
*/
extern char *TclGetRegError();
#if defined(HAVE_PTMX_BSD) && defined(HAVE_PTMX)
/*
* Some systems have both PTMX and PTMX_BSD.
* In fact, alphaev56-dec-osf4.0e has /dev/pts, /dev/pty, /dev/ptym,
* /dev/ptm, /dev/ptmx, and /dev/ptmx_bsd
* Suggestion from Martin Buchholz <martin@xemacs.org> is that BSD
* is usually deprecated and so should be here.
*/
#undef HAVE_PTMX_BSD
#endif
/* Linux and Digital systems can be configured to have both.
According to Ashley Pittman <ashley@ilo.dec.com>, Digital works better
with openpty which supports 4000 while ptmx supports 60. */
#if defined(HAVE_OPENPTY) && defined(HAVE_PTMX)
#undef HAVE_PTMX
#endif
#if defined(HAVE_PTYM) && defined(HAVE_PTMX)
/*
* HP-UX 10.0 with streams (optional) have both PTMX and PTYM. I don't
* know which is preferred but seeing as how the HP trap stuff is so
* unusual, it is probably safer to stick with the native HP pty support,
* too.
*/
#undef HAVE_PTMX
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifdef NO_STDLIB_H
#include "../compat/stdlib.h"
#else
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_SYSMACROS_H
#include <sys/sysmacros.h>
#endif
#ifdef HAVE_PTYTRAP
#include <sys/ptyio.h>
#endif
#include <sys/file.h>
#ifdef HAVE_SYS_FCNTL_H
# include <sys/fcntl.h>
#else
# include <fcntl.h>
#endif
#if defined(_SEQUENT_)
# include <sys/strpty.h>
#endif
#if defined(HAVE_PTMX) && defined(HAVE_STROPTS_H)
# include <sys/stropts.h>
#endif
#include "exp_win.h"
#include "exp_rename.h"
#include "exp_pty.h"
void expDiagLog();
void expDiagLogPtr();
#include <errno.h>
/*extern char *sys_errlist[];*/
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
/* Convex getpty is different than older-style getpty */
/* Convex getpty is really just a cover function that does the traversal */
/* across the domain of pty names. It makes no attempt to verify that */
/* they can actually be used. Indded, the logic in the man page is */
/* wrong because it will allow you to allocate ptys that your own account */
/* already has in use. */
#if defined(HAVE_GETPTY) && defined(CONVEX)
#undef HAVE_GETPTY
#define HAVE_CONVEX_GETPTY
extern char *getpty();
static char *master_name;
static char slave_name[] = "/dev/ptyXX";
static char *tty_bank; /* ptr to char [p-z] denoting
which bank it is */
static char *tty_num; /* ptr to char [0-f] denoting
which number it is */
#endif
#if defined(_SEQUENT_) && !defined(HAVE_PTMX)
/* old-style SEQUENT, new-style uses ptmx */
static char *master_name, *slave_name;
#endif /* _SEQUENT */
/* very old SGIs prefer _getpty over ptc */
#if defined(HAVE__GETPTY) && defined(HAVE_PTC) && !defined(HAVE_GETPTY)
#undef HAVE_PTC
#endif
#if defined(HAVE_PTC)
static char slave_name[] = "/dev/ttyqXXX";
/* some machines (e.g., SVR4.0 StarServer) have all of these and */
/* HAVE_PTC works best */
#undef HAVE_GETPTY
#undef HAVE__GETPTY
#endif
#if defined(HAVE__GETPTY) || defined(HAVE_PTC_PTS) || defined(HAVE_PTMX)
static char *slave_name;
#endif
#if defined(HAVE_GETPTY)
#include <sys/vty.h>
static char master_name[MAXPTYNAMELEN];
static char slave_name[MAXPTYNAMELEN];
#endif
#if !defined(HAVE_GETPTY) && !defined(HAVE__GETPTY) && !defined(HAVE_PTC) && !defined(HAVE_PTC_PTS) && !defined(HAVE_PTMX) && !defined(HAVE_CONVEX_GETPTY) && !defined(_SEQUENT_) && !defined(HAVE_SCO_CLIST_PTYS) && !defined(HAVE_OPENPTY)
#ifdef HAVE_PTYM
/* strange order and missing d is intentional */
static char banks[] = "pqrstuvwxyzabcefghijklo";
static char master_name[] = "/dev/ptym/ptyXXXX";
static char slave_name[] = "/dev/pty/ttyXXXX";
static char *slave_bank;
static char *slave_num;
#else
static char banks[] = "pqrstuvwxyzPQRSTUVWXYZ";
static char master_name[] = "/dev/ptyXX";
static char slave_name [] = "/dev/ttyXX";
#endif /* HAVE_PTYM */
static char *tty_type; /* ptr to char [pt] denoting
whether it is a pty or tty */
static char *tty_bank; /* ptr to char [p-z] denoting
which bank it is */
static char *tty_num; /* ptr to char [0-f] denoting
which number it is */
#endif
#if defined(HAVE_SCO_CLIST_PTYS)
# define MAXPTYNAMELEN 64
static char master_name[MAXPTYNAMELEN];
static char slave_name[MAXPTYNAMELEN];
#endif /* HAVE_SCO_CLIST_PTYS */
#ifdef HAVE_OPENPTY
static char master_name[64];
static char slave_name[64];
#endif
char *exp_pty_slave_name;
char *exp_pty_error;
#if 0
static void
pty_stty(s,name)
char *s; /* args to stty */
char *name; /* name of pty */
{
#define MAX_ARGLIST 10240
char buf[MAX_ARGLIST]; /* overkill is easier */
RETSIGTYPE (*old)(); /* save old sigalarm handler */
int pid;
old = signal(SIGCHLD, SIG_DFL);
switch (pid = fork()) {
case 0: /* child */
exec_stty(STTY_BIN,STTY_BIN,s);
break;
case -1: /* fail */
default: /* parent */
waitpid(pid);
break;
}
signal(SIGCHLD, old); /* restore signal handler */
}
exec_stty(s)
char *s;
{
char *args[50];
char *cp;
int argi = 0;
int quoting = FALSE;
int in_token = FALSE; /* TRUE if we are reading a token */
args[0] = cp = s;
while (*s) {
if (quoting) {
if (*s == '\\' && *(s+1) == '"') { /* quoted quote */
s++; /* get past " */
*cp++ = *s++;
} else if (*s == '\"') { /* close quote */
end_token
quoting = FALSE;
} else *cp++ = *s++; /* suck up anything */
} else if (*s == '\"') { /* open quote */
in_token = TRUE;
quoting = TRUE;
s++;
} else if (isspace(*s)) {
end_token
} else {
*cp++ = *s++;
in_token = TRUE;
}
}
end_token
args[argi] = (char *) 0; /* terminate argv */
execvp(args[0],args);
}
#endif /*0*/
static void
pty_stty(s,name)
char *s; /* args to stty */
char *name; /* name of pty */
{
#define MAX_ARGLIST 10240
char buf[MAX_ARGLIST]; /* overkill is easier */
RETSIGTYPE (*old)(); /* save old sigalarm handler */
#ifdef STTY_READS_STDOUT
sprintf(buf,"%s %s > %s",STTY_BIN,s,name);
#else
sprintf(buf,"%s %s < %s",STTY_BIN,s,name);
#endif
old = signal(SIGCHLD, SIG_DFL);
system(buf);
signal(SIGCHLD, old); /* restore signal handler */
}
int exp_dev_tty; /* file descriptor to /dev/tty or -1 if none */
static int knew_dev_tty;/* true if we had our hands on /dev/tty at any time */
exp_tty exp_tty_original;
#define GET_TTYTYPE 0
#define SET_TTYTYPE 1
static void
ttytype(request,fd,ttycopy,ttyinit,s)
int request;
int fd;
/* following are used only if request == SET_TTYTYPE */
int ttycopy; /* true/false, copy from /dev/tty */
int ttyinit; /* if true, initialize to sane state */
char *s; /* stty args */
{
if (request == GET_TTYTYPE) {
#ifdef HAVE_TCSETATTR
if (-1 == tcgetattr(fd, &exp_tty_original)) {
#else
if (-1 == ioctl(fd, TCGETS, (char *)&exp_tty_original)) {
#endif
knew_dev_tty = FALSE;
exp_dev_tty = -1;
}
exp_window_size_get(fd);
} else { /* type == SET_TTYTYPE */
if (ttycopy && knew_dev_tty) {
#ifdef HAVE_TCSETATTR
(void) tcsetattr(fd, TCSADRAIN, &exp_tty_current);
#else
(void) ioctl(fd, TCSETS, (char *)&exp_tty_current);
#endif
exp_window_size_set(fd);
}
#ifdef __CENTERLINE__
#undef DFLT_STTY
#define DFLT_STTY "sane"
#endif
/* Apollo Domain doesn't need this */
#ifdef DFLT_STTY
if (ttyinit) {
/* overlay parms originally supplied by Makefile */
/* As long as BSD stty insists on stdout == stderr, we can no longer write */
/* diagnostics to parent stderr, since stderr has is now child's */
/* Maybe someday they will fix stty? */
/* expDiagLogPtrStr("exp_getptyslave: (default) stty %s\n",DFLT_STTY);*/
pty_stty(DFLT_STTY,slave_name);
}
#endif
/* lastly, give user chance to override any terminal parms */
if (s) {
/* give user a chance to override any terminal parms */
/* expDiagLogPtrStr("exp_getptyslave: (user-requested) stty %s\n",s);*/
pty_stty(s,slave_name);
}
}
}
void
exp_init_pty()
{
#if !defined(HAVE_GETPTY) && !defined(HAVE__GETPTY) && !defined(HAVE_PTC) && !defined(HAVE_PTC_PTS) && !defined(HAVE_PTMX) && !defined(HAVE_CONVEX_GETPTY) && !defined(_SEQUENT_) && !defined(HAVE_SCO_CLIST_PTYS) && !defined(HAVE_OPENPTY)
#ifdef HAVE_PTYM
static char dummy;
tty_bank = &master_name[strlen("/dev/ptym/pty")];
tty_num = &master_name[strlen("/dev/ptym/ptyX")];
slave_bank = &slave_name[strlen("/dev/pty/tty")];
slave_num = &slave_name[strlen("/dev/pty/ttyX")];
#else
tty_bank = &master_name[strlen("/dev/pty")];
tty_num = &master_name[strlen("/dev/ptyp")];
tty_type = &slave_name[strlen("/dev/")];
#endif
#endif /* HAVE_PTYM */
exp_dev_tty = open("/dev/tty",O_RDWR);
knew_dev_tty = (exp_dev_tty != -1);
if (knew_dev_tty) ttytype(GET_TTYTYPE,exp_dev_tty,0,0,(char *)0);
}
#ifndef R_OK
/* 3b2 doesn't define these according to jthomas@nmsu.edu. */
#define R_OK 04
#define W_OK 02
#endif
static char * ttyname_checked(int fd) {
const char *result;
result= ttyname(fd);
if (!result) {
perror("expect: pty_termios: system configuration problem:"
" ttyname() failed");
exit(-1);
}
return result;
}
int
exp_getptymaster()
{
char *hex, *bank;
struct stat stat_buf;
int master = -1;
int slave = -1;
int num;
exp_pty_error = 0;
#define TEST_PTY 1
#if defined(HAVE_PTMX) || defined(HAVE_PTMX_BSD)
#undef TEST_PTY
#if defined(HAVE_PTMX_BSD)
if ((master = open("/dev/ptmx_bsd", O_RDWR)) == -1) return(-1);
#else
if ((master = open("/dev/ptmx", O_RDWR)) == -1) return(-1);
#endif
if ((slave_name = (char *)ptsname(master)) == NULL) {
close(master);
return(-1);
}
if (grantpt(master)) {
static char buf[500];
exp_pty_error = buf;
sprintf(exp_pty_error,"grantpt(%s) failed - likely reason is that your system administrator (in a rage of blind passion to rid the system of security holes) removed setuid from the utility used internally by grantpt to change pty permissions. Tell your system admin to reestablish setuid on the utility. Get the utility name by running Expect under truss or trace.", expErrnoMsg(errno));
close(master);
return(-1);
}
if (-1 == (int)unlockpt(master)) {
static char buf[500];
exp_pty_error = buf;
sprintf(exp_pty_error,"unlockpt(%s) failed.", expErrnoMsg(errno));
close(master);
return(-1);
}
#ifdef TIOCFLUSH
(void) ioctl(master,TIOCFLUSH,(char *)0);
#endif /* TIOCFLUSH */
exp_pty_slave_name = slave_name;
return(master);
#endif
#if defined(HAVE__GETPTY) /* SGI needs it this way */
#undef TEST_PTY
slave_name = _getpty(&master, O_RDWR, 0600, 0);
if (slave_name == NULL)
return (-1);
exp_pty_slave_name = slave_name;
return(master);
#endif
#if defined(HAVE_PTC) && !defined(HAVE__GETPTY) /* old SGI, version 3 */
#undef TEST_PTY
master = open("/dev/ptc", O_RDWR);
if (master >= 0) {
int ptynum;
if (fstat(master, &stat_buf) < 0) {
close(master);
return(-1);
}
ptynum = minor(stat_buf.st_rdev);
sprintf(slave_name,"/dev/ttyq%d",ptynum);
}
exp_pty_slave_name = slave_name;
return(master);
#endif
#if defined(HAVE_GETPTY) && !defined(HAVE__GETPTY)
#undef TEST_PTY
master = getpty(master_name, slave_name, O_RDWR);
/* is it really necessary to verify slave side is usable? */
exp_pty_slave_name = slave_name;
return master;
#endif
#if defined(HAVE_PTC_PTS)
#undef TEST_PTY
master = open("/dev/ptc",O_RDWR);
if (master >= 0) {
/* never fails */
slave_name = ttyname_checked(master);
}
exp_pty_slave_name = slave_name;
return(master);
#endif
#if defined(_SEQUENT_) && !defined(HAVE_PTMX)
#undef TEST_PTY
/* old-style SEQUENT, new-style uses ptmx */
master = getpseudotty(&slave_name, &master_name);
exp_pty_slave_name = slave_name;
return(master);
#endif /* _SEQUENT_ */
#if defined(HAVE_OPENPTY)
#undef TEST_PTY
if (openpty(&master, &slave, master_name, 0, 0) != 0) {
close(master);
close(slave);
return -1;
}
strcpy(slave_name, ttyname_checked(slave));
exp_pty_slave_name = slave_name;
close(slave);
return master;
#endif /* HAVE_OPENPTY */
#if defined(TEST_PTY)
/*
* all pty allocation mechanisms after this require testing
*/
if (exp_pty_test_start() == -1) return -1;
#if !defined(HAVE_CONVEX_GETPTY) && !defined(HAVE_PTYM) && !defined(HAVE_SCO_CLIST_PTYS)
for (bank = banks;*bank;bank++) {
*tty_bank = *bank;
*tty_num = '0';
if (stat(master_name, &stat_buf) < 0) break;
for (hex = "0123456789abcdef";*hex;hex++) {
*tty_num = *hex;
strcpy(slave_name,master_name);
*tty_type = 't';
master = exp_pty_test(master_name,slave_name,*tty_bank,tty_num);
if (master >= 0) goto done;
}
}
#endif
#ifdef HAVE_SCO_CLIST_PTYS
for (num = 0; ; num++) {
char num_str [16];
sprintf (num_str, "%d", num);
sprintf (master_name, "%s%s", "/dev/ptyp", num_str);
if (stat (master_name, &stat_buf) < 0)
break;
sprintf (slave_name, "%s%s", "/dev/ttyp", num_str);
master = exp_pty_test(master_name,slave_name,'0',num_str);
if (master >= 0)
goto done;
}
#endif
#ifdef HAVE_PTYM
/* systems with PTYM follow this idea:
/dev/ptym/pty[a-ce-z][0-9a-f] master pseudo terminals
/dev/pty/tty[a-ce-z][0-9a-f] slave pseudo terminals
/dev/ptym/pty[a-ce-z][0-9][0-9] master pseudo terminals
/dev/pty/tty[a-ce-z][0-9][0-9] slave pseudo terminals
SPPUX (Convex's HPUX compatible) follows the PTYM convention but
extends it:
/dev/ptym/pty[a-ce-z][0-9][0-9][0-9] master pseudo terminals
/dev/pty/tty[a-ce-z][0-9][0-9][0-9] slave pseudo terminals
The code does not distinguish between HPUX and SPPUX because there
is no reason to. HPUX will merely fail the extended SPPUX tests.
In fact, most SPPUX systems will fail simply because few systems
will actually have the extended ptys. However, the tests are
fast so it is no big deal.
*/
/*
* pty[a-ce-z][0-9a-f]
*/
for (bank = banks;*bank;bank++) {
*tty_bank = *bank;
sprintf(tty_num,"0");
if (stat(master_name, &stat_buf) < 0) break;
*(slave_num+1) = '\0';
for (hex = "0123456789abcdef";*hex;hex++) {
*tty_num = *hex;
*slave_bank = *tty_bank;
*slave_num = *tty_num;
master = exp_pty_test(master_name,slave_name,*tty_bank,tty_num);
if (master >= 0) goto done;
}
}
/*
* tty[p-za-ce-o][0-9][0-9]
*/
for (bank = banks;*bank;bank++) {
*tty_bank = *bank;
sprintf(tty_num,"00");
if (stat(master_name, &stat_buf) < 0) break;
for (num = 0; num<100; num++) {
*slave_bank = *tty_bank;
sprintf(tty_num,"%02d",num);
strcpy(slave_num,tty_num);
master = exp_pty_test(master_name,slave_name,*tty_bank,tty_num);
if (master >= 0) goto done;
}
}
/*
* tty[p-za-ce-o][0-9][0-9][0-9]
*/
for (bank = banks;*bank;bank++) {
*tty_bank = *bank;
sprintf(tty_num,"000");
if (stat(master_name, &stat_buf) < 0) break;
for (num = 0; num<1000; num++) {
*slave_bank = *tty_bank;
sprintf(tty_num,"%03d",num);
strcpy(slave_num,tty_num);
master = exp_pty_test(master_name,slave_name,*tty_bank,tty_num);
if (master >= 0) goto done;
}
}
#endif /* HAVE_PTYM */
#if defined(HAVE_CONVEX_GETPTY)
for (;;) {
if ((master_name = getpty()) == NULL) return -1;
strcpy(slave_name,master_name);
slave_name[5] = 't';/* /dev/ptyXY ==> /dev/ttyXY */
tty_bank = &slave_name[8];
tty_num = &slave_name[9];
master = exp_pty_test(master_name,slave_name,*tty_bank,tty_num);
if (master >= 0) goto done;
}
#endif
done:
exp_pty_test_end();
exp_pty_slave_name = slave_name;
return(master);
#endif /* defined(TEST_PTY) */
}
/* if slave is opened in a child, slave_control(1) must be executed after */
/* master is opened (when child is opened is irrelevent) */
/* if slave is opened in same proc as master, slave_control(1) must executed */
/* after slave is opened */
/*ARGSUSED*/
void
exp_slave_control(master,control)
int master;
int control; /* if 1, enable pty trapping of close/open/ioctl */
{
#ifdef HAVE_PTYTRAP
ioctl(master, TIOCTRAP, &control);
#endif /* HAVE_PTYTRAP */
}
int
exp_getptyslave(
int ttycopy,
int ttyinit,
CONST char *stty_args)
{
int slave, slave2;
char buf[10240];
if (0 > (slave = open(slave_name, O_RDWR))) {
static char buf[500];
exp_pty_error = buf;
sprintf(exp_pty_error,"open(%s,rw) = %d (%s)",slave_name,slave,expErrnoMsg(errno));
return(-1);
}
#if defined(HAVE_PTMX_BSD)
if (ioctl (slave, I_LOOK, buf) != 0)
if (ioctl (slave, I_PUSH, "ldterm")) {
expDiagLogPtrStrStr("ioctl(%d,I_PUSH,\"ldterm\") = %s\n",slave,expErrnoMsg(errno));
}
#else
#if defined(HAVE_PTMX)
if (ioctl(slave, I_PUSH, "ptem")) {
expDiagLogPtrStrStr("ioctl(%d,I_PUSH,\"ptem\") = %s\n",slave,expErrnoMsg(errno));
}
if (ioctl(slave, I_PUSH, "ldterm")) {
expDiagLogPtrStrStr("ioctl(%d,I_PUSH,\"ldterm\") = %s\n",slave,expErrnoMsg(errno));
}
if (ioctl(slave, I_PUSH, "ttcompat")) {
expDiagLogPtrStrStr("ioctl(%d,I_PUSH,\"ttcompat\") = %s\n",slave,expErrnoMsg(errno));
}
#endif
#endif
if (0 == slave) {
/* if opened in a new process, slave will be 0 (and */
/* ultimately, 1 and 2 as well) */
/* duplicate 0 onto 1 and 2 to prepare for stty */
fcntl(0,F_DUPFD,1);
fcntl(0,F_DUPFD,2);
}
ttytype(SET_TTYTYPE,slave,ttycopy,ttyinit,stty_args);
#if 0
#ifdef HAVE_PTYTRAP
/* do another open, to tell master that slave is done fiddling */
/* with pty and master does not have to wait to do further acks */
if (0 > (slave2 = open(slave_name, O_RDWR))) return(-1);
close(slave2);
#endif /* HAVE_PTYTRAP */
#endif
(void) exp_pty_unlock();
return(slave);
}
#ifdef HAVE_PTYTRAP
#include <sys/ptyio.h>
#include <sys/time.h>
/* This function attempts to deal with HP's pty interface. This
function simply returns an indication of what was trapped (or -1 for
failure), the parent deals with the details.
Originally, I tried to just trap open's but that is not enough. When
the pty is initialized, ioctl's are generated and if not trapped will
hang the child if no further trapping is done. (This could occur if
parent spawns a process and then immediatley does a close.) So
instead, the parent must trap the ioctl's. It probably suffices to
trap the write ioctl's (and tiocsctty which some hp's need) -
conceivably, stty could be smart enough not to do write's if the tty
settings are already correct. In that case, we'll have to rethink
this.
Suggestions from HP engineers encouraged. I cannot imagine how this
interface was intended to be used!
*/
int
exp_wait_for_slave_open(fd)
int fd;
{
fd_set excep;
struct timeval t;
struct request_info ioctl_info;
int rc;
int found = 0;
int maxfds = sysconf(_SC_OPEN_MAX);
t.tv_sec = 30; /* 30 seconds */
t.tv_usec = 0;
FD_ZERO(&excep);
FD_SET(fd,&excep);
rc = select(maxfds,
(SELECT_MASK_TYPE *)0,
(SELECT_MASK_TYPE *)0,
(SELECT_MASK_TYPE *)&excep,
&t);
if (rc != 1) {
expDiagLogPtrStr("spawned process never started: %s\r\n",expErrnoMsg(errno));
return(-1);
}
if (ioctl(fd,TIOCREQCHECK,&ioctl_info) < 0) {
expDiagLogPtrStr("ioctl(TIOCREQCHECK) failed: %s\r\n",expErrnoMsg(errno));
return(-1);
}
found = ioctl_info.request;
expDiagLogPtrX("trapped pty op = %x",found);
if (found == TIOCOPEN) {
expDiagLogPtr(" TIOCOPEN");
} else if (found == TIOCCLOSE) {
expDiagLogPtr(" TIOCCLOSE");
}
#ifdef TIOCSCTTY
if (found == TIOCSCTTY) {
expDiagLogPtr(" TIOCSCTTY");
}
#endif
if (found & IOC_IN) {
expDiagLogPtr(" IOC_IN (set)");
} else if (found & IOC_OUT) {
expDiagLogPtr(" IOC_OUT (get)");
}
expDiagLogPtr("\n");
if (ioctl(fd, TIOCREQSET, &ioctl_info) < 0) {
expDiagLogPtrStr("ioctl(TIOCREQSET) failed: %s\r\n",expErrnoMsg(errno));
return(-1);
}
return(found);
}
#endif
void
exp_pty_exit()
{
/* a stub so we can do weird things on the cray */
}
|