summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubomir Rintel <lkundrak@v3.sk>2016-08-31 20:52:48 +0200
committerLubomir Rintel <lkundrak@v3.sk>2016-08-31 22:54:48 +0200
commit33523fa48f7ad94564c0b0f72067e1e3b60c7565 (patch)
treec5d5d6c94cc724e2674dc3bd62260270901ca5e1
parent10bbc8224cb50ec0cc3ff23afd9fed15b2a09841 (diff)
downloadNetworkManager-lr/ac.tar.gz
cli: use nmc_do_cmd to get the client and check if the daemon is runninglr/ac
The makes use of asynchronous client initialization, making things a bit faster and reduces code duplication too.
-rw-r--r--clients/cli/agent.c17
-rw-r--r--clients/cli/connections.c40
-rw-r--r--clients/cli/devices.c48
-rw-r--r--clients/cli/general.c54
-rw-r--r--clients/cli/nmcli.c34
-rw-r--r--clients/cli/nmcli.h1
6 files changed, 54 insertions, 140 deletions
diff --git a/clients/cli/agent.c b/clients/cli/agent.c
index bc3e6c3aa0..06db06d7fd 100644
--- a/clients/cli/agent.c
+++ b/clients/cli/agent.c
@@ -205,24 +205,15 @@ do_agent_all (NmCli *nmc, int argc, char **argv)
}
static const NMCCommand agent_cmds[] = {
- { "secret", do_agent_secret, usage_agent_secret, FALSE },
- { "polkit", do_agent_polkit, usage_agent_polkit, FALSE },
- { "all", do_agent_all, usage_agent_all, FALSE },
- { NULL, do_agent_all, usage, FALSE },
+ { "secret", do_agent_secret, usage_agent_secret, TRUE, TRUE },
+ { "polkit", do_agent_polkit, usage_agent_polkit, TRUE, TRUE },
+ { "all", do_agent_all, usage_agent_all, TRUE, TRUE },
+ { NULL, do_agent_all, usage, TRUE, TRUE },
};
NMCResultCode
do_agent (NmCli *nmc, int argc, char **argv)
{
- /* Get NMClient object */
- nmc->get_client (nmc);
-
- /* Check whether NetworkManager is running */
- if (!nm_client_get_nm_running (nmc->client)) {
- g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
- return NMC_RESULT_ERROR_NM_NOT_RUNNING;
- }
-
nmc_do_cmd (nmc, agent_cmds, *argv, argc, argv);
return nmc->return_value;
diff --git a/clients/cli/connections.c b/clients/cli/connections.c
index 23157c5d64..39d4add627 100644
--- a/clients/cli/connections.c
+++ b/clients/cli/connections.c
@@ -8790,20 +8790,20 @@ nmcli_con_tab_completion (const char *text, int start, int end)
}
static const NMCCommand connection_cmds[] = {
- { "show", do_connections_show, usage_connection_show, FALSE },
- { "up", do_connection_up, usage_connection_up, FALSE },
- { "down", do_connection_down, usage_connection_down, FALSE },
- { "add", do_connection_add, usage_connection_add, FALSE },
- { "edit", do_connection_edit, usage_connection_edit, FALSE },
- { "delete", do_connection_delete, usage_connection_delete, FALSE },
- { "reload", do_connection_reload, usage_connection_reload, FALSE },
- { "load", do_connection_load, usage_connection_load, FALSE },
- { "modify", do_connection_modify, usage_connection_modify, FALSE },
- { "clone", do_connection_clone, usage_connection_clone, FALSE },
- { "import", do_connection_import, usage_connection_import, FALSE },
- { "export", do_connection_export, usage_connection_export, FALSE },
- { "monitor", do_connection_monitor, usage_connection_monitor, FALSE },
- { NULL, do_connections_show, usage, FALSE },
+ { "show", do_connections_show, usage_connection_show, TRUE, TRUE },
+ { "up", do_connection_up, usage_connection_up, TRUE, TRUE },
+ { "down", do_connection_down, usage_connection_down, TRUE, TRUE },
+ { "add", do_connection_add, usage_connection_add, TRUE, TRUE },
+ { "edit", do_connection_edit, usage_connection_edit, TRUE, TRUE },
+ { "delete", do_connection_delete, usage_connection_delete, TRUE, TRUE },
+ { "reload", do_connection_reload, usage_connection_reload, TRUE, TRUE },
+ { "load", do_connection_load, usage_connection_load, TRUE, TRUE },
+ { "modify", do_connection_modify, usage_connection_modify, TRUE, TRUE },
+ { "clone", do_connection_clone, usage_connection_clone, TRUE, TRUE },
+ { "import", do_connection_import, usage_connection_import, TRUE, TRUE },
+ { "export", do_connection_export, usage_connection_export, TRUE, TRUE },
+ { "monitor", do_connection_monitor, usage_connection_monitor, TRUE, TRUE },
+ { NULL, do_connections_show, usage, TRUE, TRUE },
};
/* Entry point function for connections-related commands: 'nmcli connection' */
@@ -8816,18 +8816,6 @@ do_connections (NmCli *nmc, int argc, char **argv)
/* Set completion function for 'nmcli con' */
rl_attempted_completion_function = (rl_completion_func_t *) nmcli_con_tab_completion;
- /* Get NMClient object early */
- nmc->get_client (nmc);
-
- /* Check whether NetworkManager is running */
- if (!nm_client_get_nm_running (nmc->client)) {
- if (!nmc->complete) {
- g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
- nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
- }
- return nmc->return_value;
- }
-
nmc_do_cmd (nmc, connection_cmds, *argv, argc, argv);
return nmc->return_value;
diff --git a/clients/cli/devices.c b/clients/cli/devices.c
index 36148578dd..78991376df 100644
--- a/clients/cli/devices.c
+++ b/clients/cli/devices.c
@@ -3598,11 +3598,11 @@ finish:
}
static NMCCommand device_wifi_cmds[] = {
- { "list", do_device_wifi_list, NULL, FALSE },
- { "connect", do_device_wifi_connect_network, NULL, FALSE },
- { "hotspot", do_device_wifi_hotspot, NULL, FALSE },
- { "rescan", do_device_wifi_rescan, NULL, FALSE },
- { NULL, do_device_wifi_list, NULL, FALSE },
+ { "list", do_device_wifi_list, NULL, TRUE, TRUE },
+ { "connect", do_device_wifi_connect_network, NULL, TRUE, TRUE },
+ { "hotspot", do_device_wifi_hotspot, NULL, TRUE, TRUE },
+ { "rescan", do_device_wifi_rescan, NULL, TRUE, TRUE },
+ { NULL, do_device_wifi_list, NULL, TRUE, TRUE },
};
static NMCResultCode
@@ -3773,8 +3773,8 @@ do_device_lldp_list (NmCli *nmc, int argc, char **argv)
}
static NMCCommand device_lldp_cmds[] = {
- { "list", do_device_lldp_list, NULL, FALSE },
- { NULL, do_device_lldp_list, NULL, FALSE },
+ { "list", do_device_lldp_list, NULL, TRUE, TRUE },
+ { NULL, do_device_lldp_list, NULL, TRUE, TRUE },
};
static NMCResultCode
@@ -3819,7 +3819,6 @@ gen_func_ifnames (const char *text, int state)
const char **ifnames;
char *ret;
- nm_cli.get_client (&nm_cli);
devices = nm_client_get_devices (nm_cli.client);
if (devices->len == 0)
return NULL;
@@ -3866,18 +3865,18 @@ nmcli_device_tab_completion (const char *text, int start, int end)
}
static const NMCCommand device_cmds[] = {
- { "status", do_devices_status, usage_device_status, FALSE },
- { "show", do_device_show, usage_device_show, FALSE },
- { "connect", do_device_connect, usage_device_connect, FALSE },
- { "reapply", do_device_reapply, usage_device_reapply, FALSE },
- { "disconnect", do_devices_disconnect, usage_device_disconnect, FALSE },
- { "delete", do_devices_delete, usage_device_delete, FALSE },
- { "set", do_device_set, usage_device_set, FALSE },
- { "monitor", do_devices_monitor, usage_device_monitor, FALSE },
- { "wifi", do_device_wifi, usage_device_wifi, FALSE },
- { "lldp", do_device_lldp, usage_device_lldp, FALSE },
- { "modify", do_device_modify, usage_device_modify, FALSE },
- { NULL, do_devices_status, usage, FALSE },
+ { "status", do_devices_status, usage_device_status, TRUE, TRUE },
+ { "show", do_device_show, usage_device_show, TRUE, TRUE },
+ { "connect", do_device_connect, usage_device_connect, TRUE, TRUE },
+ { "reapply", do_device_reapply, usage_device_reapply, TRUE, TRUE },
+ { "disconnect", do_devices_disconnect, usage_device_disconnect, TRUE, TRUE },
+ { "delete", do_devices_delete, usage_device_delete, TRUE, TRUE },
+ { "set", do_device_set, usage_device_set, TRUE, TRUE },
+ { "monitor", do_devices_monitor, usage_device_monitor, TRUE, TRUE },
+ { "wifi", do_device_wifi, usage_device_wifi, FALSE, FALSE },
+ { "lldp", do_device_lldp, usage_device_lldp, FALSE, FALSE },
+ { "modify", do_device_modify, usage_device_modify, TRUE, TRUE },
+ { NULL, do_devices_status, usage, TRUE, TRUE },
};
NMCResultCode
@@ -3888,15 +3887,6 @@ do_devices (NmCli *nmc, int argc, char **argv)
rl_attempted_completion_function = (rl_completion_func_t *) nmcli_device_tab_completion;
- /* Get NMClient object early */
- nmc->get_client (nmc);
-
- /* Check whether NetworkManager is running */
- if (!nm_client_get_nm_running (nmc->client)) {
- g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
- return NMC_RESULT_ERROR_NM_NOT_RUNNING;
- }
-
nmc_do_cmd (nmc, device_cmds, *argv, argc, argv);
return nmc->return_value;
diff --git a/clients/cli/general.c b/clients/cli/general.c
index d844f1fb19..cd1548baf0 100644
--- a/clients/cli/general.c
+++ b/clients/cli/general.c
@@ -340,14 +340,6 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl
return FALSE;
}
- nmc->get_client (nmc); /* create NMClient */
-
- if (!nm_client_get_nm_running (nmc->client)) {
- g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
- nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
- return FALSE;
- }
-
state = nm_client_get_state (nmc->client);
startup = nm_client_get_startup (nmc->client);
connectivity = nm_client_get_connectivity (nmc->client);
@@ -490,14 +482,6 @@ show_nm_permissions (NmCli *nmc)
return FALSE;
}
- nmc->get_client (nmc); /* create NMClient */
-
- if (!nm_client_get_nm_running (nmc->client)) {
- g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
- nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
- return FALSE;
- }
-
nmc->print_fields.header_name = _("NetworkManager permissions");
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (nmc->output_data, arr);
@@ -562,7 +546,6 @@ show_general_logging (NmCli *nmc)
return FALSE;
}
- nmc->get_client (nmc); /* create NMClient */
nm_client_get_logging (nmc->client, &level, &domains, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: %s."), error->message);
@@ -618,7 +601,6 @@ do_general_logging (NmCli *nmc, int argc, char **argv)
return error->code;
}
- nmc->get_client (nmc); /* create NMClient */
nm_client_set_logging (nmc->client, level, domains, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: failed to set logging: %s"),
@@ -656,7 +638,6 @@ do_general_hostname (NmCli *nmc, int argc, char **argv)
/* no arguments -> get hostname */
char *hostname = NULL;
- nmc->get_client (nmc); /* create NMClient */
g_object_get (nmc->client, NM_CLIENT_HOSTNAME, &hostname, NULL);
if (hostname)
g_print ("%s\n", hostname);
@@ -669,7 +650,6 @@ do_general_hostname (NmCli *nmc, int argc, char **argv)
g_print ("Warning: ignoring extra garbage after '%s' hostname\n", hostname);
nmc->should_wait++;
- nmc->get_client (nmc); /* create NMClient */
nm_client_save_hostname_async (nmc->client, hostname, NULL, save_hostname_cb, nmc);
}
@@ -678,11 +658,11 @@ do_general_hostname (NmCli *nmc, int argc, char **argv)
}
static const NMCCommand general_cmds[] = {
- { "status", do_general_status, usage_general_status, FALSE },
- { "hostname", do_general_hostname, usage_general_hostname, FALSE },
- { "permissions", do_general_permissions, usage_general_permissions, FALSE },
- { "logging", do_general_logging, usage_general_logging, FALSE },
- { NULL, do_general_status, usage_general, FALSE },
+ { "status", do_general_status, usage_general_status, TRUE, TRUE },
+ { "hostname", do_general_hostname, usage_general_hostname, TRUE, TRUE },
+ { "permissions", do_general_permissions, usage_general_permissions, TRUE, TRUE },
+ { "logging", do_general_logging, usage_general_logging, TRUE, TRUE },
+ { NULL, do_general_status, usage_general, TRUE, TRUE },
};
/*
@@ -788,7 +768,6 @@ do_networking (NmCli *nmc, int argc, char **argv)
} else if (matches (*argv, "check") == 0) {
GError *error = NULL;
- nmc->get_client (nmc); /* create NMClient */
nm_client_check_connectivity (nmc->client, NULL, &error);
if (error) {
g_string_printf (nmc->return_text, _("Error: %s."), error->message);
@@ -812,7 +791,6 @@ do_networking (NmCli *nmc, int argc, char **argv)
goto finish;
}
- nmc->get_client (nmc); /* create NMClient */
nm_client_networking_set_enabled (nmc->client, enable_flag, NULL);
} else {
if (nmc->complete)
@@ -854,7 +832,6 @@ do_radio_all (NmCli *nmc, int argc, char **argv)
if (!nmc_switch_parse_on_off (nmc, *(argv-1), *argv, &enable_flag))
return nmc->return_value;
- nmc->get_client (nmc); /* create NMClient */
nm_client_wireless_set_enabled (nmc->client, enable_flag);
nm_client_wimax_set_enabled (nmc->client, enable_flag);
nm_client_wwan_set_enabled (nmc->client, enable_flag);
@@ -883,7 +860,6 @@ do_radio_wifi (NmCli *nmc, int argc, char **argv)
if (!nmc_switch_parse_on_off (nmc, *(argv-1), *argv, &enable_flag))
return nmc->return_value;
- nmc->get_client (nmc); /* create NMClient */
nm_client_wireless_set_enabled (nmc->client, enable_flag);
}
@@ -910,7 +886,6 @@ do_radio_wwan (NmCli *nmc, int argc, char **argv)
if (!nmc_switch_parse_on_off (nmc, *(argv-1), *argv, &enable_flag))
return nmc->return_value;
- nmc->get_client (nmc); /* create NMClient */
nm_client_wwan_set_enabled (nmc->client, enable_flag);
}
@@ -918,10 +893,10 @@ do_radio_wwan (NmCli *nmc, int argc, char **argv)
}
static const NMCCommand radio_cmds[] = {
- { "all", do_radio_all, usage_radio_all, FALSE },
- { "wifi", do_radio_wifi, usage_radio_wifi, FALSE },
- { "wwan", do_radio_wwan, usage_radio_wwan, FALSE },
- { NULL, do_radio_all, usage_radio, FALSE },
+ { "all", do_radio_all, usage_radio_all, TRUE, TRUE },
+ { "wifi", do_radio_wifi, usage_radio_wifi, TRUE, TRUE },
+ { "wwan", do_radio_wwan, usage_radio_wwan, TRUE, TRUE },
+ { NULL, do_radio_all, usage_radio, TRUE, TRUE },
};
/*
@@ -1157,15 +1132,6 @@ do_overview (NmCli *nmc, int argc, char **argv)
/* Register polkit agent */
nmc_start_polkit_agent_start_try (nmc);
- /* Get NMClient object early */
- nmc->get_client (nmc);
-
- /* Check whether NetworkManager is running */
- if (!nm_client_get_nm_running (nmc->client)) {
- g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
- return NMC_RESULT_ERROR_NM_NOT_RUNNING;
- }
-
/* The VPN connections don't have devices (yet?). */
p = nm_client_get_active_connections (nmc->client);
for (i = 0; i < p->len; i++) {
@@ -1242,8 +1208,6 @@ do_monitor (NmCli *nmc, int argc, char **argv)
return nmc->return_value;
}
- nmc->get_client (nmc); /* create NMClient */
-
if (!nm_client_get_nm_running (nmc->client)) {
char *str;
diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c
index 62b74d2698..2f957be788 100644
--- a/clients/cli/nmcli.c
+++ b/clients/cli/nmcli.c
@@ -206,14 +206,14 @@ usage (void)
}
static const NMCCommand nmcli_cmds[] = {
- { "general", do_general, NULL, FALSE },
- { "monitor", do_monitor, NULL, FALSE },
- { "networking", do_networking, NULL, FALSE },
- { "radio", do_radio, NULL, FALSE },
- { "connection", do_connections, NULL, FALSE },
- { "device", do_devices, NULL, FALSE },
- { "agent", do_agent, NULL, FALSE },
- { NULL, do_overview, usage, FALSE },
+ { "general", do_general, NULL, FALSE, FALSE },
+ { "monitor", do_monitor, NULL, TRUE, FALSE },
+ { "networking", do_networking, NULL, FALSE, FALSE },
+ { "radio", do_radio, NULL, FALSE, FALSE },
+ { "connection", do_connections, NULL, FALSE, FALSE },
+ { "device", do_devices, NULL, FALSE, FALSE },
+ { "agent", do_agent, NULL, FALSE, FALSE },
+ { NULL, do_overview, usage, TRUE, TRUE },
};
static gboolean
@@ -589,29 +589,11 @@ nmc_value_transforms_register (void)
nmc_convert_bytes_to_string);
}
-static NMClient *
-nmc_get_client (NmCli *nmc)
-{
- GError *error = NULL;
-
- if (!nmc->client) {
- nmc->client = nm_client_new (NULL, &error);
- if (!nmc->client) {
- g_critical (_("Error: Could not create NMClient object: %s."), error->message);
- g_clear_error (&error);
- exit (NMC_RESULT_ERROR_UNKNOWN);
- }
- }
-
- return nmc->client;
-}
-
/* Initialize NmCli structure - set default values */
static void
nmc_init (NmCli *nmc)
{
nmc->client = NULL;
- nmc->get_client = &nmc_get_client;
nmc->return_value = NMC_RESULT_SUCCESS;
nmc->return_text = g_string_new (_("Success"));
diff --git a/clients/cli/nmcli.h b/clients/cli/nmcli.h
index 9f15f51626..d9c5393c02 100644
--- a/clients/cli/nmcli.h
+++ b/clients/cli/nmcli.h
@@ -132,7 +132,6 @@ typedef enum {
/* NmCli - main structure */
typedef struct _NmCli {
NMClient *client; /* Pointer to NMClient of libnm */
- NMClient *(*get_client) (struct _NmCli *nmc); /* Pointer to function for creating NMClient */
NMCResultCode return_value; /* Return code of nmcli */
GString *return_text; /* Reason text */