diff options
-rw-r--r-- | src/gpg-error.h.in | 3 | ||||
-rw-r--r-- | src/gpgrt-int.h | 7 | ||||
-rw-r--r-- | src/spawn-posix.c | 19 | ||||
-rw-r--r-- | src/spawn-w32.c | 13 |
4 files changed, 32 insertions, 10 deletions
diff --git a/src/gpg-error.h.in b/src/gpg-error.h.in index ccacf35..161a3ad 100644 --- a/src/gpg-error.h.in +++ b/src/gpg-error.h.in @@ -1087,6 +1087,9 @@ void _gpgrt_log_assert (const char *expr, const char *file, int line, #define GPGRT_SPAWN_NONBLOCK 16 /* Set the streams to non-blocking. */ #define GPGRT_SPAWN_RUN_ASFW 64 /* Use AllowSetForegroundWindow on W32. */ #define GPGRT_SPAWN_DETACHED 128 /* Start the process in the background. */ +#define GPGRT_SPAWN_KEEP_STDIN 256 +#define GPGRT_SPAWN_KEEP_STDOUT 512 +#define GPGRT_SPAWN_KEEP_STDERR 1024 #if 0 diff --git a/src/gpgrt-int.h b/src/gpgrt-int.h index 8df7901..78d5938 100644 --- a/src/gpgrt-int.h +++ b/src/gpgrt-int.h @@ -675,6 +675,13 @@ gpg_err_code_t _gpgrt_make_pipe (int filedes[2], estream_t *r_fp, * On W32 run AllowSetForegroundWindow for the child. Note that * due to unknown problems this actually allows * SetForegroundWindow for all children of this process. + * + * GNUPG_SPAWN_KEEP_STDIN + * GNUPG_SPAWN_KEEP_STDOUT + * GNUPG_SPAWN_KEEP_STDERR + * Do not assign /dev/null to a non-required standard file + * descriptor. + * */ gpg_err_code_t _gpgrt_spawn_process (const char *pgmname, const char *argv[], diff --git a/src/spawn-posix.c b/src/spawn-posix.c index 54a6a68..eb4f534 100644 --- a/src/spawn-posix.c +++ b/src/spawn-posix.c @@ -258,16 +258,21 @@ get_all_open_fds (void) static void do_exec (const char *pgmname, const char *argv[], int fd_in, int fd_out, int fd_err, - int *except, void (*preexec)(void) ) + int *except, void (*preexec)(void), unsigned int flags) { char **arg_list; int i, j; int fds[3]; + int nodevnull[3]; fds[0] = fd_in; fds[1] = fd_out; fds[2] = fd_err; + nodevnull[0] = !!(flags & GPGRT_SPAWN_KEEP_STDIN); + nodevnull[1] = !!(flags & GPGRT_SPAWN_KEEP_STDOUT); + nodevnull[2] = !!(flags & GPGRT_SPAWN_KEEP_STDERR); + /* Create the command line argument array. */ i = 0; if (argv) @@ -292,7 +297,9 @@ do_exec (const char *pgmname, const char *argv[], /* Assign /dev/null to unused FDs. */ for (i=0; i <= 2; i++) { - if (fds[i] == -1 ) + if (nodevnull[i]) + continue; + if (fds[i] == -1) { fds[i] = open ("/dev/null", i? O_WRONLY : O_RDONLY); if (fds[i] == -1) @@ -304,6 +311,8 @@ do_exec (const char *pgmname, const char *argv[], /* Connect the standard files. */ for (i=0; i <= 2; i++) { + if (nodevnull[i]) + continue; if (fds[i] != i && dup2 (fds[i], i) == -1) _gpgrt_log_fatal ("dup2 std%s failed: %s\n", i==0?"in":i==1?"out":"err", strerror (errno)); @@ -503,7 +512,7 @@ _gpgrt_spawn_process (const char *pgmname, const char *argv[], _gpgrt_fclose (outfp); _gpgrt_fclose (errfp); do_exec (pgmname, argv, inpipe[0], outpipe[1], errpipe[1], - except, preexec); + except, preexec, flags); /*NOTREACHED*/ } @@ -549,7 +558,7 @@ _gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], gcry_control (GCRYCTL_TERM_SECMEM); can be done. */ /* Run child. */ - do_exec (pgmname, argv, infd, outfd, errfd, NULL, NULL); + do_exec (pgmname, argv, infd, outfd, errfd, NULL, NULL, 0); /*NOTREACHED*/ } @@ -849,7 +858,7 @@ _gpgrt_spawn_process_detached (const char *pgmname, const char *argv[], putenv (p); } - do_exec (pgmname, argv, -1, -1, -1, NULL, NULL); + do_exec (pgmname, argv, -1, -1, -1, NULL, NULL, 0); /*NOTREACHED*/ } diff --git a/src/spawn-w32.c b/src/spawn-w32.c index 90c0538..8fecfa6 100644 --- a/src/spawn-w32.c +++ b/src/spawn-w32.c @@ -530,11 +530,14 @@ _gpgrt_spawn_process (const char *pgmname, const char *argv[], return err; if (inpipe[0] == INVALID_HANDLE_VALUE) - nullhd[0] = w32_open_null (0); - if (outpipe[1] == INVALID_HANDLE_VALUE) - nullhd[1] = w32_open_null (1); - if (errpipe[1] == INVALID_HANDLE_VALUE) - nullhd[2] = w32_open_null (1); + nullhd[0] = ((flags & GPGRT_SPAWN_KEEP_STDIN)? + GetStdHandle (STD_INPUT_HANDLE) : w32_open_null (0)); + if (outpipe[1] == INVALID_HANDLE_VALUE) + nullhd[1] = ((flags & GPGRT_SPAWN_KEEP_STDOUT)? + GetStdHandle (STD_OUTPUT_HANDLE) : w32_open_null (1)); + if (errpipe[1] == INVALID_HANDLE_VALUE) + nullhd[2] = ((flags & GPGRT_SPAWN_KEEP_STDOUT)? + GetStdHandle (STD_ERROR_HANDLE) : w32_open_null (1)); /* Start the process. Note that we can't run the PREEXEC function because this might change our own environment. */ |