[gccsdk] [Bug 236] GCC 4.1.2 Rel 2 Dev 2012-05-01: fork/execvp/dup2 redirection problem

bugzilla-daemon at riscos.info bugzilla-daemon at riscos.info
Sun May 6 09:53:43 PDT 2012


http://www.riscos.info/bugzilla3/show_bug.cgi?id=236

--- Comment #1 from Duncan Moore <duncan_moore at ntlworld.com> 2012-05-06 09:53:42 PDT ---
GCCSDK GCC 4.1.2 Release 2 Development 2012-05-05
SharedUnixLibrary 1.12
VRPC RISC OS 4.39

The 2012-05-05 test version has resolved the above issue.

However, with this code:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>    // open
#include <unistd.h>   // close
#include <sys/wait.h> // wait WEXITSTATUS

//---------------------------------------------------------------------------
static int redirect(char* const cmd[],int file_in,int file_out,int file_err) {
  pid_t childpid; // Child's pid.
  int retval;     // Child process return code.
  int status;     // Parent process return code.

  childpid=fork(); // Create new process.
  if (childpid==0) { // Child process.
    fprintf(stdout,"child output 1\n");
    fprintf(stderr,"child error  1\n");
    if (file_in >=0) dup2(file_in, STDIN_FILENO);  // Redirect standard input
    if (file_out>=0) dup2(file_out,STDOUT_FILENO); // Redirect standard output
    if (file_err>=0) dup2(file_err,STDERR_FILENO); // Redirect standard error
    fprintf(stdout,"child output 2\n");
    fprintf(stderr,"child error  2\n");
    retval=execvp(cmd[0],cmd); // execvp() does not return on success.
    return retval;
  }
  else if (childpid>0) { // Parent process.
    wait(&status); // Wait for child to exit, and store its status.
    fprintf(stdout,"parent output\n");
    fprintf(stderr,"parent error\n");
    return WEXITSTATUS(status);
  } else { // fork() returns -1 on failure.
    fprintf(stderr,"fork() error\n");
    exit(1);
  }
}
//---------------------------------------------------------------------------
int main(void) {
  int file_in =open("file_in",O_RDONLY);
  int file_out=open("file_out",O_WRONLY|O_CREAT);
  int file_err=open("file_err",O_WRONLY|O_CREAT);

  printf("start\n");

  char* cmd[]={"gcc","-dumpmachine",0};
  if (redirect(cmd,file_in,file_out,file_err)!=0) printf("system error\n");

  close(file_in);
  close(file_out);
  close(file_err);

  printf("end\n");

  return 0;
}


what I think I should get is:

*test
start
child output 1
child error  1
parent output
parent error
end
*Type file_out
child output 2
arm-unknown-riscos
*Type file_err
child error  2
*

but what I actually get is:

*test
start
child output 1
child error  1
end
*Type file_out
child output 2
arm-unknown-riscos
parent output
parent error

*Type file_err
child error  2
*

i.e. the parent standard output and error output is being redirected to
file_out, when it should be going to the terminal. The two affected lines have
bytes 13 10 for the line terminator, rather than just 10.
If the execvp() line is replaced by exit(1), then the parent standard output
and error output go to the terminal as expected.

-- 
Configure bugmail: http://www.riscos.info/bugzilla3/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching all bug changes.




More information about the gcc mailing list