Files
NetworkManager/clients/cli/nmcli.h
Thomas Haller a1b25a47b0 cli: rework printing of nmcli connection for multiple active connections
The output of `nmcli connection show` contains also information about
whether the profile is currently active, for example the device and
the current (activation) state.

Even when a profile can be activated only once (without supporting
mutiple activations at the same time), there are moments when a
connection is activating and still deactivating on another device.
NetworkManager ensures in the case with single activations that
a profile cannot be in state "activated" multiple times. But that
doesn't mean, that one profile cannot have multiple active connection
which reference it. That was already handled wrongly before, because
`nmcli connection show` would only search the first matching
active-connection. That is, it would arbitrarily pick an active
connection in case there were multiple and only show activation
state about one.
Furthermore, we will soon also add the possibility, that a profile can be
active multiple times (at the same time). Especially then, we need to
extend the output format to show all the devices on which the profile is
currently active.

Rework printing the connection list to use nmc_print(), and fix various
issues.

- as discussed, a profile may have multiple active connections at each time.
  There are only two possibilities: if a profile is active multiple
  times, show a line for each activation, or otherwise, show the
  information about multiple activations combined in one line, e.g. by
  printing "DEVICE eth0,eth1". This patch, does the former.
  We will now print a line for each active connection, to show
  all the devices and activation states in multiple lines.
  Yes, this may result in the same profile being printed multiple times.
  That is a change in behavior, and inconvenient if you do something
  like

     for UUID in $(nmcli connection show | awk '{print$2}'); do ...

  However, above is anyway wrong because it assumes that there are no
  spaces in the connection name. The proper way to do this is like

     for UUID in $(nmcli -g UUID connection show); do ...

  In the latter case, whenever a user selects a subset of fields
  (--fields, --get) which don't print information about active connections,
  these multiple lines are combined. So, above still works as expected,
  never returning duplicate UUIDs.

- if a user has no permissions to see a connection, we previously
  would print "<invisible> $NAME". No longer do this but just print
  the ID was it is reported by the active-connection. If the goal
  of this was to prevent users from accidentally access the non-existing
  connection by $NAME, then this was a bad solution, because a script
  would instead try to access "<invisible> $NAME". This is now solved
  better by hiding the active connection if the user selects "-g NAME".

- the --order option now sorts according to how the fields are shown.
  For example, with --terse mode, it will evaluate type "802-11-wireless"
  but with pretty mode it will consider "wifi". This may change the
  ordering in which connections are shown. Also, for sorting the name,
  we use g_utf8_collate() because it's unicode.
2018-06-01 16:03:23 +02:00

179 lines
6.8 KiB
C

/* nmcli - command-line tool to control NetworkManager
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 2010 - 2018 Red Hat, Inc.
*/
#ifndef NMC_NMCLI_H
#define NMC_NMCLI_H
#include "nm-secret-agent-old.h"
#include "nm-meta-setting-desc.h"
struct _NMPolkitListener;
typedef char *(*NmcCompEntryFunc) (const char *, int);
/* nmcli exit codes */
typedef enum {
/* Indicates successful execution */
NMC_RESULT_SUCCESS = 0,
/* Unknown / unspecified error */
NMC_RESULT_ERROR_UNKNOWN = 1,
/* Wrong invocation of nmcli */
NMC_RESULT_ERROR_USER_INPUT = 2,
/* A timeout expired */
NMC_RESULT_ERROR_TIMEOUT_EXPIRED = 3,
/* Error in connection activation */
NMC_RESULT_ERROR_CON_ACTIVATION = 4,
/* Error in connection deactivation */
NMC_RESULT_ERROR_CON_DEACTIVATION = 5,
/* Error in device disconnect */
NMC_RESULT_ERROR_DEV_DISCONNECT = 6,
/* Error in connection deletion */
NMC_RESULT_ERROR_CON_DEL = 7,
/* NetworkManager is not running */
NMC_RESULT_ERROR_NM_NOT_RUNNING = 8,
/* No more used, keep to preserve API */
NMC_RESULT_ERROR_VERSIONS_MISMATCH = 9,
/* Connection/Device/AP not found */
NMC_RESULT_ERROR_NOT_FOUND = 10,
/* --complete-args signals a file name may follow */
NMC_RESULT_COMPLETE_FILE = 65,
} NMCResultCode;
typedef enum {
NMC_PRINT_TERSE = 0,
NMC_PRINT_NORMAL = 1,
NMC_PRINT_PRETTY = 2
} NMCPrintOutput;
static inline NMMetaAccessorGetType
nmc_print_output_to_accessor_get_type (NMCPrintOutput print_output)
{
return (print_output != NMC_PRINT_TERSE)
? NM_META_ACCESSOR_GET_TYPE_PRETTY
: NM_META_ACCESSOR_GET_TYPE_PARSABLE;
}
/* === Output fields === */
typedef enum {
NMC_OF_FLAG_FIELD_NAMES = 0x00000001, /* Print field names instead of values */
NMC_OF_FLAG_SECTION_PREFIX = 0x00000002, /* Use the first value as section prefix for the other field names - just in multiline */
NMC_OF_FLAG_MAIN_HEADER_ADD = 0x00000004, /* Print main header in addition to values/field names */
NMC_OF_FLAG_MAIN_HEADER_ONLY = 0x00000008, /* Print main header only */
} NmcOfFlags;
extern const NMMetaType nmc_meta_type_generic_info;
typedef struct _NmcOutputField NmcOutputField;
typedef struct _NmcMetaGenericInfo NmcMetaGenericInfo;
struct _NmcOutputField {
const NMMetaAbstractInfo *info;
int width; /* Width in screen columns */
void *value; /* Value of current field - char* or char** (NULL-terminated array) */
gboolean value_is_array; /* Whether value is char** instead of char* */
gboolean free_value; /* Whether to free the value */
NmcOfFlags flags; /* Flags - whether and how to print values/field names/headers */
NMMetaColor color; /* Use this color to print value */
};
typedef struct _NmcConfig {
NMCPrintOutput print_output; /* Output mode */
gboolean use_colors; /* Whether to use colors for output: option '--color' */
bool multiline_output; /* Multiline output instead of default tabular */
bool escape_values; /* Whether to escape ':' and '\' in terse tabular mode */
bool in_editor; /* Whether running the editor - nmcli con edit' */
bool show_secrets; /* Whether to display secrets (both input and output): option '--show-secrets' */
bool overview; /* Overview mode (hide default values) */
const char *palette[_NM_META_COLOR_NUM]; /* Color palette */
} NmcConfig;
typedef struct _NmcOutputData {
GPtrArray *output_data; /* GPtrArray of arrays of NmcOutputField structs - accumulates data for output */
} NmcOutputData;
/* NmCli - main structure */
typedef struct _NmCli {
NMClient *client; /* Pointer to NMClient of libnm */
NMCResultCode return_value; /* Return code of nmcli */
GString *return_text; /* Reason text */
pid_t pager_pid; /* PID of a pager, if one was spawned */
int timeout; /* Operation timeout */
NMSecretAgentOld *secret_agent; /* Secret agent */
GHashTable *pwds_hash; /* Hash table with passwords in passwd-file */
struct _NMPolkitListener *pk_listener; /* polkit agent listener */
int should_wait; /* Semaphore indicating whether nmcli should not end or not yet */
gboolean nowait_flag; /* '--nowait' option; used for passing to callbacks */
gboolean mode_specified; /* Whether tabular/multiline mode was specified via '--mode' option */
union {
const NmcConfig nmc_config;
NmcConfig nmc_config_mutable;
};
char *required_fields; /* Required fields in output: '--fields' option */
gboolean ask; /* Ask for missing parameters: option '--ask' */
gboolean complete; /* Autocomplete the command line */
gboolean editor_status_line; /* Whether to display status line in connection editor */
gboolean editor_save_confirmation; /* Whether to ask for confirmation on saving connections with 'autoconnect=yes' */
char *palette_buffer; /* Buffer with sequences for terminal-colors.d(5)-based coloring. */
} NmCli;
#define NMC_RETURN(nmc, rvalue) \
G_STMT_START { \
return ((nmc)->return_value = (rvalue)); \
} G_STMT_END
extern NmCli nm_cli;
/* Error quark for GError domain */
#define NMCLI_ERROR (nmcli_error_quark ())
GQuark nmcli_error_quark (void);
extern GMainLoop *loop;
gboolean nmc_seen_sigint (void);
void nmc_clear_sigint (void);
void nmc_set_sigquit_internal (void);
void nmc_exit (void);
void nmc_empty_output_fields (NmcOutputData *output_data);
#define NMC_OUTPUT_DATA_DEFINE_SCOPED(out) \
gs_unref_array GArray *out##_indices = NULL; \
nm_auto (nmc_empty_output_fields) NmcOutputData out = { \
.output_data = g_ptr_array_new_full (20, g_free), \
}
#endif /* NMC_NMCLI_H */