From 5ad97e8fa62882b2014a67b4e57dab3b37ccaf4c Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Thu, 20 Oct 2022 14:23:20 +0900 Subject: gpgrt_spawn_process, gpgrt_spawn_process_fd: Change the API. * src/gpg-error.h.in (gpgrt_spawn_process): Remove PREEXEC argument. (gpgrt_spawn_process_fd): Add AFTER_FORK_CB and AFTER_FORK_CB_ARG. * src/gpgrt-int.h (_gpgrt_spawn_process): Remove PREEXEC argument. (_gpgrt_spawn_process_fd): Add AFTER_FORK_CB and AFTER_FORK_CB_ARG. * src/spawn-posix.c (do_exec): Remove PREEXEC argument. (_gpgrt_spawn_process): Remove PREEXEC argument. (_gpgrt_spawn_process_fd): Add AFTER_FORK_CB and AFTER_FORK_CB_ARG. (_gpgrt_spawn_process_detached): Follow the change of do_exec. * src/spawn-w32.c (_gpgrt_spawn_process): Remove PREEXEC argument. (_gpgrt_spawn_process_fd): Add AFTER_FORK_CB and AFTER_FORK_CB_ARG. * src/visibility.c (gpgrt_spawn_process): Remove PREEXEC argument. (gpgrt_spawn_process_fd): Add AFTER_FORK_CB and AFTER_FORK_CB_ARG. -- GnuPG-bug-id: 6249 Signed-off-by: NIIBE Yutaka --- src/gpg-error.h.in | 5 +++-- src/gpgrt-int.h | 13 ++++++++----- src/spawn-posix.c | 25 ++++++++++++------------- src/spawn-w32.c | 14 ++++++++------ src/visibility.c | 12 ++++++++---- 5 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/gpg-error.h.in b/src/gpg-error.h.in index 161a3ad..aca7243 100644 --- a/src/gpg-error.h.in +++ b/src/gpg-error.h.in @@ -1103,8 +1103,7 @@ gpg_err_code_t gpgrt_make_pipe (int filedes[2], gpgrt_stream_t *r_fp, /* Fork and exec PGMNAME. */ gpg_err_code_t gpgrt_spawn_process (const char *pgmname, const char *argv[], - int *execpt, void (*preexec)(void), - unsigned int flags, + int *execpt, unsigned int flags, gpgrt_stream_t *r_infp, gpgrt_stream_t *r_outfp, gpgrt_stream_t *r_errfp, @@ -1113,6 +1112,8 @@ gpg_err_code_t gpgrt_spawn_process (const char *pgmname, const char *argv[], /* Fork and exec PGNNAME and connect the process to the given FDs. */ gpg_err_code_t gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], int infd, int outfd, int errfd, + void (*after_fork_cb)(void *), + void *after_fork_cb_arg, pid_t *pid); /* Fork and exec PGMNAME as a detached process. */ diff --git a/src/gpgrt-int.h b/src/gpgrt-int.h index 78d5938..209e974 100644 --- a/src/gpgrt-int.h +++ b/src/gpgrt-int.h @@ -646,8 +646,6 @@ gpg_err_code_t _gpgrt_make_pipe (int filedes[2], estream_t *r_fp, * * The arguments for the process are expected in the NULL terminated * array ARGV. The program name itself should not be included there. - * If PREEXEC is not NULL, the given function will be called right - * before the exec. * * IF EXCEPT is not NULL, it is expected to be an ordered list of file * descriptors, terminated by an entry with the value (-1). These @@ -685,7 +683,7 @@ gpg_err_code_t _gpgrt_make_pipe (int filedes[2], estream_t *r_fp, */ gpg_err_code_t _gpgrt_spawn_process (const char *pgmname, const char *argv[], - int *execpt, void (*preexec)(void), unsigned int flags, + int *execpt, unsigned int flags, estream_t *r_infp, estream_t *r_outfp, estream_t *r_errfp, @@ -699,10 +697,15 @@ _gpgrt_spawn_process (const char *pgmname, const char *argv[], * terminated array ARGV. The program name itself should not be * included there. Calling gpgrt_wait_process and * gpgrt_release_process is required. Returns 0 on success or an - * error code. */ + * error code. + * If AFTER_FORK_CB is not NULL, the given function will be called + * right after the fork, by child process. + */ gpg_err_code_t _gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], int infd, int outfd, int errfd, + void (*after_fork_cb)(void *), + void *after_fork_cb_arg, pid_t *pid); /* Spawn a new process and immediately detach from it. The name of @@ -713,7 +716,7 @@ gpg_err_code_t _gpgrt_spawn_process_fd (const char *pgmname, * provide an absolute file name. */ gpg_err_code_t _gpgrt_spawn_process_detached (const char *pgmname, const char *argv[], - const char *envp[] ); + const char *envp[]); /* If HANG is true, waits for the process identified by PID to exit; * if HANG is false, checks whether the process has terminated. diff --git a/src/spawn-posix.c b/src/spawn-posix.c index eb4f534..3ac79ff 100644 --- a/src/spawn-posix.c +++ b/src/spawn-posix.c @@ -258,7 +258,7 @@ get_all_open_fds (void) static void do_exec (const char *pgmname, const char *argv[], int fd_in, int fd_out, int fd_err, - int *except, void (*preexec)(void), unsigned int flags) + int *except, unsigned int flags) { char **arg_list; int i, j; @@ -321,8 +321,6 @@ do_exec (const char *pgmname, const char *argv[], /* Close all other files. */ close_all_fds (3, except); - if (preexec) - preexec (); execv (pgmname, arg_list); /* No way to print anything, as we have may have closed all streams. */ _exit (127); @@ -403,7 +401,7 @@ _gpgrt_make_pipe (int filedes[2], estream_t *r_fp, int direction, /* Fork and exec the PGMNAME, see gpgrt-int.h for details. */ gpg_err_code_t _gpgrt_spawn_process (const char *pgmname, const char *argv[], - int *except, void (*preexec)(void), unsigned int flags, + int *except, unsigned int flags, estream_t *r_infp, estream_t *r_outfp, estream_t *r_errfp, @@ -506,13 +504,11 @@ _gpgrt_spawn_process (const char *pgmname, const char *argv[], if (!*pid) { /* This is the child. */ - /* FIXME: Needs to be done by preexec: - gcry_control (GCRYCTL_TERM_SECMEM); */ _gpgrt_fclose (infp); _gpgrt_fclose (outfp); _gpgrt_fclose (errfp); do_exec (pgmname, argv, inpipe[0], outpipe[1], errpipe[1], - except, preexec, flags); + except, flags); /*NOTREACHED*/ } @@ -538,7 +534,10 @@ _gpgrt_spawn_process (const char *pgmname, const char *argv[], /* Fork and exec the PGMNAME using FDs, see gpgrt-int.h for details. */ gpg_err_code_t _gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], - int infd, int outfd, int errfd, pid_t *pid) + int infd, int outfd, int errfd, + void (*after_fork_cb)(void *), + void *after_fork_cb_arg, + pid_t *pid) { gpg_error_t err; @@ -554,11 +553,11 @@ _gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], if (!*pid) { - /* FIXME: We need to add a preexec so that a - gcry_control (GCRYCTL_TERM_SECMEM); - can be done. */ + if (after_fork_cb) + (*after_fork_cb) (after_fork_cb_arg); + /* Run child. */ - do_exec (pgmname, argv, infd, outfd, errfd, NULL, NULL, 0); + do_exec (pgmname, argv, infd, outfd, errfd, NULL, 0); /*NOTREACHED*/ } @@ -858,7 +857,7 @@ _gpgrt_spawn_process_detached (const char *pgmname, const char *argv[], putenv (p); } - do_exec (pgmname, argv, -1, -1, -1, NULL, NULL, 0); + do_exec (pgmname, argv, -1, -1, -1, NULL, 0); /*NOTREACHED*/ } diff --git a/src/spawn-w32.c b/src/spawn-w32.c index 8fecfa6..963b6a5 100644 --- a/src/spawn-w32.c +++ b/src/spawn-w32.c @@ -386,7 +386,7 @@ _gpgrt_make_pipe (int filedes[2], estream_t *r_fp, int direction, int nonblock) /* Fork and exec the PGMNAME, see gpgrt-int.h for details. */ gpg_err_code_t _gpgrt_spawn_process (const char *pgmname, const char *argv[], - int *except, void (*preexec)(void), unsigned int flags, + int *except, unsigned int flags, estream_t *r_infp, estream_t *r_outfp, estream_t *r_errfp, pid_t *pid) { @@ -539,10 +539,6 @@ _gpgrt_spawn_process (const char *pgmname, const char *argv[], nullhd[2] = ((flags & GPGRT_SPAWN_KEEP_STDOUT)? GetStdHandle (STD_ERROR_HANDLE) : w32_open_null (1)); - /* Start the process. Note that we can't run the PREEXEC function - because this might change our own environment. */ - (void)preexec; - memset (&si, 0, sizeof si); si.cb = sizeof (si); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; @@ -645,7 +641,10 @@ _gpgrt_spawn_process (const char *pgmname, const char *argv[], /* Fork and exec the PGMNAME using FDs, see gpgrt-int.h for details. */ gpg_err_code_t _gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], - int infd, int outfd, int errfd, pid_t *pid) + int infd, int outfd, int errfd, + void (*after_fork_cb)(void *), + void *after_fork_cb_arg, + pid_t *pid) { gpg_err_code_t err; SECURITY_ATTRIBUTES sec_attr; @@ -655,6 +654,9 @@ _gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], int ret, i; HANDLE stdhd[3]; + (void)after_fork_cb; + (void)after_fork_cb_arg; + /* Setup return values. */ *pid = (pid_t)INVALID_HANDLE_VALUE; diff --git a/src/visibility.c b/src/visibility.c index fdf4805..831b418 100644 --- a/src/visibility.c +++ b/src/visibility.c @@ -1102,19 +1102,23 @@ gpgrt_make_pipe (int filedes[2], estream_t *r_fp, int direction, int nonblock) gpg_err_code_t gpgrt_spawn_process (const char *pgmname, const char *argv[], - int *except, void (*preexec)(void), unsigned int flags, + int *except, unsigned int flags, estream_t *r_infp, estream_t *r_outfp, estream_t *r_errfp, pid_t *pid) { - return _gpgrt_spawn_process (pgmname, argv, except, preexec, flags, + return _gpgrt_spawn_process (pgmname, argv, except, flags, r_infp, r_outfp, r_errfp, pid); } gpg_err_code_t gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], - int infd, int outfd, int errfd, pid_t *pid) + int infd, int outfd, int errfd, + void (*after_fork_cb)(void *), + void *after_fork_cb_arg, + pid_t *pid) { - return _gpgrt_spawn_process_fd (pgmname, argv, infd, outfd, errfd, pid); + return _gpgrt_spawn_process_fd (pgmname, argv, infd, outfd, errfd, + after_fork_cb, after_fork_cb_arg, pid); } gpg_err_code_t -- cgit v1.2.1