init.c revision 303975
1196239Simp/*
2196239Simp * Copyright (c) 1997 - 2001, 2003, 2006 Kungliga Tekniska H��gskolan
3196239Simp * (Royal Institute of Technology, Stockholm, Sweden).
4196239Simp * All rights reserved.
5196239Simp *
6196239Simp * Redistribution and use in source and binary forms, with or without
7196239Simp * modification, are permitted provided that the following conditions
8196239Simp * are met:
9196239Simp *
10196239Simp * 1. Redistributions of source code must retain the above copyright
11196239Simp *    notice, this list of conditions and the following disclaimer.
12196239Simp *
13196239Simp * 2. Redistributions in binary form must reproduce the above copyright
14196239Simp *    notice, this list of conditions and the following disclaimer in the
15196239Simp *    documentation and/or other materials provided with the distribution.
16196239Simp *
17196239Simp * 3. Neither the name of the Institute nor the names of its contributors
18196239Simp *    may be used to endorse or promote products derived from this software
19196239Simp *    without specific prior written permission.
20196239Simp *
21196239Simp * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22196239Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23196239Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24196239Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25196239Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26196239Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27196239Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28196239Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29196239Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30196239Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31196239Simp * SUCH DAMAGE.
32196239Simp */
33196239Simp
34196239Simp#include "gsskrb5_locl.h"
35196239Simp
36196239Simpstatic HEIMDAL_MUTEX context_mutex = HEIMDAL_MUTEX_INITIALIZER;
37196239Simpstatic int created_key;
38196239Simpstatic HEIMDAL_thread_key context_key;
39196239Simp
40196239Simpstatic void
41196239Simpdestroy_context(void *ptr)
42196239Simp{
43196239Simp    krb5_context context = ptr;
44196239Simp
45196239Simp    if (context == NULL)
46197003Simp	return;
47196239Simp    krb5_free_context(context);
48196239Simp}
49196239Simp
50196239Simpkrb5_error_code
51196239Simp_gsskrb5_init (krb5_context *context)
52196239Simp{
53196239Simp    krb5_error_code ret = 0;
54196239Simp
55196239Simp    HEIMDAL_MUTEX_lock(&context_mutex);
56196239Simp
57196239Simp    if (!created_key) {
58196239Simp	HEIMDAL_key_create(&context_key, destroy_context, ret);
59196239Simp	if (ret) {
60196239Simp	    HEIMDAL_MUTEX_unlock(&context_mutex);
61196239Simp	    return ret;
62196239Simp	}
63196239Simp	created_key = 1;
64196239Simp    }
65196239Simp    HEIMDAL_MUTEX_unlock(&context_mutex);
66196239Simp
67196239Simp    *context = HEIMDAL_getspecific(context_key);
68196239Simp    if (*context == NULL) {
69196239Simp
70196239Simp	ret = krb5_init_context(context);
71196239Simp	if (ret == 0) {
72196239Simp	    HEIMDAL_setspecific(context_key, *context, ret);
73196239Simp	    if (ret) {
74196239Simp		krb5_free_context(*context);
75196239Simp		*context = NULL;
76196239Simp	    }
77196239Simp	}
78196239Simp    }
79196239Simp
80196239Simp    return ret;
81196239Simp}
82196239Simp