ppp: make ppp-manager cancellable via GCancellable

Previously nm_ppp_manager_stop() would return a handle which
makes it easy to cancel the operation.

However, sometimes, we may want to cancel an operation based on
an GCancellable. So, extend nm_ppp_manager_stop() to hook it
with a cancellable.

Essentially, move the code from nm-modem.c to nm-ppp-manager-call.c,
where it belongs and where the functionality gets available to every
component.
This commit is contained in:
Thomas Haller 2018-10-12 15:13:20 +02:00
parent 9b935fad9b
commit dd4968fa16
8 changed files with 49 additions and 43 deletions

View file

@ -523,7 +523,7 @@ adsl_cleanup (NMDeviceAdsl *self)
if (priv->ppp_manager) {
g_signal_handlers_disconnect_by_func (priv->ppp_manager, G_CALLBACK (ppp_state_changed), self);
g_signal_handlers_disconnect_by_func (priv->ppp_manager, G_CALLBACK (ppp_ip4_config), self);
nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL);
nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL, NULL);
g_clear_object (&priv->ppp_manager);
}

View file

@ -1347,7 +1347,7 @@ deactivate (NMDevice *device)
nm_clear_g_source (&priv->pppoe_wait_id);
if (priv->ppp_manager) {
nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL);
nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL, NULL);
g_clear_object (&priv->ppp_manager);
}

View file

@ -221,7 +221,7 @@ deactivate (NMDevice *device)
NMDevicePppPrivate *priv = NM_DEVICE_PPP_GET_PRIVATE (self);
if (priv->ppp_manager) {
nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL);
nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL, NULL);
g_clear_object (&priv->ppp_manager);
}
}

View file

@ -1126,7 +1126,7 @@ deactivate_cleanup (NMModem *self, NMDevice *device)
if (priv->ppp_manager) {
g_signal_handlers_disconnect_by_data (priv->ppp_manager, self);
nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL);
nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL, NULL);
g_clear_object (&priv->ppp_manager);
}
@ -1173,19 +1173,11 @@ typedef struct {
gpointer callback_user_data;
DeactivateContextStep step;
NMPPPManager *ppp_manager;
NMPPPManagerStopHandle *ppp_stop_handle;
gulong ppp_stop_cancellable_id;
} DeactivateContext;
static void
deactivate_context_complete (DeactivateContext *ctx, GError *error)
{
if (ctx->ppp_stop_handle)
nm_ppp_manager_stop_cancel (ctx->ppp_stop_handle);
nm_assert (!ctx->ppp_stop_handle);
nm_assert (ctx->ppp_stop_cancellable_id == 0);
if (ctx->callback)
ctx->callback (ctx->self, error, ctx->callback_user_data);
nm_g_object_unref (ctx->ppp_manager);
@ -1221,14 +1213,6 @@ ppp_manager_stop_ready (NMPPPManager *ppp_manager,
{
DeactivateContext *ctx = user_data;
nm_assert (ctx->ppp_stop_handle == handle);
ctx->ppp_stop_handle = NULL;
if (ctx->ppp_stop_cancellable_id) {
g_cancellable_disconnect (ctx->cancellable,
nm_steal_int (&ctx->ppp_stop_cancellable_id));
}
if (was_cancelled)
return;
@ -1236,15 +1220,6 @@ ppp_manager_stop_ready (NMPPPManager *ppp_manager,
deactivate_step (ctx);
}
static void
ppp_manager_stop_cancelled (GCancellable *cancellable,
gpointer user_data)
{
DeactivateContext *ctx = user_data;
nm_ppp_manager_stop_cancel (ctx->ppp_stop_handle);
}
static void
deactivate_step (DeactivateContext *ctx)
{
@ -1274,17 +1249,10 @@ deactivate_step (DeactivateContext *ctx)
case DEACTIVATE_CONTEXT_STEP_PPP_MANAGER_STOP:
/* If we have a PPP manager, stop it */
if (ctx->ppp_manager) {
nm_assert (!ctx->ppp_stop_handle);
if (ctx->cancellable) {
ctx->ppp_stop_cancellable_id = g_cancellable_connect (ctx->cancellable,
G_CALLBACK (ppp_manager_stop_cancelled),
ctx,
NULL);
}
ctx->ppp_stop_handle = nm_ppp_manager_stop (ctx->ppp_manager,
ppp_manager_stop_ready,
ctx);
return;
nm_ppp_manager_stop (ctx->ppp_manager,
ctx->cancellable,
ppp_manager_stop_ready,
ctx);
}
ctx->step++;
/* fall through */

View file

@ -126,12 +126,13 @@ nm_ppp_manager_start (NMPPPManager *self,
NMPPPManagerStopHandle *
nm_ppp_manager_stop (NMPPPManager *self,
GCancellable *cancellable,
NMPPPManagerStopCallback callback,
gpointer user_data)
{
g_return_val_if_fail (ppp_ops, NULL);
return ppp_ops->stop (self, callback, user_data);
return ppp_ops->stop (self, cancellable, callback, user_data);
}
void

View file

@ -40,6 +40,7 @@ gboolean nm_ppp_manager_start (NMPPPManager *self,
GError **error);
NMPPPManagerStopHandle *nm_ppp_manager_stop (NMPPPManager *self,
GCancellable *cancellable,
NMPPPManagerStopCallback callback,
gpointer user_data);

View file

@ -137,9 +137,12 @@ G_DEFINE_TYPE (NMPPPManager, nm_ppp_manager, NM_TYPE_DBUS_OBJECT)
static void _ppp_cleanup (NMPPPManager *self);
static NMPPPManagerStopHandle *_ppp_manager_stop (NMPPPManager *self,
GCancellable *cancellable,
NMPPPManagerStopCallback callback,
gpointer user_data);
static void _ppp_manager_stop_cancel (NMPPPManagerStopHandle *handle);
/*****************************************************************************/
static void
@ -791,7 +794,7 @@ pppd_timed_out (gpointer data)
NMPPPManager *self = NM_PPP_MANAGER (data);
_LOGW ("pppd timed out or didn't initialize our dbus module");
_ppp_manager_stop (self, NULL, NULL);
_ppp_manager_stop (self, NULL, NULL, NULL);
g_signal_emit (self, signals[STATE_CHANGED], 0, (guint) NM_PPP_STATUS_DEAD);
@ -1170,6 +1173,10 @@ struct _NMPPPManagerStopHandle {
* pppd process terminated. */
GObject *shutdown_waitobj;
GCancellable *cancellable;
gulong cancellable_id;
guint idle_id;
};
@ -1179,6 +1186,13 @@ _stop_handle_complete (NMPPPManagerStopHandle *handle, gboolean was_cancelled)
gs_unref_object NMPPPManager *self = NULL;
NMPPPManagerStopCallback callback;
if (handle->cancellable_id) {
g_cancellable_disconnect (handle->cancellable,
nm_steal_int (&handle->cancellable_id));
}
g_clear_object (&handle->cancellable);
self = g_steal_pointer (&handle->self);
if (!self)
return;
@ -1219,8 +1233,20 @@ _stop_idle_cb (gpointer user_data)
return G_SOURCE_REMOVE;
}
static void
_stop_cancelled_cb (GCancellable *cancellable,
gpointer user_data)
{
NMPPPManagerStopHandle *handle = user_data;
nm_clear_g_signal_handler (handle->cancellable,
&handle->cancellable_id);
_ppp_manager_stop_cancel (handle);
}
static NMPPPManagerStopHandle *
_ppp_manager_stop (NMPPPManager *self,
GCancellable *cancellable,
NMPPPManagerStopCallback callback,
gpointer user_data)
{
@ -1246,6 +1272,13 @@ _ppp_manager_stop (NMPPPManager *self,
handle->self = g_object_ref (self);
handle->callback = callback;
handle->user_data = user_data;
if (cancellable) {
handle->cancellable = g_object_ref (cancellable);
handle->cancellable_id = g_cancellable_connect (cancellable,
G_CALLBACK (_stop_cancelled_cb),
handle,
NULL);
}
if (!priv->pid) {
/* No PID. There is nothing to kill, however, invoke the callback in
@ -1272,6 +1305,8 @@ _ppp_manager_stop (NMPPPManager *self,
return handle;
}
/*****************************************************************************/
static void
_ppp_manager_stop_cancel (NMPPPManagerStopHandle *handle)
{
@ -1360,7 +1395,7 @@ dispose (GObject *object)
* still stop. */
g_warn_if_fail (!priv->pid);
g_warn_if_fail (!nm_dbus_object_is_exported (NM_DBUS_OBJECT (self)));
_ppp_manager_stop (self, NULL, NULL);
_ppp_manager_stop (self, NULL, NULL, NULL);
g_clear_object (&priv->act_req);

View file

@ -40,6 +40,7 @@ typedef const struct {
GError **err);
NMPPPManagerStopHandle *(*stop) (NMPPPManager *manager,
GCancellable *cancellable,
NMPPPManagerStopCallback callback,
gpointer user_data);