]> granicus.if.org Git - postgresql/commitdiff
Implement error checking for pthreads calls in thread-safe mode. They really
authorMagnus Hagander <magnus@hagander.net>
Fri, 16 May 2008 18:30:53 +0000 (18:30 +0000)
committerMagnus Hagander <magnus@hagander.net>
Fri, 16 May 2008 18:30:53 +0000 (18:30 +0000)
should always succeed, but in the likely event of a failure we would
previously fall through *without locking* - the new code will exit(1).

Printing the error message on stderr will not work for all applications, but
it's better than nothing at all - and our API doesn't provide a way to return
the error to the caller.

src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/fe-secure.c
src/interfaces/libpq/libpq-int.h
src/interfaces/libpq/pthread-win32.c

index dd6c988790bee1d64c67b650c80803c625341f39..ec9a9a570a5987bcd4ba80d997d6f400a5ccb20c 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.357 2008/03/31 02:43:14 tgl Exp $
+ *       $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.358 2008/05/16 18:30:53 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -3835,14 +3835,23 @@ default_threadlock(int acquire)
                while (InterlockedExchange(&mutex_initlock, 1) == 1)
                         /* loop, another thread own the lock */ ;
                if (singlethread_lock == NULL)
-                       pthread_mutex_init(&singlethread_lock, NULL);
+               {
+                       if (pthread_mutex_init(&singlethread_lock, NULL))
+                               PGTHREAD_ERROR("failed to initialize mutex");
+               }
                InterlockedExchange(&mutex_initlock, 0);
        }
 #endif
        if (acquire)
-               pthread_mutex_lock(&singlethread_lock);
+       {
+               if (pthread_mutex_lock(&singlethread_lock))
+                       PGTHREAD_ERROR("failed to lock mutex");
+       }
        else
-               pthread_mutex_unlock(&singlethread_lock);
+       {
+               if (pthread_mutex_unlock(&singlethread_lock))
+                       PGTHREAD_ERROR("failed to unlock mutex");
+       }
 #endif
 }
 
index 4414baba4a82d03d281ef67bf17a2261a0a3a2d8..2f72b0a5de4d8b8bd99296b36207e138acf3779d 100644 (file)
@@ -11,7 +11,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.104 2008/03/31 02:43:14 tgl Exp $
+ *       $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.105 2008/05/16 18:30:53 mha Exp $
  *
  * NOTES
  *       [ Most of these notes are wrong/obsolete, but perhaps not all ]
@@ -796,12 +796,21 @@ static void
 pq_lockingcallback(int mode, int n, const char *file, int line)
 {
        if (mode & CRYPTO_LOCK)
-               pthread_mutex_lock(&pq_lockarray[n]);
+       {
+               if (pthread_mutex_lock(&pq_lockarray[n]))
+                       PGTHREAD_ERROR("failed to lock mutex");
+       }
        else
-               pthread_mutex_unlock(&pq_lockarray[n]);
+       {
+               if (pthread_mutex_unlock(&pq_lockarray[n]))
+                       PGTHREAD_ERROR("failed to unlock mutex");
+       }
 }
 #endif   /* ENABLE_THREAD_SAFETY */
 
+/*
+ * Also see similar code in fe-connect.c, default_threadlock()
+ */
 static int
 init_ssl_system(PGconn *conn)
 {
@@ -817,11 +826,15 @@ init_ssl_system(PGconn *conn)
                while (InterlockedExchange(&mutex_initlock, 1) == 1)
                         /* loop, another thread own the lock */ ;
                if (init_mutex == NULL)
-                       pthread_mutex_init(&init_mutex, NULL);
+               {
+                       if (pthread_mutex_init(&init_mutex, NULL))
+                               return -1;
+               }
                InterlockedExchange(&mutex_initlock, 0);
        }
 #endif
-       pthread_mutex_lock(&init_mutex);
+       if (pthread_mutex_lock(&init_mutex))
+               return -1;
 
        if (pq_initssllib && pq_lockarray == NULL)
        {
@@ -836,7 +849,10 @@ init_ssl_system(PGconn *conn)
                        return -1;
                }
                for (i = 0; i < CRYPTO_num_locks(); i++)
-                       pthread_mutex_init(&pq_lockarray[i], NULL);
+               {
+                       if (pthread_mutex_init(&pq_lockarray[i], NULL))
+                               return -1;
+               }
 
                CRYPTO_set_locking_callback(pq_lockingcallback);
        }
index d39b8df83808e2e2d8aef769c06f832cf09a830e..89b37473506cc256d74013603443be27f827456c 100644 (file)
@@ -12,7 +12,7 @@
  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.129 2008/01/01 19:46:00 momjian Exp $
+ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.130 2008/05/16 18:30:53 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -439,6 +439,13 @@ extern bool pqGetHomeDirectory(char *buf, int bufsize);
 #ifdef ENABLE_THREAD_SAFETY
 extern pgthreadlock_t pg_g_threadlock;
 
+#define PGTHREAD_ERROR(msg) \
+       do { \
+               fprintf(stderr, "%s\n", msg); \
+               exit(1); \
+       } while (0)
+
+
 #define pglock_thread()                pg_g_threadlock(true)
 #define pgunlock_thread()      pg_g_threadlock(false)
 #else
index ef6dc22e58a94fad4e9b3e61f1294ac198d0a654..1fdd264171c6ecb1fee03f12dc31139da66cab3d 100644 (file)
@@ -5,7 +5,7 @@
 *
 * Copyright (c) 2004-2008, PostgreSQL Global Development Group
 * IDENTIFICATION
-*      $PostgreSQL: pgsql/src/interfaces/libpq/pthread-win32.c,v 1.15 2008/01/01 19:46:00 momjian Exp $
+*      $PostgreSQL: pgsql/src/interfaces/libpq/pthread-win32.c,v 1.16 2008/05/16 18:30:53 mha Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -32,20 +32,27 @@ pthread_getspecific(pthread_key_t key)
        return NULL;
 }
 
-void
+int
 pthread_mutex_init(pthread_mutex_t *mp, void *attr)
 {
        *mp = CreateMutex(0, 0, 0);
+       if (*mp == NULL)
+               return 1;
+       return 0;
 }
 
-void
+int
 pthread_mutex_lock(pthread_mutex_t *mp)
 {
-       WaitForSingleObject(*mp, INFINITE);
+       if (WaitForSingleObject(*mp, INFINITE) != WAIT_OBJECT_0)
+               return 1;
+       return 0;
 }
 
-void
+int
 pthread_mutex_unlock(pthread_mutex_t *mp)
 {
-       ReleaseMutex(*mp);
+       if (!ReleaseMutex(*mp))
+               return 1;
+       return 0;
 }