The loop in the handler for the "core show locks" could potentially block for

some amount of time.  Be a little bit more careful and prepare all of the
output in an intermediary buffer while holding a global resource.  Then, after
releasing it, send the output to ast_cli().


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@85647 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Russell Bryant
2007-10-15 19:11:38 +00:00
parent 3ba62c30b9
commit 9c47228dff

View File

@@ -707,8 +707,12 @@ static const char *locktype2str(enum ast_lock_type type)
static int handle_show_locks(int fd, int argc, char *argv[]) static int handle_show_locks(int fd, int argc, char *argv[])
{ {
struct thr_lock_info *lock_info; struct thr_lock_info *lock_info;
struct ast_dynamic_str *str;
ast_cli(fd, "\n" if (!(str = ast_dynamic_str_create(4096)))
return RESULT_FAILURE;
ast_dynamic_str_append(&str, 0, "\n"
"=======================================================================\n" "=======================================================================\n"
"=== Currently Held Locks ==============================================\n" "=== Currently Held Locks ==============================================\n"
"=======================================================================\n" "=======================================================================\n"
@@ -719,12 +723,13 @@ static int handle_show_locks(int fd, int argc, char *argv[])
pthread_mutex_lock(&lock_infos_lock.mutex); pthread_mutex_lock(&lock_infos_lock.mutex);
AST_LIST_TRAVERSE(&lock_infos, lock_info, entry) { AST_LIST_TRAVERSE(&lock_infos, lock_info, entry) {
int i; int i;
ast_cli(fd, "=== Thread ID: %u (%s)\n", (int) lock_info->thread_id, ast_dynamic_str_append(&str, 0, "=== Thread ID: %u (%s)\n", (int) lock_info->thread_id,
lock_info->thread_name); lock_info->thread_name);
pthread_mutex_lock(&lock_info->lock); pthread_mutex_lock(&lock_info->lock);
for (i = 0; i < lock_info->num_locks; i++) { for (i = 0; i < lock_info->num_locks; i++) {
ast_cli(fd, "=== ---> %sLock #%d (%s): %s %d %s %s %p (%d)\n", ast_dynamic_str_append(&str, 0, "=== ---> %sLock #%d (%s): %s %d %s %s %p (%d)\n",
lock_info->locks[i].pending > 0 ? "Waiting for " : lock_info->locks[i].pending < 0 ? "Tried and failed to get " : "", i, lock_info->locks[i].pending > 0 ? "Waiting for " :
lock_info->locks[i].pending < 0 ? "Tried and failed to get " : "", i,
lock_info->locks[i].file, lock_info->locks[i].file,
locktype2str(lock_info->locks[i].type), locktype2str(lock_info->locks[i].type),
lock_info->locks[i].line_num, lock_info->locks[i].line_num,
@@ -733,15 +738,19 @@ static int handle_show_locks(int fd, int argc, char *argv[])
lock_info->locks[i].times_locked); lock_info->locks[i].times_locked);
} }
pthread_mutex_unlock(&lock_info->lock); pthread_mutex_unlock(&lock_info->lock);
ast_cli(fd, "=== -------------------------------------------------------------------\n" ast_dynamic_str_append(&str, 0, "=== -------------------------------------------------------------------\n"
"===\n"); "===\n");
} }
pthread_mutex_unlock(&lock_infos_lock.mutex); pthread_mutex_unlock(&lock_infos_lock.mutex);
ast_cli(fd, "=======================================================================\n" ast_dynamic_str_append(&str, 0, "=======================================================================\n"
"\n"); "\n");
return 0; ast_cli(fd, "%s", str->str);
free(str);
return RESULT_SUCCESS;
} }
static char show_locks_help[] = static char show_locks_help[] =