2 * support for the Lisp function RUN-PROGRAM and friends
6 * This software is part of the SBCL system. See the README file for
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.
18 #ifndef LISP_FEATURE_WIN32
22 #include <sys/types.h>
26 #include <sys/ioctl.h>
29 #include <sys/ioctl.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>
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
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;
62 #if defined(LISP_FEATURE_OPENBSD)
65 set_pty(char *pty_name)
69 if ((fd = open(pty_name, O_RDWR, 0)) == -1 ||
72 return (set_noecho(STDIN_FILENO));
75 #else /* !LISP_FEATURE_OPENBSD */
78 set_pty(char *pty_name)
82 #if !defined(LISP_FEATURE_HPUX) && !defined(SVR4)
83 fd = open("/dev/tty", O_RDWR, 0);
85 ioctl(fd, TIOCNOTTY, 0);
89 if ((fd = open(pty_name, O_RDWR, 0)) == -1)
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)
112 if (!pipe(channel)) {
113 if (-1==fcntl(channel[1], F_SETFD, FD_CLOEXEC)) {
121 if ((-1 != pid) && (-1 != channel[1])) {
123 int bytes = sizeof(int);
125 char *p = (char*)&child_errno;
127 /* Try to read child errno from channel. */
128 while ((bytes > 0) &&
129 (n = read(channel[0], p, bytes))) {
131 if (EINTR == errno) {
143 waitpid(pid, NULL, 0);
144 /* Our convention to tell Lisp that it was the exec that
145 * failed, not the fork. */
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. */
158 #if defined(LISP_FEATURE_HPUX) || defined(LISP_FEATURE_OPENBSD)
160 #elif defined(LISP_FEATURE_DARWIN)
161 setpgid(0, getpid());
162 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
165 setpgrp(0, getpid());
168 tcsetpgrp(0, getpgrp());
171 /* unblock signals */
173 sigprocmask(SIG_SETMASK, &sset, NULL);
175 /* If we are supposed to be part of some other pty, go for it. */
179 /* Set up stdin, stdout, and stderr */
187 /* Close all other fds. */
189 for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
190 if (fd != channel[1]) close(fd);
192 for (fd = getdtablesize()-1; fd >= 3; fd--)
193 if (fd != channel[1]) close(fd);
199 /* Exec the program. */
201 execvp(program, argv);
203 execv(program, argv);
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);
210 char *p = (char*)&our_errno;
211 while ((bytes > 0) &&
212 (n = write(channel[1], p, bytes))) {
214 if (EINTR == errno) {
228 #else /* !LISP_FEATURE_WIN32 */
230 # include <windows.h>
231 # include <process.h>
237 #define READ_HANDLE 0
238 #define WRITE_HANDLE 1
240 /* These functions do not attempt to deal with wchar_t variations. */
242 /* Get the value of _environ maintained by MSVCRT */
243 char **msvcrt_environ ( void ) {
247 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
250 const char *const *argv,
260 int stdout_backup, stdin_backup, stderr_backup, wait_mode;
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 ) );
269 /* If we are not using stdin/out/err
270 * then duplicate the new pipes to current stdin/out/err handles.
272 * Default std fds are used if in, out or err parameters
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;
280 if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
281 if ( _dup2 ( in, _fileno ( stdin ) ) != 0 ) goto error_exit_out;
283 if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
284 if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
287 /* Set the wait mode. */
289 wait_mode = P_NOWAIT;
294 /* Spawn process given on the command line*/
296 hProcess = (HANDLE) spawnvp ( wait_mode, program, (char* const* )argv );
298 hProcess = (HANDLE) spawnv ( wait_mode, program, (char* const* )argv );
300 /* Now that the process is launched, replace the original
301 * in/out/err handles and close the backups. */
303 if ( _dup2 ( stderr_backup, _fileno ( stderr ) ) != 0 ) goto error_exit;
305 if ( _dup2 ( stdin_backup, _fileno ( stdin ) ) != 0 ) goto error_exit;
307 if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
312 close ( stdout_backup );
313 close ( stdin_backup );
314 close ( stderr_backup );
321 #endif /* !LISP_FEATURE_WIN32 */