More conservative defaults in GENCGC
[sbcl.git] / src / runtime / run-program.c
1 /*
2  * support for the Lisp function RUN-PROGRAM and friends
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15
16 #include "sbcl.h"
17
18 #ifndef LISP_FEATURE_WIN32
19
20 #include <stdlib.h>
21 #include <sys/file.h>
22 #include <sys/types.h>
23 #include <signal.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <sys/ioctl.h>
27 #include <unistd.h>
28
29 #include <sys/ioctl.h>
30 #include <termios.h>
31
32 #ifdef LISP_FEATURE_OPENBSD
33 /* FIXME: there has to be a better way to avoid ./util.h here */
34 #include </usr/include/util.h>
35 #endif
36
37 /* borrowed from detachtty's detachtty.c, in turn borrowed from APUE
38  * example code found at
39  * http://www.yendor.com/programming/unix/apue/pty/main.c
40
41 -brkint
42
43  */
44
45 int set_noecho(int fd)
46 {
47     struct termios  stermios;
48
49     if (tcgetattr(fd, &stermios) < 0) return 0;
50
51     stermios.c_lflag &= ~(  ECHO | /* ECHOE |  ECHOK | */  ECHONL);
52     stermios.c_oflag |= (ONLCR);
53     stermios.c_iflag &= ~(BRKINT);
54     stermios.c_iflag |= (ICANON|ICRNL);
55
56     stermios.c_cc[VERASE]=0177;
57     if (tcsetattr(fd, TCSANOW, &stermios) < 0) return 0;
58     return 1;
59 }
60
61 #if defined(LISP_FEATURE_OPENBSD)
62
63 int
64 set_pty(char *pty_name)
65 {
66     int fd;
67
68     if ((fd = open(pty_name, O_RDWR, 0)) == -1 ||
69         login_tty(fd) == -1)
70         return (0);
71     return (set_noecho(STDIN_FILENO));
72 }
73
74 #else /* !LISP_FEATURE_OPENBSD */
75
76 int
77 set_pty(char *pty_name)
78 {
79     int fd;
80
81 #if !defined(LISP_FEATURE_HPUX) && !defined(SVR4)
82     fd = open("/dev/tty", O_RDWR, 0);
83     if (fd >= 0) {
84         ioctl(fd, TIOCNOTTY, 0);
85         close(fd);
86     }
87 #endif
88     if ((fd = open(pty_name, O_RDWR, 0)) == -1)
89         return (-1);
90     dup2(fd, 0);
91     set_noecho(0);
92     dup2(fd, 1);
93     dup2(fd, 2);
94     close(fd);
95     return (0);
96 }
97
98 #endif /* !LISP_FEATURE_OPENBSD */
99
100 extern char **environ;
101 int spawn(char *program, char *argv[], int sin, int sout, int serr,
102           int search, char *envp[], char *pty_name, int wait)
103 {
104     int pid = fork();
105     int fd;
106     sigset_t sset;
107
108     if (pid != 0)
109         return pid;
110
111     /* Put us in our own process group, but only if we need not
112      * share stdin with our parent. In the latter case we claim
113      * control of the terminal. */
114     if (sin >= 0) {
115 #if defined(LISP_FEATURE_HPUX) || defined(LISP_FEATURE_OPENBSD)
116       setsid();
117 #elif defined(LISP_FEATURE_DARWIN)
118       setpgid(0, getpid());
119 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
120       setpgrp();
121 #else
122       setpgrp(0, getpid());
123 #endif
124     } else {
125       tcsetpgrp(0, getpgrp());
126     }
127
128     /* unblock signals */
129     sigemptyset(&sset);
130     sigprocmask(SIG_SETMASK, &sset, NULL);
131
132     /* If we are supposed to be part of some other pty, go for it. */
133     if (pty_name)
134         set_pty(pty_name);
135     else {
136     /* Set up stdin, stdout, and stderr */
137     if (sin >= 0)
138         dup2(sin, 0);
139     if (sout >= 0)
140         dup2(sout, 1);
141     if (serr >= 0)
142         dup2(serr, 2);
143     }
144     /* Close all other fds. */
145 #ifdef SVR4
146     for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
147         close(fd);
148 #else
149     for (fd = getdtablesize()-1; fd >= 3; fd--)
150         close(fd);
151 #endif
152
153     environ = envp;
154     /* Exec the program. */
155     if (search)
156       execvp(program, argv);
157     else
158       execv(program, argv);
159
160     exit (1);
161 }
162 #else  /* !LISP_FEATURE_WIN32 */
163
164 #  include <windows.h>
165 #  include <process.h>
166 #  include <stdio.h>
167 #  include <stdlib.h>
168 #  include <fcntl.h>
169 #  include <io.h>
170
171 #define   READ_HANDLE  0
172 #define   WRITE_HANDLE 1
173
174 /* These functions do not attempt to deal with wchar_t variations. */
175
176 /* Get the value of _environ maintained by MSVCRT */
177 char **msvcrt_environ ( void ) {
178     return ( _environ );
179 }
180
181 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
182 HANDLE spawn (
183     const char *program,
184     const char *const *argv,
185     int in,
186     int out,
187     int err,
188     int search,
189     char *envp,
190     char *ptyname,
191     int wait
192     )
193 {
194     int stdout_backup, stdin_backup, stderr_backup, wait_mode;
195     HANDLE hProcess;
196     HANDLE hReturn;
197
198     /* Duplicate and save the original stdin/out/err handles. */
199     stdout_backup = _dup (  _fileno ( stdout ) );
200     stdin_backup  = _dup (  _fileno ( stdin  ) );
201     stderr_backup = _dup (  _fileno ( stderr ) );
202
203     /* If we are not using stdin/out/err
204      * then duplicate the new pipes to current stdin/out/err handles.
205      *
206      * Default std fds are used if in, out or err parameters
207      * are -1. */
208
209     hReturn = (HANDLE)-1;
210     hProcess = (HANDLE)-1;
211     if ( ( out >= 0 ) && ( out != _fileno ( stdout ) ) ) {
212         if ( _dup2 ( out, _fileno ( stdout ) ) != 0 ) goto error_exit;
213     }
214     if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
215         if ( _dup2 ( in,  _fileno ( stdin )  ) != 0 ) goto error_exit_out;
216     }
217     if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
218         if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
219     }
220
221     /* Set the wait mode. */
222     if ( 0 == wait ) {
223         wait_mode = P_NOWAIT;
224     } else {
225         wait_mode = P_WAIT;
226     }
227
228     /* Spawn process given on the command line*/
229     if (search)
230         hProcess = (HANDLE) spawnvp ( wait_mode, program, argv );
231     else
232         hProcess = (HANDLE) spawnv ( wait_mode, program, argv );
233
234     /* Now that the process is launched, replace the original
235      * in/out/err handles and close the backups. */
236
237     if ( _dup2 ( stderr_backup, _fileno ( stderr ) ) != 0 ) goto error_exit;
238  error_exit_in:
239     if ( _dup2 ( stdin_backup,  _fileno ( stdin )  ) != 0 ) goto error_exit;
240  error_exit_out:
241     if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
242
243     hReturn = hProcess;
244
245  error_exit:
246     close ( stdout_backup );
247     close ( stdin_backup  );
248     close ( stderr_backup );
249
250     return hReturn;
251
252 }
253
254
255 #endif /* !LISP_FEATURE_WIN32 */