Remove more disassembler bogosity
[sbcl.git] / src / runtime / run-program.c
blob4521146e36f5d1d1fe3decfd62bd5273da2a13ad
1 /*
2 * support for the Lisp function RUN-PROGRAM and friends
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
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.
16 #include "sbcl.h"
18 #ifndef LISP_FEATURE_WIN32
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>
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
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
42 -brkint
46 int set_noecho(int fd)
48 struct termios stermios;
50 if (tcgetattr(fd, &stermios) < 0) return 0;
52 stermios.c_lflag &= ~( ECHO | /* ECHOE | ECHOK | */ ECHONL);
53 stermios.c_oflag |= (ONLCR);
54 stermios.c_iflag &= ~(BRKINT);
55 stermios.c_iflag |= (ICANON|ICRNL);
57 stermios.c_cc[VERASE]=0177;
58 if (tcsetattr(fd, TCSANOW, &stermios) < 0) return 0;
59 return 1;
62 #if defined(LISP_FEATURE_OPENBSD)
64 int
65 set_pty(char *pty_name)
67 int fd;
69 if ((fd = open(pty_name, O_RDWR, 0)) == -1 ||
70 login_tty(fd) == -1)
71 return (0);
72 return (set_noecho(STDIN_FILENO));
75 #else /* !LISP_FEATURE_OPENBSD */
77 int
78 set_pty(char *pty_name)
80 int fd;
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);
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);
99 #endif /* !LISP_FEATURE_OPENBSD */
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, char *pwd)
105 pid_t pid;
106 int fd;
107 int channel[2];
108 sigset_t sset;
109 int failure_code = 2;
111 channel[0] = -1;
112 channel[1] = -1;
113 if (!pipe(channel)) {
114 if (-1==fcntl(channel[1], F_SETFD, FD_CLOEXEC)) {
115 close(channel[1]);
116 channel[1] = -1;
120 pid = fork();
121 if (pid) {
122 if ((-1 != pid) && (-1 != channel[1])) {
123 int child_errno = 0;
124 int bytes = sizeof(int);
125 int n;
126 char *p = (char*)&child_errno;
127 close(channel[1]);
128 /* Try to read child errno from channel. */
129 while ((bytes > 0) &&
130 (n = read(channel[0], p, bytes))) {
131 if (-1 == n) {
132 if (EINTR == errno) {
133 continue;
134 } else {
135 break;
137 } else {
138 bytes -= n;
139 p += n;
142 close(channel[0]);
143 if (child_errno) {
144 int status;
145 waitpid(pid, &status, 0);
146 /* Our convention to tell Lisp that it was the exec or
147 chdir that failed, not the fork. */
148 /* FIXME: there are other values waitpid(2) can return. */
149 if (WIFEXITED(status)) {
150 pid = -WEXITSTATUS(status);
152 errno = child_errno;
155 return pid;
157 close (channel[0]);
159 /* Put us in our own process group, but only if we need not
160 * share stdin with our parent. In the latter case we claim
161 * control of the terminal. */
162 if (sin >= 0) {
163 #if defined(LISP_FEATURE_HPUX) || defined(LISP_FEATURE_OPENBSD)
164 setsid();
165 #elif defined(LISP_FEATURE_DARWIN)
166 setpgid(0, getpid());
167 #elif defined(SVR4) || defined(__linux__) || defined(__osf__) || defined(__GLIBC__)
168 setpgrp();
169 #else
170 setpgrp(0, getpid());
171 #endif
172 } else {
173 tcsetpgrp(0, getpgrp());
176 /* unblock signals */
177 sigemptyset(&sset);
178 sigprocmask(SIG_SETMASK, &sset, NULL);
180 /* If we are supposed to be part of some other pty, go for it. */
181 if (pty_name)
182 set_pty(pty_name);
183 else {
184 /* Set up stdin, stdout, and stderr */
185 if (sin >= 0)
186 dup2(sin, 0);
187 if (sout >= 0)
188 dup2(sout, 1);
189 if (serr >= 0)
190 dup2(serr, 2);
192 /* Close all other fds. */
193 #ifdef SVR4
194 for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
195 if (fd != channel[1]) close(fd);
196 #else
197 for (fd = getdtablesize()-1; fd >= 3; fd--)
198 if (fd != channel[1]) close(fd);
199 #endif
201 if (pwd && chdir(pwd) < 0) {
202 failure_code = 3;
203 } else {
204 if (envp) {
205 environ = envp;
207 /* Exec the program. */
208 if (search)
209 execvp(program, argv);
210 else
211 execv(program, argv);
214 /* When exec or chdir fails and channel is available, send the errno value. */
215 if (-1 != channel[1]) {
216 int our_errno = errno;
217 int bytes = sizeof(int);
218 int n;
219 char *p = (char*)&our_errno;
220 while ((bytes > 0) &&
221 (n = write(channel[1], p, bytes))) {
222 if (-1 == n) {
223 if (EINTR == errno) {
224 continue;
225 } else {
226 break;
228 } else {
229 bytes -= n;
230 p += n;
233 close(channel[1]);
235 _exit(failure_code);
237 #else /* !LISP_FEATURE_WIN32 */
239 # include <windows.h>
240 # include <process.h>
241 # include <stdio.h>
242 # include <stdlib.h>
243 # include <fcntl.h>
244 # include <io.h>
246 #define READ_HANDLE 0
247 #define WRITE_HANDLE 1
249 /* These functions do not attempt to deal with wchar_t variations. */
251 /* Get the value of _environ maintained by MSVCRT */
252 char **msvcrt_environ ( void ) {
253 return ( _environ );
256 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
257 HANDLE spawn (
258 const char *program,
259 const char *const *argv,
260 int in,
261 int out,
262 int err,
263 int search,
264 char *envp,
265 char *ptyname,
266 int wait,
267 char *pwd
270 int stdout_backup, stdin_backup, stderr_backup, wait_mode;
271 HANDLE hProcess;
272 HANDLE hReturn;
274 /* Duplicate and save the original stdin/out/err handles. */
275 stdout_backup = _dup ( _fileno ( stdout ) );
276 stdin_backup = _dup ( _fileno ( stdin ) );
277 stderr_backup = _dup ( _fileno ( stderr ) );
279 /* If we are not using stdin/out/err
280 * then duplicate the new pipes to current stdin/out/err handles.
282 * Default std fds are used if in, out or err parameters
283 * are -1. */
285 hReturn = (HANDLE)-1;
286 hProcess = (HANDLE)-1;
287 if ( ( out >= 0 ) && ( out != _fileno ( stdout ) ) ) {
288 if ( _dup2 ( out, _fileno ( stdout ) ) != 0 ) goto error_exit;
290 if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
291 if ( _dup2 ( in, _fileno ( stdin ) ) != 0 ) goto error_exit_out;
293 if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
294 if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
297 /* Set the wait mode. */
298 if ( 0 == wait ) {
299 wait_mode = P_NOWAIT;
300 } else {
301 wait_mode = P_WAIT;
304 /* Change working directory if supplied. */
305 if (pwd) {
306 if (chdir(pwd) < 0) {
307 goto error_exit;
311 /* Spawn process given on the command line*/
312 if (search)
313 hProcess = (HANDLE) spawnvp ( wait_mode, program, (char* const* )argv );
314 else
315 hProcess = (HANDLE) spawnv ( wait_mode, program, (char* const* )argv );
317 /* Now that the process is launched, replace the original
318 * in/out/err handles and close the backups. */
320 if ( _dup2 ( stderr_backup, _fileno ( stderr ) ) != 0 ) goto error_exit;
321 error_exit_in:
322 if ( _dup2 ( stdin_backup, _fileno ( stdin ) ) != 0 ) goto error_exit;
323 error_exit_out:
324 if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
326 hReturn = hProcess;
328 error_exit:
329 close ( stdout_backup );
330 close ( stdin_backup );
331 close ( stderr_backup );
333 return hReturn;
338 #endif /* !LISP_FEATURE_WIN32 */