1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-10 21:38:50 +01:00

w32: Fix inheriting HANDLEs.

Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2023-05-02 22:21:53 +09:00
parent b674a704e9
commit b9e65faa5d
No known key found for this signature in database
GPG Key ID: 640114AF89DE6054
2 changed files with 75 additions and 42 deletions

View File

@ -465,6 +465,7 @@ spawn_detached (const char *pgmname, char *cmdline,
gpg_err_code_t ec; gpg_err_code_t ec;
int ret; int ret;
struct spawn_cb_arg sca; struct spawn_cb_arg sca;
BOOL ask_inherit = FALSE;
ec = gnupg_access (pgmname, X_OK); ec = gnupg_access (pgmname, X_OK);
if (ec) if (ec)
@ -475,7 +476,6 @@ spawn_detached (const char *pgmname, char *cmdline,
memset (&si, 0, sizeof si); memset (&si, 0, sizeof si);
sca.ask_inherit = FALSE;
sca.allow_foreground_window = FALSE; sca.allow_foreground_window = FALSE;
sca.hd[0] = INVALID_HANDLE_VALUE; sca.hd[0] = INVALID_HANDLE_VALUE;
sca.hd[1] = INVALID_HANDLE_VALUE; sca.hd[1] = INVALID_HANDLE_VALUE;
@ -485,6 +485,43 @@ spawn_detached (const char *pgmname, char *cmdline,
if (spawn_cb) if (spawn_cb)
(*spawn_cb) (&sca); (*spawn_cb) (&sca);
if (sca.inherit_hds)
{
SIZE_T attr_list_size = 0;
HANDLE hd[16];
HANDLE *hd_p = sca.inherit_hds;
int j = 0;
if (hd_p)
{
while (*hd_p != INVALID_HANDLE_VALUE)
if (j < DIM (hd))
hd[j++] = *hd_p++;
else
{
log_error ("Too much handles\n");
break;
}
}
if (j)
{
InitializeProcThreadAttributeList (NULL, 1, 0, &attr_list_size);
si.lpAttributeList = xtrymalloc (attr_list_size);
if (si.lpAttributeList == NULL)
{
xfree (cmdline);
return gpg_err_code_from_syserror ();
}
InitializeProcThreadAttributeList (si.lpAttributeList, 1, 0,
&attr_list_size);
UpdateProcThreadAttribute (si.lpAttributeList, 0,
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
hd, sizeof (HANDLE) * j, NULL, NULL);
ask_inherit = TRUE;
}
}
/* Prepare security attributes. */ /* Prepare security attributes. */
memset (&sec_attr, 0, sizeof sec_attr ); memset (&sec_attr, 0, sizeof sec_attr );
sec_attr.nLength = sizeof sec_attr; sec_attr.nLength = sizeof sec_attr;
@ -510,7 +547,7 @@ spawn_detached (const char *pgmname, char *cmdline,
wcmdline, /* Command line arguments. */ wcmdline, /* Command line arguments. */
&sec_attr, /* Process security attributes. */ &sec_attr, /* Process security attributes. */
&sec_attr, /* Thread security attributes. */ &sec_attr, /* Thread security attributes. */
sca.ask_inherit, /* Inherit handles. */ ask_inherit, /* Inherit handles. */
cr_flags, /* Creation flags. */ cr_flags, /* Creation flags. */
NULL, /* Environment. */ NULL, /* Environment. */
NULL, /* Use current drive/directory. */ NULL, /* Use current drive/directory. */
@ -553,12 +590,8 @@ spawn_detached (const char *pgmname, char *cmdline,
void void
gnupg_spawn_helper (struct spawn_cb_arg *sca) gnupg_spawn_helper (struct spawn_cb_arg *sca)
{ {
int *user_except = sca->arg; HANDLE *user_except = sca->arg;
sca->inherit_hds = user_except;
if (user_except[0] == -1)
sca->ask_inherit = 0;
else
sca->ask_inherit = 1;
} }
gpg_err_code_t gpg_err_code_t
@ -583,6 +616,7 @@ gnupg_process_spawn (const char *pgmname, const char *argv[],
HANDLE hd_err[2]; HANDLE hd_err[2];
struct spawn_cb_arg sca; struct spawn_cb_arg sca;
int i; int i;
BOOL ask_inherit = FALSE;
check_syscall_func (); check_syscall_func ();
@ -706,7 +740,6 @@ gnupg_process_spawn (const char *pgmname, const char *argv[],
memset (&si, 0, sizeof si); memset (&si, 0, sizeof si);
sca.ask_inherit = FALSE;
sca.allow_foreground_window = FALSE; sca.allow_foreground_window = FALSE;
sca.hd[0] = hd_in[0]; sca.hd[0] = hd_in[0];
sca.hd[1] = hd_out[1]; sca.hd[1] = hd_out[1];
@ -749,37 +782,38 @@ gnupg_process_spawn (const char *pgmname, const char *argv[],
} }
} }
InitializeProcThreadAttributeList (NULL, 1, 0, &attr_list_size); if (j)
si.lpAttributeList = xtrymalloc (attr_list_size); {
if (si.lpAttributeList == NULL) InitializeProcThreadAttributeList (NULL, 1, 0, &attr_list_size);
{ si.lpAttributeList = xtrymalloc (attr_list_size);
if ((flags & GNUPG_PROCESS_STDIN_PIPE) if (si.lpAttributeList == NULL)
|| !(flags & GNUPG_PROCESS_STDIN_KEEP)) {
CloseHandle (hd_in[0]); if ((flags & GNUPG_PROCESS_STDIN_PIPE)
if ((flags & GNUPG_PROCESS_STDIN_PIPE)) || !(flags & GNUPG_PROCESS_STDIN_KEEP))
CloseHandle (hd_in[1]); CloseHandle (hd_in[0]);
if ((flags & GNUPG_PROCESS_STDOUT_PIPE)) if ((flags & GNUPG_PROCESS_STDIN_PIPE))
CloseHandle (hd_out[0]); CloseHandle (hd_in[1]);
if ((flags & GNUPG_PROCESS_STDOUT_PIPE) if ((flags & GNUPG_PROCESS_STDOUT_PIPE))
|| !(flags & GNUPG_PROCESS_STDOUT_KEEP)) CloseHandle (hd_out[0]);
CloseHandle (hd_out[1]); if ((flags & GNUPG_PROCESS_STDOUT_PIPE)
if ((flags & GNUPG_PROCESS_STDERR_PIPE)) || !(flags & GNUPG_PROCESS_STDOUT_KEEP))
CloseHandle (hd_err[0]); CloseHandle (hd_out[1]);
if ((flags & GNUPG_PROCESS_STDERR_PIPE) if ((flags & GNUPG_PROCESS_STDERR_PIPE))
|| !(flags & GNUPG_PROCESS_STDERR_KEEP)) CloseHandle (hd_err[0]);
CloseHandle (hd_err[1]); if ((flags & GNUPG_PROCESS_STDERR_PIPE)
xfree (wpgmname); || !(flags & GNUPG_PROCESS_STDERR_KEEP))
xfree (wcmdline); CloseHandle (hd_err[1]);
xfree (process); xfree (process);
xfree (cmdline); xfree (cmdline);
return gpg_err_code_from_syserror (); return gpg_err_code_from_syserror ();
} }
InitializeProcThreadAttributeList (si.lpAttributeList, 1, 0, InitializeProcThreadAttributeList (si.lpAttributeList, 1, 0,
&attr_list_size); &attr_list_size);
UpdateProcThreadAttribute (si.lpAttributeList, 0, UpdateProcThreadAttribute (si.lpAttributeList, 0,
PROC_THREAD_ATTRIBUTE_HANDLE_LIST, PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
hd, sizeof (HANDLE) * j, NULL, NULL); hd, sizeof (HANDLE) * j, NULL, NULL);
sca.ask_inherit = TRUE; ask_inherit = TRUE;
}
} }
/* Prepare security attributes. */ /* Prepare security attributes. */
@ -808,7 +842,7 @@ gnupg_process_spawn (const char *pgmname, const char *argv[],
wcmdline, /* Command line arguments. */ wcmdline, /* Command line arguments. */
&sec_attr, /* Process security attributes. */ &sec_attr, /* Process security attributes. */
&sec_attr, /* Thread security attributes. */ &sec_attr, /* Thread security attributes. */
sca.ask_inherit, /* Inherit handles. */ ask_inherit, /* Inherit handles. */
cr_flags, /* Creation flags. */ cr_flags, /* Creation flags. */
NULL, /* Environment. */ NULL, /* Environment. */
NULL, /* Use current drive/directory. */ NULL, /* Use current drive/directory. */

View File

@ -81,7 +81,6 @@ struct spawn_cb_arg;
struct spawn_cb_arg { struct spawn_cb_arg {
HANDLE hd[3]; HANDLE hd[3];
HANDLE *inherit_hds; HANDLE *inherit_hds;
BOOL ask_inherit;
BOOL allow_foreground_window; BOOL allow_foreground_window;
void *arg; void *arg;
}; };