nm-online.c 7.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* -*- 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.
18
 * Copyright (C) 2008 - 2014 Red Hat, Inc.
19 20 21
 *
 */

Robert Love's avatar
 
Robert Love committed
22 23 24 25 26
/*
 * nm-online.c - Are we online?
 *
 * Return values:
 *
27
 * 	0	: already online or connection established within given timeout
Robert Love's avatar
 
Robert Love committed
28
 *	1	: offline or not online within given timeout
29
 *	2	: unspecified error
Robert Love's avatar
 
Robert Love committed
30 31 32 33
 *
 * Robert Love <rml@novell.com>
 */

34
#include <config.h>
Robert Love's avatar
 
Robert Love committed
35 36 37

#include <stdio.h>
#include <stdlib.h>
38
#include <getopt.h>
39
#include <locale.h>
Robert Love's avatar
 
Robert Love committed
40

41
#include <glib/gi18n.h>
Robert Love's avatar
 
Robert Love committed
42

43
#include <NetworkManager.h>
Robert Love's avatar
 
Robert Love committed
44

45
#define PROGRESS_STEPS 15
46
#define WAIT_STARTUP_TAG "wait-startup"
47

48
typedef struct
49
{
50 51 52
	gint64 start_timestamp_ms;
	gint64 end_timestamp_ms;
	gint64 progress_step_duration;
53
	gboolean quiet;
54 55
} Timeout;

56 57 58 59
static void
client_properties_changed (GObject *object,
                           GParamSpec *pspec,
                           gpointer loop)
Robert Love's avatar
 
Robert Love committed
60
{
61
	NMClient *client = NM_CLIENT (object);
62 63
	NMState state;
	gboolean wait_startup = GPOINTER_TO_UINT (g_object_get_data (object, WAIT_STARTUP_TAG));
64

65 66
	if (!nm_client_get_manager_running (client))
		return;
67 68 69 70 71 72 73 74 75 76 77

	if (wait_startup) {
		if (!nm_client_get_startup (client))
			g_main_loop_quit (loop);
	} else {
		state = nm_client_get_state (client);
		if (   state == NM_STATE_CONNECTED_LOCAL
		    || state == NM_STATE_CONNECTED_SITE
		    || state == NM_STATE_CONNECTED_GLOBAL)
			g_main_loop_quit (loop);
	}
Robert Love's avatar
 
Robert Love committed
78 79
}

Dan Winship's avatar
Dan Winship committed
80 81
static gboolean
handle_timeout (gpointer data)
Robert Love's avatar
 
Robert Love committed
82
{
83 84 85 86 87
	const Timeout *timeout = data;
	const gint64 now = g_get_monotonic_time () / (G_USEC_PER_SEC / 1000);
	gint64 remaining_ms = timeout->end_timestamp_ms - now;
	const gint64 elapsed_ms = now - timeout->start_timestamp_ms;
	int progress_next_step_i = 0;
88

89
	if (!timeout->quiet) {
90 91 92 93 94 95
		int i;

		/* calculate the next step (not the current): floor()+1 */
		progress_next_step_i = (elapsed_ms / timeout->progress_step_duration) + 1;
		progress_next_step_i = MIN (progress_next_step_i, PROGRESS_STEPS);

96
		g_print (_("\rConnecting"));
97 98 99
		for (i = 0; i < PROGRESS_STEPS; i++)
			putchar (i < progress_next_step_i ? '.' : ' ');
		g_print (" %4lds", (long) (MAX (0, remaining_ms) / 1000));
100 101
		fflush (stdout);
	}
102

103
	if (remaining_ms <= 3) {
104 105
		if (!timeout->quiet)
			g_print ("\n");
106
		exit (1);
107
	}
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	if (!timeout->quiet) {
		gint64 rem;

		/* synchronize the timeout with the ticking of the seconds. */
		rem = remaining_ms % 1000;
		if (rem <= 3)
			rem = rem + G_USEC_PER_SEC;
		rem = rem + 10; /* add small offset to awake a bit after the second ticks */
		if (remaining_ms > rem)
			remaining_ms = rem;

		/* synchronize the timeout with the steps of the progress bar. */
		rem = (progress_next_step_i * timeout->progress_step_duration) - elapsed_ms;
		if (rem <= 3)
			rem = rem + timeout->progress_step_duration;
		rem = rem + 10; /* add small offset to awake a bit after the time out */
		if (remaining_ms > rem)
			remaining_ms = rem;
	}

	g_timeout_add (remaining_ms, handle_timeout, (void *) timeout);
	return G_SOURCE_REMOVE;
Robert Love's avatar
 
Robert Love committed
131 132
}

Dan Winship's avatar
Dan Winship committed
133 134
int
main (int argc, char *argv[])
Robert Love's avatar
 
Robert Love committed
135
{
136
	int t_secs = 30;
137
	gboolean exit_no_nm = FALSE;
138
	gboolean wait_startup = FALSE;
139
	Timeout timeout;
140 141
	GOptionContext *opt_ctx = NULL;
	gboolean success;
142
	NMClient *client;
143
	NMState state = NM_STATE_UNKNOWN;
144
	GMainLoop *loop;
145
	gint64 remaining_ms;
146
	GError *error = NULL;
147 148

	GOptionEntry options[] = {
149
		{"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>"},
150
		{"exit", 'x', 0, G_OPTION_ARG_NONE, &exit_no_nm, N_("Exit immediately if NetworkManager is not running or connecting"), NULL},
151
		{"quiet", 'q', 0, G_OPTION_ARG_NONE, &timeout.quiet, N_("Don't print anything"), NULL},
152
		{"wait-for-startup", 's', 0, G_OPTION_ARG_NONE, &wait_startup, N_("Wait for NetworkManager startup instead of a connection"), NULL},
153 154 155
		{NULL}
	};

156
	timeout.start_timestamp_ms = g_get_monotonic_time () / (G_USEC_PER_SEC / 1000);
157 158
	timeout.quiet = FALSE;

159 160 161 162 163 164 165 166 167
	/* 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);

	opt_ctx = g_option_context_new (NULL);
	g_option_context_set_translation_domain (opt_ctx, GETTEXT_PACKAGE);
168 169 170 171 172
	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,
173
	                              _("Waits for NetworkManager to finish activating startup network connections."));
174 175 176 177 178

	success = g_option_context_parse (opt_ctx, &argc, &argv, NULL);
	g_option_context_free (opt_ctx);

	if (!success) {
179 180
		g_printerr ("%s: %s\n", argv[0],
		            _("Invalid option.  Please use --help to see a list of valid options."));
181 182
		return 2;
	}
183 184

	if (t_secs < 0 || t_secs > 3600)  {
185 186
		g_printerr ("%s: %s\n", argv[0],
		            _("Invalid option.  Please use --help to see a list of valid options."));
187
		return 2;
Robert Love's avatar
 
Robert Love committed
188
	}
189
	remaining_ms = t_secs * 1000;
Robert Love's avatar
 
Robert Love committed
190

191
#if !GLIB_CHECK_VERSION (2, 35, 0)
Robert Love's avatar
 
Robert Love committed
192
	g_type_init ();
193
#endif
Robert Love's avatar
 
Robert Love committed
194

195
	client = nm_client_new (NULL, &error);
196
	if (!client) {
197 198
		g_printerr (_("Error: Could not create NMClient object: %s."), error->message);
		g_error_free (error);
199 200 201 202
		return 2;
	}

	loop = g_main_loop_new (NULL, FALSE);
Robert Love's avatar
 
Robert Love committed
203

204 205
	g_object_set_data (G_OBJECT (client), WAIT_STARTUP_TAG, GUINT_TO_POINTER (wait_startup));
	state = nm_client_get_state (client);
206
	if (!nm_client_get_manager_running (client)) {
207 208
		if (exit_no_nm) {
			g_object_unref (client);
209
			return 1;
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
		}
	} else if (wait_startup) {
		if (!nm_client_get_startup (client)) {
			g_object_unref (client);
			return 0;
		}
	} else {
		if (   state == NM_STATE_CONNECTED_LOCAL
		    || state == NM_STATE_CONNECTED_SITE
		    || state == NM_STATE_CONNECTED_GLOBAL) {
			g_object_unref (client);
			return 0;
		}
	}
	if (exit_no_nm && (state != NM_STATE_CONNECTING)) {
		g_object_unref (client);
226
		return 1;
227 228
	}

229
	if (remaining_ms == 0) {
230 231 232
		g_object_unref (client);
		return 1;
	}
Robert Love's avatar
 
Robert Love committed
233

234 235
	g_signal_connect (client, "notify",
	                  G_CALLBACK (client_properties_changed), loop);
Robert Love's avatar
 
Robert Love committed
236

237 238 239 240 241 242
	timeout.end_timestamp_ms = timeout.start_timestamp_ms + remaining_ms;
	timeout.progress_step_duration = (timeout.end_timestamp_ms - timeout.start_timestamp_ms + PROGRESS_STEPS/2) / PROGRESS_STEPS;

	g_timeout_add (timeout.quiet ? remaining_ms : 0,
	               handle_timeout, &timeout);

Robert Love's avatar
 
Robert Love committed
243
	g_main_loop_run (loop);
244 245 246
	g_main_loop_unref (loop);

	g_object_unref (client);
Robert Love's avatar
 
Robert Love committed
247

248
	return 0;
Robert Love's avatar
 
Robert Love committed
249
}