0.8.6.28:
[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 <stdlib.h>
17 #include <sys/file.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <sys/ioctl.h>
22 #if defined(SVR4) || defined(__linux__)
23 #include <unistd.h>
24 #endif
25
26 #include <sys/ioctl.h>
27 #include <termios.h>
28
29
30 /* borrowed from detachtty's detachtty.c, in turn borrowed from APUE
31  * example code found at
32  * http://www.yendor.com/programming/unix/apue/pty/main.c
33
34 -brkint 
35
36  */
37
38 int set_noecho(int fd) 
39 {
40     struct termios  stermios;
41     
42     if (tcgetattr(fd, &stermios) < 0) return 0;
43     
44     stermios.c_lflag &= ~(  ECHO | /* ECHOE |  ECHOK | */  ECHONL);
45     stermios.c_oflag |= (ONLCR); 
46     stermios.c_iflag &= ~(BRKINT);
47     stermios.c_iflag |= (ICANON|ICRNL); 
48
49     stermios.c_cc[VERASE]=0177;
50     if (tcsetattr(fd, TCSANOW, &stermios) < 0) return 0;
51     return 1;
52 }
53
54 int spawn(char *program, char *argv[], char *envp[], char *pty_name,
55           int stdin, int stdout, int stderr)
56 {
57     int pid = fork();
58     int fd;
59
60     if (pid != 0)
61         return pid;
62
63     /* Put us in our own process group. */
64 #if defined(hpux)
65     setsid();
66 #elif defined(SVR4) || defined(__linux__)
67     setpgrp();
68 #else
69     setpgrp(0, getpid());
70 #endif
71
72     /* If we are supposed to be part of some other pty, go for it. */
73     if (pty_name) {
74 #if !defined(hpux) && !defined(SVR4)
75         fd = open("/dev/tty", O_RDWR, 0);
76         if (fd >= 0) {
77             ioctl(fd, TIOCNOTTY, 0);
78             close(fd);
79         }
80 #endif
81         fd = open(pty_name, O_RDWR, 0);
82         dup2(fd, 0);
83         set_noecho(0);
84         dup2(fd, 1);
85         dup2(fd, 2);
86         close(fd);
87     } else{
88     /* Set up stdin, stdout, and stderr */
89     if (stdin >= 0)
90         dup2(stdin, 0);
91     if (stdout >= 0)
92         dup2(stdout, 1);
93     if (stderr >= 0)
94         dup2(stderr, 2);
95     }
96     /* Close all other fds. */
97 #ifdef SVR4
98     for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
99         close(fd);
100 #else
101     for (fd = getdtablesize()-1; fd >= 3; fd--)
102         close(fd);
103 #endif
104
105     /* Exec the program. */
106     execve(program, argv, envp);
107
108     /* It didn't work, so try /bin/sh. */
109     argv[0] = program;
110     argv[-1] = "sh";
111     execve("/bin/sh", argv-1, envp);
112
113     /* The exec didn't work, flame out. */
114     exit(1);
115 }