889e41a11882c810997951443db250b9d456fcd4
[vsys.git] / vsyssh / vsyssh.c
1 /* gcc -Wall -O2 -g chpid.c -o chpid */
2 #define _XOPEN_SOURCE
3 #define _XOPEN_SOURCE_EXTENDED
4 #define _SVID_SOURCE
5 #define _GNU_SOURCE
6 #include <stdio.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/syscall.h>
13 #include <sys/wait.h>
14 #include <sys/time.h>
15 #include <sys/select.h>
16 #include <sys/resource.h>
17 #include <sys/mount.h>
18 #include <sys/vfs.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <sched.h>
22 #include <stdarg.h>
23 #include <dirent.h>
24
25 int main(int argc, char **argv, char **envp)
26 {
27         if (argc<2) {
28                 printf("Usage: vsyssh <vsys entry> [cmd]\n");
29                 exit(1);
30         }
31         else {
32                 int vfd0,vfd1;
33                 char *inf,*outf;
34                 struct timeval tv;
35
36                 inf=(char *)malloc(strlen(argv[1])+3);
37                 outf=(char *)malloc(strlen(argv[1])+4);
38                 strcpy(inf,argv[1]);
39                 strcpy(outf,argv[1]);
40                 strcat(inf,".in");
41                 strcat(outf,".out");
42
43                 vfd1 = open(inf,O_WRONLY|O_NONBLOCK);
44                 vfd0 = open(outf,O_RDONLY|O_NONBLOCK);
45
46                 if (vfd0==-1 || vfd1 == -1) {
47                         printf("Error opening vsys entry %s\n", argv[1]);
48                         exit(1);
49                 }
50
51                 if (argc<3) {
52                         fd_set set;
53                         FD_ZERO(&set);
54                         FD_SET(0, &set);
55                         FD_SET(vfd0, &set);
56
57                         while (1) {
58                                 int ret;
59                                 printf("vsys>");fflush(stdout);
60                                 FD_SET(0, &set);
61                                 FD_SET(vfd0, &set);
62                                 ret = select(vfd0+1, &set, NULL, NULL, NULL);
63                                 if (FD_ISSET(0,&set)) {
64                                         char lineread[2048];
65                                         int ret;
66                                         ret=read(0,lineread,2048);
67                                         write(vfd1,lineread,ret);
68                                         FD_CLR(0,&set);
69                                 } if (FD_ISSET(vfd0,&set)) {
70                                         char lineread[2048];
71                                         int ret;
72                                         ret=read(vfd0,lineread,2048);
73                                         write(1,lineread,ret);
74                                         FD_CLR(vfd0,&set);
75                                 }
76                         }
77
78                 }
79                 else {
80                         close(0);
81                         close(1);
82
83                         dup2(vfd0,0);
84                         dup2(vfd1,1);
85                         execve(argv[3],argv+3,envp);
86                 }
87        }
88
89        return;
90
91 }