From 796829ce620aea19964647fc1376bc3ff677beda Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2008 18:35:21 +0000 Subject: [PATCH] 2008-09-08 Dan Williams Patch from Alexander Sack * libnm-util/crypto_gnutls.c libnm-util/crypto_nss.c - (crypto_init, crypto_deinit): just use a boolean instead of a refcount * libnm-util/nm-utils.c libnm-util/nm-utils.h libnm-util/libnm-util.ver - (nm_utils_init): initialize libnm-util - (nm_utils_deinit): de-initialize libnm-util and clean up resources * libnm-util/nm-setting-8021x.c - (nm_setting_802_1x_class_init): init libnm-util when needed git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@4047 4912f4e0-d625-0410-9fb7-b9a5a253dbdc --- ChangeLog | 17 +++++++++++++++++ libnm-util/crypto_gnutls.c | 27 ++++++++++++++------------- libnm-util/crypto_nss.c | 32 +++++++++++++++++--------------- libnm-util/libnm-util.ver | 2 ++ libnm-util/nm-setting-8021x.c | 4 ++-- libnm-util/nm-utils.c | 31 +++++++++++++++++++++++++++++-- libnm-util/nm-utils.h | 4 ++++ 7 files changed, 85 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index 148036c83d..d9eb3d11d9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2008-09-08 Dan Williams + + Patch from Alexander Sack + + * libnm-util/crypto_gnutls.c + libnm-util/crypto_nss.c + - (crypto_init, crypto_deinit): just use a boolean instead of a refcount + + * libnm-util/nm-utils.c + libnm-util/nm-utils.h + libnm-util/libnm-util.ver + - (nm_utils_init): initialize libnm-util + - (nm_utils_deinit): de-initialize libnm-util and clean up resources + + * libnm-util/nm-setting-8021x.c + - (nm_setting_802_1x_class_init): init libnm-util when needed + 2008-09-05 Dan Williams Patch from Roy Marples and others diff --git a/libnm-util/crypto_gnutls.c b/libnm-util/crypto_gnutls.c index 922f379215..9613cbad9a 100644 --- a/libnm-util/crypto_gnutls.c +++ b/libnm-util/crypto_gnutls.c @@ -29,30 +29,31 @@ #include "crypto.h" -static guint32 refcount = 0; +static gboolean initialized = FALSE; gboolean crypto_init (GError **error) { - if (refcount == 0) { - if (gnutls_global_init() != 0) { - gnutls_global_deinit(); - g_set_error (error, NM_CRYPTO_ERROR, - NM_CRYPTO_ERR_INIT_FAILED, - "%s", - _("Failed to initialize the crypto engine.")); - return FALSE; - } + if (initialized) + return TRUE; + + if (gnutls_global_init() != 0) { + gnutls_global_deinit(); + g_set_error (error, NM_CRYPTO_ERROR, + NM_CRYPTO_ERR_INIT_FAILED, + "%s", + _("Failed to initialize the crypto engine.")); + return FALSE; } - refcount++; + + initialized = TRUE; return TRUE; } void crypto_deinit (void) { - refcount--; - if (refcount == 0) + if (initialized) gnutls_global_deinit(); } diff --git a/libnm-util/crypto_nss.c b/libnm-util/crypto_nss.c index fae4f6d4d2..d5ad3d0213 100644 --- a/libnm-util/crypto_nss.c +++ b/libnm-util/crypto_nss.c @@ -33,33 +33,35 @@ #include "crypto.h" -static guint32 refcount = 0; +static gboolean initialized = FALSE; gboolean crypto_init (GError **error) { - if (refcount == 0) { - SECStatus ret; + SECStatus ret; - PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 1); - ret = NSS_NoDB_Init (NULL); - if (ret != SECSuccess) { - g_set_error (error, NM_CRYPTO_ERROR, - NM_CRYPTO_ERR_INIT_FAILED, - _("Failed to initialize the crypto engine: %d."), - PR_GetError ()); - return FALSE; - } + if (initialized) + return TRUE; + + PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 1); + ret = NSS_NoDB_Init (NULL); + if (ret != SECSuccess) { + PR_Cleanup (); + g_set_error (error, NM_CRYPTO_ERROR, + NM_CRYPTO_ERR_INIT_FAILED, + _("Failed to initialize the crypto engine: %d."), + PR_GetError ()); + return FALSE; } - refcount++; + + initialized = TRUE; return TRUE; } void crypto_deinit (void) { - refcount--; - if (refcount == 0) { + if (initialized) { NSS_Shutdown (); PR_Cleanup (); } diff --git a/libnm-util/libnm-util.ver b/libnm-util/libnm-util.ver index 9f5fa87114..80f478302e 100644 --- a/libnm-util/libnm-util.ver +++ b/libnm-util/libnm-util.ver @@ -99,8 +99,10 @@ global: nm_setting_wireless_security_error_quark; nm_setting_wireless_security_get_type; nm_setting_wireless_security_new; + nm_utils_deinit; nm_utils_escape_ssid; nm_utils_gvalue_hash_dup; + nm_utils_init; nm_utils_ip4_addresses_from_gvalue; nm_utils_ip4_addresses_to_gvalue; nm_utils_ip4_netmask_to_prefix; diff --git a/libnm-util/nm-setting-8021x.c b/libnm-util/nm-setting-8021x.c index fd12f7820e..45caaf9f4d 100644 --- a/libnm-util/nm-setting-8021x.c +++ b/libnm-util/nm-setting-8021x.c @@ -902,8 +902,8 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE | NM_SETTING_PARAM_SECRET)); /* Initialize crypto lbrary. */ - if (!crypto_init (&error)) { - g_warning ("Couldn't initilize crypto system: %d %s", + if (!nm_utils_init (&error)) { + g_warning ("Couldn't initilize nm-utils/crypto system: %d %s", error->code, error->message); g_error_free (error); } diff --git a/libnm-util/nm-utils.c b/libnm-util/nm-utils.c index 0bca14e1f6..c5d3eea7ec 100644 --- a/libnm-util/nm-utils.c +++ b/libnm-util/nm-utils.c @@ -242,6 +242,34 @@ string_to_utf8 (const char *str, gsize len) return converted; } +/* init, deinit for libnm_util */ + +static gboolean initialized = FALSE; + +gboolean +nm_utils_init (GError **error) +{ + if (!initialized) { + if (!crypto_init (error)) { + return FALSE; + } + atexit (nm_utils_deinit); + initialized = TRUE; + } + return TRUE; +} + +void +nm_utils_deinit (void) +{ + if (initialized) { + crypto_deinit (); + initialized = FALSE; + } +} + +/* ssid helpers */ + char * nm_utils_ssid_to_utf8 (const char *ssid, guint32 len) { @@ -1128,7 +1156,7 @@ nm_utils_uuid_generate_from_string (const char *s) uuid_t *uuid; char *buf = NULL; - if (!crypto_init (&error)) { + if (!nm_utils_init (&error)) { nm_warning ("error initializing crypto: (%d) %s", error ? error->code : 0, error ? error->message : "unknown"); @@ -1152,7 +1180,6 @@ nm_utils_uuid_generate_from_string (const char *s) out: g_free (uuid); - crypto_deinit (); return buf; } diff --git a/libnm-util/nm-utils.h b/libnm-util/nm-utils.h index ff123ab3cd..7e66c58baa 100644 --- a/libnm-util/nm-utils.h +++ b/libnm-util/nm-utils.h @@ -136,6 +136,10 @@ G_STMT_START \ G_BREAKPOINT (); \ } G_STMT_END +/* init, deinit nm_utils */ +gboolean nm_utils_init (GError **error); +void nm_utils_deinit (void); + /* SSID helpers */ gboolean nm_utils_is_empty_ssid (const guint8 * ssid, int len); const char *nm_utils_escape_ssid (const guint8 *ssid, guint32 len);