Commit 9e5ddb58 authored by Dan Winship's avatar Dan Winship

examples: port dbus-glib-based examples to gdbus

Port the dbus-glib-based examples to GDBus.

Also, don't use libnm in them at all; there's not much point in
examples that use the D-Bus API directly if they're just going to fall
back to libnm for the hard stuff... (And also, this avoids the problem
that GDBus uses GVariant, while the libnm-core APIs currently still
use GHashTables.)

Also fix up some comment grammar and copyright style, and add emacs
modelines where missing.

Also rename the existing GDBus-based examples to have names ending in
"-gdbus", not "-GDBus", since there's no reason to gratuitously
capitalize here.
parent 89228569
......@@ -116,15 +116,14 @@ valgrind-*.log
/docs/libnm/libnm.types
/docs/libnm/version.xml
/examples/C/glib/add-connection-dbus-glib
/examples/C/glib/add-connection-gdbus
/examples/C/glib/add-connection-libnm
/examples/C/glib/get-active-connections-dbus-glib
/examples/C/glib/get-active-connections-gdbus
/examples/C/glib/get-ap-info-libnm
/examples/C/glib/list-connections-dbus-glib
/examples/C/glib/list-connections-gdbus
/examples/C/glib/list-connections-libnm
/examples/C/glib/monitor-nm-running-GDBus
/examples/C/glib/monitor-nm-running-dbus-glib
/examples/C/glib/monitor-nm-state-GDBus
/examples/C/glib/monitor-nm-running-gdbus
/examples/C/glib/monitor-nm-state-gdbus
/examples/C/qt/add-connection-wired
/examples/C/qt/list-connections
/examples/C/qt/change-ipv4-addresses
......
......@@ -8,21 +8,19 @@ AM_CPPFLAGS = \
$(GLIB_CFLAGS)
noinst_PROGRAMS = \
add-connection-dbus-glib \
add-connection-gdbus \
add-connection-libnm \
get-active-connections-dbus-glib \
list-connections-dbus-glib \
list-connections-libnm \
get-active-connections-gdbus \
get-ap-info-libnm \
monitor-nm-running-dbus-glib \
monitor-nm-running-GDBus \
monitor-nm-state-GDBus
list-connections-gdbus \
list-connections-libnm \
monitor-nm-running-gdbus \
monitor-nm-state-gdbus
add_connection_dbus_glib_SOURCES = add-connection-dbus-glib.c
add_connection_dbus_glib_LDADD = \
$(top_builddir)/libnm/libnm.la \
$(DBUS_LIBS) \
$(GLIB_LIBS)
add_connection_gdbus_SOURCES = add-connection-gdbus.c
add_connection_gdbus_LDADD = \
$(GLIB_LIBS) \
$(UUID_LIBS)
add_connection_libnm_SOURCES = add-connection-libnm.c
add_connection_libnm_LDADD = \
......@@ -30,51 +28,30 @@ add_connection_libnm_LDADD = \
$(DBUS_LIBS) \
$(GLIB_LIBS)
get_active_connections_dbus_glib_SOURCES = get-active-connections-dbus-glib.c
get_active_connections_dbus_glib_LDADD = \
$(top_builddir)/libnm/libnm.la \
$(DBUS_LIBS) \
get_active_connections_gdbus_SOURCES = get-active-connections-gdbus.c
get_active_connections_gdbus_LDADD = \
$(GLIB_LIBS)
list_connections_dbus_glib_SOURCES = list-connections-dbus-glib.c
list_connections_dbus_glib_LDADD = \
get_ap_info_libnm_SOURCES = get-ap-info-libnm.c
get_ap_info_libnm_LDADD = \
$(top_builddir)/libnm/libnm.la \
$(DBUS_LIBS) \
$(GLIB_LIBS)
list_connections_libnm_SOURCES = list-connections-libnm.c
list_connections_libnm_LDADD = \
$(top_builddir)/libnm/libnm.la \
$(DBUS_LIBS) \
list_connections_gdbus_SOURCES = list-connections-gdbus.c
list_connections_gdbus_LDADD = \
$(GLIB_LIBS)
get_ap_info_libnm_SOURCES = get-ap-info-libnm.c
get_ap_info_libnm_LDADD = \
list_connections_libnm_SOURCES = list-connections-libnm.c
list_connections_libnm_LDADD = \
$(top_builddir)/libnm/libnm.la \
$(DBUS_LIBS) \
$(GLIB_LIBS)
monitor_nm_running_dbus_glib_SOURCES = monitor-nm-running-dbus-glib.c
monitor_nm_running_dbus_glib_LDADD = \
$(DBUS_LIBS) \
$(GLIB_LIBS)
monitor_nm_running_GDBus_SOURCES = monitor-nm-running-GDBus.c
monitor_nm_running_GDBus_LDADD = \
monitor_nm_running_gdbus_SOURCES = monitor-nm-running-gdbus.c
monitor_nm_running_gdbus_LDADD = \
$(GLIB_LIBS)
monitor_nm_state_GDBus_SOURCES = monitor-nm-state-GDBus.c
monitor_nm_state_GDBus_LDADD = \
monitor_nm_state_gdbus_SOURCES = monitor-nm-state-gdbus.c
monitor_nm_state_gdbus_LDADD = \
$(GLIB_LIBS)
EXTRA_DIST = \
add-connection-dbus-glib.c \
add-connection-libnm.c \
get-active-connections-dbus-glib.c \
list-connections-dbus-glib.c \
list-connections-libnm.c \
get-ap-info-libnm.c \
monitor-nm-running-dbus-glib.c \
monitor-nm-running-GDBus.c \
monitor-nm-state-GDBus.c
/* -*- 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 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright 2011 Red Hat, Inc.
*/
/*
* The example shows how to call AddConnection() D-Bus method to add
* a connection to system settings service. It uses dbus-glib and libnm
* libraries.
*
* Compile with:
* gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm` add-connection-glib.c -o add-connection-glib
*/
#include <glib.h>
#include <dbus/dbus-glib.h>
#include <NetworkManager.h>
#define DBUS_TYPE_G_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))
#define DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, DBUS_TYPE_G_MAP_OF_VARIANT))
static void
add_connection (DBusGProxy *proxy, const char *con_name)
{
NMConnection *connection;
NMSettingConnection *s_con;
NMSettingWired *s_wired;
NMSettingIP4Config *s_ip4;
char *uuid, *new_con_path = NULL;
GHashTable *hash;
GError *error = NULL;
/* Create a new connection object */
connection = (NMConnection *) nm_simple_connection_new ();
/* Build up the 'connection' Setting */
s_con = (NMSettingConnection *) nm_setting_connection_new ();
uuid = nm_utils_uuid_generate ();
g_object_set (G_OBJECT (s_con),
NM_SETTING_CONNECTION_UUID, uuid,
NM_SETTING_CONNECTION_ID, con_name,
NM_SETTING_CONNECTION_TYPE, "802-3-ethernet",
NULL);
g_free (uuid);
nm_connection_add_setting (connection, NM_SETTING (s_con));
/* Build up the 'wired' Setting */
s_wired = (NMSettingWired *) nm_setting_wired_new ();
nm_connection_add_setting (connection, NM_SETTING (s_wired));
/* Build up the 'ipv4' Setting */
s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
g_object_set (G_OBJECT (s_ip4),
NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
NULL);
nm_connection_add_setting (connection, NM_SETTING (s_ip4));
hash = nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL);
/* Call AddConnection with the hash as argument */
if (!dbus_g_proxy_call (proxy, "AddConnection", &error,
DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, hash,
G_TYPE_INVALID,
DBUS_TYPE_G_OBJECT_PATH, &new_con_path,
G_TYPE_INVALID)) {
g_print ("Error adding connection: %s %s",
dbus_g_error_get_name (error),
error->message);
g_clear_error (&error);
} else {
g_print ("Added: %s\n", new_con_path);
g_free (new_con_path);
}
g_hash_table_destroy (hash);
g_object_unref (connection);
}
int main (int argc, char *argv[])
{
DBusGConnection *bus;
DBusGProxy *proxy;
#if !GLIB_CHECK_VERSION (2, 35, 0)
/* Initialize GType system */
g_type_init ();
#endif
/* Get system bus */
bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
/* Create a D-Bus proxy; NM_DBUS_* defined in nm-dbus-interface.h */
proxy = dbus_g_proxy_new_for_name (bus,
NM_DBUS_SERVICE,
NM_DBUS_PATH_SETTINGS,
NM_DBUS_INTERFACE_SETTINGS);
/* Add a connection */
add_connection (proxy, "__Test connection__");
g_object_unref (proxy);
dbus_g_connection_unref (bus);
return 0;
}
/* -*- 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 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 2011, 2014 Red Hat, Inc.
*/
/*
* The example shows how to call the AddConnection() D-Bus method to add a
* connection to the system settings service. It uses GDBus, plus a few defines
* from the NetworkManager headers. Contrast add-connection-libnm, which is
* higher level because it uses libnm.
*
* Compile with:
* gcc -Wall `pkg-config --cflags libnm` `pkg-config --cflags --libs gio-2.0` -luuid add-connection-gdbus.c -o add-connection-gdbus
*/
#include <gio/gio.h>
#include <uuid/uuid.h>
#include <NetworkManager.h>
/* copied from libnm-core/nm-utils.c */
char *
nm_utils_uuid_generate (void)
{
uuid_t uuid;
char *buf;
buf = g_malloc0 (37);
uuid_generate_random (uuid);
uuid_unparse_lower (uuid, &buf[0]);
return buf;
}
static void
add_connection (GDBusProxy *proxy, const char *con_name)
{
GVariantBuilder connection_builder;
GVariantBuilder setting_builder;
char *uuid;
const char *new_con_path;
GVariant *ret;
GError *error = NULL;
/* Initialize connection GVariantBuilder */
g_variant_builder_init (&connection_builder, G_VARIANT_TYPE ("a{sa{sv}}"));
/* Build up the 'connection' Setting */
g_variant_builder_init (&setting_builder, G_VARIANT_TYPE ("a{sv}"));
uuid = nm_utils_uuid_generate ();
g_variant_builder_add (&setting_builder, "{sv}",
NM_SETTING_CONNECTION_UUID,
g_variant_new_string (uuid));
g_free (uuid);
g_variant_builder_add (&setting_builder, "{sv}",
NM_SETTING_CONNECTION_ID,
g_variant_new_string (con_name));
g_variant_builder_add (&setting_builder, "{sv}",
NM_SETTING_CONNECTION_TYPE,
g_variant_new_string (NM_SETTING_WIRED_SETTING_NAME));
g_variant_builder_add (&connection_builder, "{sa{sv}}",
NM_SETTING_CONNECTION_SETTING_NAME,
&setting_builder);
/* Add the (empty) 'wired' Setting */
g_variant_builder_init (&setting_builder, G_VARIANT_TYPE ("a{sv}"));
g_variant_builder_add (&connection_builder, "{sa{sv}}",
NM_SETTING_WIRED_SETTING_NAME,
&setting_builder);
/* Build up the 'ipv4' Setting */
g_variant_builder_init (&setting_builder, G_VARIANT_TYPE ("a{sv}"));
g_variant_builder_add (&setting_builder, "{sv}",
NM_SETTING_IP4_CONFIG_METHOD,
g_variant_new_string (NM_SETTING_IP4_CONFIG_METHOD_AUTO));
g_variant_builder_add (&connection_builder, "{sa{sv}}",
NM_SETTING_IP4_CONFIG_SETTING_NAME,
&setting_builder);
/* Call AddConnection with the connection dictionary as argument.
* (g_variant_new() will consume the floating GVariant returned from
* &connection_builder, and g_dbus_proxy_call_sync() will consume the
* floating variant returned from g_variant_new(), so no cleanup is needed.
*/
ret = g_dbus_proxy_call_sync (proxy,
"AddConnection",
g_variant_new ("(a{sa{sv}})", &connection_builder),
G_DBUS_CALL_FLAGS_NONE, -1,
NULL, &error);
if (ret) {
g_variant_get (ret, "(&o)", &new_con_path);
g_print ("Added: %s\n", new_con_path);
g_variant_unref (ret);
} else {
g_dbus_error_strip_remote_error (error);
g_print ("Error adding connection: %s\n", error->message);
g_clear_error (&error);
}
}
int
main (int argc, char *argv[])
{
GDBusProxy *proxy;
GError *error = NULL;
#if !GLIB_CHECK_VERSION (2, 35, 0)
/* Initialize GType system */
g_type_init ();
#endif
/* Create a D-Bus proxy; NM_DBUS_* defined in nm-dbus-interface.h */
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
NM_DBUS_SERVICE,
NM_DBUS_PATH_SETTINGS,
NM_DBUS_INTERFACE_SETTINGS,
NULL, &error);
if (!proxy) {
g_dbus_error_strip_remote_error (error);
g_print ("Could not create NetworkManager D-Bus proxy: %s\n", error->message);
g_error_free (error);
return 1;
}
/* Add a connection */
add_connection (proxy, "__Test connection__");
g_object_unref (proxy);
return 0;
}
......@@ -14,14 +14,14 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright 2011 Red Hat, Inc.
* Copyright 2011 Red Hat, Inc.
*/
/*
* The example shows how to add a new connection using libnm.
* Contrast this example with add-connection-dbus-glib.c, which is a bit lower
* level and talks directly to NM using dbus-glib. This example is simpler
* because libnm handles much of the low-level stuff for you.
* The example shows how to add a new connection using libnm. Contrast this
* example with add-connection-gdbus.c, which is a bit lower level and talks
* directly to NM using GDBus. This example is simpler because libnm handles
* much of the low-level stuff for you.
*
* Compile with:
* gcc -Wall `pkg-config --libs --cflags glib-2.0 libnm` add-connection-libnm.c -o add-connection-libnm
......@@ -98,7 +98,8 @@ add_connection (NMRemoteSettings *settings, GMainLoop *loop, const char *con_nam
}
int main (int argc, char *argv[])
int
main (int argc, char *argv[])
{
NMRemoteSettings *settings;
GMainLoop *loop;
......
/* -*- 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
......@@ -13,19 +14,18 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright 2010 Red Hat, Inc.
* Copyright 2010 Red Hat, Inc.
*/
/*
* The example shows how to get info about APs visible by Wi-Fi devices
* using libnm (that wraps direct D-Bus calls).
* The example shows how to get info about APs visible by Wi-Fi devices using
* libnm.
*
* Compile with:
* gcc -Wall `pkg-config --libs --cflags glib-2.0 libnm` get-ap-info-libnm.c -o get-ap-info-libnm
*/
#include <glib.h>
#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <stdlib.h>
......@@ -188,7 +188,8 @@ show_wifi_device_info (NMDevice *device)
}
}
int main (int argc, char *argv[])
int
main (int argc, char *argv[])
{
NMClient *client;
const GPtrArray *devices;
......
/* -*- 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
......@@ -13,78 +14,75 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright 2011 Red Hat, Inc.
* Copyright 2011, 2014 Red Hat, Inc.
*/
/*
* The example shows how to list connections from System Settings service using direct
* D-Bus call of ListConnections method.
* The example uses dbus-glib libraries and libnm headers.
* The example shows how to list connections from the System Settings service
* using direct D-Bus calls. The example uses GDBus, plus a few defines from
* the NetworkManager headers. Contrast list-connections-libnm, which is higher
* level because it uses libnm.
*
* Compile with:
* gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1` `pkg-config --cflags libnm` list-connections-dbus.c -o list-connections-dbus
* gcc -Wall `pkg-config --cflags libnm` `pkg-config --cflags --libs gio-2.0` list-connections-gdbus.c -o list-connections-gdbus
*/
#include <glib.h>
#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <gio/gio.h>
#include <NetworkManager.h>
#define DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH))
static void
list_connections (DBusGProxy *proxy)
list_connections (GDBusProxy *proxy)
{
int i;
GError *error = NULL;
GPtrArray *con_array;
gboolean success;
GVariant *ret;
char **paths;
/* Call ListConnections D-Bus method */
success = dbus_g_proxy_call (proxy, "ListConnections", &error,
/* No input arguments */
G_TYPE_INVALID,
DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH, &con_array, /* Return values */
G_TYPE_INVALID);
if (!success) {
printf ("ListConnections failed: %s", error->message);
ret = g_dbus_proxy_call_sync (proxy,
"ListConnections",
NULL,
G_DBUS_CALL_FLAGS_NONE, -1,
NULL, &error);
if (!ret) {
g_dbus_error_strip_remote_error (error);
g_print ("ListConnections failed: %s\n", error->message);
g_error_free (error);
return;
}
for (i = 0; con_array && i < con_array->len; i++) {
char *connection_path = g_ptr_array_index (con_array, i);
printf ("%s\n", connection_path);
g_free (connection_path);
}
g_ptr_array_free (con_array, TRUE);
g_variant_get (ret, "(^ao)", &paths);
g_variant_unref (ret);
for (i = 0; paths[i]; i++)
g_print ("%s\n", paths[i]);
g_strfreev (paths);
}
int main (int argc, char *argv[])
int
main (int argc, char *argv[])
{
DBusGConnection *bus;
DBusGProxy *proxy;
GDBusProxy *proxy;
#if !GLIB_CHECK_VERSION (2, 35, 0)
/* Initialize GType system */
g_type_init ();
#endif
/* Get system bus */
bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
/* Create a D-Bus proxy; NM_DBUS_* defined in nm-dbus-interface.h */
proxy = dbus_g_proxy_new_for_name (bus,
NM_DBUS_SERVICE,
NM_DBUS_PATH_SETTINGS,
NM_DBUS_INTERFACE_SETTINGS);
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
NM_DBUS_SERVICE,
NM_DBUS_PATH_SETTINGS,
NM_DBUS_INTERFACE_SETTINGS,
NULL, NULL);
g_assert (proxy != NULL);
/* List connections of system settings service */
list_connections (proxy);
g_object_unref (proxy);
dbus_g_connection_unref (bus);
return 0;
}
/* -*- 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
......@@ -13,12 +14,13 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright 2011 Red Hat, Inc.
* Copyright 2011 Red Hat, Inc.
*/
/*
* The example shows how to list connections from System Settings service using libnm
* (that wraps direct D-Bus calls).
* The example shows how to list connections from the System Settings service
* using libnm. Contrast this example with list-connections-gdbus.c, which is a
* bit lower level and talks directly to NM using GDBus.
*
* Compile with:
* gcc -Wall `pkg-config --libs --cflags glib-2.0 libnm` list-connections-libnm.c -o list-connections-libnm
......
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* vim: set ft=c ts=4 sts=4 sw=4 noexpandtab smartindent: */
/*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright 2012 Red Hat, Inc.
*/
/*
* This example monitors whether NM is running by checking D-Bus
* NameOwnerChanged signal.
* It uses dbus-glib library.
*
* Standalone compilation:
* gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1` monitor-nm-running.c -o monitor-nm-running
*/
#include <glib.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-bindings.h>
#include <string.h>
#define NM_DBUS_SERVICE "org.freedesktop.NetworkManager"
static void
proxy_name_owner_changed (DBusGProxy *proxy,
const char *name,
const char *old_owner,
const char *new_owner,
gpointer user_data)
{
gboolean *nm_running = (gboolean *) user_data;
gboolean old_good = (old_owner && strlen (old_owner));
gboolean new_good = (new_owner && strlen (new_owner));
gboolean new_running = FALSE;
/* We are only interested in NetworkManager */
if (!name || strcmp (name, NM_DBUS_SERVICE) != 0)
return;
if (!old_good && new_good)
new_running = TRUE;
else if (old_good && !new_good)
new_running = FALSE;
*nm_running = new_running;
g_print ("name: '%s', old_owner: '%s', new_owner: '%s'", name, old_owner, new_owner);
g_print (" => NM is %s\n", *nm_running ? "running" : "not running");
}
int
main (int argc, char *argv[])
{
DBusGConnection *bus;
DBusGProxy *bus_proxy;
GMainLoop *loop = NULL;
GError *err = NULL;
gboolean nm_running;
#if !GLIB_CHECK_VERSION (2, 35, 0)
/* Initialize GType system */
g_type_init ();
#endif
g_print ("Monitor 'org.freedesktop.NetworkManager' D-Bus name\n");
g_print ("===================================================\n");
/* Get system bus */
bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
/* Create a D-Bus proxy to D-Bus daemon */
bus_proxy = dbus_g_proxy_new_for_name (bus,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus");
if (!bus_proxy) {
g_message ("Error: Couldn't create D-Bus object proxy for org.freedesktop.DBus.");
dbus_g_connection_unref (bus);
return -1;
}
/* Call NameHasOwner method to find out if NM is running. When NM runs it claims
* 'org.freedesktop.NetworkManager' service name on D-Bus */
if (!org_freedesktop_DBus_name_has_owner (bus_proxy, NM_DBUS_SERVICE, &nm_running, &err)) {
g_message ("Error: NameHasOwner request failed: %s",
(err && err->message) ? err->message : "(unknown)");
g_clear_error (&err);
g_object_unref (bus_proxy);
dbus_g_connection_unref (bus);
return -1;
}
g_print ("NM is %s\n", nm_running ? "running" : "not running");
/* Connect to NameOwnerChanged signal to monitor NM running state */
dbus_g_proxy_add_signal (bus_proxy, "NameOwnerChanged",
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_INVALID);
dbus_g_proxy_connect_signal (bus_proxy,
"NameOwnerChanged",
G_CALLBACK (proxy_name_owner_changed),
&nm_running, NULL);
loop = g_main_loop_new (NULL, FALSE); /* Create main loop */
g_main_loop_run (loop); /* Run main loop */
g_object_unref (bus_proxy);
dbus_g_connection_unref (bus);
return 0;
}
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* vim: set ft=c ts=4 sts=4 sw=4 noexpandtab smartindent: */
/*
* 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
......@@ -15,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright 2012 Red Hat, Inc.