]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
The loop in the handler for the "core show locks" could potentially block for
authorRussell Bryant <russell@russellbryant.com>
Mon, 15 Oct 2007 19:11:38 +0000 (19:11 +0000)
committerRussell Bryant <russell@russellbryant.com>
Mon, 15 Oct 2007 19:11:38 +0000 (19:11 +0000)
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

main/utils.c

index 5d4bd1cdcce7fca1eff38863e43d2922a54919e4..8c2c568f1bc7157a68204336c6c5ecad2b1e592a 100644 (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[])
 {
        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"
                    "=== Currently Held Locks ==============================================\n"
                    "=======================================================================\n"
@@ -719,12 +723,13 @@ static int handle_show_locks(int fd, int argc, char *argv[])
        pthread_mutex_lock(&lock_infos_lock.mutex);
        AST_LIST_TRAVERSE(&lock_infos, lock_info, entry) {
                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);
                pthread_mutex_lock(&lock_info->lock);
                for (i = 0; i < lock_info->num_locks; i++) {
-                       ast_cli(fd, "=== ---> %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,
+                       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].file, 
                                locktype2str(lock_info->locks[i].type),
                                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);
                }
                pthread_mutex_unlock(&lock_info->lock);
-               ast_cli(fd, "=== -------------------------------------------------------------------\n"
+               ast_dynamic_str_append(&str, 0, "=== -------------------------------------------------------------------\n"
                            "===\n");
        }
        pthread_mutex_unlock(&lock_infos_lock.mutex);
 
-       ast_cli(fd, "=======================================================================\n"
+       ast_dynamic_str_append(&str, 0, "=======================================================================\n"
                    "\n");
 
-       return 0;
+       ast_cli(fd, "%s", str->str);
+
+       free(str);
+
+       return RESULT_SUCCESS;
 }
 
 static char show_locks_help[] =