From 153f1128f81a934868a4175e73342b1adb9a969e Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 14 Jun 2019 10:12:56 +0200 Subject: [PATCH] core: Use a better name for the internal synchronous wait. * src/wait.c (_gpgme_wait_on_condition): Rename to ... (_gpgme_sync_wait): this. Small code refactoring. (_gpgme_wait_one, _gpgme_wait_one_ext): Remove and change all callers to use _gpgme_sync_wait. * src/fdtable.c (_gpgme_fdtable_run_io_cbs): Move TRACE up. -- Signed-off-by: Werner Koch --- src/decrypt-verify.c | 4 +-- src/decrypt.c | 2 +- src/delete.c | 4 +-- src/edit.c | 6 ++-- src/encrypt-sign.c | 2 +- src/encrypt.c | 2 +- src/export.c | 7 ++-- src/fdtable.c | 4 +-- src/genkey.c | 12 +++---- src/getauditlog.c | 3 +- src/import.c | 4 +-- src/keylist.c | 2 +- src/keysign.c | 2 +- src/opassuan.c | 2 +- src/ops.h | 7 ++-- src/passwd.c | 3 +- src/sign.c | 2 +- src/spawn.c | 2 +- src/tofupolicy.c | 2 +- src/trustlist.c | 2 +- src/verify.c | 2 +- src/vfs-create.c | 3 +- src/vfs-mount.c | 3 +- src/wait.c | 81 +++++++++++++++++--------------------------- 24 files changed, 69 insertions(+), 94 deletions(-) diff --git a/src/decrypt-verify.c b/src/decrypt-verify.c index b63318f2..18aa58cc 100644 --- a/src/decrypt-verify.c +++ b/src/decrypt-verify.c @@ -126,7 +126,7 @@ gpgme_op_decrypt_verify (gpgme_ctx_t ctx, gpgme_data_t cipher, err = decrypt_verify_start (ctx, 1, GPGME_DECRYPT_VERIFY, cipher, plain); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); ctx->ignore_mdc_error = 0; /* Always reset. */ return TRACE_ERR (err); } @@ -177,7 +177,7 @@ gpgme_op_decrypt_ext (gpgme_ctx_t ctx, else err = _gpgme_decrypt_start (ctx, 1, flags, cipher, plain); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); ctx->ignore_mdc_error = 0; /* Always reset. */ return TRACE_ERR (err); } diff --git a/src/decrypt.c b/src/decrypt.c index f8847996..885f93d5 100644 --- a/src/decrypt.c +++ b/src/decrypt.c @@ -613,7 +613,7 @@ gpgme_op_decrypt (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain) err = _gpgme_decrypt_start (ctx, 1, 0, cipher, plain); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); ctx->ignore_mdc_error = 0; /* Always reset. */ return TRACE_ERR (err); } diff --git a/src/delete.c b/src/delete.c index cd7ab570..15be0b2d 100644 --- a/src/delete.c +++ b/src/delete.c @@ -162,7 +162,7 @@ gpgme_op_delete (gpgme_ctx_t ctx, const gpgme_key_t key, int allow_secret) err = delete_start (ctx, 1, key, allow_secret ? GPGME_DELETE_ALLOW_SECRET : 0); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return err; } @@ -204,6 +204,6 @@ gpgme_op_delete_ext (gpgme_ctx_t ctx, const gpgme_key_t key, err = delete_start (ctx, 1, key, flags); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return err; } diff --git a/src/edit.c b/src/edit.c index a7d2bb51..21682491 100644 --- a/src/edit.c +++ b/src/edit.c @@ -186,7 +186,7 @@ gpgme_op_interact (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags, err = interact_start (ctx, 1, key, flags, fnc, fnc_value, out); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return err; } @@ -266,7 +266,7 @@ gpgme_op_edit (gpgme_ctx_t ctx, gpgme_key_t key, err = edit_start (ctx, 1, 0, key, fnc, fnc_value, out); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } @@ -309,6 +309,6 @@ gpgme_op_card_edit (gpgme_ctx_t ctx, gpgme_key_t key, err = edit_start (ctx, 1, 1, key, fnc, fnc_value, out); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } diff --git a/src/encrypt-sign.c b/src/encrypt-sign.c index e8272d8e..3af92079 100644 --- a/src/encrypt-sign.c +++ b/src/encrypt-sign.c @@ -171,7 +171,7 @@ gpgme_op_encrypt_sign_ext (gpgme_ctx_t ctx, gpgme_key_t recp[], err = encrypt_sign_start (ctx, 1, recp, recpstring, flags, plain, cipher); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } diff --git a/src/encrypt.c b/src/encrypt.c index c5f668ea..61158b9c 100644 --- a/src/encrypt.c +++ b/src/encrypt.c @@ -319,7 +319,7 @@ gpgme_op_encrypt_ext (gpgme_ctx_t ctx, gpgme_key_t recp[], err = encrypt_start (ctx, 1, recp, recpstring, flags, plain, cipher); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } diff --git a/src/export.c b/src/export.c index abf260ad..81ec2936 100644 --- a/src/export.c +++ b/src/export.c @@ -202,7 +202,7 @@ gpgme_op_export (gpgme_ctx_t ctx, const char *pattern, err = export_start (ctx, 1, pattern, mode, keydata); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return err; } @@ -319,7 +319,7 @@ gpgme_op_export_ext (gpgme_ctx_t ctx, const char *pattern[], err = export_ext_start (ctx, 1, pattern, mode, keydata); if (!err) { - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); if (!err) { /* For this synchronous operation we check for operational @@ -459,7 +459,7 @@ gpgme_op_export_keys (gpgme_ctx_t ctx, err = export_keys_start (ctx, 1, keys, mode, keydata); if (!err) { - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); if (!err) { /* For this synchronous operation we check for operational @@ -478,4 +478,3 @@ gpgme_op_export_keys (gpgme_ctx_t ctx, return TRACE_ERR (err); } - diff --git a/src/fdtable.c b/src/fdtable.c index de240fc3..13d6ef94 100644 --- a/src/fdtable.c +++ b/src/fdtable.c @@ -481,11 +481,11 @@ _gpgme_fdtable_run_io_cbs (uint64_t owner, gpg_error_t *r_op_err) unsigned int cb_count; gpgme_ctx_t actx; + TRACE_BEG (DEBUG_SYSIO, __func__, NULL, "ctx=%lu", owner); + if (r_op_err) *r_op_err = 0; - TRACE_BEG (DEBUG_SYSIO, __func__, NULL, "ctx=%lu", owner); - for (;;) { fd = -1; diff --git a/src/genkey.c b/src/genkey.c index 77576b18..e23ef2d6 100644 --- a/src/genkey.c +++ b/src/genkey.c @@ -316,7 +316,7 @@ gpgme_op_genkey (gpgme_ctx_t ctx, const char *parms, gpgme_data_t pubkey, err = genkey_start (ctx, 1, parms, pubkey, seckey); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } @@ -400,7 +400,7 @@ gpgme_op_createkey (gpgme_ctx_t ctx, const char *userid, const char *algo, err = createkey_start (ctx, 1, userid, algo, reserved, expires, anchorkey, flags); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } @@ -487,7 +487,7 @@ gpgme_op_createsubkey (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo, err = createsubkey_start (ctx, 1, key, algo, reserved, expires, flags); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } @@ -571,7 +571,7 @@ gpgme_op_adduid (gpgme_ctx_t ctx, err = addrevuid_start (ctx, 1, 0, key, userid, flags); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } @@ -608,7 +608,7 @@ gpgme_op_revuid (gpgme_ctx_t ctx, err = addrevuid_start (ctx, 1, GENKEY_EXTRAFLAG_REVOKE, key, userid, flags); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } @@ -640,7 +640,7 @@ set_uid_flag (gpgme_ctx_t ctx, int synchronous, return err = gpg_error (GPG_ERR_UNKNOWN_NAME); if (synchronous && !err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } diff --git a/src/getauditlog.c b/src/getauditlog.c index 13473873..750dae37 100644 --- a/src/getauditlog.c +++ b/src/getauditlog.c @@ -99,7 +99,6 @@ gpgme_op_getauditlog (gpgme_ctx_t ctx, gpgme_data_t output, unsigned int flags) err = getauditlog_start (ctx, 1, output, flags); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } - diff --git a/src/import.c b/src/import.c index 2834aec2..679966ce 100644 --- a/src/import.c +++ b/src/import.c @@ -316,7 +316,7 @@ gpgme_op_import (gpgme_ctx_t ctx, gpgme_data_t keydata) err = _gpgme_op_import_start (ctx, 1, keydata); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } @@ -433,7 +433,7 @@ gpgme_op_import_keys (gpgme_ctx_t ctx, gpgme_key_t *keys) err = _gpgme_op_import_keys_start (ctx, 1, keys); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } diff --git a/src/keylist.c b/src/keylist.c index cdb115fd..44b70f8e 100644 --- a/src/keylist.c +++ b/src/keylist.c @@ -1216,7 +1216,7 @@ gpgme_op_keylist_next (gpgme_ctx_t ctx, gpgme_key_t *r_key) if (!opd->key_queue) { - err = _gpgme_wait_on_condition (ctx, &opd->key_cond, NULL); + err = _gpgme_sync_wait (ctx, &opd->key_cond, NULL); if (err) return TRACE_ERR (err); diff --git a/src/keysign.c b/src/keysign.c index 44ab5ff2..c50df26a 100644 --- a/src/keysign.c +++ b/src/keysign.c @@ -214,6 +214,6 @@ gpgme_op_keysign (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, err = keysign_start (ctx, 1, key, userid, expires, flags); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } diff --git a/src/opassuan.c b/src/opassuan.c index bfb06869..497eded3 100644 --- a/src/opassuan.c +++ b/src/opassuan.c @@ -136,7 +136,7 @@ gpgme_op_assuan_transact_ext (gpgme_ctx_t ctx, if (err) goto out; - err = _gpgme_wait_one_ext (ctx, &op_err); + err = _gpgme_sync_wait (ctx, NULL, &op_err); if (op_err) { TRACE_LOG ("op_err = %s <%s>", gpgme_strerror (op_err), diff --git a/src/ops.h b/src/ops.h index 013b2cd2..c8b7b3ed 100644 --- a/src/ops.h +++ b/src/ops.h @@ -33,10 +33,9 @@ void _gpgme_release_result (gpgme_ctx_t ctx); /* From wait.c. */ -gpgme_error_t _gpgme_wait_one (gpgme_ctx_t ctx); -gpgme_error_t _gpgme_wait_one_ext (gpgme_ctx_t ctx, gpgme_error_t *op_err); -gpgme_error_t _gpgme_wait_on_condition (gpgme_ctx_t ctx, volatile int *cond, - gpgme_error_t *op_err); +gpgme_error_t _gpgme_sync_wait (gpgme_ctx_t ctx, + volatile int *cond, + gpg_error_t *op_err); /* From data.c. */ diff --git a/src/passwd.c b/src/passwd.c index 0ba8c08d..e02ffb56 100644 --- a/src/passwd.c +++ b/src/passwd.c @@ -199,7 +199,6 @@ gpgme_op_passwd (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags) err = passwd_start (ctx, 1, key, flags); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } - diff --git a/src/sign.c b/src/sign.c index 31081aea..e54aa246 100644 --- a/src/sign.c +++ b/src/sign.c @@ -495,6 +495,6 @@ gpgme_op_sign (gpgme_ctx_t ctx, gpgme_data_t plain, gpgme_data_t sig, err = sign_start (ctx, 1, plain, sig, mode); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } diff --git a/src/spawn.c b/src/spawn.c index c5431b14..7f71d66f 100644 --- a/src/spawn.c +++ b/src/spawn.c @@ -101,6 +101,6 @@ gpgme_op_spawn (gpgme_ctx_t ctx, const char *file, const char *argv[], err = spawn_start (ctx, 1, file, argv, datain, dataout, dataerr, flags); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } diff --git a/src/tofupolicy.c b/src/tofupolicy.c index 79593186..c2936a51 100644 --- a/src/tofupolicy.c +++ b/src/tofupolicy.c @@ -180,6 +180,6 @@ gpgme_op_tofu_policy (gpgme_ctx_t ctx, err = tofu_policy_start (ctx, 1, key, policy); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } diff --git a/src/trustlist.c b/src/trustlist.c index a0e82385..8a966002 100644 --- a/src/trustlist.c +++ b/src/trustlist.c @@ -230,7 +230,7 @@ gpgme_op_trustlist_next (gpgme_ctx_t ctx, gpgme_trust_item_t *r_item) if (!opd->trust_queue) { - err = _gpgme_wait_on_condition (ctx, &opd->trust_cond, NULL); + err = _gpgme_sync_wait (ctx, &opd->trust_cond, NULL); if (err) return TRACE_ERR (err); if (!opd->trust_cond) diff --git a/src/verify.c b/src/verify.c index f6bc23c4..eab933b4 100644 --- a/src/verify.c +++ b/src/verify.c @@ -1194,7 +1194,7 @@ gpgme_op_verify (gpgme_ctx_t ctx, gpgme_data_t sig, gpgme_data_t signed_text, err = verify_start (ctx, 1, sig, signed_text, plaintext); if (!err) - err = _gpgme_wait_one (ctx); + err = _gpgme_sync_wait (ctx, NULL, NULL); return TRACE_ERR (err); } diff --git a/src/vfs-create.c b/src/vfs-create.c index 51b8307c..7e071ba3 100644 --- a/src/vfs-create.c +++ b/src/vfs-create.c @@ -97,7 +97,7 @@ gpgme_op_vfs_transact (gpgme_ctx_t ctx, err = vfs_start (ctx, 1, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); if (!err) - err = _gpgme_wait_one_ext (ctx, op_err); + err = _gpgme_sync_wait (ctx, NULL, op_err); return err; } @@ -202,4 +202,3 @@ gpgme_op_vfs_create (gpgme_ctx_t ctx, gpgme_key_t recp[], err = _gpgme_op_vfs_create (ctx, recp, container_file, flags, op_err); return TRACE_ERR (err); } - diff --git a/src/vfs-mount.c b/src/vfs-mount.c index c6ee7c97..4ef12277 100644 --- a/src/vfs-mount.c +++ b/src/vfs-mount.c @@ -154,7 +154,7 @@ gpgme_op_vfs_transact (gpgme_ctx_t ctx, err = vfs_start (ctx, 1, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); if (!err) - err = _gpgme_wait_one_ext (ctx, op_err); + err = _gpgme_sync_wait (ctx, NULL, op_err); return err; } @@ -244,4 +244,3 @@ gpgme_op_vfs_mount (gpgme_ctx_t ctx, const char *container_file, err = _gpgme_op_vfs_mount (ctx, container_file, mount_dir, flags, op_err); return TRACE_ERR (err); } - diff --git a/src/wait.c b/src/wait.c index 7418f00d..b88b7813 100644 --- a/src/wait.c +++ b/src/wait.c @@ -385,25 +385,29 @@ gpgme_wait (gpgme_ctx_t ctx, gpgme_error_t *status, int hang) -/* If COND is a null pointer, wait until the blocking operation in CTX - finished and return its error value. Otherwise, wait until COND is - satisfied or the operation finished. */ +/* Wait until the blocking operation in context CTX has finished and + * return the error value. If COND is not NULL return early if COND + * is satisfied. A session based error will be returned at R_OP_ERR + * if it is not NULL. */ gpgme_error_t -_gpgme_wait_on_condition (gpgme_ctx_t ctx, volatile int *cond, - gpgme_error_t *r_op_err) +_gpgme_sync_wait (gpgme_ctx_t ctx, volatile int *cond, gpg_error_t *r_op_err) { gpgme_error_t err = 0; int hang = 1; io_select_t fds = NULL; unsigned int nfds; - int op_err; int nr; + TRACE_BEG (DEBUG_SYSIO, __func__, NULL, "ctx=%lu", CTXSERIAL (ctx)); + if (r_op_err) *r_op_err = 0; if (!ctx) - return gpg_error (GPG_ERR_INV_VALUE); + { + err = gpg_error (GPG_ERR_INV_VALUE); + goto leave; + } do { @@ -415,35 +419,29 @@ _gpgme_wait_on_condition (gpgme_ctx_t ctx, volatile int *cond, { err = gpg_error_from_syserror (); if (gpg_err_code (err) != GPG_ERR_MISSING_ERRNO) - { - free (fds); - return err; - } - /* Nothing to select. Run the select anyway, so that we use - * its timeout. */ + goto leave; } - - nr = _gpgme_io_select (fds, nfds, 0); - if (nr < 0) - { - /* An error occurred. Close all fds in this context, and - signal it. */ - err = gpg_error_from_syserror (); - _gpgme_cancel_with_err (ctx->serial, err, 0); - free (fds); - return err; - } - _gpgme_fdtable_set_signaled (fds, nfds); - - err = _gpgme_fdtable_run_io_cbs (ctx->serial, r_op_err); - if (err || (r_op_err && *r_op_err)) + else { - free (fds); - return err; + nr = _gpgme_io_select (fds, nfds, 0); + if (nr < 0) + { + /* An error occurred. Close all fds in this context, and + signal it. */ + err = gpg_error_from_syserror (); + _gpgme_cancel_with_err (ctx->serial, err, 0); + goto leave; + } + _gpgme_fdtable_set_signaled (fds, nfds); + + err = _gpgme_fdtable_run_io_cbs (ctx->serial, r_op_err); + if (err || (r_op_err && *r_op_err)) + goto leave; } if (!_gpgme_fdtable_io_cb_count (ctx->serial)) { + /* No more matching fds with IO callbacks. */ struct gpgme_io_event_done_data data = {0, 0}; _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &data); hang = 0; @@ -452,26 +450,9 @@ _gpgme_wait_on_condition (gpgme_ctx_t ctx, volatile int *cond, hang = 0; } while (hang); + err = 0; + leave: free (fds); - return 0; -} - - -/* Wait until the blocking operation in context CTX has finished and - return the error value. This variant can not be used for - session-based protocols. */ -gpgme_error_t -_gpgme_wait_one (gpgme_ctx_t ctx) -{ - return _gpgme_wait_on_condition (ctx, NULL, NULL); -} - -/* Wait until the blocking operation in context CTX has finished and - return the error value. This is the right variant to use for - sesion-based protocols. */ -gpgme_error_t -_gpgme_wait_one_ext (gpgme_ctx_t ctx, gpgme_error_t *op_err) -{ - return _gpgme_wait_on_condition (ctx, NULL, op_err); + return TRACE_ERR (err); }