Public feed
#889849
#include <signal.h>
#include "exec.h"
#include "config.h"
namespace myexec {
int execbase::setprog(const char *newprog){
if(prog==NULL){prog=new char[MAXLINE];}
strcpy(prog,newprog);
return strlen(prog);
}
int execbase::exec(){
int stdinfd[2],stdoutfd[2];
const char *shell_args[4] = { "/bin/bash", "-c", NULL, NULL };
if (pipe(stdinfd)==-1){return -1;}
if (pipe(stdoutfd)==-1){return -2;}
shell_args[2]=prog;
this->pid=fork();
if(this->pid==-1){
close(stdinfd[0]);close(stdinfd[1]);
close(stdoutfd[0]);close(stdoutfd[1]);
return -3;
}
if(pid!=0){
//printf("i'm a father:O\n");
this->infd=stdinfd[1];
close(stdinfd[0]);
this->outfd=stdoutfd[0];
close(stdoutfd[1]);
printf("starting handle\n");
return this->handle();
}
//printf("i'm the child\n");
setsid();
setuid(getuid());
setgid(getgid());
signal(SIGINT,SIG_IGN);
signal(SIGQUIT,SIG_DFL);
putenv("TERM=tty");
dup2(stdinfd[0],STDIN_FILENO);
dup2(stdoutfd[1],STDOUT_FILENO);
dup2(stdoutfd[1],STDERR_FILENO);
for(int n=3;n<256;n++)close(n);
execvp(shell_args[0],(char**)shell_args);
fprintf(stderr,"error exec'ing\n");
_exit(-1);
}
int execbase::handle(){
printf("old handle started\n");
char *buffer=new char[MAXLINE];
int ret;
while((ret=read(this->outfd,buffer,511))){
printf("got a chunk");
write(STDOUT_FILENO,buffer,ret);
}
return 0;
}
}