[gccsdk] [Bug 236] New: 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
Thu May 3 06:42:25 PDT 2012


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

           Summary: GCC 4.1.2 Rel 2 Dev 2012-05-01: fork/execvp/dup2
                    redirection problem
           Product: GCC/GCCSDK
           Version: other
          Platform: Other
        OS/Version: RISC OS
            Status: NEW
          Severity: normal
          Priority: P1
         Component: C compiler
        AssignedTo: John.Tytgat at aaug.net
        ReportedBy: duncan_moore at ntlworld.com
   Estimated Hours: 0.0


GCCSDK GCC 4.1.2 Release 2 Development 2012-05-01
SharedUnixLibrary 1.12
VRPC RISC OS 4.39

#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.
    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
    retval=execvp(cmd[0],cmd); // execvp() does not return on success, only on
failure.
    return retval;
  }
  else if (childpid>0) { // Parent process.
    wait(&status); // Wait for child to exit, and store its status.
    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("%i %i %i\n",file_in,file_out,file_err);

  printf("1\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("2\n");

  return 0;
}

This is what I think we should get:

*test
-1 3 4
1
2
*Type file_err
*Type file_out
arm-unknown-riscos
*

What I get is, file_err and file_out are correct, but

*test
-1 3 4
1

Error 0xe59ff400:
  pc: 0001fffc

Fatal signal received: EMT trap

Stack backtrace:

Running thread 0x2ee3c
  (  807f7c) pc:    22050 lr:    225d0 sp:   807f80  __write_backtrace()
  (  807fe4) pc:    22174 lr:    22c74 sp:   807fe8  __unixlib_raise_signal()
  (  807ff4) pc:    22bac lr:    15eec sp:   806f20  UnixLibErrorHandler()
  (  806f48) pc:    15e68 lr:    190b0 sp:   806f4c  __ttywrite()
  (  806f6c) pc:    18fcc lr:    23d6c sp:   806f70  write()
  (  806f88) pc:    23c98 lr:    243a8 sp:   806f8c  __flsbuf()
  (  806fa4) pc:    242f8 lr:     8908 sp:   806fa8  fputc()
  (  806fbc) pc:     88d0 lr:     831c sp:   806fc0  puts()
  (  806fec) pc:     8240 lr:    11078 sp:   806ff0  main()

-- 
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