nmcli.c 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* nmcli - command-line tool to control NetworkManager
 *
 * Jiri Klimes <jklimes@redhat.com>
 *
 * 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.
 *
19
 * (C) Copyright 2010 - 2014 Red Hat, Inc.
20 21 22 23 24 25 26 27 28
 */

/* Generated configuration file */
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
29
#include <pthread.h>
30
#include <locale.h>
31 32
#include <readline/readline.h>
#include <readline/history.h>
33 34 35 36 37 38

#include <glib.h>
#include <glib/gi18n.h>

#include "nmcli.h"
#include "utils.h"
39
#include "common.h"
40 41 42 43
#include "connections.h"
#include "devices.h"
#include "network-manager.h"

44 45 46 47 48
#if defined(NM_DIST_VERSION)
# define NMCLI_VERSION NM_DIST_VERSION
#else
# define NMCLI_VERSION VERSION
#endif
49

50 51 52 53
/* Global NmCli object */
// FIXME: Currently, we pass NmCli over in most APIs, but we might refactor
// that and use the global variable directly instead.
NmCli nm_cli;
54 55 56 57 58 59 60 61 62

typedef struct {
	NmCli *nmc;
	int argc;
	char **argv;
} ArgsInfo;

/* --- Global variables --- */
GMainLoop *loop = NULL;
63
static sigset_t signal_set;
64 65


66 67 68 69 70 71 72 73 74 75 76 77
/* Get an error quark for use with GError */
GQuark
nmcli_error_quark (void)
{
	static GQuark error_quark = 0;

	if (G_UNLIKELY (error_quark == 0))
		error_quark = g_quark_from_static_string ("nmcli-error-quark");

	return error_quark;
}

78 79 80 81
static void
usage (const char *prog_name)
{
	fprintf (stderr,
82 83
	         _("Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
	         "\n"
84
	         "OPTIONS\n"
85 86
	         "  -t[erse]                                   terse output\n"
	         "  -p[retty]                                  pretty output\n"
87
	         "  -m[ode] tabular|multiline                  output mode\n"
88 89
	         "  -f[ields] <field1,field2,...>|all|common   specify fields to output\n"
	         "  -e[scape] yes|no                           escape columns separators in values\n"
90
	         "  -n[ocheck]                                 don't check nmcli and NetworkManager versions\n"
91
	         "  -a[sk]                                     ask for missing parameters\n"
92
	         "  -w[ait] <seconds>                          set timeout waiting for finishing operations\n"
93
	         "  -v[ersion]                                 show program version\n"
94 95
	         "  -h[elp]                                    print this help\n"
	         "\n"
96
	         "OBJECT\n"
97
	         "  g[eneral]       NetworkManager's general status and operations\n"
98 99
	         "  n[etworking]    overall networking control\n"
	         "  r[adio]         NetworkManager radio switches\n"
100 101 102
	         "  c[onnection]    NetworkManager's connections\n"
	         "  d[evice]        devices managed by NetworkManager\n"
	         "\n"),
103 104 105 106 107 108 109 110 111 112 113 114 115 116
	          prog_name);
}

static NMCResultCode 
do_help (NmCli *nmc, int argc, char **argv)
{
	usage ("nmcli");
	return NMC_RESULT_SUCCESS;
}

static const struct cmd {
	const char *cmd;
	NMCResultCode (*func) (NmCli *nmc, int argc, char **argv);
} nmcli_cmds[] = {
117
	{ "general",    do_general },
118 119
	{ "networking", do_networking },
	{ "radio",      do_radio },
120 121
	{ "connection", do_connections },
	{ "device",     do_devices },
122 123 124 125 126 127 128 129 130 131 132 133 134 135
	{ "help",       do_help },
	{ 0 }
};

static NMCResultCode
do_cmd (NmCli *nmc, const char *argv0, int argc, char **argv)
{
	const struct cmd *c;

	for (c = nmcli_cmds; c->cmd; ++c) {
		if (matches (argv0, c->cmd) == 0)
			return c->func (nmc, argc-1, argv+1);
	}

136 137
	g_string_printf (nmc->return_text, _("Error: Object '%s' is unknown, try 'nmcli help'."), argv0);
	nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	return nmc->return_value;
}

static NMCResultCode
parse_command_line (NmCli *nmc, int argc, char **argv)
{
	char *base;

	base = strrchr (argv[0], '/');
	if (base == NULL)
		base = argv[0];
	else
		base++;

	/* parse options */
	while (argc > 1) {
		char *opt = argv[1];
		/* '--' ends options */
		if (strcmp (opt, "--") == 0) {
			argc--; argv++;
			break;
		}
		if (opt[0] != '-')
			break;
		if (opt[1] == '-')
			opt++;
		if (matches (opt, "-terse") == 0) {
165
			if (nmc->print_output == NMC_PRINT_TERSE) {
166 167
				g_string_printf (nmc->return_text, _("Error: Option '--terse' is specified the second time."));
				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
168 169 170
				return nmc->return_value;
			}
			else if (nmc->print_output == NMC_PRINT_PRETTY) {
171 172
				g_string_printf (nmc->return_text, _("Error: Option '--terse' is mutually exclusive with '--pretty'."));
				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
173 174 175 176
				return nmc->return_value;
			}
			else
				nmc->print_output = NMC_PRINT_TERSE;
177
		} else if (matches (opt, "-pretty") == 0) {
178
			if (nmc->print_output == NMC_PRINT_PRETTY) {
179 180
				g_string_printf (nmc->return_text, _("Error: Option '--pretty' is specified the second time."));
				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
181 182 183
				return nmc->return_value;
			}
			else if (nmc->print_output == NMC_PRINT_TERSE) {
184 185
				g_string_printf (nmc->return_text, _("Error: Option '--pretty' is mutually exclusive with '--terse'."));
				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
186 187 188 189
				return nmc->return_value;
			}
			else
				nmc->print_output = NMC_PRINT_PRETTY;
190 191 192 193 194 195 196 197
		} else if (matches (opt, "-mode") == 0) {
			nmc->mode_specified = TRUE;
			next_arg (&argc, &argv);
			if (argc <= 1) {
		 		g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
				return nmc->return_value;
			}
198
			if (matches (argv[1], "tabular") == 0)
199
				nmc->multiline_output = FALSE;
200
			else if (matches (argv[1], "multiline") == 0)
201 202 203 204 205 206
				nmc->multiline_output = TRUE;
			else {
		 		g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), argv[1], opt);
				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
				return nmc->return_value;
			}
207 208 209 210
		} else if (matches (opt, "-escape") == 0) {
			next_arg (&argc, &argv);
			if (argc <= 1) {
		 		g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
211
				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
212 213
				return nmc->return_value;
			}
214
			if (matches (argv[1], "yes") == 0)
215
				nmc->escape_values = TRUE;
216
			else if (matches (argv[1], "no") == 0)
217 218 219
				nmc->escape_values = FALSE;
			else {
		 		g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), argv[1], opt);
220
				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
221 222 223 224 225 226
				return nmc->return_value;
			}
		} else if (matches (opt, "-fields") == 0) {
			next_arg (&argc, &argv);
			if (argc <= 1) {
		 		g_string_printf (nmc->return_text, _("Error: fields for '%s' options are missing."), opt);
227
				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
228 229 230
				return nmc->return_value;
			}
			nmc->required_fields = g_strdup (argv[1]);
231 232
		} else if (matches (opt, "-nocheck") == 0) {
			nmc->nocheck_ver = TRUE;
233 234
		} else if (matches (opt, "-ask") == 0) {
			nmc->ask = TRUE;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
		} else if (matches (opt, "-wait") == 0) {
			unsigned long timeout;
			next_arg (&argc, &argv);
			if (argc <= 1) {
		 		g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
				return nmc->return_value;
			}
			if (!nmc_string_to_uint (argv[1], TRUE, 0, G_MAXINT, &timeout)) {
		 		g_string_printf (nmc->return_text, _("Error: '%s' is not a valid timeout for '%s' option."),
				                 argv[1], opt);
				nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
				return nmc->return_value;
			}
			nmc->timeout = (int) timeout;
250 251 252 253 254 255 256
		} else if (matches (opt, "-version") == 0) {
			printf (_("nmcli tool, version %s\n"), NMCLI_VERSION);
			return NMC_RESULT_SUCCESS;
		} else if (matches (opt, "-help") == 0) {
			usage (base);
			return NMC_RESULT_SUCCESS;
		} else {
257 258
			g_string_printf (nmc->return_text, _("Error: Option '%s' is unknown, try 'nmcli -help'."), opt);
			nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
259 260 261 262 263 264 265 266 267 268 269 270 271
			return nmc->return_value;
		}
		argc--;
		argv++;
	}

	if (argc > 1)
		return do_cmd (nmc, argv[1], argc-1, argv+1);

	usage (base);
	return nmc->return_value;
}

272 273
static gboolean nmcli_sigint = FALSE;
static pthread_mutex_t sigint_mutex = PTHREAD_MUTEX_INITIALIZER;
274
static gboolean nmcli_sigquit_internal = FALSE;
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

gboolean
nmc_seen_sigint (void)
{
	gboolean sigint;

	pthread_mutex_lock (&sigint_mutex);
	sigint = nmcli_sigint;
	pthread_mutex_unlock (&sigint_mutex);
	return sigint;
}

void
nmc_clear_sigint (void)
{
	pthread_mutex_lock (&sigint_mutex);
	nmcli_sigint = FALSE;
	pthread_mutex_unlock (&sigint_mutex);
}

295 296 297 298 299 300
void
nmc_set_sigquit_internal (void)
{
	nmcli_sigquit_internal = TRUE;
}

301 302 303 304 305 306 307 308 309 310 311
static int
event_hook_for_readline (void)
{
	/* Make readline() exit on SIGINT */
	if (nmc_seen_sigint ()) {
		rl_echo_signal_char (SIGINT);
		rl_stuff_char ('\n');
	}
	return 0;
}

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
void *signal_handling_thread (void *arg);
/*
 * Thread function waiting for signals and processing them.
 * Wait for signals in signal set. The semantics of sigwait() require that all
 * threads (including the thread calling sigwait()) have the signal masked, for
 * reliable operation. Otherwise, a signal that arrives while this thread is
 * not blocked in sigwait() might be delivered to another thread.
 */
void *
signal_handling_thread (void *arg) {
	int signo;

	while (1) {
		sigwait (&signal_set, &signo);

		switch (signo) {
		case SIGINT:
329 330 331 332 333 334 335 336
			if (nmc_get_in_readline ()) {
				/* Don't quit when in readline, only signal we received SIGINT */
				pthread_mutex_lock (&sigint_mutex);
				nmcli_sigint = TRUE;
				pthread_mutex_unlock (&sigint_mutex);
			} else {
				/* We can quit nmcli */
				nmc_cleanup_readline ();
337 338
				printf (_("\nError: nmcli terminated by signal %s (%d)\n"),
				        strsignal (signo), signo);
339 340 341
				exit (1);
			}
			break;
342 343
		case SIGQUIT:
		case SIGTERM:
344
			nmc_cleanup_readline ();
345 346 347
			if (!nmcli_sigquit_internal)
				printf (_("\nError: nmcli terminated by signal %s (%d)\n"),
				        strsignal (signo), signo);
348 349 350 351 352
			exit (1);
			break;
		default:
			break;
		}
353
	}
354
	return NULL;
355 356
}

357 358 359 360 361 362 363
/*
 * Mask the signals we are interested in and create a signal handling thread.
 * Because all threads inherit the signal mask from their creator, all threads
 * in the process will have the signals masked. That's why setup_signals() has
 * to be called before creating other threads.
 */
static gboolean
364 365
setup_signals (void)
{
366 367 368 369 370 371 372 373 374 375 376
	pthread_t signal_thread_id;
	int status;

	sigemptyset (&signal_set);
	sigaddset (&signal_set, SIGINT);
	sigaddset (&signal_set, SIGQUIT);
	sigaddset (&signal_set, SIGTERM);

	/* Block all signals of interest. */
	status = pthread_sigmask (SIG_BLOCK, &signal_set, NULL);
	if (status != 0) {
377
		fprintf (stderr, _("Failed to set signal mask: %d\n"), status);
378 379 380
		return FALSE;
	}

381
	/* Create the signal handling thread. */
382 383
	status = pthread_create (&signal_thread_id, NULL, signal_handling_thread, NULL);
	if (status != 0) {
384
		fprintf (stderr, _("Failed to create signal handling thread: %d\n"), status);
385 386 387 388
		return FALSE;
	}

	return TRUE;
389 390 391 392 393
}

static NMClient *
nmc_get_client (NmCli *nmc)
{
394 395
	GError *error = NULL;

396
	if (!nmc->client) {
397
		nmc->client = nm_client_new (NULL, &error);
398
		if (!nmc->client) {
399 400
			g_critical (_("Error: Could not create NMClient object: %s."), error->message);
			g_clear_error (&error);
401
			exit (NMC_RESULT_ERROR_UNKNOWN);
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
		}
	}

	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"));

418
	nmc->timeout = -1;
419 420 421 422 423 424

	nmc->system_settings = NULL;
	nmc->system_settings_running = FALSE;
	nmc->system_connections = NULL;

	nmc->should_wait = FALSE;
425
	nmc->nowait_flag = TRUE;
426
	nmc->print_output = NMC_PRINT_NORMAL;
427
	nmc->multiline_output = FALSE;
428
	nmc->mode_specified = FALSE;
429 430
	nmc->escape_values = TRUE;
	nmc->required_fields = NULL;
431
	nmc->output_data = g_ptr_array_new_full (20, g_free);
432
	memset (&nmc->print_fields, '\0', sizeof (NmcPrintFields));
433
	nmc->nocheck_ver = FALSE;
434
	nmc->ask = FALSE;
435
	nmc->in_editor = FALSE;
436
	nmc->editor_status_line = FALSE;
437
	nmc->editor_save_confirmation = TRUE;
438
	nmc->editor_prompt_color = NMC_TERM_COLOR_NORMAL;
439 440 441 442 443 444 445 446 447 448 449
}

static void
nmc_cleanup (NmCli *nmc)
{
	if (nmc->client) g_object_unref (nmc->client);

	g_string_free (nmc->return_text, TRUE);

	if (nmc->system_settings) g_object_unref (nmc->system_settings);
	g_slist_free (nmc->system_connections);
450 451

	g_free (nmc->required_fields);
452 453
	nmc_empty_output_fields (nmc);
	g_ptr_array_unref (nmc->output_data);
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
}

static gboolean
start (gpointer data)
{
	ArgsInfo *info = (ArgsInfo *) data;
	info->nmc->return_value = parse_command_line (info->nmc, info->argc, info->argv);

	if (!info->nmc->should_wait)
		g_main_loop_quit (loop);

	return FALSE;
}


int
main (int argc, char *argv[])
{
472
	ArgsInfo args_info = { &nm_cli, argc, argv };
473

474 475 476 477
	/* Set up unix signal handling */
	if (!setup_signals ())
		exit (NMC_RESULT_ERROR_UNKNOWN);

478 479 480 481 482 483 484 485 486 487
	/* Set locale to use environment variables */
	setlocale (LC_ALL, "");

#ifdef GETTEXT_PACKAGE
	/* Set i18n stuff */
	bindtextdomain (GETTEXT_PACKAGE, NMCLI_LOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
#endif

488
#if !GLIB_CHECK_VERSION (2, 35, 0)
489
	g_type_init ();
490
#endif
491

492 493
	/* readline init */
	rl_event_hook = event_hook_for_readline;
494 495 496 497
	/* Set 0.01s timeout to mitigate slowness in readline when a broken version is used.
	 * See https://bugzilla.redhat.com/show_bug.cgi?id=1109946
	 */
	rl_set_keyboard_input_timeout (10000);
498

499
	nmc_init (&nm_cli);
500 501 502 503 504 505
	g_idle_add (start, &args_info);

	loop = g_main_loop_new (NULL, FALSE);  /* create main loop */
	g_main_loop_run (loop);                /* run main loop */

	/* Print result descripting text */
506 507
	if (nm_cli.return_value != NMC_RESULT_SUCCESS) {
		fprintf (stderr, "%s\n", nm_cli.return_text->str);
508 509 510
	}

	g_main_loop_unref (loop);
511
	nmc_cleanup (&nm_cli);
512

513
	return nm_cli.return_value;
514
}