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 stdin
from its parent (myprogram
) and its parent's stdin
is the pipe that accepts data from cat file.tar.gz
. You would like to run a child that is forced to read data from the tty, not the inherited stdin
pipe.
You can open /dev/tty
to get a reference to the tty. It will fail if there is no tty. For example:
close(0);
open("/dev/tty", O_RDWR);
/* The file descriptor returned by open should be 0 */
However, I notice that you redirect the child command's stdout
to a pipe to your program. If the command you are exec
ing 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 connect stdin
to a tty.
By the way: why do you use bash -c <command>
instead of sh -c <command>
in your execlp
? This way you introduce an unnecessary dependency on bash
where all you really need is the standard POSIX bourne shell sh
. exec
ing through sh -c
is the standard way to launch a command through a shell. This is what's done, for example, by system()
.