-
Notifications
You must be signed in to change notification settings - Fork 5
/
childio.c
265 lines (235 loc) · 7.38 KB
/
childio.c
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
/*
* childio.c -- set up communication with child processes
*
* Copyright 1991 by Digital Equipment Corporation, Maynard,
* Massachusetts.
*
* Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
* 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free
* Software Foundation, Inc.
*
* The following terms apply to Digital Equipment Corporation's copyright
* interest in XBoard:
* ------------------------------------------------------------------------
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of Digital not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
* DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
* ------------------------------------------------------------------------
*
* The following terms apply to the enhanced version of XBoard
* distributed by the Free Software Foundation:
* ------------------------------------------------------------------------
*
* GNU XBoard is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* GNU XBoard is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/. *
*
*------------------------------------------------------------------------
** See the file ChangeLog for a revision history. */
/* This file splits into two entirely different pieces of code
depending on whether USE_PTYS is 1. The whole reason for all
the pty nonsense is that select() does not work on pipes in System-V
derivatives (at least some of them). This is a problem because
XtAppAddInput works by adding its argument to a select that is done
deep inside Xlib.
*/
#include "config.h"
#include <stdio.h>
#include <signal.h>
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#include "common.h"
#include "frontend.h"
#include "backend.h" /* for safeStrCpy */
#if !USE_PTYS
/* This code is for systems where pipes work properly */
void
SetUpChildIO (int to_prog[2], int from_prog[2])
{
signal(SIGPIPE, SIG_IGN);
pipe(to_prog);
pipe(from_prog);
}
#else /* USE_PTYS == 1 */
/* This code is for all systems where we must use ptys */
#include <errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#if HAVE_STROPTS_H
# include <stropts.h>
#endif /* HAVE_STROPTS_H */
#if HAVE_SYS_FCNTL_H
# include <sys/fcntl.h>
#else /* not HAVE_SYS_FCNTL_H */
# if HAVE_FCNTL_H
# include <fcntl.h>
# endif /* HAVE_FCNTL_H */
#endif /* not HAVE_SYS_FCNTL_H */
int PseudoTTY P((char pty_name[]));
int
SetUpChildIO (int to_prog[2], int from_prog[2])
{
char pty_name[MSG_SIZ];
if ((to_prog[1] = PseudoTTY(pty_name)) == -1) {
DisplayFatalError("Can't open pseudo-tty", errno, 1);
ExitEvent(1);
}
from_prog[0] = to_prog[1];
to_prog[0] = from_prog[1] = open(pty_name, O_RDWR, 0);
#if HAVE_STROPTS_H /* do we really need this?? pipe-like behavior is fine */
if (ioctl (to_prog[0], I_PUSH, "ptem") == -1 ||
ioctl (to_prog[0], I_PUSH, "ldterm") == -1 ||
ioctl (to_prog[0], I_PUSH, "ttcompat") == -1) {
# ifdef NOTDEF /* seems some systems don't have or need ptem and ttcompat */
DisplayFatalError("Can't ioctl pseudo-tty", errno, 1);
ExitEvent(1);
# endif /*NOTDEF*/
}
#endif /* HAVE_STROPTS_H */
}
#if HAVE_GRANTPT
/* This code is for SVR4 */
int
PseudoTTY (char pty_name[])
{
extern char *ptsname();
char *ptss;
int fd;
fd = open("/dev/ptmx", O_RDWR);
if (fd < 0) return fd;
if (grantpt(fd) == -1) return -1;
if (unlockpt(fd) == -1) return -1;
if (!(ptss = ptsname(fd))) return -1;
safeStrCpy(pty_name, ptss, sizeof(pty_name)/sizeof(pty_name[0]));
return fd;
}
#else /* not HAVE_GRANTPT */
#if HAVE__GETPTY
/* This code is for IRIX */
int
PseudoTTY (char pty_name[])
{
int fd;
char *ptyn;
ptyn = _getpty(&fd, O_RDWR, 0600, 0);
if (ptyn == NULL) return -1;
safeStrCpy(pty_name, ptyn, sizeof(pty_name)/sizeof(pty_name[0]));
return fd;
}
#else /* not HAVE__GETPTY */
#if HAVE_LIBSEQ
/* This code is for Sequent DYNIX/ptx. Untested. --tpm */
int
PseudoTTY (char pty_name[])
{
int fd;
char *slave, *master;
fd = getpseudotty(&slave, &master);
if (fd < 0) return fd;
safeStrCpy(pty_name, slave, sizeof(pty_name)/sizeof(pty_name[0]));
return fd;
}
#else /* not HAVE_LIBSEQ */
/* This code is for all other systems */
/* The code is adapted from GNU Emacs 19.24 */
#ifndef FIRST_PTY_LETTER
#define FIRST_PTY_LETTER 'p'
#endif
#ifndef LAST_PTY_LETTER
#define LAST_PTY_LETTER 'z'
#endif
int
PseudoTTY (char pty_name[])
{
struct stat stb;
register c, i;
int fd;
/* Some systems name their pseudoterminals so that there are gaps in
the usual sequence - for example, on HP9000/S700 systems, there
are no pseudoterminals with names ending in 'f'. So we wait for
three failures in a row before deciding that we've reached the
end of the ptys. */
int failed_count = 0;
#ifdef PTY_ITERATION
PTY_ITERATION
#else
for (c = FIRST_PTY_LETTER; c <= LAST_PTY_LETTER; c++)
for (i = 0; i < 16; i++)
#endif
{
#ifdef PTY_NAME_SPRINTF
PTY_NAME_SPRINTF
#else
sprintf (pty_name, "/dev/pty%c%x", c, i);
#endif /* no PTY_NAME_SPRINTF */
#ifdef PTY_OPEN
PTY_OPEN;
#else /* no PTY_OPEN */
if (stat (pty_name, &stb) < 0)
{
failed_count++;
if (failed_count >= 3)
return -1;
}
else
failed_count = 0;
fd = open (pty_name, O_RDWR, 0);
#endif /* no PTY_OPEN */
if (fd >= 0)
{
/* check to make certain that both sides are available
this avoids a nasty yet stupid bug in rlogins */
#ifdef PTY_TTY_NAME_SPRINTF
PTY_TTY_NAME_SPRINTF
#else
sprintf (pty_name, "/dev/tty%c%x", c, i);
#endif /* no PTY_TTY_NAME_SPRINTF */
#ifndef UNIPLUS
if (access (pty_name, 6) != 0)
{
close (fd);
continue;
}
#endif /* not UNIPLUS */
#ifdef IBMRTAIX
/* On AIX, the parent gets SIGHUP when a pty attached
child dies. So, we ignore SIGHUP once we've started
a child on a pty. Note that this may cause xboard
not to die when it should, i.e., when its own
controlling tty goes away.
*/
signal(SIGHUP, SIG_IGN);
#endif /* IBMRTAIX */
return fd;
}
}
return -1;
}
#endif /* not HAVE_LIBSEQ */
#endif /* not HAVE__GETPTY */
#endif /* not HAVE_GRANTPT */
#endif /* USE_PTYS */