[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

Redirecting STDERR and STDOUT in C  using library

Asked by dpkpl in Unix Systems Programming

Tags: attach, descriptors, dup2, stderr, stdout

Hi All,

I want to execute a binary and trap the stdout and stderr . I don't have the code for the binary. I have got the solution for this on the same list which is working fine. Many Thanks to Dave.
Here's the code :
=================================
Main Program:
/*
* Sample program to demonstrate super_popen
*
* by dave madden <dhm@webvision.com>
*/
#include <stdio.h>
#include <errno.h>
#include <sys/select.h>

int
super_popen( FILE **SI_SO_SE, const char *cmd )
{
    int           si[2] = { -1, -1 };
    int           so[2] = { -1, -1 };
    int           se[2] = { -1, -1 };
    pid_t      pid;
    int           e;
    int           fd;

    /*
     * Create 3 pipes to talk to subprocess on stdin/stdout/stderr
     */
    if (pipe( si ) != 0 || pipe( so ) != 0 || pipe( se ) != 0) goto SHUTDOWN;

    if ((pid = fork( )) == -1) goto SHUTDOWN;

    if (pid) {                         /* in parent */
         wait(0);
         close( si[0] );
         close( so[1] );
         close( se[1] );
         SI_SO_SE[0] = fdopen( si[1], "w" );
         SI_SO_SE[1] = fdopen( so[0], "r" );
         SI_SO_SE[2] = fdopen( se[0], "r" );
         
         return 0;                    /* OK! */
    }

    /*
     * This is the child process...we have to fiddle around with the
     * pipe descriptors and then exec the program.
     */

    /*
     * Make sure none of our stdin/stdout/stderr descriptors are
     * on FDs 0, 1, or 2 (if they are, it'll screw things up when
     * we do the dup2()'s)
     */
    if (si[0] < 2 || so[1] < 2 || se[1] < 2) {
         int      free_index = 0;
         int      free_fds[3];
         printf( "have to (Edited by Computer101) with FDs in child\n" );
         for (fd = 3; fd < FD_SETSIZE; fd++) {
              if (fd != si[0] && fd != si[1] && fd != se[1]) {
                   free_fds[free_index++] = fd;
                   if (free_index == 3) break;
              }
         }
         if (dup2( si[0], free_fds[0] ) == -1 ||
              dup2( so[1], free_fds[1] ) == -1 ||
              dup2( se[1], free_fds[2] ) == -1) exit( errno );
         si[0] = free_fds[0];
         so[1] = free_fds[1];
         se[1] = free_fds[2];
    }
         
    /* close all other descriptors...don't want child to have copies */
    for (fd = 0; fd < FD_SETSIZE; fd++)
         if (fd != si[0] && fd != so[1] && fd != se[1]) close( fd );
   
     dup2( si[0], 0 );               /* attach pipes to FD 0, 1, and 2 */
    dup2( so[1], 1 );
    dup2( se[1], 2 );

    close( si[0] );
    close( so[1] );
    close( se[1] );

/*     write( 1, "Hello\n", 6 );
    write( 2, "World\n", 6 );
    exit( 0 );*/

    exit( execl( "/usr/bin/sh", "sh", "-c", cmd, 0 ) ); <=========My binary with arguments works fine

 SHUTDOWN:     /* something went wrong...close the pipes & etc */
    e = errno;
    close( si[0] ); close( si[1] );
    close( so[0] ); close( so[1] );
    close( se[0] ); close( se[1] );
    errno = e;
    return -1;
}

int
main( int argc, char **argv ) <========================Changed to a function in Library and called from my
{                                                                                                     application
    FILE     *child_io[3];

    if (argc != 2) {
         printf( "usage: %s <name-of-program-to-run>\n", argv[0] );
         exit( 1 );
    }

    if (super_popen( child_io, argv[1] ) == 0) {
         char      buf[256];
         
         fprintf( child_io[0], "Message to child\n" );
         fflush( child_io[0] );

         while (fgets( buf, sizeof(buf), child_io[1] ) != 0) { <================= Cannot read This Goes on endlessly
              printf( "Child STDOUT: \"%.*s\"\n", strlen(buf) - 1, buf );                           when function is called from library
         }

         while (fgets( buf, sizeof(buf), child_io[2] ) != 0) { <================= Cannot read This Goes on endlessly
              printf( "Child STDERR: \"%.*s\"\n", strlen(buf) - 1, buf );                            when function is called from library
         }
    } else {
         printf("couldn't run %s: %d (%s)\n", argv[1], errno, strerror(errno));
    }

    exit( 0 );
}





=================================

But my problem is that I want to use all this code in a static library so I converted the main function to a function and made static library using codewarrior .Now when I call this function from my application it does everything finely except that it is not able to read from the File pointers being returned in the parent process in super_popen.
My application is MACOS X application.

(Please see my comments at relevant places in the code)

In the nutshell I cannot read from child_io[1] and child_io[2] when the function is called from the library.

Can somebody please point out what the problem is ?

Thanks in Advance
Deepak Kapila

=============================================
This question has been deleted with no points refunded.  01/25/2004 09:32AM PST

Computer101
E-E Admin
=============================================
 
Related Solutions
Keywords: Redirecting STDERR and STDOUT in C …
 
Loading Advertisement...
 
[+][-]08/08/03 12:04 AM, ID: 9106177Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/08/03 12:27 AM, ID: 9106251Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/08/03 01:10 AM, ID: 9106418Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/18/04 05:03 AM, ID: 10140086Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/25/04 09:33 AM, ID: 10196051Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Unix Systems Programming
Tags: attach, descriptors, dup2, stderr, stdout
Sign Up Now!
Solution Provided By: Computer101
Participating Experts: 3
Solution Grade: A
 
 
Loading Advertisement...
20091021-EE-VQP-81