* cygwin.din (dup3): Export.
(pipe2): Export. * dtable.cc (dtable::dup_worker): Take additional flags parameter. Handle O_CLOEXEC flag. (dtable::dup3): Rename from dup2. Take additional flags parameter. Check for valid flags. Drop check for newfd == oldfd. * dtable.h (dtable::dup_worker): Add flags parameter. (dtable::dup3): Rename from dup2. * fcntl.cc (fcntl64): Add F_DUPFD_CLOEXEC case. * fhandler.h (fhandler_mailslot::get_object_attr): Add flags parameter. * fhandler.cc (fhandler_base::open): Use security attribute with inheritance according to setting of O_CLOEXEC flag. * fhandler_console.cc (fhandler_console::open): Ditto. * fhandler_fifo.cc (sec_user_cloexec): New inline function to create security attribute with inheritance according to setting of O_CLOEXEC flag. (fhandler_fifo::open): Call sec_user_cloexec to fetch security attribute. (fhandler_fifo::wait): Ditto. * fhandler_mem.cc (fhandler_dev_mem::open): Ditto. * fhandler_mailslot.cc (fhandler_mailslot::get_object_attr): Take additional flags parameter. Use security attribute with inheritance according to setting of O_CLOEXEC flag. (fhandler_mailslot::open): Call get_object_attr with flags parameter. * fhandler_registry.cc (fhandler_registry::open): Call set_close_on_exec on real handles to accommodate O_CLOEXEC flag. * fhandler_tty.cc (fhandler_tty_slave::open): Ditto. * fhandler_tape.cc: Create mutex with inheritance according to setting of O_CLOEXEC flag. * pipe.cc: Replace usage of O_NOINHERIT with O_CLOEXEC. (fhandler_pipe::init): Simplify setting close_on_exec flag. (fhandler_pipe::open): Remove setting close_on_exec flag. (fhandler_pipe::create): Use security attribute with inheritance according to setting of O_CLOEXEC flag. (pipe2): New exported function. * posix_ipc.cc: Throughout, open backing files with O_CLOEXEC flag to follow POSIX semantics. * security.h (sec_none_cloexec): New define. * syscalls.cc (dup): Add missing extern "C" qualifier. Accommodate renaming of dtable::dup2 to dtable::dup3. (dup2): Ditto. Check newfd == oldfd here. (dup3): New function. Check newfd == oldfd here. (open): Set close_on_exec flag according to O_CLOEXEC flag before calling fhandler->open. * include/cygwin/version.h (CYGWIN_VERSION_API_MINOR): Bump.
This commit is contained in:
@@ -559,7 +559,7 @@ build_fh_pc (path_conv& pc, bool set_name)
|
||||
}
|
||||
|
||||
fhandler_base *
|
||||
dtable::dup_worker (fhandler_base *oldfh)
|
||||
dtable::dup_worker (fhandler_base *oldfh, int flags)
|
||||
{
|
||||
/* Don't call set_name in build_fh_pc. It will be called in
|
||||
fhandler_base::operator= below. Calling it twice will result
|
||||
@@ -579,7 +579,11 @@ dtable::dup_worker (fhandler_base *oldfh)
|
||||
}
|
||||
else
|
||||
{
|
||||
newfh->close_on_exec (false);
|
||||
/* The O_CLOEXEC flag enforces close-on-exec behaviour. */
|
||||
if (flags & O_CLOEXEC)
|
||||
newfh->set_close_on_exec (true);
|
||||
else
|
||||
newfh->close_on_exec (false);
|
||||
debug_printf ("duped '%s' old %p, new %p", oldfh->get_name (), oldfh->get_io_handle (), newfh->get_io_handle ());
|
||||
}
|
||||
}
|
||||
@@ -587,13 +591,13 @@ dtable::dup_worker (fhandler_base *oldfh)
|
||||
}
|
||||
|
||||
int
|
||||
dtable::dup2 (int oldfd, int newfd)
|
||||
dtable::dup3 (int oldfd, int newfd, int flags)
|
||||
{
|
||||
int res = -1;
|
||||
fhandler_base *newfh = NULL; // = NULL to avoid an incorrect warning
|
||||
|
||||
MALLOC_CHECK;
|
||||
debug_printf ("dup2 (%d, %d)", oldfd, newfd);
|
||||
debug_printf ("dup3 (%d, %d, %p)", oldfd, newfd, flags);
|
||||
lock ();
|
||||
|
||||
if (not_open (oldfd))
|
||||
@@ -602,21 +606,20 @@ dtable::dup2 (int oldfd, int newfd)
|
||||
set_errno (EBADF);
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (newfd < 0)
|
||||
{
|
||||
syscall_printf ("new fd out of bounds: %d", newfd);
|
||||
set_errno (EBADF);
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (newfd == oldfd)
|
||||
if ((flags & ~O_CLOEXEC) != 0)
|
||||
{
|
||||
res = newfd;
|
||||
goto done;
|
||||
syscall_printf ("invalid flags value %x", flags);
|
||||
set_errno (EINVAL);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((newfh = dup_worker (fds[oldfd])) == NULL)
|
||||
if ((newfh = dup_worker (fds[oldfd], flags)) == NULL)
|
||||
{
|
||||
res = -1;
|
||||
goto done;
|
||||
@@ -644,7 +647,7 @@ dtable::dup2 (int oldfd, int newfd)
|
||||
done:
|
||||
MALLOC_CHECK;
|
||||
unlock ();
|
||||
syscall_printf ("%d = dup2 (%d, %d)", res, oldfd, newfd);
|
||||
syscall_printf ("%d = dup3 (%d, %d, %p)", res, oldfd, newfd, flags);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
Reference in New Issue
Block a user