2010-03-22 16:00:54 +01:00
|
|
|
/* exechelp-w32.c - Fork and exec helpers for W32.
|
2010-03-08 13:18:19 +01:00
|
|
|
* Copyright (C) 2004, 2007, 2008, 2009,
|
|
|
|
* 2010 Free Software Foundation, Inc.
|
2004-12-06 19:28:56 +01:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2004-12-06 19:28:56 +01:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2004-12-06 19:28:56 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-04 21:49:40 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2004-12-06 19:28:56 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2010-03-22 16:00:54 +01:00
|
|
|
#if !defined(HAVE_W32_SYSTEM) || defined (HAVE_W32CE_SYSTEM)
|
|
|
|
#error This code is only used on W32.
|
|
|
|
#endif
|
|
|
|
|
2004-12-06 19:28:56 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2010-03-10 13:24:58 +01:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
# include <signal.h>
|
|
|
|
#endif
|
2011-02-04 12:57:53 +01:00
|
|
|
#include <unistd.h>
|
2006-09-07 17:13:33 +02:00
|
|
|
#include <fcntl.h>
|
2006-10-17 16:34:42 +02:00
|
|
|
|
Port to npth.
* configure.ac: Don't check for PTH but for NPTH.
(AH_BOTTOM): Remove PTH_SYSCALL_SOFT.
(have_pth): Rename to ...
(have_npth): ... this.
(USE_GNU_NPTH): Rename to ...
(USE_GNU_PTH): ... this.
* m4/npth.m4: New file.
* agent/Makefile.am, agent/cache.c, agent/call-pinentry.c,
agent/call-scd.c, agent/findkey.c, agent/gpg-agent.c,
agent/trustlist.c, common/Makefile.am, common/estream.c,
common/exechelp-posix.c, common/exechelp-w32.c,
common/exechelp-w32ce.c, common/http.c, common/init.c,
common/sysutils.c, dirmngr/Makefile.am, dirmngr/crlfetch.c,
dirmngr/dirmngr.c, dirmngr/dirmngr_ldap.c, dirmngr/ldap-wrapper-ce.c,
dirmngr/ldap-wrapper.c, dirmngr/ldap.c, g13/Makefile.am,
g13/call-gpg.c, g13/g13.c, g13/runner.c, scd/Makefile.am,
scd/apdu.c, scd/app.c, scd/ccid-driver.c, scd/command.c,
scd/scdaemon.c, tools/Makefile.am: Port to npth.
2012-01-03 22:12:37 +01:00
|
|
|
#ifdef WITHOUT_NPTH /* Give the Makefile a chance to build without Pth. */
|
|
|
|
#undef HAVE_NPTH
|
|
|
|
#undef USE_NPTH
|
2006-10-17 16:34:42 +02:00
|
|
|
#endif
|
|
|
|
|
2014-05-02 08:06:10 +02:00
|
|
|
#ifdef HAVE_NPTH
|
Port to npth.
* configure.ac: Don't check for PTH but for NPTH.
(AH_BOTTOM): Remove PTH_SYSCALL_SOFT.
(have_pth): Rename to ...
(have_npth): ... this.
(USE_GNU_NPTH): Rename to ...
(USE_GNU_PTH): ... this.
* m4/npth.m4: New file.
* agent/Makefile.am, agent/cache.c, agent/call-pinentry.c,
agent/call-scd.c, agent/findkey.c, agent/gpg-agent.c,
agent/trustlist.c, common/Makefile.am, common/estream.c,
common/exechelp-posix.c, common/exechelp-w32.c,
common/exechelp-w32ce.c, common/http.c, common/init.c,
common/sysutils.c, dirmngr/Makefile.am, dirmngr/crlfetch.c,
dirmngr/dirmngr.c, dirmngr/dirmngr_ldap.c, dirmngr/ldap-wrapper-ce.c,
dirmngr/ldap-wrapper.c, dirmngr/ldap.c, g13/Makefile.am,
g13/call-gpg.c, g13/g13.c, g13/runner.c, scd/Makefile.am,
scd/apdu.c, scd/app.c, scd/ccid-driver.c, scd/command.c,
scd/scdaemon.c, tools/Makefile.am: Port to npth.
2012-01-03 22:12:37 +01:00
|
|
|
#include <npth.h>
|
2004-12-06 19:28:56 +01:00
|
|
|
#endif
|
2009-03-19 08:09:31 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_STAT
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-12-06 19:28:56 +01:00
|
|
|
#include "util.h"
|
|
|
|
#include "i18n.h"
|
2009-04-01 12:51:53 +02:00
|
|
|
#include "sysutils.h"
|
2004-12-06 19:28:56 +01:00
|
|
|
#include "exechelp.h"
|
|
|
|
|
2004-12-15 15:15:54 +01:00
|
|
|
/* Define to 1 do enable debugging. */
|
2016-01-26 12:43:40 +01:00
|
|
|
#define DEBUG_W32_SPAWN 0
|
2004-12-15 15:15:54 +01:00
|
|
|
|
2004-12-06 19:28:56 +01:00
|
|
|
|
2007-08-22 12:55:07 +02:00
|
|
|
/* It seems Vista doesn't grok X_OK and so fails access() tests.
|
|
|
|
Previous versions interpreted X_OK as F_OK anyway, so we'll just
|
|
|
|
use F_OK directly. */
|
|
|
|
#undef X_OK
|
|
|
|
#define X_OK F_OK
|
2010-03-10 13:24:58 +01:00
|
|
|
|
2004-12-15 15:15:54 +01:00
|
|
|
/* We assume that a HANDLE can be represented by an int which should
|
|
|
|
be true for all i386 systems (HANDLE is defined as void *) and
|
|
|
|
these are the only systems for which Windows is available. Further
|
|
|
|
we assume that -1 denotes an invalid handle. */
|
|
|
|
# define fd_to_handle(a) ((HANDLE)(a))
|
|
|
|
# define handle_to_fd(a) ((int)(a))
|
|
|
|
# define pid_to_handle(a) ((HANDLE)(a))
|
|
|
|
# define handle_to_pid(a) ((int)(a))
|
|
|
|
|
|
|
|
|
2009-03-19 08:09:31 +01:00
|
|
|
/* Return the maximum number of currently allowed open file
|
|
|
|
descriptors. Only useful on POSIX systems but returns a value on
|
|
|
|
other systems too. */
|
|
|
|
int
|
|
|
|
get_max_fds (void)
|
|
|
|
{
|
|
|
|
int max_fds = -1;
|
|
|
|
|
|
|
|
#ifdef OPEN_MAX
|
|
|
|
if (max_fds == -1)
|
|
|
|
max_fds = OPEN_MAX;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (max_fds == -1)
|
|
|
|
max_fds = 256; /* Arbitrary limit. */
|
|
|
|
|
|
|
|
return max_fds;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-23 21:26:05 +02:00
|
|
|
/* Under Windows this is a dummy function. */
|
2009-03-19 08:09:31 +01:00
|
|
|
void
|
|
|
|
close_all_fds (int first, int *except)
|
|
|
|
{
|
2010-08-23 21:26:05 +02:00
|
|
|
(void)first;
|
|
|
|
(void)except;
|
2009-03-19 08:09:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Returns an array with all currently open file descriptors. The end
|
|
|
|
of the array is marked by -1. The caller needs to release this
|
|
|
|
array using the *standard free* and not with xfree. This allow the
|
2015-11-16 12:41:46 +01:00
|
|
|
use of this function right at startup even before libgcrypt has
|
2009-03-19 08:09:31 +01:00
|
|
|
been initialized. Returns NULL on error and sets ERRNO
|
|
|
|
accordingly. */
|
|
|
|
int *
|
|
|
|
get_all_open_fds (void)
|
|
|
|
{
|
|
|
|
int *array;
|
|
|
|
size_t narray;
|
|
|
|
int fd, max_fd, idx;
|
|
|
|
#ifndef HAVE_STAT
|
|
|
|
array = calloc (1, sizeof *array);
|
|
|
|
if (array)
|
|
|
|
array[0] = -1;
|
|
|
|
#else /*HAVE_STAT*/
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
max_fd = get_max_fds ();
|
|
|
|
narray = 32; /* If you change this change also t-exechelp.c. */
|
|
|
|
array = calloc (narray, sizeof *array);
|
|
|
|
if (!array)
|
|
|
|
return NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-03-19 08:09:31 +01:00
|
|
|
/* Note: The list we return is ordered. */
|
|
|
|
for (idx=0, fd=0; fd < max_fd; fd++)
|
|
|
|
if (!(fstat (fd, &statbuf) == -1 && errno == EBADF))
|
|
|
|
{
|
|
|
|
if (idx+1 >= narray)
|
|
|
|
{
|
|
|
|
int *tmp;
|
|
|
|
|
|
|
|
narray += (narray < 256)? 32:256;
|
|
|
|
tmp = realloc (array, narray * sizeof *array);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
free (array);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
array = tmp;
|
|
|
|
}
|
|
|
|
array[idx++] = fd;
|
|
|
|
}
|
|
|
|
array[idx] = -1;
|
|
|
|
#endif /*HAVE_STAT*/
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-20 13:16:42 +02:00
|
|
|
/* Helper function to build_w32_commandline. */
|
|
|
|
static char *
|
|
|
|
build_w32_commandline_copy (char *buffer, const char *string)
|
|
|
|
{
|
|
|
|
char *p = buffer;
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
if (!*string) /* Empty string. */
|
|
|
|
p = stpcpy (p, "\"\"");
|
|
|
|
else if (strpbrk (string, " \t\n\v\f\""))
|
|
|
|
{
|
2010-03-22 15:22:41 +01:00
|
|
|
/* Need to do some kind of quoting. */
|
2007-06-20 13:16:42 +02:00
|
|
|
p = stpcpy (p, "\"");
|
|
|
|
for (s=string; *s; s++)
|
|
|
|
{
|
|
|
|
*p++ = *s;
|
|
|
|
if (*s == '\"')
|
|
|
|
*p++ = *s;
|
|
|
|
}
|
|
|
|
*p++ = '\"';
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
p = stpcpy (p, string);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2004-12-15 15:15:54 +01:00
|
|
|
/* Build a command line for use with W32's CreateProcess. On success
|
|
|
|
CMDLINE gets the address of a newly allocated string. */
|
|
|
|
static gpg_error_t
|
2011-02-04 12:57:53 +01:00
|
|
|
build_w32_commandline (const char *pgmname, const char * const *argv,
|
2007-06-20 13:16:42 +02:00
|
|
|
char **cmdline)
|
2004-12-15 15:15:54 +01:00
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
const char *s;
|
|
|
|
char *buf, *p;
|
|
|
|
|
|
|
|
*cmdline = NULL;
|
2007-06-20 13:16:42 +02:00
|
|
|
n = 0;
|
|
|
|
s = pgmname;
|
|
|
|
n += strlen (s) + 1 + 2; /* (1 space, 2 quoting */
|
|
|
|
for (; *s; s++)
|
|
|
|
if (*s == '\"')
|
|
|
|
n++; /* Need to double inner quotes. */
|
2004-12-15 15:15:54 +01:00
|
|
|
for (i=0; (s=argv[i]); i++)
|
|
|
|
{
|
|
|
|
n += strlen (s) + 1 + 2; /* (1 space, 2 quoting */
|
|
|
|
for (; *s; s++)
|
|
|
|
if (*s == '\"')
|
|
|
|
n++; /* Need to double inner quotes. */
|
|
|
|
}
|
|
|
|
n++;
|
|
|
|
|
|
|
|
buf = p = xtrymalloc (n);
|
|
|
|
if (!buf)
|
2006-09-14 18:50:33 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2004-12-15 15:15:54 +01:00
|
|
|
|
2007-06-20 13:16:42 +02:00
|
|
|
p = build_w32_commandline_copy (p, pgmname);
|
2011-02-04 12:57:53 +01:00
|
|
|
for (i=0; argv[i]; i++)
|
2004-12-15 15:15:54 +01:00
|
|
|
{
|
2007-06-20 13:16:42 +02:00
|
|
|
*p++ = ' ';
|
|
|
|
p = build_w32_commandline_copy (p, argv[i]);
|
2004-12-15 15:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*cmdline= buf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-14 14:14:25 +01:00
|
|
|
#define INHERIT_READ 1
|
|
|
|
#define INHERIT_WRITE 2
|
|
|
|
#define INHERIT_BOTH (INHERIT_READ|INHERIT_WRITE)
|
|
|
|
|
|
|
|
/* Create pipe. FLAGS indicates which ends are inheritable. */
|
2004-12-15 15:15:54 +01:00
|
|
|
static int
|
2016-01-14 14:14:25 +01:00
|
|
|
create_inheritable_pipe (HANDLE filedes[2], int flags)
|
2004-12-15 15:15:54 +01:00
|
|
|
{
|
2016-01-14 14:14:25 +01:00
|
|
|
HANDLE r, w;
|
2004-12-15 15:15:54 +01:00
|
|
|
SECURITY_ATTRIBUTES sec_attr;
|
|
|
|
|
|
|
|
memset (&sec_attr, 0, sizeof sec_attr );
|
|
|
|
sec_attr.nLength = sizeof sec_attr;
|
2016-01-14 14:14:25 +01:00
|
|
|
sec_attr.bInheritHandle = TRUE;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-12-15 15:15:54 +01:00
|
|
|
if (!CreatePipe (&r, &w, &sec_attr, 0))
|
|
|
|
return -1;
|
|
|
|
|
2016-01-14 14:14:25 +01:00
|
|
|
if ((flags & INHERIT_READ) == 0)
|
|
|
|
if (! SetHandleInformation (r, HANDLE_FLAG_INHERIT, 0))
|
|
|
|
goto fail;
|
2004-12-15 15:15:54 +01:00
|
|
|
|
2016-01-14 14:14:25 +01:00
|
|
|
if ((flags & INHERIT_WRITE) == 0)
|
|
|
|
if (! SetHandleInformation (w, HANDLE_FLAG_INHERIT, 0))
|
|
|
|
goto fail;
|
2009-09-30 17:28:38 +02:00
|
|
|
|
2010-08-20 14:18:38 +02:00
|
|
|
filedes[0] = r;
|
|
|
|
filedes[1] = w;
|
2009-09-30 17:28:38 +02:00
|
|
|
return 0;
|
2016-01-14 14:14:25 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
log_error ("SetHandleInformation failed: %s\n", w32_strerror (-1));
|
|
|
|
CloseHandle (r);
|
|
|
|
CloseHandle (w);
|
|
|
|
return -1;
|
2009-09-30 17:28:38 +02:00
|
|
|
}
|
2004-12-15 15:15:54 +01:00
|
|
|
|
|
|
|
|
2007-08-27 20:10:27 +02:00
|
|
|
static HANDLE
|
|
|
|
w32_open_null (int for_write)
|
|
|
|
{
|
|
|
|
HANDLE hfile;
|
|
|
|
|
2010-03-22 13:46:05 +01:00
|
|
|
hfile = CreateFileW (L"nul",
|
|
|
|
for_write? GENERIC_WRITE : GENERIC_READ,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL, OPEN_EXISTING, 0, NULL);
|
2007-08-27 20:10:27 +02:00
|
|
|
if (hfile == INVALID_HANDLE_VALUE)
|
2012-06-05 19:29:22 +02:00
|
|
|
log_debug ("can't open 'nul': %s\n", w32_strerror (-1));
|
2007-08-27 20:10:27 +02:00
|
|
|
return hfile;
|
|
|
|
}
|
2006-09-07 17:13:33 +02:00
|
|
|
|
2004-12-06 19:28:56 +01:00
|
|
|
|
2010-03-22 15:22:41 +01:00
|
|
|
static gpg_error_t
|
2016-01-14 14:14:25 +01:00
|
|
|
do_create_pipe (int filedes[2], int flags)
|
2007-08-29 18:59:20 +02:00
|
|
|
{
|
|
|
|
gpg_error_t err = 0;
|
2010-08-20 14:18:38 +02:00
|
|
|
HANDLE fds[2];
|
2007-08-29 18:59:20 +02:00
|
|
|
|
|
|
|
filedes[0] = filedes[1] = -1;
|
|
|
|
err = gpg_error (GPG_ERR_GENERAL);
|
2016-01-14 14:14:25 +01:00
|
|
|
if (!create_inheritable_pipe (fds, flags))
|
2009-09-30 17:28:38 +02:00
|
|
|
{
|
2016-01-25 17:45:19 +01:00
|
|
|
filedes[0] = _open_osfhandle (handle_to_fd (fds[0]), O_RDONLY);
|
2009-09-30 17:28:38 +02:00
|
|
|
if (filedes[0] == -1)
|
|
|
|
{
|
2010-08-20 14:18:38 +02:00
|
|
|
log_error ("failed to translate osfhandle %p\n", fds[0]);
|
|
|
|
CloseHandle (fds[1]);
|
2009-09-30 17:28:38 +02:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
else
|
2009-09-30 17:28:38 +02:00
|
|
|
{
|
2016-01-25 17:45:19 +01:00
|
|
|
filedes[1] = _open_osfhandle (handle_to_fd (fds[1]), O_APPEND);
|
2009-09-30 17:28:38 +02:00
|
|
|
if (filedes[1] == -1)
|
|
|
|
{
|
2010-08-20 14:18:38 +02:00
|
|
|
log_error ("failed to translate osfhandle %p\n", fds[1]);
|
2009-09-30 17:28:38 +02:00
|
|
|
close (filedes[0]);
|
|
|
|
filedes[0] = -1;
|
2010-08-20 14:18:38 +02:00
|
|
|
CloseHandle (fds[1]);
|
2009-09-30 17:28:38 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-03-22 15:22:41 +01:00
|
|
|
/* Portable function to create a pipe. Under Windows the write end is
|
2016-05-27 15:41:55 +02:00
|
|
|
inheritable. If R_FP is not NULL, an estream is created for the
|
|
|
|
read end and stored at R_FP. */
|
2010-03-22 15:22:41 +01:00
|
|
|
gpg_error_t
|
2016-05-27 15:41:55 +02:00
|
|
|
gnupg_create_inbound_pipe (int filedes[2], estream_t *r_fp, int nonblock)
|
2010-03-22 15:22:41 +01:00
|
|
|
{
|
2016-05-27 15:41:55 +02:00
|
|
|
if (r_fp)
|
|
|
|
return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
|
|
|
|
else
|
|
|
|
return do_create_pipe (filedes, INHERIT_WRITE);
|
2010-03-22 15:22:41 +01:00
|
|
|
}
|
|
|
|
|
2009-09-30 17:28:38 +02:00
|
|
|
|
|
|
|
/* Portable function to create a pipe. Under Windows the read end is
|
2016-05-27 15:41:55 +02:00
|
|
|
inheritable. If R_FP is not NULL, an estream is created for the
|
|
|
|
write end and stored at R_FP. */
|
2009-09-30 17:28:38 +02:00
|
|
|
gpg_error_t
|
2016-05-27 15:41:55 +02:00
|
|
|
gnupg_create_outbound_pipe (int filedes[2], estream_t *r_fp, int nonblock)
|
2009-09-30 17:28:38 +02:00
|
|
|
{
|
2016-05-27 15:41:55 +02:00
|
|
|
if (r_fp)
|
|
|
|
return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
|
|
|
|
else
|
|
|
|
return do_create_pipe (filedes, INHERIT_READ);
|
2016-01-14 14:14:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Portable function to create a pipe. Under Windows both ends are
|
|
|
|
inheritable. */
|
|
|
|
gpg_error_t
|
|
|
|
gnupg_create_pipe (int filedes[2])
|
|
|
|
{
|
|
|
|
return do_create_pipe (filedes, INHERIT_BOTH);
|
2007-08-29 18:59:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-24 13:15:30 +01:00
|
|
|
/* Fork and exec the PGMNAME, see exechelp.h for details. */
|
2004-12-06 19:28:56 +01:00
|
|
|
gpg_error_t
|
|
|
|
gnupg_spawn_process (const char *pgmname, const char *argv[],
|
2016-05-27 22:48:04 +02:00
|
|
|
int *except, void (*preexec)(void), unsigned int flags,
|
2015-09-28 18:10:21 +02:00
|
|
|
estream_t *r_infp,
|
2010-08-20 14:18:38 +02:00
|
|
|
estream_t *r_outfp,
|
|
|
|
estream_t *r_errfp,
|
|
|
|
pid_t *pid)
|
2004-12-06 19:28:56 +01:00
|
|
|
{
|
2004-12-15 15:15:54 +01:00
|
|
|
gpg_error_t err;
|
|
|
|
SECURITY_ATTRIBUTES sec_attr;
|
2011-02-04 12:57:53 +01:00
|
|
|
PROCESS_INFORMATION pi =
|
2004-12-15 15:15:54 +01:00
|
|
|
{
|
|
|
|
NULL, /* Returns process handle. */
|
|
|
|
0, /* Returns primary thread handle. */
|
|
|
|
0, /* Returns pid. */
|
|
|
|
0 /* Returns tid. */
|
|
|
|
};
|
|
|
|
STARTUPINFO si;
|
|
|
|
int cr_flags;
|
|
|
|
char *cmdline;
|
2015-09-28 18:10:21 +02:00
|
|
|
HANDLE inpipe[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
|
2010-08-20 14:18:38 +02:00
|
|
|
HANDLE outpipe[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
|
|
|
|
HANDLE errpipe[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
|
2015-09-28 18:10:21 +02:00
|
|
|
estream_t infp = NULL;
|
2010-08-20 14:18:38 +02:00
|
|
|
estream_t outfp = NULL;
|
|
|
|
estream_t errfp = NULL;
|
|
|
|
HANDLE nullhd[3] = {INVALID_HANDLE_VALUE,
|
|
|
|
INVALID_HANDLE_VALUE,
|
|
|
|
INVALID_HANDLE_VALUE};
|
2010-06-07 15:33:02 +02:00
|
|
|
int i;
|
2010-08-20 14:18:38 +02:00
|
|
|
es_syshd_t syshd;
|
2016-05-27 15:25:03 +02:00
|
|
|
gpg_err_source_t errsource = default_errsource;
|
2004-12-06 19:28:56 +01:00
|
|
|
|
2016-05-27 22:48:04 +02:00
|
|
|
(void)except; /* Not yet used. */
|
|
|
|
|
2015-09-28 18:10:21 +02:00
|
|
|
if (r_infp)
|
|
|
|
*r_infp = NULL;
|
2010-08-20 14:18:38 +02:00
|
|
|
if (r_outfp)
|
|
|
|
*r_outfp = NULL;
|
|
|
|
if (r_errfp)
|
|
|
|
*r_errfp = NULL;
|
|
|
|
*pid = (pid_t)(-1); /* Always required. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2016-01-19 12:45:04 +01:00
|
|
|
if (r_infp)
|
2010-06-07 15:33:02 +02:00
|
|
|
{
|
2016-01-14 14:14:25 +01:00
|
|
|
if (create_inheritable_pipe (inpipe, INHERIT_READ))
|
2010-08-20 14:18:38 +02:00
|
|
|
{
|
2015-09-28 18:10:21 +02:00
|
|
|
err = gpg_err_make (errsource, GPG_ERR_GENERAL);
|
|
|
|
log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
syshd.type = ES_SYSHD_HANDLE;
|
|
|
|
syshd.u.handle = inpipe[1];
|
|
|
|
infp = es_sysopen (&syshd, "w");
|
|
|
|
if (!infp)
|
|
|
|
{
|
|
|
|
err = gpg_err_make (errsource, gpg_err_code_from_syserror ());
|
|
|
|
log_error (_("error creating a stream for a pipe: %s\n"),
|
|
|
|
gpg_strerror (err));
|
|
|
|
CloseHandle (inpipe[0]);
|
|
|
|
CloseHandle (inpipe[1]);
|
|
|
|
inpipe[0] = inpipe[1] = INVALID_HANDLE_VALUE;
|
|
|
|
return err;
|
2011-02-04 12:57:53 +01:00
|
|
|
}
|
2010-06-07 15:33:02 +02:00
|
|
|
}
|
|
|
|
|
2010-08-20 14:18:38 +02:00
|
|
|
if (r_outfp)
|
|
|
|
{
|
2016-01-14 14:14:25 +01:00
|
|
|
if (create_inheritable_pipe (outpipe, INHERIT_WRITE))
|
2010-08-20 14:18:38 +02:00
|
|
|
{
|
|
|
|
err = gpg_err_make (errsource, GPG_ERR_GENERAL);
|
|
|
|
log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
syshd.type = ES_SYSHD_HANDLE;
|
|
|
|
syshd.u.handle = outpipe[0];
|
|
|
|
outfp = es_sysopen (&syshd, "r");
|
|
|
|
if (!outfp)
|
|
|
|
{
|
|
|
|
err = gpg_err_make (errsource, gpg_err_code_from_syserror ());
|
|
|
|
log_error (_("error creating a stream for a pipe: %s\n"),
|
|
|
|
gpg_strerror (err));
|
|
|
|
CloseHandle (outpipe[0]);
|
|
|
|
CloseHandle (outpipe[1]);
|
|
|
|
outpipe[0] = outpipe[1] = INVALID_HANDLE_VALUE;
|
2015-09-28 18:10:21 +02:00
|
|
|
if (infp)
|
|
|
|
es_fclose (infp);
|
|
|
|
else if (inpipe[1] != INVALID_HANDLE_VALUE)
|
2016-01-19 13:09:06 +01:00
|
|
|
CloseHandle (inpipe[1]);
|
2015-09-28 18:10:21 +02:00
|
|
|
if (inpipe[0] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (inpipe[0]);
|
2010-08-20 14:18:38 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_errfp)
|
|
|
|
{
|
2016-01-14 14:14:25 +01:00
|
|
|
if (create_inheritable_pipe (errpipe, INHERIT_WRITE))
|
2010-08-20 14:18:38 +02:00
|
|
|
{
|
|
|
|
err = gpg_err_make (errsource, GPG_ERR_GENERAL);
|
|
|
|
log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
|
|
|
|
return err;
|
|
|
|
}
|
2010-06-07 15:33:02 +02:00
|
|
|
|
2010-08-20 14:18:38 +02:00
|
|
|
syshd.type = ES_SYSHD_HANDLE;
|
|
|
|
syshd.u.handle = errpipe[0];
|
|
|
|
errfp = es_sysopen (&syshd, "r");
|
|
|
|
if (!errfp)
|
|
|
|
{
|
|
|
|
err = gpg_err_make (errsource, gpg_err_code_from_syserror ());
|
|
|
|
log_error (_("error creating a stream for a pipe: %s\n"),
|
|
|
|
gpg_strerror (err));
|
|
|
|
CloseHandle (errpipe[0]);
|
|
|
|
CloseHandle (errpipe[1]);
|
|
|
|
errpipe[0] = errpipe[1] = INVALID_HANDLE_VALUE;
|
|
|
|
if (outfp)
|
|
|
|
es_fclose (outfp);
|
|
|
|
else if (outpipe[0] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (outpipe[0]);
|
|
|
|
if (outpipe[1] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (outpipe[1]);
|
2015-09-28 18:10:21 +02:00
|
|
|
if (infp)
|
|
|
|
es_fclose (infp);
|
|
|
|
else if (inpipe[1] != INVALID_HANDLE_VALUE)
|
2016-01-19 13:09:06 +01:00
|
|
|
CloseHandle (inpipe[1]);
|
2015-09-28 18:10:21 +02:00
|
|
|
if (inpipe[0] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (inpipe[0]);
|
2010-08-20 14:18:38 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2004-12-15 15:15:54 +01:00
|
|
|
|
|
|
|
/* Prepare security attributes. */
|
|
|
|
memset (&sec_attr, 0, sizeof sec_attr );
|
|
|
|
sec_attr.nLength = sizeof sec_attr;
|
|
|
|
sec_attr.bInheritHandle = FALSE;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-12-15 15:15:54 +01:00
|
|
|
/* Build the command line. */
|
|
|
|
err = build_w32_commandline (pgmname, argv, &cmdline);
|
|
|
|
if (err)
|
2011-02-04 12:57:53 +01:00
|
|
|
return err;
|
2004-12-15 15:15:54 +01:00
|
|
|
|
2016-01-20 12:13:35 +01:00
|
|
|
if (inpipe[0] == INVALID_HANDLE_VALUE)
|
2010-08-20 14:18:38 +02:00
|
|
|
nullhd[0] = w32_open_null (0);
|
2016-01-20 12:13:35 +01:00
|
|
|
if (outpipe[1] == INVALID_HANDLE_VALUE)
|
|
|
|
nullhd[1] = w32_open_null (1);
|
|
|
|
if (errpipe[1] == INVALID_HANDLE_VALUE)
|
|
|
|
nullhd[2] = w32_open_null (1);
|
2010-06-07 15:33:02 +02:00
|
|
|
|
2004-12-15 15:15:54 +01:00
|
|
|
/* Start the process. Note that we can't run the PREEXEC function
|
2010-08-20 14:18:38 +02:00
|
|
|
because this might change our own environment. */
|
|
|
|
(void)preexec;
|
|
|
|
|
2004-12-15 15:15:54 +01:00
|
|
|
memset (&si, 0, sizeof si);
|
|
|
|
si.cb = sizeof (si);
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
|
|
|
si.wShowWindow = DEBUG_W32_SPAWN? SW_SHOW : SW_MINIMIZE;
|
2015-09-28 18:10:21 +02:00
|
|
|
si.hStdInput = inpipe[0] == INVALID_HANDLE_VALUE? nullhd[0] : inpipe[0];
|
2010-08-20 14:18:38 +02:00
|
|
|
si.hStdOutput = outpipe[1] == INVALID_HANDLE_VALUE? nullhd[1] : outpipe[1];
|
|
|
|
si.hStdError = errpipe[1] == INVALID_HANDLE_VALUE? nullhd[2] : errpipe[1];
|
2004-12-15 15:15:54 +01:00
|
|
|
|
|
|
|
cr_flags = (CREATE_DEFAULT_ERROR_MODE
|
2015-09-28 18:10:21 +02:00
|
|
|
| ((flags & GNUPG_SPAWN_DETACHED)? DETACHED_PROCESS : 0)
|
2004-12-15 15:15:54 +01:00
|
|
|
| GetPriorityClass (GetCurrentProcess ())
|
2011-02-04 12:57:53 +01:00
|
|
|
| CREATE_SUSPENDED);
|
2012-06-05 19:29:22 +02:00
|
|
|
/* log_debug ("CreateProcess, path='%s' cmdline='%s'\n", pgmname, cmdline); */
|
2004-12-15 15:15:54 +01:00
|
|
|
if (!CreateProcess (pgmname, /* Program to start. */
|
|
|
|
cmdline, /* Command line arguments. */
|
|
|
|
&sec_attr, /* Process security attributes. */
|
|
|
|
&sec_attr, /* Thread security attributes. */
|
|
|
|
TRUE, /* Inherit handles. */
|
|
|
|
cr_flags, /* Creation flags. */
|
|
|
|
NULL, /* Environment. */
|
|
|
|
NULL, /* Use current drive/directory. */
|
|
|
|
&si, /* Startup information. */
|
|
|
|
&pi /* Returns process information. */
|
|
|
|
))
|
|
|
|
{
|
|
|
|
log_error ("CreateProcess failed: %s\n", w32_strerror (-1));
|
|
|
|
xfree (cmdline);
|
2015-09-28 18:10:21 +02:00
|
|
|
if (infp)
|
|
|
|
es_fclose (infp);
|
|
|
|
else if (inpipe[1] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (outpipe[1]);
|
|
|
|
if (inpipe[0] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (inpipe[0]);
|
2010-08-20 14:18:38 +02:00
|
|
|
if (outfp)
|
|
|
|
es_fclose (outfp);
|
|
|
|
else if (outpipe[0] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (outpipe[0]);
|
|
|
|
if (outpipe[1] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (outpipe[1]);
|
|
|
|
if (errfp)
|
|
|
|
es_fclose (errfp);
|
|
|
|
else if (errpipe[0] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (errpipe[0]);
|
|
|
|
if (errpipe[1] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (errpipe[1]);
|
|
|
|
return gpg_err_make (errsource, GPG_ERR_GENERAL);
|
2004-12-15 15:15:54 +01:00
|
|
|
}
|
|
|
|
xfree (cmdline);
|
|
|
|
cmdline = NULL;
|
|
|
|
|
2010-06-07 15:33:02 +02:00
|
|
|
/* Close the inherited handles to /dev/null. */
|
|
|
|
for (i=0; i < DIM (nullhd); i++)
|
|
|
|
if (nullhd[i] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (nullhd[i]);
|
|
|
|
|
2010-08-20 14:18:38 +02:00
|
|
|
/* Close the inherited ends of the pipes. */
|
2015-09-28 18:10:21 +02:00
|
|
|
if (inpipe[0] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (inpipe[0]);
|
2010-08-20 14:18:38 +02:00
|
|
|
if (outpipe[1] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (outpipe[1]);
|
|
|
|
if (errpipe[1] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (errpipe[1]);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-08-20 14:18:38 +02:00
|
|
|
/* log_debug ("CreateProcess ready: hProcess=%p hThread=%p" */
|
|
|
|
/* " dwProcessID=%d dwThreadId=%d\n", */
|
|
|
|
/* pi.hProcess, pi.hThread, */
|
|
|
|
/* (int) pi.dwProcessId, (int) pi.dwThreadId); */
|
|
|
|
/* log_debug (" outfp=%p errfp=%p\n", outfp, errfp); */
|
|
|
|
|
2009-04-01 12:51:53 +02:00
|
|
|
/* Fixme: For unknown reasons AllowSetForegroundWindow returns an
|
2010-08-20 14:18:38 +02:00
|
|
|
invalid argument error if we pass it the correct processID. As a
|
|
|
|
workaround we use -1 (ASFW_ANY). */
|
2015-09-28 18:10:21 +02:00
|
|
|
if ((flags & GNUPG_SPAWN_RUN_ASFW))
|
2009-04-01 12:51:53 +02:00
|
|
|
gnupg_allow_set_foregound_window ((pid_t)(-1)/*pi.dwProcessId*/);
|
2004-12-15 15:15:54 +01:00
|
|
|
|
2007-06-20 13:16:42 +02:00
|
|
|
/* Process has been created suspended; resume it now. */
|
2004-12-15 15:15:54 +01:00
|
|
|
ResumeThread (pi.hThread);
|
2011-02-04 12:57:53 +01:00
|
|
|
CloseHandle (pi.hThread);
|
2004-12-15 15:15:54 +01:00
|
|
|
|
2015-09-28 18:10:21 +02:00
|
|
|
if (r_infp)
|
|
|
|
*r_infp = infp;
|
2010-08-20 14:18:38 +02:00
|
|
|
if (r_outfp)
|
|
|
|
*r_outfp = outfp;
|
|
|
|
if (r_errfp)
|
|
|
|
*r_errfp = errfp;
|
2004-12-15 15:15:54 +01:00
|
|
|
|
|
|
|
*pid = handle_to_pid (pi.hProcess);
|
|
|
|
return 0;
|
|
|
|
|
2004-12-06 19:28:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-27 20:10:27 +02:00
|
|
|
|
|
|
|
/* Simplified version of gnupg_spawn_process. This function forks and
|
|
|
|
then execs PGMNAME, while connecting INFD to stdin, OUTFD to stdout
|
|
|
|
and ERRFD to stderr (any of them may be -1 to connect them to
|
|
|
|
/dev/null). The arguments for the process are expected in the NULL
|
|
|
|
terminated array ARGV. The program name itself should not be
|
|
|
|
included there. Calling gnupg_wait_process is required.
|
|
|
|
|
|
|
|
Returns 0 on success or an error code. */
|
|
|
|
gpg_error_t
|
|
|
|
gnupg_spawn_process_fd (const char *pgmname, const char *argv[],
|
|
|
|
int infd, int outfd, int errfd, pid_t *pid)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
SECURITY_ATTRIBUTES sec_attr;
|
|
|
|
PROCESS_INFORMATION pi = { NULL, 0, 0, 0 };
|
|
|
|
STARTUPINFO si;
|
|
|
|
char *cmdline;
|
|
|
|
int i;
|
|
|
|
HANDLE stdhd[3];
|
|
|
|
|
|
|
|
/* Setup return values. */
|
|
|
|
*pid = (pid_t)(-1);
|
|
|
|
|
|
|
|
/* Prepare security attributes. */
|
|
|
|
memset (&sec_attr, 0, sizeof sec_attr );
|
|
|
|
sec_attr.nLength = sizeof sec_attr;
|
|
|
|
sec_attr.bInheritHandle = FALSE;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-08-27 20:10:27 +02:00
|
|
|
/* Build the command line. */
|
|
|
|
err = build_w32_commandline (pgmname, argv, &cmdline);
|
|
|
|
if (err)
|
2011-02-04 12:57:53 +01:00
|
|
|
return err;
|
2007-08-27 20:10:27 +02:00
|
|
|
|
|
|
|
memset (&si, 0, sizeof si);
|
|
|
|
si.cb = sizeof (si);
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
|
|
|
si.wShowWindow = DEBUG_W32_SPAWN? SW_SHOW : SW_MINIMIZE;
|
|
|
|
stdhd[0] = infd == -1? w32_open_null (0) : INVALID_HANDLE_VALUE;
|
|
|
|
stdhd[1] = outfd == -1? w32_open_null (1) : INVALID_HANDLE_VALUE;
|
|
|
|
stdhd[2] = errfd == -1? w32_open_null (1) : INVALID_HANDLE_VALUE;
|
|
|
|
si.hStdInput = infd == -1? stdhd[0] : (void*)_get_osfhandle (infd);
|
|
|
|
si.hStdOutput = outfd == -1? stdhd[1] : (void*)_get_osfhandle (outfd);
|
|
|
|
si.hStdError = errfd == -1? stdhd[2] : (void*)_get_osfhandle (errfd);
|
|
|
|
|
2012-06-05 19:29:22 +02:00
|
|
|
/* log_debug ("CreateProcess, path='%s' cmdline='%s'\n", pgmname, cmdline); */
|
2007-08-27 20:10:27 +02:00
|
|
|
if (!CreateProcess (pgmname, /* Program to start. */
|
|
|
|
cmdline, /* Command line arguments. */
|
|
|
|
&sec_attr, /* Process security attributes. */
|
|
|
|
&sec_attr, /* Thread security attributes. */
|
|
|
|
TRUE, /* Inherit handles. */
|
|
|
|
(CREATE_DEFAULT_ERROR_MODE
|
|
|
|
| GetPriorityClass (GetCurrentProcess ())
|
2008-02-15 23:57:52 +01:00
|
|
|
| CREATE_SUSPENDED | DETACHED_PROCESS),
|
2007-08-27 20:10:27 +02:00
|
|
|
NULL, /* Environment. */
|
|
|
|
NULL, /* Use current drive/directory. */
|
|
|
|
&si, /* Startup information. */
|
|
|
|
&pi /* Returns process information. */
|
|
|
|
))
|
|
|
|
{
|
|
|
|
log_error ("CreateProcess failed: %s\n", w32_strerror (-1));
|
|
|
|
err = gpg_error (GPG_ERR_GENERAL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
err = 0;
|
|
|
|
xfree (cmdline);
|
|
|
|
for (i=0; i < 3; i++)
|
|
|
|
if (stdhd[i] != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (stdhd[i]);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2008-12-05 13:01:01 +01:00
|
|
|
/* log_debug ("CreateProcess ready: hProcess=%p hThread=%p" */
|
|
|
|
/* " dwProcessID=%d dwThreadId=%d\n", */
|
|
|
|
/* pi.hProcess, pi.hThread, */
|
|
|
|
/* (int) pi.dwProcessId, (int) pi.dwThreadId); */
|
2007-08-27 20:10:27 +02:00
|
|
|
|
|
|
|
/* Process has been created suspended; resume it now. */
|
|
|
|
ResumeThread (pi.hThread);
|
2011-02-04 12:57:53 +01:00
|
|
|
CloseHandle (pi.hThread);
|
2007-08-27 20:10:27 +02:00
|
|
|
|
|
|
|
*pid = handle_to_pid (pi.hProcess);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
/* See exechelp.h for a description. */
|
2004-12-06 19:28:56 +01:00
|
|
|
gpg_error_t
|
2010-06-09 18:53:51 +02:00
|
|
|
gnupg_wait_process (const char *pgmname, pid_t pid, int hang, int *r_exitcode)
|
2004-12-06 19:28:56 +01:00
|
|
|
{
|
2016-01-14 18:20:14 +01:00
|
|
|
return gnupg_wait_processes (&pgmname, &pid, 1, hang, r_exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See exechelp.h for a description. */
|
|
|
|
gpg_error_t
|
|
|
|
gnupg_wait_processes (const char **pgmnames, pid_t *pids, size_t count,
|
|
|
|
int hang, int *r_exitcodes)
|
|
|
|
{
|
|
|
|
gpg_err_code_t ec = 0;
|
|
|
|
size_t i;
|
|
|
|
HANDLE *procs;
|
2004-12-15 15:15:54 +01:00
|
|
|
int code;
|
|
|
|
|
2016-01-14 18:20:14 +01:00
|
|
|
procs = xtrycalloc (count, sizeof *procs);
|
|
|
|
if (procs == NULL)
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
if (r_exitcodes)
|
|
|
|
r_exitcodes[i] = -1;
|
|
|
|
|
|
|
|
if (pids[i] == (pid_t)(-1))
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
2007-08-29 11:51:37 +02:00
|
|
|
|
2016-01-14 18:20:14 +01:00
|
|
|
procs[i] = fd_to_handle (pids[i]);
|
|
|
|
}
|
2004-12-15 15:15:54 +01:00
|
|
|
|
|
|
|
/* FIXME: We should do a pth_waitpid here. However this has not yet
|
|
|
|
been implemented. A special W32 pth system call would even be
|
|
|
|
better. */
|
2016-01-14 18:20:14 +01:00
|
|
|
code = WaitForMultipleObjects (count, procs, TRUE, hang? INFINITE : 0);
|
2011-02-04 12:57:53 +01:00
|
|
|
switch (code)
|
2004-12-15 15:15:54 +01:00
|
|
|
{
|
2010-06-09 18:53:51 +02:00
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
ec = GPG_ERR_TIMEOUT;
|
2016-01-14 18:20:14 +01:00
|
|
|
goto leave;
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
case WAIT_FAILED:
|
2016-01-14 18:20:14 +01:00
|
|
|
log_error (_("waiting for processes to terminate failed: %s\n"),
|
|
|
|
w32_strerror (-1));
|
2010-06-09 18:53:51 +02:00
|
|
|
ec = GPG_ERR_GENERAL;
|
2016-01-14 18:20:14 +01:00
|
|
|
goto leave;
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
case WAIT_OBJECT_0:
|
2016-01-14 18:20:14 +01:00
|
|
|
for (i = 0; i < count; i++)
|
2010-06-09 18:53:51 +02:00
|
|
|
{
|
2016-01-14 18:20:14 +01:00
|
|
|
DWORD exc;
|
|
|
|
|
|
|
|
if (! GetExitCodeProcess (procs[i], &exc))
|
|
|
|
{
|
|
|
|
log_error (_("error getting exit code of process %d: %s\n"),
|
|
|
|
(int) pids[i], w32_strerror (-1) );
|
|
|
|
ec = GPG_ERR_GENERAL;
|
|
|
|
}
|
|
|
|
else if (exc)
|
|
|
|
{
|
|
|
|
if (!r_exitcodes)
|
|
|
|
log_error (_("error running '%s': exit status %d\n"),
|
|
|
|
pgmnames[i], (int)exc);
|
|
|
|
else
|
|
|
|
r_exitcodes[i] = (int)exc;
|
|
|
|
ec = GPG_ERR_GENERAL;
|
|
|
|
}
|
2016-02-04 17:03:48 +01:00
|
|
|
else
|
2016-01-14 18:20:14 +01:00
|
|
|
{
|
|
|
|
if (r_exitcodes)
|
|
|
|
r_exitcodes[i] = 0;
|
|
|
|
}
|
2010-06-09 18:53:51 +02:00
|
|
|
}
|
|
|
|
break;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
default:
|
2016-01-14 18:20:14 +01:00
|
|
|
log_error ("WaitForMultipleObjects returned unexpected "
|
|
|
|
"code %d\n", code);
|
2010-06-09 18:53:51 +02:00
|
|
|
ec = GPG_ERR_GENERAL;
|
|
|
|
break;
|
2004-12-15 15:15:54 +01:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2016-01-14 18:20:14 +01:00
|
|
|
leave:
|
2004-12-06 19:28:56 +01:00
|
|
|
return gpg_err_make (GPG_ERR_SOURCE_DEFAULT, ec);
|
|
|
|
}
|
|
|
|
|
2006-09-07 17:13:33 +02:00
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
gnupg_release_process (pid_t pid)
|
|
|
|
{
|
|
|
|
if (pid != (pid_t)INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
HANDLE process = (HANDLE)pid;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
CloseHandle (process);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-16 12:41:46 +01:00
|
|
|
/* Spawn a new process and immediately detach from it. The name of
|
2006-09-07 17:13:33 +02:00
|
|
|
the program to exec is PGMNAME and its arguments are in ARGV (the
|
|
|
|
programname is automatically passed as first argument).
|
|
|
|
Environment strings in ENVP are set. An error is returned if
|
|
|
|
pgmname is not executable; to make this work it is necessary to
|
|
|
|
provide an absolute file name. All standard file descriptors are
|
|
|
|
connected to /dev/null. */
|
|
|
|
gpg_error_t
|
|
|
|
gnupg_spawn_process_detached (const char *pgmname, const char *argv[],
|
|
|
|
const char *envp[] )
|
|
|
|
{
|
2007-06-20 13:16:42 +02:00
|
|
|
gpg_error_t err;
|
|
|
|
SECURITY_ATTRIBUTES sec_attr;
|
2011-02-04 12:57:53 +01:00
|
|
|
PROCESS_INFORMATION pi =
|
2007-06-20 13:16:42 +02:00
|
|
|
{
|
|
|
|
NULL, /* Returns process handle. */
|
|
|
|
0, /* Returns primary thread handle. */
|
|
|
|
0, /* Returns pid. */
|
|
|
|
0 /* Returns tid. */
|
|
|
|
};
|
|
|
|
STARTUPINFO si;
|
|
|
|
int cr_flags;
|
|
|
|
char *cmdline;
|
|
|
|
|
|
|
|
|
2014-10-03 11:58:58 +02:00
|
|
|
/* We don't use ENVP. */
|
2008-10-20 15:53:23 +02:00
|
|
|
(void)envp;
|
2007-06-20 13:16:42 +02:00
|
|
|
|
|
|
|
if (access (pgmname, X_OK))
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
|
|
|
/* Prepare security attributes. */
|
|
|
|
memset (&sec_attr, 0, sizeof sec_attr );
|
|
|
|
sec_attr.nLength = sizeof sec_attr;
|
|
|
|
sec_attr.bInheritHandle = FALSE;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-06-20 13:16:42 +02:00
|
|
|
/* Build the command line. */
|
|
|
|
err = build_w32_commandline (pgmname, argv, &cmdline);
|
|
|
|
if (err)
|
2011-02-04 12:57:53 +01:00
|
|
|
return err;
|
2007-06-20 13:16:42 +02:00
|
|
|
|
|
|
|
/* Start the process. */
|
|
|
|
memset (&si, 0, sizeof si);
|
|
|
|
si.cb = sizeof (si);
|
|
|
|
si.dwFlags = STARTF_USESHOWWINDOW;
|
|
|
|
si.wShowWindow = DEBUG_W32_SPAWN? SW_SHOW : SW_MINIMIZE;
|
|
|
|
|
|
|
|
cr_flags = (CREATE_DEFAULT_ERROR_MODE
|
|
|
|
| GetPriorityClass (GetCurrentProcess ())
|
|
|
|
| CREATE_NEW_PROCESS_GROUP
|
2011-02-04 12:57:53 +01:00
|
|
|
| DETACHED_PROCESS);
|
2012-06-05 19:29:22 +02:00
|
|
|
/* log_debug ("CreateProcess(detached), path='%s' cmdline='%s'\n", */
|
2008-12-05 13:01:01 +01:00
|
|
|
/* pgmname, cmdline); */
|
2007-06-20 13:16:42 +02:00
|
|
|
if (!CreateProcess (pgmname, /* Program to start. */
|
|
|
|
cmdline, /* Command line arguments. */
|
|
|
|
&sec_attr, /* Process security attributes. */
|
|
|
|
&sec_attr, /* Thread security attributes. */
|
2007-06-21 20:44:48 +02:00
|
|
|
FALSE, /* Inherit handles. */
|
2007-06-20 13:16:42 +02:00
|
|
|
cr_flags, /* Creation flags. */
|
|
|
|
NULL, /* Environment. */
|
|
|
|
NULL, /* Use current drive/directory. */
|
|
|
|
&si, /* Startup information. */
|
|
|
|
&pi /* Returns process information. */
|
|
|
|
))
|
|
|
|
{
|
|
|
|
log_error ("CreateProcess(detached) failed: %s\n", w32_strerror (-1));
|
|
|
|
xfree (cmdline);
|
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
|
|
|
}
|
|
|
|
xfree (cmdline);
|
|
|
|
cmdline = NULL;
|
|
|
|
|
2008-12-05 13:01:01 +01:00
|
|
|
/* log_debug ("CreateProcess(detached) ready: hProcess=%p hThread=%p" */
|
|
|
|
/* " dwProcessID=%d dwThreadId=%d\n", */
|
|
|
|
/* pi.hProcess, pi.hThread, */
|
|
|
|
/* (int) pi.dwProcessId, (int) pi.dwThreadId); */
|
2007-06-20 13:16:42 +02:00
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
CloseHandle (pi.hThread);
|
2016-01-22 11:11:55 +01:00
|
|
|
CloseHandle (pi.hProcess);
|
2007-06-20 13:16:42 +02:00
|
|
|
|
|
|
|
return 0;
|
2006-09-07 17:13:33 +02:00
|
|
|
}
|
2009-10-13 21:17:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Kill a process; that is send an appropriate signal to the process.
|
|
|
|
gnupg_wait_process must be called to actually remove the process
|
|
|
|
from the system. An invalid PID is ignored. */
|
|
|
|
void
|
|
|
|
gnupg_kill_process (pid_t pid)
|
|
|
|
{
|
2010-03-22 16:00:54 +01:00
|
|
|
if (pid != (pid_t) INVALID_HANDLE_VALUE)
|
2009-10-13 21:17:24 +02:00
|
|
|
{
|
|
|
|
HANDLE process = (HANDLE) pid;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-10-13 21:17:24 +02:00
|
|
|
/* Arbitrary error code. */
|
|
|
|
TerminateProcess (process, 1);
|
|
|
|
}
|
|
|
|
}
|