1.0.12.37: RUN-PROGRAM now uses execvp(3) to search for executables
[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 extern char **environ;
58 int spawn(char *program, char *argv[], int stdin, int stdout, int stderr,
59           int search, char *envp[], char *pty_name, int wait)
60 {
61     int pid = fork();
62     int fd;
63     sigset_t sset;
64
65     if (pid != 0)
66         return pid;
67
68     /* Put us in our own process group. */
69 #if defined(hpux)
70     setsid();
71 #elif defined(LISP_FEATURE_DARWIN)
72     setpgid(0, getpid());
73 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
74     setpgrp();
75 #else
76     setpgrp(0, getpid());
77 #endif
78
79     /* unblock signals */
80     sigemptyset(&sset);
81     sigprocmask(SIG_SETMASK, &sset, NULL);
82
83     /* If we are supposed to be part of some other pty, go for it. */
84     if (pty_name) {
85 #if !defined(hpux) && !defined(SVR4)
86         fd = open("/dev/tty", O_RDWR, 0);
87         if (fd >= 0) {
88             ioctl(fd, TIOCNOTTY, 0);
89             close(fd);
90         }
91 #endif
92         fd = open(pty_name, O_RDWR, 0);
93         dup2(fd, 0);
94         set_noecho(0);
95         dup2(fd, 1);
96         dup2(fd, 2);
97         close(fd);
98     } else{
99     /* Set up stdin, stdout, and stderr */
100     if (stdin >= 0)
101         dup2(stdin, 0);
102     if (stdout >= 0)
103         dup2(stdout, 1);
104     if (stderr >= 0)
105         dup2(stderr, 2);
106     }
107     /* Close all other fds. */
108 #ifdef SVR4
109     for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
110         close(fd);
111 #else
112     for (fd = getdtablesize()-1; fd >= 3; fd--)
113         close(fd);
114 #endif
115
116     environ = envp;
117     /* Exec the program. */
118     if (search)
119       execvp(program, argv);
120     else
121       execv(program, argv);
122
123     exit (1);
124 }
125 #else  /* !LISP_FEATURE_WIN32 */
126
127 #  include <windows.h>
128 #  include <process.h>
129 #  include <stdio.h>
130 #  include <stdlib.h>
131 #  include <fcntl.h>
132 #  include <io.h>
133
134 #define   READ_HANDLE  0
135 #define   WRITE_HANDLE 1
136
137 /* These functions do not attempt to deal with wchar_t variations. */
138
139 /* Get the value of _environ maintained by MSVCRT */
140 char **msvcrt_environ ( void ) {
141     return ( _environ );
142 }
143
144 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
145 HANDLE spawn (
146     const char *program,
147     const char *const *argv,
148     int in,
149     int out,
150     int err,
151     int search,
152     char *envp,
153     char *ptyname,
154     int wait
155     )
156 {
157     int stdout_backup, stdin_backup, stderr_backup, wait_mode;
158     HANDLE hProcess;
159     HANDLE hReturn;
160
161     /* Duplicate and save the original stdin/out/err handles. */
162     stdout_backup = _dup (  _fileno ( stdout ) );
163     stdin_backup  = _dup (  _fileno ( stdin  ) );
164     stderr_backup = _dup (  _fileno ( stderr ) );
165
166     /* If we are not using stdin/out/err
167      * then duplicate the new pipes to current stdin/out/err handles.
168      *
169      * Default std fds are used if in, out or err parameters
170      * are -1. */
171
172     hReturn = (HANDLE)-1;
173     hProcess = (HANDLE)-1;
174     if ( ( out >= 0 ) && ( out != _fileno ( stdout ) ) ) {
175         if ( _dup2 ( out, _fileno ( stdout ) ) != 0 ) goto error_exit;
176     }
177     if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
178         if ( _dup2 ( in,  _fileno ( stdin )  ) != 0 ) goto error_exit_out;
179     }
180     if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
181         if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
182     }
183
184     /* Set the wait mode. */
185     if ( 0 == wait ) {
186         wait_mode = P_NOWAIT;
187     } else {
188         wait_mode = P_WAIT;
189     }
190
191     /* Spawn process given on the command line*/
192     if (search)
193       hProcess = (HANDLE) spawnvp ( wait_mode, program, argv );
194     else
195       hProcess = (HANDLE) spawnv ( wait_mode, program, argv );
196
197     /* Now that the process is launched, replace the original
198      * in/out/err handles and close the backups. */
199
200     if ( _dup2 ( stderr_backup, _fileno ( stderr ) ) != 0 ) goto error_exit;
201  error_exit_in:
202     if ( _dup2 ( stdin_backup,  _fileno ( stdin )  ) != 0 ) goto error_exit;
203  error_exit_out:
204     if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
205
206     hReturn = hProcess;
207
208  error_exit:
209     close ( stdout_backup );
210     close ( stdin_backup  );
211     close ( stderr_backup );
212
213     return hReturn;
214
215 }
216
217
218 #endif /* !LISP_FEATURE_WIN32 */