From 6c20e8393eba4a9f330143b2158e28ea594cbadd Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Mon, 31 Oct 2022 11:07:57 +0900 Subject: spawn: Fix spawn_cb of gpgrt_spawn_process_fd. * src/gpg-error.def.in (gpgrt_close_all_fds): New. * src/gpg-error.vers (gpgrt_close_all_fds): New. * src/gpg-error.h.in (GPGRT_SPAWN_INHERIT_FILE): New. (gpgrt_spawn_process_fd): SPAWN_CB having return value. * src/gpgrt-int.h (_gpgrt_spawn_process_fd): SPAWN_CB change. * src/spawn-posix.c (_gpgrt_close_all_fds): Rename from close_all_fds, and export it. (do_exec): Support the case not closing fds. (_gpgrt_spawn_process_fd): Handle return value of SPAWN_CB to determine closing all fds or not. * src/spawn-w32.c (_gpgrt_spawn_process_fd): Run SPAWN_CB. (_gpgrt_close_all_fds): New. * src/visibility.c (gpgrt_close_all_fds): New. * src/visibility.h (gpgrt_close_all_fds): New. -- Now, we have the API of gpgrt_spawn_process_fd for POSIX and Windows. Giving portable semantics for spawning a process is difficult, and it is still difficult for users to writing a function for SPAWN_CB with portability, but gpgrt_spawn_process_fd gives the feature of spawning a process. GnuPG-bug-id: 6249 Signed-off-by: NIIBE Yutaka --- src/gpgrt-int.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src/gpgrt-int.h') diff --git a/src/gpgrt-int.h b/src/gpgrt-int.h index b95b44c..c414272 100644 --- a/src/gpgrt-int.h +++ b/src/gpgrt-int.h @@ -696,15 +696,19 @@ _gpgrt_spawn_process (const char *pgmname, const char *argv[], * The arguments for the process are expected in the NULL 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. If AFTER_FORK_CB is not - * NULL, the given function will be called right after the fork, by - * child process. + * Returns 0 on success or an error code. If SPAWN_CB is not NULL, + * the given function will be called with SPAWN_CB_ARG to determine if + * file descriptors/handles should be inherited or not. The callback + * function should return 1 to ask keeping file descriptors/handles. + * If SPAWN_CB is NULL, or it returns 0, all file descriptors (except + * INFD, OUTFD, and ERRFD) will be closed on POSIX machine. On POSIX + * machine, it is 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, + int (*spawn_cb) (void *), + void *spawn_cb_arg, pid_t *pid); /* Spawn a new process and immediately detach from it. The name of @@ -758,6 +762,9 @@ void _gpgrt_kill_process (pid_t pid); * It is a nop if PID is invalid. */ void _gpgrt_release_process (pid_t pid); +/* Close all file resources (descriptors), except KEEP_FDS. */ +void _gpgrt_close_all_fds (int from, int *keep_fds); + /* * Local prototypes for argparse. -- cgit v1.2.3