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