From 7ae6e980e4f003ceeeb4f5d99dc9e8e5aa6b8d6a Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Thu, 3 Nov 2016 18:07:11 +0100 Subject: [PATCH] core: move plugin path reading function to nm-core-utils.c Even if it's called only from one place, it can be considered an utility function. --- src/devices/nm-device-factory.c | 97 ++------------------------------ src/nm-core-utils.c | 98 +++++++++++++++++++++++++++++++++ src/nm-core-utils.h | 2 + 3 files changed, 104 insertions(+), 93 deletions(-) diff --git a/src/devices/nm-device-factory.c b/src/devices/nm-device-factory.c index 7049c698f8..58bf8b7e95 100644 --- a/src/devices/nm-device-factory.c +++ b/src/devices/nm-device-factory.c @@ -31,6 +31,9 @@ #include "platform/nm-platform.h" #include "nm-utils.h" +#define PLUGIN_PREFIX "libnm-device-plugin-" +#define PLUGIN_PATH_TAG "NMManager-plugin-path" + /*****************************************************************************/ enum { @@ -328,98 +331,6 @@ nm_device_factory_manager_for_each_factory (NMDeviceFactoryManagerFactoryFunc ca g_slist_free (list); } -#define PLUGIN_PREFIX "libnm-device-plugin-" -#define PLUGIN_PATH_TAG "NMManager-plugin-path" - -struct read_device_factory_paths_data { - char *path; - struct stat st; -}; - -static gint -read_device_factory_paths_sort_fcn (gconstpointer a, gconstpointer b) -{ - const struct read_device_factory_paths_data *da = a; - const struct read_device_factory_paths_data *db = b; - time_t ta, tb; - - ta = MAX (da->st.st_mtime, da->st.st_ctime); - tb = MAX (db->st.st_mtime, db->st.st_ctime); - - if (ta < tb) - return 1; - if (ta > tb) - return -1; - return 0; -} - -static char** -read_device_factory_paths (void) -{ - GDir *dir; - GError *error = NULL; - const char *item; - GArray *paths; - char **result; - guint i; - - dir = g_dir_open (NMPLUGINDIR, 0, &error); - if (!dir) { - nm_log_warn (LOGD_PLATFORM, "device plugin: failed to open directory %s: %s", - NMPLUGINDIR, - error->message); - g_clear_error (&error); - return NULL; - } - - paths = g_array_new (FALSE, FALSE, sizeof (struct read_device_factory_paths_data)); - - while ((item = g_dir_read_name (dir))) { - int errsv; - struct read_device_factory_paths_data data; - - if (!g_str_has_prefix (item, PLUGIN_PREFIX)) - continue; - if (g_str_has_suffix (item, ".la")) - continue; - - data.path = g_build_filename (NMPLUGINDIR, item, NULL); - - if (stat (data.path, &data.st) != 0) { - errsv = errno; - nm_log_warn (LOGD_PLATFORM, "device plugin: skip invalid file %s (error during stat: %s)", data.path, strerror (errsv)); - goto NEXT; - } - if (!S_ISREG (data.st.st_mode)) - goto NEXT; - if (data.st.st_uid != 0) { - nm_log_warn (LOGD_PLATFORM, "device plugin: skip invalid file %s (file must be owned by root)", data.path); - goto NEXT; - } - if (data.st.st_mode & (S_IWGRP | S_IWOTH | S_ISUID)) { - nm_log_warn (LOGD_PLATFORM, "device plugin: skip invalid file %s (invalid file permissions)", data.path); - goto NEXT; - } - - g_array_append_val (paths, data); - continue; -NEXT: - g_free (data.path); - } - g_dir_close (dir); - - /* sort filenames by modification time. */ - g_array_sort (paths, read_device_factory_paths_sort_fcn); - - result = g_new (char *, paths->len + 1); - for (i = 0; i < paths->len; i++) - result[i] = g_array_index (paths, struct read_device_factory_paths_data, i).path; - result[i] = NULL; - - g_array_free (paths, TRUE); - return result; -} - static gboolean _add_factory (NMDeviceFactory *factory, gboolean check_duplicates, @@ -502,7 +413,7 @@ nm_device_factory_manager_load_factories (NMDeviceFactoryManagerFactoryFunc call _ADD_INTERNAL (nm_vlan_device_factory_get_type); _ADD_INTERNAL (nm_vxlan_device_factory_get_type); - paths = read_device_factory_paths (); + paths = nm_utils_read_plugin_paths (NMPLUGINDIR, PLUGIN_PREFIX); if (!paths) return; diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index d4d9be5bc8..df6a7fc94a 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -3781,3 +3781,101 @@ nm_utils_file_set_contents (const gchar *filename, return TRUE; } + +struct plugin_info { + char *path; + struct stat st; +}; + +static gint +read_device_factory_paths_sort_fcn (gconstpointer a, gconstpointer b) +{ + const struct plugin_info *da = a; + const struct plugin_info *db = b; + time_t ta, tb; + + ta = MAX (da->st.st_mtime, da->st.st_ctime); + tb = MAX (db->st.st_mtime, db->st.st_ctime); + + if (ta < tb) + return 1; + if (ta > tb) + return -1; + return 0; +} + +char ** +nm_utils_read_plugin_paths (const char *dirname, const char *prefix) +{ + GDir *dir; + GError *error = NULL; + const char *item; + GArray *paths; + char **result; + guint i; + + g_return_val_if_fail (dirname, NULL); + g_return_val_if_fail (prefix, NULL); + + dir = g_dir_open (dirname, 0, &error); + if (!dir) { + nm_log_warn (LOGD_CORE, "device plugin: failed to open directory %s: %s", + dirname, + error->message); + g_clear_error (&error); + return NULL; + } + + paths = g_array_new (FALSE, FALSE, sizeof (struct plugin_info)); + + while ((item = g_dir_read_name (dir))) { + int errsv; + struct plugin_info data; + + if (!g_str_has_prefix (item, prefix)) + continue; + if (g_str_has_suffix (item, ".la")) + continue; + + data.path = g_build_filename (dirname, item, NULL); + + if (stat (data.path, &data.st) != 0) { + errsv = errno; + nm_log_warn (LOGD_CORE, + "plugin: skip invalid file %s (error during stat: %s)", + data.path, strerror (errsv)); + goto skip; + } + if (!S_ISREG (data.st.st_mode)) + goto skip; + if (data.st.st_uid != 0) { + nm_log_warn (LOGD_CORE, + "plugin: skip invalid file %s (file must be owned by root)", + data.path); + goto skip; + } + if (data.st.st_mode & (S_IWGRP | S_IWOTH | S_ISUID)) { + nm_log_warn (LOGD_CORE, + "plugin: skip invalid file %s (invalid file permissions)", + data.path); + goto skip; + } + + g_array_append_val (paths, data); + continue; +skip: + g_free (data.path); + } + g_dir_close (dir); + + /* sort filenames by modification time. */ + g_array_sort (paths, read_device_factory_paths_sort_fcn); + + result = g_new (char *, paths->len + 1); + for (i = 0; i < paths->len; i++) + result[i] = g_array_index (paths, struct plugin_info, i).path; + result[i] = NULL; + + g_array_free (paths, TRUE); + return result; +} diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index 929031a1b8..7bcee8947c 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -451,4 +451,6 @@ gboolean nm_utils_file_set_contents (const gchar *filename, mode_t mode, GError **error); +char **nm_utils_read_plugin_paths (const char *dirname, const char *prefix); + #endif /* __NM_CORE_UTILS_H__ */