Blob


1 #include <u.h>
2 #include <signal.h>
3 #include <libc.h>
4 #include "term.h"
6 int loginshell;
8 static void
9 sys(char *buf, int devnull)
10 {
11 char buf2[100];
12 char *f[20];
13 int nf, pid;
15 notedisable("sys: child");
16 strcpy(buf2, buf);
17 nf = tokenize(buf2, f, nelem(f));
18 f[nf] = nil;
19 switch(pid = fork()){
20 case 0:
21 close(1);
22 open("/dev/null", OREAD);
23 close(2);
24 open("/dev/null", OREAD);
25 execvp(f[0], f);
26 _exit(2);
27 default:
28 waitpid();
29 }
30 }
32 int
33 rcstart(int argc, char **argv, int *pfd, int *tfd)
34 {
35 int fd[2], i, pid;
36 char *cmd, *xargv[3];
37 char slave[256];
38 int sfd;
40 if(argc == 0){
41 argc = 2;
42 argv = xargv;
43 argv[0] = getenv("SHELL");
44 if(argv[0] == 0)
45 argv[0] = "rc";
46 argv[1] = "-i";
47 argv[2] = 0;
48 }
49 cmd = argv[0];
50 if(loginshell){
51 argv[0] = malloc(strlen(cmd)+2);
52 strcpy(argv[0]+1, cmd);
53 argv[0][0] = '-';
54 }
56 /*
57 * fd0 is slave (tty), fd1 is master (pty)
58 */
59 fd[0] = fd[1] = -1;
60 if(getpts(fd, slave) < 0){
61 exit(3);
62 sysfatal("getpts: %r\n");
63 }
64 /*
65 * notedisable("sys: window size change");
66 *
67 * Can't disable because will be inherited by other programs
68 * like if you run an xterm from the prompt, and then xterm's
69 * resizes won't get handled right. Sigh.
70 *
71 * Can't not disable because when we stty below we'll get a
72 * signal, which will drop us into the thread library note handler,
73 * which will get all confused because we just forked and thus
74 * have an unknown pid.
75 *
76 * So disable it internally. ARGH!
77 */
78 notifyoff("sys: window size change");
80 pid = fork();
81 switch(pid){
82 case 0:
83 putenv("TERM", "9term");
84 sfd = childpty(fd, slave);
85 dup(sfd, 0);
86 dup(sfd, 1);
87 dup(sfd, 2);
88 sys("stty tabs -onlcr icanon echo erase '^h' intr '^?'", 0);
89 sys("stty onocr", 1); /* not available on mac */
90 if(noecho)
91 sys("stty -echo", 0);
92 for(i=3; i<100; i++)
93 close(i);
94 signal(SIGINT, SIG_DFL);
95 signal(SIGHUP, SIG_DFL);
96 signal(SIGTERM, SIG_DFL);
97 execvp(cmd, argv);
98 fprint(2, "exec %s failed: %r\n", argv[0]);
99 _exit(2);
100 break;
101 case -1:
102 sysfatal("proc failed: %r");
103 break;
105 *pfd = fd[1];
106 close(fd[0]);
107 if(tfd){
108 if((*tfd = open(slave, OREAD)) < 0)
109 sysfatal("parent open %s: %r", slave);
111 return pid;