close-on-exec handles are left open by exec parent

egor duda
Tue Jul 24 04:50:00 GMT 2001


there was bug reports about "expect> spawn ping" not returning to
expect prompt. Here's the simple testcase and analysis:

#include <unistd.h>
#include <sys/fcntl.h>

main (int argc, char** argv)
  int x[2];
  char a;
  int ret, pid;
  if (pipe (x) < 0)
      perror ("pipe");
      return 1;
  pid = fork ();
  switch (pid)
    case -1:
      perror ("fork"); return 1;
    case 0:
      close (x[0]); fcntl (x[1], F_SETFD, 1);
      execlp ("ping.exe", "ping.exe", "-t", "", NULL);
      close (x[1]);
      ret = read (x[0], &a, sizeof (a));
      if (ret < 0)
          perror ("read");
          return 1;
      return 0;

test.exe create pipe and forks. when write end of the pipe is marked
as close-on-exec, we DuplicateHandle () appropriately. but forked
instance left running after execlp () is performed, and it doesn't
close it's own copy of pipe handle. so read () blocks forever.

i can see 2 ways to fix it. We can either always perform
dtable::fixup_before_exec() and close handles marked as close-on-exec
there, or always close all handles when exec child is started and exec
parent cycles waiting for its child to exit.

egor.   icq 5165414 fidonet 2:5020/496.19

More information about the Cygwin-developers mailing list