diff options
author | Dan Williams <dcbw@redhat.com> | 2014-05-14 15:50:33 -0500 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2014-06-06 13:43:45 -0500 |
commit | 5150cb88c26d13e8c7c188f4f10614b6d318ad89 (patch) | |
tree | 7c66a15e09767aa288530b6b6ff5fd8b148d762f | |
parent | 4cc13befd3cc242c5065ce6ea91e335b705b2f36 (diff) | |
download | NetworkManager-5150cb88c26d13e8c7c188f4f10614b6d318ad89.tar.gz |
dispatcher: only dispatch if scripts exist
If there are no dispatcher scripts, don't bother dispatching any
events. This saves some time configuring networking if the event
would have no effect anyway.
-rw-r--r-- | callouts/nm-dispatcher-api.h | 2 | ||||
-rw-r--r-- | callouts/nm-dispatcher.c | 2 | ||||
-rw-r--r-- | src/main.c | 3 | ||||
-rw-r--r-- | src/nm-dispatcher.c | 55 | ||||
-rw-r--r-- | src/nm-dispatcher.h | 2 |
5 files changed, 62 insertions, 2 deletions
diff --git a/callouts/nm-dispatcher-api.h b/callouts/nm-dispatcher-api.h index 9fea487ada..96db789283 100644 --- a/callouts/nm-dispatcher-api.h +++ b/callouts/nm-dispatcher-api.h @@ -20,6 +20,8 @@ #include <dbus/dbus-glib.h> +#define NMD_SCRIPT_DIR NMCONFDIR "/dispatcher.d" + /* dbus-glib types for dispatcher call return value */ #define DISPATCHER_TYPE_RESULT (dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_INVALID)) #define DISPATCHER_TYPE_RESULT_ARRAY (dbus_g_type_get_collection ("GPtrArray", DISPATCHER_TYPE_RESULT)) diff --git a/callouts/nm-dispatcher.c b/callouts/nm-dispatcher.c index e9cd2e74e2..3747fd563a 100644 --- a/callouts/nm-dispatcher.c +++ b/callouts/nm-dispatcher.c @@ -41,8 +41,6 @@ #include "nm-dispatcher-utils.h" #include "nm-glib-compat.h" -#define NMD_SCRIPT_DIR NMCONFDIR "/dispatcher.d" - static GMainLoop *loop = NULL; static gboolean debug = FALSE; diff --git a/src/main.c b/src/main.c index 4bd2b4324c..0ba65b0b8a 100644 --- a/src/main.c +++ b/src/main.c @@ -54,6 +54,7 @@ #include "nm-config.h" #include "nm-posix-signals.h" #include "nm-session-monitor.h" +#include "nm-dispatcher.h" #if !defined(NM_DIST_VERSION) # define NM_DIST_VERSION VERSION @@ -604,6 +605,8 @@ main (int argc, char *argv[]) dhcp_mgr = nm_dhcp_manager_get (); g_assert (dhcp_mgr != NULL); + nm_dispatcher_init (); + settings = nm_settings_new (&error); if (!settings) { nm_log_err (LOGD_CORE, "failed to initialize settings storage: %s", diff --git a/src/nm-dispatcher.c b/src/nm-dispatcher.c index bc10e920ce..24abd04186 100644 --- a/src/nm-dispatcher.c +++ b/src/nm-dispatcher.c @@ -31,6 +31,7 @@ #include "nm-dbus-glib-types.h" #include "nm-glib-compat.h" +static gboolean do_dispatch = TRUE; static GSList *requests = NULL; static void @@ -134,12 +135,15 @@ fill_vpn_props (NMIP4Config *ip4_config, typedef struct { DispatcherFunc callback; gpointer user_data; + guint idle_id; } DispatchInfo; static void dispatcher_info_free (DispatchInfo *info) { requests = g_slist_remove (requests, info); + if (info->idle_id) + g_source_remove (info->idle_id); g_free (info); } @@ -261,6 +265,18 @@ action_to_string (DispatcherAction action) g_assert_not_reached (); } +static gboolean +dispatcher_idle_cb (gpointer user_data) +{ + DispatchInfo *info = user_data; + + info->idle_id = 0; + if (info->callback) + info->callback (info, info->user_data); + dispatcher_info_free (info); + return G_SOURCE_REMOVE; +} + static gconstpointer _dispatcher_call (DispatcherAction action, NMConnection *connection, @@ -299,6 +315,16 @@ _dispatcher_call (DispatcherAction action, if (action == DISPATCHER_ACTION_VPN_UP) g_return_val_if_fail (vpn_ip4_config != NULL, NULL); + if (do_dispatch == FALSE) { + info = g_malloc0 (sizeof (*info)); + info->callback = callback; + info->user_data = user_data; + info->idle_id = g_idle_add (dispatcher_idle_cb, info); + requests = g_slist_append (requests, info); + nm_log_dbg (LOGD_DISPATCH, "ignoring request; no scripts in " NMD_SCRIPT_DIR); + return info; + } + g_connection = nm_dbus_manager_get_connection (nm_dbus_manager_get ()); proxy = dbus_g_proxy_new_for_name (g_connection, NM_DISPATCHER_DBUS_SERVICE, @@ -416,3 +442,32 @@ nm_dispatcher_call_cancel (gconstpointer call) ((DispatchInfo *) call)->callback = NULL; } +static void +dispatcher_dir_changed (GFileMonitor *monitor) +{ + GDir *dir; + + /* Default to dispatching on any errors */ + do_dispatch = TRUE; + dir = g_dir_open (NMD_SCRIPT_DIR, 0, NULL); + if (dir) { + do_dispatch = !!g_dir_read_name (dir); + g_dir_close (dir); + } +} + +void +nm_dispatcher_init (void) +{ + GFile *file; + static GFileMonitor *monitor; + + file = g_file_new_for_path (NMD_SCRIPT_DIR); + monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, NULL, NULL); + if (monitor) { + g_signal_connect (monitor, "changed", G_CALLBACK (dispatcher_dir_changed), NULL); + dispatcher_dir_changed (monitor); + } + g_object_unref (file); +} + diff --git a/src/nm-dispatcher.h b/src/nm-dispatcher.h index 05a6c87514..179eb2f31e 100644 --- a/src/nm-dispatcher.h +++ b/src/nm-dispatcher.h @@ -61,4 +61,6 @@ gconstpointer nm_dispatcher_call_vpn (DispatcherAction action, void nm_dispatcher_call_cancel (gconstpointer call); +void nm_dispatcher_init (void); + #endif /* NM_DISPATCHER_H */ |