You are not logged in.
Pages: 1
My goal is to write a program which redirects the io streams of a child program to a variety of files. I have gotten it to work, mostly.
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
int shell_pid;
int redirect_io()
{
// io stream file names
char *out = "acs_stdout";
char *in = "acs_stdin";
// actual redirection
freopen(out, "w", stdout);
freopen(out, "w", stderr);
freopen(in, "r", stdin);
return 0;
}
int fork_shell()
{
// fork another thread to run shell
shell_pid = fork();
if (shell_pid == 0)
{
char *cmd[] = {"/bin/bash", NULL};
execv(cmd[0], cmd);
_exit(0);
}
return 0;
}
int shell_write(char *s)
{
// write command to stdin file
fprintf(stdin, "%s\n", s);
return 0;
}
int main()
{
redirect_io();
shell_write("ls"); // i have tried this command after fork_shell(). there is no difference
fork_shell();
waitpid(shell_pid, 0, 0); // wait until the shell is done
return 0;
}
My problem is that after that first command (ls) is run, bash just quits. It completes the ls command and the correct output is in acs_stdout (my stdout file). Do any of you have any ideas on what might be wrong?
Last edited by Lexion (2010-07-28 16:22:29)
urxvtc / wmii / zsh / configs / onebluecat.net
Arch will not hold your hand
Offline
[...] int redirect_io() { [...] freopen(in, "r", stdin); return 0; } [...] int shell_write(char *s) { // write command to stdin file fprintf(stdin, "%s\n", s); return 0; } [...]
I don't think its related, but you are writting to a file opened for reading. I believe bash is exiting because after reading the ls command there's nothing left in the file and it gets an EOF.
You should be using pipes here:
int fork_shell()
{
/* call pipe(), it will give you two file descriptors,
* one for each end of the pipe.
*/
// fork another thread to run shell
shell_pid = fork();
if (shell_pid == 0)
{
/* close the write end */
/* reassing stdin to the read end, using dup() i think */
/* reopen stdout to a file (or the write end of another pipe
* if you want the output to go back to the parent process)
*/
char *cmd[] = {"/bin/bash", NULL};
execv(cmd[0], cmd);
_exit(0);
}
/* you may reassing stdout to the write end of the pipe or use it
* explicitly when sending commands to bash
*/
return 0;
}
Last edited by diegonc (2010-07-28 14:24:02)
Offline
Thanks for the quick reply. Bash no longer quits now, but when I run the program, it does this:
$ program
ls
And doesn't quit until I press Ctl-C. It appears to start bash, and then print "ls" to stdout. Here is the code that I changed:
int shell_pipe[2];
#define SHELL_PIPE_READ shell_pipe[0]
#define SHELL_PIPE_WRITE shell_pipe[1]
int fork_shell()
{
pipe(shell_pipe);
shell_pid = fork();
if (shell_pid == 0)
{
close(SHELL_WRITE);
dup2(SHELL_READ, STDIN_FILENO);
char *cmd[] = {"/bin/bash", NULL};
execv(cmd[0], cmd);
_exit(0);
}
return 0;
}
urxvtc / wmii / zsh / configs / onebluecat.net
Arch will not hold your hand
Offline
Looks like shell_write was not updated
Offline
Thanks alot. It works. yay!
urxvtc / wmii / zsh / configs / onebluecat.net
Arch will not hold your hand
Offline
Pages: 1