1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-01-03 12:11:33 +01:00

change the semantics of spawn_cb routine.

--

Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2022-11-17 15:16:03 +09:00
parent 729951f4c2
commit 4736debd01
No known key found for this signature in database
GPG Key ID: 640114AF89DE6054
4 changed files with 19 additions and 75 deletions

View File

@ -989,10 +989,12 @@ posix_open_null (int for_write)
static void
my_exec (const char *pgmname, const char *argv[],
int fd_in, int fd_out, int fd_err, int ask_inherit)
int fd_in, int fd_out, int fd_err,
int (*spawn_cb) (void *), void *spawn_cb_arg)
{
int i;
int fds[3];
int ask_inherit = 0;
fds[0] = fd_in;
fds[1] = fd_out;
@ -1013,6 +1015,9 @@ my_exec (const char *pgmname, const char *argv[],
close (fds[i]);
}
if (spawn_cb)
ask_inherit = (*spawn_cb) (spawn_cb_arg);
/* Close all other files. */
if (!ask_inherit)
close_all_fds (3, NULL);
@ -1025,7 +1030,7 @@ my_exec (const char *pgmname, const char *argv[],
static gpg_err_code_t
spawn_detached (gnupg_process_t process,
const char *pgmname, const char *argv[],
int (*spawn_cb) (struct spawn_cb_arg *), void *spawn_cb_arg)
int (*spawn_cb) (void *), void *spawn_cb_arg)
{
gpg_err_code_t ec;
pid_t pid;
@ -1061,14 +1066,6 @@ spawn_detached (gnupg_process_t process,
if (!pid)
{
pid_t pid2;
struct spawn_cb_arg arg;
int ask_inherit = 0;
arg.std_fds[0] = arg.std_fds[1] = arg.std_fds[2] = -1;
arg.arg = spawn_cb_arg;
if (spawn_cb)
ask_inherit = (*spawn_cb) (&arg);
if (setsid() == -1 || chdir ("/"))
_exit (1);
@ -1079,7 +1076,7 @@ spawn_detached (gnupg_process_t process,
if (pid2)
_exit (0); /* Let the parent exit immediately. */
my_exec (pgmname, argv, -1, -1, -1, ask_inherit);
my_exec (pgmname, argv, -1, -1, -1, spawn_cb, spawn_cb_arg);
/*NOTREACHED*/
}
@ -1107,8 +1104,7 @@ spawn_detached (gnupg_process_t process,
gpg_err_code_t
gnupg_process_spawn (const char *pgmname, const char *argv1[],
unsigned int flags,
int (*spawn_cb) (struct spawn_cb_arg *),
void *spawn_cb_arg,
int (*spawn_cb) (void *), void *spawn_cb_arg,
gnupg_process_t *r_process)
{
gpg_err_code_t ec;
@ -1281,14 +1277,6 @@ gnupg_process_spawn (const char *pgmname, const char *argv1[],
if (!pid)
{
struct spawn_cb_arg arg;
int ask_inherit = 0;
arg.std_fds[0] = fd_in[0];
arg.std_fds[1] = fd_out[1];
arg.std_fds[2] = fd_err[1];
arg.arg = spawn_cb_arg;
if (fd_in[1] >= 0)
close (fd_in[1]);
if (fd_out[0] >= 0)
@ -1296,11 +1284,9 @@ gnupg_process_spawn (const char *pgmname, const char *argv1[],
if (fd_err[0] >= 0)
close (fd_err[0]);
if (spawn_cb)
ask_inherit = (*spawn_cb) (&arg);
/* Run child. */
my_exec (pgmname, argv, fd_in[0], fd_out[1], fd_err[1], ask_inherit);
my_exec (pgmname, argv, fd_in[0], fd_out[1], fd_err[1],
spawn_cb, spawn_cb_arg);
/*NOTREACHED*/
}

View File

@ -1094,7 +1094,7 @@ post_syscall (void)
static gpg_err_code_t
spawn_detached (gnupg_process_t process,
const char *pgmname, char *cmdline,
int (*spawn_cb) (struct spawn_cb_arg *), void *spawn_cb_arg)
int (*spawn_cb) (void *), void *spawn_cb_arg)
{
SECURITY_ATTRIBUTES sec_attr;
PROCESS_INFORMATION pi = { NULL, 0, 0, 0 };
@ -1115,14 +1115,7 @@ spawn_detached (gnupg_process_t process,
}
if (spawn_cb)
{
struct spawn_cb_arg arg;
arg.std_fds[0] = arg.std_fds[1] = arg.std_fds[2] = -1;
arg.arg = spawn_cb_arg;
ask_inherit = (*spawn_cb) (&arg);
}
ask_inherit = (*spawn_cb) (spawn_cb_arg);
/* Prepare security attributes. */
memset (&sec_attr, 0, sizeof sec_attr );
@ -1201,7 +1194,7 @@ spawn_detached (gnupg_process_t process,
gpg_err_code_t
gnupg_process_spawn (const char *pgmname, const char *argv[],
unsigned int flags,
int (*spawn_cb) (struct spawn_cb_arg *),
int (*spawn_cb) (void *),
void *spawn_cb_arg,
gnupg_process_t *r_process)
{
@ -1332,14 +1325,7 @@ gnupg_process_spawn (const char *pgmname, const char *argv[],
}
if (spawn_cb)
{
struct spawn_cb_arg arg;
arg.std_fds[0] = arg.std_fds[1] = arg.std_fds[2] = -1;
arg.arg = spawn_cb_arg;
ask_inherit = (*spawn_cb) (&arg);
}
ask_inherit = (*spawn_cb) (spawn_cb_arg);
/* Prepare security attributes. */
memset (&sec_attr, 0, sizeof sec_attr );

View File

@ -210,10 +210,6 @@ gpg_error_t gnupg_spawn_process_detached (const char *pgmname,
/* The opaque type for a subprocess. */
typedef struct gnupg_process *gnupg_process_t;
struct spawn_cb_arg {
int std_fds[3];
void *arg;
};
/* Internal flag to ihnerit file descriptor/handle */
#define GNUPG_PROCESS_INHERIT_FILE (1 << 0)
@ -240,7 +236,7 @@ struct spawn_cb_arg {
/* Spawn PGMNAME. */
gpg_err_code_t gnupg_process_spawn (const char *pgmname, const char *argv[],
unsigned int flags,
int (*spawn_cb) (struct spawn_cb_arg *),
int (*spawn_cb) (void *),
void *spawn_cb_arg,
gnupg_process_t *r_process);

View File

@ -302,35 +302,11 @@ copy_buffer_flush (struct copy_buffer *c, estream_t sink)
static int
close_all_except (struct spawn_cb_arg *arg)
close_all_except (void *arg)
{
int except[32];
int i = 0;
int fd_in = arg->std_fds[0];
int fd_out = arg->std_fds[1];
int fd_err = arg->std_fds[2];
int *exceptclose = arg->arg;
int *exceptclose = arg;
if (fd_in >= 0)
except[i++] = fd_in;
if (fd_out >= 0)
except[i++] = fd_out;
if (fd_err >= 0)
except[i++] = fd_err;
for (; i < 31; i++)
{
int fd = *exceptclose++;
if (fd < 0)
break;
else
except[i] = fd;
}
except[i++] = -1;
close_all_fds (3, except);
close_all_fds (3, exceptclose);
return 1;
}