diff options
author | NIIBE Yutaka <gniibe@fsij.org> | 2022-11-04 14:41:28 +0900 |
---|---|---|
committer | NIIBE Yutaka <gniibe@fsij.org> | 2022-11-04 14:41:28 +0900 |
commit | 5d30adb5ad376a07576b258b9395adadf5576867 (patch) | |
tree | 3639ded8cf8b7e237e3baeb9d5573fdfe0edb12a /src/spawn-w32.c | |
parent | 6c20e8393eba4a9f330143b2158e28ea594cbadd (diff) | |
download | libgpg-error-5d30adb5ad376a07576b258b9395adadf5576867.tar.gz |
spawn: Introduce gpgrt_process_t and use it for spawn API.
* configure.ac (AC_FUNC_FORK): No use.
* src/gpg-error.h.in (@define:gpgrt_process_t@): New.
(@define:pid_t@): Remove.
(gpgrt_spawn_process, gpgrt_spawn_process_fd): Use gpgrt_process_t.
(gpgrt_wait_process, gpgrt_wait_processes): Likewise.
(gpgrt_kill_process, gpgrt_release_process): Likewise.
* src/gpgrt-int.h (_gpgrt_spawn_process): Likewise.
(_gpgrt_spawn_process_fd): Likewise.
(_gpgrt_wait_process, _gpgrt_wait_processes): Likewise.
(_gpgrt_kill_process, _gpgrt_release_process): Likewise.
* src/mkheader.c (write_special): Handle @define:gpgrt_process_t@.
Remove handling of @define:pid_t@.
* src/spawn-posix.c (_gpgrt_spawn_process): Use gpgrt_process_t.
(_gpgrt_spawn_process_fd): Likewise.
(_gpgrt_wait_process, _gpgrt_wait_processes): Likewise.
(_gpgrt_kill_process, _gpgrt_release_process): Likewise.
* src/spawn-w32.c (_gpgrt_spawn_process): Use gpgrt_process_t.
(_gpgrt_spawn_process_fd): Likewise.
(_gpgrt_wait_process, _gpgrt_wait_processes): Likewise.
(_gpgrt_kill_process, _gpgrt_release_process): Likewise.
--
GnuPG-bug-id: 6249
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
Diffstat (limited to 'src/spawn-w32.c')
-rw-r--r-- | src/spawn-w32.c | 101 |
1 files changed, 57 insertions, 44 deletions
diff --git a/src/spawn-w32.c b/src/spawn-w32.c index d17f971..324b5fe 100644 --- a/src/spawn-w32.c +++ b/src/spawn-w32.c @@ -65,24 +65,6 @@ #define fd_to_handle(a) ((HANDLE)(a)) #define handle_to_fd(a) ((intptr_t)(a)) -/* For pid_t and HANDLE: - - * We assume that a HANDLE can be represented by an int which should - * be true for all i386 systems. - * - * On 64-bit machine, it is no longer true, as a type, however, as - * long as the range of the value in the type HANDLE can be - * represented by an int, it works. - * - * FIXME with original MinGW: Breaking ABI for pid_t will be needed - * when the value won't fit within 32-bit range on 64-bit machine. - * - * Note that pid_t is 64-bit integer in sys/types.h with MinGW-w64. - * So, no problem with MinGW-w64. - */ -#define pid_to_handle(a) ((HANDLE)(a)) -#define handle_to_pid(a) ((pid_t)(a)) - /* Return the maximum number of currently allowed open file * descriptors. Only useful on POSIX systems but returns a value on @@ -383,12 +365,42 @@ _gpgrt_make_pipe (int filedes[2], estream_t *r_fp, int direction, int nonblock) } +/* + * UNION PROCESS_ID: + * + * gpgrt_process_t is an object which represents process handle. + * It must be same size as HANDLE and must have same bit pattern. + */ +union process { + gpgrt_process_t process_id; + HANDLE process_handle; +}; + +static gpgrt_process_t +convert_from_handle (HANDLE process_handle) +{ + union process u; + + u.process_handle = process_handle; + return u.process_id; +} + +static HANDLE +convert_from_process (gpgrt_process_t process_id) +{ + union process u; + + u.process_id = process_id; + return u.process_handle; +} + + /* 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, unsigned int flags, estream_t *r_infp, estream_t *r_outfp, estream_t *r_errfp, - pid_t *pid) + gpgrt_process_t *r_process_id) { gpg_err_code_t err; SECURITY_ATTRIBUTES sec_attr; @@ -424,7 +436,7 @@ _gpgrt_spawn_process (const char *pgmname, const char *argv[], *r_outfp = NULL; if (r_errfp) *r_errfp = NULL; - *pid = (pid_t)INVALID_HANDLE_VALUE; /* Always required. */ + *r_process_id = convert_from_handle (INVALID_HANDLE_VALUE); if (r_infp) { @@ -633,7 +645,7 @@ _gpgrt_spawn_process (const char *pgmname, const char *argv[], if (r_errfp) *r_errfp = errfp; - *pid = handle_to_pid (pi.hProcess); + *r_process_id = convert_from_handle (pi.hProcess); return 0; } @@ -644,7 +656,7 @@ _gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], int infd, int outfd, int errfd, int (*spawn_cb) (void *), void *spawn_cb_arg, - pid_t *pid) + gpgrt_process_t *r_process_id) { gpg_err_code_t err; SECURITY_ATTRIBUTES sec_attr; @@ -659,7 +671,7 @@ _gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], ask_inherit = (*spawn_cb) (spawn_cb_arg); /* Setup return values. */ - *pid = (pid_t)INVALID_HANDLE_VALUE; + *r_process_id = convert_from_handle (INVALID_HANDLE_VALUE); /* Prepare security attributes. */ memset (&sec_attr, 0, sizeof sec_attr ); @@ -727,23 +739,24 @@ _gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], ResumeThread (pi.hThread); CloseHandle (pi.hThread); - *pid = handle_to_pid (pi.hProcess); + *r_process_id = convert_from_handle (pi.hProcess); return 0; } /* See gpgrt-int.h for a description. */ gpg_err_code_t -_gpgrt_wait_process (const char *pgmname, pid_t pid, int hang, int *r_exitcode) +_gpgrt_wait_process (const char *pgmname, gpgrt_process_t process_id, + int hang, int *r_exitcode) { - return _gpgrt_wait_processes (&pgmname, &pid, 1, hang, r_exitcode); + return _gpgrt_wait_processes (&pgmname, &process_id, 1, hang, r_exitcode); } /* See gpgrt-int.h for a description. */ gpg_err_code_t -_gpgrt_wait_processes (const char **pgmnames, pid_t *pids, size_t count, - int hang, int *r_exitcodes) +_gpgrt_wait_processes (const char **pgmnames, gpgrt_process_t *process_ids, + size_t count, int hang, int *r_exitcodes) { gpg_err_code_t ec = 0; size_t i; @@ -756,13 +769,15 @@ _gpgrt_wait_processes (const char **pgmnames, pid_t *pids, size_t count, for (i = 0; i < count; i++) { + HANDLE process_handle = convert_from_process (process_ids[i]); + if (r_exitcodes) r_exitcodes[i] = -1; - if (pids[i] == (pid_t)INVALID_HANDLE_VALUE) + if (process_handle == INVALID_HANDLE_VALUE) return GPG_ERR_INV_VALUE; - procs[i] = pid_to_handle (pids[i]); + procs[i] = process_handle; } _gpgrt_pre_syscall (); @@ -787,9 +802,9 @@ _gpgrt_wait_processes (const char **pgmnames, pid_t *pids, size_t count, if (! GetExitCodeProcess (procs[i], &exc)) { - _gpgrt_log_error (_("error getting exit code of process %d:" + _gpgrt_log_error (_("error getting exit code of process %p:" " ec=%d\n"), - (int) pids[i], (int)GetLastError ()); + process_ids[i], (int)GetLastError ()); ec = GPG_ERR_GENERAL; } else if (exc) @@ -824,7 +839,7 @@ _gpgrt_wait_processes (const char **pgmnames, pid_t *pids, size_t count, /* See gpgrt-int.h for a description. */ gpg_err_code_t _gpgrt_spawn_process_detached (const char *pgmname, const char *argv[], - const char *envp[] ) + const char *envp[]) { gpg_err_code_t err; SECURITY_ATTRIBUTES sec_attr; @@ -907,29 +922,27 @@ _gpgrt_spawn_process_detached (const char *pgmname, const char *argv[], gnupg_wait_process must be called to actually remove the process from the system. An invalid PID is ignored. */ void -_gpgrt_kill_process (pid_t pid) +_gpgrt_kill_process (gpgrt_process_t process_id) { - if (pid != (pid_t)INVALID_HANDLE_VALUE) - { - HANDLE process = (HANDLE) pid; + HANDLE process_handle = convert_from_process (process_id); + if (process_handle != INVALID_HANDLE_VALUE) + { /* Arbitrary error code. */ _gpgrt_pre_syscall (); - TerminateProcess (process, 1); + TerminateProcess (process_handle, 1); _gpgrt_post_syscall (); } } void -_gpgrt_release_process (pid_t pid) +_gpgrt_release_process (gpgrt_process_t process_id) { - if (pid != (pid_t)INVALID_HANDLE_VALUE) - { - HANDLE process = (HANDLE)pid; + HANDLE process_handle = convert_from_process (process_id); - CloseHandle (process); - } + if (process_handle != INVALID_HANDLE_VALUE) + CloseHandle (process_handle); } void |