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

posix: factor out call_spawn_cb.

--

Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2022-11-30 10:03:29 +09:00
parent 236a8a3cfb
commit f9af139685
No known key found for this signature in database
GPG Key ID: 640114AF89DE6054

View File

@ -988,42 +988,45 @@ posix_open_null (int for_write)
} }
static void static void
my_exec (const char *pgmname, const char *argv[], call_spawn_cb (struct spawn_cb_arg *sca,
int fd_in, int fd_out, int fd_err, int fd_in, int fd_out, int fd_err,
void (*spawn_cb) (struct spawn_cb_arg *), void *spawn_cb_arg) void (*spawn_cb) (struct spawn_cb_arg *), void *spawn_cb_arg)
{ {
int i; sca->fds[0] = fd_in;
struct spawn_cb_arg sca; sca->fds[1] = fd_out;
sca->fds[2] = fd_err;
sca.fds[0] = fd_in; sca->except_fds = NULL;
sca.fds[1] = fd_out; sca->arg = spawn_cb_arg;
sca.fds[2] = fd_err;
sca.except_fds = NULL;
sca.arg = spawn_cb_arg;
if (spawn_cb) if (spawn_cb)
(*spawn_cb) (&sca); (*spawn_cb) (sca);
}
static void
my_exec (const char *pgmname, const char *argv[], struct spawn_cb_arg *sca)
{
int i;
/* Assign /dev/null to unused FDs. */ /* Assign /dev/null to unused FDs. */
for (i = 0; i <= 2; i++) for (i = 0; i <= 2; i++)
if (sca.fds[i] == -1) if (sca->fds[i] == -1)
sca.fds[i] = posix_open_null (i); sca->fds[i] = posix_open_null (i);
/* Connect the standard files. */ /* Connect the standard files. */
for (i = 0; i <= 2; i++) for (i = 0; i <= 2; i++)
if (sca.fds[i] != i) if (sca->fds[i] != i)
{ {
if (dup2 (sca.fds[i], i) == -1) if (dup2 (sca->fds[i], i) == -1)
log_fatal ("dup2 std%s failed: %s\n", log_fatal ("dup2 std%s failed: %s\n",
i==0?"in":i==1?"out":"err", strerror (errno)); i==0?"in":i==1?"out":"err", strerror (errno));
/* /*
* We don't close sca.fds[i] here, but close them by * We don't close sca.fds[i] here, but close them by
* close_all_fds. Note that there may be same one in three of * close_all_fds. Note that there may be same one in three of
* sca.fds[i]. * sca->fds[i].
*/ */
} }
/* Close all other files. */ /* Close all other files. */
close_all_fds (3, sca.except_fds); close_all_fds (3, sca->except_fds);
execv (pgmname, (char *const *)argv); execv (pgmname, (char *const *)argv);
/* No way to print anything, as we have may have closed all streams. */ /* No way to print anything, as we have may have closed all streams. */
@ -1069,6 +1072,7 @@ spawn_detached (gnupg_process_t process,
if (!pid) if (!pid)
{ {
pid_t pid2; pid_t pid2;
struct spawn_cb_arg sca;
if (setsid() == -1 || chdir ("/")) if (setsid() == -1 || chdir ("/"))
_exit (1); _exit (1);
@ -1079,7 +1083,9 @@ spawn_detached (gnupg_process_t process,
if (pid2) if (pid2)
_exit (0); /* Let the parent exit immediately. */ _exit (0); /* Let the parent exit immediately. */
my_exec (pgmname, argv, -1, -1, -1, spawn_cb, spawn_cb_arg); call_spawn_cb (&sca, -1, -1, -1, spawn_cb, spawn_cb_arg);
my_exec (pgmname, argv, &sca);
/*NOTREACHED*/ /*NOTREACHED*/
} }
@ -1282,6 +1288,8 @@ gnupg_process_spawn (const char *pgmname, const char *argv1[],
if (!pid) if (!pid)
{ {
struct spawn_cb_arg sca;
if (fd_in[1] >= 0) if (fd_in[1] >= 0)
close (fd_in[1]); close (fd_in[1]);
if (fd_out[0] >= 0) if (fd_out[0] >= 0)
@ -1289,9 +1297,11 @@ gnupg_process_spawn (const char *pgmname, const char *argv1[],
if (fd_err[0] >= 0) if (fd_err[0] >= 0)
close (fd_err[0]); close (fd_err[0]);
/* Run child. */ call_spawn_cb (&sca, fd_in[0], fd_out[1], fd_err[1],
my_exec (pgmname, argv, fd_in[0], fd_out[1], fd_err[1],
spawn_cb, spawn_cb_arg); spawn_cb, spawn_cb_arg);
/* Run child. */
my_exec (pgmname, argv, &sca);
/*NOTREACHED*/ /*NOTREACHED*/
} }