2008-12-20 14:46:41 +00:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* 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, 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 (C) 2006 - 2008 Novell, Inc.
|
2014-03-12 12:34:13 +00:00
|
|
|
* Copyright (C) 2008 - 2014 Red Hat, Inc.
|
2008-12-20 14:46:41 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-04-26 19:34:03 +00:00
|
|
|
/*
|
|
|
|
* nm-online.c - Are we online?
|
|
|
|
*
|
|
|
|
* Return values:
|
|
|
|
*
|
2017-01-31 14:13:05 +00:00
|
|
|
* 0: already online or connection established within given timeout
|
|
|
|
* 1: offline or not online within given timeout
|
|
|
|
* 2: unspecified error
|
2006-04-26 19:34:03 +00:00
|
|
|
*
|
|
|
|
* Robert Love <rml@novell.com>
|
|
|
|
*/
|
|
|
|
|
2016-02-19 13:57:48 +00:00
|
|
|
#include "nm-default.h"
|
2006-04-26 19:34:03 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-04-15 19:47:23 +00:00
|
|
|
#include <getopt.h>
|
2012-07-09 13:11:43 +00:00
|
|
|
#include <locale.h>
|
2006-04-26 19:34:03 +00:00
|
|
|
|
2012-07-09 13:11:43 +00:00
|
|
|
#define PROGRESS_STEPS 15
|
2017-01-31 14:13:05 +00:00
|
|
|
|
2018-06-29 09:00:38 +00:00
|
|
|
#define EXIT_NONE -1
|
2017-01-31 14:13:05 +00:00
|
|
|
#define EXIT_FAILURE_OFFLINE 1
|
|
|
|
#define EXIT_FAILURE_ERROR 2
|
|
|
|
#define EXIT_FAILURE_LIBNM_BUG 42
|
|
|
|
#define EXIT_FAILURE_UNSPECIFIED 43
|
2012-07-09 13:11:43 +00:00
|
|
|
|
2013-08-13 21:49:05 +00:00
|
|
|
typedef struct
|
2006-05-02 14:47:59 +00:00
|
|
|
{
|
2016-12-23 13:30:58 +00:00
|
|
|
GMainLoop *loop;
|
|
|
|
NMClient *client;
|
2017-01-31 14:13:05 +00:00
|
|
|
GCancellable *client_new_cancellable;
|
|
|
|
guint client_new_timeout_id;
|
|
|
|
guint handle_timeout_id;
|
|
|
|
gulong client_notify_id;
|
2016-12-23 13:30:58 +00:00
|
|
|
gboolean exit_no_nm;
|
|
|
|
gboolean wait_startup;
|
2017-01-31 14:13:05 +00:00
|
|
|
gboolean quiet;
|
2014-03-12 14:51:11 +00:00
|
|
|
gint64 start_timestamp_ms;
|
|
|
|
gint64 end_timestamp_ms;
|
|
|
|
gint64 progress_step_duration;
|
2017-01-31 14:13:05 +00:00
|
|
|
int retval;
|
2016-12-23 13:30:58 +00:00
|
|
|
} OnlineData;
|
2006-05-02 14:47:59 +00:00
|
|
|
|
2017-01-31 14:13:05 +00:00
|
|
|
static gint64
|
|
|
|
_now_ms (void)
|
|
|
|
{
|
|
|
|
return g_get_monotonic_time () / (G_USEC_PER_SEC / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_return (OnlineData *data, int retval)
|
|
|
|
{
|
|
|
|
nm_assert (data);
|
|
|
|
nm_assert (data->retval == EXIT_FAILURE_UNSPECIFIED);
|
|
|
|
|
|
|
|
data->retval = retval;
|
2017-02-07 18:39:48 +00:00
|
|
|
nm_clear_g_signal_handler (data->client, &data->client_notify_id);
|
2017-01-31 14:13:05 +00:00
|
|
|
g_main_loop_quit (data->loop);
|
|
|
|
}
|
|
|
|
|
2013-08-13 21:49:05 +00:00
|
|
|
static void
|
2018-06-29 09:00:38 +00:00
|
|
|
_print_progress (gboolean wait_startup, int progress_next_step_i, gint64 remaining_ms, int retval)
|
2017-01-31 14:13:05 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
j = progress_next_step_i < 0 ? PROGRESS_STEPS : progress_next_step_i;
|
|
|
|
|
|
|
|
g_print ("\r%s", _("Connecting"));
|
|
|
|
for (i = 0; i < PROGRESS_STEPS; i++)
|
|
|
|
putchar (i < j ? '.' : ' ');
|
2017-02-02 17:55:39 +00:00
|
|
|
g_print (" %4lds", (long) (MAX (0, remaining_ms + 999) / 1000));
|
2018-06-29 09:00:38 +00:00
|
|
|
if (retval != EXIT_NONE) {
|
|
|
|
const char *result;
|
|
|
|
|
|
|
|
if (wait_startup) {
|
|
|
|
if (retval == EXIT_SUCCESS)
|
|
|
|
result = "started";
|
|
|
|
else if (retval == EXIT_FAILURE_OFFLINE)
|
|
|
|
result = "startup-pending";
|
|
|
|
else
|
|
|
|
result = "failure";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (retval == EXIT_SUCCESS)
|
|
|
|
result = "online";
|
|
|
|
else
|
|
|
|
result = "offline";
|
|
|
|
}
|
|
|
|
|
|
|
|
g_print (" [%s]\n", result);
|
|
|
|
}
|
2017-01-31 14:13:05 +00:00
|
|
|
fflush (stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-12-23 13:30:58 +00:00
|
|
|
quit_if_connected (OnlineData *data)
|
2006-04-26 19:34:03 +00:00
|
|
|
{
|
2014-03-12 12:34:13 +00:00
|
|
|
NMState state;
|
2008-04-15 19:47:23 +00:00
|
|
|
|
2016-12-23 13:30:58 +00:00
|
|
|
state = nm_client_get_state (data->client);
|
|
|
|
if (!nm_client_get_nm_running (data->client)) {
|
|
|
|
if (data->exit_no_nm) {
|
2017-01-31 14:13:05 +00:00
|
|
|
_return (data, EXIT_FAILURE_OFFLINE);
|
|
|
|
return TRUE;
|
2016-12-23 13:30:58 +00:00
|
|
|
}
|
|
|
|
} else if (data->wait_startup) {
|
|
|
|
if (!nm_client_get_startup (data->client)) {
|
2017-01-31 14:13:05 +00:00
|
|
|
_return (data, EXIT_SUCCESS);
|
|
|
|
return TRUE;
|
2016-12-23 13:30:58 +00:00
|
|
|
}
|
2014-03-12 12:34:13 +00:00
|
|
|
} else {
|
|
|
|
if ( state == NM_STATE_CONNECTED_LOCAL
|
|
|
|
|| state == NM_STATE_CONNECTED_SITE
|
2016-12-23 13:30:58 +00:00
|
|
|
|| state == NM_STATE_CONNECTED_GLOBAL) {
|
2017-01-31 14:13:05 +00:00
|
|
|
_return (data, EXIT_SUCCESS);
|
|
|
|
return TRUE;
|
2016-12-23 13:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (data->exit_no_nm && (state != NM_STATE_CONNECTING)) {
|
2017-01-31 14:13:05 +00:00
|
|
|
_return (data, EXIT_FAILURE_OFFLINE);
|
|
|
|
return TRUE;
|
2014-03-12 12:34:13 +00:00
|
|
|
}
|
2017-01-31 14:13:05 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2006-04-26 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 13:30:58 +00:00
|
|
|
static void
|
|
|
|
client_properties_changed (GObject *object,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2017-01-31 14:13:05 +00:00
|
|
|
quit_if_connected (user_data);
|
2016-12-23 13:30:58 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 13:07:39 +00:00
|
|
|
static gboolean
|
2016-12-23 13:30:58 +00:00
|
|
|
handle_timeout (gpointer user_data)
|
2006-04-26 19:34:03 +00:00
|
|
|
{
|
2017-01-31 14:13:05 +00:00
|
|
|
OnlineData *data = user_data;
|
|
|
|
const gint64 now = _now_ms ();
|
2016-12-23 13:30:58 +00:00
|
|
|
gint64 remaining_ms = data->end_timestamp_ms - now;
|
|
|
|
const gint64 elapsed_ms = now - data->start_timestamp_ms;
|
2014-03-12 14:51:11 +00:00
|
|
|
int progress_next_step_i = 0;
|
2006-05-02 14:47:59 +00:00
|
|
|
|
2017-01-31 14:13:05 +00:00
|
|
|
data->handle_timeout_id = 0;
|
2006-05-02 14:47:59 +00:00
|
|
|
|
2014-03-12 14:51:11 +00:00
|
|
|
if (remaining_ms <= 3) {
|
2017-01-31 14:13:05 +00:00
|
|
|
_return (data, EXIT_FAILURE_OFFLINE);
|
|
|
|
return G_SOURCE_REMOVE;
|
2008-04-15 19:47:23 +00:00
|
|
|
}
|
2006-05-02 14:47:59 +00:00
|
|
|
|
2016-12-23 13:30:58 +00:00
|
|
|
if (!data->quiet) {
|
2014-03-12 14:51:11 +00:00
|
|
|
gint64 rem;
|
|
|
|
|
2017-01-31 14:13:05 +00:00
|
|
|
/* calculate the next step (not the current): floor()+1 */
|
|
|
|
progress_next_step_i = NM_MIN ((elapsed_ms / data->progress_step_duration) + 1, PROGRESS_STEPS);
|
2018-06-29 09:00:38 +00:00
|
|
|
_print_progress (data->wait_startup, progress_next_step_i, remaining_ms, EXIT_NONE);
|
2017-01-31 14:13:05 +00:00
|
|
|
|
2014-03-12 14:51:11 +00:00
|
|
|
/* synchronize the timeout with the ticking of the seconds. */
|
|
|
|
rem = remaining_ms % 1000;
|
|
|
|
if (rem <= 3)
|
|
|
|
rem = rem + G_USEC_PER_SEC;
|
2017-01-31 14:13:05 +00:00
|
|
|
/* add small offset to awake a bit after the second ticks */
|
|
|
|
remaining_ms = NM_MIN (remaining_ms, rem + 10);
|
2014-03-12 14:51:11 +00:00
|
|
|
|
|
|
|
/* synchronize the timeout with the steps of the progress bar. */
|
2016-12-23 13:30:58 +00:00
|
|
|
rem = (progress_next_step_i * data->progress_step_duration) - elapsed_ms;
|
2014-03-12 14:51:11 +00:00
|
|
|
if (rem <= 3)
|
2016-12-23 13:30:58 +00:00
|
|
|
rem = rem + data->progress_step_duration;
|
2017-01-31 14:13:05 +00:00
|
|
|
/* add small offset to awake a bit after the second ticks */
|
|
|
|
remaining_ms = NM_MIN (remaining_ms, rem + 10);
|
2014-03-12 14:51:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 14:13:05 +00:00
|
|
|
data->handle_timeout_id = g_timeout_add (remaining_ms, handle_timeout, data);
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
got_client_timeout (gpointer user_data)
|
|
|
|
{
|
|
|
|
OnlineData *data = user_data;
|
|
|
|
|
|
|
|
data->client_new_timeout_id = 0;
|
|
|
|
data->quiet = TRUE;
|
|
|
|
g_printerr (_("Error: timeout creating NMClient object\n"));
|
|
|
|
_return (data, EXIT_FAILURE_LIBNM_BUG);
|
2014-03-12 14:51:11 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
2006-04-26 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 13:30:58 +00:00
|
|
|
static void
|
|
|
|
got_client (GObject *source_object, GAsyncResult *res, gpointer user_data)
|
|
|
|
{
|
|
|
|
OnlineData *data = user_data;
|
2017-01-31 14:13:05 +00:00
|
|
|
gs_free_error GError *error = NULL;
|
|
|
|
NMClient *client;
|
|
|
|
|
|
|
|
nm_clear_g_source (&data->client_new_timeout_id);
|
|
|
|
g_clear_object (&data->client_new_cancellable);
|
2016-12-23 13:30:58 +00:00
|
|
|
|
2017-01-31 14:13:05 +00:00
|
|
|
client = nm_client_new_finish (res, &error);
|
|
|
|
if (!client) {
|
|
|
|
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
|
|
|
|
return;
|
|
|
|
data->quiet = TRUE;
|
|
|
|
g_printerr (_("Error: Could not create NMClient object: %s\n"),
|
2016-12-23 13:30:58 +00:00
|
|
|
error->message);
|
2017-01-31 14:13:05 +00:00
|
|
|
_return (data, EXIT_FAILURE_ERROR);
|
|
|
|
return;
|
2016-12-23 13:30:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 14:13:05 +00:00
|
|
|
data->client = client;
|
|
|
|
|
|
|
|
if (quit_if_connected (data))
|
|
|
|
return;
|
|
|
|
|
|
|
|
data->client_notify_id = g_signal_connect (data->client, "notify",
|
|
|
|
G_CALLBACK (client_properties_changed), data);
|
|
|
|
data->handle_timeout_id = g_timeout_add (data->quiet ? NM_MAX (0, data->end_timestamp_ms - _now_ms ()) : 0, handle_timeout, data);
|
2016-12-23 13:30:58 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 13:07:39 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
2006-04-26 19:34:03 +00:00
|
|
|
{
|
2017-01-31 14:13:05 +00:00
|
|
|
OnlineData data = {
|
|
|
|
.retval = EXIT_FAILURE_UNSPECIFIED,
|
|
|
|
};
|
2014-03-12 14:51:11 +00:00
|
|
|
int t_secs = 30;
|
2008-04-15 19:47:23 +00:00
|
|
|
GOptionContext *opt_ctx = NULL;
|
|
|
|
gboolean success;
|
|
|
|
GOptionEntry options[] = {
|
2013-07-11 13:21:22 +00:00
|
|
|
{"timeout", 't', 0, G_OPTION_ARG_INT, &t_secs, N_("Time to wait for a connection, in seconds (without the option, default value is 30)"), "<timeout>"},
|
2016-12-23 13:30:58 +00:00
|
|
|
{"exit", 'x', 0, G_OPTION_ARG_NONE, &data.exit_no_nm, N_("Exit immediately if NetworkManager is not running or connecting"), NULL},
|
|
|
|
{"quiet", 'q', 0, G_OPTION_ARG_NONE, &data.quiet, N_("Don't print anything"), NULL},
|
|
|
|
{"wait-for-startup", 's', 0, G_OPTION_ARG_NONE, &data.wait_startup, N_("Wait for NetworkManager startup instead of a connection"), NULL},
|
2017-01-31 14:13:05 +00:00
|
|
|
{ NULL },
|
2008-04-15 19:47:23 +00:00
|
|
|
};
|
|
|
|
|
2012-07-09 13:11:43 +00:00
|
|
|
/* Set locale to be able to use environment variables */
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
|
|
|
bindtextdomain (GETTEXT_PACKAGE, NMLOCALEDIR);
|
|
|
|
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
|
|
|
|
textdomain (GETTEXT_PACKAGE);
|
|
|
|
|
2017-01-31 14:13:05 +00:00
|
|
|
data.start_timestamp_ms = _now_ms ();
|
|
|
|
|
2012-07-09 13:11:43 +00:00
|
|
|
opt_ctx = g_option_context_new (NULL);
|
|
|
|
g_option_context_set_translation_domain (opt_ctx, GETTEXT_PACKAGE);
|
2008-04-15 19:47:23 +00:00
|
|
|
g_option_context_set_ignore_unknown_options (opt_ctx, FALSE);
|
|
|
|
g_option_context_set_help_enabled (opt_ctx, TRUE);
|
|
|
|
g_option_context_add_main_entries (opt_ctx, options, NULL);
|
|
|
|
|
|
|
|
g_option_context_set_summary (opt_ctx,
|
2013-08-13 21:49:05 +00:00
|
|
|
_("Waits for NetworkManager to finish activating startup network connections."));
|
2008-04-15 19:47:23 +00:00
|
|
|
|
|
|
|
success = g_option_context_parse (opt_ctx, &argc, &argv, NULL);
|
|
|
|
g_option_context_free (opt_ctx);
|
|
|
|
|
|
|
|
if (!success) {
|
2013-08-13 21:49:05 +00:00
|
|
|
g_printerr ("%s: %s\n", argv[0],
|
|
|
|
_("Invalid option. Please use --help to see a list of valid options."));
|
2017-01-31 14:13:05 +00:00
|
|
|
return EXIT_FAILURE_ERROR;
|
2008-04-15 19:47:23 +00:00
|
|
|
}
|
2014-03-12 14:51:11 +00:00
|
|
|
|
|
|
|
if (t_secs < 0 || t_secs > 3600) {
|
2013-08-13 21:49:05 +00:00
|
|
|
g_printerr ("%s: %s\n", argv[0],
|
|
|
|
_("Invalid option. Please use --help to see a list of valid options."));
|
2017-01-31 14:13:05 +00:00
|
|
|
return EXIT_FAILURE_ERROR;
|
2006-04-26 19:34:03 +00:00
|
|
|
}
|
2017-01-31 14:13:05 +00:00
|
|
|
|
|
|
|
if (t_secs == 0)
|
|
|
|
data.quiet = TRUE;
|
2006-04-26 19:34:03 +00:00
|
|
|
|
2016-12-23 13:30:58 +00:00
|
|
|
data.loop = g_main_loop_new (NULL, FALSE);
|
2014-03-12 14:51:11 +00:00
|
|
|
|
2017-01-31 14:13:05 +00:00
|
|
|
data.end_timestamp_ms = data.start_timestamp_ms + (t_secs * 1000);
|
2017-01-30 23:11:10 +00:00
|
|
|
data.progress_step_duration = NM_MAX (1, (data.end_timestamp_ms - data.start_timestamp_ms + PROGRESS_STEPS/2) / PROGRESS_STEPS);
|
2014-03-12 14:51:11 +00:00
|
|
|
|
2017-01-31 14:13:05 +00:00
|
|
|
data.client_new_cancellable = g_cancellable_new ();
|
|
|
|
|
|
|
|
data.client_new_timeout_id = g_timeout_add_seconds (30, got_client_timeout, &data);
|
|
|
|
nm_client_new_async (data.client_new_cancellable, got_client, &data);
|
2013-08-13 21:49:05 +00:00
|
|
|
|
2016-12-23 13:30:58 +00:00
|
|
|
g_main_loop_run (data.loop);
|
2017-01-31 14:13:05 +00:00
|
|
|
|
|
|
|
nm_clear_g_cancellable (&data.client_new_cancellable);
|
|
|
|
nm_clear_g_source (&data.client_new_timeout_id);
|
|
|
|
nm_clear_g_source (&data.handle_timeout_id);
|
|
|
|
nm_clear_g_signal_handler (data.client, &data.client_notify_id);
|
|
|
|
g_clear_object (&data.client);
|
|
|
|
|
|
|
|
g_clear_pointer (&data.loop, g_main_loop_unref);
|
|
|
|
|
|
|
|
if (!data.quiet)
|
2018-06-29 09:00:38 +00:00
|
|
|
_print_progress (data.wait_startup, -1, NM_MAX (0, data.end_timestamp_ms - _now_ms ()), data.retval);
|
2006-04-26 19:34:03 +00:00
|
|
|
|
2016-12-23 13:30:58 +00:00
|
|
|
return data.retval;
|
2006-04-26 19:34:03 +00:00
|
|
|
}
|