Commit d8a31794 authored by Thomas Haller's avatar Thomas Haller

connectivity: rework async connectivity check requests

An asynchronous request should either be cancellable or not keep
the target object alive. Preferably both.

Otherwise, it is impossible to do a controlled shutdown when terminating
NetworkManager. Currently, when NetworkManager is about to terminate,
it just quits the mainloop and essentially leaks everything. That is a
bug. If we ever want to fix that, every asynchronous request must be
cancellable in a controlled way (or it must not prevent objects from
getting disposed, where disposing the object automatically cancels the
callback).

Rework the asynchronous request for connectivity check to

- return a handle that can be used to cancel the operation.
  Cancelling is optional. The caller may choose to ignore the handle
  because the asynchronous operation does not keep the target object
  alive. That means, it is still possible to shutdown, by everybody
  giving up their reference to the target object. In which case the
  callback will be invoked during dispose() of the target object.

- also, the callback will always be invoked exactly once, and never
  synchronously from within the asynchronous start call. But during
  cancel(), the callback is invoked synchronously from within cancel().
  Note that it's only allowed to cancel an action at most once, and
  never after the callback is invoked (also not from within the callback
  itself).

- also, NMConnectivity already supports a fake handler, in case
  connectivity check is disabled via configuration. Hence, reuse
  the same code paths also when compiling without --enable-concheck.
  That means, instead of having #if WITH_CONCHECK at various callers,
  move them into NMConnectivity. The downside is, that if you build
  without concheck, there is a small overhead compared to before. The
  upside is, we reuse the same code paths when compiling with or without
  concheck.

- also, the patch synchronizes the connecitivty states. For example,
  previously `nmcli networking connectivity check` would schedule
  requests in parallel, and return the accumulated result of the individual
  requests.
  However, the global connectivity state of the manager might have have
  been the same as the answer to the explicit connecitivity check,
  because while the answer for the manual check is waiting for all
  pending checks to complete, the global connectivity state could
  already change. That is just wrong. There are not multiple global
  connectivity states at the same time, there is just one. A manual
  connectivity check should have the meaning of ensure that the global
  state is up to date, but it still should return the global
  connectivity state -- not the answers for several connectivity checks
  issued in parallel.
  This is related to commit b799de28
  (libnm: update property in the manager after connectivity check),
  which tries to address a similar problem client side.
  Similarly, each device has a connectivity state. While there might
  be several connectivity checks per device pending, whenever a check
  completes, it can update the per-device state (and return that device
  state as result), but the immediate answer of the individual check
  might not matter. This is especially the case, when a later request
  returns earlier and obsoletes all earlier requests. In that case,
  earlier requests return with the result of the currend devices
  connectivity state.

This patch cleans up the internal API and gives a better defined behavior
to the user (thus, the simple API which simplifies implementation for the
caller). However, the implementation of getting this API right and properly
handle cancel and destruction of the target object is more complicated and
complex. But this but is not just for the sake of a nicer API. This fixes
actual issues explained above.

Also, get rid of GAsyncResult to track information about the pending request.
Instead, allocate our own handle structure, which ends up to be nicer
because it's strongly typed and has exactly the properties that are
useful to track the request. Also, it gets rid of the awkward
_finish() API by passing the relevant arguments to the callback
directly.
parent 4417b8bf
......@@ -157,6 +157,15 @@ typedef struct {
that the original configuration didn't change. */
} AppliedConfig;
struct _NMDeviceConnectivityHandle {
CList concheck_lst;
NMDevice *self;
NMDeviceConnectivityCallback callback;
gpointer user_data;
NMConnectivityCheckHandle *c_handle;
guint64 seq;
};
/*****************************************************************************/
enum {
......@@ -531,9 +540,14 @@ typedef struct _NMDevicePrivate {
NMNetns *netns;
NMLldpListener *lldp_listener;
NMConnectivityState connectivity_state;
NMConnectivity *concheck_mgr;
gulong concheck_periodic_id;
guint64 concheck_seq;
NMConnectivityState connectivity_state;
CList concheck_lst_head;
guint check_delete_unrealized_id;
......@@ -728,6 +742,16 @@ nm_device_get_platform (NMDevice *self)
return nm_netns_get_platform (nm_device_get_netns (self));
}
static NMConnectivity *
concheck_get_mgr (NMDevice *self)
{
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
if (G_UNLIKELY (!priv->concheck_mgr))
priv->concheck_mgr = g_object_ref (nm_connectivity_get ());
return priv->concheck_mgr;
}
static NMIP4Config *
_ip4_config_new (NMDevice *self)
{
......@@ -1861,16 +1885,13 @@ nm_device_get_route_metric_default (NMDeviceType device_type)
static gboolean
default_route_metric_penalty_detect (NMDevice *self)
{
#if WITH_CONCHECK
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
/* currently we don't differentiate between IPv4 and IPv6 when detecting
* connectivity. */
if ( priv->connectivity_state != NM_CONNECTIVITY_FULL
&& nm_connectivity_check_enabled (nm_connectivity_get ())) {
&& nm_connectivity_check_enabled (concheck_get_mgr (self)))
return TRUE;
}
#endif
return FALSE;
}
......@@ -2165,171 +2186,227 @@ nm_device_get_physical_port_id (NMDevice *self)
/*****************************************************************************/
static void
update_connectivity_state (NMDevice *self, NMConnectivityState state)
concheck_update_state (NMDevice *self, NMConnectivityState state)
{
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
/* If the connectivity check is disabled, make an optimistic guess. */
if (state == NM_CONNECTIVITY_UNKNOWN) {
if (state == NM_CONNECTIVITY_ERROR)
return;
/* If the connectivity check is disabled and we obtain a fake
* result, make an optimistic guess. */
if (state == NM_CONNECTIVITY_FAKE) {
if (priv->state == NM_DEVICE_STATE_ACTIVATED) {
if (nm_device_get_best_default_route (self, AF_UNSPEC))
state = NM_CONNECTIVITY_FULL;
else
state = NM_CONNECTIVITY_LIMITED;
} else {
} else
state = NM_CONNECTIVITY_NONE;
}
}
if (priv->connectivity_state != state) {
#if WITH_CONCHECK
_LOGD (LOGD_CONCHECK, "state changed from %s to %s",
nm_connectivity_state_to_string (priv->connectivity_state),
nm_connectivity_state_to_string (state));
#endif
priv->connectivity_state = state;
_notify (self, PROP_CONNECTIVITY);
if (priv->connectivity_state == state)
return;
if ( priv->state == NM_DEVICE_STATE_ACTIVATED
&& !nm_device_sys_iface_state_is_external (self)) {
if ( nm_device_get_best_default_route (self, AF_INET)
&& !ip_config_merge_and_apply (self, AF_INET, TRUE))
_LOGW (LOGD_IP4, "Failed to update IPv4 route metric");
if ( nm_device_get_best_default_route (self, AF_INET6)
&& !ip_config_merge_and_apply (self, AF_INET6, TRUE))
_LOGW (LOGD_IP6, "Failed to update IPv6 route metric");
}
_LOGD (LOGD_CONCHECK, "state changed from %s to %s",
nm_connectivity_state_to_string (priv->connectivity_state),
nm_connectivity_state_to_string (state));
priv->connectivity_state = state;
_notify (self, PROP_CONNECTIVITY);
if ( priv->state == NM_DEVICE_STATE_ACTIVATED
&& !nm_device_sys_iface_state_is_external (self)) {
if ( nm_device_get_best_default_route (self, AF_INET)
&& !ip_config_merge_and_apply (self, AF_INET, TRUE))
_LOGW (LOGD_IP4, "Failed to update IPv4 route metric");
if ( nm_device_get_best_default_route (self, AF_INET6)
&& !ip_config_merge_and_apply (self, AF_INET6, TRUE))
_LOGW (LOGD_IP6, "Failed to update IPv6 route metric");
}
}
typedef struct {
NMDevice *self;
NMDeviceConnectivityCallback callback;
gpointer user_data;
guint64 seq;
} ConnectivityCheckData;
static void
concheck_periodic (NMConnectivity *connectivity, NMDevice *self)
{
nm_device_check_connectivity (self, NULL, NULL);
}
static void
concheck_done (ConnectivityCheckData *data)
concheck_periodic_update (NMDevice *self)
{
NMDevice *self = data->self;
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
/* The unsolicited connectivity checks don't hook a callback. */
if (data->callback)
data->callback (data->self, priv->connectivity_state, data->user_data);
g_object_unref (data->self);
g_slice_free (ConnectivityCheckData, data);
if ( priv->state == NM_DEVICE_STATE_ACTIVATED
&& nm_device_get_best_default_route (self, AF_UNSPEC)) {
if (!priv->concheck_periodic_id) {
priv->concheck_periodic_id = g_signal_connect (concheck_get_mgr (self),
NM_CONNECTIVITY_PERIODIC_CHECK,
G_CALLBACK (concheck_periodic), self);
nm_device_check_connectivity (self, NULL, NULL);
}
} else {
if (priv->concheck_periodic_id) {
/* The default route has gone off, trigger a final connectivity check. */
nm_clear_g_signal_handler (priv->concheck_mgr, &priv->concheck_periodic_id);
nm_device_check_connectivity (self, NULL, NULL);
}
}
}
#if WITH_CONCHECK
static void
concheck_cb (GObject *source_object, GAsyncResult *result, gpointer user_data)
concheck_handle_complete (NMDeviceConnectivityHandle *handle,
GError *error)
{
ConnectivityCheckData *data = user_data;
NMDevice *self = data->self;
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
NMConnectivity *connectivity = NM_CONNECTIVITY (source_object);
NMConnectivityState state;
GError *error = NULL;
/* The moment we invoke the callback, we unlink it. It signals
* that @handle is handled -- as far as the callee of callback
* is concerned. */
c_list_unlink (&handle->concheck_lst);
state = nm_connectivity_check_finish (connectivity, result, &error);
if (error) {
_LOGW (LOGD_DEVICE, "connectivity checking on '%s' failed: %s",
nm_device_get_iface (self), error->message);
g_error_free (error);
if (handle->c_handle)
nm_connectivity_check_cancel (g_steal_pointer (&handle->c_handle));
if (handle->callback) {
handle->callback (handle->self,
handle,
NM_DEVICE_GET_PRIVATE (handle->self)->connectivity_state,
error,
handle->user_data);
}
if (data->seq == priv->concheck_seq)
update_connectivity_state (data->self, state);
concheck_done (data);
g_slice_free (NMDeviceConnectivityHandle, handle);
}
#endif /* WITH_CONCHECK */
static gboolean
no_concheck (gpointer user_data)
static void
concheck_cb (NMConnectivity *connectivity,
NMConnectivityCheckHandle *c_handle,
NMConnectivityState state,
GError *error,
gpointer user_data)
{
ConnectivityCheckData *data = user_data;
gs_unref_object NMDevice *self = NULL;
NMDevicePrivate *priv;
NMDeviceConnectivityHandle *handle;
NMDeviceConnectivityHandle *other_handle;
gboolean handle_is_alive;
guint64 seq;
concheck_done (data);
return G_SOURCE_REMOVE;
if (nm_utils_error_is_cancelled (error, FALSE)) {
/* the only place where we nm_connectivity_check_cancel(@c_handle), is
* from inside concheck_handle_event(). This is a recursive call,
* nothing to do. */
return;
}
/* we keep NMConnectivity instance alive. It cannot be disposing. */
nm_assert (!nm_utils_error_is_cancelled (error, TRUE));
handle = user_data;
nm_assert (handle->c_handle == c_handle);
handle->c_handle = NULL;
/* keep @self alive, while we invoke callbacks. */
self = g_object_ref (handle->self);
priv = NM_DEVICE_GET_PRIVATE (self);
nm_assert (!handle || c_list_contains (&priv->concheck_lst_head, &handle->concheck_lst));
seq = handle->seq;
/* first update the new state, and emit signals. */
concheck_update_state (self, state);
handle_is_alive = FALSE;
/* we might have invoked callbacks during concheck_update_state(). The caller might have
* cancelled and thus destroyed @handle. We have to check whether handle is still alive,
* by searching it in the list of alive handles.
*
* Also, we might want to complete all pending callbacks that were started before
* @handle, as they are automatically obsoleted. */
check_handles:
c_list_for_each_entry (other_handle, &priv->concheck_lst_head, concheck_lst) {
if (other_handle->seq >= seq) {
/* it's not guaranteed that @handle is still in the list. It might already
* be canceled while invoking callbacks for a previous other_handle.
* If it is already cancelled, @handle is a dangling pointer.
*
* Since @seq is assigned uniquely and increasing, either @other_handle is
* @handle (and thus, handle is alive), or it isn't. */
if (other_handle == handle)
handle_is_alive = TRUE;
break;
}
nm_assert (other_handle != handle);
if (!NM_IN_SET (state, NM_CONNECTIVITY_ERROR)) {
/* we also want to complete handles that were started before the current
* @handle. Their response is out-dated. */
concheck_handle_complete (other_handle, NULL);
/* we invoked callbacks, other handles might be cancelled and removed from the list.
* Need to iterate the list from the start. */
goto check_handles;
}
}
if (!handle_is_alive) {
/* We didn't find @handle in the list of alive handles. Thus, the handles
* was cancelled while we were invoking events. Nothing to do, and don't
* touch the dangling pointer. */
return;
}
concheck_handle_complete (handle, NULL);
}
void
NMDeviceConnectivityHandle *
nm_device_check_connectivity (NMDevice *self,
NMDeviceConnectivityCallback callback,
gpointer user_data)
{
ConnectivityCheckData *data;
#if WITH_CONCHECK
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
#endif
static guint64 seq_counter = 0;
NMDevicePrivate *priv;
NMDeviceConnectivityHandle *handle;
data = g_slice_new0 (ConnectivityCheckData);
data->self = g_object_ref (self);
data->callback = callback;
data->user_data = user_data;
g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
#if WITH_CONCHECK
if (priv->concheck_periodic_id) {
data->seq = ++priv->concheck_seq;
priv = NM_DEVICE_GET_PRIVATE (self);
/* Kick off a real connectivity check. */
nm_connectivity_check_async (nm_connectivity_get (),
nm_device_get_ip_iface (self),
concheck_cb,
data);
return;
}
#endif
handle = g_slice_new0 (NMDeviceConnectivityHandle);
handle->seq = ++seq_counter;
handle->self = self;
handle->callback = callback;
handle->user_data = user_data;
c_list_link_tail (&priv->concheck_lst_head, &handle->concheck_lst);
/* Fake one. */
g_idle_add (no_concheck, data);
handle->c_handle = nm_connectivity_check_start (concheck_get_mgr (self),
nm_device_get_ip_iface (self),
concheck_cb,
handle);
return handle;
}
NMConnectivityState
nm_device_get_connectivity_state (NMDevice *self)
void
nm_device_check_connectivity_cancel (NMDeviceConnectivityHandle *handle)
{
g_return_val_if_fail (NM_IS_DEVICE (self), NM_CONNECTIVITY_UNKNOWN);
gs_free_error GError *cancelled_error = NULL;
return NM_DEVICE_GET_PRIVATE (self)->connectivity_state;
}
g_return_if_fail (handle);
g_return_if_fail (NM_IS_DEVICE (handle->self));
g_return_if_fail (!c_list_is_empty (&handle->concheck_lst));
#if WITH_CONCHECK
static void
concheck_periodic (NMConnectivity *connectivity, NMDevice *self)
{
nm_device_check_connectivity (self, NULL, NULL);
nm_utils_error_set_cancelled (&cancelled_error, FALSE, "NMDevice");
concheck_handle_complete (handle, cancelled_error);
}
#endif
static void
concheck_periodic_update (NMDevice *self)
NMConnectivityState
nm_device_get_connectivity_state (NMDevice *self)
{
#if WITH_CONCHECK
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
gboolean check_enable;
check_enable = (priv->state == NM_DEVICE_STATE_ACTIVATED)
&& nm_device_get_best_default_route (self, AF_UNSPEC);
if (check_enable && !priv->concheck_periodic_id) {
/* We just gained a default route. Enable periodic checking. */
priv->concheck_periodic_id = g_signal_connect (nm_connectivity_get (),
NM_CONNECTIVITY_PERIODIC_CHECK,
G_CALLBACK (concheck_periodic), self);
/* Also kick off a check right away. */
nm_device_check_connectivity (self, NULL, NULL);
} else if (!check_enable && priv->concheck_periodic_id) {
/* The default route has gone off, and so has connectivity. */
nm_clear_g_signal_handler (nm_connectivity_get (), &priv->concheck_periodic_id);
update_connectivity_state (self, NM_CONNECTIVITY_NONE);
}
#else
/* update_connectivity_state() figures out how to lie about
* connectivity state if the actual state is not really known. */
update_connectivity_state (self, NM_CONNECTIVITY_UNKNOWN);
#endif
g_return_val_if_fail (NM_IS_DEVICE (self), NM_CONNECTIVITY_UNKNOWN);
return NM_DEVICE_GET_PRIVATE (self)->connectivity_state;
}
/*****************************************************************************/
......@@ -14429,10 +14506,12 @@ nm_device_init (NMDevice *self)
self->_priv = priv;
c_list_init (&priv->concheck_lst_head);
c_list_init (&self->devices_lst);
c_list_init (&priv->slaves);
priv->connectivity_state = NM_CONNECTIVITY_UNKNOWN;
priv->netns = g_object_ref (NM_NETNS_GET);
priv->autoconnect_blocked_flags = DEFAULT_AUTOCONNECT
......@@ -14546,11 +14625,19 @@ dispose (GObject *object)
NMDevice *self = NM_DEVICE (object);
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
NMPlatform *platform;
NMDeviceConnectivityHandle *con_handle;
gs_free_error GError *cancelled_error = NULL;
_LOGD (LOGD_DEVICE, "disposing");
nm_assert (c_list_is_empty (&self->devices_lst));
while ((con_handle = c_list_first_entry (&priv->concheck_lst_head, NMDeviceConnectivityHandle, concheck_lst))) {
if (!cancelled_error)
nm_utils_error_set_cancelled (&cancelled_error, FALSE, "NMDevice");
concheck_handle_complete (con_handle, cancelled_error);
}
nm_clear_g_cancellable (&priv->deactivating_cancellable);
nm_device_assume_state_reset (self);
......@@ -14624,6 +14711,8 @@ dispose (GObject *object)
g_clear_object (&priv->lldp_listener);
}
nm_clear_g_signal_handler (priv->concheck_mgr, &priv->concheck_periodic_id);
G_OBJECT_CLASS (nm_device_parent_class)->dispose (object);
if (nm_clear_g_source (&priv->queued_state.id)) {
......@@ -14665,8 +14754,9 @@ finalize (GObject *object)
/* for testing, NMDeviceTest does not invoke NMDevice::constructed,
* and thus @settings might be unset. */
if (priv->settings)
g_object_unref (priv->settings);
nm_g_object_unref (priv->settings);
nm_g_object_unref (priv->concheck_mgr);
g_object_unref (priv->netns);
}
......
......@@ -775,12 +775,20 @@ gboolean nm_device_hw_addr_get_cloned (NMDevice *self,
gboolean *preserve,
GError **error);
typedef struct _NMDeviceConnectivityHandle NMDeviceConnectivityHandle;
typedef void (*NMDeviceConnectivityCallback) (NMDevice *self,
NMDeviceConnectivityHandle *handle,
NMConnectivityState state,
GError *error,
gpointer user_data);
void nm_device_check_connectivity (NMDevice *self,
NMDeviceConnectivityCallback callback,
gpointer user_data);
NMDeviceConnectivityHandle *nm_device_check_connectivity (NMDevice *self,
NMDeviceConnectivityCallback callback,
gpointer user_data);
void nm_device_check_connectivity_cancel (NMDeviceConnectivityHandle *handle);
NMConnectivityState nm_device_get_connectivity_state (NMDevice *self);
typedef struct _NMBtVTableNetworkServer NMBtVTableNetworkServer;
......
......@@ -402,9 +402,7 @@ main (int argc, char *argv[])
manager))
goto done;
#if WITH_CONCHECK
NM_UTILS_KEEP_ALIVE (manager, nm_connectivity_get (), "NMManager-depends-on-NMConnectivity");
#endif
nm_dispatcher_init ();
......
......@@ -30,34 +30,55 @@
#include <curl/curl.h>
#endif
#include "nm-utils/c-list.h"
#include "nm-config.h"
#include "NetworkManagerUtils.h"
#define HEADER_STATUS_ONLINE "X-NetworkManager-Status: online\r\n"
/*****************************************************************************/
NM_UTILS_LOOKUP_STR_DEFINE (nm_connectivity_state_to_string, NMConnectivityState,
NM_UTILS_LOOKUP_STR_DEFINE_STATIC (_state_to_string, int /*NMConnectivityState*/,
NM_UTILS_LOOKUP_DEFAULT_WARN ("???"),
NM_UTILS_LOOKUP_STR_ITEM (NM_CONNECTIVITY_UNKNOWN, "UNKNOWN"),
NM_UTILS_LOOKUP_STR_ITEM (NM_CONNECTIVITY_NONE, "NONE"),
NM_UTILS_LOOKUP_STR_ITEM (NM_CONNECTIVITY_LIMITED, "LIMITED"),
NM_UTILS_LOOKUP_STR_ITEM (NM_CONNECTIVITY_PORTAL, "PORTAL"),
NM_UTILS_LOOKUP_STR_ITEM (NM_CONNECTIVITY_FULL, "FULL"),
NM_UTILS_LOOKUP_STR_ITEM (NM_CONNECTIVITY_ERROR, "ERROR"),
NM_UTILS_LOOKUP_STR_ITEM (NM_CONNECTIVITY_FAKE, "FAKE"),
);
const char *
nm_connectivity_state_to_string (NMConnectivityState state)
{
return _state_to_string (state);
}
/*****************************************************************************/
struct _NMConnectivityCheckHandle {
CList handles_lst;
NMConnectivity *self;
NMConnectivityCheckCallback callback;
gpointer user_data;
char *ifspec;
#if WITH_CONCHECK
struct {
char *response;
CURL *curl_ehandle;
struct curl_slist *request_headers;
GString *recv_msg;
} concheck;
#endif
typedef struct {
GSimpleAsyncResult *simple;
char *response;
CURL *curl_ehandle;
size_t msg_size;
char *msg;
struct curl_slist *request_headers;
guint timeout_id;
char *ifspec;
} NMConnectivityCheckHandle;
};
enum {
PERIODIC_CHECK,
......@@ -68,14 +89,19 @@ enum {
static guint signals[LAST_SIGNAL] = { 0 };
typedef struct {
CList handles_lst_head;
char *uri;
char *response;
gboolean enabled;
guint interval;
NMConfig *config;
guint periodic_check_id;
CURLM *curl_mhandle;
guint curl_timer;
#if WITH_CONCHECK
struct {
CURLM *curl_mhandle;
guint curl_timer;
guint periodic_check_id;
} concheck;
#endif
} NMConnectivityPrivate;
struct _NMConnectivity {
......@@ -105,111 +131,172 @@ NM_DEFINE_SINGLETON_GETTER (NMConnectivity, nm_connectivity_get, NM_TYPE_CONNECT
\
if (nm_logging_enabled (__level, _NMLOG2_DOMAIN)) { \
_nm_log (__level, _NMLOG2_DOMAIN, 0, \
&cb_data->ifspec[3], NULL, \
"connectivity: (%s) " \
_NM_UTILS_MACRO_FIRST (__VA_ARGS__), \
&cb_data->ifspec[3] \
_NM_UTILS_MACRO_REST (__VA_ARGS__)); \
(cb_data->ifspec ? &cb_data->ifspec[3] : NULL), \
NULL, \
"connectivity: (%s) " \
_NM_UTILS_MACRO_FIRST (__VA_ARGS__), \
(cb_data->ifspec ? &cb_data->ifspec[3] : "") \
_NM_UTILS_MACRO_REST (__VA_ARGS__)); \
} \
} G_STMT_END
/*****************************************************************************/
static void
finish_cb_data (NMConnectivityCheckHandle *cb_data, NMConnectivityState new_state)
cb_data_invoke_callback (NMConnectivityCheckHandle *cb_data,
NMConnectivityState state,
GError *error,
const char *log_message)
{
/* Contrary to what cURL manual claim it is *not* safe to remove
* the easy handle "at any moment"; specifically not from the
* write function. Thus here we just dissociate the cb_data from
* the easy handle and the easy handle will be cleaned up when the
* message goes to CURLMSG_DONE in curl_check_connectivity(). */
curl_easy_setopt (cb_data->curl_ehandle, CURLOPT_PRIVATE, NULL);
g_simple_async_result_set_op_res_gssize (cb_data->simple, new_state);
g_simple_async_result_complete (cb_data->simple);
g_object_unref (cb_data->simple);
curl_slist_free_all (cb_data->request_headers);
g_free (cb_data->response);
g_free (cb_data->msg);
NMConnectivityCheckCallback callback;
nm_assert (cb_data);
nm_assert (NM_IS_CONNECTIVITY (cb_data->self));
callback = cb_data->callback;
if (!callback)
return;
cb_data->callback = NULL;
nm_assert (log_message);
_LOG2D ("check completed: %s; %s",
nm_connectivity_state_to_string (state),
log_message);
callback (cb_data->self,
cb_data,
state,
error,
cb_data->user_data);
}
static void
cb_data_free (NMConnectivityCheckHandle *cb_data,
NMConnectivityState state,
GError *error,
const char *log_message)
{
NMConnectivity *self;
nm_assert (cb_data);
self = cb_data->self;
nm_assert (NM_IS_CONNECTIVITY (self));
c_list_unlink (&cb_data->handles_lst);
#if WITH_CONCHECK
if (cb_data->concheck.curl_ehandle) {
NMConnectivityPrivate *priv;
/* Contrary to what cURL manual claim it is *not* safe to remove
* the easy handle "at any moment"; specifically not from the
* write function. Thus here we just dissociate the cb_data from
* the easy handle and the easy handle will be cleaned up when the
* message goes to CURLMSG_DONE in curl_check_connectivity(). */
curl_easy_setopt (cb_data->concheck.curl_ehandle, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt (cb_data->concheck.curl_ehandle, CURLOPT_WRITEDATA, NULL);
curl_easy_setopt (cb_data->concheck.curl_ehandle, CURLOPT_HEADERFUNCTION, NULL);
curl_easy_setopt (cb_data->concheck.curl_ehandle, CURLOPT_HEADERDATA, NULL);
curl_easy_setopt (cb_data->concheck.curl_ehandle, CURLOPT_PRIVATE, NULL);
curl_easy_setopt (cb_data->concheck.curl_ehandle, CURLOPT_HTTPHEADER, NULL);
priv = NM_CONNECTIVITY_GET_PRIVATE (self);
curl_multi_remove_handle (priv->concheck.curl_mhandle, cb_data->concheck.curl_ehandle);
curl_easy_cleanup (cb_data->concheck.curl_ehandle);
curl_slist_free_all (cb_data->concheck.request_headers);
}
#endif
nm_clear_g_source (&cb_data->timeout_id);
cb_data_invoke_callback (cb_data, state, error, log_message);
#if WITH_CONCHECK
g_free (cb_data->concheck.response);
if (cb_data->concheck.recv_msg)
g_string_free (cb_data->concheck.recv_msg, TRUE);
#endif
g_free (cb_data->ifspec);
g_source_remove (cb_data->timeout_id);
g_slice_free (NMConnectivityCheckHandle, cb_data);
}
/*****************************************************************************/
#if WITH_CONCHECK
static const char *
_check_handle_get_response (NMConnectivityCheckHandle *cb_data)
{
return cb_data->concheck.response ?: NM_CONFIG_DEFAULT_CONNECTIVITY_RESPONSE;
}
static void
curl_check_connectivity (CURLM *mhandle, CURLMcode ret)
curl_check_connectivity (CURLM *mhandle, int sockfd, int ev_bitmask)
{
NMConnectivityCheckHandle *cb_data;
CURLMsg *msg;
CURLcode eret;
CURL *easy_handle;
gint m_left;
long response_code;
CURLMcode ret;
int running_handles;
ret = curl_multi_socket_action (mhandle, sockfd, ev_bitmask, &running_handles);
if (ret != CURLM_OK)
_LOGW ("connectivity check failed");
_LOGE ("connectivity check failed: %d", ret);
while ((msg = curl_multi_info_read (mhandle, &m_left))) {
if (msg->msg != CURLMSG_DONE)