[Freeswitch-svn] [commit] r10727 - freeswitch/branches/cseket/src/mod/applications/mod_voicemail

FreeSWITCH SVN cseket at freeswitch.org
Fri Dec 12 00:58:45 PST 2008


Author: cseket
Date: Fri Dec 12 03:58:45 2008
New Revision: 10727

Log:
incomplete profile rescan (MODAPP-177)

Modified:
   freeswitch/branches/cseket/src/mod/applications/mod_voicemail/mod_voicemail.c

Modified: freeswitch/branches/cseket/src/mod/applications/mod_voicemail/mod_voicemail.c
==============================================================================
--- freeswitch/branches/cseket/src/mod/applications/mod_voicemail/mod_voicemail.c	(original)
+++ freeswitch/branches/cseket/src/mod/applications/mod_voicemail/mod_voicemail.c	Fri Dec 12 03:58:45 2008
@@ -54,6 +54,7 @@
 static struct {
 	switch_hash_t *profile_hash;
 	int debug;
+	switch_mutex_t *mutex;
 	switch_memory_pool_t *pool;
 } globals;
 
@@ -125,6 +126,8 @@
 	uint32_t record_sample_rate;
 	switch_odbc_handle_t *master_odbc;
 	switch_bool_t auto_playback_recordings;
+	switch_thread_rwlock_t *rwlock;
+	switch_memory_pool_t *pool;
 };
 typedef struct vm_profile vm_profile_t;
 
@@ -232,15 +235,41 @@
 	"   password        VARCHAR(255)\n" 
 	");\n";
 
+static void destroy_profile(vm_profile_t *profile) 
+{
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Destroying Profile %s\n", profile->name);
+#ifdef SWITCH_HAVE_ODBC
+	if (profile->odbc_dsn && profile->master_odbc) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Closing ODBC Database! %s\n", profile->name);
+		switch_odbc_handle_destroy(&profile->master_odbc);
+	}
+#endif
+	switch_core_destroy_memory_pool(&profile->pool);
+}
+
+static vm_profile_t *create_profile(char *name) 
+{
+	vm_profile_t *profile = NULL;
+	switch_memory_pool_t *pool;
+
+	switch_core_new_memory_pool(&pool);
+
+	profile = switch_core_alloc(pool, sizeof(*profile));
+	profile->pool = pool;
+	switch_thread_rwlock_create(&profile->rwlock, pool);
+	profile->name = switch_core_strdup(pool, name);
+
+	return profile;
+}
+
+
 static switch_status_t load_config(void)
 {
 	char *cf = "voicemail.conf";
-	vm_profile_t *profile = NULL;
+	vm_profile_t *profile = NULL, *oldprofile = NULL;
 	switch_xml_t cfg, xml, settings, param, x_profile, x_profiles, x_email;
-
-	memset(&globals, 0, sizeof(globals));
-	switch_core_new_memory_pool(&globals.pool);
-	switch_core_hash_init(&globals.profile_hash, globals.pool);
+	switch_hash_index_t *hi;
+	void *val;
 
 	if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf);
@@ -262,6 +291,38 @@
 		goto end;
 	}
 
+
+	/* destroy old profiles */
+  destroy:
+	for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
+		switch_hash_this(hi, NULL, NULL, &val);
+		profile = (vm_profile_t *) val;
+		int found = 0;
+
+		for (x_profile = switch_xml_child(x_profiles, "profile"); x_profile; x_profile = x_profile->next) {
+			char *name = (char *) switch_xml_attr_soft(x_profile, "name");
+			if (!(strcasecmp(profile->name, name))) {
+				found = 1;
+				break;
+			}
+		}
+		if (found) {
+			continue;
+		}
+			
+		/* delete from hash, so new comers can't find it */
+		switch_mutex_lock(globals.mutex);
+		switch_core_hash_delete(globals.profile_hash, profile->name);
+		switch_mutex_unlock(globals.mutex);
+
+		/* wait for readers */
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Deleting profile %s, when readers are left\n", profile->name);
+		switch_thread_rwlock_wrlock(profile->rwlock);
+
+		destroy_profile(profile);
+		goto destroy;
+	}
+
 	for (x_profile = switch_xml_child(x_profiles, "profile"); x_profile; x_profile = x_profile->next) {
 		char *name = (char *) switch_xml_attr_soft(x_profile, "name");
 		char *odbc_dsn = NULL, *odbc_user = NULL, *odbc_pass = NULL;
@@ -323,13 +384,21 @@
 
 		db = NULL;
 
+		if (switch_strlen_zero(name)) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No name specified.\n");
+			continue;
+		}
+
+		oldprofile = switch_core_hash_find(globals.profile_hash, name);
+		profile = create_profile(name);
+
 		if ((x_email = switch_xml_child(x_profile, "email"))) {
 			if ((param = switch_xml_child(x_email, "body"))) {
-				email_body = switch_core_strdup(globals.pool, param->txt);
+				email_body = switch_core_strdup(profile->pool, param->txt);
 			}
 
 			if ((param = switch_xml_child(x_email, "headers"))) {
-				email_headers = switch_core_strdup(globals.pool, param->txt);
+				email_headers = switch_core_strdup(profile->pool, param->txt);
 			}
 
 			for (param = switch_xml_child(x_email, "param"); param; param = param->next) {
@@ -588,7 +657,7 @@
 				}
 			} else if (!strcasecmp(var, "odbc-dsn") && !switch_strlen_zero(val)) {
 #ifdef SWITCH_HAVE_ODBC
-				odbc_dsn = switch_core_strdup(globals.pool, val);
+				odbc_dsn = switch_core_strdup(profile->pool, val);
 				if ((odbc_user = strchr(odbc_dsn, ':'))) {
 					*odbc_user++ = '\0';
 					if ((odbc_pass = strchr(odbc_user, ':'))) {
@@ -603,165 +672,173 @@
 			}
 		}
 
-		if (switch_strlen_zero(name)) {
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No name specified.\n");
-		} else {
-			profile = switch_core_alloc(globals.pool, sizeof(*profile));
-			profile->name = switch_core_strdup(globals.pool, name);
 
-			if (!switch_strlen_zero(odbc_dsn) && !switch_strlen_zero(odbc_user) && !switch_strlen_zero(odbc_pass)) {
-				profile->odbc_dsn = odbc_dsn;
-				profile->odbc_user = odbc_user;
-				profile->odbc_pass = odbc_pass;
-			} else {
-				profile->dbname = switch_core_sprintf(globals.pool, "voicemail_%s", name);
-			}
-			if (profile->odbc_dsn) {
+		if (!switch_strlen_zero(odbc_dsn) && !switch_strlen_zero(odbc_user) && !switch_strlen_zero(odbc_pass)) {
+			profile->odbc_dsn = odbc_dsn;
+			profile->odbc_user = odbc_user;
+			profile->odbc_pass = odbc_pass;
+		} else {
+			profile->dbname = switch_core_sprintf(profile->pool, "voicemail_%s", name);
+		}
+		if (profile->odbc_dsn) {
 #ifdef SWITCH_HAVE_ODBC
-				if (!(profile->master_odbc = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
-					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
-					continue;
+			if (!(profile->master_odbc = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
+				continue;
 
-				}
-				if (switch_odbc_handle_connect(profile->master_odbc) != SWITCH_ODBC_SUCCESS) {
-					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
-					continue;
-				}
+			}
+			if (switch_odbc_handle_connect(profile->master_odbc) != SWITCH_ODBC_SUCCESS) {
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
+				continue;
+			}
 
-				if (switch_odbc_handle_exec(profile->master_odbc, "select count(message_len) from voicemail_msgs", NULL) != SWITCH_ODBC_SUCCESS) {
-					switch_odbc_handle_exec(profile->master_odbc, "drop table voicemail_msgs", NULL);
-					switch_odbc_handle_exec(profile->master_odbc, vm_sql, NULL);
-				}
+			if (switch_odbc_handle_exec(profile->master_odbc, "select count(message_len) from voicemail_msgs", NULL) != SWITCH_ODBC_SUCCESS) {
+				switch_odbc_handle_exec(profile->master_odbc, "drop table voicemail_msgs", NULL);
+				switch_odbc_handle_exec(profile->master_odbc, vm_sql, NULL);
+			}
 
-				if (switch_odbc_handle_exec(profile->master_odbc, "select count(username) from voicemail_prefs", NULL) != SWITCH_ODBC_SUCCESS) {
-					switch_odbc_handle_exec(profile->master_odbc, "drop table voicemail_prefs", NULL);
-					switch_odbc_handle_exec(profile->master_odbc, vm_pref_sql, NULL);
-				}
+			if (switch_odbc_handle_exec(profile->master_odbc, "select count(username) from voicemail_prefs", NULL) != SWITCH_ODBC_SUCCESS) {
+				switch_odbc_handle_exec(profile->master_odbc, "drop table voicemail_prefs", NULL);
+				switch_odbc_handle_exec(profile->master_odbc, vm_pref_sql, NULL);
+			}
 
-				if (switch_odbc_handle_exec(profile->master_odbc, "select count(password) from voicemail_prefs", NULL) != SWITCH_ODBC_SUCCESS) {
-					switch_odbc_handle_exec(profile->master_odbc, "alter table voicemail_prefs add password varchar(255)", NULL);
-				}
+			if (switch_odbc_handle_exec(profile->master_odbc, "select count(password) from voicemail_prefs", NULL) != SWITCH_ODBC_SUCCESS) {
+				switch_odbc_handle_exec(profile->master_odbc, "alter table voicemail_prefs add password varchar(255)", NULL);
+			}
 
-				if (switch_odbc_handle_exec(profile->master_odbc, "select count(message_len) from voicemail_data", NULL) == SWITCH_ODBC_SUCCESS) {
-					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Old table voicemail_data found, migrating data!\n");
-					/* XXX: Old table found.. migrating data into new table */
-					if (switch_odbc_handle_exec(profile->master_odbc,
-												"insert into voicemail_msgs select created_epoch, read_epoch, user as username, domain, uuid,"
-												"cid_name, cid_number, in_folder, file_path, message_len, flags, read_flags from voicemail_data",
-												NULL) != SWITCH_ODBC_SUCCESS) {
-						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failed to migrate old voicemail_data to voicemail_msgs!\n");
-						continue;
-					}
-					switch_odbc_handle_exec(profile->master_odbc, "drop table voicemail_data", NULL);
+			if (switch_odbc_handle_exec(profile->master_odbc, "select count(message_len) from voicemail_data", NULL) == SWITCH_ODBC_SUCCESS) {
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Old table voicemail_data found, migrating data!\n");
+				/* XXX: Old table found.. migrating data into new table */
+				if (switch_odbc_handle_exec(profile->master_odbc,
+											"insert into voicemail_msgs select created_epoch, read_epoch, user as username, domain, uuid,"
+											"cid_name, cid_number, in_folder, file_path, message_len, flags, read_flags from voicemail_data",
+											NULL) != SWITCH_ODBC_SUCCESS) {
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failed to migrate old voicemail_data to voicemail_msgs!\n");
+					continue;
 				}
+				switch_odbc_handle_exec(profile->master_odbc, "drop table voicemail_data", NULL);
+			}
 #endif
-			} else {
-				if ((db = switch_core_db_open_file(profile->dbname))) {
-					char *errmsg;
-					switch_core_db_test_reactive(db, "select count(message_len) from voicemail_msgs", "drop table voicemail_msgs", vm_sql);
-
-					switch_core_db_exec(db, "select count(message_len) from voicemail_data", NULL, NULL, &errmsg);
+		} else {
+			if ((db = switch_core_db_open_file(profile->dbname))) {
+				char *errmsg;
+				switch_core_db_test_reactive(db, "select count(message_len) from voicemail_msgs", "drop table voicemail_msgs", vm_sql);
+
+				switch_core_db_exec(db, "select count(message_len) from voicemail_data", NULL, NULL, &errmsg);
+				if (errmsg) {
+					switch_core_db_free(errmsg);
+					errmsg = NULL;
+				} else {
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Migrating data from voicemail_data to voicemail_msgs!\n");
+					switch_core_db_exec(db, "insert into voicemail_msgs select created_epoch, read_epoch, user as username, domain, uuid,"
+										"cid_name, cid_number, in_folder, file_path, message_len, flags, read_flags from voicemail_data",
+										NULL, NULL, &errmsg);
 					if (errmsg) {
+						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n", errmsg);
 						switch_core_db_free(errmsg);
 						errmsg = NULL;
-					} else {
-						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Migrating data from voicemail_data to voicemail_msgs!\n");
-						switch_core_db_exec(db, "insert into voicemail_msgs select created_epoch, read_epoch, user as username, domain, uuid,"
-											"cid_name, cid_number, in_folder, file_path, message_len, flags, read_flags from voicemail_data",
-											NULL, NULL, &errmsg);
-						if (errmsg) {
-							switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n", errmsg);
-							switch_core_db_free(errmsg);
-							errmsg = NULL;
-						}
-						switch_core_db_exec(db, "drop table voicemail_data", NULL, NULL, &errmsg);
-						if (errmsg) {
-							switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n", errmsg);
-							switch_core_db_free(errmsg);
-							errmsg = NULL;
-						}
 					}
+					switch_core_db_exec(db, "drop table voicemail_data", NULL, NULL, &errmsg);
+					if (errmsg) {
+						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n", errmsg);
+						switch_core_db_free(errmsg);
+						errmsg = NULL;
+					}
+				}
 
 
-					switch_core_db_test_reactive(db, "select count(username) from voicemail_prefs", "drop table voicemail_prefs", vm_pref_sql);
-					switch_core_db_test_reactive(db, "select count(password) from voicemail_prefs", NULL, 
+				switch_core_db_test_reactive(db, "select count(username) from voicemail_prefs", "drop table voicemail_prefs", vm_pref_sql);
+				switch_core_db_test_reactive(db, "select count(password) from voicemail_prefs", NULL, 
 												 "alter table voicemail_prefs add password varchar(255)");
-				} else {
-					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open SQL Database!\n");
-					continue;
-				}
-				switch_core_db_close(db);
+			} else {
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open SQL Database!\n");
+				continue;
 			}
+			switch_core_db_close(db);
+		}
 
-			profile->web_head = web_head;
-			profile->web_tail = web_tail;
+		profile->web_head = web_head;
+		profile->web_tail = web_tail;
 
-			profile->email_body = email_body;
-			profile->email_headers = email_headers;
-			if (notify_email_headers) {
-				profile->notify_email_body = notify_email_body;
-				profile->notify_email_headers = notify_email_headers;
-			} else {
-				profile->notify_email_body = email_body;
-				profile->notify_email_headers = email_headers;
-			}
-			profile->email_from = switch_core_strdup(globals.pool, email_from);
-			profile->date_fmt = switch_core_strdup(globals.pool, date_fmt);
-			profile->play_date_announcement = play_date_announcement;
-
-			profile->digit_timeout = timeout;
-			profile->max_login_attempts = max_login_attempts;
-			profile->min_record_len = min_record_len;
-			profile->max_record_len = max_record_len;
-			*profile->terminator_key = *terminator_key;
-			*profile->play_new_messages_key = *play_new_messages_key;
-			*profile->play_saved_messages_key = *play_saved_messages_key;
-			*profile->main_menu_key = *main_menu_key;
-			*profile->config_menu_key = *config_menu_key;
-			*profile->record_greeting_key = *record_greeting_key;
-			*profile->choose_greeting_key = *choose_greeting_key;
-			*profile->record_name_key = *record_name_key;
-			*profile->change_pass_key = *change_pass_key;
-			*profile->record_file_key = *record_file_key;
-			*profile->listen_file_key = *listen_file_key;
-			*profile->save_file_key = *save_file_key;
-			*profile->delete_file_key = *delete_file_key;
-			*profile->undelete_file_key = *undelete_file_key;
-			*profile->email_key = *email_key;
-			*profile->callback_key = *callback_key;
-			*profile->pause_key = *pause_key;
-			*profile->restart_key = *restart_key;
-			*profile->ff_key = *ff_key;
-			*profile->rew_key = *rew_key;
-			*profile->urgent_key = *urgent_key;
-			*profile->operator_key = *operator_key;
-			*profile->vmain_key = *vmain_key;
-			*profile->forward_key = *forward_key;
-			*profile->prepend_key = *prepend_key;
-			profile->record_threshold = record_threshold;
-			profile->record_silence_hits = record_silence_hits;
-			profile->record_sample_rate = record_sample_rate;
-
-			profile->auto_playback_recordings = auto_playback_recordings;
-			profile->operator_ext = switch_core_strdup(globals.pool, operator_ext);
-			profile->vmain_ext = switch_core_strdup(globals.pool, vmain_ext);
-			profile->storage_dir = switch_core_strdup(globals.pool, storage_dir);
-			profile->tone_spec = switch_core_strdup(globals.pool, tone_spec);
-			profile->callback_dialplan = switch_core_strdup(globals.pool, callback_dialplan);
-			profile->callback_context = switch_core_strdup(globals.pool, callback_context);
-
-			profile->record_title = switch_core_strdup(globals.pool, record_title);
-			profile->record_comment = switch_core_strdup(globals.pool, record_comment);
-			profile->record_copyright = switch_core_strdup(globals.pool, record_copyright);
+		profile->email_body = email_body;
+		profile->email_headers = email_headers;
+		if (notify_email_headers) {
+			profile->notify_email_body = notify_email_body;
+			profile->notify_email_headers = notify_email_headers;
+		} else {
+			profile->notify_email_body = email_body;
+			profile->notify_email_headers = email_headers;
+		}
+		profile->email_from = switch_core_strdup(profile->pool, email_from);
+		profile->date_fmt = switch_core_strdup(profile->pool, date_fmt);
+		profile->play_date_announcement = play_date_announcement;
+
+		profile->digit_timeout = timeout;
+		profile->max_login_attempts = max_login_attempts;
+		profile->min_record_len = min_record_len;
+		profile->max_record_len = max_record_len;
+		*profile->terminator_key = *terminator_key;
+		*profile->play_new_messages_key = *play_new_messages_key;
+		*profile->play_saved_messages_key = *play_saved_messages_key;
+		*profile->main_menu_key = *main_menu_key;
+		*profile->config_menu_key = *config_menu_key;
+		*profile->record_greeting_key = *record_greeting_key;
+		*profile->choose_greeting_key = *choose_greeting_key;
+		*profile->record_name_key = *record_name_key;
+		*profile->change_pass_key = *change_pass_key;
+		*profile->record_file_key = *record_file_key;
+		*profile->listen_file_key = *listen_file_key;
+		*profile->save_file_key = *save_file_key;
+		*profile->delete_file_key = *delete_file_key;
+		*profile->undelete_file_key = *undelete_file_key;
+		*profile->email_key = *email_key;
+		*profile->callback_key = *callback_key;
+		*profile->pause_key = *pause_key;
+		*profile->restart_key = *restart_key;
+		*profile->ff_key = *ff_key;
+		*profile->rew_key = *rew_key;
+		*profile->urgent_key = *urgent_key;
+		*profile->operator_key = *operator_key;
+		*profile->vmain_key = *vmain_key;
+		*profile->forward_key = *forward_key;
+		*profile->prepend_key = *prepend_key;
+		profile->record_threshold = record_threshold;
+		profile->record_silence_hits = record_silence_hits;
+		profile->record_sample_rate = record_sample_rate;
+		profile->auto_playback_recordings = auto_playback_recordings;
+		profile->operator_ext = switch_core_strdup(profile->pool, operator_ext);
+		profile->vmain_ext = switch_core_strdup(profile->pool, vmain_ext);
+		profile->storage_dir = switch_core_strdup(profile->pool, storage_dir);
+		profile->tone_spec = switch_core_strdup(profile->pool, tone_spec);
+		profile->callback_dialplan = switch_core_strdup(profile->pool, callback_dialplan);
+		profile->callback_context = switch_core_strdup(profile->pool, callback_context);
+		profile->record_title = switch_core_strdup(profile->pool, record_title);
+		profile->record_comment = switch_core_strdup(profile->pool, record_comment);
+		profile->record_copyright = switch_core_strdup(profile->pool, record_copyright);
+		switch_copy_string(profile->file_ext, file_ext, sizeof(profile->file_ext));
+		switch_mutex_init(&profile->mutex, SWITCH_MUTEX_UNNESTED, profile->pool);
+
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Added Profile %s\n", profile->name);
+		if (oldprofile) {
+
+			/* wait */
+            /* deadlock on concurrent voicemail rescan */
+			while (switch_thread_rwlock_trywrlock_timeout(oldprofile->rwlock, 10) != SWITCH_STATUS_SUCCESS);
+				
+			switch_mutex_lock(globals.mutex);
+			/* change the profile*/
+			switch_core_hash_delete(globals.profile_hash, oldprofile->name);
+			switch_core_hash_insert(globals.profile_hash, profile->name, profile);
+			switch_mutex_unlock(globals.mutex);
 
-			switch_copy_string(profile->file_ext, file_ext, sizeof(profile->file_ext));
-			switch_mutex_init(&profile->mutex, SWITCH_MUTEX_NESTED, globals.pool);
+			destroy_profile(oldprofile);
 
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Added Profile %s\n", profile->name);
+		} else {
 			switch_core_hash_insert(globals.profile_hash, profile->name, profile);
 		}
 	}
 
+
   end:
 	switch_xml_free(xml);
 	return SWITCH_STATUS_SUCCESS;
@@ -1626,11 +1703,16 @@
 		caller_id_number = caller_profile->caller_id_number;
 	}
 
+	switch_mutex_lock(globals.mutex);
+
 	if (!(profile = switch_core_hash_find(globals.profile_hash, profile_name))) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error invalid profile %s\n", profile_name);
+		switch_mutex_unlock(globals.mutex);
 		return;
 	}
-
+	switch_thread_rwlock_rdlock(profile->rwlock);
+	switch_mutex_unlock(globals.mutex);
+	
 	timeout = profile->digit_timeout;
 	attempts = profile->max_login_attempts;
 	status = switch_ivr_phrase_macro(session, VM_HELLO_MACRO, NULL, NULL, &args);
@@ -2146,6 +2228,8 @@
 
   end:
 
+	switch_thread_rwlock_unlock(profile->rwlock);
+
 	if (switch_channel_ready(channel)) {
 		if (failed) {
 			status = switch_ivr_phrase_macro(session, VM_ABORT_MACRO, NULL, NULL, NULL);
@@ -2548,6 +2632,7 @@
 		goto end;
 	}
 
+	switch_mutex_lock(globals.mutex);
 	if (!(profile = switch_core_hash_find(globals.profile_hash, domain))) {
 		profile = switch_core_hash_find(globals.profile_hash, "default");
 	}
@@ -2555,11 +2640,15 @@
 	if (!profile) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find profile\n");
 		status = SWITCH_STATUS_FALSE;
+		switch_mutex_unlock(globals.mutex);
 	} else {
 		switch_xml_t x_domain, xml_root;
 		switch_event_t *my_params = NULL;
 		switch_xml_t ut;
 
+		switch_thread_rwlock_rdlock(profile->rwlock);
+		switch_mutex_unlock(globals.mutex);
+
 		switch_event_create(&my_params, SWITCH_EVENT_REQUEST_PARAMS);
 		switch_assert(my_params);
 		switch_event_add_header_string(my_params, SWITCH_STACK_BOTTOM, "domain", domain);
@@ -2595,6 +2684,7 @@
 				}
 			}
 		}
+		switch_thread_rwlock_unlock(profile->rwlock);
 		
 		switch_core_destroy_memory_pool(&pool);
 
@@ -2658,11 +2748,18 @@
 	}
 
 	memset(&cbt, 0, sizeof(cbt));
+
+	switch_mutex_lock(globals.mutex);
+
 	if (!(profile = switch_core_hash_find(globals.profile_hash, profile_name))) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error invalid profile %s\n", profile_name);
-		return SWITCH_STATUS_FALSE;
+		switch_mutex_unlock(globals.mutex);
+ 		return SWITCH_STATUS_FALSE;;
 	}
 
+	switch_thread_rwlock_rdlock(profile->rwlock);
+	switch_mutex_unlock(globals.mutex);
+
 	if (id) {
 		int ok = 1;
 		switch_event_t *params = NULL;
@@ -2903,6 +3000,8 @@
 
  end:
 
+	switch_thread_rwlock_unlock(profile->rwlock);
+
 	if (x_domain_root) {
 		switch_xml_free(x_domain_root);
 		x_domain_root = NULL;
@@ -3025,7 +3124,7 @@
 				*p++ = '\0';
 				how = p;
 			}
-
+			switch_mutex_lock(globals.mutex);
 			for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
 				switch_hash_this(hi, NULL, NULL, &val);
 				profile = (vm_profile_t *) val;
@@ -3033,6 +3132,7 @@
 				message_count(profile, id, domain, "inbox", &total_new_messages, &total_saved_messages,
 							  &total_new_urgent_messages, &total_saved_urgent_messages);
 			}
+			switch_mutex_unlock(globals.mutex);
 		}
 
 		switch_safe_free(id);
@@ -3132,7 +3232,6 @@
 
 }
 
-#define VOICEMAIL_SYNTAX "rss [<host> <port> <uri> <user> <domain>]"
 
 struct holder {
 	vm_profile_t *profile;
@@ -3565,6 +3664,7 @@
 	return SWITCH_STATUS_SUCCESS;
 }
 
+#define VOICEMAIL_SYNTAX "rss [<host> <port> <uri> <user> <domain>] | rescan [reloadxml]"
 SWITCH_STANDARD_API(voicemail_api_function)
 {
 	int argc = 0;
@@ -3575,6 +3675,8 @@
 	vm_profile_t *profile = NULL;
 	char *path_info = NULL;
 	int rss = 0, xarg = 0;
+	switch_hash_index_t *hi;
+	void *val = NULL;
 
 	if (session) {
 		return SWITCH_STATUS_FALSE;
@@ -3599,6 +3701,34 @@
 		if (!strcasecmp(argv[0], "rss")) {
 			rss++;
 			xarg++;
+		} else if (!strcasecmp(argv[0], "rescan")) {
+			switch_xml_t xml_root;
+			const char *err;
+			if (argc > 1 && !strcasecmp(argv[1], "reloadxml")) {
+				if ((xml_root = switch_xml_open_root(1, &err))) {
+					switch_xml_free(xml_root);
+				}
+				stream->write_function(stream, "Reload XML [%s]\n", err);
+			}
+			if (load_config() == SWITCH_STATUS_SUCCESS) {
+				stream->write_function(stream, "+OK scan complete\n");
+			} else {
+				stream->write_function(stream, "-ERR scan failed\n");
+				goto error;
+			}
+			goto done;
+		} else if (!strcasecmp(argv[0], "status")) {
+			stream->write_function(stream, "============================\n");
+			switch_mutex_lock(globals.mutex);
+			for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
+				vm_profile_t * profile = NULL;
+				switch_hash_this(hi, NULL, NULL, &val);
+				profile = (vm_profile_t *) val;
+				stream->write_function(stream, "Profile: %s\n", profile->name);
+			}
+			switch_mutex_unlock(globals.mutex);
+			stream->write_function(stream, "============================\n");
+			goto done;
 		}
 	}
 
@@ -3617,6 +3747,7 @@
 		goto error;
 	}
 
+	switch_mutex_lock(globals.mutex);
 	profile = switch_core_hash_find(globals.profile_hash, domain);
 
 	if (!profile) {
@@ -3638,9 +3769,13 @@
 
 	if (!profile) {
 		stream->write_function(stream, "Can't find profile.\n");
+		switch_mutex_unlock(globals.mutex);
 		goto error;
 	}
 
+	switch_thread_rwlock_rdlock(profile->rwlock);
+	switch_mutex_unlock(globals.mutex);
+
 	if (path_info) {
 		if (!strncasecmp(path_info, "get/", 4)) {
 			do_play(profile, user, domain, path_info + 4, stream);
@@ -3654,6 +3789,7 @@
 	if (rss || (path_info && !strncasecmp(path_info, "rss", 3))) {
 		do_rss(profile, user, domain, host, port, uri, stream);
 	}
+	switch_thread_rwlock_unlock(profile->rwlock);
 
 	goto done;
 
@@ -3683,6 +3819,12 @@
 		return SWITCH_STATUS_TERM;
 	}
 
+	memset(&globals, 0, sizeof(globals));
+	switch_core_new_memory_pool(&globals.pool);
+	switch_core_hash_init(&globals.profile_hash, globals.pool);
+	switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, globals.pool);
+
+
 	if ((status = load_config()) != SWITCH_STATUS_SUCCESS) {
 		return status;
 	}



More information about the Freeswitch-svn mailing list