Debian Codegraph @

gui/GraphLib/libsrc/uimxR5/src/sp_pty.c

raw
1 /*---------------------------------------------------------------------
2 * $Date: 2009-08-18 15:23:20 $ $Revision: 1.6 $
3 *---------------------------------------------------------------------
4 * Copyright (c) 1992, Visual Edge Software Ltd.
5 *
6 * ALL RIGHTS RESERVED. Permission to use, copy, modify, and
7 * distribute this software and its documentation for any purpose
8 * and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Visual Edge Software not be
12 * used in advertising or publicity pertaining to distribution of
13 * the software without specific, written prior permission. The year
14 * included in the notice is the year of the creation of the work.
15 *---------------------------------------------------------------------
16 * File_Description_Section
17 *--------------------------------------------------------------------
18
19 .VERSION
20 090818 last modif
21
22 */
23
24
25
26 #if defined(__linux__)
27 #include <termios.h>
28
29 #if defined(__alpha__)
30
31 #ifndef _ALPHA_TERMIOS_H
32 struct ltchars {
33 char t_suspc;
34 char t_dsuspc;
35 char t_rprntc;
36 char t_flushc;
37 char t_werasc;
38 char t_lnextc;
39 };
40 #endif
41 #endif
42 #endif
43
44 #if defined (sgi)
45 #include <sys/ttold.h>
46 #endif
47
48 #if defined(__FreeBSD__) || defined(__Darwin__)
49 #define USE_OLD_TTY /*need to get B9600 definition in <sys/ttydev.h>*/
50 #include <sys/ttycom.h>
51 #include <sys/filio.h>
52 #include <sys/ttydev.h>
53 #include <sys/ttydefaults.h>
54 #include <sys/ioctl_compat.h>
55 #endif
56
57 #include <stdlib.h>
58 #include <unistd.h>
59
60
61 #include "uxproto.h"
62 #include "version.h"
63 #include "subproc.h"
64 #include "sp_spmd.h"
65 #include "sp_pty.h"
66
67
68
69 extern void UxMarkFileCloseOnExec();
70
71
72 /*--- file global variables ---*/
73 M_FILE_VERSION("$Header")
74
75
76 /* The following structures are initialized in main() in order
77 ** to eliminate any assumptions about the internal order of their
78 ** contents.
79 */
80 #ifdef USE_SYSV_TERMIO
81 struct termio d_tio;
82 struct termio tio;
83 #ifdef TIOCSLTC
84 struct ltchars d_ltc;
85 struct ltchars ltc;
86 #endif /* TIOCSLTC */
87
88
89 #ifdef TIOCLSET
90 unsigned int d_lmode;
91 unsigned int lmode;
92 #endif /* TIOCLSET */
93 #else /* not USE_SYSV_TERMIO */
94 struct sgttyb d_sg = {
95 0, 0, 0177, CKILL, EVENP|ODDP|ECHO|XTABS|CRMOD
96 };
97 struct sgttyb sg;
98
99 struct tchars d_tc = {
100 CINTR, CQUIT, CSTART,
101 CSTOP, CEOF, CBRK,
102 };
103 struct tchars tc;
104
105 struct ltchars d_ltc = {
106 CSUSP, CDSUSP, CRPRNT,
107 CFLUSH, CWERASE, CLNEXT
108 };
109 struct ltchars ltc;
110
111 int d_discipline = NTTYDISC;
112 int discipline;
113
114 long int d_lmode = LCRTBS|LCRTERA|LCRTKIL|LCTLECH;
115 long int lmode;
116 #endif /* USE_SYSV_TERMIO */
117
118
119 #ifndef HP
120 char PTY_dev[] = "/dev";
121 char PTY_0_9a_f[] = "0123456789abcdef";
122 char PTY_p_r[] = "pqr";
123 #define PTY_null (char *) 0
124
125 struct _pty_dirs pty_dirs[] = {
126 {PTY_dev, PTY_dev, PTY_p_r, PTY_0_9a_f, PTY_null, False},
127 {PTY_null, PTY_null, PTY_null, PTY_null, PTY_null, False},
128 };
129
130 #else /* HP */
131 char PTY_dev[] = "/dev";
132 char PTY_dev_pty[] = "/dev/pty";
133 char PTY_dev_ptym[] = "/dev/ptym";
134 char PTY_a_ce_o[] = "abcefghijklmno";
135 char PTY_0_9[] = "0123456789";
136 char PTY_0_9a_f[] = "0123456789abcdef";
137 char PTY_p_z[] = "pqrstuvwxyz";
138 char PTY_p_r[] = "pqr";
139 #define PTY_null (char *) 0
140
141 struct _pty_dirs pty_dirs[] = {
142 {PTY_dev_pty, PTY_dev_ptym, PTY_a_ce_o, PTY_0_9, PTY_0_9, True},
143 {PTY_dev_pty, PTY_dev_ptym, PTY_p_z, PTY_0_9, PTY_0_9, True},
144 {PTY_dev_pty, PTY_dev_ptym, PTY_a_ce_o, PTY_0_9a_f, PTY_null, True},
145 {PTY_dev_pty, PTY_dev_ptym, PTY_p_z, PTY_0_9a_f, PTY_null, False},
146 {PTY_dev, PTY_dev, PTY_p_r, PTY_0_9a_f, PTY_null, False},
147 {PTY_null, PTY_null, PTY_null, PTY_null, PTY_null, False},
148 };
149 #endif /* HP */
150
151 char *ttydev = (char *) 0;
152 char *ptydev = (char *) 0;
153
154 int pc_pipe[2]; /* parent to child communication... */
155 int cp_pipe[2]; /* child to parent communication... */
156
157 jmp_buf env;
158
159 /*--- forward declaration of static functions ---*/
160 static void hungtty UXPROTO((int));
161 static void pass_syserr UXPROTO((char *msg1, char *msg2));
162
163 /*----------------------------------------------------------------------
164 * NAME: <void UxPtyExitHandler()>
165 * DESCRIPTION: <Find out process that died and run whatever necessary
166 before exiting >
167 * PARAMETERS: none
168 * RETURN: void
169 * EXT REFS: UxSpmd
170 * EXT EFFECTS: globals or other things altered
171 * ASSUMPTIONS:
172 * REVISIONS: 05/01/93 fix3810 clean-up
173 dd/mm/yy fix# fix_description
174 *--------------------------------------------------------------------*/
175 void UxPtyExitHandler()
176 {
177 int status, pid;
178
179 pid = UxWhatSpIsDead(&status);
180
181
182 UxOnExitRunExitCb(pid, status);
183 return;
184 }
185
186
187
188 /*----------------------------------------------------------------------
189 * NAME: <static hungtty()>
190 * DESCRIPTION: <description>
191 * PARAMETERS: none
192 * RETURN: none
193 * EXT REFS: env
194 * EXT EFFECTS: globals or other things altered
195 * ASSUMPTIONS:
196 * REVISIONS: 05/01/93 fix3810 clean-up
197 dd/mm/yy fix# fix_description
198 *--------------------------------------------------------------------*/
199 static void hungtty(sig)
200 int sig;
201 {
202 longjmp(env, 1);
203 }
204
205
206 /*----------------------------------------------------------------------
207 * NAME: <int UxGetPtty(pty, reset)>
208 * DESCRIPTION: < Find an available pty/tty line and open it,
209 setting the passed variable. This function is based on the
210 X11 xterm and hpterm get_pty routine. It does not open the
211 pty slave because it must be opened in the child process
212 in order to prevent possible process group problems. For
213 this reason, it is designed to be reentrant to allow it
214 to be called repeatedly in the event that the child process
215 is unable to open the child pty.
216
217 It is also written to be easily ported to different
218 architectures that support different pty naming schemes.
219
220 The master tty file device name is left in global ptydev.
221 The slave tty file device name is left in global ttydev.
222
223 A separate block (currently used only for sgi)
224 handles the case where one master device is always opened,
225 and the minor number of the result identifies the slave.
226 * PARAMETERS: int *pty;
227 int reset;
228 * RETURN: ERROR/NO_ERROR
229 * EXT REFS: globals_used
230 * EXT EFFECTS: globals or other things altered
231 * ASSUMPTIONS:
232 * REVISIONS: 10/10/90 fix1085 IRIX support added
233 05/01/93 fix3810 clean-up
234 11/03/93 fix3989 use multiplexed special file for pty
235 dd/mm/yy fix# fix_description
236 *--------------------------------------------------------------------*/
237 int UxGetPtty(pty, reset)
238 int *pty;
239 int reset;
240 {
241 static struct _pty_dirs *pty_dirs_ptr;
242 static char *char_1;
243 static char *char_2;
244 static char *char_3;
245 static int first = 1;
246 static int initialize = 1;
247 int tty;
248
249 if (reset || initialize) {
250 /* set up pointers into pty filename structures stuff... */
251 pty_dirs_ptr = pty_dirs;
252 char_1 = pty_dirs_ptr->char_1;
253 char_2 = pty_dirs_ptr->char_2;
254 char_3 = pty_dirs_ptr->char_3;
255 }
256
257 if (initialize) {
258 /* initialize terminal modes... */
259
260 #ifdef USE_SYSV_TERMIO
261 /* Initialization is done here rather than above in order
262 ** to prevent any assumptions about the order of the contents
263 ** of the various terminal structures (which may change from
264 ** implementation to implementation).
265 */
266 #ifdef macII
267 d_tio.c_iflag = ICRNL|IXON;
268 d_tio.c_oflag = OPOST|ONLCR|TAB3;
269 d_tio.c_cflag = B9600|CS8|CREAD|PARENB|HUPCL;
270 d_tio.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK;
271
272 d_tio.c_line = 0;
273
274 d_tio.c_cc[VINTR] = CINTR;
275 d_tio.c_cc[VQUIT] = CQUIT;
276 d_tio.c_cc[VERASE] = CERASE;
277 d_tio.c_cc[VKILL] = CKILL;
278 d_tio.c_cc[VEOF] = CEOF;
279 d_tio.c_cc[VEOL] = CNUL;
280 d_tio.c_cc[VEOL2] = CNUL;
281 d_tio.c_cc[VSWTCH] = CNUL;
282
283 d_ltc.t_suspc = CSUSP; /* t_suspc */
284 d_ltc.t_dsuspc = CDSUSP; /* t_dsuspc */
285 d_ltc.t_rprntc = 0; /* reserved...*/
286 d_ltc.t_flushc = 0;
287 d_ltc.t_werasc = 0;
288 d_ltc.t_lnextc = 0;
289 #else /* macII */
290
291 d_tio.c_iflag = ICRNL|IXON|IXOFF;
292 d_tio.c_oflag = OPOST|ONLCR|TAB3;
293
294 #ifdef BAUD_0
295 d_tio.c_cflag = CS8|CREAD|PARENB|HUPCL;
296 /* Needed to turn on pacing from the pty to _subproc.c.
297 * This will cause the program generating output to block
298 * if the pty input buffer fills up.
299 */
300 #else /* !BAUD_0 */
301
302 d_tio.c_cflag = B9600|CS8|CREAD|PARENB|HUPCL;
303 #endif /* !BAUD_0 */
304
305 d_tio.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK;
306 d_tio.c_line = 0;
307 d_tio.c_cc[VINTR] = 0x7f; /* DEL */
308 d_tio.c_cc[VQUIT] = '\\' & 0x3f; /* '^\' */
309 d_tio.c_cc[VERASE] = '#'; /* '#' */
310 d_tio.c_cc[VKILL] = '@'; /* '@' */
311 d_tio.c_cc[VEOF] = 'D' & 0x3f; /* '^D' */
312 d_tio.c_cc[VEOL] = '@' & 0x3f; /* '^@' */
313
314 #ifdef VSWTCH
315 d_tio.c_cc[VSWTCH] = '@' & 0x3f; /* '^@' */
316 #endif /* VSWTCH */
317
318 /* now, try to inherit tty settings */
319 {
320 int i;
321
322 for (i = 0; i <= 2; i++) {
323 struct termio deftio;
324 if (ioctl (i, TCGETA, &deftio) == 0) {
325 d_tio.c_cc[VINTR] = deftio.c_cc[VINTR];
326 d_tio.c_cc[VQUIT] = deftio.c_cc[VQUIT];
327 d_tio.c_cc[VERASE] = deftio.c_cc[VERASE];
328 d_tio.c_cc[VKILL] = deftio.c_cc[VKILL];
329 d_tio.c_cc[VEOF] = deftio.c_cc[VEOF];
330 d_tio.c_cc[VEOL] = deftio.c_cc[VEOL];
331 #ifdef VSWTCH
332 d_tio.c_cc[VSWTCH] = deftio.c_cc[VSWTCH];
333 #endif /* VSWTCH */
334 break;
335 }
336 }
337 }
338
339 #ifdef TIOCSLTC
340 d_ltc.t_suspc = '\000'; /* t_suspc */
341 d_ltc.t_dsuspc = '\000'; /* t_dsuspc */
342 d_ltc.t_rprntc = '\377'; /* reserved...*/
343 d_ltc.t_flushc = '\377';
344 d_ltc.t_werasc = '\377';
345 d_ltc.t_lnextc = '\377';
346 #endif /* TIOCSLTC */
347
348 #ifdef TIOCLSET
349 d_lmode = 0;
350 #endif /* TIOCLSET */
351 #endif /* macII */
352 #endif /* USE_SYSV_TERMIO */
353
354 initialize = 0;
355
356 /* Try to inherit modes from /dev/tty... */
357
358 /*
359 * Sometimes /dev/tty hangs on open (as in the case of a pty
360 * that has gone away). Simply make up some reasonable
361 * defaults.
362 */
363
364 { void (*save_func)() =
365 signal(SIGALRM, hungtty);
366 alarm(2); /* alarm(1) might return too soon */
367
368 if (! setjmp(env)) {
369 tty = open ("/dev/tty", O_RDWR, 0);
370 alarm(0);
371 } else {
372 tty = -1;
373 errno = ENXIO;
374 }
375 signal(SIGALRM, save_func);
376 /*
377 * We should probably save/restore the alarm() value also,
378 * in case the application is using alarms and using GetPtty().
379 */
380 }
381
382 if (tty < 0) {
383
384 #ifdef USE_SYSV_TERMIO
385 tio = d_tio;
386 #ifdef TIOCSLTC
387 ltc = d_ltc;
388 #endif /* TIOCSLTC */
389
390 #ifdef TIOCLSET
391 lmode = d_lmode;
392 #endif /* TIOCLSET */
393
394 #else /* not USE_SYSV_TERMIO */
395 sg = d_sg;
396 tc = d_tc;
397 discipline = d_discipline;
398 ltc = d_ltc;
399 lmode = d_lmode;
400 #endif /* USE_SYSV_TERMIO */
401
402 } else {
403 /* Get a copy of the current terminal's state...
404 */
405
406
407 #ifdef USE_SYSV_TERMIO
408 if (ioctl(tty, TCGETA, &tio) == -1)
409 tio = d_tio; /* should probably print an error message... */
410 #ifdef TIOCSLTC
411 if (ioctl(tty, TIOCGLTC, &ltc) == -1)
412 ltc = d_ltc; /* should probably print an error message... */
413 #endif /* TIOCSLTC */
414
415 #ifdef TIOCLSET
416 if (ioctl(tty, TIOCLGET, &lmode) == -1)
417 lmode = d_lmode; /* should probably print an error message... */
418 #endif /* TIOCLSET */
419
420 #else /* not USE_SYSV_TERMIO */
421 if (ioctl(tty, TIOCGETP, (char *)&sg) == -1)
422 sg = d_sg; /* should probably print an error message... */
423 if (ioctl(tty, TIOCGETC, (char *)&tc) == -1)
424 tc = d_tc;; /* should probably print an error message... */
425 if (ioctl(tty, TIOCGETD, (char *)&discipline) == -1)
426 /* should probably print an error message... */
427 discipline = d_discipline;
428 if (ioctl(tty, TIOCGLTC, (char *)&ltc) == -1)
429 ltc = d_ltc;; /* should probably print an error message... */
430 if (ioctl(tty, TIOCLGET, (char *)&lmode) == -1)
431 lmode = d_lmode; /* should probably print an error message... */
432 #endif /* USE_SYSV_TERMIO */
433
434 (void) close (tty);
435 }
436 }
437
438 /* Find a master/slave device pair. */
439
440 #if defined(sgi)
441
442 /* On IRIX,
443 * master/slave pty pairs are found by opening /dev/ptc as master.
444 * The resulting minor number N identifies the slave /dev/ttyqN.
445 */
446 ttydev = (char *)
447 UxCopyString( _getpty( pty, O_RDWR|O_NDELAY, 0666, 0));
448
449 #elif defined(ibmr2)
450 /* On ibm, pty and tty names are /dev/ptc/xx and /dev/pts/xx where xx
451 * is the channel number.
452 * Master/slave pty pairs are found by opening /dev/ptc as master.
453 * The slave name (/dev/pts) is obtained using ttyname().
454 */
455 *pty = open("/dev/ptc", O_RDWR, 0);
456 ttydev = UxCopyString(ttyname(*pty));
457 #else /* not sgi or ibmr2 */
458
459 /* On HP (and many other machines) pty master/slaves
460 * are opened as two devices with parallel names,
461 * such as /dev/ptym/pty38 and /dev/pty/tty38
462 */
463
464 /* as long as these are valid, we have a good pair... */
465 while (pty_dirs_ptr->pty_dir && pty_dirs_ptr->ptym_dir) {
466 /* allocate space for the pty slave... */
467 if (ttydev)
468 (void) UxFree(ttydev);
469 ttydev = UxMalloc((unsigned) strlen(pty_dirs_ptr->pty_dir) + 8);
470
471 /* allocate space for the pty master... */
472 if (ptydev)
473 (void) UxFree(ptydev);
474 ptydev = UxMalloc((unsigned) strlen(pty_dirs_ptr->ptym_dir) + 8);
475
476 /* check malloc success... */
477 if (!ttydev || !ptydev) {
478 UxInternalError(__FILE__, __LINE__,
479 CGETS( MS_MISC_MALLOC_ERR, DS_MS_MISC_MALLOC_ERR));
480 return(ERROR);
481 }
482
483 while ((first || !pty_dirs_ptr->fast) && *char_1) {
484 while ((first || !pty_dirs_ptr->fast) && *char_2) {
485 if ((first || !pty_dirs_ptr->fast) && char_3) {
486 while ((first || !pty_dirs_ptr->fast) && *char_3) {
487 (void) sprintf(ttydev, "%s/tty%c%c%c",
488 pty_dirs_ptr->pty_dir,
489 *char_1, *char_2, *char_3);
490 (void) sprintf(ptydev, "%s/pty%c%c%c",
491 pty_dirs_ptr->ptym_dir,
492 *char_1, *char_2, *char_3);
493 if ((*pty = open(ptydev, O_RDWR)) >= 0) {
494 /* we need to set things up for our next entry
495 * into this function...
496 */
497 (void) char_3++;
498 /* success... */
499 #ifdef HP
500 /* We need to set the modes here. If we wait until
501 * after the program is writing to the pty slave
502 * before we do this we end up with some of the
503 * output getting flushed on us. It tends to be
504 * visible as the first 256 (255?) characters or
505 * so getting dropped.
506 */
507
508 UX_SETUP_PARENT_COMMUNICATION(*pty);
509 #endif
510 return(NO_ERROR);
511 } else {
512 /* if the device is busy, then we should keep
513 * going until we hit one that doesn't exist...
514 */
515 if (errno != EBUSY)
516 first = False;
517 }
518 (void) char_3++;
519 }
520 } else {
521 (void) sprintf(ttydev, "%s/tty%c%c",
522 pty_dirs_ptr->pty_dir,
523 *char_1, *char_2);
524 (void) sprintf(ptydev, "%s/pty%c%c",
525 pty_dirs_ptr->ptym_dir,
526 *char_1, *char_2);
527 if ((*pty = open(ptydev, O_RDWR)) >= 0) {
528 /* we need to set things up for our next entry into
529 * this function!
530 */
531 (void) char_2++;
532 /* success... */
533 #ifdef HP
534 /* We need to set the modes here. If we wait until
535 * after the program is writing to the pty slave
536 * before we do this we end up with some of the
537 * output getting flushed on us. It tends to be
538 * visible as the first 256 (255?) characters or
539 * so getting dropped.
540 */
541
542 UX_SETUP_PARENT_COMMUNICATION(*pty);
543 #endif
544
545 return(NO_ERROR);
546 } else {
547 /* if the device is busy, then we should keep going
548 * until we hit one that doesn't exist...
549 */
550 if (errno != EBUSY)
551 first = False;
552
553 }
554 }
555 char_3 = pty_dirs_ptr->char_3;
556 (void) char_2++;
557 }
558 char_2 = pty_dirs_ptr->char_2;
559 (void) char_1++;
560 }
561 first = True;
562 (void) pty_dirs_ptr++;
563 char_1 = pty_dirs_ptr->char_1;
564 char_2 = pty_dirs_ptr->char_2;
565 char_3 = pty_dirs_ptr->char_3;
566 }
567
568 /* we were unable to allocate a pty master... */
569 error_msg(CGETS( MS_MISC_PTY_DRV_ERR, DS_MS_MISC_PTY_DRV_ERR));
570 return(ERROR);
571
572 #endif /* defined(sgi) */
573
574 }
575
576
577 /*----------------------------------------------------------------------
578 * NAME: <static void pass_syserr(msg1, msg2)>
579 * DESCRIPTION: <This function is called by the child process to pass back
580 to the parent an error message. >
581 * PARAMETERS: char *msg1, *msg2 -- message to print
582 * RETURN: void
583 * EXT REFS: cp_pipe
584 * EXT EFFECTS: globals or other things altered
585 * ASSUMPTIONS:
586 * REVISIONS: 22/02/90 creation
587 05/01/93 fix3810 clean-up
588 dd/mm/yy fix# fix_description
589 *--------------------------------------------------------------------*/
590 static void pass_syserr(msg1, msg2)
591 char *msg1;
592 char *msg2;
593 {
594 handshake_t handshake;
595
596 /* let our master know that the open failed... */
597 handshake.status = SUBPROC_ERROR;
598 handshake.error = errno;
599 if (msg1) {
600 (void) strcpy(handshake.buffer, msg1);
601 if (msg2) {
602 (void) strcat(handshake.buffer, ": ");
603 (void) strcat(handshake.buffer, msg2);
604 }
605 } else {
606 *handshake.buffer = 0;
607 }
608
609
610 (void) write(cp_pipe[1], (char *) &handshake, sizeof(handshake));
611 return;
612 }
613
614
615
616 /*----------------------------------------------------------------------
617 * NAME: <int UxSetTty()>
618 * DESCRIPTION: <Open the pty slave>
619 * PARAMETERS: none
620 * RETURN: int - the opened tty
621 * EXT REFS: cp_pipe, pc_pipe, ttydev
622 * EXT EFFECTS: ttydev
623 * ASSUMPTIONS:
624 * REVISIONS: 05/01/93 fix3810 clean-up
625 dd/mm/yy fix# fix_description
626 *--------------------------------------------------------------------*/
627 int UxSetTty()
628 {
629 int tty;
630 int i;
631 handshake_t handshake;
632 static int first = 1;
633 static int force_failures = 0;
634 int failures = 5;
635
636 if (first) {
637 if (getenv("SUBPROC_PTYOPEN_FAIL"))
638 force_failures = 1;
639 first = 0;
640 }
641
642
643 /* now it is safe to go and open up the pty slave... */
644 while (1) {
645 /* The following code allows debugging of slave side
646 * problems openning up the pty slave.
647 */
648 if (force_failures && (failures-- > 0)) {
649
650
651 tty = -1;
652 errno = EACCES;
653 } else {
654 tty = open(ttydev, O_RDWR, 0);
655 }
656
657 if (tty >= 0) {
658
659 #ifdef USE_SYSV_PGRP
660 /* We need to make sure that we are actually the
661 * process group leader for the pty. If
662 * we are, then we should now be able to open
663 * /dev/tty...
664 */
665 if ((i = open("/dev/tty", O_RDWR, 0)) >= 0) {
666 /* success!... */
667 (void) close(i);
668 break;
669 }
670 #else /* USE_SYSV_PGRP */
671 break;
672 #endif /* USE_SYSV_PGRP */
673 }
674
675 /* We were unable to open up a pty. Inform our master, and
676 * wait for a new one...
677 */
678 handshake.status = PTY_BAD;
679 handshake.error = errno;
680 (void) strcpy(handshake.buffer, ttydev);
681
682
683 (void) write(cp_pipe[1], (char *) &handshake,
684 sizeof(handshake));
685
686 /* get reply from parent... */
687
688 if (read(pc_pipe[0], (char *) &handshake, sizeof(handshake)) <
689 sizeof(handshake)) {
690 /* parent terminated... */
691 (void) _exit(1);
692 }
693
694 if (handshake.status == PTY_NOMORE) {
695 /* No more ptys, let's shutdown... */
696 (void) _exit(1);
697 }
698
699 /* We have a new pty to try... */
700 (void) UxFree(ttydev);
701 ttydev = UxMalloc((unsigned) (strlen(handshake.buffer) + 1));
702 if (!ttydev) {
703 (void) _exit(1);
704 }
705
706 (void) strcpy(ttydev, handshake.buffer);
707 }
708 return(tty);
709 }
710
711
712 /*----------------------------------------------------------------------
713 * NAME: <void UxPtyChildCommSetup(tty, echo)>
714 * DESCRIPTION: <Set communication parameters for tty slave>
715 * PARAMETERS: int tty - tty for which to set communication parameters
716 int echo - do we want echo or not ?
717 * RETURN: void
718 * EXT REFS: globals_used
719 * EXT EFFECTS: globals or other things altered
720 * ASSUMPTIONS:
721 * REVISIONS: 05/01/93 fix3810 clean-up
722 08/01/93 fix3818 to remove r's at end for HP/IBM
723 07/07/93 fix4339 dg88 port
724 dd/mm/yy fix# fix_description
725 *--------------------------------------------------------------------*/
726 void UxPtyChildCommSetup(tty, echo)
727 int tty;
728 int echo;
729 {
730 int i;
731
732 #ifdef USE_SYSV_TERMIO
733 #if defined(sgi) || defined(ibmr2)
734 /* If the control tty had its modes screwed around with,
735 eg. by lineedit in the shell, or emacs, etc. then tio
736 will have bad values. Let's just get termio from the
737 new tty and tailor it. */
738 if (ioctl (tty, TCGETA, &tio) == -1)
739 pass_syserr(ttydev, "TCGETA");
740 tio.c_lflag |= ECHOE;
741 #endif /* sgi or ibmr2 */
742
743 /* Now is also the time to change the modes of the
744 * child pty.
745 */
746 /* input: nl->nl, don't ignore cr, cr->nl */
747 tio.c_iflag &= ~(INLCR|IGNCR);
748 tio.c_iflag |= ICRNL;
749 /* ouput: cr->cr, nl is not return, no delays, ln->cr/nl */
750 tio.c_oflag &=
751 ~(OCRNL|ONLRET|NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
752
753 /* to remove r's at end */
754 #if defined(sgi) || defined(Magnum) || defined(HP) || defined(ibmr2) || defined(dg88)
755 tio.c_oflag &= ~ONLCR;
756 #else
757 tio.c_oflag |= ONLCR;
758 #endif /* sgi || magnum || HP || ibmr2 || dg88 */
759
760 #ifdef BAUD_0
761 /* baud rate is 0 (don't care) */
762 tio.c_cflag &= ~(CBAUD);
763 #else /* !BAUD_0 */
764 /* baud rate is 9600 (nice default) */
765 tio.c_cflag &= ~(CBAUD);
766 tio.c_cflag |= B9600;
767 #endif /* !BAUD_0 */
768
769 /* enable signals, canonical processing (erase, kill, etc),
770 ** echo
771 */
772 tio.c_lflag |= ISIG|ICANON|ECHO;
773 if (!echo)
774 tio.c_lflag &= ~ECHO;
775 /* reset EOL to defalult value */
776 tio.c_cc[VEOL] = '@' & 0x3f; /* '^@' */
777 /* certain shells (ksh & csh) change EOF as well */
778 tio.c_cc[VEOF] = 'D' & 0x3f; /* '^D' */
779 if (ioctl (tty, TCSETA, &tio) == -1)
780 ; /* should probably print an error message... */
781 #ifdef TIOCSLTC
782 if (ioctl (tty, TIOCSLTC, &ltc) == -1) {
783 /* print an error message... */
784 pass_syserr(ttydev, "TIOCSLTC");
785 }
786 #endif /* TIOCSLTC */
787
788 #ifdef TIOCLSET
789 if (ioctl (tty, TIOCLSET, (char *)&lmode) == -1) {
790 /* print an error message... */
791 pass_syserr(ttydev, "TIOCLSET");
792 }
793 #endif /* TIOCLSET */
794
795 #else /* USE_SYSV_TERMIO */
796 sg.sg_flags &= ~(ALLDELAY | XTABS | CBREAK | RAW);
797 sg.sg_flags |= ECHO | CRMOD;
798 sg.sg_flags &= ~CRMOD; /* to remove r's at end */
799
800 if (!echo)
801 sg.sg_flags &= ~ECHO;
802 /* make sure speed is set on pty so that editors work right*/
803 sg.sg_ispeed = B9600;
804 sg.sg_ospeed = B9600;
805 /* reset t_brkc to default value */
806 tc.t_brkc = -1;
807
808 if (ioctl (tty, TIOCSETP, (char *)&sg) == -1) {
809 /* print an error message... */
810 pass_syserr(ttydev, "TIOCSETP");
811 }
812 if (ioctl (tty, TIOCSETC, (char *)&tc) == -1) {
813 /* print an error message... */
814 pass_syserr(ttydev, "TIOCSETC");
815 }
816 if (ioctl (tty, TIOCSETD, (char *)&discipline) == -1) {
817 /* print an error message... */
818 pass_syserr(ttydev, "TIOCSETD");
819 }
820 if (ioctl (tty, TIOCSLTC, (char *)&ltc) == -1) {
821 /* print an error message... */
822 pass_syserr(ttydev, "TIOCSLTC");
823 }
824 if (ioctl (tty, TIOCLSET, (char *)&lmode) == -1) {
825 /* print an error message... */
826 pass_syserr(ttydev, "TIOCLSET");
827 }
828 #endif /* !USE_SYSV_TERMIO */
829
830 /* set up stdin, out, err to pty slave... */
831 for (i = 0; i < 3; i++) {
832 if (i != tty) {
833 (void) close(i);
834 (void) dup(tty);
835 }
836 }
837
838 (void) setpgrp();
839
840 }
841
842
843 /*----------------------------------------------------------------------
844 * NAME: <int UxPtyStartChild(pty, echo, args)>
845 * DESCRIPTION: <fork the process and exec a new process with the args
846 passed.>
847 * PARAMETERS: int pty -- master pty (for child to close)
848 int echo -- echo on or off
849 char **args -- the argument list
850 * RETURN: ERROR or new process id
851 * EXT REFS: pc_pipe, cp_pipe
852 * EXT EFFECTS: globals or other things altered
853 * ASSUMPTIONS:
854 * REVISIONS: 05/01/93 fix3810 clean-up
855 dd/mm/yy fix# fix_description
856 *--------------------------------------------------------------------*/
857 int UxPtyStartChild(pty, echo, args)
858 int pty;
859 int echo;
860 char **args;
861
862 {
863 int pid, i;
864 int tty;
865 handshake_t handshake;
866
867
868 for (i = 0; (i < 10) && ((pid = fork()) < 0); i++) {
869 /* out of resources?... */
870 if (errno != EAGAIN)
871 /* no!... */
872 break;
873
874 /* give things a chance to clear up... */
875 (void) sleep((unsigned long) 2);
876 }
877 if (pid < 0) {
878 UxSyserr(args[0], NULL);
879 return(ERROR);
880 }
881
882 if (pid == 0) {
883
884 /* close the parent's sides of the pipes... */
885 (void) close(cp_pipe[0]);
886 (void) close(pc_pipe[1]);
887
888 /* make sure the pipes are outside the range of 0, 1, 2... */
889 if (cp_pipe[1] < 3) {
890 i = fcntl(cp_pipe[1], F_DUPFD, 3);
891 if (i >= 0) {
892 (void) close(cp_pipe[1]);
893 cp_pipe[1] = i;
894 }
895 }
896
897 if (pc_pipe[0] < 3) {
898 i = fcntl(pc_pipe[0], F_DUPFD, 3);
899 if (i >= 0) {
900 (void) close(pc_pipe[0]);
901 pc_pipe[0] = i;
902 }
903 }
904
905 #ifdef USE_SYSV_PGRP
906 /* now, let's kick ourself into a new process group... */
907 (void) setpgrp();
908 #endif /* USE_SYSV_PGRP */
909
910
911 tty = UX_SET_CHILD_SIDE();
912
913 /* Good open...
914 * Set up the pty slave...
915 */
916
917 (void) UX_SETUP_CHILD_COMMUNICATION(tty, echo);
918
919 /* mark all other files as close on exec... */
920 UxMarkFileCloseOnExec (1);
921
922 (void) execvp(args[0], args);
923
924 /* reset close-on-exec flag */
925 UxMarkFileCloseOnExec (0);
926
927 pass_syserr(args[0], NULL);
928 /* Added a new handshake type to let the master know that we
929 * were unable to exec the program and we should return an error.
930 * Previously, this information was lost and we returned NO_ERROR.
931 */
932 handshake.status = SUBPROC_EXEC_ERROR;
933 (void) strcpy(handshake.buffer, args[0]);
934
935 (void) write(cp_pipe[1], (char *) &handshake, sizeof(handshake));
936 _exit(1);
937 }
938
939 return(pid);
940 }
941
942
943 /*----------------------------------------------------------------------
944 * NAME: <int set_pseudo(pty)>
945 * DESCRIPTION: <Set the characteristic for the pseudo terminal pair.
946 Note: This routine is the most likely to have portability
947 problems.>
948 * PARAMETERS: int pty -- the pseudo terminal line
949 * RETURN: ERROR/NO_ERROR
950 * EXT REFS: globals_used
951 * EXT EFFECTS: globals or other things altered
952 * ASSUMPTIONS:
953 * REVISIONS: 05/01/93 fix3810 clean-up
954 07/07/93 fix4339 dg88 port
955 dd/mm/yy fix# fix_description
956 *--------------------------------------------------------------------*/
957
958
959
960 #if defined(HP) || defined(_TEK88) || defined(_MOTOR88) || defined(sco)
961
962 int UxSetPseudo(pty)
963 int pty;
964 {
965 static int off_flag = 0;
966 static int *off;
967 int one= 1;
968
969 off = &off_flag;
970
971
972 /* set master side */
973 #if !defined(sco)
974
975 #ifndef Cygwin
976 if(ioctl(pty, TIOCREMOTE, off) == -1){
977 UxSyserr(ptydev, "TIOCREMOTE");
978 return(ERROR);
979 }
980 #endif
981
982 if(ioctl(pty, TIOCPKT, off) == -1){
983 UxSyserr(ptydev, "TIOCPKT");
984 return(ERROR);
985 }
986
987 #endif /* sco */
988
989
990 #ifdef HP
991 ioctl(pty, TIOCTTY, &one);
992 #endif
993 return(NO_ERROR);
994 }
995
996
997
998 #else /* not HP, _MOTOR88, _TEK88 */
999
1000 int UxSetPseudo(pty)
1001 int pty;
1002
1003 {
1004 static int on_flag = 1;
1005 static int off_flag = 0;
1006 static int *on;
1007 static int *off;
1008
1009 on = &on_flag;
1010 off = &off_flag;
1011
1012
1013 /* set master side */
1014 #if !defined(sgi) && !defined(ibmr2) && !defined(dg88) && !defined(Cygwin)
1015 if(ioctl(pty, TIOCREMOTE, off) == -1){
1016 UxSyserr(ptydev, "TIOCREMOTE");
1017 return(ERROR);
1018 }
1019 #endif
1020
1021 #if !defined(DGUX) && !defined(SOLARIS)
1022 /* Don't use packet mode */
1023 if(ioctl(pty, TIOCPKT, off) == -1){
1024 UxSyserr(ptydev, "TIOCPKT");
1025 return(ERROR);
1026 }
1027 #endif /* DGUX */
1028
1029 /* Use non-blocking IO */
1030 if(ioctl(pty, FIONBIO, on) == -1){
1031 UxSyserr(ptydev, "FIONBIO");
1032 return(ERROR);
1033 }
1034
1035 /* Don't use asynchronous IO */
1036 #if !defined(sgi) && !defined(magnum) && !defined(ibmr2) && !defined(Cygwin)
1037 if(ioctl(pty, FIOASYNC, off) == -1){
1038 UxSyserr(ptydev, "FIOASYNC");
1039 return(ERROR);
1040 }
1041 #endif
1042
1043 return(NO_ERROR);
1044 }
1045 #endif /* HP || _TEK88 || _MOTOR88 */
1046
1047
1048 /*----------------------------------------------------------------------
1049 * NAME: <char ** UxPtyPrepareCommand(command)>
1050 * DESCRIPTION: <Prepare command line to be sent to subprocess>
1051 * PARAMETERS: char *command - command line
1052 * RETURN: char * - pointer to clean command to be sent to subprocess
1053 * EXT REFS: globals_used
1054 * EXT EFFECTS: globals or other things altered
1055 * ASSUMPTIONS:
1056 * REVISIONS: 05/01/93 fix3810 clean-up
1057 dd/mm/yy fix# fix_description
1058 *--------------------------------------------------------------------*/
1059 char ** UxPtyPrepareCommand(command)
1060 char *command;
1061
1062 {
1063 char ** clean_command;
1064 char *proc;
1065 char *proc_args;
1066 int finished;
1067
1068 proc = command;
1069 for (proc_args = proc, finished = 0; !finished; ) {
1070 switch (*proc_args) {
1071 case ' ': case '\t':
1072 *proc_args++ = '\0';
1073 /*FALLTHROUGH*/
1074 case '\0': /* End of string. */
1075 finished = 1;
1076 break;
1077 default:
1078 proc_args++;
1079 break;
1080 }
1081 }
1082 clean_command = UxBuildArgs(proc, proc_args);
1083 return(clean_command);
1084 }
1085
1086
1087 /*----------------------------------------------------------------------
1088 * NAME: <int UxPtyExecSubprocess(cmd, echo, t_pty, t_pid)>
1089 * DESCRIPTION: <Get the pseudo terminal pair, sets the characteristics
1090 of it, start the childprocess and sets the output
1091 side (pty) side as an Input Source to the Xtoolkit.>
1092 * PARAMETERS: char *cmd -- the full command line
1093 * RETURN: ERROR/NO_ERROR
1094 * EXT REFS: cp_pipe, pc_pipe
1095 * EXT EFFECTS: globals or other things altered
1096 * ASSUMPTIONS: 05/01/93 fix3810 clean-up
1097 dd/mm/yy fix# fix_description
1098 *--------------------------------------------------------------------*/
1099 int UxPtyExecSubprocess(cmd, echo, t_pty, t_pid)
1100 char *cmd;
1101 int echo;
1102 int *t_pty;
1103 int *t_pid;
1104
1105 {
1106 char **args;
1107
1108 handshake_t handshake;
1109 int flags;
1110 int pid;
1111 int pty;
1112 int done;
1113
1114
1115 if(! (args = UX_PREPARE_COMMAND(cmd)) ) {
1116 return(ERROR);
1117 }
1118
1119 if (pipe(pc_pipe) || pipe(cp_pipe)) {
1120 UxInternalError(__FILE__, __LINE__,
1121 CGETS( MS_MISC_PIPE_ERR, DS_MS_MISC_PIPE_ERR));
1122 return(ERROR);
1123 }
1124
1125 if(UX_SET_PARENT_SIDE(&pty, 1) == ERROR)
1126 return(ERROR);
1127
1128 if( (pid = UX_START_CHILD(pty, echo, args)) == ERROR) {
1129 if (pty >= 0)
1130 close(pty);
1131 pty = -1;
1132 return(ERROR);
1133 }
1134
1135 /* This is the parent side of the fork...
1136 */
1137 /* close the child's side of the pipes... */
1138 (void) close(cp_pipe[1]);
1139 (void) close(pc_pipe[0]);
1140
1141 for (done = 0; !done; ) {
1142
1143 if (read(cp_pipe[0], &handshake, sizeof(handshake)) <= 0) {
1144 /* Our child is done talking to us. If it terminated due
1145 * to an error, we will catch the death of child and
1146 * clean up.
1147 */
1148 (void) close(cp_pipe[0]);
1149 (void) close(pc_pipe[1]);
1150
1151 /* we can stop looping now... */
1152 break;
1153 }
1154
1155 switch(handshake.status) {
1156 case SUBPROC_ERROR:
1157
1158 /* Child has an error message that they would like us to
1159 * print...
1160 */
1161 /* set errno... */
1162 errno = handshake.error;
1163 if (*handshake.buffer) {
1164 /* they passed us a string to print... */
1165 UxSyserr(handshake.buffer, NULL);
1166 } else {
1167 /* no message to print... */
1168 UxSyserr(NULL, NULL);
1169 }
1170 break;
1171
1172 case SUBPROC_EXEC_ERROR:
1173
1174 /* Child was unable to exec the subprocess...
1175 */
1176 (void) close(cp_pipe[0]);
1177 (void) close(pc_pipe[1]);
1178 /* Just to be safe, don't close the pty unless it is a
1179 * valid file descriptor, and after doing it, make it
1180 * invalid so we won't accidently close it again
1181 * (who knows if it will point to anything important).
1182 */
1183 if (pty >= 0)
1184 (void) close(pty);
1185 pty = -1;
1186 return(ERROR);
1187
1188 case PTY_GOOD:
1189
1190 /* Success! Let's free up resources and continue...
1191 */
1192 done = True;
1193 (void) close(cp_pipe[0]);
1194 (void) close(pc_pipe[1]);
1195 break;
1196
1197 case PTY_BAD:
1198
1199 /* Pty slave open failed, get them another one...
1200 */
1201 /* get errno from our child... */
1202 errno = handshake.error;
1203
1204 if (pty >= 0)
1205 (void) close(pty);
1206 pty = -1;
1207 if (UX_SET_PARENT_SIDE(&pty, 0) == ERROR) {
1208 /* no more ptys... */
1209 handshake.status = PTY_NOMORE;
1210
1211 (void) write(pc_pipe[1], &handshake, sizeof(handshake));
1212 (void) close(cp_pipe[0]);
1213 (void) close(pc_pipe[1]);
1214 return(ERROR);
1215 }
1216
1217 handshake.status = PTY_NEW;
1218 (void) strcpy(handshake.buffer, ttydev);
1219
1220 (void) write(pc_pipe[1], &handshake, sizeof(handshake));
1221 break;
1222
1223
1224 default: /* for PTY_NEW and PTY_NOMORE */
1225 break;
1226 }
1227 }
1228
1229
1230 if( (flags = fcntl(pty, F_GETFL, 0)) == -1) {
1231 /* CG. 2509002 I believe it meant kill()*/
1232 /* signal(SIGKILL, pid); */
1233 kill(pid, SIGKILL);
1234 UxSyserr(ptydev, "F_GETFL");
1235 if (pty >= 0)
1236 close(pty);
1237 pty = -1;
1238 return(ERROR);
1239 }
1240
1241 /* O_NDELAY and FNDELAY have the same value. But only by chance ! */
1242 if( fcntl(pty, F_SETFL, flags|O_NDELAY) == -1) {
1243 /* CG. 2509002 I believe it meant kill()*/
1244 /* signal(SIGKILL, pid); */
1245 kill(pid, SIGKILL);
1246 UxSyserr(ptydev, "F_SETFL");
1247 if (pty >= 0)
1248 close(pty);
1249 pty = -1;
1250 return(ERROR);
1251 }
1252
1253 #if !(defined(HP) || defined(sgi) || defined(ibmr2))
1254 /* set up pty master... */
1255 if(UX_SETUP_PARENT_COMMUNICATION(pty) == ERROR) {
1256 if (pty >= 0)
1257 close(pty);
1258 pty = -1;
1259 return(ERROR);
1260 }
1261 #endif /* HP || sgi || ibmr2 */
1262
1263 *t_pid = pid;
1264 *t_pty = pty;
1265
1266 return(NO_ERROR);
1267
1268 }
1269