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