mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-07 22:28:13 +02:00
cli: split evaluation of activation state
The code used to determine the activation state is useful for other clients as well, let's split it into a dedicated function that can be shared.
This commit is contained in:
parent
802116db43
commit
aec559cea5
1 changed files with 59 additions and 41 deletions
|
|
@ -2089,17 +2089,20 @@ active_connection_state_reason_to_string (NMActiveConnectionStateReason reason)
|
||||||
g_return_val_if_reached (_("Invalid reason"));
|
g_return_val_if_reached (_("Invalid reason"));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static NMActiveConnectionState
|
||||||
check_activated (ActivateConnectionInfo *info)
|
get_effective_activation_state (NMActiveConnection *active,
|
||||||
|
NMDevice *device,
|
||||||
|
const char **reason)
|
||||||
{
|
{
|
||||||
NmCli *nmc = info->nmc;
|
NMActiveConnectionState ac_state;
|
||||||
NMDevice *device = info->device;
|
|
||||||
NMActiveConnection *active = info->active;
|
|
||||||
NMActiveConnectionStateReason ac_reason;
|
NMActiveConnectionStateReason ac_reason;
|
||||||
NMDeviceState dev_state = NM_DEVICE_STATE_UNKNOWN;
|
NMDeviceState dev_state = NM_DEVICE_STATE_UNKNOWN;
|
||||||
NMDeviceStateReason dev_reason = NM_DEVICE_STATE_REASON_UNKNOWN;
|
NMDeviceStateReason dev_reason = NM_DEVICE_STATE_REASON_UNKNOWN;
|
||||||
const char *reason;
|
|
||||||
|
|
||||||
|
g_return_val_if_fail (active, NM_ACTIVE_CONNECTION_STATE_UNKNOWN);
|
||||||
|
g_return_val_if_fail (reason, NM_ACTIVE_CONNECTION_STATE_UNKNOWN);
|
||||||
|
|
||||||
|
*reason = NULL;
|
||||||
ac_reason = nm_active_connection_get_state_reason (active);
|
ac_reason = nm_active_connection_get_state_reason (active);
|
||||||
|
|
||||||
if (device) {
|
if (device) {
|
||||||
|
|
@ -2107,18 +2110,9 @@ check_activated (ActivateConnectionInfo *info)
|
||||||
dev_reason = nm_device_get_state_reason (device);
|
dev_reason = nm_device_get_state_reason (device);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (nm_active_connection_get_state (active)) {
|
ac_state = nm_active_connection_get_state (active);
|
||||||
|
switch (ac_state) {
|
||||||
case NM_ACTIVE_CONNECTION_STATE_ACTIVATED:
|
|
||||||
if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
|
|
||||||
nmc_terminal_erase_line ();
|
|
||||||
g_print (_("Connection successfully activated (D-Bus active path: %s)\n"),
|
|
||||||
nm_object_get_path (NM_OBJECT (active)));
|
|
||||||
activate_connection_info_finish (info);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case NM_ACTIVE_CONNECTION_STATE_DEACTIVATED:
|
case NM_ACTIVE_CONNECTION_STATE_DEACTIVATED:
|
||||||
|
|
||||||
if ( !device
|
if ( !device
|
||||||
|| ac_reason != NM_ACTIVE_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED
|
|| ac_reason != NM_ACTIVE_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED
|
||||||
|| nm_device_get_active_connection (device) != active) {
|
|| nm_device_get_active_connection (device) != active) {
|
||||||
|
|
@ -2127,55 +2121,79 @@ check_activated (ActivateConnectionInfo *info)
|
||||||
* - or, @ac_reason is specific
|
* - or, @ac_reason is specific
|
||||||
* - or, @device no longer references the current @active
|
* - or, @device no longer references the current @active
|
||||||
* >> we complete with @ac_reason. */
|
* >> we complete with @ac_reason. */
|
||||||
reason = active_connection_state_reason_to_string (ac_reason);
|
*reason = active_connection_state_reason_to_string (ac_reason);
|
||||||
} else if ( dev_state <= NM_DEVICE_STATE_DISCONNECTED
|
} else if ( dev_state <= NM_DEVICE_STATE_DISCONNECTED
|
||||||
|| dev_state >= NM_DEVICE_STATE_FAILED) {
|
|| dev_state >= NM_DEVICE_STATE_FAILED) {
|
||||||
/* (2)
|
/* (2)
|
||||||
* - not (1)
|
* - not (1)
|
||||||
* - and, the device is no longer in an activated state,
|
* - and, the device is no longer in an activated state,
|
||||||
* >> we complete with @dev_reason. */
|
* >> we complete with @dev_reason. */
|
||||||
reason = nmc_device_reason_to_string (dev_reason);
|
*reason = nmc_device_reason_to_string (dev_reason);
|
||||||
} else {
|
} else {
|
||||||
/* (3)
|
/* (3)
|
||||||
* we wait for the device go disconnect. We will get a better
|
* we wait for the device go disconnect. We will get a better
|
||||||
* failure reason from the device (2). */
|
* failure reason from the device (2). */
|
||||||
reason = NULL;
|
return NM_ACTIVE_CONNECTION_STATE_UNKNOWN;
|
||||||
}
|
|
||||||
|
|
||||||
if (reason) {
|
|
||||||
g_string_printf (nmc->return_text, _("Error: Connection activation failed: %s"),
|
|
||||||
reason);
|
|
||||||
nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
|
|
||||||
activate_connection_info_finish (info);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NM_ACTIVE_CONNECTION_STATE_ACTIVATING:
|
case NM_ACTIVE_CONNECTION_STATE_ACTIVATING:
|
||||||
/* activating master connection does not automatically activate any slaves, so their
|
/* activating master connection does not automatically activate any slaves, so their
|
||||||
* active connection state will not progress beyond ACTIVATING state.
|
* active connection state will not progress beyond ACTIVATING state.
|
||||||
* Monitor the device instead. */
|
* Monitor the device instead. */
|
||||||
|
|
||||||
if (nmc->secret_agent) {
|
|
||||||
NMRemoteConnection *connection = nm_active_connection_get_connection (active);
|
|
||||||
|
|
||||||
nm_secret_agent_simple_enable (NM_SECRET_AGENT_SIMPLE (nmc->secret_agent),
|
|
||||||
nm_connection_get_path (NM_CONNECTION (connection)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( device
|
if ( device
|
||||||
&& ( NM_IS_DEVICE_BOND (device)
|
&& ( NM_IS_DEVICE_BOND (device)
|
||||||
|| NM_IS_DEVICE_TEAM (device)
|
|| NM_IS_DEVICE_TEAM (device)
|
||||||
|| NM_IS_DEVICE_BRIDGE (device))
|
|| NM_IS_DEVICE_BRIDGE (device))
|
||||||
&& dev_state >= NM_DEVICE_STATE_IP_CONFIG
|
&& dev_state >= NM_DEVICE_STATE_IP_CONFIG
|
||||||
&& dev_state <= NM_DEVICE_STATE_ACTIVATED) {
|
&& dev_state <= NM_DEVICE_STATE_ACTIVATED) {
|
||||||
if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
|
*reason = "master waiting for slaves";
|
||||||
nmc_terminal_erase_line ();
|
return NM_ACTIVE_CONNECTION_STATE_ACTIVATED;
|
||||||
g_print (_("Connection successfully activated (master waiting for slaves) (D-Bus active path: %s)\n"),
|
|
||||||
nm_object_get_path (NM_OBJECT (active)));
|
|
||||||
activate_connection_info_finish (info);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ac_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
check_activated (ActivateConnectionInfo *info)
|
||||||
|
{
|
||||||
|
NMActiveConnectionState ac_state;
|
||||||
|
NmCli *nmc = info->nmc;
|
||||||
|
const char *reason = NULL;
|
||||||
|
|
||||||
|
ac_state = get_effective_activation_state (info->active, info->device, &reason);
|
||||||
|
switch (ac_state) {
|
||||||
|
case NM_ACTIVE_CONNECTION_STATE_ACTIVATED:
|
||||||
|
if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
|
||||||
|
nmc_terminal_erase_line ();
|
||||||
|
if (reason) {
|
||||||
|
g_print (_("Connection successfully activated (%s) (D-Bus active path: %s)\n"),
|
||||||
|
reason,
|
||||||
|
nm_object_get_path (NM_OBJECT (info->active)));
|
||||||
|
} else {
|
||||||
|
g_print (_("Connection successfully activated (D-Bus active path: %s)\n"),
|
||||||
|
nm_object_get_path (NM_OBJECT (info->active)));
|
||||||
|
}
|
||||||
|
activate_connection_info_finish (info);
|
||||||
|
break;
|
||||||
|
case NM_ACTIVE_CONNECTION_STATE_DEACTIVATED:
|
||||||
|
nm_assert (reason);
|
||||||
|
g_string_printf (nmc->return_text, _("Error: Connection activation failed: %s"),
|
||||||
|
reason);
|
||||||
|
nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
|
||||||
|
activate_connection_info_finish (info);
|
||||||
|
break;
|
||||||
|
case NM_ACTIVE_CONNECTION_STATE_ACTIVATING:
|
||||||
|
if (nmc->secret_agent) {
|
||||||
|
NMRemoteConnection *connection = nm_active_connection_get_connection (info->active);
|
||||||
|
|
||||||
|
nm_secret_agent_simple_enable (NM_SECRET_AGENT_SIMPLE (nmc->secret_agent),
|
||||||
|
nm_connection_get_path (NM_CONNECTION (connection)));
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue