diff options
author | NIIBE Yutaka <[email protected]> | 2025-07-15 06:27:13 +0000 |
---|---|---|
committer | NIIBE Yutaka <[email protected]> | 2025-07-15 06:27:13 +0000 |
commit | 311fb769d1ddb1aa6d7463b5b516359afebf208a (patch) | |
tree | 74af0e046a3417360bef2f2a9c674849143657da | |
parent | w32:spawn: Fix resource leaks for HANDLEs by w32_open_null. (diff) | |
download | libgpg-error-311fb769d1ddb1aa6d7463b5b516359afebf208a.tar.gz libgpg-error-311fb769d1ddb1aa6d7463b5b516359afebf208a.zip |
w32:spawn: New flag GPGRT_PROCESS_STDIO_NUL.
* src/gpg-error.h.in (GPGRT_PROCESS_STDIO_NUL): New.
* src/spawn-w32.c (w32_open_null): Add ENABLE_NULL_DEVICE argument.
(_gpgrt_process_spawn): Follow the change.
--
GnuPG-bug-id: 7723
Signed-off-by: NIIBE Yutaka <[email protected]>
-rw-r--r-- | src/gpg-error.h.in | 4 | ||||
-rw-r--r-- | src/spawn-w32.c | 34 |
2 files changed, 27 insertions, 11 deletions
diff --git a/src/gpg-error.h.in b/src/gpg-error.h.in index 4b4c60f..7672b71 100644 --- a/src/gpg-error.h.in +++ b/src/gpg-error.h.in @@ -1262,6 +1262,10 @@ void _gpgrt_log_assert (const char *expr, const char *file, int line, /* Allow the child process to set the foreground window (Windows only). */ #define GPGRT_PROCESS_ALLOW_SET_FG (1 << 4) +/* Child process uses "NUL" device for its stdin/stdout/stderr + * (Windows only). */ +#define GPGRT_PROCESS_STDIO_NUL (1 << 5) + /* Specify how to keep/connect standard fds. */ #define GPGRT_PROCESS_STDIN_PIPE (1 << 8) #define GPGRT_PROCESS_STDOUT_PIPE (1 << 9) diff --git a/src/spawn-w32.c b/src/spawn-w32.c index a3189ed..6c44a1a 100644 --- a/src/spawn-w32.c +++ b/src/spawn-w32.c @@ -211,17 +211,28 @@ create_inheritable_pipe (HANDLE filedes[2], int flags) static HANDLE -w32_open_null (int for_write) +w32_open_null (int for_write, int enable_null_device) { HANDLE hfile; - hfile = CreateFileW (L"nul", - for_write? GENERIC_WRITE : GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, OPEN_EXISTING, 0, NULL); - if (hfile == INVALID_HANDLE_VALUE) - _gpgrt_log_debug ("can't open 'nul': ec=%d\n", (int)GetLastError ()); - return hfile; + if (enable_null_device) + { + SECURITY_ATTRIBUTES sec_attr; + + /* Prepare security attributes. */ + memset (&sec_attr, 0, sizeof sec_attr ); + sec_attr.nLength = sizeof sec_attr; + sec_attr.bInheritHandle = TRUE; + hfile = CreateFileW (L"nul", + for_write? GENERIC_WRITE : GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, + &sec_attr, OPEN_EXISTING, 0, NULL); + if (hfile == INVALID_HANDLE_VALUE) + _gpgrt_log_debug ("can't open 'nul': ec=%d\n", (int)GetLastError ()); + return hfile; + } + else + return INVALID_HANDLE_VALUE; } @@ -732,6 +743,7 @@ _gpgrt_process_spawn (const char *pgmname, const char *argv[], BOOL ask_inherit = FALSE; struct gpgrt_spawn_actions act_default; char *env = NULL; + int enable_null_device = !!(flags & GPGRT_PROCESS_STDIO_NUL); if (!act) { @@ -865,7 +877,7 @@ _gpgrt_process_spawn (const char *pgmname, const char *argv[], hd_in[0] = act->hd[0]; } else if (hd_in[0] == INVALID_HANDLE_VALUE) - hd_in[0] = w32_open_null (0); + hd_in[0] = w32_open_null (0, enable_null_device); if (act->hd[1] != INVALID_HANDLE_VALUE) { if ((flags & GPGRT_PROCESS_STDOUT_PIPE)) @@ -877,7 +889,7 @@ _gpgrt_process_spawn (const char *pgmname, const char *argv[], hd_out[1] = act->hd[1]; } else if (hd_out[1] == INVALID_HANDLE_VALUE) - hd_out[1] = w32_open_null (1); + hd_out[1] = w32_open_null (1, enable_null_device); if (act->hd[2] != INVALID_HANDLE_VALUE) { if ((flags & GPGRT_PROCESS_STDERR_PIPE)) @@ -889,7 +901,7 @@ _gpgrt_process_spawn (const char *pgmname, const char *argv[], hd_err[1] = act->hd[2]; } else if (hd_err[1] == INVALID_HANDLE_VALUE) - hd_err[1] = w32_open_null (1); + hd_err[1] = w32_open_null (1, enable_null_device); { HANDLE hd[32]; |