1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-10 21:38:50 +01:00

common: Make use of default_errsource in exechelp.

* common/exechelp-posix.c (my_error_from_syserror, my_error): New.
Use them instead of gpg_error and gpg_error_from_syserror.
(create_pipe_and_estream): Remove arg ERRSOURCE and fix use of
OUTBOUND which has a wrong name.  Adjust callers.
(gnupg_spawn_process): Remove arg ERRSOURCE and replace by use of
DEFAULT_ERRSOURCE.
* common/exechelp-w32.c (gnupg_spawn_process): Ditto.
* common/exechelp-w32ce.c (gnupg_spawn_process): Ditto.
* common/exectool.c (gnupg_exec_tool_stream):  Do not pass
GPG_ERROR_FROM_SYSERROR.
* tools/gpgconf-comp.c (gc_component_check_options): Ditto.
(retrieve_options_from_program): Ditto.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2016-05-27 15:25:03 +02:00
parent 6c957c3d88
commit 96c7901ec1
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
6 changed files with 63 additions and 53 deletions

View File

@ -73,6 +73,20 @@
#include "exechelp.h"
/* Helper */
static inline gpg_error_t
my_error_from_syserror (void)
{
return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
}
static inline gpg_error_t
my_error (int errcode)
{
return gpg_err_make (default_errsource, errcode);
}
/* Return the maximum number of currently allowed open file
descriptors. Only useful on POSIX systems but returns a value on
other systems too. */
@ -285,13 +299,47 @@ do_create_pipe (int filedes[2])
if (pipe (filedes) == -1)
{
err = gpg_error_from_syserror ();
err = my_error_from_syserror ();
filedes[0] = filedes[1] = -1;
}
return err;
}
static gpg_error_t
create_pipe_and_estream (int filedes[2], estream_t *r_fp,
int outbound, int nonblock)
{
gpg_error_t err;
if (pipe (filedes) == -1)
{
err = my_error_from_syserror ();
log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
filedes[0] = filedes[1] = -1;
*r_fp = NULL;
return err;
}
if (!outbound)
*r_fp = es_fdopen (filedes[0], nonblock? "r,nonblock" : "r");
else
*r_fp = es_fdopen (filedes[1], nonblock? "w,nonblock" : "w");
if (!*r_fp)
{
err = my_error_from_syserror ();
log_error (_("error creating a stream for a pipe: %s\n"),
gpg_strerror (err));
close (filedes[0]);
close (filedes[1]);
filedes[0] = filedes[1] = -1;
return err;
}
return 0;
}
/* Portable function to create a pipe. Under Windows the write end is
inheritable. */
gpg_error_t
@ -319,46 +367,9 @@ gnupg_create_pipe (int filedes[2])
}
static gpg_error_t
create_pipe_and_estream (int filedes[2], estream_t *r_fp,
int outbound, int nonblock,
gpg_err_source_t errsource)
{
gpg_error_t err;
if (pipe (filedes) == -1)
{
err = gpg_err_make (errsource, gpg_err_code_from_syserror ());
log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
filedes[0] = filedes[1] = -1;
*r_fp = NULL;
return err;
}
if (outbound)
*r_fp = es_fdopen (filedes[0], nonblock? "r,nonblock" : "r");
else
*r_fp = es_fdopen (filedes[1], nonblock? "w,nonblock" : "w");
if (!*r_fp)
{
err = gpg_err_make (errsource, gpg_err_code_from_syserror ());
log_error (_("error creating a stream for a pipe: %s\n"),
gpg_strerror (err));
close (filedes[0]);
close (filedes[1]);
filedes[0] = filedes[1] = -1;
return err;
}
return 0;
}
/* Fork and exec the PGMNAME, see exechelp.h for details. */
gpg_error_t
gnupg_spawn_process (const char *pgmname, const char *argv[],
gpg_err_source_t errsource,
void (*preexec)(void), unsigned int flags,
estream_t *r_infp,
estream_t *r_outfp,
@ -384,14 +395,14 @@ gnupg_spawn_process (const char *pgmname, const char *argv[],
if (r_infp)
{
err = create_pipe_and_estream (inpipe, &infp, 0, nonblock, errsource);
err = create_pipe_and_estream (inpipe, &infp, 1, nonblock);
if (err)
return err;
}
if (r_outfp)
{
err = create_pipe_and_estream (outpipe, &outfp, 1, nonblock, errsource);
err = create_pipe_and_estream (outpipe, &outfp, 0, nonblock);
if (err)
{
if (infp)
@ -407,7 +418,7 @@ gnupg_spawn_process (const char *pgmname, const char *argv[],
if (r_errfp)
{
err = create_pipe_and_estream (errpipe, &errfp, 1, nonblock, errsource);
err = create_pipe_and_estream (errpipe, &errfp, 0, nonblock);
if (err)
{
if (infp)
@ -432,7 +443,7 @@ gnupg_spawn_process (const char *pgmname, const char *argv[],
*pid = fork ();
if (*pid == (pid_t)(-1))
{
err = gpg_err_make (errsource, gpg_err_code_from_syserror ());
err = my_error_from_syserror ();
log_error (_("error forking process: %s\n"), gpg_strerror (err));
if (infp)
@ -505,7 +516,7 @@ gnupg_spawn_process_fd (const char *pgmname, const char *argv[],
*pid = fork ();
if (*pid == (pid_t)(-1))
{
err = gpg_error_from_syserror ();
err = my_error_from_syserror ();
log_error (_("error forking process: %s\n"), strerror (errno));
return err;
}
@ -543,7 +554,7 @@ gnupg_wait_processes (const char **pgmnames, pid_t *pids, size_t count,
r_exitcodes[i] = -1;
if (pids[i] == (pid_t)(-1))
return gpg_error (GPG_ERR_INV_VALUE);
return my_error (GPG_ERR_INV_VALUE);
}
left = count;
@ -638,16 +649,16 @@ gnupg_spawn_process_detached (const char *pgmname, const char *argv[],
int i;
if (getuid() != geteuid())
return gpg_error (GPG_ERR_BUG);
return my_error (GPG_ERR_BUG);
if (access (pgmname, X_OK))
return gpg_error_from_syserror ();
return my_error_from_syserror ();
pid = fork ();
if (pid == (pid_t)(-1))
{
log_error (_("error forking process: %s\n"), strerror (errno));
return gpg_error_from_syserror ();
return my_error_from_syserror ();
}
if (!pid)
{

View File

@ -349,7 +349,6 @@ gnupg_create_pipe (int filedes[2])
/* Fork and exec the PGMNAME, see exechelp.h for details. */
gpg_error_t
gnupg_spawn_process (const char *pgmname, const char *argv[],
gpg_err_source_t errsource,
void (*preexec)(void), unsigned int flags,
estream_t *r_infp,
estream_t *r_outfp,
@ -379,6 +378,7 @@ gnupg_spawn_process (const char *pgmname, const char *argv[],
INVALID_HANDLE_VALUE};
int i;
es_syshd_t syshd;
gpg_err_source_t errsource = default_errsource;
if (r_infp)
*r_infp = NULL;

View File

@ -509,7 +509,6 @@ create_process (const char *pgmname, const char *cmdline,
/* Fork and exec the PGMNAME, see exechelp.h for details. */
gpg_error_t
gnupg_spawn_process (const char *pgmname, const char *argv[],
gpg_err_source_t errsource,
void (*preexec)(void), unsigned int flags,
estream_t *r_infp,
estream_t *r_outfp,
@ -534,6 +533,7 @@ gnupg_spawn_process (const char *pgmname, const char *argv[],
} errpipe = {INVALID_HANDLE_VALUE, 0};
estream_t outfp = NULL;
estream_t errfp = NULL;
gpg_err_source_t errsource = default_errsource;
(void)preexec;
(void)flags;

View File

@ -112,7 +112,6 @@ gpg_error_t gnupg_create_pipe (int filedes[2]);
*/
gpg_error_t
gnupg_spawn_process (const char *pgmname, const char *argv[],
gpg_err_source_t errsource,
void (*preexec)(void), unsigned int flags,
estream_t *r_infp,
estream_t *r_outfp,

View File

@ -251,7 +251,7 @@ gnupg_exec_tool_stream (const char *pgmname, const char *argv[],
copy_buffer_init (&cpbuf[0]);
copy_buffer_init (&cpbuf[1]);
err = gnupg_spawn_process (pgmname, argv, GPG_ERR_SOURCE_DEFAULT,
err = gnupg_spawn_process (pgmname, argv,
NULL, GNUPG_SPAWN_NONBLOCK,
input? &infp : NULL,
&outfp, &errfp, &pid);

View File

@ -1626,7 +1626,7 @@ gc_component_check_options (int component, estream_t out, const char *conf_file)
result = 0;
errlines = NULL;
err = gnupg_spawn_process (pgmname, argv, GPG_ERR_SOURCE_DEFAULT, NULL, 0,
err = gnupg_spawn_process (pgmname, argv, NULL, 0,
NULL, NULL, &errfp, &pid);
if (err)
result |= 1; /* Program could not be run. */
@ -1965,7 +1965,7 @@ retrieve_options_from_program (gc_component_t component, gc_backend_t backend)
argv[0] = "--gpgconf-list";
argv[1] = NULL;
err = gnupg_spawn_process (pgmname, argv, GPG_ERR_SOURCE_DEFAULT, NULL, 0,
err = gnupg_spawn_process (pgmname, argv, NULL, 0,
NULL, &outfp, NULL, &pid);
if (err)
{