]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-11225: [freeswitch-core] Crash in fs_cli #resolve
authorAnthony Minessale <anthm@freeswitch.org>
Mon, 9 Jul 2018 20:02:13 +0000 (20:02 +0000)
committerMuteesa Fred <muteesafred@hotmail.com>
Tue, 24 Jul 2018 07:21:56 +0000 (07:21 +0000)
libs/esl/fs_cli.c

index 39c445ba5c16e7e971923bf44c028f7999154ad0..b4a58381758b2269fc2bbba3b1745387a4015df9 100644 (file)
@@ -89,7 +89,7 @@ static int pcount = 0;
 static esl_handle_t *global_handle;
 static cli_profile_t *global_profile;
 static int running = 1;
-static int thread_running = 0;
+static int thread_running = 0, thread_up = 0, check_up = 0;
 static char *filter_uuid;
 static char *logfilter;
 static int timeout = 0;
@@ -671,40 +671,45 @@ static void clear_line(void)
 
 static void redisplay(void)
 {
+       esl_mutex_lock(MUTEX);
+       {
 #ifdef HAVE_LIBEDIT
-#ifdef HAVE_DECL_EL_REFRESH
+#ifdef XHAVE_DECL_EL_REFRESH
 #ifdef HAVE_EL_WSET
-       /* Current libedit versions don't implement EL_REFRESH in eln.c so
-        * use the wide version instead. */
-       el_wset(el, EL_REFRESH);
+               /* Current libedit versions don't implement EL_REFRESH in eln.c so
+                * use the wide version instead. */
+               el_wset(el, EL_REFRESH);
 #else
-       /* This will work on future libedit versions and versions built
-        * without wide character support. */
-       el_set(el, EL_REFRESH);
+               /* This will work on future libedit versions and versions built
+                * without wide character support. */
+               el_set(el, EL_REFRESH);
 #endif
 #else
-       /* Old libedit versions don't implement EL_REFRESH at all so use
-        * our own implementation instead. */
-       const LineInfo *lf = el_line(el);
-       const char *c = lf->buffer;
-       if (global_profile->batch_mode) return;
-       printf("%s",prompt_str);
-       while (c < lf->lastchar && *c) {
-               putchar(*c);
-               c++;
-       }
-       {
-               int pos = (int)(lf->cursor - lf->buffer);
-               char s1[12], s2[12];
-               putchar('\r');
-               snprintf(s1, sizeof(s1), "\033[%dC", bare_prompt_str_len);
-               snprintf(s2, sizeof(s2), "\033[%dC", pos);
-               printf("%s%s",s1,s2);
-       }
-       fflush(stdout);
-       return;
+               /* Old libedit versions don't implement EL_REFRESH at all so use
+                * our own implementation instead. */
+               const LineInfo *lf = el_line(el);
+               const char *c = lf->buffer;
+               if (global_profile->batch_mode) return;
+               printf("%s",prompt_str);
+               while (c < lf->lastchar && *c) {
+                       putchar(*c);
+                       c++;
+               }
+               {
+                       int pos = (int)(lf->cursor - lf->buffer);
+                       char s1[12], s2[12] = "";
+                       
+                       putchar('\r');
+                       snprintf(s1, sizeof(s1), "\033[%dC", bare_prompt_str_len);                      
+                       if (pos) snprintf(s2, sizeof(s2), "\033[%dC", pos);
+                       printf("%s%s",s1,s2);
+               }
+               fflush(stdout);
 #endif
 #endif
+       }
+       esl_mutex_unlock(MUTEX);
+       return;
 }
 
 static int output_printf(const char *fmt, ...)
@@ -726,6 +731,9 @@ static void *msg_thread_run(esl_thread_t *me, void *obj)
 {
        esl_handle_t *handle = (esl_handle_t *) obj;
        thread_running = 1;
+       esl_mutex_lock(MUTEX);
+       thread_up = 1;
+       esl_mutex_unlock(MUTEX);
        while(thread_running && handle->connected) {
                int aok = 1;
                esl_status_t status;
@@ -846,6 +854,10 @@ static void *msg_thread_run(esl_thread_t *me, void *obj)
                }
                //sleep_ms(1);
        }
+
+       esl_mutex_lock(MUTEX);  
+       thread_up = 0;
+       esl_mutex_unlock(MUTEX);  
        thread_running = 0;
        esl_log(ESL_LOG_DEBUG, "Thread Done\n");
        return NULL;
@@ -1165,8 +1177,10 @@ static unsigned char esl_console_complete(const char *buffer, const char *cursor
                snprintf(cmd_str, sizeof(cmd_str), "api console_complete %s\n\n", buf);
        }
 
+       esl_mutex_lock(MUTEX);
        esl_send_recv(global_handle, cmd_str);
-
+       esl_mutex_unlock(MUTEX);
+       
        if (global_handle->last_sr_event && global_handle->last_sr_event->body) {
                char *r = global_handle->last_sr_event->body;
                char *w, *p1;
@@ -1828,6 +1842,7 @@ int main(int argc, char *argv[])
        output_printf("%s\n", handle.last_sr_reply);
        while (running > 0) {
                int r;
+
 #ifdef HAVE_LIBEDIT
                if (!(global_profile->batch_mode)) {
                        line = el_gets(el, &count);
@@ -1869,6 +1884,13 @@ int main(int argc, char *argv[])
        global_handle = NULL;
        thread_running = 0;
 
+       do {
+               esl_mutex_lock(MUTEX);
+               check_up = thread_up;
+               esl_mutex_unlock(MUTEX);
+               sleep_ms(10);
+       } while (check_up > 0);
+       
        esl_mutex_destroy(&MUTEX);
 
        return 0;