I’m using some pipe and fork to execute a command like
cat file.tar.gz | myprogram -c “tar -zvt” -i “remoteMachineIp”
But if i do
myprogram -c “bash” -i “remoteMachineIp”
The bash is ok, but no tty.
Without the tty if i call the vim command it opens the vi messed.
How can i call the bash on execlp with the tty.
Code Example
if(cm->pid2==0){ //pipe and fork to send data to my execlp
close(cm->fout[0]);
dup2(cm->fout[1], 1);
execlp("bash", "bash", "-c", command.c_str(), NULL); // command is mycommand so is bash -c "bash"
}
else{
close(cm->fout[1]);
while((size=read(cm->fout[0], buffer, 2000)) > 0){
cm->message->send(buffer, size); //send the data to my program
}
//Close it and send the return code
The problem is the execlp returns the bash to me, but no tty
If i call ssh user@machine “bash” the bash comes with problem, but jut ssh user@machine its ok
How can i call the bash with the tty?
Thanks
I guess that your concern is that the command you launch inherits
stdinfrom its parent (myprogram) and its parent’sstdinis the pipe that accepts data fromcat file.tar.gz. You would like to run a child that is forced to read data from the tty, not the inheritedstdinpipe.You can open
/dev/ttyto get a reference to the tty. It will fail if there is no tty. For example:However, I notice that you redirect the child command’s
stdoutto a pipe to your program. If the command you areexecing is interactive, this will not work. An interactive command needs to have both its input and its output connected to a tty. Launching an interactive command with its output connected to a pipe probably does not make any sense, regardless of whether you connectstdinto a tty.By the way: why do you use
bash -c <command>instead ofsh -c <command>in yourexeclp? This way you introduce an unnecessary dependency onbashwhere all you really need is the standard POSIX bourne shellsh.execing throughsh -cis the standard way to launch a command through a shell. This is what’s done, for example, bysystem().