execl bug?
Christopher Weimann
csw at k12hq.com
Wed Dec 29 15:05:23 PST 2004
It seems that the open handle is carried through and usable by the child
program unless the parent has done something to move the file pointer.
For example the program below (tst.c) opens a file, reads a line, rewinds
then uses execl to call "cat -" which ought to send the file to stdout.
I thought I must be misunderstanding how execl is supposed to work so
I tried it on a Redhat box to see if everything behaves the same. It
doesn't. On Redhat the file is displayed just as I would expect.
At first I thought this was a close-on-exec problem but that is not
set, plus it works if I remove the fgets call.
What am I missing?
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
int
main(argc, argv)
int argc;
char **argv;
{
pid_t pid;
int status ;
char buff[ 513 ];
int tempfd = -1 ;
if ((tempfd=open("tst.c", O_RDWR|O_CREAT, 0600)) < 0) {
perror("open");
}
close(0);
dup(tempfd);
close(tempfd);
printf( "\nclose on exec is %d \n" , fcntl( fileno(stdin), F_GETFD) && FD_CLOEXEC ) ;
/* comment out the following three lines and all is well on FreeBSD */
fgets( buff, 512, stdin ) ;
printf( "%s", buff ) ;
rewind( stdin ) ;
if ( (pid = fork()) < 0){
perror("execl error");
_exit(1) ;
} else if (pid == 0) { /* child */
if (execl( "/bin/cat","cat", "-" , (char *) 0) < 0) {
perror("execl error");
_exit(1) ;
}
}
if (waitpid(pid, &status, 0) < 0){ /* parent */
perror("execl error");
exit(1) ;
}
if( WIFEXITED(status) ) {
exit(WEXITSTATUS(status));
}else{
exit( 1 ) ;
}
}
--
More information about the freebsd-hackers
mailing list