cleanup: use size_t for new_areas_index and max_new_areas
[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 #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     environ = envp;
197     /* Exec the program. */
198     if (search)
199       execvp(program, argv);
200     else
201       execv(program, argv);
202
203     /* When exec fails and channel is available, send the errno value. */
204     if (-1 != channel[1]) {
205         int our_errno = errno;
206         int bytes = sizeof(int);
207         int n;
208         char *p = (char*)&our_errno;
209         while ((bytes > 0) &&
210                (n = write(channel[1], p, bytes))) {
211             if (-1 == n) {
212                 if (EINTR == errno) {
213                     continue;
214                 } else {
215                     break;
216                 }
217             } else {
218                 bytes -= n;
219                 p += n;
220             }
221         }
222         close(channel[1]);
223     }
224     _exit(1);
225 }
226 #else  /* !LISP_FEATURE_WIN32 */
227
228 #  include <windows.h>
229 #  include <process.h>
230 #  include <stdio.h>
231 #  include <stdlib.h>
232 #  include <fcntl.h>
233 #  include <io.h>
234
235 #define   READ_HANDLE  0
236 #define   WRITE_HANDLE 1
237
238 /* These functions do not attempt to deal with wchar_t variations. */
239
240 /* Get the value of _environ maintained by MSVCRT */
241 char **msvcrt_environ ( void ) {
242     return ( _environ );
243 }
244
245 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
246 HANDLE spawn (
247     const char *program,
248     const char *const *argv,
249     int in,
250     int out,
251     int err,
252     int search,
253     char *envp,
254     char *ptyname,
255     int wait
256     )
257 {
258     int stdout_backup, stdin_backup, stderr_backup, wait_mode;
259     HANDLE hProcess;
260     HANDLE hReturn;
261
262     /* Duplicate and save the original stdin/out/err handles. */
263     stdout_backup = _dup (  _fileno ( stdout ) );
264     stdin_backup  = _dup (  _fileno ( stdin  ) );
265     stderr_backup = _dup (  _fileno ( stderr ) );
266
267     /* If we are not using stdin/out/err
268      * then duplicate the new pipes to current stdin/out/err handles.
269      *
270      * Default std fds are used if in, out or err parameters
271      * are -1. */
272
273     hReturn = (HANDLE)-1;
274     hProcess = (HANDLE)-1;
275     if ( ( out >= 0 ) && ( out != _fileno ( stdout ) ) ) {
276         if ( _dup2 ( out, _fileno ( stdout ) ) != 0 ) goto error_exit;
277     }
278     if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
279         if ( _dup2 ( in,  _fileno ( stdin )  ) != 0 ) goto error_exit_out;
280     }
281     if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
282         if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
283     }
284
285     /* Set the wait mode. */
286     if ( 0 == wait ) {
287         wait_mode = P_NOWAIT;
288     } else {
289         wait_mode = P_WAIT;
290     }
291
292     /* Spawn process given on the command line*/
293     if (search)
294         hProcess = (HANDLE) spawnvp ( wait_mode, program, argv );
295     else
296         hProcess = (HANDLE) spawnv ( wait_mode, program, argv );
297
298     /* Now that the process is launched, replace the original
299      * in/out/err handles and close the backups. */
300
301     if ( _dup2 ( stderr_backup, _fileno ( stderr ) ) != 0 ) goto error_exit;
302  error_exit_in:
303     if ( _dup2 ( stdin_backup,  _fileno ( stdin )  ) != 0 ) goto error_exit;
304  error_exit_out:
305     if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
306
307     hReturn = hProcess;
308
309  error_exit:
310     close ( stdout_backup );
311     close ( stdin_backup  );
312     close ( stderr_backup );
313
314     return hReturn;
315
316 }
317
318
319 #endif /* !LISP_FEATURE_WIN32 */