61ad7617bd871f974c4a846654d21d79e85d19d4
[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
32
33 /* borrowed from detachtty's detachtty.c, in turn borrowed from APUE
34  * example code found at
35  * http://www.yendor.com/programming/unix/apue/pty/main.c
36
37 -brkint
38
39  */
40
41 int set_noecho(int fd)
42 {
43     struct termios  stermios;
44
45     if (tcgetattr(fd, &stermios) < 0) return 0;
46
47     stermios.c_lflag &= ~(  ECHO | /* ECHOE |  ECHOK | */  ECHONL);
48     stermios.c_oflag |= (ONLCR);
49     stermios.c_iflag &= ~(BRKINT);
50     stermios.c_iflag |= (ICANON|ICRNL);
51
52     stermios.c_cc[VERASE]=0177;
53     if (tcsetattr(fd, TCSANOW, &stermios) < 0) return 0;
54     return 1;
55 }
56
57 int spawn(char *program, char *argv[], char *envp[], char *pty_name,
58           int stdin, int stdout, int stderr)
59 {
60     int pid = fork();
61     int fd;
62     sigset_t sset;
63
64     if (pid != 0)
65         return pid;
66
67     /* Put us in our own process group. */
68 #if defined(hpux)
69     setsid();
70 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
71     setpgrp();
72 #else
73     setpgrp(0, getpid());
74 #endif
75
76     /* unblock signals */
77     sigemptyset(&sset);
78     sigprocmask(SIG_SETMASK, &sset, NULL);
79
80     /* If we are supposed to be part of some other pty, go for it. */
81     if (pty_name) {
82 #if !defined(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         fd = open(pty_name, O_RDWR, 0);
90         dup2(fd, 0);
91         set_noecho(0);
92         dup2(fd, 1);
93         dup2(fd, 2);
94         close(fd);
95     } else{
96     /* Set up stdin, stdout, and stderr */
97     if (stdin >= 0)
98         dup2(stdin, 0);
99     if (stdout >= 0)
100         dup2(stdout, 1);
101     if (stderr >= 0)
102         dup2(stderr, 2);
103     }
104     /* Close all other fds. */
105 #ifdef SVR4
106     for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
107         close(fd);
108 #else
109     for (fd = getdtablesize()-1; fd >= 3; fd--)
110         close(fd);
111 #endif
112
113     /* Exec the program. */
114     execve(program, argv, envp);
115
116     /* It didn't work, so try /bin/sh. */
117     argv[0] = program;
118     argv[-1] = "sh";
119     execve("/bin/sh", argv-1, envp);
120
121     /* The exec didn't work, flame out. */
122     exit(1);
123 }
124 #else  /* !LISP_FEATURE_WIN32 */
125
126 #  include <windows.h>
127 #  include <process.h>
128 #  include <stdio.h>
129 #  include <stdlib.h>
130 #  include <fcntl.h>
131 #  include <io.h>
132
133 #define   READ_HANDLE  0
134 #define   WRITE_HANDLE 1
135
136 /* These functions do not attempt to deal with wchar_t variations. */
137
138 /* Get the value of _environ maintained by MSVCRT */
139 char **msvcrt_environ ( void ) {
140     return ( _environ );
141 }
142
143 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
144 HANDLE spawn (
145     const char *program,
146     const char *const *argv,
147     int in,
148     int out,
149     int err,
150     int wait
151     )
152 {
153     int stdout_backup, stdin_backup, stderr_backup, wait_mode;
154     HANDLE hProcess;
155     HANDLE hReturn;
156
157     /* Duplicate and save the original stdin/out/err handles. */
158     stdout_backup = _dup (  _fileno ( stdout ) );
159     stdin_backup  = _dup (  _fileno ( stdin  ) );
160     stderr_backup = _dup (  _fileno ( stderr ) );
161
162     /* If we are not using stdin/out/err
163      * then duplicate the new pipes to current stdin/out/err handles.
164      *
165      * Default std fds are used if in, out or err parameters
166      * are -1. */
167
168     hReturn = (HANDLE)-1;
169     hProcess = (HANDLE)-1;
170     if ( ( out >= 0 ) && ( out != _fileno ( stdout ) ) ) {
171         if ( _dup2 ( out, _fileno ( stdout ) ) != 0 ) goto error_exit;
172     }
173     if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
174         if ( _dup2 ( in,  _fileno ( stdin )  ) != 0 ) goto error_exit_out;
175     }
176     if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
177         if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
178     }
179
180     /* Set the wait mode. */
181     if ( 0 == wait ) {
182         wait_mode = P_NOWAIT;
183     } else {
184         wait_mode = P_WAIT;
185     }
186
187     /* Spawn process given on the command line*/
188     hProcess = (HANDLE) spawnvp ( wait_mode, program, argv );
189
190     /* Now that the process is launched, replace the original
191      * in/out/err handles and close the backups. */
192
193     if ( _dup2 ( stderr_backup, _fileno ( stderr ) ) != 0 ) goto error_exit;
194  error_exit_in:
195     if ( _dup2 ( stdin_backup,  _fileno ( stdin )  ) != 0 ) goto error_exit;
196  error_exit_out:
197     if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
198
199     hReturn = hProcess;
200
201  error_exit:
202     close ( stdout_backup );
203     close ( stdin_backup  );
204     close ( stderr_backup );
205
206     return hReturn;
207
208 }
209
210
211 #endif /* !LISP_FEATURE_WIN32 */