1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-03-11 22:52:47 +01:00

Replace pipe+gnupg_spawn_process_fd by gnupg_process_spawn.

--

For g13.

Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2022-11-25 10:57:11 +09:00
parent 2227411d77
commit d579fa1b04
No known key found for this signature in database
GPG Key ID: 640114AF89DE6054
5 changed files with 45 additions and 56 deletions

View File

@ -28,10 +28,10 @@
#include "g13.h" #include "g13.h"
#include "../common/i18n.h" #include "../common/i18n.h"
#include "keyblob.h" #include "keyblob.h"
#include "be-encfs.h"
#include "runner.h"
#include "../common/sysutils.h" #include "../common/sysutils.h"
#include "../common/exechelp.h" #include "../common/exechelp.h"
#include "runner.h"
#include "be-encfs.h"
/* Command values used to run the encfs tool. */ /* Command values used to run the encfs tool. */
@ -218,12 +218,11 @@ run_encfs_tool (ctrl_t ctrl, enum encfs_cmds cmd,
gpg_error_t err; gpg_error_t err;
encfs_parm_t parm; encfs_parm_t parm;
runner_t runner = NULL; runner_t runner = NULL;
int outbound[2] = { -1, -1 };
int inbound[2] = { -1, -1 };
const char *pgmname; const char *pgmname;
const char *argv[10]; const char *argv[10];
pid_t pid = (pid_t)(-1);
int idx; int idx;
gnupg_process_t proc;
int inbound, outbound;
(void)ctrl; (void)ctrl;
@ -246,15 +245,6 @@ run_encfs_tool (ctrl_t ctrl, enum encfs_cmds cmd,
if (err) if (err)
goto leave; goto leave;
err = gnupg_create_inbound_pipe (inbound, NULL, 0);
if (!err)
err = gnupg_create_outbound_pipe (outbound, NULL, 0);
if (err)
{
log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
goto leave;
}
pgmname = ENCFS; pgmname = ENCFS;
idx = 0; idx = 0;
argv[idx++] = "-f"; argv[idx++] = "-f";
@ -267,47 +257,42 @@ run_encfs_tool (ctrl_t ctrl, enum encfs_cmds cmd,
argv[idx++] = NULL; argv[idx++] = NULL;
assert (idx <= DIM (argv)); assert (idx <= DIM (argv));
err = gnupg_spawn_process_fd (pgmname, argv, err = gnupg_process_spawn (pgmname, argv,
outbound[0], -1, inbound[1], &pid); (GNUPG_PROCESS_STDIN_PIPE | GNUPG_PROCESS_STDOUT_NULL
| GNUPG_PROCESS_STDERR_PIPE),
NULL, NULL, &proc);
if (err) if (err)
{ {
log_error ("error spawning '%s': %s\n", pgmname, gpg_strerror (err)); log_error ("error spawning '%s': %s\n", pgmname, gpg_strerror (err));
goto leave; goto leave;
} }
close (outbound[0]); outbound[0] = -1;
close ( inbound[1]); inbound[1] = -1;
runner_set_fds (runner, inbound[0], outbound[1]); err = gnupg_process_get_fds (proc, 0, &outbound, NULL, &inbound);
inbound[0] = -1; /* Now owned by RUNNER. */ if (err)
outbound[1] = -1; /* Now owned by RUNNER. */ {
log_error ("error get fds '%s': %s\n", pgmname, gpg_strerror (err));
gnupg_process_release (proc);
goto leave;
}
runner_set_fds (runner, inbound, outbound);
runner_set_handler (runner, encfs_handler, encfs_handler_cleanup, parm); runner_set_handler (runner, encfs_handler, encfs_handler_cleanup, parm);
parm = NULL; /* Now owned by RUNNER. */ parm = NULL; /* Now owned by RUNNER. */
runner_set_pid (runner, pid); runner_set_proc (runner, proc);
pid = (pid_t)(-1); /* The process is now owned by RUNNER. */
err = runner_spawn (runner); err = runner_spawn (runner);
if (err) if (err)
goto leave; {
gnupg_process_release (proc);
goto leave;
}
*r_id = runner_get_rid (runner); *r_id = runner_get_rid (runner);
log_info ("running '%s' in the background\n", pgmname); log_info ("running '%s' in the background\n", pgmname);
leave: leave:
if (inbound[0] != -1)
close (inbound[0]);
if (inbound[1] != -1)
close (inbound[1]);
if (outbound[0] != -1)
close (outbound[0]);
if (outbound[1] != -1)
close (outbound[1]);
if (pid != (pid_t)(-1))
{
gnupg_wait_process (pgmname, pid, 1, NULL);
gnupg_release_process (pid);
}
runner_release (runner); runner_release (runner);
encfs_handler_cleanup (parm); encfs_handler_cleanup (parm);
return err; return err;

View File

@ -40,6 +40,7 @@
#include "../common/gc-opt-flags.h" #include "../common/gc-opt-flags.h"
#include "../common/asshelp.h" #include "../common/asshelp.h"
#include "../common/init.h" #include "../common/init.h"
#include "../common/exechelp.h"
#include "keyblob.h" #include "keyblob.h"
#include "server.h" #include "server.h"
#include "runner.h" #include "runner.h"

View File

@ -34,10 +34,11 @@
#include "backend.h" #include "backend.h"
#include "g13tuple.h" #include "g13tuple.h"
#include "mountinfo.h" #include "mountinfo.h"
#include "runner.h"
#include "../common/host2net.h" #include "../common/host2net.h"
#include "server.h" /*(g13_keyblob_decrypt)*/ #include "server.h" /*(g13_keyblob_decrypt)*/
#include "../common/sysutils.h" #include "../common/sysutils.h"
#include "../common/exechelp.h"
#include "runner.h"
#include "call-syshelp.h" #include "call-syshelp.h"

View File

@ -29,8 +29,8 @@
#include "g13.h" #include "g13.h"
#include "../common/i18n.h" #include "../common/i18n.h"
#include "keyblob.h" #include "keyblob.h"
#include "runner.h"
#include "../common/exechelp.h" #include "../common/exechelp.h"
#include "runner.h"
#include "mountinfo.h" #include "mountinfo.h"
/* The runner object. */ /* The runner object. */
@ -55,7 +55,7 @@ struct runner_s
2 = Thread is running and someone is holding a reference. */ 2 = Thread is running and someone is holding a reference. */
int refcount; int refcount;
pid_t pid; /* PID of the backend's process (the engine). */ gnupg_process_t proc; /* Process of the backend's process (the engine). */
int in_fd; /* File descriptors to read from the engine. */ int in_fd; /* File descriptors to read from the engine. */
int out_fd; /* File descriptors to write to the engine. */ int out_fd; /* File descriptors to write to the engine. */
engine_handler_fnc_t handler; /* The handler functions. */ engine_handler_fnc_t handler; /* The handler functions. */
@ -157,16 +157,16 @@ runner_release (runner_t runner)
if (runner->handler_cleanup) if (runner->handler_cleanup)
runner->handler_cleanup (runner->handler_data); runner->handler_cleanup (runner->handler_data);
if (runner->pid != (pid_t)(-1)) if (runner->proc)
{ {
/* The process has not been cleaned up - do it now. */ /* The process has not been cleaned up - do it now. */
gnupg_kill_process (runner->pid); gnupg_process_terminate (runner->proc);
/* (Actually we should use the program name and not the /* (Actually we should use the program name and not the
arbitrary NAME of the runner object. However it does not arbitrary NAME of the runner object. However it does not
matter because that information is only used for matter because that information is only used for
diagnostics.) */ diagnostics.) */
gnupg_wait_process (runner->name, runner->pid, 1, NULL); gnupg_process_wait (runner->proc, 1);
gnupg_release_process (runner->pid); gnupg_process_release (runner->proc);
} }
xfree (runner->name); xfree (runner->name);
@ -212,7 +212,7 @@ runner_new (runner_t *r_runner, const char *name)
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
} }
runner->refcount = 1; runner->refcount = 1;
runner->pid = (pid_t)(-1); runner->proc = NULL;
runner->in_fd = -1; runner->in_fd = -1;
runner->out_fd = -1; runner->out_fd = -1;
@ -266,15 +266,15 @@ runner_set_fds (runner_t runner, int in_fd, int out_fd)
} }
/* Set the PID of the backend engine. After this call the engine is /* Set the PROC of the backend engine. After this call the engine is
owned by the runner object. */ owned by the runner object. */
void void
runner_set_pid (runner_t runner, pid_t pid) runner_set_proc (runner_t runner, gnupg_process_t proc)
{ {
if (check_already_spawned (runner, "runner_set_fds")) if (check_already_spawned (runner, "runner_set_proc"))
return; return;
runner->pid = pid; runner->proc = proc;
} }
@ -366,15 +366,17 @@ runner_thread (void *arg)
} }
/* Now wait for the process to finish. */ /* Now wait for the process to finish. */
if (!err && runner->pid != (pid_t)(-1)) if (!err && runner->proc)
{ {
int exitcode; int exitcode;
log_debug ("runner thread waiting ...\n"); log_debug ("runner thread waiting ...\n");
err = gnupg_wait_process (runner->name, runner->pid, 1, &exitcode); err = gnupg_process_wait (runner->proc, 1);
gnupg_release_process (runner->pid); if (!err)
runner->pid = (pid_t)(-1); gnupg_process_ctl (runner->proc, GNUPG_PROCESS_GET_EXIT_ID, &exitcode);
if (err) gnupg_process_release (runner->proc);
runner->proc = NULL;
if (exitcode)
log_error ("running '%s' failed (exitcode=%d): %s\n", log_error ("running '%s' failed (exitcode=%d): %s\n",
runner->name, exitcode, gpg_strerror (err)); runner->name, exitcode, gpg_strerror (err));
log_debug ("runner thread waiting finished\n"); log_debug ("runner thread waiting finished\n");
@ -473,7 +475,7 @@ runner_cancel (runner_t runner)
need to change the thread to wait on an event. */ need to change the thread to wait on an event. */
runner->cancel_flag = 1; runner->cancel_flag = 1;
/* For now we use the brutal way and kill the process. */ /* For now we use the brutal way and kill the process. */
gnupg_kill_process (runner->pid); gnupg_process_terminate (runner->proc);
} }
} }

View File

@ -49,7 +49,7 @@ runner_t runner_find_by_rid (unsigned int rid);
/* Functions to set properties of the runner. */ /* Functions to set properties of the runner. */
void runner_set_fds (runner_t runner, int in_fd, int out_fd); void runner_set_fds (runner_t runner, int in_fd, int out_fd);
void runner_set_pid (runner_t runner, pid_t pid); void runner_set_proc (runner_t runner, gnupg_process_t proc);
/* Register the handler functions with a runner. */ /* Register the handler functions with a runner. */
void runner_set_handler (runner_t runner, void runner_set_handler (runner_t runner,