| 1 |
/* t-lock-single-posix.c - Check the lock functions with single thread |
| 2 |
* Copyright (C) 2021 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of libgpg-error. |
| 5 |
* |
| 6 |
* libgpg-error is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* libgpg-error is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#if HAVE_CONFIG_H |
| 21 |
# include <config.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <assert.h> |
| 28 |
#include <sys/types.h> |
| 29 |
#include <unistd.h> |
| 30 |
|
| 31 |
#define PGM "t-lock-single" |
| 32 |
|
| 33 |
#include "t-common.h" |
| 34 |
|
| 35 |
GPGRT_LOCK_DEFINE (the_lock); |
| 36 |
|
| 37 |
static int locking_used; |
| 38 |
|
| 39 |
#ifdef USE_POSIX_THREADS_FROM_LIBC |
| 40 |
#include <pthread.h> |
| 41 |
|
| 42 |
static void * |
| 43 |
thread_func (void *arg) |
| 44 |
{ |
| 45 |
(void)arg; |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
#endif |
| 49 |
|
| 50 |
static void |
| 51 |
run_test (void) |
| 52 |
{ |
| 53 |
gpg_err_code_t rc; |
| 54 |
|
| 55 |
rc = gpgrt_lock_lock (&the_lock); |
| 56 |
if (rc) |
| 57 |
fail ("gpgrt_lock_lock failed at %d: %s", __LINE__, gpg_strerror (rc)); |
| 58 |
|
| 59 |
rc = gpgrt_lock_unlock (&the_lock); |
| 60 |
if (rc) |
| 61 |
fail ("gpgrt_lock_unlock failed at %d: %s", __LINE__,gpg_strerror (rc)); |
| 62 |
|
| 63 |
if (locking_used) |
| 64 |
/*It was incremented, even with single thread. */ |
| 65 |
fail ("Single thread situation is not correctly handled\n"); |
| 66 |
|
| 67 |
#ifdef USE_POSIX_THREADS_FROM_LIBC |
| 68 |
/* |
| 69 |
* With !USE_POSIX_THREADS_FROM_LIBC, it is determined at link time. |
| 70 |
* |
| 71 |
* With USE_POSIX_THREADS_FROM_LIBC, it is determined at runtime, |
| 72 |
* and the condition is changed when an application actually uses |
| 73 |
* thread. |
| 74 |
*/ |
| 75 |
{ |
| 76 |
pthread_t rthread; |
| 77 |
pthread_create (&rthread, NULL, thread_func, NULL); |
| 78 |
|
| 79 |
rc = gpgrt_lock_lock (&the_lock); |
| 80 |
if (rc) |
| 81 |
fail ("gpgrt_lock_lock failed at %d: %s", __LINE__, gpg_strerror (rc)); |
| 82 |
|
| 83 |
rc = gpgrt_lock_unlock (&the_lock); |
| 84 |
if (rc) |
| 85 |
fail ("gpgrt_lock_unlock failed at %d: %s", __LINE__,gpg_strerror (rc)); |
| 86 |
|
| 87 |
pthread_join (rthread, NULL); |
| 88 |
|
| 89 |
if (!locking_used) |
| 90 |
/*It was *NOT* incremented, even with multiple threads. */ |
| 91 |
fail ("Multiple threads situation is not correctly handled\n"); |
| 92 |
} |
| 93 |
#endif |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
static void |
| 98 |
syscall_clam_pre (void) |
| 99 |
{ |
| 100 |
if (debug) |
| 101 |
show ("syscall pre called\n"); |
| 102 |
locking_used++; |
| 103 |
} |
| 104 |
|
| 105 |
static void |
| 106 |
syscall_clam_post (void) |
| 107 |
{ |
| 108 |
if (debug) |
| 109 |
show ("syscall post called\n"); |
| 110 |
} |
| 111 |
|
| 112 |
int |
| 113 |
main (int argc, char **argv) |
| 114 |
{ |
| 115 |
int last_argc = -1; |
| 116 |
int rc; |
| 117 |
|
| 118 |
if (argc) |
| 119 |
{ |
| 120 |
argc--; argv++; |
| 121 |
} |
| 122 |
while (argc && last_argc != argc ) |
| 123 |
{ |
| 124 |
last_argc = argc; |
| 125 |
if (!strcmp (*argv, "--help")) |
| 126 |
{ |
| 127 |
puts ( |
| 128 |
"usage: ./t-lock-single [options]\n" |
| 129 |
"\n" |
| 130 |
"Options:\n" |
| 131 |
" --verbose Show what is going on\n" |
| 132 |
" --debug Flyswatter\n" |
| 133 |
); |
| 134 |
exit (0); |
| 135 |
} |
| 136 |
if (!strcmp (*argv, "--verbose")) |
| 137 |
{ |
| 138 |
verbose = 1; |
| 139 |
argc--; argv++; |
| 140 |
} |
| 141 |
else if (!strcmp (*argv, "--debug")) |
| 142 |
{ |
| 143 |
verbose = debug = 1; |
| 144 |
argc--; argv++; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if (!gpg_error_check_version (GPG_ERROR_VERSION)) |
| 149 |
{ |
| 150 |
die ("gpg_error_check_version returned an error"); |
| 151 |
errorcount++; |
| 152 |
} |
| 153 |
|
| 154 |
gpgrt_set_syscall_clamp (syscall_clam_pre, syscall_clam_post); |
| 155 |
|
| 156 |
rc = gpgrt_lock_init (&the_lock); |
| 157 |
if (rc) |
| 158 |
fail ("gpgrt_lock_init failed at %d: %s", __LINE__, gpg_strerror (rc)); |
| 159 |
|
| 160 |
run_test (); |
| 161 |
|
| 162 |
gpgrt_lock_destroy (&the_lock); |
| 163 |
|
| 164 |
return errorcount ? 1 : 0; |
| 165 |
} |