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