X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=vsyssh%2Fvsyssh.c;h=fd5d62a73404f248d5ce330123cf1d9c49f8459a;hb=0a4a3a68d48188e50ae321557f4527a8dffaf73a;hp=fd82c6e9ecba9a93f2e71262e4f0af7911d231c5;hpb=97bb62063d093da99f914f8c07cd512e8862a282;p=vsys.git diff --git a/vsyssh/vsyssh.c b/vsyssh/vsyssh.c index fd82c6e..fd5d62a 100644 --- a/vsyssh/vsyssh.c +++ b/vsyssh/vsyssh.c @@ -1,4 +1,4 @@ -/* gcc -Wall -O2 -g chpid.c -o chpid */ +/* gcc -Wall -O2 -g vsyssh.c -o vsyssh */ #define _XOPEN_SOURCE #define _XOPEN_SOURCE_EXTENDED #define _SVID_SOURCE @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -16,78 +17,107 @@ #include #include #include +#include #include #include #include #include #include -int main(int argc, char **argv, char **envp) -{ - if (argc<2) { - printf("Usage: vsyssh [cmd]\n"); - exit(1); - } - else { - int vfd0,vfd1; - char *inf,*outf; - struct timeval tv; +void pipe_handler (int sig) { + printf("SIGPIPE"); +} + +#define FILELEN 256 + +int main(int argc, char **argv, char **envp) { - inf=(char *)malloc(strlen(argv[1])+3); - outf=(char *)malloc(strlen(argv[1])+4); - strcpy(inf,argv[1]); - strcpy(outf,argv[1]); - strcat(inf,".in"); - strcat(outf,".out"); - tv.tv_sec = 100; - tv.tv_usec = 0; + if ( (argc<2) || (strcmp(argv[1],"--help")==0) ) { + printf("Usage: vsyssh [cmd]\n"); + exit(1); + } - vfd1 = open(inf,O_WRONLY|O_NONBLOCK); - vfd0 = open(outf,O_RDONLY|O_NONBLOCK); + { + uid_t uid=getuid(); + if (uid!=0) { + printf ("%s requires root privileges, please run under sudo\n",argv[0]); + exit(1); + } + } - if (vfd0==-1 || vfd1 == -1) { - printf("Error opening vsys entry %s\n", argv[1]); - exit(1); - } + { + int vfd0,vfd1; + char inf[FILELEN], outf[FILELEN]; + int fatal=0; - if (argc<3) { - fd_set set; - FD_ZERO(&set); - FD_SET(0, &set); - FD_SET(vfd0, &set); + signal(SIGPIPE,pipe_handler); + + sprintf(inf,"/vsys/%s.in",argv[1]); + sprintf(outf,"/vsys/%s.out",argv[1]); + vfd0 = open(outf,O_RDONLY|O_NONBLOCK); + if (vfd0<0) { + printf("Error opening vsys channel %s (%m)\n",outf); + fatal=1; + } + vfd1 = open(inf,O_WRONLY); + if (vfd1<0) { + printf("Error opening vsys channel %s (%m)\n",inf); + fatal=1; + } - while (1) { - int ret; - printf("vsys>");fflush(stdout); - FD_SET(0, &set); - FD_SET(vfd0, &set); - ret = select(vfd0+1, &set, NULL, NULL, &tv); - if (FD_ISSET(0,&set)) { - char lineread[2048]; - int ret; - ret=read(0,lineread,2048); - write(vfd1,lineread,ret); - FD_CLR(0,&set); - } if (FD_ISSET(vfd0,&set)) { - char lineread[2048]; - int ret; - ret=read(vfd0,lineread,2048); - write(1,lineread,ret); - FD_CLR(vfd0,&set); - } - } + if (fcntl(vfd0, F_SETFL, O_RDONLY) == -1) { + printf("Error making pipe blocking: %m\n"); + fatal=1; + } + + if (fatal) { exit(1);} + + if (argc<3) { + /* interactive mode */ + fd_set set; + char do_input = 1, do_output = 1; + + while (1) { + int ret; + printf("vsys>");fflush(stdout); + FD_ZERO(&set); + if (do_input) + FD_SET(0, &set); + if (do_output) + FD_SET(vfd0, &set); + ret = select(vfd0+1, &set, NULL, NULL, NULL); + if (FD_ISSET(0,&set)) { + char lineread[2048]; + int ret; + ret=read(0,lineread,2048); + /*printf ("read=%d\n",ret);*/ + if (ret == 0) + do_input = 0; + lineread[ret]='\0'; + printf ("writing %s\n",lineread); + write(vfd1,lineread,ret); + } + if (FD_ISSET(vfd0,&set)) { + char lineread[2048]; + int ret; + ret = read(vfd0,lineread,2048); + if (ret == 0) + break; + write(1,lineread,ret); + } + } - } - else { - close(0); - close(1); + } else { + /* line mode */ + close(0); + close(1); - dup2(vfd0,0); - dup2(vfd1,1); - execve(argv[3],argv+3,envp); - } - } + dup2(vfd0,0); + dup2(vfd1,1); + execve(argv[2],argv+2,envp); + } + } - return; + return -1; }