gboolean
nm_netlink_monitor_subscribe (NMNetlinkMonitor *self, int group, GError **error)
{
	NMNetlinkMonitorPrivate *priv;
	int subs, err;

	g_return_val_if_fail (NM_IS_NETLINK_MONITOR (self), FALSE);

	priv = NM_NETLINK_MONITOR_GET_PRIVATE (self);

	if (!priv->nlh_event) {
		if (!nm_netlink_monitor_open_connection (self, error))
			return FALSE;
	}

	subs = get_subs (self, group) + 1;
	if (subs == 1) {
		err = nl_socket_add_membership (priv->nlh_event, group);
		if (err < 0) {
			g_set_error (error, NM_NETLINK_MONITOR_ERROR,
			             NM_NETLINK_MONITOR_ERROR_NETLINK_JOIN_GROUP,
			             _("unable to join netlink group: %s"),
			             nl_geterror (err));
			return FALSE;
		}
	}

	/* Update # of subscriptions for this group */
	set_subs (self, group, subs);

	return TRUE;
}
NMNetlinkMonitor *
nm_netlink_monitor_get (void)
{
	static NMNetlinkMonitor *singleton = NULL;
	GError *error = NULL;

	if (!singleton) {
		singleton = (NMNetlinkMonitor *) g_object_new (NM_TYPE_NETLINK_MONITOR, NULL);
		g_return_val_if_fail (singleton != NULL, NULL);

		if (nm_netlink_monitor_open_connection (singleton, &error)) {
			nm_netlink_monitor_attach (singleton);
			/* Request initial status of cards */
			nm_netlink_monitor_request_status (singleton);
		} else {
			nm_log_warn (LOGD_HW, "Failed to connect to netlink: (%d) %s",
			             error ? error->code : 0,
			             (error && error->message) ? error->message : "(unknown)");
			g_clear_error (&error);

			g_object_unref (singleton);
			singleton = NULL;
		}
	} else
		g_object_ref (singleton);

	return singleton;
}
Beispiel #3
0
int
main (void)
{
	NmNetlinkMonitor *monitor;
	GMainLoop *loop;
	GError *error;

	g_type_init ();

	monitor = nm_netlink_monitor_new ();

	error = NULL;
	nm_netlink_monitor_open_connection (monitor, &error);

	if (error != NULL)
	{
		g_printerr ("could not open connection: %s\n",
			    error->message);
		g_error_free (error);
		return 1;
	}

	loop = g_main_loop_new (NULL, FALSE);

	g_signal_connect (G_OBJECT (monitor),
			  "interface-connected",
			  G_CALLBACK (device_added), NULL);

	g_signal_connect (G_OBJECT (monitor),
			  "interface-disconnected",
			  G_CALLBACK (device_removed), NULL);

	nm_netlink_monitor_attach (monitor, NULL);

	nm_netlink_monitor_request_status (monitor, &error);

	if (error != NULL)
	{
		g_printerr ("could not request status of interfaces: %s\n",
			    error->message);
		g_error_free (error);
		return 2;
	}

	g_main_loop_run (loop);

	return 0;
}