Port to x86-64 versions of Windows
[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 #include <sys/wait.h>
29 #include <sys/ioctl.h>
30 #include <termios.h>
31 #include <errno.h>
32
33 #ifdef LISP_FEATURE_OPENBSD
34 /* FIXME: there has to be a better way to avoid ./util.h here */
35 #include </usr/include/util.h>
36 #endif
37
38 /* borrowed from detachtty's detachtty.c, in turn borrowed from APUE
39  * example code found at
40  * http://www.yendor.com/programming/unix/apue/pty/main.c
41
42 -brkint
43
44  */
45
46 int set_noecho(int fd)
47 {
48     struct termios  stermios;
49
50     if (tcgetattr(fd, &stermios) < 0) return 0;
51
52     stermios.c_lflag &= ~(  ECHO | /* ECHOE |  ECHOK | */  ECHONL);
53     stermios.c_oflag |= (ONLCR);
54     stermios.c_iflag &= ~(BRKINT);
55     stermios.c_iflag |= (ICANON|ICRNL);
56
57     stermios.c_cc[VERASE]=0177;
58     if (tcsetattr(fd, TCSANOW, &stermios) < 0) return 0;
59     return 1;
60 }
61
62 #if defined(LISP_FEATURE_OPENBSD)
63
64 int
65 set_pty(char *pty_name)
66 {
67     int fd;
68
69     if ((fd = open(pty_name, O_RDWR, 0)) == -1 ||
70         login_tty(fd) == -1)
71         return (0);
72     return (set_noecho(STDIN_FILENO));
73 }
74
75 #else /* !LISP_FEATURE_OPENBSD */
76
77 int
78 set_pty(char *pty_name)
79 {
80     int fd;
81
82 #if !defined(LISP_FEATURE_HPUX) && !defined(SVR4)
83     fd = open("/dev/tty", O_RDWR, 0);
84     if (fd >= 0) {
85         ioctl(fd, TIOCNOTTY, 0);
86         close(fd);
87     }
88 #endif
89     if ((fd = open(pty_name, O_RDWR, 0)) == -1)
90         return (-1);
91     dup2(fd, 0);
92     set_noecho(0);
93     dup2(fd, 1);
94     dup2(fd, 2);
95     close(fd);
96     return (0);
97 }
98
99 #endif /* !LISP_FEATURE_OPENBSD */
100
101 extern char **environ;
102 int spawn(char *program, char *argv[], int sin, int sout, int serr,
103           int search, char *envp[], char *pty_name, int wait)
104 {
105     pid_t pid;
106     int fd;
107     int channel[2];
108     sigset_t sset;
109
110     channel[0] = -1;
111     channel[1] = -1;
112     if (!pipe(channel)) {
113         if (-1==fcntl(channel[1], F_SETFD,  FD_CLOEXEC)) {
114             close(channel[1]);
115             channel[1] = -1;
116         }
117     }
118
119     pid = fork();
120     if (pid) {
121         if ((-1 != pid) && (-1 != channel[1])) {
122             int child_errno = 0;
123             int bytes = sizeof(int);
124             int n;
125             char *p = (char*)&child_errno;
126             close(channel[1]);
127             /* Try to read child errno from channel. */
128             while ((bytes > 0) &&
129                    (n = read(channel[0], p, bytes))) {
130                 if (-1 == n) {
131                     if (EINTR == errno) {
132                         continue;
133                     } else {
134                         break;
135                     }
136                 } else {
137                     bytes -= n;
138                     p += n;
139                 }
140             }
141             close(channel[0]);
142             if (child_errno) {
143                 waitpid(pid, NULL, 0);
144                 /* Our convention to tell Lisp that it was the exec that
145                  * failed, not the fork. */
146                 pid = -2;
147                 errno = child_errno;
148             }
149         }
150         return pid;
151     }
152     close (channel[0]);
153
154     /* Put us in our own process group, but only if we need not
155      * share stdin with our parent. In the latter case we claim
156      * control of the terminal. */
157     if (sin >= 0) {
158 #if defined(LISP_FEATURE_HPUX) || defined(LISP_FEATURE_OPENBSD)
159       setsid();
160 #elif defined(LISP_FEATURE_DARWIN)
161       setpgid(0, getpid());
162 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
163       setpgrp();
164 #else
165       setpgrp(0, getpid());
166 #endif
167     } else {
168       tcsetpgrp(0, getpgrp());
169     }
170
171     /* unblock signals */
172     sigemptyset(&sset);
173     sigprocmask(SIG_SETMASK, &sset, NULL);
174
175     /* If we are supposed to be part of some other pty, go for it. */
176     if (pty_name)
177         set_pty(pty_name);
178     else {
179     /* Set up stdin, stdout, and stderr */
180     if (sin >= 0)
181         dup2(sin, 0);
182     if (sout >= 0)
183         dup2(sout, 1);
184     if (serr >= 0)
185         dup2(serr, 2);
186     }
187     /* Close all other fds. */
188 #ifdef SVR4
189     for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
190         if (fd != channel[1]) close(fd);
191 #else
192     for (fd = getdtablesize()-1; fd >= 3; fd--)
193         if (fd != channel[1]) close(fd);
194 #endif
195
196     if (envp) {
197       environ = envp;
198     }
199     /* Exec the program. */
200     if (search)
201       execvp(program, argv);
202     else
203       execv(program, argv);
204
205     /* When exec fails and channel is available, send the errno value. */
206     if (-1 != channel[1]) {
207         int our_errno = errno;
208         int bytes = sizeof(int);
209         int n;
210         char *p = (char*)&our_errno;
211         while ((bytes > 0) &&
212                (n = write(channel[1], p, bytes))) {
213             if (-1 == n) {
214                 if (EINTR == errno) {
215                     continue;
216                 } else {
217                     break;
218                 }
219             } else {
220                 bytes -= n;
221                 p += n;
222             }
223         }
224         close(channel[1]);
225     }
226     _exit(1);
227 }
228 #else  /* !LISP_FEATURE_WIN32 */
229
230 #  include <windows.h>
231 #  include <process.h>
232 #  include <stdio.h>
233 #  include <stdlib.h>
234 #  include <fcntl.h>
235 #  include <io.h>
236
237 #define   READ_HANDLE  0
238 #define   WRITE_HANDLE 1
239
240 /* These functions do not attempt to deal with wchar_t variations. */
241
242 /* Get the value of _environ maintained by MSVCRT */
243 char **msvcrt_environ ( void ) {
244     return ( _environ );
245 }
246
247 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
248 HANDLE spawn (
249     const char *program,
250     const char *const *argv,
251     int in,
252     int out,
253     int err,
254     int search,
255     char *envp,
256     char *ptyname,
257     int wait
258     )
259 {
260     int stdout_backup, stdin_backup, stderr_backup, wait_mode;
261     HANDLE hProcess;
262     HANDLE hReturn;
263
264     /* Duplicate and save the original stdin/out/err handles. */
265     stdout_backup = _dup (  _fileno ( stdout ) );
266     stdin_backup  = _dup (  _fileno ( stdin  ) );
267     stderr_backup = _dup (  _fileno ( stderr ) );
268
269     /* If we are not using stdin/out/err
270      * then duplicate the new pipes to current stdin/out/err handles.
271      *
272      * Default std fds are used if in, out or err parameters
273      * are -1. */
274
275     hReturn = (HANDLE)-1;
276     hProcess = (HANDLE)-1;
277     if ( ( out >= 0 ) && ( out != _fileno ( stdout ) ) ) {
278         if ( _dup2 ( out, _fileno ( stdout ) ) != 0 ) goto error_exit;
279     }
280     if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
281         if ( _dup2 ( in,  _fileno ( stdin )  ) != 0 ) goto error_exit_out;
282     }
283     if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
284         if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
285     }
286
287     /* Set the wait mode. */
288     if ( 0 == wait ) {
289         wait_mode = P_NOWAIT;
290     } else {
291         wait_mode = P_WAIT;
292     }
293
294     /* Spawn process given on the command line*/
295     if (search)
296         hProcess = (HANDLE) spawnvp ( wait_mode, program, (char* const* )argv );
297     else
298         hProcess = (HANDLE) spawnv ( wait_mode, program, (char* const* )argv );
299
300     /* Now that the process is launched, replace the original
301      * in/out/err handles and close the backups. */
302
303     if ( _dup2 ( stderr_backup, _fileno ( stderr ) ) != 0 ) goto error_exit;
304  error_exit_in:
305     if ( _dup2 ( stdin_backup,  _fileno ( stdin )  ) != 0 ) goto error_exit;
306  error_exit_out:
307     if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
308
309     hReturn = hProcess;
310
311  error_exit:
312     close ( stdout_backup );
313     close ( stdin_backup  );
314     close ( stderr_backup );
315
316     return hReturn;
317
318 }
319
320
321 #endif /* !LISP_FEATURE_WIN32 */