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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/* Copyright (c) 2006 Thorsten Kukuk
This file is part of ypbind-mt.
Author: Thorsten Kukuk <kukuk@suse.de>
The ypbind-mt are free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
ypbind-mt 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 ypbind-mt; see the file COPYING. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#ifdef USE_DBUS_NM
#define DBUS_API_SUBJECT_TO_CHANGE 1
#include <string.h>
#include <libintl.h>
#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus-glib.h>
#include <NetworkManager/NetworkManager.h>
#include "ypbind.h"
#include "log_msg.h"
#include "local.h"
#ifndef _
#define _(String) gettext (String)
#endif
int is_online = 0;
int dbus_is_initialized = 0;
pthread_mutex_t mutex_dbus = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_dbus = PTHREAD_COND_INITIALIZER;
static void
go_offline (void)
{
if (is_online == 0) /* Do nothing if already offline. */
return;
if (debug_flag)
log_msg (LOG_DEBUG, _("Switch to offline mode"));
is_online = 0;
portmapper_disconnect ();
clear_server ();
}
static void
go_online (void)
{
if (is_online) /* Do nothing if already online. */
return;
if (debug_flag)
log_msg (LOG_DEBUG, _("Switch to online mode"));
is_online = 1;
/* Reload config file, may have changed. */
if (debug_flag)
log_msg (LOG_DEBUG, _("Going online, reloading config file."));
clear_server ();
if (use_broadcast)
add_server (domain, NULL, 0);
else
load_config (0);
if (portmapper_connect () != 0)
{
go_offline (); /* go offline again */
}
do_binding ();
}
static int dbus_init (void);
static gboolean
dbus_reconnect (gpointer user_data)
{
gboolean status;
status = dbus_init ();
if (debug_flag)
log_msg (LOG_DEBUG, "Reconnect %s",
status ? "successful" : "failed");
return !status;
}
static DBusHandlerResult
dbus_filter (DBusConnection *connection,
DBusMessage *message, void *user_data __attribute__((unused)))
{
DBusHandlerResult handled = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL,
"Disconnected"))
{
/* D-Bus system bus went away */
log_msg (LOG_INFO, "Lost connection to D-Bus\n");
dbus_connection_unref (connection);
connection = NULL;
g_timeout_add (1000, dbus_reconnect, NULL);
handled = DBUS_HANDLER_RESULT_HANDLED;
}
else if (dbus_message_is_signal (message, NM_DBUS_INTERFACE,
"StateChange"))
{
NMState state = NM_STATE_UNKNOWN;
if (dbus_message_get_args (message, NULL, DBUS_TYPE_UINT32,
&state, DBUS_TYPE_INVALID))
{
if (state == NM_STATE_CONNECTED)
go_online ();
else if (state == NM_STATE_DISCONNECTED)
go_offline ();
}
handled = DBUS_HANDLER_RESULT_HANDLED;
}
else if (dbus_message_is_signal (message, NM_DBUS_INTERFACE,
"DeviceNoLongerActive"))
{
go_offline ();
handled = DBUS_HANDLER_RESULT_HANDLED;
}
else if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
"NameOwnerChanged"))
{
if (debug_flag)
{
char *service = NULL;
char *old_owner = NULL;
char *new_owner = NULL;
if (dbus_message_get_args (message, NULL,
DBUS_TYPE_STRING, &service,
DBUS_TYPE_STRING, &old_owner,
DBUS_TYPE_STRING, &new_owner,
DBUS_TYPE_INVALID))
{
if (strcmp (service, NM_DBUS_SERVICE) == 0)
{
/* Check if it was NetworkManager who dropped off or
jumped on the system bus. */
int old_owner_good =
(old_owner && (strlen (old_owner) > 0));
int new_owner_good =
(new_owner && (strlen (new_owner) > 0));
if (!old_owner_good && new_owner_good)
log_msg (LOG_DEBUG,
"NetworkManager is on the system bus");
else if (old_owner_good && !new_owner_good)
log_msg (LOG_DEBUG,
"NetworkManager left the system bus");
}
}
handled = DBUS_HANDLER_RESULT_HANDLED;
}
}
else if (debug_flag)
{
log_msg (LOG_DEBUG, "interface: %s, object path: %s, method: %s",
dbus_message_get_interface(message),
dbus_message_get_path (message),
dbus_message_get_member (message));
}
return handled;
}
static int
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)
return -1;
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection, message,
-1, &error);
dbus_message_unref (message);
if (!reply)
return 0;
if (!dbus_message_get_args (reply, NULL, DBUS_TYPE_UINT32, &state,
DBUS_TYPE_INVALID))
return -1;
if (state != NM_STATE_CONNECTED)
return 0;
return 1;
}
static int
check_for_nm (DBusConnection *connection)
{
if (dbus_bus_name_has_owner (connection, NM_DBUS_SERVICE, NULL))
{
if (debug_flag)
log_msg (LOG_DEBUG, "NetworkManager is running.\n");
return 1;
}
else
{
if (debug_flag)
log_msg (LOG_DEBUG, "NetworkManager is not running.\n");
return 0;
}
}
static int
dbus_init (void)
{
DBusConnection *connection = NULL;
DBusError error;
dbus_error_init (&error);
connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
if (connection == NULL || dbus_error_is_set (&error))
{
log_msg (LOG_ERR, "Connection to D-BUS system message bus failed: %s.",
error.message);
dbus_error_free (&error);
goto out;
}
dbus_connection_set_exit_on_disconnect (connection, FALSE);
if (!dbus_connection_add_filter (connection, dbus_filter, NULL, NULL))
goto out;
dbus_bus_add_match (connection, "type='signal',"
"interface='" DBUS_INTERFACE_DBUS "',"
"sender='" DBUS_SERVICE_DBUS "'",
&error);
if (dbus_error_is_set (&error))
{
log_msg (LOG_ERR, "Error adding match, %s: %s",
error.name, error.message);
dbus_error_free (&error);
goto out;
}
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))
{
log_msg (LOG_ERR, "Error adding match, %s: %s",
error.name, error.message);
dbus_error_free (&error);
goto out;
}
dbus_connection_setup_with_g_main (connection, NULL);
out:
if (connection)
{
if (!check_for_nm (connection))
{
/* NetworkManager not in use. */
dbus_connection_close (connection);
is_online = 1;
return 0;
}
if (check_online (connection) == 1)
{
if (debug_flag)
log_msg (LOG_DEBUG, "Are already online");
is_online = 1;
}
else
{
if (debug_flag)
log_msg (LOG_DEBUG, "Are offline");
is_online = 0;
}
return 1;
}
else
return 0;
}
/* This thread handles the NetworkManager communication over DBUS */
void *
watch_dbus_nm (void *param __attribute__ ((unused)))
{
static int status = 1;
GMainLoop *loop;
int dbus_init_ret;
g_type_init ();
dbus_init_ret = dbus_init ();
if (dbus_init_ret != 1)
{
log_msg (LOG_DEBUG, "Assuming online mode\n");
is_online = 1;
}
/* Signal the main thread that we have the dbus connection
initialized. So we can continue. */
pthread_mutex_lock(&mutex_dbus);
dbus_is_initialized = 1;
pthread_cond_broadcast(&cond_dbus);
pthread_mutex_unlock(&mutex_dbus);
/* Now return if no DBUS/NetworkManager is in use. */
if (dbus_init_ret != 1)
{
status = 0;
return &status;
}
loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop);
return &status;
}
#endif
|