execl bug?
From: Christopher Weimann (csw_at_k12hq.com)
Date: 12/30/04
- Previous message: Matteo Riondato: "Creating Compressed Loop FS from stdin"
- Next in thread: Dan Nelson: "Re: execl bug?"
- Reply: Dan Nelson: "Re: execl bug?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 29 Dec 2004 18:05:21 -0500 To: freebsd-hackers@freebsd.org
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 ) ;
}
}
-- _______________________________________________ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"
- Previous message: Matteo Riondato: "Creating Compressed Loop FS from stdin"
- Next in thread: Dan Nelson: "Re: execl bug?"
- Reply: Dan Nelson: "Re: execl bug?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|