Commit 69967b04 authored by Werner Koch's avatar Werner Koch

A whole bunch of changes to allow building for W32.

parent 53ae21e7
2004-12-15 Werner Koch <wk@g10code.com>
* Makefile.am (SUBDIRS) [W32]: Do not build in tests/.
* acinclude.m4: Add proper macro name quoting for use with
automake 1.9.
* configure.ac: Add replacement check for ttyname.
Removed support for a included zlib.
2004-12-06 Werner Koch <wk@g10code.com>
* configure.ac (have_w32_system): New. Disable Pth checks for W32.
......
......@@ -53,8 +53,14 @@ else
scd =
endif
if HAVE_W32_SYSTEM
tests =
else
tests = tests
endif
SUBDIRS = m4 intl jnlib common ${kbx} \
${gpg} ${sm} ${agent} ${scd} tools po doc tests
${gpg} ${sm} ${agent} ${scd} tools po doc ${tests}
dist-hook:
@set -e; \
......
......@@ -20,7 +20,7 @@ dnl Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
dnl GNUPG_CHECK_TYPEDEF(TYPE, HAVE_NAME)
dnl Check whether a typedef exists and create a #define $2 if it exists
dnl
AC_DEFUN(GNUPG_CHECK_TYPEDEF,
AC_DEFUN([GNUPG_CHECK_TYPEDEF],
[ AC_MSG_CHECKING(for $1 typedef)
AC_CACHE_VAL(gnupg_cv_typedef_$1,
[AC_TRY_COMPILE([#define _GNU_SOURCE 1
......@@ -38,7 +38,7 @@ AC_DEFUN(GNUPG_CHECK_TYPEDEF,
dnl GNUPG_CHECK_GNUMAKE
dnl
AC_DEFUN(GNUPG_CHECK_GNUMAKE,
AC_DEFUN([GNUPG_CHECK_GNUMAKE],
[
if ${MAKE-make} --version 2>/dev/null | grep '^GNU ' >/dev/null 2>&1; then
:
......@@ -55,7 +55,7 @@ AC_DEFUN(GNUPG_CHECK_GNUMAKE,
dnl GNUPG_CHECK_FAQPROG
dnl
AC_DEFUN(GNUPG_CHECK_FAQPROG,
AC_DEFUN([GNUPG_CHECK_FAQPROG],
[ AC_MSG_CHECKING(for faqprog.pl)
if faqprog.pl -V 2>/dev/null | grep '^faqprog.pl ' >/dev/null 2>&1; then
working_faqprog=yes
......@@ -82,7 +82,7 @@ dnl fi
dnl GNUPG_CHECK_DOCBOOK_TO_TEXI
dnl
AC_DEFUN(GNUPG_CHECK_DOCBOOK_TO_TEXI,
AC_DEFUN([GNUPG_CHECK_DOCBOOK_TO_TEXI],
[
AC_CHECK_PROG(DOCBOOK_TO_TEXI, docbook2texi, yes, no)
AC_MSG_CHECKING(for sgml to texi tools)
......@@ -101,7 +101,7 @@ AC_DEFUN(GNUPG_CHECK_DOCBOOK_TO_TEXI,
dnl GNUPG_CHECK_ENDIAN
dnl define either LITTLE_ENDIAN_HOST or BIG_ENDIAN_HOST
dnl
define(GNUPG_CHECK_ENDIAN,
AC_DEFUN([GNUPG_CHECK_ENDIAN],
[
tmp_assumed_endian=big
if test "$cross_compiling" = yes; then
......@@ -158,7 +158,7 @@ define(GNUPG_CHECK_ENDIAN,
# Check for the getsockopt SO_PEERCRED
AC_DEFUN(GNUPG_SYS_SO_PEERCRED,
AC_DEFUN([GNUPG_SYS_SO_PEERCRED],
[ AC_MSG_CHECKING(for SO_PEERCRED)
AC_CACHE_VAL(gnupg_cv_sys_so_peercred,
[AC_TRY_COMPILE([#include <sys/socket.h>],
......@@ -183,7 +183,7 @@ AC_DEFUN(GNUPG_SYS_SO_PEERCRED,
# either be "yes" or "no" and decided on the default value for
# build_NAME and whether --enable-NAME or --disable-NAME is shown with
# ./configure --help
AC_DEFUN(GNUPG_BUILD_PROGRAM,
AC_DEFUN([GNUPG_BUILD_PROGRAM],
[build_$1=$2
m4_if([$2],[yes],[
AC_ARG_ENABLE([$1], AC_HELP_STRING([--disable-$1],
......@@ -210,7 +210,7 @@ AC_DEFUN(GNUPG_BUILD_PROGRAM,
# If the version is sufficient, HAVE_PTH will be set to yes.
#
# Taken form the m4 macros which come with Pth
AC_DEFUN(GNUPG_PTH_VERSION_CHECK,
AC_DEFUN([GNUPG_PTH_VERSION_CHECK],
[
_pth_version=`$PTH_CONFIG --version | awk 'NR==1 {print [$]3}'`
_req_version="ifelse([$1],,1.2.0,$1)"
......@@ -253,7 +253,7 @@ AC_DEFUN(GNUPG_PTH_VERSION_CHECK,
# mlock is there a macro using memlk()
dnl GNUPG_CHECK_MLOCK
dnl
define(GNUPG_CHECK_MLOCK,
AC_DEFUN([GNUPG_CHECK_MLOCK],
[ AC_CHECK_FUNCS(mlock)
if test "$ac_cv_func_mlock" = "no"; then
AC_CHECK_HEADERS(sys/mman.h)
......@@ -343,7 +343,7 @@ define(GNUPG_CHECK_MLOCK,
dnl Stolen from gcc
dnl Define MKDIR_TAKES_ONE_ARG if mkdir accepts only one argument instead
dnl of the usual 2.
AC_DEFUN(GNUPG_FUNC_MKDIR_TAKES_ONE_ARG,
AC_DEFUN([GNUPG_FUNC_MKDIR_TAKES_ONE_ARG],
[AC_CHECK_HEADERS(sys/stat.h unistd.h direct.h)
AC_CACHE_CHECK([if mkdir takes one argument], gnupg_cv_mkdir_takes_one_arg,
[AC_TRY_COMPILE([
......@@ -371,7 +371,7 @@ dnl AM_PATH_OPENSC([MINIMUM-VERSION,
dnl [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND ]]])
dnl Test for OpenSC and define OPENSC_CFLAGS and OPENSC_LIBS
dnl
AC_DEFUN(AM_PATH_OPENSC,
AC_DEFUN([AM_PATH_OPENSC],
[ AC_ARG_WITH(opensc-prefix,
AC_HELP_STRING([--with-opensc-prefix=PFX],
[prefix where OpenSC is installed (optional)]),
......
2004-12-15 Werner Koch <wk@g10code.com>
* gpg-agent.c [W32]: Various hacks to make it work.
* findkey.c (agent_write_private_key) [W32]: Adjust open call.
* call-scd.c (start_scd) [W32]: Don't check whether the daemon
didn't died. To hard to do under Windows.
(start_scd) [W32]: Disable sending of the event signal option.
* protect-tool.c (read_file, export_p12_file) [W32]: Use setmode
to get stdout and stin into binary mode.
2004-12-05 Moritz Schulte <moritz@g10code.com>
* query.c (start_pinentry): Allow CTRL be NULL.
......
......@@ -215,7 +215,7 @@ start_scd (ctrl_t ctrl)
/* We better do a sanity check now to see whether it has
accidently died. */
#ifndef HAVE_W32_SYSTEM /* fixme */
#ifndef HAVE_W32_SYSTEM
pid = assuan_get_pid (scd_ctx);
if (pid != (pid_t)(-1) && pid
&& ((rc=waitpid (pid, NULL, WNOHANG))==-1 || (rc == pid)) )
......
......@@ -33,7 +33,7 @@
#ifndef HAVE_W32_SYSTEM
#include <sys/socket.h>
#include <sys/un.h>
#endif
#endif /*HAVE_W32_SYSTEM*/
#include <unistd.h>
#include <signal.h>
#ifdef USE_GNU_PTH
......@@ -438,17 +438,18 @@ main (int argc, char **argv )
/* Libgcrypt requires us to register the threading model first.
Note that this will also do the pth_init. */
#if defined(USE_GNU_PTH) && !defined(HAVE_W32_SYSTEM)
#ifdef USE_GNU_PTH
# ifdef HAVE_W32_SYSTEM
pth_init ();
# else /*!HAVE_W32_SYSTEM*/
err = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pth);
if (err)
{
log_fatal ("can't register GNU Pth with Libgcrypt: %s\n",
gpg_strerror (err));
}
#endif /*USE_GNU_PTH && !HAVE_W32_SYSTEM*/
#ifdef HAVE_W32_SYSTEM
pth_init ();
#endif
# endif/*!HAVE_W32_SYSTEM*/
#endif /*USE_GNU_PTH*/
/* Check that the libraries are suitable. Do it here because
the option parsing may need services of the library. */
......@@ -716,12 +717,11 @@ main (int argc, char **argv )
}
/* Make sure that we have a default ttyname. */
#ifndef HAVE_W32_SYSTEM
if (!default_ttyname && ttyname (1))
default_ttyname = xstrdup (ttyname (1));
if (!default_ttytype && getenv ("TERM"))
default_ttytype = xstrdup (getenv ("TERM"));
#endif
if (pipe_server)
{ /* this is the simple pipe based server */
......
......@@ -35,6 +35,9 @@
#ifdef HAVE_LANGINFO_CODESET
#include <langinfo.h>
#endif
#ifdef HAVE_DOSISH_SYSTEM
#include <fcntl.h> /* for setmode() */
#endif
#define JNLIB_NEED_LOG_LOGV
#include "agent.h"
......@@ -262,6 +265,9 @@ read_file (const char *fname, size_t *r_length)
size_t nread, bufsize = 0;
fp = stdin;
#ifdef HAVE_DOSISH_SYSTEM
setmode ( fileno(fp) , O_BINARY );
#endif
buf = NULL;
buflen = 0;
#define NCHUNK 8192
......@@ -975,6 +981,9 @@ export_p12_file (const char *fname)
if (!key)
return;
#ifdef HAVE_DOSISH_SYSTEM
setmode ( fileno (stdout) , O_BINARY );
#endif
fwrite (key, keylen, 1, stdout);
xfree (key);
}
......@@ -1056,12 +1065,12 @@ main (int argc, char **argv )
gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
#ifdef __MINGW32__
#ifdef HAVE_W32_SYSTEM
opt_homedir = read_w32_registry_string ( NULL,
"Software\\GNU\\GnuPG", "HomeDir" );
#else
#else /*!HAVE_W32_SYSTEM*/
opt_homedir = getenv ("GNUPGHOME");
#endif
#endif /*!HAVE_W32_SYSTEM*/
if (!opt_homedir || !*opt_homedir)
opt_homedir = GNUPG_DEFAULT_HOMEDIR;
......@@ -1213,9 +1222,10 @@ get_passphrase (int promptno)
if (!pw)
{
if (err)
log_error ("error while asking for the passphrase\n");
log_error (_("error while asking for the passphrase: %s\n"),
gpg_strerror (err));
else
log_info ("cancelled\n");
log_info (_("cancelled\n"));
agent_exit (0);
}
......
2004-12-15 Werner Koch <wk@g10code.com>
* sysutils.h [W32]: Prototypes for registry functions.
* w32reg.c: Include sysutils.h
* simple-pwquery.c [W32]: Dummy code to allow a build.
* exechelp.c [W32]: Implemented for W32 .
* ttyname.c: New.
* asshelp.c (send_one_option): New.
(send_pinentry_environment): Cleaned up and made sure that empty
values are not send.
2004-12-07 Werner Koch <wk@g10code.com>
* asshelp.c (send_pinentry_environment) [W32]: Do not use ttyname.
......
......@@ -32,8 +32,32 @@
#include "asshelp.h"
/* Send the assuan command pertaining to the pinenry environment. The
OPT_* arguments are optional and may be used to overide the
static gpg_error_t
send_one_option (assuan_context_t ctx, const char *name, const char *value)
{
gpg_error_t err;
char *optstr;
if (!value || !*value)
err = 0; /* Avoid sending empty strings. */
else if (asprintf (&optstr, "OPTION %s=%s", name, value ) < 0)
err = gpg_error_from_errno (errno);
else
{
assuan_error_t ae;
ae = assuan_transact (ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL);
err = ae? map_assuan_err (ae) : 0;
free (optstr);
}
return err;
}
/* Send the assuan commands pertaining to the pinenry environment. The
OPT_* arguments are optional and may be used to override the
defaults taken from the current locale. */
gpg_error_t
send_pinentry_environment (assuan_context_t ctx,
......@@ -43,62 +67,49 @@ send_pinentry_environment (assuan_context_t ctx,
const char *opt_lc_ctype,
const char *opt_lc_messages)
{
int rc = 0;
gpg_error_t err = 0;
char *dft_display = NULL;
char *dft_ttyname = NULL;
char *dft_ttytype = NULL;
char *old_lc = NULL;
char *dft_lc = NULL;
/* Send the DISPLAY variable. */
dft_display = getenv ("DISPLAY");
if (opt_display || dft_display)
{
char *optstr;
if (asprintf (&optstr, "OPTION display=%s",
opt_display ? opt_display : dft_display) < 0)
return gpg_error_from_errno (errno);
rc = assuan_transact (ctx, optstr, NULL, NULL, NULL, NULL, NULL,
NULL);
free (optstr);
if (rc)
return map_assuan_err (rc);
err = send_one_option (ctx, "display",
opt_display ? opt_display : dft_display);
if (err)
return err;
}
/* Send the name of the TTY. */
if (!opt_ttyname)
{
dft_ttyname = getenv ("GPG_TTY");
#ifdef HAVE_DOSISH_SYSTEM
if (!dft_ttyname || !*dft_ttyname )
dft_ttyname = "/dev/tty"; /* Use a fake. */
#else
if ((!dft_ttyname || !*dft_ttyname) && ttyname (0))
dft_ttyname = ttyname (0);
#endif
}
if (opt_ttyname || dft_ttyname)
{
char *optstr;
if (asprintf (&optstr, "OPTION ttyname=%s",
opt_ttyname ? opt_ttyname : dft_ttyname) < 0)
return gpg_error_from_errno (errno);
rc = assuan_transact (ctx, optstr, NULL, NULL, NULL, NULL, NULL,
NULL);
free (optstr);
if (rc)
return map_assuan_err (rc);
err = send_one_option (ctx, "ttyname",
opt_ttyname ? opt_ttyname : dft_ttyname);
if (err)
return err;
}
/* Send the type of the TTY. */
dft_ttytype = getenv ("TERM");
if (opt_ttytype || (dft_ttyname && dft_ttytype))
{
char *optstr;
if (asprintf (&optstr, "OPTION ttytype=%s",
opt_ttyname ? opt_ttytype : dft_ttytype) < 0)
return gpg_error_from_errno (errno);
rc = assuan_transact (ctx, optstr, NULL, NULL, NULL, NULL, NULL,
NULL);
free (optstr);
if (rc)
return map_assuan_err (rc);
err = send_one_option (ctx, "ttytype",
opt_ttyname ? opt_ttytype : dft_ttytype);
if (err)
return err;
}
/* Send the value for LC_CTYPE. */
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
old_lc = setlocale (LC_CTYPE, NULL);
if (old_lc)
......@@ -111,18 +122,8 @@ send_pinentry_environment (assuan_context_t ctx,
#endif
if (opt_lc_ctype || (dft_ttyname && dft_lc))
{
char *optstr;
if (asprintf (&optstr, "OPTION lc-ctype=%s",
opt_lc_ctype ? opt_lc_ctype : dft_lc) < 0)
rc = gpg_error_from_errno (errno);
else
{
rc = assuan_transact (ctx, optstr, NULL, NULL, NULL, NULL, NULL,
NULL);
free (optstr);
if (rc)
rc = map_assuan_err (rc);
}
err = send_one_option (ctx, "lc-ctype",
opt_lc_ctype ? opt_lc_ctype : dft_lc);
}
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
if (old_lc)
......@@ -131,8 +132,10 @@ send_pinentry_environment (assuan_context_t ctx,
free (old_lc);
}
#endif
if (rc)
return rc;
if (err)
return err;
/* Send the value for LC_MESSAGES. */
#if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
old_lc = setlocale (LC_MESSAGES, NULL);
if (old_lc)
......@@ -145,18 +148,8 @@ send_pinentry_environment (assuan_context_t ctx,
#endif
if (opt_lc_messages || (dft_ttyname && dft_lc))
{
char *optstr;
if (asprintf (&optstr, "OPTION lc-messages=%s",
opt_lc_messages ? opt_lc_messages : dft_lc) < 0)
rc = gpg_error_from_errno (errno);
else
{
rc = assuan_transact (ctx, optstr, NULL, NULL, NULL, NULL, NULL,
NULL);
free (optstr);
if (rc)
rc = map_assuan_err (rc);
}
err = send_one_option (ctx, "display",
opt_lc_messages ? opt_lc_messages : dft_lc);
}
#if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
if (old_lc)
......@@ -165,7 +158,9 @@ send_pinentry_environment (assuan_context_t ctx,
free (old_lc);
}
#endif
if (err)
return err;
return rc;
return 0;
}
......@@ -30,8 +30,7 @@
#ifdef USE_GNU_PTH
#include <pth.h>
#endif
#ifdef _WIN32
#else
#ifndef HAVE_W32_SYSTEM
#include <sys/wait.h>
#endif
......@@ -39,6 +38,9 @@
#include "i18n.h"
#include "exechelp.h"
/* Define to 1 do enable debugging. */
#define DEBUG_W32_SPAWN 1
#ifdef _POSIX_OPEN_MAX
#define MAX_OPEN_FDS _POSIX_OPEN_MAX
......@@ -57,6 +59,105 @@
#endif
#ifdef HAVE_W32_SYSTEM
/* 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))
#endif
#ifdef HAVE_W32_SYSTEM
/* 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
build_w32_commandline (const char *pgmname, const char **argv, char **cmdline)
{
int i, n;
const char *s;
char *buf, *p;
*cmdline = NULL;
n = strlen (pgmname);
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)
return gpg_error_from_errno (errno);
/* fixme: PGMNAME may not contain spaces etc. */
p = stpcpy (p, pgmname);
for (i=0; argv[i]; i++)
{
if (!*argv[i]) /* Empty string. */
p = stpcpy (p, " \"\"");
else if (strpbrk (argv[i], " \t\n\v\f\""))
{
p = stpcpy (p, " \"");
for (s=argv[i]; *s; s++)
{
*p++ = *s;
if (*s == '\"')
*p++ = *s;
}
*p++ = '\"';
*p = 0;
}
else
p = stpcpy (stpcpy (p, " "), argv[i]);
}
*cmdline= buf;
return 0;
}
#endif /*HAVE_W32_SYSTEM*/
#ifdef HAVE_W32_SYSTEM
/* Create pipe where the write end is inheritable. */
static int
create_inheritable_pipe (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(), w,
GetCurrentProcess(), &h, 0,
TRUE, DUPLICATE_SAME_ACCESS ))
{
log_error ("DuplicateHandle failed: %s\n", w32_strerror (-1));
CloseHandle (r);
CloseHandle (w);
return -1;
}
CloseHandle (w);
w = h;
filedes[0] = handle_to_fd (r);
filedes[1] = handle_to_fd (w);
return 0;
}
#endif /*HAVE_W32_SYSTEM*/
/* Fork and exec the PGMNAME, connect the file descriptor of INFILE to
stdin, write the output to OUTFILE, return a new stream in
......@@ -73,10 +174,121 @@ gnupg_spawn_process (const char *pgmname, const char *argv[],
void (*preexec)(void),
FILE **statusfile, pid_t *pid)
{
#ifdef _WIN32
return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
#ifdef HAVE_W32_SYSTEM
gpg_error_t err;
SECURITY_ATTRIBUTES sec_attr;
PROCESS_INFORMATION pi =
{
NULL, /* Returns process handle. */
0, /* Returns primary thread handle. */
0, /* Returns pid. */
0 /* Returns tid. */
};
STARTUPINFO si;
int cr_flags;
char *cmdline;
int fd, fdout, rp[2];
#else /* !_WIN32 */
/* Setup return values. */
*statusfile = NULL;
*pid = (pid_t)(-1);
fflush (infile);
rewind (infile);
fd = _get_osfhandle (fileno (infile));
fdout = _get_osfhandle (fileno (outfile));
if (fd == -1 || fdout == -1)
log_fatal ("no file descriptor for file passed to gnupg_spawn_process\n");
/* Prepare security attributes. */
memset (&sec_attr, 0, sizeof sec_attr );
sec_attr.nLength = sizeof sec_attr;
sec_attr.bInheritHandle = FALSE;
/* Build the command line. */
err = build_w32_commandline (pgmname, argv, &cmdline);
if (err)
return err;
/* Create a pipe. */
if (create_inheritable_pipe (rp))
{
err = gpg_error (GPG_ERR_GENERAL);
log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
xfree (cmdline);
return err;
}
/* Start the process. Note that we can't run the PREEXEC function
because this would change our own environment. */
memset (&si, 0, sizeof si);
si.cb = sizeof (si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = DEBUG_W32_SPAWN? SW_SHOW : SW_MINIMIZE;
si.hStdInput = fd_to_handle (fd);
si.hStdOutput = fd_to_handle (fdout);
si.hStdError = fd_to_handle (rp[1]);
cr_flags = (CREATE_DEFAULT_ERROR_MODE
| GetPriorityClass (GetCurrentProcess ())
| CREATE_SUSPENDED);
log_debug ("CreateProcess, path=`%s' cmdline=`%s'", pgmname, cmdline);
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);
CloseHandle (fd_to_handle (rp[0]));
CloseHandle (fd_to_handle (rp[1]));
return gpg_error (GPG_ERR_GENERAL);
}
xfree (cmdline);
cmdline = NULL;
/* Close the other end of the pipe. */
CloseHandle (fd_to_handle (rp[1]));
log_debug ("CreateProcess ready: hProcess=%p hThread=%p"
" dwProcessID=%d dwThreadId=%d\n",
pi.hProcess, pi.hThread,
(int) pi.dwProcessId, (int) pi.dwThreadId);
/* Process ha been created suspended; resume it now. */
ResumeThread (pi.hThread);
CloseHandle (pi.hThread);
{
int x;
x = _open_osfhandle (rp[0], 0);
if (x == -1)
log_error ("failed to translate osfhandle %p\n", (void*)rp[0] );
else
{
log_debug ("_open_osfhandle %p yields %d\n", (void*)fd, x );
*statusfile = fdopen (x, "r");
}
}
if (!*statusfile)
{
err = gpg_error_from_errno (errno);
log_error (_("can't fdopen pipe for reading: %s\n"), gpg_strerror (err));
CloseHandle (pi.hProcess);
return err;
}
*pid = handle_to_pid (pi.hProcess);
return 0;
#else /* !HAVE_W32_SYSTEM */
gpg_error_t err;
int fd, fdout, rp[2];
......@@ -87,8 +299,7 @@ gnupg_spawn_process (const char *pgmname, const char *argv[],
fd = fileno (infile);
fdout = fileno (outfile);
if (fd == -1 || fdout == -1)
log_fatal ("no file descriptor for file passed"
" to gnupg_spawn_process: %s\n", strerror (errno) );
log_fatal ("no file descriptor for file passed to gnupg_spawn_process\n");
if (pipe (rp) == -1)
{
......@@ -170,7 +381,7 @@ gnupg_spawn_process (const char *pgmname, const char *argv[],
}
return 0;
#endif /* !_WIN32 */
#endif /* !HAVE_W32_SYSTEM */
}
......@@ -183,10 +394,51 @@ gnupg_wait_process (const char *pgmname, pid_t pid)
{
gpg_err_code_t ec;
#ifdef _WIN32
ec = GPG_ERR_NOT_IMPLEMENTED;
#ifdef HAVE_W32_SYSTEM
HANDLE proc = fd_to_handle (pid);
int code;
DWORD exc;
if (pid == (pid_t)(-1))
return gpg_error (GPG_ERR_INV_VALUE);
/* 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. */
code = WaitForSingleObject (proc, INFINITE);
switch (code)
{
case WAIT_FAILED:
log_error (_("waiting for process %d to terminate failed: %s\n"),
(int)pid, w32_strerror (-1));
ec = GPG_ERR_GENERAL;
break;
case WAIT_OBJECT_0:
if (!GetExitCodeProcess (proc, &exc))
{
log_error (_("error getting exit code of process %d: %s\n"),
(int)pid, w32_strerror (-1) );
ec = GPG_ERR_GENERAL;
}