2015-12-14 09:38:46 +01:00
|
|
|
|
/* exectool.c - Utility functions to execute a helper tool
|
2015-11-30 12:53:57 +01:00
|
|
|
|
* Copyright (C) 2015 Werner Koch
|
2016-07-26 14:31:11 +02:00
|
|
|
|
* Copyright (C) 2016 g10 Code GmbH
|
2015-11-30 12:53:57 +01:00
|
|
|
|
*
|
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
|
*
|
2015-12-14 09:44:38 +01:00
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of either
|
2015-11-30 12:53:57 +01:00
|
|
|
|
*
|
2015-12-14 09:44:38 +01: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,
|
2015-11-30 12:53:57 +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
|
2016-11-05 12:02:19 +01:00
|
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2015-11-30 12:53:57 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <assert.h>
|
2015-11-30 14:23:02 +01:00
|
|
|
|
#include <gpg-error.h>
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
|
|
|
|
#include <assuan.h>
|
common,agent,gpg,dirmngr,g13,scd,tests,tools: New spawn function.
* common/exechelp-posix.c (do_exec, gnupg_spawn_process): Remove.
(check_syscall_func, pre_syscall, post_syscall) : New.
(do_create_socketpair, posix_open_null, call_spawn_cb): New.
(my_exec, spawn_detached, gnupg_spawn_helper): New.
(gnupg_process_spawn, process_kill, gnupg_process_terminate): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_release): New.
(gnupg_process_wait_list): New.
* common/exechelp-w32.c: Add definition of _WIN32_WINNT as 0x600.
(check_syscall_func, pre_syscall, post_syscall): New.
(gnupg_spawn_process): Remove.
(check_windows_version): New.
(spawn_detached, gnupg_spawn_helper, gnupg_process_spawn): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_kill, process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_terminate): New.
(gnupg_process_release, gnupg_process_wait_list): New.
* common/exechelp.h: Re-write for new API.
* common/exectool.c (gnupg_exec_tool_stream): Follow the change.
* common/asshelp.c (start_new_service): Likewise.
* agent/genkey.c (do_check_passphrase_pattern): Likewise.
* dirmngr/ldap-wrapper.c (struct wrapper_context_s): Use PROC.
(destroy_wrapper): Follow the change of API.
(read_log_data): Follow the change of API, use printable_pid.
(ldap_reaper_thread, ldap_wrapper_release_context): Likewise.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Likewise.
* g10/photoid.c (run_with_pipe): Follow the change of API.
(show_photo): Likewise.
* g13/be-encfs.c (run_umount_helper): Likewise.
(run_encfs_tool): Likewise.
* g13/g13.c: Add including ./common/exechelp.h.
* g13/mount.c: Likewise.
* g13/runner.c: Follow the change of API.
* g13/runner.h: Follow the change of API.
* scd/app.c (setup_env): New.
(report_change): Follow the change of API.
* tests/gpgscm/ffi.c (proc_object_finalize): New.
(proc_object_to_string): New.
(proc_wrap, proc_unwrap): New.
(do_spawn_process): Remove.
(do_process_spawn): New.
(setup_std_fds): New.
(do_spawn_process_fd): Remove.
(do_process_spawn_fd): New.
(do_wait_process): Remove.
(do_process_wait): New.
(do_wait_processes): Remove.
* tests/gpgscm/t-child.scm: Follow the change of API.
* tests/gpgscm/tests.scm: Likewise.
* tests/openpgp/defs.scm: Likewise.
* tests/tpm2dtests/defs.scm: Likewise.
* tools/gpg-card.c: Likewise.
* tools/gpgconf-comp.c: Likewise.
* tools/gpgconf.c: Likewise.
* tools/gpgtar-create.c: Likewise.
* tools/gpgtar-extract.c: Likewise.
* tools/gpgtar-list.c: Likewise.
--
GnuPG-bug-id: 6275
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2023-05-11 19:18:21 +09:00
|
|
|
|
|
2015-11-30 12:53:57 +01:00
|
|
|
|
#include "i18n.h"
|
2015-11-30 13:00:11 +01:00
|
|
|
|
#include "logging.h"
|
2015-11-30 12:53:57 +01:00
|
|
|
|
#include "membuf.h"
|
2015-11-30 14:23:02 +01:00
|
|
|
|
#include "mischelp.h"
|
common,agent,gpg,dirmngr,g13,scd,tests,tools: New spawn function.
* common/exechelp-posix.c (do_exec, gnupg_spawn_process): Remove.
(check_syscall_func, pre_syscall, post_syscall) : New.
(do_create_socketpair, posix_open_null, call_spawn_cb): New.
(my_exec, spawn_detached, gnupg_spawn_helper): New.
(gnupg_process_spawn, process_kill, gnupg_process_terminate): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_release): New.
(gnupg_process_wait_list): New.
* common/exechelp-w32.c: Add definition of _WIN32_WINNT as 0x600.
(check_syscall_func, pre_syscall, post_syscall): New.
(gnupg_spawn_process): Remove.
(check_windows_version): New.
(spawn_detached, gnupg_spawn_helper, gnupg_process_spawn): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_kill, process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_terminate): New.
(gnupg_process_release, gnupg_process_wait_list): New.
* common/exechelp.h: Re-write for new API.
* common/exectool.c (gnupg_exec_tool_stream): Follow the change.
* common/asshelp.c (start_new_service): Likewise.
* agent/genkey.c (do_check_passphrase_pattern): Likewise.
* dirmngr/ldap-wrapper.c (struct wrapper_context_s): Use PROC.
(destroy_wrapper): Follow the change of API.
(read_log_data): Follow the change of API, use printable_pid.
(ldap_reaper_thread, ldap_wrapper_release_context): Likewise.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Likewise.
* g10/photoid.c (run_with_pipe): Follow the change of API.
(show_photo): Likewise.
* g13/be-encfs.c (run_umount_helper): Likewise.
(run_encfs_tool): Likewise.
* g13/g13.c: Add including ./common/exechelp.h.
* g13/mount.c: Likewise.
* g13/runner.c: Follow the change of API.
* g13/runner.h: Follow the change of API.
* scd/app.c (setup_env): New.
(report_change): Follow the change of API.
* tests/gpgscm/ffi.c (proc_object_finalize): New.
(proc_object_to_string): New.
(proc_wrap, proc_unwrap): New.
(do_spawn_process): Remove.
(do_process_spawn): New.
(setup_std_fds): New.
(do_spawn_process_fd): Remove.
(do_process_spawn_fd): New.
(do_wait_process): Remove.
(do_process_wait): New.
(do_wait_processes): Remove.
* tests/gpgscm/t-child.scm: Follow the change of API.
* tests/gpgscm/tests.scm: Likewise.
* tests/openpgp/defs.scm: Likewise.
* tests/tpm2dtests/defs.scm: Likewise.
* tools/gpg-card.c: Likewise.
* tools/gpgconf-comp.c: Likewise.
* tools/gpgconf.c: Likewise.
* tools/gpgtar-create.c: Likewise.
* tools/gpgtar-extract.c: Likewise.
* tools/gpgtar-list.c: Likewise.
--
GnuPG-bug-id: 6275
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2023-05-11 19:18:21 +09:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
#define NEED_STRUCT_SPAWN_CB_ARG 1
|
|
|
|
|
#endif
|
2015-11-30 12:53:57 +01:00
|
|
|
|
#include "exechelp.h"
|
|
|
|
|
#include "sysutils.h"
|
2015-11-30 14:23:02 +01:00
|
|
|
|
#include "util.h"
|
2016-05-28 00:07:09 +02:00
|
|
|
|
#include "exectool.h"
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
const char *pgmname;
|
2016-05-28 00:07:09 +02:00
|
|
|
|
exec_tool_status_cb_t status_cb;
|
|
|
|
|
void *status_cb_value;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
int cont;
|
2019-01-30 14:38:37 +01:00
|
|
|
|
int quiet;
|
2016-05-28 00:07:09 +02:00
|
|
|
|
size_t used;
|
|
|
|
|
size_t buffer_size;
|
|
|
|
|
char *buffer;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
} read_and_log_buffer_t;
|
|
|
|
|
|
|
|
|
|
|
2015-12-14 19:55:34 +01:00
|
|
|
|
static inline gpg_error_t
|
|
|
|
|
my_error_from_syserror (void)
|
|
|
|
|
{
|
|
|
|
|
return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-11-30 12:53:57 +01:00
|
|
|
|
static void
|
|
|
|
|
read_and_log_stderr (read_and_log_buffer_t *state, es_poll_t *fderr)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
|
|
if (!fderr)
|
|
|
|
|
{
|
|
|
|
|
/* Flush internal buffer. */
|
|
|
|
|
if (state->used)
|
|
|
|
|
{
|
|
|
|
|
const char *pname;
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
state->buffer[state->used] = 0;
|
|
|
|
|
state->used = 0;
|
|
|
|
|
|
|
|
|
|
pname = strrchr (state->pgmname, '/');
|
|
|
|
|
if (pname && pname != state->pgmname && pname[1])
|
|
|
|
|
pname++;
|
|
|
|
|
else
|
|
|
|
|
pname = state->pgmname;
|
|
|
|
|
len = strlen (pname);
|
2016-05-28 00:07:09 +02:00
|
|
|
|
|
|
|
|
|
if (state->status_cb
|
|
|
|
|
&& !strncmp (state->buffer, "[GNUPG:] ", 9)
|
|
|
|
|
&& state->buffer[9] >= 'A' && state->buffer[9] <= 'Z')
|
|
|
|
|
{
|
|
|
|
|
char *rest;
|
|
|
|
|
|
|
|
|
|
rest = strchr (state->buffer + 9, ' ');
|
|
|
|
|
if (!rest)
|
|
|
|
|
{
|
|
|
|
|
/* Set REST to an empty string. */
|
|
|
|
|
rest = state->buffer + strlen (state->buffer);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*rest++ = 0;
|
|
|
|
|
trim_spaces (rest);
|
|
|
|
|
}
|
|
|
|
|
state->status_cb (state->status_cb_value,
|
|
|
|
|
state->buffer + 9, rest);
|
|
|
|
|
}
|
2019-01-30 14:38:37 +01:00
|
|
|
|
else if (state->quiet)
|
|
|
|
|
;
|
2016-05-28 00:07:09 +02:00
|
|
|
|
else if (!state->cont
|
2015-11-30 12:53:57 +01:00
|
|
|
|
&& !strncmp (state->buffer, pname, len)
|
|
|
|
|
&& strlen (state->buffer) > strlen (pname)
|
|
|
|
|
&& state->buffer[len] == ':' )
|
2016-05-28 00:07:09 +02:00
|
|
|
|
{
|
|
|
|
|
/* PGMNAME plus colon is identical to the start of
|
|
|
|
|
the output: print only the output. */
|
|
|
|
|
log_info ("%s\n", state->buffer);
|
|
|
|
|
}
|
2015-11-30 12:53:57 +01:00
|
|
|
|
else
|
|
|
|
|
log_info ("%s%c %s\n",
|
|
|
|
|
pname, state->cont? '+':':', state->buffer);
|
|
|
|
|
}
|
|
|
|
|
state->cont = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
c = es_fgetc (fderr->stream);
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
{
|
|
|
|
|
if (es_feof (fderr->stream))
|
|
|
|
|
{
|
|
|
|
|
fderr->ignore = 1; /* Not anymore needed. */
|
|
|
|
|
}
|
|
|
|
|
else if (es_ferror (fderr->stream))
|
|
|
|
|
{
|
2015-12-14 19:55:34 +01:00
|
|
|
|
err = my_error_from_syserror ();
|
2015-11-30 12:53:57 +01:00
|
|
|
|
log_error ("error reading stderr of '%s': %s\n",
|
|
|
|
|
state->pgmname, gpg_strerror (err));
|
|
|
|
|
fderr->ignore = 1; /* Disable. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (c == '\n')
|
|
|
|
|
{
|
|
|
|
|
read_and_log_stderr (state, NULL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-28 00:07:09 +02:00
|
|
|
|
if (state->used >= state->buffer_size - 1)
|
2015-11-30 12:53:57 +01:00
|
|
|
|
{
|
2016-05-28 00:07:09 +02:00
|
|
|
|
if (state->status_cb)
|
|
|
|
|
{
|
|
|
|
|
/* A status callback requires that we have a full
|
|
|
|
|
* line. Thus we need to enlarget the buffer in
|
|
|
|
|
* this case. */
|
|
|
|
|
char *newbuffer;
|
|
|
|
|
size_t newsize = state->buffer_size + 256;
|
|
|
|
|
|
|
|
|
|
newbuffer = xtrymalloc (newsize);
|
|
|
|
|
if (!newbuffer)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error allocating memory for status cb: %s\n",
|
|
|
|
|
gpg_strerror (my_error_from_syserror ()));
|
|
|
|
|
/* We better disable the status CB in this case. */
|
|
|
|
|
state->status_cb = NULL;
|
|
|
|
|
read_and_log_stderr (state, NULL);
|
|
|
|
|
state->cont = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
memcpy (newbuffer, state->buffer, state->used);
|
|
|
|
|
xfree (state->buffer);
|
|
|
|
|
state->buffer = newbuffer;
|
|
|
|
|
state->buffer_size = newsize;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
read_and_log_stderr (state, NULL);
|
|
|
|
|
state->cont = 1;
|
|
|
|
|
}
|
2015-11-30 12:53:57 +01:00
|
|
|
|
}
|
|
|
|
|
state->buffer[state->used++] = c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-30 14:23:02 +01:00
|
|
|
|
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
2015-11-30 14:23:02 +01:00
|
|
|
|
/* A buffer to copy from one stream to another. */
|
|
|
|
|
struct copy_buffer
|
|
|
|
|
{
|
|
|
|
|
char buffer[4096];
|
|
|
|
|
char *writep;
|
|
|
|
|
size_t nread;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize a copy buffer. */
|
|
|
|
|
static void
|
|
|
|
|
copy_buffer_init (struct copy_buffer *c)
|
|
|
|
|
{
|
|
|
|
|
c->writep = c->buffer;
|
|
|
|
|
c->nread = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Securely wipe a copy buffer. */
|
|
|
|
|
static void
|
|
|
|
|
copy_buffer_shred (struct copy_buffer *c)
|
|
|
|
|
{
|
2016-07-26 14:49:02 +02:00
|
|
|
|
if (c == NULL)
|
|
|
|
|
return;
|
2015-11-30 14:23:02 +01:00
|
|
|
|
wipememory (c->buffer, sizeof c->buffer);
|
|
|
|
|
c->writep = NULL;
|
|
|
|
|
c->nread = ~0U;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Copy data from SOURCE to SINK using copy buffer C. */
|
2015-11-30 12:53:57 +01:00
|
|
|
|
static gpg_error_t
|
2015-11-30 14:23:02 +01:00
|
|
|
|
copy_buffer_do_copy (struct copy_buffer *c, estream_t source, estream_t sink)
|
2015-11-30 12:53:57 +01:00
|
|
|
|
{
|
2015-11-30 14:23:02 +01:00
|
|
|
|
gpg_error_t err;
|
2016-06-28 14:38:35 +02:00
|
|
|
|
size_t nwritten = 0;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
2015-11-30 14:23:02 +01:00
|
|
|
|
if (c->nread == 0)
|
2015-11-30 12:53:57 +01:00
|
|
|
|
{
|
2015-11-30 14:23:02 +01:00
|
|
|
|
c->writep = c->buffer;
|
2017-01-19 14:58:23 +01:00
|
|
|
|
if (es_read (source, c->buffer, sizeof c->buffer, &c->nread))
|
2015-11-30 12:53:57 +01:00
|
|
|
|
{
|
2017-01-19 09:57:27 +01:00
|
|
|
|
err = my_error_from_syserror ();
|
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_EAGAIN)
|
2015-11-30 14:23:02 +01:00
|
|
|
|
return 0; /* We will just retry next time. */
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
2017-01-19 09:57:27 +01:00
|
|
|
|
return err;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
}
|
2015-11-30 14:23:02 +01:00
|
|
|
|
|
2017-01-19 09:57:27 +01:00
|
|
|
|
log_assert (c->nread <= sizeof c->buffer);
|
2015-11-30 14:23:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->nread == 0)
|
|
|
|
|
return 0; /* Done copying. */
|
|
|
|
|
|
2016-10-18 17:57:19 +02:00
|
|
|
|
nwritten = 0;
|
2017-01-19 14:58:23 +01:00
|
|
|
|
if (sink && es_write (sink, c->writep, c->nread, &nwritten))
|
|
|
|
|
err = my_error_from_syserror ();
|
|
|
|
|
else
|
|
|
|
|
err = 0;
|
2016-10-18 17:57:19 +02:00
|
|
|
|
|
2017-01-19 09:57:27 +01:00
|
|
|
|
log_assert (nwritten <= c->nread);
|
2016-10-18 17:57:19 +02:00
|
|
|
|
c->writep += nwritten;
|
|
|
|
|
c->nread -= nwritten;
|
2017-01-19 09:57:27 +01:00
|
|
|
|
log_assert (c->writep - c->buffer <= sizeof c->buffer);
|
2016-10-18 17:57:19 +02:00
|
|
|
|
|
2015-11-30 14:23:02 +01:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
2017-01-19 09:57:27 +01:00
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_EAGAIN)
|
2015-11-30 14:23:02 +01:00
|
|
|
|
return 0; /* We will just retry next time. */
|
|
|
|
|
|
2017-01-19 09:57:27 +01:00
|
|
|
|
return err;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-27 22:48:04 +02:00
|
|
|
|
if (sink && es_fflush (sink) && errno != EAGAIN)
|
2015-12-14 19:55:34 +01:00
|
|
|
|
err = my_error_from_syserror ();
|
2015-11-30 14:23:02 +01:00
|
|
|
|
|
2015-11-30 12:53:57 +01:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-11-30 14:23:02 +01:00
|
|
|
|
/* Flush the remaining data to SINK. */
|
|
|
|
|
static gpg_error_t
|
|
|
|
|
copy_buffer_flush (struct copy_buffer *c, estream_t sink)
|
|
|
|
|
{
|
2017-01-19 14:58:23 +01:00
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
|
size_t nwritten = 0;
|
2015-11-30 14:23:02 +01:00
|
|
|
|
|
2017-01-19 14:58:23 +01:00
|
|
|
|
if (es_write (sink, c->writep, c->nread, &nwritten))
|
|
|
|
|
err = my_error_from_syserror ();
|
2017-01-18 18:14:41 +01:00
|
|
|
|
|
2017-01-19 09:57:27 +01:00
|
|
|
|
log_assert (nwritten <= c->nread);
|
2017-01-18 18:14:41 +01:00
|
|
|
|
c->writep += nwritten;
|
|
|
|
|
c->nread -= nwritten;
|
2017-01-19 09:57:27 +01:00
|
|
|
|
log_assert (c->writep - c->buffer <= sizeof c->buffer);
|
2017-01-18 18:14:41 +01:00
|
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
2015-11-30 14:23:02 +01:00
|
|
|
|
|
2017-01-18 18:14:41 +01:00
|
|
|
|
if (es_fflush (sink))
|
|
|
|
|
err = my_error_from_syserror ();
|
|
|
|
|
|
|
|
|
|
return err;
|
2015-11-30 14:23:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-11-30 12:53:57 +01:00
|
|
|
|
/* Run the program PGMNAME with the command line arguments given in
|
2016-05-27 22:48:04 +02:00
|
|
|
|
* the NULL terminates array ARGV. If INPUT is not NULL it will be
|
|
|
|
|
* fed to stdin of the process. stderr is logged using log_info and
|
Spelling cleanup.
No functional changes, just fixing minor spelling issues.
---
Most of these were identified from the command line by running:
codespell \
--ignore-words-list fpr,stati,keyserver,keyservers,asign,cas,iff,ifset \
--skip '*.po,ChangeLog*,help.*.txt,*.jpg,*.eps,*.pdf,*.png,*.gpg,*.asc' \
doc g13 g10 kbx agent artwork scd tests tools am common dirmngr sm \
NEWS README README.maint TODO
Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
2020-02-18 09:34:42 -05:00
|
|
|
|
* the process's stdout is written to OUTPUT. If OUTPUT is NULL the
|
2016-05-27 22:48:04 +02:00
|
|
|
|
* output is discarded. If INEXTRA is given, an additional input
|
|
|
|
|
* stream will be passed to the child; to tell the child about this
|
|
|
|
|
* ARGV is scanned and the first occurrence of an argument
|
|
|
|
|
* "-&@INEXTRA@" is replaced by the concatenation of "-&" and the
|
|
|
|
|
* child's file descriptor of the pipe created for the INEXTRA stream.
|
|
|
|
|
*
|
|
|
|
|
* On error a diagnostic is printed and an error code returned. */
|
2015-11-30 12:53:57 +01:00
|
|
|
|
gpg_error_t
|
2015-12-14 09:38:46 +01:00
|
|
|
|
gnupg_exec_tool_stream (const char *pgmname, const char *argv[],
|
2016-05-27 22:48:04 +02:00
|
|
|
|
estream_t input, estream_t inextra,
|
2016-05-28 00:07:09 +02:00
|
|
|
|
estream_t output,
|
|
|
|
|
exec_tool_status_cb_t status_cb,
|
|
|
|
|
void *status_cb_value)
|
2015-11-30 12:53:57 +01:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
common,agent,gpg,dirmngr,g13,scd,tests,tools: New spawn function.
* common/exechelp-posix.c (do_exec, gnupg_spawn_process): Remove.
(check_syscall_func, pre_syscall, post_syscall) : New.
(do_create_socketpair, posix_open_null, call_spawn_cb): New.
(my_exec, spawn_detached, gnupg_spawn_helper): New.
(gnupg_process_spawn, process_kill, gnupg_process_terminate): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_release): New.
(gnupg_process_wait_list): New.
* common/exechelp-w32.c: Add definition of _WIN32_WINNT as 0x600.
(check_syscall_func, pre_syscall, post_syscall): New.
(gnupg_spawn_process): Remove.
(check_windows_version): New.
(spawn_detached, gnupg_spawn_helper, gnupg_process_spawn): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_kill, process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_terminate): New.
(gnupg_process_release, gnupg_process_wait_list): New.
* common/exechelp.h: Re-write for new API.
* common/exectool.c (gnupg_exec_tool_stream): Follow the change.
* common/asshelp.c (start_new_service): Likewise.
* agent/genkey.c (do_check_passphrase_pattern): Likewise.
* dirmngr/ldap-wrapper.c (struct wrapper_context_s): Use PROC.
(destroy_wrapper): Follow the change of API.
(read_log_data): Follow the change of API, use printable_pid.
(ldap_reaper_thread, ldap_wrapper_release_context): Likewise.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Likewise.
* g10/photoid.c (run_with_pipe): Follow the change of API.
(show_photo): Likewise.
* g13/be-encfs.c (run_umount_helper): Likewise.
(run_encfs_tool): Likewise.
* g13/g13.c: Add including ./common/exechelp.h.
* g13/mount.c: Likewise.
* g13/runner.c: Follow the change of API.
* g13/runner.h: Follow the change of API.
* scd/app.c (setup_env): New.
(report_change): Follow the change of API.
* tests/gpgscm/ffi.c (proc_object_finalize): New.
(proc_object_to_string): New.
(proc_wrap, proc_unwrap): New.
(do_spawn_process): Remove.
(do_process_spawn): New.
(setup_std_fds): New.
(do_spawn_process_fd): Remove.
(do_process_spawn_fd): New.
(do_wait_process): Remove.
(do_process_wait): New.
(do_wait_processes): Remove.
* tests/gpgscm/t-child.scm: Follow the change of API.
* tests/gpgscm/tests.scm: Likewise.
* tests/openpgp/defs.scm: Likewise.
* tests/tpm2dtests/defs.scm: Likewise.
* tools/gpg-card.c: Likewise.
* tools/gpgconf-comp.c: Likewise.
* tools/gpgconf.c: Likewise.
* tools/gpgtar-create.c: Likewise.
* tools/gpgtar-extract.c: Likewise.
* tools/gpgtar-list.c: Likewise.
--
GnuPG-bug-id: 6275
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2023-05-11 19:18:21 +09:00
|
|
|
|
gnupg_process_t proc = NULL;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
estream_t infp = NULL;
|
2016-05-27 22:48:04 +02:00
|
|
|
|
estream_t extrafp = NULL;
|
2016-07-26 14:31:11 +02:00
|
|
|
|
estream_t outfp = NULL, errfp = NULL;
|
2016-05-27 22:48:04 +02:00
|
|
|
|
es_poll_t fds[4];
|
2024-06-04 14:50:57 +09:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
HANDLE exceptclose[2];
|
|
|
|
|
#else
|
2016-05-27 22:48:04 +02:00
|
|
|
|
int exceptclose[2];
|
2024-06-04 14:50:57 +09:00
|
|
|
|
#endif
|
2016-05-27 22:48:04 +02:00
|
|
|
|
int extrapipe[2] = {-1, -1};
|
|
|
|
|
char extrafdbuf[20];
|
|
|
|
|
const char *argsave = NULL;
|
|
|
|
|
int argsaveidx;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
int count;
|
|
|
|
|
read_and_log_buffer_t fderrstate;
|
2016-07-26 14:49:02 +02:00
|
|
|
|
struct copy_buffer *cpbuf_in = NULL, *cpbuf_out = NULL, *cpbuf_extra = NULL;
|
2019-01-30 14:38:37 +01:00
|
|
|
|
int quiet = 0;
|
2024-05-31 15:36:39 +09:00
|
|
|
|
gnupg_spawn_actions_t act = NULL;
|
2024-06-04 14:50:57 +09:00
|
|
|
|
int i = 0;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
|
|
|
|
memset (fds, 0, sizeof fds);
|
|
|
|
|
memset (&fderrstate, 0, sizeof fderrstate);
|
2016-07-26 14:49:02 +02:00
|
|
|
|
|
2019-01-30 14:38:37 +01:00
|
|
|
|
/* If the first argument to the program is "--quiet" avoid all extra
|
|
|
|
|
* diagnostics. */
|
|
|
|
|
quiet = (argv && argv[0] && !strcmp (argv[0], "--quiet"));
|
|
|
|
|
|
2016-07-26 14:49:02 +02:00
|
|
|
|
cpbuf_in = xtrymalloc (sizeof *cpbuf_in);
|
|
|
|
|
if (cpbuf_in == NULL)
|
|
|
|
|
{
|
|
|
|
|
err = my_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
copy_buffer_init (cpbuf_in);
|
|
|
|
|
|
|
|
|
|
cpbuf_out = xtrymalloc (sizeof *cpbuf_out);
|
|
|
|
|
if (cpbuf_out == NULL)
|
|
|
|
|
{
|
|
|
|
|
err = my_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
copy_buffer_init (cpbuf_out);
|
|
|
|
|
|
|
|
|
|
cpbuf_extra = xtrymalloc (sizeof *cpbuf_extra);
|
|
|
|
|
if (cpbuf_extra == NULL)
|
|
|
|
|
{
|
|
|
|
|
err = my_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
copy_buffer_init (cpbuf_extra);
|
2016-05-27 22:48:04 +02:00
|
|
|
|
|
2016-05-28 00:07:09 +02:00
|
|
|
|
fderrstate.pgmname = pgmname;
|
2019-01-30 14:38:37 +01:00
|
|
|
|
fderrstate.quiet = quiet;
|
2016-05-28 00:07:09 +02:00
|
|
|
|
fderrstate.status_cb = status_cb;
|
|
|
|
|
fderrstate.status_cb_value = status_cb_value;
|
|
|
|
|
fderrstate.buffer_size = 256;
|
|
|
|
|
fderrstate.buffer = xtrymalloc (fderrstate.buffer_size);
|
|
|
|
|
if (!fderrstate.buffer)
|
2016-07-26 14:31:11 +02:00
|
|
|
|
{
|
|
|
|
|
err = my_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2016-05-28 00:07:09 +02:00
|
|
|
|
|
2016-05-27 22:48:04 +02:00
|
|
|
|
if (inextra)
|
|
|
|
|
{
|
|
|
|
|
err = gnupg_create_outbound_pipe (extrapipe, &extrafp, 1);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
2019-01-30 14:38:37 +01:00
|
|
|
|
log_error ("error creating outbound pipe for extra fp: %s\n",
|
2016-05-27 22:48:04 +02:00
|
|
|
|
gpg_strerror (err));
|
2016-07-26 14:31:11 +02:00
|
|
|
|
goto leave;
|
2016-05-27 22:48:04 +02:00
|
|
|
|
}
|
2024-06-04 14:50:57 +09:00
|
|
|
|
/* Do not close in child. */
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
exceptclose[i] = (HANDLE)_get_osfhandle (extrapipe[0]);
|
|
|
|
|
#else
|
|
|
|
|
exceptclose[i] = extrapipe[0];
|
|
|
|
|
#endif
|
2016-05-27 22:48:04 +02:00
|
|
|
|
/* Now find the argument marker and replace by the pipe's fd.
|
|
|
|
|
Yeah, that is an ugly non-thread safe hack but it safes us to
|
|
|
|
|
create a copy of the array. */
|
2017-02-28 09:34:29 +01:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2024-06-06 08:56:27 +09:00
|
|
|
|
# ifdef _WIN64
|
|
|
|
|
snprintf (extrafdbuf, sizeof extrafdbuf, "-&%llu",
|
|
|
|
|
(unsigned long long)exceptclose[i]);
|
|
|
|
|
# else
|
2017-02-28 09:34:29 +01:00
|
|
|
|
snprintf (extrafdbuf, sizeof extrafdbuf, "-&%lu",
|
2024-06-04 14:50:57 +09:00
|
|
|
|
(unsigned long)exceptclose[i]);
|
2024-06-06 08:56:27 +09:00
|
|
|
|
# endif
|
2017-02-28 09:34:29 +01:00
|
|
|
|
#else
|
2024-06-04 14:50:57 +09:00
|
|
|
|
snprintf (extrafdbuf, sizeof extrafdbuf, "-&%d", exceptclose[i]);
|
2017-02-28 09:34:29 +01:00
|
|
|
|
#endif
|
2016-05-27 22:48:04 +02:00
|
|
|
|
for (argsaveidx=0; argv[argsaveidx]; argsaveidx++)
|
|
|
|
|
if (!strcmp (argv[argsaveidx], "-&@INEXTRA@"))
|
|
|
|
|
{
|
|
|
|
|
argsave = argv[argsaveidx];
|
|
|
|
|
argv[argsaveidx] = extrafdbuf;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-06-04 14:50:57 +09:00
|
|
|
|
i++;
|
2016-05-27 22:48:04 +02:00
|
|
|
|
}
|
2024-06-04 14:50:57 +09:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
exceptclose[i] = INVALID_HANDLE_VALUE;
|
|
|
|
|
#else
|
|
|
|
|
exceptclose[i] = -1;
|
|
|
|
|
#endif
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
2024-05-31 15:36:39 +09:00
|
|
|
|
err = gnupg_spawn_actions_new (&act);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
gnupg_spawn_actions_set_inherit_handles (act, exceptclose);
|
|
|
|
|
#else
|
|
|
|
|
gnupg_spawn_actions_set_inherit_fds (act, exceptclose);
|
|
|
|
|
#endif
|
common,agent,gpg,dirmngr,g13,scd,tests,tools: New spawn function.
* common/exechelp-posix.c (do_exec, gnupg_spawn_process): Remove.
(check_syscall_func, pre_syscall, post_syscall) : New.
(do_create_socketpair, posix_open_null, call_spawn_cb): New.
(my_exec, spawn_detached, gnupg_spawn_helper): New.
(gnupg_process_spawn, process_kill, gnupg_process_terminate): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_release): New.
(gnupg_process_wait_list): New.
* common/exechelp-w32.c: Add definition of _WIN32_WINNT as 0x600.
(check_syscall_func, pre_syscall, post_syscall): New.
(gnupg_spawn_process): Remove.
(check_windows_version): New.
(spawn_detached, gnupg_spawn_helper, gnupg_process_spawn): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_kill, process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_terminate): New.
(gnupg_process_release, gnupg_process_wait_list): New.
* common/exechelp.h: Re-write for new API.
* common/exectool.c (gnupg_exec_tool_stream): Follow the change.
* common/asshelp.c (start_new_service): Likewise.
* agent/genkey.c (do_check_passphrase_pattern): Likewise.
* dirmngr/ldap-wrapper.c (struct wrapper_context_s): Use PROC.
(destroy_wrapper): Follow the change of API.
(read_log_data): Follow the change of API, use printable_pid.
(ldap_reaper_thread, ldap_wrapper_release_context): Likewise.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Likewise.
* g10/photoid.c (run_with_pipe): Follow the change of API.
(show_photo): Likewise.
* g13/be-encfs.c (run_umount_helper): Likewise.
(run_encfs_tool): Likewise.
* g13/g13.c: Add including ./common/exechelp.h.
* g13/mount.c: Likewise.
* g13/runner.c: Follow the change of API.
* g13/runner.h: Follow the change of API.
* scd/app.c (setup_env): New.
(report_change): Follow the change of API.
* tests/gpgscm/ffi.c (proc_object_finalize): New.
(proc_object_to_string): New.
(proc_wrap, proc_unwrap): New.
(do_spawn_process): Remove.
(do_process_spawn): New.
(setup_std_fds): New.
(do_spawn_process_fd): Remove.
(do_process_spawn_fd): New.
(do_wait_process): Remove.
(do_process_wait): New.
(do_wait_processes): Remove.
* tests/gpgscm/t-child.scm: Follow the change of API.
* tests/gpgscm/tests.scm: Likewise.
* tests/openpgp/defs.scm: Likewise.
* tests/tpm2dtests/defs.scm: Likewise.
* tools/gpg-card.c: Likewise.
* tools/gpgconf-comp.c: Likewise.
* tools/gpgconf.c: Likewise.
* tools/gpgtar-create.c: Likewise.
* tools/gpgtar-extract.c: Likewise.
* tools/gpgtar-list.c: Likewise.
--
GnuPG-bug-id: 6275
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2023-05-11 19:18:21 +09:00
|
|
|
|
err = gnupg_process_spawn (pgmname, argv,
|
|
|
|
|
((input
|
|
|
|
|
? GNUPG_PROCESS_STDIN_PIPE
|
|
|
|
|
: 0)
|
|
|
|
|
| GNUPG_PROCESS_STDOUT_PIPE
|
2024-05-31 15:36:39 +09:00
|
|
|
|
| GNUPG_PROCESS_STDERR_PIPE), act, &proc);
|
common,agent,gpg,dirmngr,g13,scd,tests,tools: New spawn function.
* common/exechelp-posix.c (do_exec, gnupg_spawn_process): Remove.
(check_syscall_func, pre_syscall, post_syscall) : New.
(do_create_socketpair, posix_open_null, call_spawn_cb): New.
(my_exec, spawn_detached, gnupg_spawn_helper): New.
(gnupg_process_spawn, process_kill, gnupg_process_terminate): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_release): New.
(gnupg_process_wait_list): New.
* common/exechelp-w32.c: Add definition of _WIN32_WINNT as 0x600.
(check_syscall_func, pre_syscall, post_syscall): New.
(gnupg_spawn_process): Remove.
(check_windows_version): New.
(spawn_detached, gnupg_spawn_helper, gnupg_process_spawn): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_kill, process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_terminate): New.
(gnupg_process_release, gnupg_process_wait_list): New.
* common/exechelp.h: Re-write for new API.
* common/exectool.c (gnupg_exec_tool_stream): Follow the change.
* common/asshelp.c (start_new_service): Likewise.
* agent/genkey.c (do_check_passphrase_pattern): Likewise.
* dirmngr/ldap-wrapper.c (struct wrapper_context_s): Use PROC.
(destroy_wrapper): Follow the change of API.
(read_log_data): Follow the change of API, use printable_pid.
(ldap_reaper_thread, ldap_wrapper_release_context): Likewise.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Likewise.
* g10/photoid.c (run_with_pipe): Follow the change of API.
(show_photo): Likewise.
* g13/be-encfs.c (run_umount_helper): Likewise.
(run_encfs_tool): Likewise.
* g13/g13.c: Add including ./common/exechelp.h.
* g13/mount.c: Likewise.
* g13/runner.c: Follow the change of API.
* g13/runner.h: Follow the change of API.
* scd/app.c (setup_env): New.
(report_change): Follow the change of API.
* tests/gpgscm/ffi.c (proc_object_finalize): New.
(proc_object_to_string): New.
(proc_wrap, proc_unwrap): New.
(do_spawn_process): Remove.
(do_process_spawn): New.
(setup_std_fds): New.
(do_spawn_process_fd): Remove.
(do_process_spawn_fd): New.
(do_wait_process): Remove.
(do_process_wait): New.
(do_wait_processes): Remove.
* tests/gpgscm/t-child.scm: Follow the change of API.
* tests/gpgscm/tests.scm: Likewise.
* tests/openpgp/defs.scm: Likewise.
* tests/tpm2dtests/defs.scm: Likewise.
* tools/gpg-card.c: Likewise.
* tools/gpgconf-comp.c: Likewise.
* tools/gpgconf.c: Likewise.
* tools/gpgtar-create.c: Likewise.
* tools/gpgtar-extract.c: Likewise.
* tools/gpgtar-list.c: Likewise.
--
GnuPG-bug-id: 6275
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2023-05-11 19:18:21 +09:00
|
|
|
|
gnupg_process_get_streams (proc, GNUPG_PROCESS_STREAM_NONBLOCK,
|
|
|
|
|
input? &infp : NULL, &outfp, &errfp);
|
2016-05-27 22:48:04 +02:00
|
|
|
|
if (extrapipe[0] != -1)
|
|
|
|
|
close (extrapipe[0]);
|
|
|
|
|
if (argsave)
|
|
|
|
|
argv[argsaveidx] = argsave;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
2019-01-30 14:38:37 +01:00
|
|
|
|
if (!quiet)
|
|
|
|
|
log_error ("error running '%s': %s\n", pgmname, gpg_strerror (err));
|
2016-07-26 14:31:11 +02:00
|
|
|
|
goto leave;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fds[0].stream = infp;
|
|
|
|
|
fds[0].want_write = 1;
|
2015-11-30 14:23:02 +01:00
|
|
|
|
if (!input)
|
2015-11-30 12:53:57 +01:00
|
|
|
|
fds[0].ignore = 1;
|
|
|
|
|
fds[1].stream = outfp;
|
|
|
|
|
fds[1].want_read = 1;
|
|
|
|
|
fds[2].stream = errfp;
|
|
|
|
|
fds[2].want_read = 1;
|
2016-05-27 22:48:04 +02:00
|
|
|
|
fds[3].stream = extrafp;
|
|
|
|
|
fds[3].want_write = 1;
|
|
|
|
|
if (!inextra)
|
|
|
|
|
fds[3].ignore = 1;
|
|
|
|
|
|
2015-11-30 12:53:57 +01:00
|
|
|
|
/* Now read as long as we have something to poll. We continue
|
|
|
|
|
reading even after EOF or error on stdout so that we get the
|
2017-04-28 10:06:33 +09:00
|
|
|
|
other error messages or remaining output. */
|
2016-06-28 14:38:35 +02:00
|
|
|
|
while (! (fds[1].ignore && fds[2].ignore))
|
2015-11-30 12:53:57 +01:00
|
|
|
|
{
|
|
|
|
|
count = es_poll (fds, DIM(fds), -1);
|
|
|
|
|
if (count == -1)
|
|
|
|
|
{
|
2015-12-14 19:55:34 +01:00
|
|
|
|
err = my_error_from_syserror ();
|
2015-11-30 12:53:57 +01:00
|
|
|
|
log_error ("error polling '%s': %s\n", pgmname, gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if (!count)
|
|
|
|
|
{
|
|
|
|
|
log_debug ("unexpected timeout while polling '%s'\n", pgmname);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fds[0].got_write)
|
|
|
|
|
{
|
2016-07-26 14:49:02 +02:00
|
|
|
|
err = copy_buffer_do_copy (cpbuf_in, input, fds[0].stream);
|
2015-11-30 14:23:02 +01:00
|
|
|
|
if (err)
|
2015-11-30 12:53:57 +01:00
|
|
|
|
{
|
2015-11-30 14:23:02 +01:00
|
|
|
|
log_error ("error feeding data to '%s': %s\n",
|
|
|
|
|
pgmname, gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
2015-11-30 14:23:02 +01:00
|
|
|
|
if (es_feof (input))
|
2015-11-30 12:53:57 +01:00
|
|
|
|
{
|
2016-07-26 14:49:02 +02:00
|
|
|
|
err = copy_buffer_flush (cpbuf_in, fds[0].stream);
|
2017-01-19 09:57:27 +01:00
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_EAGAIN)
|
2017-01-18 18:14:41 +01:00
|
|
|
|
continue; /* Retry next time. */
|
2015-11-30 14:23:02 +01:00
|
|
|
|
if (err)
|
2015-11-30 12:53:57 +01:00
|
|
|
|
{
|
2015-11-30 14:23:02 +01:00
|
|
|
|
log_error ("error feeding data to '%s': %s\n",
|
|
|
|
|
pgmname, gpg_strerror (err));
|
|
|
|
|
goto leave;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
}
|
2015-11-30 14:23:02 +01:00
|
|
|
|
|
2015-11-30 12:53:57 +01:00
|
|
|
|
fds[0].ignore = 1; /* ready. */
|
|
|
|
|
es_fclose (infp); infp = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-27 22:48:04 +02:00
|
|
|
|
if (fds[3].got_write)
|
|
|
|
|
{
|
|
|
|
|
log_assert (inextra);
|
2016-07-26 14:49:02 +02:00
|
|
|
|
err = copy_buffer_do_copy (cpbuf_extra, inextra, fds[3].stream);
|
2016-05-27 22:48:04 +02:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error feeding data to '%s': %s\n",
|
|
|
|
|
pgmname, gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (es_feof (inextra))
|
|
|
|
|
{
|
2016-07-26 14:49:02 +02:00
|
|
|
|
err = copy_buffer_flush (cpbuf_extra, fds[3].stream);
|
2017-01-19 09:57:27 +01:00
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_EAGAIN)
|
2017-01-18 18:14:41 +01:00
|
|
|
|
continue; /* Retry next time. */
|
2016-05-27 22:48:04 +02:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error feeding data to '%s': %s\n",
|
|
|
|
|
pgmname, gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fds[3].ignore = 1; /* ready. */
|
|
|
|
|
es_fclose (extrafp); extrafp = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-30 12:53:57 +01:00
|
|
|
|
if (fds[1].got_read)
|
2015-11-30 14:23:02 +01:00
|
|
|
|
{
|
2016-07-26 14:49:02 +02:00
|
|
|
|
err = copy_buffer_do_copy (cpbuf_out, fds[1].stream, output);
|
2015-11-30 14:23:02 +01:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error reading data from '%s': %s\n",
|
|
|
|
|
pgmname, gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2016-06-28 14:38:35 +02:00
|
|
|
|
|
|
|
|
|
if (es_feof (fds[1].stream))
|
|
|
|
|
{
|
2016-07-26 14:49:02 +02:00
|
|
|
|
err = copy_buffer_flush (cpbuf_out, output);
|
2016-06-28 14:38:35 +02:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error reading data from '%s': %s\n",
|
|
|
|
|
pgmname, gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fds[1].ignore = 1; /* ready. */
|
|
|
|
|
}
|
2015-11-30 14:23:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-30 12:53:57 +01:00
|
|
|
|
if (fds[2].got_read)
|
|
|
|
|
read_and_log_stderr (&fderrstate, fds + 2);
|
2015-11-30 14:23:02 +01:00
|
|
|
|
}
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
|
|
|
|
read_and_log_stderr (&fderrstate, NULL); /* Flush. */
|
|
|
|
|
es_fclose (infp); infp = NULL;
|
2016-05-27 22:48:04 +02:00
|
|
|
|
es_fclose (extrafp); extrafp = NULL;
|
2015-11-30 12:53:57 +01:00
|
|
|
|
es_fclose (outfp); outfp = NULL;
|
|
|
|
|
es_fclose (errfp); errfp = NULL;
|
|
|
|
|
|
common,agent,gpg,dirmngr,g13,scd,tests,tools: New spawn function.
* common/exechelp-posix.c (do_exec, gnupg_spawn_process): Remove.
(check_syscall_func, pre_syscall, post_syscall) : New.
(do_create_socketpair, posix_open_null, call_spawn_cb): New.
(my_exec, spawn_detached, gnupg_spawn_helper): New.
(gnupg_process_spawn, process_kill, gnupg_process_terminate): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_release): New.
(gnupg_process_wait_list): New.
* common/exechelp-w32.c: Add definition of _WIN32_WINNT as 0x600.
(check_syscall_func, pre_syscall, post_syscall): New.
(gnupg_spawn_process): Remove.
(check_windows_version): New.
(spawn_detached, gnupg_spawn_helper, gnupg_process_spawn): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_kill, process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_terminate): New.
(gnupg_process_release, gnupg_process_wait_list): New.
* common/exechelp.h: Re-write for new API.
* common/exectool.c (gnupg_exec_tool_stream): Follow the change.
* common/asshelp.c (start_new_service): Likewise.
* agent/genkey.c (do_check_passphrase_pattern): Likewise.
* dirmngr/ldap-wrapper.c (struct wrapper_context_s): Use PROC.
(destroy_wrapper): Follow the change of API.
(read_log_data): Follow the change of API, use printable_pid.
(ldap_reaper_thread, ldap_wrapper_release_context): Likewise.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Likewise.
* g10/photoid.c (run_with_pipe): Follow the change of API.
(show_photo): Likewise.
* g13/be-encfs.c (run_umount_helper): Likewise.
(run_encfs_tool): Likewise.
* g13/g13.c: Add including ./common/exechelp.h.
* g13/mount.c: Likewise.
* g13/runner.c: Follow the change of API.
* g13/runner.h: Follow the change of API.
* scd/app.c (setup_env): New.
(report_change): Follow the change of API.
* tests/gpgscm/ffi.c (proc_object_finalize): New.
(proc_object_to_string): New.
(proc_wrap, proc_unwrap): New.
(do_spawn_process): Remove.
(do_process_spawn): New.
(setup_std_fds): New.
(do_spawn_process_fd): Remove.
(do_process_spawn_fd): New.
(do_wait_process): Remove.
(do_process_wait): New.
(do_wait_processes): Remove.
* tests/gpgscm/t-child.scm: Follow the change of API.
* tests/gpgscm/tests.scm: Likewise.
* tests/openpgp/defs.scm: Likewise.
* tests/tpm2dtests/defs.scm: Likewise.
* tools/gpg-card.c: Likewise.
* tools/gpgconf-comp.c: Likewise.
* tools/gpgconf.c: Likewise.
* tools/gpgtar-create.c: Likewise.
* tools/gpgtar-extract.c: Likewise.
* tools/gpgtar-list.c: Likewise.
--
GnuPG-bug-id: 6275
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2023-05-11 19:18:21 +09:00
|
|
|
|
err = gnupg_process_wait (proc, 1);
|
|
|
|
|
if (!err)
|
|
|
|
|
{ /* To be compatible to old wait_process. */
|
|
|
|
|
int status;
|
|
|
|
|
|
|
|
|
|
gnupg_process_ctl (proc, GNUPG_PROCESS_GET_EXIT_ID, &status);
|
|
|
|
|
if (status)
|
|
|
|
|
err = gpg_error (GPG_ERR_GENERAL);
|
|
|
|
|
}
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
|
|
|
|
leave:
|
common,agent,gpg,dirmngr,g13,scd,tests,tools: New spawn function.
* common/exechelp-posix.c (do_exec, gnupg_spawn_process): Remove.
(check_syscall_func, pre_syscall, post_syscall) : New.
(do_create_socketpair, posix_open_null, call_spawn_cb): New.
(my_exec, spawn_detached, gnupg_spawn_helper): New.
(gnupg_process_spawn, process_kill, gnupg_process_terminate): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_release): New.
(gnupg_process_wait_list): New.
* common/exechelp-w32.c: Add definition of _WIN32_WINNT as 0x600.
(check_syscall_func, pre_syscall, post_syscall): New.
(gnupg_spawn_process): Remove.
(check_windows_version): New.
(spawn_detached, gnupg_spawn_helper, gnupg_process_spawn): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_kill, process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_terminate): New.
(gnupg_process_release, gnupg_process_wait_list): New.
* common/exechelp.h: Re-write for new API.
* common/exectool.c (gnupg_exec_tool_stream): Follow the change.
* common/asshelp.c (start_new_service): Likewise.
* agent/genkey.c (do_check_passphrase_pattern): Likewise.
* dirmngr/ldap-wrapper.c (struct wrapper_context_s): Use PROC.
(destroy_wrapper): Follow the change of API.
(read_log_data): Follow the change of API, use printable_pid.
(ldap_reaper_thread, ldap_wrapper_release_context): Likewise.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Likewise.
* g10/photoid.c (run_with_pipe): Follow the change of API.
(show_photo): Likewise.
* g13/be-encfs.c (run_umount_helper): Likewise.
(run_encfs_tool): Likewise.
* g13/g13.c: Add including ./common/exechelp.h.
* g13/mount.c: Likewise.
* g13/runner.c: Follow the change of API.
* g13/runner.h: Follow the change of API.
* scd/app.c (setup_env): New.
(report_change): Follow the change of API.
* tests/gpgscm/ffi.c (proc_object_finalize): New.
(proc_object_to_string): New.
(proc_wrap, proc_unwrap): New.
(do_spawn_process): Remove.
(do_process_spawn): New.
(setup_std_fds): New.
(do_spawn_process_fd): Remove.
(do_process_spawn_fd): New.
(do_wait_process): Remove.
(do_process_wait): New.
(do_wait_processes): Remove.
* tests/gpgscm/t-child.scm: Follow the change of API.
* tests/gpgscm/tests.scm: Likewise.
* tests/openpgp/defs.scm: Likewise.
* tests/tpm2dtests/defs.scm: Likewise.
* tools/gpg-card.c: Likewise.
* tools/gpgconf-comp.c: Likewise.
* tools/gpgconf.c: Likewise.
* tools/gpgtar-create.c: Likewise.
* tools/gpgtar-extract.c: Likewise.
* tools/gpgtar-list.c: Likewise.
--
GnuPG-bug-id: 6275
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2023-05-11 19:18:21 +09:00
|
|
|
|
if (err && proc)
|
|
|
|
|
gnupg_process_terminate (proc);
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
|
|
|
|
es_fclose (infp);
|
2016-05-27 22:48:04 +02:00
|
|
|
|
es_fclose (extrafp);
|
2015-11-30 12:53:57 +01:00
|
|
|
|
es_fclose (outfp);
|
|
|
|
|
es_fclose (errfp);
|
common,agent,gpg,dirmngr,g13,scd,tests,tools: New spawn function.
* common/exechelp-posix.c (do_exec, gnupg_spawn_process): Remove.
(check_syscall_func, pre_syscall, post_syscall) : New.
(do_create_socketpair, posix_open_null, call_spawn_cb): New.
(my_exec, spawn_detached, gnupg_spawn_helper): New.
(gnupg_process_spawn, process_kill, gnupg_process_terminate): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_release): New.
(gnupg_process_wait_list): New.
* common/exechelp-w32.c: Add definition of _WIN32_WINNT as 0x600.
(check_syscall_func, pre_syscall, post_syscall): New.
(gnupg_spawn_process): Remove.
(check_windows_version): New.
(spawn_detached, gnupg_spawn_helper, gnupg_process_spawn): New.
(gnupg_process_get_fds, gnupg_process_get_streams): New.
(process_kill, process_vctl, gnupg_process_ctl): New.
(gnupg_process_wait, gnupg_process_terminate): New.
(gnupg_process_release, gnupg_process_wait_list): New.
* common/exechelp.h: Re-write for new API.
* common/exectool.c (gnupg_exec_tool_stream): Follow the change.
* common/asshelp.c (start_new_service): Likewise.
* agent/genkey.c (do_check_passphrase_pattern): Likewise.
* dirmngr/ldap-wrapper.c (struct wrapper_context_s): Use PROC.
(destroy_wrapper): Follow the change of API.
(read_log_data): Follow the change of API, use printable_pid.
(ldap_reaper_thread, ldap_wrapper_release_context): Likewise.
(ldap_wrapper_connection_cleanup, ldap_wrapper): Likewise.
* g10/photoid.c (run_with_pipe): Follow the change of API.
(show_photo): Likewise.
* g13/be-encfs.c (run_umount_helper): Likewise.
(run_encfs_tool): Likewise.
* g13/g13.c: Add including ./common/exechelp.h.
* g13/mount.c: Likewise.
* g13/runner.c: Follow the change of API.
* g13/runner.h: Follow the change of API.
* scd/app.c (setup_env): New.
(report_change): Follow the change of API.
* tests/gpgscm/ffi.c (proc_object_finalize): New.
(proc_object_to_string): New.
(proc_wrap, proc_unwrap): New.
(do_spawn_process): Remove.
(do_process_spawn): New.
(setup_std_fds): New.
(do_spawn_process_fd): Remove.
(do_process_spawn_fd): New.
(do_wait_process): Remove.
(do_process_wait): New.
(do_wait_processes): Remove.
* tests/gpgscm/t-child.scm: Follow the change of API.
* tests/gpgscm/tests.scm: Likewise.
* tests/openpgp/defs.scm: Likewise.
* tests/tpm2dtests/defs.scm: Likewise.
* tools/gpg-card.c: Likewise.
* tools/gpgconf-comp.c: Likewise.
* tools/gpgconf.c: Likewise.
* tools/gpgtar-create.c: Likewise.
* tools/gpgtar-extract.c: Likewise.
* tools/gpgtar-list.c: Likewise.
--
GnuPG-bug-id: 6275
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2023-05-11 19:18:21 +09:00
|
|
|
|
gnupg_process_release (proc);
|
2024-05-31 15:36:39 +09:00
|
|
|
|
gnupg_spawn_actions_release (act);
|
2015-11-30 12:53:57 +01:00
|
|
|
|
|
2016-07-26 14:49:02 +02:00
|
|
|
|
copy_buffer_shred (cpbuf_in);
|
|
|
|
|
xfree (cpbuf_in);
|
|
|
|
|
copy_buffer_shred (cpbuf_out);
|
|
|
|
|
xfree (cpbuf_out);
|
|
|
|
|
copy_buffer_shred (cpbuf_extra);
|
|
|
|
|
xfree (cpbuf_extra);
|
2016-05-28 00:07:09 +02:00
|
|
|
|
xfree (fderrstate.buffer);
|
2015-11-30 14:23:02 +01:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* A dummy free function to pass to 'es_mopen'. */
|
|
|
|
|
static void
|
|
|
|
|
nop_free (void *ptr)
|
|
|
|
|
{
|
|
|
|
|
(void) ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Run the program PGMNAME with the command line arguments given in
|
|
|
|
|
the NULL terminates array ARGV. If INPUT_STRING is not NULL it
|
|
|
|
|
will be fed to stdin of the process. stderr is logged using
|
Spelling cleanup.
No functional changes, just fixing minor spelling issues.
---
Most of these were identified from the command line by running:
codespell \
--ignore-words-list fpr,stati,keyserver,keyservers,asign,cas,iff,ifset \
--skip '*.po,ChangeLog*,help.*.txt,*.jpg,*.eps,*.pdf,*.png,*.gpg,*.asc' \
doc g13 g10 kbx agent artwork scd tests tools am common dirmngr sm \
NEWS README README.maint TODO
Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
2020-02-18 09:34:42 -05:00
|
|
|
|
log_info and the process's stdout is returned in a newly malloced
|
2015-11-30 14:23:02 +01:00
|
|
|
|
buffer RESULT with the length stored at RESULTLEN if not given as
|
|
|
|
|
NULL. A hidden Nul is appended to the output. On error NULL is
|
|
|
|
|
stored at RESULT, a diagnostic is printed, and an error code
|
|
|
|
|
returned. */
|
|
|
|
|
gpg_error_t
|
2015-12-14 09:38:46 +01:00
|
|
|
|
gnupg_exec_tool (const char *pgmname, const char *argv[],
|
|
|
|
|
const char *input_string,
|
|
|
|
|
char **result, size_t *resultlen)
|
2015-11-30 14:23:02 +01:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
estream_t input = NULL;
|
|
|
|
|
estream_t output;
|
|
|
|
|
size_t len;
|
|
|
|
|
size_t nread;
|
|
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
|
if (resultlen)
|
|
|
|
|
*resultlen = 0;
|
|
|
|
|
|
|
|
|
|
if (input_string)
|
|
|
|
|
{
|
|
|
|
|
len = strlen (input_string);
|
|
|
|
|
input = es_mopen ((char *) input_string, len, len,
|
|
|
|
|
0 /* don't grow */, NULL, nop_free, "rb");
|
|
|
|
|
if (! input)
|
2015-12-14 19:55:34 +01:00
|
|
|
|
return my_error_from_syserror ();
|
2015-11-30 14:23:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output = es_fopenmem (0, "wb");
|
|
|
|
|
if (! output)
|
|
|
|
|
{
|
2015-12-14 19:55:34 +01:00
|
|
|
|
err = my_error_from_syserror ();
|
2015-11-30 14:23:02 +01:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 00:07:09 +02:00
|
|
|
|
err = gnupg_exec_tool_stream (pgmname, argv, input, NULL, output, NULL, NULL);
|
2015-11-30 14:23:02 +01:00
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
len = es_ftello (output);
|
|
|
|
|
err = es_fseek (output, 0, SEEK_SET);
|
|
|
|
|
if (err)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
2016-02-02 18:18:31 +01:00
|
|
|
|
*result = xtrymalloc (len + 1);
|
|
|
|
|
if (!*result)
|
2015-11-30 14:23:02 +01:00
|
|
|
|
{
|
2015-12-14 19:55:34 +01:00
|
|
|
|
err = my_error_from_syserror ();
|
2015-11-30 14:23:02 +01:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 18:18:31 +01:00
|
|
|
|
if (len)
|
2015-11-30 14:23:02 +01:00
|
|
|
|
{
|
2017-01-19 14:58:23 +01:00
|
|
|
|
if (es_read (output, *result, len, &nread))
|
|
|
|
|
{
|
|
|
|
|
err = my_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2016-02-02 18:18:31 +01:00
|
|
|
|
if (nread != len)
|
|
|
|
|
log_fatal ("%s: short read from memstream\n", __func__);
|
2015-11-30 14:23:02 +01:00
|
|
|
|
}
|
2016-02-02 18:18:31 +01:00
|
|
|
|
(*result)[len] = 0;
|
|
|
|
|
|
|
|
|
|
if (resultlen)
|
|
|
|
|
*resultlen = len;
|
2015-11-30 14:23:02 +01:00
|
|
|
|
|
|
|
|
|
leave:
|
2016-02-02 18:18:31 +01:00
|
|
|
|
es_fclose (input);
|
2015-11-30 14:23:02 +01:00
|
|
|
|
es_fclose (output);
|
2016-02-02 18:18:31 +01:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
xfree (*result);
|
|
|
|
|
*result = NULL;
|
|
|
|
|
}
|
2015-11-30 12:53:57 +01:00
|
|
|
|
return err;
|
|
|
|
|
}
|