diff --git a/common/ChangeLog b/common/ChangeLog index f4a8833ab..60428210e 100644 --- a/common/ChangeLog +++ b/common/ChangeLog @@ -1,5 +1,12 @@ 2010-03-22 Werner Koch + * exechelp.c (create_inheritable_pipe_r) + (create_inheritable_pipe_w): Fold both into ... + (create_inheritable_pipe): .. New. Change callers to use this. + (gnupg_create_inbound_pipe, gnupg_create_outbound_pipe): Factor + code out to ... + (do_create_pipe): .. New. + * init.c (parse_std_file_handles): Change to use rendezvous ids. 2010-03-15 Werner Koch diff --git a/common/exechelp.c b/common/exechelp.c index d156cef5d..36a50335a 100644 --- a/common/exechelp.c +++ b/common/exechelp.c @@ -250,7 +250,7 @@ build_w32_commandline_copy (char *buffer, const char *string) p = stpcpy (p, "\"\""); else if (strpbrk (string, " \t\n\v\f\"")) { - /* Need top do some kind of quoting. */ + /* Need to do some kind of quoting. */ p = stpcpy (p, "\""); for (s=string; *s; s++) { @@ -311,9 +311,10 @@ build_w32_commandline (const char *pgmname, const char * const *argv, #ifdef HAVE_W32_SYSTEM -/* Create pipe where the write end is inheritable. */ +/* Create pipe where one end is inheritable: With an INHERIT_IDX of 0 + the read end is inheritable, with 1 the write end is inheritable. */ static int -create_inheritable_pipe_w (int filedes[2]) +create_inheritable_pipe (int filedes[2], int inherit_idx) { HANDLE r, w, h; SECURITY_ATTRIBUTES sec_attr; @@ -325,7 +326,7 @@ create_inheritable_pipe_w (int filedes[2]) if (!CreatePipe (&r, &w, &sec_attr, 0)) return -1; - if (!DuplicateHandle (GetCurrentProcess(), w, + if (!DuplicateHandle (GetCurrentProcess(), inherit_idx? w : r, GetCurrentProcess(), &h, 0, TRUE, DUPLICATE_SAME_ACCESS )) { @@ -334,39 +335,17 @@ create_inheritable_pipe_w (int filedes[2]) CloseHandle (w); return -1; } - CloseHandle (w); - w = h; - filedes[0] = handle_to_fd (r); - filedes[1] = handle_to_fd (w); - return 0; -} - -/* Create pipe where the read end is inheritable. */ -static int -create_inheritable_pipe_r (int filedes[2]) -{ - HANDLE r, w, h; - SECURITY_ATTRIBUTES sec_attr; - - memset (&sec_attr, 0, sizeof sec_attr ); - sec_attr.nLength = sizeof sec_attr; - sec_attr.bInheritHandle = FALSE; - - if (!CreatePipe (&r, &w, &sec_attr, 0)) - return -1; - - if (!DuplicateHandle (GetCurrentProcess(), r, - GetCurrentProcess(), &h, 0, - TRUE, DUPLICATE_SAME_ACCESS )) + if (inherit_idx) { - log_error ("DuplicateHandle failed: %s\n", w32_strerror (-1)); - CloseHandle (r); CloseHandle (w); - return -1; + w = h; + } + else + { + CloseHandle (r); + r = h; } - CloseHandle (r); - r = h; filedes[0] = handle_to_fd (r); filedes[1] = handle_to_fd (w); @@ -454,10 +433,8 @@ do_exec (const char *pgmname, const char *argv[], #endif /*!HAVE_W32_SYSTEM*/ -/* Portable function to create a pipe. Under Windows the write end is - inheritable. */ -gpg_error_t -gnupg_create_inbound_pipe (int filedes[2]) +static gpg_error_t +do_create_pipe (int filedes[2], int inherit_idx) { gpg_error_t err = 0; #if HAVE_W32_SYSTEM @@ -465,7 +442,7 @@ gnupg_create_inbound_pipe (int filedes[2]) filedes[0] = filedes[1] = -1; err = gpg_error (GPG_ERR_GENERAL); - if (!create_inheritable_pipe_w (fds)) + if (!create_inheritable_pipe (fds, inherit_idx)) { filedes[0] = _open_osfhandle (fds[0], 0); if (filedes[0] == -1) @@ -497,48 +474,21 @@ gnupg_create_inbound_pipe (int filedes[2]) return err; } +/* Portable function to create a pipe. Under Windows the write end is + inheritable. */ +gpg_error_t +gnupg_create_inbound_pipe (int filedes[2]) +{ + return do_create_pipe (filedes, 1); +} + /* Portable function to create a pipe. Under Windows the read end is inheritable. */ gpg_error_t gnupg_create_outbound_pipe (int filedes[2]) { - gpg_error_t err = 0; -#if HAVE_W32_SYSTEM - int fds[2]; - - filedes[0] = filedes[1] = -1; - err = gpg_error (GPG_ERR_GENERAL); - if (!create_inheritable_pipe_r (fds)) - { - filedes[0] = _open_osfhandle (fds[0], 0); - if (filedes[0] == -1) - { - log_error ("failed to translate osfhandle %p\n", (void*)fds[0]); - CloseHandle (fd_to_handle (fds[1])); - } - else - { - filedes[1] = _open_osfhandle (fds[1], 1); - if (filedes[1] == -1) - { - log_error ("failed to translate osfhandle %p\n", (void*)fds[1]); - close (filedes[0]); - filedes[0] = -1; - CloseHandle (fd_to_handle (fds[1])); - } - else - err = 0; - } - } -#else - if (pipe (filedes) == -1) - { - err = gpg_error_from_syserror (); - filedes[0] = filedes[1] = -1; - } -#endif - return err; + return do_create_pipe (filedes, 0); } @@ -606,7 +556,7 @@ gnupg_spawn_process (const char *pgmname, const char *argv[], return err; /* Create a pipe. */ - if (create_inheritable_pipe_w (rp)) + if (create_inheritable_pipe (rp, 1)) { err = gpg_error (GPG_ERR_GENERAL); log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));