Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 29 Dec 2004 18:05:21 -0500
From:      Christopher Weimann <csw@k12hq.com>
To:        freebsd-hackers@freebsd.org
Subject:   execl bug?
Message-ID:  <20041229230521.GB31984@smtp.k12us.com>

next in thread | raw e-mail | index | archive | help
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 ) ;
   }

}

-- 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20041229230521.GB31984>