pipe-fork-execve

Hi everyone , after a pipe() system call i've forked and entred into the child process to execve a shell script .the problem here is that when the execve sys call fail , i want to send the error code (eg errno) to the parent process using the pipe writer side p[1] , there is nothing received in the reader-side of the parent process , checkout:

int p[2];
pid_t pid;

pipe(p);
pid = fork();
if( pid < 1 ){
/* bla bla */
close(p[0]);
close(p[1]);
return ;
}
if( pid == 0 ){
close(p[0]);

fcntl(p[1],F_SETFD,0) ; /* do not close on exec */

execve("/bin/sh",arg,env);
int err = errno;
write(p[1],&err,sizeof(int)); /* why there is nothing received in my parent process */
_exit(127);
}

/* parent process /
close(p[1]);
if( read(p[0],&code,sizeof(int) ) < 0){
perror("nothing received");
/
this is true even if the execve() fail and there is something sent.. why */
}
Any one can help..thanks

The variable p must be an array of 2 integers and your program is ending here even if the fork call is successful (pid==0):

if( pid < 1 ){
/* bla bla */
return ;

Regards

Fixed but the problem isn't there.anyone could help

int p[2];
pid_t pid;

pipe(p);
pid = fork();
# if 0 
/* delete them */
/* en.. pid == 0 also meets pid < 1 , so child ends here */
if( pid < 1 ){
/* bla bla */
close(p[0]);
close(p[1]);
return ;
}
# endif
if( pid == 0 ){
close(p[0]);

fcntl(p[1],F_SETFD,0) ; /* do not close on exec */ /* i think this is redundant */

execve("/bin/sh",arg,env);
int err = errno;
write(p[1],&err,sizeof(int)); /* why there is nothing received in my parent process */
_exit(127);
}

/* parent process */
close(p[1]);
if( read(p[0],&code,sizeof(int) ) < 0){
perror("nothing received");
/* this is true even if the execve() fail and there is something sent.. why */
}

ivhb is right, fork return twice one is the child (return 0 ), the other is the the child pid