2 * childio.c -- set up communication with child processes $Id:
3 * childio.c,v 1.2 1994/11/12 19:31:41 mann Exp mann $
5 * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.
6 * Enhancements Copyright 1992-95 Free Software Foundation, Inc.
8 * The following terms apply to Digital Equipment Corporation's copyright
10 * ------------------------------------------------------------------------
13 * Permission to use, copy, modify, and distribute this software and its
14 * documentation for any purpose and without fee is hereby granted,
15 * provided that the above copyright notice appear in all copies and that
16 * both that copyright notice and this permission notice appear in
17 * supporting documentation, and that the name of Digital not be
18 * used in advertising or publicity pertaining to distribution of the
19 * software without specific, written prior permission.
21 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
22 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
23 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
24 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
25 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
28 * ------------------------------------------------------------------------
30 * The following terms apply to the enhanced version of XBoard distributed
31 * by the Free Software Foundation:
32 * ------------------------------------------------------------------------
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
43 * You should have received a copy of the GNU General Public License
44 * along with this program; if not, write to the Free Software
45 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
46 * ------------------------------------------------------------------------
48 * See the file ChangeLog for a revision history. */
50 /* This file splits into two entirely different pieces of code
51 depending on whether USE_PTYS is 1. The whole reason for all
52 the pty nonsense is that select() does not work on pipes in System-V
53 derivatives (at least some of them). This is a problem because
54 XtAppAddInput works by adding its argument to a select that is done
69 /* This code is for systems where pipes work properly */
71 void SetUpChildIO(to_prog, from_prog)
72 int to_prog[2], from_prog[2];
74 signal(SIGPIPE, SIG_IGN);
79 #else /* USE_PTYS == 1 */
80 /* This code is for all systems where we must use ptys */
84 #include <sys/ioctl.h>
87 #endif /* HAVE_STROPTS_H */
89 # include <sys/fcntl.h>
90 #else /* not HAVE_SYS_FCNTL_H */
93 # endif /* HAVE_FCNTL_H */
94 #endif /* not HAVE_SYS_FCNTL_H */
96 int PseudoTTY P((char pty_name[]));
98 int SetUpChildIO(to_prog, from_prog)
99 int to_prog[2], from_prog[2];
101 char pty_name[MSG_SIZ];
103 if ((to_prog[1] = PseudoTTY(pty_name)) == -1) {
104 DisplayFatalError("Can't open pseudo-tty", errno, 1);
107 from_prog[0] = to_prog[1];
108 to_prog[0] = from_prog[1] = open(pty_name, O_RDWR, 0);
110 #if HAVE_STROPTS_H /* do we really need this?? pipe-like behavior is fine */
111 if (ioctl (to_prog[0], I_PUSH, "ptem") == -1 ||
112 ioctl (to_prog[0], I_PUSH, "ldterm") == -1 ||
113 ioctl (to_prog[0], I_PUSH, "ttcompat") == -1) {
114 # ifdef NOTDEF /* seems some systems don't have or need ptem and ttcompat */
115 DisplayFatalError("Can't ioctl pseudo-tty", errno, 1);
119 #endif /* HAVE_STROPTS_H */
124 /* This code is for SVR4 */
126 int PseudoTTY(pty_name)
129 extern char *ptsname();
133 fd = open("/dev/ptmx", O_RDWR);
134 if (fd < 0) return fd;
135 if (grantpt(fd) == -1) return -1;
136 if (unlockpt(fd) == -1) return -1;
137 if (!(ptss = ptsname(fd))) return -1;
138 strcpy(pty_name, ptss);
142 #else /* not HAVE_GRANTPT */
144 /* This code is for IRIX */
146 int PseudoTTY(pty_name)
152 ptyn = _getpty(&fd, O_RDWR, 0600, 0);
153 if (ptyn == NULL) return -1;
154 strcpy(pty_name, ptyn);
158 #else /* not HAVE__GETPTY */
160 /* This code is for Sequent DYNIX/ptx. Untested. --tpm */
162 int PseudoTTY(pty_name)
166 char *slave, *master;
168 fd = getpseudotty(&slave, &master);
169 if (fd < 0) return fd;
170 strcpy(pty_name, slave);
174 #else /* not HAVE_LIBSEQ */
175 /* This code is for all other systems */
176 /* The code is adapted from GNU Emacs 19.24 */
178 #ifndef FIRST_PTY_LETTER
179 #define FIRST_PTY_LETTER 'p'
181 #ifndef LAST_PTY_LETTER
182 #define LAST_PTY_LETTER 'z'
185 int PseudoTTY(pty_name)
192 /* Some systems name their pseudoterminals so that there are gaps in
193 the usual sequence - for example, on HP9000/S700 systems, there
194 are no pseudoterminals with names ending in 'f'. So we wait for
195 three failures in a row before deciding that we've reached the
197 int failed_count = 0;
202 for (c = FIRST_PTY_LETTER; c <= LAST_PTY_LETTER; c++)
203 for (i = 0; i < 16; i++)
206 #ifdef PTY_NAME_SPRINTF
209 sprintf (pty_name, "/dev/pty%c%x", c, i);
210 #endif /* no PTY_NAME_SPRINTF */
214 #else /* no PTY_OPEN */
215 if (stat (pty_name, &stb) < 0)
218 if (failed_count >= 3)
223 fd = open (pty_name, O_RDWR, 0);
224 #endif /* no PTY_OPEN */
228 /* check to make certain that both sides are available
229 this avoids a nasty yet stupid bug in rlogins */
230 #ifdef PTY_TTY_NAME_SPRINTF
233 sprintf (pty_name, "/dev/tty%c%x", c, i);
234 #endif /* no PTY_TTY_NAME_SPRINTF */
236 if (access (pty_name, 6) != 0)
241 #endif /* not UNIPLUS */
243 /* On AIX, the parent gets SIGHUP when a pty attached
244 child dies. So, we ignore SIGHUP once we've started
245 a child on a pty. Note that this may cause xboard
246 not to die when it should, i.e., when its own
247 controlling tty goes away.
249 signal(SIGHUP, SIG_IGN);
250 #endif /* IBMRTAIX */
257 #endif /* not HAVE_LIBSEQ */
258 #endif /* not HAVE__GETPTY */
259 #endif /* not HAVE_GRANTPT */
260 #endif /* USE_PTYS */