tests: fix locking in test_thread_setuid.c
authorStefan Metzmacher <metze@samba.org>
Mon, 16 Jan 2023 14:43:21 +0000 (15:43 +0100)
committerAndreas Schneider <asn@samba.org>
Tue, 17 Jan 2023 13:22:25 +0000 (14:22 +0100)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
tests/test_thread_setuid.c

index 8b52c3834661cbd0b41b98294447257652b0b774..db65c969a467d27b588c4a63ab819c3e0d8add7a 100644 (file)
@@ -25,8 +25,8 @@
 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
 
 
-static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
-static pthread_mutex_t sleep_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t uwrap_getuid_sync_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t uwrap_setuid_sync_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static void *uwrap_getuid_sync(void *arg)
 {
@@ -34,12 +34,12 @@ static void *uwrap_getuid_sync(void *arg)
 
        (void) arg; /* unused */
 
-       pthread_mutex_unlock(&sleep_mutex);
-       pthread_mutex_lock(&wait_mutex);
+       pthread_mutex_lock(&uwrap_getuid_sync_mutex);
 
        u = getuid();
        assert_int_equal(u, 888);
 
+       pthread_mutex_unlock(&uwrap_getuid_sync_mutex);
        return NULL;
 }
 
@@ -49,9 +49,12 @@ static void *uwrap_setuid_sync(void *arg)
 
        (void) arg; /* unused */
 
+       pthread_mutex_lock(&uwrap_setuid_sync_mutex);
+
        rc = setuid(888);
        assert_int_equal(rc, 0);
 
+       pthread_mutex_unlock(&uwrap_setuid_sync_mutex);
        return NULL;
 }
 
@@ -62,8 +65,8 @@ static void test_real_sync_setuid(void **state)
 
        (void) state; /* unused */
 
-       pthread_mutex_lock(&wait_mutex);
-       pthread_mutex_lock(&sleep_mutex);
+       pthread_mutex_lock(&uwrap_getuid_sync_mutex);
+       pthread_mutex_lock(&uwrap_setuid_sync_mutex);
 
        /* Create thread which will wait for change. */
        pthread_create(&threads[0],
@@ -71,16 +74,16 @@ static void test_real_sync_setuid(void **state)
                       uwrap_getuid_sync,
                       NULL);
 
-       pthread_mutex_lock(&sleep_mutex);
-
        pthread_create(&threads[1],
                       NULL,
                       uwrap_setuid_sync,
                       NULL);
 
+       pthread_mutex_unlock(&uwrap_setuid_sync_mutex);
+
        pthread_join(threads[1], NULL);
 
-       pthread_mutex_unlock(&wait_mutex);
+       pthread_mutex_unlock(&uwrap_getuid_sync_mutex);
 
        pthread_join(threads[0], NULL);