From 4736debd01ecfc0f77f99a257caea174201310fa Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Thu, 17 Nov 2022 15:16:03 +0900 Subject: [PATCH] change the semantics of spawn_cb routine. -- Signed-off-by: NIIBE Yutaka --- common/exechelp-posix.c | 36 +++++++++++------------------------- common/exechelp-w32.c | 22 ++++------------------ common/exechelp.h | 6 +----- common/exectool.c | 30 +++--------------------------- 4 files changed, 19 insertions(+), 75 deletions(-) diff --git a/common/exechelp-posix.c b/common/exechelp-posix.c index e3f8a15f4..0c09abbf4 100644 --- a/common/exechelp-posix.c +++ b/common/exechelp-posix.c @@ -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*/ } diff --git a/common/exechelp-w32.c b/common/exechelp-w32.c index 587c96af2..14cf84ce5 100644 --- a/common/exechelp-w32.c +++ b/common/exechelp-w32.c @@ -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 ); diff --git a/common/exechelp.h b/common/exechelp.h index df0952e7d..69b085fca 100644 --- a/common/exechelp.h +++ b/common/exechelp.h @@ -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); diff --git a/common/exectool.c b/common/exectool.c index 8eb68acaf..a09747d88 100644 --- a/common/exectool.c +++ b/common/exectool.c @@ -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; }