cli: avoid passing full NmCli global variable to nm_cli_spawn_pager()

We should not use global variables, and we should minimize the state
that we pass around. Instead of requiring the full NmCli struct in
nm_cli_spawn_pager(), pass only the necessary data.

This reduces our use of global variables.
This commit is contained in:
Thomas Haller
2020-04-02 13:59:53 +02:00
parent 7627173c0e
commit dbf697c759
5 changed files with 20 additions and 12 deletions

View File

@@ -2093,7 +2093,7 @@ do_connections_show (NmCli *nmc, int argc, char **argv)
}
}
nm_cli_spawn_pager (nmc);
nm_cli_spawn_pager (&nmc->nmc_config, &nmc->pager_data);
items = con_show_get_items (nmc, active_only, show_active_fields, order);
g_ptr_array_add (items, NULL);

View File

@@ -556,7 +556,7 @@ print_permissions (void *user_data)
permissions[i] = GINT_TO_POINTER (nm_auth_permission_sorted[i]);
permissions[i] = NULL;
nm_cli_spawn_pager (nmc);
nm_cli_spawn_pager (&nmc->nmc_config, &nmc->pager_data);
if (!nmc_print (&nmc->nmc_config,
permissions,
@@ -1447,7 +1447,7 @@ do_overview (NmCli *nmc, int argc, char **argv)
/* Register polkit agent */
nmc_start_polkit_agent_start_try (nmc);
nm_cli_spawn_pager (nmc);
nm_cli_spawn_pager (&nmc->nmc_config, &nmc->pager_data);
/* The VPN connections don't have devices (yet?). */
p = nm_client_get_active_connections (nmc->client);

View File

@@ -911,11 +911,12 @@ signal_handler (gpointer user_data)
}
void
nm_cli_spawn_pager (NmCli *nmc)
nm_cli_spawn_pager (const NmcConfig *nmc_config,
NmcPagerData *pager_data)
{
if (nmc->pager_pid > 0)
if (pager_data->pid != 0)
return;
nmc->pager_pid = nmc_terminal_spawn_pager (&nmc->nmc_config);
pager_data->pid = nmc_terminal_spawn_pager (nmc_config);
}
static void
@@ -937,13 +938,14 @@ nmc_cleanup (NmCli *nmc)
nm_clear_g_free (&nmc->required_fields);
if (nmc->pager_pid > 0) {
if (nmc->pager_data.pid != 0) {
pid_t pid = nm_steal_int (&nmc->pager_data.pid);
fclose (stdout);
fclose (stderr);
do {
ret = waitpid (nmc->pager_pid, NULL, 0);
ret = waitpid (pid, NULL, 0);
} while (ret == -1 && errno == EINTR);
nmc->pager_pid = 0;
}
nm_clear_g_free (&nmc->palette_buffer);

View File

@@ -101,6 +101,10 @@ typedef struct _NmcConfig {
const char *palette[_NM_META_COLOR_NUM]; /* Color palette */
} NmcConfig;
typedef struct {
pid_t pid;
} NmcPagerData;
typedef struct _NmcOutputData {
GPtrArray *output_data; /* GPtrArray of arrays of NmcOutputField structs - accumulates data for output */
} NmcOutputData;
@@ -111,7 +115,8 @@ typedef struct _NmCli {
NMCResultCode return_value; /* Return code of nmcli */
GString *return_text; /* Reason text */
pid_t pager_pid; /* PID of a pager, if one was spawned */
NmcPagerData pager_data;
int timeout; /* Operation timeout */
@@ -150,7 +155,8 @@ void nmc_clear_sigint (void);
void nmc_set_sigquit_internal (void);
void nmc_exit (void);
void nm_cli_spawn_pager (NmCli *nmc);
void nm_cli_spawn_pager (const NmcConfig *nmc_config,
NmcPagerData *pager_data);
void nmc_empty_output_fields (NmcOutputData *output_data);

View File

@@ -1587,7 +1587,7 @@ print_required_fields (const NmcConfig *nmc_config,
gboolean field_names = of_flags & NMC_OF_FLAG_FIELD_NAMES;
gboolean section_prefix = of_flags & NMC_OF_FLAG_SECTION_PREFIX;
nm_cli_spawn_pager (&nm_cli);
nm_cli_spawn_pager (nmc_config, &nm_cli.pager_data);
/* --- Main header --- */
if ( nmc_config->print_output == NMC_PRINT_PRETTY