global.c 8.12 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
#include "common.h"
#include "global.h"
9
#include "hash.h"
10
#include "sysdir.h"
11
#include "git2/global.h"
12
#include "git2/sys/openssl.h"
13 14
#include "thread-utils.h"

15 16 17

git_mutex git__mwindow_mutex;

18 19
#define MAX_SHUTDOWN_CB 8

20 21 22
#ifdef GIT_SSL
# include <openssl/ssl.h>
SSL_CTX *git__ssl_ctx;
23
# ifdef GIT_THREADS
24
static git_mutex *openssl_locks;
25
# endif
26 27
#endif

28 29 30
static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
static git_atomic git__n_shutdown_callbacks;
static git_atomic git__n_inits;
31 32 33 34

void git__on_shutdown(git_global_shutdown_fn callback)
{
	int count = git_atomic_inc(&git__n_shutdown_callbacks);
35
	assert(count <= MAX_SHUTDOWN_CB && count > 0);
36 37 38 39 40 41 42
	git__shutdown_callbacks[count - 1] = callback;
}

static void git__shutdown(void)
{
	int pos;

43 44 45 46 47
	for (pos = git_atomic_get(&git__n_shutdown_callbacks); pos > 0; pos = git_atomic_dec(&git__n_shutdown_callbacks)) {
		git_global_shutdown_fn cb = git__swap(git__shutdown_callbacks[pos - 1], NULL);
		if (cb != NULL)
			cb();
	}
48

49 50
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#if defined(GIT_THREADS) && defined(GIT_SSL)
void openssl_locking_function(int mode, int n, const char *file, int line)
{
	int lock;

	GIT_UNUSED(file);
	GIT_UNUSED(line);

	lock = mode & CRYPTO_LOCK;

	if (lock) {
		git_mutex_lock(&openssl_locks[n]);
	} else {
		git_mutex_unlock(&openssl_locks[n]);
	}
}

68
static void shutdown_ssl_locking(void)
69
{
70 71 72
	int num_locks, i;

	num_locks = CRYPTO_num_locks();
73
	CRYPTO_set_locking_callback(NULL);
74 75 76

	for (i = 0; i < num_locks; ++i)
		git_mutex_free(openssl_locks);
77 78 79
	git__free(openssl_locks);
}
#endif
80

81 82 83
static void init_ssl(void)
{
#ifdef GIT_SSL
84 85 86 87 88 89 90
	long ssl_opts = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;

	/* Older OpenSSL and MacOS OpenSSL doesn't have this */
#ifdef SSL_OP_NO_COMPRESSION
	ssl_opts |= SSL_OP_NO_COMPRESSION;
#endif

91 92
	SSL_load_error_strings();
	OpenSSL_add_ssl_algorithms();
93 94 95 96 97 98
	/*
	 * Load SSLv{2,3} and TLSv1 so that we can talk with servers
	 * which use the SSL hellos, which are often used for
	 * compatibility. We then disable SSL so we only allow OpenSSL
	 * to speak TLSv1 to perform the encryption itself.
	 */
99
	git__ssl_ctx = SSL_CTX_new(SSLv23_method());
100
	SSL_CTX_set_options(git__ssl_ctx, ssl_opts);
101 102 103 104 105 106
	SSL_CTX_set_mode(git__ssl_ctx, SSL_MODE_AUTO_RETRY);
	SSL_CTX_set_verify(git__ssl_ctx, SSL_VERIFY_NONE, NULL);
	if (!SSL_CTX_set_default_verify_paths(git__ssl_ctx)) {
		SSL_CTX_free(git__ssl_ctx);
		git__ssl_ctx = NULL;
	}
107 108
#endif
}
109

110 111 112
int git_openssl_set_locking(void)
{
#ifdef GIT_SSL
113
# ifdef GIT_THREADS
114
	int num_locks, i;
115

116 117 118
	num_locks = CRYPTO_num_locks();
	openssl_locks = git__calloc(num_locks, sizeof(git_mutex));
	GITERR_CHECK_ALLOC(openssl_locks);
119

120 121 122 123 124
	for (i = 0; i < num_locks; i++) {
		if (git_mutex_init(&openssl_locks[i]) != 0) {
			giterr_set(GITERR_SSL, "failed to initialize openssl locks");
			return -1;
		}
125
	}
126

127 128 129 130 131 132
	CRYPTO_set_locking_callback(openssl_locking_function);
	git__on_shutdown(shutdown_ssl_locking);
	return 0;
# else
	giterr_set(GITERR_THREAD, "libgit2 as not built with threads");
	return -1;
133
# endif
134
#else
135 136
	giterr_set(GITERR_SSL, "libgit2 was not built with OpenSSL support");
	return -1;
137 138 139
#endif
}

140 141 142 143
/**
 * Handle the global state with TLS
 *
 * If libgit2 is built with GIT_THREADS enabled,
144
 * the `git_libgit2_init()` function must be called
145 146 147 148 149 150 151 152 153 154 155 156
 * before calling any other function of the library.
 *
 * This function allocates a TLS index (using pthreads
 * or the native Win32 API) to store the global state
 * on a per-thread basis.
 *
 * Any internal method that requires global state will
 * then call `git__global_state()` which returns a pointer
 * to the global state structure; this pointer is lazily
 * allocated on each thread.
 *
 * Before shutting down the library, the
157
 * `git_libgit2_shutdown` method must be called to free
158 159 160 161 162 163 164 165
 * the previously reserved TLS index.
 *
 * If libgit2 is built without threading support, the
 * `git__global_statestate()` call returns a pointer to a single,
 * statically allocated global state. The `git_thread_`
 * functions are not available in that case.
 */

166
/*
167
 * `git_libgit2_init()` allows subsystems to perform global setup,
168
 * which may take place in the global scope.  An explicit memory
169
 * fence exists at the exit of `git_libgit2_init()`.  Without this,
170 171 172 173
 * CPU cores are free to reorder cache invalidation of `_tls_init`
 * before cache invalidation of the subsystems' newly written global
 * state.
 */
174 175 176
#if defined(GIT_THREADS) && defined(GIT_WIN32)

static DWORD _tls_index;
177
static volatile LONG _mutex = 0;
178

179
static int synchronized_threads_init(void)
180
{
181 182
	int error;

183
	_tls_index = TlsAlloc();
Russell Belfer committed
184 185
	if (git_mutex_init(&git__mwindow_mutex))
		return -1;
186 187

	/* Initialize any other subsystems that have global state */
188
	if ((error = git_hash_global_init()) >= 0)
189
		error = git_sysdir_global_init();
190

191
	win32_pthread_initialize();
192

193 194 195
	return error;
}

196
int git_libgit2_init(void)
197
{
198
	int ret;
199 200 201

	/* Enter the lock */
	while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
202

203
	/* Only do work on a 0 -> 1 transition of the refcount */
204 205 206 207
	if ((ret = git_atomic_inc(&git__n_inits)) == 1) {
		if (synchronized_threads_init() < 0)
			ret = -1;
	}
208

209 210 211
	/* Exit the lock */
	InterlockedExchange(&_mutex, 0);

212
	return ret;
213 214
}

215
static void synchronized_threads_shutdown(void)
216
{
217
	/* Shut down any subsystems that have global state */
218
	git__shutdown();
219
	TlsFree(_tls_index);
220
	git_mutex_free(&git__mwindow_mutex);
221 222
}

223
int git_libgit2_shutdown(void)
224
{
225 226
	int ret;

227 228
	/* Enter the lock */
	while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
229

230
	/* Only do work on a 1 -> 0 transition of the refcount */
231
	if ((ret = git_atomic_dec(&git__n_inits)) == 0)
232
		synchronized_threads_shutdown();
233 234 235

	/* Exit the lock */
	InterlockedExchange(&_mutex, 0);
236 237

	return ret;
238 239 240 241 242 243
}

git_global_st *git__global_state(void)
{
	void *ptr;

244
	assert(git_atomic_get(&git__n_inits) > 0);
245

246 247 248
	if ((ptr = TlsGetValue(_tls_index)) != NULL)
		return ptr;

249
	ptr = git__malloc(sizeof(git_global_st));
250 251 252 253 254 255 256 257 258 259 260
	if (!ptr)
		return NULL;

	memset(ptr, 0x0, sizeof(git_global_st));
	TlsSetValue(_tls_index, ptr);
	return ptr;
}

#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)

static pthread_key_t _tls_key;
261 262
static pthread_once_t _once_init = PTHREAD_ONCE_INIT;
int init_error = 0;
263 264 265

static void cb__free_status(void *st)
{
266 267 268
	git_global_st *state = (git_global_st *) st;
	git__free(state->error_t.message);

269
	git__free(st);
270 271
}

272
static void init_once(void)
273
{
274 275
	if ((init_error = git_mutex_init(&git__mwindow_mutex)) != 0)
		return;
276
	pthread_key_create(&_tls_key, &cb__free_status);
277

278

279
	/* Initialize any other subsystems that have global state */
280
	if ((init_error = git_hash_global_init()) >= 0)
281
		init_error = git_sysdir_global_init();
282

283 284 285
	/* OpenSSL needs to be initialized from the main thread */
	init_ssl();

286
	GIT_MEMORY_BARRIER;
287
}
288

289
int git_libgit2_init(void)
290
{
291 292
	int ret;

293
	pthread_once(&_once_init, init_once);
294 295 296
	ret = git_atomic_inc(&git__n_inits);

	return init_error ? init_error : ret;
297 298
}

299
int git_libgit2_shutdown(void)
300
{
301
	void *ptr = NULL;
302
	pthread_once_t new_once = PTHREAD_ONCE_INIT;
303
	int ret;
304

305 306
	if ((ret = git_atomic_dec(&git__n_inits)) > 0)
		return ret;
307

308 309 310
	/* Shut down any subsystems that have global state */
	git__shutdown();

311
	ptr = pthread_getspecific(_tls_key);
312 313
	pthread_setspecific(_tls_key, NULL);
	git__free(ptr);
Russell Belfer committed
314

315
	pthread_key_delete(_tls_key);
316
	git_mutex_free(&git__mwindow_mutex);
317
	_once_init = new_once;
318 319

	return ret;
320 321 322 323 324 325
}

git_global_st *git__global_state(void)
{
	void *ptr;

326
	assert(git_atomic_get(&git__n_inits) > 0);
327

328 329 330
	if ((ptr = pthread_getspecific(_tls_key)) != NULL)
		return ptr;

331
	ptr = git__malloc(sizeof(git_global_st));
332 333 334 335 336 337 338 339 340 341 342 343
	if (!ptr)
		return NULL;

	memset(ptr, 0x0, sizeof(git_global_st));
	pthread_setspecific(_tls_key, ptr);
	return ptr;
}

#else

static git_global_st __state;

344
int git_libgit2_init(void)
345
{
346 347 348 349 350 351 352
	static int ssl_inited = 0;

	if (!ssl_inited) {
		init_ssl();
		ssl_inited = 1;
	}

353
	return git_atomic_inc(&git__n_inits);
354 355
}

356
int git_libgit2_shutdown(void)
357
{
358 359
	int ret;

360
	/* Shut down any subsystems that have global state */
361
	if (ret = git_atomic_dec(&git__n_inits))
362
		git__shutdown();
363 364

	return ret;
365 366 367 368 369 370 371 372
}

git_global_st *git__global_state(void)
{
	return &__state;
}

#endif /* GIT_THREADS */