[Libguestfs] [nbdkit PATCH 1/4] server: Check for pthread lock failures

Eric Blake eblake at redhat.com
Wed Apr 24 22:24:38 UTC 2019


Low-level pthread locks should not fail except in extreme cases of
programmer bugs; we're better off calling attention to such bugs
rather than just assuming that they work and continuing on with
possibly inconsistent state.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 server/connections.c | 20 ++++++++++++--------
 server/locks.c       | 42 ++++++++++++++++++++++++++----------------
 2 files changed, 38 insertions(+), 24 deletions(-)

diff --git a/server/connections.c b/server/connections.c
index a30a541..b7d9a6a 100644
--- a/server/connections.c
+++ b/server/connections.c
@@ -91,11 +91,13 @@ connection_get_status (struct connection *conn)
 {
   int r;

-  if (conn->nworkers)
-    pthread_mutex_lock (&conn->status_lock);
+  if (conn->nworkers &&
+      pthread_mutex_lock (&conn->status_lock))
+    abort ();
   r = conn->status;
-  if (conn->nworkers)
-    pthread_mutex_unlock (&conn->status_lock);
+  if (conn->nworkers &&
+      pthread_mutex_unlock (&conn->status_lock))
+    abort ();
   return r;
 }

@@ -105,12 +107,14 @@ connection_get_status (struct connection *conn)
 int
 connection_set_status (struct connection *conn, int value)
 {
-  if (conn->nworkers)
-    pthread_mutex_lock (&conn->status_lock);
+  if (conn->nworkers &&
+      pthread_mutex_lock (&conn->status_lock))
+    abort ();
   if (value < conn->status)
     conn->status = value;
-  if (conn->nworkers)
-    pthread_mutex_unlock (&conn->status_lock);
+  if (conn->nworkers &&
+      pthread_mutex_unlock (&conn->status_lock))
+    abort ();
   return value;
 }

diff --git a/server/locks.c b/server/locks.c
index f4d6497..d70baf2 100644
--- a/server/locks.c
+++ b/server/locks.c
@@ -55,49 +55,59 @@ lock_init_thread_model (void)
 void
 lock_connection (void)
 {
-  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS)
-    pthread_mutex_lock (&connection_lock);
+  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS &&
+      pthread_mutex_lock (&connection_lock))
+    abort ();
 }

 void
 unlock_connection (void)
 {
-  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS)
-    pthread_mutex_unlock (&connection_lock);
+  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS &&
+      pthread_mutex_unlock (&connection_lock))
+    abort ();
 }

 void
 lock_request (struct connection *conn)
 {
-  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS)
-    pthread_mutex_lock (&all_requests_lock);
+  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS &&
+      pthread_mutex_lock (&all_requests_lock))
+    abort ();

-  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS)
-    pthread_mutex_lock (&conn->request_lock);
+  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS &&
+      pthread_mutex_lock (&conn->request_lock))
+    abort ();

-  pthread_rwlock_rdlock (&unload_prevention_lock);
+  if (pthread_rwlock_rdlock (&unload_prevention_lock))
+    abort ();
 }

 void
 unlock_request (struct connection *conn)
 {
-  pthread_rwlock_unlock (&unload_prevention_lock);
+  if (pthread_rwlock_unlock (&unload_prevention_lock))
+    abort ();

-  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS)
-    pthread_mutex_unlock (&conn->request_lock);
+  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS &&
+      pthread_mutex_unlock (&conn->request_lock))
+    abort ();

-  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS)
-    pthread_mutex_unlock (&all_requests_lock);
+  if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS &&
+      pthread_mutex_unlock (&all_requests_lock))
+    abort ();
 }

 void
 lock_unload (void)
 {
-  pthread_rwlock_wrlock (&unload_prevention_lock);
+  if (pthread_rwlock_wrlock (&unload_prevention_lock))
+    abort ();
 }

 void
 unlock_unload (void)
 {
-  pthread_rwlock_unlock (&unload_prevention_lock);
+  if (pthread_rwlock_unlock (&unload_prevention_lock))
+    abort ();
 }
-- 
2.20.1




More information about the Libguestfs mailing list