summary refs log tree commit diff
path: root/src/nm-checkpoint.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2018-05-11 22:08:45 +0200
committerMichael Biebl <biebl@debian.org>2018-05-11 22:08:45 +0200
commitee9c73a923909e23a649407be77e25235d769e25 (patch)
treee21c923621fa278e737da693df9eb60ea31a6067 /src/nm-checkpoint.c
parentf60117b41d5433be1b4a96d82cd11d0c3dce9b63 (diff)
New upstream version 1.10.8 upstream/1.10.8
Diffstat (limited to 'src/nm-checkpoint.c')
-rw-r--r--src/nm-checkpoint.c288
1 files changed, 96 insertions, 192 deletions
diff --git a/src/nm-checkpoint.c b/src/nm-checkpoint.c
index a17f7eda..bc57d449 100644
--- a/src/nm-checkpoint.c
+++ b/src/nm-checkpoint.c
@@ -25,7 +25,6 @@
 #include <string.h>
 
 #include "nm-active-connection.h"
-#include "nm-act-request.h"
 #include "nm-auth-subject.h"
 #include "nm-core-utils.h"
 #include "nm-dbus-interface.h"
@@ -35,6 +34,7 @@
 #include "settings/nm-settings-connection.h"
 #include "nm-simple-connection.h"
 #include "nm-utils.h"
+#include "introspection/org.freedesktop.NetworkManager.Checkpoint.h"
 
 /*****************************************************************************/
 
@@ -50,35 +50,36 @@ typedef struct {
 	NMActivationReason activation_reason;
 } DeviceCheckpoint;
 
-NM_GOBJECT_PROPERTIES_DEFINE (NMCheckpoint,
+NM_GOBJECT_PROPERTIES_DEFINE_BASE (
 	PROP_DEVICES,
 	PROP_CREATED,
 	PROP_ROLLBACK_TIMEOUT,
 );
 
-struct _NMCheckpointPrivate {
+typedef struct {
 	/* properties */
 	GHashTable *devices;
-	gint64 created_at_ms;
-	guint32 rollback_timeout_s;
-	guint timeout_id;
-	/* private members */
+	gint64 created;
+	guint32 rollback_timeout;
 	/* private members */
 	NMManager *manager;
+	gint64 rollback_ts;
 	NMCheckpointCreateFlags flags;
 	GHashTable *connection_uuids;
+} NMCheckpointPrivate;
 
-	NMCheckpointTimeoutCallback timeout_cb;
-	gpointer timeout_data;
+struct _NMCheckpoint {
+	NMExportedObject parent;
+	NMCheckpointPrivate _priv;
 };
 
 struct _NMCheckpointClass {
-	NMDBusObjectClass parent;
+	NMExportedObjectClass parent;
 };
 
-G_DEFINE_TYPE (NMCheckpoint, nm_checkpoint, NM_TYPE_DBUS_OBJECT)
+G_DEFINE_TYPE (NMCheckpoint, nm_checkpoint, NM_TYPE_EXPORTED_OBJECT)
 
-#define NM_CHECKPOINT_GET_PRIVATE(self) _NM_GET_PRIVATE_PTR (self, NMCheckpoint, NM_IS_CHECKPOINT)
+#define NM_CHECKPOINT_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMCheckpoint, NM_IS_CHECKPOINT)
 
 /*****************************************************************************/
 
@@ -102,53 +103,20 @@ G_DEFINE_TYPE (NMCheckpoint, nm_checkpoint, NM_TYPE_DBUS_OBJECT)
 
 /*****************************************************************************/
 
-void
-nm_checkpoint_log_destroy (NMCheckpoint *self)
+guint64
+nm_checkpoint_get_rollback_ts (NMCheckpoint *self)
 {
-	_LOGI ("destroy %s", nm_dbus_object_get_path (NM_DBUS_OBJECT (self)));
-}
+	g_return_val_if_fail (NM_IS_CHECKPOINT (self), 0);
 
-void
-nm_checkpoint_set_timeout_callback (NMCheckpoint *self,
-                                    NMCheckpointTimeoutCallback callback,
-                                    gpointer user_data)
-{
-	NMCheckpointPrivate *priv = NM_CHECKPOINT_GET_PRIVATE (self);
-
-	/* in glib world, we would have a GSignal for this. But as there
-	 * is only one subscriber, it's simpler to just set and unset(!)
-	 * the callback this way. */
-	priv->timeout_cb = callback;
-	priv->timeout_data = user_data;
+	return NM_CHECKPOINT_GET_PRIVATE (self)->rollback_ts;
 }
 
-NMDevice *
-nm_checkpoint_includes_devices (NMCheckpoint *self, NMDevice *const*devices, guint n_devices)
+gboolean
+nm_checkpoint_includes_device (NMCheckpoint *self, NMDevice *device)
 {
 	NMCheckpointPrivate *priv = NM_CHECKPOINT_GET_PRIVATE (self);
-	guint i;
 
-	for (i = 0; i < n_devices; i++) {
-		if (g_hash_table_contains (priv->devices, devices[i]))
-			return devices[i];
-	}
-	return NULL;
-}
-
-NMDevice *
-nm_checkpoint_includes_devices_of (NMCheckpoint *self, NMCheckpoint *cp_for_devices)
-{
-	NMCheckpointPrivate *priv = NM_CHECKPOINT_GET_PRIVATE (self);
-	NMCheckpointPrivate *priv2 = NM_CHECKPOINT_GET_PRIVATE (cp_for_devices);
-	GHashTableIter iter;
-	NMDevice *device;
-
-	g_hash_table_iter_init (&iter, priv2->devices);
-	while (g_hash_table_iter_next (&iter, (gpointer *) &device, NULL)) {
-		if (g_hash_table_contains (priv->devices, device))
-			return device;
-	}
-	return NULL;
+	return g_hash_table_contains (priv->devices, device);
 }
 
 static NMSettingsConnection *
@@ -216,7 +184,7 @@ nm_checkpoint_rollback (NMCheckpoint *self)
 	GError *local_error = NULL;
 	GVariantBuilder builder;
 
-	_LOGI ("rollback of %s", nm_dbus_object_get_path (NM_DBUS_OBJECT (self)));
+	_LOGI ("rollback of %s", nm_exported_object_get_path ((NMExportedObject *) self));
 	 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{su}"));
 
 	/* Start rolling-back each device */
@@ -338,7 +306,7 @@ activate:
 				                                     &local_error)) {
 					_LOGW ("rollback: reactivation of connection %s/%s failed: %s",
 					       nm_connection_get_id ((NMConnection *) connection),
-					       nm_connection_get_uuid ((NMConnection *) connection),
+					       nm_connection_get_uuid ((NMConnection *	) connection),
 					       local_error->message);
 					g_clear_error (&local_error);
 					result = NM_ROLLBACK_RESULT_ERR_FAILED;
@@ -383,19 +351,21 @@ next_dev:
 	}
 
 	if (NM_FLAGS_HAS (priv->flags, NM_CHECKPOINT_CREATE_FLAG_DISCONNECT_NEW_DEVICES)) {
-		const CList *tmp_lst;
+		const GSList *list;
 		NMDeviceState state;
-
-		nm_manager_for_each_device (priv->manager, device, tmp_lst) {
-			if (g_hash_table_contains (priv->devices, device))
-				continue;
-			state = nm_device_get_state (device);
-			if (   state > NM_DEVICE_STATE_DISCONNECTED
-			    && state < NM_DEVICE_STATE_DEACTIVATING) {
-				_LOGD ("rollback: disconnecting new device %s", nm_device_get_iface (device));
-				nm_device_state_changed (device,
-				                         NM_DEVICE_STATE_DEACTIVATING,
-				                         NM_DEVICE_STATE_REASON_USER_REQUESTED);
+		NMDevice *dev;
+
+		for (list = nm_manager_get_devices (priv->manager); list ; list = g_slist_next (list)) {
+			dev = list->data;
+			if (!g_hash_table_contains (priv->devices, dev)) {
+				state = nm_device_get_state (dev);
+				if (   state > NM_DEVICE_STATE_DISCONNECTED
+				    && state < NM_DEVICE_STATE_DEACTIVATING) {
+					_LOGD ("rollback: disconnecting new device %s", nm_device_get_iface (dev));
+					nm_device_state_changed (dev,
+					                         NM_DEVICE_STATE_DEACTIVATING,
+					                         NM_DEVICE_STATE_REASON_USER_REQUESTED);
+				}
 			}
 		}
 
@@ -405,7 +375,8 @@ next_dev:
 }
 
 static DeviceCheckpoint *
-device_checkpoint_create (NMDevice *device)
+device_checkpoint_create (NMDevice *device,
+                          GError **error)
 {
 	DeviceCheckpoint *dev_checkpoint;
 	NMConnection *applied_connection;
@@ -413,10 +384,7 @@ device_checkpoint_create (NMDevice *device)
 	const char *path;
 	NMActRequest *act_request;
 
-	nm_assert (NM_IS_DEVICE (device));
-	nm_assert (nm_device_is_real (device));
-
-	path = nm_dbus_object_get_path (NM_DBUS_OBJECT (device));
+	path = nm_exported_object_get_path (NM_EXPORTED_OBJECT (device));
 
 	dev_checkpoint = g_slice_new0 (DeviceCheckpoint);
 	dev_checkpoint->device = g_object_ref (device);
@@ -425,21 +393,25 @@ device_checkpoint_create (NMDevice *device)
 	dev_checkpoint->realized = nm_device_is_real (device);
 
 	if (nm_device_get_unmanaged_mask (device, NM_UNMANAGED_USER_EXPLICIT)) {
-		dev_checkpoint->unmanaged_explicit = !!nm_device_get_unmanaged_flags (device,
-		                                                                      NM_UNMANAGED_USER_EXPLICIT);
+		dev_checkpoint->unmanaged_explicit =
+			!!nm_device_get_unmanaged_flags (device, NM_UNMANAGED_USER_EXPLICIT);
 	} else
 		dev_checkpoint->unmanaged_explicit = NM_UNMAN_FLAG_OP_FORGET;
 
-	act_request = nm_device_get_act_request (device);
-	if (act_request) {
-		settings_connection = nm_act_request_get_settings_connection (act_request);
-		applied_connection = nm_act_request_get_applied_connection (act_request);
+	applied_connection = nm_device_get_applied_connection (device);
+	if (applied_connection) {
+		dev_checkpoint->applied_connection =
+			nm_simple_connection_new_clone (applied_connection);
 
-		dev_checkpoint->applied_connection = nm_simple_connection_new_clone (applied_connection);
+		settings_connection = nm_device_get_settings_connection (device);
+		g_return_val_if_fail (settings_connection, NULL);
 		dev_checkpoint->settings_connection =
-		    nm_simple_connection_new_clone (NM_CONNECTION (settings_connection));
+			nm_simple_connection_new_clone (NM_CONNECTION (settings_connection));
+
+		act_request = nm_device_get_act_request (device);
+		g_return_val_if_fail (act_request, NULL);
 		dev_checkpoint->ac_version_id =
-		    nm_active_connection_version_id_get (NM_ACTIVE_CONNECTION (act_request));
+			nm_active_connection_version_id_get (NM_ACTIVE_CONNECTION (act_request));
 		dev_checkpoint->activation_reason =
 		    nm_active_connection_get_activation_reason (NM_ACTIVE_CONNECTION (act_request));
 	}
@@ -460,57 +432,6 @@ device_checkpoint_destroy (gpointer data)
 	g_slice_free (DeviceCheckpoint, dev_checkpoint);
 }
 
-static gboolean
-_timeout_cb (gpointer user_data)
-{
-	NMCheckpoint *self = user_data;
-	NMCheckpointPrivate *priv = NM_CHECKPOINT_GET_PRIVATE (self);
-
-	priv->timeout_id = 0;
-
-	if (priv->timeout_cb)
-		priv->timeout_cb (self, priv->timeout_data);
-
-	/* beware, @self likely got destroyed! */
-	return G_SOURCE_REMOVE;
-}
-
-void
-nm_checkpoint_adjust_rollback_timeout (NMCheckpoint *self, guint32 add_timeout)
-{
-	guint32 rollback_timeout_s;
-	gint64 now_ms, add_timeout_ms, rollback_timeout_ms;
-
-	NMCheckpointPrivate *priv = NM_CHECKPOINT_GET_PRIVATE (self);
-
-	nm_clear_g_source (&priv->timeout_id);
-
-	if (add_timeout == 0)
-		rollback_timeout_s = 0;
-	else {
-		now_ms = nm_utils_get_monotonic_timestamp_ms ();
-		add_timeout_ms = ((gint64) add_timeout) * 1000;
-		rollback_timeout_ms = (now_ms - priv->created_at_ms) + add_timeout_ms;
-
-		/* round to nearest integer second. Since NM_CHECKPOINT_ROLLBACK_TIMEOUT is
-		 * in units seconds, it will be able to exactly express the timeout. */
-		rollback_timeout_s = NM_MIN ((rollback_timeout_ms + 500) / 1000, (gint64) G_MAXUINT32);
-
-		/* we expect the timeout to be positive, because add_timeout_ms is positive.
-		 * We cannot accept a zero, because it means "infinity". */
-		nm_assert (rollback_timeout_s > 0);
-
-		priv->timeout_id = g_timeout_add (NM_MIN (add_timeout_ms, (gint64) G_MAXUINT32),
-		                                  _timeout_cb,
-		                                  self);
-	}
-
-	if (rollback_timeout_s != priv->rollback_timeout_s) {
-		priv->rollback_timeout_s = rollback_timeout_s;
-		_notify (self, PROP_ROLLBACK_TIMEOUT);
-	}
-}
-
 /*****************************************************************************/
 
 static void
@@ -519,20 +440,22 @@ get_property (GObject *object, guint prop_id,
 {
 	NMCheckpoint *self = NM_CHECKPOINT (object);
 	NMCheckpointPrivate *priv = NM_CHECKPOINT_GET_PRIVATE (self);
+	gs_free_slist GSList *devices = NULL;
+	GHashTableIter iter;
+	NMDevice *device;
 
 	switch (prop_id) {
 	case PROP_DEVICES:
-		nm_dbus_utils_g_value_set_object_path_from_hash (value,
-		                                                 priv->devices,
-		                                                 FALSE);
+		g_hash_table_iter_init (&iter, priv->devices);
+		while (g_hash_table_iter_next (&iter, (gpointer *) &device, NULL))
+			devices = g_slist_append (devices, device);
+		nm_utils_g_value_set_object_path_array (value, devices, NULL, NULL);
 		break;
 	case PROP_CREATED:
-		g_value_set_int64 (value,
-		                   nm_utils_monotonic_timestamp_as_boottime (priv->created_at_ms,
-		                                                             NM_UTILS_NS_PER_MSEC));
+		g_value_set_int64 (value, priv->created);
 		break;
 	case PROP_ROLLBACK_TIMEOUT:
-		g_value_set_uint (value, priv->rollback_timeout_s);
+		g_value_set_uint (value, priv->rollback_timeout);
 		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -545,47 +468,47 @@ get_property (GObject *object, guint prop_id,
 static void
 nm_checkpoint_init (NMCheckpoint *self)
 {
-	NMCheckpointPrivate *priv;
-
-	priv = G_TYPE_INSTANCE_GET_PRIVATE (self, NM_TYPE_CHECKPOINT, NMCheckpointPrivate);
-
-	self->_priv = priv;
-
-	c_list_init (&self->checkpoints_lst);
+	NMCheckpointPrivate *priv = NM_CHECKPOINT_GET_PRIVATE (self);
 
-	priv->devices = g_hash_table_new_full (nm_direct_hash, NULL,
+	priv->devices = g_hash_table_new_full (g_direct_hash, g_direct_equal,
 	                                       NULL, device_checkpoint_destroy);
 }
 
 NMCheckpoint *
-nm_checkpoint_new (NMManager *manager, GPtrArray *devices, guint32 rollback_timeout_s,
-                   NMCheckpointCreateFlags flags)
+nm_checkpoint_new (NMManager *manager, GPtrArray *devices, guint32 rollback_timeout,
+                   NMCheckpointCreateFlags flags, GError **error)
 {
 	NMCheckpoint *self;
 	NMCheckpointPrivate *priv;
 	NMSettingsConnection *const *con;
-	gint64 rollback_timeout_ms;
+	DeviceCheckpoint *dev_checkpoint;
+	NMDevice *device;
 	guint i;
 
 	g_return_val_if_fail (manager, NULL);
 	g_return_val_if_fail (devices, NULL);
-	g_return_val_if_fail (devices->len > 0, NULL);
+	g_return_val_if_fail (!error || !*error, NULL);
+
+	if (!devices->len) {
+		g_set_error_literal (error,
+		                     NM_MANAGER_ERROR,
+		                     NM_MANAGER_ERROR_INVALID_ARGUMENTS,
+		                     "no device available");
+		return NULL;
+	}
 
 	self = g_object_new (NM_TYPE_CHECKPOINT, NULL);
 
 	priv = NM_CHECKPOINT_GET_PRIVATE (self);
 	priv->manager = manager;
-	priv->rollback_timeout_s = rollback_timeout_s;
-	priv->created_at_ms = nm_utils_get_monotonic_timestamp_ms ();
+	priv->created = nm_utils_monotonic_timestamp_as_boottime (nm_utils_get_monotonic_timestamp_ms (),
+	                                                          NM_UTILS_NS_PER_MSEC);
+	priv->rollback_timeout = rollback_timeout;
+	priv->rollback_ts = rollback_timeout ?
+	    (nm_utils_get_monotonic_timestamp_ms () + ((gint64) rollback_timeout * 1000)) :
+	    0;
 	priv->flags = flags;
 
-	if (rollback_timeout_s != 0) {
-		rollback_timeout_ms = ((gint64) rollback_timeout_s) * 1000;
-		priv->timeout_id = g_timeout_add (NM_MIN (rollback_timeout_ms, (gint64) G_MAXUINT32),
-		                                  _timeout_cb,
-		                                  self);
-	}
-
 	if (NM_FLAGS_HAS (flags, NM_CHECKPOINT_CREATE_FLAG_DELETE_NEW_CONNECTIONS)) {
 		priv->connection_uuids = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, NULL);
 		for (con = nm_settings_get_connections (nm_settings_get (), NULL); *con; con++) {
@@ -595,15 +518,13 @@ nm_checkpoint_new (NMManager *manager, GPtrArray *devices, guint32 rollback_time
 	}
 
 	for (i = 0; i < devices->len; i++) {
-		NMDevice *device = devices->pdata[i];
-
-		/* FIXME: as long as the check point instance exists, it won't let go
-		 *        of the device. That is a bug, for example, if you have a ethernet
-		 *        device that gets removed (rmmod), the checkpoint will reference
-		 *        a non-existing D-Bus path of a device. */
-		g_hash_table_insert (priv->devices,
-		                     device,
-		                     device_checkpoint_create (device));
+		device = (NMDevice *) devices->pdata[i];
+		dev_checkpoint = device_checkpoint_create (device, error);
+		if (!dev_checkpoint) {
+			g_object_unref (self);
+			return NULL;
+		}
+		g_hash_table_insert (priv->devices, device, dev_checkpoint);
 	}
 
 	return self;
@@ -615,41 +536,20 @@ dispose (GObject *object)
 	NMCheckpoint *self = NM_CHECKPOINT (object);
 	NMCheckpointPrivate *priv = NM_CHECKPOINT_GET_PRIVATE (self);
 
-	nm_assert (c_list_is_empty (&self->checkpoints_lst));
-
 	g_clear_pointer (&priv->devices, g_hash_table_unref);
 	g_clear_pointer (&priv->connection_uuids, g_hash_table_unref);
 
-	nm_clear_g_source (&priv->timeout_id);
-
 	G_OBJECT_CLASS (nm_checkpoint_parent_class)->dispose (object);
 }
 
-static const NMDBusInterfaceInfoExtended interface_info_checkpoint = {
-	.parent = NM_DEFINE_GDBUS_INTERFACE_INFO_INIT (
-		NM_DBUS_INTERFACE_CHECKPOINT,
-		.signals = NM_DEFINE_GDBUS_SIGNAL_INFOS (
-			&nm_signal_info_property_changed_legacy,
-		),
-		.properties = NM_DEFINE_GDBUS_PROPERTY_INFOS (
-			NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE_L ("Devices",         "ao", NM_CHECKPOINT_DEVICES),
-			NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE_L ("Created",         "x",  NM_CHECKPOINT_CREATED),
-			NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE_L ("RollbackTimeout", "u",  NM_CHECKPOINT_ROLLBACK_TIMEOUT),
-		),
-	),
-	.legacy_property_changed = TRUE,
-};
-
 static void
 nm_checkpoint_class_init (NMCheckpointClass *checkpoint_class)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (checkpoint_class);
-	NMDBusObjectClass *dbus_object_class = NM_DBUS_OBJECT_CLASS (checkpoint_class);
+	NMExportedObjectClass *exported_object_class = NM_EXPORTED_OBJECT_CLASS (checkpoint_class);
 
-	g_type_class_add_private (object_class, sizeof (NMCheckpointPrivate));
-
-	dbus_object_class->export_path = NM_DBUS_EXPORT_PATH_NUMBERED (NM_DBUS_PATH"/Checkpoint");
-	dbus_object_class->interface_infos = NM_DBUS_INTERFACE_INFOS (&interface_info_checkpoint);
+	exported_object_class->export_path = NM_EXPORT_PATH_NUMBERED (NM_DBUS_PATH"/Checkpoint");
+	exported_object_class->export_on_construction = FALSE;
 
 	object_class->dispose = dispose;
 	object_class->get_property = get_property;
@@ -673,4 +573,8 @@ nm_checkpoint_class_init (NMCheckpointClass *checkpoint_class)
 	                       G_PARAM_STATIC_STRINGS);
 
 	g_object_class_install_properties (object_class, _PROPERTY_ENUMS_LAST, obj_properties);
+
+	nm_exported_object_class_add_interface (NM_EXPORTED_OBJECT_CLASS (checkpoint_class),
+	                                        NMDBUS_TYPE_CHECKPOINT_SKELETON,
+	                                        NULL);
 }