1 /*
2 * childio.c -- set up communication with child processes
3 *
4 * Copyright 1991 by Digital Equipment Corporation, Maynard,
5 * Massachusetts.
6 *
7 * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8 * 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free
9 * Software Foundation, Inc.
10 *
11 * The following terms apply to Digital Equipment Corporation's copyright
12 * interest in XBoard:
13 * ------------------------------------------------------------------------
14 * All Rights Reserved
15 *
16 * Permission to use, copy, modify, and distribute this software and its
17 * documentation for any purpose and without fee is hereby granted,
18 * provided that the above copyright notice appear in all copies and that
19 * both that copyright notice and this permission notice appear in
20 * supporting documentation, and that the name of Digital not be
21 * used in advertising or publicity pertaining to distribution of the
22 * software without specific, written prior permission.
23 *
24 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
26 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
27 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
28 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
29 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
30 * SOFTWARE.
31 * ------------------------------------------------------------------------
32 *
33 * The following terms apply to the enhanced version of XBoard
34 * distributed by the Free Software Foundation:
35 * ------------------------------------------------------------------------
36 *
37 * GNU XBoard is free software: you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation, either version 3 of the License, or (at
40 * your option) any later version.
41 *
42 * GNU XBoard is distributed in the hope that it will be useful, but
43 * WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
45 * General Public License for more details.
46 *
47 * You should have received a copy of the GNU General Public License
48 * along with this program. If not, see http://www.gnu.org/licenses/. *
49 *
50 *------------------------------------------------------------------------
51 ** See the file ChangeLog for a revision history. */
52
53 /* This file splits into two entirely different pieces of code
54 depending on whether USE_PTYS is 1. The whole reason for all
55 the pty nonsense is that select() does not work on pipes in System-V
56 derivatives (at least some of them). This is a problem because
57 XtAppAddInput works by adding its argument to a select that is done
58 deep inside Xlib.
59 */
60
61 #include "config.h"
62
63 #include <stdio.h>
64 #include <signal.h>
65 #if HAVE_UNISTD_H
66 # include <unistd.h>
67 #endif
68
69 #include "common.h"
70 #include "frontend.h"
71 #include "backend.h" /* for safeStrCpy */
72
73 #if !USE_PTYS
74 /* This code is for systems where pipes work properly */
75
76 void
SetUpChildIO(int to_prog[2],int from_prog[2])77 SetUpChildIO (int to_prog[2], int from_prog[2])
78 {
79 signal(SIGPIPE, SIG_IGN);
80 pipe(to_prog);
81 pipe(from_prog);
82 }
83
84 #else /* USE_PTYS == 1 */
85 /* This code is for all systems where we must use ptys */
86
87 #include <errno.h>
88 #include <sys/stat.h>
89 #include <sys/ioctl.h>
90 #if HAVE_STROPTS_H
91 # include <stropts.h>
92 #endif /* HAVE_STROPTS_H */
93 #if HAVE_SYS_FCNTL_H
94 # include <sys/fcntl.h>
95 #else /* not HAVE_SYS_FCNTL_H */
96 # if HAVE_FCNTL_H
97 # include <fcntl.h>
98 # endif /* HAVE_FCNTL_H */
99 #endif /* not HAVE_SYS_FCNTL_H */
100
101 int PseudoTTY P((char pty_name[]));
102
103 int
SetUpChildIO(int to_prog[2],int from_prog[2])104 SetUpChildIO (int to_prog[2], int from_prog[2])
105 {
106 char pty_name[MSG_SIZ];
107
108 if ((to_prog[1] = PseudoTTY(pty_name)) == -1) {
109 DisplayFatalError("Can't open pseudo-tty", errno, 1);
110 ExitEvent(1);
111 }
112 from_prog[0] = to_prog[1];
113 to_prog[0] = from_prog[1] = open(pty_name, O_RDWR, 0);
114
115 #if HAVE_STROPTS_H /* do we really need this?? pipe-like behavior is fine */
116 if (ioctl (to_prog[0], I_PUSH, "ptem") == -1 ||
117 ioctl (to_prog[0], I_PUSH, "ldterm") == -1 ||
118 ioctl (to_prog[0], I_PUSH, "ttcompat") == -1) {
119 # ifdef NOTDEF /* seems some systems don't have or need ptem and ttcompat */
120 DisplayFatalError("Can't ioctl pseudo-tty", errno, 1);
121 ExitEvent(1);
122 # endif /*NOTDEF*/
123 }
124 #endif /* HAVE_STROPTS_H */
125
126 }
127
128 #if HAVE_GRANTPT
129 /* This code is for SVR4 */
130
131 int
PseudoTTY(char pty_name[])132 PseudoTTY (char pty_name[])
133 {
134 extern char *ptsname();
135 char *ptss;
136 int fd;
137
138 fd = open("/dev/ptmx", O_RDWR);
139 if (fd < 0) return fd;
140 if (grantpt(fd) == -1) return -1;
141 if (unlockpt(fd) == -1) return -1;
142 if (!(ptss = ptsname(fd))) return -1;
143 safeStrCpy(pty_name, ptss, sizeof(pty_name)/sizeof(pty_name[0]));
144 return fd;
145 }
146
147 #else /* not HAVE_GRANTPT */
148 #if HAVE__GETPTY
149 /* This code is for IRIX */
150
151 int
PseudoTTY(char pty_name[])152 PseudoTTY (char pty_name[])
153 {
154 int fd;
155 char *ptyn;
156
157 ptyn = _getpty(&fd, O_RDWR, 0600, 0);
158 if (ptyn == NULL) return -1;
159 safeStrCpy(pty_name, ptyn, sizeof(pty_name)/sizeof(pty_name[0]));
160 return fd;
161 }
162
163 #else /* not HAVE__GETPTY */
164 #if HAVE_LIBSEQ
165 /* This code is for Sequent DYNIX/ptx. Untested. --tpm */
166
167 int
PseudoTTY(char pty_name[])168 PseudoTTY (char pty_name[])
169 {
170 int fd;
171 char *slave, *master;
172
173 fd = getpseudotty(&slave, &master);
174 if (fd < 0) return fd;
175 safeStrCpy(pty_name, slave, sizeof(pty_name)/sizeof(pty_name[0]));
176 return fd;
177 }
178
179 #else /* not HAVE_LIBSEQ */
180 /* This code is for all other systems */
181 /* The code is adapted from GNU Emacs 19.24 */
182
183 #ifndef FIRST_PTY_LETTER
184 #define FIRST_PTY_LETTER 'p'
185 #endif
186 #ifndef LAST_PTY_LETTER
187 #define LAST_PTY_LETTER 'z'
188 #endif
189
190 int
PseudoTTY(char pty_name[])191 PseudoTTY (char pty_name[])
192 {
193 struct stat stb;
194 register c, i;
195 int fd;
196
197 /* Some systems name their pseudoterminals so that there are gaps in
198 the usual sequence - for example, on HP9000/S700 systems, there
199 are no pseudoterminals with names ending in 'f'. So we wait for
200 three failures in a row before deciding that we've reached the
201 end of the ptys. */
202 int failed_count = 0;
203
204 #ifdef PTY_ITERATION
205 PTY_ITERATION
206 #else
207 for (c = FIRST_PTY_LETTER; c <= LAST_PTY_LETTER; c++)
208 for (i = 0; i < 16; i++)
209 #endif
210 {
211 #ifdef PTY_NAME_SPRINTF
212 PTY_NAME_SPRINTF
213 #else
214 sprintf (pty_name, "/dev/pty%c%x", c, i);
215 #endif /* no PTY_NAME_SPRINTF */
216
217 #ifdef PTY_OPEN
218 PTY_OPEN;
219 #else /* no PTY_OPEN */
220 if (stat (pty_name, &stb) < 0)
221 {
222 failed_count++;
223 if (failed_count >= 3)
224 return -1;
225 }
226 else
227 failed_count = 0;
228 fd = open (pty_name, O_RDWR, 0);
229 #endif /* no PTY_OPEN */
230
231 if (fd >= 0)
232 {
233 /* check to make certain that both sides are available
234 this avoids a nasty yet stupid bug in rlogins */
235 #ifdef PTY_TTY_NAME_SPRINTF
236 PTY_TTY_NAME_SPRINTF
237 #else
238 sprintf (pty_name, "/dev/tty%c%x", c, i);
239 #endif /* no PTY_TTY_NAME_SPRINTF */
240 #ifndef UNIPLUS
241 if (access (pty_name, 6) != 0)
242 {
243 close (fd);
244 continue;
245 }
246 #endif /* not UNIPLUS */
247 #ifdef IBMRTAIX
248 /* On AIX, the parent gets SIGHUP when a pty attached
249 child dies. So, we ignore SIGHUP once we've started
250 a child on a pty. Note that this may cause xboard
251 not to die when it should, i.e., when its own
252 controlling tty goes away.
253 */
254 signal(SIGHUP, SIG_IGN);
255 #endif /* IBMRTAIX */
256 return fd;
257 }
258 }
259 return -1;
260 }
261
262 #endif /* not HAVE_LIBSEQ */
263 #endif /* not HAVE__GETPTY */
264 #endif /* not HAVE_GRANTPT */
265 #endif /* USE_PTYS */
266