init.c revision 178825
1227825Stheraven/*
2227825Stheraven * Copyright (c) 1997 - 2001, 2003, 2006 Kungliga Tekniska H�gskolan
3227825Stheraven * (Royal Institute of Technology, Stockholm, Sweden).
4227825Stheraven * All rights reserved.
5227825Stheraven *
6227825Stheraven * Redistribution and use in source and binary forms, with or without
7227825Stheraven * modification, are permitted provided that the following conditions
8227825Stheraven * are met:
9227825Stheraven *
10227825Stheraven * 1. Redistributions of source code must retain the above copyright
11227825Stheraven *    notice, this list of conditions and the following disclaimer.
12227825Stheraven *
13227825Stheraven * 2. Redistributions in binary form must reproduce the above copyright
14227825Stheraven *    notice, this list of conditions and the following disclaimer in the
15227825Stheraven *    documentation and/or other materials provided with the distribution.
16227825Stheraven *
17227825Stheraven * 3. Neither the name of the Institute nor the names of its contributors
18227825Stheraven *    may be used to endorse or promote products derived from this software
19227825Stheraven *    without specific prior written permission.
20227825Stheraven *
21227825Stheraven * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22227825Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23227825Stheraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24227825Stheraven * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25227825Stheraven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26227825Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27227825Stheraven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28227825Stheraven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29227825Stheraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30227825Stheraven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31227825Stheraven * SUCH DAMAGE.
32234976Stheraven */
33227825Stheraven
34227825Stheraven#include "krb5/gsskrb5_locl.h"
35227825Stheraven
36246487StheravenRCSID("$Id: init.c 19031 2006-11-13 18:02:57Z lha $");
37227825Stheraven
38227825Stheravenstatic HEIMDAL_MUTEX context_mutex = HEIMDAL_MUTEX_INITIALIZER;
39227825Stheravenstatic int created_key;
40227825Stheravenstatic HEIMDAL_thread_key context_key;
41227825Stheraven
42227825Stheravenstatic void
43227825Stheravendestroy_context(void *ptr)
44227825Stheraven{
45246487Stheraven    krb5_context context = ptr;
46227825Stheraven
47227825Stheraven    if (context == NULL)
48227825Stheraven	return;
49227825Stheraven    krb5_free_context(context);
50227825Stheraven}
51234976Stheraven
52234976Stheravenkrb5_error_code
53234976Stheraven_gsskrb5_init (krb5_context *context)
54234976Stheraven{
55234976Stheraven    krb5_error_code ret = 0;
56227825Stheraven
57227825Stheraven    HEIMDAL_MUTEX_lock(&context_mutex);
58227825Stheraven
59227825Stheraven    if (!created_key) {
60227825Stheraven	HEIMDAL_key_create(&context_key, destroy_context, ret);
61227825Stheraven	if (ret) {
62234976Stheraven	    HEIMDAL_MUTEX_unlock(&context_mutex);
63227825Stheraven	    return ret;
64227825Stheraven	}
65234976Stheraven	created_key = 1;
66227825Stheraven    }
67227825Stheraven    HEIMDAL_MUTEX_unlock(&context_mutex);
68227825Stheraven
69234976Stheraven    *context = HEIMDAL_getspecific(context_key);
70227825Stheraven    if (*context == NULL) {
71227825Stheraven
72234976Stheraven	ret = krb5_init_context(context);
73227825Stheraven	if (ret == 0) {
74227825Stheraven	    HEIMDAL_setspecific(context_key, *context, ret);
75234976Stheraven	    if (ret) {
76227825Stheraven		krb5_free_context(*context);
77227825Stheraven		*context = NULL;
78234976Stheraven	    }
79227825Stheraven	}
80227825Stheraven    }
81234976Stheraven
82227825Stheraven    return ret;
83227825Stheraven}
84234976Stheraven