summaryrefslogtreecommitdiff
path: root/libnm_glib/libnm_glib.c
blob: 3cbcaf6ba7f19e9bf3ddff2b2f48873943488c74 (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* libnm_glib -- Access NetworkManager information from applications
 *
 * Dan Williams <dcbw@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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * (C) Copyright 2005 Red Hat, Inc.
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "NetworkManager.h"
#include "libnm_glib.h"

#define	DBUS_NO_SERVICE_ERROR			"org.freedesktop.DBus.Error.ServiceDoesNotExist"

struct libnm_glib_ctx
{
	unsigned char	 check;

	GMainContext	*g_main_ctx;
	GMainLoop		*g_main_loop;
	DBusConnection	*dbus_con;
	guint		 dbus_watcher;
	gboolean		 thread_done;
	gboolean		 thread_inited;

	GSList		*callbacks;
	GMutex		*callbacks_lock;
	gint			 callback_id_last;

	libnm_glib_status	 nm_status;
};

typedef struct libnm_glib_callback
{
	gint					 id;
	GMainContext			*gmain_ctx;
	libnm_glib_ctx			*libnm_glib_ctx;
	libnm_glib_callback_func	 func;
	gpointer				 user_data;
} libnm_glib_callback;


static void libnm_glib_schedule_dbus_watcher (libnm_glib_ctx *ctx);
static DBusConnection * libnm_glib_dbus_init (gpointer *user_data, GMainContext *context);



static char *libnm_glib_get_nm_status (DBusConnection *con)
{
	DBusMessage	*message;
	DBusMessage	*reply;
	DBusError		 error;
	char			*status = NULL;
	char			*ret = NULL;

	g_return_val_if_fail (con != NULL, NULL);

	if (!(message = dbus_message_new_method_call (NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, "status")))
	{
		fprintf (stderr, "libnm_glib_get_nm_status(): Couldn't allocate the dbus message\n");
		return NULL;
	}

	dbus_error_init (&error);
	reply = dbus_connection_send_with_reply_and_block (con, message, -1, &error);
	dbus_message_unref (message);
	if (dbus_error_is_set (&error))
	{
		fprintf (stderr, "libnm_glib_get_nm_status(): %s raised:\n %s\n\n", error.name, error.message);
		dbus_error_free (&error);
		return NULL;
	}

	if (!reply)
	{
		fprintf (stderr, "libnm_glib_get_nm_status(): dbus reply message was NULL\n" );
		return NULL;
	}
	
	dbus_error_init (&error);
	if (!(dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &status, DBUS_TYPE_INVALID)))
	{
		fprintf (stderr, "libnm_glib_get_nm_status(): error while getting args: name='%s' message='%s'\n", error.name, error.message);
		if (dbus_error_is_set (&error))
			dbus_error_free (&error);
		dbus_message_unref (reply);
		return g_strdup ("error");
	}
	ret = g_strdup (status);
	dbus_message_unref (reply);

	return ret;
}


static gboolean libnm_glib_callback_helper (gpointer user_data)
{
	libnm_glib_callback	*cb_data = (libnm_glib_callback *)user_data;

	g_return_val_if_fail (cb_data != NULL, FALSE);
	g_return_val_if_fail (cb_data->func != NULL, FALSE);
	g_return_val_if_fail (cb_data->libnm_glib_ctx != NULL, FALSE);

	(*(cb_data->func)) (cb_data->libnm_glib_ctx, cb_data->user_data);

	return FALSE; /* never reschedule ourselves */
}

static void libnm_glib_schedule_single_callback (libnm_glib_ctx *ctx, libnm_glib_callback *callback)
{
	GSource				*source;

	g_return_if_fail (ctx != NULL);
	g_return_if_fail (callback != NULL);

	callback->libnm_glib_ctx = ctx;

	source = g_idle_source_new ();
	g_source_set_callback (source, libnm_glib_callback_helper, callback, NULL);
	g_source_attach (source, callback->gmain_ctx);
	g_source_unref (source);
}


static void libnm_glib_call_callbacks (libnm_glib_ctx *ctx)
{
	GSList	*elem;

	g_return_if_fail (ctx != NULL);

	g_mutex_lock (ctx->callbacks_lock);
	for (elem = ctx->callbacks; elem; elem = g_slist_next (elem))
	{
		libnm_glib_callback *callback = (libnm_glib_callback *)(elem->data);
		if (callback)
			libnm_glib_schedule_single_callback (ctx, callback);
	}
	g_mutex_unlock (ctx->callbacks_lock);
}


static void libnm_glib_update_status (libnm_glib_ctx *ctx, char *status)
{
	libnm_glib_status	old_status;

	g_return_if_fail (ctx != NULL);

	old_status = ctx->nm_status;
	if (!status)
		ctx->nm_status = LIBNM_NO_NETWORKMANAGER;
	else if (strcmp (status, "connected") == 0)
		ctx->nm_status = LIBNM_ACTIVE_NETWORK_CONNECTION;
	else
		ctx->nm_status = LIBNM_NO_NETWORK_CONNECTION;

	if (old_status != ctx->nm_status)
		libnm_glib_call_callbacks (ctx);
}


static DBusHandlerResult libnm_glib_dbus_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
{
	libnm_glib_ctx		*ctx = (libnm_glib_ctx *)user_data;
	gboolean		 handled = TRUE;
	DBusError	 	 error;

	g_return_val_if_fail (ctx != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
	g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
	g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);

	dbus_error_init (&error);
	if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected"))
	{
		/* Try to reactivate our connection to dbus on the next pass through the event loop */
		ctx->nm_status = LIBNM_NO_DBUS;
		dbus_connection_disconnect (ctx->dbus_con);
		libnm_glib_schedule_dbus_watcher (ctx);
	}
#if (DBUS_VERSION_MAJOR == 0 && DBUS_VERSION_MINOR == 22)
	else if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS, "ServiceCreated"))
	{
		char 	*service;

		if (    dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &service, DBUS_TYPE_INVALID)
			&& (strcmp (service, NM_DBUS_SERVICE) == 0))
		{
			char *status_string = libnm_glib_get_nm_status (ctx->dbus_con);
			libnm_glib_update_status (ctx, status_string);
			g_free (status_string);
		}
	}
	else if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS, "ServiceDeleted"))
	{
		char 	*service;

		if (    dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &service, DBUS_TYPE_INVALID)
			&& (strcmp (service, NM_DBUS_SERVICE) == 0))
		{
			ctx->nm_status = LIBNM_NO_NETWORKMANAGER;
		}
	}
#elif (DBUS_VERSION_MAJOR == 0 && DBUS_VERSION_MINOR == 23)
	else if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS, "ServiceOwnerChanged"))
	{
		/* New signal for dbus 0.23... */
		char 	*service;
		char		*old_owner;
		char		*new_owner;

		if (    dbus_message_get_args (message, &error,
									DBUS_TYPE_STRING, &service,
									DBUS_TYPE_STRING, &old_owner,
									DBUS_TYPE_STRING, &new_owner,
									DBUS_TYPE_INVALID))
		{
			if (strcmp (service, NM_DBUS_SERVICE) == 0)
			{
				gboolean old_owner_good = (old_owner && (strlen (old_owner) > 0));
				gboolean new_owner_good = (new_owner && (strlen (new_owner) > 0));

				if (!old_owner_good && new_owner_good)	/* Equivalent to old ServiceCreated signal */
				{
					char *status_string = libnm_glib_get_nm_status (ctx->dbus_con);
					libnm_glib_update_status (ctx, status_string);
					g_free (status_string);
				}
				else if (old_owner_good && !new_owner_good)	/* Equivalent to old ServiceDeleted signal */
					ctx->nm_status = LIBNM_NO_NETWORKMANAGER;
			}
		}
	}
#elif ((DBUS_VERSION_MAJOR == 0) && ((DBUS_VERSION_MINOR == 30) || (DBUS_VERSION_MINOR == 31) || (DBUS_VERSION_MINOR == 32) || (DBUS_VERSION_MINOR == 33) || (DBUS_VERSION_MINOR == 34)))
	else if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
	{
		/* New signal for dbus 0.23... */
		char 	*service;
		char		*old_owner;
		char		*new_owner;

		if (    dbus_message_get_args (message, &error,
									DBUS_TYPE_STRING, &service,
									DBUS_TYPE_STRING, &old_owner,
									DBUS_TYPE_STRING, &new_owner,
									DBUS_TYPE_INVALID))
		{
			if (strcmp (service, NM_DBUS_SERVICE) == 0)
			{
				gboolean old_owner_good = (old_owner && (strlen (old_owner) > 0));
				gboolean new_owner_good = (new_owner && (strlen (new_owner) > 0));

				if (!old_owner_good && new_owner_good)	/* Equivalent to old ServiceCreated signal */
				{
					char *status_string = libnm_glib_get_nm_status (ctx->dbus_con);
					libnm_glib_update_status (ctx, status_string);
					g_free (status_string);
				}
				else if (old_owner_good && !new_owner_good)	/* Equivalent to old ServiceDeleted signal */
					ctx->nm_status = LIBNM_NO_NETWORKMANAGER;
			}
		}
	}

#else
#error "Unrecognized version of DBUS."
#endif
	else if (    dbus_message_is_signal (message, NM_DBUS_INTERFACE, "DeviceNowActive")
			|| dbus_message_is_signal (message, NM_DBUS_INTERFACE, "DeviceNoLongerActive")
			|| dbus_message_is_signal (message, NM_DBUS_INTERFACE, "DeviceActivating")
			|| dbus_message_is_signal (message, NM_DBUS_INTERFACE, "DevicesChanged"))
	{
		char *status_string = libnm_glib_get_nm_status (ctx->dbus_con);
		libnm_glib_update_status (ctx, status_string);
		g_free (status_string);
	}
	else if (dbus_message_is_signal (message, NM_DBUS_INTERFACE, "NetworkStatusChange"))
	{
		char		*status_string = NULL;

		if (!(dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &status_string, DBUS_TYPE_INVALID)))
			status_string = g_strdup ("error");
		else
			status_string = g_strdup (status_string);

		libnm_glib_update_status (ctx, status_string);
		g_free (status_string);
	}
	else
		handled = FALSE;

	if (dbus_error_is_set (&error))
		dbus_error_free (&error);

	return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}


/*
 * libnm_glib_dbus_init
 *
 * Initialize a connection to dbus and set up our callbacks.
 *
 */
static DBusConnection * libnm_glib_dbus_init (gpointer *user_data, GMainContext *context)
{
	DBusConnection	*connection = NULL;
	DBusError		 error;

	dbus_error_init (&error);
	connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
	if (dbus_error_is_set (&error))
	{
		fprintf (stderr, "libnm: error, %s raised:\n %s\n\n", error.name, error.message);
		dbus_error_free (&error);
		return (NULL);
	}
	if (!connection)
		return NULL;

	if (!dbus_connection_add_filter (connection, libnm_glib_dbus_filter, user_data, NULL))
		return (NULL);

	dbus_connection_set_exit_on_disconnect (connection, FALSE);
	dbus_connection_setup_with_g_main (connection, context);

	dbus_error_init (&error);
	dbus_bus_add_match(connection,
				"type='signal',"
				"interface='" DBUS_INTERFACE_DBUS "',"
				"sender='" DBUS_SERVICE_DBUS "'",
				&error);
	if (dbus_error_is_set (&error))
		dbus_error_free (&error);

	dbus_error_init (&error);
	dbus_bus_add_match(connection,
				"type='signal',"
				"interface='" NM_DBUS_INTERFACE "',"
				"path='" NM_DBUS_PATH "',"
				"sender='" NM_DBUS_SERVICE "'",
				&error);
	if (dbus_error_is_set (&error))
		dbus_error_free (&error);

	return (connection);
}


/*
 * libnm_glib_dbus_watcher
 *
 * Repeatedly try to re-activate the connection to dbus.
 *
 */
static gboolean libnm_glib_dbus_watcher (gpointer user_data)
{
	libnm_glib_ctx	*ctx = (libnm_glib_ctx *)user_data;

	g_return_val_if_fail (ctx != NULL, FALSE);

	if (!ctx->dbus_con)
		ctx->dbus_con = libnm_glib_dbus_init ((gpointer)ctx, ctx->g_main_ctx);

	if (ctx->dbus_con)
		return (FALSE);	/* Don't reschedule ourselves if we have a connection to dbus */

	/* Reschule ourselves if we _still_ don't have a connection to dbus */
	return (TRUE);
}


/*
 * libnm_glib_schedule_dbus_watcher
 *
 * Schedule an idle handler in our main loop to repeatedly
 * attempt to re-activate the dbus connection until connected.
 *
 */
static void libnm_glib_schedule_dbus_watcher (libnm_glib_ctx *ctx)
{
	g_return_if_fail (ctx != NULL);

	if (ctx->dbus_watcher == 0)
	{
		GSource	*source = g_idle_source_new ();
		g_source_set_callback (source, libnm_glib_dbus_watcher, (gpointer)ctx, NULL);
		ctx->dbus_watcher = g_source_attach (source, ctx->g_main_ctx);
		g_source_unref (source);
	}
}


/*
 * libnm_glib_dbus_worker
 *
 * Main thread for libnm
 *
 */
static gpointer libnm_glib_dbus_worker (gpointer user_data)
{
	libnm_glib_ctx	*ctx = (libnm_glib_ctx *)user_data;

	g_return_val_if_fail (ctx != NULL, NULL);

	/* If dbus isn't up yet, schedule an idle handler to check for dbus.
	 * We also need a way to reconnect to dbus if the connection ever goes
	 * down.  Should probably be done by a timeout polling dbus_connection_is_connected()
	 * or by getting connection status out of libdbus or something.
	 */
	if (!ctx->dbus_con)
		libnm_glib_schedule_dbus_watcher (ctx);
	{
		char		*status_string = NULL;

		/* Get initial status */
		status_string = libnm_glib_get_nm_status (ctx->dbus_con);
		libnm_glib_update_status (ctx, status_string);
		g_free (status_string);
	}

	ctx->thread_inited = TRUE;
	g_main_loop_run (ctx->g_main_loop);
	ctx->thread_done = TRUE;

	return NULL;
}


static void libnm_glib_ctx_free (libnm_glib_ctx *ctx)
{
	g_return_if_fail (ctx != NULL);

	if (ctx->check == 0xDD)
	{
		fprintf (stderr, "libnm_glib_ctx_free(): context %p already freed!\n", ctx);
		return;
	}

	if (ctx->g_main_ctx)
		g_main_context_unref (ctx->g_main_ctx);
	if (ctx->g_main_loop)
		g_main_loop_unref (ctx->g_main_loop);

	if (ctx->dbus_con)
		dbus_connection_disconnect (ctx->dbus_con);

	if (ctx->callbacks_lock)
		g_mutex_free (ctx->callbacks_lock);

	g_slist_foreach (ctx->callbacks, (GFunc)g_free, NULL);
	g_slist_free (ctx->callbacks);

	memset (ctx, 0, sizeof (libnm_glib_ctx));
	memset (&(ctx->check), 0xDD, sizeof (ctx->check));
	g_free (ctx);
}


static libnm_glib_ctx *libnm_glib_ctx_new (void)
{
	libnm_glib_ctx *ctx = g_malloc0 (sizeof (libnm_glib_ctx));

	if (!ctx)
		return NULL;

	if (!(ctx->g_main_ctx = g_main_context_new ()))
		goto error;
	if (!(ctx->g_main_loop = g_main_loop_new (ctx->g_main_ctx, FALSE)))
		goto error;
	if (!(ctx->callbacks_lock = g_mutex_new ()))
		goto error;

success:
	return ctx;

error:
	libnm_glib_ctx_free (ctx);
	return NULL;
}


libnm_glib_ctx *libnm_glib_init (void)
{
	GError	*error = NULL;
	libnm_glib_ctx	*ctx = NULL;

	g_type_init ();
	if (!g_thread_supported ())
		g_thread_init (NULL);
	dbus_g_thread_init ();

	ctx = libnm_glib_ctx_new();
	if (!ctx)
		return NULL;

	/* We don't care if dbus isn't around yet, we keep checking for it and
	 * intialize our connection when it comes up.
	 */
	ctx->dbus_con = libnm_glib_dbus_init ((gpointer)ctx, ctx->g_main_ctx);
	if (ctx->dbus_con)
	{
		char *status_string = libnm_glib_get_nm_status (ctx->dbus_con);
		libnm_glib_update_status (ctx, status_string);
		g_free (status_string);
	}

	if (!g_thread_create (libnm_glib_dbus_worker, ctx, FALSE, &error))
	{
		if (error)
			g_error_free (error);
		goto error;	
	}

	/* Wait until initialization of the thread */
	while (!ctx->thread_inited)
		g_usleep (G_USEC_PER_SEC / 2);

success:
	return ctx;

error:
	libnm_glib_ctx_free (ctx);
	return NULL;
}


void libnm_glib_shutdown (libnm_glib_ctx *ctx)
{
	g_return_if_fail (ctx != NULL);

	g_main_loop_quit (ctx->g_main_loop);
	while (!(ctx->thread_done))
		g_usleep (G_USEC_PER_SEC / 2);

	libnm_glib_ctx_free (ctx);
}


libnm_glib_status libnm_glib_get_network_status (const libnm_glib_ctx *ctx)
{
	if (!ctx)
		return LIBNM_INVALID_CONTEXT;

	return ctx->nm_status;
}


gint libnm_glib_register_callback	(libnm_glib_ctx *ctx, libnm_glib_callback_func func, gpointer user_data, GMainContext *g_main_ctx)
{
	libnm_glib_callback		*callback = NULL;

	g_return_val_if_fail (ctx != NULL, -1);
	g_return_val_if_fail (func != NULL, -1);
	
	callback = g_malloc0 (sizeof (libnm_glib_callback));
	if (!callback)
		return -1;

	callback->id = ctx->callback_id_last++;
	callback->func = func;
	callback->gmain_ctx = g_main_ctx;
	callback->libnm_glib_ctx = ctx;
	callback->user_data = user_data;

	g_mutex_lock (ctx->callbacks_lock);
	ctx->callbacks = g_slist_append (ctx->callbacks, callback);
	libnm_glib_schedule_single_callback (ctx, callback);
	g_mutex_unlock (ctx->callbacks_lock);

	return (callback->id);
}


void libnm_glib_unregister_callback (libnm_glib_ctx *ctx, gint id)
{
	GSList *elem;

	g_return_if_fail (ctx != NULL);
	g_return_if_fail (id < 0);

	g_mutex_lock (ctx->callbacks_lock);
	elem = ctx->callbacks;
	while (elem)
	{
		libnm_glib_callback *callback = (libnm_glib_callback *)(elem->data);
		if (callback && (callback->id == id))
		{
			ctx->callbacks = g_slist_remove_link (ctx->callbacks, elem);
			break;
		}

		elem = g_slist_next (elem);
	}
	g_mutex_unlock (ctx->callbacks_lock);
}