bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: new module 'dup3'


From: Bruno Haible
Subject: Re: new module 'dup3'
Date: Mon, 24 Aug 2009 01:34:01 +0200
User-agent: KMail/1.9.9

Eric Blake wrote:
> In the case where oldfd was previously marked O_NOINHERIT, dup2() on mingw
> appears to create newfd as O_NOINHERIT as well.  I think we have to rework
> this patch to use DuplicateHandle/_open_osfhandle on both control paths.

No, this is not true in my experiments (done on Windows XP):

$ cat showfd.c
#include <io.h>
#include <stdio.h>
#include <unistd.h>
#include <windows.h>
int main ()
{
  int fd;
  for (fd = 0; fd < 10; fd++)
    printf ("fd %d %s\n", fd,
            (HANDLE) _get_osfhandle (fd) == INVALID_HANDLE_VALUE ? "closed" : 
"open");
  return 0;
}

$ cat main.c
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <process.h>
int main ()
{
  int f3 = open ("foo.tmp", O_CREAT | O_TRUNC | O_RDWR, 0600);
  printf ("f3 = %d\n", f3);
  int f4 = open ("foo.tmp", O_CREAT | O_TRUNC | O_RDWR | O_NOINHERIT, 0600);
  printf ("f4 = %d\n", f4);
  int f5 = dup2 (f3, 5);
  printf ("f5 = %d\n", f5);
  int f6 = dup2 (f4, 6);
  printf ("f6 = %d\n", f6);
  fflush (stdout);
  _spawnl (_P_WAIT, "./showfd.exe", "showfd.exe", NULL);
  return 0;
}

$ gcc -O -Wall showfd.c -mno-cygwin -o showfd.exe
$ gcc -O -Wall main.c -mno-cygwin -o main.exe
$ ./main.exe
f3 = 3
f4 = 4
f5 = 0
f6 = 0
fd 0 open
fd 1 open
fd 2 open
fd 3 open
fd 4 closed
fd 5 open
fd 6 open
fd 7 closed
fd 8 closed
fd 9 closed

You see, while fd 4 was not inherited by the child process, fd 6 (being a dup2
of fd 4) was inherited. So, dup2 has reset the O_NOINHERIT flag on the newfd.
Just like POSIX says in [1].

Bruno

[1] http://www.opengroup.org/onlinepubs/9699919799/functions/dup.html




reply via email to

[Prev in Thread] Current Thread [Next in Thread]