From 6c80f7f9c5c504a9494e93be014e994f5a0ce14e Mon Sep 17 00:00:00 2001 From: Ken Raeburn Date: Sun, 14 Mar 2004 07:08:40 +0000 Subject: [PATCH] Add a mutex to protect the set manipulations * gssapiP_generic.h: Include k5-thread.h. (g_set): Add a mutex. (G_SET_INIT): Initialize it. * util_validate.c (g_save, g_validate, g_delete): Lock the mutex while working on the set. (BDB version untested.) git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@16167 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/gssapi/generic/ChangeLog | 6 ++ src/lib/gssapi/generic/gssapiP_generic.h | 6 +- src/lib/gssapi/generic/util_validate.c | 96 +++++++++++++++++++----- 3 files changed, 87 insertions(+), 21 deletions(-) diff --git a/src/lib/gssapi/generic/ChangeLog b/src/lib/gssapi/generic/ChangeLog index fb380c249..563eb2f06 100644 --- a/src/lib/gssapi/generic/ChangeLog +++ b/src/lib/gssapi/generic/ChangeLog @@ -14,6 +14,12 @@ g_delete_cred_id, g_delete_ctx_id): Updated first argument type. * gssapiP_generic.h: Declarations updated. + * gssapiP_generic.h: Include k5-thread.h. + (g_set): Add a mutex. + (G_SET_INIT): Initialize it. + * util_validate.c (g_save, g_validate, g_delete): Lock the mutex + while working on the set. (BDB version untested.) + 2004-02-08 Ken Raeburn * util_ordering.c (g_queue_externalize, g_queue_internalize): diff --git a/src/lib/gssapi/generic/gssapiP_generic.h b/src/lib/gssapi/generic/gssapiP_generic.h index ef2d6dd30..30afc5264 100644 --- a/src/lib/gssapi/generic/gssapiP_generic.h +++ b/src/lib/gssapi/generic/gssapiP_generic.h @@ -35,6 +35,8 @@ #endif #endif +#include "k5-thread.h" + #include "gssapi_generic.h" #include "gssapi_err_generic.h" @@ -134,10 +136,10 @@ typedef UINT64_TYPE gssint_uint64; typedef struct _g_set_elt *g_set_elt; typedef struct { - /* k5_mutex_t mutex; */ + k5_mutex_t mutex; void *data; } g_set; -#define G_SET_INIT { /* K5_MUTEX_INITIALIZER, */ 0 } +#define G_SET_INIT { K5_MUTEX_INITIALIZER, 0 } int g_set_init (g_set_elt *s); int g_set_destroy (g_set_elt *s); diff --git a/src/lib/gssapi/generic/util_validate.c b/src/lib/gssapi/generic/util_validate.c index 4c0c6d848..779260892 100644 --- a/src/lib/gssapi/generic/util_validate.c +++ b/src/lib/gssapi/generic/util_validate.c @@ -64,11 +64,18 @@ static int g_save(db, type, ptr) #endif void *ptr; { + int ret; #ifdef HAVE_BSD_DB - DB **vdb = (DB **) &db->data; + DB **vdb; vkey vk; DBT key; + ret = k5_mutex_lock(&db->mutex); + if (ret) + return 0; + + vdb = (DB **) &db->data; + if (!*vdb) *vdb = dbopen(NULL, O_CREAT|O_RDWR, O_CREAT|O_RDWR, DB_HASH, NULL); @@ -78,15 +85,27 @@ static int g_save(db, type, ptr) key.data = &vk; key.size = sizeof(vk); - return((*((*vdb)->put))(*vdb, &key, &dbtone, 0) == 0); + ret = ((*((*vdb)->put))(*vdb, &key, &dbtone, 0) == 0); + k5_mutex_unlock(&db->mutex); + return ret; #else - g_set_elt *gs = (g_set_elt *) &db->data; + g_set_elt *gs; + + ret = k5_mutex_lock(&db->mutex); + if (ret) + return 0; + + gs = (g_set_elt *) &db->data; if (!*gs) - if (g_set_init(gs)) + if (g_set_init(gs)) { + k5_mutex_unlock(&db->mutex); return(0); + } - return(g_set_entry_add(gs, ptr, type) == 0); + ret = (g_set_entry_add(gs, ptr, type) == 0); + k5_mutex_unlock(&db->mutex); + return ret; #endif } @@ -99,13 +118,21 @@ static int g_validate(db, type, ptr) #endif void *ptr; { + int ret; #ifdef HAVE_BSD_DB - DB **vdb = (DB **) &db->data; + DB **vdb; vkey vk; DBT key, value; - if (!*vdb) + ret = k5_mutex_lock(&db->mutex); + if (ret) + return 0; + + vdb = (DB **) &db->data; + if (!*vdb) { + k5_mutex_unlock(&db->mutex); return(0); + } vk.type = type; vk.ptr = ptr; @@ -113,21 +140,33 @@ static int g_validate(db, type, ptr) key.data = &vk; key.size = sizeof(vk); - if ((*((*vdb)->get))(*vdb, &key, &value, 0)) + if ((*((*vdb)->get))(*vdb, &key, &value, 0)) { + k5_mutex_unlock(&db->mutex); return(0); + } + k5_mutex_unlock(&db->mutex); return((value.size == sizeof(one)) && (*((int *) value.data) == one)); #else - g_set_elt *gs = (g_set_elt *) &db->data; + g_set_elt *gs; void *value; - if (!*gs) - return(0); + ret = k5_mutex_lock(&db->mutex); + if (ret) + return 0; - if (g_set_entry_get(gs, ptr, (void **) &value)) + gs = (g_set_elt *) &db->data; + if (!*gs) { + k5_mutex_unlock(&db->mutex); return(0); + } + if (g_set_entry_get(gs, ptr, (void **) &value)) { + k5_mutex_unlock(&db->mutex); + return(0); + } + k5_mutex_unlock(&db->mutex); return(value == type); #endif } @@ -141,13 +180,21 @@ static int g_delete(db, type, ptr) #endif void *ptr; { + int ret; #ifdef HAVE_BSD_DB - DB **vdb = (DB **) &db->data; + DB **vdb; vkey vk; DBT key; - if (!*vdb) + ret = k5_mutex_lock(&db->mutex); + if (ret) + return 0; + + vdb = (DB **) &db->data; + if (!*vdb) { + k5_mutex_unlock(&db->mutex); return(0); + } vk.type = type; vk.ptr = ptr; @@ -155,16 +202,27 @@ static int g_delete(db, type, ptr) key.data = &vk; key.size = sizeof(vk); - return((*((*vdb)->del))(*vdb, &key, 0) == 0); + ret = ((*((*vdb)->del))(*vdb, &key, 0) == 0); + k5_mutex_unlock(&db->mutex); + return ret; #else - g_set_elt *gs = (g_set_elt *) &db->data; + g_set_elt *gs; - if (!*gs) - return(0); + ret = k5_mutex_lock(&db->mutex); + if (ret) + return 0; - if (g_set_entry_delete(gs, ptr)) + gs = (g_set_elt *) &db->data; + if (!*gs) { + k5_mutex_unlock(&db->mutex); return(0); + } + if (g_set_entry_delete(gs, ptr)) { + k5_mutex_unlock(&db->mutex); + return(0); + } + k5_mutex_unlock(&db->mutex); return(1); #endif } -- 2.26.2