krb5_context: embed mutex in structure

Instead of allocating a separate mutex object on the heap,
include the HEIMDAL_MUTEX in the krb5_context structure.

Change-Id: If6db484177410487176985e43e3b43e0f2166518
This commit is contained in:
Nicolas Williams
2016-03-31 22:06:37 -05:00
committed by Jeffrey Altman
parent 1f53a40827
commit c80816f9c3
5 changed files with 20 additions and 38 deletions

View File

@@ -422,12 +422,7 @@ krb5_init_context(krb5_context *context)
if(!p) if(!p)
return ENOMEM; return ENOMEM;
p->mutex = malloc(sizeof(HEIMDAL_MUTEX)); HEIMDAL_MUTEX_init(&p->mutex);
if (p->mutex == NULL) {
free(p);
return ENOMEM;
}
HEIMDAL_MUTEX_init(p->mutex);
p->flags |= KRB5_CTX_F_HOMEDIR_ACCESS; p->flags |= KRB5_CTX_F_HOMEDIR_ACCESS;
@@ -520,13 +515,7 @@ krb5_copy_context(krb5_context context, krb5_context *out)
if (p == NULL) if (p == NULL)
return krb5_enomem(context); return krb5_enomem(context);
p->mutex = malloc(sizeof(HEIMDAL_MUTEX)); HEIMDAL_MUTEX_init(&p->mutex);
if (p->mutex == NULL) {
free(p);
return krb5_enomem(context);
}
HEIMDAL_MUTEX_init(p->mutex);
if (context->default_cc_name) if (context->default_cc_name)
p->default_cc_name = strdup(context->default_cc_name); p->default_cc_name = strdup(context->default_cc_name);
@@ -627,8 +616,7 @@ krb5_free_context(krb5_context context)
hx509_context_free(&context->hx509ctx); hx509_context_free(&context->hx509ctx);
#endif #endif
HEIMDAL_MUTEX_destroy(context->mutex); HEIMDAL_MUTEX_destroy(&context->mutex);
free(context->mutex);
if (context->flags & KRB5_CTX_F_SOCKETS_INITIALIZED) { if (context->flags & KRB5_CTX_F_SOCKETS_INITIALIZED) {
rk_SOCK_EXIT(); rk_SOCK_EXIT();
} }

View File

@@ -49,12 +49,7 @@ krb5_init_context(krb5_context *context)
if(!p) if(!p)
return ENOMEM; return ENOMEM;
p->mutex = malloc(sizeof(HEIMDAL_MUTEX)); HEIMDAL_MUTEX_init(&p->mutex);
if (p->mutex == NULL) {
free(p);
return ENOMEM;
}
HEIMDAL_MUTEX_init(p->mutex);
*context = p; *context = p;
return 0; return 0;
@@ -65,8 +60,7 @@ krb5_free_context(krb5_context context)
{ {
krb5_clear_error_message(context); krb5_clear_error_message(context);
HEIMDAL_MUTEX_destroy(context->mutex); HEIMDAL_MUTEX_destroy(&context->mutex);
free(context->mutex);
if (context->flags & KRB5_CTX_F_SOCKETS_INITIALIZED) { if (context->flags & KRB5_CTX_F_SOCKETS_INITIALIZED) {
rk_SOCK_EXIT(); rk_SOCK_EXIT();
} }

View File

@@ -623,10 +623,10 @@ krb5_get_error_string(krb5_context context)
{ {
char *ret = NULL; char *ret = NULL;
HEIMDAL_MUTEX_lock(context->mutex); HEIMDAL_MUTEX_lock(&context->mutex);
if (context->error_string) if (context->error_string)
ret = strdup(context->error_string); ret = strdup(context->error_string);
HEIMDAL_MUTEX_unlock(context->mutex); HEIMDAL_MUTEX_unlock(&context->mutex);
return ret; return ret;
} }
@@ -635,9 +635,9 @@ krb5_have_error_string(krb5_context context)
KRB5_DEPRECATED_FUNCTION("Use krb5_get_error_message instead") KRB5_DEPRECATED_FUNCTION("Use krb5_get_error_message instead")
{ {
char *str; char *str;
HEIMDAL_MUTEX_lock(context->mutex); HEIMDAL_MUTEX_lock(&context->mutex);
str = context->error_string; str = context->error_string;
HEIMDAL_MUTEX_unlock(context->mutex); HEIMDAL_MUTEX_unlock(&context->mutex);
return str != NULL; return str != NULL;
} }

View File

@@ -47,12 +47,12 @@
KRB5_LIB_FUNCTION void KRB5_LIB_CALL KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_clear_error_message(krb5_context context) krb5_clear_error_message(krb5_context context)
{ {
HEIMDAL_MUTEX_lock(context->mutex); HEIMDAL_MUTEX_lock(&context->mutex);
if (context->error_string) if (context->error_string)
free(context->error_string); free(context->error_string);
context->error_code = 0; context->error_code = 0;
context->error_string = NULL; context->error_string = NULL;
HEIMDAL_MUTEX_unlock(context->mutex); HEIMDAL_MUTEX_unlock(&context->mutex);
} }
/** /**
@@ -105,7 +105,7 @@ krb5_vset_error_message (krb5_context context, krb5_error_code ret,
if (context == NULL) if (context == NULL)
return; return;
HEIMDAL_MUTEX_lock(context->mutex); HEIMDAL_MUTEX_lock(&context->mutex);
if (context->error_string) { if (context->error_string) {
free(context->error_string); free(context->error_string);
context->error_string = NULL; context->error_string = NULL;
@@ -114,7 +114,7 @@ krb5_vset_error_message (krb5_context context, krb5_error_code ret,
r = vasprintf(&context->error_string, fmt, args); r = vasprintf(&context->error_string, fmt, args);
if (r < 0) if (r < 0)
context->error_string = NULL; context->error_string = NULL;
HEIMDAL_MUTEX_unlock(context->mutex); HEIMDAL_MUTEX_unlock(&context->mutex);
if (context->error_string) if (context->error_string)
_krb5_debug(context, 100, "error message: %s: %d", context->error_string, ret); _krb5_debug(context, 100, "error message: %s: %d", context->error_string, ret);
} }
@@ -168,13 +168,13 @@ krb5_vprepend_error_message(krb5_context context, krb5_error_code ret,
if (context == NULL) if (context == NULL)
return; return;
HEIMDAL_MUTEX_lock(context->mutex); HEIMDAL_MUTEX_lock(&context->mutex);
if (context->error_code != ret) { if (context->error_code != ret) {
HEIMDAL_MUTEX_unlock(context->mutex); HEIMDAL_MUTEX_unlock(&context->mutex);
return; return;
} }
if (vasprintf(&str, fmt, args) < 0 || str == NULL) { if (vasprintf(&str, fmt, args) < 0 || str == NULL) {
HEIMDAL_MUTEX_unlock(context->mutex); HEIMDAL_MUTEX_unlock(&context->mutex);
return; return;
} }
if (context->error_string) { if (context->error_string) {
@@ -189,7 +189,7 @@ krb5_vprepend_error_message(krb5_context context, krb5_error_code ret,
free(str); free(str);
} else } else
context->error_string = str; context->error_string = str;
HEIMDAL_MUTEX_unlock(context->mutex); HEIMDAL_MUTEX_unlock(&context->mutex);
} }
/** /**
@@ -226,13 +226,13 @@ krb5_get_error_message(krb5_context context, krb5_error_code code)
*/ */
if (context) if (context)
{ {
HEIMDAL_MUTEX_lock(context->mutex); HEIMDAL_MUTEX_lock(&context->mutex);
if (context->error_string && if (context->error_string &&
(code == context->error_code || context->error_code == 0)) (code == context->error_code || context->error_code == 0))
{ {
str = strdup(context->error_string); str = strdup(context->error_string);
} }
HEIMDAL_MUTEX_unlock(context->mutex); HEIMDAL_MUTEX_unlock(&context->mutex);
if (str) if (str)
return str; return str;

View File

@@ -292,7 +292,7 @@ typedef struct krb5_context_data {
char *default_cc_name; char *default_cc_name;
char *default_cc_name_env; char *default_cc_name_env;
int default_cc_name_set; int default_cc_name_set;
void *mutex; /* protects error_string */ HEIMDAL_MUTEX mutex; /* protects error_string */
int large_msg_size; int large_msg_size;
int max_msg_size; int max_msg_size;
int tgs_negative_timeout; /* timeout for TGS negative cache */ int tgs_negative_timeout; /* timeout for TGS negative cache */