Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting exec output to a buffer or file

Tags:

c

fork

exec

I'm writing a C program where I fork(), exec(), and wait(). I'd like to take the output of the program I exec'ed to write it to file or buffer.

For example, if I exec ls I want to write file1 file2 etc to buffer/file. I don't think there is a way to read stdout, so does that mean I have to use a pipe? Is there a general procedure here that I haven't been able to find?

like image 444
devin Avatar asked Apr 09 '10 04:04

devin


People also ask

How do I redirect an output to a file?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

How do I redirect the output of an already running process?

The way we can redirect the output is by closing the current file descriptor and then reopening it, pointing to the new output. We'll do this using the open and dup2 functions. There are two default outputs in Unix systems, stdout and stderr. stdout is associated with file descriptor 1 and stderr to 2.

Can we redirect the output of a command to a file and display at the same time?

Redirecting output to Multiple files and screen: If you want to redirect the screen output to multiple files, the only thing you have to do is add the file names at the end of the tee command.


2 Answers

For sending the output to another file (I'm leaving out error checking to focus on the important details):

if (fork() == 0) {     // child     int fd = open(file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);      dup2(fd, 1);   // make stdout go to file     dup2(fd, 2);   // make stderr go to file - you may choose to not do this                    // or perhaps send stderr to another file      close(fd);     // fd no longer needed - the dup'ed handles are sufficient      exec(...); } 

For sending the output to a pipe so you can then read the output into a buffer:

int pipefd[2]; pipe(pipefd);  if (fork() == 0) {     close(pipefd[0]);    // close reading end in the child      dup2(pipefd[1], 1);  // send stdout to the pipe     dup2(pipefd[1], 2);  // send stderr to the pipe      close(pipefd[1]);    // this descriptor is no longer needed      exec(...); } else {     // parent      char buffer[1024];      close(pipefd[1]);  // close the write end of the pipe in the parent      while (read(pipefd[0], buffer, sizeof(buffer)) != 0)     {     } } 
like image 109
R Samuel Klatchko Avatar answered Sep 21 '22 20:09

R Samuel Klatchko


You need to decide exactly what you want to do - and preferably explain it a bit more clearly.

Option 1: File

If you know which file you want the output of the executed command to go to, then:

  1. Ensure that the parent and child agree on the name (parent decides name before forking).
  2. Parent forks - you have two processes.
  3. Child reorganizes things so that file descriptor 1 (standard output) goes to the file.
  4. Usually, you can leave standard error alone; you might redirect standard input from /dev/null.
  5. Child then execs relevant command; said command runs and any standard output goes to the file (this is the basic shell I/O redirection).
  6. Executed process then terminates.
  7. Meanwhile, the parent process can adopt one of two main strategies:
    • Open the file for reading, and keep reading until it reaches an EOF. It then needs to double check whether the child died (so there won't be any more data to read), or hang around waiting for more input from the child.
    • Wait for the child to die and then open the file for reading.
    • The advantage of the first is that the parent can do some of its work while the child is also running; the advantage of the second is that you don't have to diddle with the I/O system (repeatedly reading past EOF).

Option 2: Pipe

If you want the parent to read the output from the child, arrange for the child to pipe its output back to the parent.

  1. Use popen() to do this the easy way. It will run the process and send the output to your parent process. Note that the parent must be active while the child is generating the output since pipes have a small buffer size (often 4-5 KB) and if the child generates more data than that while the parent is not reading, the child will block until the parent reads. If the parent is waiting for the child to die, you have a deadlock.
  2. Use pipe() etc to do this the hard way. Parent calls pipe(), then forks. The child sorts out the plumbing so that the write end of the pipe is its standard output, and ensures that all other file descriptors relating to the pipe are closed. This might well use the dup2() system call. It then executes the required process, which sends its standard output down the pipe.
  3. Meanwhile, the parent also closes the unwanted ends of the pipe, and then starts reading. When it gets EOF on the pipe, it knows the child has finished and closed the pipe; it can close its end of the pipe too.
like image 25
Jonathan Leffler Avatar answered Sep 21 '22 20:09

Jonathan Leffler