More chnages to use estream. Add a way to replace the standard

descriptors.
This commit is contained in:
Werner Koch 2010-03-22 12:46:05 +00:00
parent 0f4eefb094
commit e64038608b
33 changed files with 241 additions and 78 deletions

View File

@ -589,7 +589,7 @@ main (int argc, char **argv )
/* Make sure that our subsystems are ready. */
i18n_init ();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
/* Libgcrypt requires us to register the threading model first.
@ -1385,7 +1385,7 @@ get_agent_scd_notify_event (void)
HANDLE h, h2;
SECURITY_ATTRIBUTES sa = { sizeof (SECURITY_ATTRIBUTES), NULL, TRUE};
/* We need to use manual reset evet object due to the way our
/* We need to use a manual reset event object due to the way our
w32-pth wait function works: If we would use an automatic
reset event we are not able to figure out which handle has
been signaled because at the time we single out the signaled

View File

@ -210,7 +210,7 @@ main (int argc, char **argv)
/* Make sure that our subsystems are ready. */
i18n_init ();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
opt_homedir = default_homedir ();

View File

@ -1025,7 +1025,7 @@ main (int argc, char **argv )
/* Make sure that our subsystems are ready. */
i18n_init ();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
if (!gcry_check_version (NEED_LIBGCRYPT_VERSION) )
{

View File

@ -76,10 +76,12 @@ if [ "$myhost" = "w32" ]; then
fi
build=`$tsdir/scripts/config.guess`
extraoptions=""
case $myhostsub in
ce)
[ -z "$w32root" ] && w32root="$HOME/w32ce_root"
toolprefixes="arm-mingw32ce"
extraoptions="--disable-scdaemon"
;;
*)
[ -z "$w32root" ] && w32root="$HOME/w32root"
@ -124,7 +126,7 @@ if [ "$myhost" = "w32" ]; then
--with-regex=${w32root} \
--with-pth-prefix=${w32root} \
--with-adns=${w32root} \
--disable-g13 "$@"
${extraoptions} --disable-g13 "$@"
rc=$?
exit $rc
fi

View File

@ -1,5 +1,42 @@
2010-03-22 Werner Koch <wk@g10code.com>
* init.c (parse_std_file_handles): Change to use rendezvous ids.
2010-03-15 Werner Koch <wk@g10code.com>
* init.c (init_common_subsystems): Add args ARGCP and
ARGVP. Change all callers to provide them.
(parse_std_file_handles): New.
* t-sysutils.c (rewind) [W32CE]: Provide a replacement.
* Makefile.am (module_tests) [W32CE]: Don't build t-exechelp for now.
* sysutils.c (gnupg_allow_set_foregound_window) [W32CE]: Don't
call AllowSetForegroundWindow.
* logging.c (isatty) [W32CE]: New.
(fun_writer, set_file_fd): Use estream even for the internal error
messages.
(log_string, log_flush): Make DUMMY_ARG_PTR static.
2010-03-15 Werner Koch <wk@g10code.com>
* asshelp.c (send_pinentry_environment) [!HAVE_SETLOCALE]: Do not
define OLD_LC.
* http.c (connect_server) [!USE_DNS_SRV]: Mark SRVTAG unused.
* dns-cert.c (get_dns_cert) [!USE_DNS_CERT]: Mark args unused.
* pka.c (get_pka_info): Ditto.
* signal.c (pause_on_sigusr): Remove. It was used in ancient gpg
version with shared memory IPC. Last caller removed on 2006-04-18.
(do_block) [W32]: Mark arg unused.
* exechelp.c (w32_open_null): Use CreateFileW.
* init.c (init_common_subsystems): Add args ARGCP and ARGVP.
Change all callers to pass them.
* logging.c (S_IRGRP, S_IROTH, S_IWGRP, S_IWOTH) [W32]: New.
(fun_writer, set_file_fd) [W32]: Disable socket code.

View File

@ -142,8 +142,11 @@ jnlib_tests = t-stringhelp t-timestuff
if HAVE_W32_SYSTEM
jnlib_tests += t-w32-reg
endif
module_tests = t-convert t-percent t-gettime t-sysutils t-sexputil t-exechelp \
module_tests = t-convert t-percent t-gettime t-sysutils t-sexputil \
t-session-env
if !HAVE_W32CE_SYSTEM
module_tests += t-exechelp
endif
module_maint_tests = t-helpfile t-b64

View File

@ -1131,6 +1131,8 @@ strusage( int level )
return p;
}
/* Set the usage handler. This function is basically a constructor. */
void
set_strusage ( const char *(*f)( int ) )
{

View File

@ -103,7 +103,9 @@ send_pinentry_environment (assuan_context_t ctx,
{
gpg_error_t err = 0;
#if defined(HAVE_SETLOCALE)
char *old_lc = NULL;
#endif
char *dft_lc = NULL;
const char *dft_ttyname;
int iterator;

View File

@ -279,6 +279,13 @@ get_dns_cert (const char *name, size_t max_size, IOBUF *iobuf,
return ret;
#endif /*!USE_ADNS*/
#else /* !USE_DNS_CERT */
(void)name;
(void)max_size;
(void)iobuf;
(void)fpr;
(void)fpr_len;
(void)url;
return -1;
#endif
}

View File

@ -245,6 +245,11 @@ static estream_mutex_t estream_list_lock;
#define ESTREAM_LIST_LOCK ESTREAM_MUTEX_LOCK (estream_list_lock)
#define ESTREAM_LIST_UNLOCK ESTREAM_MUTEX_UNLOCK (estream_list_lock)
/* File descriptors registered to be used as the standard file handles. */
static int custom_std_fds[3];
static unsigned char custom_std_fds_valid[3];
#ifndef EOPNOTSUPP
# define EOPNOTSUPP ENOSYS
#endif
@ -2239,7 +2244,7 @@ es_fopencookie (void *ES__RESTRICT cookie,
estream_t
do_fdopen (int filedes, const char *mode, int no_close)
do_fdopen (int filedes, const char *mode, int no_close, int with_locked_list)
{
unsigned int modeflags;
int create_called;
@ -2261,7 +2266,7 @@ do_fdopen (int filedes, const char *mode, int no_close)
create_called = 1;
err = es_create (&stream, cookie, filedes, estream_functions_fd,
modeflags, 0);
modeflags, with_locked_list);
out:
@ -2274,14 +2279,14 @@ do_fdopen (int filedes, const char *mode, int no_close)
estream_t
es_fdopen (int filedes, const char *mode)
{
return do_fdopen (filedes, mode, 0);
return do_fdopen (filedes, mode, 0, 0);
}
/* A variant of es_fdopen which does not close FILEDES at the end. */
estream_t
es_fdopen_nc (int filedes, const char *mode)
{
return do_fdopen (filedes, mode, 1);
return do_fdopen (filedes, mode, 1, 0);
}
@ -2344,6 +2349,23 @@ es_fpopen_nc (FILE *fp, const char *mode)
}
/* Set custom standard descriptors to be used for stdin, stdout and
stderr. This function needs to be called before any of the
standard streams are accessed. */
void
_es_set_std_fd (int no, int fd)
{
ESTREAM_LIST_LOCK;
if (no >= 0 && no < 3 && !custom_std_fds_valid[no])
{
custom_std_fds[no] = fd;
custom_std_fds_valid[no] = 1;
}
ESTREAM_LIST_UNLOCK;
}
/* Return the stream used for stdin, stdout or stderr. */
estream_t
_es_get_std_stream (int fd)
{
@ -2359,6 +2381,17 @@ _es_get_std_stream (int fd)
stream = list_obj->car;
break;
}
if (!stream)
{
/* Standard stream not yet created. We first try to create them
from registered file descriptors. */
if (!fd && custom_std_fds_valid[0])
stream = do_fdopen (custom_std_fds[0], "r", 1, 1);
else if (fd == 1 && custom_std_fds_valid[1])
stream = do_fdopen (custom_std_fds[1], "a", 1, 1);
else if (custom_std_fds_valid[2])
stream = do_fdopen (custom_std_fds[1], "a", 1, 1);
}
if (!stream)
{
/* Standard stream not yet created - do it now. */

View File

@ -80,6 +80,7 @@
#define es_fdopen_nc _ESTREAM_PREFIX(es_fdopen_nc)
#define es_fpopen _ESTREAM_PREFIX(es_fpopen)
#define es_fpopen_nc _ESTREAM_PREFIX(es_fpopen_nc)
#define _es_set_std_fd _ESTREAM_PREFIX(_es_set_std_fd)
#define _es_get_std_stream _ESTREAM_PREFIX(_es_get_std_stream)
#define es_freopen _ESTREAM_PREFIX(es_freopen)
#define es_fopencookie _ESTREAM_PREFIX(es_fopencookie)
@ -251,6 +252,7 @@ int es_fclose (estream_t stream);
int es_fileno (estream_t stream);
int es_fileno_unlocked (estream_t stream);
void _es_set_std_fd (int no, int fd);
estream_t _es_get_std_stream (int fd);
#define es_stdin _es_get_std_stream (0)
@ -364,7 +366,6 @@ int es_write_sanitized_utf8_buffer (estream_t stream,
size_t *bytes_written);
#endif /*GNUPG_MAJOR_VERSION*/
#ifdef __cplusplus
}
#endif

View File

@ -381,10 +381,10 @@ w32_open_null (int for_write)
{
HANDLE hfile;
hfile = CreateFile ("nul",
for_write? GENERIC_WRITE : GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
hfile = CreateFileW (L"nul",
for_write? GENERIC_WRITE : GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hfile == INVALID_HANDLE_VALUE)
log_debug ("can't open `nul': %s\n", w32_strerror (-1));
return hfile;

View File

@ -1600,6 +1600,8 @@ connect_server (const char *server, unsigned short port,
srvcount = getsrv (srvname, &serverlist);
}
}
#else
(void)srvtag;
#endif /*USE_DNS_SRV*/
if (!serverlist)

View File

@ -33,16 +33,23 @@
#include "util.h"
#ifdef HAVE_W32CE_SYSTEM
#include <assuan.h>
static void parse_std_file_handles (int *argcp, char ***argvp);
#endif /*HAVE_W32CE_SYSTEM*/
/* This function is to be used early at program startup to make sure
that some subsystems are initialized. This is in particular
important for W32 to initialize the sockets so that our socket
emulation code used directly as well as in libassuan may be used.
It should best be called before any I/O is done so that setup
required for logging is ready. CAUTION: This might be called while
running suid(root). */
required for logging is ready. ARGCP and ARGVP are the addresses
of the parameters given to main. This function may modify them.
CAUTION: This might be called while running suid(root). */
void
init_common_subsystems (void)
init_common_subsystems (int *argcp, char ***argvp)
{
/* Try to auto set the character set. */
set_native_charset (NULL);
@ -66,5 +73,85 @@ init_common_subsystems (void)
/* Initialize the Estream library. */
es_init ();
/* Special hack for Windows CE: We extract some options from arg
to setup the standard handles. */
#ifdef HAVE_W32CE_SYSTEM
parse_std_file_handles (argcp, argvp);
#else
(void)argcp;
(void)argvp;
#endif
}
/* WindowsCE uses a very strange way of handling the standard streams.
There is a function SetStdioPath to associate a standard stream
with a file or a device but what we really want is to use pipes as
standard streams. Despite that we implement pipes using a device,
we would have some limitations on the number of open pipes due to
the 3 character limit of device file name. Thus we don't take this
path. Another option would be to install a file system driver with
support for pipes; this would allow us to get rid of the device
name length limitation. However, with GnuPG we can get away be
redefining the standard streams and passing the handles to be used
on the command line. This has also the advantage that it makes
creating a process much easier and does not require the
SetStdioPath set and restore game. The caller needs to pass the
rendezvous ids using up to three options:
-&S0=<handle> -&S1=<handle> -&S2=<handle>
They are all optional but they must be the first arguments on the
command line. Parsing stops as soon as an invalid option is found.
These rendezvous ids are then used to finish the pipe creation.*/
#ifdef HAVE_W32CE_SYSTEM
static void
parse_std_file_handles (int *argcp, char ***argvp)
{
int argc = *argcp;
char **argv = *argvp;
const char *s;
assuan_fd_t fd;
int i;
int fixup = 0;
if (!argc)
return;
for (argc--, argv++; argc; argc--, argv++)
{
s = *argv;
if (*s == '-' && s[1] == '&' && s[2] == 'S'
&& (s[3] == '0' || s[3] == '1' || s[3] == '2')
&& s[4] == '='
&& (strchr ("-01234567890", s[5]) || !strcmp (s+5, "null")))
{
if (s[5] == 'n')
fd = ASSUAN_INVALID_FD;
else
fd = _assuan_w32ce_finish_pipe (atoi (s+5), s[3] != '0');
_es_set_std_fd (s[3] - '0', (int)fd);
fixup++;
}
else
break;
}
if (fixup)
{
argc = *argcp;
argc -= fixup;
*argcp = argc;
argv = *argvp;
for (i=1; i < argc; i++)
argv[i] = argv[i + fixup];
for (; i < argc + fixup; i++)
argv[i] = NULL;
}
}
#endif /*HAVE_W32CE_SYSTEM*/

View File

@ -20,7 +20,7 @@
#ifndef GNUPG_COMMON_INIT_H
#define GNUPG_COMMON_INIT_H
void init_common_subsystems (void);
void init_common_subsystems (int *argcp, char ***argvp);
#endif /*GNUPG_COMMON_INIT_H*/

View File

@ -52,6 +52,11 @@
#endif
#ifdef HAVE_W32CE_SYSTEM
# define isatty(a) (0)
#endif
static estream_t logstream;
static int log_socket = -1;
static char prefix_buffer[80];
@ -138,9 +143,9 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size)
if (cookie->fd == -1)
{
if (!cookie->quiet && !running_detached
&& isatty (fileno (stderr)))
fprintf (stderr, "failed to create socket for logging: %s\n",
strerror(errno));
&& isatty (es_fileno (es_stderr)))
es_fprintf (es_stderr, "failed to create socket for logging: %s\n",
strerror(errno));
}
else
{
@ -156,9 +161,9 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size)
if (connect (cookie->fd, (struct sockaddr *) &addr, addrlen) == -1)
{
if (!cookie->quiet && !running_detached
&& isatty (fileno (stderr)))
fprintf (stderr, "can't connect to `%s': %s\n",
cookie->name, strerror(errno));
&& isatty (es_fileno (es_stderr)))
es_fprintf (es_stderr, "can't connect to `%s': %s\n",
cookie->name, strerror(errno));
close (cookie->fd);
cookie->fd = -1;
}
@ -193,14 +198,14 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size)
return (ssize_t)size; /* Okay. */
if (!running_detached && cookie->fd != -1
&& isatty (fileno (stderr)))
&& isatty (es_fileno (es_stderr)))
{
if (*cookie->name)
fprintf (stderr, "error writing to `%s': %s\n",
cookie->name, strerror(errno));
es_fprintf (es_stderr, "error writing to `%s': %s\n",
cookie->name, strerror(errno));
else
fprintf (stderr, "error writing to file descriptor %d: %s\n",
cookie->fd, strerror(errno));
es_fprintf (es_stderr, "error writing to file descriptor %d: %s\n",
cookie->fd, strerror(errno));
}
if (cookie->is_socket && cookie->fd != -1)
{
@ -246,7 +251,7 @@ set_file_fd (const char *name, int fd)
if (name && !strcmp (name, "-"))
{
name = NULL;
fd = fileno (stderr);
fd = es_fileno (es_stderr);
}
#ifndef HAVE_W32_SYSTEM
@ -514,8 +519,10 @@ void
log_string (int level, const char *string)
{
/* We need to provide a dummy arg_ptr. volatile is needed to
suppress compiler warnings. */
volatile va_list dummy_arg_ptr;
suppress compiler warnings. The static is required for gcc 4.4
because it seems that it detects that a volatile automatic
variable is not any good if not initialized. */
static volatile va_list dummy_arg_ptr;
do_logv (level, 1, string, dummy_arg_ptr);
}
@ -597,7 +604,7 @@ log_printf (const char *fmt, ...)
void
log_flush (void)
{
volatile va_list dummy_arg_ptr;
static volatile va_list dummy_arg_ptr;
do_logv (JNLIB_LOG_CONT, 1, NULL, dummy_arg_ptr);
}

View File

@ -274,6 +274,8 @@ get_pka_info (const char *address, unsigned char *fpr)
char *
get_pka_info (const char *address, unsigned char *fpr)
{
(void)address;
(void)fpr;
return NULL;
}
#endif /* !USE_DNS_PKA */

View File

@ -170,38 +170,13 @@ gnupg_init_signals (int mode, void (*fast_cleanup)(void))
#endif
}
void
gnupg_pause_on_sigusr (int which)
{
#ifndef HAVE_DOSISH_SYSTEM
# ifdef HAVE_SIGPROCMASK
sigset_t mask, oldmask;
assert (which == 1);
sigemptyset( &mask );
sigaddset( &mask, SIGUSR1 );
sigprocmask( SIG_BLOCK, &mask, &oldmask );
while (!caught_sigusr1)
sigsuspend (&oldmask);
caught_sigusr1 = 0;
sigprocmask (SIG_UNBLOCK, &mask, NULL);
# else
assert (which == 1);
sighold (SIGUSR1);
while (!caught_sigusr1)
sigpause(SIGUSR1);
caught_sigusr1 = 0;
sigrelease(SIGUSR1);
# endif /*!HAVE_SIGPROCMASK*/
#endif
}
static void
do_block( int block )
do_block (int block)
{
#ifndef HAVE_DOSISH_SYSTEM
#ifdef HAVE_DOSISH_SYSTEM
(void)block;
#else /*!HAVE_DOSISH_SYSTEM*/
static int is_blocked;
#ifdef HAVE_SIGPROCMASK
static sigset_t oldmask;
@ -247,7 +222,7 @@ do_block( int block )
is_blocked = 0;
}
#endif /*!HAVE_SIGPROCMASK*/
#endif /*HAVE_DOSISH_SYSTEM*/
#endif /*!HAVE_DOSISH_SYSTEM*/
}

View File

@ -498,7 +498,7 @@ gnupg_allow_set_foregound_window (pid_t pid)
if (!pid)
log_info ("%s called with invalid pid %lu\n",
"gnupg_allow_set_foregound_window", (unsigned long)pid);
#ifdef HAVE_W32_SYSTEM
#if defined(HAVE_W32_SYSTEM) && !defined(HAVE_W32CE_SYSTEM)
else if (!AllowSetForegroundWindow ((pid_t)pid == (pid_t)(-1)?ASFW_ANY:pid))
log_info ("AllowSetForegroundWindow(%lu) failed: %s\n",
(unsigned long)pid, w32_strerror (-1));

View File

@ -24,6 +24,10 @@
#include "util.h"
#include "sysutils.h"
#ifdef HAVE_W32CE_SYSTEM
# define rewind(f) do { fseek (f, 0, SEEK_SET); clearerr (f); } while (0)
#endif
#define pass() do { ; } while(0)
#define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
__FILE__,__LINE__, (a)); \

View File

@ -141,7 +141,6 @@ gnupg_copy_time (gnupg_isotime_t d, const gnupg_isotime_t s)
/*-- signal.c --*/
void gnupg_init_signals (int mode, void (*fast_cleanup)(void));
void gnupg_pause_on_sigusr (int which);
void gnupg_block_all_signals (void);
void gnupg_unblock_all_signals (void);

View File

@ -484,7 +484,7 @@ GNUPG_CHECK_USTAR
# We need to compile and run a program on the build machine. A
# comment in libgpg-error says that the AC_PROG_CC_FOR_BUILD macro in
# the AC archive is broken for autoconf 2.57. Given that tehre is no
# the AC archive is broken for autoconf 2.57. Given that there is no
# newer version of that macro, we assume that it is also broken for
# autoconf 2.61 and thus we use a simple but usually sufficient
# approach.

View File

@ -1940,7 +1940,7 @@ main (int argc, char **argv)
/* Make sure that our subsystems are ready. */
i18n_init();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
/* Check that the libraries are suitable. Do it right here because the
option parsing may need services of the library. */

View File

@ -148,7 +148,7 @@ main( int argc, char **argv )
/* Make sure that our subsystems are ready. */
i18n_init();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
gnupg_init_signals (0, NULL);

View File

@ -359,7 +359,7 @@ main ( int argc, char **argv)
/* Make sure that our subsystems are ready. */
i18n_init ();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
/* Libgcrypt requires us to register the threading model first.
Note that this will also do the pth_init. */

View File

@ -419,7 +419,7 @@ main( int argc, char **argv )
/* Make sure that our subsystems are ready. */
i18n_init ();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
/* Check that the libraries are suitable. Do it here because
the option parsing may need services of the library. */

View File

@ -406,7 +406,7 @@ main (int argc, char **argv )
/* Make sure that our subsystems are ready. */
i18n_init ();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
/* Libgcrypt requires us to register the threading model first.

View File

@ -911,7 +911,7 @@ main ( int argc, char **argv)
/* Make sure that our subsystems are ready. */
i18n_init();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
/* Check that the libraries are suitable. Do it here because the
option parse may need services of the library */

View File

@ -1248,7 +1248,7 @@ gpgsm_server (certlist_t default_recplist)
/* We use a pipe based server so that we can work from scripts.
assuan_init_pipe_server will automagically detect when we are
called with a socketpair and ignore FIELDES in this case. */
called with a socketpair and ignore FILEDES in this case. */
filedes[0] = assuan_fdopen (0);
filedes[1] = assuan_fdopen (1);
rc = assuan_new (&ctx);

View File

@ -169,7 +169,7 @@ main (int argc, char **argv )
/* Make sure that our subsystems are ready. */
i18n_init ();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
/* We need Libgcrypt for hashing. */
if (!gcry_check_version (NEED_LIBGCRYPT_VERSION) )

View File

@ -1158,7 +1158,7 @@ main (int argc, char **argv)
/* Make sure that our subsystems are ready. */
i18n_init();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
assuan_set_gpg_err_source (0);

View File

@ -152,7 +152,7 @@ main (int argc, char **argv)
/* Make sure that our subsystems are ready. */
i18n_init();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
/* Parse the command line. */
pargs.argc = &argc;

View File

@ -884,7 +884,7 @@ main (int argc, char **argv)
/* Make sure that our subsystems are ready. */
i18n_init();
init_common_subsystems ();
init_common_subsystems (&argc, &argv);
opt.homedir = default_homedir ();