/*
*
* Copyright 2004,2005,2006 by the Massachusetts Institute of Technology.
* All Rights Reserved.
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*
*
* Preliminary thread support.
*/
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include "k5-thread.h"
#include "k5-platform.h"
#include "supp-int.h"
#ifndef ENABLE_THREADS /* no thread support */
int krb5int_pthread_loaded (void)
{
return 0;
}
struct tsd_block {
};
void krb5int_thread_detach_hook (void)
{
/* XXX Memory leak here!
Need to destroy all TLS objects we know about for this thread. */
struct tsd_block *t;
int i, err;
if (err)
return;
t = TlsGetValue(tls_idx);
if (t == NULL)
return;
for (i = 0; i < K5_KEY_MAX; i++) {
void *v = t->values[i];
t->values[i] = 0;
(*destructors[i])(v);
}
}
}
/* Stub function not used on Windows. */
int krb5int_pthread_loaded (void)
{
return 0;
}
#else /* POSIX threads */
loaded so this support code stays in the process, and gssapi is
loaded, unloaded, and loaded again. */
/* This is not safe yet!
Thread termination concurrent with key deletion can cause two
threads to interfere. It's a bit tricky, since one of the threads
will want to remove this structure from the list being walked by
the other.
Other cases, like looking up data while the library owning the key
is in the process of being unloaded, we don't worry about. */
struct tsd_block {
};
#ifdef HAVE_PRAGMA_WEAK_REF
static void loaded_test_aux(void)
{
if (flag_pthread_loaded == -1)
flag_pthread_loaded = 1;
else
/* Could we have been called twice? */
flag_pthread_loaded = 0;
}
int krb5int_pthread_loaded (void)
{
int x = flag_pthread_loaded;
if (x != -1)
return x;
if (&pthread_getspecific == 0
|| &pthread_setspecific == 0
|| &pthread_key_create == 0
|| &pthread_key_delete == 0
|| &pthread_once == 0
|| &pthread_mutex_lock == 0
|| &pthread_mutex_unlock == 0
|| &pthread_mutex_destroy == 0
|| &pthread_mutex_init == 0
|| &pthread_self == 0
|| &pthread_equal == 0
/* Any program that's really multithreaded will have to be
able to create threads. */
|| &pthread_create == 0
|| &pthread_join == 0
/* Okay, all the interesting functions -- or stubs for them --
seem to be present. If we call pthread_once, does it
actually seem to cause the indicated function to get called
exactly one time? */
/* This catches cases where pthread_once does nothing, and
never causes the function to get called. That's a pretty
clear violation of the POSIX spec, but hey, it happens. */
|| flag_pthread_loaded < 0) {
flag_pthread_loaded = 0;
return 0;
}
/* If we wanted to be super-paranoid, we could try testing whether
pthread_get/setspecific work, too. I don't know -- so far --
of any system with non-functional stubs for those. */
return flag_pthread_loaded;
}
#else
#endif
static void thread_termination(void *);
{
if (err == 0) {
/* Make multiple passes in case, for example, a libkrb5 cleanup
function wants to print out an error message, which causes
com_err to allocate a thread-specific buffer, after we just
freed up the old one.
Shouldn't actually happen, if we're careful, but check just in
case. */
pass = 0;
none_found = 0;
none_found = 1;
for (i = 0; i < K5_KEY_MAX; i++) {
void *v = t->values[i];
t->values[i] = 0;
(*destructors[i])(v);
none_found = 0;
}
}
}
free (t);
}
/* remove thread from global linked list */
}
#endif /* no threads vs Win32 vs POSIX */
{
struct tsd_block *t;
int err;
if (err)
return NULL;
#ifndef ENABLE_THREADS
t = &tsd_no_threads;
t = TlsGetValue(tls_idx);
#else /* POSIX */
if (K5_PTHREADS_LOADED)
t = pthread_getspecific(key);
else
t = GET_NO_PTHREAD_TSD();
#endif
if (t == NULL)
return NULL;
}
{
struct tsd_block *t;
int err;
if (err)
return err;
#ifndef ENABLE_THREADS
t = &tsd_no_threads;
t = TlsGetValue(tls_idx);
if (t == NULL) {
int i;
t = malloc(sizeof(*t));
if (t == NULL)
return errno;
for (i = 0; i < K5_KEY_MAX; i++)
t->values[i] = 0;
/* add to global linked list */
/* t->next = 0; */
if (!err) {
free(t);
return GetLastError();
}
}
#else /* POSIX */
if (K5_PTHREADS_LOADED) {
t = pthread_getspecific(key);
if (t == NULL) {
int i;
t = malloc(sizeof(*t));
if (t == NULL)
return errno;
for (i = 0; i < K5_KEY_MAX; i++)
t->values[i] = 0;
/* add to global linked list */
t->next = 0;
if (err) {
free(t);
return err;
}
}
} else {
t = GET_NO_PTHREAD_TSD();
}
#endif
return 0;
}
{
int err;
if (err)
return err;
#ifndef ENABLE_THREADS
err = 0;
/* XXX: This can raise EXCEPTION_POSSIBLE_DEADLOCK. */
err = 0;
#else /* POSIX */
if (err == 0) {
}
#endif
return 0;
}
{
#ifndef ENABLE_THREADS
destructors[keynum] = 0;
destructors_set[keynum] = 0;
/* XXX: This can raise EXCEPTION_POSSIBLE_DEADLOCK. */
/* XXX Memory leak here!
Need to destroy the associated data for all threads.
But watch for race conditions in case threads are going away too. */
destructors_set[keynum] = 0;
destructors[keynum] = 0;
#else /* POSIX */
{
int err;
/* XXX RESOURCE LEAK:
Need to destroy the allocated objects first! */
if (err == 0) {
destructors_set[keynum] = 0;
}
}
#endif
return 0;
}
int krb5int_call_thread_support_init (void)
{
}
#include "cache-addrinfo.h"
#ifdef DEBUG_THREADS_STATS
#include <stdio.h>
#endif
int krb5int_thread_support_init (void)
{
int err;
#ifdef SHOW_INITFINI_FUNCS
printf("krb5int_thread_support_init\n");
#endif
#ifdef DEBUG_THREADS_STATS
/* stats_logfile = stderr; */
if (stats_logfile == NULL)
#endif
#ifndef ENABLE_THREADS
/* Nothing to do for TLS initialization. */
/* XXX This can raise an exception if memory is low! */
#else /* POSIX */
if (err)
return err;
if (K5_PTHREADS_LOADED) {
if (err)
return err;
}
#endif
err = krb5int_init_fac();
if (err)
return err;
err = krb5int_err_init();
if (err)
return err;
return 0;
}
void krb5int_thread_support_fini (void)
{
return;
#ifdef SHOW_INITFINI_FUNCS
printf("krb5int_thread_support_fini\n");
#endif
#ifndef ENABLE_THREADS
/* Do nothing. */
/* ... free stuff ... */
#else /* POSIX */
return;
if (K5_PTHREADS_LOADED)
/* ... delete stuff ... */
#endif
#ifdef DEBUG_THREADS_STATS
/* XXX Should close if not stderr, in case unloading library but
not exiting. */
#endif
}
#ifdef DEBUG_THREADS_STATS
void KRB5_CALLCONV
{
now = get_current_time();
(void) krb5int_call_thread_support_init();
m->count++;
m->time_acquired = now;
}
void KRB5_CALLCONV
{
}
#include <math.h>
static double
{
/* SUM((x_i - mu)^2)
= SUM(x_i^2 - 2*mu*x_i + mu^2)
= SUM(x_i^2) - 2*mu*SUM(x_i) + N*mu^2
Standard deviation rho^2 = SUM(...) / N. */
return sqrt(rho_squared);
}
void KRB5_CALLCONV
{
char *p;
/* Tweak this to only record data on "interesting" locks. */
return;
return;
if (p == NULL)
p = m->loc_created.filename;
else
p++;
(void *) m, m->loc_created.lineno, p);
else {
"\tlocked %d time%s; wait %lu/%f/%lu/%fus, hold %lu/%f/%lu/%fus\n",
sd_hold);
}
}
#else
/* On Windows, everything defined in the export list must be defined.
The UNIX systems where we're using the export list don't seem to
care. */
void KRB5_CALLCONV
{
}
void KRB5_CALLCONV
{
}
void KRB5_CALLCONV
{
}
#endif
/* Mutex allocation functions, for use in plugins that may not know
what options a given set of libraries was compiled with. */
int KRB5_CALLCONV
{
int err;
return errno;
if (err) {
return err;
}
*m = ptr;
return 0;
}
void KRB5_CALLCONV
{
(void) k5_mutex_destroy (m);
free (m);
}
/* Callable versions of the various macros. */
int KRB5_CALLCONV
{
return k5_mutex_lock (m);
}
int KRB5_CALLCONV
{
return k5_mutex_unlock (m);
}