|
Packit |
fc043f |
/* spawn-w32.c - Fork and exec helpers for W32.
|
|
Packit |
fc043f |
* Copyright (C) 2004, 2007-2009, 2010 Free Software Foundation, Inc.
|
|
Packit |
fc043f |
* Copyright (C) 2004, 2006-2012, 2014-2017 g10 Code GmbH
|
|
Packit |
fc043f |
*
|
|
Packit |
fc043f |
* This file is part of Libgpg-error.
|
|
Packit |
fc043f |
*
|
|
Packit |
fc043f |
* Libgpg-error is free software; you can redistribute it and/or
|
|
Packit |
fc043f |
* modify it under the terms of the GNU Lesser General Public License
|
|
Packit |
fc043f |
* as published by the Free Software Foundation; either version 2.1 of
|
|
Packit |
fc043f |
* the License, or (at your option) any later version.
|
|
Packit |
fc043f |
*
|
|
Packit |
fc043f |
* Libgpg-error is distributed in the hope that it will be useful, but
|
|
Packit |
fc043f |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
fc043f |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
fc043f |
* Lesser General Public License for more details.
|
|
Packit |
fc043f |
*
|
|
Packit |
fc043f |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
fc043f |
* License along with this program; if not, see <https://www.gnu.org/licenses/>.
|
|
Packit |
fc043f |
* SPDX-License-Identifier: LGPL-2.1+
|
|
Packit |
fc043f |
*
|
|
Packit |
fc043f |
* This file was originally a part of GnuPG.
|
|
Packit |
fc043f |
*/
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#include <config.h>
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#if !defined(HAVE_W32_SYSTEM) || defined (HAVE_W32CE_SYSTEM)
|
|
Packit |
fc043f |
#error This code is only used on W32.
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#include <stdio.h>
|
|
Packit |
fc043f |
#include <stdlib.h>
|
|
Packit |
fc043f |
#include <string.h>
|
|
Packit |
fc043f |
#include <errno.h>
|
|
Packit |
fc043f |
#include <assert.h>
|
|
Packit |
fc043f |
#ifdef HAVE_SIGNAL_H
|
|
Packit |
fc043f |
# include <signal.h>
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
#include <unistd.h>
|
|
Packit |
fc043f |
#include <fcntl.h>
|
|
Packit |
fc043f |
#ifdef HAVE_STAT
|
|
Packit |
fc043f |
# include <sys/stat.h>
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
#define WIN32_LEAN_AND_MEAN /* We only need the OS core stuff. */
|
|
Packit |
fc043f |
#include <windows.h>
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#include "gpgrt-int.h"
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Define to 1 do enable debugging. */
|
|
Packit |
fc043f |
#define DEBUG_W32_SPAWN 0
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* It seems Vista doesn't grok X_OK and so fails access() tests.
|
|
Packit |
fc043f |
* Previous versions interpreted X_OK as F_OK anyway, so we'll just
|
|
Packit |
fc043f |
* use F_OK directly. */
|
|
Packit |
fc043f |
#undef X_OK
|
|
Packit |
fc043f |
#define X_OK F_OK
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* We assume that a HANDLE can be represented by an int which should
|
|
Packit |
fc043f |
* be true for all i386 systems (HANDLE is defined as void *) and
|
|
Packit |
fc043f |
* these are the only systems for which Windows is available. Further
|
|
Packit |
fc043f |
* we assume that -1 denotes an invalid handle.
|
|
Packit |
fc043f |
* FIXME: With Windows 64 this is no longer true.
|
|
Packit |
fc043f |
*/
|
|
Packit |
fc043f |
#define fd_to_handle(a) ((HANDLE)(a))
|
|
Packit |
fc043f |
#define handle_to_fd(a) ((int)(a))
|
|
Packit |
fc043f |
#define pid_to_handle(a) ((HANDLE)(a))
|
|
Packit |
fc043f |
#define handle_to_pid(a) ((int)(a))
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Return the maximum number of currently allowed open file
|
|
Packit |
fc043f |
* descriptors. Only useful on POSIX systems but returns a value on
|
|
Packit |
fc043f |
* other systems too. */
|
|
Packit |
fc043f |
int
|
|
Packit |
fc043f |
get_max_fds (void)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
int max_fds = -1;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#ifdef OPEN_MAX
|
|
Packit |
fc043f |
if (max_fds == -1)
|
|
Packit |
fc043f |
max_fds = OPEN_MAX;
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (max_fds == -1)
|
|
Packit |
fc043f |
max_fds = 256; /* Arbitrary limit. */
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
return max_fds;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Under Windows this is a dummy function. */
|
|
Packit |
fc043f |
/* static void */
|
|
Packit |
fc043f |
/* close_all_fds (int first, int *except) */
|
|
Packit |
fc043f |
/* { */
|
|
Packit |
fc043f |
/* (void)first; */
|
|
Packit |
fc043f |
/* (void)except; */
|
|
Packit |
fc043f |
/* } */
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Returns an array with all currently open file descriptors. The end
|
|
Packit |
fc043f |
* of the array is marked by -1. The caller needs to release this
|
|
Packit |
fc043f |
* array using the *standard free* and not with xfree. This allow the
|
|
Packit |
fc043f |
* use of this function right at startup even before libgcrypt has
|
|
Packit |
fc043f |
* been initialized. Returns NULL on error and sets ERRNO
|
|
Packit |
fc043f |
* accordingly. Note that fstat prints a warning to DebugView for all
|
|
Packit |
fc043f |
* invalid fds which is a bit annoying. We actually do not need this
|
|
Packit |
fc043f |
* function in real code (close_all_fds is a dummy anyway) but we keep
|
|
Packit |
fc043f |
* it for use by t-exechelp.c. */
|
|
Packit |
fc043f |
#if 0
|
|
Packit |
fc043f |
int *
|
|
Packit |
fc043f |
get_all_open_fds (void)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
int *array;
|
|
Packit |
fc043f |
size_t narray;
|
|
Packit |
fc043f |
int fd, max_fd, idx;
|
|
Packit |
fc043f |
#ifndef HAVE_STAT
|
|
Packit |
fc043f |
array = calloc (1, sizeof *array);
|
|
Packit |
fc043f |
if (array)
|
|
Packit |
fc043f |
array[0] = -1;
|
|
Packit |
fc043f |
#else /*HAVE_STAT*/
|
|
Packit |
fc043f |
struct stat statbuf;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
max_fd = get_max_fds ();
|
|
Packit |
fc043f |
narray = 32; /* If you change this change also t-exechelp.c. */
|
|
Packit |
fc043f |
array = calloc (narray, sizeof *array);
|
|
Packit |
fc043f |
if (!array)
|
|
Packit |
fc043f |
return NULL;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Note: The list we return is ordered. */
|
|
Packit |
fc043f |
for (idx=0, fd=0; fd < max_fd; fd++)
|
|
Packit |
fc043f |
if (!(fstat (fd, &statbuf) == -1 && errno == EBADF))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (idx+1 >= narray)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
int *tmp;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
narray += (narray < 256)? 32:256;
|
|
Packit |
fc043f |
tmp = realloc (array, narray * sizeof *array);
|
|
Packit |
fc043f |
if (!tmp)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
free (array);
|
|
Packit |
fc043f |
return NULL;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
array = tmp;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
array[idx++] = fd;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
array[idx] = -1;
|
|
Packit |
fc043f |
#endif /*HAVE_STAT*/
|
|
Packit |
fc043f |
return array;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Helper function to build_w32_commandline. */
|
|
Packit |
fc043f |
static char *
|
|
Packit |
fc043f |
build_w32_commandline_copy (char *buffer, const char *string)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
char *p = buffer;
|
|
Packit |
fc043f |
const char *s;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (!*string) /* Empty string. */
|
|
Packit |
fc043f |
p = stpcpy (p, "\"\"");
|
|
Packit |
fc043f |
else if (strpbrk (string, " \t\n\v\f\""))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
/* Need to do some kind of quoting. */
|
|
Packit |
fc043f |
p = stpcpy (p, "\"");
|
|
Packit |
fc043f |
for (s=string; *s; s++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
*p++ = *s;
|
|
Packit |
fc043f |
if (*s == '\"')
|
|
Packit |
fc043f |
*p++ = *s;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
*p++ = '\"';
|
|
Packit |
fc043f |
*p = 0;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
p = stpcpy (p, string);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
return p;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Build a command line for use with W32's CreateProcess. On success
|
|
Packit |
fc043f |
* CMDLINE gets the address of a newly allocated string. */
|
|
Packit |
fc043f |
static gpg_err_code_t
|
|
Packit |
fc043f |
build_w32_commandline (const char *pgmname, const char * const *argv,
|
|
Packit |
fc043f |
char **cmdline)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
int i, n;
|
|
Packit |
fc043f |
const char *s;
|
|
Packit |
fc043f |
char *buf, *p;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
*cmdline = NULL;
|
|
Packit |
fc043f |
n = 0;
|
|
Packit |
fc043f |
s = pgmname;
|
|
Packit |
fc043f |
n += strlen (s) + 1 + 2; /* (1 space, 2 quoting */
|
|
Packit |
fc043f |
for (; *s; s++)
|
|
Packit |
fc043f |
if (*s == '\"')
|
|
Packit |
fc043f |
n++; /* Need to double inner quotes. */
|
|
Packit |
fc043f |
for (i=0; (s=argv[i]); i++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
n += strlen (s) + 1 + 2; /* (1 space, 2 quoting */
|
|
Packit |
fc043f |
for (; *s; s++)
|
|
Packit |
fc043f |
if (*s == '\"')
|
|
Packit |
fc043f |
n++; /* Need to double inner quotes. */
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
n++;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
buf = p = xtrymalloc (n);
|
|
Packit |
fc043f |
if (!buf)
|
|
Packit |
fc043f |
return _gpg_err_code_from_syserror ();
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
p = build_w32_commandline_copy (p, pgmname);
|
|
Packit |
fc043f |
for (i=0; argv[i]; i++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
*p++ = ' ';
|
|
Packit |
fc043f |
p = build_w32_commandline_copy (p, argv[i]);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
*cmdline= buf;
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#define INHERIT_READ 1
|
|
Packit |
fc043f |
#define INHERIT_WRITE 2
|
|
Packit |
fc043f |
#define INHERIT_BOTH (INHERIT_READ|INHERIT_WRITE)
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Create pipe. FLAGS indicates which ends are inheritable. */
|
|
Packit |
fc043f |
static int
|
|
Packit |
fc043f |
create_inheritable_pipe (HANDLE filedes[2], int flags)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
HANDLE r, w;
|
|
Packit |
fc043f |
SECURITY_ATTRIBUTES sec_attr;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
memset (&sec_attr, 0, sizeof sec_attr );
|
|
Packit |
fc043f |
sec_attr.nLength = sizeof sec_attr;
|
|
Packit |
fc043f |
sec_attr.bInheritHandle = TRUE;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
_gpgrt_pre_syscall ();
|
|
Packit |
fc043f |
if (!CreatePipe (&r, &w, &sec_attr, 0))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
_gpgrt_post_syscall ();
|
|
Packit |
fc043f |
return -1;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
_gpgrt_post_syscall ();
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if ((flags & INHERIT_READ) == 0)
|
|
Packit |
fc043f |
if (! SetHandleInformation (r, HANDLE_FLAG_INHERIT, 0))
|
|
Packit |
fc043f |
goto fail;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if ((flags & INHERIT_WRITE) == 0)
|
|
Packit |
fc043f |
if (! SetHandleInformation (w, HANDLE_FLAG_INHERIT, 0))
|
|
Packit |
fc043f |
goto fail;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
filedes[0] = r;
|
|
Packit |
fc043f |
filedes[1] = w;
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
fail:
|
|
Packit |
fc043f |
_gpgrt_log_error ("SetHandleInformation failed: ec=%d\n",
|
|
Packit |
fc043f |
(int)GetLastError ());
|
|
Packit |
fc043f |
CloseHandle (r);
|
|
Packit |
fc043f |
CloseHandle (w);
|
|
Packit |
fc043f |
return -1;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static HANDLE
|
|
Packit |
fc043f |
w32_open_null (int for_write)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
HANDLE hfile;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
hfile = CreateFileW (L"nul",
|
|
Packit |
fc043f |
for_write? GENERIC_WRITE : GENERIC_READ,
|
|
Packit |
fc043f |
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
Packit |
fc043f |
NULL, OPEN_EXISTING, 0, NULL);
|
|
Packit |
fc043f |
if (hfile == INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
_gpgrt_log_debug ("can't open 'nul': ec=%d\n", (int)GetLastError ());
|
|
Packit |
fc043f |
return hfile;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static gpg_err_code_t
|
|
Packit |
fc043f |
do_create_pipe_and_estream (int filedes[2],
|
|
Packit |
fc043f |
estream_t *r_fp, int direction, int nonblock)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
gpg_err_code_t err = 0;
|
|
Packit |
fc043f |
int flags;
|
|
Packit |
fc043f |
HANDLE fds[2];
|
|
Packit |
fc043f |
gpgrt_syshd_t syshd;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (direction < 0)
|
|
Packit |
fc043f |
flags = INHERIT_WRITE;
|
|
Packit |
fc043f |
else if (direction > 0)
|
|
Packit |
fc043f |
flags = INHERIT_READ;
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
flags = INHERIT_BOTH;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
filedes[0] = filedes[1] = -1;
|
|
Packit |
fc043f |
err = GPG_ERR_GENERAL;
|
|
Packit |
fc043f |
if (!create_inheritable_pipe (fds, flags))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
filedes[0] = _open_osfhandle (handle_to_fd (fds[0]), O_RDONLY);
|
|
Packit |
fc043f |
if (filedes[0] == -1)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
_gpgrt_log_error ("failed to translate osfhandle %p\n", fds[0]);
|
|
Packit |
fc043f |
CloseHandle (fds[1]);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
filedes[1] = _open_osfhandle (handle_to_fd (fds[1]), O_APPEND);
|
|
Packit |
fc043f |
if (filedes[1] == -1)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
_gpgrt_log_error ("failed to translate osfhandle %p\n", fds[1]);
|
|
Packit |
fc043f |
close (filedes[0]);
|
|
Packit |
fc043f |
filedes[0] = -1;
|
|
Packit |
fc043f |
CloseHandle (fds[1]);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
err = 0;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (! err && r_fp)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
syshd.type = ES_SYSHD_HANDLE;
|
|
Packit |
fc043f |
if (direction < 0)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
syshd.u.handle = fds[0];
|
|
Packit |
fc043f |
*r_fp = _gpgrt_sysopen (&syshd, nonblock? "r,nonblock" : "r");
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
syshd.u.handle = fds[1];
|
|
Packit |
fc043f |
*r_fp = _gpgrt_sysopen (&syshd, nonblock? "w,nonblock" : "w");
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
if (!*r_fp)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = _gpg_err_code_from_syserror ();
|
|
Packit |
fc043f |
_gpgrt_log_error (_("error creating a stream for a pipe: %s\n"),
|
|
Packit |
fc043f |
_gpg_strerror (err));
|
|
Packit |
fc043f |
close (filedes[0]);
|
|
Packit |
fc043f |
close (filedes[1]);
|
|
Packit |
fc043f |
filedes[0] = filedes[1] = -1;
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Create a pipe. The DIRECTION parameter gives the type of the created pipe:
|
|
Packit |
fc043f |
* DIRECTION < 0 := Inbound pipe: On Windows the write end is inheritable.
|
|
Packit |
fc043f |
* DIRECTION > 0 := Outbound pipe: On Windows the read end is inheritable.
|
|
Packit |
fc043f |
* If R_FP is NULL a standard pipe and no stream is created, DIRECTION
|
|
Packit |
fc043f |
* should then be 0. */
|
|
Packit |
fc043f |
gpg_err_code_t
|
|
Packit |
fc043f |
_gpgrt_make_pipe (int filedes[2], estream_t *r_fp, int direction, int nonblock)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (r_fp && direction)
|
|
Packit |
fc043f |
return do_create_pipe_and_estream (filedes, r_fp, direction, nonblock);
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
return do_create_pipe_and_estream (filedes, NULL, 0, 0);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Fork and exec the PGMNAME, see gpgrt-int.h for details. */
|
|
Packit |
fc043f |
gpg_err_code_t
|
|
Packit |
fc043f |
_gpgrt_spawn_process (const char *pgmname, const char *argv[],
|
|
Packit |
fc043f |
int *except, void (*preexec)(void), unsigned int flags,
|
|
Packit |
fc043f |
estream_t *r_infp, estream_t *r_outfp, estream_t *r_errfp,
|
|
Packit |
fc043f |
pid_t *pid)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
gpg_err_code_t err;
|
|
Packit |
fc043f |
SECURITY_ATTRIBUTES sec_attr;
|
|
Packit |
fc043f |
PROCESS_INFORMATION pi =
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
NULL, /* Returns process handle. */
|
|
Packit |
fc043f |
0, /* Returns primary thread handle. */
|
|
Packit |
fc043f |
0, /* Returns pid. */
|
|
Packit |
fc043f |
0 /* Returns tid. */
|
|
Packit |
fc043f |
};
|
|
Packit |
fc043f |
STARTUPINFO si;
|
|
Packit |
fc043f |
int cr_flags;
|
|
Packit |
fc043f |
char *cmdline;
|
|
Packit |
fc043f |
HANDLE inpipe[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
|
|
Packit |
fc043f |
HANDLE outpipe[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
|
|
Packit |
fc043f |
HANDLE errpipe[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
|
|
Packit |
fc043f |
estream_t infp = NULL;
|
|
Packit |
fc043f |
estream_t outfp = NULL;
|
|
Packit |
fc043f |
estream_t errfp = NULL;
|
|
Packit |
fc043f |
HANDLE nullhd[3] = {INVALID_HANDLE_VALUE,
|
|
Packit |
fc043f |
INVALID_HANDLE_VALUE,
|
|
Packit |
fc043f |
INVALID_HANDLE_VALUE};
|
|
Packit |
fc043f |
int i;
|
|
Packit |
fc043f |
es_syshd_t syshd;
|
|
Packit |
fc043f |
int nonblock = !!(flags & GPGRT_SPAWN_NONBLOCK);
|
|
Packit |
fc043f |
int ret;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
(void)except; /* Not yet used. */
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (r_infp)
|
|
Packit |
fc043f |
*r_infp = NULL;
|
|
Packit |
fc043f |
if (r_outfp)
|
|
Packit |
fc043f |
*r_outfp = NULL;
|
|
Packit |
fc043f |
if (r_errfp)
|
|
Packit |
fc043f |
*r_errfp = NULL;
|
|
Packit |
fc043f |
*pid = (pid_t)(-1); /* Always required. */
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (r_infp)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (create_inheritable_pipe (inpipe, INHERIT_READ))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = GPG_ERR_GENERAL;
|
|
Packit |
fc043f |
_gpgrt_log_error (_("error creating a pipe: %s\n"),
|
|
Packit |
fc043f |
_gpg_strerror (err));
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
syshd.type = ES_SYSHD_HANDLE;
|
|
Packit |
fc043f |
syshd.u.handle = inpipe[1];
|
|
Packit |
fc043f |
infp = _gpgrt_sysopen (&syshd, nonblock? "w,nonblock" : "w");
|
|
Packit |
fc043f |
if (!infp)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = _gpg_err_code_from_syserror ();
|
|
Packit |
fc043f |
_gpgrt_log_error (_("error creating a stream for a pipe: %s\n"),
|
|
Packit |
fc043f |
_gpg_strerror (err));
|
|
Packit |
fc043f |
CloseHandle (inpipe[0]);
|
|
Packit |
fc043f |
CloseHandle (inpipe[1]);
|
|
Packit |
fc043f |
inpipe[0] = inpipe[1] = INVALID_HANDLE_VALUE;
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (r_outfp)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (create_inheritable_pipe (outpipe, INHERIT_WRITE))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = GPG_ERR_GENERAL;
|
|
Packit |
fc043f |
_gpgrt_log_error (_("error creating a pipe: %s\n"),
|
|
Packit |
fc043f |
_gpg_strerror (err));
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
syshd.type = ES_SYSHD_HANDLE;
|
|
Packit |
fc043f |
syshd.u.handle = outpipe[0];
|
|
Packit |
fc043f |
outfp = _gpgrt_sysopen (&syshd, nonblock? "r,nonblock" : "r");
|
|
Packit |
fc043f |
if (!outfp)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = _gpg_err_code_from_syserror ();
|
|
Packit |
fc043f |
_gpgrt_log_error (_("error creating a stream for a pipe: %s\n"),
|
|
Packit |
fc043f |
_gpg_strerror (err));
|
|
Packit |
fc043f |
CloseHandle (outpipe[0]);
|
|
Packit |
fc043f |
CloseHandle (outpipe[1]);
|
|
Packit |
fc043f |
outpipe[0] = outpipe[1] = INVALID_HANDLE_VALUE;
|
|
Packit |
fc043f |
if (infp)
|
|
Packit |
fc043f |
_gpgrt_fclose (infp);
|
|
Packit |
fc043f |
else if (inpipe[1] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (inpipe[1]);
|
|
Packit |
fc043f |
if (inpipe[0] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (inpipe[0]);
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (r_errfp)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (create_inheritable_pipe (errpipe, INHERIT_WRITE))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = GPG_ERR_GENERAL;
|
|
Packit |
fc043f |
_gpgrt_log_error (_("error creating a pipe: %s\n"),
|
|
Packit |
fc043f |
_gpg_strerror (err));
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
syshd.type = ES_SYSHD_HANDLE;
|
|
Packit |
fc043f |
syshd.u.handle = errpipe[0];
|
|
Packit |
fc043f |
errfp = _gpgrt_sysopen (&syshd, nonblock? "r,nonblock" : "r");
|
|
Packit |
fc043f |
if (!errfp)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = _gpg_err_code_from_syserror ();
|
|
Packit |
fc043f |
_gpgrt_log_error (_("error creating a stream for a pipe: %s\n"),
|
|
Packit |
fc043f |
_gpg_strerror (err));
|
|
Packit |
fc043f |
CloseHandle (errpipe[0]);
|
|
Packit |
fc043f |
CloseHandle (errpipe[1]);
|
|
Packit |
fc043f |
errpipe[0] = errpipe[1] = INVALID_HANDLE_VALUE;
|
|
Packit |
fc043f |
if (outfp)
|
|
Packit |
fc043f |
_gpgrt_fclose (outfp);
|
|
Packit |
fc043f |
else if (outpipe[0] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (outpipe[0]);
|
|
Packit |
fc043f |
if (outpipe[1] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (outpipe[1]);
|
|
Packit |
fc043f |
if (infp)
|
|
Packit |
fc043f |
_gpgrt_fclose (infp);
|
|
Packit |
fc043f |
else if (inpipe[1] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (inpipe[1]);
|
|
Packit |
fc043f |
if (inpipe[0] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (inpipe[0]);
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Prepare security attributes. */
|
|
Packit |
fc043f |
memset (&sec_attr, 0, sizeof sec_attr );
|
|
Packit |
fc043f |
sec_attr.nLength = sizeof sec_attr;
|
|
Packit |
fc043f |
sec_attr.bInheritHandle = FALSE;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Build the command line. */
|
|
Packit |
fc043f |
err = build_w32_commandline (pgmname, argv, &cmdline);
|
|
Packit |
fc043f |
if (err)
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (inpipe[0] == INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
nullhd[0] = w32_open_null (0);
|
|
Packit |
fc043f |
if (outpipe[1] == INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
nullhd[1] = w32_open_null (1);
|
|
Packit |
fc043f |
if (errpipe[1] == INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
nullhd[2] = w32_open_null (1);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Start the process. Note that we can't run the PREEXEC function
|
|
Packit |
fc043f |
because this might change our own environment. */
|
|
Packit |
fc043f |
(void)preexec;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
memset (&si, 0, sizeof si);
|
|
Packit |
fc043f |
si.cb = sizeof (si);
|
|
Packit |
fc043f |
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
|
Packit |
fc043f |
si.wShowWindow = DEBUG_W32_SPAWN? SW_SHOW : SW_MINIMIZE;
|
|
Packit |
fc043f |
si.hStdInput = inpipe[0] == INVALID_HANDLE_VALUE? nullhd[0] : inpipe[0];
|
|
Packit |
fc043f |
si.hStdOutput = outpipe[1] == INVALID_HANDLE_VALUE? nullhd[1] : outpipe[1];
|
|
Packit |
fc043f |
si.hStdError = errpipe[1] == INVALID_HANDLE_VALUE? nullhd[2] : errpipe[1];
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
cr_flags = (CREATE_DEFAULT_ERROR_MODE
|
|
Packit |
fc043f |
| ((flags & GPGRT_SPAWN_DETACHED)? DETACHED_PROCESS : 0)
|
|
Packit |
fc043f |
| GetPriorityClass (GetCurrentProcess ())
|
|
Packit |
fc043f |
| CREATE_SUSPENDED);
|
|
Packit |
fc043f |
_gpgrt_log_debug ("CreateProcess, path='%s' cmdline='%s'\n",
|
|
Packit |
fc043f |
pgmname, cmdline);
|
|
Packit |
fc043f |
ret = CreateProcess (pgmname, /* Program to start. */
|
|
Packit |
fc043f |
cmdline, /* Command line arguments. */
|
|
Packit |
fc043f |
&sec_attr, /* Process security attributes. */
|
|
Packit |
fc043f |
&sec_attr, /* Thread security attributes. */
|
|
Packit |
fc043f |
TRUE, /* Inherit handles. */
|
|
Packit |
fc043f |
cr_flags, /* Creation flags. */
|
|
Packit |
fc043f |
NULL, /* Environment. */
|
|
Packit |
fc043f |
NULL, /* Use current drive/directory. */
|
|
Packit |
fc043f |
&si, /* Startup information. */
|
|
Packit |
fc043f |
&pi /* Returns process information. */
|
|
Packit |
fc043f |
);
|
|
Packit |
fc043f |
if (!ret)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
_gpgrt_log_error ("CreateProcess failed: ec=%d\n", (int)GetLastError ());
|
|
Packit |
fc043f |
xfree (cmdline);
|
|
Packit |
fc043f |
if (infp)
|
|
Packit |
fc043f |
_gpgrt_fclose (infp);
|
|
Packit |
fc043f |
else if (inpipe[1] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (outpipe[1]);
|
|
Packit |
fc043f |
if (inpipe[0] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (inpipe[0]);
|
|
Packit |
fc043f |
if (outfp)
|
|
Packit |
fc043f |
_gpgrt_fclose (outfp);
|
|
Packit |
fc043f |
else if (outpipe[0] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (outpipe[0]);
|
|
Packit |
fc043f |
if (outpipe[1] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (outpipe[1]);
|
|
Packit |
fc043f |
if (errfp)
|
|
Packit |
fc043f |
_gpgrt_fclose (errfp);
|
|
Packit |
fc043f |
else if (errpipe[0] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (errpipe[0]);
|
|
Packit |
fc043f |
if (errpipe[1] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (errpipe[1]);
|
|
Packit |
fc043f |
return GPG_ERR_GENERAL;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
xfree (cmdline);
|
|
Packit |
fc043f |
cmdline = NULL;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Close the inherited handles to /dev/null. */
|
|
Packit |
fc043f |
for (i=0; i < DIM (nullhd); i++)
|
|
Packit |
fc043f |
if (nullhd[i] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (nullhd[i]);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Close the inherited ends of the pipes. */
|
|
Packit |
fc043f |
if (inpipe[0] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (inpipe[0]);
|
|
Packit |
fc043f |
if (outpipe[1] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (outpipe[1]);
|
|
Packit |
fc043f |
if (errpipe[1] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (errpipe[1]);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
_gpgrt_log_debug ("CreateProcess ready: hProcess=%p hThread=%p"
|
|
Packit |
fc043f |
" dwProcessID=%d dwThreadId=%d\n",
|
|
Packit |
fc043f |
pi.hProcess, pi.hThread,
|
|
Packit |
fc043f |
(int) pi.dwProcessId, (int) pi.dwThreadId);
|
|
Packit |
fc043f |
_gpgrt_log_debug (" outfp=%p errfp=%p\n", outfp, errfp);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if ((flags & GPGRT_SPAWN_RUN_ASFW))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
/* Fixme: For unknown reasons AllowSetForegroundWindow returns
|
|
Packit |
fc043f |
* an invalid argument error if we pass it the correct
|
|
Packit |
fc043f |
* processID. As a workaround we use -1 (ASFW_ANY). */
|
|
Packit |
fc043f |
if (!AllowSetForegroundWindow (ASFW_ANY /*pi.dwProcessId*/))
|
|
Packit |
fc043f |
_gpgrt_log_info ("AllowSetForegroundWindow() failed: ec=%d\n",
|
|
Packit |
fc043f |
(int)GetLastError ());
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Process has been created suspended; resume it now. */
|
|
Packit |
fc043f |
_gpgrt_pre_syscall ();
|
|
Packit |
fc043f |
ResumeThread (pi.hThread);
|
|
Packit |
fc043f |
CloseHandle (pi.hThread);
|
|
Packit |
fc043f |
_gpgrt_post_syscall ();
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (r_infp)
|
|
Packit |
fc043f |
*r_infp = infp;
|
|
Packit |
fc043f |
if (r_outfp)
|
|
Packit |
fc043f |
*r_outfp = outfp;
|
|
Packit |
fc043f |
if (r_errfp)
|
|
Packit |
fc043f |
*r_errfp = errfp;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
*pid = handle_to_pid (pi.hProcess);
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Fork and exec the PGMNAME using FDs, see gpgrt-int.h for details. */
|
|
Packit |
fc043f |
gpg_err_code_t
|
|
Packit |
fc043f |
_gpgrt_spawn_process_fd (const char *pgmname, const char *argv[],
|
|
Packit |
fc043f |
int infd, int outfd, int errfd, pid_t *pid)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
gpg_err_code_t err;
|
|
Packit |
fc043f |
SECURITY_ATTRIBUTES sec_attr;
|
|
Packit |
fc043f |
PROCESS_INFORMATION pi = { NULL, 0, 0, 0 };
|
|
Packit |
fc043f |
STARTUPINFO si;
|
|
Packit |
fc043f |
char *cmdline;
|
|
Packit |
fc043f |
int ret, i;
|
|
Packit |
fc043f |
HANDLE stdhd[3];
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Setup return values. */
|
|
Packit |
fc043f |
*pid = (pid_t)(-1);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Prepare security attributes. */
|
|
Packit |
fc043f |
memset (&sec_attr, 0, sizeof sec_attr );
|
|
Packit |
fc043f |
sec_attr.nLength = sizeof sec_attr;
|
|
Packit |
fc043f |
sec_attr.bInheritHandle = FALSE;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Build the command line. */
|
|
Packit |
fc043f |
err = build_w32_commandline (pgmname, argv, &cmdline);
|
|
Packit |
fc043f |
if (err)
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
memset (&si, 0, sizeof si);
|
|
Packit |
fc043f |
si.cb = sizeof (si);
|
|
Packit |
fc043f |
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
|
Packit |
fc043f |
si.wShowWindow = DEBUG_W32_SPAWN? SW_SHOW : SW_MINIMIZE;
|
|
Packit |
fc043f |
stdhd[0] = infd == -1? w32_open_null (0) : INVALID_HANDLE_VALUE;
|
|
Packit |
fc043f |
stdhd[1] = outfd == -1? w32_open_null (1) : INVALID_HANDLE_VALUE;
|
|
Packit |
fc043f |
stdhd[2] = errfd == -1? w32_open_null (1) : INVALID_HANDLE_VALUE;
|
|
Packit |
fc043f |
si.hStdInput = infd == -1? stdhd[0] : (void*)_get_osfhandle (infd);
|
|
Packit |
fc043f |
si.hStdOutput = outfd == -1? stdhd[1] : (void*)_get_osfhandle (outfd);
|
|
Packit |
fc043f |
si.hStdError = errfd == -1? stdhd[2] : (void*)_get_osfhandle (errfd);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
_gpgrt_log_debug ("CreateProcess, path='%s' cmdline='%s'\n",
|
|
Packit |
fc043f |
pgmname, cmdline);
|
|
Packit |
fc043f |
ret = CreateProcess (pgmname, /* Program to start. */
|
|
Packit |
fc043f |
cmdline, /* Command line arguments. */
|
|
Packit |
fc043f |
&sec_attr, /* Process security attributes. */
|
|
Packit |
fc043f |
&sec_attr, /* Thread security attributes. */
|
|
Packit |
fc043f |
TRUE, /* Inherit handles. */
|
|
Packit |
fc043f |
(CREATE_DEFAULT_ERROR_MODE
|
|
Packit |
fc043f |
| GetPriorityClass (GetCurrentProcess ())
|
|
Packit |
fc043f |
| CREATE_SUSPENDED | DETACHED_PROCESS),
|
|
Packit |
fc043f |
NULL, /* Environment. */
|
|
Packit |
fc043f |
NULL, /* Use current drive/directory. */
|
|
Packit |
fc043f |
&si, /* Startup information. */
|
|
Packit |
fc043f |
&pi /* Returns process information. */
|
|
Packit |
fc043f |
);
|
|
Packit |
fc043f |
if (!ret)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
_gpgrt_log_error ("CreateProcess failed: ec=%d\n", (int)GetLastError ());
|
|
Packit |
fc043f |
err = GPG_ERR_GENERAL;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
err = 0;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
xfree (cmdline);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
for (i=0; i < 3; i++)
|
|
Packit |
fc043f |
if (stdhd[i] != INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
CloseHandle (stdhd[i]);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (err)
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
_gpgrt_log_debug ("CreateProcess ready: hProcess=%p hThread=%p"
|
|
Packit |
fc043f |
" dwProcessID=%d dwThreadId=%d\n",
|
|
Packit |
fc043f |
pi.hProcess, pi.hThread,
|
|
Packit |
fc043f |
(int) pi.dwProcessId, (int) pi.dwThreadId);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Process has been created suspended; resume it now. */
|
|
Packit |
fc043f |
ResumeThread (pi.hThread);
|
|
Packit |
fc043f |
CloseHandle (pi.hThread);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
*pid = handle_to_pid (pi.hProcess);
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* See gpgrt-int.h for a description. */
|
|
Packit |
fc043f |
gpg_err_code_t
|
|
Packit |
fc043f |
_gpgrt_wait_process (const char *pgmname, pid_t pid, int hang, int *r_exitcode)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
return _gpgrt_wait_processes (&pgmname, &pid, 1, hang, r_exitcode);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* See gpgrt-int.h for a description. */
|
|
Packit |
fc043f |
gpg_err_code_t
|
|
Packit |
fc043f |
_gpgrt_wait_processes (const char **pgmnames, pid_t *pids, size_t count,
|
|
Packit |
fc043f |
int hang, int *r_exitcodes)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
gpg_err_code_t ec = 0;
|
|
Packit |
fc043f |
size_t i;
|
|
Packit |
fc043f |
HANDLE *procs;
|
|
Packit |
fc043f |
int code;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
procs = xtrycalloc (count, sizeof *procs);
|
|
Packit |
fc043f |
if (procs == NULL)
|
|
Packit |
fc043f |
return _gpg_err_code_from_syserror ();
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
for (i = 0; i < count; i++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (r_exitcodes)
|
|
Packit |
fc043f |
r_exitcodes[i] = -1;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (pids[i] == (pid_t)(-1))
|
|
Packit |
fc043f |
return GPG_ERR_INV_VALUE;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
procs[i] = fd_to_handle (pids[i]);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
_gpgrt_pre_syscall ();
|
|
Packit |
fc043f |
code = WaitForMultipleObjects (count, procs, TRUE, hang? INFINITE : 0);
|
|
Packit |
fc043f |
_gpgrt_post_syscall ();
|
|
Packit |
fc043f |
switch (code)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
case WAIT_TIMEOUT:
|
|
Packit |
fc043f |
ec = GPG_ERR_TIMEOUT;
|
|
Packit |
fc043f |
goto leave;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
case WAIT_FAILED:
|
|
Packit |
fc043f |
_gpgrt_log_error (_("waiting for processes to terminate failed: ec=%d\n"),
|
|
Packit |
fc043f |
(int)GetLastError ());
|
|
Packit |
fc043f |
ec = GPG_ERR_GENERAL;
|
|
Packit |
fc043f |
goto leave;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
case WAIT_OBJECT_0:
|
|
Packit |
fc043f |
for (i = 0; i < count; i++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
DWORD exc;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (! GetExitCodeProcess (procs[i], &exc))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
_gpgrt_log_error (_("error getting exit code of process %d:"
|
|
Packit |
fc043f |
" ec=%d\n"),
|
|
Packit |
fc043f |
(int) pids[i], (int)GetLastError ());
|
|
Packit |
fc043f |
ec = GPG_ERR_GENERAL;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else if (exc)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (!r_exitcodes)
|
|
Packit |
fc043f |
_gpgrt_log_error (_("error running '%s': exit status %d\n"),
|
|
Packit |
fc043f |
pgmnames[i], (int)exc);
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
r_exitcodes[i] = (int)exc;
|
|
Packit |
fc043f |
ec = GPG_ERR_GENERAL;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (r_exitcodes)
|
|
Packit |
fc043f |
r_exitcodes[i] = 0;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
break;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
default:
|
|
Packit |
fc043f |
_gpgrt_log_debug ("WaitForMultipleObjects returned unexpected code %d\n",
|
|
Packit |
fc043f |
code);
|
|
Packit |
fc043f |
ec = GPG_ERR_GENERAL;
|
|
Packit |
fc043f |
break;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
leave:
|
|
Packit |
fc043f |
return ec;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* See gpgrt-int.h for a description. */
|
|
Packit |
fc043f |
gpg_err_code_t
|
|
Packit |
fc043f |
_gpgrt_spawn_process_detached (const char *pgmname, const char *argv[],
|
|
Packit |
fc043f |
const char *envp[] )
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
gpg_err_code_t err;
|
|
Packit |
fc043f |
SECURITY_ATTRIBUTES sec_attr;
|
|
Packit |
fc043f |
PROCESS_INFORMATION pi =
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
NULL, /* Returns process handle. */
|
|
Packit |
fc043f |
0, /* Returns primary thread handle. */
|
|
Packit |
fc043f |
0, /* Returns pid. */
|
|
Packit |
fc043f |
0 /* Returns tid. */
|
|
Packit |
fc043f |
};
|
|
Packit |
fc043f |
STARTUPINFO si;
|
|
Packit |
fc043f |
int cr_flags;
|
|
Packit |
fc043f |
char *cmdline;
|
|
Packit |
fc043f |
int ret;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* We don't use ENVP. */
|
|
Packit |
fc043f |
(void)envp;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (access (pgmname, X_OK))
|
|
Packit |
fc043f |
return _gpg_err_code_from_syserror ();
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Prepare security attributes. */
|
|
Packit |
fc043f |
memset (&sec_attr, 0, sizeof sec_attr );
|
|
Packit |
fc043f |
sec_attr.nLength = sizeof sec_attr;
|
|
Packit |
fc043f |
sec_attr.bInheritHandle = FALSE;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Build the command line. */
|
|
Packit |
fc043f |
err = build_w32_commandline (pgmname, argv, &cmdline);
|
|
Packit |
fc043f |
if (err)
|
|
Packit |
fc043f |
return err;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Start the process. */
|
|
Packit |
fc043f |
memset (&si, 0, sizeof si);
|
|
Packit |
fc043f |
si.cb = sizeof (si);
|
|
Packit |
fc043f |
si.dwFlags = STARTF_USESHOWWINDOW;
|
|
Packit |
fc043f |
si.wShowWindow = DEBUG_W32_SPAWN? SW_SHOW : SW_MINIMIZE;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
cr_flags = (CREATE_DEFAULT_ERROR_MODE
|
|
Packit |
fc043f |
| GetPriorityClass (GetCurrentProcess ())
|
|
Packit |
fc043f |
| CREATE_NEW_PROCESS_GROUP
|
|
Packit |
fc043f |
| DETACHED_PROCESS);
|
|
Packit |
fc043f |
_gpgrt_log_debug ("CreateProcess(detached), path='%s' cmdline='%s'\n",
|
|
Packit |
fc043f |
pgmname, cmdline);
|
|
Packit |
fc043f |
ret = CreateProcess (pgmname, /* Program to start. */
|
|
Packit |
fc043f |
cmdline, /* Command line arguments. */
|
|
Packit |
fc043f |
&sec_attr, /* Process security attributes. */
|
|
Packit |
fc043f |
&sec_attr, /* Thread security attributes. */
|
|
Packit |
fc043f |
FALSE, /* Inherit handles. */
|
|
Packit |
fc043f |
cr_flags, /* Creation flags. */
|
|
Packit |
fc043f |
NULL, /* Environment. */
|
|
Packit |
fc043f |
NULL, /* Use current drive/directory. */
|
|
Packit |
fc043f |
&si, /* Startup information. */
|
|
Packit |
fc043f |
&pi /* Returns process information. */
|
|
Packit |
fc043f |
);
|
|
Packit |
fc043f |
if (!ret)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
_gpgrt_log_error ("CreateProcess(detached) failed: ec=%d\n",
|
|
Packit |
fc043f |
(int)GetLastError ());
|
|
Packit |
fc043f |
xfree (cmdline);
|
|
Packit |
fc043f |
return GPG_ERR_GENERAL;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
xfree (cmdline);
|
|
Packit |
fc043f |
cmdline = NULL;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
_gpgrt_log_debug ("CreateProcess(detached) ready: hProcess=%p hThread=%p"
|
|
Packit |
fc043f |
" dwProcessID=%d dwThreadId=%d\n",
|
|
Packit |
fc043f |
pi.hProcess, pi.hThread,
|
|
Packit |
fc043f |
(int) pi.dwProcessId, (int) pi.dwThreadId);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
CloseHandle (pi.hThread);
|
|
Packit |
fc043f |
CloseHandle (pi.hProcess);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Kill a process; that is send an appropriate signal to the process.
|
|
Packit |
fc043f |
gnupg_wait_process must be called to actually remove the process
|
|
Packit |
fc043f |
from the system. An invalid PID is ignored. */
|
|
Packit |
fc043f |
void
|
|
Packit |
fc043f |
_gpgrt_kill_process (pid_t pid)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (pid != (pid_t) INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
HANDLE process = (HANDLE) pid;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Arbitrary error code. */
|
|
Packit |
fc043f |
_gpgrt_pre_syscall ();
|
|
Packit |
fc043f |
TerminateProcess (process, 1);
|
|
Packit |
fc043f |
_gpgrt_post_syscall ();
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
void
|
|
Packit |
fc043f |
_gpgrt_release_process (pid_t pid)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (pid != (pid_t)INVALID_HANDLE_VALUE)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
HANDLE process = (HANDLE)pid;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
CloseHandle (process);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|