2022-09-09 Bruno Haible <bruno@clisp.org>
+ posix_spawn-internal: Optimize DuplicateHandle calls on native Windows.
+ * lib/spawni.c (open_handle): Return an inheritable HANDLE.
+ (do_open): Don't call DuplicateHandle. Remove curr_process parameter.
+ (__spawni): Update.
+
posix_spawn-internal: Optimize DuplicateHandle calls on native Windows.
* lib/windows-spawn.h (KEEP_OPEN_IN_PARENT): New macro.
* lib/windows-spawn.c (init_inheritable_handles): When a handle is
return memiszero (s, sizeof (sigset_t));
}
-/* Opens a HANDLE to a file.
+/* Opens an inheritable HANDLE to a file.
Upon failure, returns INVALID_HANDLE_VALUE with errno set. */
static HANDLE
open_handle (const char *name, int flags, mode_t mode)
CreateFile
<https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilea>
<https://docs.microsoft.com/en-us/windows/desktop/FileIO/creating-and-opening-files> */
+ SECURITY_ATTRIBUTES sec_attr;
+ sec_attr.nLength = sizeof (SECURITY_ATTRIBUTES);
+ sec_attr.lpSecurityDescriptor = NULL;
+ sec_attr.bInheritHandle = TRUE;
HANDLE handle =
CreateFile (rname,
((flags & (O_WRONLY | O_RDWR)) != 0
? GENERIC_READ | GENERIC_WRITE
: GENERIC_READ),
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- NULL,
+ &sec_attr,
((flags & O_CREAT) != 0
? ((flags & O_EXCL) != 0
? CREATE_NEW
static int
do_open (struct inheritable_handles *inh_handles, int newfd,
const char *filename, const char *directory,
- int flags, mode_t mode, HANDLE curr_process)
+ int flags, mode_t mode)
{
if (!(newfd >= 0 && newfd < _getmaxstdio ()))
{
return -1;
}
free (filename_to_free);
- /* Duplicate the handle, so that it becomes inheritable. */
- if (!DuplicateHandle (curr_process, handle,
- curr_process, &inh_handles->handles[newfd],
- 0, TRUE,
- DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS))
- {
- errno = EBADF; /* arbitrary */
- return -1;
- }
+ inh_handles->handles[newfd] = handle;
inh_handles->flags[newfd] =
((flags & O_APPEND) != 0 ? 32 : 0) | KEEP_OPEN_IN_CHILD;
return 0;
int flags = action->action.open_action.oflag;
mode_t mode = action->action.open_action.mode;
if (do_open (&inh_handles, newfd, filename, directory,
- flags, mode, curr_process)
+ flags, mode)
< 0)
goto failed_2;
}