1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/* -*- 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.
*
*/
/*
* nm-online.c - Are we online?
*
* Return values:
*
* 0 : already online or connection established within given timeout
* 1 : offline or not online within given timeout
* 2 : unspecified error
*
* Robert Love <rml@novell.com>
*/
#define PROGRESS_STEPS 15
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus-glib.h>
#include "NetworkManager.h"
#include "nm-glib-compat.h"
typedef struct
{
int value;
double norm;
gboolean quiet;
} Timeout;
static GMainLoop *loop;
static DBusHandlerResult dbus_filter (DBusConnection *connection G_GNUC_UNUSED,
DBusMessage *message,
void *user_data G_GNUC_UNUSED)
{
NMState state;
if (!dbus_message_is_signal (message, NM_DBUS_INTERFACE, "StateChanged"))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (!dbus_message_get_args (message, NULL, DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (state == NM_STATE_CONNECTED)
g_main_loop_quit (loop);
return DBUS_HANDLER_RESULT_HANDLED;
}
static NMState check_online (DBusConnection *connection)
{
DBusMessage *message, *reply;
DBusError error;
dbus_uint32_t state;
message = dbus_message_new_method_call (NM_DBUS_SERVICE, NM_DBUS_PATH,
NM_DBUS_INTERFACE, "state");
if (!message)
exit (2);
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection, message,
-1, &error);
dbus_message_unref (message);
if (!reply)
return NM_STATE_UNKNOWN;
if (!dbus_message_get_args (reply, NULL, DBUS_TYPE_UINT32, &state,
DBUS_TYPE_INVALID))
exit (2);
return state;
}
static gboolean handle_timeout (gpointer data)
{
int i = PROGRESS_STEPS;
Timeout *timeout = (Timeout *) data;
if (!timeout->quiet) {
g_print ("\rConnecting");
for (; i > 0; i--)
putchar ((timeout->value >= (i * timeout->norm)) ? ' ' : '.');
if (timeout->value)
g_print (" %4is", timeout->value);
fflush (stdout);
}
timeout->value--;
if (timeout->value < 0) {
if (!timeout->quiet)
g_print ("\n");
exit (1);
}
return TRUE;
}
int main (int argc, char *argv[])
{
DBusConnection *connection;
DBusError error;
NMState state;
gint t_secs = -1;
gboolean exit_no_nm = FALSE;
gboolean quiet = FALSE;
Timeout timeout;
GOptionContext *opt_ctx = NULL;
gboolean success;
GOptionEntry options[] = {
{"timeout", 't', 0, G_OPTION_ARG_INT, &t_secs, "Time to wait for a connection, in seconds (default is 30)", NULL},
{"exit", 'x', 0, G_OPTION_ARG_NONE, &exit_no_nm, "Exit immediately if NetworkManager is not running or connecting", NULL},
{"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, "Don't print anything", NULL},
{NULL}
};
opt_ctx = g_option_context_new ("");
g_option_context_set_translation_domain (opt_ctx, "UTF-8");
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,
"Waits for a successful connection in NetworkManager.");
success = g_option_context_parse (opt_ctx, &argc, &argv, NULL);
g_option_context_free (opt_ctx);
if (!success) {
g_warning ("Invalid option. Please use --help to see a list of valid options.");
return 2;
}
if (t_secs > -1)
timeout.value = t_secs;
else
timeout.value = 30;
if (timeout.value < 0 || timeout.value > 3600) {
g_warning ("Invalid option. Please use --help to see a list of valid options.");
return 2;
}
g_type_init ();
loop = g_main_loop_new (NULL, FALSE);
dbus_error_init (&error);
connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
if (connection == NULL) {
dbus_error_free (&error);
return 2;
}
dbus_connection_setup_with_g_main (connection, NULL);
if (!dbus_connection_add_filter (connection, dbus_filter, NULL, NULL))
return 2;
dbus_bus_add_match (connection,
"type='signal',"
"interface='" NM_DBUS_INTERFACE "',"
"sender='" NM_DBUS_SERVICE "',"
"path='" NM_DBUS_PATH "'", &error);
if (dbus_error_is_set (&error)) {
dbus_error_free (&error);
return 2;
}
/* Check after we setup the filter to ensure that we cannot race. */
state = check_online (connection);
if (state == NM_STATE_CONNECTED)
return 0;
if (exit_no_nm && (state != NM_STATE_CONNECTING))
return 1;
if (timeout.value) {
timeout.norm = (double) timeout.value / (double) PROGRESS_STEPS;
g_timeout_add_seconds (1, handle_timeout, &timeout);
}
timeout.quiet = quiet;
g_main_loop_run (loop);
return 0;
}
|