diff options
author | NIIBE Yutaka <[email protected]> | 2024-06-10 05:09:12 +0000 |
---|---|---|
committer | NIIBE Yutaka <[email protected]> | 2024-06-10 05:09:12 +0000 |
commit | 3fa943e30f8983613568460281e62a13580015d7 (patch) | |
tree | a115d3beec4ce8697a2a706c3d744485b4449d55 | |
parent | Cleanup for spawn use from libgpg-error. (diff) | |
download | libassuan-3fa943e30f8983613568460281e62a13580015d7.tar.gz libassuan-3fa943e30f8983613568460281e62a13580015d7.zip |
Remove use of _assuan_pipe.
Signed-off-by: NIIBE Yutaka <[email protected]>
-rw-r--r-- | src/assuan-defs.h | 16 | ||||
-rw-r--r-- | src/assuan-pipe-connect.c | 115 | ||||
-rw-r--r-- | src/libassuan.def | 2 | ||||
-rw-r--r-- | src/libassuan.vers | 2 | ||||
-rw-r--r-- | src/system-posix.c | 9 | ||||
-rw-r--r-- | src/system-w32.c | 51 | ||||
-rw-r--r-- | src/system.c | 107 |
7 files changed, 87 insertions, 215 deletions
diff --git a/src/assuan-defs.h b/src/assuan-defs.h index f0740c6..f881413 100644 --- a/src/assuan-defs.h +++ b/src/assuan-defs.h @@ -297,7 +297,6 @@ void _assuan_free (assuan_context_t ctx, void *ptr); /* System hooks. */ void _assuan_usleep (assuan_context_t ctx, unsigned int usec); -int _assuan_pipe (assuan_context_t ctx, assuan_fd_t fd[2], int inherit_idx); int _assuan_close (assuan_context_t ctx, assuan_fd_t fd); int _assuan_close_inheritable (assuan_context_t ctx, assuan_fd_t fd); ssize_t _assuan_read (assuan_context_t ctx, assuan_fd_t fd, void *buffer, @@ -308,13 +307,6 @@ int _assuan_recvmsg (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg, int flags); int _assuan_sendmsg (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg, int flags); -int _assuan_spawn (assuan_context_t ctx, const char *name, const char *argv[], - assuan_fd_t fd_in, assuan_fd_t fd_out, - assuan_fd_t *fd_child_list, - void (*atfork) (void *opaque), - void *atforkvalue, unsigned int flags); -assuan_pid_t _assuan_waitpid (assuan_context_t ctx, assuan_pid_t pid, - int nowait, int *status, int options); int _assuan_socketpair (assuan_context_t ctx, int namespace, int style, int protocol, assuan_fd_t filedes[2]); assuan_fd_t _assuan_socket (assuan_context_t ctx, int namespace, @@ -323,13 +315,7 @@ int _assuan_connect (assuan_context_t ctx, assuan_fd_t sock, struct sockaddr *addr, socklen_t length); void __assuan_usleep (assuan_context_t ctx, unsigned int usec); -int __assuan_pipe (assuan_context_t ctx, assuan_fd_t fd[2], int inherit_idx); int __assuan_close (assuan_context_t ctx, assuan_fd_t fd); -int __assuan_spawn (assuan_context_t ctx, assuan_pid_t *r_pid, const char *name, - const char **argv, assuan_fd_t fd_in, assuan_fd_t fd_out, - assuan_fd_t *fd_child_list, - void (*atfork) (void *opaque, int reserved), - void *atforkvalue, unsigned int flags); int __assuan_socketpair (assuan_context_t ctx, int _namespace, int style, int protocol, assuan_fd_t filedes[2]); assuan_fd_t __assuan_socket (assuan_context_t ctx, int _namespace, @@ -344,8 +330,6 @@ int __assuan_recvmsg (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg, int flags); int __assuan_sendmsg (assuan_context_t ctx, assuan_fd_t fd, const assuan_msghdr_t msg, int flags); -assuan_pid_t __assuan_waitpid (assuan_context_t ctx, assuan_pid_t pid, - int nowait, int *status, int options); /*-- assuan-pipe-server.c --*/ void _assuan_release_context (assuan_context_t ctx); diff --git a/src/assuan-pipe-connect.c b/src/assuan-pipe-connect.c index ec2800f..a09ca0c 100644 --- a/src/assuan-pipe-connect.c +++ b/src/assuan-pipe-connect.c @@ -174,6 +174,86 @@ at_pipe_fork_cb (void *opaque) } +static int +my_spawn (assuan_context_t ctx, const char *name, const char **argv, + assuan_fd_t *fd_child_list, void (*atfork) (void *opaque), + void *atforkvalue, unsigned int spawn_flags) +{ + int i; + gpgrt_spawn_actions_t act = NULL; + gpg_err_code_t ec; + gpgrt_process_t proc = NULL; + int keep_stderr = 0; + assuan_fd_t *fdp; + + TRACE_BEG4 (ctx, ASSUAN_LOG_CTX, "my_spawn", ctx, + "name=%s,atfork=%p,atforkvalue=%p,flags=%i", + name ? name : "(null)", + atfork, atforkvalue, spawn_flags); + + if (name) + { + i = 0; + while (argv[i]) + { + TRACE_LOG2 ("argv[%2i] = %s", i, argv[i]); + i++; + } + } + i = 0; + if (fd_child_list) + { + while (fd_child_list[i] != ASSUAN_INVALID_FD) + { + TRACE_LOG2 ("fd_child_list[%2i] = 0x%x", i, fd_child_list[i]); + i++; + } + } + + if (fd_child_list) + { + for (fdp = fd_child_list; *fdp != ASSUAN_INVALID_FD; fdp++) + if (*fdp == (assuan_fd_t)STDERR_FILENO) + { + keep_stderr = 1; + break; + } + } + if (keep_stderr) + spawn_flags |= GPGRT_PROCESS_STDERR_KEEP; + + ec = gpgrt_spawn_actions_new (&act); + if (ec) + return -1; + +#ifdef HAVE_W32_SYSTEM + gpgrt_spawn_actions_set_inherit_handles (act, fd_child_list); +#else + gpgrt_spawn_actions_set_inherit_fds (act, fd_child_list); + gpgrt_spawn_actions_set_atfork (act, atfork, atforkvalue); +#endif + ec = gpgrt_process_spawn (name, argv+1, spawn_flags, act, &proc); + gpgrt_spawn_actions_release (act); + if (ec) + return -1; + ctx->server_proc = proc; + + if (proc) + { +#ifdef HAVE_W32_SYSTEM + HANDLE inbound, outbound; + ec = gpgrt_process_ctl (proc, GPGRT_PROCESS_GET_HANDLES, + &inbound, &outbound, NULL); +#else + int inbound, outbound; + ec = gpgrt_process_get_fds (proc, 0, &inbound, &outbound, NULL); +#endif + ctx->inbound.fd = outbound; + ctx->outbound.fd = inbound; + } + return TRACE_SYSERR (0); +} + static gpg_error_t pipe_connect (assuan_context_t ctx, const char *name, const char **argv, @@ -182,8 +262,6 @@ pipe_connect (assuan_context_t ctx, void *atforkvalue, unsigned int flags) { gpg_error_t rc; - assuan_fd_t rp[2]; - assuan_fd_t wp[2]; int res; struct at_pipe_fork atp; unsigned int spawn_flags; @@ -198,30 +276,14 @@ pipe_connect (assuan_context_t ctx, if (! ctx->flags.no_fixsignals) fix_signals (); - if (_assuan_pipe (ctx, rp, 1) < 0) - return _assuan_error (ctx, gpg_err_code_from_syserror ()); - - if (_assuan_pipe (ctx, wp, 0) < 0) - { - _assuan_close (ctx, rp[0]); - _assuan_close_inheritable (ctx, rp[1]); - return _assuan_error (ctx, gpg_err_code_from_syserror ()); - } - - spawn_flags = 0; + spawn_flags = GPGRT_PROCESS_STDIN_PIPE|GPGRT_PROCESS_STDOUT_PIPE; if (flags & ASSUAN_PIPE_CONNECT_DETACHED) - spawn_flags |= ASSUAN_SPAWN_DETACHED; + spawn_flags |= GPGRT_PROCESS_NO_CONSOLE; - /* FIXME: Use atfork handler that closes child fds on Unix. */ - res = _assuan_spawn (ctx, name, argv, wp[0], rp[1], - fd_child_list, at_pipe_fork_cb, &atp, spawn_flags); + res = my_spawn (ctx, name, argv, fd_child_list, at_pipe_fork_cb, &atp, spawn_flags); if (res < 0) { rc = gpg_err_code_from_syserror (); - _assuan_close (ctx, rp[0]); - _assuan_close_inheritable (ctx, rp[1]); - _assuan_close_inheritable (ctx, wp[0]); - _assuan_close (ctx, wp[1]); return _assuan_error (ctx, rc); } @@ -239,10 +301,6 @@ pipe_connect (assuan_context_t ctx, argv[0] = "client"; } - /* Close the stdin/stdout child fds in the parent. */ - _assuan_close_inheritable (ctx, rp[1]); - _assuan_close_inheritable (ctx, wp[0]); - ctx->engine.release = _assuan_client_release; ctx->engine.readfnc = _assuan_simple_read; ctx->engine.writefnc = _assuan_simple_write; @@ -255,8 +313,6 @@ pipe_connect (assuan_context_t ctx, ctx->finish_handler = _assuan_client_finish; ctx->max_accepts = 1; ctx->accept_handler = NULL; - ctx->inbound.fd = rp[0]; /* Our inbound is read end of read pipe. */ - ctx->outbound.fd = wp[1]; /* Our outbound is write end of write pipe. */ rc = initial_handshake (ctx); if (rc) @@ -362,9 +418,8 @@ socketpair_connect (assuan_context_t ctx, const char *name, const char **argv, atp.peer_fd = fds[1]; child_fds[0] = fds[1]; - rc = _assuan_spawn (ctx, name, argv, ASSUAN_INVALID_FD, - ASSUAN_INVALID_FD, child_fds, at_socketpair_fork_cb, - &atp, 0); + rc = my_spawn (ctx, name, argv, child_fds, at_socketpair_fork_cb, + &atp, 0); if (rc < 0) { err = gpg_err_code_from_syserror (); diff --git a/src/libassuan.def b/src/libassuan.def index 235d01b..d00af57 100644 --- a/src/libassuan.def +++ b/src/libassuan.def @@ -89,7 +89,7 @@ EXPORTS assuan_write_line @68 assuan_write_status @69 __assuan_close @70 - __assuan_pipe @71 +; __assuan_pipe @71 __assuan_socketpair @72 ; __assuan_spawn @73 __assuan_usleep @74 diff --git a/src/libassuan.vers b/src/libassuan.vers index 6839db2..a1b2ef8 100644 --- a/src/libassuan.vers +++ b/src/libassuan.vers @@ -111,7 +111,7 @@ LIBASSUAN_1.0 { assuan_control; __assuan_close; - __assuan_pipe; +# __assuan_pipe; __assuan_socketpair; # __assuan_spawn; __assuan_usleep; diff --git a/src/system-posix.c b/src/system-posix.c index 780906d..e8c62fc 100644 --- a/src/system-posix.c +++ b/src/system-posix.c @@ -87,15 +87,6 @@ __assuan_usleep (assuan_context_t ctx, unsigned int usec) -/* Create a pipe with one inheritable end. Easy for Posix. */ -int -__assuan_pipe (assuan_context_t ctx, assuan_fd_t fd[2], int inherit_idx) -{ - return pipe (fd); -} - - - /* Close the given file descriptor, created with _assuan_pipe or one of the socket functions. Easy for Posix. */ int diff --git a/src/system-w32.c b/src/system-w32.c index 9248edf..ca26351 100644 --- a/src/system-w32.c +++ b/src/system-w32.c @@ -64,57 +64,6 @@ __assuan_usleep (assuan_context_t ctx, unsigned int usec) -/* Create a pipe with one inheritable end. Default implementation. */ -int -__assuan_pipe (assuan_context_t ctx, assuan_fd_t fd[2], int inherit_idx) -{ - HANDLE rh; - HANDLE wh; - HANDLE th; - SECURITY_ATTRIBUTES sec_attr; - - memset (&sec_attr, 0, sizeof (sec_attr)); - sec_attr.nLength = sizeof (sec_attr); - sec_attr.bInheritHandle = FALSE; - - if (!CreatePipe (&rh, &wh, &sec_attr, 0)) - { - TRACE1 (ctx, ASSUAN_LOG_SYSIO, "__assuan_pipe", ctx, - "CreatePipe failed: %s", _assuan_w32_strerror (ctx, -1)); - gpg_err_set_errno (EIO); - return -1; - } - - if (! DuplicateHandle (GetCurrentProcess(), (inherit_idx == 0) ? rh : wh, - GetCurrentProcess(), &th, 0, - TRUE, DUPLICATE_SAME_ACCESS )) - { - TRACE1 (ctx, ASSUAN_LOG_SYSIO, "__assuan_pipe", ctx, - "DuplicateHandle failed: %s", _assuan_w32_strerror (ctx, -1)); - CloseHandle (rh); - CloseHandle (wh); - gpg_err_set_errno (EIO); - return -1; - } - if (inherit_idx == 0) - { - CloseHandle (rh); - rh = th; - } - else - { - CloseHandle (wh); - wh = th; - } - - fd[0] = rh; - fd[1] = wh; - - return 0; -} - - - /* Close the given file descriptor, created with _assuan_pipe or one of the socket functions. Default implementation. */ int diff --git a/src/system.c b/src/system.c index 0862681..11fd698 100644 --- a/src/system.c +++ b/src/system.c @@ -111,24 +111,6 @@ _assuan_usleep (assuan_context_t ctx, unsigned int usec) -/* Create a pipe with one inheritable end. */ -int -_assuan_pipe (assuan_context_t ctx, assuan_fd_t fd[2], int inherit_idx) -{ - int err; - TRACE_BEG2 (ctx, ASSUAN_LOG_SYSIO, "_assuan_pipe", ctx, - "inherit_idx=%i (Assuan uses it for %s)", - inherit_idx, inherit_idx ? "reading" : "writing"); - - err = __assuan_pipe (ctx, fd, inherit_idx); - if (err) - return TRACE_SYSRES (err); - - return TRACE_SUC2 ("read=0x%x, write=0x%x", fd[0], fd[1]); -} - - - /* Close the given file descriptor, created with _assuan_pipe or one of the socket functions. */ int @@ -287,95 +269,6 @@ _assuan_sendmsg (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg, return res; #endif } - - - -/* Create a new process from NAME and ARGV. Provide FD_IN and FD_OUT - as stdin and stdout. Inherit the ASSUAN_INVALID_FD-terminated - FD_CHILD_LIST as given (no remapping), which must be inheritable. - On Unix, call ATFORK with ATFORKVALUE after fork and before exec. */ -int -_assuan_spawn (assuan_context_t ctx, const char *name, const char **argv, - assuan_fd_t fd_in, assuan_fd_t fd_out, - assuan_fd_t *fd_child_list, - void (*atfork) (void *opaque), - void *atforkvalue, unsigned int flags) -{ - int res; - int i; - gpgrt_spawn_actions_t act = NULL; - unsigned int spawn_flags; - gpg_err_code_t ec; - gpgrt_process_t proc = NULL; - int keep_stderr = 0; - assuan_fd_t *fdp; - - TRACE_BEG6 (ctx, ASSUAN_LOG_CTX, "_assuan_spawn", ctx, - "name=%s,fd_in=0x%x,fd_out=0x%x," - "atfork=%p,atforkvalue=%p,flags=%i", - name ? name : "(null)", fd_in, fd_out, - atfork, atforkvalue, flags); - - if (name) - { - i = 0; - while (argv[i]) - { - TRACE_LOG2 ("argv[%2i] = %s", i, argv[i]); - i++; - } - } - i = 0; - if (fd_child_list) - { - while (fd_child_list[i] != ASSUAN_INVALID_FD) - { - TRACE_LOG2 ("fd_child_list[%2i] = 0x%x", i, fd_child_list[i]); - i++; - } - } - - spawn_flags = GPGRT_PROCESS_STDIN_PIPE|GPGRT_PROCESS_STDOUT_PIPE; - if ((flags & ASSUAN_SPAWN_DETACHED)) - spawn_flags |= GPGRT_PROCESS_NO_CONSOLE; - - if (fd_child_list) - { - for (fdp = fd_child_list; *fdp != ASSUAN_INVALID_FD; fdp++) - if (*fdp == (assuan_fd_t)STDERR_FILENO) - { - keep_stderr = 1; - break; - } - } - if (keep_stderr) - spawn_flags |= GPGRT_PROCESS_STDERR_KEEP; - - ec = gpgrt_spawn_actions_new (&act); - if (ec) - { - return -1; - } - -#ifdef HAVE_W32_SYSTEM - gpgrt_spawn_actions_set_inherit_handles (act, fd_child_list); - gpgrt_spawn_actions_set_redirect (act, fd_in, fd_out, ASSUAN_INVALID_FD); -#else - gpgrt_spawn_actions_set_inherit_fds (act, fd_child_list); - gpgrt_spawn_actions_set_redirect (act, fd_in, fd_out, -1); - gpgrt_spawn_actions_set_atfork (act, atfork, atforkvalue); -#endif - ec = gpgrt_process_spawn (name, argv+1, spawn_flags, act, &proc); - gpgrt_spawn_actions_release (act); - if (ec) - { - return -1; - } - ctx->server_proc = proc; - res = 0; - - return TRACE_SYSERR (res); -} int _assuan_socketpair (assuan_context_t ctx, int namespace, int style, |