Blob


1 #include <u.h>
2 #include <signal.h>
3 #include <libc.h>
4 #include "term.h"
6 static void
7 sys(char *buf, int devnull)
8 {
9 char buf2[100];
10 char *f[20];
11 int nf, pid;
13 notedisable("sys: child");
14 strcpy(buf2, buf);
15 nf = tokenize(buf2, f, nelem(f));
16 f[nf] = nil;
17 switch(pid = fork()){
18 case 0:
19 close(1);
20 open("/dev/null", OREAD);
21 close(2);
22 open("/dev/null", OREAD);
23 execvp(f[0], f);
24 _exit(2);
25 default:
26 waitpid();
27 }
28 }
30 int
31 rcstart(int argc, char **argv, int *pfd, int *tfd)
32 {
33 int fd[2], i, pid;
34 char *xargv[3];
35 char slave[256];
36 int sfd;
38 if(argc == 0){
39 argc = 2;
40 argv = xargv;
41 argv[0] = getenv("SHELL");
42 if(argv[0] == 0)
43 argv[0] = "rc";
44 argv[1] = "-i";
45 argv[2] = 0;
46 }
47 /*
48 * fd0 is slave (tty), fd1 is master (pty)
49 */
50 fd[0] = fd[1] = -1;
51 if(getpts(fd, slave) < 0){
52 exit(3);
53 sysfatal("getpts: %r\n");
54 }
55 notedisable("sys: window size change");
56 pid = fork();
57 switch(pid){
58 case 0:
59 putenv("TERM", "9term");
60 sfd = childpty(fd, slave);
61 dup(sfd, 0);
62 dup(sfd, 1);
63 dup(sfd, 2);
64 sys("stty tabs -onlcr icanon echo erase '^h' intr '^?'", 0);
65 sys("stty onocr", 1); /* not available on mac */
66 if(noecho)
67 sys("stty -echo", 0);
68 for(i=3; i<100; i++)
69 close(i);
70 signal(SIGINT, SIG_DFL);
71 signal(SIGHUP, SIG_DFL);
72 signal(SIGTERM, SIG_DFL);
73 execvp(argv[0], argv);
74 fprint(2, "exec %s failed: %r\n", argv[0]);
75 _exit(2);
76 break;
77 case -1:
78 sysfatal("proc failed: %r");
79 break;
80 }
81 *pfd = fd[1];
82 close(fd[0]);
83 if(tfd){
84 if((*tfd = open(slave, OREAD)) < 0)
85 sysfatal("parent open %s: %r", slave);
86 }
87 return pid;
88 }