core: add dispatcher callbacks and simplify dispatcher function prototypes

Remove unused args for the non-VPN cases to cut down on the NULL NULL NULL
stuff since we're also adding two more arguments.  Add the ability for
callers to give a callback that should be called when the dispatcher is
done.
This commit is contained in:
Dan Williams
2012-06-05 11:20:11 -05:00
parent 0201d6da87
commit 15ca7cd56c
5 changed files with 125 additions and 39 deletions

View File

@@ -1596,7 +1596,7 @@ dhcp4_lease_change (NMDevice *device, NMIP4Config *config)
} }
/* Notify dispatcher scripts of new DHCP4 config */ /* Notify dispatcher scripts of new DHCP4 config */
nm_utils_call_dispatcher ("dhcp4-change", connection, device, NULL, NULL, NULL); nm_dispatcher_call (DISPATCHER_ACTION_DHCP4_CHANGE, connection, device, NULL, NULL);
} }
static void static void
@@ -1971,7 +1971,7 @@ dhcp6_lease_change (NMDevice *device)
nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, reason); nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, reason);
} else { } else {
/* Notify dispatcher scripts of new DHCPv6 config */ /* Notify dispatcher scripts of new DHCPv6 config */
nm_utils_call_dispatcher ("dhcp6-change", connection, device, NULL, NULL, NULL); nm_dispatcher_call (DISPATCHER_ACTION_DHCP6_CHANGE, connection, device, NULL, NULL);
} }
} }
@@ -4631,7 +4631,7 @@ nm_device_state_changed (NMDevice *device,
case NM_DEVICE_STATE_ACTIVATED: case NM_DEVICE_STATE_ACTIVATED:
nm_log_info (LOGD_DEVICE, "Activation (%s) successful, device activated.", nm_log_info (LOGD_DEVICE, "Activation (%s) successful, device activated.",
nm_device_get_iface (device)); nm_device_get_iface (device));
nm_utils_call_dispatcher ("up", nm_act_request_get_connection (req), device, NULL, NULL, NULL); nm_dispatcher_call (DISPATCHER_ACTION_UP, nm_act_request_get_connection (req), device, NULL, NULL);
break; break;
case NM_DEVICE_STATE_FAILED: case NM_DEVICE_STATE_FAILED:
connection = nm_act_request_get_connection (req); connection = nm_act_request_get_connection (req);
@@ -4650,7 +4650,7 @@ nm_device_state_changed (NMDevice *device,
} }
if (old_state == NM_DEVICE_STATE_ACTIVATED) if (old_state == NM_DEVICE_STATE_ACTIVATED)
nm_utils_call_dispatcher ("down", nm_act_request_get_connection (req), device, NULL, NULL, NULL); nm_dispatcher_call (DISPATCHER_ACTION_DOWN, nm_act_request_get_connection (req), device, NULL, NULL);
/* Dispose of the cached activation request */ /* Dispose of the cached activation request */
if (req) if (req)

View File

@@ -129,6 +129,8 @@ fill_vpn_props (NMIP4Config *ip4_config,
} }
typedef struct { typedef struct {
DispatcherFunc callback;
gpointer user_data;
NMDBusManager *dbus_mgr; NMDBusManager *dbus_mgr;
} DispatchInfo; } DispatchInfo;
@@ -160,6 +162,7 @@ dispatch_result_to_string (DispatchResult result)
static void static void
dispatcher_done_cb (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data) dispatcher_done_cb (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data)
{ {
DispatchInfo *info = user_data;
GError *error = NULL; GError *error = NULL;
GPtrArray *results = NULL; GPtrArray *results = NULL;
guint i; guint i;
@@ -202,17 +205,50 @@ dispatcher_done_cb (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data)
nm_log_warn (LOGD_CORE, "Dispatcher failed: (%d) %s", error->code, error->message); nm_log_warn (LOGD_CORE, "Dispatcher failed: (%d) %s", error->code, error->message);
} }
if (info->callback)
info->callback (call, info->user_data);
g_clear_error (&error); g_clear_error (&error);
g_object_unref (proxy); g_object_unref (proxy);
} }
void static const char *
nm_utils_call_dispatcher (const char *action, action_to_string (DispatcherAction action)
NMConnection *connection, {
NMDevice *device, switch (action) {
const char *vpn_iface, case DISPATCHER_ACTION_HOSTNAME:
NMIP4Config *vpn_ip4_config, return "hostname";
NMIP6Config *vpn_ip6_config) case DISPATCHER_ACTION_UP:
return "up";
case DISPATCHER_ACTION_PRE_DOWN:
return "pre-down";
case DISPATCHER_ACTION_DOWN:
return "down";
case DISPATCHER_ACTION_VPN_UP:
return "vpn-up";
case DISPATCHER_ACTION_VPN_PRE_DOWN:
return "vpn-pre-down";
case DISPATCHER_ACTION_VPN_DOWN:
return "vpn-down";
case DISPATCHER_ACTION_DHCP4_CHANGE:
return "dhcp4-change";
case DISPATCHER_ACTION_DHCP6_CHANGE:
return "dhcp6-change";
default:
break;
}
g_assert_not_reached ();
}
static gpointer
_dispatcher_call (DispatcherAction action,
NMConnection *connection,
NMDevice *device,
const char *vpn_iface,
NMIP4Config *vpn_ip4_config,
NMIP6Config *vpn_ip6_config,
DispatcherFunc callback,
gpointer user_data)
{ {
NMDBusManager *dbus_mgr; NMDBusManager *dbus_mgr;
DBusGProxy *proxy; DBusGProxy *proxy;
@@ -229,14 +265,12 @@ nm_utils_call_dispatcher (const char *action,
DBusGProxyCall *call; DBusGProxyCall *call;
DispatchInfo *info; DispatchInfo *info;
g_return_if_fail (action != NULL);
/* All actions except 'hostname' require a device */ /* All actions except 'hostname' require a device */
if (strcmp (action, "hostname") != 0) if (action != DISPATCHER_ACTION_HOSTNAME)
g_return_if_fail (NM_IS_DEVICE (device)); g_return_val_if_fail (NM_IS_DEVICE (device), NULL);
/* VPN actions require at least an IPv4 config (for now) */ /* VPN actions require at least an IPv4 config (for now) */
if (strcmp (action, "vpn-up") == 0) if (action == DISPATCHER_ACTION_VPN_UP)
g_return_if_fail (vpn_ip4_config != NULL); g_return_val_if_fail (vpn_ip4_config != NULL, NULL);
dbus_mgr = nm_dbus_manager_get (); dbus_mgr = nm_dbus_manager_get ();
g_connection = nm_dbus_manager_get_connection (dbus_mgr); g_connection = nm_dbus_manager_get_connection (dbus_mgr);
@@ -247,7 +281,7 @@ nm_utils_call_dispatcher (const char *action,
if (!proxy) { if (!proxy) {
nm_log_err (LOGD_CORE, "could not get dispatcher proxy!"); nm_log_err (LOGD_CORE, "could not get dispatcher proxy!");
g_object_unref (dbus_mgr); g_object_unref (dbus_mgr);
return; return NULL;
} }
if (connection) { if (connection) {
@@ -271,7 +305,7 @@ nm_utils_call_dispatcher (const char *action,
vpn_ip6_props = value_hash_create (); vpn_ip6_props = value_hash_create ();
/* hostname actions only send the hostname */ /* hostname actions only send the hostname */
if (strcmp (action, "hostname") != 0) { if (action != DISPATCHER_ACTION_HOSTNAME) {
fill_device_props (device, fill_device_props (device,
device_props, device_props,
device_ip4_props, device_ip4_props,
@@ -283,6 +317,8 @@ nm_utils_call_dispatcher (const char *action,
} }
info = g_malloc0 (sizeof (*info)); info = g_malloc0 (sizeof (*info));
info->callback = callback;
info->user_data = user_data;
info->dbus_mgr = dbus_mgr; info->dbus_mgr = dbus_mgr;
/* Send the action to the dispatcher */ /* Send the action to the dispatcher */
@@ -291,7 +327,7 @@ nm_utils_call_dispatcher (const char *action,
info, info,
(GDestroyNotify) dispatcher_info_free, (GDestroyNotify) dispatcher_info_free,
15000, 15000,
G_TYPE_STRING, action, G_TYPE_STRING, action_to_string (action),
DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, connection_hash, DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, connection_hash,
DBUS_TYPE_G_MAP_OF_VARIANT, connection_props, DBUS_TYPE_G_MAP_OF_VARIANT, connection_props,
DBUS_TYPE_G_MAP_OF_VARIANT, device_props, DBUS_TYPE_G_MAP_OF_VARIANT, device_props,
@@ -312,6 +348,30 @@ nm_utils_call_dispatcher (const char *action,
g_hash_table_destroy (device_dhcp6_props); g_hash_table_destroy (device_dhcp6_props);
g_hash_table_destroy (vpn_ip4_props); g_hash_table_destroy (vpn_ip4_props);
g_hash_table_destroy (vpn_ip6_props); g_hash_table_destroy (vpn_ip6_props);
return call;
} }
gpointer
nm_dispatcher_call (DispatcherAction action,
NMConnection *connection,
NMDevice *device,
DispatcherFunc callback,
gpointer user_data)
{
return _dispatcher_call (action, connection, device, NULL, NULL, NULL, callback, user_data);
}
gpointer
nm_dispatcher_call_vpn (DispatcherAction action,
NMConnection *connection,
NMDevice *device,
const char *vpn_iface,
NMIP4Config *vpn_ip4_config,
NMIP6Config *vpn_ip6_config,
DispatcherFunc callback,
gpointer user_data)
{
return _dispatcher_call (action, connection, device, vpn_iface, vpn_ip4_config, vpn_ip6_config, callback, user_data);
}

View File

@@ -30,11 +30,33 @@
#include "nm-ip6-config.h" #include "nm-ip6-config.h"
#include "nm-connection.h" #include "nm-connection.h"
void nm_utils_call_dispatcher (const char *action, typedef enum {
NMConnection *connection, DISPATCHER_ACTION_HOSTNAME,
NMDevice *device, DISPATCHER_ACTION_UP,
const char *vpn_iface, DISPATCHER_ACTION_PRE_DOWN,
NMIP4Config *vpn_ip4_config, DISPATCHER_ACTION_DOWN,
NMIP6Config *vpn_ip6_config); DISPATCHER_ACTION_VPN_UP,
DISPATCHER_ACTION_VPN_PRE_DOWN,
DISPATCHER_ACTION_VPN_DOWN,
DISPATCHER_ACTION_DHCP4_CHANGE,
DISPATCHER_ACTION_DHCP6_CHANGE
} DispatcherAction;
typedef void (*DispatcherFunc) (gpointer call, gpointer user_data);
gpointer nm_dispatcher_call (DispatcherAction action,
NMConnection *connection,
NMDevice *device,
DispatcherFunc callback,
gpointer user_data);
gpointer nm_dispatcher_call_vpn (DispatcherAction action,
NMConnection *connection,
NMDevice *device,
const char *vpn_iface,
NMIP4Config *vpn_ip4_config,
NMIP6Config *vpn_ip6_config,
DispatcherFunc callback,
gpointer user_data);
#endif /* NM_DISPATCHER_H */ #endif /* NM_DISPATCHER_H */

View File

@@ -260,7 +260,7 @@ _set_hostname (NMPolicy *policy,
g_object_unref (dns_mgr); g_object_unref (dns_mgr);
if (nm_policy_set_system_hostname (policy->cur_hostname, msg)) if (nm_policy_set_system_hostname (policy->cur_hostname, msg))
nm_utils_call_dispatcher ("hostname", NULL, NULL, NULL, NULL, NULL); nm_dispatcher_call (DISPATCHER_ACTION_HOSTNAME, NULL, NULL, NULL, NULL);
} }
static void static void

View File

@@ -243,23 +243,27 @@ nm_vpn_connection_set_vpn_state (NMVPNConnection *connection,
nm_connection_clear_secrets (priv->connection); nm_connection_clear_secrets (priv->connection);
/* Let dispatcher scripts know we're up and running */ /* Let dispatcher scripts know we're up and running */
nm_utils_call_dispatcher ("vpn-up", nm_dispatcher_call_vpn (DISPATCHER_ACTION_VPN_UP,
priv->connection, priv->connection,
priv->parent_dev, priv->parent_dev,
priv->ip_iface, priv->ip_iface,
priv->ip4_config, priv->ip4_config,
priv->ip6_config); priv->ip6_config,
NULL,
NULL);
break; break;
case NM_VPN_CONNECTION_STATE_FAILED: case NM_VPN_CONNECTION_STATE_FAILED:
case NM_VPN_CONNECTION_STATE_DISCONNECTED: case NM_VPN_CONNECTION_STATE_DISCONNECTED:
if (old_vpn_state == NM_VPN_CONNECTION_STATE_ACTIVATED) { if (old_vpn_state == NM_VPN_CONNECTION_STATE_ACTIVATED) {
/* Let dispatcher scripts know we're about to go down */ /* Let dispatcher scripts know we're about to go down */
nm_utils_call_dispatcher ("vpn-down", nm_dispatcher_call_vpn (DISPATCHER_ACTION_VPN_DOWN,
priv->connection, priv->connection,
priv->parent_dev, priv->parent_dev,
priv->ip_iface, priv->ip_iface,
NULL, NULL,
NULL); NULL,
NULL,
NULL);
} }
/* Tear down and clean up the connection */ /* Tear down and clean up the connection */