wmem: allow wmem_destroy_list to ignore a NULL list.
[metze/wireshark/wip.git] / extcap.c
index a6a34544f249abf07aa17dcbbbcb7ca6abeefc03..cdd8e0f70e02cb6792f90e8baa5f2d6077fe504f 100644 (file)
--- a/extcap.c
+++ b/extcap.c
@@ -25,9 +25,7 @@
 #include <unistd.h>
 #endif
 
-#ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
-#endif
 #ifdef HAVE_SYS_WAIT_H
 #include <sys/wait.h>
 #endif
@@ -39,7 +37,6 @@
 
 #include "ui/iface_toolbar.h"
 
-#include <wsutil/glib-compat.h>
 #include <wsutil/file_util.h>
 #include <wsutil/filesystem.h>
 #include <wsutil/ws_pipe.h>
@@ -93,8 +90,67 @@ typedef struct _extcap_callback_info_t
 /* Callback definition for extcap_foreach */
 typedef gboolean(*extcap_cb_t)(extcap_callback_info_t info_structure);
 
+/** GThreadPool does not support pushing new work from a thread while waiting
+ * for the thread pool to finish. This data structure tracks ongoing work.
+ * See https://gitlab.gnome.org/GNOME/glib/issues/1598 */
+typedef struct thread_pool {
+    GThreadPool    *pool;
+    gint            count;  /**< Number of tasks that have not finished. */
+    GCond           cond;
+    GMutex          data_mutex;
+} thread_pool_t;
+
+/**
+ * Callback definition for extcap_run_all, invoked with a thread pool (to
+ * schedule new tasks), an opaque data parameter, and the output from last task
+ * (or NULL if it failed). The output must be freed by the callback function.
+ * The implementation MUST be thread-safe.
+ */
+typedef void (*extcap_run_cb_t)(thread_pool_t *pool, void *data, char *output);
+
+typedef struct extcap_run_task {
+    const char     *extcap_path;
+    char          **argv;       /**< NULL-terminated arguments list, freed when the task is completed. */
+    extcap_run_cb_t output_cb;
+    void           *data;       /** Parameter to be passed to output_cb. */
+} extcap_run_task_t;
+
+typedef struct extcap_iface_info {
+    char *ifname;                       /**< Interface name. */
+    char *output;                       /**< Output of --extcap-config. */
+} extcap_iface_info_t;
+
+typedef struct extcap_run_extcaps_info {
+    char    *extcap_path;               /**< Extcap program path, MUST be the first member.  */
+    char    *output;                    /**< Output of --extcap-interfaces. */
+    guint   num_interfaces;             /**< Number of discovered interfaces. */
+    extcap_iface_info_t *iface_infos;   /**< Per-interface information. */
+} extcap_run_extcaps_info_t;
+
+
 static void extcap_load_interface_list(void);
 
+static gboolean
+thread_pool_push(thread_pool_t *pool, gpointer data, GError **error)
+{
+    g_mutex_lock(&pool->data_mutex);
+    ++pool->count;
+    g_mutex_unlock(&pool->data_mutex);
+    return g_thread_pool_push(pool->pool, data, error);
+}
+
+static void
+thread_pool_wait(thread_pool_t *pool)
+{
+    g_mutex_lock(&pool->data_mutex);
+    while (pool->count != 0) {
+        g_cond_wait(&pool->cond, &pool->data_mutex);
+    }
+    g_cond_clear(&pool->cond);
+    g_mutex_unlock(&pool->data_mutex);
+    g_mutex_clear(&pool->data_mutex);
+}
+
 GHashTable *
 extcap_loaded_interfaces(void)
 {
@@ -119,52 +175,35 @@ extcap_clear_interfaces(void)
     _tool_for_ifname = NULL;
 }
 
-guint extcap_count(void)
+/**
+ * Obtains a list of extcap program paths. Use g_slist_free_full(paths, g_free)
+ * to destroy the list.
+ */
+static GSList *
+extcap_get_extcap_paths(void)
 {
-    const char *dirname = get_extcap_dir();
     GDir *dir;
-    guint count;
-
-    count = 0;
-
-    if ((dir = g_dir_open(dirname, 0, NULL)) != NULL)
-    {
-        GString *extcap_path = NULL;
-        const gchar *file;
+    const char *dirname = get_extcap_dir();
+    const gchar *file;
+    GSList *paths = NULL;
 
-        extcap_path = g_string_new("");
-        while ((file = g_dir_read_name(dir)) != NULL)
-        {
+    if ((dir = g_dir_open(dirname, 0, NULL)) != NULL) {
+        while ((file = g_dir_read_name(dir)) != NULL) {
             /* full path to extcap binary */
-            g_string_printf(extcap_path, "%s" G_DIR_SEPARATOR_S "%s", dirname, file);
+            gchar *extcap_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", dirname, file);
             /* treat anything executable as an extcap binary */
-            if (g_file_test(extcap_path->str, G_FILE_TEST_IS_REGULAR) &&
-                g_file_test(extcap_path->str, G_FILE_TEST_IS_EXECUTABLE))
-            {
-                count++;
+            if (g_file_test(extcap_path, G_FILE_TEST_IS_REGULAR) &&
+                g_file_test(extcap_path, G_FILE_TEST_IS_EXECUTABLE)) {
+                paths = g_slist_append(paths, extcap_path);
+            } else {
+                g_free(extcap_path);
             }
-        }
 
+        }
         g_dir_close(dir);
-        g_string_free(extcap_path, TRUE);
-    }
-    return count;
-}
-
-static gboolean
-extcap_if_exists(const gchar *ifname)
-{
-    if (!ifname || !_tool_for_ifname)
-    {
-        return FALSE;
     }
 
-    if (g_hash_table_lookup(_tool_for_ifname, ifname))
-    {
-        return TRUE;
-    }
-
-    return FALSE;
+    return paths;
 }
 
 static extcap_interface *
@@ -226,8 +265,7 @@ extcap_free_toolbar_control(iface_toolbar_control *control)
     if (control->ctrl_type == INTERFACE_TYPE_STRING) {
         g_free(control->default_value.string);
     }
-    g_list_foreach(control->values, (GFunc)extcap_free_toolbar_value, NULL);
-    g_list_free(control->values);
+    g_list_free_full(control->values, (GDestroyNotify)extcap_free_toolbar_value);
     g_free(control);
 }
 
@@ -244,24 +282,10 @@ extcap_free_toolbar(gpointer data)
     g_free(toolbar->menu_title);
     g_free(toolbar->help);
     g_list_free_full(toolbar->ifnames, g_free);
-    g_list_foreach(toolbar->controls, (GFunc)extcap_free_toolbar_control, NULL);
-    g_list_free(toolbar->controls);
+    g_list_free_full(toolbar->controls, (GDestroyNotify)extcap_free_toolbar_control);
     g_free(toolbar);
 }
 
-static gboolean
-extcap_if_exists_for_extcap(const gchar *ifname, const gchar *extcap)
-{
-    extcap_interface *entry = extcap_find_interface_for_ifname(ifname);
-
-    if (entry && strcmp(entry->extcap_path, extcap) == 0)
-    {
-        return TRUE;
-    }
-
-    return FALSE;
-}
-
 static gchar *
 extcap_if_executable(const gchar *ifname)
 {
@@ -289,63 +313,162 @@ extcap_iface_toolbar_add(const gchar *extcap, iface_toolbar *toolbar_entry)
     g_free(toolname);
 }
 
-/* Note: args does not need to be NULL-terminated. */
-static gboolean extcap_foreach(GList * arguments,
-                                      extcap_cb_t cb, extcap_callback_info_t cb_info)
+static gchar **
+extcap_convert_arguments_to_array(GList * arguments)
+{
+    gchar ** result = NULL;
+    if ( arguments )
+    {
+        GList * walker = g_list_first(arguments);
+        int cnt = 0;
+
+        result = (gchar **) g_malloc0(sizeof(gchar *) * (g_list_length(arguments)));
+
+        while(walker)
+        {
+            result[cnt] = g_strdup((const gchar *)walker->data);
+            walker = g_list_next(walker);
+            cnt++;
+        }
+    }
+    return result;
+}
+
+static void extcap_free_array(gchar ** args, int argc)
+{
+    int cnt = 0;
+
+    for ( cnt = 0; cnt < argc; cnt++ )
+        g_free(args[cnt]);
+    g_free(args);
+}
+
+static void
+extcap_free_extcaps_info_array(extcap_run_extcaps_info_t *infos, guint count)
+{
+    for (guint i = 0; i < count; i++) {
+        g_free(infos[i].extcap_path);
+        g_free(infos[i].output);
+        for (guint j = 0; j < infos[i].num_interfaces; j++) {
+            extcap_iface_info_t *iface_info = &infos[i].iface_infos[j];
+            g_free(iface_info->ifname);
+            g_free(iface_info->output);
+        }
+        g_free(infos[i].iface_infos);
+    }
+    g_free(infos);
+}
+
+static void
+extcap_run_one(const extcap_interface *interface, GList *arguments, extcap_cb_t cb, void *user_data, char **err_str)
 {
-    GDir *dir;
-    gboolean keep_going;
     const char *dirname = get_extcap_dir();
+    gchar **args = extcap_convert_arguments_to_array(arguments);
+    int cnt = g_list_length(arguments);
+    gchar *command_output;
+    if (ws_pipe_spawn_sync(dirname, interface->extcap_path, cnt, args, &command_output)) {
+        extcap_callback_info_t cb_info = {
+            .ifname = interface->call,
+            .extcap = interface->extcap_path,
+            .output = command_output,
+            .data = user_data,
+            .err_str = err_str,
+        };
+        cb(cb_info);
+        g_free(command_output);
+    }
+    extcap_free_array(args, cnt);
+}
 
-    keep_going = TRUE;
+/** Thread callback to run an extcap program and pass its output. */
+static void
+extcap_thread_callback(gpointer data, gpointer user_data)
+{
+    extcap_run_task_t *task = (extcap_run_task_t *)data;
+    thread_pool_t *pool = (thread_pool_t *)user_data;
+    const char *dirname = get_extcap_dir();
 
-    if (arguments && (dir = g_dir_open(dirname, 0, NULL)) != NULL)
-    {
-        GString *extcap_path = NULL;
-        const gchar *file;
+    char *command_output;
+    if (ws_pipe_spawn_sync(dirname, task->extcap_path, g_strv_length(task->argv), task->argv, &command_output)) {
+        task->output_cb(pool, task->data, command_output);
+    } else {
+        task->output_cb(pool, task->data, NULL);
+    }
+    g_strfreev(task->argv);
+    g_free(task);
 
-        extcap_path = g_string_new("");
-        while (keep_going && (file = g_dir_read_name(dir)) != NULL)
-        {
-            gchar *command_output = NULL;
-            gchar ** args = (gchar **) g_malloc0(sizeof(gchar *) * (g_list_length(arguments)));
-            GList * walker = g_list_first(arguments);
-            int cnt = 0;
-            while(walker)
-            {
-                args[cnt] = g_strdup((const gchar *)walker->data);
-                walker = g_list_next(walker);
-                cnt++;
-            }
+    // Notify when all tasks are completed and no new subtasks were created.
+    g_mutex_lock(&pool->data_mutex);
+    if (--pool->count == 0) {
+        g_cond_signal(&pool->cond);
+    }
+    g_mutex_unlock(&pool->data_mutex);
+}
 
-            /* full path to extcap binary */
-            g_string_printf(extcap_path, "%s" G_DIR_SEPARATOR_S "%s", dirname, file);
-            /* treat anything executable as an extcap binary */
-            if (g_file_test(extcap_path->str, G_FILE_TEST_IS_REGULAR) &&
-                g_file_test(extcap_path->str, G_FILE_TEST_IS_EXECUTABLE))
-            {
-                if (extcap_if_exists(cb_info.ifname) && !extcap_if_exists_for_extcap(cb_info.ifname, extcap_path->str))
-                {
-                    continue;
-                }
+/*
+ * Run all extcap programs with the given arguments list, invoke the callback to
+ * do some processing and return the results.
+ *
+ * @param [IN] argv NULL-terminated arguments list.
+ * @param [IN] output_cb Thread callback function that receives the output.
+ * @param [IN] data_size Size of the per-program information that will be returned.
+ * @param [OUT] count Size of the returned array.
+ * @return Array of information or NULL if there are none. The first member of
+ * each element (char *extcap_path) must be freed.
+ */
+static gpointer
+extcap_run_all(const char *argv[], extcap_run_cb_t output_cb, gsize data_size, guint *count)
+{
+    /* Need enough space for at least 'extcap_path'. */
+    g_assert(data_size >= sizeof(char *));
 
-                if (ws_pipe_spawn_sync((gchar *) dirname, extcap_path->str, cnt, args, &command_output))
-                {
-                    cb_info.output = command_output;
-                    cb_info.extcap = extcap_path->str;
+    GSList *paths = extcap_get_extcap_paths();
+    int i = 0;
+#if GLIB_CHECK_VERSION(2,36,0)
+    int max_threads = (int)g_get_num_processors();
+#else
+    // If the number of processors is unavailable, just use some sane maximum.
+    // extcap should not be CPU bound, so -1 could also be used for unlimited.
+    int max_threads = 8;
+#endif
 
-                    keep_going = cb(cb_info);
-                }
+    if (!paths) {
+        *count = 0;
+        return NULL;
+    }
 
-                g_free(command_output);
-            }
-        }
+    guint64 start_time = g_get_monotonic_time();
+    guint paths_count = g_slist_length(paths);
+    /* GSList is not thread-safe, so pre-allocate an array instead. */
+    gpointer infos = g_malloc0_n(paths_count, data_size);
 
-        g_dir_close(dir);
-        g_string_free(extcap_path, TRUE);
+    thread_pool_t pool;
+    pool.pool = g_thread_pool_new(extcap_thread_callback, &pool, max_threads, FALSE, NULL);
+    pool.count = 0;
+    g_cond_init(&pool.cond);
+    g_mutex_init(&pool.data_mutex);
+
+    for (GSList *path = paths; path; path = g_slist_next(path), i++) {
+        extcap_run_task_t *task = g_new0(extcap_run_task_t, 1);
+
+        task->extcap_path = (char *)path->data;
+        task->argv = g_strdupv((char **)argv);
+        task->output_cb = output_cb;
+        task->data = ((char *)infos) + (i * data_size);
+        *((char **)task->data) = (char *)path->data;
+
+        thread_pool_push(&pool, task, NULL);
     }
+    g_slist_free(paths);    /* Note: the contents are transferred to 'infos'. */
+
+    /* Wait for all (sub)tasks to complete. */
+    thread_pool_wait(&pool);
+    g_thread_pool_free(pool.pool, FALSE, TRUE);
 
-    return keep_going;
+    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap: completed discovery of %d tools in %.3fms",
+            paths_count, (g_get_monotonic_time() - start_time) / 1000.0);
+    *count = paths_count;
+    return infos;
 }
 
 static void extcap_free_dlt(gpointer d, gpointer user_data _U_)
@@ -438,18 +561,14 @@ extcap_get_if_dlts(const gchar *ifname, char **err_str)
         *err_str = NULL;
     }
 
-    if (extcap_if_exists(ifname))
+    extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
+    if (interface)
     {
-        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_LIST_DLTS) );
-        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE) );
-        arguments = g_list_append(arguments, g_strdup(ifname) );
-
-        extcap_callback_info_t cb_info;
-        cb_info.data = &caps;
-        cb_info.err_str = err_str;
-        cb_info.ifname = ifname;
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_LIST_DLTS));
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
+        arguments = g_list_append(arguments, g_strdup(ifname));
 
-        extcap_foreach(arguments, cb_dlt, cb_info);
+        extcap_run_one(interface, arguments, cb_dlt, &caps, err_str);
 
         g_list_free_full(arguments, g_free);
     }
@@ -482,8 +601,7 @@ static void extcap_free_interfaces(GList *interfaces)
         return;
     }
 
-    g_list_foreach(interfaces, (GFunc)extcap_free_interface, NULL);
-    g_list_free(interfaces);
+    g_list_free_full(interfaces, extcap_free_interface);
 }
 
 static gint
@@ -563,17 +681,6 @@ append_extcap_interface_list(GList *list, char **err_str _U_)
     return list;
 }
 
-static void
-extcap_register_preferences_callback(gpointer key, gpointer value _U_, gpointer user_data _U_)
-{
-    GList *arguments;
-
-    arguments = extcap_get_if_configuration((gchar *)key);
-    /* Memory for prefs are external to an interface, they are part of
-     * extcap core, so the parsed arguments can be freed. */
-    extcap_free_if_configuration(arguments, TRUE);
-}
-
 void extcap_register_preferences(void)
 {
     if (prefs.capture_no_extcap)
@@ -586,11 +693,8 @@ void extcap_register_preferences(void)
         return;
     }
 
-    if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
-        extcap_load_interface_list();
-
-
-    g_hash_table_foreach(_tool_for_ifname, extcap_register_preferences_callback, NULL);
+    // Will load information about extcaps and their supported config.
+    extcap_load_interface_list();
 }
 
 /**
@@ -667,7 +771,7 @@ extcap_pref_for_argument(const gchar *ifname, struct _extcap_arg *arg)
     struct preference *pref = NULL;
 
     GRegex *regex_name = g_regex_new("[-]+", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
-    GRegex *regex_ifname = g_regex_new("(?![a-zA-Z1-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
+    GRegex *regex_ifname = g_regex_new("(?![a-zA-Z0-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
     if (regex_name && regex_ifname)
     {
         if (prefs_find_module("extcap"))
@@ -712,7 +816,7 @@ static gboolean cb_preference(extcap_callback_info_t cb_info)
         GList *walker = arguments;
 
         GRegex *regex_name = g_regex_new("[-]+", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
-        GRegex *regex_ifname = g_regex_new("(?![a-zA-Z1-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
+        GRegex *regex_ifname = g_regex_new("(?![a-zA-Z0-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
         if (regex_name && regex_ifname)
         {
             while (walker != NULL)
@@ -783,23 +887,18 @@ extcap_get_if_configuration(const char *ifname)
 {
     GList * arguments = NULL;
     GList *ret = NULL;
-    gchar **err_str = NULL;
 
-    if (extcap_if_exists(ifname))
+    extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
+    if (interface)
     {
         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
               get_extcap_dir());
 
-        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_CONFIG) );
-        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE) );
-        arguments = g_list_append(arguments, g_strdup(ifname) );
-
-        extcap_callback_info_t cb_info;
-        cb_info.data = &ret;
-        cb_info.err_str = err_str;
-        cb_info.ifname = ifname;
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_CONFIG));
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
+        arguments = g_list_append(arguments, g_strdup(ifname));
 
-        extcap_foreach(arguments, cb_preference, cb_info);
+        extcap_run_one(interface, arguments, cb_preference, &ret, NULL);
 
         g_list_free_full(arguments, g_free);
     }
@@ -814,7 +913,7 @@ static gboolean cb_reload_preference(extcap_callback_info_t cb_info)
 
     arguments = extcap_parse_values(cb_info.output);
 
-    walker = g_list_first((GList *)(arguments));
+    walker = g_list_first(arguments);
     while (walker != NULL)
     {
         extcap_value * val = (extcap_value *)walker->data;
@@ -832,18 +931,18 @@ extcap_get_if_configuration_values(const char * ifname, const char * argname, GH
 {
     GList * args = NULL;
     GList *ret = NULL;
-    gchar **err_str = NULL;
 
-    if (extcap_if_exists(ifname))
+    extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
+    if (interface)
     {
         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
               get_extcap_dir());
 
-        args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_CONFIG) );
-        args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_INTERFACE) );
-        args = g_list_append(args, g_strdup(ifname) );
-        args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_RELOAD_OPTION) );
-        args = g_list_append(args, g_strdup(argname) );
+        args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_CONFIG));
+        args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
+        args = g_list_append(args, g_strdup(ifname));
+        args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_RELOAD_OPTION));
+        args = g_list_append(args, g_strdup(argname));
 
         if ( arguments )
         {
@@ -851,18 +950,13 @@ extcap_get_if_configuration_values(const char * ifname, const char * argname, GH
             while ( keys )
             {
                 const gchar * key_data = (const gchar *)keys->data;
-                args = g_list_append(args, g_strdup(key_data) );
-                args = g_list_append(args, g_strdup((const gchar *)g_hash_table_lookup(arguments, key_data)) );
+                args = g_list_append(args, g_strdup(key_data));
+                args = g_list_append(args, g_strdup((const gchar *)g_hash_table_lookup(arguments, key_data)));
                 keys = g_list_next(keys);
             }
         }
 
-        extcap_callback_info_t cb_info;
-        cb_info.data = &ret;
-        cb_info.err_str = err_str;
-        cb_info.ifname = ifname;
-
-        extcap_foreach(args, cb_reload_preference, cb_info);
+        extcap_run_one(interface, args, cb_reload_preference, &ret, NULL);
 
         g_list_free_full(args, g_free);
     }
@@ -885,7 +979,7 @@ extcap_has_configuration(const char *ifname, gboolean is_required)
 
     gboolean found = FALSE;
 
-    arguments = extcap_get_if_configuration((const char *)(ifname));
+    arguments = extcap_get_if_configuration(ifname);
     walker = g_list_first(arguments);
 
     while (walker != NULL && !found)
@@ -977,22 +1071,18 @@ extcap_verify_capture_filter(const char *ifname, const char *filter, gchar **err
     GList * arguments = NULL;
     extcap_filter_status status = EXTCAP_FILTER_UNKNOWN;
 
-    if (extcap_if_exists(ifname))
+    extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
+    if (interface)
     {
         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
               get_extcap_dir());
 
-        arguments = g_list_append(arguments, EXTCAP_ARGUMENT_CAPTURE_FILTER);
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_CAPTURE_FILTER));
         arguments = g_list_append(arguments, g_strdup(filter));
-        arguments = g_list_append(arguments, EXTCAP_ARGUMENT_INTERFACE);
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
         arguments = g_list_append(arguments, g_strdup(ifname));
 
-        extcap_callback_info_t cb_info;
-        cb_info.data = &status;
-        cb_info.err_str = err_str;
-        cb_info.ifname = ifname;
-
-        extcap_foreach(arguments, cb_verify_filter, cb_info);
+        extcap_run_one(interface, arguments, cb_verify_filter, &status, err_str);
         g_list_free_full(arguments, g_free);
     }
 
@@ -1100,13 +1190,13 @@ void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg)
         pipedata = (ws_pipe_t *) interface_opts->extcap_pipedata;
         if (pipedata)
         {
-            if (pipedata->stderr_fd > 0 && ws_pipe_data_available(pipedata->stderr_fd))
+            if (pipedata->stderr_fd > 0)
             {
                 buffer = (gchar *)g_malloc0(STDERR_BUFFER_SIZE + 1);
                 ws_read_string_from_pipe(ws_get_pipe_handle(pipedata->stderr_fd), buffer, STDERR_BUFFER_SIZE + 1);
                 if (strlen(buffer) > 0)
                 {
-                    pipedata->stderr_msg = g_strdup_printf("%s", buffer);
+                    pipedata->stderr_msg = g_strdup(buffer);
                     pipedata->exitcode = 1;
                 }
                 g_free(buffer);
@@ -1114,7 +1204,7 @@ void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg)
 
 #ifndef _WIN32
             /* Final child watch may not have been called */
-            if (interface_opts->extcap_child_watch != 0)
+            if (interface_opts->extcap_child_watch > 0)
             {
                 extcap_child_watch_cb(pipedata->pid, 0, capture_opts);
                 /* it will have changed in extcap_child_watch_cb */
@@ -1130,7 +1220,7 @@ void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg)
 
             if (overwrite_exitcode || pipedata->exitcode != 0)
             {
-                if (pipedata->stderr_msg != 0)
+                if (pipedata->stderr_msg != NULL)
                 {
                     if (*errormsg == NULL)
                     {
@@ -1156,16 +1246,25 @@ void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg)
             interface_opts->extcap_child_watch = 0;
         }
 
-        if (interface_opts->extcap_pid != WS_INVALID_PID)
-        {
-#ifdef _WIN32
-            TerminateProcess(interface_opts->extcap_pid, 0);
-#endif
-            g_spawn_close_pid(interface_opts->extcap_pid);
-            interface_opts->extcap_pid = WS_INVALID_PID;
+        if (pipedata) {
+            if (pipedata->stdout_fd > 0)
+            {
+                ws_close(pipedata->stdout_fd);
+            }
+
+            if (pipedata->stderr_fd > 0)
+            {
+                ws_close(pipedata->stderr_fd);
+            }
 
-            g_free(interface_opts->extcap_pipedata);
-            interface_opts->extcap_pipedata = NULL;
+            if (interface_opts->extcap_pid != WS_INVALID_PID)
+            {
+                ws_pipe_close(pipedata);
+                interface_opts->extcap_pid = WS_INVALID_PID;
+
+                g_free(pipedata);
+                interface_opts->extcap_pipedata = NULL;
+            }
         }
     }
 }
@@ -1347,6 +1446,11 @@ GPtrArray *extcap_prepare_arguments(interface_options *interface_opts)
     return result;
 }
 
+static void ptr_array_free(gpointer data, gpointer user_data _U_)
+{
+    g_free(data);
+}
+
 /* call mkfifo for each extcap,
  * returns FALSE if there's an error creating a FIFO */
 gboolean
@@ -1373,12 +1477,12 @@ extcap_init_interfaces(capture_options *capture_opts)
         if (extcap_has_toolbar(interface_opts->name))
         {
             extcap_create_pipe(interface_opts->name, &interface_opts->extcap_control_in,
-                               EXTCAP_CONTROL_IN_PREFIX, FALSE);
+                               EXTCAP_CONTROL_IN_PREFIX);
 #ifdef _WIN32
             interface_opts->extcap_control_in_h = pipe_h;
 #endif
             extcap_create_pipe(interface_opts->name, &interface_opts->extcap_control_out,
-                               EXTCAP_CONTROL_OUT_PREFIX, FALSE);
+                               EXTCAP_CONTROL_OUT_PREFIX);
 #ifdef _WIN32
             interface_opts->extcap_control_out_h = pipe_h;
 #endif
@@ -1386,7 +1490,7 @@ extcap_init_interfaces(capture_options *capture_opts)
 
         /* create pipe for fifo */
         if (!extcap_create_pipe(interface_opts->name, &interface_opts->extcap_fifo,
-                                EXTCAP_PIPE_PREFIX, TRUE))
+                                EXTCAP_PIPE_PREFIX))
         {
             return FALSE;
         }
@@ -1401,7 +1505,7 @@ extcap_init_interfaces(capture_options *capture_opts)
 
         pid = ws_pipe_spawn_async(pipedata, args);
 
-        g_ptr_array_foreach(args, (GFunc)g_free, NULL);
+        g_ptr_array_foreach(args, ptr_array_free, NULL);
         g_ptr_array_free(args, TRUE);
 
         if (pid == WS_INVALID_PID)
@@ -1448,7 +1552,7 @@ extcap_init_interfaces(capture_options *capture_opts)
 }
 
 #ifdef _WIN32
-gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *pipe_prefix, gboolean byte_mode)
+gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *pipe_prefix)
 {
     gchar timestr[ 14 + 1 ];
     time_t current_time;
@@ -1474,8 +1578,7 @@ gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *pipe
     pipe_h = CreateNamedPipe(
                  utf_8to16(pipename),
                  PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
-                 (byte_mode ? (PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT) :
-                              (PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT)),
+                 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
                  1, 65536, 65536,
                  300,
                  &security);
@@ -1495,7 +1598,7 @@ gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *pipe
     return TRUE;
 }
 #else
-gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *pipe_prefix, gboolean byte_mode _U_)
+gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *pipe_prefix)
 {
     gchar *temp_name = NULL;
     int fd = 0;
@@ -1600,49 +1703,47 @@ static void remove_extcap_entry(gpointer entry, gpointer data _U_)
         extcap_free_interface(entry);
 }
 
-static gboolean cb_load_interfaces(extcap_callback_info_t cb_info)
+static void
+process_new_extcap(const char *extcap, char *output)
 {
     GList * interfaces = NULL, * control_items = NULL, * walker = NULL;
     extcap_interface * int_iter = NULL;
     extcap_info * element = NULL;
     iface_toolbar * toolbar_entry = NULL;
-    gchar * toolname = g_path_get_basename(cb_info.extcap);
+    gchar * toolname = g_path_get_basename(extcap);
 
     GList * interface_keys = g_hash_table_get_keys(_loaded_interfaces);
 
     /* Load interfaces from utility */
-    interfaces = extcap_parse_interfaces(cb_info.output, &control_items);
+    interfaces = extcap_parse_interfaces(output, &control_items);
 
-    if (control_items)
-    {
-        toolbar_entry = g_new0(iface_toolbar, 1);
-        toolbar_entry->controls = control_items;
-    }
-
-    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Loading interface list for %s ", cb_info.extcap);
+    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Loading interface list for %s ", extcap);
 
     /* Seems, that there where no interfaces to be loaded */
     if ( ! interfaces || g_list_length(interfaces) == 0 )
     {
-        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Cannot load interfaces for %s", cb_info.extcap );
-        /* Some utilities, androiddump for example, may actually don't present any interfaces, even
-         * if the utility itself is present. In such a case, we return here, but do not return
-         * FALSE, or otherwise further loading of other utilities will be stopped */
+        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Cannot load interfaces for %s", extcap );
         g_list_free(interface_keys);
         g_free(toolname);
-        return TRUE;
+        return;
     }
 
     /* Load or create the storage element for the tool */
     element = extcap_ensure_interface(toolname, TRUE);
     if ( element == NULL )
     {
-        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_ERROR, "Cannot store interface %s, maybe duplicate?", cb_info.extcap );
+        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_ERROR, "Cannot store interface %s, maybe duplicate?", extcap );
         g_list_foreach(interfaces, remove_extcap_entry, NULL);
         g_list_free(interfaces);
         g_list_free(interface_keys);
         g_free(toolname);
-        return FALSE;
+        return;
+    }
+
+    if (control_items)
+    {
+        toolbar_entry = g_new0(iface_toolbar, 1);
+        toolbar_entry->controls = control_items;
     }
 
     walker = interfaces;
@@ -1667,7 +1768,7 @@ static gboolean cb_load_interfaces(extcap_callback_info_t cb_info)
             {
                 element->version = g_strdup(int_iter->version);
                 element->basename = g_strdup(toolname);
-                element->full_path = g_strdup(cb_info.extcap);
+                element->full_path = g_strdup(extcap);
                 element->help = g_strdup(int_iter->help);
             }
 
@@ -1690,7 +1791,7 @@ static gboolean cb_load_interfaces(extcap_callback_info_t cb_info)
             if ( g_list_find(interface_keys, int_iter->call) )
             {
                 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Extcap interface \"%s\" is already provided by \"%s\" ",
-                      int_iter->call, (gchar *)extcap_if_executable(int_iter->call));
+                      int_iter->call, extcap_if_executable(int_iter->call));
                 walker = g_list_next(walker);
                 continue;
             }
@@ -1698,7 +1799,7 @@ static gboolean cb_load_interfaces(extcap_callback_info_t cb_info)
             if ((int_iter->call != NULL) && (int_iter->display))
                 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  Interface [%s] \"%s\" ", int_iter->call, int_iter->display);
 
-            int_iter->extcap_path = g_strdup(cb_info.extcap);
+            int_iter->extcap_path = g_strdup(extcap);
 
             /* Only set the help, if it exists and no parsed help information is present */
             if ( ! int_iter->help && help )
@@ -1723,14 +1824,116 @@ static gboolean cb_load_interfaces(extcap_callback_info_t cb_info)
     if (toolbar_entry && toolbar_entry->menu_title)
     {
         iface_toolbar_add(toolbar_entry);
-        extcap_iface_toolbar_add(cb_info.extcap, toolbar_entry);
+        extcap_iface_toolbar_add(extcap, toolbar_entry);
     }
 
     g_list_foreach(interfaces, remove_extcap_entry, NULL);
     g_list_free(interfaces);
     g_list_free(interface_keys);
     g_free(toolname);
-    return TRUE;
+}
+
+
+/** Thread callback to save the output of a --extcap-config call. */
+static void
+extcap_process_config_cb(thread_pool_t *pool _U_, void *data, char *output)
+{
+    extcap_iface_info_t *iface_info = (extcap_iface_info_t *)data;
+    iface_info->output = output;
+}
+
+/**
+ * Thread callback to process discovered interfaces, scheduling more tasks to
+ * retrieve the configuration for each interface. Called once for every extcap
+ * program.
+ */
+static void
+extcap_process_interfaces_cb(thread_pool_t *pool, void *data, char *output)
+{
+    extcap_run_extcaps_info_t *info = (extcap_run_extcaps_info_t *)data;
+    guint i = 0;
+    guint num_interfaces = 0;
+
+    if (!output) {
+        // No interfaces available, nothing to do.
+        return;
+    }
+
+    // Save output for process_new_extcap.
+    info->output = output;
+
+    // Are there any interfaces to query information from?
+    GList *interfaces = extcap_parse_interfaces(output, NULL);
+    for (GList *iface = interfaces; iface; iface = g_list_next(iface)) {
+        extcap_interface *intf = (extcap_interface *)iface->data;
+        if (intf->if_type == EXTCAP_SENTENCE_INTERFACE) {
+            ++num_interfaces;
+        }
+    }
+    if (num_interfaces == 0) {
+        // nothing to do.
+        g_list_free_full(interfaces, extcap_free_interface);
+        return;
+    }
+
+    /* GSList is not thread-safe, so pre-allocate an array instead. */
+    info->iface_infos = g_new0(extcap_iface_info_t, num_interfaces);
+    info->num_interfaces = num_interfaces;
+
+    // Schedule new commands to retrieve the configuration.
+    for (GList *iface = interfaces; iface; iface = g_list_next(iface)) {
+        extcap_interface *intf = (extcap_interface *)iface->data;
+        if (intf->if_type != EXTCAP_SENTENCE_INTERFACE) {
+            continue;
+        }
+
+        const char *argv[] = {
+            EXTCAP_ARGUMENT_CONFIG,
+            EXTCAP_ARGUMENT_INTERFACE,
+            intf->call,
+            NULL
+        };
+        extcap_run_task_t *task = g_new0(extcap_run_task_t, 1);
+        extcap_iface_info_t *iface_info = &info->iface_infos[i++];
+
+        task->extcap_path = info->extcap_path;
+        task->argv = g_strdupv((char **)argv);
+        task->output_cb = extcap_process_config_cb;
+        task->data = iface_info;
+        iface_info->ifname = g_strdup(intf->call);
+
+        thread_pool_push(pool, task, NULL);
+    }
+    g_list_free_full(interfaces, extcap_free_interface);
+}
+
+/**
+ * Thread callback to check whether the new-style --list-interfaces call with an
+ * explicit function succeeded. If not, schedule a call without the new version
+ * argument.
+ */
+static void
+extcap_list_interfaces_cb(thread_pool_t *pool, void *data, char *output)
+{
+    extcap_run_extcaps_info_t *info = (extcap_run_extcaps_info_t *)data;
+
+    if (!output) {
+        /* No output available, schedule a fallback query. */
+        const char *argv[] = {
+            EXTCAP_ARGUMENT_LIST_INTERFACES,
+            NULL
+        };
+        extcap_run_task_t *task = g_new0(extcap_run_task_t, 1);
+
+        task->extcap_path = info->extcap_path;
+        task->argv = g_strdupv((char **)argv);
+        task->output_cb = extcap_process_interfaces_cb;
+        task->data = info;
+
+        thread_pool_push(pool, task, NULL);
+    } else {
+        extcap_process_interfaces_cb(pool, info, output);
+    }
 }
 
 
@@ -1741,8 +1944,6 @@ static gboolean cb_load_interfaces(extcap_callback_info_t cb_info)
 static void
 extcap_load_interface_list(void)
 {
-    gchar *error = NULL;
-
     if (prefs.capture_no_extcap)
         return;
 
@@ -1763,9 +1964,11 @@ extcap_load_interface_list(void)
 
     if (_loaded_interfaces == NULL)
     {
-        GList * arguments = NULL;
         int major = 0;
         int minor = 0;
+        guint count = 0;
+        extcap_run_extcaps_info_t *infos;
+        GList *unused_arguments = NULL;
 
         _loaded_interfaces = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_interface_info);
         /* Cleanup lookup table */
@@ -1777,35 +1980,42 @@ extcap_load_interface_list(void)
             _tool_for_ifname = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
         }
 
-        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_LIST_INTERFACES));
-        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_VERSION));
-
         get_ws_version_number(&major, &minor, NULL);
+        char *arg_version = g_strdup_printf("%s=%d.%d", EXTCAP_ARGUMENT_VERSION, major, minor);
+        const char *argv[] = {
+            EXTCAP_ARGUMENT_LIST_INTERFACES,
+            arg_version,
+            NULL
+        };
+        infos = (extcap_run_extcaps_info_t *)extcap_run_all(argv,
+                extcap_list_interfaces_cb, sizeof(extcap_run_extcaps_info_t),
+                &count);
+        for (guint i = 0; i < count; i++) {
+            if (!infos[i].output) {
+                continue;
+            }
 
-        arguments = g_list_append(arguments, g_strdup_printf("%d.%d", major, minor));
-
-        extcap_callback_info_t cb_info;
-        cb_info.data = NULL;
-        cb_info.ifname = NULL;
-        cb_info.err_str = &error;
-
-        extcap_foreach(arguments, cb_load_interfaces, cb_info);
+            // Save new extcap and each discovered interface.
+            process_new_extcap(infos[i].extcap_path, infos[i].output);
+            for (guint j = 0; j < infos[i].num_interfaces; j++) {
+                extcap_iface_info_t *iface_info = &infos[i].iface_infos[j];
 
-        /* Compatibility mode. Utility did not accept version argument, therefore we just ask for the interfaces
-         * and assume, that the utility will handle compatibility on it's end */
-        if ( ! cb_info.data )
-        {
-            g_free(error);
-            error = NULL;
-
-            g_list_free_full(arguments, g_free);
-            arguments = NULL;
+                if (!iface_info->output) {
+                    continue;
+                }
 
-            arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_LIST_INTERFACES));
-            extcap_foreach(arguments, cb_load_interfaces, cb_info);
+                extcap_callback_info_t cb_info = {
+                    .ifname = iface_info->ifname,
+                    .output = iface_info->output,
+                    .data = &unused_arguments,
+                };
+                cb_preference(cb_info);
+            }
         }
-
-        g_list_free_full(arguments, g_free);
+        /* XXX rework cb_preference such that this unused list can be removed. */
+        extcap_free_if_configuration(unused_arguments, TRUE);
+        extcap_free_extcaps_info_array(infos, count);
+        g_free(arg_version);
     }
 }