<p>Sean Bright has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/14686">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_musiconhold.c: Prevent crash with realtime MoH<br><br>The MoH class internal file vector is potentially being manipulated by<br>multiple threads at the same time without sufficient locking. Switch to<br>a reference counted list and operate on copies where necessary.<br><br>Two other minor changes:<br><br>* Use struct dirent::d_type if it is available to avoid a stat() call<br><br>* Make the music class directory absolute at initialization time instead<br>  of each time we look for files<br><br>ASTERISK-28927 #close<br><br>Change-Id: I479c5dcf88db670956e8cac177b5826c986b0217<br>---<br>M res/res_musiconhold.c<br>1 file changed, 188 insertions(+), 87 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/86/14686/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_musiconhold.c b/res/res_musiconhold.c</span><br><span>index 01a14b9..faeeaa3 100644</span><br><span>--- a/res/res_musiconhold.c</span><br><span>+++ b/res/res_musiconhold.c</span><br><span>@@ -166,13 +166,13 @@</span><br><span> </span><br><span> struct mohclass {</span><br><span>      char name[MAX_MUSICCLASS];</span><br><span style="color: hsl(0, 100%, 40%);">-      char dir[256];</span><br><span style="color: hsl(120, 100%, 40%);">+        char dir[PATH_MAX];</span><br><span>  char args[256];</span><br><span>      char announcement[256];</span><br><span>      char mode[80];</span><br><span>       char digit;</span><br><span style="color: hsl(0, 100%, 40%);">-     /*! A vector of filenames in "files" mode */</span><br><span style="color: hsl(0, 100%, 40%);">-  struct ast_vector_string files;</span><br><span style="color: hsl(120, 100%, 40%);">+       /*! An immutable vector of filenames in "files" or "playlist" mode */</span><br><span style="color: hsl(120, 100%, 40%);">+     struct ast_vector_string *files;</span><br><span>     unsigned int flags;</span><br><span>  /*! The format from the MOH source, not applicable to "files" mode */</span><br><span>      struct ast_format *format;</span><br><span>@@ -313,6 +313,7 @@</span><br><span> static int ast_moh_files_next(struct ast_channel *chan)</span><br><span> {</span><br><span>     struct moh_files_state *state = ast_channel_music_state(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+        struct ast_vector_string *files;</span><br><span>     int tries;</span><br><span>   size_t file_count;</span><br><span> </span><br><span>@@ -332,16 +333,21 @@</span><br><span>               state->announcement = 0;</span><br><span>  }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   file_count = AST_VECTOR_SIZE(&state->class->files);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(state->class);</span><br><span style="color: hsl(120, 100%, 40%);">+    files = ao2_bump(state->class->files);</span><br><span style="color: hsl(120, 100%, 40%);">+  ao2_unlock(state->class);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        file_count = AST_VECTOR_SIZE(files);</span><br><span>         if (!file_count) {</span><br><span>           ast_log(LOG_WARNING, "No files available for class '%s'\n", state->class->name);</span><br><span style="color: hsl(120, 100%, 40%);">+              ao2_ref(files, -1);</span><br><span>          return -1;</span><br><span>   }</span><br><span> </span><br><span>        if (state->pos == 0 && ast_strlen_zero(state->save_pos_filename)) {</span><br><span>            /* First time so lets play the file. */</span><br><span>              state->save_pos = -1;</span><br><span style="color: hsl(0, 100%, 40%);">-        } else if (state->save_pos >= 0 && state->save_pos < file_count && !strcmp(AST_VECTOR_GET(&state->class->files, state->save_pos), state->save_pos_filename)) {</span><br><span style="color: hsl(120, 100%, 40%);">+    } else if (state->save_pos >= 0 && state->save_pos < file_count && !strcmp(AST_VECTOR_GET(files, state->save_pos), state->save_pos_filename)) {</span><br><span>            /* If a specific file has been saved confirm it still exists and that it is still valid */</span><br><span>           state->pos = state->save_pos;</span><br><span>          state->save_pos = -1;</span><br><span>@@ -349,7 +355,7 @@</span><br><span>               /* Get a random file and ensure we can open it */</span><br><span>            for (tries = 0; tries < 20; tries++) {</span><br><span>                    state->pos = ast_random() % file_count;</span><br><span style="color: hsl(0, 100%, 40%);">-                      if (ast_fileexists(AST_VECTOR_GET(&state->class->files, state->pos), NULL, NULL) > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       if (ast_fileexists(AST_VECTOR_GET(files, state->pos), NULL, NULL) > 0) {</span><br><span>                               break;</span><br><span>                       }</span><br><span>            }</span><br><span>@@ -364,21 +370,22 @@</span><br><span>    }</span><br><span> </span><br><span>        for (tries = 0; tries < file_count; ++tries) {</span><br><span style="color: hsl(0, 100%, 40%);">-               if (ast_openstream_full(chan, AST_VECTOR_GET(&state->class->files, state->pos), ast_channel_language(chan), 1)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                if (ast_openstream_full(chan, AST_VECTOR_GET(files, state->pos), ast_channel_language(chan), 1)) {</span><br><span>                        break;</span><br><span>               }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-           ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", AST_VECTOR_GET(&state->class->files, state->pos), strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+             ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", AST_VECTOR_GET(files, state->pos), strerror(errno));</span><br><span>             state->pos++;</span><br><span>             state->pos %= file_count;</span><br><span>         }</span><br><span> </span><br><span>        if (tries == file_count) {</span><br><span style="color: hsl(120, 100%, 40%);">+            ao2_ref(files, -1);</span><br><span>          return -1;</span><br><span>   }</span><br><span> </span><br><span>        /* Record the pointer to the filename for position resuming later */</span><br><span style="color: hsl(0, 100%, 40%);">-    ast_copy_string(state->save_pos_filename, AST_VECTOR_GET(&state->class->files, state->pos), sizeof(state->save_pos_filename));</span><br><span style="color: hsl(120, 100%, 40%);">+     ast_copy_string(state->save_pos_filename, AST_VECTOR_GET(files, state->pos), sizeof(state->save_pos_filename));</span><br><span> </span><br><span>         ast_debug(1, "%s Opened file %d '%s'\n", ast_channel_name(chan), state->pos, state->save_pos_filename);</span><br><span> </span><br><span>@@ -395,6 +402,7 @@</span><br><span>            }</span><br><span>    }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(files, -1);</span><br><span>  return 0;</span><br><span> }</span><br><span> </span><br><span>@@ -504,7 +512,9 @@</span><br><span>             }</span><br><span>    }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   file_count = AST_VECTOR_SIZE(&class->files);</span><br><span style="color: hsl(120, 100%, 40%);">+   ao2_lock(class);</span><br><span style="color: hsl(120, 100%, 40%);">+      file_count = AST_VECTOR_SIZE(class->files);</span><br><span style="color: hsl(120, 100%, 40%);">+        ao2_unlock(class);</span><br><span> </span><br><span>       /* Resume MOH from where we left off last time or start from scratch? */</span><br><span>     if (state->save_total != file_count || strcmp(state->name, class->name) != 0) {</span><br><span>@@ -1074,8 +1084,38 @@</span><br><span>    .digit    = moh_handle_digit,</span><br><span> };</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static void make_directory_path_absolute(struct mohclass *clz, const char *value)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (*value == '/') {</span><br><span style="color: hsl(120, 100%, 40%);">+          ast_copy_string(clz->dir, value, sizeof(clz->dir));</span><br><span style="color: hsl(120, 100%, 40%);">+     } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              snprintf(clz->dir, sizeof(clz->dir), "%s/%s", ast_config_AST_DATA_DIR, value);</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void moh_file_vector_destructor(void *obj)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  struct ast_vector_string *files = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+        AST_VECTOR_RESET(files, ast_free);</span><br><span style="color: hsl(120, 100%, 40%);">+    AST_VECTOR_FREE(files);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct ast_vector_string *moh_file_vector_alloc(int initial_capacity)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_vector_string *files = ao2_alloc_options(</span><br><span style="color: hsl(120, 100%, 40%);">+          sizeof(struct ast_vector_string),</span><br><span style="color: hsl(120, 100%, 40%);">+             moh_file_vector_destructor,</span><br><span style="color: hsl(120, 100%, 40%);">+           AO2_ALLOC_OPT_LOCK_NOLOCK);</span><br><span style="color: hsl(120, 100%, 40%);">+   if (files) {</span><br><span style="color: hsl(120, 100%, 40%);">+          AST_VECTOR_INIT(files, initial_capacity);</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     return files;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static void moh_parse_options(struct ast_variable *var, struct mohclass *mohclass)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_vector_string *playlist_entries = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>         for (; var; var = var->next) {</span><br><span>            if (!strcasecmp(var->name, "name")) {</span><br><span>                   ast_copy_string(mohclass->name, var->value, sizeof(mohclass->name));</span><br><span>@@ -1083,7 +1123,16 @@</span><br><span>                       ast_copy_string(mohclass->mode, var->value, sizeof(mohclass->mode));</span><br><span>                } else if (!strcasecmp(var->name, "entry")) {</span><br><span>                   if (ast_begins_with(var->value, "/") || ast_begins_with(var->value, "http://") || ast_begins_with(var->value, "https://")) {</span><br><span style="color: hsl(0, 100%, 40%);">-                             char *dup = ast_strdup(var->value);</span><br><span style="color: hsl(120, 100%, 40%);">+                                char *dup;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                          if (!playlist_entries) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                      playlist_entries = moh_file_vector_alloc(16);</span><br><span style="color: hsl(120, 100%, 40%);">+                                 if (!playlist_entries) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                              continue;</span><br><span style="color: hsl(120, 100%, 40%);">+                                     }</span><br><span style="color: hsl(120, 100%, 40%);">+                             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                           dup = ast_strdup(var->value);</span><br><span>                             if (!dup) {</span><br><span>                                  continue;</span><br><span>                            }</span><br><span>@@ -1096,12 +1145,13 @@</span><br><span>                                                  dup);</span><br><span>                                        }</span><br><span>                            }</span><br><span style="color: hsl(0, 100%, 40%);">-                               AST_VECTOR_APPEND(&mohclass->files, dup);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                            AST_VECTOR_APPEND(playlist_entries, dup);</span><br><span>                    } else {</span><br><span>                             ast_log(LOG_ERROR, "Playlist entries must be a URL or absolute path, '%s' provided.\n", var->value);</span><br><span>                    }</span><br><span>            } else if (!strcasecmp(var->name, "directory")) {</span><br><span style="color: hsl(0, 100%, 40%);">-                  ast_copy_string(mohclass->dir, var->value, sizeof(mohclass->dir));</span><br><span style="color: hsl(120, 100%, 40%);">+                   make_directory_path_absolute(mohclass, var->value);</span><br><span>               } else if (!strcasecmp(var->name, "application")) {</span><br><span>                     ast_copy_string(mohclass->args, var->value, sizeof(mohclass->args));</span><br><span>                } else if (!strcasecmp(var->name, "announcement")) {</span><br><span>@@ -1150,86 +1200,110 @@</span><br><span>                 }</span><br><span>    }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   AST_VECTOR_COMPACT(&mohclass->files);</span><br><span style="color: hsl(120, 100%, 40%);">+  if (playlist_entries) {</span><br><span style="color: hsl(120, 100%, 40%);">+               /* If we aren't in playlist mode, drop any list we may have already built */</span><br><span style="color: hsl(120, 100%, 40%);">+              if (strcasecmp(mohclass->mode, "playlist")) {</span><br><span style="color: hsl(120, 100%, 40%);">+                    ao2_ref(playlist_entries, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+                        return;</span><br><span style="color: hsl(120, 100%, 40%);">+               }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           AST_VECTOR_COMPACT(playlist_entries);</span><br><span style="color: hsl(120, 100%, 40%);">+         ao2_replace(mohclass->files, playlist_entries);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static int moh_scan_files(struct mohclass *class) {</span><br><span style="color: hsl(120, 100%, 40%);">+static int moh_scan_files(struct mohclass *class)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   DIR *dir;</span><br><span style="color: hsl(120, 100%, 40%);">+     struct dirent *entry;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_vector_string *files;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-    DIR *files_DIR;</span><br><span style="color: hsl(0, 100%, 40%);">- struct dirent *files_dirent;</span><br><span style="color: hsl(0, 100%, 40%);">-    char dir_path[PATH_MAX - sizeof(class->dir)];</span><br><span style="color: hsl(0, 100%, 40%);">-        char filepath[PATH_MAX];</span><br><span style="color: hsl(0, 100%, 40%);">-        char *ext;</span><br><span style="color: hsl(0, 100%, 40%);">-      struct stat statbuf;</span><br><span style="color: hsl(0, 100%, 40%);">-    int res;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-        if (class->dir[0] != '/') {</span><br><span style="color: hsl(0, 100%, 40%);">-          snprintf(dir_path, sizeof(dir_path), "%s/%s", ast_config_AST_DATA_DIR, class->dir);</span><br><span style="color: hsl(0, 100%, 40%);">-        } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                ast_copy_string(dir_path, class->dir, sizeof(dir_path));</span><br><span style="color: hsl(0, 100%, 40%);">-     }</span><br><span style="color: hsl(0, 100%, 40%);">-       ast_debug(4, "Scanning '%s' for files for class '%s'\n", dir_path, class->name);</span><br><span style="color: hsl(0, 100%, 40%);">-   files_DIR = opendir(dir_path);</span><br><span style="color: hsl(0, 100%, 40%);">-  if (!files_DIR) {</span><br><span style="color: hsl(0, 100%, 40%);">-               ast_log(LOG_WARNING, "Cannot open dir %s or dir does not exist\n", dir_path);</span><br><span style="color: hsl(120, 100%, 40%);">+       ast_debug(4, "Scanning '%s' for files for class '%s'\n", class->dir, class->name);</span><br><span style="color: hsl(120, 100%, 40%);">+    dir = opendir(class->dir);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!dir) {</span><br><span style="color: hsl(120, 100%, 40%);">+           ast_log(LOG_WARNING, "Cannot open '%s': %s\n", class->dir, strerror(errno));</span><br><span>            return -1;</span><br><span>   }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   AST_VECTOR_RESET(&class->files, ast_free);</span><br><span style="color: hsl(120, 100%, 40%);">+     files = moh_file_vector_alloc(16); /* 16 seems like a reasonable default */</span><br><span style="color: hsl(120, 100%, 40%);">+   if (!files) {</span><br><span style="color: hsl(120, 100%, 40%);">+         closedir(dir);</span><br><span style="color: hsl(120, 100%, 40%);">+                return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   while ((files_dirent = readdir(files_DIR))) {</span><br><span style="color: hsl(0, 100%, 40%);">-           char *filepath_copy;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-            /* The file name must be at least long enough to have the file type extension */</span><br><span style="color: hsl(0, 100%, 40%);">-                if ((strlen(files_dirent->d_name) < 4))</span><br><span style="color: hsl(0, 100%, 40%);">-                   continue;</span><br><span style="color: hsl(120, 100%, 40%);">+     while ((entry = readdir(dir))) {</span><br><span style="color: hsl(120, 100%, 40%);">+              char *path, *extension;</span><br><span> </span><br><span>          /* Skip files that starts with a dot */</span><br><span style="color: hsl(0, 100%, 40%);">-         if (files_dirent->d_name[0] == '.')</span><br><span style="color: hsl(0, 100%, 40%);">-                  continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-               /* Skip files without extensions... they are not audio */</span><br><span style="color: hsl(0, 100%, 40%);">-               if (!strchr(files_dirent->d_name, '.'))</span><br><span style="color: hsl(0, 100%, 40%);">-                      continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-               snprintf(filepath, sizeof(filepath), "%s/%s", dir_path, files_dirent->d_name);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-             if (stat(filepath, &statbuf))</span><br><span style="color: hsl(0, 100%, 40%);">-                       continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-               if (!S_ISREG(statbuf.st_mode))</span><br><span style="color: hsl(0, 100%, 40%);">-                  continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-               if ((ext = strrchr(filepath, '.')))</span><br><span style="color: hsl(0, 100%, 40%);">-                     *ext = '\0';</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-            /* if the file is present in multiple formats, ensure we only put it into the list once */</span><br><span style="color: hsl(0, 100%, 40%);">-              if (AST_VECTOR_GET_CMP(&class->files, &filepath[0], !strcmp)) {</span><br><span style="color: hsl(120, 100%, 40%);">+            if (entry->d_name[0] == '.') {</span><br><span>                    continue;</span><br><span>            }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-           filepath_copy = ast_strdup(filepath);</span><br><span style="color: hsl(0, 100%, 40%);">-           if (!filepath_copy) {</span><br><span style="color: hsl(120, 100%, 40%);">+         /* The filename must be at least long enough to have the file type extension */</span><br><span style="color: hsl(120, 100%, 40%);">+               if (strlen(entry->d_name) < 4) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        continue;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           /* Skip files without extensions */</span><br><span style="color: hsl(120, 100%, 40%);">+           if (!strrchr(entry->d_name, '.')) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        continue;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           /* Whoever frees the vector is responsible for freeing this string */</span><br><span style="color: hsl(120, 100%, 40%);">+         if (ast_asprintf(&path, "%s/%s", class->dir, entry->d_name) < 0) {</span><br><span>                    break;</span><br><span>               }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+         /* Only regular files are candidates */</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef _DIRENT_HAVE_D_TYPE</span><br><span style="color: hsl(120, 100%, 40%);">+           if (entry->d_type != DT_UNKNOWN) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 if (entry->d_type != DT_REG) {</span><br><span style="color: hsl(120, 100%, 40%);">+                             ast_free(path);</span><br><span style="color: hsl(120, 100%, 40%);">+                               continue;</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</span><br><span style="color: hsl(120, 100%, 40%);">+             } else</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+                {</span><br><span style="color: hsl(120, 100%, 40%);">+                     struct stat statbuf;</span><br><span style="color: hsl(120, 100%, 40%);">+                  if (stat(path, &statbuf) || !S_ISREG(statbuf.st_mode)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                          ast_free(path);</span><br><span style="color: hsl(120, 100%, 40%);">+                               continue;</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           /* Truncate off the extension */</span><br><span style="color: hsl(120, 100%, 40%);">+              if ((extension = strrchr(path, '.'))) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       *extension = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+            }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           /* If the file is present in multiple formats, ensure we only put it into the list once */</span><br><span style="color: hsl(120, 100%, 40%);">+            if (AST_VECTOR_GET_CMP(files, path, !strcmp)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       ast_free(path);</span><br><span style="color: hsl(120, 100%, 40%);">+                       continue;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>          if (ast_test_flag(class, MOH_SORTALPHA)) {</span><br><span style="color: hsl(0, 100%, 40%);">-                      res = AST_VECTOR_ADD_SORTED(&class->files, filepath_copy, strcasecmp);</span><br><span style="color: hsl(120, 100%, 40%);">+                 if (AST_VECTOR_ADD_SORTED(files, path, strcasecmp)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                         ast_free(path);</span><br><span style="color: hsl(120, 100%, 40%);">+                               break;</span><br><span style="color: hsl(120, 100%, 40%);">+                        }</span><br><span>            } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                        res = AST_VECTOR_APPEND(&class->files, filepath_copy);</span><br><span style="color: hsl(0, 100%, 40%);">-           }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-               if (res) {</span><br><span style="color: hsl(0, 100%, 40%);">-                      ast_free(filepath_copy);</span><br><span style="color: hsl(0, 100%, 40%);">-                        break;</span><br><span style="color: hsl(120, 100%, 40%);">+                        if (AST_VECTOR_APPEND(files, path)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                         ast_free(path);</span><br><span style="color: hsl(120, 100%, 40%);">+                               break;</span><br><span style="color: hsl(120, 100%, 40%);">+                        }</span><br><span>            }</span><br><span>    }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   closedir(files_DIR);</span><br><span style="color: hsl(120, 100%, 40%);">+  closedir(dir);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-      AST_VECTOR_COMPACT(&class->files);</span><br><span style="color: hsl(120, 100%, 40%);">+     AST_VECTOR_COMPACT(files);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-  return AST_VECTOR_SIZE(&class->files);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(class);</span><br><span style="color: hsl(120, 100%, 40%);">+      ao2_replace(class->files, files);</span><br><span style="color: hsl(120, 100%, 40%);">+  ao2_unlock(class);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  return AST_VECTOR_SIZE(files);</span><br><span> }</span><br><span> </span><br><span> static int init_files_class(struct mohclass *class)</span><br><span>@@ -1355,7 +1429,13 @@</span><br><span>                      return -1;</span><br><span>           }</span><br><span>    } else if (!strcasecmp(moh->mode, "playlist")) {</span><br><span style="color: hsl(0, 100%, 40%);">-           if (!AST_VECTOR_SIZE(&moh->files)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           size_t file_count;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          ao2_lock(moh);</span><br><span style="color: hsl(120, 100%, 40%);">+                file_count = AST_VECTOR_SIZE(moh->files);</span><br><span style="color: hsl(120, 100%, 40%);">+          ao2_unlock(moh);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+            if (!file_count) {</span><br><span>                   if (unref) {</span><br><span>                                 moh = mohclass_unref(moh, "unreffing potential new moh class (no playlist entries)");</span><br><span>                      }</span><br><span>@@ -1504,7 +1584,13 @@</span><br><span>           class->format = ao2_bump(ast_format_slin);</span><br><span>                class->srcfd = -1;</span><br><span>                class->kill_delay = 100000;</span><br><span style="color: hsl(0, 100%, 40%);">-          AST_VECTOR_INIT(&class->files, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           /* We create an empty one by default */</span><br><span style="color: hsl(120, 100%, 40%);">+               class->files = moh_file_vector_alloc(0);</span><br><span style="color: hsl(120, 100%, 40%);">+           if (!class->files) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       ao2_ref(class, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+                   return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span>    }</span><br><span> </span><br><span>        return class;</span><br><span>@@ -1647,7 +1733,7 @@</span><br><span>                                mohclass->start -= respawn_time;</span><br><span> </span><br><span>                              if (!strcasecmp(mohclass->mode, "files")) {</span><br><span style="color: hsl(0, 100%, 40%);">-                                        if (!moh_scan_files(mohclass)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                      if (moh_scan_files(mohclass) <= 0) {</span><br><span>                                              mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (moh_scan_files failed)");</span><br><span>                                               return -1;</span><br><span>                                   }</span><br><span>@@ -1660,7 +1746,13 @@</span><br><span>                                           ast_set_flag(mohclass, MOH_RANDOMIZE);</span><br><span>                                       }</span><br><span>                            } else if (!strcasecmp(mohclass->mode, "playlist")) {</span><br><span style="color: hsl(0, 100%, 40%);">-                                      if (!AST_VECTOR_SIZE(&mohclass->files)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                      size_t file_count;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                                  ao2_lock(mohclass);</span><br><span style="color: hsl(120, 100%, 40%);">+                                   file_count = AST_VECTOR_SIZE(mohclass->files);</span><br><span style="color: hsl(120, 100%, 40%);">+                                     ao2_unlock(mohclass);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                                       if (!file_count) {</span><br><span>                                           mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (no playlist entries)");</span><br><span>                                                 return -1;</span><br><span>                                   }</span><br><span>@@ -1723,14 +1815,20 @@</span><br><span> </span><br><span>      /* If we are using a cached realtime class with files, re-scan the files */</span><br><span>  if (!var && ast_test_flag(global_flags, MOH_CACHERTCLASSES) && mohclass->realtime && !strcasecmp(mohclass->mode, "files")) {</span><br><span style="color: hsl(0, 100%, 40%);">-            if (!moh_scan_files(mohclass)) {</span><br><span style="color: hsl(120, 100%, 40%);">+              if (moh_scan_files(mohclass) <= 0) {</span><br><span>                      mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (moh_scan_files failed)");</span><br><span>                       return -1;</span><br><span>           }</span><br><span>    }</span><br><span> </span><br><span>        if (!state || !state->class || strcmp(mohclass->name, state->class->name)) {</span><br><span style="color: hsl(0, 100%, 40%);">-                if (AST_VECTOR_SIZE(&mohclass->files)) {</span><br><span style="color: hsl(120, 100%, 40%);">+               size_t file_count;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          ao2_lock(mohclass);</span><br><span style="color: hsl(120, 100%, 40%);">+           file_count = AST_VECTOR_SIZE(mohclass->files);</span><br><span style="color: hsl(120, 100%, 40%);">+             ao2_unlock(mohclass);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+               if (file_count) {</span><br><span>                    res = ast_activate_generator(chan, &moh_file_stream, mohclass);</span><br><span>          } else {</span><br><span>                     res = ast_activate_generator(chan, &mohgen, mohclass);</span><br><span>@@ -1775,6 +1873,7 @@</span><br><span>   while ((member = AST_LIST_REMOVE_HEAD(&class->members, list))) {</span><br><span>              ast_free(member);</span><br><span>    }</span><br><span style="color: hsl(120, 100%, 40%);">+     ao2_cleanup(class->files);</span><br><span>        ao2_unlock(class);</span><br><span> </span><br><span>       /* Kill the thread first, so it cannot restart the child process while the</span><br><span>@@ -1809,9 +1908,6 @@</span><br><span>           class->srcfd = -1;</span><br><span>        }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   AST_VECTOR_RESET(&class->files, ast_free);</span><br><span style="color: hsl(0, 100%, 40%);">-       AST_VECTOR_FREE(&class->files);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span>       if (class->timer) {</span><br><span>               ast_timer_close(class->timer);</span><br><span>            class->timer = NULL;</span><br><span>@@ -1993,16 +2089,21 @@</span><br><span> </span><br><span>        i = ao2_iterator_init(mohclasses, 0);</span><br><span>        for (; (class = ao2_t_iterator_next(&i, "Show files iterator")); mohclass_unref(class, "Unref iterator in moh show files")) {</span><br><span style="color: hsl(0, 100%, 40%);">-           int x;</span><br><span style="color: hsl(120, 100%, 40%);">+                struct ast_vector_string *files;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-            if (!AST_VECTOR_SIZE(&class->files)) {</span><br><span style="color: hsl(0, 100%, 40%);">-                   continue;</span><br><span style="color: hsl(120, 100%, 40%);">+             ao2_lock(class);</span><br><span style="color: hsl(120, 100%, 40%);">+              files = ao2_bump(class->files);</span><br><span style="color: hsl(120, 100%, 40%);">+            ao2_unlock(class);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          if (AST_VECTOR_SIZE(files)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 int x;</span><br><span style="color: hsl(120, 100%, 40%);">+                        ast_cli(a->fd, "Class: %s\n", class->name);</span><br><span style="color: hsl(120, 100%, 40%);">+                   for (x = 0; x < AST_VECTOR_SIZE(files); x++) {</span><br><span style="color: hsl(120, 100%, 40%);">+                             ast_cli(a->fd, "\tFile: %s\n", AST_VECTOR_GET(files, x));</span><br><span style="color: hsl(120, 100%, 40%);">+                        }</span><br><span>            }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-           ast_cli(a->fd, "Class: %s\n", class->name);</span><br><span style="color: hsl(0, 100%, 40%);">-             for (x = 0; x < AST_VECTOR_SIZE(&class->files); x++) {</span><br><span style="color: hsl(0, 100%, 40%);">-                        ast_cli(a->fd, "\tFile: %s\n", AST_VECTOR_GET(&class->files, x));</span><br><span style="color: hsl(0, 100%, 40%);">-           }</span><br><span style="color: hsl(120, 100%, 40%);">+             ao2_ref(files, -1);</span><br><span>  }</span><br><span>    ao2_iterator_destroy(&i);</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/14686">change 14686</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/14686"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: I479c5dcf88db670956e8cac177b5826c986b0217 </div>
<div style="display:none"> Gerrit-Change-Number: 14686 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Sean Bright <sean.bright@gmail.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>