1 /* pty_termios.c - routines to allocate ptys - termios version
3 Written by: Don Libes, NIST, 2/6/90
5 This file is in the public domain. However, the author and NIST
6 would appreciate credit if you use this file or parts of it.
13 #if defined(SIGCLD) && !defined(SIGCHLD)
14 #define SIGCHLD SIGCLD
17 #include "expect_cf.h"
20 The following functions are linked from the Tcl library. They
21 don't cause anything else in the library to be dragged in, so it
22 shouldn't cause any problems (e.g., bloat).
24 The functions are relatively small but painful enough that I don't care
25 to recode them. You may, if you absolutely want to get rid of any
28 extern char *TclGetRegError();
30 #if defined(HAVE_PTMX_BSD) && defined(HAVE_PTMX)
32 * Some systems have both PTMX and PTMX_BSD.
33 * In fact, alphaev56-dec-osf4.0e has /dev/pts, /dev/pty, /dev/ptym,
34 * /dev/ptm, /dev/ptmx, and /dev/ptmx_bsd
35 * Suggestion from Martin Buchholz <martin@xemacs.org> is that BSD
36 * is usually deprecated and so should be here.
41 /* Linux and Digital systems can be configured to have both.
42 According to Ashley Pittman <ashley@ilo.dec.com>, Digital works better
43 with openpty which supports 4000 while ptmx supports 60. */
44 #if defined(HAVE_OPENPTY) && defined(HAVE_PTMX)
48 #if defined(HAVE_PTYM) && defined(HAVE_PTMX)
50 * HP-UX 10.0 with streams (optional) have both PTMX and PTYM. I don't
51 * know which is preferred but seeing as how the HP trap stuff is so
52 * unusual, it is probably safer to stick with the native HP pty support,
61 #ifdef HAVE_INTTYPES_H
62 # include <inttypes.h>
64 #include <sys/types.h>
68 #include "../compat/stdlib.h"
76 #ifdef HAVE_SYSMACROS_H
77 #include <sys/sysmacros.h>
81 #include <sys/ptyio.h>
86 #ifdef HAVE_SYS_FCNTL_H
87 # include <sys/fcntl.h>
92 #if defined(_SEQUENT_)
93 # include <sys/strpty.h>
96 #if defined(HAVE_PTMX) && defined(HAVE_STROPTS_H)
97 # include <sys/stropts.h>
102 #include "exp_tty_in.h"
103 #include "exp_rename.h"
107 void expDiagLogPtr();
110 /*extern char *sys_errlist[];*/
117 /* Convex getpty is different than older-style getpty */
118 /* Convex getpty is really just a cover function that does the traversal */
119 /* across the domain of pty names. It makes no attempt to verify that */
120 /* they can actually be used. Indded, the logic in the man page is */
121 /* wrong because it will allow you to allocate ptys that your own account */
122 /* already has in use. */
123 #if defined(HAVE_GETPTY) && defined(CONVEX)
125 #define HAVE_CONVEX_GETPTY
126 extern char *getpty();
127 static char *master_name;
128 static char slave_name[] = "/dev/ptyXX";
129 static char *tty_bank; /* ptr to char [p-z] denoting
131 static char *tty_num; /* ptr to char [0-f] denoting
132 which number it is */
135 #if defined(_SEQUENT_) && !defined(HAVE_PTMX)
136 /* old-style SEQUENT, new-style uses ptmx */
137 static char *master_name, *slave_name;
138 #endif /* _SEQUENT */
140 /* very old SGIs prefer _getpty over ptc */
141 #if defined(HAVE__GETPTY) && defined(HAVE_PTC) && !defined(HAVE_GETPTY)
145 #if defined(HAVE_PTC)
146 static char slave_name[] = "/dev/ttyqXXX";
147 /* some machines (e.g., SVR4.0 StarServer) have all of these and */
148 /* HAVE_PTC works best */
153 #if defined(HAVE__GETPTY) || defined(HAVE_PTC_PTS) || defined(HAVE_PTMX)
154 static char *slave_name;
157 #if defined(HAVE_GETPTY)
159 static char master_name[MAXPTYNAMELEN];
160 static char slave_name[MAXPTYNAMELEN];
163 #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)
165 /* strange order and missing d is intentional */
166 static char banks[] = "pqrstuvwxyzabcefghijklo";
167 static char master_name[] = "/dev/ptym/ptyXXXX";
168 static char slave_name[] = "/dev/pty/ttyXXXX";
169 static char *slave_bank;
170 static char *slave_num;
172 static char banks[] = "pqrstuvwxyzPQRSTUVWXYZ";
173 static char master_name[] = "/dev/ptyXX";
174 static char slave_name [] = "/dev/ttyXX";
175 #endif /* HAVE_PTYM */
177 static char *tty_type; /* ptr to char [pt] denoting
178 whether it is a pty or tty */
179 static char *tty_bank; /* ptr to char [p-z] denoting
181 static char *tty_num; /* ptr to char [0-f] denoting
182 which number it is */
185 #if defined(HAVE_SCO_CLIST_PTYS)
186 # define MAXPTYNAMELEN 64
187 static char master_name[MAXPTYNAMELEN];
188 static char slave_name[MAXPTYNAMELEN];
189 #endif /* HAVE_SCO_CLIST_PTYS */
192 static char master_name[64];
193 static char slave_name[64];
196 char *exp_pty_slave_name;
202 char *s; /* args to stty */
203 char *name; /* name of pty */
205 #define MAX_ARGLIST 10240
206 char buf[MAX_ARGLIST]; /* overkill is easier */
207 RETSIGTYPE (*old)(); /* save old sigalarm handler */
210 old = signal(SIGCHLD, SIG_DFL);
211 switch (pid = fork()) {
213 exec_stty(STTY_BIN,STTY_BIN,s);
216 default: /* parent */
221 signal(SIGCHLD, old); /* restore signal handler */
231 int in_token = FALSE; /* TRUE if we are reading a token */
236 if (*s == '\\' && *(s+1) == '"') { /* quoted quote */
237 s++; /* get past " */
239 } else if (*s == '\"') { /* close quote */
242 } else *cp++ = *s++; /* suck up anything */
243 } else if (*s == '\"') { /* open quote */
247 } else if (isspace(*s)) {
255 args[argi] = (char *) 0; /* terminate argv */
256 execvp(args[0],args);
262 char *s; /* args to stty */
263 char *name; /* name of pty */
265 #define MAX_ARGLIST 10240
266 char buf[MAX_ARGLIST]; /* overkill is easier */
267 RETSIGTYPE (*old)(); /* save old sigalarm handler */
269 #ifdef STTY_READS_STDOUT
270 sprintf(buf,"%s %s > %s",STTY_BIN,s,name);
272 sprintf(buf,"%s %s < %s",STTY_BIN,s,name);
274 old = signal(SIGCHLD, SIG_DFL);
276 signal(SIGCHLD, old); /* restore signal handler */
279 int exp_dev_tty; /* file descriptor to /dev/tty or -1 if none */
280 static int knew_dev_tty;/* true if we had our hands on /dev/tty at any time */
282 exp_tty exp_tty_original;
284 #define GET_TTYTYPE 0
285 #define SET_TTYTYPE 1
287 ttytype(request,fd,ttycopy,ttyinit,s)
290 /* following are used only if request == SET_TTYTYPE */
291 int ttycopy; /* true/false, copy from /dev/tty */
292 int ttyinit; /* if true, initialize to sane state */
293 char *s; /* stty args */
295 if (request == GET_TTYTYPE) {
296 #ifdef HAVE_TCSETATTR
297 if (-1 == tcgetattr(fd, &exp_tty_original)) {
299 if (-1 == ioctl(fd, TCGETS, (char *)&exp_tty_original)) {
301 knew_dev_tty = FALSE;
304 exp_window_size_get(fd);
305 } else { /* type == SET_TTYTYPE */
306 if (ttycopy && knew_dev_tty) {
307 #ifdef HAVE_TCSETATTR
308 (void) tcsetattr(fd, TCSADRAIN, &exp_tty_current);
310 (void) ioctl(fd, TCSETS, (char *)&exp_tty_current);
313 exp_window_size_set(fd);
316 #ifdef __CENTERLINE__
318 #define DFLT_STTY "sane"
321 /* Apollo Domain doesn't need this */
324 /* overlay parms originally supplied by Makefile */
325 /* As long as BSD stty insists on stdout == stderr, we can no longer write */
326 /* diagnostics to parent stderr, since stderr has is now child's */
327 /* Maybe someday they will fix stty? */
328 /* expDiagLogPtrStr("exp_getptyslave: (default) stty %s\n",DFLT_STTY);*/
329 pty_stty(DFLT_STTY,slave_name);
333 /* lastly, give user chance to override any terminal parms */
335 /* give user a chance to override any terminal parms */
336 /* expDiagLogPtrStr("exp_getptyslave: (user-requested) stty %s\n",s);*/
337 pty_stty(s,slave_name);
345 #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)
348 tty_bank = &master_name[strlen("/dev/ptym/pty")];
349 tty_num = &master_name[strlen("/dev/ptym/ptyX")];
350 slave_bank = &slave_name[strlen("/dev/pty/tty")];
351 slave_num = &slave_name[strlen("/dev/pty/ttyX")];
353 tty_bank = &master_name[strlen("/dev/pty")];
354 tty_num = &master_name[strlen("/dev/ptyp")];
355 tty_type = &slave_name[strlen("/dev/")];
358 #endif /* HAVE_PTYM */
361 exp_dev_tty = open("/dev/tty",O_RDWR);
362 knew_dev_tty = (exp_dev_tty != -1);
363 if (knew_dev_tty) ttytype(GET_TTYTYPE,exp_dev_tty,0,0,(char *)0);
367 /* 3b2 doesn't define these according to jthomas@nmsu.edu. */
376 struct stat stat_buf;
385 #if defined(HAVE_PTMX) || defined(HAVE_PTMX_BSD)
387 #if defined(HAVE_PTMX_BSD)
388 if ((master = open("/dev/ptmx_bsd", O_RDWR)) == -1) return(-1);
390 if ((master = open("/dev/ptmx", O_RDWR)) == -1) return(-1);
392 if ((slave_name = (char *)ptsname(master)) == NULL) {
396 if (grantpt(master)) {
397 static char buf[500];
399 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));
403 if (-1 == (int)unlockpt(master)) {
404 static char buf[500];
406 sprintf(exp_pty_error,"unlockpt(%s) failed.", expErrnoMsg(errno));
411 (void) ioctl(master,TIOCFLUSH,(char *)0);
412 #endif /* TIOCFLUSH */
414 exp_pty_slave_name = slave_name;
418 #if defined(HAVE__GETPTY) /* SGI needs it this way */
420 slave_name = _getpty(&master, O_RDWR, 0600, 0);
421 if (slave_name == NULL)
423 exp_pty_slave_name = slave_name;
427 #if defined(HAVE_PTC) && !defined(HAVE__GETPTY) /* old SGI, version 3 */
429 master = open("/dev/ptc", O_RDWR);
433 if (fstat(master, &stat_buf) < 0) {
437 ptynum = minor(stat_buf.st_rdev);
438 sprintf(slave_name,"/dev/ttyq%d",ptynum);
440 exp_pty_slave_name = slave_name;
444 #if defined(HAVE_GETPTY) && !defined(HAVE__GETPTY)
446 master = getpty(master_name, slave_name, O_RDWR);
447 /* is it really necessary to verify slave side is usable? */
448 exp_pty_slave_name = slave_name;
452 #if defined(HAVE_PTC_PTS)
454 master = open("/dev/ptc",O_RDWR);
457 slave_name = ttyname(master);
459 exp_pty_slave_name = slave_name;
463 #if defined(_SEQUENT_) && !defined(HAVE_PTMX)
465 /* old-style SEQUENT, new-style uses ptmx */
466 master = getpseudotty(&slave_name, &master_name);
467 exp_pty_slave_name = slave_name;
469 #endif /* _SEQUENT_ */
471 #if defined(HAVE_OPENPTY)
473 if (openpty(&master, &slave, master_name, 0, 0) != 0) {
478 strcpy(slave_name, ttyname(slave));
479 exp_pty_slave_name = slave_name;
482 #endif /* HAVE_OPENPTY */
484 #if defined(TEST_PTY)
486 * all pty allocation mechanisms after this require testing
488 if (exp_pty_test_start() == -1) return -1;
490 #if !defined(HAVE_CONVEX_GETPTY) && !defined(HAVE_PTYM) && !defined(HAVE_SCO_CLIST_PTYS)
491 for (bank = banks;*bank;bank++) {
494 if (stat(master_name, &stat_buf) < 0) break;
495 for (hex = "0123456789abcdef";*hex;hex++) {
497 strcpy(slave_name,master_name);
499 master = exp_pty_test(master_name,slave_name,*tty_bank,tty_num);
500 if (master >= 0) goto done;
505 #ifdef HAVE_SCO_CLIST_PTYS
506 for (num = 0; ; num++) {
509 sprintf (num_str, "%d", num);
510 sprintf (master_name, "%s%s", "/dev/ptyp", num_str);
511 if (stat (master_name, &stat_buf) < 0)
513 sprintf (slave_name, "%s%s", "/dev/ttyp", num_str);
515 master = exp_pty_test(master_name,slave_name,'0',num_str);
522 /* systems with PTYM follow this idea:
524 /dev/ptym/pty[a-ce-z][0-9a-f] master pseudo terminals
525 /dev/pty/tty[a-ce-z][0-9a-f] slave pseudo terminals
526 /dev/ptym/pty[a-ce-z][0-9][0-9] master pseudo terminals
527 /dev/pty/tty[a-ce-z][0-9][0-9] slave pseudo terminals
529 SPPUX (Convex's HPUX compatible) follows the PTYM convention but
532 /dev/ptym/pty[a-ce-z][0-9][0-9][0-9] master pseudo terminals
533 /dev/pty/tty[a-ce-z][0-9][0-9][0-9] slave pseudo terminals
535 The code does not distinguish between HPUX and SPPUX because there
536 is no reason to. HPUX will merely fail the extended SPPUX tests.
537 In fact, most SPPUX systems will fail simply because few systems
538 will actually have the extended ptys. However, the tests are
539 fast so it is no big deal.
543 * pty[a-ce-z][0-9a-f]
546 for (bank = banks;*bank;bank++) {
548 sprintf(tty_num,"0");
549 if (stat(master_name, &stat_buf) < 0) break;
550 *(slave_num+1) = '\0';
551 for (hex = "0123456789abcdef";*hex;hex++) {
553 *slave_bank = *tty_bank;
554 *slave_num = *tty_num;
555 master = exp_pty_test(master_name,slave_name,*tty_bank,tty_num);
556 if (master >= 0) goto done;
561 * tty[p-za-ce-o][0-9][0-9]
564 for (bank = banks;*bank;bank++) {
566 sprintf(tty_num,"00");
567 if (stat(master_name, &stat_buf) < 0) break;
568 for (num = 0; num<100; num++) {
569 *slave_bank = *tty_bank;
570 sprintf(tty_num,"%02d",num);
571 strcpy(slave_num,tty_num);
572 master = exp_pty_test(master_name,slave_name,*tty_bank,tty_num);
573 if (master >= 0) goto done;
578 * tty[p-za-ce-o][0-9][0-9][0-9]
580 for (bank = banks;*bank;bank++) {
582 sprintf(tty_num,"000");
583 if (stat(master_name, &stat_buf) < 0) break;
584 for (num = 0; num<1000; num++) {
585 *slave_bank = *tty_bank;
586 sprintf(tty_num,"%03d",num);
587 strcpy(slave_num,tty_num);
588 master = exp_pty_test(master_name,slave_name,*tty_bank,tty_num);
589 if (master >= 0) goto done;
593 #endif /* HAVE_PTYM */
595 #if defined(HAVE_CONVEX_GETPTY)
597 if ((master_name = getpty()) == NULL) return -1;
599 strcpy(slave_name,master_name);
600 slave_name[5] = 't';/* /dev/ptyXY ==> /dev/ttyXY */
602 tty_bank = &slave_name[8];
603 tty_num = &slave_name[9];
604 master = exp_pty_test(master_name,slave_name,*tty_bank,tty_num);
605 if (master >= 0) goto done;
611 exp_pty_slave_name = slave_name;
614 #endif /* defined(TEST_PTY) */
617 /* if slave is opened in a child, slave_control(1) must be executed after */
618 /* master is opened (when child is opened is irrelevent) */
619 /* if slave is opened in same proc as master, slave_control(1) must executed */
620 /* after slave is opened */
623 exp_slave_control(master,control)
625 int control; /* if 1, enable pty trapping of close/open/ioctl */
628 ioctl(master, TIOCTRAP, &control);
629 #endif /* HAVE_PTYTRAP */
636 CONST char *stty_args)
641 if (0 > (slave = open(slave_name, O_RDWR))) {
642 static char buf[500];
644 sprintf(exp_pty_error,"open(%s,rw) = %d (%s)",slave_name,slave,expErrnoMsg(errno));
648 #if defined(HAVE_PTMX_BSD)
649 if (ioctl (slave, I_LOOK, buf) != 0)
650 if (ioctl (slave, I_PUSH, "ldterm")) {
651 expDiagLogPtrStrStr("ioctl(%d,I_PUSH,\"ldterm\") = %s\n",slave,expErrnoMsg(errno));
654 #if defined(HAVE_PTMX)
655 if (ioctl(slave, I_PUSH, "ptem")) {
656 expDiagLogPtrStrStr("ioctl(%d,I_PUSH,\"ptem\") = %s\n",slave,expErrnoMsg(errno));
658 if (ioctl(slave, I_PUSH, "ldterm")) {
659 expDiagLogPtrStrStr("ioctl(%d,I_PUSH,\"ldterm\") = %s\n",slave,expErrnoMsg(errno));
661 if (ioctl(slave, I_PUSH, "ttcompat")) {
662 expDiagLogPtrStrStr("ioctl(%d,I_PUSH,\"ttcompat\") = %s\n",slave,expErrnoMsg(errno));
668 /* if opened in a new process, slave will be 0 (and */
669 /* ultimately, 1 and 2 as well) */
671 /* duplicate 0 onto 1 and 2 to prepare for stty */
676 ttytype(SET_TTYTYPE,slave,ttycopy,ttyinit,stty_args);
680 /* do another open, to tell master that slave is done fiddling */
681 /* with pty and master does not have to wait to do further acks */
682 if (0 > (slave2 = open(slave_name, O_RDWR))) return(-1);
684 #endif /* HAVE_PTYTRAP */
687 (void) exp_pty_unlock();
692 #include <sys/ptyio.h>
693 #include <sys/time.h>
695 /* This function attempts to deal with HP's pty interface. This
696 function simply returns an indication of what was trapped (or -1 for
697 failure), the parent deals with the details.
699 Originally, I tried to just trap open's but that is not enough. When
700 the pty is initialized, ioctl's are generated and if not trapped will
701 hang the child if no further trapping is done. (This could occur if
702 parent spawns a process and then immediatley does a close.) So
703 instead, the parent must trap the ioctl's. It probably suffices to
704 trap the write ioctl's (and tiocsctty which some hp's need) -
705 conceivably, stty could be smart enough not to do write's if the tty
706 settings are already correct. In that case, we'll have to rethink
709 Suggestions from HP engineers encouraged. I cannot imagine how this
710 interface was intended to be used!
715 exp_wait_for_slave_open(fd)
720 struct request_info ioctl_info;
724 int maxfds = sysconf(_SC_OPEN_MAX);
726 t.tv_sec = 30; /* 30 seconds */
733 (SELECT_MASK_TYPE *)0,
734 (SELECT_MASK_TYPE *)0,
735 (SELECT_MASK_TYPE *)&excep,
738 expDiagLogPtrStr("spawned process never started: %s\r\n",expErrnoMsg(errno));
741 if (ioctl(fd,TIOCREQCHECK,&ioctl_info) < 0) {
742 expDiagLogPtrStr("ioctl(TIOCREQCHECK) failed: %s\r\n",expErrnoMsg(errno));
746 found = ioctl_info.request;
748 expDiagLogPtrX("trapped pty op = %x",found);
749 if (found == TIOCOPEN) {
750 expDiagLogPtr(" TIOCOPEN");
751 } else if (found == TIOCCLOSE) {
752 expDiagLogPtr(" TIOCCLOSE");
756 if (found == TIOCSCTTY) {
757 expDiagLogPtr(" TIOCSCTTY");
761 if (found & IOC_IN) {
762 expDiagLogPtr(" IOC_IN (set)");
763 } else if (found & IOC_OUT) {
764 expDiagLogPtr(" IOC_OUT (get)");
769 if (ioctl(fd, TIOCREQSET, &ioctl_info) < 0) {
770 expDiagLogPtrStr("ioctl(TIOCREQSET) failed: %s\r\n",expErrnoMsg(errno));
780 /* a stub so we can do weird things on the cray */