aboutsummaryrefslogtreecommitdiffstats
path: root/kbx/kbx-client-util.c
diff options
context:
space:
mode:
Diffstat (limited to 'kbx/kbx-client-util.c')
-rw-r--r--kbx/kbx-client-util.c54
1 files changed, 30 insertions, 24 deletions
diff --git a/kbx/kbx-client-util.c b/kbx/kbx-client-util.c
index f6456a508..b900586c8 100644
--- a/kbx/kbx-client-util.c
+++ b/kbx/kbx-client-util.c
@@ -53,6 +53,7 @@ struct kbx_client_data_s
/* Condition variable to sync the datastream with the command. */
npth_mutex_t mutex;
npth_cond_t cond;
+ npth_t thd;
/* The data received from the keyboxd and an error code if there was
* a problem (in which case DATA is also set to NULL. This is only
@@ -103,7 +104,6 @@ prepare_data_pipe (kbx_client_data_t kcd)
int rc;
int inpipe[2];
estream_t infp;
- npth_t thread;
npth_attr_t tattr;
kcd->fp = NULL;
@@ -118,14 +118,18 @@ prepare_data_pipe (kbx_client_data_t kcd)
return err; /* That should not happen. */
}
- err = assuan_sendfd (kcd->ctx, INT2FD (inpipe[1]));
+#ifdef HAVE_W32_SYSTEM
+ err = assuan_sendfd (kcd->ctx, (HANDLE)_get_osfhandle (inpipe[1]));
+#else
+ err = assuan_sendfd (kcd->ctx, inpipe[1]);
+#endif
if (err)
{
- log_error ("sending sending fd %d to keyboxd: %s <%s>\n",
+ log_error ("sending fd %d to keyboxd: %s <%s>\n",
inpipe[1], gpg_strerror (err), gpg_strsource (err));
es_fclose (infp);
gnupg_close_pipe (inpipe[1]);
- return 0; /* Server may not support fd-passing. */
+ return err;
}
err = assuan_transact (kcd->ctx, "OUTPUT FD",
@@ -135,12 +139,12 @@ prepare_data_pipe (kbx_client_data_t kcd)
log_info ("keyboxd does not accept our fd: %s <%s>\n",
gpg_strerror (err), gpg_strsource (err));
es_fclose (infp);
- return 0;
+ return err;
}
+ close (inpipe[1]);
kcd->fp = infp;
-
rc = npth_attr_init (&tattr);
if (rc)
{
@@ -150,8 +154,8 @@ prepare_data_pipe (kbx_client_data_t kcd)
kcd->fp = NULL;
return err;
}
- npth_attr_setdetachstate (&tattr, NPTH_CREATE_DETACHED);
- rc = npth_create (&thread, &tattr, datastream_thread, kcd);
+ npth_attr_setdetachstate (&tattr, NPTH_CREATE_JOINABLE);
+ rc = npth_create (&kcd->thd, &tattr, datastream_thread, kcd);
if (rc)
{
err = gpg_error_from_errno (rc);
@@ -162,6 +166,7 @@ prepare_data_pipe (kbx_client_data_t kcd)
return err;
}
+ npth_attr_destroy (&tattr);
return 0;
}
@@ -193,13 +198,8 @@ datastream_thread (void *arg)
gnupg_sleep (1);
continue;
}
- if (nread != 4)
- {
- err = gpg_error (GPG_ERR_EIO);
- log_error ("error reading data length from keyboxd: %s\n",
- "short read");
- continue;
- }
+ if (nread < 4)
+ break;
datalen = buf32_to_size_t (lenbuf);
/* log_debug ("keyboxd announced %zu bytes\n", datalen); */
@@ -294,8 +294,7 @@ kbx_client_data_new (kbx_client_data_t *r_kcd, assuan_context_t ctx,
{
err = gpg_error_from_errno (rc);
log_error ("error initializing mutex: %s\n", gpg_strerror (err));
- xfree (kcd);
- return err;
+ goto leave; /* Use D-lines. */
}
rc = npth_cond_init (&kcd->cond, NULL);
if (rc)
@@ -303,8 +302,7 @@ kbx_client_data_new (kbx_client_data_t *r_kcd, assuan_context_t ctx,
err = gpg_error_from_errno (rc);
log_error ("error initializing condition: %s\n", gpg_strerror (err));
npth_mutex_destroy (&kcd->mutex);
- xfree (kcd);
- return err;
+ goto leave; /* Use D-lines. */
}
err = prepare_data_pipe (kcd);
@@ -312,8 +310,7 @@ kbx_client_data_new (kbx_client_data_t *r_kcd, assuan_context_t ctx,
{
npth_cond_destroy (&kcd->cond);
npth_mutex_destroy (&kcd->mutex);
- xfree (kcd);
- return err;
+ /* Use D-lines. */
}
leave:
@@ -329,11 +326,20 @@ kbx_client_data_release (kbx_client_data_t kcd)
if (!kcd)
return;
+
fp = kcd->fp;
+ if (!fp)
+ {
+ xfree (kcd);
+ return;
+ }
+
+ if (npth_join (kcd->thd, NULL))
+ log_error ("kbx_client_data_release failed on npth_join");
+
kcd->fp = NULL;
- es_fclose (fp); /* That close should let the thread run into an error. */
- /* FIXME: Make thread killing explicit. Otherwise we run in a
- * log_fatal due to the destroyed mutex. */
+ es_fclose (fp);
+
npth_cond_destroy (&kcd->cond);
npth_mutex_destroy (&kcd->mutex);
xfree (kcd);