1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-06-18 00:49:50 +02:00

Change spawn_cb semantics.

--

So that the callback can replace fds for stdin/out/err.

Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2022-11-25 20:10:31 +09:00
parent 202d7b47e7
commit 7571fd4cd0
No known key found for this signature in database
GPG Key ID: 640114AF89DE6054
2 changed files with 20 additions and 12 deletions

View File

@ -993,31 +993,33 @@ my_exec (const char *pgmname, const char *argv[],
int (*spawn_cb) (void *), void *spawn_cb_arg)
{
int i;
int fds[3];
int ask_inherit = 0;
struct spawn_cb_arg sca;
fds[0] = fd_in;
fds[1] = fd_out;
fds[2] = fd_err;
sca.fds[0] = fd_in;
sca.fds[1] = fd_out;
sca.fds[2] = fd_err;
sca.arg = spawn_cb_arg;
/* Assign /dev/null to unused FDs. */
for (i = 0; i <= 2; i++)
if (fds[i] == -1)
fds[i] = posix_open_null (i);
sca.fds[i] = posix_open_null (i);
if (spawn_cb)
ask_inherit = (*spawn_cb) (&sca);
/* Connect the standard files. */
for (i = 0; i <= 2; i++)
if (fds[i] != i)
if (sca.fds[i] != i)
{
if (dup2 (fds[i], i) == -1)
if (dup2 (sca.fds[i], i) == -1)
log_fatal ("dup2 std%s failed: %s\n",
i==0?"in":i==1?"out":"err", strerror (errno));
close (fds[i]);
close (sca.fds[i]);
}
if (spawn_cb)
ask_inherit = (*spawn_cb) (spawn_cb_arg);
/* Close all other files. */
if (!ask_inherit)
close_all_fds (3, NULL);

View File

@ -211,6 +211,7 @@ gpg_error_t gnupg_spawn_process_detached (const char *pgmname,
/* The opaque type for a subprocess. */
typedef struct gnupg_process *gnupg_process_t;
#ifdef HAVE_W32_SYSTEM
struct spawn_cb_arg;
#ifdef NEED_STRUCT_SPAWN_CB_ARG
struct spawn_cb_arg {
void *plpAttributeList;
@ -218,6 +219,11 @@ struct spawn_cb_arg {
void *arg;
};
#endif
#else
struct spawn_cb_arg {
int fds[3];
void *arg;
};
#endif
/* Internal flag to ihnerit file descriptor/handle */
@ -245,7 +251,7 @@ struct spawn_cb_arg {
/* Spawn PGMNAME. */
gpg_err_code_t gnupg_process_spawn (const char *pgmname, const char *argv[],
unsigned int flags,
int (*spawn_cb) (void *),
int (*spawn_cb) (struct spawn_cb_arg *),
void *spawn_cb_arg,
gnupg_process_t *r_process);