cleanup: refactor copy_large_unboxed_object and copy_large_object
[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             if (child_errno) {
142                 waitpid(pid, NULL, 0);
143                 /* Our convention to tell Lisp that it was the exec that
144                  * failed, not the fork. */
145                 pid = -2;
146                 errno = child_errno;
147             }
148         }
149         return pid;
150     }
151     close (channel[0]);
152
153     /* Put us in our own process group, but only if we need not
154      * share stdin with our parent. In the latter case we claim
155      * control of the terminal. */
156     if (sin >= 0) {
157 #if defined(LISP_FEATURE_HPUX) || defined(LISP_FEATURE_OPENBSD)
158       setsid();
159 #elif defined(LISP_FEATURE_DARWIN)
160       setpgid(0, getpid());
161 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
162       setpgrp();
163 #else
164       setpgrp(0, getpid());
165 #endif
166     } else {
167       tcsetpgrp(0, getpgrp());
168     }
169
170     /* unblock signals */
171     sigemptyset(&sset);
172     sigprocmask(SIG_SETMASK, &sset, NULL);
173
174     /* If we are supposed to be part of some other pty, go for it. */
175     if (pty_name)
176         set_pty(pty_name);
177     else {
178     /* Set up stdin, stdout, and stderr */
179     if (sin >= 0)
180         dup2(sin, 0);
181     if (sout >= 0)
182         dup2(sout, 1);
183     if (serr >= 0)
184         dup2(serr, 2);
185     }
186     /* Close all other fds. */
187 #ifdef SVR4
188     for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
189         if (fd != channel[1]) close(fd);
190 #else
191     for (fd = getdtablesize()-1; fd >= 3; fd--)
192         if (fd != channel[1]) close(fd);
193 #endif
194
195     environ = envp;
196     /* Exec the program. */
197     if (search)
198       execvp(program, argv);
199     else
200       execv(program, argv);
201
202     /* When exec fails and channel is available, send the errno value. */
203     if (-1 != channel[1]) {
204         int our_errno = errno;
205         int bytes = sizeof(int);
206         int n;
207         char *p = (char*)&our_errno;
208         while ((bytes > 0) &&
209                (n = write(channel[1], p, bytes))) {
210             if (-1 == n) {
211                 if (EINTR == errno) {
212                     continue;
213                 } else {
214                     break;
215                 }
216             } else {
217                 bytes -= n;
218                 p += n;
219             }
220         }
221         close(channel[1]);
222     }
223     _exit(1);
224 }
225 #else  /* !LISP_FEATURE_WIN32 */
226
227 #  include <windows.h>
228 #  include <process.h>
229 #  include <stdio.h>
230 #  include <stdlib.h>
231 #  include <fcntl.h>
232 #  include <io.h>
233
234 #define   READ_HANDLE  0
235 #define   WRITE_HANDLE 1
236
237 /* These functions do not attempt to deal with wchar_t variations. */
238
239 /* Get the value of _environ maintained by MSVCRT */
240 char **msvcrt_environ ( void ) {
241     return ( _environ );
242 }
243
244 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
245 HANDLE spawn (
246     const char *program,
247     const char *const *argv,
248     int in,
249     int out,
250     int err,
251     int search,
252     char *envp,
253     char *ptyname,
254     int wait
255     )
256 {
257     int stdout_backup, stdin_backup, stderr_backup, wait_mode;
258     HANDLE hProcess;
259     HANDLE hReturn;
260
261     /* Duplicate and save the original stdin/out/err handles. */
262     stdout_backup = _dup (  _fileno ( stdout ) );
263     stdin_backup  = _dup (  _fileno ( stdin  ) );
264     stderr_backup = _dup (  _fileno ( stderr ) );
265
266     /* If we are not using stdin/out/err
267      * then duplicate the new pipes to current stdin/out/err handles.
268      *
269      * Default std fds are used if in, out or err parameters
270      * are -1. */
271
272     hReturn = (HANDLE)-1;
273     hProcess = (HANDLE)-1;
274     if ( ( out >= 0 ) && ( out != _fileno ( stdout ) ) ) {
275         if ( _dup2 ( out, _fileno ( stdout ) ) != 0 ) goto error_exit;
276     }
277     if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
278         if ( _dup2 ( in,  _fileno ( stdin )  ) != 0 ) goto error_exit_out;
279     }
280     if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
281         if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
282     }
283
284     /* Set the wait mode. */
285     if ( 0 == wait ) {
286         wait_mode = P_NOWAIT;
287     } else {
288         wait_mode = P_WAIT;
289     }
290
291     /* Spawn process given on the command line*/
292     if (search)
293         hProcess = (HANDLE) spawnvp ( wait_mode, program, argv );
294     else
295         hProcess = (HANDLE) spawnv ( wait_mode, program, argv );
296
297     /* Now that the process is launched, replace the original
298      * in/out/err handles and close the backups. */
299
300     if ( _dup2 ( stderr_backup, _fileno ( stderr ) ) != 0 ) goto error_exit;
301  error_exit_in:
302     if ( _dup2 ( stdin_backup,  _fileno ( stdin )  ) != 0 ) goto error_exit;
303  error_exit_out:
304     if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
305
306     hReturn = hProcess;
307
308  error_exit:
309     close ( stdout_backup );
310     close ( stdin_backup  );
311     close ( stderr_backup );
312
313     return hReturn;
314
315 }
316
317
318 #endif /* !LISP_FEATURE_WIN32 */