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